lua.c 8.8 KB

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