pager.c 2.5 KB

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