blob: 41446d4f0543df18fb2430188d886ed1975a55d9 [file] [log] [blame]
Linus Torvaldsf67b45f2006-02-28 11:26:21 -08001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Jeff Kingea27a182008-07-22 03:14:12 -04003#include "run-command.h"
Jeff Kinga3da8822009-01-22 01:03:28 -05004#include "sigchain.h"
Nguyễn Thái Ngọc Duy65b5f942018-05-20 20:40:06 +02005#include "alias.h"
Linus Torvaldsf67b45f2006-02-28 11:26:21 -08006
Junio C Hamanoa3d023d2009-10-30 20:45:34 -05007#ifndef DEFAULT_PAGER
8#define DEFAULT_PAGER "less"
9#endif
10
René Scharfed3180272014-08-19 21:09:35 +020011static struct child_process pager_process = CHILD_PROCESS_INIT;
Jeff Kingc0c08892016-09-12 20:23:48 -070012static const char *pager_program;
Jeff Kingea27a182008-07-22 03:14:12 -040013
Takashi Iwai507d7802015-09-04 11:35:57 +020014static void wait_for_pager(int in_signal)
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010015{
Takashi Iwai507d7802015-09-04 11:35:57 +020016 if (!in_signal) {
17 fflush(stdout);
18 fflush(stderr);
19 }
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010020 /* signal EOF to pager */
21 close(1);
22 close(2);
Takashi Iwai507d7802015-09-04 11:35:57 +020023 if (in_signal)
24 finish_command_in_signal(&pager_process);
25 else
26 finish_command(&pager_process);
27}
28
29static void wait_for_pager_atexit(void)
30{
31 wait_for_pager(0);
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010032}
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080033
Jeff Kinga3da8822009-01-22 01:03:28 -050034static void wait_for_pager_signal(int signo)
35{
Takashi Iwai507d7802015-09-04 11:35:57 +020036 wait_for_pager(1);
Jeff Kinga3da8822009-01-22 01:03:28 -050037 sigchain_pop(signo);
38 raise(signo);
39}
40
Jeff King4babb832016-09-12 20:23:44 -070041static int core_pager_config(const char *var, const char *value, void *data)
42{
43 if (!strcmp(var, "core.pager"))
44 return git_config_string(&pager_program, var, value);
45 return 0;
46}
47
Jonathan Nieder64778d22010-02-14 05:59:59 -060048const char *git_pager(int stdout_is_tty)
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080049{
Jonathan Nieder63618242009-10-30 20:41:27 -050050 const char *pager;
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080051
Jonathan Nieder64778d22010-02-14 05:59:59 -060052 if (!stdout_is_tty)
Jonathan Nieder63618242009-10-30 20:41:27 -050053 return NULL;
54
55 pager = getenv("GIT_PAGER");
Junio C Hamanocad3a202007-08-06 21:08:43 -070056 if (!pager) {
57 if (!pager_program)
Jeff Kingeed27072016-09-12 20:23:56 -070058 read_early_config(core_pager_config, NULL);
Brian Gernhardt54adf372007-07-03 14:18:11 -040059 pager = pager_program;
Junio C Hamanocad3a202007-08-06 21:08:43 -070060 }
Brian Gernhardt54adf372007-07-03 14:18:11 -040061 if (!pager)
Matthias Lederhoferc27d2052006-07-31 15:27:00 +020062 pager = getenv("PAGER");
63 if (!pager)
Junio C Hamanoa3d023d2009-10-30 20:45:34 -050064 pager = DEFAULT_PAGER;
Jeff Kinged016612013-09-03 03:41:50 -040065 if (!*pager || !strcmp(pager, "cat"))
Jonathan Nieder63618242009-10-30 20:41:27 -050066 pager = NULL;
67
68 return pager;
69}
70
Eric Wong995bc222016-08-04 11:40:25 +000071static void setup_pager_env(struct argv_array *env)
72{
73 const char **argv;
74 int i;
75 char *pager_env = xstrdup(PAGER_ENV);
76 int n = split_cmdline(pager_env, &argv);
77
78 if (n < 0)
79 die("malformed build-time PAGER_ENV: %s",
80 split_cmdline_strerror(n));
81
82 for (i = 0; i < n; i++) {
83 char *cp = strchr(argv[i], '=');
84
85 if (!cp)
86 die("malformed build-time PAGER_ENV");
87
88 *cp = '\0';
89 if (!getenv(argv[i])) {
90 *cp = '=';
91 argv_array_push(env, argv[i]);
92 }
93 }
94 free(pager_env);
95 free(argv);
96}
97
Junio C Hamano3e3a4a42016-02-16 14:34:44 -080098void prepare_pager_args(struct child_process *pager_process, const char *pager)
99{
100 argv_array_push(&pager_process->args, pager);
101 pager_process->use_shell = 1;
Eric Wong995bc222016-08-04 11:40:25 +0000102 setup_pager_env(&pager_process->env_array);
Jeff Hostetlereee73d12019-02-22 14:25:04 -0800103 pager_process->trace2_child_class = "pager";
Junio C Hamano3e3a4a42016-02-16 14:34:44 -0800104}
105
Jonathan Nieder63618242009-10-30 20:41:27 -0500106void setup_pager(void)
107{
Jonathan Nieder64778d22010-02-14 05:59:59 -0600108 const char *pager = git_pager(isatty(1));
Jonathan Nieder63618242009-10-30 20:41:27 -0500109
Jörn Engelc0459ca2014-04-21 16:46:22 -0400110 if (!pager)
Johannes Schindelin402461a2006-04-16 04:44:25 +0200111 return;
112
Zbigniew Jędrzejewski-Szmekad6c3732012-02-12 15:12:32 +0100113 /*
Jeff Kingbe11f7a2018-05-11 05:25:16 -0400114 * After we redirect standard output, we won't be able to use an ioctl
115 * to get the terminal size. Let's grab it now, and then set $COLUMNS
116 * to communicate it to any sub-processes.
Zbigniew Jędrzejewski-Szmekad6c3732012-02-12 15:12:32 +0100117 */
Jeff Kingbe11f7a2018-05-11 05:25:16 -0400118 {
119 char buf[64];
120 xsnprintf(buf, sizeof(buf), "%d", term_columns());
121 setenv("COLUMNS", buf, 0);
122 }
Zbigniew Jędrzejewski-Szmekad6c3732012-02-12 15:12:32 +0100123
Jeff King2e6c0122011-08-17 22:02:29 -0700124 setenv("GIT_PAGER_IN_USE", "true", 1);
Junio C Hamano85fb65e2006-06-06 16:58:40 -0700125
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +0100126 /* spawn the pager */
Junio C Hamano3e3a4a42016-02-16 14:34:44 -0800127 prepare_pager_args(&pager_process, pager);
Jeff Kingea27a182008-07-22 03:14:12 -0400128 pager_process.in = -1;
Junio C Hamano124b5192015-07-03 10:18:45 -0700129 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +0100130 if (start_command(&pager_process))
131 return;
132
133 /* original process continues, but writes to the pipe */
134 dup2(pager_process.in, 1);
Junio C Hamanoa8335022008-12-15 00:33:34 -0800135 if (isatty(2))
136 dup2(pager_process.in, 2);
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +0100137 close(pager_process.in);
138
139 /* this makes sure that the parent terminates after the pager */
Jeff Kinga3da8822009-01-22 01:03:28 -0500140 sigchain_push_common(wait_for_pager_signal);
Takashi Iwai507d7802015-09-04 11:35:57 +0200141 atexit(wait_for_pager_atexit);
Linus Torvaldsf67b45f2006-02-28 11:26:21 -0800142}
Jeff King6e9af862007-12-11 01:27:33 -0500143
144int pager_in_use(void)
145{
Jeff Kingdf2a6e32017-03-24 14:59:12 -0400146 return git_env_bool("GIT_PAGER_IN_USE", 0);
Jeff King6e9af862007-12-11 01:27:33 -0500147}
Zbigniew Jędrzejewski-Szmekad6c3732012-02-12 15:12:32 +0100148
149/*
150 * Return cached value (if set) or $COLUMNS environment variable (if
151 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
152 * and default to 80 if all else fails.
153 */
154int term_columns(void)
155{
156 static int term_columns_at_startup;
157
158 char *col_string;
159 int n_cols;
160
161 if (term_columns_at_startup)
162 return term_columns_at_startup;
163
164 term_columns_at_startup = 80;
165
166 col_string = getenv("COLUMNS");
167 if (col_string && (n_cols = atoi(col_string)) > 0)
168 term_columns_at_startup = n_cols;
169#ifdef TIOCGWINSZ
170 else {
171 struct winsize ws;
172 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
173 term_columns_at_startup = ws.ws_col;
174 }
175#endif
176
177 return term_columns_at_startup;
178}
Junio C Hamano4d9e0792012-02-20 00:15:11 -0800179
180/*
SZEDER Gáborcd1096b2019-06-24 20:13:16 +0200181 * Clear the entire line, leave cursor in first column.
182 */
183void term_clear_line(void)
184{
185 if (is_terminal_dumb())
186 /*
187 * Fall back to print a terminal width worth of space
188 * characters (hoping that the terminal is still as wide
189 * as it was upon the first call to term_columns()).
190 */
191 fprintf(stderr, "\r%*s\r", term_columns(), "");
192 else
193 /*
194 * On non-dumb terminals use an escape sequence to clear
195 * the whole line, no matter how wide the terminal.
196 */
197 fputs("\r\033[K", stderr);
198}
199
200/*
Zbigniew Jędrzejewski-Szmekec7ff5b2012-02-12 15:16:20 +0100201 * How many columns do we need to show this number in decimal?
202 */
Jeff Kingd306f3d2015-02-05 03:14:19 -0500203int decimal_width(uintmax_t number)
Zbigniew Jędrzejewski-Szmekec7ff5b2012-02-12 15:16:20 +0100204{
Jeff Kingd306f3d2015-02-05 03:14:19 -0500205 int width;
Zbigniew Jędrzejewski-Szmekec7ff5b2012-02-12 15:16:20 +0100206
Jeff Kingd306f3d2015-02-05 03:14:19 -0500207 for (width = 1; number >= 10; width++)
208 number /= 10;
Zbigniew Jędrzejewski-Szmekec7ff5b2012-02-12 15:16:20 +0100209 return width;
210}
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +0700211
Jeff King6a1e1bc2016-09-12 20:23:52 -0700212struct pager_command_config_data {
213 const char *cmd;
214 int want;
215 char *value;
216};
217
218static int pager_command_config(const char *var, const char *value, void *vdata)
219{
220 struct pager_command_config_data *data = vdata;
221 const char *cmd;
222
223 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
Martin Ågren89576612017-08-07 20:20:49 +0200224 int b = git_parse_maybe_bool(value);
Jeff King6a1e1bc2016-09-12 20:23:52 -0700225 if (b >= 0)
226 data->want = b;
227 else {
228 data->want = 1;
229 data->value = xstrdup(value);
230 }
231 }
232
233 return 0;
234}
235
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +0700236/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
237int check_pager_config(const char *cmd)
238{
Jeff King6a1e1bc2016-09-12 20:23:52 -0700239 struct pager_command_config_data data;
240
241 data.cmd = cmd;
242 data.want = -1;
243 data.value = NULL;
244
Jeff Kingeed27072016-09-12 20:23:56 -0700245 read_early_config(pager_command_config, &data);
Jeff King6a1e1bc2016-09-12 20:23:52 -0700246
247 if (data.value)
248 pager_program = data.value;
249 return data.want;
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +0700250}