lua.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. ** $Id: lua.c,v 1.88 2002/05/23 19:43:04 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. static void report (int status) {
  46. if (status) {
  47. lua_getglobal(L, "_ALERT");
  48. lua_pushvalue(L, -2);
  49. lua_pcall(L, 1, 0, 0);
  50. lua_pop(L, 1);
  51. }
  52. }
  53. static int lcall (int clear) {
  54. int status;
  55. int top = lua_gettop(L);
  56. lua_getglobal(L, "_ERRORMESSAGE");
  57. lua_insert(L, top);
  58. signal(SIGINT, laction);
  59. status = lua_pcall(L, 0, LUA_MULTRET, top);
  60. signal(SIGINT, SIG_DFL);
  61. if (status == 0) {
  62. if (clear) lua_settop(L, top); /* remove eventual results */
  63. else lua_remove(L, top); /* else remove only error function */
  64. }
  65. return status;
  66. }
  67. static void print_usage (void) {
  68. fprintf(stderr,
  69. "usage: %s [options]. Available options are:\n"
  70. " - execute stdin as a file\n"
  71. " -c close Lua when exiting\n"
  72. " -e stat execute string `stat'\n"
  73. " -f name execute file `name' with remaining arguments in table `arg'\n"
  74. " -i enter interactive mode\n"
  75. " -v print version information\n"
  76. " a=b set global `a' to string `b'\n"
  77. " name execute file `name'\n",
  78. LUA_PROGNAME);
  79. }
  80. static int l_panic (lua_State *l) {
  81. (void)l;
  82. fputs("unable to recover; exiting\n", stderr);
  83. return 0;
  84. }
  85. static void print_version (void) {
  86. printf("%.80s %.80s\n", LUA_VERSION, LUA_COPYRIGHT);
  87. }
  88. static void assign (const char *arg) {
  89. char *eq = strchr(arg, '='); /* arg is `name=value'; find the `=' */
  90. lua_pushlstring(L, arg, eq-arg); /* push name */
  91. lua_pushstring(L, eq+1); /* push value */
  92. lua_settable(L, LUA_GLOBALSINDEX); /* _G.name = value */
  93. }
  94. static void getargs (char *argv[]) {
  95. int i;
  96. lua_newtable(L);
  97. for (i=0; argv[i]; i++) {
  98. /* arg[i] = argv[i] */
  99. lua_pushnumber(L, i);
  100. lua_pushstring(L, argv[i]);
  101. lua_rawset(L, -3);
  102. }
  103. /* arg.n = maximum index in table `arg' */
  104. lua_pushliteral(L, "n");
  105. lua_pushnumber(L, i-1);
  106. lua_rawset(L, -3);
  107. }
  108. static int l_alert (lua_State *l) {
  109. fputs(luaL_check_string(l, 1), stderr);
  110. putc('\n', stderr);
  111. return 0;
  112. }
  113. static int l_getargs (lua_State *l) {
  114. char **argv = (char **)lua_touserdata(l, lua_upvalueindex(1));
  115. getargs(argv);
  116. return 1;
  117. }
  118. static int docall (int status) {
  119. if (status == 0) status = lcall(1);
  120. report(status);
  121. return status;
  122. }
  123. static int file_input (const char *name) {
  124. return docall(luaL_loadfile(L, name));
  125. }
  126. static int dostring (const char *s, const char *name) {
  127. return docall(luaL_loadbuffer(L, s, strlen(s), name));
  128. }
  129. #ifdef USE_READLINE
  130. #include <readline/readline.h>
  131. #include <readline/history.h>
  132. #define save_line(b) if (strcspn(b, " \t\n") != 0) add_history(b)
  133. #define push_line(b) if (incomplete) lua_pushstring(L, "\n"); lua_pushstring(L, b); free(b)
  134. #else
  135. #define save_line(b)
  136. #define push_line(b) lua_pushstring(L, b)
  137. /* maximum length of an input line */
  138. #ifndef MAXINPUT
  139. #define MAXINPUT 512
  140. #endif
  141. static char *readline (const char *prompt) {
  142. static char buffer[MAXINPUT];
  143. if (prompt) {
  144. fputs(prompt, stdout);
  145. fflush(stdout);
  146. }
  147. return fgets(buffer, sizeof(buffer), stdin);
  148. }
  149. #endif
  150. static const char *get_prompt (int firstline) {
  151. const char *p = NULL;
  152. lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
  153. p = lua_tostring(L, -1);
  154. if (p == NULL) p = (firstline ? PROMPT : PROMPT2);
  155. lua_pop(L, 1); /* remove global */
  156. return p;
  157. }
  158. static int incomplete (int status) {
  159. if (status == LUA_ERRSYNTAX &&
  160. strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {
  161. lua_pop(L, 1);
  162. return 1;
  163. }
  164. else
  165. return 0;
  166. }
  167. static int load_string (void) {
  168. int firstline = 1;
  169. int status;
  170. lua_settop(L, 0);
  171. do { /* repeat until gets a complete line */
  172. char *buffer = readline(get_prompt(firstline));
  173. if (buffer == NULL) { /* input end? */
  174. lua_settop(L, 0);
  175. return -1; /* input end */
  176. }
  177. if (firstline && buffer[0] == '=') {
  178. buffer[0] = ' ';
  179. lua_pushstring(L, "return");
  180. }
  181. firstline = 0;
  182. push_line(buffer);
  183. lua_concat(L, lua_gettop(L));
  184. status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
  185. } while (incomplete(status)); /* repeat loop to get rest of `line' */
  186. save_line(lua_tostring(L, 1));
  187. lua_remove(L, 1);
  188. return status;
  189. }
  190. static void manual_input (int version) {
  191. int status;
  192. if (version) print_version();
  193. while ((status = load_string()) != -1) {
  194. if (status == 0) status = lcall(0);
  195. report(status);
  196. if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
  197. lua_getglobal(L, "print");
  198. lua_insert(L, 1);
  199. lua_call(L, lua_gettop(L)-1, 0);
  200. }
  201. }
  202. printf("\n");
  203. }
  204. static int handle_argv (char *argv[], int *toclose) {
  205. if (*argv == NULL) { /* no more arguments? */
  206. if (isatty(0)) {
  207. manual_input(1);
  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 = 0; argv[i] != NULL; i++) {
  215. if (argv[i][0] != '-') { /* not an option? */
  216. if (strchr(argv[i], '='))
  217. assign(argv[i]);
  218. else
  219. if (file_input(argv[i]))
  220. return EXIT_FAILURE; /* stop if file fails */
  221. }
  222. else switch (argv[i][1]) { /* option */
  223. case '\0': {
  224. file_input(NULL); /* executes stdin as a file */
  225. break;
  226. }
  227. case 'i': {
  228. manual_input(0);
  229. break;
  230. }
  231. case 'c': {
  232. *toclose = 1;
  233. break;
  234. }
  235. case 'v': {
  236. print_version();
  237. break;
  238. }
  239. case 'e': {
  240. i++;
  241. if (argv[i] == NULL) {
  242. print_usage();
  243. return EXIT_FAILURE;
  244. }
  245. if (dostring(argv[i], "=prog. argument") != 0) {
  246. fprintf(stderr, "%s: error running argument `%.99s'\n",
  247. LUA_PROGNAME, argv[i]);
  248. return EXIT_FAILURE;
  249. }
  250. break;
  251. }
  252. case 'f': {
  253. i++;
  254. if (argv[i] == NULL) {
  255. print_usage();
  256. return EXIT_FAILURE;
  257. }
  258. getargs(argv+i); /* collect remaining arguments */
  259. lua_setglobal(L, "arg");
  260. return file_input(argv[i]); /* stop scanning arguments */
  261. }
  262. case 's': {
  263. fprintf(stderr,
  264. "%s: option `-s' is deprecated (dynamic stack now)\n",
  265. LUA_PROGNAME);
  266. break;
  267. }
  268. default: {
  269. print_usage();
  270. return EXIT_FAILURE;
  271. }
  272. }
  273. }
  274. }
  275. return EXIT_SUCCESS;
  276. }
  277. static void register_own (char *argv[]) {
  278. lua_pushudataval(L, argv);
  279. lua_pushcclosure(L, l_getargs, 1);
  280. lua_setglobal(L, "getargs");
  281. lua_register(L, "_ALERT", l_alert);
  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. static int handle_luainit (void) {
  292. const char *init = getenv("LUA_INIT");
  293. if (init == NULL) return 0; /* status OK */
  294. else if (init[0] == '@')
  295. return file_input(init+1);
  296. else
  297. return dostring(init, "=LUA_INIT");
  298. }
  299. int main (int argc, char *argv[]) {
  300. int status;
  301. int toclose = 0;
  302. (void)argc; /* to avoid warnings */
  303. L = lua_open(); /* create state */
  304. lua_setpanicf(L, l_panic);
  305. LUA_USERINIT(L); /* open libraries */
  306. register_own(argv); /* create own function */
  307. status = handle_luainit();
  308. if (status != 0) return status;
  309. status = handle_argv(argv+1, &toclose);
  310. if (toclose)
  311. lua_close(L);
  312. return status;
  313. }