lua.c 8.6 KB

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