Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4 : |
| 2 | * args.c - Command line argument parsing. |
| 3 | * |
| 4 | * Copyright 2006 Rob Landley <rob@landley.net> |
| 5 | */ |
| 6 | |
| 7 | #include "toys.h" |
| 8 | |
| 9 | // Design goals: |
| 10 | // Don't use getopt() |
| 11 | // Don't permute original arguments. |
| 12 | // handle --long gracefully "(noshort)a(along)b(blong1)(blong2)" |
| 13 | // After each argument: |
| 14 | // Note that pointer and long are always the same size, even on 64 bit. |
| 15 | // : plus a string argument, keep most recent if more than one |
| 16 | // * plus a string argument, appended to a list |
Rob Landley | 028a544 | 2007-01-25 16:11:23 -0500 | [diff] [blame] | 17 | // # plus a signed long argument (TODO: Bounds checking?) |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 18 | // @ plus an occurrence counter (which is a long) |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 19 | // (longopt) |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 20 | // | this is required. If more than one marked, only one required. |
Rob Landley | 1a35c47 | 2008-07-03 19:14:23 -0500 | [diff] [blame] | 21 | // ^ Stop parsing after encountering this argument |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 22 | // |
| 23 | // These modify other option letters (previously seen in string): |
| 24 | // +X enabling this enables X (switch on) |
| 25 | // ~X enabling this disables X (switch off) |
| 26 | // !X die with error if X already set (x!x die if x supplied twice) |
| 27 | // [yz] needs at least one of y or z. |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 28 | // at the beginning: |
Rob Landley | b1487dc | 2008-06-26 22:48:43 -0500 | [diff] [blame] | 29 | // ^ stop at first nonoption argument |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 30 | // <0 at least # leftover arguments needed (default 0) |
| 31 | // >9 at most # leftover arguments needed (default MAX_INT) |
Rob Landley | 028a544 | 2007-01-25 16:11:23 -0500 | [diff] [blame] | 32 | // ? don't show_usage() on unknown argument. |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 33 | // & first argument has imaginary dash (ala tar/ps) |
| 34 | // If given twice, all arguments have imaginary dash |
| 35 | |
| 36 | // Notes from getopt man page |
| 37 | // - and -- cannot be arguments. |
| 38 | // -- force end of arguments |
| 39 | // - is a synonym for stdin in file arguments |
| 40 | // -abc means -a -b -c |
| 41 | |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 42 | /* This uses a getopt-like option string, but not getopt() itself. We call |
| 43 | * it the get_opt string. |
Rob Landley | 2c22685 | 2007-11-15 18:30:30 -0600 | [diff] [blame] | 44 | * |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 45 | * Each option in the get_opt string corresponds to a bit position in the |
| 46 | * return value. The rightmost argument is (1<<0), the next to last is (1<<1) |
| 47 | * and so on. If the option isn't seen in argv[], its bit remains 0. |
Rob Landley | 028a544 | 2007-01-25 16:11:23 -0500 | [diff] [blame] | 48 | * |
| 49 | * Options which have an argument fill in the corresponding slot in the global |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 50 | * union "this" (see generated/globals.h), which it treats as an array of longs |
| 51 | * (note that sizeof(long)==sizeof(pointer) is guaranteed by LP64). |
Rob Landley | 028a544 | 2007-01-25 16:11:23 -0500 | [diff] [blame] | 52 | * |
| 53 | * You don't have to free the option strings, which point into the environment |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 54 | * space. List objects should be freed by main() when command_main() returns. |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 55 | * |
| 56 | * Example: |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 57 | * Calling get_optflags() when toys.which->options="ab:c:d" and |
| 58 | * argv = ["command", "-b", "fruit", "-d", "walrus"] results in: |
| 59 | * |
| 60 | * Changes to struct toys: |
| 61 | * toys.optflags = 5 (-b=4 | -d=1) |
| 62 | * toys.optargs[0]="walrus" (leftover argument) |
| 63 | * toys.optargs[1]=NULL (end of list) |
| 64 | * toys.optc=1 (there was 1 leftover argument) |
| 65 | * |
| 66 | * Changes to union this: |
| 67 | * this[0]=NULL (because -c didn't get an argument this time) |
| 68 | * this[1]="fruit" (argument to -b) |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 69 | */ |
| 70 | |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 71 | // Linked list of all known options (get_opt string is parsed into this). |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 72 | struct opts { |
| 73 | struct opts *next; |
Rob Landley | 1a35c47 | 2008-07-03 19:14:23 -0500 | [diff] [blame] | 74 | long *arg; // Pointer into union "this" to store arguments at. |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 75 | uint32_t edx[3]; // Flag mask to enable/disable/exclude. |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 76 | int c; // Short argument character |
Rob Landley | 1a35c47 | 2008-07-03 19:14:23 -0500 | [diff] [blame] | 77 | int flags; // |=1, ^=2 |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 78 | char type; // Type of arguments to store |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 79 | }; |
| 80 | |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 81 | // State during argument parsing. |
| 82 | struct getoptflagstate |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 83 | { |
| 84 | int argc; |
| 85 | char *arg; |
| 86 | struct opts *opts, *this; |
Rob Landley | 1a35c47 | 2008-07-03 19:14:23 -0500 | [diff] [blame] | 87 | int noerror, nodash_now, stopearly; |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 88 | uint32_t excludes; |
| 89 | }; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 90 | |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 91 | // Parse one command line option. |
| 92 | |
Rob Landley | 1a221d9 | 2008-05-17 17:13:26 -0500 | [diff] [blame] | 93 | static int gotflag(struct getoptflagstate *gof) |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 94 | { |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 95 | int type; |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 96 | struct opts *opt = gof->this; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 97 | |
| 98 | // Did we recognize this option? |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 99 | if (!opt) { |
Rob Landley | 1a221d9 | 2008-05-17 17:13:26 -0500 | [diff] [blame] | 100 | if (gof->noerror) return 1; |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 101 | error_exit("Unknown option %s", gof->arg); |
| 102 | } |
| 103 | toys.optflags |= opt->edx[0]; |
| 104 | toys.optflags &= ~opt->edx[1]; |
| 105 | gof->excludes = opt->edx[2]; |
Rob Landley | 1a35c47 | 2008-07-03 19:14:23 -0500 | [diff] [blame] | 106 | if (opt->flags&2) gof->stopearly=2; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 107 | |
| 108 | // Does this option take an argument? |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 109 | gof->arg++; |
| 110 | type = opt->type; |
Rob Landley | 58c6c1b | 2006-11-25 13:34:51 -0500 | [diff] [blame] | 111 | if (type) { |
| 112 | |
| 113 | // Handle "-xblah" and "-x blah", but also a third case: "abxc blah" |
| 114 | // to make "tar xCjfv blah1 blah2 thingy" work like |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 115 | // "tar -x -C blah1 -j -f blah2 -v thingy" |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 116 | if (!gof->nodash_now && !gof->arg[0]) { |
| 117 | gof->arg = toys.argv[++gof->argc]; |
| 118 | // TODO: The following line doesn't display --longopt correctly |
| 119 | if (!gof->arg) error_exit("Missing argument to -%c",opt->c); |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 120 | } |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 121 | |
Rob Landley | 58c6c1b | 2006-11-25 13:34:51 -0500 | [diff] [blame] | 122 | // Grab argument. |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 123 | if (!gof->arg && !(gof->arg = toys.argv[++(gof->argc)])) |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 124 | error_exit("Missing argument"); |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 125 | if (type == ':') *(opt->arg) = (long)gof->arg; |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 126 | else if (type == '*') { |
Rob Landley | 4ac6656 | 2008-06-16 19:27:35 -0500 | [diff] [blame] | 127 | struct arg_list **list; |
| 128 | |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 129 | list = (struct arg_list **)opt->arg; |
Rob Landley | 4ac6656 | 2008-06-16 19:27:35 -0500 | [diff] [blame] | 130 | while (*list) list=&((*list)->next); |
| 131 | *list = xzalloc(sizeof(struct arg_list)); |
| 132 | (*list)->arg = gof->arg; |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 133 | } else if (type == '#') *(opt->arg) = atolx((char *)gof->arg); |
Rob Landley | 4ac6656 | 2008-06-16 19:27:35 -0500 | [diff] [blame] | 134 | else if (type == '@') ++*(opt->arg); |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 135 | |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 136 | gof->arg = ""; |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 137 | } |
Rob Landley | 58c6c1b | 2006-11-25 13:34:51 -0500 | [diff] [blame] | 138 | |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 139 | gof->this = NULL; |
Rob Landley | 1a221d9 | 2008-05-17 17:13:26 -0500 | [diff] [blame] | 140 | return 0; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 141 | } |
| 142 | |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 143 | // Fill out toys.optflags and toys.optargs. |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 144 | |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 145 | static char *plustildenot = "+~!"; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 146 | void get_optflags(void) |
| 147 | { |
Rob Landley | 1a35c47 | 2008-07-03 19:14:23 -0500 | [diff] [blame] | 148 | int nodash = 0, minargs = 0, maxargs; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 149 | struct longopts { |
| 150 | struct longopts *next; |
| 151 | struct opts *opt; |
| 152 | char *str; |
| 153 | int len; |
| 154 | } *longopts = NULL; |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 155 | struct getoptflagstate gof; |
Rob Landley | 1a221d9 | 2008-05-17 17:13:26 -0500 | [diff] [blame] | 156 | long *nextarg = (long *)&this, saveflags; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 157 | char *options = toys.which->options; |
Rob Landley | 8e99874 | 2008-05-04 18:52:29 -0500 | [diff] [blame] | 158 | char *letters[]={"s",""}; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 159 | |
Rob Landley | d06c58d | 2007-10-11 15:36:36 -0500 | [diff] [blame] | 160 | if (CFG_HELP) toys.exithelp++; |
Rob Landley | 54ebcce | 2006-11-19 20:35:19 -0500 | [diff] [blame] | 161 | // Allocate memory for optargs |
| 162 | maxargs = 0; |
| 163 | while (toys.argv[maxargs++]); |
| 164 | toys.optargs = xzalloc(sizeof(char *)*maxargs); |
| 165 | maxargs = INT_MAX; |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 166 | bzero(&gof, sizeof(struct getoptflagstate)); |
Rob Landley | 54ebcce | 2006-11-19 20:35:19 -0500 | [diff] [blame] | 167 | |
| 168 | // Parse option format |
Rob Landley | 2a813ff | 2006-11-19 17:29:35 -0500 | [diff] [blame] | 169 | if (options) { |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 170 | |
Rob Landley | 2a813ff | 2006-11-19 17:29:35 -0500 | [diff] [blame] | 171 | // Parse leading special behavior indicators |
| 172 | for (;;) { |
Rob Landley | 1a35c47 | 2008-07-03 19:14:23 -0500 | [diff] [blame] | 173 | if (*options == '^') gof.stopearly++; |
Rob Landley | 2a813ff | 2006-11-19 17:29:35 -0500 | [diff] [blame] | 174 | else if (*options == '<') minargs=*(++options)-'0'; |
| 175 | else if (*options == '>') maxargs=*(++options)-'0'; |
Rob Landley | 028a544 | 2007-01-25 16:11:23 -0500 | [diff] [blame] | 176 | else if (*options == '?') gof.noerror++; |
Rob Landley | 2a813ff | 2006-11-19 17:29:35 -0500 | [diff] [blame] | 177 | else if (*options == '&') nodash++; |
| 178 | else break; |
| 179 | options++; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 180 | } |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 181 | |
Rob Landley | 1a35c47 | 2008-07-03 19:14:23 -0500 | [diff] [blame] | 182 | if (!*options) gof.stopearly++; |
Rob Landley | 2a813ff | 2006-11-19 17:29:35 -0500 | [diff] [blame] | 183 | // Parse rest of opts into array |
| 184 | while (*options) { |
Rob Landley | aba353e | 2008-03-24 00:32:25 -0500 | [diff] [blame] | 185 | char *temp; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 186 | |
Rob Landley | 2a813ff | 2006-11-19 17:29:35 -0500 | [diff] [blame] | 187 | // Allocate a new option entry when necessary |
| 188 | if (!gof.this) { |
| 189 | gof.this = xzalloc(sizeof(struct opts)); |
| 190 | gof.this->next = gof.opts; |
| 191 | gof.opts = gof.this; |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 192 | ++*(gof.this->edx); |
Rob Landley | 2a813ff | 2006-11-19 17:29:35 -0500 | [diff] [blame] | 193 | } |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 194 | // Each option must start with "(" or an option character. (Bare |
Rob Landley | 2a813ff | 2006-11-19 17:29:35 -0500 | [diff] [blame] | 195 | // longopts only come at the start of the string.) |
| 196 | if (*options == '(') { |
| 197 | char *end; |
| 198 | struct longopts *lo = xmalloc(sizeof(struct longopts)); |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 199 | |
Rob Landley | 2a813ff | 2006-11-19 17:29:35 -0500 | [diff] [blame] | 200 | // Find the end of the longopt |
| 201 | for (end = ++options; *end && *end != ')'; end++); |
Rob Landley | de05a70 | 2007-01-31 14:37:01 -0500 | [diff] [blame] | 202 | if (CFG_TOYBOX_DEBUG && !*end) |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 203 | error_exit("Bug1 in get_opt"); |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 204 | |
Rob Landley | 2a813ff | 2006-11-19 17:29:35 -0500 | [diff] [blame] | 205 | // Allocate and init a new struct longopts |
| 206 | lo = xmalloc(sizeof(struct longopts)); |
| 207 | lo->next = longopts; |
| 208 | lo->opt = gof.this; |
| 209 | lo->str = options; |
| 210 | lo->len = end-options; |
| 211 | longopts = lo; |
| 212 | options = end; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 213 | |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 214 | // Mark this as struct opt as used, even when no short opt. |
| 215 | if (!gof.this->c) gof.this->c = -1; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 216 | |
Rob Landley | 2a813ff | 2006-11-19 17:29:35 -0500 | [diff] [blame] | 217 | // If this is the start of a new option that wasn't a longopt, |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 218 | |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 219 | } else if (strchr(":*#@", *options)) { |
| 220 | gof.this->type = *options; |
| 221 | } else if (0 != (temp = strchr(plustildenot, *options))) { |
| 222 | int i=0, idx = temp - plustildenot; |
| 223 | struct opts *opt; |
| 224 | |
Rob Landley | aba353e | 2008-03-24 00:32:25 -0500 | [diff] [blame] | 225 | if (!*++options && CFG_TOYBOX_DEBUG) |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 226 | error_exit("Bug2 in get_opt"); |
| 227 | // Find this option flag (in previously parsed struct opt) |
| 228 | for (opt = gof.this; ; opt = opt->next) { |
| 229 | if (CFG_TOYBOX_DEBUG && !opt) error_exit("Bug3 in get_opt"); |
| 230 | if (opt->c == *options) break; |
| 231 | i++; |
| 232 | } |
| 233 | gof.this->edx[idx] |= 1<<i; |
| 234 | |
Rob Landley | 2a813ff | 2006-11-19 17:29:35 -0500 | [diff] [blame] | 235 | } else if (*options == '[') { |
Rob Landley | 1a35c47 | 2008-07-03 19:14:23 -0500 | [diff] [blame] | 236 | } else if (*options == '|') gof.this->flags |= 1; |
| 237 | else if (*options == '^') gof.this->flags |= 2; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 238 | |
Rob Landley | 2a813ff | 2006-11-19 17:29:35 -0500 | [diff] [blame] | 239 | // At this point, we've hit the end of the previous option. The |
| 240 | // current character is the start of a new option. If we've already |
| 241 | // assigned an option to this struct, loop to allocate a new one. |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 242 | // (It'll get back here afterwards and fall through to next else.) |
Rob Landley | 1a35c47 | 2008-07-03 19:14:23 -0500 | [diff] [blame] | 243 | else if(gof.this->c) { |
Rob Landley | 2a813ff | 2006-11-19 17:29:35 -0500 | [diff] [blame] | 244 | gof.this = NULL; |
| 245 | continue; |
| 246 | |
| 247 | // Claim this option, loop to see what's after it. |
| 248 | } else gof.this->c = *options; |
| 249 | |
| 250 | options++; |
| 251 | } |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 252 | } |
| 253 | |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 254 | // Initialize enable/disable/exclude masks and pointers to store arguments. |
| 255 | // (We have to calculate all this ahead of time because longopts jump into |
| 256 | // the middle of the list.) |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 257 | gof.argc = 0; |
Rob Landley | 028a544 | 2007-01-25 16:11:23 -0500 | [diff] [blame] | 258 | for (gof.this = gof.opts; gof.this; gof.this = gof.this->next) { |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 259 | int i; |
| 260 | |
| 261 | for (i=0;i<3;i++) gof.this->edx[i] <<= gof.argc; |
| 262 | gof.argc++; |
| 263 | if (gof.this->type) { |
Rob Landley | 028a544 | 2007-01-25 16:11:23 -0500 | [diff] [blame] | 264 | gof.this->arg = (void *)nextarg; |
| 265 | *(nextarg++) = 0; |
| 266 | } |
| 267 | } |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 268 | |
| 269 | // Iterate through command line arguments, skipping argv[0] |
| 270 | for (gof.argc=1; toys.argv[gof.argc]; gof.argc++) { |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 271 | gof.arg = toys.argv[gof.argc]; |
| 272 | gof.this = NULL; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 273 | |
| 274 | // Parse this argument |
Rob Landley | 1a35c47 | 2008-07-03 19:14:23 -0500 | [diff] [blame] | 275 | if (gof.stopearly>1) goto notflag; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 276 | |
| 277 | gof.nodash_now = 0; |
| 278 | |
| 279 | // Various things with dashes |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 280 | if (*gof.arg == '-') { |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 281 | |
| 282 | // Handle - |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 283 | if (!gof.arg[1]) goto notflag; |
| 284 | gof.arg++; |
| 285 | if (*gof.arg=='-') { |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 286 | struct longopts *lo; |
| 287 | |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 288 | gof.arg++; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 289 | // Handle -- |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 290 | if (!*gof.arg) { |
Rob Landley | 1a35c47 | 2008-07-03 19:14:23 -0500 | [diff] [blame] | 291 | gof.stopearly += 2; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 292 | goto notflag; |
| 293 | } |
| 294 | // Handle --longopt |
| 295 | |
| 296 | for (lo = longopts; lo; lo = lo->next) { |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 297 | if (!strncmp(gof.arg, lo->str, lo->len)) { |
| 298 | if (gof.arg[lo->len]) { |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 299 | if (gof.arg[lo->len]=='=' && lo->opt->type) |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 300 | gof.arg += lo->len; |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 301 | else continue; |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 302 | } |
| 303 | // It's a match. |
| 304 | gof.arg = ""; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 305 | gof.this = lo->opt; |
| 306 | break; |
| 307 | } |
| 308 | } |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 309 | |
Rob Landley | 1a221d9 | 2008-05-17 17:13:26 -0500 | [diff] [blame] | 310 | // Should we handle this --longopt as a non-option argument? |
| 311 | if (!lo && gof.noerror) { |
| 312 | gof.arg-=2; |
| 313 | goto notflag; |
| 314 | } |
| 315 | |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 316 | // Long option parsed, handle option. |
Rob Landley | 61190a3 | 2008-02-18 03:32:17 -0600 | [diff] [blame] | 317 | gotflag(&gof); |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 318 | continue; |
| 319 | } |
| 320 | |
| 321 | // Handle things that don't start with a dash. |
| 322 | } else { |
| 323 | if (nodash && (nodash>1 || gof.argc == 1)) gof.nodash_now = 1; |
| 324 | else goto notflag; |
| 325 | } |
| 326 | |
| 327 | // At this point, we have the args part of -args. Loop through |
| 328 | // each entry (could be -abc meaning -a -b -c) |
Rob Landley | 1a221d9 | 2008-05-17 17:13:26 -0500 | [diff] [blame] | 329 | saveflags = toys.optflags; |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 330 | while (*gof.arg) { |
| 331 | |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 332 | // Identify next option char. |
Rob Landley | fdb667e | 2006-11-24 00:15:21 -0500 | [diff] [blame] | 333 | for (gof.this = gof.opts; gof.this; gof.this = gof.this->next) |
| 334 | if (*gof.arg == gof.this->c) break; |
| 335 | |
| 336 | // Handle option char (advancing past what was used) |
Rob Landley | 1a221d9 | 2008-05-17 17:13:26 -0500 | [diff] [blame] | 337 | if (gotflag(&gof) ) { |
| 338 | toys.optflags = saveflags; |
| 339 | gof.arg = toys.argv[gof.argc]; |
| 340 | goto notflag; |
| 341 | } |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 342 | } |
| 343 | continue; |
| 344 | |
| 345 | // Not a flag, save value in toys.optargs[] |
| 346 | notflag: |
Rob Landley | 1a35c47 | 2008-07-03 19:14:23 -0500 | [diff] [blame] | 347 | if (gof.stopearly) gof.stopearly++; |
Rob Landley | 26bf9e6 | 2008-02-12 17:36:13 -0600 | [diff] [blame] | 348 | toys.optargs[toys.optc++] = toys.argv[gof.argc]; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // Sanity check |
Rob Landley | 8e99874 | 2008-05-04 18:52:29 -0500 | [diff] [blame] | 352 | if (toys.optc<minargs) { |
| 353 | error_exit("Need%s %d argument%s", letters[!!(minargs-1)], minargs, |
| 354 | letters[!(minargs-1)]); |
| 355 | } |
Rob Landley | 26bf9e6 | 2008-02-12 17:36:13 -0600 | [diff] [blame] | 356 | if (toys.optc>maxargs) |
Rob Landley | 8e99874 | 2008-05-04 18:52:29 -0500 | [diff] [blame] | 357 | error_exit("Max %d argument%s", maxargs, letters[!(maxargs-1)]); |
Rob Landley | d06c58d | 2007-10-11 15:36:36 -0500 | [diff] [blame] | 358 | if (CFG_HELP) toys.exithelp = 0; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 359 | } |