lua.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. ** lua.c
  3. ** Linguagem para Usuarios de Aplicacao
  4. */
  5. char *rcs_lua="$Id: lua.c,v 1.4 1995/02/07 16:04:15 lhf 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. %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. static void manual_input (void)
  30. {
  31. char buffer[250];
  32. while (gets(buffer) != 0)
  33. lua_dostring(buffer);
  34. }
  35. int main (int argc, char *argv[])
  36. {
  37. int i;
  38. int result = 0;
  39. iolib_open ();
  40. strlib_open ();
  41. mathlib_open ();
  42. lua_register("argv", lua_getargv);
  43. if (argc < 2)
  44. manual_input();
  45. else
  46. {
  47. for (i=1; i<argc; i++)
  48. if (strcmp(argv[i], "--") == 0)
  49. {
  50. lua_argc = argc-i-1;
  51. lua_argv = argv+i;
  52. break;
  53. }
  54. for (i=1; i<argc; i++)
  55. {
  56. if (strcmp(argv[i], "--") == 0)
  57. break;
  58. else if (strcmp(argv[i], "-") == 0)
  59. manual_input();
  60. else if (strcmp(argv[i], "-v") == 0)
  61. printf("%s %s\n\n", LUA_VERSION, LUA_COPYRIGHT);
  62. else
  63. result = lua_dofile (argv[i]);
  64. }
  65. }
  66. return result;
  67. }