lua.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /*
  2. ** $Id: lua.c $
  3. ** Lua stand-alone interpreter
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lua_c
  7. #include "lprefix.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include "lua.h"
  13. #include "lauxlib.h"
  14. #include "lualib.h"
  15. #if !defined(LUA_PROGNAME)
  16. #define LUA_PROGNAME "lua"
  17. #endif
  18. #if !defined(LUA_INIT_VAR)
  19. #define LUA_INIT_VAR "LUA_INIT"
  20. #endif
  21. #define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX
  22. static lua_State *globalL = NULL;
  23. static const char *progname = LUA_PROGNAME;
  24. #if defined(LUA_USE_POSIX) /* { */
  25. /*
  26. ** Use 'sigaction' when available.
  27. */
  28. static void setsignal (int sig, void (*handler)(int)) {
  29. struct sigaction sa;
  30. sa.sa_handler = handler;
  31. sa.sa_flags = 0;
  32. sigemptyset(&sa.sa_mask); /* do not mask any signal */
  33. sigaction(sig, &sa, NULL);
  34. }
  35. #else /* }{ */
  36. #define setsignal signal
  37. #endif /* } */
  38. /*
  39. ** Hook set by signal function to stop the interpreter.
  40. */
  41. static void lstop (lua_State *L, lua_Debug *ar) {
  42. (void)ar; /* unused arg. */
  43. lua_sethook(L, NULL, 0, 0); /* reset hook */
  44. luaL_error(L, "interrupted!");
  45. }
  46. /*
  47. ** Function to be called at a C signal. Because a C signal cannot
  48. ** just change a Lua state (as there is no proper synchronization),
  49. ** this function only sets a hook that, when called, will stop the
  50. ** interpreter.
  51. */
  52. static void laction (int i) {
  53. int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT;
  54. setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
  55. lua_sethook(globalL, lstop, flag, 1);
  56. }
  57. static void print_usage (const char *badoption) {
  58. lua_writestringerror("%s: ", progname);
  59. if (badoption[1] == 'e' || badoption[1] == 'l')
  60. lua_writestringerror("'%s' needs argument\n", badoption);
  61. else
  62. lua_writestringerror("unrecognized option '%s'\n", badoption);
  63. lua_writestringerror(
  64. "usage: %s [options] [script [args]]\n"
  65. "Available options are:\n"
  66. " -e stat execute string 'stat'\n"
  67. " -i enter interactive mode after executing 'script'\n"
  68. " -l mod require library 'mod' into global 'mod'\n"
  69. " -l g=mod require library 'mod' into global 'g'\n"
  70. " -v show version information\n"
  71. " -E ignore environment variables\n"
  72. " -W turn warnings on\n"
  73. " -- stop handling options\n"
  74. " - stop handling options and execute stdin\n"
  75. ,
  76. progname);
  77. }
  78. /*
  79. ** Prints an error message, adding the program name in front of it
  80. ** (if present)
  81. */
  82. static void l_message (const char *pname, const char *msg) {
  83. if (pname) lua_writestringerror("%s: ", pname);
  84. lua_writestringerror("%s\n", msg);
  85. }
  86. /*
  87. ** Check whether 'status' is not OK and, if so, prints the error
  88. ** message on the top of the stack. It assumes that the error object
  89. ** is a string, as it was either generated by Lua or by 'msghandler'.
  90. */
  91. static int report (lua_State *L, int status) {
  92. if (status != LUA_OK) {
  93. const char *msg = lua_tostring(L, -1);
  94. l_message(progname, msg);
  95. lua_pop(L, 1); /* remove message */
  96. }
  97. return status;
  98. }
  99. /*
  100. ** Message handler used to run all chunks
  101. */
  102. static int msghandler (lua_State *L) {
  103. const char *msg = lua_tostring(L, 1);
  104. if (msg == NULL) { /* is error object not a string? */
  105. if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */
  106. lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */
  107. return 1; /* that is the message */
  108. else
  109. msg = lua_pushfstring(L, "(error object is a %s value)",
  110. luaL_typename(L, 1));
  111. }
  112. luaL_traceback(L, L, msg, 1); /* append a standard traceback */
  113. return 1; /* return the traceback */
  114. }
  115. /*
  116. ** Interface to 'lua_pcall', which sets appropriate message function
  117. ** and C-signal handler. Used to run all chunks.
  118. */
  119. static int docall (lua_State *L, int narg, int nres) {
  120. int status;
  121. int base = lua_gettop(L) - narg; /* function index */
  122. lua_pushcfunction(L, msghandler); /* push message handler */
  123. lua_insert(L, base); /* put it under function and args */
  124. globalL = L; /* to be available to 'laction' */
  125. setsignal(SIGINT, laction); /* set C-signal handler */
  126. status = lua_pcall(L, narg, nres, base);
  127. setsignal(SIGINT, SIG_DFL); /* reset C-signal handler */
  128. lua_remove(L, base); /* remove message handler from the stack */
  129. return status;
  130. }
  131. static void print_version (void) {
  132. lua_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT));
  133. lua_writeline();
  134. }
  135. /*
  136. ** Create the 'arg' table, which stores all arguments from the
  137. ** command line ('argv'). It should be aligned so that, at index 0,
  138. ** it has 'argv[script]', which is the script name. The arguments
  139. ** to the script (everything after 'script') go to positive indices;
  140. ** other arguments (before the script name) go to negative indices.
  141. ** If there is no script name, assume interpreter's name as base.
  142. */
  143. static void createargtable (lua_State *L, char **argv, int argc, int script) {
  144. int i, narg;
  145. if (script == argc) script = 0; /* no script name? */
  146. narg = argc - (script + 1); /* number of positive indices */
  147. lua_createtable(L, narg, script + 1);
  148. for (i = 0; i < argc; i++) {
  149. lua_pushstring(L, argv[i]);
  150. lua_rawseti(L, -2, i - script);
  151. }
  152. lua_setglobal(L, "arg");
  153. }
  154. static int dochunk (lua_State *L, int status) {
  155. if (status == LUA_OK) status = docall(L, 0, 0);
  156. return report(L, status);
  157. }
  158. static int dofile (lua_State *L, const char *name) {
  159. return dochunk(L, luaL_loadfile(L, name));
  160. }
  161. static int dostring (lua_State *L, const char *s, const char *name) {
  162. return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name));
  163. }
  164. /*
  165. ** Receives 'globname[=modname]' and runs 'globname = require(modname)'.
  166. */
  167. static int dolibrary (lua_State *L, char *globname) {
  168. int status;
  169. char *modname = strchr(globname, '=');
  170. if (modname == NULL) /* no explicit name? */
  171. modname = globname; /* module name is equal to global name */
  172. else {
  173. *modname = '\0'; /* global name ends here */
  174. modname++; /* module name starts after the '=' */
  175. }
  176. lua_getglobal(L, "require");
  177. lua_pushstring(L, modname);
  178. status = docall(L, 1, 1); /* call 'require(modname)' */
  179. if (status == LUA_OK)
  180. lua_setglobal(L, globname); /* globname = require(modname) */
  181. return report(L, status);
  182. }
  183. /*
  184. ** Push on the stack the contents of table 'arg' from 1 to #arg
  185. */
  186. static int pushargs (lua_State *L) {
  187. int i, n;
  188. if (lua_getglobal(L, "arg") != LUA_TTABLE)
  189. luaL_error(L, "'arg' is not a table");
  190. n = (int)luaL_len(L, -1);
  191. luaL_checkstack(L, n + 3, "too many arguments to script");
  192. for (i = 1; i <= n; i++)
  193. lua_rawgeti(L, -i, i);
  194. lua_remove(L, -i); /* remove table from the stack */
  195. return n;
  196. }
  197. static int handle_script (lua_State *L, char **argv) {
  198. int status;
  199. const char *fname = argv[0];
  200. if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
  201. fname = NULL; /* stdin */
  202. status = luaL_loadfile(L, fname);
  203. if (status == LUA_OK) {
  204. int n = pushargs(L); /* push arguments to script */
  205. status = docall(L, n, LUA_MULTRET);
  206. }
  207. return report(L, status);
  208. }
  209. /* bits of various argument indicators in 'args' */
  210. #define has_error 1 /* bad option */
  211. #define has_i 2 /* -i */
  212. #define has_v 4 /* -v */
  213. #define has_e 8 /* -e */
  214. #define has_E 16 /* -E */
  215. /*
  216. ** Traverses all arguments from 'argv', returning a mask with those
  217. ** needed before running any Lua code (or an error code if it finds
  218. ** any invalid argument). 'first' returns the first not-handled argument
  219. ** (either the script name or a bad argument in case of error).
  220. */
  221. static int collectargs (char **argv, int *first) {
  222. int args = 0;
  223. int i;
  224. for (i = 1; argv[i] != NULL; i++) {
  225. *first = i;
  226. if (argv[i][0] != '-') /* not an option? */
  227. return args; /* stop handling options */
  228. switch (argv[i][1]) { /* else check option */
  229. case '-': /* '--' */
  230. if (argv[i][2] != '\0') /* extra characters after '--'? */
  231. return has_error; /* invalid option */
  232. *first = i + 1;
  233. return args;
  234. case '\0': /* '-' */
  235. return args; /* script "name" is '-' */
  236. case 'E':
  237. if (argv[i][2] != '\0') /* extra characters? */
  238. return has_error; /* invalid option */
  239. args |= has_E;
  240. break;
  241. case 'W':
  242. if (argv[i][2] != '\0') /* extra characters? */
  243. return has_error; /* invalid option */
  244. break;
  245. case 'i':
  246. args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */
  247. case 'v':
  248. if (argv[i][2] != '\0') /* extra characters? */
  249. return has_error; /* invalid option */
  250. args |= has_v;
  251. break;
  252. case 'e':
  253. args |= has_e; /* FALLTHROUGH */
  254. case 'l': /* both options need an argument */
  255. if (argv[i][2] == '\0') { /* no concatenated argument? */
  256. i++; /* try next 'argv' */
  257. if (argv[i] == NULL || argv[i][0] == '-')
  258. return has_error; /* no next argument or it is another option */
  259. }
  260. break;
  261. default: /* invalid option */
  262. return has_error;
  263. }
  264. }
  265. *first = i; /* no script name */
  266. return args;
  267. }
  268. /*
  269. ** Processes options 'e' and 'l', which involve running Lua code, and
  270. ** 'W', which also affects the state.
  271. ** Returns 0 if some code raises an error.
  272. */
  273. static int runargs (lua_State *L, char **argv, int n) {
  274. int i;
  275. for (i = 1; i < n; i++) {
  276. int option = argv[i][1];
  277. lua_assert(argv[i][0] == '-'); /* already checked */
  278. switch (option) {
  279. case 'e': case 'l': {
  280. int status;
  281. char *extra = argv[i] + 2; /* both options need an argument */
  282. if (*extra == '\0') extra = argv[++i];
  283. lua_assert(extra != NULL);
  284. status = (option == 'e')
  285. ? dostring(L, extra, "=(command line)")
  286. : dolibrary(L, extra);
  287. if (status != LUA_OK) return 0;
  288. break;
  289. }
  290. case 'W':
  291. lua_warning(L, "@on", 0); /* warnings on */
  292. break;
  293. }
  294. }
  295. return 1;
  296. }
  297. static int handle_luainit (lua_State *L) {
  298. const char *name = "=" LUA_INITVARVERSION;
  299. const char *init = getenv(name + 1);
  300. if (init == NULL) {
  301. name = "=" LUA_INIT_VAR;
  302. init = getenv(name + 1); /* try alternative name */
  303. }
  304. if (init == NULL) return LUA_OK;
  305. else if (init[0] == '@')
  306. return dofile(L, init+1);
  307. else
  308. return dostring(L, init, name);
  309. }
  310. /*
  311. ** {==================================================================
  312. ** Read-Eval-Print Loop (REPL)
  313. ** ===================================================================
  314. */
  315. #if !defined(LUA_PROMPT)
  316. #define LUA_PROMPT "> "
  317. #define LUA_PROMPT2 ">> "
  318. #endif
  319. #if !defined(LUA_MAXINPUT)
  320. #define LUA_MAXINPUT 512
  321. #endif
  322. /*
  323. ** lua_stdin_is_tty detects whether the standard input is a 'tty' (that
  324. ** is, whether we're running lua interactively).
  325. */
  326. #if !defined(lua_stdin_is_tty) /* { */
  327. #if defined(LUA_USE_POSIX) /* { */
  328. #include <unistd.h>
  329. #define lua_stdin_is_tty() isatty(0)
  330. #elif defined(LUA_USE_WINDOWS) /* }{ */
  331. #include <io.h>
  332. #include <windows.h>
  333. #define lua_stdin_is_tty() _isatty(_fileno(stdin))
  334. #else /* }{ */
  335. /* ISO C definition */
  336. #define lua_stdin_is_tty() 1 /* assume stdin is a tty */
  337. #endif /* } */
  338. #endif /* } */
  339. /*
  340. ** lua_readline defines how to show a prompt and then read a line from
  341. ** the standard input.
  342. ** lua_saveline defines how to "save" a read line in a "history".
  343. ** lua_freeline defines how to free a line read by lua_readline.
  344. */
  345. #if !defined(lua_readline) /* { */
  346. #if defined(LUA_USE_READLINE) /* { */
  347. #include <readline/readline.h>
  348. #include <readline/history.h>
  349. #define lua_initreadline(L) ((void)L, rl_readline_name="lua")
  350. #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
  351. #define lua_saveline(L,line) ((void)L, add_history(line))
  352. #define lua_freeline(L,b) ((void)L, free(b))
  353. #else /* }{ */
  354. #define lua_initreadline(L) ((void)L)
  355. #define lua_readline(L,b,p) \
  356. ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
  357. fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */
  358. #define lua_saveline(L,line) { (void)L; (void)line; }
  359. #define lua_freeline(L,b) { (void)L; (void)b; }
  360. #endif /* } */
  361. #endif /* } */
  362. /*
  363. ** Return the string to be used as a prompt by the interpreter. Leave
  364. ** the string (or nil, if using the default value) on the stack, to keep
  365. ** it anchored.
  366. */
  367. static const char *get_prompt (lua_State *L, int firstline) {
  368. if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL)
  369. return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */
  370. else { /* apply 'tostring' over the value */
  371. const char *p = luaL_tolstring(L, -1, NULL);
  372. lua_remove(L, -2); /* remove original value */
  373. return p;
  374. }
  375. }
  376. /* mark in error messages for incomplete statements */
  377. #define EOFMARK "<eof>"
  378. #define marklen (sizeof(EOFMARK)/sizeof(char) - 1)
  379. /*
  380. ** Check whether 'status' signals a syntax error and the error
  381. ** message at the top of the stack ends with the above mark for
  382. ** incomplete statements.
  383. */
  384. static int incomplete (lua_State *L, int status) {
  385. if (status == LUA_ERRSYNTAX) {
  386. size_t lmsg;
  387. const char *msg = lua_tolstring(L, -1, &lmsg);
  388. if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) {
  389. lua_pop(L, 1);
  390. return 1;
  391. }
  392. }
  393. return 0; /* else... */
  394. }
  395. /*
  396. ** Prompt the user, read a line, and push it into the Lua stack.
  397. */
  398. static int pushline (lua_State *L, int firstline) {
  399. char buffer[LUA_MAXINPUT];
  400. char *b = buffer;
  401. size_t l;
  402. const char *prmt = get_prompt(L, firstline);
  403. int readstatus = lua_readline(L, b, prmt);
  404. if (readstatus == 0)
  405. return 0; /* no input (prompt will be popped by caller) */
  406. lua_pop(L, 1); /* remove prompt */
  407. l = strlen(b);
  408. if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
  409. b[--l] = '\0'; /* remove it */
  410. if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */
  411. lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */
  412. else
  413. lua_pushlstring(L, b, l);
  414. lua_freeline(L, b);
  415. return 1;
  416. }
  417. /*
  418. ** Try to compile line on the stack as 'return <line>;'; on return, stack
  419. ** has either compiled chunk or original line (if compilation failed).
  420. */
  421. static int addreturn (lua_State *L) {
  422. const char *line = lua_tostring(L, -1); /* original line */
  423. const char *retline = lua_pushfstring(L, "return %s;", line);
  424. int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
  425. if (status == LUA_OK) {
  426. lua_remove(L, -2); /* remove modified line */
  427. if (line[0] != '\0') /* non empty? */
  428. lua_saveline(L, line); /* keep history */
  429. }
  430. else
  431. lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */
  432. return status;
  433. }
  434. /*
  435. ** Read multiple lines until a complete Lua statement
  436. */
  437. static int multiline (lua_State *L) {
  438. for (;;) { /* repeat until gets a complete statement */
  439. size_t len;
  440. const char *line = lua_tolstring(L, 1, &len); /* get what it has */
  441. int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
  442. if (!incomplete(L, status) || !pushline(L, 0)) {
  443. lua_saveline(L, line); /* keep history */
  444. return status; /* cannot or should not try to add continuation line */
  445. }
  446. lua_pushliteral(L, "\n"); /* add newline... */
  447. lua_insert(L, -2); /* ...between the two lines */
  448. lua_concat(L, 3); /* join them */
  449. }
  450. }
  451. /*
  452. ** Read a line and try to load (compile) it first as an expression (by
  453. ** adding "return " in front of it) and second as a statement. Return
  454. ** the final status of load/call with the resulting function (if any)
  455. ** in the top of the stack.
  456. */
  457. static int loadline (lua_State *L) {
  458. int status;
  459. lua_settop(L, 0);
  460. if (!pushline(L, 1))
  461. return -1; /* no input */
  462. if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
  463. status = multiline(L); /* try as command, maybe with continuation lines */
  464. lua_remove(L, 1); /* remove line from the stack */
  465. lua_assert(lua_gettop(L) == 1);
  466. return status;
  467. }
  468. /*
  469. ** Prints (calling the Lua 'print' function) any values on the stack
  470. */
  471. static void l_print (lua_State *L) {
  472. int n = lua_gettop(L);
  473. if (n > 0) { /* any result to be printed? */
  474. luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
  475. lua_getglobal(L, "print");
  476. lua_insert(L, 1);
  477. if (lua_pcall(L, n, 0, 0) != LUA_OK)
  478. l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)",
  479. lua_tostring(L, -1)));
  480. }
  481. }
  482. /*
  483. ** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and
  484. ** print any results.
  485. */
  486. static void doREPL (lua_State *L) {
  487. int status;
  488. const char *oldprogname = progname;
  489. progname = NULL; /* no 'progname' on errors in interactive mode */
  490. lua_initreadline(L);
  491. while ((status = loadline(L)) != -1) {
  492. if (status == LUA_OK)
  493. status = docall(L, 0, LUA_MULTRET);
  494. if (status == LUA_OK) l_print(L);
  495. else report(L, status);
  496. }
  497. lua_settop(L, 0); /* clear stack */
  498. lua_writeline();
  499. progname = oldprogname;
  500. }
  501. /* }================================================================== */
  502. /*
  503. ** Main body of stand-alone interpreter (to be called in protected mode).
  504. ** Reads the options and handles them all.
  505. */
  506. static int pmain (lua_State *L) {
  507. int argc = (int)lua_tointeger(L, 1);
  508. char **argv = (char **)lua_touserdata(L, 2);
  509. int script;
  510. int args = collectargs(argv, &script);
  511. luaL_checkversion(L); /* check that interpreter has correct version */
  512. if (argv[0] && argv[0][0]) progname = argv[0];
  513. if (args == has_error) { /* bad arg? */
  514. print_usage(argv[script]); /* 'script' has index of bad arg. */
  515. return 0;
  516. }
  517. if (args & has_v) /* option '-v'? */
  518. print_version();
  519. if (args & has_E) { /* option '-E'? */
  520. lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */
  521. lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
  522. }
  523. luaL_openlibs(L); /* open standard libraries */
  524. createargtable(L, argv, argc, script); /* create table 'arg' */
  525. lua_gc(L, LUA_GCGEN, 0, 0); /* GC in generational mode */
  526. if (!(args & has_E)) { /* no option '-E'? */
  527. if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */
  528. return 0; /* error running LUA_INIT */
  529. }
  530. if (!runargs(L, argv, script)) /* execute arguments -e and -l */
  531. return 0; /* something failed */
  532. if (script < argc && /* execute main script (if there is one) */
  533. handle_script(L, argv + script) != LUA_OK)
  534. return 0;
  535. if (args & has_i) /* -i option? */
  536. doREPL(L); /* do read-eval-print loop */
  537. else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */
  538. if (lua_stdin_is_tty()) { /* running in interactive mode? */
  539. print_version();
  540. doREPL(L); /* do read-eval-print loop */
  541. }
  542. else dofile(L, NULL); /* executes stdin as a file */
  543. }
  544. lua_pushboolean(L, 1); /* signal no errors */
  545. return 1;
  546. }
  547. int main (int argc, char **argv) {
  548. int status, result;
  549. lua_State *L = luaL_newstate(); /* create state */
  550. if (L == NULL) {
  551. l_message(argv[0], "cannot create state: not enough memory");
  552. return EXIT_FAILURE;
  553. }
  554. lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */
  555. lua_pushinteger(L, argc); /* 1st argument */
  556. lua_pushlightuserdata(L, argv); /* 2nd argument */
  557. status = lua_pcall(L, 2, 1, 0); /* do the call */
  558. result = lua_toboolean(L, -1); /* get result */
  559. report(L, status);
  560. lua_close(L);
  561. return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
  562. }