]>
Commit | Line | Data |
---|---|---|
f67b45f8 | 1 | #include "cache.h" |
ea27a18c | 2 | #include "run-command.h" |
a3da8821 | 3 | #include "sigchain.h" |
f67b45f8 | 4 | |
a3d023d0 JH |
5 | #ifndef DEFAULT_PAGER |
6 | #define DEFAULT_PAGER "less" | |
7 | #endif | |
8 | ||
f67b45f8 | 9 | /* |
bfdd9ffd JS |
10 | * This is split up from the rest of git so that we can do |
11 | * something different on Windows. | |
f67b45f8 LT |
12 | */ |
13 | ||
d3180279 | 14 | static struct child_process pager_process = CHILD_PROCESS_INIT; |
ea27a18c | 15 | |
507d7804 | 16 | static void wait_for_pager(int in_signal) |
bfdd9ffd | 17 | { |
507d7804 TI |
18 | if (!in_signal) { |
19 | fflush(stdout); | |
20 | fflush(stderr); | |
21 | } | |
bfdd9ffd JS |
22 | /* signal EOF to pager */ |
23 | close(1); | |
24 | close(2); | |
507d7804 TI |
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); | |
bfdd9ffd | 34 | } |
f67b45f8 | 35 | |
a3da8821 JK |
36 | static void wait_for_pager_signal(int signo) |
37 | { | |
507d7804 | 38 | wait_for_pager(1); |
a3da8821 JK |
39 | sigchain_pop(signo); |
40 | raise(signo); | |
41 | } | |
42 | ||
64778d24 | 43 | const char *git_pager(int stdout_is_tty) |
f67b45f8 | 44 | { |
63618245 | 45 | const char *pager; |
f67b45f8 | 46 | |
64778d24 | 47 | if (!stdout_is_tty) |
63618245 JN |
48 | return NULL; |
49 | ||
50 | pager = getenv("GIT_PAGER"); | |
cad3a205 JH |
51 | if (!pager) { |
52 | if (!pager_program) | |
ef90d6d4 | 53 | git_config(git_default_config, NULL); |
54adf370 | 54 | pager = pager_program; |
cad3a205 | 55 | } |
c27d205a ML |
56 | if (!pager) |
57 | pager = getenv("PAGER"); | |
402461aa | 58 | if (!pager) |
a3d023d0 | 59 | pager = DEFAULT_PAGER; |
ed016612 | 60 | if (!*pager || !strcmp(pager, "cat")) |
63618245 JN |
61 | pager = NULL; |
62 | ||
63 | return pager; | |
64 | } | |
65 | ||
995bc22d EW |
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 | ||
3e3a4a41 JH |
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; | |
995bc22d | 97 | setup_pager_env(&pager_process->env_array); |
3e3a4a41 JH |
98 | } |
99 | ||
63618245 JN |
100 | void setup_pager(void) |
101 | { | |
64778d24 | 102 | const char *pager = git_pager(isatty(1)); |
63618245 | 103 | |
c0459ca4 | 104 | if (!pager) |
402461aa JS |
105 | return; |
106 | ||
ad6c3739 ZJS |
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 | ||
2e6c012e | 113 | setenv("GIT_PAGER_IN_USE", "true", 1); |
85fb65ed | 114 | |
bfdd9ffd | 115 | /* spawn the pager */ |
3e3a4a41 | 116 | prepare_pager_args(&pager_process, pager); |
ea27a18c | 117 | pager_process.in = -1; |
124b5190 | 118 | argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE"); |
bfdd9ffd JS |
119 | if (start_command(&pager_process)) |
120 | return; | |
121 | ||
122 | /* original process continues, but writes to the pipe */ | |
123 | dup2(pager_process.in, 1); | |
a8335024 JH |
124 | if (isatty(2)) |
125 | dup2(pager_process.in, 2); | |
bfdd9ffd JS |
126 | close(pager_process.in); |
127 | ||
128 | /* this makes sure that the parent terminates after the pager */ | |
a3da8821 | 129 | sigchain_push_common(wait_for_pager_signal); |
507d7804 | 130 | atexit(wait_for_pager_atexit); |
f67b45f8 | 131 | } |
6e9af863 JK |
132 | |
133 | int pager_in_use(void) | |
134 | { | |
135 | const char *env; | |
6e9af863 JK |
136 | env = getenv("GIT_PAGER_IN_USE"); |
137 | return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0; | |
138 | } | |
ad6c3739 ZJS |
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 | } | |
4d9e079e | 170 | |
ec7ff5ba ZJS |
171 | /* |
172 | * How many columns do we need to show this number in decimal? | |
173 | */ | |
d306f3d3 | 174 | int decimal_width(uintmax_t number) |
ec7ff5ba | 175 | { |
d306f3d3 | 176 | int width; |
ec7ff5ba | 177 | |
d306f3d3 JK |
178 | for (width = 1; number >= 10; width++) |
179 | number /= 10; | |
ec7ff5ba ZJS |
180 | return width; |
181 | } | |
4914c962 | 182 | |
586f414a TA |
183 | /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */ |
184 | int check_pager_config(const char *cmd) | |
4914c962 | 185 | { |
586f414a TA |
186 | int want = -1; |
187 | struct strbuf key = STRBUF_INIT; | |
188 | const char *value = NULL; | |
189 | strbuf_addf(&key, "pager.%s", cmd); | |
9e9de18f JK |
190 | if (git_config_key_is_valid(key.buf) && |
191 | !git_config_get_value(key.buf, &value)) { | |
586f414a | 192 | int b = git_config_maybe_bool(key.buf, value); |
4914c962 | 193 | if (b >= 0) |
586f414a | 194 | want = b; |
4914c962 | 195 | else { |
586f414a TA |
196 | want = 1; |
197 | pager_program = xstrdup(value); | |
4914c962 NTND |
198 | } |
199 | } | |
586f414a TA |
200 | strbuf_release(&key); |
201 | return want; | |
4914c962 | 202 | } |