pager: remove obsolete comment
[git/git.git] / pager.c
... / ...
CommitLineData
1#include "cache.h"
2#include "run-command.h"
3#include "sigchain.h"
4
5#ifndef DEFAULT_PAGER
6#define DEFAULT_PAGER "less"
7#endif
8
9static struct child_process pager_process = CHILD_PROCESS_INIT;
10
11static void wait_for_pager(int in_signal)
12{
13 if (!in_signal) {
14 fflush(stdout);
15 fflush(stderr);
16 }
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);
24}
25
26static void wait_for_pager_atexit(void)
27{
28 wait_for_pager(0);
29}
30
31static void wait_for_pager_signal(int signo)
32{
33 wait_for_pager(1);
34 sigchain_pop(signo);
35 raise(signo);
36}
37
38const char *git_pager(int stdout_is_tty)
39{
40 const char *pager;
41
42 if (!stdout_is_tty)
43 return NULL;
44
45 pager = getenv("GIT_PAGER");
46 if (!pager) {
47 if (!pager_program)
48 git_config(git_default_config, NULL);
49 pager = pager_program;
50 }
51 if (!pager)
52 pager = getenv("PAGER");
53 if (!pager)
54 pager = DEFAULT_PAGER;
55 if (!*pager || !strcmp(pager, "cat"))
56 pager = NULL;
57
58 return pager;
59}
60
61void 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
71void setup_pager(void)
72{
73 const char *pager = git_pager(isatty(1));
74
75 if (!pager)
76 return;
77
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
84 setenv("GIT_PAGER_IN_USE", "true", 1);
85
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;
92
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);
98
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);
102}
103
104int pager_in_use(void)
105{
106 const char *env;
107 env = getenv("GIT_PAGER_IN_USE");
108 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
109}
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 */
116int 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}
141
142/*
143 * How many columns do we need to show this number in decimal?
144 */
145int decimal_width(uintmax_t number)
146{
147 int width;
148
149 for (width = 1; number >= 10; width++)
150 number /= 10;
151 return width;
152}
153
154/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
155int check_pager_config(const char *cmd)
156{
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);
169 }
170 }
171 strbuf_release(&key);
172 return want;
173}