branch: description for non-existent branch errors
[alt-git.git] / pager.c
blobb66bbff2785cb6ed032edb4f0d9cd68f6a841c36
1 #include "cache.h"
2 #include "config.h"
3 #include "run-command.h"
4 #include "sigchain.h"
5 #include "alias.h"
7 #ifndef DEFAULT_PAGER
8 #define DEFAULT_PAGER "less"
9 #endif
11 static struct child_process pager_process;
12 static const char *pager_program;
14 /* Is the value coming back from term_columns() just a guess? */
15 static int term_columns_guessed;
18 static void close_pager_fds(void)
20 /* signal EOF to pager */
21 close(1);
22 close(2);
25 static void wait_for_pager_atexit(void)
27 fflush(stdout);
28 fflush(stderr);
29 close_pager_fds();
30 finish_command(&pager_process);
33 static void wait_for_pager_signal(int signo)
35 close_pager_fds();
36 finish_command_in_signal(&pager_process);
37 sigchain_pop(signo);
38 raise(signo);
41 static int core_pager_config(const char *var, const char *value,
42 void *data UNUSED)
44 if (!strcmp(var, "core.pager"))
45 return git_config_string(&pager_program, var, value);
46 return 0;
49 const char *git_pager(int stdout_is_tty)
51 const char *pager;
53 if (!stdout_is_tty)
54 return NULL;
56 pager = getenv("GIT_PAGER");
57 if (!pager) {
58 if (!pager_program)
59 read_early_config(core_pager_config, NULL);
60 pager = pager_program;
62 if (!pager)
63 pager = getenv("PAGER");
64 if (!pager)
65 pager = DEFAULT_PAGER;
66 if (!*pager || !strcmp(pager, "cat"))
67 pager = NULL;
69 return pager;
72 static void setup_pager_env(struct strvec *env)
74 const char **argv;
75 int i;
76 char *pager_env = xstrdup(PAGER_ENV);
77 int n = split_cmdline(pager_env, &argv);
79 if (n < 0)
80 die("malformed build-time PAGER_ENV: %s",
81 split_cmdline_strerror(n));
83 for (i = 0; i < n; i++) {
84 char *cp = strchr(argv[i], '=');
86 if (!cp)
87 die("malformed build-time PAGER_ENV");
89 *cp = '\0';
90 if (!getenv(argv[i])) {
91 *cp = '=';
92 strvec_push(env, argv[i]);
95 free(pager_env);
96 free(argv);
99 void prepare_pager_args(struct child_process *pager_process, const char *pager)
101 strvec_push(&pager_process->args, pager);
102 pager_process->use_shell = 1;
103 setup_pager_env(&pager_process->env);
104 pager_process->trace2_child_class = "pager";
107 void setup_pager(void)
109 const char *pager = git_pager(isatty(1));
111 if (!pager)
112 return;
115 * After we redirect standard output, we won't be able to use an ioctl
116 * to get the terminal size. Let's grab it now, and then set $COLUMNS
117 * to communicate it to any sub-processes.
120 char buf[64];
121 xsnprintf(buf, sizeof(buf), "%d", term_columns());
122 if (!term_columns_guessed)
123 setenv("COLUMNS", buf, 0);
126 setenv("GIT_PAGER_IN_USE", "true", 1);
128 child_process_init(&pager_process);
130 /* spawn the pager */
131 prepare_pager_args(&pager_process, pager);
132 pager_process.in = -1;
133 strvec_push(&pager_process.env, "GIT_PAGER_IN_USE");
134 if (start_command(&pager_process))
135 return;
137 /* original process continues, but writes to the pipe */
138 dup2(pager_process.in, 1);
139 if (isatty(2))
140 dup2(pager_process.in, 2);
141 close(pager_process.in);
143 /* this makes sure that the parent terminates after the pager */
144 sigchain_push_common(wait_for_pager_signal);
145 atexit(wait_for_pager_atexit);
148 int pager_in_use(void)
150 return git_env_bool("GIT_PAGER_IN_USE", 0);
154 * Return cached value (if set) or $COLUMNS environment variable (if
155 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
156 * and default to 80 if all else fails.
158 int term_columns(void)
160 static int term_columns_at_startup;
162 char *col_string;
163 int n_cols;
165 if (term_columns_at_startup)
166 return term_columns_at_startup;
168 term_columns_at_startup = 80;
169 term_columns_guessed = 1;
171 col_string = getenv("COLUMNS");
172 if (col_string && (n_cols = atoi(col_string)) > 0) {
173 term_columns_at_startup = n_cols;
174 term_columns_guessed = 0;
176 #ifdef TIOCGWINSZ
177 else {
178 struct winsize ws;
179 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col) {
180 term_columns_at_startup = ws.ws_col;
181 term_columns_guessed = 0;
184 #endif
186 return term_columns_at_startup;
190 * Clear the entire line, leave cursor in first column.
192 void term_clear_line(void)
194 if (is_terminal_dumb())
196 * Fall back to print a terminal width worth of space
197 * characters (hoping that the terminal is still as wide
198 * as it was upon the first call to term_columns()).
200 fprintf(stderr, "\r%*s\r", term_columns(), "");
201 else
203 * On non-dumb terminals use an escape sequence to clear
204 * the whole line, no matter how wide the terminal.
206 fputs("\r\033[K", stderr);
210 * How many columns do we need to show this number in decimal?
212 int decimal_width(uintmax_t number)
214 int width;
216 for (width = 1; number >= 10; width++)
217 number /= 10;
218 return width;
221 struct pager_command_config_data {
222 const char *cmd;
223 int want;
224 char *value;
227 static int pager_command_config(const char *var, const char *value, void *vdata)
229 struct pager_command_config_data *data = vdata;
230 const char *cmd;
232 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
233 int b = git_parse_maybe_bool(value);
234 if (b >= 0)
235 data->want = b;
236 else {
237 data->want = 1;
238 data->value = xstrdup(value);
242 return 0;
245 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
246 int check_pager_config(const char *cmd)
248 struct pager_command_config_data data;
250 data.cmd = cmd;
251 data.want = -1;
252 data.value = NULL;
254 read_early_config(pager_command_config, &data);
256 if (data.value)
257 pager_program = data.value;
258 return data.want;