pager.c 2.2 KB

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