]>
Commit | Line | Data |
---|---|---|
f67b45f8 | 1 | #include "cache.h" |
ea27a18c | 2 | #include "run-command.h" |
a3da8821 | 3 | #include "sigchain.h" |
f67b45f8 | 4 | |
a3d023d0 JH |
5 | #ifndef DEFAULT_PAGER |
6 | #define DEFAULT_PAGER "less" | |
7 | #endif | |
8 | ||
f67b45f8 | 9 | /* |
bfdd9ffd JS |
10 | * This is split up from the rest of git so that we can do |
11 | * something different on Windows. | |
f67b45f8 LT |
12 | */ |
13 | ||
6e9af863 JK |
14 | static int spawned_pager; |
15 | ||
71064e3f | 16 | #ifndef WIN32 |
ea27a18c | 17 | static void pager_preexec(void) |
f67b45f8 | 18 | { |
35ce8622 LT |
19 | /* |
20 | * Work around bug in "less" by not starting it until we | |
21 | * have real input | |
22 | */ | |
23 | fd_set in; | |
24 | ||
25 | FD_ZERO(&in); | |
26 | FD_SET(0, &in); | |
27 | select(1, &in, NULL, &in, NULL); | |
f67b45f8 | 28 | } |
ea27a18c | 29 | #endif |
bfdd9ffd | 30 | |
ac0ba18d | 31 | static const char *pager_argv[] = { NULL, NULL }; |
ea27a18c JK |
32 | static struct child_process pager_process; |
33 | ||
bfdd9ffd JS |
34 | static void wait_for_pager(void) |
35 | { | |
36 | fflush(stdout); | |
37 | fflush(stderr); | |
38 | /* signal EOF to pager */ | |
39 | close(1); | |
40 | close(2); | |
41 | finish_command(&pager_process); | |
42 | } | |
f67b45f8 | 43 | |
a3da8821 JK |
44 | static void wait_for_pager_signal(int signo) |
45 | { | |
46 | wait_for_pager(); | |
47 | sigchain_pop(signo); | |
48 | raise(signo); | |
49 | } | |
50 | ||
64778d24 | 51 | const char *git_pager(int stdout_is_tty) |
f67b45f8 | 52 | { |
63618245 | 53 | const char *pager; |
f67b45f8 | 54 | |
64778d24 | 55 | if (!stdout_is_tty) |
63618245 JN |
56 | return NULL; |
57 | ||
58 | pager = getenv("GIT_PAGER"); | |
cad3a205 JH |
59 | if (!pager) { |
60 | if (!pager_program) | |
ef90d6d4 | 61 | git_config(git_default_config, NULL); |
54adf370 | 62 | pager = pager_program; |
cad3a205 | 63 | } |
c27d205a ML |
64 | if (!pager) |
65 | pager = getenv("PAGER"); | |
402461aa | 66 | if (!pager) |
a3d023d0 | 67 | pager = DEFAULT_PAGER; |
caef71a5 | 68 | else if (!*pager || !strcmp(pager, "cat")) |
63618245 JN |
69 | pager = NULL; |
70 | ||
71 | return pager; | |
72 | } | |
73 | ||
74 | void setup_pager(void) | |
75 | { | |
64778d24 | 76 | const char *pager = git_pager(isatty(1)); |
63618245 JN |
77 | |
78 | if (!pager) | |
402461aa JS |
79 | return; |
80 | ||
6e9af863 | 81 | spawned_pager = 1; /* means we are emitting to terminal */ |
85fb65ed | 82 | |
bfdd9ffd | 83 | /* spawn the pager */ |
ac0ba18d JK |
84 | pager_argv[0] = pager; |
85 | pager_process.use_shell = 1; | |
ea27a18c JK |
86 | pager_process.argv = pager_argv; |
87 | pager_process.in = -1; | |
25fc1786 JS |
88 | if (!getenv("LESS")) { |
89 | static const char *env[] = { "LESS=FRSX", NULL }; | |
90 | pager_process.env = env; | |
91 | } | |
71064e3f | 92 | #ifndef WIN32 |
ea27a18c JK |
93 | pager_process.preexec_cb = pager_preexec; |
94 | #endif | |
bfdd9ffd JS |
95 | if (start_command(&pager_process)) |
96 | return; | |
97 | ||
98 | /* original process continues, but writes to the pipe */ | |
99 | dup2(pager_process.in, 1); | |
a8335024 JH |
100 | if (isatty(2)) |
101 | dup2(pager_process.in, 2); | |
bfdd9ffd JS |
102 | close(pager_process.in); |
103 | ||
104 | /* this makes sure that the parent terminates after the pager */ | |
a3da8821 | 105 | sigchain_push_common(wait_for_pager_signal); |
bfdd9ffd | 106 | atexit(wait_for_pager); |
f67b45f8 | 107 | } |
6e9af863 JK |
108 | |
109 | int pager_in_use(void) | |
110 | { | |
111 | const char *env; | |
112 | ||
113 | if (spawned_pager) | |
114 | return 1; | |
115 | ||
116 | env = getenv("GIT_PAGER_IN_USE"); | |
117 | return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0; | |
118 | } |