setup: adopt shared init-db & clone code
[git.git] / pager.c
blob63055d0873f01c01809927ecd56d09a10f500fbe
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 const char *pager_program;
18 /* Is the value coming back from term_columns() just a guess? */
19 static int term_columns_guessed;
22 static void close_pager_fds(void)
24 /* signal EOF to pager */
25 close(1);
26 close(2);
29 static void wait_for_pager_atexit(void)
31 fflush(stdout);
32 fflush(stderr);
33 close_pager_fds();
34 finish_command(&pager_process);
37 static void wait_for_pager_signal(int signo)
39 close_pager_fds();
40 finish_command_in_signal(&pager_process);
41 sigchain_pop(signo);
42 raise(signo);
45 static int core_pager_config(const char *var, const char *value,
46 void *data UNUSED)
48 if (!strcmp(var, "core.pager"))
49 return git_config_string(&pager_program, var, value);
50 return 0;
53 const char *git_pager(int stdout_is_tty)
55 const char *pager;
57 if (!stdout_is_tty)
58 return NULL;
60 pager = getenv("GIT_PAGER");
61 if (!pager) {
62 if (!pager_program)
63 read_early_config(core_pager_config, NULL);
64 pager = pager_program;
66 if (!pager)
67 pager = getenv("PAGER");
68 if (!pager)
69 pager = DEFAULT_PAGER;
70 if (!*pager || !strcmp(pager, "cat"))
71 pager = NULL;
73 return pager;
76 static void setup_pager_env(struct strvec *env)
78 const char **argv;
79 int i;
80 char *pager_env = xstrdup(PAGER_ENV);
81 int n = split_cmdline(pager_env, &argv);
83 if (n < 0)
84 die("malformed build-time PAGER_ENV: %s",
85 split_cmdline_strerror(n));
87 for (i = 0; i < n; i++) {
88 char *cp = strchr(argv[i], '=');
90 if (!cp)
91 die("malformed build-time PAGER_ENV");
93 *cp = '\0';
94 if (!getenv(argv[i])) {
95 *cp = '=';
96 strvec_push(env, argv[i]);
99 free(pager_env);
100 free(argv);
103 void prepare_pager_args(struct child_process *pager_process, const char *pager)
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";
111 void setup_pager(void)
113 const char *pager = git_pager(isatty(1));
115 if (!pager)
116 return;
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.
124 char buf[64];
125 xsnprintf(buf, sizeof(buf), "%d", term_columns());
126 if (!term_columns_guessed)
127 setenv("COLUMNS", buf, 0);
130 setenv("GIT_PAGER_IN_USE", "true", 1);
132 child_process_init(&pager_process);
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;
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);
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);
152 int pager_in_use(void)
154 return git_env_bool("GIT_PAGER_IN_USE", 0);
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.
162 int term_columns(void)
164 static int term_columns_at_startup;
166 char *col_string;
167 int n_cols;
169 if (term_columns_at_startup)
170 return term_columns_at_startup;
172 term_columns_at_startup = 80;
173 term_columns_guessed = 1;
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;
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;
188 #endif
190 return term_columns_at_startup;
194 * Clear the entire line, leave cursor in first column.
196 void term_clear_line(void)
198 if (is_terminal_dumb())
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()).
204 fprintf(stderr, "\r%*s\r", term_columns(), "");
205 else
207 * On non-dumb terminals use an escape sequence to clear
208 * the whole line, no matter how wide the terminal.
210 fputs("\r\033[K", stderr);
214 * How many columns do we need to show this number in decimal?
216 int decimal_width(uintmax_t number)
218 int width;
220 for (width = 1; number >= 10; width++)
221 number /= 10;
222 return width;
225 struct pager_command_config_data {
226 const char *cmd;
227 int want;
228 char *value;
231 static int pager_command_config(const char *var, const char *value, void *vdata)
233 struct pager_command_config_data *data = vdata;
234 const char *cmd;
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);
246 return 0;
249 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
250 int check_pager_config(const char *cmd)
252 struct pager_command_config_data data;
254 data.cmd = cmd;
255 data.want = -1;
256 data.value = NULL;
258 read_early_config(pager_command_config, &data);
260 if (data.value)
261 pager_program = data.value;
262 return data.want;