lua.c 7.4 KB

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