]> git.ipfire.org Git - thirdparty/git.git/blob - pager.c
editor: move editor-related functions and declarations into common file
[thirdparty/git.git] / pager.c
1 #include "cache.h"
2 #include "config.h"
3 #include "editor.h"
4 #include "run-command.h"
5 #include "sigchain.h"
6 #include "alias.h"
7
8 #ifndef DEFAULT_PAGER
9 #define DEFAULT_PAGER "less"
10 #endif
11
12 static struct child_process pager_process;
13 static const char *pager_program;
14
15 /* Is the value coming back from term_columns() just a guess? */
16 static int term_columns_guessed;
17
18
19 static void close_pager_fds(void)
20 {
21 /* signal EOF to pager */
22 close(1);
23 close(2);
24 }
25
26 static void wait_for_pager_atexit(void)
27 {
28 fflush(stdout);
29 fflush(stderr);
30 close_pager_fds();
31 finish_command(&pager_process);
32 }
33
34 static void wait_for_pager_signal(int signo)
35 {
36 close_pager_fds();
37 finish_command_in_signal(&pager_process);
38 sigchain_pop(signo);
39 raise(signo);
40 }
41
42 static int core_pager_config(const char *var, const char *value,
43 void *data UNUSED)
44 {
45 if (!strcmp(var, "core.pager"))
46 return git_config_string(&pager_program, var, value);
47 return 0;
48 }
49
50 const char *git_pager(int stdout_is_tty)
51 {
52 const char *pager;
53
54 if (!stdout_is_tty)
55 return NULL;
56
57 pager = getenv("GIT_PAGER");
58 if (!pager) {
59 if (!pager_program)
60 read_early_config(core_pager_config, NULL);
61 pager = pager_program;
62 }
63 if (!pager)
64 pager = getenv("PAGER");
65 if (!pager)
66 pager = DEFAULT_PAGER;
67 if (!*pager || !strcmp(pager, "cat"))
68 pager = NULL;
69
70 return pager;
71 }
72
73 static void setup_pager_env(struct strvec *env)
74 {
75 const char **argv;
76 int i;
77 char *pager_env = xstrdup(PAGER_ENV);
78 int n = split_cmdline(pager_env, &argv);
79
80 if (n < 0)
81 die("malformed build-time PAGER_ENV: %s",
82 split_cmdline_strerror(n));
83
84 for (i = 0; i < n; i++) {
85 char *cp = strchr(argv[i], '=');
86
87 if (!cp)
88 die("malformed build-time PAGER_ENV");
89
90 *cp = '\0';
91 if (!getenv(argv[i])) {
92 *cp = '=';
93 strvec_push(env, argv[i]);
94 }
95 }
96 free(pager_env);
97 free(argv);
98 }
99
100 void prepare_pager_args(struct child_process *pager_process, const char *pager)
101 {
102 strvec_push(&pager_process->args, pager);
103 pager_process->use_shell = 1;
104 setup_pager_env(&pager_process->env);
105 pager_process->trace2_child_class = "pager";
106 }
107
108 void setup_pager(void)
109 {
110 const char *pager = git_pager(isatty(1));
111
112 if (!pager)
113 return;
114
115 /*
116 * After we redirect standard output, we won't be able to use an ioctl
117 * to get the terminal size. Let's grab it now, and then set $COLUMNS
118 * to communicate it to any sub-processes.
119 */
120 {
121 char buf[64];
122 xsnprintf(buf, sizeof(buf), "%d", term_columns());
123 if (!term_columns_guessed)
124 setenv("COLUMNS", buf, 0);
125 }
126
127 setenv("GIT_PAGER_IN_USE", "true", 1);
128
129 child_process_init(&pager_process);
130
131 /* spawn the pager */
132 prepare_pager_args(&pager_process, pager);
133 pager_process.in = -1;
134 strvec_push(&pager_process.env, "GIT_PAGER_IN_USE");
135 if (start_command(&pager_process))
136 return;
137
138 /* original process continues, but writes to the pipe */
139 dup2(pager_process.in, 1);
140 if (isatty(2))
141 dup2(pager_process.in, 2);
142 close(pager_process.in);
143
144 /* this makes sure that the parent terminates after the pager */
145 sigchain_push_common(wait_for_pager_signal);
146 atexit(wait_for_pager_atexit);
147 }
148
149 int pager_in_use(void)
150 {
151 return git_env_bool("GIT_PAGER_IN_USE", 0);
152 }
153
154 /*
155 * Return cached value (if set) or $COLUMNS environment variable (if
156 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
157 * and default to 80 if all else fails.
158 */
159 int term_columns(void)
160 {
161 static int term_columns_at_startup;
162
163 char *col_string;
164 int n_cols;
165
166 if (term_columns_at_startup)
167 return term_columns_at_startup;
168
169 term_columns_at_startup = 80;
170 term_columns_guessed = 1;
171
172 col_string = getenv("COLUMNS");
173 if (col_string && (n_cols = atoi(col_string)) > 0) {
174 term_columns_at_startup = n_cols;
175 term_columns_guessed = 0;
176 }
177 #ifdef TIOCGWINSZ
178 else {
179 struct winsize ws;
180 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col) {
181 term_columns_at_startup = ws.ws_col;
182 term_columns_guessed = 0;
183 }
184 }
185 #endif
186
187 return term_columns_at_startup;
188 }
189
190 /*
191 * Clear the entire line, leave cursor in first column.
192 */
193 void term_clear_line(void)
194 {
195 if (is_terminal_dumb())
196 /*
197 * Fall back to print a terminal width worth of space
198 * characters (hoping that the terminal is still as wide
199 * as it was upon the first call to term_columns()).
200 */
201 fprintf(stderr, "\r%*s\r", term_columns(), "");
202 else
203 /*
204 * On non-dumb terminals use an escape sequence to clear
205 * the whole line, no matter how wide the terminal.
206 */
207 fputs("\r\033[K", stderr);
208 }
209
210 /*
211 * How many columns do we need to show this number in decimal?
212 */
213 int decimal_width(uintmax_t number)
214 {
215 int width;
216
217 for (width = 1; number >= 10; width++)
218 number /= 10;
219 return width;
220 }
221
222 struct pager_command_config_data {
223 const char *cmd;
224 int want;
225 char *value;
226 };
227
228 static int pager_command_config(const char *var, const char *value, void *vdata)
229 {
230 struct pager_command_config_data *data = vdata;
231 const char *cmd;
232
233 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
234 int b = git_parse_maybe_bool(value);
235 if (b >= 0)
236 data->want = b;
237 else {
238 data->want = 1;
239 data->value = xstrdup(value);
240 }
241 }
242
243 return 0;
244 }
245
246 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
247 int check_pager_config(const char *cmd)
248 {
249 struct pager_command_config_data data;
250
251 data.cmd = cmd;
252 data.want = -1;
253 data.value = NULL;
254
255 read_early_config(pager_command_config, &data);
256
257 if (data.value)
258 pager_program = data.value;
259 return data.want;
260 }