lua.c 4.0 KB

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