revoke.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <stdlib.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include "proto.h"
  6. #include <grass/glocale.h>
  7. int reinvoke_script(const struct context *ctx, const char *filename)
  8. {
  9. struct Option *option;
  10. struct Flag *flag;
  11. /* Because shell from MINGW and CygWin converts all variables
  12. * to uppercase it was necessary to use uppercase variables.
  13. * Set both until all scripts are updated */
  14. for (flag = ctx->first_flag; flag; flag = flag->next_flag) {
  15. char buff[16];
  16. sprintf(buff, "GIS_FLAG_%c=%d", flag->key, flag->answer ? 1 : 0);
  17. putenv(G_store(buff));
  18. sprintf(buff, "GIS_FLAG_%c=%d", toupper(flag->key),
  19. flag->answer ? 1 : 0);
  20. G_debug(2, "set %s", buff);
  21. putenv(G_store(buff));
  22. }
  23. for (option = ctx->first_option; option; option = option->next_opt) {
  24. char upper[4096];
  25. char *str;
  26. G_asprintf(&str, "GIS_OPT_%s=%s", option->key,
  27. option->answer ? option->answer : "");
  28. putenv(str);
  29. strcpy(upper, option->key);
  30. G_str_to_upper(upper);
  31. G_asprintf(&str, "GIS_OPT_%s=%s", upper,
  32. option->answer ? option->answer : "");
  33. G_debug(2, "set %s", str);
  34. putenv(str);
  35. }
  36. #ifdef __MINGW32__
  37. {
  38. /* execlp() and _spawnlp ( _P_OVERLAY,..) do not work, they return
  39. * immediately and that breaks scripts running GRASS scripts
  40. * because they dont wait until GRASS script finished */
  41. /* execlp( "sh", "sh", filename, "@ARGS_PARSED@", NULL); */
  42. /* _spawnlp ( _P_OVERLAY, filename, filename, "@ARGS_PARSED@", NULL ); */
  43. int ret;
  44. char *shell = getenv("GRASS_SH");
  45. if (shell == NULL)
  46. shell = "sh";
  47. ret = G_spawn(shell, shell, filename, "@ARGS_PARSED@", NULL);
  48. G_debug(1, "ret = %d", ret);
  49. if (ret == -1) {
  50. perror(_("G_spawn() failed"));
  51. return EXIT_FAILURE;
  52. }
  53. return EXIT_SUCCESS;
  54. }
  55. #else
  56. execl(filename, filename, "@ARGS_PARSED@", NULL);
  57. perror(_("execl() failed"));
  58. return EXIT_FAILURE;
  59. #endif
  60. }