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