lua.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. ** $Id: lua.c,v 1.30 1999/12/21 17:34:23 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. "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. " -v print version information\n"
  58. " a=b set global `a' to string `b'\n"
  59. " name execute file `name'\n"
  60. );
  61. }
  62. static void print_version (void) {
  63. printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
  64. }
  65. static void assign (char *arg) {
  66. if (strlen(arg) >= 500)
  67. fprintf(stderr, "lua: shell argument too long");
  68. else {
  69. char buffer[500];
  70. char *eq = strchr(arg, '=');
  71. lua_pushstring(eq+1);
  72. strncpy(buffer, arg, eq-arg);
  73. buffer[eq-arg] = 0;
  74. lua_setglobal(buffer);
  75. }
  76. }
  77. static void getargs (int argc, char *argv[]) {
  78. lua_beginblock(); {
  79. int i;
  80. lua_Object args = lua_createtable();
  81. lua_pushobject(args);
  82. lua_setglobal("arg");
  83. for (i=0; i<argc; i++) {
  84. /* arg[i] = argv[i] */
  85. lua_pushobject(args); lua_pushnumber(i);
  86. lua_pushstring(argv[i]); lua_settable();
  87. }
  88. /* arg.n = maximum index in table `arg' */
  89. lua_pushobject(args); lua_pushstring("n");
  90. lua_pushnumber(argc-1); lua_settable();
  91. } lua_endblock();
  92. }
  93. static void file_input (char **argv, int arg) {
  94. int result = ldo(lua_dofile, argv[arg]);
  95. if (result) {
  96. if (result == 2) {
  97. fprintf(stderr, "lua: cannot execute file ");
  98. perror(argv[arg]);
  99. }
  100. exit(1);
  101. }
  102. }
  103. static void manual_input (int prompt) {
  104. int cont = 1;
  105. while (cont) {
  106. char buffer[BUFSIZ];
  107. int i = 0;
  108. lua_beginblock();
  109. if (prompt) {
  110. const char *s = lua_getstring(lua_getglobal("_PROMPT"));
  111. if (!s) s = PROMPT;
  112. printf("%s", s);
  113. }
  114. for(;;) {
  115. int c = getchar();
  116. if (c == EOF) {
  117. cont = 0;
  118. break;
  119. }
  120. else if (c == '\n') {
  121. if (i>0 && buffer[i-1] == '\\')
  122. buffer[i-1] = '\n';
  123. else break;
  124. }
  125. else if (i >= BUFSIZ-1) {
  126. fprintf(stderr, "lua: argument line too long\n");
  127. break;
  128. }
  129. else buffer[i++] = (char)c;
  130. }
  131. buffer[i] = '\0';
  132. ldo(lua_dostring, buffer);
  133. lua_endblock();
  134. }
  135. printf("\n");
  136. }
  137. int main (int argc, char *argv[]) {
  138. int i;
  139. lua_state = lua_newstate("stack", 1024, "builtin", 1, NULL);
  140. lua_userinit();
  141. if (argc < 2) { /* no arguments? */
  142. if (isatty(0)) {
  143. print_version();
  144. manual_input(1);
  145. }
  146. else
  147. ldo(lua_dofile, NULL); /* executes stdin as a file */
  148. }
  149. else for (i=1; i<argc; i++) {
  150. if (argv[i][0] == '-') { /* option? */
  151. switch (argv[i][1]) {
  152. case 0:
  153. ldo(lua_dofile, NULL); /* executes stdin as a file */
  154. break;
  155. case 'i':
  156. manual_input(1);
  157. break;
  158. case 'q':
  159. manual_input(0);
  160. break;
  161. case 'd':
  162. lua_setdebug(lua_state, 1);
  163. if (i==argc-1) { /* last argument? */
  164. print_version();
  165. manual_input(1);
  166. }
  167. break;
  168. case 'v':
  169. print_version();
  170. break;
  171. case 'e':
  172. i++;
  173. if (i>=argc) {
  174. print_message();
  175. exit(1);
  176. }
  177. if (ldo(lua_dostring, argv[i]) != 0) {
  178. fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
  179. exit(1);
  180. }
  181. break;
  182. case 'f':
  183. i++;
  184. if (i>=argc) {
  185. print_message();
  186. exit(1);
  187. }
  188. getargs(argc-i, argv+i); /* collect following arguments */
  189. file_input(argv, i);
  190. i = argc; /* stop running arguments */
  191. break;
  192. default:
  193. print_message();
  194. exit(1);
  195. }
  196. }
  197. else if (strchr(argv[i], '='))
  198. assign(argv[i]);
  199. else
  200. file_input(argv, i);
  201. }
  202. #ifdef DEBUG
  203. lua_close();
  204. #endif
  205. return 0;
  206. }