lua.c 9.2 KB

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