lua.c 2.7 KB

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