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