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[] = { 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[0] = pager;
85 pager_process.use_shell = 1;
86 pager_process.argv = pager_argv;
87 pager_process.in = -1;
88 if (!getenv("LESS")) {
89 static const char *env[] = { "LESS=FRSX", NULL };
90 pager_process.env = env;
93 pager_process.preexec_cb = pager_preexec;
95 if (start_command(&pager_process))
98 /* original process continues, but writes to the pipe */
99 dup2(pager_process.in, 1);
101 dup2(pager_process.in, 2);
102 close(pager_process.in);
104 /* this makes sure that the parent terminates after the pager */
105 sigchain_push_common(wait_for_pager_signal);
106 atexit(wait_for_pager);
109 int pager_in_use(void)
116 env = getenv("GIT_PAGER_IN_USE");
117 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;