** 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. In case of error, 'first' is the index of the bad ** argument. Otherwise, 'first' is -1 if there is no program name, ** 0 if there is no script name, or the index of the script name. */
| 284 | ** 0 if there is no script name, or the index of the script name. |
| 285 | */ |
| 286 | static int collectargs (char **argv, int *first) { |
| 287 | int args = 0; |
| 288 | int i; |
| 289 | if (argv[0] != NULL) { /* is there a program name? */ |
| 290 | if (argv[0][0]) /* not empty? */ |
| 291 | progname = argv[0]; /* save it */ |
| 292 | } |
| 293 | else { /* no program name */ |
| 294 | *first = -1; |
| 295 | return 0; |
| 296 | } |
| 297 | for (i = 1; argv[i] != NULL; i++) { /* handle arguments */ |
| 298 | *first = i; |
| 299 | if (argv[i][0] != '-') /* not an option? */ |
| 300 | return args; /* stop handling options */ |
| 301 | switch (argv[i][1]) { /* else check option */ |
| 302 | case '-': /* '--' */ |
| 303 | if (argv[i][2] != '\0') /* extra characters after '--'? */ |
| 304 | return has_error; /* invalid option */ |
| 305 | *first = i + 1; |
| 306 | return args; |
| 307 | case '\0': /* '-' */ |
| 308 | return args; /* script "name" is '-' */ |
| 309 | case 'E': |
| 310 | if (argv[i][2] != '\0') /* extra characters? */ |
| 311 | return has_error; /* invalid option */ |
| 312 | args |= has_E; |
| 313 | break; |
| 314 | case 'W': |
| 315 | if (argv[i][2] != '\0') /* extra characters? */ |
| 316 | return has_error; /* invalid option */ |
| 317 | break; |
| 318 | case 'i': |
| 319 | args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ |
| 320 | case 'v': |
| 321 | if (argv[i][2] != '\0') /* extra characters? */ |
| 322 | return has_error; /* invalid option */ |
| 323 | args |= has_v; |
| 324 | break; |
| 325 | case 'e': |
| 326 | args |= has_e; /* FALLTHROUGH */ |
| 327 | case 'l': /* both options need an argument */ |
| 328 | if (argv[i][2] == '\0') { /* no concatenated argument? */ |
| 329 | i++; /* try next 'argv' */ |
| 330 | if (argv[i] == NULL || argv[i][0] == '-') |
| 331 | return has_error; /* no next argument or it is another option */ |
| 332 | } |
| 333 | break; |
| 334 | default: /* invalid option */ |
| 335 | return has_error; |
| 336 | } |
| 337 | } |
| 338 | *first = 0; /* no script name */ |
| 339 | return args; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | /* |