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