2
0

lua.c 8.2 KB

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