lua.c 8.9 KB

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