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