new option `-c' to close lua.

This commit is contained in:
Roberto Ierusalimschy 2000-05-10 14:00:21 -03:00
parent dc1e4f5073
commit 8ac0bbf64b
1 changed files with 16 additions and 11 deletions

27
lua.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lua.c,v 1.36 2000/03/30 17:19:48 roberto Exp roberto $ ** $Id: lua.c,v 1.37 2000/04/14 17:46:29 roberto Exp roberto $
** Lua stand-alone interpreter ** Lua stand-alone interpreter
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -69,6 +69,7 @@ static void print_message (void) {
fprintf(stderr, fprintf(stderr,
"usage: lua [options]. Available options are:\n" "usage: lua [options]. Available options are:\n"
" - execute stdin as a file\n" " - execute stdin as a file\n"
" -c close lua when exiting\n"
" -d turn debug on\n" " -d turn debug on\n"
" -e stat execute string `stat'\n" " -e stat execute string `stat'\n"
" -f name execute file `name' with remaining arguments in table `arg'\n" " -f name execute file `name' with remaining arguments in table `arg'\n"
@ -166,12 +167,14 @@ static void manual_input (int version, int prompt) {
int main (int argc, char *argv[]) { int main (int argc, char *argv[]) {
int toclose = 0;
int status = EXIT_SUCCESS;
int i = 1; int i = 1;
if (i < argc && argv[1][0] == '-' && argv[1][1] == 's') { if (i < argc && argv[1][0] == '-' && argv[1][1] == 's') {
int stacksize = atoi(&argv[1][2]); int stacksize = atoi(&argv[1][2]);
if (stacksize == 0) { if (stacksize == 0) {
fprintf(stderr, "lua: invalid stack size ('%s')\n", &argv[1][2]); fprintf(stderr, "lua: invalid stack size ('%s')\n", &argv[1][2]);
exit(1); exit(EXIT_FAILURE);
} }
i++; i++;
lua_state = lua_newstate("stack", stacksize, NULL); lua_state = lua_newstate("stack", stacksize, NULL);
@ -207,6 +210,9 @@ int main (int argc, char *argv[]) {
manual_input(1, 1); manual_input(1, 1);
} }
break; break;
case 'c':
toclose = 1;
break;
case 'v': case 'v':
print_version(); print_version();
break; break;
@ -214,18 +220,18 @@ int main (int argc, char *argv[]) {
i++; i++;
if (i >= argc) { if (i >= argc) {
print_message(); print_message();
exit(1); status = EXIT_FAILURE; goto endloop;
} }
if (ldo(lua_dostring, argv[i]) != 0) { if (ldo(lua_dostring, argv[i]) != 0) {
fprintf(stderr, "lua: error running argument `%s'\n", argv[i]); fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
exit(1); status = EXIT_FAILURE; goto endloop;
} }
break; break;
case 'f': case 'f':
i++; i++;
if (i >= argc) { if (i >= argc) {
print_message(); print_message();
exit(1); status = EXIT_FAILURE; goto endloop;
} }
lua_pushobject(getargs(argv+i)); /* collect remaining arguments */ lua_pushobject(getargs(argv+i)); /* collect remaining arguments */
lua_setglobal("arg"); lua_setglobal("arg");
@ -234,10 +240,10 @@ int main (int argc, char *argv[]) {
break; break;
case 's': case 's':
fprintf(stderr, "lua: stack size (`-s') must be the first option\n"); fprintf(stderr, "lua: stack size (`-s') must be the first option\n");
exit(1); status = EXIT_FAILURE; goto endloop;
default: default:
print_message(); print_message();
exit(1); status = EXIT_FAILURE; goto endloop;
} }
} }
else if (strchr(argv[i], '=')) else if (strchr(argv[i], '='))
@ -246,9 +252,8 @@ int main (int argc, char *argv[]) {
file_input(argv[i]); file_input(argv[i]);
} }
endloop: endloop:
#ifdef DEBUG if (toclose)
lua_close(); lua_close();
#endif return status;
return 0;
} }