lua.c 3.9 KB

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