lua.c 1.5 KB

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