Teach git var about GIT_PAGER
[git/gitweb.git] / pager.c
blob0b63d99fe770153bdfcc99d23ef45befc14d45b0
1 #include "cache.h"
2 #include "run-command.h"
3 #include "sigchain.h"
5 /*
6 * This is split up from the rest of git so that we can do
7 * something different on Windows.
8 */
10 static int spawned_pager;
12 #ifndef WIN32
13 static void pager_preexec(void)
16 * Work around bug in "less" by not starting it until we
17 * have real input
19 fd_set in;
21 FD_ZERO(&in);
22 FD_SET(0, &in);
23 select(1, &in, NULL, &in, NULL);
25 #endif
27 static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
28 static struct child_process pager_process;
30 static void wait_for_pager(void)
32 fflush(stdout);
33 fflush(stderr);
34 /* signal EOF to pager */
35 close(1);
36 close(2);
37 finish_command(&pager_process);
40 static void wait_for_pager_signal(int signo)
42 wait_for_pager();
43 sigchain_pop(signo);
44 raise(signo);
47 const char *git_pager(void)
49 const char *pager;
51 if (!isatty(1))
52 return NULL;
54 pager = getenv("GIT_PAGER");
55 if (!pager) {
56 if (!pager_program)
57 git_config(git_default_config, NULL);
58 pager = pager_program;
60 if (!pager)
61 pager = getenv("PAGER");
62 if (!pager)
63 pager = "less";
64 else if (!*pager || !strcmp(pager, "cat"))
65 pager = NULL;
67 return pager;
70 void setup_pager(void)
72 const char *pager = git_pager();
74 if (!pager)
75 return;
77 spawned_pager = 1; /* means we are emitting to terminal */
79 /* spawn the pager */
80 pager_argv[2] = pager;
81 pager_process.argv = pager_argv;
82 pager_process.in = -1;
83 if (!getenv("LESS")) {
84 static const char *env[] = { "LESS=FRSX", NULL };
85 pager_process.env = env;
87 #ifndef WIN32
88 pager_process.preexec_cb = pager_preexec;
89 #endif
90 if (start_command(&pager_process))
91 return;
93 /* original process continues, but writes to the pipe */
94 dup2(pager_process.in, 1);
95 if (isatty(2))
96 dup2(pager_process.in, 2);
97 close(pager_process.in);
99 /* this makes sure that the parent terminates after the pager */
100 sigchain_push_common(wait_for_pager_signal);
101 atexit(wait_for_pager);
104 int pager_in_use(void)
106 const char *env;
108 if (spawned_pager)
109 return 1;
111 env = getenv("GIT_PAGER_IN_USE");
112 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;