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