lua.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. ** $Id: lua.c,v 1.42 2000/06/30 19:17:08 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. #ifndef PROMPT
  14. #define PROMPT "> "
  15. #endif
  16. #ifdef _POSIX_SOURCE
  17. #include <unistd.h>
  18. #else
  19. static int isatty (int x) { return x==0; } /* assume stdin is a tty */
  20. #endif
  21. typedef void (*handler)(int); /* type for signal actions */
  22. static void laction (int i);
  23. static lua_Hook old_linehook = NULL;
  24. static lua_Hook old_callhook = NULL;
  25. static handler lreset (void) {
  26. return signal(SIGINT, laction);
  27. }
  28. static void lstop (void) {
  29. lua_setlinehook(lua_state, old_linehook);
  30. lua_setcallhook(lua_state, old_callhook);
  31. lreset();
  32. lua_error("interrupted!");
  33. }
  34. static void laction (int i) {
  35. (void)i; /* to avoid warnings */
  36. signal(SIGINT, SIG_DFL); /* if another SIGINT happens before lstop,
  37. terminate process (default action) */
  38. old_linehook = lua_setlinehook(lua_state, (lua_Hook)lstop);
  39. old_callhook = lua_setcallhook(lua_state, (lua_Hook)lstop);
  40. }
  41. static int ldo (int (*f)(lua_State *L, const char *), const char *name) {
  42. int res;
  43. handler h = lreset();
  44. res = f(lua_state, name); /* dostring | dofile */
  45. signal(SIGINT, h); /* restore old action */
  46. if (res == LUA_ERRMEM) {
  47. /* Lua gives no message in such case, so lua.c provides one */
  48. fprintf(stderr, "lua: memory allocation error\n");
  49. }
  50. return res;
  51. }
  52. static void print_message (void) {
  53. fprintf(stderr,
  54. "usage: lua [options]. Available options are:\n"
  55. " - execute stdin as a file\n"
  56. " -c close lua when exiting\n"
  57. " -d turn debug on\n"
  58. " -e stat execute string `stat'\n"
  59. " -f name execute file `name' with remaining arguments in table `arg'\n"
  60. " -i enter interactive mode with prompt\n"
  61. " -q enter interactive mode without prompt\n"
  62. " -sNUM set stack size to NUM (must be the first option)\n"
  63. " -v print version information\n"
  64. " a=b set global `a' to string `b'\n"
  65. " name execute file `name'\n"
  66. );
  67. }
  68. static void print_version (void) {
  69. printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
  70. }
  71. static void assign (char *arg) {
  72. char *eq = strchr(arg, '=');
  73. *eq = '\0'; /* spilt `arg' in two strings (name & value) */
  74. lua_pushstring(eq+1);
  75. lua_setglobal(arg);
  76. }
  77. static lua_Object getargs (char *argv[]) {
  78. lua_Object args = lua_createtable();
  79. int i;
  80. for (i=0; argv[i]; i++) {
  81. /* arg[i] = argv[i] */
  82. lua_pushobject(args); lua_pushnumber(i);
  83. lua_pushstring(argv[i]); lua_settable();
  84. }
  85. /* arg.n = maximum index in table `arg' */
  86. lua_pushobject(args); lua_pushstring("n");
  87. lua_pushnumber(i-1); lua_settable();
  88. return args;
  89. }
  90. static void l_getargs (void) {
  91. char **argv = (char **)lua_getuserdata(lua_getparam(1));
  92. lua_pushobject(getargs(argv));
  93. }
  94. static void file_input (const char *argv) {
  95. int result = ldo(lua_dofile, argv);
  96. if (result) {
  97. if (result == LUA_ERRFILE) {
  98. fprintf(stderr, "lua: cannot execute file ");
  99. perror(argv);
  100. }
  101. exit(1);
  102. }
  103. }
  104. /* maximum length of an input string */
  105. #ifndef MAXINPUT
  106. #define MAXINPUT BUFSIZ
  107. #endif
  108. static void manual_input (int version, int prompt) {
  109. int cont = 1;
  110. if (version) print_version();
  111. while (cont) {
  112. char buffer[MAXINPUT];
  113. int i = 0;
  114. lua_beginblock();
  115. if (prompt) {
  116. const char *s = lua_getstring(lua_getglobal("_PROMPT"));
  117. if (!s) s = PROMPT;
  118. fputs(s, stdout);
  119. }
  120. for(;;) {
  121. int c = getchar();
  122. if (c == EOF) {
  123. cont = 0;
  124. break;
  125. }
  126. else if (c == '\n') {
  127. if (i>0 && buffer[i-1] == '\\')
  128. buffer[i-1] = '\n';
  129. else break;
  130. }
  131. else if (i >= MAXINPUT-1) {
  132. fprintf(stderr, "lua: input line too long\n");
  133. break;
  134. }
  135. else buffer[i++] = (char)c;
  136. }
  137. buffer[i] = '\0';
  138. ldo(lua_dostring, buffer);
  139. lua_endblock();
  140. }
  141. printf("\n");
  142. }
  143. int main (int argc, char *argv[]) {
  144. int toclose = 0;
  145. int status = EXIT_SUCCESS;
  146. int i = 1;
  147. int stacksize = 0;
  148. if (i < argc && argv[1][0] == '-' && argv[1][1] == 's') {
  149. stacksize = atoi(&argv[1][2]);
  150. if (stacksize == 0) {
  151. fprintf(stderr, "lua: invalid stack size ('%s')\n", &argv[1][2]);
  152. exit(EXIT_FAILURE);
  153. }
  154. i++;
  155. }
  156. lua_state = lua_newstate(stacksize, 1);
  157. lua_userinit();
  158. lua_pushuserdata(argv);
  159. lua_pushcclosure(l_getargs, 1);
  160. lua_setglobal("getargs");
  161. if (i >= argc) { /* no other arguments? */
  162. if (isatty(0)) {
  163. manual_input(1, 1);
  164. }
  165. else
  166. ldo(lua_dofile, NULL); /* executes stdin as a file */
  167. }
  168. else for (; i<argc; i++) {
  169. if (argv[i][0] == '-') { /* option? */
  170. switch (argv[i][1]) {
  171. case 0:
  172. ldo(lua_dofile, NULL); /* executes stdin as a file */
  173. break;
  174. case 'i':
  175. manual_input(0, 1);
  176. break;
  177. case 'q':
  178. manual_input(0, 0);
  179. break;
  180. case 'd':
  181. lua_setdebug(lua_state, 1);
  182. if (i+1 >= argc) { /* last argument? */
  183. manual_input(1, 1);
  184. }
  185. break;
  186. case 'c':
  187. toclose = 1;
  188. break;
  189. case 'v':
  190. print_version();
  191. break;
  192. case 'e':
  193. i++;
  194. if (i >= argc) {
  195. print_message();
  196. status = EXIT_FAILURE; goto endloop;
  197. }
  198. if (ldo(lua_dostring, argv[i]) != 0) {
  199. fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
  200. status = EXIT_FAILURE; goto endloop;
  201. }
  202. break;
  203. case 'f':
  204. i++;
  205. if (i >= argc) {
  206. print_message();
  207. status = EXIT_FAILURE; goto endloop;
  208. }
  209. lua_pushobject(getargs(argv+i)); /* collect remaining arguments */
  210. lua_setglobal("arg");
  211. file_input(argv[i]);
  212. goto endloop; /* stop scanning arguments */
  213. break;
  214. case 's':
  215. fprintf(stderr, "lua: stack size (`-s') must be the first option\n");
  216. status = EXIT_FAILURE; goto endloop;
  217. default:
  218. print_message();
  219. status = EXIT_FAILURE; goto endloop;
  220. }
  221. }
  222. else if (strchr(argv[i], '='))
  223. assign(argv[i]);
  224. else
  225. file_input(argv[i]);
  226. }
  227. endloop:
  228. if (toclose)
  229. lua_close();
  230. return status;
  231. }