xref: /freebsd-13.1/contrib/lua/src/lua.c (revision 0495ed39)
18e3e3a7aSWarner Losh /*
2*0495ed39SKyle Evans ** $Id: lua.c $
38e3e3a7aSWarner Losh ** Lua stand-alone interpreter
48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h
58e3e3a7aSWarner Losh */
68e3e3a7aSWarner Losh 
78e3e3a7aSWarner Losh #define lua_c
88e3e3a7aSWarner Losh 
98e3e3a7aSWarner Losh #include "lprefix.h"
108e3e3a7aSWarner Losh 
118e3e3a7aSWarner Losh 
128e3e3a7aSWarner Losh #include <stdio.h>
138e3e3a7aSWarner Losh #include <stdlib.h>
148e3e3a7aSWarner Losh #include <string.h>
158e3e3a7aSWarner Losh 
16*0495ed39SKyle Evans #include <signal.h>
17*0495ed39SKyle Evans 
188e3e3a7aSWarner Losh #include "lua.h"
198e3e3a7aSWarner Losh 
208e3e3a7aSWarner Losh #include "lauxlib.h"
218e3e3a7aSWarner Losh #include "lualib.h"
228e3e3a7aSWarner Losh 
238e3e3a7aSWarner Losh 
248e3e3a7aSWarner Losh #if !defined(LUA_PROGNAME)
258e3e3a7aSWarner Losh #define LUA_PROGNAME		"lua"
268e3e3a7aSWarner Losh #endif
278e3e3a7aSWarner Losh 
288e3e3a7aSWarner Losh #if !defined(LUA_INIT_VAR)
298e3e3a7aSWarner Losh #define LUA_INIT_VAR		"LUA_INIT"
308e3e3a7aSWarner Losh #endif
318e3e3a7aSWarner Losh 
328e3e3a7aSWarner Losh #define LUA_INITVARVERSION	LUA_INIT_VAR LUA_VERSUFFIX
338e3e3a7aSWarner Losh 
348e3e3a7aSWarner Losh 
358e3e3a7aSWarner Losh static lua_State *globalL = NULL;
368e3e3a7aSWarner Losh 
378e3e3a7aSWarner Losh static const char *progname = LUA_PROGNAME;
388e3e3a7aSWarner Losh 
398e3e3a7aSWarner Losh 
408e3e3a7aSWarner Losh /*
418e3e3a7aSWarner Losh ** Hook set by signal function to stop the interpreter.
428e3e3a7aSWarner Losh */
lstop(lua_State * L,lua_Debug * ar)438e3e3a7aSWarner Losh static void lstop (lua_State *L, lua_Debug *ar) {
448e3e3a7aSWarner Losh   (void)ar;  /* unused arg. */
458e3e3a7aSWarner Losh   lua_sethook(L, NULL, 0, 0);  /* reset hook */
468e3e3a7aSWarner Losh   luaL_error(L, "interrupted!");
478e3e3a7aSWarner Losh }
488e3e3a7aSWarner Losh 
498e3e3a7aSWarner Losh 
508e3e3a7aSWarner Losh /*
518e3e3a7aSWarner Losh ** Function to be called at a C signal. Because a C signal cannot
528e3e3a7aSWarner Losh ** just change a Lua state (as there is no proper synchronization),
538e3e3a7aSWarner Losh ** this function only sets a hook that, when called, will stop the
548e3e3a7aSWarner Losh ** interpreter.
558e3e3a7aSWarner Losh */
laction(int i)568e3e3a7aSWarner Losh static void laction (int i) {
57*0495ed39SKyle Evans   int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT;
588e3e3a7aSWarner Losh   signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
59*0495ed39SKyle Evans   lua_sethook(globalL, lstop, flag, 1);
608e3e3a7aSWarner Losh }
618e3e3a7aSWarner Losh 
628e3e3a7aSWarner Losh 
print_usage(const char * badoption)638e3e3a7aSWarner Losh static void print_usage (const char *badoption) {
648e3e3a7aSWarner Losh   lua_writestringerror("%s: ", progname);
658e3e3a7aSWarner Losh   if (badoption[1] == 'e' || badoption[1] == 'l')
668e3e3a7aSWarner Losh     lua_writestringerror("'%s' needs argument\n", badoption);
678e3e3a7aSWarner Losh   else
688e3e3a7aSWarner Losh     lua_writestringerror("unrecognized option '%s'\n", badoption);
698e3e3a7aSWarner Losh   lua_writestringerror(
708e3e3a7aSWarner Losh   "usage: %s [options] [script [args]]\n"
718e3e3a7aSWarner Losh   "Available options are:\n"
728e3e3a7aSWarner Losh   "  -e stat  execute string 'stat'\n"
738e3e3a7aSWarner Losh   "  -i       enter interactive mode after executing 'script'\n"
74e112e9d2SKyle Evans   "  -l name  require library 'name' into global 'name'\n"
758e3e3a7aSWarner Losh   "  -v       show version information\n"
768e3e3a7aSWarner Losh   "  -E       ignore environment variables\n"
77*0495ed39SKyle Evans   "  -W       turn warnings on\n"
788e3e3a7aSWarner Losh   "  --       stop handling options\n"
798e3e3a7aSWarner Losh   "  -        stop handling options and execute stdin\n"
808e3e3a7aSWarner Losh   ,
818e3e3a7aSWarner Losh   progname);
828e3e3a7aSWarner Losh }
838e3e3a7aSWarner Losh 
848e3e3a7aSWarner Losh 
858e3e3a7aSWarner Losh /*
868e3e3a7aSWarner Losh ** Prints an error message, adding the program name in front of it
878e3e3a7aSWarner Losh ** (if present)
888e3e3a7aSWarner Losh */
l_message(const char * pname,const char * msg)898e3e3a7aSWarner Losh static void l_message (const char *pname, const char *msg) {
908e3e3a7aSWarner Losh   if (pname) lua_writestringerror("%s: ", pname);
918e3e3a7aSWarner Losh   lua_writestringerror("%s\n", msg);
928e3e3a7aSWarner Losh }
938e3e3a7aSWarner Losh 
948e3e3a7aSWarner Losh 
958e3e3a7aSWarner Losh /*
968e3e3a7aSWarner Losh ** Check whether 'status' is not OK and, if so, prints the error
978e3e3a7aSWarner Losh ** message on the top of the stack. It assumes that the error object
988e3e3a7aSWarner Losh ** is a string, as it was either generated by Lua or by 'msghandler'.
998e3e3a7aSWarner Losh */
report(lua_State * L,int status)1008e3e3a7aSWarner Losh static int report (lua_State *L, int status) {
1018e3e3a7aSWarner Losh   if (status != LUA_OK) {
1028e3e3a7aSWarner Losh     const char *msg = lua_tostring(L, -1);
1038e3e3a7aSWarner Losh     l_message(progname, msg);
1048e3e3a7aSWarner Losh     lua_pop(L, 1);  /* remove message */
1058e3e3a7aSWarner Losh   }
1068e3e3a7aSWarner Losh   return status;
1078e3e3a7aSWarner Losh }
1088e3e3a7aSWarner Losh 
1098e3e3a7aSWarner Losh 
1108e3e3a7aSWarner Losh /*
1118e3e3a7aSWarner Losh ** Message handler used to run all chunks
1128e3e3a7aSWarner Losh */
msghandler(lua_State * L)1138e3e3a7aSWarner Losh static int msghandler (lua_State *L) {
1148e3e3a7aSWarner Losh   const char *msg = lua_tostring(L, 1);
1158e3e3a7aSWarner Losh   if (msg == NULL) {  /* is error object not a string? */
1168e3e3a7aSWarner Losh     if (luaL_callmeta(L, 1, "__tostring") &&  /* does it have a metamethod */
1178e3e3a7aSWarner Losh         lua_type(L, -1) == LUA_TSTRING)  /* that produces a string? */
1188e3e3a7aSWarner Losh       return 1;  /* that is the message */
1198e3e3a7aSWarner Losh     else
1208e3e3a7aSWarner Losh       msg = lua_pushfstring(L, "(error object is a %s value)",
1218e3e3a7aSWarner Losh                                luaL_typename(L, 1));
1228e3e3a7aSWarner Losh   }
1238e3e3a7aSWarner Losh   luaL_traceback(L, L, msg, 1);  /* append a standard traceback */
1248e3e3a7aSWarner Losh   return 1;  /* return the traceback */
1258e3e3a7aSWarner Losh }
1268e3e3a7aSWarner Losh 
1278e3e3a7aSWarner Losh 
1288e3e3a7aSWarner Losh /*
1298e3e3a7aSWarner Losh ** Interface to 'lua_pcall', which sets appropriate message function
1308e3e3a7aSWarner Losh ** and C-signal handler. Used to run all chunks.
1318e3e3a7aSWarner Losh */
docall(lua_State * L,int narg,int nres)1328e3e3a7aSWarner Losh static int docall (lua_State *L, int narg, int nres) {
1338e3e3a7aSWarner Losh   int status;
1348e3e3a7aSWarner Losh   int base = lua_gettop(L) - narg;  /* function index */
1358e3e3a7aSWarner Losh   lua_pushcfunction(L, msghandler);  /* push message handler */
1368e3e3a7aSWarner Losh   lua_insert(L, base);  /* put it under function and args */
1378e3e3a7aSWarner Losh   globalL = L;  /* to be available to 'laction' */
1388e3e3a7aSWarner Losh   signal(SIGINT, laction);  /* set C-signal handler */
1398e3e3a7aSWarner Losh   status = lua_pcall(L, narg, nres, base);
1408e3e3a7aSWarner Losh   signal(SIGINT, SIG_DFL); /* reset C-signal handler */
1418e3e3a7aSWarner Losh   lua_remove(L, base);  /* remove message handler from the stack */
1428e3e3a7aSWarner Losh   return status;
1438e3e3a7aSWarner Losh }
1448e3e3a7aSWarner Losh 
1458e3e3a7aSWarner Losh 
print_version(void)1468e3e3a7aSWarner Losh static void print_version (void) {
1478e3e3a7aSWarner Losh   lua_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT));
1488e3e3a7aSWarner Losh   lua_writeline();
1498e3e3a7aSWarner Losh }
1508e3e3a7aSWarner Losh 
1518e3e3a7aSWarner Losh 
1528e3e3a7aSWarner Losh /*
1538e3e3a7aSWarner Losh ** Create the 'arg' table, which stores all arguments from the
1548e3e3a7aSWarner Losh ** command line ('argv'). It should be aligned so that, at index 0,
1558e3e3a7aSWarner Losh ** it has 'argv[script]', which is the script name. The arguments
1568e3e3a7aSWarner Losh ** to the script (everything after 'script') go to positive indices;
1578e3e3a7aSWarner Losh ** other arguments (before the script name) go to negative indices.
1588e3e3a7aSWarner Losh ** If there is no script name, assume interpreter's name as base.
1598e3e3a7aSWarner Losh */
createargtable(lua_State * L,char ** argv,int argc,int script)1608e3e3a7aSWarner Losh static void createargtable (lua_State *L, char **argv, int argc, int script) {
1618e3e3a7aSWarner Losh   int i, narg;
1628e3e3a7aSWarner Losh   if (script == argc) script = 0;  /* no script name? */
1638e3e3a7aSWarner Losh   narg = argc - (script + 1);  /* number of positive indices */
1648e3e3a7aSWarner Losh   lua_createtable(L, narg, script + 1);
1658e3e3a7aSWarner Losh   for (i = 0; i < argc; i++) {
1668e3e3a7aSWarner Losh     lua_pushstring(L, argv[i]);
1678e3e3a7aSWarner Losh     lua_rawseti(L, -2, i - script);
1688e3e3a7aSWarner Losh   }
1698e3e3a7aSWarner Losh   lua_setglobal(L, "arg");
1708e3e3a7aSWarner Losh }
1718e3e3a7aSWarner Losh 
1728e3e3a7aSWarner Losh 
dochunk(lua_State * L,int status)1738e3e3a7aSWarner Losh static int dochunk (lua_State *L, int status) {
1748e3e3a7aSWarner Losh   if (status == LUA_OK) status = docall(L, 0, 0);
1758e3e3a7aSWarner Losh   return report(L, status);
1768e3e3a7aSWarner Losh }
1778e3e3a7aSWarner Losh 
1788e3e3a7aSWarner Losh 
dofile(lua_State * L,const char * name)1798e3e3a7aSWarner Losh static int dofile (lua_State *L, const char *name) {
1808e3e3a7aSWarner Losh   return dochunk(L, luaL_loadfile(L, name));
1818e3e3a7aSWarner Losh }
1828e3e3a7aSWarner Losh 
1838e3e3a7aSWarner Losh 
dostring(lua_State * L,const char * s,const char * name)1848e3e3a7aSWarner Losh static int dostring (lua_State *L, const char *s, const char *name) {
1858e3e3a7aSWarner Losh   return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name));
1868e3e3a7aSWarner Losh }
1878e3e3a7aSWarner Losh 
1888e3e3a7aSWarner Losh 
1898e3e3a7aSWarner Losh /*
1908e3e3a7aSWarner Losh ** Calls 'require(name)' and stores the result in a global variable
1918e3e3a7aSWarner Losh ** with the given name.
1928e3e3a7aSWarner Losh */
dolibrary(lua_State * L,const char * name)1938e3e3a7aSWarner Losh static int dolibrary (lua_State *L, const char *name) {
1948e3e3a7aSWarner Losh   int status;
1958e3e3a7aSWarner Losh   lua_getglobal(L, "require");
1968e3e3a7aSWarner Losh   lua_pushstring(L, name);
1978e3e3a7aSWarner Losh   status = docall(L, 1, 1);  /* call 'require(name)' */
1988e3e3a7aSWarner Losh   if (status == LUA_OK)
1998e3e3a7aSWarner Losh     lua_setglobal(L, name);  /* global[name] = require return */
2008e3e3a7aSWarner Losh   return report(L, status);
2018e3e3a7aSWarner Losh }
2028e3e3a7aSWarner Losh 
2038e3e3a7aSWarner Losh 
2048e3e3a7aSWarner Losh /*
205*0495ed39SKyle Evans ** Push on the stack the contents of table 'arg' from 1 to #arg
206*0495ed39SKyle Evans */
pushargs(lua_State * L)207*0495ed39SKyle Evans static int pushargs (lua_State *L) {
208*0495ed39SKyle Evans   int i, n;
209*0495ed39SKyle Evans   if (lua_getglobal(L, "arg") != LUA_TTABLE)
210*0495ed39SKyle Evans     luaL_error(L, "'arg' is not a table");
211*0495ed39SKyle Evans   n = (int)luaL_len(L, -1);
212*0495ed39SKyle Evans   luaL_checkstack(L, n + 3, "too many arguments to script");
213*0495ed39SKyle Evans   for (i = 1; i <= n; i++)
214*0495ed39SKyle Evans     lua_rawgeti(L, -i, i);
215*0495ed39SKyle Evans   lua_remove(L, -i);  /* remove table from the stack */
216*0495ed39SKyle Evans   return n;
217*0495ed39SKyle Evans }
218*0495ed39SKyle Evans 
219*0495ed39SKyle Evans 
handle_script(lua_State * L,char ** argv)220*0495ed39SKyle Evans static int handle_script (lua_State *L, char **argv) {
221*0495ed39SKyle Evans   int status;
222*0495ed39SKyle Evans   const char *fname = argv[0];
223*0495ed39SKyle Evans   if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
224*0495ed39SKyle Evans     fname = NULL;  /* stdin */
225*0495ed39SKyle Evans   status = luaL_loadfile(L, fname);
226*0495ed39SKyle Evans   if (status == LUA_OK) {
227*0495ed39SKyle Evans     int n = pushargs(L);  /* push arguments to script */
228*0495ed39SKyle Evans     status = docall(L, n, LUA_MULTRET);
229*0495ed39SKyle Evans   }
230*0495ed39SKyle Evans   return report(L, status);
231*0495ed39SKyle Evans }
232*0495ed39SKyle Evans 
233*0495ed39SKyle Evans 
234*0495ed39SKyle Evans /* bits of various argument indicators in 'args' */
235*0495ed39SKyle Evans #define has_error	1	/* bad option */
236*0495ed39SKyle Evans #define has_i		2	/* -i */
237*0495ed39SKyle Evans #define has_v		4	/* -v */
238*0495ed39SKyle Evans #define has_e		8	/* -e */
239*0495ed39SKyle Evans #define has_E		16	/* -E */
240*0495ed39SKyle Evans 
241*0495ed39SKyle Evans 
242*0495ed39SKyle Evans /*
243*0495ed39SKyle Evans ** Traverses all arguments from 'argv', returning a mask with those
244*0495ed39SKyle Evans ** needed before running any Lua code (or an error code if it finds
245*0495ed39SKyle Evans ** any invalid argument). 'first' returns the first not-handled argument
246*0495ed39SKyle Evans ** (either the script name or a bad argument in case of error).
247*0495ed39SKyle Evans */
collectargs(char ** argv,int * first)248*0495ed39SKyle Evans static int collectargs (char **argv, int *first) {
249*0495ed39SKyle Evans   int args = 0;
250*0495ed39SKyle Evans   int i;
251*0495ed39SKyle Evans   for (i = 1; argv[i] != NULL; i++) {
252*0495ed39SKyle Evans     *first = i;
253*0495ed39SKyle Evans     if (argv[i][0] != '-')  /* not an option? */
254*0495ed39SKyle Evans         return args;  /* stop handling options */
255*0495ed39SKyle Evans     switch (argv[i][1]) {  /* else check option */
256*0495ed39SKyle Evans       case '-':  /* '--' */
257*0495ed39SKyle Evans         if (argv[i][2] != '\0')  /* extra characters after '--'? */
258*0495ed39SKyle Evans           return has_error;  /* invalid option */
259*0495ed39SKyle Evans         *first = i + 1;
260*0495ed39SKyle Evans         return args;
261*0495ed39SKyle Evans       case '\0':  /* '-' */
262*0495ed39SKyle Evans         return args;  /* script "name" is '-' */
263*0495ed39SKyle Evans       case 'E':
264*0495ed39SKyle Evans         if (argv[i][2] != '\0')  /* extra characters? */
265*0495ed39SKyle Evans           return has_error;  /* invalid option */
266*0495ed39SKyle Evans         args |= has_E;
267*0495ed39SKyle Evans         break;
268*0495ed39SKyle Evans       case 'W':
269*0495ed39SKyle Evans         if (argv[i][2] != '\0')  /* extra characters? */
270*0495ed39SKyle Evans           return has_error;  /* invalid option */
271*0495ed39SKyle Evans         break;
272*0495ed39SKyle Evans       case 'i':
273*0495ed39SKyle Evans         args |= has_i;  /* (-i implies -v) *//* FALLTHROUGH */
274*0495ed39SKyle Evans       case 'v':
275*0495ed39SKyle Evans         if (argv[i][2] != '\0')  /* extra characters? */
276*0495ed39SKyle Evans           return has_error;  /* invalid option */
277*0495ed39SKyle Evans         args |= has_v;
278*0495ed39SKyle Evans         break;
279*0495ed39SKyle Evans       case 'e':
280*0495ed39SKyle Evans         args |= has_e;  /* FALLTHROUGH */
281*0495ed39SKyle Evans       case 'l':  /* both options need an argument */
282*0495ed39SKyle Evans         if (argv[i][2] == '\0') {  /* no concatenated argument? */
283*0495ed39SKyle Evans           i++;  /* try next 'argv' */
284*0495ed39SKyle Evans           if (argv[i] == NULL || argv[i][0] == '-')
285*0495ed39SKyle Evans             return has_error;  /* no next argument or it is another option */
286*0495ed39SKyle Evans         }
287*0495ed39SKyle Evans         break;
288*0495ed39SKyle Evans       default:  /* invalid option */
289*0495ed39SKyle Evans         return has_error;
290*0495ed39SKyle Evans     }
291*0495ed39SKyle Evans   }
292*0495ed39SKyle Evans   *first = i;  /* no script name */
293*0495ed39SKyle Evans   return args;
294*0495ed39SKyle Evans }
295*0495ed39SKyle Evans 
296*0495ed39SKyle Evans 
297*0495ed39SKyle Evans /*
298*0495ed39SKyle Evans ** Processes options 'e' and 'l', which involve running Lua code, and
299*0495ed39SKyle Evans ** 'W', which also affects the state.
300*0495ed39SKyle Evans ** Returns 0 if some code raises an error.
301*0495ed39SKyle Evans */
runargs(lua_State * L,char ** argv,int n)302*0495ed39SKyle Evans static int runargs (lua_State *L, char **argv, int n) {
303*0495ed39SKyle Evans   int i;
304*0495ed39SKyle Evans   for (i = 1; i < n; i++) {
305*0495ed39SKyle Evans     int option = argv[i][1];
306*0495ed39SKyle Evans     lua_assert(argv[i][0] == '-');  /* already checked */
307*0495ed39SKyle Evans     switch (option) {
308*0495ed39SKyle Evans       case 'e':  case 'l': {
309*0495ed39SKyle Evans         int status;
310*0495ed39SKyle Evans         const char *extra = argv[i] + 2;  /* both options need an argument */
311*0495ed39SKyle Evans         if (*extra == '\0') extra = argv[++i];
312*0495ed39SKyle Evans         lua_assert(extra != NULL);
313*0495ed39SKyle Evans         status = (option == 'e')
314*0495ed39SKyle Evans                  ? dostring(L, extra, "=(command line)")
315*0495ed39SKyle Evans                  : dolibrary(L, extra);
316*0495ed39SKyle Evans         if (status != LUA_OK) return 0;
317*0495ed39SKyle Evans         break;
318*0495ed39SKyle Evans       }
319*0495ed39SKyle Evans       case 'W':
320*0495ed39SKyle Evans         lua_warning(L, "@on", 0);  /* warnings on */
321*0495ed39SKyle Evans         break;
322*0495ed39SKyle Evans     }
323*0495ed39SKyle Evans   }
324*0495ed39SKyle Evans   return 1;
325*0495ed39SKyle Evans }
326*0495ed39SKyle Evans 
327*0495ed39SKyle Evans 
handle_luainit(lua_State * L)328*0495ed39SKyle Evans static int handle_luainit (lua_State *L) {
329*0495ed39SKyle Evans   const char *name = "=" LUA_INITVARVERSION;
330*0495ed39SKyle Evans   const char *init = getenv(name + 1);
331*0495ed39SKyle Evans   if (init == NULL) {
332*0495ed39SKyle Evans     name = "=" LUA_INIT_VAR;
333*0495ed39SKyle Evans     init = getenv(name + 1);  /* try alternative name */
334*0495ed39SKyle Evans   }
335*0495ed39SKyle Evans   if (init == NULL) return LUA_OK;
336*0495ed39SKyle Evans   else if (init[0] == '@')
337*0495ed39SKyle Evans     return dofile(L, init+1);
338*0495ed39SKyle Evans   else
339*0495ed39SKyle Evans     return dostring(L, init, name);
340*0495ed39SKyle Evans }
341*0495ed39SKyle Evans 
342*0495ed39SKyle Evans 
343*0495ed39SKyle Evans /*
344*0495ed39SKyle Evans ** {==================================================================
345*0495ed39SKyle Evans ** Read-Eval-Print Loop (REPL)
346*0495ed39SKyle Evans ** ===================================================================
347*0495ed39SKyle Evans */
348*0495ed39SKyle Evans 
349*0495ed39SKyle Evans #if !defined(LUA_PROMPT)
350*0495ed39SKyle Evans #define LUA_PROMPT		"> "
351*0495ed39SKyle Evans #define LUA_PROMPT2		">> "
352*0495ed39SKyle Evans #endif
353*0495ed39SKyle Evans 
354*0495ed39SKyle Evans #if !defined(LUA_MAXINPUT)
355*0495ed39SKyle Evans #define LUA_MAXINPUT		512
356*0495ed39SKyle Evans #endif
357*0495ed39SKyle Evans 
358*0495ed39SKyle Evans 
359*0495ed39SKyle Evans /*
360*0495ed39SKyle Evans ** lua_stdin_is_tty detects whether the standard input is a 'tty' (that
361*0495ed39SKyle Evans ** is, whether we're running lua interactively).
362*0495ed39SKyle Evans */
363*0495ed39SKyle Evans #if !defined(lua_stdin_is_tty)	/* { */
364*0495ed39SKyle Evans 
365*0495ed39SKyle Evans #if defined(LUA_USE_POSIX)	/* { */
366*0495ed39SKyle Evans 
367*0495ed39SKyle Evans #include <unistd.h>
368*0495ed39SKyle Evans #define lua_stdin_is_tty()	isatty(0)
369*0495ed39SKyle Evans 
370*0495ed39SKyle Evans #elif defined(LUA_USE_WINDOWS)	/* }{ */
371*0495ed39SKyle Evans 
372*0495ed39SKyle Evans #include <io.h>
373*0495ed39SKyle Evans #include <windows.h>
374*0495ed39SKyle Evans 
375*0495ed39SKyle Evans #define lua_stdin_is_tty()	_isatty(_fileno(stdin))
376*0495ed39SKyle Evans 
377*0495ed39SKyle Evans #else				/* }{ */
378*0495ed39SKyle Evans 
379*0495ed39SKyle Evans /* ISO C definition */
380*0495ed39SKyle Evans #define lua_stdin_is_tty()	1  /* assume stdin is a tty */
381*0495ed39SKyle Evans 
382*0495ed39SKyle Evans #endif				/* } */
383*0495ed39SKyle Evans 
384*0495ed39SKyle Evans #endif				/* } */
385*0495ed39SKyle Evans 
386*0495ed39SKyle Evans 
387*0495ed39SKyle Evans /*
388*0495ed39SKyle Evans ** lua_readline defines how to show a prompt and then read a line from
389*0495ed39SKyle Evans ** the standard input.
390*0495ed39SKyle Evans ** lua_saveline defines how to "save" a read line in a "history".
391*0495ed39SKyle Evans ** lua_freeline defines how to free a line read by lua_readline.
392*0495ed39SKyle Evans */
393*0495ed39SKyle Evans #if !defined(lua_readline)	/* { */
394*0495ed39SKyle Evans 
395*0495ed39SKyle Evans #if defined(LUA_USE_READLINE)	/* { */
396*0495ed39SKyle Evans 
397*0495ed39SKyle Evans #include <readline/readline.h>
398*0495ed39SKyle Evans #include <readline/history.h>
399*0495ed39SKyle Evans #define lua_initreadline(L)	((void)L, rl_readline_name="lua")
400*0495ed39SKyle Evans #define lua_readline(L,b,p)	((void)L, ((b)=readline(p)) != NULL)
401*0495ed39SKyle Evans #define lua_saveline(L,line)	((void)L, add_history(line))
402*0495ed39SKyle Evans #define lua_freeline(L,b)	((void)L, free(b))
403*0495ed39SKyle Evans 
404*0495ed39SKyle Evans #else				/* }{ */
405*0495ed39SKyle Evans 
406*0495ed39SKyle Evans #define lua_initreadline(L)  ((void)L)
407*0495ed39SKyle Evans #define lua_readline(L,b,p) \
408*0495ed39SKyle Evans         ((void)L, fputs(p, stdout), fflush(stdout),  /* show prompt */ \
409*0495ed39SKyle Evans         fgets(b, LUA_MAXINPUT, stdin) != NULL)  /* get line */
410*0495ed39SKyle Evans #define lua_saveline(L,line)	{ (void)L; (void)line; }
411*0495ed39SKyle Evans #define lua_freeline(L,b)	{ (void)L; (void)b; }
412*0495ed39SKyle Evans 
413*0495ed39SKyle Evans #endif				/* } */
414*0495ed39SKyle Evans 
415*0495ed39SKyle Evans #endif				/* } */
416*0495ed39SKyle Evans 
417*0495ed39SKyle Evans 
418*0495ed39SKyle Evans /*
419*0495ed39SKyle Evans ** Return the string to be used as a prompt by the interpreter. Leave
420*0495ed39SKyle Evans ** the string (or nil, if using the default value) on the stack, to keep
421*0495ed39SKyle Evans ** it anchored.
4228e3e3a7aSWarner Losh */
get_prompt(lua_State * L,int firstline)4238e3e3a7aSWarner Losh static const char *get_prompt (lua_State *L, int firstline) {
424*0495ed39SKyle Evans   if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL)
425*0495ed39SKyle Evans     return (firstline ? LUA_PROMPT : LUA_PROMPT2);  /* use the default */
426*0495ed39SKyle Evans   else {  /* apply 'tostring' over the value */
427*0495ed39SKyle Evans     const char *p = luaL_tolstring(L, -1, NULL);
428*0495ed39SKyle Evans     lua_remove(L, -2);  /* remove original value */
4298e3e3a7aSWarner Losh     return p;
4308e3e3a7aSWarner Losh   }
431*0495ed39SKyle Evans }
4328e3e3a7aSWarner Losh 
4338e3e3a7aSWarner Losh /* mark in error messages for incomplete statements */
4348e3e3a7aSWarner Losh #define EOFMARK		"<eof>"
4358e3e3a7aSWarner Losh #define marklen		(sizeof(EOFMARK)/sizeof(char) - 1)
4368e3e3a7aSWarner Losh 
4378e3e3a7aSWarner Losh 
4388e3e3a7aSWarner Losh /*
4398e3e3a7aSWarner Losh ** Check whether 'status' signals a syntax error and the error
4408e3e3a7aSWarner Losh ** message at the top of the stack ends with the above mark for
4418e3e3a7aSWarner Losh ** incomplete statements.
4428e3e3a7aSWarner Losh */
incomplete(lua_State * L,int status)4438e3e3a7aSWarner Losh static int incomplete (lua_State *L, int status) {
4448e3e3a7aSWarner Losh   if (status == LUA_ERRSYNTAX) {
4458e3e3a7aSWarner Losh     size_t lmsg;
4468e3e3a7aSWarner Losh     const char *msg = lua_tolstring(L, -1, &lmsg);
4478e3e3a7aSWarner Losh     if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) {
4488e3e3a7aSWarner Losh       lua_pop(L, 1);
4498e3e3a7aSWarner Losh       return 1;
4508e3e3a7aSWarner Losh     }
4518e3e3a7aSWarner Losh   }
4528e3e3a7aSWarner Losh   return 0;  /* else... */
4538e3e3a7aSWarner Losh }
4548e3e3a7aSWarner Losh 
4558e3e3a7aSWarner Losh 
4568e3e3a7aSWarner Losh /*
4578e3e3a7aSWarner Losh ** Prompt the user, read a line, and push it into the Lua stack.
4588e3e3a7aSWarner Losh */
pushline(lua_State * L,int firstline)4598e3e3a7aSWarner Losh static int pushline (lua_State *L, int firstline) {
4608e3e3a7aSWarner Losh   char buffer[LUA_MAXINPUT];
4618e3e3a7aSWarner Losh   char *b = buffer;
4628e3e3a7aSWarner Losh   size_t l;
4638e3e3a7aSWarner Losh   const char *prmt = get_prompt(L, firstline);
4648e3e3a7aSWarner Losh   int readstatus = lua_readline(L, b, prmt);
4658e3e3a7aSWarner Losh   if (readstatus == 0)
4668e3e3a7aSWarner Losh     return 0;  /* no input (prompt will be popped by caller) */
4678e3e3a7aSWarner Losh   lua_pop(L, 1);  /* remove prompt */
4688e3e3a7aSWarner Losh   l = strlen(b);
4698e3e3a7aSWarner Losh   if (l > 0 && b[l-1] == '\n')  /* line ends with newline? */
4708e3e3a7aSWarner Losh     b[--l] = '\0';  /* remove it */
4718e3e3a7aSWarner Losh   if (firstline && b[0] == '=')  /* for compatibility with 5.2, ... */
4728e3e3a7aSWarner Losh     lua_pushfstring(L, "return %s", b + 1);  /* change '=' to 'return' */
4738e3e3a7aSWarner Losh   else
4748e3e3a7aSWarner Losh     lua_pushlstring(L, b, l);
4758e3e3a7aSWarner Losh   lua_freeline(L, b);
4768e3e3a7aSWarner Losh   return 1;
4778e3e3a7aSWarner Losh }
4788e3e3a7aSWarner Losh 
4798e3e3a7aSWarner Losh 
4808e3e3a7aSWarner Losh /*
4818e3e3a7aSWarner Losh ** Try to compile line on the stack as 'return <line>;'; on return, stack
4828e3e3a7aSWarner Losh ** has either compiled chunk or original line (if compilation failed).
4838e3e3a7aSWarner Losh */
addreturn(lua_State * L)4848e3e3a7aSWarner Losh static int addreturn (lua_State *L) {
4858e3e3a7aSWarner Losh   const char *line = lua_tostring(L, -1);  /* original line */
4868e3e3a7aSWarner Losh   const char *retline = lua_pushfstring(L, "return %s;", line);
4878e3e3a7aSWarner Losh   int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
4888e3e3a7aSWarner Losh   if (status == LUA_OK) {
4898e3e3a7aSWarner Losh     lua_remove(L, -2);  /* remove modified line */
4908e3e3a7aSWarner Losh     if (line[0] != '\0')  /* non empty? */
4918e3e3a7aSWarner Losh       lua_saveline(L, line);  /* keep history */
4928e3e3a7aSWarner Losh   }
4938e3e3a7aSWarner Losh   else
4948e3e3a7aSWarner Losh     lua_pop(L, 2);  /* pop result from 'luaL_loadbuffer' and modified line */
4958e3e3a7aSWarner Losh   return status;
4968e3e3a7aSWarner Losh }
4978e3e3a7aSWarner Losh 
4988e3e3a7aSWarner Losh 
4998e3e3a7aSWarner Losh /*
5008e3e3a7aSWarner Losh ** Read multiple lines until a complete Lua statement
5018e3e3a7aSWarner Losh */
multiline(lua_State * L)5028e3e3a7aSWarner Losh static int multiline (lua_State *L) {
5038e3e3a7aSWarner Losh   for (;;) {  /* repeat until gets a complete statement */
5048e3e3a7aSWarner Losh     size_t len;
5058e3e3a7aSWarner Losh     const char *line = lua_tolstring(L, 1, &len);  /* get what it has */
5068e3e3a7aSWarner Losh     int status = luaL_loadbuffer(L, line, len, "=stdin");  /* try it */
5078e3e3a7aSWarner Losh     if (!incomplete(L, status) || !pushline(L, 0)) {
5088e3e3a7aSWarner Losh       lua_saveline(L, line);  /* keep history */
5098e3e3a7aSWarner Losh       return status;  /* cannot or should not try to add continuation line */
5108e3e3a7aSWarner Losh     }
5118e3e3a7aSWarner Losh     lua_pushliteral(L, "\n");  /* add newline... */
5128e3e3a7aSWarner Losh     lua_insert(L, -2);  /* ...between the two lines */
5138e3e3a7aSWarner Losh     lua_concat(L, 3);  /* join them */
5148e3e3a7aSWarner Losh   }
5158e3e3a7aSWarner Losh }
5168e3e3a7aSWarner Losh 
5178e3e3a7aSWarner Losh 
5188e3e3a7aSWarner Losh /*
5198e3e3a7aSWarner Losh ** Read a line and try to load (compile) it first as an expression (by
5208e3e3a7aSWarner Losh ** adding "return " in front of it) and second as a statement. Return
5218e3e3a7aSWarner Losh ** the final status of load/call with the resulting function (if any)
5228e3e3a7aSWarner Losh ** in the top of the stack.
5238e3e3a7aSWarner Losh */
loadline(lua_State * L)5248e3e3a7aSWarner Losh static int loadline (lua_State *L) {
5258e3e3a7aSWarner Losh   int status;
5268e3e3a7aSWarner Losh   lua_settop(L, 0);
5278e3e3a7aSWarner Losh   if (!pushline(L, 1))
5288e3e3a7aSWarner Losh     return -1;  /* no input */
5298e3e3a7aSWarner Losh   if ((status = addreturn(L)) != LUA_OK)  /* 'return ...' did not work? */
5308e3e3a7aSWarner Losh     status = multiline(L);  /* try as command, maybe with continuation lines */
5318e3e3a7aSWarner Losh   lua_remove(L, 1);  /* remove line from the stack */
5328e3e3a7aSWarner Losh   lua_assert(lua_gettop(L) == 1);
5338e3e3a7aSWarner Losh   return status;
5348e3e3a7aSWarner Losh }
5358e3e3a7aSWarner Losh 
5368e3e3a7aSWarner Losh 
5378e3e3a7aSWarner Losh /*
5388e3e3a7aSWarner Losh ** Prints (calling the Lua 'print' function) any values on the stack
5398e3e3a7aSWarner Losh */
l_print(lua_State * L)5408e3e3a7aSWarner Losh static void l_print (lua_State *L) {
5418e3e3a7aSWarner Losh   int n = lua_gettop(L);
5428e3e3a7aSWarner Losh   if (n > 0) {  /* any result to be printed? */
5438e3e3a7aSWarner Losh     luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
5448e3e3a7aSWarner Losh     lua_getglobal(L, "print");
5458e3e3a7aSWarner Losh     lua_insert(L, 1);
5468e3e3a7aSWarner Losh     if (lua_pcall(L, n, 0, 0) != LUA_OK)
5478e3e3a7aSWarner Losh       l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)",
5488e3e3a7aSWarner Losh                                              lua_tostring(L, -1)));
5498e3e3a7aSWarner Losh   }
5508e3e3a7aSWarner Losh }
5518e3e3a7aSWarner Losh 
5528e3e3a7aSWarner Losh 
5538e3e3a7aSWarner Losh /*
5548e3e3a7aSWarner Losh ** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and
5558e3e3a7aSWarner Losh ** print any results.
5568e3e3a7aSWarner Losh */
doREPL(lua_State * L)5578e3e3a7aSWarner Losh static void doREPL (lua_State *L) {
5588e3e3a7aSWarner Losh   int status;
5598e3e3a7aSWarner Losh   const char *oldprogname = progname;
5608e3e3a7aSWarner Losh   progname = NULL;  /* no 'progname' on errors in interactive mode */
561*0495ed39SKyle Evans   lua_initreadline(L);
5628e3e3a7aSWarner Losh   while ((status = loadline(L)) != -1) {
5638e3e3a7aSWarner Losh     if (status == LUA_OK)
5648e3e3a7aSWarner Losh       status = docall(L, 0, LUA_MULTRET);
5658e3e3a7aSWarner Losh     if (status == LUA_OK) l_print(L);
5668e3e3a7aSWarner Losh     else report(L, status);
5678e3e3a7aSWarner Losh   }
5688e3e3a7aSWarner Losh   lua_settop(L, 0);  /* clear stack */
5698e3e3a7aSWarner Losh   lua_writeline();
5708e3e3a7aSWarner Losh   progname = oldprogname;
5718e3e3a7aSWarner Losh }
5728e3e3a7aSWarner Losh 
573*0495ed39SKyle Evans /* }================================================================== */
5748e3e3a7aSWarner Losh 
5758e3e3a7aSWarner Losh 
5768e3e3a7aSWarner Losh /*
5778e3e3a7aSWarner Losh ** Main body of stand-alone interpreter (to be called in protected mode).
5788e3e3a7aSWarner Losh ** Reads the options and handles them all.
5798e3e3a7aSWarner Losh */
pmain(lua_State * L)5808e3e3a7aSWarner Losh static int pmain (lua_State *L) {
5818e3e3a7aSWarner Losh   int argc = (int)lua_tointeger(L, 1);
5828e3e3a7aSWarner Losh   char **argv = (char **)lua_touserdata(L, 2);
5838e3e3a7aSWarner Losh   int script;
5848e3e3a7aSWarner Losh   int args = collectargs(argv, &script);
5858e3e3a7aSWarner Losh   luaL_checkversion(L);  /* check that interpreter has correct version */
5868e3e3a7aSWarner Losh   if (argv[0] && argv[0][0]) progname = argv[0];
5878e3e3a7aSWarner Losh   if (args == has_error) {  /* bad arg? */
5888e3e3a7aSWarner Losh     print_usage(argv[script]);  /* 'script' has index of bad arg. */
5898e3e3a7aSWarner Losh     return 0;
5908e3e3a7aSWarner Losh   }
5918e3e3a7aSWarner Losh   if (args & has_v)  /* option '-v'? */
5928e3e3a7aSWarner Losh     print_version();
5938e3e3a7aSWarner Losh   if (args & has_E) {  /* option '-E'? */
5948e3e3a7aSWarner Losh     lua_pushboolean(L, 1);  /* signal for libraries to ignore env. vars. */
5958e3e3a7aSWarner Losh     lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
5968e3e3a7aSWarner Losh   }
5978e3e3a7aSWarner Losh   luaL_openlibs(L);  /* open standard libraries */
5988e3e3a7aSWarner Losh   createargtable(L, argv, argc, script);  /* create table 'arg' */
599*0495ed39SKyle Evans   lua_gc(L, LUA_GCGEN, 0, 0);  /* GC in generational mode */
6008e3e3a7aSWarner Losh   if (!(args & has_E)) {  /* no option '-E'? */
6018e3e3a7aSWarner Losh     if (handle_luainit(L) != LUA_OK)  /* run LUA_INIT */
6028e3e3a7aSWarner Losh       return 0;  /* error running LUA_INIT */
6038e3e3a7aSWarner Losh   }
6048e3e3a7aSWarner Losh   if (!runargs(L, argv, script))  /* execute arguments -e and -l */
6058e3e3a7aSWarner Losh     return 0;  /* something failed */
6068e3e3a7aSWarner Losh   if (script < argc &&  /* execute main script (if there is one) */
6078e3e3a7aSWarner Losh       handle_script(L, argv + script) != LUA_OK)
6088e3e3a7aSWarner Losh     return 0;
6098e3e3a7aSWarner Losh   if (args & has_i)  /* -i option? */
6108e3e3a7aSWarner Losh     doREPL(L);  /* do read-eval-print loop */
6118e3e3a7aSWarner Losh   else if (script == argc && !(args & (has_e | has_v))) {  /* no arguments? */
6128e3e3a7aSWarner Losh     if (lua_stdin_is_tty()) {  /* running in interactive mode? */
6138e3e3a7aSWarner Losh       print_version();
6148e3e3a7aSWarner Losh       doREPL(L);  /* do read-eval-print loop */
6158e3e3a7aSWarner Losh     }
6168e3e3a7aSWarner Losh     else dofile(L, NULL);  /* executes stdin as a file */
6178e3e3a7aSWarner Losh   }
6188e3e3a7aSWarner Losh   lua_pushboolean(L, 1);  /* signal no errors */
6198e3e3a7aSWarner Losh   return 1;
6208e3e3a7aSWarner Losh }
6218e3e3a7aSWarner Losh 
6228e3e3a7aSWarner Losh 
main(int argc,char ** argv)6238e3e3a7aSWarner Losh int main (int argc, char **argv) {
6248e3e3a7aSWarner Losh   int status, result;
6258e3e3a7aSWarner Losh   lua_State *L = luaL_newstate();  /* create state */
6268e3e3a7aSWarner Losh   if (L == NULL) {
6278e3e3a7aSWarner Losh     l_message(argv[0], "cannot create state: not enough memory");
6288e3e3a7aSWarner Losh     return EXIT_FAILURE;
6298e3e3a7aSWarner Losh   }
6308e3e3a7aSWarner Losh   lua_pushcfunction(L, &pmain);  /* to call 'pmain' in protected mode */
6318e3e3a7aSWarner Losh   lua_pushinteger(L, argc);  /* 1st argument */
6328e3e3a7aSWarner Losh   lua_pushlightuserdata(L, argv); /* 2nd argument */
6338e3e3a7aSWarner Losh   status = lua_pcall(L, 2, 1, 0);  /* do the call */
6348e3e3a7aSWarner Losh   result = lua_toboolean(L, -1);  /* get result */
6358e3e3a7aSWarner Losh   report(L, status);
6368e3e3a7aSWarner Losh   lua_close(L);
6378e3e3a7aSWarner Losh   return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
6388e3e3a7aSWarner Losh }
6398e3e3a7aSWarner Losh 
640