1
0
mirror of https://github.com/lua/lua.git synced 2025-01-28 06:03:00 +08:00

several small changes (mainly function names)

This commit is contained in:
Roberto Ierusalimschy 2004-07-13 16:56:44 -03:00
parent 4206d7ed60
commit a535ad6fee

143
lua.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lua.c,v 1.128 2004/06/17 14:06:52 roberto Exp roberto $ ** $Id: lua.c,v 1.129 2004/07/01 14:26:28 roberto Exp roberto $
** Lua stand-alone interpreter ** Lua stand-alone interpreter
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -28,38 +28,23 @@
static lua_State *L = NULL; static lua_State *globalL = NULL;
static const char *progname = PROGNAME; static const char *progname = PROGNAME;
static const luaL_reg lualibs[] = { static void lstop (lua_State *L, lua_Debug *ar) {
{"base", luaopen_base},
{"table", luaopen_table},
{"io", luaopen_io},
{"string", luaopen_string},
{"math", luaopen_math},
{"debug", luaopen_debug},
{"loadlib", luaopen_loadlib},
/* add your libraries here */
LUA_EXTRALIBS
{NULL, NULL}
};
static void lstop (lua_State *l, lua_Debug *ar) {
(void)ar; /* unused arg. */ (void)ar; /* unused arg. */
lua_sethook(l, NULL, 0, 0); lua_sethook(L, NULL, 0, 0);
luaL_error(l, "interrupted!"); luaL_error(L, "interrupted!");
} }
static void laction (int i) { static void laction (int i) {
signal(i, SIG_DFL); /* if another SIGINT happens before lstop, signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
terminate process (default action) */ terminate process (default action) */
lua_sethook(L, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
} }
@ -83,7 +68,7 @@ static void l_message (const char *pname, const char *msg) {
} }
static int report (int status) { static int report (lua_State *L, int status) {
if (status && !lua_isnil(L, -1)) { if (status && !lua_isnil(L, -1)) {
const char *msg = lua_tostring(L, -1); const char *msg = lua_tostring(L, -1);
if (msg == NULL) msg = "(error object is not a string)"; if (msg == NULL) msg = "(error object is not a string)";
@ -94,7 +79,7 @@ static int report (int status) {
} }
static int lcall (int narg, int clear) { static int docall (lua_State *L, int narg, int clear) {
int status; int status;
int base = lua_gettop(L) - narg; /* function index */ int base = lua_gettop(L) - narg; /* function index */
lua_pushliteral(L, "_TRACEBACK"); lua_pushliteral(L, "_TRACEBACK");
@ -113,7 +98,7 @@ static void print_version (void) {
} }
static int getargs (char *argv[], int n) { static int getargs (lua_State *L, char *argv[], int n) {
int i, narg; int i, narg;
for (i=n+1; argv[i]; i++) { for (i=n+1; argv[i]; i++) {
luaL_checkstack(L, 1, "too many arguments to script"); luaL_checkstack(L, 1, "too many arguments to script");
@ -130,22 +115,22 @@ static int getargs (char *argv[], int n) {
} }
static int file_input (const char *name) { static int dofile (lua_State *L, const char *name) {
int status = luaL_loadfile(L, name) || lcall(0, 1); int status = luaL_loadfile(L, name) || docall(L, 0, 1);
return report(status); return report(L, status);
} }
static int dostring (const char *s, const char *name) { static int dostring (lua_State *L, const char *s, const char *name) {
int status = luaL_loadbuffer(L, s, strlen(s), name) || lcall(0, 1); int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
return report(status); return report(L, status);
} }
static int load_file (const char *name) { static int dolibrary (lua_State *L, const char *name) {
name = luaL_searchpath(L, name, NULL); name = luaL_searchpath(L, name, NULL);
if (name == NULL) return report(1); if (name == NULL) return report(L, 1);
else return file_input(name); else return dofile(L, name);
} }
@ -163,7 +148,7 @@ static int load_file (const char *name) {
#endif #endif
static int readline (lua_State *l, const char *prompt) { static int readline (lua_State *L, const char *prompt) {
static char buffer[MAXINPUT]; static char buffer[MAXINPUT];
if (prompt) { if (prompt) {
fputs(prompt, stdout); fputs(prompt, stdout);
@ -172,7 +157,7 @@ static int readline (lua_State *l, const char *prompt) {
if (fgets(buffer, sizeof(buffer), stdin) == NULL) if (fgets(buffer, sizeof(buffer), stdin) == NULL)
return 0; /* read fails */ return 0; /* read fails */
else { else {
lua_pushstring(l, buffer); lua_pushstring(L, buffer);
return 1; return 1;
} }
} }
@ -180,7 +165,7 @@ static int readline (lua_State *l, const char *prompt) {
#endif #endif
static const char *get_prompt (int firstline) { static const char *get_prompt (lua_State *L, int firstline) {
const char *p = NULL; const char *p = NULL;
lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2"); lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");
lua_rawget(L, LUA_GLOBALSINDEX); lua_rawget(L, LUA_GLOBALSINDEX);
@ -191,7 +176,7 @@ static const char *get_prompt (int firstline) {
} }
static int incomplete (int status) { static int incomplete (lua_State *L, int status) {
if (status == LUA_ERRSYNTAX && if (status == LUA_ERRSYNTAX &&
strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) { strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {
lua_pop(L, 1); lua_pop(L, 1);
@ -202,10 +187,10 @@ static int incomplete (int status) {
} }
static int load_string (void) { static int loadline (lua_State *L) {
int status; int status;
lua_settop(L, 0); lua_settop(L, 0);
if (lua_readline(L, get_prompt(1)) == 0) /* no input? */ if (lua_readline(L, get_prompt(L, 1)) == 0) /* no input? */
return -1; return -1;
if (lua_tostring(L, -1)[0] == '=') { /* line starts with `=' ? */ if (lua_tostring(L, -1)[0] == '=') { /* line starts with `=' ? */
lua_pushfstring(L, "return %s", lua_tostring(L, -1)+1);/* `=' -> `return' */ lua_pushfstring(L, "return %s", lua_tostring(L, -1)+1);/* `=' -> `return' */
@ -213,8 +198,8 @@ static int load_string (void) {
} }
for (;;) { /* repeat until gets a complete line */ for (;;) { /* repeat until gets a complete line */
status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
if (!incomplete(status)) break; /* cannot try to add lines? */ if (!incomplete(L, status)) break; /* cannot try to add lines? */
if (lua_readline(L, get_prompt(0)) == 0) /* no more input? */ if (lua_readline(L, get_prompt(L, 0)) == 0) /* no more input? */
return -1; return -1;
lua_concat(L, lua_gettop(L)); /* join lines */ lua_concat(L, lua_gettop(L)); /* join lines */
} }
@ -224,13 +209,14 @@ static int load_string (void) {
} }
static void manual_input (void) { static void dotty (lua_State *L) {
int status; int status;
const char *oldprogname = progname; const char *oldprogname = progname;
progname = NULL; progname = NULL;
while ((status = load_string()) != -1) { print_version();
if (status == 0) status = lcall(0, 0); while ((status = loadline(L)) != -1) {
report(status); if (status == 0) status = docall(L, 0, 0);
report(L, 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");
lua_insert(L, 1); lua_insert(L, 1);
@ -245,25 +231,23 @@ static void manual_input (void) {
} }
static int checkvar (lua_State *l) { static int checkvar (lua_State *L) {
const char *name = lua_tostring(l, 2); const char *name = lua_tostring(L, 2);
if (name) if (name)
luaL_error(l, "attempt to access undefined variable `%s'", name); luaL_error(L, "attempt to access undefined variable `%s'", name);
return 0; return 0;
} }
#define clearinteractive(i) (*i &= 2) #define clearinteractive(i) (*i &= 2)
static int handle_argv (char *argv[], int *interactive) { static int handle_argv (lua_State *L, char *argv[], int *interactive) {
if (argv[1] == NULL) { /* no arguments? */ if (argv[1] == NULL) { /* no arguments? */
*interactive = 0; *interactive = 0;
if (stdin_is_tty()) { if (stdin_is_tty())
print_version(); dotty(L);
manual_input();
}
else else
file_input(NULL); /* executes stdin as a file */ dofile(L, NULL); /* executes stdin as a file */
} }
else { /* other arguments; loop over them */ else { /* other arguments; loop over them */
int i; int i;
@ -280,7 +264,7 @@ static int handle_argv (char *argv[], int *interactive) {
} }
case '\0': { case '\0': {
clearinteractive(interactive); clearinteractive(interactive);
file_input(NULL); /* executes stdin as a file */ dofile(L, NULL); /* executes stdin as a file */
break; break;
} }
case 'i': { case 'i': {
@ -307,7 +291,7 @@ static int handle_argv (char *argv[], int *interactive) {
print_usage(); print_usage();
return 1; return 1;
} }
if (dostring(chunk, "=<command line>") != 0) if (dostring(L, chunk, "=<command line>") != 0)
return 1; return 1;
break; break;
} }
@ -318,7 +302,7 @@ static int handle_argv (char *argv[], int *interactive) {
print_usage(); print_usage();
return 1; return 1;
} }
if (load_file(filename)) if (dolibrary(L, filename))
return 1; /* stop if file fails */ return 1; /* stop if file fails */
break; break;
} }
@ -331,39 +315,30 @@ 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];
int narg = getargs(argv, i); /* collect arguments */ int narg = getargs(L, argv, i); /* collect arguments */
int status; int status;
lua_setglobal(L, "arg"); lua_setglobal(L, "arg");
clearinteractive(interactive); clearinteractive(interactive);
status = luaL_loadfile(L, filename); status = luaL_loadfile(L, filename);
lua_insert(L, -(narg+1)); lua_insert(L, -(narg+1));
if (status == 0) if (status == 0)
status = lcall(narg, 0); status = docall(L, narg, 0);
else else
lua_pop(L, narg); lua_pop(L, narg);
return report(status); return report(L, status);
} }
} }
return 0; return 0;
} }
static void openstdlibs (lua_State *l) { static int handle_luainit (lua_State *L) {
const luaL_reg *lib = lualibs;
for (; lib->func; lib++) {
lib->func(l); /* open library */
lua_settop(l, 0); /* discard any results */
}
}
static int handle_luainit (void) {
const char *init = getenv("LUA_INIT"); const char *init = getenv("LUA_INIT");
if (init == NULL) return 0; /* status OK */ if (init == NULL) return 0; /* status OK */
else if (init[0] == '@') else if (init[0] == '@')
return file_input(init+1); return dofile(L, init+1);
else else
return dostring(init, "=LUA_INIT"); return dostring(L, init, "=LUA_INIT");
} }
@ -374,17 +349,17 @@ struct Smain {
}; };
static int pmain (lua_State *l) { static int pmain (lua_State *L) {
struct Smain *s = (struct Smain *)lua_touserdata(l, 1); struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
int status; int status;
int interactive = 1; int interactive = 1;
if (s->argv[0] && s->argv[0][0]) progname = s->argv[0]; if (s->argv[0] && s->argv[0][0]) progname = s->argv[0];
L = l; globalL = L;
lua_userinit(l); /* open libraries */ lua_userinit(L); /* open libraries */
status = handle_luainit(); status = handle_luainit(L);
if (status == 0) { if (status == 0) {
status = handle_argv(s->argv, &interactive); status = handle_argv(L, s->argv, &interactive);
if (status == 0 && interactive) manual_input(); if (status == 0 && interactive) dotty(L);
} }
s->status = status; s->status = status;
return 0; return 0;
@ -394,16 +369,16 @@ static int pmain (lua_State *l) {
int main (int argc, char *argv[]) { int main (int argc, char *argv[]) {
int status; int status;
struct Smain s; struct Smain s;
lua_State *l = lua_open(); /* create state */ lua_State *L = lua_open(); /* create state */
if (l == NULL) { if (L == NULL) {
l_message(argv[0], "cannot create state: not enough memory"); l_message(argv[0], "cannot create state: not enough memory");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
s.argc = argc; s.argc = argc;
s.argv = argv; s.argv = argv;
status = lua_cpcall(l, &pmain, &s); status = lua_cpcall(L, &pmain, &s);
report(status); report(L, status);
lua_close(l); lua_close(L);
return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS; return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
} }