lua.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. ** $Id: lua.c,v 1.77 2002/02/14 22:23:43 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 LUA_PROGNAME
  20. #define LUA_PROGNAME "lua: "
  21. #endif
  22. #ifndef PROMPT
  23. #define PROMPT "> "
  24. #endif
  25. #ifndef PROMPT2
  26. #define PROMPT2 ">> "
  27. #endif
  28. #ifndef LUA_USERINIT
  29. #define LUA_USERINIT(L) openstdlibs(L)
  30. #endif
  31. static lua_State *L = NULL;
  32. static lua_Hook old_linehook = NULL;
  33. static lua_Hook old_callhook = NULL;
  34. static void lstop (void) {
  35. lua_setlinehook(L, old_linehook);
  36. lua_setcallhook(L, old_callhook);
  37. lua_error(L, "interrupted!");
  38. }
  39. static void laction (int i) {
  40. (void)i; /* to avoid warnings */
  41. signal(SIGINT, SIG_DFL); /* if another SIGINT happens before lstop,
  42. terminate process (default action) */
  43. old_linehook = lua_setlinehook(L, (lua_Hook)lstop);
  44. old_callhook = lua_setcallhook(L, (lua_Hook)lstop);
  45. }
  46. /* Lua gives no message in such cases, so we provide one */
  47. static void report (int result) {
  48. if (result == LUA_ERRMEM || result == LUA_ERRERR)
  49. fprintf(stderr, "%s%s\n", LUA_PROGNAME, luaL_errstr(result));
  50. }
  51. static int ldo (int (*f)(lua_State *l, const char *), const char *name,
  52. int clear) {
  53. int result;
  54. int top = lua_gettop(L);
  55. signal(SIGINT, laction);
  56. result = f(L, name); /* dostring | dofile */
  57. signal(SIGINT, SIG_DFL);
  58. if (clear) lua_settop(L, top); /* remove eventual results */
  59. report(result);
  60. return result;
  61. }
  62. static void print_usage (void) {
  63. fprintf(stderr,
  64. "usage: lua [options]. Available options are:\n"
  65. " - execute stdin as a file\n"
  66. " -c close Lua when exiting\n"
  67. " -e stat execute string `stat'\n"
  68. " -f name execute file `name' with remaining arguments in table `arg'\n"
  69. " -i enter interactive mode\n"
  70. " -v print version information\n"
  71. " a=b set global `a' to string `b'\n"
  72. " name execute file `name'\n"
  73. );
  74. }
  75. static void print_version (void) {
  76. printf("%.80s %.80s\n", LUA_VERSION, LUA_COPYRIGHT);
  77. }
  78. static void assign (char *arg) {
  79. char *eq = strchr(arg, '=');
  80. *eq = '\0'; /* spilt `arg' in two strings (name & value) */
  81. lua_pushstring(L, eq+1);
  82. lua_setglobal(L, arg);
  83. }
  84. static void getargs (char *argv[]) {
  85. int i;
  86. lua_newtable(L);
  87. for (i=0; argv[i]; i++) {
  88. /* arg[i] = argv[i] */
  89. lua_pushnumber(L, i);
  90. lua_pushstring(L, argv[i]);
  91. lua_rawset(L, -3);
  92. }
  93. /* arg.n = maximum index in table `arg' */
  94. lua_pushliteral(L, "n");
  95. lua_pushnumber(L, i-1);
  96. lua_rawset(L, -3);
  97. }
  98. static int l_getargs (lua_State *l) {
  99. char **argv = (char **)lua_touserdata(l, lua_upvalueindex(1));
  100. getargs(argv);
  101. return 1;
  102. }
  103. static int file_input (const char *name) {
  104. int result = ldo(lua_dofile, name, 1);
  105. if (result) {
  106. if (result == LUA_ERRFILE) {
  107. fprintf(stderr, "%s%s ", LUA_PROGNAME, luaL_errstr(result));
  108. perror(name);
  109. }
  110. return EXIT_FAILURE;
  111. }
  112. else
  113. return EXIT_SUCCESS;
  114. }
  115. #ifdef USE_READLINE
  116. #include <readline/readline.h>
  117. #include <readline/history.h>
  118. #define save_line(b) if (strcspn(b, " \t\n") != 0) add_history(b)
  119. #define push_line(b) if (incomplete) lua_pushstring(L, "\n"); lua_pushstring(L, b); free(b)
  120. #else
  121. #define save_line(b)
  122. #define push_line(b) lua_pushstring(L, b)
  123. /* maximum length of an input line */
  124. #ifndef MAXINPUT
  125. #define MAXINPUT 512
  126. #endif
  127. static char *readline (const char *prompt) {
  128. static char buffer[MAXINPUT];
  129. if (prompt) {
  130. fputs(prompt, stdout);
  131. fflush(stdout);
  132. }
  133. return fgets(buffer, sizeof(buffer), stdin);
  134. }
  135. #endif
  136. static const char *get_prompt (int incomplete) {
  137. const char *p = NULL;
  138. lua_getglobal(L, incomplete ? "_PROMPT2" : "_PROMPT");
  139. p = lua_tostring(L, -1);
  140. if (p == NULL) p = (incomplete ? PROMPT2 : PROMPT);
  141. lua_pop(L, 1); /* remove global */
  142. return p;
  143. }
  144. static int incomplete = 0;
  145. static int trap_eof (lua_State *l) {
  146. const char *s = lua_tostring(l, 1);
  147. if (strstr(s, "last token read: `<eof>'") != NULL)
  148. incomplete = 1;
  149. else
  150. fprintf(stderr, "error: %s\n", s);
  151. return 0;
  152. }
  153. static int load_string (void) {
  154. lua_getglobal(L, LUA_ERRORMESSAGE);
  155. lua_pushvalue(L, 1);
  156. lua_setglobal(L, LUA_ERRORMESSAGE);
  157. incomplete = 0;
  158. for (;;) { /* repeat until gets a complete line */
  159. int result;
  160. char *buffer = readline(get_prompt(incomplete));
  161. if (buffer == NULL) { /* input end? */
  162. lua_settop(L, 2);
  163. lua_setglobal(L, LUA_ERRORMESSAGE);
  164. return 0;
  165. }
  166. if (!incomplete && buffer[0] == '=') {
  167. buffer[0] = ' ';
  168. lua_pushstring(L, "return");
  169. }
  170. push_line(buffer);
  171. lua_concat(L, lua_gettop(L)-2);
  172. incomplete = 0;
  173. result = lua_loadbuffer(L, lua_tostring(L, 3), lua_strlen(L, 3), "=stdin");
  174. if (incomplete) continue; /* repeat loop to get rest of `line' */
  175. save_line(lua_tostring(L, 3));
  176. lua_remove(L, 3);
  177. if (result == 0) {
  178. lua_insert(L, 2); /* swap compiled chunk with old _ERRORMESSAGE */
  179. lua_setglobal(L, LUA_ERRORMESSAGE); /* restore old _ERRORMESSAGE */
  180. return 1;
  181. }
  182. else
  183. report(result);
  184. }
  185. }
  186. static int lcall (lua_State *l, const char *name) {
  187. (void)name; /* to avoid warnings */
  188. return lua_call(l, 0, LUA_MULTRET);
  189. }
  190. static void manual_input (int version) {
  191. if (version) print_version();
  192. lua_pushcfunction(L, trap_eof); /* set up handler for incomplete lines */
  193. while (load_string()) {
  194. ldo(lcall, NULL, 0);
  195. if (lua_gettop(L) > 1) { /* any result to print? */
  196. lua_getglobal(L, "print");
  197. lua_insert(L, 2);
  198. lua_call(L, lua_gettop(L)-2, 0);
  199. }
  200. else
  201. lua_settop(L, 1); /* remove eventual results */
  202. }
  203. printf("\n");
  204. lua_settop(L, 0); /* remove trap_eof */
  205. }
  206. static int handle_argv (char *argv[], int *toclose) {
  207. if (*argv == NULL) { /* no more arguments? */
  208. if (isatty(0)) {
  209. manual_input(1);
  210. }
  211. else
  212. ldo(lua_dofile, NULL, 1); /* executes stdin as a file */
  213. }
  214. else { /* other arguments; loop over them */
  215. int i;
  216. for (i = 0; argv[i] != NULL; i++) {
  217. if (argv[i][0] != '-') { /* not an option? */
  218. if (strchr(argv[i], '='))
  219. assign(argv[i]);
  220. else
  221. if (file_input(argv[i]) != EXIT_SUCCESS)
  222. return EXIT_FAILURE; /* stop if file fails */
  223. }
  224. else switch (argv[i][1]) { /* option */
  225. case '\0': {
  226. ldo(lua_dofile, NULL, 1); /* executes stdin as a file */
  227. break;
  228. }
  229. case 'i': {
  230. manual_input(0);
  231. break;
  232. }
  233. case 'c': {
  234. *toclose = 1;
  235. break;
  236. }
  237. case 'v': {
  238. print_version();
  239. break;
  240. }
  241. case 'e': {
  242. i++;
  243. if (argv[i] == NULL) {
  244. print_usage();
  245. return EXIT_FAILURE;
  246. }
  247. if (ldo(lua_dostring, argv[i], 1) != 0) {
  248. fprintf(stderr, LUA_PROGNAME "error running argument `%.99s'\n",
  249. argv[i]);
  250. return EXIT_FAILURE;
  251. }
  252. break;
  253. }
  254. case 'f': {
  255. i++;
  256. if (argv[i] == NULL) {
  257. print_usage();
  258. return EXIT_FAILURE;
  259. }
  260. getargs(argv+i); /* collect remaining arguments */
  261. lua_setglobal(L, "arg");
  262. return file_input(argv[i]); /* stop scanning arguments */
  263. }
  264. case 's': {
  265. fprintf(stderr, LUA_PROGNAME
  266. "option `-s' is deprecated (dynamic stack now)\n");
  267. break;
  268. }
  269. default: {
  270. print_usage();
  271. return EXIT_FAILURE;
  272. }
  273. }
  274. }
  275. }
  276. return EXIT_SUCCESS;
  277. }
  278. static void register_getargs (char *argv[]) {
  279. lua_newuserdatabox(L, argv);
  280. lua_pushcclosure(L, l_getargs, 1);
  281. lua_setglobal(L, "getargs");
  282. }
  283. static void openstdlibs (lua_State *l) {
  284. lua_baselibopen(l);
  285. lua_iolibopen(l);
  286. lua_strlibopen(l);
  287. lua_mathlibopen(l);
  288. lua_dblibopen(l);
  289. }
  290. int main (int argc, char *argv[]) {
  291. int status;
  292. int toclose = 0;
  293. (void)argc; /* to avoid warnings */
  294. L = lua_open(); /* create state */
  295. LUA_USERINIT(L); /* open libraries */
  296. register_getargs(argv); /* create `getargs' function */
  297. status = handle_argv(argv+1, &toclose);
  298. if (toclose)
  299. lua_close(L);
  300. return status;
  301. }