lua.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. ** $Id: lua.c,v 1.145 2005/06/03 20:16:16 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. " -w trap access to undefined globals\n"
  36. " -- stop handling options\n" ,
  37. progname);
  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. }
  43. static int report (lua_State *L, int status) {
  44. if (status && !lua_isnil(L, -1)) {
  45. const char *msg = lua_tostring(L, -1);
  46. if (msg == NULL) msg = "(error object is not a string)";
  47. l_message(progname, msg);
  48. lua_pop(L, 1);
  49. }
  50. return status;
  51. }
  52. static int traceback (lua_State *L) {
  53. luaL_getfield(L, LUA_GLOBALSINDEX, "debug.traceback");
  54. if (!lua_isfunction(L, -1))
  55. lua_pop(L, 1);
  56. else {
  57. lua_pushvalue(L, 1); /* pass error message */
  58. lua_pushinteger(L, 2); /* skip this function and traceback */
  59. lua_call(L, 2, 1); /* call debug.traceback */
  60. }
  61. return 1;
  62. }
  63. static int docall (lua_State *L, int narg, int clear) {
  64. int status;
  65. int base = lua_gettop(L) - narg; /* function index */
  66. lua_pushcfunction(L, traceback); /* push traceback function */
  67. lua_insert(L, base); /* put it under chunk and args */
  68. signal(SIGINT, laction);
  69. status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
  70. signal(SIGINT, SIG_DFL);
  71. lua_remove(L, base); /* remove traceback function */
  72. return status;
  73. }
  74. static void print_version (void) {
  75. l_message(NULL, LUA_VERSION " " LUA_COPYRIGHT);
  76. }
  77. static int getargs (lua_State *L, int argc, char **argv, int n) {
  78. int narg = argc - (n + 1); /* number of arguments to the script */
  79. int i;
  80. luaL_checkstack(L, narg + 3, "too many arguments to script");
  81. for (i=n+1; i < argc; i++)
  82. lua_pushstring(L, argv[i]);
  83. lua_newtable(L);
  84. for (i=0; i < argc; i++) {
  85. lua_pushstring(L, argv[i]);
  86. lua_rawseti(L, -2, i - n);
  87. }
  88. return narg;
  89. }
  90. static int dofile (lua_State *L, const char *name) {
  91. int status = luaL_loadfile(L, name) || docall(L, 0, 1);
  92. return report(L, status);
  93. }
  94. static int dostring (lua_State *L, const char *s, const char *name) {
  95. int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
  96. return report(L, status);
  97. }
  98. static int dolibrary (lua_State *L, const char *name) {
  99. lua_getglobal(L, "require");
  100. lua_pushstring(L, name);
  101. return report(L, lua_pcall(L, 1, 0, 0));
  102. }
  103. static const char *get_prompt (lua_State *L, int firstline) {
  104. const char *p;
  105. lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");
  106. lua_rawget(L, LUA_GLOBALSINDEX);
  107. p = lua_tostring(L, -1);
  108. if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
  109. lua_pop(L, 1); /* remove global */
  110. return p;
  111. }
  112. static int incomplete (lua_State *L, int status) {
  113. if (status == LUA_ERRSYNTAX) {
  114. size_t lmsg;
  115. const char *msg = lua_tolstring(L, -1, &lmsg);
  116. const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
  117. if (strstr(msg, LUA_QL("<eof>")) == tp) {
  118. lua_pop(L, 1);
  119. return 1;
  120. }
  121. }
  122. return 0; /* else... */
  123. }
  124. static int pushline (lua_State *L, int firstline) {
  125. char buffer[LUA_MAXINPUT];
  126. char *b = buffer;
  127. size_t l;
  128. const char *prmt = get_prompt(L, firstline);
  129. if (lua_readline(L, b, prmt) == 0)
  130. return 0; /* no input */
  131. l = strlen(b);
  132. if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
  133. b[l-1] = '\0'; /* remove it */
  134. if (firstline && b[0] == '=') /* first line starts with `=' ? */
  135. lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
  136. else
  137. lua_pushstring(L, b);
  138. lua_freeline(L, b);
  139. return 1;
  140. }
  141. static int loadline (lua_State *L) {
  142. int status;
  143. lua_settop(L, 0);
  144. if (!pushline(L, 1))
  145. return -1; /* no input */
  146. for (;;) { /* repeat until gets a complete line */
  147. status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
  148. if (!incomplete(L, status)) break; /* cannot try to add lines? */
  149. if (!pushline(L, 0)) /* no more input? */
  150. return -1;
  151. lua_pushliteral(L, "\n"); /* add a new line... */
  152. lua_insert(L, -2); /* ...between the two lines */
  153. lua_concat(L, 3); /* join them */
  154. }
  155. lua_saveline(L, 1);
  156. lua_remove(L, 1); /* remove line */
  157. return status;
  158. }
  159. static void dotty (lua_State *L) {
  160. int status;
  161. const char *oldprogname = progname;
  162. progname = NULL;
  163. print_version();
  164. while ((status = loadline(L)) != -1) {
  165. if (status == 0) status = docall(L, 0, 0);
  166. report(L, status);
  167. if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
  168. lua_getglobal(L, "print");
  169. lua_insert(L, 1);
  170. if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
  171. l_message(progname, lua_pushfstring(L,
  172. "error calling " LUA_QL("print") " (%s)",
  173. lua_tostring(L, -1)));
  174. }
  175. }
  176. lua_settop(L, 0); /* clear stack */
  177. fputs("\n", stdout);
  178. progname = oldprogname;
  179. }
  180. static int checkvar (lua_State *L) {
  181. const char *name = lua_tostring(L, 2);
  182. if (name)
  183. luaL_error(L, "attempt to access undefined variable " LUA_QS, name);
  184. return 0;
  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 'w': {
  223. if (lua_getmetatable(L, LUA_GLOBALSINDEX)) {
  224. lua_pushcfunction(L, checkvar);
  225. lua_setfield(L, -2, "__index");
  226. }
  227. break;
  228. }
  229. case 'e': {
  230. const char *chunk = argv[i] + 2;
  231. clearinteractive(interactive);
  232. if (*chunk == '\0') chunk = argv[++i];
  233. if (chunk == NULL) {
  234. print_usage();
  235. return 1;
  236. }
  237. if (dostring(L, chunk, "=(command line)") != 0)
  238. return 1;
  239. break;
  240. }
  241. case 'l': {
  242. const char *filename = argv[i] + 2;
  243. if (*filename == '\0') filename = argv[++i];
  244. if (filename == NULL) {
  245. print_usage();
  246. return 1;
  247. }
  248. if (dolibrary(L, filename))
  249. return 1; /* stop if file fails */
  250. break;
  251. }
  252. default: {
  253. clearinteractive(interactive);
  254. print_usage();
  255. return 1;
  256. }
  257. }
  258. } endloop:
  259. if (argv[i] != NULL) {
  260. int status;
  261. const char *filename = argv[i];
  262. int narg = getargs(L, argc, argv, i); /* collect arguments */
  263. lua_setglobal(L, "arg");
  264. clearinteractive(interactive);
  265. status = luaL_loadfile(L, filename);
  266. lua_insert(L, -(narg+1));
  267. if (status == 0)
  268. status = docall(L, narg, 0);
  269. else
  270. lua_pop(L, narg);
  271. return report(L, status);
  272. }
  273. }
  274. return 0;
  275. }
  276. static int handle_luainit (lua_State *L) {
  277. const char *init = getenv("LUA_INIT");
  278. if (init == NULL) return 0; /* status OK */
  279. else if (init[0] == '@')
  280. return dofile(L, init+1);
  281. else
  282. return dostring(L, init, "=LUA_INIT");
  283. }
  284. struct Smain {
  285. int argc;
  286. char **argv;
  287. int status;
  288. };
  289. static int pmain (lua_State *L) {
  290. struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
  291. int status;
  292. int interactive = 1;
  293. if (s->argv[0] && s->argv[0][0]) progname = s->argv[0];
  294. globalL = L;
  295. luaL_openlibs(L); /* open libraries */
  296. status = handle_luainit(L);
  297. if (status == 0) {
  298. status = handle_argv(L, s->argc, s->argv, &interactive);
  299. if (status == 0 && interactive) dotty(L);
  300. }
  301. s->status = status;
  302. return 0;
  303. }
  304. int main (int argc, char **argv) {
  305. int status;
  306. struct Smain s;
  307. lua_State *L = lua_open(); /* create state */
  308. if (L == NULL) {
  309. l_message(argv[0], "cannot create state: not enough memory");
  310. return EXIT_FAILURE;
  311. }
  312. s.argc = argc;
  313. s.argv = argv;
  314. status = lua_cpcall(L, &pmain, &s);
  315. report(L, status);
  316. lua_close(L);
  317. return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
  318. }