3 #include "run-command.h"
7 #define DEFAULT_PAGER "less"
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)
19 /* signal EOF to pager */
23 finish_command_in_signal(&pager_process);
25 finish_command(&pager_process);
28 static void wait_for_pager_atexit(void)
33 static void wait_for_pager_signal(int 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);
47 const char *git_pager(int stdout_is_tty)
54 pager = getenv("GIT_PAGER");
57 read_early_config(core_pager_config, NULL);
58 pager = pager_program;
61 pager = getenv("PAGER");
63 pager = DEFAULT_PAGER;
64 if (!*pager || !strcmp(pager, "cat"))
70 static void setup_pager_env(struct argv_array *env)
74 char *pager_env = xstrdup(PAGER_ENV);
75 int n = split_cmdline(pager_env, &argv);
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], '=');
85 die("malformed build-time PAGER_ENV");
88 if (!getenv(argv[i])) {
90 argv_array_push(env, argv[i]);
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));
112 * After we redirect standard output, we won't be able to use an ioctl
113 * to get the terminal size. Let's grab it now, and then set $COLUMNS
114 * to communicate it to any sub-processes.
118 xsnprintf(buf, sizeof(buf), "%d", term_columns());
119 setenv("COLUMNS", buf, 0);
122 setenv("GIT_PAGER_IN_USE", "true", 1);
124 /* spawn the pager */
125 prepare_pager_args(&pager_process, pager);
126 pager_process.in = -1;
127 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
128 if (start_command(&pager_process))
131 /* original process continues, but writes to the pipe */
132 dup2(pager_process.in, 1);
134 dup2(pager_process.in, 2);
135 close(pager_process.in);
137 /* this makes sure that the parent terminates after the pager */
138 sigchain_push_common(wait_for_pager_signal);
139 atexit(wait_for_pager_atexit);
142 int pager_in_use(void)
144 return git_env_bool("GIT_PAGER_IN_USE", 0);
148 * Return cached value (if set) or $COLUMNS environment variable (if
149 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
150 * and default to 80 if all else fails.
152 int term_columns(void)
154 static int term_columns_at_startup;
159 if (term_columns_at_startup)
160 return term_columns_at_startup;
162 term_columns_at_startup = 80;
164 col_string = getenv("COLUMNS");
165 if (col_string && (n_cols = atoi(col_string)) > 0)
166 term_columns_at_startup = n_cols;
170 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
171 term_columns_at_startup = ws.ws_col;
175 return term_columns_at_startup;
179 * How many columns do we need to show this number in decimal?
181 int decimal_width(uintmax_t number)
185 for (width = 1; number >= 10; width++)
190 struct pager_command_config_data {
196 static int pager_command_config(const char *var, const char *value, void *vdata)
198 struct pager_command_config_data *data = vdata;
201 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
202 int b = git_parse_maybe_bool(value);
207 data->value = xstrdup(value);
214 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
215 int check_pager_config(const char *cmd)
217 struct pager_command_config_data data;
223 read_early_config(pager_command_config, &data);
226 pager_program = data.value;