lua.c 9.7 KB

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