lua.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. ** $Id: lua.c,v 1.95 2002/07/09 18:19:44 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. int moreinput = 1;
  166. lua_settop(L, 0);
  167. if (read_line(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 (!moreinput || !incomplete(status)) /* cannot try to add lines? */
  176. break;
  177. lua_pushliteral(L, "\n"); /* no; add line separator */
  178. moreinput = read_line(get_prompt(0));
  179. lua_concat(L, lua_gettop(L)); /* join lines and line separator */
  180. }
  181. save_line(lua_tostring(L, 1));
  182. lua_remove(L, 1); /* remove line */
  183. return status;
  184. }
  185. static void manual_input (void) {
  186. int status;
  187. const char *oldprogname = progname;
  188. progname = NULL;
  189. while ((status = load_string()) != -1) {
  190. if (status == 0) status = lcall(0);
  191. report(status, 0);
  192. if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
  193. lua_getglobal(L, "print");
  194. lua_insert(L, 1);
  195. lua_pcall(L, lua_gettop(L)-1, 0);
  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\n");
  256. break;
  257. }
  258. case 's': {
  259. l_message(progname, "option `-s' is deprecated\n");
  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. }