lua.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. ** $Id: lua.c,v 1.87 2002/05/16 19:09:19 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 void print_version (void) {
  81. printf("%.80s %.80s\n", LUA_VERSION, LUA_COPYRIGHT);
  82. }
  83. static void assign (char *arg) {
  84. char *eq = strchr(arg, '=');
  85. *eq = '\0'; /* spilt `arg' in two strings (name & value) */
  86. lua_pushstring(L, eq+1);
  87. lua_setglobal(L, arg);
  88. }
  89. static void getargs (char *argv[]) {
  90. int i;
  91. lua_newtable(L);
  92. for (i=0; argv[i]; i++) {
  93. /* arg[i] = argv[i] */
  94. lua_pushnumber(L, i);
  95. lua_pushstring(L, argv[i]);
  96. lua_rawset(L, -3);
  97. }
  98. /* arg.n = maximum index in table `arg' */
  99. lua_pushliteral(L, "n");
  100. lua_pushnumber(L, i-1);
  101. lua_rawset(L, -3);
  102. }
  103. static int l_alert (lua_State *l) {
  104. fputs(luaL_check_string(l, 1), stderr);
  105. putc('\n', stderr);
  106. return 0;
  107. }
  108. static int l_getargs (lua_State *l) {
  109. char **argv = (char **)lua_touserdata(l, lua_upvalueindex(1));
  110. getargs(argv);
  111. return 1;
  112. }
  113. static int docall (int status) {
  114. if (status == 0) status = lcall(1);
  115. report(status);
  116. return status;
  117. }
  118. static int file_input (const char *name) {
  119. return docall(lua_loadfile(L, name));
  120. }
  121. static int dostring (const char *s, const char *name) {
  122. return docall(lua_loadbuffer(L, s, strlen(s), name));
  123. }
  124. #ifdef USE_READLINE
  125. #include <readline/readline.h>
  126. #include <readline/history.h>
  127. #define save_line(b) if (strcspn(b, " \t\n") != 0) add_history(b)
  128. #define push_line(b) if (incomplete) lua_pushstring(L, "\n"); lua_pushstring(L, b); free(b)
  129. #else
  130. #define save_line(b)
  131. #define push_line(b) lua_pushstring(L, b)
  132. /* maximum length of an input line */
  133. #ifndef MAXINPUT
  134. #define MAXINPUT 512
  135. #endif
  136. static char *readline (const char *prompt) {
  137. static char buffer[MAXINPUT];
  138. if (prompt) {
  139. fputs(prompt, stdout);
  140. fflush(stdout);
  141. }
  142. return fgets(buffer, sizeof(buffer), stdin);
  143. }
  144. #endif
  145. static const char *get_prompt (int firstline) {
  146. const char *p = NULL;
  147. lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
  148. p = lua_tostring(L, -1);
  149. if (p == NULL) p = (firstline ? PROMPT : PROMPT2);
  150. lua_pop(L, 1); /* remove global */
  151. return p;
  152. }
  153. static int incomplete (int status) {
  154. if (status == LUA_ERRSYNTAX &&
  155. strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {
  156. lua_pop(L, 1);
  157. return 1;
  158. }
  159. else
  160. return 0;
  161. }
  162. static int load_string (void) {
  163. int firstline = 1;
  164. int status;
  165. lua_settop(L, 0);
  166. do { /* repeat until gets a complete line */
  167. char *buffer = readline(get_prompt(firstline));
  168. if (buffer == NULL) { /* input end? */
  169. lua_settop(L, 0);
  170. return -1; /* input end */
  171. }
  172. if (firstline && buffer[0] == '=') {
  173. buffer[0] = ' ';
  174. lua_pushstring(L, "return");
  175. }
  176. firstline = 0;
  177. push_line(buffer);
  178. lua_concat(L, lua_gettop(L));
  179. status = lua_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
  180. } while (incomplete(status)); /* repeat loop to get rest of `line' */
  181. save_line(lua_tostring(L, 1));
  182. lua_remove(L, 1);
  183. return status;
  184. }
  185. static void manual_input (int version) {
  186. int status;
  187. if (version) print_version();
  188. while ((status = load_string()) != -1) {
  189. if (status == 0) status = lcall(0);
  190. report(status);
  191. if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
  192. lua_getglobal(L, "print");
  193. lua_insert(L, 1);
  194. lua_call(L, lua_gettop(L)-1, 0);
  195. }
  196. }
  197. printf("\n");
  198. }
  199. static int handle_argv (char *argv[], int *toclose) {
  200. if (*argv == NULL) { /* no more arguments? */
  201. if (isatty(0)) {
  202. manual_input(1);
  203. }
  204. else
  205. file_input(NULL); /* executes stdin as a file */
  206. }
  207. else { /* other arguments; loop over them */
  208. int i;
  209. for (i = 0; argv[i] != NULL; i++) {
  210. if (argv[i][0] != '-') { /* not an option? */
  211. if (strchr(argv[i], '='))
  212. assign(argv[i]);
  213. else
  214. if (file_input(argv[i]))
  215. return EXIT_FAILURE; /* stop if file fails */
  216. }
  217. else switch (argv[i][1]) { /* option */
  218. case '\0': {
  219. file_input(NULL); /* executes stdin as a file */
  220. break;
  221. }
  222. case 'i': {
  223. manual_input(0);
  224. break;
  225. }
  226. case 'c': {
  227. *toclose = 1;
  228. break;
  229. }
  230. case 'v': {
  231. print_version();
  232. break;
  233. }
  234. case 'e': {
  235. i++;
  236. if (argv[i] == NULL) {
  237. print_usage();
  238. return EXIT_FAILURE;
  239. }
  240. if (dostring(argv[i], "=prog. argument") != 0) {
  241. fprintf(stderr, "%s: error running argument `%.99s'\n",
  242. LUA_PROGNAME, argv[i]);
  243. return EXIT_FAILURE;
  244. }
  245. break;
  246. }
  247. case 'f': {
  248. i++;
  249. if (argv[i] == NULL) {
  250. print_usage();
  251. return EXIT_FAILURE;
  252. }
  253. getargs(argv+i); /* collect remaining arguments */
  254. lua_setglobal(L, "arg");
  255. return file_input(argv[i]); /* stop scanning arguments */
  256. }
  257. case 's': {
  258. fprintf(stderr,
  259. "%s: option `-s' is deprecated (dynamic stack now)\n",
  260. LUA_PROGNAME);
  261. break;
  262. }
  263. default: {
  264. print_usage();
  265. return EXIT_FAILURE;
  266. }
  267. }
  268. }
  269. }
  270. return EXIT_SUCCESS;
  271. }
  272. static void register_own (char *argv[]) {
  273. lua_pushudataval(L, argv);
  274. lua_pushcclosure(L, l_getargs, 1);
  275. lua_setglobal(L, "getargs");
  276. lua_register(L, "_ALERT", l_alert);
  277. }
  278. static void openstdlibs (lua_State *l) {
  279. lua_baselibopen(l);
  280. lua_tablibopen(l);
  281. lua_iolibopen(l);
  282. lua_strlibopen(l);
  283. lua_mathlibopen(l);
  284. lua_dblibopen(l);
  285. }
  286. static int handle_luainit (void) {
  287. const char *init = getenv("LUA_INIT");
  288. if (init == NULL) return 0; /* status OK */
  289. else if (init[0] == '@')
  290. return file_input(init+1);
  291. else
  292. return dostring(init, "=LUA_INIT");
  293. }
  294. int main (int argc, char *argv[]) {
  295. int status;
  296. int toclose = 0;
  297. (void)argc; /* to avoid warnings */
  298. L = lua_open(); /* create state */
  299. LUA_USERINIT(L); /* open libraries */
  300. register_own(argv); /* create own function */
  301. status = handle_luainit();
  302. if (status != 0) return status;
  303. status = handle_argv(argv+1, &toclose);
  304. if (toclose)
  305. lua_close(L);
  306. return status;
  307. }