lua.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. ** $Id: lua.c,v 1.2 1997/10/06 14:51:32 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 manual_input (void)
  22. {
  23. if (isatty(0)) {
  24. char buffer[250];
  25. while (1) {
  26. lua_beginblock();
  27. printf("%s", lua_getstring(lua_getglobal("_PROMPT")));
  28. if (fgets(buffer, sizeof(buffer), stdin) == 0)
  29. break;
  30. lua_dostring(buffer);
  31. lua_endblock();
  32. }
  33. printf("\n");
  34. }
  35. else
  36. lua_dofile(NULL); /* executes stdin as a file */
  37. }
  38. int main (int argc, char *argv[])
  39. {
  40. int i;
  41. setlocale(LC_ALL, "");
  42. lua_iolibopen();
  43. lua_strlibopen();
  44. lua_mathlibopen();
  45. lua_pushstring("> "); lua_setglobal("_PROMPT");
  46. if (argc < 2) {
  47. printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
  48. manual_input();
  49. }
  50. else for (i=1; i<argc; i++) {
  51. if (strcmp(argv[i], "-") == 0)
  52. manual_input();
  53. else if (strcmp(argv[i], "-d") == 0)
  54. lua_debug = 1;
  55. else if (strcmp(argv[i], "-v") == 0)
  56. printf("%s %s\n(written by %s)\n\n",
  57. LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
  58. else if ((strcmp(argv[i], "-e") == 0 && i++) || strchr(argv[i], '=')) {
  59. if (lua_dostring(argv[i]) != 0) {
  60. fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
  61. return 1;
  62. }
  63. }
  64. else {
  65. int result = lua_dofile (argv[i]);
  66. if (result) {
  67. if (result == 2) {
  68. fprintf(stderr, "lua: cannot execute file ");
  69. perror(argv[i]);
  70. }
  71. return 1;
  72. }
  73. }
  74. }
  75. return 0;
  76. }