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