lua.c 8.7 KB

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