lua.c 7.5 KB

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