diff --git a/lua.c b/lua.c index 4e52ac61..fccf5236 100644 --- a/lua.c +++ b/lua.c @@ -1,10 +1,11 @@ /* -** $Id: lua.c,v 1.12 1997/12/22 20:03:50 roberto Exp roberto $ +** $Id: lua.c,v 1.13 1998/01/19 19:49:49 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ +#include #include #include #include @@ -27,6 +28,39 @@ #endif +typedef void (*handler)(int); /* type for signal actions */ + +static void laction (int i); + +static handler lreset (void) +{ + lua_linehook = NULL; + lua_callhook = NULL; + return signal(SIGINT, laction); +} + +static void lstop (void) +{ + lreset(); + lua_error("interrupted!"); +} + +static void laction (int i) +{ + lua_linehook = (lua_LHFunction)lstop; + lua_callhook = (lua_CHFunction)lstop; +} + +static int ldo (int (*f)(char *), char *name) +{ + int res; + handler h = lreset(); + res = f(name); /* dostring | dofile */ + signal(SIGINT, h); /* restore old action */ + return res; +} + + static void print_message (void) { fprintf(stderr, @@ -85,7 +119,7 @@ static void manual_input (int prompt) else buffer[i++] = c; } buffer[i] = 0; - lua_dostring(buffer); + ldo(lua_dostring, buffer); lua_endblock(); } printf("\n"); @@ -106,13 +140,13 @@ int main (int argc, char *argv[]) manual_input(1); } else - lua_dofile(NULL); /* executes stdin as a file */ + ldo(lua_dofile, NULL); /* executes stdin as a file */ } else for (i=1; i