pretty: fix adding linefeed when placeholder is not expanded
[git/debian.git] / pager.c
blobee435de67562dc5a3142a153e41672c292924640
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 wait_for_pager(int in_signal)
16 if (!in_signal) {
17 fflush(stdout);
18 fflush(stderr);
20 /* signal EOF to pager */
21 close(1);
22 close(2);
23 if (in_signal)
24 finish_command_in_signal(&pager_process);
25 else
26 finish_command(&pager_process);
29 static void wait_for_pager_atexit(void)
31 wait_for_pager(0);
34 static void wait_for_pager_signal(int signo)
36 wait_for_pager(1);
37 sigchain_pop(signo);
38 raise(signo);
41 static int core_pager_config(const char *var, const char *value, void *data)
43 if (!strcmp(var, "core.pager"))
44 return git_config_string(&pager_program, var, value);
45 return 0;
48 const char *git_pager(int stdout_is_tty)
50 const char *pager;
52 if (!stdout_is_tty)
53 return NULL;
55 pager = getenv("GIT_PAGER");
56 if (!pager) {
57 if (!pager_program)
58 read_early_config(core_pager_config, NULL);
59 pager = pager_program;
61 if (!pager)
62 pager = getenv("PAGER");
63 if (!pager)
64 pager = DEFAULT_PAGER;
65 if (!*pager || !strcmp(pager, "cat"))
66 pager = NULL;
68 return pager;
71 static void setup_pager_env(struct strvec *env)
73 const char **argv;
74 int i;
75 char *pager_env = xstrdup(PAGER_ENV);
76 int n = split_cmdline(pager_env, &argv);
78 if (n < 0)
79 die("malformed build-time PAGER_ENV: %s",
80 split_cmdline_strerror(n));
82 for (i = 0; i < n; i++) {
83 char *cp = strchr(argv[i], '=');
85 if (!cp)
86 die("malformed build-time PAGER_ENV");
88 *cp = '\0';
89 if (!getenv(argv[i])) {
90 *cp = '=';
91 strvec_push(env, argv[i]);
94 free(pager_env);
95 free(argv);
98 void prepare_pager_args(struct child_process *pager_process, const char *pager)
100 strvec_push(&pager_process->args, pager);
101 pager_process->use_shell = 1;
102 setup_pager_env(&pager_process->env_array);
103 pager_process->trace2_child_class = "pager";
106 void setup_pager(void)
108 const char *pager = git_pager(isatty(1));
110 if (!pager)
111 return;
114 * After we redirect standard output, we won't be able to use an ioctl
115 * to get the terminal size. Let's grab it now, and then set $COLUMNS
116 * to communicate it to any sub-processes.
119 char buf[64];
120 xsnprintf(buf, sizeof(buf), "%d", term_columns());
121 setenv("COLUMNS", buf, 0);
124 setenv("GIT_PAGER_IN_USE", "true", 1);
126 /* spawn the pager */
127 prepare_pager_args(&pager_process, pager);
128 pager_process.in = -1;
129 strvec_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
130 if (start_command(&pager_process))
131 return;
133 /* original process continues, but writes to the pipe */
134 dup2(pager_process.in, 1);
135 if (isatty(2))
136 dup2(pager_process.in, 2);
137 close(pager_process.in);
139 /* this makes sure that the parent terminates after the pager */
140 sigchain_push_common(wait_for_pager_signal);
141 atexit(wait_for_pager_atexit);
144 int pager_in_use(void)
146 return git_env_bool("GIT_PAGER_IN_USE", 0);
150 * Return cached value (if set) or $COLUMNS environment variable (if
151 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
152 * and default to 80 if all else fails.
154 int term_columns(void)
156 static int term_columns_at_startup;
158 char *col_string;
159 int n_cols;
161 if (term_columns_at_startup)
162 return term_columns_at_startup;
164 term_columns_at_startup = 80;
166 col_string = getenv("COLUMNS");
167 if (col_string && (n_cols = atoi(col_string)) > 0)
168 term_columns_at_startup = n_cols;
169 #ifdef TIOCGWINSZ
170 else {
171 struct winsize ws;
172 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
173 term_columns_at_startup = ws.ws_col;
175 #endif
177 return term_columns_at_startup;
181 * Clear the entire line, leave cursor in first column.
183 void term_clear_line(void)
185 if (is_terminal_dumb())
187 * Fall back to print a terminal width worth of space
188 * characters (hoping that the terminal is still as wide
189 * as it was upon the first call to term_columns()).
191 fprintf(stderr, "\r%*s\r", term_columns(), "");
192 else
194 * On non-dumb terminals use an escape sequence to clear
195 * the whole line, no matter how wide the terminal.
197 fputs("\r\033[K", stderr);
201 * How many columns do we need to show this number in decimal?
203 int decimal_width(uintmax_t number)
205 int width;
207 for (width = 1; number >= 10; width++)
208 number /= 10;
209 return width;
212 struct pager_command_config_data {
213 const char *cmd;
214 int want;
215 char *value;
218 static int pager_command_config(const char *var, const char *value, void *vdata)
220 struct pager_command_config_data *data = vdata;
221 const char *cmd;
223 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
224 int b = git_parse_maybe_bool(value);
225 if (b >= 0)
226 data->want = b;
227 else {
228 data->want = 1;
229 data->value = xstrdup(value);
233 return 0;
236 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
237 int check_pager_config(const char *cmd)
239 struct pager_command_config_data data;
241 data.cmd = cmd;
242 data.want = -1;
243 data.value = NULL;
245 read_early_config(pager_command_config, &data);
247 if (data.value)
248 pager_program = data.value;
249 return data.want;