2 #include "run-command.h"
6 #define DEFAULT_PAGER "less"
9 static struct child_process pager_process = CHILD_PROCESS_INIT;
10 static const char *pager_program;
12 static void wait_for_pager(int in_signal)
18 /* signal EOF to pager */
22 finish_command_in_signal(&pager_process);
24 finish_command(&pager_process);
27 static void wait_for_pager_atexit(void)
32 static void wait_for_pager_signal(int signo)
39 static int core_pager_config(const char *var, const char *value, void *data)
41 if (!strcmp(var, "core.pager"))
42 return git_config_string(&pager_program, var, value);
46 const char *git_pager(int stdout_is_tty)
53 pager = getenv("GIT_PAGER");
56 read_early_config(core_pager_config, NULL);
57 pager = pager_program;
60 pager = getenv("PAGER");
62 pager = DEFAULT_PAGER;
63 if (!*pager || !strcmp(pager, "cat"))
69 static void setup_pager_env(struct argv_array *env)
73 char *pager_env = xstrdup(PAGER_ENV);
74 int n = split_cmdline(pager_env, &argv);
77 die("malformed build-time PAGER_ENV: %s",
78 split_cmdline_strerror(n));
80 for (i = 0; i < n; i++) {
81 char *cp = strchr(argv[i], '=');
84 die("malformed build-time PAGER_ENV");
87 if (!getenv(argv[i])) {
89 argv_array_push(env, argv[i]);
96 void prepare_pager_args(struct child_process *pager_process, const char *pager)
98 argv_array_push(&pager_process->args, pager);
99 pager_process->use_shell = 1;
100 setup_pager_env(&pager_process->env_array);
103 void setup_pager(void)
105 const char *pager = git_pager(isatty(1));
111 * force computing the width of the terminal before we redirect
112 * the standard output to the pager.
114 (void) term_columns();
116 setenv("GIT_PAGER_IN_USE", "true", 1);
118 /* spawn the pager */
119 prepare_pager_args(&pager_process, pager);
120 pager_process.in = -1;
121 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
122 if (start_command(&pager_process))
125 /* original process continues, but writes to the pipe */
126 dup2(pager_process.in, 1);
128 dup2(pager_process.in, 2);
129 close(pager_process.in);
131 /* this makes sure that the parent terminates after the pager */
132 sigchain_push_common(wait_for_pager_signal);
133 atexit(wait_for_pager_atexit);
136 int pager_in_use(void)
139 env = getenv("GIT_PAGER_IN_USE");
140 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
144 * Return cached value (if set) or $COLUMNS environment variable (if
145 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
146 * and default to 80 if all else fails.
148 int term_columns(void)
150 static int term_columns_at_startup;
155 if (term_columns_at_startup)
156 return term_columns_at_startup;
158 term_columns_at_startup = 80;
160 col_string = getenv("COLUMNS");
161 if (col_string && (n_cols = atoi(col_string)) > 0)
162 term_columns_at_startup = n_cols;
166 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
167 term_columns_at_startup = ws.ws_col;
171 return term_columns_at_startup;
175 * How many columns do we need to show this number in decimal?
177 int decimal_width(uintmax_t number)
181 for (width = 1; number >= 10; width++)
186 struct pager_command_config_data {
192 static int pager_command_config(const char *var, const char *value, void *vdata)
194 struct pager_command_config_data *data = vdata;
197 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
198 int b = git_config_maybe_bool(var, value);
203 data->value = xstrdup(value);
210 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
211 int check_pager_config(const char *cmd)
213 struct pager_command_config_data data;
219 read_early_config(pager_command_config, &data);
222 pager_program = data.value;