lua.c 7.5 KB

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