summaryrefslogtreecommitdiff
path: root/ruby.c
diff options
context:
space:
mode:
Diffstat (limited to 'ruby.c')
-rw-r--r--ruby.c195
1 files changed, 112 insertions, 83 deletions
diff --git a/ruby.c b/ruby.c
index 87bb14b..7b029cd 100644
--- a/ruby.c
+++ b/ruby.c
@@ -18,20 +18,8 @@
#include <sys/stat.h>
#include <signal.h>
-#include "getopt.h"
-
static int version, copyright;
-static struct option long_options[] =
-{
- {"debug", 0, 0, 'd'},
- {"yydebug", 0, 0, 'y'},
- {"verbose", 0, 0, 'v'},
- {"version", 0, &version, 1},
- {"copyright", 0, &copyright, 1},
- {0, 0, 0, 0}
-};
-
int debug = 0;
int verbose = 0;
static int sflag = FALSE;
@@ -67,12 +55,11 @@ proc_options(argcp, argvp)
{
int argc = *argcp;
char **argv = *argvp;
+ int script_given, do_search;
+ char *s;
+
extern VALUE rb_load_path;
- extern char *optarg;
- extern int optind;
- int c, i, j, script_given, do_search, opt_index;
extern VALUE RS, ORS, FS;
- char *src;
if (argc == 0) return;
@@ -80,114 +67,155 @@ proc_options(argcp, argvp)
script_given = FALSE;
do_search = FALSE;
- optind = 0;
- while ((c = getopt_long(argc, argv, "+acC:de:F:i:I:lnpR:svxX:yS",
- long_options, &opt_index)) != EOF) {
- switch (c) {
+ for (argc--,argv++; argc > 0; argc--,argv++) {
+ if (argv[0][0] != '-' || !argv[0][1]) break;
+
+ s = argv[0]+1;
+ reswitch:
+ switch (*s) {
+ case 'a':
+ do_split = TRUE;
+ s++;
+ goto reswitch;
+
case 'p':
do_print = TRUE;
/* through */
case 'n':
do_loop = TRUE;
- break;
+ s++;
+ goto reswitch;
case 'd':
debug = TRUE;
- break;
+ s++;
+ goto reswitch;
case 'y':
yydebug = 1;
- break;
+ s++;
+ goto reswitch;
case 'v':
verbose = TRUE;
show_version();
- break;
+ s++;
+ goto reswitch;
+
+ case 'c':
+ do_check = TRUE;
+ s++;
+ goto reswitch;
+
+ case 's':
+ sflag = TRUE;
+ s++;
+ goto reswitch;
+
+ case 'l':
+ do_line = TRUE;
+ ORS = RS;
+ s++;
+ goto reswitch;
+
+ case 'S':
+ do_search = TRUE;
+ s++;
+ goto reswitch;
case 'e':
script_given++;
if (script == 0) script = "-e";
- lex_setsrc("-e", optarg, strlen(optarg));
+ if (argv[1]) {
+ lex_setsrc("-e", argv[1], strlen(argv[1]));
+ argc--,argv++;
+ }
+ else {
+ lex_setsrc("-e", "", 0);
+ }
yyparse();
break;
case 'i':
- inplace = strdup(optarg);
- break;
-
- case 'c':
- do_check = TRUE;
+ inplace = strdup(s+1);
break;
case 'x':
xflag = TRUE;
+ s++;
+ if (*s && chdir(s) < 0) {
+ Fatal("Can't chdir to %s", s);
+ }
break;
- case 'X':
- if (chdir(optarg) < 0)
- Fatal("Can't chdir to %s", optarg);
+ case 'F':
+ FS = str_new2(s+1);
break;
- case 's':
- sflag = TRUE;
- break;
+ case 'K':
+ s++;
+ rb_set_kanjicode(s);
+ s++;
+ goto reswitch;
- case 'l':
- do_line = TRUE;
- ORS = RS;
+ case 'I':
+ ary_unshift(rb_load_path, str_new2(s+1));
break;
- case 'R':
+ case '0':
{
- char *p = optarg;
-
- while (*p) {
- if (*p < '0' || '7' < *p) {
- break;
- }
- p++;
- }
- if (*p) {
- RS = str_new2(optarg);
+ int numlen;
+ int v;
+ char c;
+
+ v = scan_oct(s, 4, &numlen);
+ s += numlen;
+ if (v > 0377) RS = Qnil;
+ else if (v == 0 && numlen >= 2) {
+ RS = str_new2("\n\n");
}
else {
- int i = strtoul(optarg, Qnil, 8);
-
- if (i == 0) RS = str_new(0, 0);
- else if (i > 0xff) RS = Qnil;
- else {
- char c = i;
- RS = str_new(&c, 1);
- }
+ c = v & 0xff;
+ RS = str_new(&c, 1);
}
}
- break;
+ goto reswitch;
- case 'F':
- FS = str_new2(optarg);
- break;
+ case 'u':
+ case 'U':
- case 'a':
- do_split = TRUE;
- break;
-
- case 'C':
- rb_set_kanjicode(optarg);
- break;
-
- case 'S':
- do_search = TRUE;
- break;
-
- case 'I':
- ary_unshift(rb_load_path, str_new2(optarg));
+ case '-':
+ if (!s[1]) {
+ argc--,argv++;
+ goto switch_end;
+ }
+ s++;
+ if (strcmp("copyright", s) == 0)
+ copyright = 1;
+ else if (strcmp("debug", s) == 0)
+ debug = 1;
+ else if (strcmp("version", s) == 0)
+ version = 1;
+ else if (strcmp("verbose", s) == 0)
+ verbose = 1;
+ else if (strcmp("yydebug", s) == 0)
+ yydebug = 1;
+ else {
+ Fatal("Unrecognized long option: --%s",s);
+ }
break;
default:
+ Fatal("Unrecognized switch: -%s",s);
+
+ case 0:
break;
}
}
+ switch_end:
+ if (*argvp[0] == Qnil) return;
+
if (version) {
show_version();
exit(0);
@@ -196,28 +224,28 @@ proc_options(argcp, argvp)
show_copyright();
}
- if (argv[0] == Qnil) return;
+ rb_setup_kcode();
if (script_given == 0) {
- if (argc == optind) { /* no more args */
+ if (argc == 0) { /* no more args */
if (verbose) exit(0);
script = "-";
load_stdin();
}
else {
- script = argv[optind];
+ script = argv[0];
if (do_search) {
script = dln_find_file(script, getenv("PATH"));
- if (!script) script = argv[optind];
+ if (!script) script = argv[0];
}
load_file(script, 1);
- optind++;
+ argc--,argv++;
}
}
xflag = FALSE;
- *argvp += optind;
- *argcp -= optind;
+ *argvp = argv;
+ *argcp = argc;
if (sflag) {
char *s;
@@ -239,6 +267,7 @@ proc_options(argcp, argvp)
}
*argcp = argc; *argvp = argv;
}
+
}
static void