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