pager.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. if (!pager_program)
  45. perf_config(perf_default_config, NULL);
  46. pager = pager_program;
  47. }
  48. if (!pager)
  49. pager = getenv("PAGER");
  50. if (!pager)
  51. pager = "less";
  52. else if (!*pager || !strcmp(pager, "cat"))
  53. return;
  54. spawned_pager = 1; /* means we are emitting to terminal */
  55. /* spawn the pager */
  56. pager_argv[2] = pager;
  57. pager_process.argv = pager_argv;
  58. pager_process.in = -1;
  59. pager_process.preexec_cb = pager_preexec;
  60. if (start_command(&pager_process))
  61. return;
  62. /* original process continues, but writes to the pipe */
  63. dup2(pager_process.in, 1);
  64. if (isatty(2))
  65. dup2(pager_process.in, 2);
  66. close(pager_process.in);
  67. /* this makes sure that the parent terminates after the pager */
  68. sigchain_push_common(wait_for_pager_signal);
  69. atexit(wait_for_pager);
  70. }
  71. int pager_in_use(void)
  72. {
  73. const char *env;
  74. if (spawned_pager)
  75. return 1;
  76. env = getenv("PERF_PAGER_IN_USE");
  77. return env ? perf_config_bool("PERF_PAGER_IN_USE", env) : 0;
  78. }