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