pager: remove obsolete comment
[git.git] / pager.c
blob70159b8858c61f8c5c5577aefcfc2569956c5ddd
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 static struct child_process pager_process = CHILD_PROCESS_INIT;
11 static void wait_for_pager(int in_signal)
13 if (!in_signal) {
14 fflush(stdout);
15 fflush(stderr);
17 /* signal EOF to pager */
18 close(1);
19 close(2);
20 if (in_signal)
21 finish_command_in_signal(&pager_process);
22 else
23 finish_command(&pager_process);
26 static void wait_for_pager_atexit(void)
28 wait_for_pager(0);
31 static void wait_for_pager_signal(int signo)
33 wait_for_pager(1);
34 sigchain_pop(signo);
35 raise(signo);
38 const char *git_pager(int stdout_is_tty)
40 const char *pager;
42 if (!stdout_is_tty)
43 return NULL;
45 pager = getenv("GIT_PAGER");
46 if (!pager) {
47 if (!pager_program)
48 git_config(git_default_config, NULL);
49 pager = pager_program;
51 if (!pager)
52 pager = getenv("PAGER");
53 if (!pager)
54 pager = DEFAULT_PAGER;
55 if (!*pager || !strcmp(pager, "cat"))
56 pager = NULL;
58 return pager;
61 void prepare_pager_args(struct child_process *pager_process, const char *pager)
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");
71 void setup_pager(void)
73 const char *pager = git_pager(isatty(1));
75 if (!pager)
76 return;
79 * force computing the width of the terminal before we redirect
80 * the standard output to the pager.
82 (void) term_columns();
84 setenv("GIT_PAGER_IN_USE", "true", 1);
86 /* spawn the pager */
87 prepare_pager_args(&pager_process, pager);
88 pager_process.in = -1;
89 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
90 if (start_command(&pager_process))
91 return;
93 /* original process continues, but writes to the pipe */
94 dup2(pager_process.in, 1);
95 if (isatty(2))
96 dup2(pager_process.in, 2);
97 close(pager_process.in);
99 /* this makes sure that the parent terminates after the pager */
100 sigchain_push_common(wait_for_pager_signal);
101 atexit(wait_for_pager_atexit);
104 int pager_in_use(void)
106 const char *env;
107 env = getenv("GIT_PAGER_IN_USE");
108 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
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.
116 int term_columns(void)
118 static int term_columns_at_startup;
120 char *col_string;
121 int n_cols;
123 if (term_columns_at_startup)
124 return term_columns_at_startup;
126 term_columns_at_startup = 80;
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;
137 #endif
139 return term_columns_at_startup;
143 * How many columns do we need to show this number in decimal?
145 int decimal_width(uintmax_t number)
147 int width;
149 for (width = 1; number >= 10; width++)
150 number /= 10;
151 return width;
154 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
155 int check_pager_config(const char *cmd)
157 int want = -1;
158 struct strbuf key = STRBUF_INIT;
159 const char *value = NULL;
160 strbuf_addf(&key, "pager.%s", cmd);
161 if (git_config_key_is_valid(key.buf) &&
162 !git_config_get_value(key.buf, &value)) {
163 int b = git_config_maybe_bool(key.buf, value);
164 if (b >= 0)
165 want = b;
166 else {
167 want = 1;
168 pager_program = xstrdup(value);
171 strbuf_release(&key);
172 return want;