lua.c 12 KB

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