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.
15 static void pager_preexec(void)
18 * Work around bug in "less" by not starting it until we
25 select(1, &in, NULL, &in, NULL);
29 static const char *pager_argv[] = { NULL, NULL };
30 static struct child_process pager_process;
32 static void wait_for_pager(void)
36 /* signal EOF to pager */
39 finish_command(&pager_process);
42 static void wait_for_pager_signal(int signo)
49 const char *git_pager(int stdout_is_tty)
56 pager = getenv("GIT_PAGER");
59 git_config(git_default_config, NULL);
60 pager = pager_program;
63 pager = getenv("PAGER");
65 pager = DEFAULT_PAGER;
66 else if (!*pager || !strcmp(pager, "cat"))
72 void setup_pager(void)
74 const char *pager = git_pager(isatty(1));
79 setenv("GIT_PAGER_IN_USE", "true", 1);
82 pager_argv[0] = pager;
83 pager_process.use_shell = 1;
84 pager_process.argv = pager_argv;
85 pager_process.in = -1;
86 if (!getenv("LESS")) {
87 static const char *env[] = { "LESS=FRSX", NULL };
88 pager_process.env = env;
91 pager_process.preexec_cb = pager_preexec;
93 if (start_command(&pager_process))
96 /* original process continues, but writes to the pipe */
97 dup2(pager_process.in, 1);
99 dup2(pager_process.in, 2);
100 close(pager_process.in);
102 /* this makes sure that the parent terminates after the pager */
103 sigchain_push_common(wait_for_pager_signal);
104 atexit(wait_for_pager);
107 int pager_in_use(void)
110 env = getenv("GIT_PAGER_IN_USE");
111 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;