lua.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. ** lua.c
  3. ** Linguagem para Usuarios de Aplicacao
  4. */
  5. char *rcs_lua="$Id: lua.c,v 1.10 1996/05/03 19:20:17 roberto Exp roberto $";
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "lua.h"
  9. #include "lualib.h"
  10. #ifdef POSIX
  11. #include <unistd.h>
  12. #else
  13. #define isatty(x) (x==0) /* assume stdin is a tty */
  14. #endif
  15. static void manual_input (void)
  16. {
  17. if (isatty(0))
  18. {
  19. char buffer[250];
  20. while (fgets(buffer, sizeof(buffer), stdin) != 0)
  21. lua_dostring(buffer);
  22. }
  23. else
  24. lua_dofile(NULL); /* executes stdin as a file */
  25. }
  26. int main (int argc, char *argv[])
  27. {
  28. int i;
  29. int result = 0;
  30. iolib_open ();
  31. strlib_open ();
  32. mathlib_open ();
  33. if (argc < 2)
  34. manual_input();
  35. else for (i=1; i<argc; i++) {
  36. if (strcmp(argv[i], "-") == 0)
  37. manual_input();
  38. else if (strcmp(argv[i], "-v") == 0)
  39. printf("%s %s\n(written by %s)\n\n",
  40. LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
  41. else if ((strcmp(argv[i], "-e") == 0 && i++) || strchr(argv[i], '=')) {
  42. if (lua_dostring(argv[i]) != 0) {
  43. fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
  44. return 1;
  45. }
  46. }
  47. else {
  48. result = lua_dofile (argv[i]);
  49. if (result) {
  50. if (result == 2) {
  51. fprintf(stderr, "lua: cannot execute file `%s' - ", argv[i]);
  52. perror(NULL);
  53. }
  54. return 1;
  55. }
  56. }
  57. }
  58. return result;
  59. }