3 #include "run-command.h"
8 #define DEFAULT_PAGER "less"
11 static struct child_process pager_process = CHILD_PROCESS_INIT;
12 static const char *pager_program;
14 static void close_pager_fds(void)
16 /* signal EOF to pager */
21 static void wait_for_pager_atexit(void)
26 finish_command(&pager_process);
29 static void wait_for_pager_signal(int signo)
32 finish_command_in_signal(&pager_process);
37 static int core_pager_config(const char *var, const char *value, void *data)
39 if (!strcmp(var, "core.pager"))
40 return git_config_string(&pager_program, var, value);
44 const char *git_pager(int stdout_is_tty)
51 pager = getenv("GIT_PAGER");
54 read_early_config(core_pager_config, NULL);
55 pager = pager_program;
58 pager = getenv("PAGER");
60 pager = DEFAULT_PAGER;
61 if (!*pager || !strcmp(pager, "cat"))
67 static void setup_pager_env(struct strvec *env)
71 char *pager_env = xstrdup(PAGER_ENV);
72 int n = split_cmdline(pager_env, &argv);
75 die("malformed build-time PAGER_ENV: %s",
76 split_cmdline_strerror(n));
78 for (i = 0; i < n; i++) {
79 char *cp = strchr(argv[i], '=');
82 die("malformed build-time PAGER_ENV");
85 if (!getenv(argv[i])) {
87 strvec_push(env, argv[i]);
94 void prepare_pager_args(struct child_process *pager_process, const char *pager)
96 strvec_push(&pager_process->args, pager);
97 pager_process->use_shell = 1;
98 setup_pager_env(&pager_process->env_array);
99 pager_process->trace2_child_class = "pager";
102 void setup_pager(void)
104 const char *pager = git_pager(isatty(1));
110 * After we redirect standard output, we won't be able to use an ioctl
111 * to get the terminal size. Let's grab it now, and then set $COLUMNS
112 * to communicate it to any sub-processes.
114 #if !defined(WIN32) || defined(TIOCGWINSZ)
117 xsnprintf(buf, sizeof(buf), "%d", term_columns());
118 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 strvec_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 * Clear the entire line, leave cursor in first column.
181 void term_clear_line(void)
183 if (is_terminal_dumb())
185 * Fall back to print a terminal width worth of space
186 * characters (hoping that the terminal is still as wide
187 * as it was upon the first call to term_columns()).
189 fprintf(stderr, "\r%*s\r", term_columns(), "");
192 * On non-dumb terminals use an escape sequence to clear
193 * the whole line, no matter how wide the terminal.
195 fputs("\r\033[K", stderr);
199 * How many columns do we need to show this number in decimal?
201 int decimal_width(uintmax_t number)
205 for (width = 1; number >= 10; width++)
210 struct pager_command_config_data {
216 static int pager_command_config(const char *var, const char *value, void *vdata)
218 struct pager_command_config_data *data = vdata;
221 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
222 int b = git_parse_maybe_bool(value);
227 data->value = xstrdup(value);
234 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
235 int check_pager_config(const char *cmd)
237 struct pager_command_config_data data;
243 read_early_config(pager_command_config, &data);
246 pager_program = data.value;