Sync with 'maint'
[alt-git.git] / pager.c
blob40b664f893c8ec61af0e6d00d58956f45b247d65
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "config.h"
5 #include "editor.h"
6 #include "pager.h"
7 #include "run-command.h"
8 #include "sigchain.h"
9 #include "alias.h"
11 int pager_use_color = 1;
13 #ifndef DEFAULT_PAGER
14 #define DEFAULT_PAGER "less"
15 #endif
17 static struct child_process pager_process;
18 static char *pager_program;
19 static int old_fd1 = -1, old_fd2 = -1;
21 /* Is the value coming back from term_columns() just a guess? */
22 static int term_columns_guessed;
25 static void close_pager_fds(void)
27 /* signal EOF to pager */
28 close(1);
29 if (old_fd2 != -1)
30 close(2);
33 static void finish_pager(void)
35 fflush(stdout);
36 fflush(stderr);
37 close_pager_fds();
38 finish_command(&pager_process);
41 static void wait_for_pager_atexit(void)
43 if (old_fd1 == -1)
44 return;
46 finish_pager();
49 void wait_for_pager(void)
51 if (old_fd1 == -1)
52 return;
54 finish_pager();
55 sigchain_pop_common();
56 unsetenv("GIT_PAGER_IN_USE");
57 dup2(old_fd1, 1);
58 close(old_fd1);
59 old_fd1 = -1;
60 if (old_fd2 != -1) {
61 dup2(old_fd2, 2);
62 close(old_fd2);
63 old_fd2 = -1;
67 static void wait_for_pager_signal(int signo)
69 if (old_fd1 == -1)
70 return;
72 close_pager_fds();
73 finish_command_in_signal(&pager_process);
74 sigchain_pop(signo);
75 raise(signo);
78 static int core_pager_config(const char *var, const char *value,
79 const struct config_context *ctx UNUSED,
80 void *data UNUSED)
82 if (!strcmp(var, "core.pager"))
83 return git_config_string(&pager_program, var, value);
84 return 0;
87 const char *git_pager(int stdout_is_tty)
89 const char *pager;
91 if (!stdout_is_tty)
92 return NULL;
94 pager = getenv("GIT_PAGER");
95 if (!pager) {
96 if (!pager_program)
97 read_early_config(the_repository,
98 core_pager_config, NULL);
99 pager = pager_program;
101 if (!pager)
102 pager = getenv("PAGER");
103 if (!pager)
104 pager = DEFAULT_PAGER;
105 if (!*pager || !strcmp(pager, "cat"))
106 pager = NULL;
108 return pager;
111 static void setup_pager_env(struct strvec *env)
113 const char **argv;
114 int i;
115 char *pager_env = xstrdup(PAGER_ENV);
116 int n = split_cmdline(pager_env, &argv);
118 if (n < 0)
119 die("malformed build-time PAGER_ENV: %s",
120 split_cmdline_strerror(n));
122 for (i = 0; i < n; i++) {
123 char *cp = strchr(argv[i], '=');
125 if (!cp)
126 die("malformed build-time PAGER_ENV");
128 *cp = '\0';
129 if (!getenv(argv[i])) {
130 *cp = '=';
131 strvec_push(env, argv[i]);
134 free(pager_env);
135 free(argv);
138 void prepare_pager_args(struct child_process *pager_process, const char *pager)
140 strvec_push(&pager_process->args, pager);
141 pager_process->use_shell = 1;
142 setup_pager_env(&pager_process->env);
143 pager_process->trace2_child_class = "pager";
146 void setup_pager(void)
148 static int once = 0;
149 const char *pager = git_pager(isatty(1));
151 if (!pager)
152 return;
155 * After we redirect standard output, we won't be able to use an ioctl
156 * to get the terminal size. Let's grab it now, and then set $COLUMNS
157 * to communicate it to any sub-processes.
160 char buf[64];
161 xsnprintf(buf, sizeof(buf), "%d", term_columns());
162 if (!term_columns_guessed)
163 setenv("COLUMNS", buf, 0);
166 setenv("GIT_PAGER_IN_USE", "true", 1);
168 child_process_init(&pager_process);
170 /* spawn the pager */
171 prepare_pager_args(&pager_process, pager);
172 pager_process.in = -1;
173 strvec_push(&pager_process.env, "GIT_PAGER_IN_USE");
174 if (start_command(&pager_process))
175 die("unable to execute pager '%s'", pager);
177 /* original process continues, but writes to the pipe */
178 old_fd1 = dup(1);
179 dup2(pager_process.in, 1);
180 if (isatty(2)) {
181 old_fd2 = dup(2);
182 dup2(pager_process.in, 2);
184 close(pager_process.in);
186 sigchain_push_common(wait_for_pager_signal);
188 if (!once) {
189 once++;
190 atexit(wait_for_pager_atexit);
194 int pager_in_use(void)
196 return git_env_bool("GIT_PAGER_IN_USE", 0);
200 * Return cached value (if set) or $COLUMNS environment variable (if
201 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
202 * and default to 80 if all else fails.
204 int term_columns(void)
206 static int term_columns_at_startup;
208 char *col_string;
209 int n_cols;
211 if (term_columns_at_startup)
212 return term_columns_at_startup;
214 term_columns_at_startup = 80;
215 term_columns_guessed = 1;
217 col_string = getenv("COLUMNS");
218 if (col_string && (n_cols = atoi(col_string)) > 0) {
219 term_columns_at_startup = n_cols;
220 term_columns_guessed = 0;
222 #ifdef TIOCGWINSZ
223 else {
224 struct winsize ws;
225 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col) {
226 term_columns_at_startup = ws.ws_col;
227 term_columns_guessed = 0;
230 #endif
232 return term_columns_at_startup;
236 * Clear the entire line, leave cursor in first column.
238 void term_clear_line(void)
240 if (!isatty(2))
241 return;
242 if (is_terminal_dumb())
244 * Fall back to print a terminal width worth of space
245 * characters (hoping that the terminal is still as wide
246 * as it was upon the first call to term_columns()).
248 fprintf(stderr, "\r%*s\r", term_columns(), "");
249 else
251 * On non-dumb terminals use an escape sequence to clear
252 * the whole line, no matter how wide the terminal.
254 fputs("\r\033[K", stderr);
258 * How many columns do we need to show this number in decimal?
260 int decimal_width(uintmax_t number)
262 int width;
264 for (width = 1; number >= 10; width++)
265 number /= 10;
266 return width;
269 struct pager_command_config_data {
270 const char *cmd;
271 int want;
272 char *value;
275 static int pager_command_config(const char *var, const char *value,
276 const struct config_context *ctx UNUSED,
277 void *vdata)
279 struct pager_command_config_data *data = vdata;
280 const char *cmd;
282 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
283 int b = git_parse_maybe_bool(value);
284 if (b >= 0)
285 data->want = b;
286 else {
287 data->want = 1;
288 data->value = xstrdup(value);
292 return 0;
295 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
296 int check_pager_config(const char *cmd)
298 struct pager_command_config_data data;
300 data.cmd = cmd;
301 data.want = -1;
302 data.value = NULL;
304 read_early_config(the_repository, pager_command_config, &data);
306 if (data.value)
307 pager_program = data.value;
308 return data.want;