lua.c 9.3 KB

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