Merge branch 'work/revert-gitk' into devel
[4msysgit-hv.git] / pager.c
blob77e9dd6ae4c563c9eab5cf3ccba478ae9ebf3f8e
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 static void pager_preexec(void)
15 * Work around bug in "less" by not starting it until we
16 * have real input
18 fd_set in;
20 FD_ZERO(&in);
21 FD_SET(0, &in);
22 select(1, &in, NULL, &in, NULL);
24 setenv("LESS", "FRSX", 0);
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 void setup_pager(void)
49 const char *pager = getenv("GIT_PAGER");
51 if (!isatty(1))
52 return;
53 if (!pager) {
54 if (!pager_program)
55 git_config(git_default_config, NULL);
56 pager = pager_program;
58 if (!pager)
59 pager = getenv("PAGER");
60 if (!pager)
61 pager = "less";
62 else if (!*pager || !strcmp(pager, "cat"))
63 return;
65 spawned_pager = 1; /* means we are emitting to terminal */
67 /* spawn the pager */
68 pager_argv[2] = pager;
69 pager_process.preexec_cb = pager_preexec;
70 pager_process.argv = &pager_argv[2];
71 pager_process.in = -1;
72 if (start_command(&pager_process)) {
73 pager_process.argv = pager_argv;
74 pager_process.in = -1;
75 if (start_command(&pager_process))
76 return;
79 /* original process continues, but writes to the pipe */
80 dup2(pager_process.in, 1);
81 if (isatty(2))
82 dup2(pager_process.in, 2);
83 close(pager_process.in);
85 /* this makes sure that the parent terminates after the pager */
86 sigchain_push_common(wait_for_pager_signal);
87 atexit(wait_for_pager);
90 int pager_in_use(void)
92 const char *env;
94 if (spawned_pager)
95 return 1;
97 env = getenv("GIT_PAGER_IN_USE");
98 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;