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