2
0

lua.c 8.6 KB

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