lua.c 1.4 KB

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