pager.c 2.3 KB

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