interrupts lua loops with "^C" (via signals)
This commit is contained in:
46
lua.c
46
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
|
** Lua stand-alone interpreter
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -27,6 +28,39 @@
|
|||||||
#endif
|
#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)
|
static void print_message (void)
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
@@ -85,7 +119,7 @@ static void manual_input (int prompt)
|
|||||||
else buffer[i++] = c;
|
else buffer[i++] = c;
|
||||||
}
|
}
|
||||||
buffer[i] = 0;
|
buffer[i] = 0;
|
||||||
lua_dostring(buffer);
|
ldo(lua_dostring, buffer);
|
||||||
lua_endblock();
|
lua_endblock();
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
@@ -106,13 +140,13 @@ int main (int argc, char *argv[])
|
|||||||
manual_input(1);
|
manual_input(1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
lua_dofile(NULL); /* executes stdin as a file */
|
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||||
}
|
}
|
||||||
else for (i=1; i<argc; i++) {
|
else for (i=1; i<argc; i++) {
|
||||||
if (argv[i][0] == '-') { /* option? */
|
if (argv[i][0] == '-') { /* option? */
|
||||||
switch (argv[i][1]) {
|
switch (argv[i][1]) {
|
||||||
case 0:
|
case 0:
|
||||||
lua_dofile(NULL); /* executes stdin as a file */
|
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
manual_input(1);
|
manual_input(1);
|
||||||
@@ -129,7 +163,7 @@ int main (int argc, char *argv[])
|
|||||||
break;
|
break;
|
||||||
case 'e':
|
case 'e':
|
||||||
i++;
|
i++;
|
||||||
if (lua_dostring(argv[i]) != 0) {
|
if (ldo(lua_dostring, argv[i]) != 0) {
|
||||||
fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
|
fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -142,7 +176,7 @@ int main (int argc, char *argv[])
|
|||||||
else if (strchr(argv[i], '='))
|
else if (strchr(argv[i], '='))
|
||||||
assign(argv[i]);
|
assign(argv[i]);
|
||||||
else {
|
else {
|
||||||
int result = lua_dofile(argv[i]);
|
int result = ldo(lua_dofile, argv[i]);
|
||||||
if (result) {
|
if (result) {
|
||||||
if (result == 2) {
|
if (result == 2) {
|
||||||
fprintf(stderr, "lua: cannot execute file ");
|
fprintf(stderr, "lua: cannot execute file ");
|
||||||
|
|||||||
Reference in New Issue
Block a user