lua.c 3.3 KB

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