lua.c 1.2 KB

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