diff options
Diffstat (limited to 'src/lua')
-rw-r--r-- | src/lua/Makefile | 4 | ||||
-rw-r--r-- | src/lua/README | 24 | ||||
-rw-r--r-- | src/lua/lua.c | 64 |
3 files changed, 52 insertions, 40 deletions
diff --git a/src/lua/Makefile b/src/lua/Makefile index 1ec83706..cf5d31b6 100644 --- a/src/lua/Makefile +++ b/src/lua/Makefile @@ -17,10 +17,10 @@ $T: $(OBJS) $(LIB)/liblua.a $(LIB)/liblualib.a $(CC) -o $@ $(OBJS) -L$(LIB) -llua -llualib -lm $(LIB)/liblua.a: - cd ..; make + cd ..; $(MAKE) $(LIB)/liblualib.a: - cd ../lib; make + cd ../lib; $(MAKE) clean: rm -f $(OBJS) $T diff --git a/src/lua/README b/src/lua/README index 31340429..db2eafb7 100644 --- a/src/lua/README +++ b/src/lua/README @@ -1,5 +1,6 @@ This client is a sample lua interpreter. -It can be used as a batch interpreter and interactively. +It can be used as a batch interpreter and also interactively. + Here are the options it understands: -v print version information @@ -11,10 +12,27 @@ Here are the options it understands: a=b sets global `a' with string `b' (no need to quote b) name dofile `name' -If no options are given, then it reads and executes lines from stdin. -In this case, each line must contain a complete statement. +If no options are given, then it reads lines from stdin and executes them +as they are read. So, each line must contain a complete statement. To span a statement across several lines, end each line with a backslash '\'. + To change the prompt, set the global variable _PROMPT to whatever you want. +You can do after calling the interpreter or on the command line with + _PROMPT="lua: " +for example. + +You must be careful when using quotes on the command line because they are +usually handled by the shell. This interpreter is good for using Lua as a standalone language. For a minimal interpreter, see etc/min.c. + +If your application simply exports new functions to Lua (which is common), +then you can use this interpreter unmodified: just define a function + + void lua_userinit (void) + +in your code. In this function, you should do whatever initializations are +need, typically exporting your functions to Lua. +If you use this scheme, you must explicily open any standard libraries you need. +See ../lib/linit.c diff --git a/src/lua/lua.c b/src/lua/lua.c index a6a92f71..5acd6173 100644 --- a/src/lua/lua.c +++ b/src/lua/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.14 1998/02/11 20:56:05 roberto Exp $ +** $Id: lua.c,v 1.21 1999/07/02 18:22:38 roberto Exp $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -15,12 +15,6 @@ #include "lualib.h" -#ifndef OLD_ANSI -#include <locale.h> -#else -#define setlocale(a,b) 0 -#endif - #ifdef _POSIX_SOURCE #include <unistd.h> #else @@ -32,27 +26,33 @@ typedef void (*handler)(int); /* type for signal actions */ static void laction (int i); -static handler lreset (void) -{ - lua_linehook = NULL; - lua_callhook = NULL; + +static lua_LHFunction old_linehook = NULL; +static lua_CHFunction old_callhook = NULL; + + +static handler lreset (void) { return signal(SIGINT, laction); } -static void lstop (void) -{ + +static void lstop (void) { + lua_setlinehook(old_linehook); + lua_setcallhook(old_callhook); lreset(); lua_error("interrupted!"); } -static void laction (int i) -{ - lua_linehook = (lua_LHFunction)lstop; - lua_callhook = (lua_CHFunction)lstop; + +static void laction (int i) { + signal(SIGINT, SIG_DFL); /* if another SIGINT happens before lstop, + terminate process (default action) */ + old_linehook = lua_setlinehook((lua_LHFunction)lstop); + old_callhook = lua_setcallhook((lua_CHFunction)lstop); } -static int ldo (int (*f)(char *), char *name) -{ + +static int ldo (int (*f)(char *), char *name) { int res; handler h = lreset(); res = f(name); /* dostring | dofile */ @@ -61,8 +61,7 @@ static int ldo (int (*f)(char *), char *name) } -static void print_message (void) -{ +static void print_message (void) { fprintf(stderr, "Lua: command line options:\n" " -v print version information\n" @@ -76,8 +75,7 @@ static void print_message (void) } -static void assign (char *arg) -{ +static void assign (char *arg) { if (strlen(arg) >= 500) fprintf(stderr, "lua: shell argument too long"); else { @@ -90,13 +88,11 @@ static void assign (char *arg) } } -#define BUF_SIZE 512 -static void manual_input (int prompt) -{ +static void manual_input (int prompt) { int cont = 1; while (cont) { - char buffer[BUF_SIZE]; + char buffer[BUFSIZ]; int i = 0; lua_beginblock(); if (prompt) @@ -112,13 +108,13 @@ static void manual_input (int prompt) buffer[i-1] = '\n'; else break; } - else if (i >= BUF_SIZE-1) { + else if (i >= BUFSIZ-1) { fprintf(stderr, "lua: argument line too long\n"); break; } - else buffer[i++] = c; + else buffer[i++] = (char)c; } - buffer[i] = 0; + buffer[i] = '\0'; ldo(lua_dostring, buffer); lua_endblock(); } @@ -129,11 +125,9 @@ static void manual_input (int prompt) int main (int argc, char *argv[]) { int i; - setlocale(LC_ALL, ""); - lua_iolibopen(); - lua_strlibopen(); - lua_mathlibopen(); + lua_open(); lua_pushstring("> "); lua_setglobal("_PROMPT"); + lua_userinit(); if (argc < 2) { /* no arguments? */ if (isatty(0)) { printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT); @@ -155,7 +149,7 @@ int main (int argc, char *argv[]) manual_input(0); break; case 'd': - lua_debug = 1; + lua_setdebug(1); break; case 'v': printf("%s %s\n(written by %s)\n\n", |