lua.c 7.5 KB

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