diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2006-10-10 14:40:17 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2006-10-10 14:40:17 -0300 |
| commit | afa0d0ac0df60e37fd9473f50041505246228b35 (patch) | |
| tree | 4e1bff8a0be61220148e1443a15899f9be2944a5 /lua.c | |
| parent | 742b8be0c13b2abd6702c54c5f3ea929c3a9245f (diff) | |
| download | lua-afa0d0ac0df60e37fd9473f50041505246228b35.tar.gz lua-afa0d0ac0df60e37fd9473f50041505246228b35.tar.bz2 lua-afa0d0ac0df60e37fd9473f50041505246228b35.zip | |
new constant LUA_OK
Diffstat (limited to 'lua.c')
| -rw-r--r-- | lua.c | 50 |
1 files changed, 26 insertions, 24 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.162 2006/09/11 14:07:24 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.163 2006/09/18 14:03:18 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 | */ |
| @@ -63,7 +63,7 @@ static void l_message (const char *pname, const char *msg) { | |||
| 63 | 63 | ||
| 64 | 64 | ||
| 65 | static int report (lua_State *L, int status) { | 65 | static int report (lua_State *L, int status) { |
| 66 | if (status && !lua_isnil(L, -1)) { | 66 | if (status != LUA_OK && !lua_isnil(L, -1)) { |
| 67 | const char *msg = lua_tostring(L, -1); | 67 | const char *msg = lua_tostring(L, -1); |
| 68 | if (msg == NULL) msg = "(error object is not a string)"; | 68 | if (msg == NULL) msg = "(error object is not a string)"; |
| 69 | l_message(progname, msg); | 69 | l_message(progname, msg); |
| @@ -101,7 +101,7 @@ static int docall (lua_State *L, int narg, int clear) { | |||
| 101 | signal(SIGINT, SIG_DFL); | 101 | signal(SIGINT, SIG_DFL); |
| 102 | lua_remove(L, base); /* remove traceback function */ | 102 | lua_remove(L, base); /* remove traceback function */ |
| 103 | /* force a complete garbage collection in case of errors */ | 103 | /* force a complete garbage collection in case of errors */ |
| 104 | if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0); | 104 | if (status != LUA_OK) lua_gc(L, LUA_GCCOLLECT, 0); |
| 105 | return status; | 105 | return status; |
| 106 | } | 106 | } |
| 107 | 107 | ||
| @@ -130,13 +130,15 @@ static int getargs (lua_State *L, char **argv, int n) { | |||
| 130 | 130 | ||
| 131 | 131 | ||
| 132 | static int dofile (lua_State *L, const char *name) { | 132 | static int dofile (lua_State *L, const char *name) { |
| 133 | int status = luaL_loadfile(L, name) || docall(L, 0, 1); | 133 | int status = luaL_loadfile(L, name); |
| 134 | if (status == LUA_OK) status = docall(L, 0, 1); | ||
| 134 | return report(L, status); | 135 | return report(L, status); |
| 135 | } | 136 | } |
| 136 | 137 | ||
| 137 | 138 | ||
| 138 | static int dostring (lua_State *L, const char *s, const char *name) { | 139 | static int dostring (lua_State *L, const char *s, const char *name) { |
| 139 | int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1); | 140 | int status = luaL_loadbuffer(L, s, strlen(s), name); |
| 141 | if (status == LUA_OK) status = docall(L, 0, 1); | ||
| 140 | return report(L, status); | 142 | return report(L, status); |
| 141 | } | 143 | } |
| 142 | 144 | ||
| @@ -218,12 +220,12 @@ static void dotty (lua_State *L) { | |||
| 218 | const char *oldprogname = progname; | 220 | const char *oldprogname = progname; |
| 219 | progname = NULL; | 221 | progname = NULL; |
| 220 | while ((status = loadline(L)) != -1) { | 222 | while ((status = loadline(L)) != -1) { |
| 221 | if (status == 0) status = docall(L, 0, 0); | 223 | if (status == LUA_OK) status = docall(L, 0, 0); |
| 222 | report(L, status); | 224 | report(L, status); |
| 223 | if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */ | 225 | if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */ |
| 224 | lua_getglobal(L, "print"); | 226 | lua_getglobal(L, "print"); |
| 225 | lua_insert(L, 1); | 227 | lua_insert(L, 1); |
| 226 | if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0) | 228 | if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK) |
| 227 | l_message(progname, lua_pushfstring(L, | 229 | l_message(progname, lua_pushfstring(L, |
| 228 | "error calling " LUA_QL("print") " (%s)", | 230 | "error calling " LUA_QL("print") " (%s)", |
| 229 | lua_tostring(L, -1))); | 231 | lua_tostring(L, -1))); |
| @@ -246,7 +248,7 @@ static int handle_script (lua_State *L, char **argv, int n) { | |||
| 246 | fname = NULL; /* stdin */ | 248 | fname = NULL; /* stdin */ |
| 247 | status = luaL_loadfile(L, fname); | 249 | status = luaL_loadfile(L, fname); |
| 248 | lua_insert(L, -(narg+1)); | 250 | lua_insert(L, -(narg+1)); |
| 249 | if (status == 0) | 251 | if (status == LUA_OK) |
| 250 | status = docall(L, narg, 0); | 252 | status = docall(L, narg, 0); |
| 251 | else | 253 | else |
| 252 | lua_pop(L, narg); | 254 | lua_pop(L, narg); |
| @@ -301,28 +303,28 @@ static int runargs (lua_State *L, char **argv, int n) { | |||
| 301 | const char *chunk = argv[i] + 2; | 303 | const char *chunk = argv[i] + 2; |
| 302 | if (*chunk == '\0') chunk = argv[++i]; | 304 | if (*chunk == '\0') chunk = argv[++i]; |
| 303 | lua_assert(chunk != NULL); | 305 | lua_assert(chunk != NULL); |
| 304 | if (dostring(L, chunk, "=(command line)") != 0) | 306 | if (dostring(L, chunk, "=(command line)") != LUA_OK) |
| 305 | return 1; | 307 | return 0; |
| 306 | break; | 308 | break; |
| 307 | } | 309 | } |
| 308 | case 'l': { | 310 | case 'l': { |
| 309 | const char *filename = argv[i] + 2; | 311 | const char *filename = argv[i] + 2; |
| 310 | if (*filename == '\0') filename = argv[++i]; | 312 | if (*filename == '\0') filename = argv[++i]; |
| 311 | lua_assert(filename != NULL); | 313 | lua_assert(filename != NULL); |
| 312 | if (dolibrary(L, filename)) | 314 | if (dolibrary(L, filename) != LUA_OK) |
| 313 | return 1; /* stop if file fails */ | 315 | return 0; /* stop if file fails */ |
| 314 | break; | 316 | break; |
| 315 | } | 317 | } |
| 316 | default: break; | 318 | default: break; |
| 317 | } | 319 | } |
| 318 | } | 320 | } |
| 319 | return 0; | 321 | return 1; |
| 320 | } | 322 | } |
| 321 | 323 | ||
| 322 | 324 | ||
| 323 | static int handle_luainit (lua_State *L) { | 325 | static int handle_luainit (lua_State *L) { |
| 324 | const char *init = getenv(LUA_INIT); | 326 | const char *init = getenv(LUA_INIT); |
| 325 | if (init == NULL) return 0; /* status OK */ | 327 | if (init == NULL) return LUA_OK; |
| 326 | else if (init[0] == '@') | 328 | else if (init[0] == '@') |
| 327 | return dofile(L, init+1); | 329 | return dofile(L, init+1); |
| 328 | else | 330 | else |
| @@ -333,7 +335,7 @@ static int handle_luainit (lua_State *L) { | |||
| 333 | struct Smain { | 335 | struct Smain { |
| 334 | int argc; | 336 | int argc; |
| 335 | char **argv; | 337 | char **argv; |
| 336 | int status; | 338 | int ok; |
| 337 | }; | 339 | }; |
| 338 | 340 | ||
| 339 | 341 | ||
| @@ -347,20 +349,20 @@ static int pmain (lua_State *L) { | |||
| 347 | lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ | 349 | lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ |
| 348 | luaL_openlibs(L); /* open libraries */ | 350 | luaL_openlibs(L); /* open libraries */ |
| 349 | lua_gc(L, LUA_GCRESTART, 0); | 351 | lua_gc(L, LUA_GCRESTART, 0); |
| 350 | s->status = handle_luainit(L); | 352 | s->ok = (handle_luainit(L) == LUA_OK); |
| 351 | if (s->status != 0) return 0; | 353 | if (!s->ok) return 0; |
| 352 | script = collectargs(argv, &has_i, &has_v, &has_e); | 354 | script = collectargs(argv, &has_i, &has_v, &has_e); |
| 353 | if (script < 0) { /* invalid args? */ | 355 | if (script < 0) { /* invalid args? */ |
| 354 | print_usage(); | 356 | print_usage(); |
| 355 | s->status = 1; | 357 | s->ok = 0; |
| 356 | return 0; | 358 | return 0; |
| 357 | } | 359 | } |
| 358 | if (has_v) print_version(); | 360 | if (has_v) print_version(); |
| 359 | s->status = runargs(L, argv, (script > 0) ? script : s->argc); | 361 | s->ok = runargs(L, argv, (script > 0) ? script : s->argc); |
| 360 | if (s->status != 0) return 0; | 362 | if (!s->ok) return 0; |
| 361 | if (script) | 363 | if (script) |
| 362 | s->status = handle_script(L, argv, script); | 364 | s->ok = (handle_script(L, argv, script) == LUA_OK); |
| 363 | if (s->status != 0) return 0; | 365 | if (!s->ok) return 0; |
| 364 | if (has_i) | 366 | if (has_i) |
| 365 | dotty(L); | 367 | dotty(L); |
| 366 | else if (script == 0 && !has_e && !has_v) { | 368 | else if (script == 0 && !has_e && !has_v) { |
| @@ -387,6 +389,6 @@ int main (int argc, char **argv) { | |||
| 387 | status = lua_cpcall(L, &pmain, &s); | 389 | status = lua_cpcall(L, &pmain, &s); |
| 388 | report(L, status); | 390 | report(L, status); |
| 389 | lua_close(L); | 391 | lua_close(L); |
| 390 | return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS; | 392 | return (s.ok && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 391 | } | 393 | } |
| 392 | 394 | ||
