lua.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. ** $Id: lua.c,v 1.11 1997/12/22 18:05:23 roberto Exp roberto $
  3. ** Lua stand-alone interpreter
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "lua.h"
  9. #include "luadebug.h"
  10. #include "lualib.h"
  11. #ifndef OLD_ANSI
  12. #include <locale.h>
  13. #else
  14. #define setlocale(a,b) 0
  15. #endif
  16. #ifdef _POSIX_SOURCE
  17. #include <unistd.h>
  18. #else
  19. #define isatty(x) (x==0) /* assume stdin is a tty */
  20. #endif
  21. static void print_message (void)
  22. {
  23. fprintf(stderr,
  24. "Lua: command line options:\n"
  25. " -v print version information\n"
  26. " -d turn debug on\n"
  27. " -e stat dostring `stat'\n"
  28. " -q interactive mode without prompt\n"
  29. " -i interactive mode with prompt\n"
  30. " - executes stdin as a file\n"
  31. " a=b sets global `a' with string `b'\n"
  32. " name dofile `name'\n\n");
  33. }
  34. static void assign (char *arg)
  35. {
  36. if (strlen(arg) >= 500)
  37. fprintf(stderr, "lua: shell argument too long");
  38. else {
  39. char buffer[500];
  40. char *eq = strchr(arg, '=');
  41. lua_pushstring(eq+1);
  42. strncpy(buffer, arg, eq-arg);
  43. buffer[eq-arg] = 0;
  44. lua_setglobal(buffer);
  45. }
  46. }
  47. #define BUF_SIZE 512
  48. static void manual_input (int prompt)
  49. {
  50. int cont = 1;
  51. while (cont) {
  52. char buffer[BUF_SIZE];
  53. int i = 0;
  54. lua_beginblock();
  55. if (prompt)
  56. printf("%s", lua_getstring(lua_getglobal("_PROMPT")));
  57. for(;;) {
  58. int c = getchar();
  59. if (c == EOF) {
  60. cont = 0;
  61. break;
  62. }
  63. else if (c == '\n') {
  64. if (i>0 && buffer[i-1] == '\\')
  65. buffer[i-1] = '\n';
  66. else break;
  67. }
  68. else if (i >= BUF_SIZE-1) {
  69. fprintf(stderr, "lua: argument line too long\n");
  70. break;
  71. }
  72. else buffer[i++] = c;
  73. }
  74. buffer[i] = 0;
  75. lua_dostring(buffer);
  76. lua_endblock();
  77. }
  78. printf("\n");
  79. }
  80. int main (int argc, char *argv[])
  81. {
  82. int i;
  83. setlocale(LC_ALL, "");
  84. lua_iolibopen();
  85. lua_strlibopen();
  86. lua_mathlibopen();
  87. lua_pushstring("> "); lua_setglobal("_PROMPT");
  88. if (argc < 2) { /* no arguments? */
  89. if (isatty(0)) {
  90. printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
  91. manual_input(1);
  92. }
  93. else
  94. lua_dofile(NULL); /* executes stdin as a file */
  95. }
  96. else for (i=1; i<argc; i++) {
  97. if (argv[i][0] == '-') { /* option? */
  98. switch (argv[i][1]) {
  99. case 0:
  100. lua_dofile(NULL); /* executes stdin as a file */
  101. break;
  102. case 'i':
  103. manual_input(1);
  104. break;
  105. case 'q':
  106. manual_input(0);
  107. break;
  108. case 'd':
  109. lua_debug = 1;
  110. break;
  111. case 'v':
  112. printf("%s %s\n(written by %s)\n\n",
  113. LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
  114. break;
  115. case 'e':
  116. i++;
  117. if (lua_dostring(argv[i]) != 0) {
  118. fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
  119. return 1;
  120. }
  121. break;
  122. default:
  123. print_message();
  124. exit(1);
  125. }
  126. }
  127. else if (strchr(argv[i], '='))
  128. assign(argv[i]);
  129. else {
  130. int result = lua_dofile(argv[i]);
  131. if (result) {
  132. if (result == 2) {
  133. fprintf(stderr, "lua: cannot execute file ");
  134. perror(argv[i]);
  135. }
  136. exit(1);
  137. }
  138. }
  139. }
  140. #ifdef DEBUG
  141. lua_close();
  142. #endif
  143. return 0;
  144. }