args.c revision 2a813ffadf19957f1798cfc68a3ea78199ce1f49
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 55struct opts { 56 struct opts *next; 57 char c; 58 int type; 59 int shift; 60 void *arg; 61}; 62 63struct getoptflagstate 64{ 65 int argc; 66 char *arg; 67 struct opts *opts, *this; 68 int noerror, nodash_now; 69}; 70 71static struct getoptflagstate gof; 72 73// Returns zero if it didn't consume the rest of the current -abcdef 74static 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 121void get_optflags(void) 122{ 123 int stopearly = 0, optarg = 0, nodash = 0, minargs = 0, maxargs = INT_MAX; 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 133 if (options) { 134 // Parse leading special behavior indicators 135 for (;;) { 136 if (*options == '+') stopearly++; 137 else if (*options == '<') minargs=*(++options)-'0'; 138 else if (*options == '>') maxargs=*(++options)-'0'; 139 else if (*options == '#') gof.noerror++; 140 else if (*options == '&') nodash++; 141 else break; 142 options++; 143 } 144 145 // Parse rest of opts into array 146 while (*options) { 147 148 // Allocate a new option entry when necessary 149 if (!gof.this) { 150 gof.this = xzalloc(sizeof(struct opts)); 151 gof.this->next = gof.opts; 152 gof.opts = gof.this; 153 } 154 // Each option must start with (or an option character. (Bare 155 // longopts only come at the start of the string.) 156 if (*options == '(') { 157 char *end; 158 struct longopts *lo = xmalloc(sizeof(struct longopts)); 159 160 // Find the end of the longopt 161 for (end = ++options; *end && *end != ')'; end++); 162 if (CFG_DEBUG && !*end) error_exit("Unterminated optstring"); 163 164 // Allocate and init a new struct longopts 165 lo = xmalloc(sizeof(struct longopts)); 166 lo->next = longopts; 167 lo->opt = gof.this; 168 lo->str = options; 169 lo->len = end-options; 170 longopts = lo; 171 options = end; 172 173 // For leading longopts (with no corresponding short opt), note 174 // that this option struct has been used. 175 gof.this->shift++; 176 177 // If this is the start of a new option that wasn't a longopt, 178 179 } else if (index(":*?@", *options)) { 180 gof.this->type |= *options; 181 // Pointer and long guaranteed to be the same size by LP64. 182 *(++nextarg) = 0; 183 gof.this->arg = (void *)nextarg; 184 } else if (*options == '|') { 185 } else if (*options == '+') { 186 } else if (*options == '~') { 187 } else if (*options == '!') { 188 } else if (*options == '[') { 189 190 // At this point, we've hit the end of the previous option. The 191 // current character is the start of a new option. If we've already 192 // assigned an option to this struct, loop to allocate a new one. 193 // (It'll get back here afterwards.) 194 } else if(gof.this->shift || gof.this->c) { 195 gof.this = NULL; 196 continue; 197 198 // Claim this option, loop to see what's after it. 199 } else gof.this->c = *options; 200 201 options++; 202 } 203 } 204 205 // Initialize shift bits (have to calculate this ahead of time because 206 // longopts jump into the middle of the list), and allocate space to 207 // store optargs. 208 gof.argc = 0; 209 for (gof.this = gof.opts; gof.this; gof.this = gof.this->next) 210 gof.this->shift = gof.argc++; 211 toys.optargs = xzalloc(sizeof(char *)*(++gof.argc)); 212 213 // Iterate through command line arguments, skipping argv[0] 214 for (gof.argc=1; toys.argv[gof.argc]; gof.argc++) { 215 char *arg = toys.argv[gof.argc]; 216 217 // Parse this argument 218 if (stopearly>1) goto notflag; 219 220 gof.nodash_now = 0; 221 222 // Various things with dashes 223 if (*arg == '-') { 224 225 // Handle - 226 if (!arg[1]) goto notflag; 227 arg++; 228 if (*arg=='-') { 229 struct longopts *lo; 230 231 arg++; 232 // Handle -- 233 if (!*arg) { 234 stopearly += 2; 235 goto notflag; 236 } 237 // Handle --longopt 238 239 for (lo = longopts; lo; lo = lo->next) { 240 if (!strncmp(arg, lo->str, lo->len)) { 241 if (arg[lo->len]) { 242 if (arg[lo->len]=='=' 243 && (lo->opt->type & 255)) 244 { 245 arg += lo->len; 246 } else continue; 247 248 // *options should be nul, this makes sure 249 // that the while (*arg) loop terminates; 250 } arg = options-1; 251 gof.this = lo->opt; 252 break; 253 } 254 } 255 // Long option parsed, jump to option handling. 256 gotflag(); 257 continue; 258 } 259 260 // Handle things that don't start with a dash. 261 } else { 262 if (nodash && (nodash>1 || gof.argc == 1)) gof.nodash_now = 1; 263 else goto notflag; 264 } 265 266 // At this point, we have the args part of -args. Loop through 267 // each entry (could be -abc meaning -a -b -c) 268 while (*arg) { 269 // Identify next option char. 270 for (gof.this = gof.opts; gof.this && *arg != gof.this->c; 271 gof.this = gof.this->next); 272 if (gotflag()) break; 273 arg++; 274 } 275 continue; 276 277 // Not a flag, save value in toys.optargs[] 278notflag: 279 if (stopearly) stopearly++; 280 toys.optargs[optarg++] = toys.argv[gof.argc]; 281 } 282 283 // Sanity check 284 if (optarg<minargs) error_exit("Need %d arguments", minargs); 285 if (optarg>maxargs) error_exit("Max %d arguments", maxargs); 286} 287