lua.c 7.8 KB

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