lua.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. ** $Id: lua.c,v 1.128 2004/06/17 14:06:52 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. /*
  15. ** generic extra include file
  16. */
  17. #ifdef LUA_USERCONFIG
  18. #include LUA_USERCONFIG
  19. #endif
  20. static lua_State *L = NULL;
  21. static const char *progname = PROGNAME;
  22. static const luaL_reg lualibs[] = {
  23. {"base", luaopen_base},
  24. {"table", luaopen_table},
  25. {"io", luaopen_io},
  26. {"string", luaopen_string},
  27. {"math", luaopen_math},
  28. {"debug", luaopen_debug},
  29. {"loadlib", luaopen_loadlib},
  30. /* add your libraries here */
  31. LUA_EXTRALIBS
  32. {NULL, NULL}
  33. };
  34. static void lstop (lua_State *l, lua_Debug *ar) {
  35. (void)ar; /* unused arg. */
  36. lua_sethook(l, NULL, 0, 0);
  37. luaL_error(l, "interrupted!");
  38. }
  39. static void laction (int i) {
  40. signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
  41. terminate process (default action) */
  42. lua_sethook(L, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
  43. }
  44. static void print_usage (void) {
  45. fprintf(stderr,
  46. "usage: %s [options] [script [args]].\n"
  47. "Available options are:\n"
  48. " - execute stdin as a file\n"
  49. " -e stat execute string `stat'\n"
  50. " -i enter interactive mode after executing `script'\n"
  51. " -l name load and run library `name'\n"
  52. " -v show version information\n"
  53. " -- stop handling options\n" ,
  54. progname);
  55. }
  56. static void l_message (const char *pname, const char *msg) {
  57. if (pname) fprintf(stderr, "%s: ", pname);
  58. fprintf(stderr, "%s\n", msg);
  59. }
  60. static int report (int status) {
  61. if (status && !lua_isnil(L, -1)) {
  62. const char *msg = lua_tostring(L, -1);
  63. if (msg == NULL) msg = "(error object is not a string)";
  64. l_message(progname, msg);
  65. lua_pop(L, 1);
  66. }
  67. return status;
  68. }
  69. static int lcall (int narg, int clear) {
  70. int status;
  71. int base = lua_gettop(L) - narg; /* function index */
  72. lua_pushliteral(L, "_TRACEBACK");
  73. lua_rawget(L, LUA_GLOBALSINDEX); /* get traceback function */
  74. lua_insert(L, base); /* put it under chunk and args */
  75. signal(SIGINT, laction);
  76. status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
  77. signal(SIGINT, SIG_DFL);
  78. lua_remove(L, base); /* remove traceback function */
  79. return status;
  80. }
  81. static void print_version (void) {
  82. l_message(NULL, LUA_VERSION " " LUA_COPYRIGHT);
  83. }
  84. static int getargs (char *argv[], int n) {
  85. int i, narg;
  86. for (i=n+1; argv[i]; i++) {
  87. luaL_checkstack(L, 1, "too many arguments to script");
  88. lua_pushstring(L, argv[i]);
  89. }
  90. narg = i-(n+1); /* number of arguments to the script (not to `lua.c') */
  91. lua_newtable(L);
  92. for (i=0; argv[i]; i++) {
  93. lua_pushnumber(L, i - n);
  94. lua_pushstring(L, argv[i]);
  95. lua_rawset(L, -3);
  96. }
  97. return narg;
  98. }
  99. static int file_input (const char *name) {
  100. int status = luaL_loadfile(L, name) || lcall(0, 1);
  101. return report(status);
  102. }
  103. static int dostring (const char *s, const char *name) {
  104. int status = luaL_loadbuffer(L, s, strlen(s), name) || lcall(0, 1);
  105. return report(status);
  106. }
  107. static int load_file (const char *name) {
  108. name = luaL_searchpath(L, name, NULL);
  109. if (name == NULL) return report(1);
  110. else return file_input(name);
  111. }
  112. /*
  113. ** this macro defines a function to show the prompt and reads the
  114. ** next line for manual input
  115. */
  116. #ifndef lua_readline
  117. #define lua_readline(L,prompt) readline(L,prompt)
  118. /* maximum length of an input line */
  119. #ifndef MAXINPUT
  120. #define MAXINPUT 512
  121. #endif
  122. static int readline (lua_State *l, const char *prompt) {
  123. static char buffer[MAXINPUT];
  124. if (prompt) {
  125. fputs(prompt, stdout);
  126. fflush(stdout);
  127. }
  128. if (fgets(buffer, sizeof(buffer), stdin) == NULL)
  129. return 0; /* read fails */
  130. else {
  131. lua_pushstring(l, buffer);
  132. return 1;
  133. }
  134. }
  135. #endif
  136. static const char *get_prompt (int firstline) {
  137. const char *p = NULL;
  138. lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");
  139. lua_rawget(L, LUA_GLOBALSINDEX);
  140. p = lua_tostring(L, -1);
  141. if (p == NULL) p = (firstline ? PROMPT : PROMPT2);
  142. lua_pop(L, 1); /* remove global */
  143. return p;
  144. }
  145. static int incomplete (int status) {
  146. if (status == LUA_ERRSYNTAX &&
  147. strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {
  148. lua_pop(L, 1);
  149. return 1;
  150. }
  151. else
  152. return 0;
  153. }
  154. static int load_string (void) {
  155. int status;
  156. lua_settop(L, 0);
  157. if (lua_readline(L, get_prompt(1)) == 0) /* no input? */
  158. return -1;
  159. if (lua_tostring(L, -1)[0] == '=') { /* line starts with `=' ? */
  160. lua_pushfstring(L, "return %s", lua_tostring(L, -1)+1);/* `=' -> `return' */
  161. lua_remove(L, -2); /* remove original line */
  162. }
  163. for (;;) { /* repeat until gets a complete line */
  164. status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
  165. if (!incomplete(status)) break; /* cannot try to add lines? */
  166. if (lua_readline(L, get_prompt(0)) == 0) /* no more input? */
  167. return -1;
  168. lua_concat(L, lua_gettop(L)); /* join lines */
  169. }
  170. lua_saveline(L, lua_tostring(L, 1));
  171. lua_remove(L, 1); /* remove line */
  172. return status;
  173. }
  174. static void manual_input (void) {
  175. int status;
  176. const char *oldprogname = progname;
  177. progname = NULL;
  178. while ((status = load_string()) != -1) {
  179. if (status == 0) status = lcall(0, 0);
  180. report(status);
  181. if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
  182. lua_getglobal(L, "print");
  183. lua_insert(L, 1);
  184. if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
  185. l_message(progname, lua_pushfstring(L, "error calling `print' (%s)",
  186. lua_tostring(L, -1)));
  187. }
  188. }
  189. lua_settop(L, 0); /* clear stack */
  190. fputs("\n", stdout);
  191. progname = oldprogname;
  192. }
  193. static int checkvar (lua_State *l) {
  194. const char *name = lua_tostring(l, 2);
  195. if (name)
  196. luaL_error(l, "attempt to access undefined variable `%s'", name);
  197. return 0;
  198. }
  199. #define clearinteractive(i) (*i &= 2)
  200. static int handle_argv (char *argv[], int *interactive) {
  201. if (argv[1] == NULL) { /* no arguments? */
  202. *interactive = 0;
  203. if (stdin_is_tty()) {
  204. print_version();
  205. manual_input();
  206. }
  207. else
  208. file_input(NULL); /* executes stdin as a file */
  209. }
  210. else { /* other arguments; loop over them */
  211. int i;
  212. for (i = 1; argv[i] != NULL; i++) {
  213. if (argv[i][0] != '-') break; /* not an option? */
  214. switch (argv[i][1]) { /* option */
  215. case '-': { /* `--' */
  216. if (argv[i][2] != '\0') {
  217. print_usage();
  218. return 1;
  219. }
  220. i++; /* skip this argument */
  221. goto endloop; /* stop handling arguments */
  222. }
  223. case '\0': {
  224. clearinteractive(interactive);
  225. file_input(NULL); /* executes stdin as a file */
  226. break;
  227. }
  228. case 'i': {
  229. *interactive = 2; /* force interactive mode after arguments */
  230. break;
  231. }
  232. case 'v': {
  233. clearinteractive(interactive);
  234. print_version();
  235. break;
  236. }
  237. case 'w': {
  238. if (lua_getmetatable(L, LUA_GLOBALSINDEX)) {
  239. lua_pushcfunction(L, checkvar);
  240. lua_setfield(L, -2, "__index");
  241. }
  242. break;
  243. }
  244. case 'e': {
  245. const char *chunk = argv[i] + 2;
  246. clearinteractive(interactive);
  247. if (*chunk == '\0') chunk = argv[++i];
  248. if (chunk == NULL) {
  249. print_usage();
  250. return 1;
  251. }
  252. if (dostring(chunk, "=<command line>") != 0)
  253. return 1;
  254. break;
  255. }
  256. case 'l': {
  257. const char *filename = argv[i] + 2;
  258. if (*filename == '\0') filename = argv[++i];
  259. if (filename == NULL) {
  260. print_usage();
  261. return 1;
  262. }
  263. if (load_file(filename))
  264. return 1; /* stop if file fails */
  265. break;
  266. }
  267. default: {
  268. clearinteractive(interactive);
  269. print_usage();
  270. return 1;
  271. }
  272. }
  273. } endloop:
  274. if (argv[i] != NULL) {
  275. const char *filename = argv[i];
  276. int narg = getargs(argv, i); /* collect arguments */
  277. int status;
  278. lua_setglobal(L, "arg");
  279. clearinteractive(interactive);
  280. status = luaL_loadfile(L, filename);
  281. lua_insert(L, -(narg+1));
  282. if (status == 0)
  283. status = lcall(narg, 0);
  284. else
  285. lua_pop(L, narg);
  286. return report(status);
  287. }
  288. }
  289. return 0;
  290. }
  291. static void openstdlibs (lua_State *l) {
  292. const luaL_reg *lib = lualibs;
  293. for (; lib->func; lib++) {
  294. lib->func(l); /* open library */
  295. lua_settop(l, 0); /* discard any results */
  296. }
  297. }
  298. static int handle_luainit (void) {
  299. const char *init = getenv("LUA_INIT");
  300. if (init == NULL) return 0; /* status OK */
  301. else if (init[0] == '@')
  302. return file_input(init+1);
  303. else
  304. return dostring(init, "=LUA_INIT");
  305. }
  306. struct Smain {
  307. int argc;
  308. char **argv;
  309. int status;
  310. };
  311. static int pmain (lua_State *l) {
  312. struct Smain *s = (struct Smain *)lua_touserdata(l, 1);
  313. int status;
  314. int interactive = 1;
  315. if (s->argv[0] && s->argv[0][0]) progname = s->argv[0];
  316. L = l;
  317. lua_userinit(l); /* open libraries */
  318. status = handle_luainit();
  319. if (status == 0) {
  320. status = handle_argv(s->argv, &interactive);
  321. if (status == 0 && interactive) manual_input();
  322. }
  323. s->status = status;
  324. return 0;
  325. }
  326. int main (int argc, char *argv[]) {
  327. int status;
  328. struct Smain s;
  329. lua_State *l = lua_open(); /* create state */
  330. if (l == NULL) {
  331. l_message(argv[0], "cannot create state: not enough memory");
  332. return EXIT_FAILURE;
  333. }
  334. s.argc = argc;
  335. s.argv = argv;
  336. status = lua_cpcall(l, &pmain, &s);
  337. report(status);
  338. lua_close(l);
  339. return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
  340. }