lua.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. ** $Id: lua.c,v 1.127 2004/06/16 20:22:43 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. lua_getglobal(L, "require");
  109. if (!lua_isfunction(L, -1)) { /* no `require' defined? */
  110. lua_pop(L, 1);
  111. return file_input(name);
  112. }
  113. else {
  114. lua_pushstring(L, name);
  115. return report(lcall(1, 1));
  116. }
  117. }
  118. /*
  119. ** this macro defines a function to show the prompt and reads the
  120. ** next line for manual input
  121. */
  122. #ifndef lua_readline
  123. #define lua_readline(L,prompt) readline(L,prompt)
  124. /* maximum length of an input line */
  125. #ifndef MAXINPUT
  126. #define MAXINPUT 512
  127. #endif
  128. static int readline (lua_State *l, const char *prompt) {
  129. static char buffer[MAXINPUT];
  130. if (prompt) {
  131. fputs(prompt, stdout);
  132. fflush(stdout);
  133. }
  134. if (fgets(buffer, sizeof(buffer), stdin) == NULL)
  135. return 0; /* read fails */
  136. else {
  137. lua_pushstring(l, buffer);
  138. return 1;
  139. }
  140. }
  141. #endif
  142. static const char *get_prompt (int firstline) {
  143. const char *p = NULL;
  144. lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");
  145. lua_rawget(L, LUA_GLOBALSINDEX);
  146. p = lua_tostring(L, -1);
  147. if (p == NULL) p = (firstline ? PROMPT : PROMPT2);
  148. lua_pop(L, 1); /* remove global */
  149. return p;
  150. }
  151. static int incomplete (int status) {
  152. if (status == LUA_ERRSYNTAX &&
  153. strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {
  154. lua_pop(L, 1);
  155. return 1;
  156. }
  157. else
  158. return 0;
  159. }
  160. static int load_string (void) {
  161. int status;
  162. lua_settop(L, 0);
  163. if (lua_readline(L, get_prompt(1)) == 0) /* no input? */
  164. return -1;
  165. if (lua_tostring(L, -1)[0] == '=') { /* line starts with `=' ? */
  166. lua_pushfstring(L, "return %s", lua_tostring(L, -1)+1);/* `=' -> `return' */
  167. lua_remove(L, -2); /* remove original line */
  168. }
  169. for (;;) { /* repeat until gets a complete line */
  170. status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
  171. if (!incomplete(status)) break; /* cannot try to add lines? */
  172. if (lua_readline(L, get_prompt(0)) == 0) /* no more input? */
  173. return -1;
  174. lua_concat(L, lua_gettop(L)); /* join lines */
  175. }
  176. lua_saveline(L, lua_tostring(L, 1));
  177. lua_remove(L, 1); /* remove line */
  178. return status;
  179. }
  180. static void manual_input (void) {
  181. int status;
  182. const char *oldprogname = progname;
  183. progname = NULL;
  184. while ((status = load_string()) != -1) {
  185. if (status == 0) status = lcall(0, 0);
  186. report(status);
  187. if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
  188. lua_getglobal(L, "print");
  189. lua_insert(L, 1);
  190. if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
  191. l_message(progname, lua_pushfstring(L, "error calling `print' (%s)",
  192. lua_tostring(L, -1)));
  193. }
  194. }
  195. lua_settop(L, 0); /* clear stack */
  196. fputs("\n", stdout);
  197. progname = oldprogname;
  198. }
  199. static int checkvar (lua_State *l) {
  200. const char *name = lua_tostring(l, 2);
  201. if (name)
  202. luaL_error(l, "attempt to access undefined variable `%s'", name);
  203. return 0;
  204. }
  205. #define clearinteractive(i) (*i &= 2)
  206. static int handle_argv (char *argv[], int *interactive) {
  207. if (argv[1] == NULL) { /* no arguments? */
  208. *interactive = 0;
  209. if (stdin_is_tty()) {
  210. print_version();
  211. manual_input();
  212. }
  213. else
  214. file_input(NULL); /* executes stdin as a file */
  215. }
  216. else { /* other arguments; loop over them */
  217. int i;
  218. for (i = 1; argv[i] != NULL; i++) {
  219. if (argv[i][0] != '-') break; /* not an option? */
  220. switch (argv[i][1]) { /* option */
  221. case '-': { /* `--' */
  222. if (argv[i][2] != '\0') {
  223. print_usage();
  224. return 1;
  225. }
  226. i++; /* skip this argument */
  227. goto endloop; /* stop handling arguments */
  228. }
  229. case '\0': {
  230. clearinteractive(interactive);
  231. file_input(NULL); /* executes stdin as a file */
  232. break;
  233. }
  234. case 'i': {
  235. *interactive = 2; /* force interactive mode after arguments */
  236. break;
  237. }
  238. case 'v': {
  239. clearinteractive(interactive);
  240. print_version();
  241. break;
  242. }
  243. case 'w': {
  244. if (lua_getmetatable(L, LUA_GLOBALSINDEX)) {
  245. lua_pushcfunction(L, checkvar);
  246. lua_setfield(L, -2, "__index");
  247. }
  248. break;
  249. }
  250. case 'e': {
  251. const char *chunk = argv[i] + 2;
  252. clearinteractive(interactive);
  253. if (*chunk == '\0') chunk = argv[++i];
  254. if (chunk == NULL) {
  255. print_usage();
  256. return 1;
  257. }
  258. if (dostring(chunk, "=<command line>") != 0)
  259. return 1;
  260. break;
  261. }
  262. case 'l': {
  263. const char *filename = argv[i] + 2;
  264. if (*filename == '\0') filename = argv[++i];
  265. if (filename == NULL) {
  266. print_usage();
  267. return 1;
  268. }
  269. if (load_file(filename))
  270. return 1; /* stop if file fails */
  271. break;
  272. }
  273. default: {
  274. clearinteractive(interactive);
  275. print_usage();
  276. return 1;
  277. }
  278. }
  279. } endloop:
  280. if (argv[i] != NULL) {
  281. const char *filename = argv[i];
  282. int narg = getargs(argv, i); /* collect arguments */
  283. int status;
  284. lua_setglobal(L, "arg");
  285. clearinteractive(interactive);
  286. status = luaL_loadfile(L, filename);
  287. lua_insert(L, -(narg+1));
  288. if (status == 0)
  289. status = lcall(narg, 0);
  290. else
  291. lua_pop(L, narg);
  292. return report(status);
  293. }
  294. }
  295. return 0;
  296. }
  297. static void openstdlibs (lua_State *l) {
  298. const luaL_reg *lib = lualibs;
  299. for (; lib->func; lib++) {
  300. lib->func(l); /* open library */
  301. lua_settop(l, 0); /* discard any results */
  302. }
  303. }
  304. static int handle_luainit (void) {
  305. const char *init = getenv("LUA_INIT");
  306. if (init == NULL) return 0; /* status OK */
  307. else if (init[0] == '@')
  308. return file_input(init+1);
  309. else
  310. return dostring(init, "=LUA_INIT");
  311. }
  312. struct Smain {
  313. int argc;
  314. char **argv;
  315. int status;
  316. };
  317. static int pmain (lua_State *l) {
  318. struct Smain *s = (struct Smain *)lua_touserdata(l, 1);
  319. int status;
  320. int interactive = 1;
  321. if (s->argv[0] && s->argv[0][0]) progname = s->argv[0];
  322. L = l;
  323. lua_userinit(l); /* open libraries */
  324. status = handle_luainit();
  325. if (status == 0) {
  326. status = handle_argv(s->argv, &interactive);
  327. if (status == 0 && interactive) manual_input();
  328. }
  329. s->status = status;
  330. return 0;
  331. }
  332. int main (int argc, char *argv[]) {
  333. int status;
  334. struct Smain s;
  335. lua_State *l = lua_open(); /* create state */
  336. if (l == NULL) {
  337. l_message(argv[0], "cannot create state: not enough memory");
  338. return EXIT_FAILURE;
  339. }
  340. s.argc = argc;
  341. s.argv = argv;
  342. status = lua_cpcall(l, &pmain, &s);
  343. report(status);
  344. lua_close(l);
  345. return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
  346. }