lua.c 3.9 KB

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