2
0

lua.c 8.9 KB

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