diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-08-04 15:16:16 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-08-04 15:16:16 -0300 |
commit | 6a5d89b39fdd22fdac03cbaeab375c58756c5b5f (patch) | |
tree | ab8bb53497f7558f14fe013daee0937e604a65af | |
parent | ae14adc272ae60457c519e55653bd2770655ac18 (diff) | |
download | lua-6a5d89b39fdd22fdac03cbaeab375c58756c5b5f.tar.gz lua-6a5d89b39fdd22fdac03cbaeab375c58756c5b5f.tar.bz2 lua-6a5d89b39fdd22fdac03cbaeab375c58756c5b5f.zip |
detail (using array instead of several variables to keep track of
enabled command-line options)
-rw-r--r-- | lua.c | 27 |
1 files changed, 17 insertions, 10 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.199 2011/05/26 16:09:40 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.200 2011/06/16 14:30:58 roberto Exp roberto $ |
3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -348,7 +348,13 @@ static int handle_script (lua_State *L, char **argv, int n) { | |||
348 | #define noextrachars(x) {if ((x)[2] != '\0') return -1;} | 348 | #define noextrachars(x) {if ((x)[2] != '\0') return -1;} |
349 | 349 | ||
350 | 350 | ||
351 | static int collectargs (char **argv, int *pi, int *pv, int *pe) { | 351 | /* indices of various argument indicators in array args */ |
352 | #define has_i 0 /* -i */ | ||
353 | #define has_v 1 /* -v */ | ||
354 | #define has_e 2 /* -e */ | ||
355 | |||
356 | |||
357 | static int collectargs (char **argv, int *args) { | ||
352 | int i; | 358 | int i; |
353 | for (i = 1; argv[i] != NULL; i++) { | 359 | for (i = 1; argv[i] != NULL; i++) { |
354 | if (argv[i][0] != '-') /* not an option? */ | 360 | if (argv[i][0] != '-') /* not an option? */ |
@@ -361,13 +367,13 @@ static int collectargs (char **argv, int *pi, int *pv, int *pe) { | |||
361 | return i; | 367 | return i; |
362 | case 'i': | 368 | case 'i': |
363 | noextrachars(argv[i]); | 369 | noextrachars(argv[i]); |
364 | *pi = 1; /* go through */ | 370 | args[has_i] = 1; /* go through */ |
365 | case 'v': | 371 | case 'v': |
366 | noextrachars(argv[i]); | 372 | noextrachars(argv[i]); |
367 | *pv = 1; | 373 | args[has_v] = 1; |
368 | break; | 374 | break; |
369 | case 'e': | 375 | case 'e': |
370 | *pe = 1; /* go through */ | 376 | args[has_e] = 1; /* go through */ |
371 | case 'l': /* both options need an argument */ | 377 | case 'l': /* both options need an argument */ |
372 | if (argv[i][2] == '\0') { /* no concatenated argument? */ | 378 | if (argv[i][2] == '\0') { /* no concatenated argument? */ |
373 | i++; /* try next 'argv' */ | 379 | i++; /* try next 'argv' */ |
@@ -430,14 +436,15 @@ static int pmain (lua_State *L) { | |||
430 | int argc = (int)lua_tointeger(L, 1); | 436 | int argc = (int)lua_tointeger(L, 1); |
431 | char **argv = (char **)lua_touserdata(L, 2); | 437 | char **argv = (char **)lua_touserdata(L, 2); |
432 | int script; | 438 | int script; |
433 | int has_i = 0, has_v = 0, has_e = 0; | 439 | int args[3]; |
440 | args[has_i] = args[has_v] = args[has_e] = 0; | ||
434 | if (argv[0] && argv[0][0]) progname = argv[0]; | 441 | if (argv[0] && argv[0][0]) progname = argv[0]; |
435 | script = collectargs(argv, &has_i, &has_v, &has_e); | 442 | script = collectargs(argv, args); |
436 | if (script < 0) { /* invalid arg? */ | 443 | if (script < 0) { /* invalid arg? */ |
437 | print_usage(argv[-script]); | 444 | print_usage(argv[-script]); |
438 | return 0; | 445 | return 0; |
439 | } | 446 | } |
440 | if (has_v) print_version(); | 447 | if (args[has_v]) print_version(); |
441 | /* open standard libraries */ | 448 | /* open standard libraries */ |
442 | luaL_checkversion(L); | 449 | luaL_checkversion(L); |
443 | lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ | 450 | lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ |
@@ -449,9 +456,9 @@ static int pmain (lua_State *L) { | |||
449 | if (!runargs(L, argv, (script > 0) ? script : argc)) return 0; | 456 | if (!runargs(L, argv, (script > 0) ? script : argc)) return 0; |
450 | /* execute main script (if there is one) */ | 457 | /* execute main script (if there is one) */ |
451 | if (script && handle_script(L, argv, script) != LUA_OK) return 0; | 458 | if (script && handle_script(L, argv, script) != LUA_OK) return 0; |
452 | if (has_i) /* -i option? */ | 459 | if (args[has_i]) /* -i option? */ |
453 | dotty(L); | 460 | dotty(L); |
454 | else if (script == 0 && !has_e && !has_v) { /* no arguments? */ | 461 | else if (script == 0 && !args[has_e] && !args[has_v]) { /* no arguments? */ |
455 | if (lua_stdin_is_tty()) { | 462 | if (lua_stdin_is_tty()) { |
456 | print_version(); | 463 | print_version(); |
457 | dotty(L); | 464 | dotty(L); |