Git 2.17
[git/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"
f67b45f8 5
a3d023d0
JH
6#ifndef DEFAULT_PAGER
7#define DEFAULT_PAGER "less"
8#endif
9
d3180279 10static struct child_process pager_process = CHILD_PROCESS_INIT;
c0c08897 11static const char *pager_program;
ea27a18c 12
507d7804 13static void wait_for_pager(int in_signal)
bfdd9ffd 14{
507d7804
TI
15 if (!in_signal) {
16 fflush(stdout);
17 fflush(stderr);
18 }
bfdd9ffd
JS
19 /* signal EOF to pager */
20 close(1);
21 close(2);
507d7804
TI
22 if (in_signal)
23 finish_command_in_signal(&pager_process);
24 else
25 finish_command(&pager_process);
26}
27
28static void wait_for_pager_atexit(void)
29{
30 wait_for_pager(0);
bfdd9ffd 31}
f67b45f8 32
a3da8821
JK
33static void wait_for_pager_signal(int signo)
34{
507d7804 35 wait_for_pager(1);
a3da8821
JK
36 sigchain_pop(signo);
37 raise(signo);
38}
39
4babb839
JK
40static int core_pager_config(const char *var, const char *value, void *data)
41{
42 if (!strcmp(var, "core.pager"))
43 return git_config_string(&pager_program, var, value);
44 return 0;
45}
46
64778d24 47const char *git_pager(int stdout_is_tty)
f67b45f8 48{
63618245 49 const char *pager;
f67b45f8 50
64778d24 51 if (!stdout_is_tty)
63618245
JN
52 return NULL;
53
54 pager = getenv("GIT_PAGER");
cad3a205
JH
55 if (!pager) {
56 if (!pager_program)
eed27072 57 read_early_config(core_pager_config, NULL);
54adf370 58 pager = pager_program;
cad3a205 59 }
c27d205a
ML
60 if (!pager)
61 pager = getenv("PAGER");
402461aa 62 if (!pager)
a3d023d0 63 pager = DEFAULT_PAGER;
ed016612 64 if (!*pager || !strcmp(pager, "cat"))
63618245
JN
65 pager = NULL;
66
67 return pager;
68}
69
995bc22d
EW
70static void setup_pager_env(struct argv_array *env)
71{
72 const char **argv;
73 int i;
74 char *pager_env = xstrdup(PAGER_ENV);
75 int n = split_cmdline(pager_env, &argv);
76
77 if (n < 0)
78 die("malformed build-time PAGER_ENV: %s",
79 split_cmdline_strerror(n));
80
81 for (i = 0; i < n; i++) {
82 char *cp = strchr(argv[i], '=');
83
84 if (!cp)
85 die("malformed build-time PAGER_ENV");
86
87 *cp = '\0';
88 if (!getenv(argv[i])) {
89 *cp = '=';
90 argv_array_push(env, argv[i]);
91 }
92 }
93 free(pager_env);
94 free(argv);
95}
96
3e3a4a41
JH
97void prepare_pager_args(struct child_process *pager_process, const char *pager)
98{
99 argv_array_push(&pager_process->args, pager);
100 pager_process->use_shell = 1;
995bc22d 101 setup_pager_env(&pager_process->env_array);
3e3a4a41
JH
102}
103
63618245
JN
104void setup_pager(void)
105{
64778d24 106 const char *pager = git_pager(isatty(1));
63618245 107
c0459ca4 108 if (!pager)
402461aa
JS
109 return;
110
ad6c3739
ZJS
111 /*
112 * force computing the width of the terminal before we redirect
113 * the standard output to the pager.
114 */
115 (void) term_columns();
116
2e6c012e 117 setenv("GIT_PAGER_IN_USE", "true", 1);
85fb65ed 118
bfdd9ffd 119 /* spawn the pager */
3e3a4a41 120 prepare_pager_args(&pager_process, pager);
ea27a18c 121 pager_process.in = -1;
124b5190 122 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
bfdd9ffd
JS
123 if (start_command(&pager_process))
124 return;
125
126 /* original process continues, but writes to the pipe */
127 dup2(pager_process.in, 1);
a8335024
JH
128 if (isatty(2))
129 dup2(pager_process.in, 2);
bfdd9ffd
JS
130 close(pager_process.in);
131
132 /* this makes sure that the parent terminates after the pager */
a3da8821 133 sigchain_push_common(wait_for_pager_signal);
507d7804 134 atexit(wait_for_pager_atexit);
f67b45f8 135}
6e9af863
JK
136
137int pager_in_use(void)
138{
df2a6e38 139 return git_env_bool("GIT_PAGER_IN_USE", 0);
6e9af863 140}
ad6c3739
ZJS
141
142/*
143 * Return cached value (if set) or $COLUMNS environment variable (if
144 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
145 * and default to 80 if all else fails.
146 */
147int term_columns(void)
148{
149 static int term_columns_at_startup;
150
151 char *col_string;
152 int n_cols;
153
154 if (term_columns_at_startup)
155 return term_columns_at_startup;
156
157 term_columns_at_startup = 80;
158
159 col_string = getenv("COLUMNS");
160 if (col_string && (n_cols = atoi(col_string)) > 0)
161 term_columns_at_startup = n_cols;
162#ifdef TIOCGWINSZ
163 else {
164 struct winsize ws;
165 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
166 term_columns_at_startup = ws.ws_col;
167 }
168#endif
169
170 return term_columns_at_startup;
171}
4d9e079e 172
ec7ff5ba
ZJS
173/*
174 * How many columns do we need to show this number in decimal?
175 */
d306f3d3 176int decimal_width(uintmax_t number)
ec7ff5ba 177{
d306f3d3 178 int width;
ec7ff5ba 179
d306f3d3
JK
180 for (width = 1; number >= 10; width++)
181 number /= 10;
ec7ff5ba
ZJS
182 return width;
183}
4914c962 184
6a1e1bc0
JK
185struct pager_command_config_data {
186 const char *cmd;
187 int want;
188 char *value;
189};
190
191static int pager_command_config(const char *var, const char *value, void *vdata)
4914c962 192{
6a1e1bc0
JK
193 struct pager_command_config_data *data = vdata;
194 const char *cmd;
195
196 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
89576613 197 int b = git_parse_maybe_bool(value);
4914c962 198 if (b >= 0)
6a1e1bc0 199 data->want = b;
4914c962 200 else {
6a1e1bc0
JK
201 data->want = 1;
202 data->value = xstrdup(value);
4914c962
NTND
203 }
204 }
6a1e1bc0
JK
205
206 return 0;
207}
208
209/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
210int check_pager_config(const char *cmd)
211{
212 struct pager_command_config_data data;
213
214 data.cmd = cmd;
215 data.want = -1;
216 data.value = NULL;
217
eed27072 218 read_early_config(pager_command_config, &data);
6a1e1bc0
JK
219
220 if (data.value)
221 pager_program = data.value;
222 return data.want;
4914c962 223}