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