call.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #ifdef __MINGW32__
  5. #include <process.h>
  6. #else
  7. #include <sys/wait.h>
  8. #endif
  9. #include "globals.h"
  10. #include "local_proto.h"
  11. #include <grass/display.h>
  12. /*
  13. * call a subroutine, but as a child process
  14. * allowing interrupts for the child
  15. */
  16. #include <signal.h>
  17. int call(int (*function) (), char *msg)
  18. {
  19. int pid;
  20. int w, status;
  21. char i_msg[80];
  22. /*
  23. * build interrupt msg
  24. */
  25. sprintf(i_msg, "Hit %s %s\n", G_unctrl(interrupt_char), msg);
  26. /*
  27. * make sure all graphics have gotten to the monitor
  28. */
  29. R_stabilize();
  30. /* fork to create child */
  31. pid = fork();
  32. if (pid < 0) {
  33. End_curses();
  34. perror("Can't fork");
  35. exit(1);
  36. }
  37. /* parent just waits for child */
  38. Curses_allow_interrupts(1);
  39. if (pid) {
  40. Curses_write_window(PROMPT_WINDOW, 1, 1, i_msg);
  41. while ((w = wait(&status)) != pid && w != -1) ;
  42. Curses_allow_interrupts(0);
  43. Curses_write_window(PROMPT_WINDOW, 1, 1, "\n");
  44. }
  45. /* child turns on interrupts and calls the function */
  46. else {
  47. signal(SIGINT, SIG_DFL);
  48. (*function) ();
  49. exit(0);
  50. }
  51. return 0;
  52. }