blob: e425070528f4f9fbf0953c9056d1ea43686e176f [file] [log] [blame]
Linus Torvaldsf67b45f2006-02-28 11:26:21 -08001#include "cache.h"
Jeff Kingea27a182008-07-22 03:14:12 -04002#include "run-command.h"
Jeff Kinga3da8822009-01-22 01:03:28 -05003#include "sigchain.h"
Linus Torvaldsf67b45f2006-02-28 11:26:21 -08004
Junio C Hamanoa3d023d2009-10-30 20:45:34 -05005#ifndef DEFAULT_PAGER
6#define DEFAULT_PAGER "less"
7#endif
8
Linus Torvaldsf67b45f2006-02-28 11:26:21 -08009/*
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010010 * This is split up from the rest of git so that we can do
11 * something different on Windows.
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080012 */
13
Jeff Kingac0ba182009-12-30 05:53:57 -050014static const char *pager_argv[] = { NULL, NULL };
René Scharfed3180272014-08-19 21:09:35 +020015static struct child_process pager_process = CHILD_PROCESS_INIT;
Jeff Kingea27a182008-07-22 03:14:12 -040016
Takashi Iwai507d7802015-09-04 11:35:57 +020017static void wait_for_pager(int in_signal)
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010018{
Takashi Iwai507d7802015-09-04 11:35:57 +020019 if (!in_signal) {
20 fflush(stdout);
21 fflush(stderr);
22 }
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010023 /* signal EOF to pager */
24 close(1);
25 close(2);
Takashi Iwai507d7802015-09-04 11:35:57 +020026 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);
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010035}
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080036
Jeff Kinga3da8822009-01-22 01:03:28 -050037static void wait_for_pager_signal(int signo)
38{
Takashi Iwai507d7802015-09-04 11:35:57 +020039 wait_for_pager(1);
Jeff Kinga3da8822009-01-22 01:03:28 -050040 sigchain_pop(signo);
41 raise(signo);
42}
43
Jonathan Nieder64778d22010-02-14 05:59:59 -060044const char *git_pager(int stdout_is_tty)
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080045{
Jonathan Nieder63618242009-10-30 20:41:27 -050046 const char *pager;
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080047
Jonathan Nieder64778d22010-02-14 05:59:59 -060048 if (!stdout_is_tty)
Jonathan Nieder63618242009-10-30 20:41:27 -050049 return NULL;
50
51 pager = getenv("GIT_PAGER");
Junio C Hamanocad3a202007-08-06 21:08:43 -070052 if (!pager) {
53 if (!pager_program)
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010054 git_config(git_default_config, NULL);
Brian Gernhardt54adf372007-07-03 14:18:11 -040055 pager = pager_program;
Junio C Hamanocad3a202007-08-06 21:08:43 -070056 }
Brian Gernhardt54adf372007-07-03 14:18:11 -040057 if (!pager)
Matthias Lederhoferc27d2052006-07-31 15:27:00 +020058 pager = getenv("PAGER");
59 if (!pager)
Junio C Hamanoa3d023d2009-10-30 20:45:34 -050060 pager = DEFAULT_PAGER;
Jeff Kinged016612013-09-03 03:41:50 -040061 if (!*pager || !strcmp(pager, "cat"))
Jonathan Nieder63618242009-10-30 20:41:27 -050062 pager = NULL;
63
64 return pager;
65}
66
67void setup_pager(void)
68{
Jonathan Nieder64778d22010-02-14 05:59:59 -060069 const char *pager = git_pager(isatty(1));
Jonathan Nieder63618242009-10-30 20:41:27 -050070
Jörn Engelc0459ca2014-04-21 16:46:22 -040071 if (!pager)
Johannes Schindelin402461a2006-04-16 04:44:25 +020072 return;
73
Zbigniew Jędrzejewski-Szmekad6c3732012-02-12 15:12:32 +010074 /*
75 * force computing the width of the terminal before we redirect
76 * the standard output to the pager.
77 */
78 (void) term_columns();
79
Jeff King2e6c0122011-08-17 22:02:29 -070080 setenv("GIT_PAGER_IN_USE", "true", 1);
Junio C Hamano85fb65e2006-06-06 16:58:40 -070081
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010082 /* spawn the pager */
Jeff Kingac0ba182009-12-30 05:53:57 -050083 pager_argv[0] = pager;
84 pager_process.use_shell = 1;
Jeff Kingea27a182008-07-22 03:14:12 -040085 pager_process.argv = pager_argv;
86 pager_process.in = -1;
René Scharfea9154592014-10-19 13:14:20 +020087 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");
Junio C Hamano124b5192015-07-03 10:18:45 -070091 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010092 if (start_command(&pager_process))
93 return;
94
95 /* original process continues, but writes to the pipe */
96 dup2(pager_process.in, 1);
Junio C Hamanoa8335022008-12-15 00:33:34 -080097 if (isatty(2))
98 dup2(pager_process.in, 2);
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010099 close(pager_process.in);
100
101 /* this makes sure that the parent terminates after the pager */
Jeff Kinga3da8822009-01-22 01:03:28 -0500102 sigchain_push_common(wait_for_pager_signal);
Takashi Iwai507d7802015-09-04 11:35:57 +0200103 atexit(wait_for_pager_atexit);
Linus Torvaldsf67b45f2006-02-28 11:26:21 -0800104}
Jeff King6e9af862007-12-11 01:27:33 -0500105
106int pager_in_use(void)
107{
108 const char *env;
Jeff King6e9af862007-12-11 01:27:33 -0500109 env = getenv("GIT_PAGER_IN_USE");
110 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
111}
Zbigniew Jędrzejewski-Szmekad6c3732012-02-12 15:12:32 +0100112
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}
Junio C Hamano4d9e0792012-02-20 00:15:11 -0800143
144/*
Zbigniew Jędrzejewski-Szmekec7ff5b2012-02-12 15:16:20 +0100145 * How many columns do we need to show this number in decimal?
146 */
Jeff Kingd306f3d2015-02-05 03:14:19 -0500147int decimal_width(uintmax_t number)
Zbigniew Jędrzejewski-Szmekec7ff5b2012-02-12 15:16:20 +0100148{
Jeff Kingd306f3d2015-02-05 03:14:19 -0500149 int width;
Zbigniew Jędrzejewski-Szmekec7ff5b2012-02-12 15:16:20 +0100150
Jeff Kingd306f3d2015-02-05 03:14:19 -0500151 for (width = 1; number >= 10; width++)
152 number /= 10;
Zbigniew Jędrzejewski-Szmekec7ff5b2012-02-12 15:16:20 +0100153 return width;
154}
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +0700155
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +0700156/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
157int check_pager_config(const char *cmd)
158{
Tanay Abhra586f4142014-08-07 09:21:23 -0700159 int want = -1;
160 struct strbuf key = STRBUF_INIT;
161 const char *value = NULL;
162 strbuf_addf(&key, "pager.%s", cmd);
Jeff King9e9de182015-08-24 02:11:33 -0400163 if (git_config_key_is_valid(key.buf) &&
164 !git_config_get_value(key.buf, &value)) {
Tanay Abhra586f4142014-08-07 09:21:23 -0700165 int b = git_config_maybe_bool(key.buf, value);
166 if (b >= 0)
167 want = b;
168 else {
169 want = 1;
170 pager_program = xstrdup(value);
171 }
172 }
173 strbuf_release(&key);
174 return want;
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +0700175}