Sync with 2.9.3
[git/git-svn.git] / pager.c
blob6470b8180df7e0de51a0d71ac47f8c078d226923
1 #include "cache.h"
2 #include "run-command.h"
3 #include "sigchain.h"
5 #ifndef DEFAULT_PAGER
6 #define DEFAULT_PAGER "less"
7 #endif
9 /*
10 * This is split up from the rest of git so that we can do
11 * something different on Windows.
14 static struct child_process pager_process = CHILD_PROCESS_INIT;
16 static void wait_for_pager(int in_signal)
18 if (!in_signal) {
19 fflush(stdout);
20 fflush(stderr);
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);
31 static void wait_for_pager_atexit(void)
33 wait_for_pager(0);
36 static void wait_for_pager_signal(int signo)
38 wait_for_pager(1);
39 sigchain_pop(signo);
40 raise(signo);
43 const char *git_pager(int stdout_is_tty)
45 const char *pager;
47 if (!stdout_is_tty)
48 return NULL;
50 pager = getenv("GIT_PAGER");
51 if (!pager) {
52 if (!pager_program)
53 git_config(git_default_config, NULL);
54 pager = pager_program;
56 if (!pager)
57 pager = getenv("PAGER");
58 if (!pager)
59 pager = DEFAULT_PAGER;
60 if (!*pager || !strcmp(pager, "cat"))
61 pager = NULL;
63 return pager;
66 static void setup_pager_env(struct argv_array *env)
68 const char **argv;
69 int i;
70 char *pager_env = xstrdup(PAGER_ENV);
71 int n = split_cmdline(pager_env, &argv);
73 if (n < 0)
74 die("malformed build-time PAGER_ENV: %s",
75 split_cmdline_strerror(n));
77 for (i = 0; i < n; i++) {
78 char *cp = strchr(argv[i], '=');
80 if (!cp)
81 die("malformed build-time PAGER_ENV");
83 *cp = '\0';
84 if (!getenv(argv[i])) {
85 *cp = '=';
86 argv_array_push(env, argv[i]);
89 free(pager_env);
90 free(argv);
93 void prepare_pager_args(struct child_process *pager_process, const char *pager)
95 argv_array_push(&pager_process->args, pager);
96 pager_process->use_shell = 1;
97 setup_pager_env(&pager_process->env_array);
100 void setup_pager(void)
102 const char *pager = git_pager(isatty(1));
104 if (!pager)
105 return;
108 * force computing the width of the terminal before we redirect
109 * the standard output to the pager.
111 (void) term_columns();
113 setenv("GIT_PAGER_IN_USE", "true", 1);
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;
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);
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);
133 int pager_in_use(void)
135 const char *env;
136 env = getenv("GIT_PAGER_IN_USE");
137 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
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.
145 int term_columns(void)
147 static int term_columns_at_startup;
149 char *col_string;
150 int n_cols;
152 if (term_columns_at_startup)
153 return term_columns_at_startup;
155 term_columns_at_startup = 80;
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;
166 #endif
168 return term_columns_at_startup;
172 * How many columns do we need to show this number in decimal?
174 int decimal_width(uintmax_t number)
176 int width;
178 for (width = 1; number >= 10; width++)
179 number /= 10;
180 return width;
183 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
184 int check_pager_config(const char *cmd)
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);
200 strbuf_release(&key);
201 return want;