aboutsummaryrefslogtreecommitdiff
path: root/lua.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-06-18 14:12:05 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-06-18 14:12:05 -0300
commita44f37513becb25d0595df1e714851870b50b6dd (patch)
treef1db3fe9656f244992a9678f3e860cd24fe29718 /lua.c
parent6ee2dbdfe94e55cc098646df6aaee0483b5fff2c (diff)
downloadlua-a44f37513becb25d0595df1e714851870b50b6dd.tar.gz
lua-a44f37513becb25d0595df1e714851870b50b6dd.tar.bz2
lua-a44f37513becb25d0595df1e714851870b50b6dd.zip
better treatment for error messages
Diffstat (limited to 'lua.c')
-rw-r--r--lua.c44
1 files changed, 24 insertions, 20 deletions
diff --git a/lua.c b/lua.c
index 27ef515b..dbbd09a2 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.89 2002/06/03 20:11:41 roberto Exp roberto $ 2** $Id: lua.c,v 1.90 2002/06/06 12:40:22 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*/
@@ -24,10 +24,6 @@ static int isatty (int x) { return x==0; } /* assume stdin is a tty */
24#endif 24#endif
25 25
26 26
27#ifndef LUA_PROGNAME
28#define LUA_PROGNAME "lua"
29#endif
30
31 27
32#ifndef PROMPT 28#ifndef PROMPT
33#define PROMPT "> " 29#define PROMPT "> "
@@ -46,6 +42,8 @@ static int isatty (int x) { return x==0; } /* assume stdin is a tty */
46 42
47static lua_State *L = NULL; 43static lua_State *L = NULL;
48 44
45static const char *progname;
46
49 47
50static lua_Hook old_linehook = NULL; 48static lua_Hook old_linehook = NULL;
51static lua_Hook old_callhook = NULL; 49static lua_Hook old_callhook = NULL;
@@ -54,7 +52,7 @@ static lua_Hook old_callhook = NULL;
54static void lstop (void) { 52static void lstop (void) {
55 lua_setlinehook(L, old_linehook); 53 lua_setlinehook(L, old_linehook);
56 lua_setcallhook(L, old_callhook); 54 lua_setcallhook(L, old_callhook);
57 lua_error(L, "interrupted!"); 55 luaL_error(L, "interrupted!");
58} 56}
59 57
60 58
@@ -68,9 +66,15 @@ static void laction (int i) {
68 66
69static void report (int status) { 67static void report (int status) {
70 if (status) { 68 if (status) {
69 if (status == LUA_ERRRUN) {
70 if (lua_isstring(L, -2) && lua_isstring(L, -1))
71 lua_concat(L, 2); /* concat error message and traceback */
72 else
73 lua_remove(L, -2); /* lease only traceback on stack */
74 }
71 lua_getglobal(L, "_ALERT"); 75 lua_getglobal(L, "_ALERT");
72 lua_pushvalue(L, -2); 76 lua_pushvalue(L, -2);
73 lua_pcall(L, 1, 0, 0); 77 lua_pcall(L, 1, 0);
74 lua_pop(L, 1); 78 lua_pop(L, 1);
75 } 79 }
76} 80}
@@ -79,15 +83,11 @@ static void report (int status) {
79static int lcall (int clear) { 83static int lcall (int clear) {
80 int status; 84 int status;
81 int top = lua_gettop(L); 85 int top = lua_gettop(L);
82 lua_getglobal(L, "_ERRORMESSAGE");
83 lua_insert(L, top);
84 signal(SIGINT, laction); 86 signal(SIGINT, laction);
85 status = lua_pcall(L, 0, LUA_MULTRET, top); 87 status = lua_pcall(L, 0, LUA_MULTRET);
86 signal(SIGINT, SIG_DFL); 88 signal(SIGINT, SIG_DFL);
87 if (status == 0) { 89 if (status == 0 && clear)
88 if (clear) lua_settop(L, top); /* remove eventual results */ 90 lua_settop(L, top); /* remove eventual results */
89 else lua_remove(L, top); /* else remove only error function */
90 }
91 return status; 91 return status;
92} 92}
93 93
@@ -103,7 +103,7 @@ static void print_usage (void) {
103 " -v print version information\n" 103 " -v print version information\n"
104 " a=b set global `a' to string `b'\n" 104 " a=b set global `a' to string `b'\n"
105 " name execute file `name'\n", 105 " name execute file `name'\n",
106 LUA_PROGNAME); 106 progname);
107} 107}
108 108
109 109
@@ -144,8 +144,8 @@ static void getargs (char *argv[]) {
144 144
145 145
146static int l_alert (lua_State *l) { 146static int l_alert (lua_State *l) {
147 fputs(luaL_check_string(l, 1), stderr); 147 if (progname) fprintf(stderr, "%s: ", progname);
148 putc('\n', stderr); 148 fprintf(stderr, "%s\n", luaL_check_string(l, 1));
149 return 0; 149 return 0;
150} 150}
151 151
@@ -248,6 +248,8 @@ static int load_string (void) {
248 248
249static void manual_input (int version) { 249static void manual_input (int version) {
250 int status; 250 int status;
251 const char *oldprogname = progname;
252 progname = NULL;
251 if (version) print_version(); 253 if (version) print_version();
252 while ((status = load_string()) != -1) { 254 while ((status = load_string()) != -1) {
253 if (status == 0) status = lcall(0); 255 if (status == 0) status = lcall(0);
@@ -255,10 +257,11 @@ static void manual_input (int version) {
255 if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */ 257 if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
256 lua_getglobal(L, "print"); 258 lua_getglobal(L, "print");
257 lua_insert(L, 1); 259 lua_insert(L, 1);
258 lua_call(L, lua_gettop(L)-1, 0); 260 lua_pcall(L, lua_gettop(L)-1, 0);
259 } 261 }
260 } 262 }
261 printf("\n"); 263 printf("\n");
264 progname = oldprogname;
262} 265}
263 266
264 267
@@ -305,7 +308,7 @@ static int handle_argv (char *argv[], int *toclose) {
305 } 308 }
306 if (dostring(argv[i], "=prog. argument") != 0) { 309 if (dostring(argv[i], "=prog. argument") != 0) {
307 fprintf(stderr, "%s: error running argument `%.99s'\n", 310 fprintf(stderr, "%s: error running argument `%.99s'\n",
308 LUA_PROGNAME, argv[i]); 311 progname, argv[i]);
309 return EXIT_FAILURE; 312 return EXIT_FAILURE;
310 } 313 }
311 break; 314 break;
@@ -323,7 +326,7 @@ static int handle_argv (char *argv[], int *toclose) {
323 case 's': { 326 case 's': {
324 fprintf(stderr, 327 fprintf(stderr,
325 "%s: option `-s' is deprecated (dynamic stack now)\n", 328 "%s: option `-s' is deprecated (dynamic stack now)\n",
326 LUA_PROGNAME); 329 progname);
327 break; 330 break;
328 } 331 }
329 default: { 332 default: {
@@ -369,6 +372,7 @@ int main (int argc, char *argv[]) {
369 int status; 372 int status;
370 int toclose = 0; 373 int toclose = 0;
371 (void)argc; /* to avoid warnings */ 374 (void)argc; /* to avoid warnings */
375 progname = argv[0];
372 L = lua_open(); /* create state */ 376 L = lua_open(); /* create state */
373 lua_atpanic(L, l_panic); 377 lua_atpanic(L, l_panic);
374 LUA_USERINIT(L); /* open libraries */ 378 LUA_USERINIT(L); /* open libraries */