xref: /freebsd/contrib/lua/src/lua.c (revision a9490b81)
18e3e3a7aSWarner Losh /*
20495ed39SKyle 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 
160495ed39SKyle Evans #include <signal.h>
170495ed39SKyle 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 
408c784bb8SWarner Losh #if defined(LUA_USE_POSIX)   /* { */
418c784bb8SWarner Losh 
428c784bb8SWarner Losh /*
438c784bb8SWarner Losh ** Use 'sigaction' when available.
448c784bb8SWarner Losh */
setsignal(int sig,void (* handler)(int))458c784bb8SWarner Losh static void setsignal (int sig, void (*handler)(int)) {
468c784bb8SWarner Losh   struct sigaction sa;
478c784bb8SWarner Losh   sa.sa_handler = handler;
488c784bb8SWarner Losh   sa.sa_flags = 0;
498c784bb8SWarner Losh   sigemptyset(&sa.sa_mask);  /* do not mask any signal */
508c784bb8SWarner Losh   sigaction(sig, &sa, NULL);
518c784bb8SWarner Losh }
528c784bb8SWarner Losh 
538c784bb8SWarner Losh #else           /* }{ */
548c784bb8SWarner Losh 
558c784bb8SWarner Losh #define setsignal            signal
568c784bb8SWarner Losh 
578c784bb8SWarner Losh #endif                               /* } */
588c784bb8SWarner Losh 
598c784bb8SWarner Losh 
608e3e3a7aSWarner Losh /*
618e3e3a7aSWarner Losh ** Hook set by signal function to stop the interpreter.
628e3e3a7aSWarner Losh */
lstop(lua_State * L,lua_Debug * ar)638e3e3a7aSWarner Losh static void lstop (lua_State *L, lua_Debug *ar) {
648e3e3a7aSWarner Losh   (void)ar;  /* unused arg. */
658e3e3a7aSWarner Losh   lua_sethook(L, NULL, 0, 0);  /* reset hook */
668e3e3a7aSWarner Losh   luaL_error(L, "interrupted!");
678e3e3a7aSWarner Losh }
688e3e3a7aSWarner Losh 
698e3e3a7aSWarner Losh 
708e3e3a7aSWarner Losh /*
718e3e3a7aSWarner Losh ** Function to be called at a C signal. Because a C signal cannot
728e3e3a7aSWarner Losh ** just change a Lua state (as there is no proper synchronization),
738e3e3a7aSWarner Losh ** this function only sets a hook that, when called, will stop the
748e3e3a7aSWarner Losh ** interpreter.
758e3e3a7aSWarner Losh */
laction(int i)768e3e3a7aSWarner Losh static void laction (int i) {
770495ed39SKyle Evans   int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT;
788c784bb8SWarner Losh   setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
790495ed39SKyle Evans   lua_sethook(globalL, lstop, flag, 1);
808e3e3a7aSWarner Losh }
818e3e3a7aSWarner Losh 
828e3e3a7aSWarner Losh 
print_usage(const char * badoption)838e3e3a7aSWarner Losh static void print_usage (const char *badoption) {
848e3e3a7aSWarner Losh   lua_writestringerror("%s: ", progname);
858e3e3a7aSWarner Losh   if (badoption[1] == 'e' || badoption[1] == 'l')
868e3e3a7aSWarner Losh     lua_writestringerror("'%s' needs argument\n", badoption);
878e3e3a7aSWarner Losh   else
888e3e3a7aSWarner Losh     lua_writestringerror("unrecognized option '%s'\n", badoption);
898e3e3a7aSWarner Losh   lua_writestringerror(
908e3e3a7aSWarner Losh   "usage: %s [options] [script [args]]\n"
918e3e3a7aSWarner Losh   "Available options are:\n"
928e3e3a7aSWarner Losh   "  -e stat   execute string 'stat'\n"
938e3e3a7aSWarner Losh   "  -i        enter interactive mode after executing 'script'\n"
948c784bb8SWarner Losh   "  -l mod    require library 'mod' into global 'mod'\n"
958c784bb8SWarner Losh   "  -l g=mod  require library 'mod' into global 'g'\n"
968e3e3a7aSWarner Losh   "  -v        show version information\n"
978e3e3a7aSWarner Losh   "  -E        ignore environment variables\n"
980495ed39SKyle Evans   "  -W        turn warnings on\n"
998e3e3a7aSWarner Losh   "  --        stop handling options\n"
1008e3e3a7aSWarner Losh   "  -         stop handling options and execute stdin\n"
1018e3e3a7aSWarner Losh   ,
1028e3e3a7aSWarner Losh   progname);
1038e3e3a7aSWarner Losh }
1048e3e3a7aSWarner Losh 
1058e3e3a7aSWarner Losh 
1068e3e3a7aSWarner Losh /*
1078e3e3a7aSWarner Losh ** Prints an error message, adding the program name in front of it
1088e3e3a7aSWarner Losh ** (if present)
1098e3e3a7aSWarner Losh */
l_message(const char * pname,const char * msg)1108e3e3a7aSWarner Losh static void l_message (const char *pname, const char *msg) {
1118e3e3a7aSWarner Losh   if (pname) lua_writestringerror("%s: ", pname);
1128e3e3a7aSWarner Losh   lua_writestringerror("%s\n", msg);
1138e3e3a7aSWarner Losh }
1148e3e3a7aSWarner Losh 
1158e3e3a7aSWarner Losh 
1168e3e3a7aSWarner Losh /*
1178e3e3a7aSWarner Losh ** Check whether 'status' is not OK and, if so, prints the error
1188e3e3a7aSWarner Losh ** message on the top of the stack. It assumes that the error object
1198e3e3a7aSWarner Losh ** is a string, as it was either generated by Lua or by 'msghandler'.
1208e3e3a7aSWarner Losh */
report(lua_State * L,int status)1218e3e3a7aSWarner Losh static int report (lua_State *L, int status) {
1228e3e3a7aSWarner Losh   if (status != LUA_OK) {
1238e3e3a7aSWarner Losh     const char *msg = lua_tostring(L, -1);
1248e3e3a7aSWarner Losh     l_message(progname, msg);
1258e3e3a7aSWarner Losh     lua_pop(L, 1);  /* remove message */
1268e3e3a7aSWarner Losh   }
1278e3e3a7aSWarner Losh   return status;
1288e3e3a7aSWarner Losh }
1298e3e3a7aSWarner Losh 
1308e3e3a7aSWarner Losh 
1318e3e3a7aSWarner Losh /*
1328e3e3a7aSWarner Losh ** Message handler used to run all chunks
1338e3e3a7aSWarner Losh */
msghandler(lua_State * L)1348e3e3a7aSWarner Losh static int msghandler (lua_State *L) {
1358e3e3a7aSWarner Losh   const char *msg = lua_tostring(L, 1);
1368e3e3a7aSWarner Losh   if (msg == NULL) {  /* is error object not a string? */
1378e3e3a7aSWarner Losh     if (luaL_callmeta(L, 1, "__tostring") &&  /* does it have a metamethod */
1388e3e3a7aSWarner Losh         lua_type(L, -1) == LUA_TSTRING)  /* that produces a string? */
1398e3e3a7aSWarner Losh       return 1;  /* that is the message */
1408e3e3a7aSWarner Losh     else
1418e3e3a7aSWarner Losh       msg = lua_pushfstring(L, "(error object is a %s value)",
1428e3e3a7aSWarner Losh                                luaL_typename(L, 1));
1438e3e3a7aSWarner Losh   }
1448e3e3a7aSWarner Losh   luaL_traceback(L, L, msg, 1);  /* append a standard traceback */
1458e3e3a7aSWarner Losh   return 1;  /* return the traceback */
1468e3e3a7aSWarner Losh }
1478e3e3a7aSWarner Losh 
1488e3e3a7aSWarner Losh 
1498e3e3a7aSWarner Losh /*
1508e3e3a7aSWarner Losh ** Interface to 'lua_pcall', which sets appropriate message function
1518e3e3a7aSWarner Losh ** and C-signal handler. Used to run all chunks.
1528e3e3a7aSWarner Losh */
docall(lua_State * L,int narg,int nres)1538e3e3a7aSWarner Losh static int docall (lua_State *L, int narg, int nres) {
1548e3e3a7aSWarner Losh   int status;
1558e3e3a7aSWarner Losh   int base = lua_gettop(L) - narg;  /* function index */
1568e3e3a7aSWarner Losh   lua_pushcfunction(L, msghandler);  /* push message handler */
1578e3e3a7aSWarner Losh   lua_insert(L, base);  /* put it under function and args */
1588e3e3a7aSWarner Losh   globalL = L;  /* to be available to 'laction' */
1598c784bb8SWarner Losh   setsignal(SIGINT, laction);  /* set C-signal handler */
1608e3e3a7aSWarner Losh   status = lua_pcall(L, narg, nres, base);
1618c784bb8SWarner Losh   setsignal(SIGINT, SIG_DFL); /* reset C-signal handler */
1628e3e3a7aSWarner Losh   lua_remove(L, base);  /* remove message handler from the stack */
1638e3e3a7aSWarner Losh   return status;
1648e3e3a7aSWarner Losh }
1658e3e3a7aSWarner Losh 
1668e3e3a7aSWarner Losh 
print_version(void)1678e3e3a7aSWarner Losh static void print_version (void) {
1688e3e3a7aSWarner Losh   lua_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT));
1698e3e3a7aSWarner Losh   lua_writeline();
1708e3e3a7aSWarner Losh }
1718e3e3a7aSWarner Losh 
1728e3e3a7aSWarner Losh 
1738e3e3a7aSWarner Losh /*
1748e3e3a7aSWarner Losh ** Create the 'arg' table, which stores all arguments from the
1758e3e3a7aSWarner Losh ** command line ('argv'). It should be aligned so that, at index 0,
1768e3e3a7aSWarner Losh ** it has 'argv[script]', which is the script name. The arguments
1778e3e3a7aSWarner Losh ** to the script (everything after 'script') go to positive indices;
1788e3e3a7aSWarner Losh ** other arguments (before the script name) go to negative indices.
1798e3e3a7aSWarner Losh ** If there is no script name, assume interpreter's name as base.
180*a9490b81SWarner Losh ** (If there is no interpreter's name either, 'script' is -1, so
181*a9490b81SWarner Losh ** table sizes are zero.)
1828e3e3a7aSWarner Losh */
createargtable(lua_State * L,char ** argv,int argc,int script)1838e3e3a7aSWarner Losh static void createargtable (lua_State *L, char **argv, int argc, int script) {
1848e3e3a7aSWarner Losh   int i, narg;
1858e3e3a7aSWarner Losh   narg = argc - (script + 1);  /* number of positive indices */
1868e3e3a7aSWarner Losh   lua_createtable(L, narg, script + 1);
1878e3e3a7aSWarner Losh   for (i = 0; i < argc; i++) {
1888e3e3a7aSWarner Losh     lua_pushstring(L, argv[i]);
1898e3e3a7aSWarner Losh     lua_rawseti(L, -2, i - script);
1908e3e3a7aSWarner Losh   }
1918e3e3a7aSWarner Losh   lua_setglobal(L, "arg");
1928e3e3a7aSWarner Losh }
1938e3e3a7aSWarner Losh 
1948e3e3a7aSWarner Losh 
dochunk(lua_State * L,int status)1958e3e3a7aSWarner Losh static int dochunk (lua_State *L, int status) {
1968e3e3a7aSWarner Losh   if (status == LUA_OK) status = docall(L, 0, 0);
1978e3e3a7aSWarner Losh   return report(L, status);
1988e3e3a7aSWarner Losh }
1998e3e3a7aSWarner Losh 
2008e3e3a7aSWarner Losh 
dofile(lua_State * L,const char * name)2018e3e3a7aSWarner Losh static int dofile (lua_State *L, const char *name) {
2028e3e3a7aSWarner Losh   return dochunk(L, luaL_loadfile(L, name));
2038e3e3a7aSWarner Losh }
2048e3e3a7aSWarner Losh 
2058e3e3a7aSWarner Losh 
dostring(lua_State * L,const char * s,const char * name)2068e3e3a7aSWarner Losh static int dostring (lua_State *L, const char *s, const char *name) {
2078e3e3a7aSWarner Losh   return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name));
2088e3e3a7aSWarner Losh }
2098e3e3a7aSWarner Losh 
2108e3e3a7aSWarner Losh 
2118e3e3a7aSWarner Losh /*
2128c784bb8SWarner Losh ** Receives 'globname[=modname]' and runs 'globname = require(modname)'.
2138e3e3a7aSWarner Losh */
dolibrary(lua_State * L,char * globname)2148c784bb8SWarner Losh static int dolibrary (lua_State *L, char *globname) {
2158e3e3a7aSWarner Losh   int status;
2168c784bb8SWarner Losh   char *modname = strchr(globname, '=');
2178c784bb8SWarner Losh   if (modname == NULL)  /* no explicit name? */
2188c784bb8SWarner Losh     modname = globname;  /* module name is equal to global name */
2198c784bb8SWarner Losh   else {
2208c784bb8SWarner Losh     *modname = '\0';  /* global name ends here */
2218c784bb8SWarner Losh     modname++;  /* module name starts after the '=' */
2228c784bb8SWarner Losh   }
2238e3e3a7aSWarner Losh   lua_getglobal(L, "require");
2248c784bb8SWarner Losh   lua_pushstring(L, modname);
2258c784bb8SWarner Losh   status = docall(L, 1, 1);  /* call 'require(modname)' */
2268e3e3a7aSWarner Losh   if (status == LUA_OK)
2278c784bb8SWarner Losh     lua_setglobal(L, globname);  /* globname = require(modname) */
2288e3e3a7aSWarner Losh   return report(L, status);
2298e3e3a7aSWarner Losh }
2308e3e3a7aSWarner Losh 
2318e3e3a7aSWarner Losh 
2328e3e3a7aSWarner Losh /*
2330495ed39SKyle Evans ** Push on the stack the contents of table 'arg' from 1 to #arg
2340495ed39SKyle Evans */
pushargs(lua_State * L)2350495ed39SKyle Evans static int pushargs (lua_State *L) {
2360495ed39SKyle Evans   int i, n;
2370495ed39SKyle Evans   if (lua_getglobal(L, "arg") != LUA_TTABLE)
2380495ed39SKyle Evans     luaL_error(L, "'arg' is not a table");
2390495ed39SKyle Evans   n = (int)luaL_len(L, -1);
2400495ed39SKyle Evans   luaL_checkstack(L, n + 3, "too many arguments to script");
2410495ed39SKyle Evans   for (i = 1; i <= n; i++)
2420495ed39SKyle Evans     lua_rawgeti(L, -i, i);
2430495ed39SKyle Evans   lua_remove(L, -i);  /* remove table from the stack */
2440495ed39SKyle Evans   return n;
2450495ed39SKyle Evans }
2460495ed39SKyle Evans 
2470495ed39SKyle Evans 
handle_script(lua_State * L,char ** argv)2480495ed39SKyle Evans static int handle_script (lua_State *L, char **argv) {
2490495ed39SKyle Evans   int status;
2500495ed39SKyle Evans   const char *fname = argv[0];
2510495ed39SKyle Evans   if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
2520495ed39SKyle Evans     fname = NULL;  /* stdin */
2530495ed39SKyle Evans   status = luaL_loadfile(L, fname);
2540495ed39SKyle Evans   if (status == LUA_OK) {
2550495ed39SKyle Evans     int n = pushargs(L);  /* push arguments to script */
2560495ed39SKyle Evans     status = docall(L, n, LUA_MULTRET);
2570495ed39SKyle Evans   }
2580495ed39SKyle Evans   return report(L, status);
2590495ed39SKyle Evans }
2600495ed39SKyle Evans 
2610495ed39SKyle Evans 
2620495ed39SKyle Evans /* bits of various argument indicators in 'args' */
2630495ed39SKyle Evans #define has_error	1	/* bad option */
2640495ed39SKyle Evans #define has_i		2	/* -i */
2650495ed39SKyle Evans #define has_v		4	/* -v */
2660495ed39SKyle Evans #define has_e		8	/* -e */
2670495ed39SKyle Evans #define has_E		16	/* -E */
2680495ed39SKyle Evans 
2690495ed39SKyle Evans 
2700495ed39SKyle Evans /*
2710495ed39SKyle Evans ** Traverses all arguments from 'argv', returning a mask with those
272*a9490b81SWarner Losh ** needed before running any Lua code or an error code if it finds any
273*a9490b81SWarner Losh ** invalid argument. In case of error, 'first' is the index of the bad
274*a9490b81SWarner Losh ** argument.  Otherwise, 'first' is -1 if there is no program name,
275*a9490b81SWarner Losh ** 0 if there is no script name, or the index of the script name.
2760495ed39SKyle Evans */
collectargs(char ** argv,int * first)2770495ed39SKyle Evans static int collectargs (char **argv, int *first) {
2780495ed39SKyle Evans   int args = 0;
2790495ed39SKyle Evans   int i;
280*a9490b81SWarner Losh   if (argv[0] != NULL) {  /* is there a program name? */
281*a9490b81SWarner Losh     if (argv[0][0])  /* not empty? */
282*a9490b81SWarner Losh       progname = argv[0];  /* save it */
283*a9490b81SWarner Losh   }
284*a9490b81SWarner Losh   else {  /* no program name */
285*a9490b81SWarner Losh     *first = -1;
286*a9490b81SWarner Losh     return 0;
287*a9490b81SWarner Losh   }
288*a9490b81SWarner Losh   for (i = 1; argv[i] != NULL; i++) {  /* handle arguments */
2890495ed39SKyle Evans     *first = i;
2900495ed39SKyle Evans     if (argv[i][0] != '-')  /* not an option? */
2910495ed39SKyle Evans         return args;  /* stop handling options */
2920495ed39SKyle Evans     switch (argv[i][1]) {  /* else check option */
2930495ed39SKyle Evans       case '-':  /* '--' */
2940495ed39SKyle Evans         if (argv[i][2] != '\0')  /* extra characters after '--'? */
2950495ed39SKyle Evans           return has_error;  /* invalid option */
2960495ed39SKyle Evans         *first = i + 1;
2970495ed39SKyle Evans         return args;
2980495ed39SKyle Evans       case '\0':  /* '-' */
2990495ed39SKyle Evans         return args;  /* script "name" is '-' */
3000495ed39SKyle Evans       case 'E':
3010495ed39SKyle Evans         if (argv[i][2] != '\0')  /* extra characters? */
3020495ed39SKyle Evans           return has_error;  /* invalid option */
3030495ed39SKyle Evans         args |= has_E;
3040495ed39SKyle Evans         break;
3050495ed39SKyle Evans       case 'W':
3060495ed39SKyle Evans         if (argv[i][2] != '\0')  /* extra characters? */
3070495ed39SKyle Evans           return has_error;  /* invalid option */
3080495ed39SKyle Evans         break;
3090495ed39SKyle Evans       case 'i':
3100495ed39SKyle Evans         args |= has_i;  /* (-i implies -v) *//* FALLTHROUGH */
3110495ed39SKyle Evans       case 'v':
3120495ed39SKyle Evans         if (argv[i][2] != '\0')  /* extra characters? */
3130495ed39SKyle Evans           return has_error;  /* invalid option */
3140495ed39SKyle Evans         args |= has_v;
3150495ed39SKyle Evans         break;
3160495ed39SKyle Evans       case 'e':
3170495ed39SKyle Evans         args |= has_e;  /* FALLTHROUGH */
3180495ed39SKyle Evans       case 'l':  /* both options need an argument */
3190495ed39SKyle Evans         if (argv[i][2] == '\0') {  /* no concatenated argument? */
3200495ed39SKyle Evans           i++;  /* try next 'argv' */
3210495ed39SKyle Evans           if (argv[i] == NULL || argv[i][0] == '-')
3220495ed39SKyle Evans             return has_error;  /* no next argument or it is another option */
3230495ed39SKyle Evans         }
3240495ed39SKyle Evans         break;
3250495ed39SKyle Evans       default:  /* invalid option */
3260495ed39SKyle Evans         return has_error;
3270495ed39SKyle Evans     }
3280495ed39SKyle Evans   }
329*a9490b81SWarner Losh   *first = 0;  /* no script name */
3300495ed39SKyle Evans   return args;
3310495ed39SKyle Evans }
3320495ed39SKyle Evans 
3330495ed39SKyle Evans 
3340495ed39SKyle Evans /*
3350495ed39SKyle Evans ** Processes options 'e' and 'l', which involve running Lua code, and
3360495ed39SKyle Evans ** 'W', which also affects the state.
3370495ed39SKyle Evans ** Returns 0 if some code raises an error.
3380495ed39SKyle Evans */
runargs(lua_State * L,char ** argv,int n)3390495ed39SKyle Evans static int runargs (lua_State *L, char **argv, int n) {
3400495ed39SKyle Evans   int i;
3410495ed39SKyle Evans   for (i = 1; i < n; i++) {
3420495ed39SKyle Evans     int option = argv[i][1];
3430495ed39SKyle Evans     lua_assert(argv[i][0] == '-');  /* already checked */
3440495ed39SKyle Evans     switch (option) {
3450495ed39SKyle Evans       case 'e':  case 'l': {
3460495ed39SKyle Evans         int status;
3478c784bb8SWarner Losh         char *extra = argv[i] + 2;  /* both options need an argument */
3480495ed39SKyle Evans         if (*extra == '\0') extra = argv[++i];
3490495ed39SKyle Evans         lua_assert(extra != NULL);
3500495ed39SKyle Evans         status = (option == 'e')
3510495ed39SKyle Evans                  ? dostring(L, extra, "=(command line)")
3520495ed39SKyle Evans                  : dolibrary(L, extra);
3530495ed39SKyle Evans         if (status != LUA_OK) return 0;
3540495ed39SKyle Evans         break;
3550495ed39SKyle Evans       }
3560495ed39SKyle Evans       case 'W':
3570495ed39SKyle Evans         lua_warning(L, "@on", 0);  /* warnings on */
3580495ed39SKyle Evans         break;
3590495ed39SKyle Evans     }
3600495ed39SKyle Evans   }
3610495ed39SKyle Evans   return 1;
3620495ed39SKyle Evans }
3630495ed39SKyle Evans 
3640495ed39SKyle Evans 
handle_luainit(lua_State * L)3650495ed39SKyle Evans static int handle_luainit (lua_State *L) {
3660495ed39SKyle Evans   const char *name = "=" LUA_INITVARVERSION;
3670495ed39SKyle Evans   const char *init = getenv(name + 1);
3680495ed39SKyle Evans   if (init == NULL) {
3690495ed39SKyle Evans     name = "=" LUA_INIT_VAR;
3700495ed39SKyle Evans     init = getenv(name + 1);  /* try alternative name */
3710495ed39SKyle Evans   }
3720495ed39SKyle Evans   if (init == NULL) return LUA_OK;
3730495ed39SKyle Evans   else if (init[0] == '@')
3740495ed39SKyle Evans     return dofile(L, init+1);
3750495ed39SKyle Evans   else
3760495ed39SKyle Evans     return dostring(L, init, name);
3770495ed39SKyle Evans }
3780495ed39SKyle Evans 
3790495ed39SKyle Evans 
3800495ed39SKyle Evans /*
3810495ed39SKyle Evans ** {==================================================================
3820495ed39SKyle Evans ** Read-Eval-Print Loop (REPL)
3830495ed39SKyle Evans ** ===================================================================
3840495ed39SKyle Evans */
3850495ed39SKyle Evans 
3860495ed39SKyle Evans #if !defined(LUA_PROMPT)
3870495ed39SKyle Evans #define LUA_PROMPT		"> "
3880495ed39SKyle Evans #define LUA_PROMPT2		">> "
3890495ed39SKyle Evans #endif
3900495ed39SKyle Evans 
3910495ed39SKyle Evans #if !defined(LUA_MAXINPUT)
3920495ed39SKyle Evans #define LUA_MAXINPUT		512
3930495ed39SKyle Evans #endif
3940495ed39SKyle Evans 
3950495ed39SKyle Evans 
3960495ed39SKyle Evans /*
3970495ed39SKyle Evans ** lua_stdin_is_tty detects whether the standard input is a 'tty' (that
3980495ed39SKyle Evans ** is, whether we're running lua interactively).
3990495ed39SKyle Evans */
4000495ed39SKyle Evans #if !defined(lua_stdin_is_tty)	/* { */
4010495ed39SKyle Evans 
4020495ed39SKyle Evans #if defined(LUA_USE_POSIX)	/* { */
4030495ed39SKyle Evans 
4040495ed39SKyle Evans #include <unistd.h>
4050495ed39SKyle Evans #define lua_stdin_is_tty()	isatty(0)
4060495ed39SKyle Evans 
4070495ed39SKyle Evans #elif defined(LUA_USE_WINDOWS)	/* }{ */
4080495ed39SKyle Evans 
4090495ed39SKyle Evans #include <io.h>
4100495ed39SKyle Evans #include <windows.h>
4110495ed39SKyle Evans 
4120495ed39SKyle Evans #define lua_stdin_is_tty()	_isatty(_fileno(stdin))
4130495ed39SKyle Evans 
4140495ed39SKyle Evans #else				/* }{ */
4150495ed39SKyle Evans 
4160495ed39SKyle Evans /* ISO C definition */
4170495ed39SKyle Evans #define lua_stdin_is_tty()	1  /* assume stdin is a tty */
4180495ed39SKyle Evans 
4190495ed39SKyle Evans #endif				/* } */
4200495ed39SKyle Evans 
4210495ed39SKyle Evans #endif				/* } */
4220495ed39SKyle Evans 
4230495ed39SKyle Evans 
4240495ed39SKyle Evans /*
4250495ed39SKyle Evans ** lua_readline defines how to show a prompt and then read a line from
4260495ed39SKyle Evans ** the standard input.
4270495ed39SKyle Evans ** lua_saveline defines how to "save" a read line in a "history".
4280495ed39SKyle Evans ** lua_freeline defines how to free a line read by lua_readline.
4290495ed39SKyle Evans */
4300495ed39SKyle Evans #if !defined(lua_readline)	/* { */
4310495ed39SKyle Evans 
4320495ed39SKyle Evans #if defined(LUA_USE_READLINE)	/* { */
4330495ed39SKyle Evans 
4340495ed39SKyle Evans #include <readline/readline.h>
4350495ed39SKyle Evans #include <readline/history.h>
4360495ed39SKyle Evans #define lua_initreadline(L)	((void)L, rl_readline_name="lua")
4370495ed39SKyle Evans #define lua_readline(L,b,p)	((void)L, ((b)=readline(p)) != NULL)
4380495ed39SKyle Evans #define lua_saveline(L,line)	((void)L, add_history(line))
4390495ed39SKyle Evans #define lua_freeline(L,b)	((void)L, free(b))
4400495ed39SKyle Evans 
4410495ed39SKyle Evans #else				/* }{ */
4420495ed39SKyle Evans 
4430495ed39SKyle Evans #define lua_initreadline(L)  ((void)L)
4440495ed39SKyle Evans #define lua_readline(L,b,p) \
4450495ed39SKyle Evans         ((void)L, fputs(p, stdout), fflush(stdout),  /* show prompt */ \
4460495ed39SKyle Evans         fgets(b, LUA_MAXINPUT, stdin) != NULL)  /* get line */
4470495ed39SKyle Evans #define lua_saveline(L,line)	{ (void)L; (void)line; }
4480495ed39SKyle Evans #define lua_freeline(L,b)	{ (void)L; (void)b; }
4490495ed39SKyle Evans 
4500495ed39SKyle Evans #endif				/* } */
4510495ed39SKyle Evans 
4520495ed39SKyle Evans #endif				/* } */
4530495ed39SKyle Evans 
4540495ed39SKyle Evans 
4550495ed39SKyle Evans /*
4560495ed39SKyle Evans ** Return the string to be used as a prompt by the interpreter. Leave
4570495ed39SKyle Evans ** the string (or nil, if using the default value) on the stack, to keep
4580495ed39SKyle Evans ** it anchored.
4598e3e3a7aSWarner Losh */
get_prompt(lua_State * L,int firstline)4608e3e3a7aSWarner Losh static const char *get_prompt (lua_State *L, int firstline) {
4610495ed39SKyle Evans   if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL)
4620495ed39SKyle Evans     return (firstline ? LUA_PROMPT : LUA_PROMPT2);  /* use the default */
4630495ed39SKyle Evans   else {  /* apply 'tostring' over the value */
4640495ed39SKyle Evans     const char *p = luaL_tolstring(L, -1, NULL);
4650495ed39SKyle Evans     lua_remove(L, -2);  /* remove original value */
4668e3e3a7aSWarner Losh     return p;
4678e3e3a7aSWarner Losh   }
4680495ed39SKyle Evans }
4698e3e3a7aSWarner Losh 
4708e3e3a7aSWarner Losh /* mark in error messages for incomplete statements */
4718e3e3a7aSWarner Losh #define EOFMARK		"<eof>"
4728e3e3a7aSWarner Losh #define marklen		(sizeof(EOFMARK)/sizeof(char) - 1)
4738e3e3a7aSWarner Losh 
4748e3e3a7aSWarner Losh 
4758e3e3a7aSWarner Losh /*
4768e3e3a7aSWarner Losh ** Check whether 'status' signals a syntax error and the error
4778e3e3a7aSWarner Losh ** message at the top of the stack ends with the above mark for
4788e3e3a7aSWarner Losh ** incomplete statements.
4798e3e3a7aSWarner Losh */
incomplete(lua_State * L,int status)4808e3e3a7aSWarner Losh static int incomplete (lua_State *L, int status) {
4818e3e3a7aSWarner Losh   if (status == LUA_ERRSYNTAX) {
4828e3e3a7aSWarner Losh     size_t lmsg;
4838e3e3a7aSWarner Losh     const char *msg = lua_tolstring(L, -1, &lmsg);
4848e3e3a7aSWarner Losh     if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) {
4858e3e3a7aSWarner Losh       lua_pop(L, 1);
4868e3e3a7aSWarner Losh       return 1;
4878e3e3a7aSWarner Losh     }
4888e3e3a7aSWarner Losh   }
4898e3e3a7aSWarner Losh   return 0;  /* else... */
4908e3e3a7aSWarner Losh }
4918e3e3a7aSWarner Losh 
4928e3e3a7aSWarner Losh 
4938e3e3a7aSWarner Losh /*
4948e3e3a7aSWarner Losh ** Prompt the user, read a line, and push it into the Lua stack.
4958e3e3a7aSWarner Losh */
pushline(lua_State * L,int firstline)4968e3e3a7aSWarner Losh static int pushline (lua_State *L, int firstline) {
4978e3e3a7aSWarner Losh   char buffer[LUA_MAXINPUT];
4988e3e3a7aSWarner Losh   char *b = buffer;
4998e3e3a7aSWarner Losh   size_t l;
5008e3e3a7aSWarner Losh   const char *prmt = get_prompt(L, firstline);
5018e3e3a7aSWarner Losh   int readstatus = lua_readline(L, b, prmt);
5028e3e3a7aSWarner Losh   if (readstatus == 0)
5038e3e3a7aSWarner Losh     return 0;  /* no input (prompt will be popped by caller) */
5048e3e3a7aSWarner Losh   lua_pop(L, 1);  /* remove prompt */
5058e3e3a7aSWarner Losh   l = strlen(b);
5068e3e3a7aSWarner Losh   if (l > 0 && b[l-1] == '\n')  /* line ends with newline? */
5078e3e3a7aSWarner Losh     b[--l] = '\0';  /* remove it */
5088e3e3a7aSWarner Losh   if (firstline && b[0] == '=')  /* for compatibility with 5.2, ... */
5098e3e3a7aSWarner Losh     lua_pushfstring(L, "return %s", b + 1);  /* change '=' to 'return' */
5108e3e3a7aSWarner Losh   else
5118e3e3a7aSWarner Losh     lua_pushlstring(L, b, l);
5128e3e3a7aSWarner Losh   lua_freeline(L, b);
5138e3e3a7aSWarner Losh   return 1;
5148e3e3a7aSWarner Losh }
5158e3e3a7aSWarner Losh 
5168e3e3a7aSWarner Losh 
5178e3e3a7aSWarner Losh /*
5188e3e3a7aSWarner Losh ** Try to compile line on the stack as 'return <line>;'; on return, stack
5198e3e3a7aSWarner Losh ** has either compiled chunk or original line (if compilation failed).
5208e3e3a7aSWarner Losh */
addreturn(lua_State * L)5218e3e3a7aSWarner Losh static int addreturn (lua_State *L) {
5228e3e3a7aSWarner Losh   const char *line = lua_tostring(L, -1);  /* original line */
5238e3e3a7aSWarner Losh   const char *retline = lua_pushfstring(L, "return %s;", line);
5248e3e3a7aSWarner Losh   int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
5258e3e3a7aSWarner Losh   if (status == LUA_OK) {
5268e3e3a7aSWarner Losh     lua_remove(L, -2);  /* remove modified line */
5278e3e3a7aSWarner Losh     if (line[0] != '\0')  /* non empty? */
5288e3e3a7aSWarner Losh       lua_saveline(L, line);  /* keep history */
5298e3e3a7aSWarner Losh   }
5308e3e3a7aSWarner Losh   else
5318e3e3a7aSWarner Losh     lua_pop(L, 2);  /* pop result from 'luaL_loadbuffer' and modified line */
5328e3e3a7aSWarner Losh   return status;
5338e3e3a7aSWarner Losh }
5348e3e3a7aSWarner Losh 
5358e3e3a7aSWarner Losh 
5368e3e3a7aSWarner Losh /*
5378e3e3a7aSWarner Losh ** Read multiple lines until a complete Lua statement
5388e3e3a7aSWarner Losh */
multiline(lua_State * L)5398e3e3a7aSWarner Losh static int multiline (lua_State *L) {
5408e3e3a7aSWarner Losh   for (;;) {  /* repeat until gets a complete statement */
5418e3e3a7aSWarner Losh     size_t len;
5428e3e3a7aSWarner Losh     const char *line = lua_tolstring(L, 1, &len);  /* get what it has */
5438e3e3a7aSWarner Losh     int status = luaL_loadbuffer(L, line, len, "=stdin");  /* try it */
5448e3e3a7aSWarner Losh     if (!incomplete(L, status) || !pushline(L, 0)) {
5458e3e3a7aSWarner Losh       lua_saveline(L, line);  /* keep history */
5468e3e3a7aSWarner Losh       return status;  /* cannot or should not try to add continuation line */
5478e3e3a7aSWarner Losh     }
5488e3e3a7aSWarner Losh     lua_pushliteral(L, "\n");  /* add newline... */
5498e3e3a7aSWarner Losh     lua_insert(L, -2);  /* ...between the two lines */
5508e3e3a7aSWarner Losh     lua_concat(L, 3);  /* join them */
5518e3e3a7aSWarner Losh   }
5528e3e3a7aSWarner Losh }
5538e3e3a7aSWarner Losh 
5548e3e3a7aSWarner Losh 
5558e3e3a7aSWarner Losh /*
5568e3e3a7aSWarner Losh ** Read a line and try to load (compile) it first as an expression (by
5578e3e3a7aSWarner Losh ** adding "return " in front of it) and second as a statement. Return
5588e3e3a7aSWarner Losh ** the final status of load/call with the resulting function (if any)
5598e3e3a7aSWarner Losh ** in the top of the stack.
5608e3e3a7aSWarner Losh */
loadline(lua_State * L)5618e3e3a7aSWarner Losh static int loadline (lua_State *L) {
5628e3e3a7aSWarner Losh   int status;
5638e3e3a7aSWarner Losh   lua_settop(L, 0);
5648e3e3a7aSWarner Losh   if (!pushline(L, 1))
5658e3e3a7aSWarner Losh     return -1;  /* no input */
5668e3e3a7aSWarner Losh   if ((status = addreturn(L)) != LUA_OK)  /* 'return ...' did not work? */
5678e3e3a7aSWarner Losh     status = multiline(L);  /* try as command, maybe with continuation lines */
5688e3e3a7aSWarner Losh   lua_remove(L, 1);  /* remove line from the stack */
5698e3e3a7aSWarner Losh   lua_assert(lua_gettop(L) == 1);
5708e3e3a7aSWarner Losh   return status;
5718e3e3a7aSWarner Losh }
5728e3e3a7aSWarner Losh 
5738e3e3a7aSWarner Losh 
5748e3e3a7aSWarner Losh /*
5758e3e3a7aSWarner Losh ** Prints (calling the Lua 'print' function) any values on the stack
5768e3e3a7aSWarner Losh */
l_print(lua_State * L)5778e3e3a7aSWarner Losh static void l_print (lua_State *L) {
5788e3e3a7aSWarner Losh   int n = lua_gettop(L);
5798e3e3a7aSWarner Losh   if (n > 0) {  /* any result to be printed? */
5808e3e3a7aSWarner Losh     luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
5818e3e3a7aSWarner Losh     lua_getglobal(L, "print");
5828e3e3a7aSWarner Losh     lua_insert(L, 1);
5838e3e3a7aSWarner Losh     if (lua_pcall(L, n, 0, 0) != LUA_OK)
5848e3e3a7aSWarner Losh       l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)",
5858e3e3a7aSWarner Losh                                              lua_tostring(L, -1)));
5868e3e3a7aSWarner Losh   }
5878e3e3a7aSWarner Losh }
5888e3e3a7aSWarner Losh 
5898e3e3a7aSWarner Losh 
5908e3e3a7aSWarner Losh /*
5918e3e3a7aSWarner Losh ** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and
5928e3e3a7aSWarner Losh ** print any results.
5938e3e3a7aSWarner Losh */
doREPL(lua_State * L)5948e3e3a7aSWarner Losh static void doREPL (lua_State *L) {
5958e3e3a7aSWarner Losh   int status;
5968e3e3a7aSWarner Losh   const char *oldprogname = progname;
5978e3e3a7aSWarner Losh   progname = NULL;  /* no 'progname' on errors in interactive mode */
5980495ed39SKyle Evans   lua_initreadline(L);
5998e3e3a7aSWarner Losh   while ((status = loadline(L)) != -1) {
6008e3e3a7aSWarner Losh     if (status == LUA_OK)
6018e3e3a7aSWarner Losh       status = docall(L, 0, LUA_MULTRET);
6028e3e3a7aSWarner Losh     if (status == LUA_OK) l_print(L);
6038e3e3a7aSWarner Losh     else report(L, status);
6048e3e3a7aSWarner Losh   }
6058e3e3a7aSWarner Losh   lua_settop(L, 0);  /* clear stack */
6068e3e3a7aSWarner Losh   lua_writeline();
6078e3e3a7aSWarner Losh   progname = oldprogname;
6088e3e3a7aSWarner Losh }
6098e3e3a7aSWarner Losh 
6100495ed39SKyle Evans /* }================================================================== */
6118e3e3a7aSWarner Losh 
6128e3e3a7aSWarner Losh 
6138e3e3a7aSWarner Losh /*
6148e3e3a7aSWarner Losh ** Main body of stand-alone interpreter (to be called in protected mode).
6158e3e3a7aSWarner Losh ** Reads the options and handles them all.
6168e3e3a7aSWarner Losh */
pmain(lua_State * L)6178e3e3a7aSWarner Losh static int pmain (lua_State *L) {
6188e3e3a7aSWarner Losh   int argc = (int)lua_tointeger(L, 1);
6198e3e3a7aSWarner Losh   char **argv = (char **)lua_touserdata(L, 2);
6208e3e3a7aSWarner Losh   int script;
6218e3e3a7aSWarner Losh   int args = collectargs(argv, &script);
622*a9490b81SWarner Losh   int optlim = (script > 0) ? script : argc; /* first argv not an option */
6238e3e3a7aSWarner Losh   luaL_checkversion(L);  /* check that interpreter has correct version */
6248e3e3a7aSWarner Losh   if (args == has_error) {  /* bad arg? */
6258e3e3a7aSWarner Losh     print_usage(argv[script]);  /* 'script' has index of bad arg. */
6268e3e3a7aSWarner Losh     return 0;
6278e3e3a7aSWarner Losh   }
6288e3e3a7aSWarner Losh   if (args & has_v)  /* option '-v'? */
6298e3e3a7aSWarner Losh     print_version();
6308e3e3a7aSWarner Losh   if (args & has_E) {  /* option '-E'? */
6318e3e3a7aSWarner Losh     lua_pushboolean(L, 1);  /* signal for libraries to ignore env. vars. */
6328e3e3a7aSWarner Losh     lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
6338e3e3a7aSWarner Losh   }
6348e3e3a7aSWarner Losh   luaL_openlibs(L);  /* open standard libraries */
6358e3e3a7aSWarner Losh   createargtable(L, argv, argc, script);  /* create table 'arg' */
636*a9490b81SWarner Losh   lua_gc(L, LUA_GCRESTART);  /* start GC... */
637*a9490b81SWarner Losh   lua_gc(L, LUA_GCGEN, 0, 0);  /* ...in generational mode */
6388e3e3a7aSWarner Losh   if (!(args & has_E)) {  /* no option '-E'? */
6398e3e3a7aSWarner Losh     if (handle_luainit(L) != LUA_OK)  /* run LUA_INIT */
6408e3e3a7aSWarner Losh       return 0;  /* error running LUA_INIT */
6418e3e3a7aSWarner Losh   }
642*a9490b81SWarner Losh   if (!runargs(L, argv, optlim))  /* execute arguments -e and -l */
6438e3e3a7aSWarner Losh     return 0;  /* something failed */
644*a9490b81SWarner Losh   if (script > 0) {  /* execute main script (if there is one) */
645*a9490b81SWarner Losh     if (handle_script(L, argv + script) != LUA_OK)
646*a9490b81SWarner Losh       return 0;  /* interrupt in case of error */
647*a9490b81SWarner Losh   }
6488e3e3a7aSWarner Losh   if (args & has_i)  /* -i option? */
6498e3e3a7aSWarner Losh     doREPL(L);  /* do read-eval-print loop */
650*a9490b81SWarner Losh   else if (script < 1 && !(args & (has_e | has_v))) { /* no active option? */
6518e3e3a7aSWarner Losh     if (lua_stdin_is_tty()) {  /* running in interactive mode? */
6528e3e3a7aSWarner Losh       print_version();
6538e3e3a7aSWarner Losh       doREPL(L);  /* do read-eval-print loop */
6548e3e3a7aSWarner Losh     }
6558e3e3a7aSWarner Losh     else dofile(L, NULL);  /* executes stdin as a file */
6568e3e3a7aSWarner Losh   }
6578e3e3a7aSWarner Losh   lua_pushboolean(L, 1);  /* signal no errors */
6588e3e3a7aSWarner Losh   return 1;
6598e3e3a7aSWarner Losh }
6608e3e3a7aSWarner Losh 
6618e3e3a7aSWarner Losh 
main(int argc,char ** argv)6628e3e3a7aSWarner Losh int main (int argc, char **argv) {
6638e3e3a7aSWarner Losh   int status, result;
6648e3e3a7aSWarner Losh   lua_State *L = luaL_newstate();  /* create state */
6658e3e3a7aSWarner Losh   if (L == NULL) {
6668e3e3a7aSWarner Losh     l_message(argv[0], "cannot create state: not enough memory");
6678e3e3a7aSWarner Losh     return EXIT_FAILURE;
6688e3e3a7aSWarner Losh   }
669*a9490b81SWarner Losh   lua_gc(L, LUA_GCSTOP);  /* stop GC while building state */
6708e3e3a7aSWarner Losh   lua_pushcfunction(L, &pmain);  /* to call 'pmain' in protected mode */
6718e3e3a7aSWarner Losh   lua_pushinteger(L, argc);  /* 1st argument */
6728e3e3a7aSWarner Losh   lua_pushlightuserdata(L, argv); /* 2nd argument */
6738e3e3a7aSWarner Losh   status = lua_pcall(L, 2, 1, 0);  /* do the call */
6748e3e3a7aSWarner Losh   result = lua_toboolean(L, -1);  /* get result */
6758e3e3a7aSWarner Losh   report(L, status);
6768e3e3a7aSWarner Losh   lua_close(L);
6778e3e3a7aSWarner Losh   return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
6788e3e3a7aSWarner Losh }
6798e3e3a7aSWarner Losh 
680