annotate lib/args.c @ 90:7c77c6ec17ee

Add "make defconfig". Modify global options to start with CONFIG_TOYBOX_.
author Rob Landley <rob@landley.net>
date Wed, 31 Jan 2007 14:37:01 -0500
parents cd1f36a96185
children aa4fa2543a65
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
78
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
111 } else if (type == '#') *(gof.this->arg) = atol((char *)gof.arg);
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
28
be59ed005902 Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents: 26
diff changeset
136 // Allocate memory for optargs
be59ed005902 Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents: 26
diff changeset
137 maxargs = 0;
be59ed005902 Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents: 26
diff changeset
138 while (toys.argv[maxargs++]);
be59ed005902 Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents: 26
diff changeset
139 toys.optargs = xzalloc(sizeof(char *)*maxargs);
be59ed005902 Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents: 26
diff changeset
140 maxargs = INT_MAX;
be59ed005902 Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents: 26
diff changeset
141
be59ed005902 Allocate a more sane amount of memory.
Rob Landley <rob@landley.net>
parents: 26
diff changeset
142 // Parse option format
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
143 if (options) {
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
144 // Parse leading special behavior indicators
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
145 for (;;) {
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
146 if (*options == '+') stopearly++;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
147 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
148 else if (*options == '>') maxargs=*(++options)-'0';
78
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
149 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
150 else if (*options == '&') nodash++;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
151 else break;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
152 options++;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
153 }
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
154
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
155 // 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
156 while (*options) {
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
157
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
158 // 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
159 if (!gof.this) {
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
160 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
161 gof.this->next = gof.opts;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
162 gof.opts = gof.this;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
163 }
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
164 // 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
165 // 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
166 if (*options == '(') {
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
167 char *end;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
168 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
169
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
170 // 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
171 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
172 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
173 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
174
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
175 // 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
176 lo = xmalloc(sizeof(struct longopts));
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
177 lo->next = longopts;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
178 lo->opt = gof.this;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
179 lo->str = options;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
180 lo->len = end-options;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
181 longopts = lo;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
182 options = end;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
183
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
184 // 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
185 // 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
186 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
187
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
188 // 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
189
78
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
190 } else if (index(":*#@", *options)) {
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
191 gof.this->type |= *options;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
192 } else if (*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 == '[') {
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
197
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
198 // 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
199 // 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
200 // 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
201 // (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
202 } 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
203 gof.this = NULL;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
204 continue;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
205
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
206 // 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
207 } 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
208
26
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
209 options++;
75b80e55bcfc Add one if() that has lots of whitespace fallout.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
210 }
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
211 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
212
78
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
213 // 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
214 // 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
215 // the list.)
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
216 gof.argc = 0;
78
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
217 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
218 gof.this->shift = gof.argc++;
78
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
219 if (gof.this->type & 255) {
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
220 gof.this->arg = (void *)nextarg;
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
221 *(nextarg++) = 0;
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
222 }
cd1f36a96185 Update args.c to implement numeric arguments.
Rob Landley <rob@landley.net>
parents: 49
diff changeset
223 }
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
224
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
225 // 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
226 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
227 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
228 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
229
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
230 // Parse this argument
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
231 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
232
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
233 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
234
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
235 // 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
236 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
237
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
238 // 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
239 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
240 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
241 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
242 struct longopts *lo;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
243
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
244 gof.arg++;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
245 // 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
246 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
247 stopearly += 2;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
248 goto notflag;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
249 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
250 // Handle --longopt
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
251
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
252 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
253 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
254 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
255 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
256 && (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
257 {
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
258 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
259 } 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
260 }
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 // 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
262 gof.arg = "";
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
263 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
264 break;
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 }
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
267
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 // 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
269 gotflag();
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
270 continue;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
271 }
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 // 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
274 } else {
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
275 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
276 else goto notflag;
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
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
279 // 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
280 // 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
281 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
282
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
283 // 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
284 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
285 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
286
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 // 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
288 gotflag();
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
289 }
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
290 continue;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
291
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
292 // 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
293 notflag:
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
294 if (stopearly) stopearly++;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
295 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
296 }
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 // Sanity check
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
diff changeset
299 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
300 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
301 }
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
302
bb4c38853a7d xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents: 32
diff changeset
303 // 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
304
bb4c38853a7d xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents: 32
diff changeset
305 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
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 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
308 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
309
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 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
311
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 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
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 // 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
315 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
316 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
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 // 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
319 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
320
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 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
322 }
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 }
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 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
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 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
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
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 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
331 {
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 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
333 }