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