** Traverses all arguments from 'argv', returning a mask with those ** needed before running any Lua code (or an error code if it finds ** any invalid argument). 'first' returns the first not-handled argument ** (either the script name or a bad argument in case of error). */
| 273 | ** (either the script name or a bad argument in case of error). |
| 274 | */ |
| 275 | static int collectargs (char **argv, int *first) { |
| 276 | int args = 0; |
| 277 | int i; |
| 278 | for (i = 1; argv[i] != NULL; i++) { |
| 279 | *first = i; |
| 280 | if (argv[i][0] != '-') /* not an option? */ |
| 281 | return args; /* stop handling options */ |
| 282 | switch (argv[i][1]) { /* else check option */ |
| 283 | case '-': /* '--' */ |
| 284 | if (argv[i][2] != '\0') /* extra characters after '--'? */ |
| 285 | return has_error; /* invalid option */ |
| 286 | *first = i + 1; |
| 287 | return args; |
| 288 | case '\0': /* '-' */ |
| 289 | return args; /* script "name" is '-' */ |
| 290 | case 'E': |
| 291 | if (argv[i][2] != '\0') /* extra characters? */ |
| 292 | return has_error; /* invalid option */ |
| 293 | args |= has_E; |
| 294 | break; |
| 295 | case 'W': |
| 296 | if (argv[i][2] != '\0') /* extra characters? */ |
| 297 | return has_error; /* invalid option */ |
| 298 | break; |
| 299 | case 'i': |
| 300 | args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ |
| 301 | case 'v': |
| 302 | if (argv[i][2] != '\0') /* extra characters? */ |
| 303 | return has_error; /* invalid option */ |
| 304 | args |= has_v; |
| 305 | break; |
| 306 | case 'e': |
| 307 | args |= has_e; /* FALLTHROUGH */ |
| 308 | case 'l': /* both options need an argument */ |
| 309 | if (argv[i][2] == '\0') { /* no concatenated argument? */ |
| 310 | i++; /* try next 'argv' */ |
| 311 | if (argv[i] == NULL || argv[i][0] == '-') |
| 312 | return has_error; /* no next argument or it is another option */ |
| 313 | } |
| 314 | break; |
| 315 | default: /* invalid option */ |
| 316 | return has_error; |
| 317 | } |
| 318 | } |
| 319 | *first = i; /* no script name */ |
| 320 | return args; |
| 321 | } |
| 322 | |
| 323 | |
| 324 | /* |