lua.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. ** lua.c
  3. ** Linguagem para Usuarios de Aplicacao
  4. */
  5. char *rcs_lua="$Id: lua.c,v 1.2 1994/11/28 17:12:49 roberto Exp celes $";
  6. #include <stdio.h>
  7. #include "lua.h"
  8. #include "lualib.h"
  9. static int lua_argc;
  10. static char **lua_argv;
  11. /*
  12. %F Allow Lua code to access argv strings.
  13. %i Receive from Lua the argument number (starting with 1).
  14. %o Return to Lua the argument, or nil if it does not exist.
  15. */
  16. static void lua_getargv (void)
  17. {
  18. lua_Object lo = lua_getparam(1);
  19. if (!lua_isnumber(lo))
  20. lua_pushnil();
  21. else
  22. {
  23. int n = (int)lua_getnumber(lo);
  24. if (n < 1 || n > lua_argc) lua_pushnil();
  25. else lua_pushstring(lua_argv[n]);
  26. }
  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. lua_register("argv", lua_getargv);
  36. if (argc < 2)
  37. {
  38. char buffer[250];
  39. while (gets(buffer) != 0)
  40. result = lua_dostring(buffer);
  41. }
  42. else
  43. {
  44. for (i=1; i<argc; i++)
  45. {
  46. if (strcmp(argv[i], "--") == 0)
  47. {
  48. lua_argc = argc-i-1;
  49. lua_argv = argv+i;
  50. break;
  51. }
  52. }
  53. for (i=1; i<argc; i++)
  54. {
  55. if (strcmp(argv[i], "--") == 0)
  56. break;
  57. else
  58. result = lua_dofile (argv[i]);
  59. }
  60. }
  61. return result;
  62. }