lua.c 8.0 KB

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