diff options
Diffstat (limited to 'lua.c')
| -rw-r--r-- | lua.c | 49 |
1 files changed, 37 insertions, 12 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.110 2002/11/25 17:47:13 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.111 2002/12/04 15:38:25 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 | */ |
| @@ -388,19 +388,44 @@ static int handle_luainit (void) { | |||
| 388 | } | 388 | } |
| 389 | 389 | ||
| 390 | 390 | ||
| 391 | int main (int argc, char *argv[]) { | 391 | struct Smain { |
| 392 | int argc; | ||
| 393 | char **argv; | ||
| 394 | int status; | ||
| 395 | }; | ||
| 396 | |||
| 397 | |||
| 398 | static int pmain (lua_State *l) { | ||
| 399 | struct Smain *s = (struct Smain *)lua_touserdata(l, 1); | ||
| 392 | int status; | 400 | int status; |
| 393 | int interactive = 0; | 401 | int interactive = 0; |
| 394 | (void)argc; /* to avoid warnings */ | 402 | progname = s->argv[0]; |
| 395 | progname = argv[0]; | 403 | L = l; |
| 396 | L = lua_open(); /* create state */ | 404 | lua_userinit(l); /* open libraries */ |
| 397 | lua_atpanic(L, l_panic); | ||
| 398 | lua_userinit(L); /* open libraries */ | ||
| 399 | status = handle_luainit(); | 405 | status = handle_luainit(); |
| 400 | if (status != 0) return status; | 406 | if (status == 0) { |
| 401 | status = handle_argv(argv, &interactive); | 407 | status = handle_argv(s->argv, &interactive); |
| 402 | if (status == 0 && interactive) manual_input(); | 408 | if (status == 0 && interactive) manual_input(); |
| 403 | lua_close(L); | 409 | } |
| 404 | return status; | 410 | s->status = status; |
| 411 | return 0; | ||
| 412 | } | ||
| 413 | |||
| 414 | |||
| 415 | int main (int argc, char *argv[]) { | ||
| 416 | int status; | ||
| 417 | struct Smain s; | ||
| 418 | lua_State *l = lua_open(); /* create state */ | ||
| 419 | if (l == NULL) { | ||
| 420 | l_message(argv[0], "cannot create state: not enough memory"); | ||
| 421 | return EXIT_FAILURE; | ||
| 422 | } | ||
| 423 | s.argc = argc; | ||
| 424 | s.argv = argv; | ||
| 425 | lua_atpanic(l, l_panic); | ||
| 426 | status = lua_cpcall(l, &pmain, &s); | ||
| 427 | report(status); | ||
| 428 | lua_close(l); | ||
| 429 | return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS; | ||
| 405 | } | 430 | } |
| 406 | 431 | ||
