lua.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. ** $Id: lua.c,v 1.16 1999/01/06 13:12:41 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 OLD_ANSI
  14. #include <locale.h>
  15. #else
  16. #define setlocale(a,b) 0
  17. #endif
  18. #ifdef _POSIX_SOURCE
  19. #include <unistd.h>
  20. #else
  21. #define isatty(x) (x==0) /* assume stdin is a tty */
  22. #endif
  23. typedef void (*handler)(int); /* type for signal actions */
  24. static void laction (int i);
  25. static lua_LHFunction old_linehook = NULL;
  26. static lua_CHFunction old_callhook = NULL;
  27. static handler lreset (void) {
  28. return signal(SIGINT, laction);
  29. }
  30. static void lstop (void) {
  31. lua_linehook = old_linehook;
  32. lua_callhook = old_callhook;
  33. lreset();
  34. lua_error("interrupted!");
  35. }
  36. static void laction (int i) {
  37. old_linehook = lua_linehook;
  38. old_callhook = lua_callhook;
  39. lua_linehook = (lua_LHFunction)lstop;
  40. lua_callhook = (lua_CHFunction)lstop;
  41. }
  42. static int ldo (int (*f)(char *), char *name) {
  43. int res;
  44. handler h = lreset();
  45. res = f(name); /* dostring | dofile */
  46. signal(SIGINT, h); /* restore old action */
  47. return res;
  48. }
  49. static void print_message (void) {
  50. fprintf(stderr,
  51. "Lua: command line options:\n"
  52. " -v print version information\n"
  53. " -d turn debug on\n"
  54. " -e stat dostring `stat'\n"
  55. " -q interactive mode without prompt\n"
  56. " -i interactive mode with prompt\n"
  57. " - executes stdin as a file\n"
  58. " a=b sets global `a' with string `b'\n"
  59. " name dofile `name'\n\n");
  60. }
  61. static void assign (char *arg) {
  62. if (strlen(arg) >= 500)
  63. fprintf(stderr, "lua: shell argument too long");
  64. else {
  65. char buffer[500];
  66. char *eq = strchr(arg, '=');
  67. lua_pushstring(eq+1);
  68. strncpy(buffer, arg, eq-arg);
  69. buffer[eq-arg] = 0;
  70. lua_setglobal(buffer);
  71. }
  72. }
  73. static void manual_input (int prompt) {
  74. int cont = 1;
  75. while (cont) {
  76. char buffer[BUFSIZ];
  77. int i = 0;
  78. lua_beginblock();
  79. if (prompt)
  80. printf("%s", lua_getstring(lua_getglobal("_PROMPT")));
  81. for(;;) {
  82. int c = getchar();
  83. if (c == EOF) {
  84. cont = 0;
  85. break;
  86. }
  87. else if (c == '\n') {
  88. if (i>0 && buffer[i-1] == '\\')
  89. buffer[i-1] = '\n';
  90. else break;
  91. }
  92. else if (i >= BUFSIZ-1) {
  93. fprintf(stderr, "lua: argument line too long\n");
  94. break;
  95. }
  96. else buffer[i++] = (char)c;
  97. }
  98. buffer[i] = '\0';
  99. ldo(lua_dostring, buffer);
  100. lua_endblock();
  101. }
  102. printf("\n");
  103. }
  104. int main (int argc, char *argv[])
  105. {
  106. int i;
  107. lua_open();
  108. lua_pushstring("> "); lua_setglobal("_PROMPT");
  109. lua_userinit();
  110. setlocale(LC_ALL, "");
  111. if (argc < 2) { /* no arguments? */
  112. if (isatty(0)) {
  113. printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
  114. manual_input(1);
  115. }
  116. else
  117. ldo(lua_dofile, NULL); /* executes stdin as a file */
  118. }
  119. else for (i=1; i<argc; i++) {
  120. if (argv[i][0] == '-') { /* option? */
  121. switch (argv[i][1]) {
  122. case 0:
  123. ldo(lua_dofile, NULL); /* executes stdin as a file */
  124. break;
  125. case 'i':
  126. manual_input(1);
  127. break;
  128. case 'q':
  129. manual_input(0);
  130. break;
  131. case 'd':
  132. lua_debug = 1;
  133. break;
  134. case 'v':
  135. printf("%s %s\n(written by %s)\n\n",
  136. LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
  137. break;
  138. case 'e':
  139. i++;
  140. if (ldo(lua_dostring, argv[i]) != 0) {
  141. fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
  142. return 1;
  143. }
  144. break;
  145. default:
  146. print_message();
  147. exit(1);
  148. }
  149. }
  150. else if (strchr(argv[i], '='))
  151. assign(argv[i]);
  152. else {
  153. int result = ldo(lua_dofile, argv[i]);
  154. if (result) {
  155. if (result == 2) {
  156. fprintf(stderr, "lua: cannot execute file ");
  157. perror(argv[i]);
  158. }
  159. exit(1);
  160. }
  161. }
  162. }
  163. #ifdef DEBUG
  164. lua_close();
  165. #endif
  166. return 0;
  167. }