lua.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. ** $Id: lua.c,v 1.27 1999/11/22 13:12:07 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. typedef void (*handler)(int); /* type for signal actions */
  19. static void laction (int i);
  20. static lua_LHFunction old_linehook = NULL;
  21. static lua_CHFunction old_callhook = NULL;
  22. static handler lreset (void) {
  23. return signal(SIGINT, laction);
  24. }
  25. static void lstop (void) {
  26. lua_setlinehook(lua_state, old_linehook);
  27. lua_setcallhook(lua_state, old_callhook);
  28. lreset();
  29. lua_error("interrupted!");
  30. }
  31. static void laction (int i) {
  32. (void)i; /* to avoid warnings */
  33. signal(SIGINT, SIG_DFL); /* if another SIGINT happens before lstop,
  34. terminate process (default action) */
  35. old_linehook = lua_setlinehook(lua_state, (lua_LHFunction)lstop);
  36. old_callhook = lua_setcallhook(lua_state, (lua_CHFunction)lstop);
  37. }
  38. static int ldo (int (*f)(lua_State *L, const char *), const char *name) {
  39. int res;
  40. handler h = lreset();
  41. res = f(lua_state, name); /* dostring | dofile */
  42. signal(SIGINT, h); /* restore old action */
  43. return res;
  44. }
  45. static void print_message (void) {
  46. fprintf(stderr,
  47. "Lua: command line options:\n"
  48. " -v print version information\n"
  49. " -d turn debug on\n"
  50. " -e stat dostring `stat'\n"
  51. " -q interactive mode without prompt\n"
  52. " -i interactive mode with prompt\n"
  53. " - execute stdin as a file\n"
  54. " -- start arguments for table `arg'\n"
  55. " a=b set global `a' with string `b'\n"
  56. " name dofile `name'\n\n");
  57. }
  58. static void assign (char *arg) {
  59. if (strlen(arg) >= 500)
  60. fprintf(stderr, "lua: shell argument too long");
  61. else {
  62. char buffer[500];
  63. char *eq = strchr(arg, '=');
  64. lua_pushstring(eq+1);
  65. strncpy(buffer, arg, eq-arg);
  66. buffer[eq-arg] = 0;
  67. lua_setglobal(buffer);
  68. }
  69. }
  70. static void getargs (int argc, char *argv[]) {
  71. lua_beginblock(); {
  72. int i, j;
  73. lua_Object args = lua_createtable();
  74. lua_pushobject(args);
  75. lua_setglobal("arg");
  76. for (i=0; i<argc; i++)
  77. if (strcmp(argv[i], "--") == 0) break;
  78. for (j = 0; j<argc; j++) {
  79. /* arg[j-i] = argv[j] */
  80. lua_pushobject(args); lua_pushnumber(j-i);
  81. lua_pushstring(argv[j]); lua_settable();
  82. }
  83. /* arg.n = maximum index in table `arg' */
  84. lua_pushobject(args); lua_pushstring("n");
  85. lua_pushnumber(argc-(i+1)); lua_settable();
  86. /* arg.nn = minimum index in table `arg' */
  87. lua_pushobject(args); lua_pushstring("nn");
  88. lua_pushnumber(-i); lua_settable();
  89. } lua_endblock();
  90. }
  91. static void manual_input (int prompt) {
  92. int cont = 1;
  93. while (cont) {
  94. char buffer[BUFSIZ];
  95. int i = 0;
  96. lua_beginblock();
  97. if (prompt)
  98. printf("%s", lua_getstring(lua_getglobal("_PROMPT")));
  99. for(;;) {
  100. int c = getchar();
  101. if (c == EOF) {
  102. cont = 0;
  103. break;
  104. }
  105. else if (c == '\n') {
  106. if (i>0 && buffer[i-1] == '\\')
  107. buffer[i-1] = '\n';
  108. else break;
  109. }
  110. else if (i >= BUFSIZ-1) {
  111. fprintf(stderr, "lua: argument line too long\n");
  112. break;
  113. }
  114. else buffer[i++] = (char)c;
  115. }
  116. buffer[i] = '\0';
  117. ldo(lua_dostring, buffer);
  118. lua_endblock();
  119. }
  120. printf("\n");
  121. }
  122. int main (int argc, char *argv[]) {
  123. int i;
  124. lua_state = lua_newstate("stack", 1024, "builtin", 1, NULL);
  125. lua_pushstring("> "); lua_setglobal("_PROMPT");
  126. lua_userinit();
  127. getargs(argc, argv);
  128. if (argc < 2) { /* no arguments? */
  129. if (isatty(0)) {
  130. printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
  131. manual_input(1);
  132. }
  133. else
  134. ldo(lua_dofile, NULL); /* executes stdin as a file */
  135. }
  136. else for (i=1; i<argc; i++) {
  137. if (argv[i][0] == '-') { /* option? */
  138. switch (argv[i][1]) {
  139. case 0:
  140. ldo(lua_dofile, NULL); /* executes stdin as a file */
  141. break;
  142. case 'i':
  143. manual_input(1);
  144. break;
  145. case 'q':
  146. manual_input(0);
  147. break;
  148. case 'd':
  149. lua_setdebug(lua_state, 1);
  150. break;
  151. case 'v':
  152. printf("%s %s\n(written by %s)\n",
  153. LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
  154. break;
  155. case 'e':
  156. i++;
  157. if (ldo(lua_dostring, argv[i]) != 0) {
  158. fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
  159. return 1;
  160. }
  161. break;
  162. case '-':
  163. i = argc; /* end loop */
  164. break;
  165. default:
  166. print_message();
  167. exit(1);
  168. }
  169. }
  170. else if (strchr(argv[i], '='))
  171. assign(argv[i]);
  172. else {
  173. int result = ldo(lua_dofile, argv[i]);
  174. if (result) {
  175. if (result == 2) {
  176. fprintf(stderr, "lua: cannot execute file ");
  177. perror(argv[i]);
  178. }
  179. exit(1);
  180. }
  181. }
  182. }
  183. #ifdef DEBUG
  184. lua_close();
  185. #endif
  186. return 0;
  187. }