2 #include "run-command.h"
6 #define DEFAULT_PAGER "less"
10 * This is split up from the rest of git so that we can do
11 * something different on Windows.
14 static struct child_process pager_process = CHILD_PROCESS_INIT;
16 static void wait_for_pager(int in_signal)
22 /* signal EOF to pager */
26 finish_command_in_signal(&pager_process);
28 finish_command(&pager_process);
31 static void wait_for_pager_atexit(void)
36 static void wait_for_pager_signal(int signo)
43 const char *git_pager(int stdout_is_tty)
50 pager = getenv("GIT_PAGER");
53 git_config(git_default_config, NULL);
54 pager = pager_program;
57 pager = getenv("PAGER");
59 pager = DEFAULT_PAGER;
60 if (!*pager || !strcmp(pager, "cat"))
66 static void setup_pager_env(struct argv_array *env)
70 char *pager_env = xstrdup(PAGER_ENV);
71 int n = split_cmdline(pager_env, &argv);
74 die("malformed build-time PAGER_ENV: %s",
75 split_cmdline_strerror(n));
77 for (i = 0; i < n; i++) {
78 char *cp = strchr(argv[i], '=');
81 die("malformed build-time PAGER_ENV");
84 if (!getenv(argv[i])) {
86 argv_array_push(env, argv[i]);
93 void prepare_pager_args(struct child_process *pager_process, const char *pager)
95 argv_array_push(&pager_process->args, pager);
96 pager_process->use_shell = 1;
97 setup_pager_env(&pager_process->env_array);
100 void setup_pager(void)
102 const char *pager = git_pager(isatty(1));
108 * force computing the width of the terminal before we redirect
109 * the standard output to the pager.
111 (void) term_columns();
113 setenv("GIT_PAGER_IN_USE", "true", 1);
115 /* spawn the pager */
116 prepare_pager_args(&pager_process, pager);
117 pager_process.in = -1;
118 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
119 if (start_command(&pager_process))
122 /* original process continues, but writes to the pipe */
123 dup2(pager_process.in, 1);
125 dup2(pager_process.in, 2);
126 close(pager_process.in);
128 /* this makes sure that the parent terminates after the pager */
129 sigchain_push_common(wait_for_pager_signal);
130 atexit(wait_for_pager_atexit);
133 int pager_in_use(void)
136 env = getenv("GIT_PAGER_IN_USE");
137 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
141 * Return cached value (if set) or $COLUMNS environment variable (if
142 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
143 * and default to 80 if all else fails.
145 int term_columns(void)
147 static int term_columns_at_startup;
152 if (term_columns_at_startup)
153 return term_columns_at_startup;
155 term_columns_at_startup = 80;
157 col_string = getenv("COLUMNS");
158 if (col_string && (n_cols = atoi(col_string)) > 0)
159 term_columns_at_startup = n_cols;
163 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
164 term_columns_at_startup = ws.ws_col;
168 return term_columns_at_startup;
172 * How many columns do we need to show this number in decimal?
174 int decimal_width(uintmax_t number)
178 for (width = 1; number >= 10; width++)
183 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
184 int check_pager_config(const char *cmd)
187 struct strbuf key = STRBUF_INIT;
188 const char *value = NULL;
189 strbuf_addf(&key, "pager.%s", cmd);
190 if (git_config_key_is_valid(key.buf) &&
191 !git_config_get_value(key.buf, &value)) {
192 int b = git_config_maybe_bool(key.buf, value);
197 pager_program = xstrdup(value);
200 strbuf_release(&key);