diff options
Diffstat (limited to 'lua.c')
| -rw-r--r-- | lua.c | 45 |
1 files changed, 28 insertions, 17 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.107 2002/11/11 13:28:06 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.108 2002/11/14 15:42:05 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 | */ |
| @@ -98,8 +98,8 @@ static void print_usage (void) { | |||
| 98 | " - execute stdin as a file\n" | 98 | " - execute stdin as a file\n" |
| 99 | " -e stat execute string `stat'\n" | 99 | " -e stat execute string `stat'\n" |
| 100 | " -i enter interactive mode after executing `script'\n" | 100 | " -i enter interactive mode after executing `script'\n" |
| 101 | " -l name execute file `name'\n" | 101 | " -l name load and run library `name'\n" |
| 102 | " -v print version information\n" | 102 | " -v show version information\n" |
| 103 | " -- stop handling options\n" , | 103 | " -- stop handling options\n" , |
| 104 | progname); | 104 | progname); |
| 105 | } | 105 | } |
| @@ -111,7 +111,7 @@ static void l_message (const char *pname, const char *msg) { | |||
| 111 | } | 111 | } |
| 112 | 112 | ||
| 113 | 113 | ||
| 114 | static void report (int status) { | 114 | static int report (int status) { |
| 115 | const char *msg; | 115 | const char *msg; |
| 116 | if (status) { | 116 | if (status) { |
| 117 | msg = lua_tostring(L, -1); | 117 | msg = lua_tostring(L, -1); |
| @@ -119,20 +119,19 @@ static void report (int status) { | |||
| 119 | l_message(progname, msg); | 119 | l_message(progname, msg); |
| 120 | lua_pop(L, 1); | 120 | lua_pop(L, 1); |
| 121 | } | 121 | } |
| 122 | return status; | ||
| 122 | } | 123 | } |
| 123 | 124 | ||
| 124 | 125 | ||
| 125 | static int lcall (int clear) { | 126 | static int lcall (int narg, int clear) { |
| 126 | int status; | 127 | int status; |
| 127 | int top = lua_gettop(L); | 128 | int base = lua_gettop(L) - narg; /* function index */ |
| 128 | lua_getglobal(L, "_TRACEBACK"); /* get traceback function */ | 129 | lua_getglobal(L, "_TRACEBACK"); /* get traceback function */ |
| 129 | lua_insert(L, top); /* put it under chunk */ | 130 | lua_insert(L, base); /* put it under chunk and args */ |
| 130 | signal(SIGINT, laction); | 131 | signal(SIGINT, laction); |
| 131 | status = lua_pcall(L, 0, LUA_MULTRET, -2); | 132 | status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); |
| 132 | signal(SIGINT, SIG_DFL); | 133 | signal(SIGINT, SIG_DFL); |
| 133 | lua_remove(L, top); /* remove traceback function */ | 134 | lua_remove(L, base); /* remove traceback function */ |
| 134 | if (status == 0 && clear) | ||
| 135 | lua_settop(L, top); /* remove eventual results */ | ||
| 136 | return status; | 135 | return status; |
| 137 | } | 136 | } |
| 138 | 137 | ||
| @@ -165,9 +164,8 @@ static void getargs (char *argv[], int n) { | |||
| 165 | 164 | ||
| 166 | 165 | ||
| 167 | static int docall (int status) { | 166 | static int docall (int status) { |
| 168 | if (status == 0) status = lcall(1); | 167 | if (status == 0) status = lcall(0, 1); |
| 169 | report(status); | 168 | return report(status); |
| 170 | return status; | ||
| 171 | } | 169 | } |
| 172 | 170 | ||
| 173 | 171 | ||
| @@ -181,6 +179,19 @@ static int dostring (const char *s, const char *name) { | |||
| 181 | } | 179 | } |
| 182 | 180 | ||
| 183 | 181 | ||
| 182 | static int load_file (const char *name) { | ||
| 183 | lua_getglobal(L, "require"); | ||
| 184 | if (!lua_isfunction(L, -1)) { /* no `require' defined? */ | ||
| 185 | lua_pop(L, 1); | ||
| 186 | return file_input(name); | ||
| 187 | } | ||
| 188 | else { | ||
| 189 | lua_pushstring(L, name); | ||
| 190 | return report(lcall(1, 1)); | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | |||
| 184 | /* | 195 | /* |
| 185 | ** this macro can be used by some `history' system to save lines | 196 | ** this macro can be used by some `history' system to save lines |
| 186 | ** read in manual input | 197 | ** read in manual input |
| @@ -268,7 +279,7 @@ static void manual_input (void) { | |||
| 268 | const char *oldprogname = progname; | 279 | const char *oldprogname = progname; |
| 269 | progname = NULL; | 280 | progname = NULL; |
| 270 | while ((status = load_string()) != -1) { | 281 | while ((status = load_string()) != -1) { |
| 271 | if (status == 0) status = lcall(0); | 282 | if (status == 0) status = lcall(0, 0); |
| 272 | report(status); | 283 | report(status); |
| 273 | if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */ | 284 | if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */ |
| 274 | lua_getglobal(L, "print"); | 285 | lua_getglobal(L, "print"); |
| @@ -331,7 +342,7 @@ static int handle_argv (char *argv[], int *interactive) { | |||
| 331 | print_usage(); | 342 | print_usage(); |
| 332 | return EXIT_FAILURE; | 343 | return EXIT_FAILURE; |
| 333 | } | 344 | } |
| 334 | if (file_input(filename)) | 345 | if (load_file(filename)) |
| 335 | return EXIT_FAILURE; /* stop if file fails */ | 346 | return EXIT_FAILURE; /* stop if file fails */ |
| 336 | break; | 347 | break; |
| 337 | } | 348 | } |
| @@ -351,7 +362,7 @@ static int handle_argv (char *argv[], int *interactive) { | |||
| 351 | } endloop: | 362 | } endloop: |
| 352 | if (argv[i] != NULL) { | 363 | if (argv[i] != NULL) { |
| 353 | const char *filename = argv[i]; | 364 | const char *filename = argv[i]; |
| 354 | getargs(argv, i); /* collect remaining arguments */ | 365 | getargs(argv, i); /* collect arguments */ |
| 355 | lua_setglobal(L, "arg"); | 366 | lua_setglobal(L, "arg"); |
| 356 | return file_input(filename); /* stop scanning arguments */ | 367 | return file_input(filename); /* stop scanning arguments */ |
| 357 | } | 368 | } |
