2
0

lua.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. ** $Id: lua.c,v 1.149 2005/08/26 17:32:05 roberto Exp roberto $
  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. fprintf(stderr,
  28. "usage: %s [options] [script [args]].\n"
  29. "Available options are:\n"
  30. " - execute stdin as a file\n"
  31. " -e stat execute string " LUA_QL("stat") "\n"
  32. " -i enter interactive mode after executing " LUA_QL("script") "\n"
  33. " -l name require library " LUA_QL("name") "\n"
  34. " -v show version information\n"
  35. " -- stop handling options\n" ,
  36. progname);
  37. fflush(stderr);
  38. }
  39. static void l_message (const char *pname, const char *msg) {
  40. if (pname) fprintf(stderr, "%s: ", pname);
  41. fprintf(stderr, "%s\n", msg);
  42. fflush(stderr);
  43. }
  44. static int report (lua_State *L, int status) {
  45. if (status && !lua_isnil(L, -1)) {
  46. const char *msg = lua_tostring(L, -1);
  47. if (msg == NULL) msg = "(error object is not a string)";
  48. l_message(progname, msg);
  49. lua_pop(L, 1);
  50. }
  51. return status;
  52. }
  53. static int traceback (lua_State *L) {
  54. lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  55. if (!lua_istable(L, -1)) {
  56. lua_pop(L, 1);
  57. return 1;
  58. }
  59. lua_getfield(L, -1, "traceback");
  60. if (!lua_isfunction(L, -1)) {
  61. lua_pop(L, 2);
  62. return 1;
  63. }
  64. lua_pushvalue(L, 1); /* pass error message */
  65. lua_pushinteger(L, 2); /* skip this function and traceback */
  66. lua_call(L, 2, 1); /* call debug.traceback */
  67. return 1;
  68. }
  69. static int docall (lua_State *L, int narg, int clear) {
  70. int status;
  71. int base = lua_gettop(L) - narg; /* function index */
  72. lua_pushcfunction(L, traceback); /* push traceback function */
  73. lua_insert(L, base); /* put it under chunk and args */
  74. signal(SIGINT, laction);
  75. status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
  76. signal(SIGINT, SIG_DFL);
  77. lua_remove(L, base); /* remove traceback function */
  78. return status;
  79. }
  80. static void print_version (void) {
  81. l_message(NULL, LUA_VERSION " " LUA_COPYRIGHT);
  82. }
  83. static int getargs (lua_State *L, int argc, char **argv, int n) {
  84. int narg = argc - (n + 1); /* number of arguments to the script */
  85. int i;
  86. luaL_checkstack(L, narg + 3, "too many arguments to script");
  87. for (i=n+1; i < argc; i++)
  88. lua_pushstring(L, argv[i]);
  89. lua_newtable(L);
  90. for (i=0; i < argc; i++) {
  91. lua_pushstring(L, argv[i]);
  92. lua_rawseti(L, -2, i - n);
  93. }
  94. return narg;
  95. }
  96. static int dofile (lua_State *L, const char *name) {
  97. int status = luaL_loadfile(L, name) || docall(L, 0, 1);
  98. return report(L, status);
  99. }
  100. static int dostring (lua_State *L, const char *s, const char *name) {
  101. int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
  102. return report(L, status);
  103. }
  104. static int dolibrary (lua_State *L, const char *name) {
  105. lua_getglobal(L, "require");
  106. lua_pushstring(L, name);
  107. return report(L, lua_pcall(L, 1, 0, 0));
  108. }
  109. static const char *get_prompt (lua_State *L, int firstline) {
  110. const char *p;
  111. lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
  112. p = lua_tostring(L, -1);
  113. if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
  114. lua_pop(L, 1); /* remove global */
  115. return p;
  116. }
  117. static int incomplete (lua_State *L, int status) {
  118. if (status == LUA_ERRSYNTAX) {
  119. size_t lmsg;
  120. const char *msg = lua_tolstring(L, -1, &lmsg);
  121. const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
  122. if (strstr(msg, LUA_QL("<eof>")) == tp) {
  123. lua_pop(L, 1);
  124. return 1;
  125. }
  126. }
  127. return 0; /* else... */
  128. }
  129. static int pushline (lua_State *L, int firstline) {
  130. char buffer[LUA_MAXINPUT];
  131. char *b = buffer;
  132. size_t l;
  133. const char *prmt = get_prompt(L, firstline);
  134. if (lua_readline(L, b, prmt) == 0)
  135. return 0; /* no input */
  136. l = strlen(b);
  137. if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
  138. b[l-1] = '\0'; /* remove it */
  139. if (firstline && b[0] == '=') /* first line starts with `=' ? */
  140. lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
  141. else
  142. lua_pushstring(L, b);
  143. lua_freeline(L, b);
  144. return 1;
  145. }
  146. static int loadline (lua_State *L) {
  147. int status;
  148. lua_settop(L, 0);
  149. if (!pushline(L, 1))
  150. return -1; /* no input */
  151. for (;;) { /* repeat until gets a complete line */
  152. status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
  153. if (!incomplete(L, status)) break; /* cannot try to add lines? */
  154. if (!pushline(L, 0)) /* no more input? */
  155. return -1;
  156. lua_pushliteral(L, "\n"); /* add a new line... */
  157. lua_insert(L, -2); /* ...between the two lines */
  158. lua_concat(L, 3); /* join them */
  159. }
  160. lua_saveline(L, 1);
  161. lua_remove(L, 1); /* remove line */
  162. return status;
  163. }
  164. static void dotty (lua_State *L) {
  165. int status;
  166. const char *oldprogname = progname;
  167. progname = NULL;
  168. print_version();
  169. while ((status = loadline(L)) != -1) {
  170. if (status == 0) status = docall(L, 0, 0);
  171. report(L, status);
  172. if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
  173. lua_getglobal(L, "print");
  174. lua_insert(L, 1);
  175. if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
  176. l_message(progname, lua_pushfstring(L,
  177. "error calling " LUA_QL("print") " (%s)",
  178. lua_tostring(L, -1)));
  179. }
  180. }
  181. lua_settop(L, 0); /* clear stack */
  182. fputs("\n", stdout);
  183. fflush(stdout);
  184. progname = oldprogname;
  185. }
  186. #define clearinteractive(i) (*i &= 2)
  187. static int handle_argv (lua_State *L, int argc, char **argv, int *interactive) {
  188. if (argv[1] == NULL) { /* no arguments? */
  189. *interactive = 0;
  190. if (lua_stdin_is_tty())
  191. dotty(L);
  192. else
  193. dofile(L, NULL); /* executes stdin as a file */
  194. }
  195. else { /* other arguments; loop over them */
  196. int i;
  197. for (i = 1; argv[i] != NULL; i++) {
  198. if (argv[i][0] != '-') break; /* not an option? */
  199. switch (argv[i][1]) { /* option */
  200. case '-': { /* `--' */
  201. if (argv[i][2] != '\0') {
  202. print_usage();
  203. return 1;
  204. }
  205. i++; /* skip this argument */
  206. goto endloop; /* stop handling arguments */
  207. }
  208. case '\0': {
  209. clearinteractive(interactive);
  210. dofile(L, NULL); /* executes stdin as a file */
  211. break;
  212. }
  213. case 'i': {
  214. *interactive = 2; /* force interactive mode after arguments */
  215. break;
  216. }
  217. case 'v': {
  218. clearinteractive(interactive);
  219. print_version();
  220. break;
  221. }
  222. case 'e': {
  223. const char *chunk = argv[i] + 2;
  224. clearinteractive(interactive);
  225. if (*chunk == '\0') chunk = argv[++i];
  226. if (chunk == NULL) {
  227. print_usage();
  228. return 1;
  229. }
  230. if (dostring(L, chunk, "=(command line)") != 0)
  231. return 1;
  232. break;
  233. }
  234. case 'l': {
  235. const char *filename = argv[i] + 2;
  236. if (*filename == '\0') filename = argv[++i];
  237. if (filename == NULL) {
  238. print_usage();
  239. return 1;
  240. }
  241. if (dolibrary(L, filename))
  242. return 1; /* stop if file fails */
  243. break;
  244. }
  245. default: {
  246. clearinteractive(interactive);
  247. print_usage();
  248. return 1;
  249. }
  250. }
  251. } endloop:
  252. if (argv[i] != NULL) {
  253. int status;
  254. const char *filename = argv[i];
  255. int narg = getargs(L, argc, argv, i); /* collect arguments */
  256. lua_setglobal(L, "arg");
  257. clearinteractive(interactive);
  258. status = luaL_loadfile(L, filename);
  259. lua_insert(L, -(narg+1));
  260. if (status == 0)
  261. status = docall(L, narg, 0);
  262. else
  263. lua_pop(L, narg);
  264. return report(L, status);
  265. }
  266. }
  267. return 0;
  268. }
  269. static int handle_luainit (lua_State *L) {
  270. const char *init = getenv("LUA_INIT");
  271. if (init == NULL) return 0; /* status OK */
  272. else if (init[0] == '@')
  273. return dofile(L, init+1);
  274. else
  275. return dostring(L, init, "=LUA_INIT");
  276. }
  277. struct Smain {
  278. int argc;
  279. char **argv;
  280. int status;
  281. };
  282. static int pmain (lua_State *L) {
  283. struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
  284. int status;
  285. int interactive = 1;
  286. if (s->argv[0] && s->argv[0][0]) progname = s->argv[0];
  287. globalL = L;
  288. luaL_openlibs(L); /* open libraries */
  289. status = handle_luainit(L);
  290. if (status == 0) {
  291. status = handle_argv(L, s->argc, s->argv, &interactive);
  292. if (status == 0 && interactive) dotty(L);
  293. }
  294. s->status = status;
  295. return 0;
  296. }
  297. int main (int argc, char **argv) {
  298. int status;
  299. struct Smain s;
  300. lua_State *L = lua_open(); /* create state */
  301. if (L == NULL) {
  302. l_message(argv[0], "cannot create state: not enough memory");
  303. return EXIT_FAILURE;
  304. }
  305. s.argc = argc;
  306. s.argv = argv;
  307. status = lua_cpcall(L, &pmain, &s);
  308. report(L, status);
  309. lua_close(L);
  310. return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
  311. }