lua.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. ** $Id: lua.c,v 1.26 1999/11/16 12:50:48 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. int i, j;
  72. lua_Object args = lua_createtable();
  73. lua_pushobject(args);
  74. lua_setglobal("arg");
  75. for (i=0; i<argc; i++)
  76. if (strcmp(argv[i], "--") == 0) break;
  77. for (j = 0; j<argc; j++) {
  78. /* arg[j-i] = argv[j] */
  79. lua_pushobject(args); lua_pushnumber(j-i);
  80. lua_pushstring(argv[j]); lua_settable();
  81. }
  82. /* arg.n = maximum index in table `arg' */
  83. lua_pushobject(args); lua_pushstring("n");
  84. lua_pushnumber(argc-(i+1)); lua_settable();
  85. /* arg.nn = minimum index in table `arg' */
  86. lua_pushobject(args); lua_pushstring("nn");
  87. lua_pushnumber(-i); lua_settable();
  88. }
  89. static void manual_input (int prompt) {
  90. int cont = 1;
  91. while (cont) {
  92. char buffer[BUFSIZ];
  93. int i = 0;
  94. lua_beginblock();
  95. if (prompt)
  96. printf("%s", lua_getstring(lua_getglobal("_PROMPT")));
  97. for(;;) {
  98. int c = getchar();
  99. if (c == EOF) {
  100. cont = 0;
  101. break;
  102. }
  103. else if (c == '\n') {
  104. if (i>0 && buffer[i-1] == '\\')
  105. buffer[i-1] = '\n';
  106. else break;
  107. }
  108. else if (i >= BUFSIZ-1) {
  109. fprintf(stderr, "lua: argument line too long\n");
  110. break;
  111. }
  112. else buffer[i++] = (char)c;
  113. }
  114. buffer[i] = '\0';
  115. ldo(lua_dostring, buffer);
  116. lua_endblock();
  117. }
  118. printf("\n");
  119. }
  120. int main (int argc, char *argv[]) {
  121. int i;
  122. lua_open();
  123. lua_pushstring("> "); lua_setglobal("_PROMPT");
  124. lua_userinit();
  125. getargs(argc, argv);
  126. if (argc < 2) { /* no arguments? */
  127. if (isatty(0)) {
  128. printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
  129. manual_input(1);
  130. }
  131. else
  132. ldo(lua_dofile, NULL); /* executes stdin as a file */
  133. }
  134. else for (i=1; i<argc; i++) {
  135. if (argv[i][0] == '-') { /* option? */
  136. switch (argv[i][1]) {
  137. case 0:
  138. ldo(lua_dofile, NULL); /* executes stdin as a file */
  139. break;
  140. case 'i':
  141. manual_input(1);
  142. break;
  143. case 'q':
  144. manual_input(0);
  145. break;
  146. case 'd':
  147. lua_setdebug(lua_state, 1);
  148. break;
  149. case 'v':
  150. printf("%s %s\n(written by %s)\n",
  151. LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
  152. break;
  153. case 'e':
  154. i++;
  155. if (ldo(lua_dostring, argv[i]) != 0) {
  156. fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
  157. return 1;
  158. }
  159. break;
  160. case '-':
  161. i = argc; /* end loop */
  162. break;
  163. default:
  164. print_message();
  165. exit(1);
  166. }
  167. }
  168. else if (strchr(argv[i], '='))
  169. assign(argv[i]);
  170. else {
  171. int result = ldo(lua_dofile, argv[i]);
  172. if (result) {
  173. if (result == 2) {
  174. fprintf(stderr, "lua: cannot execute file ");
  175. perror(argv[i]);
  176. }
  177. exit(1);
  178. }
  179. }
  180. }
  181. #ifdef DEBUG
  182. lua_close();
  183. #endif
  184. return 0;
  185. }