]>
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 | ||
d3180279 | 9 | static struct child_process pager_process = CHILD_PROCESS_INIT; |
ea27a18c | 10 | |
507d7804 | 11 | static void wait_for_pager(int in_signal) |
bfdd9ffd | 12 | { |
507d7804 TI |
13 | if (!in_signal) { |
14 | fflush(stdout); | |
15 | fflush(stderr); | |
16 | } | |
bfdd9ffd JS |
17 | /* signal EOF to pager */ |
18 | close(1); | |
19 | close(2); | |
507d7804 TI |
20 | if (in_signal) |
21 | finish_command_in_signal(&pager_process); | |
22 | else | |
23 | finish_command(&pager_process); | |
24 | } | |
25 | ||
26 | static void wait_for_pager_atexit(void) | |
27 | { | |
28 | wait_for_pager(0); | |
bfdd9ffd | 29 | } |
f67b45f8 | 30 | |
a3da8821 JK |
31 | static void wait_for_pager_signal(int signo) |
32 | { | |
507d7804 | 33 | wait_for_pager(1); |
a3da8821 JK |
34 | sigchain_pop(signo); |
35 | raise(signo); | |
36 | } | |
37 | ||
64778d24 | 38 | const char *git_pager(int stdout_is_tty) |
f67b45f8 | 39 | { |
63618245 | 40 | const char *pager; |
f67b45f8 | 41 | |
64778d24 | 42 | if (!stdout_is_tty) |
63618245 JN |
43 | return NULL; |
44 | ||
45 | pager = getenv("GIT_PAGER"); | |
cad3a205 JH |
46 | if (!pager) { |
47 | if (!pager_program) | |
ef90d6d4 | 48 | git_config(git_default_config, NULL); |
54adf370 | 49 | pager = pager_program; |
cad3a205 | 50 | } |
c27d205a ML |
51 | if (!pager) |
52 | pager = getenv("PAGER"); | |
402461aa | 53 | if (!pager) |
a3d023d0 | 54 | pager = DEFAULT_PAGER; |
ed016612 | 55 | if (!*pager || !strcmp(pager, "cat")) |
63618245 JN |
56 | pager = NULL; |
57 | ||
58 | return pager; | |
59 | } | |
60 | ||
3e3a4a41 JH |
61 | void prepare_pager_args(struct child_process *pager_process, const char *pager) |
62 | { | |
63 | argv_array_push(&pager_process->args, pager); | |
64 | pager_process->use_shell = 1; | |
65 | if (!getenv("LESS")) | |
66 | argv_array_push(&pager_process->env_array, "LESS=FRX"); | |
67 | if (!getenv("LV")) | |
68 | argv_array_push(&pager_process->env_array, "LV=-c"); | |
69 | } | |
70 | ||
63618245 JN |
71 | void setup_pager(void) |
72 | { | |
64778d24 | 73 | const char *pager = git_pager(isatty(1)); |
63618245 | 74 | |
c0459ca4 | 75 | if (!pager) |
402461aa JS |
76 | return; |
77 | ||
ad6c3739 ZJS |
78 | /* |
79 | * force computing the width of the terminal before we redirect | |
80 | * the standard output to the pager. | |
81 | */ | |
82 | (void) term_columns(); | |
83 | ||
2e6c012e | 84 | setenv("GIT_PAGER_IN_USE", "true", 1); |
85fb65ed | 85 | |
bfdd9ffd | 86 | /* spawn the pager */ |
3e3a4a41 | 87 | prepare_pager_args(&pager_process, pager); |
ea27a18c | 88 | pager_process.in = -1; |
124b5190 | 89 | argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE"); |
bfdd9ffd JS |
90 | if (start_command(&pager_process)) |
91 | return; | |
92 | ||
93 | /* original process continues, but writes to the pipe */ | |
94 | dup2(pager_process.in, 1); | |
a8335024 JH |
95 | if (isatty(2)) |
96 | dup2(pager_process.in, 2); | |
bfdd9ffd JS |
97 | close(pager_process.in); |
98 | ||
99 | /* this makes sure that the parent terminates after the pager */ | |
a3da8821 | 100 | sigchain_push_common(wait_for_pager_signal); |
507d7804 | 101 | atexit(wait_for_pager_atexit); |
f67b45f8 | 102 | } |
6e9af863 JK |
103 | |
104 | int pager_in_use(void) | |
105 | { | |
106 | const char *env; | |
6e9af863 JK |
107 | env = getenv("GIT_PAGER_IN_USE"); |
108 | return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0; | |
109 | } | |
ad6c3739 ZJS |
110 | |
111 | /* | |
112 | * Return cached value (if set) or $COLUMNS environment variable (if | |
113 | * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive), | |
114 | * and default to 80 if all else fails. | |
115 | */ | |
116 | int term_columns(void) | |
117 | { | |
118 | static int term_columns_at_startup; | |
119 | ||
120 | char *col_string; | |
121 | int n_cols; | |
122 | ||
123 | if (term_columns_at_startup) | |
124 | return term_columns_at_startup; | |
125 | ||
126 | term_columns_at_startup = 80; | |
127 | ||
128 | col_string = getenv("COLUMNS"); | |
129 | if (col_string && (n_cols = atoi(col_string)) > 0) | |
130 | term_columns_at_startup = n_cols; | |
131 | #ifdef TIOCGWINSZ | |
132 | else { | |
133 | struct winsize ws; | |
134 | if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col) | |
135 | term_columns_at_startup = ws.ws_col; | |
136 | } | |
137 | #endif | |
138 | ||
139 | return term_columns_at_startup; | |
140 | } | |
4d9e079e | 141 | |
ec7ff5ba ZJS |
142 | /* |
143 | * How many columns do we need to show this number in decimal? | |
144 | */ | |
d306f3d3 | 145 | int decimal_width(uintmax_t number) |
ec7ff5ba | 146 | { |
d306f3d3 | 147 | int width; |
ec7ff5ba | 148 | |
d306f3d3 JK |
149 | for (width = 1; number >= 10; width++) |
150 | number /= 10; | |
ec7ff5ba ZJS |
151 | return width; |
152 | } | |
4914c962 | 153 | |
586f414a TA |
154 | /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */ |
155 | int check_pager_config(const char *cmd) | |
4914c962 | 156 | { |
586f414a TA |
157 | int want = -1; |
158 | struct strbuf key = STRBUF_INIT; | |
159 | const char *value = NULL; | |
160 | strbuf_addf(&key, "pager.%s", cmd); | |
9e9de18f JK |
161 | if (git_config_key_is_valid(key.buf) && |
162 | !git_config_get_value(key.buf, &value)) { | |
586f414a | 163 | int b = git_config_maybe_bool(key.buf, value); |
4914c962 | 164 | if (b >= 0) |
586f414a | 165 | want = b; |
4914c962 | 166 | else { |
586f414a TA |
167 | want = 1; |
168 | pager_program = xstrdup(value); | |
4914c962 NTND |
169 | } |
170 | } | |
586f414a TA |
171 | strbuf_release(&key); |
172 | return want; | |
4914c962 | 173 | } |