2 ** $Id: lua.c,v 1.21 1999/07/02 18:22:38 roberto Exp $
3 ** Lua stand-alone interpreter
4 ** See Copyright Notice in lua.h
21 #define isatty(x) (x==0) /* assume stdin is a tty */
25 typedef void (*handler
)(int); /* type for signal actions */
27 static void laction (int i
);
30 static lua_LHFunction old_linehook
= NULL
;
31 static lua_CHFunction old_callhook
= NULL
;
34 static handler
lreset (void) {
35 return signal(SIGINT
, laction
);
39 static void lstop (void) {
40 lua_setlinehook(old_linehook
);
41 lua_setcallhook(old_callhook
);
43 lua_error("interrupted!");
47 static void laction (int i
) {
48 signal(SIGINT
, SIG_DFL
); /* if another SIGINT happens before lstop,
49 terminate process (default action) */
50 old_linehook
= lua_setlinehook((lua_LHFunction
)lstop
);
51 old_callhook
= lua_setcallhook((lua_CHFunction
)lstop
);
55 static int ldo (int (*f
)(char *), char *name
) {
58 res
= f(name
); /* dostring | dofile */
59 signal(SIGINT
, h
); /* restore old action */
64 static void print_message (void) {
66 "Lua: command line options:\n"
67 " -v print version information\n"
69 " -e stat dostring `stat'\n"
70 " -q interactive mode without prompt\n"
71 " -i interactive mode with prompt\n"
72 " - executes stdin as a file\n"
73 " a=b sets global `a' with string `b'\n"
74 " name dofile `name'\n\n");
78 static void assign (char *arg
) {
79 if (strlen(arg
) >= 500)
80 fprintf(stderr
, "lua: shell argument too long");
83 char *eq
= strchr(arg
, '=');
85 strncpy(buffer
, arg
, eq
-arg
);
87 lua_setglobal(buffer
);
92 static void manual_input (int prompt
) {
99 printf("%s", lua_getstring(lua_getglobal("_PROMPT")));
106 else if (c
== '\n') {
107 if (i
>0 && buffer
[i
-1] == '\\')
111 else if (i
>= BUFSIZ
-1) {
112 fprintf(stderr
, "lua: argument line too long\n");
115 else buffer
[i
++] = (char)c
;
118 ldo(lua_dostring
, buffer
);
125 int main (int argc
, char *argv
[])
129 lua_pushstring("> "); lua_setglobal("_PROMPT");
131 if (argc
< 2) { /* no arguments? */
133 printf("%s %s\n", LUA_VERSION
, LUA_COPYRIGHT
);
137 ldo(lua_dofile
, NULL
); /* executes stdin as a file */
139 else for (i
=1; i
<argc
; i
++) {
140 if (argv
[i
][0] == '-') { /* option? */
141 switch (argv
[i
][1]) {
143 ldo(lua_dofile
, NULL
); /* executes stdin as a file */
155 printf("%s %s\n(written by %s)\n\n",
156 LUA_VERSION
, LUA_COPYRIGHT
, LUA_AUTHORS
);
160 if (ldo(lua_dostring
, argv
[i
]) != 0) {
161 fprintf(stderr
, "lua: error running argument `%s'\n", argv
[i
]);
170 else if (strchr(argv
[i
], '='))
173 int result
= ldo(lua_dofile
, argv
[i
]);
176 fprintf(stderr
, "lua: cannot execute file ");