builtin/submodule--helper: fix leaking error buffer
[git/gitster.git] / pager.c
blob9c24ce6263385eb4af306453c1eb2668edd3af1e
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"
9 int pager_use_color = 1;
11 #ifndef DEFAULT_PAGER
12 #define DEFAULT_PAGER "less"
13 #endif
15 static struct child_process pager_process;
16 static char *pager_program;
17 static int old_fd1 = -1, old_fd2 = -1;
19 /* Is the value coming back from term_columns() just a guess? */
20 static int term_columns_guessed;
23 static void close_pager_fds(void)
25 /* signal EOF to pager */
26 close(1);
27 if (old_fd2 != -1)
28 close(2);
31 static void finish_pager(void)
33 fflush(stdout);
34 fflush(stderr);
35 close_pager_fds();
36 finish_command(&pager_process);
39 static void wait_for_pager_atexit(void)
41 if (old_fd1 == -1)
42 return;
44 finish_pager();
47 void wait_for_pager(void)
49 if (old_fd1 == -1)
50 return;
52 finish_pager();
53 sigchain_pop_common();
54 unsetenv("GIT_PAGER_IN_USE");
55 dup2(old_fd1, 1);
56 close(old_fd1);
57 old_fd1 = -1;
58 if (old_fd2 != -1) {
59 dup2(old_fd2, 2);
60 close(old_fd2);
61 old_fd2 = -1;
65 static void wait_for_pager_signal(int signo)
67 if (old_fd1 == -1)
68 return;
70 close_pager_fds();
71 finish_command_in_signal(&pager_process);
72 sigchain_pop(signo);
73 raise(signo);
76 static int core_pager_config(const char *var, const char *value,
77 const struct config_context *ctx UNUSED,
78 void *data UNUSED)
80 if (!strcmp(var, "core.pager"))
81 return git_config_string(&pager_program, var, value);
82 return 0;
85 const char *git_pager(int stdout_is_tty)
87 const char *pager;
89 if (!stdout_is_tty)
90 return NULL;
92 pager = getenv("GIT_PAGER");
93 if (!pager) {
94 if (!pager_program)
95 read_early_config(core_pager_config, NULL);
96 pager = pager_program;
98 if (!pager)
99 pager = getenv("PAGER");
100 if (!pager)
101 pager = DEFAULT_PAGER;
102 if (!*pager || !strcmp(pager, "cat"))
103 pager = NULL;
105 return pager;
108 static void setup_pager_env(struct strvec *env)
110 const char **argv;
111 int i;
112 char *pager_env = xstrdup(PAGER_ENV);
113 int n = split_cmdline(pager_env, &argv);
115 if (n < 0)
116 die("malformed build-time PAGER_ENV: %s",
117 split_cmdline_strerror(n));
119 for (i = 0; i < n; i++) {
120 char *cp = strchr(argv[i], '=');
122 if (!cp)
123 die("malformed build-time PAGER_ENV");
125 *cp = '\0';
126 if (!getenv(argv[i])) {
127 *cp = '=';
128 strvec_push(env, argv[i]);
131 free(pager_env);
132 free(argv);
135 void prepare_pager_args(struct child_process *pager_process, const char *pager)
137 strvec_push(&pager_process->args, pager);
138 pager_process->use_shell = 1;
139 setup_pager_env(&pager_process->env);
140 pager_process->trace2_child_class = "pager";
143 void setup_pager(void)
145 static int once = 0;
146 const char *pager = git_pager(isatty(1));
148 if (!pager)
149 return;
152 * After we redirect standard output, we won't be able to use an ioctl
153 * to get the terminal size. Let's grab it now, and then set $COLUMNS
154 * to communicate it to any sub-processes.
157 char buf[64];
158 xsnprintf(buf, sizeof(buf), "%d", term_columns());
159 if (!term_columns_guessed)
160 setenv("COLUMNS", buf, 0);
163 setenv("GIT_PAGER_IN_USE", "true", 1);
165 child_process_init(&pager_process);
167 /* spawn the pager */
168 prepare_pager_args(&pager_process, pager);
169 pager_process.in = -1;
170 strvec_push(&pager_process.env, "GIT_PAGER_IN_USE");
171 if (start_command(&pager_process))
172 die("unable to execute pager '%s'", pager);
174 /* original process continues, but writes to the pipe */
175 old_fd1 = dup(1);
176 dup2(pager_process.in, 1);
177 if (isatty(2)) {
178 old_fd2 = dup(2);
179 dup2(pager_process.in, 2);
181 close(pager_process.in);
183 sigchain_push_common(wait_for_pager_signal);
185 if (!once) {
186 once++;
187 atexit(wait_for_pager_atexit);
191 int pager_in_use(void)
193 return git_env_bool("GIT_PAGER_IN_USE", 0);
197 * Return cached value (if set) or $COLUMNS environment variable (if
198 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
199 * and default to 80 if all else fails.
201 int term_columns(void)
203 static int term_columns_at_startup;
205 char *col_string;
206 int n_cols;
208 if (term_columns_at_startup)
209 return term_columns_at_startup;
211 term_columns_at_startup = 80;
212 term_columns_guessed = 1;
214 col_string = getenv("COLUMNS");
215 if (col_string && (n_cols = atoi(col_string)) > 0) {
216 term_columns_at_startup = n_cols;
217 term_columns_guessed = 0;
219 #ifdef TIOCGWINSZ
220 else {
221 struct winsize ws;
222 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col) {
223 term_columns_at_startup = ws.ws_col;
224 term_columns_guessed = 0;
227 #endif
229 return term_columns_at_startup;
233 * Clear the entire line, leave cursor in first column.
235 void term_clear_line(void)
237 if (!isatty(2))
238 return;
239 if (is_terminal_dumb())
241 * Fall back to print a terminal width worth of space
242 * characters (hoping that the terminal is still as wide
243 * as it was upon the first call to term_columns()).
245 fprintf(stderr, "\r%*s\r", term_columns(), "");
246 else
248 * On non-dumb terminals use an escape sequence to clear
249 * the whole line, no matter how wide the terminal.
251 fputs("\r\033[K", stderr);
255 * How many columns do we need to show this number in decimal?
257 int decimal_width(uintmax_t number)
259 int width;
261 for (width = 1; number >= 10; width++)
262 number /= 10;
263 return width;
266 struct pager_command_config_data {
267 const char *cmd;
268 int want;
269 char *value;
272 static int pager_command_config(const char *var, const char *value,
273 const struct config_context *ctx UNUSED,
274 void *vdata)
276 struct pager_command_config_data *data = vdata;
277 const char *cmd;
279 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
280 int b = git_parse_maybe_bool(value);
281 if (b >= 0)
282 data->want = b;
283 else {
284 data->want = 1;
285 data->value = xstrdup(value);
289 return 0;
292 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
293 int check_pager_config(const char *cmd)
295 struct pager_command_config_data data;
297 data.cmd = cmd;
298 data.want = -1;
299 data.value = NULL;
301 read_early_config(pager_command_config, &data);
303 if (data.value)
304 pager_program = data.value;
305 return data.want;