lua.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. ** $Id: lua.c,v 1.160.1.2 2007/12/28 15:32:23 roberto Exp $
  3. ** Lua stand-alone interpreter
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <signal.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define lua_c
  11. #include "lua.h"
  12. #include "lauxlib.h"
  13. #include "lualib.h"
  14. static lua_State *globalL = NULL;
  15. static const char *progname = LUA_PROGNAME;
  16. static void lstop(lua_State *L, lua_Debug *ar)
  17. {
  18. (void)ar; /* unused arg. */
  19. lua_sethook(L, NULL, 0, 0);
  20. luaL_error(L, "interrupted!");
  21. }
  22. static void laction(int i)
  23. {
  24. signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
  25. terminate process (default action) */
  26. lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
  27. }
  28. static void print_usage(void)
  29. {
  30. #if defined(LUA_USE_STDIO)
  31. fprintf(stderr,
  32. #else
  33. luai_writestringerror(
  34. #endif
  35. "usage: %s [options] [script [args]].\n"
  36. "Available options are:\n"
  37. " -e stat execute string " LUA_QL("stat") "\n"
  38. " -l name require library " LUA_QL("name") "\n"
  39. " -m limit set memory limit. (units are in Kbytes)\n"
  40. " -i enter interactive mode after executing " LUA_QL("script") "\n"
  41. " -v show version information\n"
  42. " -- stop handling options\n"
  43. " - execute stdin and stop handling options\n"
  44. ,
  45. progname);
  46. #if defined(LUA_USE_STDIO)
  47. fflush(stderr);
  48. #endif
  49. }
  50. static void l_message(const char *pname, const char *msg)
  51. {
  52. #if defined(LUA_USE_STDIO)
  53. if (pname) fprintf(stderr, "%s: ", pname);
  54. fprintf(stderr, "%s\n", msg);
  55. fflush(stderr);
  56. #else
  57. if (pname) luai_writestringerror("%s: ", pname);
  58. luai_writestringerror("%s\n", msg);
  59. #endif
  60. }
  61. static int report(lua_State *L, int status)
  62. {
  63. if (status && !lua_isnil(L, -1))
  64. {
  65. const char *msg = lua_tostring(L, -1);
  66. if (msg == NULL) msg = "(error object is not a string)";
  67. l_message(progname, msg);
  68. lua_pop(L, 1);
  69. }
  70. return status;
  71. }
  72. static int traceback(lua_State *L)
  73. {
  74. if (!lua_isstring(L, 1)) /* 'message' not a string? */
  75. return 1; /* keep it intact */
  76. lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  77. if (!lua_istable(L, -1) && !lua_isrotable(L, -1))
  78. {
  79. lua_pop(L, 1);
  80. return 1;
  81. }
  82. lua_getfield(L, -1, "traceback");
  83. if (!lua_isfunction(L, -1) && !lua_islightfunction(L, -1))
  84. {
  85. lua_pop(L, 2);
  86. return 1;
  87. }
  88. lua_pushvalue(L, 1); /* pass error message */
  89. lua_pushinteger(L, 2); /* skip this function and traceback */
  90. lua_call(L, 2, 1); /* call debug.traceback */
  91. return 1;
  92. }
  93. static int docall(lua_State *L, int narg, int clear)
  94. {
  95. int status;
  96. int base = lua_gettop(L) - narg; /* function index */
  97. lua_pushcfunction(L, traceback); /* push traceback function */
  98. lua_insert(L, base); /* put it under chunk and args */
  99. signal(SIGINT, laction);
  100. status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
  101. signal(SIGINT, SIG_DFL);
  102. lua_remove(L, base); /* remove traceback function */
  103. /* force a complete garbage collection in case of errors */
  104. if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
  105. return status;
  106. }
  107. static void print_version(void)
  108. {
  109. l_message(NULL, LUA_RELEASE " " LUA_COPYRIGHT);
  110. }
  111. static int getargs(lua_State *L, char **argv, int n)
  112. {
  113. int narg;
  114. int i;
  115. int argc = 0;
  116. while (argv[argc]) argc++; /* count total number of arguments */
  117. narg = argc - (n + 1); /* number of arguments to the script */
  118. luaL_checkstack(L, narg + 3, "too many arguments to script");
  119. for (i = n + 1; i < argc; i++)
  120. lua_pushstring(L, argv[i]);
  121. lua_createtable(L, narg, n + 1);
  122. for (i = 0; i < argc; i++)
  123. {
  124. lua_pushstring(L, argv[i]);
  125. lua_rawseti(L, -2, i - n);
  126. }
  127. return narg;
  128. }
  129. static int dofile(lua_State *L, const char *name)
  130. {
  131. int status = luaL_loadfile(L, name) || docall(L, 0, 1);
  132. return report(L, status);
  133. }
  134. static int dostring(lua_State *L, const char *s, const char *name)
  135. {
  136. int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
  137. return report(L, status);
  138. }
  139. static int dolibrary(lua_State *L, const char *name)
  140. {
  141. lua_getglobal(L, "require");
  142. lua_pushstring(L, name);
  143. return report(L, docall(L, 1, 1));
  144. }
  145. static const char *get_prompt(lua_State *L, int firstline)
  146. {
  147. const char *p;
  148. lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
  149. p = lua_tostring(L, -1);
  150. if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
  151. lua_pop(L, 1); /* remove global */
  152. return p;
  153. }
  154. static int incomplete(lua_State *L, int status)
  155. {
  156. if (status == LUA_ERRSYNTAX)
  157. {
  158. size_t lmsg;
  159. const char *msg = lua_tolstring(L, -1, &lmsg);
  160. const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
  161. if (strstr(msg, LUA_QL("<eof>")) == tp)
  162. {
  163. lua_pop(L, 1);
  164. return 1;
  165. }
  166. }
  167. return 0; /* else... */
  168. }
  169. static int pushline(lua_State *L, int firstline)
  170. {
  171. char buffer[LUA_MAXINPUT];
  172. char *b = buffer;
  173. size_t l;
  174. const char *prmt = get_prompt(L, firstline);
  175. if (lua_readline(L, b, prmt) == 0)
  176. return 0; /* no input */
  177. l = strlen(b);
  178. if (l > 0 && b[l - 1] == '\n') /* line ends with newline? */
  179. b[l - 1] = '\0'; /* remove it */
  180. if (firstline && b[0] == '=') /* first line starts with `=' ? */
  181. lua_pushfstring(L, "return %s", b + 1); /* change it to `return' */
  182. else
  183. lua_pushstring(L, b);
  184. lua_freeline(L, b);
  185. return 1;
  186. }
  187. static int loadline(lua_State *L)
  188. {
  189. int status;
  190. lua_settop(L, 0);
  191. if (!pushline(L, 1))
  192. return -1; /* no input */
  193. for (;;) /* repeat until gets a complete line */
  194. {
  195. status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
  196. if (!incomplete(L, status)) break; /* cannot try to add lines? */
  197. if (!pushline(L, 0)) /* no more input? */
  198. return -1;
  199. lua_pushliteral(L, "\n"); /* add a new line... */
  200. lua_insert(L, -2); /* ...between the two lines */
  201. lua_concat(L, 3); /* join them */
  202. }
  203. lua_saveline(L, 1);
  204. lua_remove(L, 1); /* remove line */
  205. return status;
  206. }
  207. static void dotty(lua_State *L)
  208. {
  209. int status;
  210. const char *oldprogname = progname;
  211. progname = NULL;
  212. while ((status = loadline(L)) != -1)
  213. {
  214. if (status == 0) status = docall(L, 0, 0);
  215. report(L, status);
  216. if (status == 0 && lua_gettop(L) > 0) /* any result to print? */
  217. {
  218. lua_getglobal(L, "print");
  219. lua_insert(L, 1);
  220. if (lua_pcall(L, lua_gettop(L) - 1, 0, 0) != 0)
  221. l_message(progname, lua_pushfstring(L,
  222. "error calling " LUA_QL("print") " (%s)",
  223. lua_tostring(L, -1)));
  224. }
  225. }
  226. lua_settop(L, 0); /* clear stack */
  227. #if defined(LUA_USE_STDIO)
  228. fputs("\n", stdout);
  229. fflush(stdout);
  230. #else
  231. luai_writeline();
  232. #endif
  233. progname = oldprogname;
  234. }
  235. static int handle_script(lua_State *L, char **argv, int n)
  236. {
  237. int status;
  238. const char *fname;
  239. int narg = getargs(L, argv, n); /* collect arguments */
  240. lua_setglobal(L, "arg");
  241. fname = argv[n];
  242. if (strcmp(fname, "-") == 0 && strcmp(argv[n - 1], "--") != 0)
  243. fname = NULL; /* stdin */
  244. status = luaL_loadfile(L, fname);
  245. lua_insert(L, -(narg + 1));
  246. if (status == 0)
  247. status = docall(L, narg, 0);
  248. else
  249. lua_pop(L, narg);
  250. return report(L, status);
  251. }
  252. /* check that argument has no extra characters at the end */
  253. #define notail(x) {if ((x)[2] != '\0') return -1;}
  254. static int collectargs(char **argv, int *pi, int *pv, int *pe)
  255. {
  256. int i;
  257. for (i = 1; argv[i] != NULL; i++)
  258. {
  259. if (argv[i][0] != '-') /* not an option? */
  260. return i;
  261. switch (argv[i][1]) /* option */
  262. {
  263. case '-':
  264. notail(argv[i]);
  265. return (argv[i + 1] != NULL ? i + 1 : 0);
  266. case '\0':
  267. return i;
  268. case 'i':
  269. notail(argv[i]);
  270. *pi = 1; /* go through */
  271. case 'v':
  272. notail(argv[i]);
  273. *pv = 1;
  274. break;
  275. case 'e':
  276. *pe = 1; /* go through */
  277. case 'm': /* go through */
  278. case 'l':
  279. if (argv[i][2] == '\0')
  280. {
  281. i++;
  282. if (argv[i] == NULL) return -1;
  283. }
  284. break;
  285. default:
  286. return -1; /* invalid option */
  287. }
  288. }
  289. return 0;
  290. }
  291. static int runargs(lua_State *L, char **argv, int n)
  292. {
  293. int i;
  294. for (i = 1; i < n; i++)
  295. {
  296. if (argv[i] == NULL) continue;
  297. lua_assert(argv[i][0] == '-');
  298. switch (argv[i][1]) /* option */
  299. {
  300. case 'e':
  301. {
  302. const char *chunk = argv[i] + 2;
  303. if (*chunk == '\0') chunk = argv[++i];
  304. lua_assert(chunk != NULL);
  305. if (dostring(L, chunk, "=(command line)") != 0)
  306. return 1;
  307. break;
  308. }
  309. case 'm':
  310. {
  311. const char *limit = argv[i] + 2;
  312. int memlimit = 0;
  313. if (*limit == '\0') limit = argv[++i];
  314. lua_assert(limit != NULL);
  315. memlimit = atoi(limit);
  316. lua_gc(L, LUA_GCSETMEMLIMIT, memlimit);
  317. break;
  318. }
  319. case 'l':
  320. {
  321. const char *filename = argv[i] + 2;
  322. if (*filename == '\0') filename = argv[++i];
  323. lua_assert(filename != NULL);
  324. if (dolibrary(L, filename))
  325. return 1; /* stop if file fails */
  326. break;
  327. }
  328. default:
  329. break;
  330. }
  331. }
  332. return 0;
  333. }
  334. static int handle_luainit(lua_State *L)
  335. {
  336. const char *init = getenv(LUA_INIT);
  337. if (init == NULL) return 0; /* status OK */
  338. else if (init[0] == '@')
  339. return dofile(L, init + 1);
  340. else
  341. return dostring(L, init, "=" LUA_INIT);
  342. }
  343. struct Smain
  344. {
  345. int argc;
  346. char **argv;
  347. int status;
  348. };
  349. static int pmain(lua_State *L)
  350. {
  351. struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
  352. char **argv = s->argv;
  353. int script;
  354. int has_i = 0, has_v = 0, has_e = 0;
  355. globalL = L;
  356. if (argv[0] && argv[0][0]) progname = argv[0];
  357. lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
  358. luaL_openlibs(L); /* open libraries */
  359. lua_gc(L, LUA_GCRESTART, 0);
  360. s->status = handle_luainit(L);
  361. if (s->status != 0) return 0;
  362. script = collectargs(argv, &has_i, &has_v, &has_e);
  363. if (script < 0) /* invalid args? */
  364. {
  365. print_usage();
  366. s->status = 1;
  367. return 0;
  368. }
  369. if (has_v) print_version();
  370. s->status = runargs(L, argv, (script > 0) ? script : s->argc);
  371. if (s->status != 0) return 0;
  372. if (script)
  373. s->status = handle_script(L, argv, script);
  374. if (s->status != 0) return 0;
  375. if (has_i)
  376. dotty(L);
  377. else if (script == 0 && !has_e && !has_v)
  378. {
  379. if (lua_stdin_is_tty())
  380. {
  381. print_version();
  382. dotty(L);
  383. }
  384. else dofile(L, NULL); /* executes stdin as a file */
  385. }
  386. return 0;
  387. }
  388. #if !defined(PKG_USING_LUA2RTT)
  389. int main(int argc, char **argv)
  390. {
  391. #else
  392. int lua_main(int argc, char **argv)
  393. {
  394. #endif
  395. int status;
  396. struct Smain s;
  397. lua_State *L = lua_open(); /* create state */
  398. if (L == NULL)
  399. {
  400. l_message(argv[0], "cannot create state: not enough memory");
  401. return EXIT_FAILURE;
  402. }
  403. s.argc = argc;
  404. s.argv = argv;
  405. status = lua_cpcall(L, &pmain, &s);
  406. report(L, status);
  407. lua_close(L);
  408. return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
  409. }