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