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