The eighth batch
[git.git] / pager.c
blob3d37dd7adaa27ed1b6bc3952711b11ff6dfc0f56
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 = CHILD_PROCESS_INIT;
12 static const char *pager_program;
14 static void close_pager_fds(void)
16 /* signal EOF to pager */
17 close(1);
18 close(2);
21 static void wait_for_pager_atexit(void)
23 fflush(stdout);
24 fflush(stderr);
25 close_pager_fds();
26 finish_command(&pager_process);
29 static void wait_for_pager_signal(int signo)
31 close_pager_fds();
32 finish_command_in_signal(&pager_process);
33 sigchain_pop(signo);
34 raise(signo);
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);
41 return 0;
44 const char *git_pager(int stdout_is_tty)
46 const char *pager;
48 if (!stdout_is_tty)
49 return NULL;
51 pager = getenv("GIT_PAGER");
52 if (!pager) {
53 if (!pager_program)
54 read_early_config(core_pager_config, NULL);
55 pager = pager_program;
57 if (!pager)
58 pager = getenv("PAGER");
59 if (!pager)
60 pager = DEFAULT_PAGER;
61 if (!*pager || !strcmp(pager, "cat"))
62 pager = NULL;
64 return pager;
67 static void setup_pager_env(struct strvec *env)
69 const char **argv;
70 int i;
71 char *pager_env = xstrdup(PAGER_ENV);
72 int n = split_cmdline(pager_env, &argv);
74 if (n < 0)
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], '=');
81 if (!cp)
82 die("malformed build-time PAGER_ENV");
84 *cp = '\0';
85 if (!getenv(argv[i])) {
86 *cp = '=';
87 strvec_push(env, argv[i]);
90 free(pager_env);
91 free(argv);
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));
106 if (!pager)
107 return;
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.
115 char buf[64];
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))
127 return;
129 /* original process continues, but writes to the pipe */
130 dup2(pager_process.in, 1);
131 if (isatty(2))
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;
154 char *col_string;
155 int n_cols;
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;
165 #ifdef TIOCGWINSZ
166 else {
167 struct winsize ws;
168 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
169 term_columns_at_startup = ws.ws_col;
171 #endif
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(), "");
188 else
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)
201 int width;
203 for (width = 1; number >= 10; width++)
204 number /= 10;
205 return width;
208 struct pager_command_config_data {
209 const char *cmd;
210 int want;
211 char *value;
214 static int pager_command_config(const char *var, const char *value, void *vdata)
216 struct pager_command_config_data *data = vdata;
217 const char *cmd;
219 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
220 int b = git_parse_maybe_bool(value);
221 if (b >= 0)
222 data->want = b;
223 else {
224 data->want = 1;
225 data->value = xstrdup(value);
229 return 0;
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;
237 data.cmd = cmd;
238 data.want = -1;
239 data.value = NULL;
241 read_early_config(pager_command_config, &data);
243 if (data.value)
244 pager_program = data.value;
245 return data.want;