lua.c 7.4 KB

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