Mercurial > hg > toybox
annotate lib/args.c @ 144:1fbc50374a30
Promote help to global config option, teach error_exit() to output usage message when called
from get_optflags().
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 11 Oct 2007 15:36:36 -0500 |
parents | 343117774a9f |
children | 1e8f4b05cb65 |
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 |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
17 // # plus a signed long argument (TODO: Bounds checking?) |
25
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) |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
30 // ? don't show_usage() on unknown argument. |
25
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 * |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
42 * Each option in options corresponds to a bit position in the return |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
43 * value (last argument is (1<<0), the next to last is (1<<1) and so on). |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
44 * If the option isn't seen in argv[] its bit is 0. |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
45 * |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
46 * Options which have an argument fill in the corresponding slot in the global |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
47 * toys.command struct, which it treats as an array of longs (note that |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
48 * sizeof(long)==sizeof(pointer) is guaranteed by LP64). |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
49 * |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
50 * You don't have to free the option strings, which point into the environment |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
51 * space. List list objects should be freed by main() when command_main() |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
52 * returns. |
25
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 * Example: |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
55 * get_optflags() when toys.which->options="ab:c:d" |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 * argv = ["command", "-b", "fruit", "-d"] |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
57 * flags = 5, toy[0]=NULL, toy[1]="fruit"; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
58 */ |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
60 // |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 struct opts { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 struct opts *next; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 char c; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 int type; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
65 int shift; |
32
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
66 long *arg; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 }; |
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 struct getoptflagstate |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
70 { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
71 int argc; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
72 char *arg; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
73 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
|
74 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
|
75 }; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
76 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
77 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
|
78 |
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
|
79 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
|
80 { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
81 int type; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
82 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 // 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
|
84 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
|
85 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
|
86 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
87 // 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
|
88 gof.arg++; |
32
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
89 type = gof.this->type & 255; |
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
90 if (type) { |
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
91 |
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
92 // Handle "-xblah" and "-x blah", but also a third case: "abxc blah" |
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
93 // to make "tar xCjfv blah1 blah2 thingy" work like |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
94 // "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
|
95 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
|
96 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
|
97 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
|
98 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
99 |
32
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
100 // Grab argument. |
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
|
101 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
|
102 error_exit("Missing argument"); |
32
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
103 if (type == ':') *(gof.this->arg) = (long)gof.arg; |
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
|
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 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
|
106 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
|
107 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
|
108 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
|
109 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
|
110 *list = temp; |
102
aa4fa2543a65
Add atolx() which understands extensions for kilobytes and megabytes and such.
Rob Landley <rob@landley.net>
parents:
90
diff
changeset
|
111 } else if (type == '#') *(gof.this->arg) = atolx((char *)gof.arg); |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
112 else if (type == '@') { |
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
|
113 } |
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
|
114 |
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
|
115 gof.arg = ""; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
116 } |
32
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
117 |
993eab821bd5
More work on option parsing. "df -t tmpfs" actually seems to work now.
Rob Landley <rob@landley.net>
parents:
29
diff
changeset
|
118 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
|
119 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
120 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
121 // 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
|
122 // 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
|
123 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
124 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
|
125 { |
28
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
126 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
|
127 struct longopts { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
128 struct longopts *next; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
129 struct opts *opt; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
130 char *str; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
131 int len; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
132 } *longopts = NULL; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
133 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
|
134 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
|
135 |
144
1fbc50374a30
Promote help to global config option, teach error_exit() to output usage message when called
Rob Landley <rob@landley.net>
parents:
127
diff
changeset
|
136 if (CFG_HELP) toys.exithelp++; |
28
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
137 // Allocate memory for optargs |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
138 maxargs = 0; |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
139 while (toys.argv[maxargs++]); |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
140 toys.optargs = xzalloc(sizeof(char *)*maxargs); |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
141 maxargs = INT_MAX; |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
142 |
be59ed005902
Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents:
26
diff
changeset
|
143 // Parse option format |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
144 if (options) { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
145 // Parse leading special behavior indicators |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
146 for (;;) { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
147 if (*options == '+') stopearly++; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
148 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
|
149 else if (*options == '>') maxargs=*(++options)-'0'; |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
150 else if (*options == '?') gof.noerror++; |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
151 else if (*options == '&') nodash++; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
152 else break; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
153 options++; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
154 } |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
155 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
156 // 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
|
157 while (*options) { |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
158 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
159 // 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
|
160 if (!gof.this) { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
161 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
|
162 gof.this->next = gof.opts; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
163 gof.opts = gof.this; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
164 } |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
165 // 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
|
166 // 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
|
167 if (*options == '(') { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
168 char *end; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
169 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
|
170 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
171 // 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
|
172 for (end = ++options; *end && *end != ')'; end++); |
90
7c77c6ec17ee
Add "make defconfig". Modify global options to start with CONFIG_TOYBOX_.
Rob Landley <rob@landley.net>
parents:
78
diff
changeset
|
173 if (CFG_TOYBOX_DEBUG && !*end) |
7c77c6ec17ee
Add "make defconfig". Modify global options to start with CONFIG_TOYBOX_.
Rob Landley <rob@landley.net>
parents:
78
diff
changeset
|
174 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
|
175 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
176 // 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
|
177 lo = xmalloc(sizeof(struct longopts)); |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
178 lo->next = longopts; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
179 lo->opt = gof.this; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
180 lo->str = options; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
181 lo->len = end-options; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
182 longopts = lo; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
183 options = end; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
184 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
185 // 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
|
186 // 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
|
187 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
|
188 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
189 // 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
|
190 |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
191 } else if (index(":*#@", *options)) { |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
192 gof.this->type |= *options; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
193 } else if (*options == '|') { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
194 } else if (*options == '+') { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
195 } else if (*options == '~') { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
196 } else if (*options == '!') { |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
197 } 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
|
198 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
199 // 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
|
200 // 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
|
201 // 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
|
202 // (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
|
203 } 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
|
204 gof.this = NULL; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
205 continue; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
206 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
207 // 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
|
208 } 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
|
209 |
26
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
210 options++; |
75b80e55bcfc
Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
211 } |
25
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 |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
214 // Initialize shift bits and pointers to store arguments. (We have to |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
215 // calculate this ahead of time because longopts jump into the middle of |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
216 // the list.) |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
217 gof.argc = 0; |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
218 for (gof.this = gof.opts; gof.this; gof.this = gof.this->next) { |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
219 gof.this->shift = gof.argc++; |
78
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
220 if (gof.this->type & 255) { |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
221 gof.this->arg = (void *)nextarg; |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
222 *(nextarg++) = 0; |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
223 } |
cd1f36a96185
Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents:
49
diff
changeset
|
224 } |
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 // 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
|
227 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
|
228 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
|
229 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
|
230 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
231 // Parse this argument |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
232 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
|
233 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
234 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
|
235 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
236 // 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
|
237 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
|
238 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
239 // 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
|
240 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
|
241 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
|
242 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
|
243 struct longopts *lo; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
244 |
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
|
245 gof.arg++; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
246 // 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
|
247 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
|
248 stopearly += 2; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
249 goto notflag; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
250 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
251 // Handle --longopt |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
252 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
253 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
|
254 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
|
255 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
|
256 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
|
257 && (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
|
258 { |
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
|
259 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
|
260 } 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
|
261 } |
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
|
262 // 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
|
263 gof.arg = ""; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
264 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
|
265 break; |
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 } |
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
|
268 |
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 // 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
|
270 gotflag(); |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
271 continue; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
272 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
273 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
274 // 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
|
275 } else { |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
276 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
|
277 else goto notflag; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
278 } |
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 // 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
|
281 // 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
|
282 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
|
283 |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
284 // 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
|
285 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
|
286 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
|
287 |
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
|
288 // 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
|
289 gotflag(); |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
290 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
291 continue; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
292 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
293 // 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
|
294 notflag: |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
295 if (stopearly) stopearly++; |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
296 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
|
297 } |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
298 |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
299 // Sanity check |
127 | 300 if (optarg<minargs) |
301 error_exit("Need %d argument%s", minargs, minargs ? "s" : ""); | |
302 if (optarg>maxargs) | |
303 error_exit("Max %d argument%s", maxargs, maxargs ? "s" : ""); | |
144
1fbc50374a30
Promote help to global config option, teach error_exit() to output usage message when called
Rob Landley <rob@landley.net>
parents:
127
diff
changeset
|
304 if (CFG_HELP) toys.exithelp = 0; |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
305 } |
49
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
306 |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
307 // Loop through files listed on the command line |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
308 |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
309 static int dofileargs(char ***files, int fd, int iswrite) |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
310 { |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
311 char *filename = *((*files)++); |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
312 static int flags[] = {O_RDONLY, O_CREAT|O_TRUNC, O_RDWR}; |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
313 |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
314 if (fd != -1) close(fd); |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
315 |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
316 for (;;) { |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
317 |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
318 // Are there no more files? |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
319 if (!*filename) |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
320 return (fd == -1) ? iswrite : -1; |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
321 |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
322 // A filename of "-" means stdin. |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
323 if (*filename == '-' && !filename[1]) return 0; |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
324 |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
325 fd = xcreate(filename, flags[iswrite], 0777); |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
326 } |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
327 } |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
328 |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
329 int readfileargs(char ***files, int fd) |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
330 { |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
331 return dofileargs(files, fd, 0); |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
332 } |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
333 |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
334 int writefileargs(char ***files, int fd) |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
335 { |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
336 return dofileargs(files, fd, 1); |
bb4c38853a7d
xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents:
32
diff
changeset
|
337 } |