lua.c 11 KB

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