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