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.
116 xsnprintf(buf
, sizeof(buf
), "%d", term_columns());
117 setenv("COLUMNS", buf
, 0);
120 setenv("GIT_PAGER_IN_USE", "true", 1);
122 /* spawn the pager */
123 prepare_pager_args(&pager_process
, pager
);
124 pager_process
.in
= -1;
125 strvec_push(&pager_process
.env_array
, "GIT_PAGER_IN_USE");
126 if (start_command(&pager_process
))
129 /* original process continues, but writes to the pipe */
130 dup2(pager_process
.in
, 1);
132 dup2(pager_process
.in
, 2);
133 close(pager_process
.in
);
135 /* this makes sure that the parent terminates after the pager */
136 sigchain_push_common(wait_for_pager_signal
);
137 atexit(wait_for_pager_atexit
);
140 int pager_in_use(void)
142 return git_env_bool("GIT_PAGER_IN_USE", 0);
146 * Return cached value (if set) or $COLUMNS environment variable (if
147 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
148 * and default to 80 if all else fails.
150 int term_columns(void)
152 static int term_columns_at_startup
;
157 if (term_columns_at_startup
)
158 return term_columns_at_startup
;
160 term_columns_at_startup
= 80;
162 col_string
= getenv("COLUMNS");
163 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
164 term_columns_at_startup
= n_cols
;
168 if (!ioctl(1, TIOCGWINSZ
, &ws
) && ws
.ws_col
)
169 term_columns_at_startup
= ws
.ws_col
;
173 return term_columns_at_startup
;
177 * Clear the entire line, leave cursor in first column.
179 void term_clear_line(void)
181 if (is_terminal_dumb())
183 * Fall back to print a terminal width worth of space
184 * characters (hoping that the terminal is still as wide
185 * as it was upon the first call to term_columns()).
187 fprintf(stderr
, "\r%*s\r", term_columns(), "");
190 * On non-dumb terminals use an escape sequence to clear
191 * the whole line, no matter how wide the terminal.
193 fputs("\r\033[K", stderr
);
197 * How many columns do we need to show this number in decimal?
199 int decimal_width(uintmax_t number
)
203 for (width
= 1; number
>= 10; width
++)
208 struct pager_command_config_data
{
214 static int pager_command_config(const char *var
, const char *value
, void *vdata
)
216 struct pager_command_config_data
*data
= vdata
;
219 if (skip_prefix(var
, "pager.", &cmd
) && !strcmp(cmd
, data
->cmd
)) {
220 int b
= git_parse_maybe_bool(value
);
225 data
->value
= xstrdup(value
);
232 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
233 int check_pager_config(const char *cmd
)
235 struct pager_command_config_data data
;
241 read_early_config(pager_command_config
, &data
);
244 pager_program
= data
.value
;