]>
Commit | Line | Data |
---|---|---|
1 | #include "cache.h" | |
2 | #include "run-command.h" | |
3 | #include "sigchain.h" | |
4 | ||
5 | #ifndef DEFAULT_PAGER | |
6 | #define DEFAULT_PAGER "less" | |
7 | #endif | |
8 | ||
9 | /* | |
10 | * This is split up from the rest of git so that we can do | |
11 | * something different on Windows. | |
12 | */ | |
13 | ||
14 | static struct child_process pager_process = CHILD_PROCESS_INIT; | |
15 | ||
16 | static void wait_for_pager(int in_signal) | |
17 | { | |
18 | if (!in_signal) { | |
19 | fflush(stdout); | |
20 | fflush(stderr); | |
21 | } | |
22 | /* signal EOF to pager */ | |
23 | close(1); | |
24 | close(2); | |
25 | if (in_signal) | |
26 | finish_command_in_signal(&pager_process); | |
27 | else | |
28 | finish_command(&pager_process); | |
29 | } | |
30 | ||
31 | static void wait_for_pager_atexit(void) | |
32 | { | |
33 | wait_for_pager(0); | |
34 | } | |
35 | ||
36 | static void wait_for_pager_signal(int signo) | |
37 | { | |
38 | wait_for_pager(1); | |
39 | sigchain_pop(signo); | |
40 | raise(signo); | |
41 | } | |
42 | ||
43 | const char *git_pager(int stdout_is_tty) | |
44 | { | |
45 | const char *pager; | |
46 | ||
47 | if (!stdout_is_tty) | |
48 | return NULL; | |
49 | ||
50 | pager = getenv("GIT_PAGER"); | |
51 | if (!pager) { | |
52 | if (!pager_program) | |
53 | git_config(git_default_config, NULL); | |
54 | pager = pager_program; | |
55 | } | |
56 | if (!pager) | |
57 | pager = getenv("PAGER"); | |
58 | if (!pager) | |
59 | pager = DEFAULT_PAGER; | |
60 | if (!*pager || !strcmp(pager, "cat")) | |
61 | pager = NULL; | |
62 | ||
63 | return pager; | |
64 | } | |
65 | ||
66 | static void setup_pager_env(struct argv_array *env) | |
67 | { | |
68 | const char **argv; | |
69 | int i; | |
70 | char *pager_env = xstrdup(PAGER_ENV); | |
71 | int n = split_cmdline(pager_env, &argv); | |
72 | ||
73 | if (n < 0) | |
74 | die("malformed build-time PAGER_ENV: %s", | |
75 | split_cmdline_strerror(n)); | |
76 | ||
77 | for (i = 0; i < n; i++) { | |
78 | char *cp = strchr(argv[i], '='); | |
79 | ||
80 | if (!cp) | |
81 | die("malformed build-time PAGER_ENV"); | |
82 | ||
83 | *cp = '\0'; | |
84 | if (!getenv(argv[i])) { | |
85 | *cp = '='; | |
86 | argv_array_push(env, argv[i]); | |
87 | } | |
88 | } | |
89 | free(pager_env); | |
90 | free(argv); | |
91 | } | |
92 | ||
93 | void prepare_pager_args(struct child_process *pager_process, const char *pager) | |
94 | { | |
95 | argv_array_push(&pager_process->args, pager); | |
96 | pager_process->use_shell = 1; | |
97 | setup_pager_env(&pager_process->env_array); | |
98 | } | |
99 | ||
100 | void setup_pager(void) | |
101 | { | |
102 | const char *pager = git_pager(isatty(1)); | |
103 | ||
104 | if (!pager) | |
105 | return; | |
106 | ||
107 | /* | |
108 | * force computing the width of the terminal before we redirect | |
109 | * the standard output to the pager. | |
110 | */ | |
111 | (void) term_columns(); | |
112 | ||
113 | setenv("GIT_PAGER_IN_USE", "true", 1); | |
114 | ||
115 | /* spawn the pager */ | |
116 | prepare_pager_args(&pager_process, pager); | |
117 | pager_process.in = -1; | |
118 | argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE"); | |
119 | if (start_command(&pager_process)) | |
120 | return; | |
121 | ||
122 | /* original process continues, but writes to the pipe */ | |
123 | dup2(pager_process.in, 1); | |
124 | if (isatty(2)) | |
125 | dup2(pager_process.in, 2); | |
126 | close(pager_process.in); | |
127 | ||
128 | /* this makes sure that the parent terminates after the pager */ | |
129 | sigchain_push_common(wait_for_pager_signal); | |
130 | atexit(wait_for_pager_atexit); | |
131 | } | |
132 | ||
133 | int pager_in_use(void) | |
134 | { | |
135 | const char *env; | |
136 | env = getenv("GIT_PAGER_IN_USE"); | |
137 | return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0; | |
138 | } | |
139 | ||
140 | /* | |
141 | * Return cached value (if set) or $COLUMNS environment variable (if | |
142 | * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive), | |
143 | * and default to 80 if all else fails. | |
144 | */ | |
145 | int term_columns(void) | |
146 | { | |
147 | static int term_columns_at_startup; | |
148 | ||
149 | char *col_string; | |
150 | int n_cols; | |
151 | ||
152 | if (term_columns_at_startup) | |
153 | return term_columns_at_startup; | |
154 | ||
155 | term_columns_at_startup = 80; | |
156 | ||
157 | col_string = getenv("COLUMNS"); | |
158 | if (col_string && (n_cols = atoi(col_string)) > 0) | |
159 | term_columns_at_startup = n_cols; | |
160 | #ifdef TIOCGWINSZ | |
161 | else { | |
162 | struct winsize ws; | |
163 | if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col) | |
164 | term_columns_at_startup = ws.ws_col; | |
165 | } | |
166 | #endif | |
167 | ||
168 | return term_columns_at_startup; | |
169 | } | |
170 | ||
171 | /* | |
172 | * How many columns do we need to show this number in decimal? | |
173 | */ | |
174 | int decimal_width(uintmax_t number) | |
175 | { | |
176 | int width; | |
177 | ||
178 | for (width = 1; number >= 10; width++) | |
179 | number /= 10; | |
180 | return width; | |
181 | } | |
182 | ||
183 | /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */ | |
184 | int check_pager_config(const char *cmd) | |
185 | { | |
186 | int want = -1; | |
187 | struct strbuf key = STRBUF_INIT; | |
188 | const char *value = NULL; | |
189 | strbuf_addf(&key, "pager.%s", cmd); | |
190 | if (git_config_key_is_valid(key.buf) && | |
191 | !git_config_get_value(key.buf, &value)) { | |
192 | int b = git_config_maybe_bool(key.buf, value); | |
193 | if (b >= 0) | |
194 | want = b; | |
195 | else { | |
196 | want = 1; | |
197 | pager_program = xstrdup(value); | |
198 | } | |
199 | } | |
200 | strbuf_release(&key); | |
201 | return want; | |
202 | } |