lua.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. ** $Id: lua.c,v 1.94 2002/06/26 16:37:39 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 arguments\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\n", 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");
  91. return 0;
  92. }
  93. static void print_version (void) {
  94. l_message(NULL, LUA_VERSION " " LUA_COPYRIGHT);
  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. #ifdef USE_READLINE
  121. #include <readline/readline.h>
  122. #include <readline/history.h>
  123. #define save_line(b) if (strcspn(b, " \t\n") != 0) add_history(b)
  124. #define push_line(b) if (incomplete) lua_pushstring(L, "\n"); lua_pushstring(L, b); free(b)
  125. #else
  126. #define save_line(b)
  127. #define push_line(b) lua_pushstring(L, b)
  128. /* maximum length of an input line */
  129. #ifndef MAXINPUT
  130. #define MAXINPUT 512
  131. #endif
  132. static char *readline (const char *prompt) {
  133. static char buffer[MAXINPUT];
  134. if (prompt) {
  135. fputs(prompt, stdout);
  136. fflush(stdout);
  137. }
  138. return fgets(buffer, sizeof(buffer), stdin);
  139. }
  140. #endif
  141. static const char *get_prompt (int firstline) {
  142. const char *p = NULL;
  143. lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
  144. p = lua_tostring(L, -1);
  145. if (p == NULL) p = (firstline ? PROMPT : PROMPT2);
  146. lua_pop(L, 1); /* remove global */
  147. return p;
  148. }
  149. static int incomplete (int status) {
  150. if (status == LUA_ERRSYNTAX &&
  151. strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {
  152. lua_pop(L, 1);
  153. return 1;
  154. }
  155. else
  156. return 0;
  157. }
  158. static int load_string (void) {
  159. int firstline = 1;
  160. int status;
  161. lua_settop(L, 0);
  162. do { /* repeat until gets a complete line */
  163. char *buffer = readline(get_prompt(firstline));
  164. if (buffer == NULL) { /* input end? */
  165. lua_settop(L, 0);
  166. return -1; /* input end */
  167. }
  168. if (firstline && buffer[0] == '=') {
  169. buffer[0] = ' ';
  170. lua_pushstring(L, "return");
  171. }
  172. firstline = 0;
  173. push_line(buffer);
  174. lua_concat(L, lua_gettop(L));
  175. status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
  176. } while (incomplete(status)); /* repeat loop to get rest of `line' */
  177. save_line(lua_tostring(L, 1));
  178. lua_remove(L, 1);
  179. return status;
  180. }
  181. static void manual_input (int version) {
  182. int status;
  183. const char *oldprogname = progname;
  184. progname = NULL;
  185. if (version) print_version();
  186. while ((status = load_string()) != -1) {
  187. if (status == 0) status = lcall(0);
  188. report(status, 0);
  189. if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
  190. lua_getglobal(L, "print");
  191. lua_insert(L, 1);
  192. lua_pcall(L, lua_gettop(L)-1, 0);
  193. }
  194. }
  195. fputs("\n", stdout);
  196. progname = oldprogname;
  197. }
  198. static int handle_argv (char *argv[], int *interactive) {
  199. if (argv[1] == NULL) { /* no more arguments? */
  200. if (isatty(0)) {
  201. manual_input(1);
  202. }
  203. else
  204. file_input(NULL); /* executes stdin as a file */
  205. }
  206. else { /* other arguments; loop over them */
  207. int i;
  208. for (i = 1; argv[i] != NULL; i++) {
  209. if (argv[i][0] != '-') break; /* not an option? */
  210. switch (argv[i][1]) { /* option */
  211. case '-': { /* `--' */
  212. i++; /* skip this argument */
  213. goto endloop; /* stop handling arguments */
  214. }
  215. case '\0': {
  216. file_input(NULL); /* executes stdin as a file */
  217. break;
  218. }
  219. case 'i': {
  220. *interactive = 1;
  221. break;
  222. }
  223. case 'v': {
  224. print_version();
  225. break;
  226. }
  227. case 'e': {
  228. const char *chunk = argv[i] + 2;
  229. if (*chunk == '\0') chunk = argv[++i];
  230. if (chunk == NULL) {
  231. print_usage();
  232. return EXIT_FAILURE;
  233. }
  234. if (dostring(chunk, "=<command line>") != 0)
  235. return EXIT_FAILURE;
  236. break;
  237. }
  238. case 'l': {
  239. const char *filename = argv[i] + 2;
  240. if (*filename == '\0') filename = argv[++i];
  241. if (filename == NULL) {
  242. print_usage();
  243. return EXIT_FAILURE;
  244. }
  245. if (file_input(filename))
  246. return EXIT_FAILURE; /* stop if file fails */
  247. break;
  248. }
  249. case 'c': {
  250. l_message(progname, "option `-c' is deprecated");
  251. break;
  252. }
  253. case 's': {
  254. l_message(progname, "option `-s' is deprecated");
  255. break;
  256. }
  257. default: {
  258. print_usage();
  259. return EXIT_FAILURE;
  260. }
  261. }
  262. } endloop:
  263. if (argv[i] != NULL) {
  264. const char *filename = argv[i];
  265. getargs(argv, i); /* collect remaining arguments */
  266. lua_setglobal(L, "arg");
  267. return file_input(filename); /* stop scanning arguments */
  268. }
  269. }
  270. return EXIT_SUCCESS;
  271. }
  272. static int openstdlibs (lua_State *l) {
  273. return lua_baselibopen(l) +
  274. lua_tablibopen(l) +
  275. lua_iolibopen(l) +
  276. lua_strlibopen(l) +
  277. lua_mathlibopen(l) +
  278. lua_dblibopen(l) +
  279. 0;
  280. }
  281. static int handle_luainit (void) {
  282. const char *init = getenv("LUA_INIT");
  283. if (init == NULL) return 0; /* status OK */
  284. else if (init[0] == '@')
  285. return file_input(init+1);
  286. else
  287. return dostring(init, "=LUA_INIT");
  288. }
  289. int main (int argc, char *argv[]) {
  290. int status;
  291. int interactive = 0;
  292. (void)argc; /* to avoid warnings */
  293. progname = argv[0];
  294. L = lua_open(); /* create state */
  295. lua_atpanic(L, l_panic);
  296. lua_pop(L, LUA_USERINIT(L)); /* open libraries, dischard any results */
  297. status = handle_luainit();
  298. if (status != 0) return status;
  299. status = handle_argv(argv, &interactive);
  300. if (status == 0 && interactive) manual_input(0);
  301. lua_close(L);
  302. return status;
  303. }