Raw
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "editor.h"
4 #include "pager.h"
5 #include "run-command.h"
6 #include "sigchain.h"
7 #include "alias.h"
8
9 int pager_use_color = 1;
10
11 #ifndef DEFAULT_PAGER
12 #define DEFAULT_PAGER "less"
13 #endif
14
15 static struct child_process pager_process;
16 static char *pager_program;
17 static int old_fd1 = -1, old_fd2 = -1;
18
19 /* Is the value coming back from term_columns() just a guess? */
20 static int term_columns_guessed;
21
22
23 static void close_pager_fds(void)
24 {
25 /* signal EOF to pager */
26 close(1);
27 if (old_fd2 != -1)
28 close(2);
29 }
30
31 static void finish_pager(void)
32 {
33 fflush(stdout);
34 fflush(stderr);
35 close_pager_fds();
36 finish_command(&pager_process);
37 }
38
39 static void wait_for_pager_atexit(void)
40 {
41 if (old_fd1 == -1)
42 return;
43
44 finish_pager();
45 }
46
47 void wait_for_pager(void)
48 {
49 if (old_fd1 == -1)
50 return;
51
52 finish_pager();
53 sigchain_pop_common();
54 unsetenv("GIT_PAGER_IN_USE");
55 dup2(old_fd1, 1);
56 close(old_fd1);
57 old_fd1 = -1;
58 if (old_fd2 != -1) {
59 dup2(old_fd2, 2);
60 close(old_fd2);
61 old_fd2 = -1;
62 }
63 }
64
65 static void wait_for_pager_signal(int signo)
66 {
67 if (old_fd1 == -1)
68 return;
69
70 close_pager_fds();
71 finish_command_in_signal(&pager_process);
72 sigchain_pop(signo);
73 raise(signo);
74 }
75
76 static int core_pager_config(const char *var, const char *value,
77 const struct config_context *ctx UNUSED,
78 void *data UNUSED)
79 {
80 if (!strcmp(var, "core.pager"))
81 return git_config_string(&pager_program, var, value);
82 return 0;
83 }
84
85 const char *git_pager(struct repository *r, int stdout_is_tty)
86 {
87 const char *pager;
88
89 if (!stdout_is_tty)
90 return NULL;
91
92 pager = getenv("GIT_PAGER");
93 if (!pager) {
94 if (!pager_program)
95 read_early_config(r,
96 core_pager_config, NULL);
97 pager = pager_program;
98 }
99 if (!pager)
100 pager = getenv("PAGER");
101 if (!pager)
102 pager = DEFAULT_PAGER;
103 if (!*pager || !strcmp(pager, "cat"))
104 pager = NULL;
105
106 return pager;
107 }
108
109 static void setup_pager_env(struct strvec *env)
110 {
111 char **argv;
112 int i;
113 char *pager_env = xstrdup(PAGER_ENV);
114 /* split_cmdline splits in place, so we know the result is writable */
115 int n = split_cmdline(pager_env, (const char ***)&argv);
116
117 if (n < 0)
118 die("malformed build-time PAGER_ENV: %s",
119 split_cmdline_strerror(n));
120
121 for (i = 0; i < n; i++) {
122 char *cp = strchr(argv[i], '=');
123
124 if (!cp)
125 die("malformed build-time PAGER_ENV");
126
127 *cp = '\0';
128 if (!getenv(argv[i])) {
129 *cp = '=';
130 strvec_push(env, argv[i]);
131 }
132 }
133 free(pager_env);
134 free(argv);
135 }
136
137 void prepare_pager_args(struct child_process *pager_process, const char *pager)
138 {
139 strvec_push(&pager_process->args, pager);
140 pager_process->use_shell = 1;
141 setup_pager_env(&pager_process->env);
142 pager_process->trace2_child_class = "pager";
143 }
144
145 void setup_pager(struct repository *r)
146 {
147 static int once = 0;
148 const char *pager = git_pager(r, isatty(1));
149
150 if (!pager)
151 return;
152
153 /*
154 * After we redirect standard output, we won't be able to use an ioctl
155 * to get the terminal size. Let's grab it now, and then set $COLUMNS
156 * to communicate it to any sub-processes.
157 */
158 {
159 char buf[64];
160 xsnprintf(buf, sizeof(buf), "%d", term_columns());
161 if (!term_columns_guessed)
162 setenv("COLUMNS", buf, 0);
163 }
164
165 setenv("GIT_PAGER_IN_USE", "true", 1);
166
167 child_process_init(&pager_process);
168
169 /* spawn the pager */
170 prepare_pager_args(&pager_process, pager);
171 pager_process.in = -1;
172 strvec_push(&pager_process.env, "GIT_PAGER_IN_USE");
173 if (start_command(&pager_process))
174 die("unable to execute pager '%s'", pager);
175
176 /* original process continues, but writes to the pipe */
177 old_fd1 = dup(1);
178 dup2(pager_process.in, 1);
179 if (isatty(2)) {
180 old_fd2 = dup(2);
181 dup2(pager_process.in, 2);
182 }
183 close(pager_process.in);
184
185 sigchain_push_common(wait_for_pager_signal);
186
187 if (!once) {
188 once++;
189 atexit(wait_for_pager_atexit);
190 }
191 }
192
193 int pager_in_use(void)
194 {
195 return git_env_bool("GIT_PAGER_IN_USE", 0);
196 }
197
198 /*
199 * Return cached value (if set) or $COLUMNS environment variable (if
200 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
201 * and default to 80 if all else fails.
202 */
203 int term_columns(void)
204 {
205 static int term_columns_at_startup;
206
207 char *col_string;
208 int n_cols;
209
210 if (term_columns_at_startup)
211 return term_columns_at_startup;
212
213 term_columns_at_startup = 80;
214 term_columns_guessed = 1;
215
216 col_string = getenv("COLUMNS");
217 if (col_string && (n_cols = atoi(col_string)) > 0) {
218 term_columns_at_startup = n_cols;
219 term_columns_guessed = 0;
220 }
221 #ifdef TIOCGWINSZ
222 else {
223 struct winsize ws;
224 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col) {
225 term_columns_at_startup = ws.ws_col;
226 term_columns_guessed = 0;
227 }
228 }
229 #endif
230
231 return term_columns_at_startup;
232 }
233
234 /*
235 * Clear the entire line, leave cursor in first column.
236 */
237 void term_clear_line(void)
238 {
239 if (!isatty(2))
240 return;
241 if (is_terminal_dumb())
242 /*
243 * Fall back to print a terminal width worth of space
244 * characters (hoping that the terminal is still as wide
245 * as it was upon the first call to term_columns()).
246 */
247 fprintf(stderr, "\r%*s\r", term_columns(), "");
248 else
249 /*
250 * On non-dumb terminals use an escape sequence to clear
251 * the whole line, no matter how wide the terminal.
252 */
253 fputs("\r\033[K", stderr);
254 }
255
256 /*
257 * How many columns do we need to show this number in decimal?
258 */
259 int decimal_width(uintmax_t number)
260 {
261 int width;
262
263 for (width = 1; number >= 10; width++)
264 number /= 10;
265 return width;
266 }
267
268 struct pager_command_config_data {
269 const char *cmd;
270 int want;
271 char *value;
272 };
273
274 static int pager_command_config(const char *var, const char *value,
275 const struct config_context *ctx UNUSED,
276 void *vdata)
277 {
278 struct pager_command_config_data *data = vdata;
279 const char *cmd;
280
281 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
282 int b = git_parse_maybe_bool(value);
283 if (b >= 0)
284 data->want = b;
285 else {
286 data->want = 1;
287 data->value = xstrdup(value);
288 }
289 }
290
291 return 0;
292 }
293
294 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
295 int check_pager_config(struct repository *r, const char *cmd)
296 {
297 struct pager_command_config_data data;
298
299 data.cmd = cmd;
300 data.want = -1;
301 data.value = NULL;
302
303 read_early_config(r, pager_command_config, &data);
304
305 if (data.value)
306 pager_program = data.value;
307 return data.want;
308 }