lua.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. ** $Id: lua.c,v 1.20 1999/06/24 19:42:02 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)(char *), 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. " - executes stdin as a file\n"
  53. " a=b sets global `a' with string `b'\n"
  54. " name dofile `name'\n\n");
  55. }
  56. static void assign (char *arg) {
  57. if (strlen(arg) >= 500)
  58. fprintf(stderr, "lua: shell argument too long");
  59. else {
  60. char buffer[500];
  61. char *eq = strchr(arg, '=');
  62. lua_pushstring(eq+1);
  63. strncpy(buffer, arg, eq-arg);
  64. buffer[eq-arg] = 0;
  65. lua_setglobal(buffer);
  66. }
  67. }
  68. static void manual_input (int prompt) {
  69. int cont = 1;
  70. while (cont) {
  71. char buffer[BUFSIZ];
  72. int i = 0;
  73. lua_beginblock();
  74. if (prompt)
  75. printf("%s", lua_getstring(lua_getglobal("_PROMPT")));
  76. for(;;) {
  77. int c = getchar();
  78. if (c == EOF) {
  79. cont = 0;
  80. break;
  81. }
  82. else if (c == '\n') {
  83. if (i>0 && buffer[i-1] == '\\')
  84. buffer[i-1] = '\n';
  85. else break;
  86. }
  87. else if (i >= BUFSIZ-1) {
  88. fprintf(stderr, "lua: argument line too long\n");
  89. break;
  90. }
  91. else buffer[i++] = (char)c;
  92. }
  93. buffer[i] = '\0';
  94. ldo(lua_dostring, buffer);
  95. lua_endblock();
  96. }
  97. printf("\n");
  98. }
  99. int main (int argc, char *argv[])
  100. {
  101. int i;
  102. lua_open();
  103. lua_pushstring("> "); lua_setglobal("_PROMPT");
  104. lua_userinit();
  105. if (argc < 2) { /* no arguments? */
  106. if (isatty(0)) {
  107. printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
  108. manual_input(1);
  109. }
  110. else
  111. ldo(lua_dofile, NULL); /* executes stdin as a file */
  112. }
  113. else for (i=1; i<argc; i++) {
  114. if (argv[i][0] == '-') { /* option? */
  115. switch (argv[i][1]) {
  116. case 0:
  117. ldo(lua_dofile, NULL); /* executes stdin as a file */
  118. break;
  119. case 'i':
  120. manual_input(1);
  121. break;
  122. case 'q':
  123. manual_input(0);
  124. break;
  125. case 'd':
  126. lua_setdebug(1);
  127. break;
  128. case 'v':
  129. printf("%s %s\n(written by %s)\n\n",
  130. LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
  131. break;
  132. case 'e':
  133. i++;
  134. if (ldo(lua_dostring, argv[i]) != 0) {
  135. fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
  136. return 1;
  137. }
  138. break;
  139. default:
  140. print_message();
  141. exit(1);
  142. }
  143. }
  144. else if (strchr(argv[i], '='))
  145. assign(argv[i]);
  146. else {
  147. int result = ldo(lua_dofile, argv[i]);
  148. if (result) {
  149. if (result == 2) {
  150. fprintf(stderr, "lua: cannot execute file ");
  151. perror(argv[i]);
  152. }
  153. exit(1);
  154. }
  155. }
  156. }
  157. #ifdef DEBUG
  158. lua_close();
  159. #endif
  160. return 0;
  161. }