new way to pass arguments: with option `-f'

This commit is contained in:
Roberto Ierusalimschy 1999-12-21 15:34:23 -02:00
parent cfdebfbc53
commit 3daeabb606
1 changed files with 36 additions and 25 deletions

61
lua.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lua.c,v 1.28 1999/12/06 11:41:28 roberto Exp roberto $ ** $Id: lua.c,v 1.29 1999/12/20 13:03:20 roberto Exp roberto $
** Lua stand-alone interpreter ** Lua stand-alone interpreter
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -74,7 +74,7 @@ static void print_message (void) {
" -q interactive mode without prompt\n" " -q interactive mode without prompt\n"
" -i interactive mode with prompt\n" " -i interactive mode with prompt\n"
" - execute stdin as a file\n" " - execute stdin as a file\n"
" -- start arguments for table `arg'\n" " -f name dofile `name' with following arguments in table `arg'\n"
" a=b set global `a' with string `b'\n" " a=b set global `a' with string `b'\n"
" name dofile `name'\n\n"); " name dofile `name'\n\n");
} }
@ -96,27 +96,34 @@ static void assign (char *arg) {
static void getargs (int argc, char *argv[]) { static void getargs (int argc, char *argv[]) {
lua_beginblock(); { lua_beginblock(); {
int i, j; int i;
lua_Object args = lua_createtable(); lua_Object args = lua_createtable();
lua_pushobject(args); lua_pushobject(args);
lua_setglobal("arg"); lua_setglobal("arg");
for (i=0; i<argc; i++) for (i=0; i<argc; i++) {
if (strcmp(argv[i], "--") == 0) break; /* arg[i] = argv[i] */
for (j = 0; j<argc; j++) { lua_pushobject(args); lua_pushnumber(i);
/* arg[j-i] = argv[j] */ lua_pushstring(argv[i]); lua_settable();
lua_pushobject(args); lua_pushnumber(j-i);
lua_pushstring(argv[j]); lua_settable();
} }
/* arg.n = maximum index in table `arg' */ /* arg.n = maximum index in table `arg' */
lua_pushobject(args); lua_pushstring("n"); lua_pushobject(args); lua_pushstring("n");
lua_pushnumber(argc-(i+1)); lua_settable(); lua_pushnumber(argc-1); lua_settable();
/* arg.nn = minimum index in table `arg' */
lua_pushobject(args); lua_pushstring("nn");
lua_pushnumber(-i); lua_settable();
} lua_endblock(); } lua_endblock();
} }
static void file_input (char **argv, int arg) {
int result = ldo(lua_dofile, argv[arg]);
if (result) {
if (result == 2) {
fprintf(stderr, "lua: cannot execute file ");
perror(argv[arg]);
}
exit(1);
}
}
static void manual_input (int prompt) { static void manual_input (int prompt) {
int cont = 1; int cont = 1;
while (cont) { while (cont) {
@ -157,7 +164,6 @@ int main (int argc, char *argv[]) {
int i; int i;
lua_state = lua_newstate("stack", 1024, "builtin", 1, NULL); lua_state = lua_newstate("stack", 1024, "builtin", 1, NULL);
lua_userinit(); lua_userinit();
getargs(argc, argv);
if (argc < 2) { /* no arguments? */ if (argc < 2) { /* no arguments? */
if (isatty(0)) { if (isatty(0)) {
printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT); printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
@ -187,11 +193,24 @@ int main (int argc, char *argv[]) {
break; break;
case 'e': case 'e':
i++; i++;
if (i>=argc) {
print_message();
exit(1);
}
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]);
return 1; exit(1);
} }
break; break;
case 'f':
i++;
if (i>=argc) {
print_message();
exit(1);
}
getargs(argc-i, argv+i); /* collect following arguments */
file_input(argv, i);
return 0; /* stop running arguments */
case '-': case '-':
i = argc; /* end loop */ i = argc; /* end loop */
break; break;
@ -202,16 +221,8 @@ int main (int argc, char *argv[]) {
} }
else if (strchr(argv[i], '=')) else if (strchr(argv[i], '='))
assign(argv[i]); assign(argv[i]);
else { else
int result = ldo(lua_dofile, argv[i]); file_input(argv, i);
if (result) {
if (result == 2) {
fprintf(stderr, "lua: cannot execute file ");
perror(argv[i]);
}
exit(1);
}
}
} }
#ifdef DEBUG #ifdef DEBUG
lua_close(); lua_close();