2
0

lua.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. ** $Id: lua.c,v 1.29 1999/12/20 13:03:20 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_LHFunction old_linehook = NULL;
  24. static lua_CHFunction 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_LHFunction)lstop);
  39. old_callhook = lua_setcallhook(lua_state, (lua_CHFunction)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. "Lua: command line options:\n"
  51. " -v print version information\n"
  52. " -d turn debug on\n"
  53. " -e stat dostring `stat'\n"
  54. " -q interactive mode without prompt\n"
  55. " -i interactive mode with prompt\n"
  56. " - execute stdin as a file\n"
  57. " -f name dofile `name' with following arguments in table `arg'\n"
  58. " a=b set global `a' with string `b'\n"
  59. " name dofile `name'\n\n");
  60. }
  61. static void assign (char *arg) {
  62. if (strlen(arg) >= 500)
  63. fprintf(stderr, "lua: shell argument too long");
  64. else {
  65. char buffer[500];
  66. char *eq = strchr(arg, '=');
  67. lua_pushstring(eq+1);
  68. strncpy(buffer, arg, eq-arg);
  69. buffer[eq-arg] = 0;
  70. lua_setglobal(buffer);
  71. }
  72. }
  73. static void getargs (int argc, char *argv[]) {
  74. lua_beginblock(); {
  75. int i;
  76. lua_Object args = lua_createtable();
  77. lua_pushobject(args);
  78. lua_setglobal("arg");
  79. for (i=0; i<argc; i++) {
  80. /* arg[i] = argv[i] */
  81. lua_pushobject(args); lua_pushnumber(i);
  82. lua_pushstring(argv[i]); lua_settable();
  83. }
  84. /* arg.n = maximum index in table `arg' */
  85. lua_pushobject(args); lua_pushstring("n");
  86. lua_pushnumber(argc-1); lua_settable();
  87. } lua_endblock();
  88. }
  89. static void file_input (char **argv, int arg) {
  90. int result = ldo(lua_dofile, argv[arg]);
  91. if (result) {
  92. if (result == 2) {
  93. fprintf(stderr, "lua: cannot execute file ");
  94. perror(argv[arg]);
  95. }
  96. exit(1);
  97. }
  98. }
  99. static void manual_input (int prompt) {
  100. int cont = 1;
  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. printf("%s", s);
  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: argument 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. int i;
  135. lua_state = lua_newstate("stack", 1024, "builtin", 1, NULL);
  136. lua_userinit();
  137. if (argc < 2) { /* no arguments? */
  138. if (isatty(0)) {
  139. printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
  140. manual_input(1);
  141. }
  142. else
  143. ldo(lua_dofile, NULL); /* executes stdin as a file */
  144. }
  145. else for (i=1; i<argc; i++) {
  146. if (argv[i][0] == '-') { /* option? */
  147. switch (argv[i][1]) {
  148. case 0:
  149. ldo(lua_dofile, NULL); /* executes stdin as a file */
  150. break;
  151. case 'i':
  152. manual_input(1);
  153. break;
  154. case 'q':
  155. manual_input(0);
  156. break;
  157. case 'd':
  158. lua_setdebug(lua_state, 1);
  159. break;
  160. case 'v':
  161. printf("%s %s\n(written by %s)\n",
  162. LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
  163. break;
  164. case 'e':
  165. i++;
  166. if (i>=argc) {
  167. print_message();
  168. exit(1);
  169. }
  170. if (ldo(lua_dostring, argv[i]) != 0) {
  171. fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
  172. exit(1);
  173. }
  174. break;
  175. case 'f':
  176. i++;
  177. if (i>=argc) {
  178. print_message();
  179. exit(1);
  180. }
  181. getargs(argc-i, argv+i); /* collect following arguments */
  182. file_input(argv, i);
  183. return 0; /* stop running arguments */
  184. case '-':
  185. i = argc; /* end loop */
  186. break;
  187. default:
  188. print_message();
  189. exit(1);
  190. }
  191. }
  192. else if (strchr(argv[i], '='))
  193. assign(argv[i]);
  194. else
  195. file_input(argv, i);
  196. }
  197. #ifdef DEBUG
  198. lua_close();
  199. #endif
  200. return 0;
  201. }