pager.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "cache.h"
  2. #include "run-command.h"
  3. #include "sigchain.h"
  4. /*
  5. * This is split up from the rest of git so that we can do
  6. * something different on Windows.
  7. */
  8. static int spawned_pager;
  9. static void pager_preexec(void)
  10. {
  11. /*
  12. * Work around bug in "less" by not starting it until we
  13. * have real input
  14. */
  15. fd_set in;
  16. FD_ZERO(&in);
  17. FD_SET(0, &in);
  18. select(1, &in, NULL, &in, NULL);
  19. setenv("LESS", "FRSX", 0);
  20. }
  21. static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
  22. static struct child_process pager_process;
  23. static void wait_for_pager(void)
  24. {
  25. fflush(stdout);
  26. fflush(stderr);
  27. /* signal EOF to pager */
  28. close(1);
  29. close(2);
  30. finish_command(&pager_process);
  31. }
  32. static void wait_for_pager_signal(int signo)
  33. {
  34. wait_for_pager();
  35. sigchain_pop(signo);
  36. raise(signo);
  37. }
  38. void setup_pager(void)
  39. {
  40. const char *pager = getenv("PERF_PAGER");
  41. if (!isatty(1))
  42. return;
  43. if (!pager)
  44. pager = getenv("PAGER");
  45. if (!(pager || access("/usr/bin/pager", X_OK)))
  46. pager = "/usr/bin/pager";
  47. if (!(pager || access("/usr/bin/less", X_OK)))
  48. pager = "/usr/bin/less";
  49. if (!pager)
  50. pager = "cat";
  51. if (!*pager || !strcmp(pager, "cat"))
  52. return;
  53. spawned_pager = 1; /* means we are emitting to terminal */
  54. /* spawn the pager */
  55. pager_argv[2] = pager;
  56. pager_process.argv = pager_argv;
  57. pager_process.in = -1;
  58. pager_process.preexec_cb = pager_preexec;
  59. if (start_command(&pager_process))
  60. return;
  61. /* original process continues, but writes to the pipe */
  62. dup2(pager_process.in, 1);
  63. if (isatty(2))
  64. dup2(pager_process.in, 2);
  65. close(pager_process.in);
  66. /* this makes sure that the parent terminates after the pager */
  67. sigchain_push_common(wait_for_pager_signal);
  68. atexit(wait_for_pager);
  69. }
  70. int pager_in_use(void)
  71. {
  72. const char *env;
  73. if (spawned_pager)
  74. return 1;
  75. env = getenv("PERF_PAGER_IN_USE");
  76. return env ? perf_config_bool("PERF_PAGER_IN_USE", env) : 0;
  77. }