diff options
Diffstat (limited to 'lua.c')
| -rw-r--r-- | lua.c | 46 |
1 files changed, 40 insertions, 6 deletions
| @@ -1,10 +1,11 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.12 1997/12/22 20:03:50 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.13 1998/01/19 19:49:49 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 | */ |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | #include <signal.h> | ||
| 8 | #include <stdio.h> | 9 | #include <stdio.h> |
| 9 | #include <stdlib.h> | 10 | #include <stdlib.h> |
| 10 | #include <string.h> | 11 | #include <string.h> |
| @@ -27,6 +28,39 @@ | |||
| 27 | #endif | 28 | #endif |
| 28 | 29 | ||
| 29 | 30 | ||
| 31 | typedef void (*handler)(int); /* type for signal actions */ | ||
| 32 | |||
| 33 | static void laction (int i); | ||
| 34 | |||
| 35 | static handler lreset (void) | ||
| 36 | { | ||
| 37 | lua_linehook = NULL; | ||
| 38 | lua_callhook = NULL; | ||
| 39 | return signal(SIGINT, laction); | ||
| 40 | } | ||
| 41 | |||
| 42 | static void lstop (void) | ||
| 43 | { | ||
| 44 | lreset(); | ||
| 45 | lua_error("interrupted!"); | ||
| 46 | } | ||
| 47 | |||
| 48 | static void laction (int i) | ||
| 49 | { | ||
| 50 | lua_linehook = (lua_LHFunction)lstop; | ||
| 51 | lua_callhook = (lua_CHFunction)lstop; | ||
| 52 | } | ||
| 53 | |||
| 54 | static int ldo (int (*f)(char *), char *name) | ||
| 55 | { | ||
| 56 | int res; | ||
| 57 | handler h = lreset(); | ||
| 58 | res = f(name); /* dostring | dofile */ | ||
| 59 | signal(SIGINT, h); /* restore old action */ | ||
| 60 | return res; | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 30 | static void print_message (void) | 64 | static void print_message (void) |
| 31 | { | 65 | { |
| 32 | fprintf(stderr, | 66 | fprintf(stderr, |
| @@ -85,7 +119,7 @@ static void manual_input (int prompt) | |||
| 85 | else buffer[i++] = c; | 119 | else buffer[i++] = c; |
| 86 | } | 120 | } |
| 87 | buffer[i] = 0; | 121 | buffer[i] = 0; |
| 88 | lua_dostring(buffer); | 122 | ldo(lua_dostring, buffer); |
| 89 | lua_endblock(); | 123 | lua_endblock(); |
| 90 | } | 124 | } |
| 91 | printf("\n"); | 125 | printf("\n"); |
| @@ -106,13 +140,13 @@ int main (int argc, char *argv[]) | |||
| 106 | manual_input(1); | 140 | manual_input(1); |
| 107 | } | 141 | } |
| 108 | else | 142 | else |
| 109 | lua_dofile(NULL); /* executes stdin as a file */ | 143 | ldo(lua_dofile, NULL); /* executes stdin as a file */ |
| 110 | } | 144 | } |
| 111 | else for (i=1; i<argc; i++) { | 145 | else for (i=1; i<argc; i++) { |
| 112 | if (argv[i][0] == '-') { /* option? */ | 146 | if (argv[i][0] == '-') { /* option? */ |
| 113 | switch (argv[i][1]) { | 147 | switch (argv[i][1]) { |
| 114 | case 0: | 148 | case 0: |
| 115 | lua_dofile(NULL); /* executes stdin as a file */ | 149 | ldo(lua_dofile, NULL); /* executes stdin as a file */ |
| 116 | break; | 150 | break; |
| 117 | case 'i': | 151 | case 'i': |
| 118 | manual_input(1); | 152 | manual_input(1); |
| @@ -129,7 +163,7 @@ int main (int argc, char *argv[]) | |||
| 129 | break; | 163 | break; |
| 130 | case 'e': | 164 | case 'e': |
| 131 | i++; | 165 | i++; |
| 132 | if (lua_dostring(argv[i]) != 0) { | 166 | if (ldo(lua_dostring, argv[i]) != 0) { |
| 133 | fprintf(stderr, "lua: error running argument `%s'\n", argv[i]); | 167 | fprintf(stderr, "lua: error running argument `%s'\n", argv[i]); |
| 134 | return 1; | 168 | return 1; |
| 135 | } | 169 | } |
| @@ -142,7 +176,7 @@ int main (int argc, char *argv[]) | |||
| 142 | else if (strchr(argv[i], '=')) | 176 | else if (strchr(argv[i], '=')) |
| 143 | assign(argv[i]); | 177 | assign(argv[i]); |
| 144 | else { | 178 | else { |
| 145 | int result = lua_dofile(argv[i]); | 179 | int result = ldo(lua_dofile, argv[i]); |
| 146 | if (result) { | 180 | if (result) { |
| 147 | if (result == 2) { | 181 | if (result == 2) { |
| 148 | fprintf(stderr, "lua: cannot execute file "); | 182 | fprintf(stderr, "lua: cannot execute file "); |
