lua.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. ** $Id: lua.c,v 1.82 2002/04/09 20:19:06 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. signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
  41. terminate process (default action) */
  42. old_linehook = lua_setlinehook(L, (lua_Hook)lstop);
  43. old_callhook = lua_setcallhook(L, (lua_Hook)lstop);
  44. }
  45. /* Lua gives no message in such cases, so we provide one */
  46. static void report (int result) {
  47. if (result == LUA_ERRMEM || result == LUA_ERRERR)
  48. fprintf(stderr, "%s%s\n", LUA_PROGNAME, luaL_errstr(result));
  49. }
  50. static int ldo (int (*f)(lua_State *l, const char *), const char *name,
  51. int clear) {
  52. int result;
  53. int top = lua_gettop(L);
  54. signal(SIGINT, laction);
  55. result = f(L, name); /* dostring | dofile */
  56. signal(SIGINT, SIG_DFL);
  57. if (clear) lua_settop(L, top); /* remove eventual results */
  58. report(result);
  59. return result;
  60. }
  61. static void print_usage (void) {
  62. fprintf(stderr,
  63. "usage: lua [options]. Available options are:\n"
  64. " - execute stdin as a file\n"
  65. " -c close Lua when exiting\n"
  66. " -e stat execute string `stat'\n"
  67. " -f name execute file `name' with remaining arguments in table `arg'\n"
  68. " -i enter interactive mode\n"
  69. " -v print version information\n"
  70. " a=b set global `a' to string `b'\n"
  71. " name execute file `name'\n"
  72. );
  73. }
  74. static void print_version (void) {
  75. printf("%.80s %.80s\n", LUA_VERSION, LUA_COPYRIGHT);
  76. }
  77. static void assign (char *arg) {
  78. char *eq = strchr(arg, '=');
  79. *eq = '\0'; /* spilt `arg' in two strings (name & value) */
  80. lua_pushstring(L, eq+1);
  81. lua_setglobal(L, arg);
  82. }
  83. static void getargs (char *argv[]) {
  84. int i;
  85. lua_newtable(L);
  86. for (i=0; argv[i]; i++) {
  87. /* arg[i] = argv[i] */
  88. lua_pushnumber(L, i);
  89. lua_pushstring(L, argv[i]);
  90. lua_rawset(L, -3);
  91. }
  92. /* arg.n = maximum index in table `arg' */
  93. lua_pushliteral(L, "n");
  94. lua_pushnumber(L, i-1);
  95. lua_rawset(L, -3);
  96. }
  97. static int l_getargs (lua_State *l) {
  98. char **argv = (char **)lua_touserdata(l, lua_upvalueindex(1));
  99. getargs(argv);
  100. return 1;
  101. }
  102. static int file_input (const char *name) {
  103. int result = ldo(lua_dofile, name, 1);
  104. if (result) {
  105. if (result == LUA_ERRFILE) {
  106. fprintf(stderr, "%s%s ", LUA_PROGNAME, luaL_errstr(result));
  107. perror(name);
  108. }
  109. return EXIT_FAILURE;
  110. }
  111. else
  112. return EXIT_SUCCESS;
  113. }
  114. #ifdef USE_READLINE
  115. #include <readline/readline.h>
  116. #include <readline/history.h>
  117. #define save_line(b) if (strcspn(b, " \t\n") != 0) add_history(b)
  118. #define push_line(b) if (incomplete) lua_pushstring(L, "\n"); lua_pushstring(L, b); free(b)
  119. #else
  120. #define save_line(b)
  121. #define push_line(b) lua_pushstring(L, b)
  122. /* maximum length of an input line */
  123. #ifndef MAXINPUT
  124. #define MAXINPUT 512
  125. #endif
  126. static char *readline (const char *prompt) {
  127. static char buffer[MAXINPUT];
  128. if (prompt) {
  129. fputs(prompt, stdout);
  130. fflush(stdout);
  131. }
  132. return fgets(buffer, sizeof(buffer), stdin);
  133. }
  134. #endif
  135. static const char *get_prompt (int incomplete) {
  136. const char *p = NULL;
  137. lua_getglobal(L, incomplete ? "_PROMPT2" : "_PROMPT");
  138. p = lua_tostring(L, -1);
  139. if (p == NULL) p = (incomplete ? PROMPT2 : PROMPT);
  140. lua_pop(L, 1); /* remove global */
  141. return p;
  142. }
  143. static int incomplete = 0;
  144. static int trap_eof (lua_State *l) {
  145. const char *s = lua_tostring(l, 1);
  146. if (strstr(s, "last token read: `<eof>'") != NULL)
  147. incomplete = 1;
  148. else
  149. fprintf(stderr, "error: %s\n", s);
  150. return 0;
  151. }
  152. static int load_string (void) {
  153. lua_getglobal(L, "_ERRORMESSAGE");
  154. lua_pushvalue(L, 1);
  155. lua_setglobal(L, "_ERRORMESSAGE");
  156. incomplete = 0;
  157. for (;;) { /* repeat until gets a complete line */
  158. int result;
  159. char *buffer = readline(get_prompt(incomplete));
  160. if (buffer == NULL) { /* input end? */
  161. lua_settop(L, 2);
  162. lua_setglobal(L, "_ERRORMESSAGE");
  163. return 0;
  164. }
  165. if (!incomplete && buffer[0] == '=') {
  166. buffer[0] = ' ';
  167. lua_pushstring(L, "return");
  168. }
  169. push_line(buffer);
  170. lua_concat(L, lua_gettop(L)-2);
  171. incomplete = 0;
  172. result = lua_loadbuffer(L, lua_tostring(L, 3), lua_strlen(L, 3), "=stdin");
  173. if (incomplete) continue; /* repeat loop to get rest of `line' */
  174. save_line(lua_tostring(L, 3));
  175. lua_remove(L, 3);
  176. if (result == 0) {
  177. lua_insert(L, 2); /* swap compiled chunk with old _ERRORMESSAGE */
  178. lua_setglobal(L, "_ERRORMESSAGE"); /* restore old _ERRORMESSAGE */
  179. return 1;
  180. }
  181. else
  182. report(result);
  183. }
  184. }
  185. static int lcall (lua_State *l, const char *name) {
  186. (void)name; /* to avoid warnings */
  187. return lua_call(l, 0, LUA_MULTRET);
  188. }
  189. static void manual_input (int version) {
  190. if (version) print_version();
  191. lua_settop(L, 0);
  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_pop(L, 1); /* 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_pushudataval(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_tablibopen(l);
  286. lua_iolibopen(l);
  287. lua_strlibopen(l);
  288. lua_mathlibopen(l);
  289. lua_dblibopen(l);
  290. }
  291. int main (int argc, char *argv[]) {
  292. int status;
  293. int toclose = 0;
  294. (void)argc; /* to avoid warnings */
  295. L = lua_open(); /* create state */
  296. LUA_USERINIT(L); /* open libraries */
  297. register_getargs(argv); /* create `getargs' function */
  298. status = handle_argv(argv+1, &toclose);
  299. if (toclose)
  300. lua_close(L);
  301. return status;
  302. }