summaryrefslogtreecommitdiff
path: root/lua.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-02-11 18:56:05 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-02-11 18:56:05 -0200
commita9dd2c67176287741a917db020eb797f62d65040 (patch)
tree68422bb25b7f659919dba20bcc905106d35d391e /lua.c
parentaee3f97acb3efd5e020f59ed89c9036c58721850 (diff)
downloadlua-a9dd2c67176287741a917db020eb797f62d65040.tar.gz
lua-a9dd2c67176287741a917db020eb797f62d65040.tar.bz2
lua-a9dd2c67176287741a917db020eb797f62d65040.zip
interrupts lua loops with "^C" (via signals)
Diffstat (limited to 'lua.c')
-rw-r--r--lua.c46
1 files changed, 40 insertions, 6 deletions
diff --git a/lua.c b/lua.c
index 4e52ac61..fccf5236 100644
--- a/lua.c
+++ b/lua.c
@@ -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
31typedef void (*handler)(int); /* type for signal actions */
32
33static void laction (int i);
34
35static handler lreset (void)
36{
37 lua_linehook = NULL;
38 lua_callhook = NULL;
39 return signal(SIGINT, laction);
40}
41
42static void lstop (void)
43{
44 lreset();
45 lua_error("interrupted!");
46}
47
48static void laction (int i)
49{
50 lua_linehook = (lua_LHFunction)lstop;
51 lua_callhook = (lua_CHFunction)lstop;
52}
53
54static 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
30static void print_message (void) 64static 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 ");