pager.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 void pager_preexec(void)
  23. {
  24. /*
  25. * Work around bug in "less" by not starting it until we
  26. * have real input
  27. */
  28. fd_set in;
  29. fd_set exception;
  30. FD_ZERO(&in);
  31. FD_ZERO(&exception);
  32. FD_SET(0, &in);
  33. FD_SET(0, &exception);
  34. select(1, &in, NULL, &exception, NULL);
  35. setenv("LESS", "FRSX", 0);
  36. }
  37. static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
  38. static struct child_process pager_process;
  39. static void wait_for_pager(void)
  40. {
  41. fflush(stdout);
  42. fflush(stderr);
  43. /* signal EOF to pager */
  44. close(1);
  45. close(2);
  46. finish_command(&pager_process);
  47. }
  48. static void wait_for_pager_signal(int signo)
  49. {
  50. wait_for_pager();
  51. sigchain_pop(signo);
  52. raise(signo);
  53. }
  54. void setup_pager(void)
  55. {
  56. const char *pager = getenv(subcmd_config.pager_env);
  57. struct winsize sz;
  58. if (!isatty(1))
  59. return;
  60. if (ioctl(1, TIOCGWINSZ, &sz) == 0)
  61. pager_columns = sz.ws_col;
  62. if (!pager)
  63. pager = getenv("PAGER");
  64. if (!(pager || access("/usr/bin/pager", X_OK)))
  65. pager = "/usr/bin/pager";
  66. if (!(pager || access("/usr/bin/less", X_OK)))
  67. pager = "/usr/bin/less";
  68. if (!pager)
  69. pager = "cat";
  70. if (!*pager || !strcmp(pager, "cat"))
  71. return;
  72. spawned_pager = 1; /* means we are emitting to terminal */
  73. /* spawn the pager */
  74. pager_argv[2] = pager;
  75. pager_process.argv = pager_argv;
  76. pager_process.in = -1;
  77. pager_process.preexec_cb = pager_preexec;
  78. if (start_command(&pager_process))
  79. return;
  80. /* original process continues, but writes to the pipe */
  81. dup2(pager_process.in, 1);
  82. if (isatty(2))
  83. dup2(pager_process.in, 2);
  84. close(pager_process.in);
  85. /* this makes sure that the parent terminates after the pager */
  86. sigchain_push_common(wait_for_pager_signal);
  87. atexit(wait_for_pager);
  88. }
  89. int pager_in_use(void)
  90. {
  91. return spawned_pager;
  92. }
  93. int pager_get_columns(void)
  94. {
  95. char *s;
  96. s = getenv("COLUMNS");
  97. if (s)
  98. return atoi(s);
  99. return (pager_columns ? pager_columns : 80) - 2;
  100. }