1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
7 #include "run-command.h"
11 int pager_use_color
= 1;
14 #define DEFAULT_PAGER "less"
17 static struct child_process pager_process
;
18 static char *pager_program
;
19 static int old_fd1
= -1, old_fd2
= -1;
21 /* Is the value coming back from term_columns() just a guess? */
22 static int term_columns_guessed
;
25 static void close_pager_fds(void)
27 /* signal EOF to pager */
33 static void finish_pager(void)
38 finish_command(&pager_process
);
41 static void wait_for_pager_atexit(void)
49 void wait_for_pager(void)
55 sigchain_pop_common();
56 unsetenv("GIT_PAGER_IN_USE");
67 static void wait_for_pager_signal(int signo
)
73 finish_command_in_signal(&pager_process
);
78 static int core_pager_config(const char *var
, const char *value
,
79 const struct config_context
*ctx UNUSED
,
82 if (!strcmp(var
, "core.pager"))
83 return git_config_string(&pager_program
, var
, value
);
87 const char *git_pager(int stdout_is_tty
)
94 pager
= getenv("GIT_PAGER");
97 read_early_config(the_repository
,
98 core_pager_config
, NULL
);
99 pager
= pager_program
;
102 pager
= getenv("PAGER");
104 pager
= DEFAULT_PAGER
;
105 if (!*pager
|| !strcmp(pager
, "cat"))
111 static void setup_pager_env(struct strvec
*env
)
115 char *pager_env
= xstrdup(PAGER_ENV
);
116 int n
= split_cmdline(pager_env
, &argv
);
119 die("malformed build-time PAGER_ENV: %s",
120 split_cmdline_strerror(n
));
122 for (i
= 0; i
< n
; i
++) {
123 char *cp
= strchr(argv
[i
], '=');
126 die("malformed build-time PAGER_ENV");
129 if (!getenv(argv
[i
])) {
131 strvec_push(env
, argv
[i
]);
138 void prepare_pager_args(struct child_process
*pager_process
, const char *pager
)
140 strvec_push(&pager_process
->args
, pager
);
141 pager_process
->use_shell
= 1;
142 setup_pager_env(&pager_process
->env
);
143 pager_process
->trace2_child_class
= "pager";
146 void setup_pager(void)
149 const char *pager
= git_pager(isatty(1));
155 * After we redirect standard output, we won't be able to use an ioctl
156 * to get the terminal size. Let's grab it now, and then set $COLUMNS
157 * to communicate it to any sub-processes.
161 xsnprintf(buf
, sizeof(buf
), "%d", term_columns());
162 if (!term_columns_guessed
)
163 setenv("COLUMNS", buf
, 0);
166 setenv("GIT_PAGER_IN_USE", "true", 1);
168 child_process_init(&pager_process
);
170 /* spawn the pager */
171 prepare_pager_args(&pager_process
, pager
);
172 pager_process
.in
= -1;
173 strvec_push(&pager_process
.env
, "GIT_PAGER_IN_USE");
174 if (start_command(&pager_process
))
175 die("unable to execute pager '%s'", pager
);
177 /* original process continues, but writes to the pipe */
179 dup2(pager_process
.in
, 1);
182 dup2(pager_process
.in
, 2);
184 close(pager_process
.in
);
186 sigchain_push_common(wait_for_pager_signal
);
190 atexit(wait_for_pager_atexit
);
194 int pager_in_use(void)
196 return git_env_bool("GIT_PAGER_IN_USE", 0);
200 * Return cached value (if set) or $COLUMNS environment variable (if
201 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
202 * and default to 80 if all else fails.
204 int term_columns(void)
206 static int term_columns_at_startup
;
211 if (term_columns_at_startup
)
212 return term_columns_at_startup
;
214 term_columns_at_startup
= 80;
215 term_columns_guessed
= 1;
217 col_string
= getenv("COLUMNS");
218 if (col_string
&& (n_cols
= atoi(col_string
)) > 0) {
219 term_columns_at_startup
= n_cols
;
220 term_columns_guessed
= 0;
225 if (!ioctl(1, TIOCGWINSZ
, &ws
) && ws
.ws_col
) {
226 term_columns_at_startup
= ws
.ws_col
;
227 term_columns_guessed
= 0;
232 return term_columns_at_startup
;
236 * Clear the entire line, leave cursor in first column.
238 void term_clear_line(void)
242 if (is_terminal_dumb())
244 * Fall back to print a terminal width worth of space
245 * characters (hoping that the terminal is still as wide
246 * as it was upon the first call to term_columns()).
248 fprintf(stderr
, "\r%*s\r", term_columns(), "");
251 * On non-dumb terminals use an escape sequence to clear
252 * the whole line, no matter how wide the terminal.
254 fputs("\r\033[K", stderr
);
258 * How many columns do we need to show this number in decimal?
260 int decimal_width(uintmax_t number
)
264 for (width
= 1; number
>= 10; width
++)
269 struct pager_command_config_data
{
275 static int pager_command_config(const char *var
, const char *value
,
276 const struct config_context
*ctx UNUSED
,
279 struct pager_command_config_data
*data
= vdata
;
282 if (skip_prefix(var
, "pager.", &cmd
) && !strcmp(cmd
, data
->cmd
)) {
283 int b
= git_parse_maybe_bool(value
);
288 data
->value
= xstrdup(value
);
295 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
296 int check_pager_config(const char *cmd
)
298 struct pager_command_config_data data
;
304 read_early_config(the_repository
, pager_command_config
, &data
);
307 pager_program
= data
.value
;