2 #include "run-command.h"
6 #define DEFAULT_PAGER "less"
10 * This is split up from the rest of git so that we can do
11 * something different on Windows.
14 static int spawned_pager;
17 static void pager_preexec(void)
20 * Work around bug in "less" by not starting it until we
27 select(1, &in, NULL, &in, NULL);
31 static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
32 static struct child_process pager_process;
34 static void wait_for_pager(void)
38 /* signal EOF to pager */
41 finish_command(&pager_process);
44 static void wait_for_pager_signal(int signo)
51 const char *git_pager(void)
58 pager = getenv("GIT_PAGER");
61 git_config(git_default_config, NULL);
62 pager = pager_program;
65 pager = getenv("PAGER");
67 pager = DEFAULT_PAGER;
68 else if (!*pager || !strcmp(pager, "cat"))
74 void setup_pager(void)
76 const char *pager = git_pager();
81 spawned_pager = 1; /* means we are emitting to terminal */
84 pager_argv[2] = pager;
85 pager_process.argv = pager_argv;
86 pager_process.in = -1;
87 if (!getenv("LESS")) {
88 static const char *env[] = { "LESS=FRSX", NULL };
89 pager_process.env = env;
92 pager_process.preexec_cb = pager_preexec;
94 if (start_command(&pager_process))
97 /* original process continues, but writes to the pipe */
98 dup2(pager_process.in, 1);
100 dup2(pager_process.in, 2);
101 close(pager_process.in);
103 /* this makes sure that the parent terminates after the pager */
104 sigchain_push_common(wait_for_pager_signal);
105 atexit(wait_for_pager);
108 int pager_in_use(void)
115 env = getenv("GIT_PAGER_IN_USE");
116 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;