lua.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. ** $Id: lua.c,v 1.9 1997/12/11 17:00:21 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. /* command line options:
  22. ** -v print version information (banner).
  23. ** -d debug on
  24. ** -e dostring on next arg
  25. ** a=b sets global 'a' with string 'b'
  26. ** -q interactive mode without prompt
  27. ** -i interactive mode with prompt
  28. ** - executes stdin as a file
  29. ** name dofile "name"
  30. */
  31. static void assign (char *arg)
  32. {
  33. if (strlen(arg) >= 500)
  34. fprintf(stderr, "lua: shell argument too long");
  35. else {
  36. char buffer[500];
  37. char *eq = strchr(arg, '=');
  38. lua_pushstring(eq+1);
  39. strncpy(buffer, arg, eq-arg);
  40. buffer[eq-arg] = 0;
  41. lua_setglobal(buffer);
  42. }
  43. }
  44. #define BUF_SIZE 512
  45. static void manual_input (int prompt)
  46. {
  47. int cont = 1;
  48. while (cont) {
  49. char buffer[BUF_SIZE];
  50. int i = 0;
  51. lua_beginblock();
  52. if (prompt)
  53. printf("%s", lua_getstring(lua_getglobal("_PROMPT")));
  54. for(;;) {
  55. int c = getchar();
  56. if (c == EOF) {
  57. cont = 0;
  58. break;
  59. }
  60. else if (c == '\n') {
  61. if (i>0 && buffer[i-1] == '\\')
  62. buffer[i-1] = '\n';
  63. else break;
  64. }
  65. else if (i >= BUF_SIZE-1) {
  66. fprintf(stderr, "lua: argument line too long\n");
  67. break;
  68. }
  69. else buffer[i++] = c;
  70. }
  71. buffer[i] = 0;
  72. lua_dostring(buffer);
  73. lua_endblock();
  74. }
  75. printf("\n");
  76. }
  77. int main (int argc, char *argv[])
  78. {
  79. int i;
  80. setlocale(LC_ALL, "");
  81. lua_iolibopen();
  82. lua_strlibopen();
  83. lua_mathlibopen();
  84. lua_pushstring("> "); lua_setglobal("_PROMPT");
  85. if (argc < 2) { /* no arguments? */
  86. if (isatty(0)) {
  87. printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
  88. manual_input(1);
  89. }
  90. else
  91. lua_dofile(NULL); /* executes stdin as a file */
  92. }
  93. else for (i=1; i<argc; i++) {
  94. if (strcmp(argv[i], "-") == 0)
  95. lua_dofile(NULL); /* executes stdin as a file */
  96. else if (strcmp(argv[i], "-i") == 0)
  97. manual_input(1);
  98. else if (strcmp(argv[i], "-q") == 0)
  99. manual_input(0);
  100. else if (strcmp(argv[i], "-d") == 0)
  101. lua_debug = 1;
  102. else if (strcmp(argv[i], "-v") == 0)
  103. printf("%s %s\n(written by %s)\n\n",
  104. LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
  105. else if (strcmp(argv[i], "-e") == 0) {
  106. i++;
  107. if (lua_dostring(argv[i]) != 0) {
  108. fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
  109. return 1;
  110. }
  111. }
  112. else if (strchr(argv[i], '='))
  113. assign(argv[i]);
  114. else {
  115. int result = lua_dofile(argv[i]);
  116. if (result) {
  117. if (result == 2) {
  118. fprintf(stderr, "lua: cannot execute file ");
  119. perror(argv[i]);
  120. }
  121. return 1;
  122. }
  123. }
  124. }
  125. #ifdef DEBUG
  126. lua_close();
  127. #endif
  128. return 0;
  129. }