** 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). */
| 266 | ** (either the script name or a bad argument in case of error). |
| 267 | */ |
| 268 | static int collectargs (char **argv, int *first) { |
| 269 | int args = 0; |
| 270 | int i; |
| 271 | for (i = 1; argv[i] != NULL; i++) { |
| 272 | *first = i; |
| 273 | if (argv[i][0] != '-') /* not an option? */ |
| 274 | return args; /* stop handling options */ |
| 275 | switch (argv[i][1]) { /* else check option */ |
| 276 | case '-': /* '--' */ |
| 277 | if (argv[i][2] != '\0') /* extra characters after '--'? */ |
| 278 | return has_error; /* invalid option */ |
| 279 | *first = i + 1; |
| 280 | return args; |
| 281 | case '\0': /* '-' */ |
| 282 | return args; /* script "name" is '-' */ |
| 283 | case 'E': |
| 284 | if (argv[i][2] != '\0') /* extra characters? */ |
| 285 | return has_error; /* invalid option */ |
| 286 | args |= has_E; |
| 287 | break; |
| 288 | case 'W': |
| 289 | if (argv[i][2] != '\0') /* extra characters? */ |
| 290 | return has_error; /* invalid option */ |
| 291 | break; |
| 292 | case 'i': |
| 293 | args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ |
| 294 | case 'v': |
| 295 | if (argv[i][2] != '\0') /* extra characters? */ |
| 296 | return has_error; /* invalid option */ |
| 297 | args |= has_v; |
| 298 | break; |
| 299 | case 'e': |
| 300 | args |= has_e; /* FALLTHROUGH */ |
| 301 | case 'l': /* both options need an argument */ |
| 302 | if (argv[i][2] == '\0') { /* no concatenated argument? */ |
| 303 | i++; /* try next 'argv' */ |
| 304 | if (argv[i] == NULL || argv[i][0] == '-') |
| 305 | return has_error; /* no next argument or it is another option */ |
| 306 | } |
| 307 | break; |
| 308 | default: /* invalid option */ |
| 309 | return has_error; |
| 310 | } |
| 311 | } |
| 312 | *first = i; /* no script name */ |
| 313 | return args; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | /* |