2
0

lua.c 9.1 KB

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