Repository for the aotus library is now located at https://github.com/apes-suite/aotus
Revision | 85dac29555247e50773ba41723730fa3eea2d647 (tree) |
---|---|
Zeit | 2022-05-02 18:28:20 |
Autor | Harald Klimach <harald.klimach@dlr....> |
Commiter | Harald Klimach |
Applied patch 1 for Lua 5.4.4
lua.c assumes that argv has at least one element.
@@ -177,10 +177,11 @@ | ||
177 | 177 | ** to the script (everything after 'script') go to positive indices; |
178 | 178 | ** other arguments (before the script name) go to negative indices. |
179 | 179 | ** If there is no script name, assume interpreter's name as base. |
180 | +** (If there is no interpreter's name either, 'script' is -1, so | |
181 | +** table sizes are zero.) | |
180 | 182 | */ |
181 | 183 | static void createargtable (lua_State *L, char **argv, int argc, int script) { |
182 | 184 | int i, narg; |
183 | - if (script == argc) script = 0; /* no script name? */ | |
184 | 185 | narg = argc - (script + 1); /* number of positive indices */ |
185 | 186 | lua_createtable(L, narg, script + 1); |
186 | 187 | for (i = 0; i < argc; i++) { |
@@ -268,14 +269,23 @@ | ||
268 | 269 | |
269 | 270 | /* |
270 | 271 | ** Traverses all arguments from 'argv', returning a mask with those |
271 | -** needed before running any Lua code (or an error code if it finds | |
272 | -** any invalid argument). 'first' returns the first not-handled argument | |
273 | -** (either the script name or a bad argument in case of error). | |
272 | +** needed before running any Lua code or an error code if it finds any | |
273 | +** invalid argument. In case of error, 'first' is the index of the bad | |
274 | +** argument. Otherwise, 'first' is -1 if there is no program name, | |
275 | +** 0 if there is no script name, or the index of the script name. | |
274 | 276 | */ |
275 | 277 | static int collectargs (char **argv, int *first) { |
276 | 278 | int args = 0; |
277 | 279 | int i; |
278 | - for (i = 1; argv[i] != NULL; i++) { | |
280 | + if (argv[0] != NULL) { /* is there a program name? */ | |
281 | + if (argv[0][0]) /* not empty? */ | |
282 | + progname = argv[0]; /* save it */ | |
283 | + } | |
284 | + else { /* no program name */ | |
285 | + *first = -1; | |
286 | + return 0; | |
287 | + } | |
288 | + for (i = 1; argv[i] != NULL; i++) { /* handle arguments */ | |
279 | 289 | *first = i; |
280 | 290 | if (argv[i][0] != '-') /* not an option? */ |
281 | 291 | return args; /* stop handling options */ |
@@ -316,7 +326,7 @@ | ||
316 | 326 | return has_error; |
317 | 327 | } |
318 | 328 | } |
319 | - *first = i; /* no script name */ | |
329 | + *first = 0; /* no script name */ | |
320 | 330 | return args; |
321 | 331 | } |
322 | 332 |
@@ -609,8 +619,8 @@ | ||
609 | 619 | char **argv = (char **)lua_touserdata(L, 2); |
610 | 620 | int script; |
611 | 621 | int args = collectargs(argv, &script); |
622 | + int optlim = (script > 0) ? script : argc; /* first argv not an option */ | |
612 | 623 | luaL_checkversion(L); /* check that interpreter has correct version */ |
613 | - if (argv[0] && argv[0][0]) progname = argv[0]; | |
614 | 624 | if (args == has_error) { /* bad arg? */ |
615 | 625 | print_usage(argv[script]); /* 'script' has index of bad arg. */ |
616 | 626 | return 0; |
@@ -628,14 +638,15 @@ | ||
628 | 638 | if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ |
629 | 639 | return 0; /* error running LUA_INIT */ |
630 | 640 | } |
631 | - if (!runargs(L, argv, script)) /* execute arguments -e and -l */ | |
641 | + if (!runargs(L, argv, optlim)) /* execute arguments -e and -l */ | |
632 | 642 | return 0; /* something failed */ |
633 | - if (script < argc && /* execute main script (if there is one) */ | |
634 | - handle_script(L, argv + script) != LUA_OK) | |
635 | - return 0; | |
643 | + if (script > 0) { /* execute main script (if there is one) */ | |
644 | + if (handle_script(L, argv + script) != LUA_OK) | |
645 | + return 0; /* interrupt in case of error */ | |
646 | + } | |
636 | 647 | if (args & has_i) /* -i option? */ |
637 | 648 | doREPL(L); /* do read-eval-print loop */ |
638 | - else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */ | |
649 | + else if (script < 1 && !(args & (has_e | has_v))) { /* no active option? */ | |
639 | 650 | if (lua_stdin_is_tty()) { /* running in interactive mode? */ |
640 | 651 | print_version(); |
641 | 652 | doREPL(L); /* do read-eval-print loop */ |