lua.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. ** $Id: lua.c,v 1.34 2000/03/03 14:58:26 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_Dbghook old_linehook = NULL;
  24. static lua_Dbghook 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_Dbghook)lstop);
  39. old_callhook = lua_setcallhook(lua_state, (lua_Dbghook)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. return res;
  47. }
  48. static void print_message (void) {
  49. fprintf(stderr,
  50. "usage: lua [options]. Available options are:\n"
  51. " - execute stdin as a file\n"
  52. " -d turn debug on\n"
  53. " -e stat execute string `stat'\n"
  54. " -f name execute file `name' with remaining arguments in table `arg'\n"
  55. " -i enter interactive mode with prompt\n"
  56. " -q enter interactive mode without prompt\n"
  57. " -sNUM set stack size to NUM (must be first option)\n"
  58. " -v print version information\n"
  59. " a=b set global `a' to string `b'\n"
  60. " name execute file `name'\n"
  61. );
  62. }
  63. static void print_version (void) {
  64. printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
  65. }
  66. static void assign (char *arg) {
  67. char *eq = strchr(arg, '=');
  68. *eq = '\0'; /* spilt `arg' in two strings (name & value) */
  69. lua_pushstring(eq+1);
  70. lua_setglobal(arg);
  71. }
  72. static void getargs (char *argv[]) {
  73. lua_beginblock(); {
  74. int i;
  75. lua_Object args = lua_createtable();
  76. lua_pushobject(args);
  77. lua_setglobal("arg");
  78. for (i=0; argv[i]; i++) {
  79. /* arg[i] = argv[i] */
  80. lua_pushobject(args); lua_pushnumber(i);
  81. lua_pushstring(argv[i]); lua_settable();
  82. }
  83. /* arg.n = maximum index in table `arg' */
  84. lua_pushobject(args); lua_pushstring("n");
  85. lua_pushnumber(i-1); lua_settable();
  86. } lua_endblock();
  87. }
  88. static void file_input (const char *argv) {
  89. int result = ldo(lua_dofile, argv);
  90. if (result) {
  91. if (result == 2) {
  92. fprintf(stderr, "lua: cannot execute file ");
  93. perror(argv);
  94. }
  95. exit(1);
  96. }
  97. }
  98. static void manual_input (int version, int prompt) {
  99. int cont = 1;
  100. if (version) print_version();
  101. while (cont) {
  102. char buffer[BUFSIZ];
  103. int i = 0;
  104. lua_beginblock();
  105. if (prompt) {
  106. const char *s = lua_getstring(lua_getglobal("_PROMPT"));
  107. if (!s) s = PROMPT;
  108. fputs(s, stdout);
  109. }
  110. for(;;) {
  111. int c = getchar();
  112. if (c == EOF) {
  113. cont = 0;
  114. break;
  115. }
  116. else if (c == '\n') {
  117. if (i>0 && buffer[i-1] == '\\')
  118. buffer[i-1] = '\n';
  119. else break;
  120. }
  121. else if (i >= BUFSIZ-1) {
  122. fprintf(stderr, "lua: input line too long\n");
  123. break;
  124. }
  125. else buffer[i++] = (char)c;
  126. }
  127. buffer[i] = '\0';
  128. ldo(lua_dostring, buffer);
  129. lua_endblock();
  130. }
  131. printf("\n");
  132. }
  133. int main (int argc, char *argv[]) {
  134. (void)argc; /* unused */
  135. argv++; /* skip program name */
  136. if (*argv && (*argv)[0] == '-' && (*argv)[1] == 's') {
  137. int stacksize = atoi((*argv)+2);
  138. if (stacksize == 0) {
  139. fprintf(stderr, "lua: invalid stack size\n");
  140. exit(1);
  141. }
  142. argv++;
  143. lua_state = lua_newstate("stack", stacksize, NULL);
  144. }
  145. else
  146. lua_state = lua_newstate(NULL);
  147. lua_userinit();
  148. if (*argv == NULL) { /* no other arguments? */
  149. if (isatty(0)) {
  150. manual_input(1, 1);
  151. }
  152. else
  153. ldo(lua_dofile, NULL); /* executes stdin as a file */
  154. }
  155. else for (; *argv; argv++) {
  156. if ((*argv)[0] == '-') { /* option? */
  157. switch ((*argv)[1]) {
  158. case 0:
  159. ldo(lua_dofile, NULL); /* executes stdin as a file */
  160. break;
  161. case 'i':
  162. manual_input(0, 1);
  163. break;
  164. case 'q':
  165. manual_input(0, 0);
  166. break;
  167. case 'd':
  168. lua_setdebug(lua_state, 1);
  169. if (*(argv+1) == NULL) { /* last argument? */
  170. manual_input(1, 1);
  171. }
  172. break;
  173. case 'v':
  174. print_version();
  175. break;
  176. case 'e':
  177. argv++;
  178. if (*argv == NULL) {
  179. print_message();
  180. exit(1);
  181. }
  182. if (ldo(lua_dostring, *argv) != 0) {
  183. fprintf(stderr, "lua: error running argument `%s'\n", *argv);
  184. exit(1);
  185. }
  186. break;
  187. case 'f':
  188. argv++;
  189. if (*argv == NULL) {
  190. print_message();
  191. exit(1);
  192. }
  193. getargs(argv); /* collect remaining arguments */
  194. file_input(*argv);
  195. goto endloop; /* stop scanning arguments */
  196. break;
  197. case 's':
  198. fprintf(stderr, "lua: stack size (`-s') must be the first option\n");
  199. exit(1);
  200. default:
  201. print_message();
  202. exit(1);
  203. }
  204. }
  205. else if (strchr(*argv, '='))
  206. assign(*argv);
  207. else
  208. file_input(*argv);
  209. }
  210. endloop:
  211. #ifdef DEBUG
  212. lua_close();
  213. #endif
  214. return 0;
  215. }