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