]> git.ipfire.org Git - thirdparty/git.git/blame - pager.c
prune: quiet ENOENT on missing directories
[thirdparty/git.git] / pager.c
CommitLineData
f67b45f8 1#include "cache.h"
b2141fc1 2#include "config.h"
ea27a18c 3#include "run-command.h"
a3da8821 4#include "sigchain.h"
65b5f948 5#include "alias.h"
f67b45f8 6
a3d023d0
JH
7#ifndef DEFAULT_PAGER
8#define DEFAULT_PAGER "less"
9#endif
10
f917f57f 11static struct child_process pager_process;
c0c08897 12static const char *pager_program;
ea27a18c 13
9b6e2c8b
JS
14/* Is the value coming back from term_columns() just a guess? */
15static int term_columns_guessed;
16
17
61ff12fa 18static void close_pager_fds(void)
bfdd9ffd 19{
bfdd9ffd
JS
20 /* signal EOF to pager */
21 close(1);
22 close(2);
507d7804
TI
23}
24
25static void wait_for_pager_atexit(void)
26{
61ff12fa
ÆAB
27 fflush(stdout);
28 fflush(stderr);
29 close_pager_fds();
30 finish_command(&pager_process);
bfdd9ffd 31}
f67b45f8 32
a3da8821
JK
33static void wait_for_pager_signal(int signo)
34{
61ff12fa
ÆAB
35 close_pager_fds();
36 finish_command_in_signal(&pager_process);
a3da8821
JK
37 sigchain_pop(signo);
38 raise(signo);
39}
40
783a86c1 41static int core_pager_config(const char *var, const char *value,
5cf88fd8 42 void *data UNUSED)
4babb839
JK
43{
44 if (!strcmp(var, "core.pager"))
45 return git_config_string(&pager_program, var, value);
46 return 0;
47}
48
64778d24 49const 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)
eed27072 59 read_early_config(core_pager_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;
ed016612 66 if (!*pager || !strcmp(pager, "cat"))
63618245
JN
67 pager = NULL;
68
69 return pager;
70}
71
c972bf4c 72static void setup_pager_env(struct strvec *env)
995bc22d
EW
73{
74 const char **argv;
75 int i;
76 char *pager_env = xstrdup(PAGER_ENV);
77 int n = split_cmdline(pager_env, &argv);
78
79 if (n < 0)
80 die("malformed build-time PAGER_ENV: %s",
81 split_cmdline_strerror(n));
82
83 for (i = 0; i < n; i++) {
84 char *cp = strchr(argv[i], '=');
85
86 if (!cp)
87 die("malformed build-time PAGER_ENV");
88
89 *cp = '\0';
90 if (!getenv(argv[i])) {
91 *cp = '=';
c972bf4c 92 strvec_push(env, argv[i]);
995bc22d
EW
93 }
94 }
95 free(pager_env);
96 free(argv);
97}
98
3e3a4a41
JH
99void prepare_pager_args(struct child_process *pager_process, const char *pager)
100{
c972bf4c 101 strvec_push(&pager_process->args, pager);
3e3a4a41 102 pager_process->use_shell = 1;
29fda24d 103 setup_pager_env(&pager_process->env);
eee73d1d 104 pager_process->trace2_child_class = "pager";
3e3a4a41
JH
105}
106
63618245
JN
107void setup_pager(void)
108{
64778d24 109 const char *pager = git_pager(isatty(1));
63618245 110
c0459ca4 111 if (!pager)
402461aa
JS
112 return;
113
ad6c3739 114 /*
be11f7ad
JK
115 * After we redirect standard output, we won't be able to use an ioctl
116 * to get the terminal size. Let's grab it now, and then set $COLUMNS
117 * to communicate it to any sub-processes.
ad6c3739 118 */
be11f7ad
JK
119 {
120 char buf[64];
121 xsnprintf(buf, sizeof(buf), "%d", term_columns());
9b6e2c8b
JS
122 if (!term_columns_guessed)
123 setenv("COLUMNS", buf, 0);
be11f7ad 124 }
ad6c3739 125
2e6c012e 126 setenv("GIT_PAGER_IN_USE", "true", 1);
85fb65ed 127
f917f57f
EM
128 child_process_init(&pager_process);
129
bfdd9ffd 130 /* spawn the pager */
3e3a4a41 131 prepare_pager_args(&pager_process, pager);
ea27a18c 132 pager_process.in = -1;
29fda24d 133 strvec_push(&pager_process.env, "GIT_PAGER_IN_USE");
bfdd9ffd
JS
134 if (start_command(&pager_process))
135 return;
136
137 /* original process continues, but writes to the pipe */
138 dup2(pager_process.in, 1);
a8335024
JH
139 if (isatty(2))
140 dup2(pager_process.in, 2);
bfdd9ffd
JS
141 close(pager_process.in);
142
143 /* this makes sure that the parent terminates after the pager */
a3da8821 144 sigchain_push_common(wait_for_pager_signal);
507d7804 145 atexit(wait_for_pager_atexit);
f67b45f8 146}
6e9af863
JK
147
148int pager_in_use(void)
149{
df2a6e38 150 return git_env_bool("GIT_PAGER_IN_USE", 0);
6e9af863 151}
ad6c3739
ZJS
152
153/*
154 * Return cached value (if set) or $COLUMNS environment variable (if
155 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
156 * and default to 80 if all else fails.
157 */
158int term_columns(void)
159{
160 static int term_columns_at_startup;
161
162 char *col_string;
163 int n_cols;
164
165 if (term_columns_at_startup)
166 return term_columns_at_startup;
167
168 term_columns_at_startup = 80;
9b6e2c8b 169 term_columns_guessed = 1;
ad6c3739
ZJS
170
171 col_string = getenv("COLUMNS");
9b6e2c8b 172 if (col_string && (n_cols = atoi(col_string)) > 0) {
ad6c3739 173 term_columns_at_startup = n_cols;
9b6e2c8b
JS
174 term_columns_guessed = 0;
175 }
ad6c3739
ZJS
176#ifdef TIOCGWINSZ
177 else {
178 struct winsize ws;
9b6e2c8b 179 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col) {
ad6c3739 180 term_columns_at_startup = ws.ws_col;
9b6e2c8b
JS
181 term_columns_guessed = 0;
182 }
ad6c3739
ZJS
183 }
184#endif
185
186 return term_columns_at_startup;
187}
4d9e079e 188
cd1096b2
SG
189/*
190 * Clear the entire line, leave cursor in first column.
191 */
192void term_clear_line(void)
193{
194 if (is_terminal_dumb())
195 /*
196 * Fall back to print a terminal width worth of space
197 * characters (hoping that the terminal is still as wide
198 * as it was upon the first call to term_columns()).
199 */
200 fprintf(stderr, "\r%*s\r", term_columns(), "");
201 else
202 /*
203 * On non-dumb terminals use an escape sequence to clear
204 * the whole line, no matter how wide the terminal.
205 */
206 fputs("\r\033[K", stderr);
207}
208
ec7ff5ba
ZJS
209/*
210 * How many columns do we need to show this number in decimal?
211 */
d306f3d3 212int decimal_width(uintmax_t number)
ec7ff5ba 213{
d306f3d3 214 int width;
ec7ff5ba 215
d306f3d3
JK
216 for (width = 1; number >= 10; width++)
217 number /= 10;
ec7ff5ba
ZJS
218 return width;
219}
4914c962 220
6a1e1bc0
JK
221struct pager_command_config_data {
222 const char *cmd;
223 int want;
224 char *value;
225};
226
227static int pager_command_config(const char *var, const char *value, void *vdata)
4914c962 228{
6a1e1bc0
JK
229 struct pager_command_config_data *data = vdata;
230 const char *cmd;
231
232 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
89576613 233 int b = git_parse_maybe_bool(value);
4914c962 234 if (b >= 0)
6a1e1bc0 235 data->want = b;
4914c962 236 else {
6a1e1bc0
JK
237 data->want = 1;
238 data->value = xstrdup(value);
4914c962
NTND
239 }
240 }
6a1e1bc0
JK
241
242 return 0;
243}
244
245/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
246int check_pager_config(const char *cmd)
247{
248 struct pager_command_config_data data;
249
250 data.cmd = cmd;
251 data.want = -1;
252 data.value = NULL;
253
eed27072 254 read_early_config(pager_command_config, &data);
6a1e1bc0
JK
255
256 if (data.value)
257 pager_program = data.value;
258 return data.want;
4914c962 259}