mirror of https://github.com/rusefi/lua.git
option -l does a `require', instead of `dofile'
This commit is contained in:
parent
642af82e81
commit
c8a79057f7
45
lua.c
45
lua.c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
** $Id: lua.c,v 1.107 2002/11/11 13:28:06 roberto Exp roberto $
|
** $Id: lua.c,v 1.108 2002/11/14 15:42:05 roberto Exp roberto $
|
||||||
** Lua stand-alone interpreter
|
** Lua stand-alone interpreter
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -98,8 +98,8 @@ static void print_usage (void) {
|
||||||
" - execute stdin as a file\n"
|
" - execute stdin as a file\n"
|
||||||
" -e stat execute string `stat'\n"
|
" -e stat execute string `stat'\n"
|
||||||
" -i enter interactive mode after executing `script'\n"
|
" -i enter interactive mode after executing `script'\n"
|
||||||
" -l name execute file `name'\n"
|
" -l name load and run library `name'\n"
|
||||||
" -v print version information\n"
|
" -v show version information\n"
|
||||||
" -- stop handling options\n" ,
|
" -- stop handling options\n" ,
|
||||||
progname);
|
progname);
|
||||||
}
|
}
|
||||||
|
@ -111,7 +111,7 @@ static void l_message (const char *pname, const char *msg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void report (int status) {
|
static int report (int status) {
|
||||||
const char *msg;
|
const char *msg;
|
||||||
if (status) {
|
if (status) {
|
||||||
msg = lua_tostring(L, -1);
|
msg = lua_tostring(L, -1);
|
||||||
|
@ -119,20 +119,19 @@ static void report (int status) {
|
||||||
l_message(progname, msg);
|
l_message(progname, msg);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int lcall (int clear) {
|
static int lcall (int narg, int clear) {
|
||||||
int status;
|
int status;
|
||||||
int top = lua_gettop(L);
|
int base = lua_gettop(L) - narg; /* function index */
|
||||||
lua_getglobal(L, "_TRACEBACK"); /* get traceback function */
|
lua_getglobal(L, "_TRACEBACK"); /* get traceback function */
|
||||||
lua_insert(L, top); /* put it under chunk */
|
lua_insert(L, base); /* put it under chunk and args */
|
||||||
signal(SIGINT, laction);
|
signal(SIGINT, laction);
|
||||||
status = lua_pcall(L, 0, LUA_MULTRET, -2);
|
status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
|
||||||
signal(SIGINT, SIG_DFL);
|
signal(SIGINT, SIG_DFL);
|
||||||
lua_remove(L, top); /* remove traceback function */
|
lua_remove(L, base); /* remove traceback function */
|
||||||
if (status == 0 && clear)
|
|
||||||
lua_settop(L, top); /* remove eventual results */
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,9 +164,8 @@ static void getargs (char *argv[], int n) {
|
||||||
|
|
||||||
|
|
||||||
static int docall (int status) {
|
static int docall (int status) {
|
||||||
if (status == 0) status = lcall(1);
|
if (status == 0) status = lcall(0, 1);
|
||||||
report(status);
|
return report(status);
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -181,6 +179,19 @@ static int dostring (const char *s, const char *name) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int load_file (const char *name) {
|
||||||
|
lua_getglobal(L, "require");
|
||||||
|
if (!lua_isfunction(L, -1)) { /* no `require' defined? */
|
||||||
|
lua_pop(L, 1);
|
||||||
|
return file_input(name);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
lua_pushstring(L, name);
|
||||||
|
return report(lcall(1, 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** this macro can be used by some `history' system to save lines
|
** this macro can be used by some `history' system to save lines
|
||||||
** read in manual input
|
** read in manual input
|
||||||
|
@ -268,7 +279,7 @@ static void manual_input (void) {
|
||||||
const char *oldprogname = progname;
|
const char *oldprogname = progname;
|
||||||
progname = NULL;
|
progname = NULL;
|
||||||
while ((status = load_string()) != -1) {
|
while ((status = load_string()) != -1) {
|
||||||
if (status == 0) status = lcall(0);
|
if (status == 0) status = lcall(0, 0);
|
||||||
report(status);
|
report(status);
|
||||||
if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
|
if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
|
||||||
lua_getglobal(L, "print");
|
lua_getglobal(L, "print");
|
||||||
|
@ -331,7 +342,7 @@ static int handle_argv (char *argv[], int *interactive) {
|
||||||
print_usage();
|
print_usage();
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
if (file_input(filename))
|
if (load_file(filename))
|
||||||
return EXIT_FAILURE; /* stop if file fails */
|
return EXIT_FAILURE; /* stop if file fails */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -351,7 +362,7 @@ static int handle_argv (char *argv[], int *interactive) {
|
||||||
} endloop:
|
} endloop:
|
||||||
if (argv[i] != NULL) {
|
if (argv[i] != NULL) {
|
||||||
const char *filename = argv[i];
|
const char *filename = argv[i];
|
||||||
getargs(argv, i); /* collect remaining arguments */
|
getargs(argv, i); /* collect arguments */
|
||||||
lua_setglobal(L, "arg");
|
lua_setglobal(L, "arg");
|
||||||
return file_input(filename); /* stop scanning arguments */
|
return file_input(filename); /* stop scanning arguments */
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue