lua.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. ** $Id: lua.c,v 1.64 2001/02/23 20:28:26 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 "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 PROMPT
  19. #define PROMPT l_s("> ")
  20. #endif
  21. #ifndef LUA_USERINIT
  22. #define LUA_USERINIT(L) openstdlibs(L)
  23. #endif
  24. #ifndef LUA_USERFINI
  25. #define LUA_USERFINI
  26. #endif
  27. /*
  28. ** global options
  29. */
  30. struct Options {
  31. int toclose;
  32. int stacksize;
  33. };
  34. static lua_State *L = NULL;
  35. typedef void (*handler)(int); /* type for signal actions */
  36. static void laction (int i);
  37. static lua_Hook old_linehook = NULL;
  38. static lua_Hook old_callhook = NULL;
  39. static handler lreset (void) {
  40. return signal(SIGINT, laction);
  41. }
  42. static void lstop (void) {
  43. lua_setlinehook(L, old_linehook);
  44. lua_setcallhook(L, old_callhook);
  45. lreset();
  46. lua_error(L, l_s("interrupted!"));
  47. }
  48. static void laction (int i) {
  49. (void)i; /* to avoid warnings */
  50. signal(SIGINT, SIG_DFL); /* if another SIGINT happens before lstop,
  51. terminate process (default action) */
  52. old_linehook = lua_setlinehook(L, (lua_Hook)lstop);
  53. old_callhook = lua_setcallhook(L, (lua_Hook)lstop);
  54. }
  55. static int ldo (int (*f)(lua_State *l, const l_char *), const l_char *name) {
  56. int res;
  57. handler h = lreset();
  58. int top = lua_gettop(L);
  59. res = f(L, name); /* dostring | dofile */
  60. lua_settop(L, top); /* remove eventual results */
  61. signal(SIGINT, h); /* restore old action */
  62. /* Lua gives no message in such cases, so lua.c provides one */
  63. if (res == LUA_ERRMEM) {
  64. fprintf(stderr, l_s("lua: memory allocation error\n"));
  65. }
  66. else if (res == LUA_ERRERR)
  67. fprintf(stderr, l_s("lua: error in error message\n"));
  68. return res;
  69. }
  70. static void print_message (void) {
  71. fprintf(stderr,
  72. l_s("usage: lua [options]. Available options are:\n")
  73. l_s(" - execute stdin as a file\n")
  74. l_s(" -c close Lua when exiting\n")
  75. l_s(" -e stat execute string `stat'\n")
  76. l_s(" -f name execute file `name' with remaining arguments in table `arg'\n")
  77. l_s(" -i enter interactive mode with prompt\n")
  78. l_s(" -q enter interactive mode without prompt\n")
  79. l_s(" -sNUM set stack size to NUM (must be the first option)\n")
  80. l_s(" -v print version information\n")
  81. l_s(" a=b set global `a' to string `b'\n")
  82. l_s(" name execute file `name'\n")
  83. );
  84. }
  85. static void print_version (void) {
  86. printf(l_s("%.80s %.80s\n"), LUA_VERSION, LUA_COPYRIGHT);
  87. }
  88. static void assign (l_char *arg) {
  89. l_char *eq = strchr(arg, l_c('='));
  90. *eq = l_c('\0'); /* spilt `arg' in two strings (name & value) */
  91. lua_pushstring(L, eq+1);
  92. lua_setglobal(L, arg);
  93. }
  94. static void getargs (l_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_settable(L, -3);
  102. }
  103. /* arg.n = maximum index in table `arg' */
  104. lua_pushliteral(L, l_s("n"));
  105. lua_pushnumber(L, i-1);
  106. lua_settable(L, -3);
  107. }
  108. static int l_getargs (lua_State *l) {
  109. l_char **argv = (l_char **)lua_touserdata(l, -1);
  110. getargs(argv);
  111. return 1;
  112. }
  113. static int file_input (const l_char *argv) {
  114. int result = ldo(lua_dofile, argv);
  115. if (result) {
  116. if (result == LUA_ERRFILE) {
  117. fprintf(stderr, l_s("lua: cannot execute file "));
  118. perror(argv);
  119. }
  120. return EXIT_FAILURE;
  121. }
  122. else
  123. return EXIT_SUCCESS;
  124. }
  125. /* maximum length of an input line */
  126. #ifndef MAXINPUT
  127. #define MAXINPUT 512
  128. #endif
  129. static const l_char *get_prompt (int prompt) {
  130. if (!prompt)
  131. return l_s("");
  132. else {
  133. const l_char *s;
  134. lua_getglobal(L, l_s("_PROMPT"));
  135. s = lua_tostring(L, -1);
  136. if (!s) s = PROMPT;
  137. lua_pop(L, 1); /* remove global */
  138. return s;
  139. }
  140. }
  141. static void manual_input (int version, int prompt) {
  142. if (version) print_version();
  143. for (;;) {
  144. fputs(get_prompt(prompt), stdout); /* show prompt */
  145. for(;;) {
  146. l_char buffer[MAXINPUT];
  147. size_t l;
  148. if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
  149. printf(l_s("\n"));
  150. return;
  151. }
  152. l = strlen(buffer);
  153. if (buffer[l-1] == l_c('\n') && buffer[l-2] == l_c('\\')) {
  154. buffer[l-2] = l_c('\n');
  155. lua_pushlstring(L, buffer, l-1);
  156. }
  157. else {
  158. lua_pushlstring(L, buffer, l);
  159. break;
  160. }
  161. }
  162. lua_concat(L, lua_gettop(L));
  163. ldo(lua_dostring, lua_tostring(L, -1));
  164. lua_settop(L, 0); /* remove eventual results */
  165. }
  166. }
  167. static int handle_argv (l_char *argv[], struct Options *opt) {
  168. if (opt->stacksize > 0) argv++; /* skip option `-s' (if present) */
  169. if (*argv == NULL) { /* no more arguments? */
  170. if (isatty(0)) {
  171. manual_input(1, 1);
  172. }
  173. else
  174. ldo(lua_dofile, NULL); /* executes stdin as a file */
  175. }
  176. else { /* other arguments; loop over them */
  177. int i;
  178. for (i = 0; argv[i] != NULL; i++) {
  179. if (argv[i][0] != l_c('-')) { /* not an option? */
  180. if (strchr(argv[i], l_c('=')))
  181. assign(argv[i]);
  182. else
  183. if (file_input(argv[i]) != EXIT_SUCCESS)
  184. return EXIT_FAILURE; /* stop if file fails */
  185. }
  186. else switch (argv[i][1]) { /* option */
  187. case 0: {
  188. ldo(lua_dofile, NULL); /* executes stdin as a file */
  189. break;
  190. }
  191. case l_c('i'): {
  192. manual_input(0, 1);
  193. break;
  194. }
  195. case l_c('q'): {
  196. manual_input(0, 0);
  197. break;
  198. }
  199. case l_c('c'): {
  200. opt->toclose = 1;
  201. break;
  202. }
  203. case l_c('v'): {
  204. print_version();
  205. break;
  206. }
  207. case l_c('e'): {
  208. i++;
  209. if (argv[i] == NULL) {
  210. print_message();
  211. return EXIT_FAILURE;
  212. }
  213. if (ldo(lua_dostring, argv[i]) != 0) {
  214. fprintf(stderr, l_s("lua: error running argument `%.99s'\n"), argv[i]);
  215. return EXIT_FAILURE;
  216. }
  217. break;
  218. }
  219. case l_c('f'): {
  220. i++;
  221. if (argv[i] == NULL) {
  222. print_message();
  223. return EXIT_FAILURE;
  224. }
  225. getargs(argv+i); /* collect remaining arguments */
  226. lua_setglobal(L, l_s("arg"));
  227. return file_input(argv[i]); /* stop scanning arguments */
  228. }
  229. case l_c('s'): {
  230. fprintf(stderr, l_s("lua: stack size (`-s') must be the first option\n"));
  231. return EXIT_FAILURE;
  232. }
  233. default: {
  234. print_message();
  235. return EXIT_FAILURE;
  236. }
  237. }
  238. }
  239. }
  240. return EXIT_SUCCESS;
  241. }
  242. static void getstacksize (int argc, l_char *argv[], struct Options *opt) {
  243. if (argc >= 2 && argv[1][0] == l_c('-') && argv[1][1] == l_c('s')) {
  244. int stacksize = strtol(&argv[1][2], NULL, 10);
  245. if (stacksize <= 0) {
  246. fprintf(stderr, l_s("lua: invalid stack size ('%.20s')\n"), &argv[1][2]);
  247. exit(EXIT_FAILURE);
  248. }
  249. opt->stacksize = stacksize;
  250. }
  251. else
  252. opt->stacksize = 0; /* no stack size */
  253. }
  254. static void register_getargs (l_char *argv[]) {
  255. lua_pushuserdata(L, argv);
  256. lua_pushcclosure(L, l_getargs, 1);
  257. lua_setglobal(L, l_s("getargs"));
  258. }
  259. static void openstdlibs (lua_State *l) {
  260. lua_baselibopen(l);
  261. lua_iolibopen(l);
  262. lua_strlibopen(l);
  263. lua_mathlibopen(l);
  264. lua_dblibopen(l);
  265. }
  266. int main (int argc, l_char *argv[]) {
  267. struct Options opt;
  268. int status;
  269. opt.toclose = 0;
  270. getstacksize(argc, argv, &opt); /* handle option `-s' */
  271. L = lua_open(opt.stacksize); /* create state */
  272. LUA_USERINIT(L); /* open libraries */
  273. register_getargs(argv); /* create `getargs' function */
  274. status = handle_argv(argv+1, &opt);
  275. if (opt.toclose)
  276. lua_close(L);
  277. return status;
  278. }