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