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 40*8c784bb8SWarner Losh #if defined(LUA_USE_POSIX) /* { */ 41*8c784bb8SWarner Losh 42*8c784bb8SWarner Losh /* 43*8c784bb8SWarner Losh ** Use 'sigaction' when available. 44*8c784bb8SWarner Losh */ 45*8c784bb8SWarner Losh static void setsignal (int sig, void (*handler)(int)) { 46*8c784bb8SWarner Losh struct sigaction sa; 47*8c784bb8SWarner Losh sa.sa_handler = handler; 48*8c784bb8SWarner Losh sa.sa_flags = 0; 49*8c784bb8SWarner Losh sigemptyset(&sa.sa_mask); /* do not mask any signal */ 50*8c784bb8SWarner Losh sigaction(sig, &sa, NULL); 51*8c784bb8SWarner Losh } 52*8c784bb8SWarner Losh 53*8c784bb8SWarner Losh #else /* }{ */ 54*8c784bb8SWarner Losh 55*8c784bb8SWarner Losh #define setsignal signal 56*8c784bb8SWarner Losh 57*8c784bb8SWarner Losh #endif /* } */ 58*8c784bb8SWarner Losh 59*8c784bb8SWarner Losh 608e3e3a7aSWarner Losh /* 618e3e3a7aSWarner Losh ** Hook set by signal function to stop the interpreter. 628e3e3a7aSWarner Losh */ 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 */ 768e3e3a7aSWarner Losh static void laction (int i) { 770495ed39SKyle Evans int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT; 78*8c784bb8SWarner 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 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" 94*8c784bb8SWarner Losh " -l mod require library 'mod' into global 'mod'\n" 95*8c784bb8SWarner 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 */ 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 */ 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 */ 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 */ 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' */ 159*8c784bb8SWarner Losh setsignal(SIGINT, laction); /* set C-signal handler */ 1608e3e3a7aSWarner Losh status = lua_pcall(L, narg, nres, base); 161*8c784bb8SWarner 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 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. 1808e3e3a7aSWarner Losh */ 1818e3e3a7aSWarner Losh static void createargtable (lua_State *L, char **argv, int argc, int script) { 1828e3e3a7aSWarner Losh int i, narg; 1838e3e3a7aSWarner Losh if (script == argc) script = 0; /* no script name? */ 1848e3e3a7aSWarner Losh narg = argc - (script + 1); /* number of positive indices */ 1858e3e3a7aSWarner Losh lua_createtable(L, narg, script + 1); 1868e3e3a7aSWarner Losh for (i = 0; i < argc; i++) { 1878e3e3a7aSWarner Losh lua_pushstring(L, argv[i]); 1888e3e3a7aSWarner Losh lua_rawseti(L, -2, i - script); 1898e3e3a7aSWarner Losh } 1908e3e3a7aSWarner Losh lua_setglobal(L, "arg"); 1918e3e3a7aSWarner Losh } 1928e3e3a7aSWarner Losh 1938e3e3a7aSWarner Losh 1948e3e3a7aSWarner Losh static int dochunk (lua_State *L, int status) { 1958e3e3a7aSWarner Losh if (status == LUA_OK) status = docall(L, 0, 0); 1968e3e3a7aSWarner Losh return report(L, status); 1978e3e3a7aSWarner Losh } 1988e3e3a7aSWarner Losh 1998e3e3a7aSWarner Losh 2008e3e3a7aSWarner Losh static int dofile (lua_State *L, const char *name) { 2018e3e3a7aSWarner Losh return dochunk(L, luaL_loadfile(L, name)); 2028e3e3a7aSWarner Losh } 2038e3e3a7aSWarner Losh 2048e3e3a7aSWarner Losh 2058e3e3a7aSWarner Losh static int dostring (lua_State *L, const char *s, const char *name) { 2068e3e3a7aSWarner Losh return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name)); 2078e3e3a7aSWarner Losh } 2088e3e3a7aSWarner Losh 2098e3e3a7aSWarner Losh 2108e3e3a7aSWarner Losh /* 211*8c784bb8SWarner Losh ** Receives 'globname[=modname]' and runs 'globname = require(modname)'. 2128e3e3a7aSWarner Losh */ 213*8c784bb8SWarner Losh static int dolibrary (lua_State *L, char *globname) { 2148e3e3a7aSWarner Losh int status; 215*8c784bb8SWarner Losh char *modname = strchr(globname, '='); 216*8c784bb8SWarner Losh if (modname == NULL) /* no explicit name? */ 217*8c784bb8SWarner Losh modname = globname; /* module name is equal to global name */ 218*8c784bb8SWarner Losh else { 219*8c784bb8SWarner Losh *modname = '\0'; /* global name ends here */ 220*8c784bb8SWarner Losh modname++; /* module name starts after the '=' */ 221*8c784bb8SWarner Losh } 2228e3e3a7aSWarner Losh lua_getglobal(L, "require"); 223*8c784bb8SWarner Losh lua_pushstring(L, modname); 224*8c784bb8SWarner Losh status = docall(L, 1, 1); /* call 'require(modname)' */ 2258e3e3a7aSWarner Losh if (status == LUA_OK) 226*8c784bb8SWarner Losh lua_setglobal(L, globname); /* globname = require(modname) */ 2278e3e3a7aSWarner Losh return report(L, status); 2288e3e3a7aSWarner Losh } 2298e3e3a7aSWarner Losh 2308e3e3a7aSWarner Losh 2318e3e3a7aSWarner Losh /* 2320495ed39SKyle Evans ** Push on the stack the contents of table 'arg' from 1 to #arg 2330495ed39SKyle Evans */ 2340495ed39SKyle Evans static int pushargs (lua_State *L) { 2350495ed39SKyle Evans int i, n; 2360495ed39SKyle Evans if (lua_getglobal(L, "arg") != LUA_TTABLE) 2370495ed39SKyle Evans luaL_error(L, "'arg' is not a table"); 2380495ed39SKyle Evans n = (int)luaL_len(L, -1); 2390495ed39SKyle Evans luaL_checkstack(L, n + 3, "too many arguments to script"); 2400495ed39SKyle Evans for (i = 1; i <= n; i++) 2410495ed39SKyle Evans lua_rawgeti(L, -i, i); 2420495ed39SKyle Evans lua_remove(L, -i); /* remove table from the stack */ 2430495ed39SKyle Evans return n; 2440495ed39SKyle Evans } 2450495ed39SKyle Evans 2460495ed39SKyle Evans 2470495ed39SKyle Evans static int handle_script (lua_State *L, char **argv) { 2480495ed39SKyle Evans int status; 2490495ed39SKyle Evans const char *fname = argv[0]; 2500495ed39SKyle Evans if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) 2510495ed39SKyle Evans fname = NULL; /* stdin */ 2520495ed39SKyle Evans status = luaL_loadfile(L, fname); 2530495ed39SKyle Evans if (status == LUA_OK) { 2540495ed39SKyle Evans int n = pushargs(L); /* push arguments to script */ 2550495ed39SKyle Evans status = docall(L, n, LUA_MULTRET); 2560495ed39SKyle Evans } 2570495ed39SKyle Evans return report(L, status); 2580495ed39SKyle Evans } 2590495ed39SKyle Evans 2600495ed39SKyle Evans 2610495ed39SKyle Evans /* bits of various argument indicators in 'args' */ 2620495ed39SKyle Evans #define has_error 1 /* bad option */ 2630495ed39SKyle Evans #define has_i 2 /* -i */ 2640495ed39SKyle Evans #define has_v 4 /* -v */ 2650495ed39SKyle Evans #define has_e 8 /* -e */ 2660495ed39SKyle Evans #define has_E 16 /* -E */ 2670495ed39SKyle Evans 2680495ed39SKyle Evans 2690495ed39SKyle Evans /* 2700495ed39SKyle Evans ** Traverses all arguments from 'argv', returning a mask with those 2710495ed39SKyle Evans ** needed before running any Lua code (or an error code if it finds 2720495ed39SKyle Evans ** any invalid argument). 'first' returns the first not-handled argument 2730495ed39SKyle Evans ** (either the script name or a bad argument in case of error). 2740495ed39SKyle Evans */ 2750495ed39SKyle Evans static int collectargs (char **argv, int *first) { 2760495ed39SKyle Evans int args = 0; 2770495ed39SKyle Evans int i; 2780495ed39SKyle Evans for (i = 1; argv[i] != NULL; i++) { 2790495ed39SKyle Evans *first = i; 2800495ed39SKyle Evans if (argv[i][0] != '-') /* not an option? */ 2810495ed39SKyle Evans return args; /* stop handling options */ 2820495ed39SKyle Evans switch (argv[i][1]) { /* else check option */ 2830495ed39SKyle Evans case '-': /* '--' */ 2840495ed39SKyle Evans if (argv[i][2] != '\0') /* extra characters after '--'? */ 2850495ed39SKyle Evans return has_error; /* invalid option */ 2860495ed39SKyle Evans *first = i + 1; 2870495ed39SKyle Evans return args; 2880495ed39SKyle Evans case '\0': /* '-' */ 2890495ed39SKyle Evans return args; /* script "name" is '-' */ 2900495ed39SKyle Evans case 'E': 2910495ed39SKyle Evans if (argv[i][2] != '\0') /* extra characters? */ 2920495ed39SKyle Evans return has_error; /* invalid option */ 2930495ed39SKyle Evans args |= has_E; 2940495ed39SKyle Evans break; 2950495ed39SKyle Evans case 'W': 2960495ed39SKyle Evans if (argv[i][2] != '\0') /* extra characters? */ 2970495ed39SKyle Evans return has_error; /* invalid option */ 2980495ed39SKyle Evans break; 2990495ed39SKyle Evans case 'i': 3000495ed39SKyle Evans args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ 3010495ed39SKyle Evans case 'v': 3020495ed39SKyle Evans if (argv[i][2] != '\0') /* extra characters? */ 3030495ed39SKyle Evans return has_error; /* invalid option */ 3040495ed39SKyle Evans args |= has_v; 3050495ed39SKyle Evans break; 3060495ed39SKyle Evans case 'e': 3070495ed39SKyle Evans args |= has_e; /* FALLTHROUGH */ 3080495ed39SKyle Evans case 'l': /* both options need an argument */ 3090495ed39SKyle Evans if (argv[i][2] == '\0') { /* no concatenated argument? */ 3100495ed39SKyle Evans i++; /* try next 'argv' */ 3110495ed39SKyle Evans if (argv[i] == NULL || argv[i][0] == '-') 3120495ed39SKyle Evans return has_error; /* no next argument or it is another option */ 3130495ed39SKyle Evans } 3140495ed39SKyle Evans break; 3150495ed39SKyle Evans default: /* invalid option */ 3160495ed39SKyle Evans return has_error; 3170495ed39SKyle Evans } 3180495ed39SKyle Evans } 3190495ed39SKyle Evans *first = i; /* no script name */ 3200495ed39SKyle Evans return args; 3210495ed39SKyle Evans } 3220495ed39SKyle Evans 3230495ed39SKyle Evans 3240495ed39SKyle Evans /* 3250495ed39SKyle Evans ** Processes options 'e' and 'l', which involve running Lua code, and 3260495ed39SKyle Evans ** 'W', which also affects the state. 3270495ed39SKyle Evans ** Returns 0 if some code raises an error. 3280495ed39SKyle Evans */ 3290495ed39SKyle Evans static int runargs (lua_State *L, char **argv, int n) { 3300495ed39SKyle Evans int i; 3310495ed39SKyle Evans for (i = 1; i < n; i++) { 3320495ed39SKyle Evans int option = argv[i][1]; 3330495ed39SKyle Evans lua_assert(argv[i][0] == '-'); /* already checked */ 3340495ed39SKyle Evans switch (option) { 3350495ed39SKyle Evans case 'e': case 'l': { 3360495ed39SKyle Evans int status; 337*8c784bb8SWarner Losh char *extra = argv[i] + 2; /* both options need an argument */ 3380495ed39SKyle Evans if (*extra == '\0') extra = argv[++i]; 3390495ed39SKyle Evans lua_assert(extra != NULL); 3400495ed39SKyle Evans status = (option == 'e') 3410495ed39SKyle Evans ? dostring(L, extra, "=(command line)") 3420495ed39SKyle Evans : dolibrary(L, extra); 3430495ed39SKyle Evans if (status != LUA_OK) return 0; 3440495ed39SKyle Evans break; 3450495ed39SKyle Evans } 3460495ed39SKyle Evans case 'W': 3470495ed39SKyle Evans lua_warning(L, "@on", 0); /* warnings on */ 3480495ed39SKyle Evans break; 3490495ed39SKyle Evans } 3500495ed39SKyle Evans } 3510495ed39SKyle Evans return 1; 3520495ed39SKyle Evans } 3530495ed39SKyle Evans 3540495ed39SKyle Evans 3550495ed39SKyle Evans static int handle_luainit (lua_State *L) { 3560495ed39SKyle Evans const char *name = "=" LUA_INITVARVERSION; 3570495ed39SKyle Evans const char *init = getenv(name + 1); 3580495ed39SKyle Evans if (init == NULL) { 3590495ed39SKyle Evans name = "=" LUA_INIT_VAR; 3600495ed39SKyle Evans init = getenv(name + 1); /* try alternative name */ 3610495ed39SKyle Evans } 3620495ed39SKyle Evans if (init == NULL) return LUA_OK; 3630495ed39SKyle Evans else if (init[0] == '@') 3640495ed39SKyle Evans return dofile(L, init+1); 3650495ed39SKyle Evans else 3660495ed39SKyle Evans return dostring(L, init, name); 3670495ed39SKyle Evans } 3680495ed39SKyle Evans 3690495ed39SKyle Evans 3700495ed39SKyle Evans /* 3710495ed39SKyle Evans ** {================================================================== 3720495ed39SKyle Evans ** Read-Eval-Print Loop (REPL) 3730495ed39SKyle Evans ** =================================================================== 3740495ed39SKyle Evans */ 3750495ed39SKyle Evans 3760495ed39SKyle Evans #if !defined(LUA_PROMPT) 3770495ed39SKyle Evans #define LUA_PROMPT "> " 3780495ed39SKyle Evans #define LUA_PROMPT2 ">> " 3790495ed39SKyle Evans #endif 3800495ed39SKyle Evans 3810495ed39SKyle Evans #if !defined(LUA_MAXINPUT) 3820495ed39SKyle Evans #define LUA_MAXINPUT 512 3830495ed39SKyle Evans #endif 3840495ed39SKyle Evans 3850495ed39SKyle Evans 3860495ed39SKyle Evans /* 3870495ed39SKyle Evans ** lua_stdin_is_tty detects whether the standard input is a 'tty' (that 3880495ed39SKyle Evans ** is, whether we're running lua interactively). 3890495ed39SKyle Evans */ 3900495ed39SKyle Evans #if !defined(lua_stdin_is_tty) /* { */ 3910495ed39SKyle Evans 3920495ed39SKyle Evans #if defined(LUA_USE_POSIX) /* { */ 3930495ed39SKyle Evans 3940495ed39SKyle Evans #include <unistd.h> 3950495ed39SKyle Evans #define lua_stdin_is_tty() isatty(0) 3960495ed39SKyle Evans 3970495ed39SKyle Evans #elif defined(LUA_USE_WINDOWS) /* }{ */ 3980495ed39SKyle Evans 3990495ed39SKyle Evans #include <io.h> 4000495ed39SKyle Evans #include <windows.h> 4010495ed39SKyle Evans 4020495ed39SKyle Evans #define lua_stdin_is_tty() _isatty(_fileno(stdin)) 4030495ed39SKyle Evans 4040495ed39SKyle Evans #else /* }{ */ 4050495ed39SKyle Evans 4060495ed39SKyle Evans /* ISO C definition */ 4070495ed39SKyle Evans #define lua_stdin_is_tty() 1 /* assume stdin is a tty */ 4080495ed39SKyle Evans 4090495ed39SKyle Evans #endif /* } */ 4100495ed39SKyle Evans 4110495ed39SKyle Evans #endif /* } */ 4120495ed39SKyle Evans 4130495ed39SKyle Evans 4140495ed39SKyle Evans /* 4150495ed39SKyle Evans ** lua_readline defines how to show a prompt and then read a line from 4160495ed39SKyle Evans ** the standard input. 4170495ed39SKyle Evans ** lua_saveline defines how to "save" a read line in a "history". 4180495ed39SKyle Evans ** lua_freeline defines how to free a line read by lua_readline. 4190495ed39SKyle Evans */ 4200495ed39SKyle Evans #if !defined(lua_readline) /* { */ 4210495ed39SKyle Evans 4220495ed39SKyle Evans #if defined(LUA_USE_READLINE) /* { */ 4230495ed39SKyle Evans 4240495ed39SKyle Evans #include <readline/readline.h> 4250495ed39SKyle Evans #include <readline/history.h> 4260495ed39SKyle Evans #define lua_initreadline(L) ((void)L, rl_readline_name="lua") 4270495ed39SKyle Evans #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) 4280495ed39SKyle Evans #define lua_saveline(L,line) ((void)L, add_history(line)) 4290495ed39SKyle Evans #define lua_freeline(L,b) ((void)L, free(b)) 4300495ed39SKyle Evans 4310495ed39SKyle Evans #else /* }{ */ 4320495ed39SKyle Evans 4330495ed39SKyle Evans #define lua_initreadline(L) ((void)L) 4340495ed39SKyle Evans #define lua_readline(L,b,p) \ 4350495ed39SKyle Evans ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ 4360495ed39SKyle Evans fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ 4370495ed39SKyle Evans #define lua_saveline(L,line) { (void)L; (void)line; } 4380495ed39SKyle Evans #define lua_freeline(L,b) { (void)L; (void)b; } 4390495ed39SKyle Evans 4400495ed39SKyle Evans #endif /* } */ 4410495ed39SKyle Evans 4420495ed39SKyle Evans #endif /* } */ 4430495ed39SKyle Evans 4440495ed39SKyle Evans 4450495ed39SKyle Evans /* 4460495ed39SKyle Evans ** Return the string to be used as a prompt by the interpreter. Leave 4470495ed39SKyle Evans ** the string (or nil, if using the default value) on the stack, to keep 4480495ed39SKyle Evans ** it anchored. 4498e3e3a7aSWarner Losh */ 4508e3e3a7aSWarner Losh static const char *get_prompt (lua_State *L, int firstline) { 4510495ed39SKyle Evans if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL) 4520495ed39SKyle Evans return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */ 4530495ed39SKyle Evans else { /* apply 'tostring' over the value */ 4540495ed39SKyle Evans const char *p = luaL_tolstring(L, -1, NULL); 4550495ed39SKyle Evans lua_remove(L, -2); /* remove original value */ 4568e3e3a7aSWarner Losh return p; 4578e3e3a7aSWarner Losh } 4580495ed39SKyle Evans } 4598e3e3a7aSWarner Losh 4608e3e3a7aSWarner Losh /* mark in error messages for incomplete statements */ 4618e3e3a7aSWarner Losh #define EOFMARK "<eof>" 4628e3e3a7aSWarner Losh #define marklen (sizeof(EOFMARK)/sizeof(char) - 1) 4638e3e3a7aSWarner Losh 4648e3e3a7aSWarner Losh 4658e3e3a7aSWarner Losh /* 4668e3e3a7aSWarner Losh ** Check whether 'status' signals a syntax error and the error 4678e3e3a7aSWarner Losh ** message at the top of the stack ends with the above mark for 4688e3e3a7aSWarner Losh ** incomplete statements. 4698e3e3a7aSWarner Losh */ 4708e3e3a7aSWarner Losh static int incomplete (lua_State *L, int status) { 4718e3e3a7aSWarner Losh if (status == LUA_ERRSYNTAX) { 4728e3e3a7aSWarner Losh size_t lmsg; 4738e3e3a7aSWarner Losh const char *msg = lua_tolstring(L, -1, &lmsg); 4748e3e3a7aSWarner Losh if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) { 4758e3e3a7aSWarner Losh lua_pop(L, 1); 4768e3e3a7aSWarner Losh return 1; 4778e3e3a7aSWarner Losh } 4788e3e3a7aSWarner Losh } 4798e3e3a7aSWarner Losh return 0; /* else... */ 4808e3e3a7aSWarner Losh } 4818e3e3a7aSWarner Losh 4828e3e3a7aSWarner Losh 4838e3e3a7aSWarner Losh /* 4848e3e3a7aSWarner Losh ** Prompt the user, read a line, and push it into the Lua stack. 4858e3e3a7aSWarner Losh */ 4868e3e3a7aSWarner Losh static int pushline (lua_State *L, int firstline) { 4878e3e3a7aSWarner Losh char buffer[LUA_MAXINPUT]; 4888e3e3a7aSWarner Losh char *b = buffer; 4898e3e3a7aSWarner Losh size_t l; 4908e3e3a7aSWarner Losh const char *prmt = get_prompt(L, firstline); 4918e3e3a7aSWarner Losh int readstatus = lua_readline(L, b, prmt); 4928e3e3a7aSWarner Losh if (readstatus == 0) 4938e3e3a7aSWarner Losh return 0; /* no input (prompt will be popped by caller) */ 4948e3e3a7aSWarner Losh lua_pop(L, 1); /* remove prompt */ 4958e3e3a7aSWarner Losh l = strlen(b); 4968e3e3a7aSWarner Losh if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ 4978e3e3a7aSWarner Losh b[--l] = '\0'; /* remove it */ 4988e3e3a7aSWarner Losh if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */ 4998e3e3a7aSWarner Losh lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */ 5008e3e3a7aSWarner Losh else 5018e3e3a7aSWarner Losh lua_pushlstring(L, b, l); 5028e3e3a7aSWarner Losh lua_freeline(L, b); 5038e3e3a7aSWarner Losh return 1; 5048e3e3a7aSWarner Losh } 5058e3e3a7aSWarner Losh 5068e3e3a7aSWarner Losh 5078e3e3a7aSWarner Losh /* 5088e3e3a7aSWarner Losh ** Try to compile line on the stack as 'return <line>;'; on return, stack 5098e3e3a7aSWarner Losh ** has either compiled chunk or original line (if compilation failed). 5108e3e3a7aSWarner Losh */ 5118e3e3a7aSWarner Losh static int addreturn (lua_State *L) { 5128e3e3a7aSWarner Losh const char *line = lua_tostring(L, -1); /* original line */ 5138e3e3a7aSWarner Losh const char *retline = lua_pushfstring(L, "return %s;", line); 5148e3e3a7aSWarner Losh int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin"); 5158e3e3a7aSWarner Losh if (status == LUA_OK) { 5168e3e3a7aSWarner Losh lua_remove(L, -2); /* remove modified line */ 5178e3e3a7aSWarner Losh if (line[0] != '\0') /* non empty? */ 5188e3e3a7aSWarner Losh lua_saveline(L, line); /* keep history */ 5198e3e3a7aSWarner Losh } 5208e3e3a7aSWarner Losh else 5218e3e3a7aSWarner Losh lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */ 5228e3e3a7aSWarner Losh return status; 5238e3e3a7aSWarner Losh } 5248e3e3a7aSWarner Losh 5258e3e3a7aSWarner Losh 5268e3e3a7aSWarner Losh /* 5278e3e3a7aSWarner Losh ** Read multiple lines until a complete Lua statement 5288e3e3a7aSWarner Losh */ 5298e3e3a7aSWarner Losh static int multiline (lua_State *L) { 5308e3e3a7aSWarner Losh for (;;) { /* repeat until gets a complete statement */ 5318e3e3a7aSWarner Losh size_t len; 5328e3e3a7aSWarner Losh const char *line = lua_tolstring(L, 1, &len); /* get what it has */ 5338e3e3a7aSWarner Losh int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ 5348e3e3a7aSWarner Losh if (!incomplete(L, status) || !pushline(L, 0)) { 5358e3e3a7aSWarner Losh lua_saveline(L, line); /* keep history */ 5368e3e3a7aSWarner Losh return status; /* cannot or should not try to add continuation line */ 5378e3e3a7aSWarner Losh } 5388e3e3a7aSWarner Losh lua_pushliteral(L, "\n"); /* add newline... */ 5398e3e3a7aSWarner Losh lua_insert(L, -2); /* ...between the two lines */ 5408e3e3a7aSWarner Losh lua_concat(L, 3); /* join them */ 5418e3e3a7aSWarner Losh } 5428e3e3a7aSWarner Losh } 5438e3e3a7aSWarner Losh 5448e3e3a7aSWarner Losh 5458e3e3a7aSWarner Losh /* 5468e3e3a7aSWarner Losh ** Read a line and try to load (compile) it first as an expression (by 5478e3e3a7aSWarner Losh ** adding "return " in front of it) and second as a statement. Return 5488e3e3a7aSWarner Losh ** the final status of load/call with the resulting function (if any) 5498e3e3a7aSWarner Losh ** in the top of the stack. 5508e3e3a7aSWarner Losh */ 5518e3e3a7aSWarner Losh static int loadline (lua_State *L) { 5528e3e3a7aSWarner Losh int status; 5538e3e3a7aSWarner Losh lua_settop(L, 0); 5548e3e3a7aSWarner Losh if (!pushline(L, 1)) 5558e3e3a7aSWarner Losh return -1; /* no input */ 5568e3e3a7aSWarner Losh if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ 5578e3e3a7aSWarner Losh status = multiline(L); /* try as command, maybe with continuation lines */ 5588e3e3a7aSWarner Losh lua_remove(L, 1); /* remove line from the stack */ 5598e3e3a7aSWarner Losh lua_assert(lua_gettop(L) == 1); 5608e3e3a7aSWarner Losh return status; 5618e3e3a7aSWarner Losh } 5628e3e3a7aSWarner Losh 5638e3e3a7aSWarner Losh 5648e3e3a7aSWarner Losh /* 5658e3e3a7aSWarner Losh ** Prints (calling the Lua 'print' function) any values on the stack 5668e3e3a7aSWarner Losh */ 5678e3e3a7aSWarner Losh static void l_print (lua_State *L) { 5688e3e3a7aSWarner Losh int n = lua_gettop(L); 5698e3e3a7aSWarner Losh if (n > 0) { /* any result to be printed? */ 5708e3e3a7aSWarner Losh luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); 5718e3e3a7aSWarner Losh lua_getglobal(L, "print"); 5728e3e3a7aSWarner Losh lua_insert(L, 1); 5738e3e3a7aSWarner Losh if (lua_pcall(L, n, 0, 0) != LUA_OK) 5748e3e3a7aSWarner Losh l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)", 5758e3e3a7aSWarner Losh lua_tostring(L, -1))); 5768e3e3a7aSWarner Losh } 5778e3e3a7aSWarner Losh } 5788e3e3a7aSWarner Losh 5798e3e3a7aSWarner Losh 5808e3e3a7aSWarner Losh /* 5818e3e3a7aSWarner Losh ** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and 5828e3e3a7aSWarner Losh ** print any results. 5838e3e3a7aSWarner Losh */ 5848e3e3a7aSWarner Losh static void doREPL (lua_State *L) { 5858e3e3a7aSWarner Losh int status; 5868e3e3a7aSWarner Losh const char *oldprogname = progname; 5878e3e3a7aSWarner Losh progname = NULL; /* no 'progname' on errors in interactive mode */ 5880495ed39SKyle Evans lua_initreadline(L); 5898e3e3a7aSWarner Losh while ((status = loadline(L)) != -1) { 5908e3e3a7aSWarner Losh if (status == LUA_OK) 5918e3e3a7aSWarner Losh status = docall(L, 0, LUA_MULTRET); 5928e3e3a7aSWarner Losh if (status == LUA_OK) l_print(L); 5938e3e3a7aSWarner Losh else report(L, status); 5948e3e3a7aSWarner Losh } 5958e3e3a7aSWarner Losh lua_settop(L, 0); /* clear stack */ 5968e3e3a7aSWarner Losh lua_writeline(); 5978e3e3a7aSWarner Losh progname = oldprogname; 5988e3e3a7aSWarner Losh } 5998e3e3a7aSWarner Losh 6000495ed39SKyle Evans /* }================================================================== */ 6018e3e3a7aSWarner Losh 6028e3e3a7aSWarner Losh 6038e3e3a7aSWarner Losh /* 6048e3e3a7aSWarner Losh ** Main body of stand-alone interpreter (to be called in protected mode). 6058e3e3a7aSWarner Losh ** Reads the options and handles them all. 6068e3e3a7aSWarner Losh */ 6078e3e3a7aSWarner Losh static int pmain (lua_State *L) { 6088e3e3a7aSWarner Losh int argc = (int)lua_tointeger(L, 1); 6098e3e3a7aSWarner Losh char **argv = (char **)lua_touserdata(L, 2); 6108e3e3a7aSWarner Losh int script; 6118e3e3a7aSWarner Losh int args = collectargs(argv, &script); 6128e3e3a7aSWarner Losh luaL_checkversion(L); /* check that interpreter has correct version */ 6138e3e3a7aSWarner Losh if (argv[0] && argv[0][0]) progname = argv[0]; 6148e3e3a7aSWarner Losh if (args == has_error) { /* bad arg? */ 6158e3e3a7aSWarner Losh print_usage(argv[script]); /* 'script' has index of bad arg. */ 6168e3e3a7aSWarner Losh return 0; 6178e3e3a7aSWarner Losh } 6188e3e3a7aSWarner Losh if (args & has_v) /* option '-v'? */ 6198e3e3a7aSWarner Losh print_version(); 6208e3e3a7aSWarner Losh if (args & has_E) { /* option '-E'? */ 6218e3e3a7aSWarner Losh lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ 6228e3e3a7aSWarner Losh lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); 6238e3e3a7aSWarner Losh } 6248e3e3a7aSWarner Losh luaL_openlibs(L); /* open standard libraries */ 6258e3e3a7aSWarner Losh createargtable(L, argv, argc, script); /* create table 'arg' */ 6260495ed39SKyle Evans lua_gc(L, LUA_GCGEN, 0, 0); /* GC in generational mode */ 6278e3e3a7aSWarner Losh if (!(args & has_E)) { /* no option '-E'? */ 6288e3e3a7aSWarner Losh if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ 6298e3e3a7aSWarner Losh return 0; /* error running LUA_INIT */ 6308e3e3a7aSWarner Losh } 6318e3e3a7aSWarner Losh if (!runargs(L, argv, script)) /* execute arguments -e and -l */ 6328e3e3a7aSWarner Losh return 0; /* something failed */ 6338e3e3a7aSWarner Losh if (script < argc && /* execute main script (if there is one) */ 6348e3e3a7aSWarner Losh handle_script(L, argv + script) != LUA_OK) 6358e3e3a7aSWarner Losh return 0; 6368e3e3a7aSWarner Losh if (args & has_i) /* -i option? */ 6378e3e3a7aSWarner Losh doREPL(L); /* do read-eval-print loop */ 6388e3e3a7aSWarner Losh else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */ 6398e3e3a7aSWarner Losh if (lua_stdin_is_tty()) { /* running in interactive mode? */ 6408e3e3a7aSWarner Losh print_version(); 6418e3e3a7aSWarner Losh doREPL(L); /* do read-eval-print loop */ 6428e3e3a7aSWarner Losh } 6438e3e3a7aSWarner Losh else dofile(L, NULL); /* executes stdin as a file */ 6448e3e3a7aSWarner Losh } 6458e3e3a7aSWarner Losh lua_pushboolean(L, 1); /* signal no errors */ 6468e3e3a7aSWarner Losh return 1; 6478e3e3a7aSWarner Losh } 6488e3e3a7aSWarner Losh 6498e3e3a7aSWarner Losh 6508e3e3a7aSWarner Losh int main (int argc, char **argv) { 6518e3e3a7aSWarner Losh int status, result; 6528e3e3a7aSWarner Losh lua_State *L = luaL_newstate(); /* create state */ 6538e3e3a7aSWarner Losh if (L == NULL) { 6548e3e3a7aSWarner Losh l_message(argv[0], "cannot create state: not enough memory"); 6558e3e3a7aSWarner Losh return EXIT_FAILURE; 6568e3e3a7aSWarner Losh } 6578e3e3a7aSWarner Losh lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ 6588e3e3a7aSWarner Losh lua_pushinteger(L, argc); /* 1st argument */ 6598e3e3a7aSWarner Losh lua_pushlightuserdata(L, argv); /* 2nd argument */ 6608e3e3a7aSWarner Losh status = lua_pcall(L, 2, 1, 0); /* do the call */ 6618e3e3a7aSWarner Losh result = lua_toboolean(L, -1); /* get result */ 6628e3e3a7aSWarner Losh report(L, status); 6638e3e3a7aSWarner Losh lua_close(L); 6648e3e3a7aSWarner Losh return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; 6658e3e3a7aSWarner Losh } 6668e3e3a7aSWarner Losh 667