lua.c 7.9 KB

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