lua.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. ** lua.c
  3. ** Linguagem para Usuarios de Aplicacao
  4. */
  5. char *rcs_lua="$Id: lua.c,v 1.7 1995/10/31 17:05:35 roberto Exp roberto $";
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "lua.h"
  9. #include "lualib.h"
  10. static int lua_argc;
  11. static char **lua_argv;
  12. /*
  13. ** although this function is POSIX, there is no standard header file that
  14. ** defines it
  15. */
  16. int isatty (int fd);
  17. /*
  18. %F Allow Lua code to access argv strings.
  19. %i Receive from Lua the argument number (starting with 1).
  20. %o Return to Lua the argument, or nil if it does not exist.
  21. */
  22. static void lua_getargv (void)
  23. {
  24. lua_Object lo = lua_getparam(1);
  25. if (!lua_isnumber(lo))
  26. lua_pushnil();
  27. else
  28. {
  29. int n = (int)lua_getnumber(lo);
  30. if (n < 1 || n > lua_argc) lua_pushnil();
  31. else lua_pushstring(lua_argv[n]);
  32. }
  33. }
  34. static void manual_input (void)
  35. {
  36. if (isatty(fileno(stdin)))
  37. {
  38. char buffer[250];
  39. while (fgets(buffer, sizeof(buffer), stdin) != 0)
  40. lua_dostring(buffer);
  41. }
  42. else
  43. lua_dofile(NULL); /* executes stdin as a file */
  44. }
  45. int main (int argc, char *argv[])
  46. {
  47. int i;
  48. int result = 0;
  49. iolib_open ();
  50. strlib_open ();
  51. mathlib_open ();
  52. lua_register("argv", lua_getargv);
  53. if (argc < 2)
  54. manual_input();
  55. else
  56. {
  57. for (i=1; i<argc; i++)
  58. if (strcmp(argv[i], "--") == 0)
  59. {
  60. lua_argc = argc-i-1;
  61. lua_argv = argv+i;
  62. break;
  63. }
  64. for (i=1; i<argc; i++)
  65. {
  66. if (strcmp(argv[i], "--") == 0)
  67. break;
  68. else if (strcmp(argv[i], "-") == 0)
  69. manual_input();
  70. else if (strcmp(argv[i], "-v") == 0)
  71. printf("%s %s\n(written by %s)\n\n",
  72. LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
  73. else
  74. result = lua_dofile (argv[i]);
  75. }
  76. }
  77. return result;
  78. }