Mercurial > hg > toybox
annotate lib/args.c @ 29:5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
author | Rob Landley <rob@landley.net> |
---|---|
date | Fri, 24 Nov 2006 00:15:21 -0500 |
parents | be59ed005902 |
children | 993eab821bd5 |
rev | line source |
---|---|
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* vi: set sw=4 ts=4 : |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 * args.c - Command line argument parsing. |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 * Copyright 2006 Rob Landley <rob@landley.net> |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 */ |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
7 #include "toys.h" |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 // Design goals: |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 // Don't use getopt() |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 // Don't permute original arguments. |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 // handle --long gracefully "(noshort)a(along)b(blong1)(blong2)" |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
13 // After each argument: |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 // Note that pointer and long are always the same size, even on 64 bit. |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
15 // : plus a string argument, keep most recent if more than one |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
16 // * plus a string argument, appended to a list |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
17 // ? plus a signed long argument (TODO: Bounds checking?) |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
18 // @ plus an occurrence counter (which is a long) |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
19 // | this is required. If more than one marked, only one required. |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
20 // (longopt) |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 // +X enabling this enables X (switch on) |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
22 // ~X enabling this disables X (switch off) |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
23 // x~x means toggle x, I.E. specifying it again switches it off. |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
24 // !X die with error if X already set (x!x die if x supplied twice) |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
25 // [yz] needs at least one of y or z. |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 // at the beginning: |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 // + stop at first nonoption argument |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 // <0 at least # leftover arguments needed (default 0) |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
29 // >9 at most # leftover arguments needed (default MAX_INT) |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
30 // # don't show_usage() on unknown argument. |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
31 // & first argument has imaginary dash (ala tar/ps) |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 // If given twice, all arguments have imaginary dash |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 // Notes from getopt man page |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 // - and -- cannot be arguments. |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 // -- force end of arguments |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 // - is a synonym for stdin in file arguments |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 // -abc means -a -b -c |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 /* This uses a getopt-like option string, but not getopt() itself. |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 * |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 * Each option in options corresponds to a bit position in the return |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 * value (last argument is (1<<0), the next to last is (1<<1) and so on. |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 * If the option isn't seen in argv its bit is 0. Options which have an |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 * argument use the next vararg. (So varargs used by options go from left to |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 * right, but bits set by arguments go from right to left.) |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 * |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 * Example: |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 * get_optflags("ab:c:d", NULL, &bstring, &cstring); |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 * argv = ["command", "-b", "fruit", "-d"] |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 * flags = 5, bstring="fruit", cstring=NULL; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 */ |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
53 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 struct opts { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
55 struct opts *next; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 char c; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
57 int type; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
58 int shift; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 void *arg; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 }; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 struct getoptflagstate |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 int argc; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
65 char *arg; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 struct opts *opts, *this; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 int noerror, nodash_now; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 }; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
69 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
70 static struct getoptflagstate gof; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
71 |
29
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
72 static void gotflag(void) |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
73 { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
74 int type; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
75 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
76 // Did we recognize this option? |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
77 if (!gof.this && !gof.noerror) error_exit("Unknown option %s\n", gof.arg); |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
78 else toys.optflags |= 1 << gof.this->shift; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
79 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
80 // Does this option take an argument? |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
81 gof.arg++; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
82 if (gof.this->type & 255) { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 // Make "tar xCjfv blah1 blah2 thingy" work like |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
84 // "tar -x -C blah1 -j -f blah2 -v thingy" |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 if (!gof.nodash_now && !*gof.arg) { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
86 gof.arg = toys.argv[++gof.argc]; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
87 if (!gof.arg) error_exit("Missing argument"); |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
88 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
89 } else gof.this = NULL; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
90 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
91 // If the last option had an argument, grab it. |
29
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
92 if (gof.this) { |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
93 type = gof.this->type & 255; |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
94 if (!gof.arg && !(gof.arg = toys.argv[++gof.argc])) |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
95 error_exit("Missing argument"); |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
96 if (type == ':') gof.this->arg = gof.arg; |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
97 else if (type == '*') { |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
98 struct arg_list *temp, **list; |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
99 list = (struct arg_list **)gof.this->arg; |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
100 temp = xmalloc(sizeof(struct arg_list)); |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
101 temp->arg = gof.arg; |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
102 temp->next = *list; |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
103 *list = temp; |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
104 } else if (type == '?') { |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
105 } else if (type == '@') { |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
106 } |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
107 |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
108 gof.arg = ""; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
109 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
110 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
111 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
112 // Fill out toys.optflags and toys.optargs. This isn't reentrant because |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
113 // we don't bzero(&gof, sizeof(gof)); |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
114 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
115 void get_optflags(void) |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
116 { |
28
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
117 int stopearly = 0, optarg = 0, nodash = 0, minargs = 0, maxargs; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
118 struct longopts { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
119 struct longopts *next; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
120 struct opts *opt; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
121 char *str; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
122 int len; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
123 } *longopts = NULL; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
124 long *nextarg = (long *)&toy; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
125 char *options = toys.which->options; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
126 |
28
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
127 // Allocate memory for optargs |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
128 maxargs = 0; |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
129 while (toys.argv[maxargs++]); |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
130 toys.optargs = xzalloc(sizeof(char *)*maxargs); |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
131 maxargs = INT_MAX; |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
132 |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
133 // Parse option format |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
134 if (options) { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
135 // Parse leading special behavior indicators |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
136 for (;;) { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
137 if (*options == '+') stopearly++; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
138 else if (*options == '<') minargs=*(++options)-'0'; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
139 else if (*options == '>') maxargs=*(++options)-'0'; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
140 else if (*options == '#') gof.noerror++; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
141 else if (*options == '&') nodash++; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
142 else break; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
143 options++; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
144 } |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
145 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
146 // Parse rest of opts into array |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
147 while (*options) { |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
148 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
149 // Allocate a new option entry when necessary |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
150 if (!gof.this) { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
151 gof.this = xzalloc(sizeof(struct opts)); |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
152 gof.this->next = gof.opts; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
153 gof.opts = gof.this; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
154 } |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
155 // Each option must start with (or an option character. (Bare |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
156 // longopts only come at the start of the string.) |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
157 if (*options == '(') { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
158 char *end; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
159 struct longopts *lo = xmalloc(sizeof(struct longopts)); |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
160 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
161 // Find the end of the longopt |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
162 for (end = ++options; *end && *end != ')'; end++); |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
163 if (CFG_DEBUG && !*end) error_exit("Unterminated optstring"); |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
164 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
165 // Allocate and init a new struct longopts |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
166 lo = xmalloc(sizeof(struct longopts)); |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
167 lo->next = longopts; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
168 lo->opt = gof.this; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
169 lo->str = options; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
170 lo->len = end-options; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
171 longopts = lo; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
172 options = end; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
173 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
174 // For leading longopts (with no corresponding short opt), note |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
175 // that this option struct has been used. |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
176 gof.this->shift++; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
177 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
178 // If this is the start of a new option that wasn't a longopt, |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
179 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
180 } else if (index(":*?@", *options)) { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
181 gof.this->type |= *options; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
182 // Pointer and long guaranteed to be the same size by LP64. |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
183 *(++nextarg) = 0; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
184 gof.this->arg = (void *)nextarg; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
185 } else if (*options == '|') { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
186 } else if (*options == '+') { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
187 } else if (*options == '~') { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
188 } else if (*options == '!') { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
189 } else if (*options == '[') { |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
190 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
191 // At this point, we've hit the end of the previous option. The |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
192 // current character is the start of a new option. If we've already |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
193 // assigned an option to this struct, loop to allocate a new one. |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
194 // (It'll get back here afterwards.) |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
195 } else if(gof.this->shift || gof.this->c) { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
196 gof.this = NULL; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
197 continue; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
198 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
199 // Claim this option, loop to see what's after it. |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
200 } else gof.this->c = *options; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
201 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
202 options++; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
203 } |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
204 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
205 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
206 // Initialize shift bits (have to calculate this ahead of time because |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
207 // longopts jump into the middle of the list), and allocate space to |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
208 // store optargs. |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
209 gof.argc = 0; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
210 for (gof.this = gof.opts; gof.this; gof.this = gof.this->next) |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
211 gof.this->shift = gof.argc++; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
212 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
213 // Iterate through command line arguments, skipping argv[0] |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
214 for (gof.argc=1; toys.argv[gof.argc]; gof.argc++) { |
29
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
215 gof.arg = toys.argv[gof.argc]; |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
216 gof.this = NULL; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
217 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
218 // Parse this argument |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
219 if (stopearly>1) goto notflag; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
220 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
221 gof.nodash_now = 0; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
222 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
223 // Various things with dashes |
29
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
224 if (*gof.arg == '-') { |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
225 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
226 // Handle - |
29
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
227 if (!gof.arg[1]) goto notflag; |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
228 gof.arg++; |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
229 if (*gof.arg=='-') { |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
230 struct longopts *lo; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
231 |
29
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
232 gof.arg++; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
233 // Handle -- |
29
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
234 if (!*gof.arg) { |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
235 stopearly += 2; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
236 goto notflag; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
237 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
238 // Handle --longopt |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
239 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
240 for (lo = longopts; lo; lo = lo->next) { |
29
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
241 if (!strncmp(gof.arg, lo->str, lo->len)) { |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
242 if (gof.arg[lo->len]) { |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
243 if (gof.arg[lo->len]=='=' |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
244 && (lo->opt->type & 255)) |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
245 { |
29
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
246 gof.arg += lo->len; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
247 } else continue; |
29
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
248 } |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
249 // It's a match. |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
250 gof.arg = ""; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
251 gof.this = lo->opt; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
252 break; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
253 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
254 } |
29
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
255 |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
256 // Long option parsed, handle option. |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
257 gotflag(); |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
258 continue; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
259 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
260 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
261 // Handle things that don't start with a dash. |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
262 } else { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
263 if (nodash && (nodash>1 || gof.argc == 1)) gof.nodash_now = 1; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
264 else goto notflag; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
265 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
266 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
267 // At this point, we have the args part of -args. Loop through |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
268 // each entry (could be -abc meaning -a -b -c) |
29
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
269 while (*gof.arg) { |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
270 |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
271 // Identify next option char. |
29
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
272 for (gof.this = gof.opts; gof.this; gof.this = gof.this->next) |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
273 if (*gof.arg == gof.this->c) break; |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
274 |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
275 // Handle option char (advancing past what was used) |
5566beb17d5a
Fix a half-dozen bugs in argument parsing. More seems to work than not now.
Rob Landley <rob@landley.net>
parents:
28
diff
changeset
|
276 gotflag(); |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
277 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
278 continue; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
279 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
280 // Not a flag, save value in toys.optargs[] |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
281 notflag: |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
282 if (stopearly) stopearly++; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
283 toys.optargs[optarg++] = toys.argv[gof.argc]; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
284 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
285 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
286 // Sanity check |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
287 if (optarg<minargs) error_exit("Need %d arguments", minargs); |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
288 if (optarg>maxargs) error_exit("Max %d arguments", maxargs); |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
289 } |