lua.c 7.4 KB

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