Merge branch 'ma/split-symref-update-fix'
[git.git] / pager.c
blob92b23e6cd1d44a26c86afeeb748ddc0aee3f9154
1 #include "cache.h"
2 #include "config.h"
3 #include "run-command.h"
4 #include "sigchain.h"
6 #ifndef DEFAULT_PAGER
7 #define DEFAULT_PAGER "less"
8 #endif
10 static struct child_process pager_process = CHILD_PROCESS_INIT;
11 static const char *pager_program;
13 static void wait_for_pager(int in_signal)
15 if (!in_signal) {
16 fflush(stdout);
17 fflush(stderr);
19 /* signal EOF to pager */
20 close(1);
21 close(2);
22 if (in_signal)
23 finish_command_in_signal(&pager_process);
24 else
25 finish_command(&pager_process);
28 static void wait_for_pager_atexit(void)
30 wait_for_pager(0);
33 static void wait_for_pager_signal(int signo)
35 wait_for_pager(1);
36 sigchain_pop(signo);
37 raise(signo);
40 static int core_pager_config(const char *var, const char *value, void *data)
42 if (!strcmp(var, "core.pager"))
43 return git_config_string(&pager_program, var, value);
44 return 0;
47 const char *git_pager(int stdout_is_tty)
49 const char *pager;
51 if (!stdout_is_tty)
52 return NULL;
54 pager = getenv("GIT_PAGER");
55 if (!pager) {
56 if (!pager_program)
57 read_early_config(core_pager_config, NULL);
58 pager = pager_program;
60 if (!pager)
61 pager = getenv("PAGER");
62 if (!pager)
63 pager = DEFAULT_PAGER;
64 if (!*pager || !strcmp(pager, "cat"))
65 pager = NULL;
67 return pager;
70 static void setup_pager_env(struct argv_array *env)
72 const char **argv;
73 int i;
74 char *pager_env = xstrdup(PAGER_ENV);
75 int n = split_cmdline(pager_env, &argv);
77 if (n < 0)
78 die("malformed build-time PAGER_ENV: %s",
79 split_cmdline_strerror(n));
81 for (i = 0; i < n; i++) {
82 char *cp = strchr(argv[i], '=');
84 if (!cp)
85 die("malformed build-time PAGER_ENV");
87 *cp = '\0';
88 if (!getenv(argv[i])) {
89 *cp = '=';
90 argv_array_push(env, argv[i]);
93 free(pager_env);
94 free(argv);
97 void prepare_pager_args(struct child_process *pager_process, const char *pager)
99 argv_array_push(&pager_process->args, pager);
100 pager_process->use_shell = 1;
101 setup_pager_env(&pager_process->env_array);
104 void setup_pager(void)
106 const char *pager = git_pager(isatty(1));
108 if (!pager)
109 return;
112 * force computing the width of the terminal before we redirect
113 * the standard output to the pager.
115 (void) term_columns();
117 setenv("GIT_PAGER_IN_USE", "true", 1);
119 /* spawn the pager */
120 prepare_pager_args(&pager_process, pager);
121 pager_process.in = -1;
122 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
123 if (start_command(&pager_process))
124 return;
126 /* original process continues, but writes to the pipe */
127 dup2(pager_process.in, 1);
128 if (isatty(2))
129 dup2(pager_process.in, 2);
130 close(pager_process.in);
132 /* this makes sure that the parent terminates after the pager */
133 sigchain_push_common(wait_for_pager_signal);
134 atexit(wait_for_pager_atexit);
137 int pager_in_use(void)
139 return git_env_bool("GIT_PAGER_IN_USE", 0);
143 * Return cached value (if set) or $COLUMNS environment variable (if
144 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
145 * and default to 80 if all else fails.
147 int term_columns(void)
149 static int term_columns_at_startup;
151 char *col_string;
152 int n_cols;
154 if (term_columns_at_startup)
155 return term_columns_at_startup;
157 term_columns_at_startup = 80;
159 col_string = getenv("COLUMNS");
160 if (col_string && (n_cols = atoi(col_string)) > 0)
161 term_columns_at_startup = n_cols;
162 #ifdef TIOCGWINSZ
163 else {
164 struct winsize ws;
165 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
166 term_columns_at_startup = ws.ws_col;
168 #endif
170 return term_columns_at_startup;
174 * How many columns do we need to show this number in decimal?
176 int decimal_width(uintmax_t number)
178 int width;
180 for (width = 1; number >= 10; width++)
181 number /= 10;
182 return width;
185 struct pager_command_config_data {
186 const char *cmd;
187 int want;
188 char *value;
191 static int pager_command_config(const char *var, const char *value, void *vdata)
193 struct pager_command_config_data *data = vdata;
194 const char *cmd;
196 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
197 int b = git_parse_maybe_bool(value);
198 if (b >= 0)
199 data->want = b;
200 else {
201 data->want = 1;
202 data->value = xstrdup(value);
206 return 0;
209 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
210 int check_pager_config(const char *cmd)
212 struct pager_command_config_data data;
214 data.cmd = cmd;
215 data.want = -1;
216 data.value = NULL;
218 read_early_config(pager_command_config, &data);
220 if (data.value)
221 pager_program = data.value;
222 return data.want;