Showing the changes to src/lua.c (1 of 77 files). Show all
Unified · Split
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.182 2009/12/22 16:47:12 roberto Exp $ | 2 | ** $Id: lua.c,v 1.190 2010/04/14 15:14:21 roberto Exp $ |
| 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 | */ |
| @@ -102,26 +102,31 @@ static void laction (int i) { · 96 unmodified lines | |||
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | 104 | ||
| 105 | static void print_usage (void) { | 105 | static void print_usage (const char *badoption) { |
| 106 | fprintf(stderr, | 106 | if (badoption[1] == 'e' || badoption[1] == 'l') { |
| 107 | luai_writestringerror("%s: ", progname); | ||
| 108 | luai_writestringerror("'%s' needs argument\n", badoption); | ||
| 109 | } else { | ||
| 110 | luai_writestringerror("%s: ", progname); | ||
| 111 | luai_writestringerror("unrecognized option '%s'\n", badoption); | ||
| 112 | } | ||
| 113 | luai_writestringerror( | ||
| 107 | "usage: %s [options] [script [args]]\n" | 114 | "usage: %s [options] [script [args]]\n" |
| 108 | "Available options are:\n" | 115 | "Available options are:\n" |
| 109 | " -e stat execute string " LUA_QL("stat") "\n" | 116 | " -e stat execute string " LUA_QL("stat") "\n" |
| 110 | " -l name require library " LUA_QL("name") "\n" | ||
| 111 | " -i enter interactive mode after executing " LUA_QL("script") "\n" | 117 | " -i enter interactive mode after executing " LUA_QL("script") "\n" |
| 118 | " -l name require library " LUA_QL("name") "\n" | ||
| 112 | " -v show version information\n" | 119 | " -v show version information\n" |
| 113 | " -- stop handling options\n" | 120 | " -- stop handling options\n" |
| 114 | " - execute stdin and stop handling options\n" | 121 | " - stop handling options and execute stdin\n" |
| 115 | , | 122 | , |
| 116 | progname); | 123 | progname); |
| 117 | fflush(stderr); | ||
| 118 | } | 124 | } |
| 119 | 125 | ||
| 120 | 126 | ||
| 121 | static void l_message (const char *pname, const char *msg) { | 127 | static void l_message (const char *pname, const char *msg) { |
| 122 | if (pname) fprintf(stderr, "%s: ", pname); | 128 | if (pname) luai_writestringerror("%s: ", pname); |
| 123 | fprintf(stderr, "%s\n", msg); | 129 | luai_writestringerror("%s\n", msg); |
| 124 | fflush(stderr); | ||
| 125 | } | 130 | } |
| 126 | 131 | ||
| 127 | 132 | ||
| @@ -214,7 +219,7 @@ static int dostring (lua_State *L, const char *s, const char *name) { · 86 unmodified lines | |||
| 214 | 219 | ||
| 215 | 220 | ||
| 216 | static int dolibrary (lua_State *L, const char *name) { | 221 | static int dolibrary (lua_State *L, const char *name) { |
| 217 | lua_getfield(L, LUA_ENVIRONINDEX, "require"); | 222 | lua_getglobal(L, "require"); |
| 218 | lua_pushstring(L, name); | 223 | lua_pushstring(L, name); |
| 219 | return report(L, docall(L, 1, 1)); | 224 | return report(L, docall(L, 1, 1)); |
| 220 | } | 225 | } |
| @@ -222,7 +227,7 @@ static int dolibrary (lua_State *L, const char *name) { · 1 unmodified line | |||
| 222 | 227 | ||
| 223 | static const char *get_prompt (lua_State *L, int firstline) { | 228 | static const char *get_prompt (lua_State *L, int firstline) { |
| 224 | const char *p; | 229 | const char *p; |
| 225 | lua_getfield(L, LUA_ENVIRONINDEX, firstline ? "_PROMPT" : "_PROMPT2"); | 230 | lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); |
| 226 | p = lua_tostring(L, -1); | 231 | p = lua_tostring(L, -1); |
| 227 | if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2); | 232 | if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2); |
| 228 | lua_pop(L, 1); /* remove global */ | 233 | lua_pop(L, 1); /* remove global */ |
| @@ -296,7 +301,7 @@ static void dotty (lua_State *L) { · 67 unmodified lines | |||
| 296 | report(L, status); | 301 | report(L, status); |
| 297 | if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */ | 302 | if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */ |
| 298 | luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); | 303 | luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); |
| 299 | lua_getfield(L, LUA_ENVIRONINDEX, "print"); | 304 | lua_getglobal(L, "print"); |
| 300 | lua_insert(L, 1); | 305 | lua_insert(L, 1); |
| 301 | if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK) | 306 | if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK) |
| 302 | l_message(progname, lua_pushfstring(L, | 307 | l_message(progname, lua_pushfstring(L, |
| @@ -306,7 +311,6 @@ static void dotty (lua_State *L) { · 3 unmodified lines | |||
| 306 | } | 311 | } |
| 307 | lua_settop(L, 0); /* clear stack */ | 312 | lua_settop(L, 0); /* clear stack */ |
| 308 | luai_writestring("\n", 1); | 313 | luai_writestring("\n", 1); |
| 309 | fflush(stdout); | ||
| 310 | progname = oldprogname; | 314 | progname = oldprogname; |
| 311 | } | 315 | } |
| 312 | 316 | ||
| @@ -315,7 +319,7 @@ static int handle_script (lua_State *L, char **argv, int n) { · 2 unmodified lines | |||
| 315 | int status; | 319 | int status; |
| 316 | const char *fname; | 320 | const char *fname; |
| 317 | int narg = getargs(L, argv, n); /* collect arguments */ | 321 | int narg = getargs(L, argv, n); /* collect arguments */ |
| 318 | lua_setfield(L, LUA_ENVIRONINDEX, "arg"); | 322 | lua_setglobal(L, "arg"); |
| 319 | fname = argv[n]; | 323 | fname = argv[n]; |
| 320 | if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) | 324 | if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) |
| 321 | fname = NULL; /* stdin */ | 325 | fname = NULL; /* stdin */ |
| @@ -356,10 +360,11 @@ static int collectargs (char **argv, int *pi, int *pv, int *pe) { · 34 unmodified lines | |||
| 356 | case 'l': | 360 | case 'l': |
| 357 | if (argv[i][2] == '\0') { | 361 | if (argv[i][2] == '\0') { |
| 358 | i++; | 362 | i++; |
| 359 | if (argv[i] == NULL) return -1; | 363 | if (argv[i] == NULL) return -(i - 1); |
| 360 | } | 364 | } |
| 361 | break; | 365 | break; |
| 362 | default: return -1; /* invalid option */ | 366 | default: /* invalid option; return its index... */ |
| 367 | return -i; /* ...as a negative value */ | ||
| 363 | } | 368 | } |
| 364 | } | 369 | } |
| 365 | return 0; | 370 | return 0; |
| @@ -369,7 +374,6 @@ static int collectargs (char **argv, int *pi, int *pv, int *pe) { · 3 unmodified lines | |||
| 369 | static int runargs (lua_State *L, char **argv, int n) { | 374 | static int runargs (lua_State *L, char **argv, int n) { |
| 370 | int i; | 375 | int i; |
| 371 | for (i = 1; i < n; i++) { | 376 | for (i = 1; i < n; i++) { |
| 372 | if (argv[i] == NULL) continue; | ||
| 373 | lua_assert(argv[i][0] == '-'); | 377 | lua_assert(argv[i][0] == '-'); |
| 374 | switch (argv[i][1]) { /* option */ | 378 | switch (argv[i][1]) { /* option */ |
| 375 | case 'e': { | 379 | case 'e': { |
| @@ -412,8 +416,8 @@ static int pmain (lua_State *L) { · 36 unmodified lines | |||
| 412 | int has_i = 0, has_v = 0, has_e = 0; | 416 | int has_i = 0, has_v = 0, has_e = 0; |
| 413 | if (argv[0] && argv[0][0]) progname = argv[0]; | 417 | if (argv[0] && argv[0][0]) progname = argv[0]; |
| 414 | script = collectargs(argv, &has_i, &has_v, &has_e); | 418 | script = collectargs(argv, &has_i, &has_v, &has_e); |
| 415 | if (script < 0) { /* invalid args? */ | 419 | if (script < 0) { /* invalid arg? */ |
| 416 | print_usage(); | 420 | print_usage(argv[-script]); |
| 417 | return 0; | 421 | return 0; |
| 418 | } | 422 | } |
| 419 | if (has_v) print_version(); | 423 | if (has_v) print_version(); |
| @@ -443,7 +447,6 @@ static int pmain (lua_State *L) { · 23 unmodified lines | |||
| 443 | 447 | ||
| 444 | 448 | ||
| 445 | int main (int argc, char **argv) { | 449 | int main (int argc, char **argv) { |
| 446 | static lua_CFunction ppmain = &pmain; | ||
| 447 | int status, result; | 450 | int status, result; |
| 448 | lua_State *L = luaL_newstate(); /* create state */ | 451 | lua_State *L = luaL_newstate(); /* create state */ |
| 449 | if (L == NULL) { | 452 | if (L == NULL) { |
| @@ -451,11 +454,10 @@ int main (int argc, char **argv) { · 1 unmodified line | |||
| 451 | return EXIT_FAILURE; | 454 | return EXIT_FAILURE; |
| 452 | } | 455 | } |
| 453 | /* call 'pmain' in protected mode */ | 456 | /* call 'pmain' in protected mode */ |
| 454 | lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_CPCALL); /* calling function */ | 457 | lua_pushcfunction(L, &pmain); |
| 455 | lua_pushlightuserdata(L, &ppmain); | 458 | lua_pushinteger(L, argc); /* 1st argument */ |
| 456 | lua_pushinteger(L, argc); | 459 | lua_pushlightuserdata(L, argv); /* 2nd argument */ |
| 457 | lua_pushlightuserdata(L, argv); | 460 | status = lua_pcall(L, 2, 1, 0); |
| 458 | status = lua_pcall(L, 3, 1, 0); | ||
| 459 | result = lua_toboolean(L, -1); /* get result */ | 461 | result = lua_toboolean(L, -1); /* get result */ |
| 460 | finalreport(L, status); | 462 | finalreport(L, status); |
| 461 | lua_close(L); | 463 | lua_close(L); |