lua.c 4.8 KB

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