lua.c 8.7 KB

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