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