revoke.c 1.9 KB

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