lua.c 8.7 KB

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