blob: 4dcb08d9674c252cbe2c2725f92eca2a0350dc93 [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
Frank Li71064e32009-09-16 10:20:22 +020014#ifndef WIN32
Jeff Kingea27a182008-07-22 03:14:12 -040015static void pager_preexec(void)
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080016{
Linus Torvalds35ce8622007-01-24 11:21:10 -080017 /*
18 * Work around bug in "less" by not starting it until we
19 * have real input
20 */
21 fd_set in;
22
23 FD_ZERO(&in);
24 FD_SET(0, &in);
25 select(1, &in, NULL, &in, NULL);
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080026}
Jeff Kingea27a182008-07-22 03:14:12 -040027#endif
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010028
Jeff Kingac0ba182009-12-30 05:53:57 -050029static const char *pager_argv[] = { NULL, NULL };
Jeff Kingea27a182008-07-22 03:14:12 -040030static struct child_process pager_process;
31
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010032static void wait_for_pager(void)
33{
34 fflush(stdout);
35 fflush(stderr);
36 /* signal EOF to pager */
37 close(1);
38 close(2);
39 finish_command(&pager_process);
40}
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080041
Jeff Kinga3da8822009-01-22 01:03:28 -050042static void wait_for_pager_signal(int signo)
43{
44 wait_for_pager();
45 sigchain_pop(signo);
46 raise(signo);
47}
48
Jonathan Nieder64778d22010-02-14 05:59:59 -060049const char *git_pager(int stdout_is_tty)
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080050{
Jonathan Nieder63618242009-10-30 20:41:27 -050051 const char *pager;
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080052
Jonathan Nieder64778d22010-02-14 05:59:59 -060053 if (!stdout_is_tty)
Jonathan Nieder63618242009-10-30 20:41:27 -050054 return NULL;
55
56 pager = getenv("GIT_PAGER");
Junio C Hamanocad3a202007-08-06 21:08:43 -070057 if (!pager) {
58 if (!pager_program)
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010059 git_config(git_default_config, NULL);
Brian Gernhardt54adf372007-07-03 14:18:11 -040060 pager = pager_program;
Junio C Hamanocad3a202007-08-06 21:08:43 -070061 }
Brian Gernhardt54adf372007-07-03 14:18:11 -040062 if (!pager)
Matthias Lederhoferc27d2052006-07-31 15:27:00 +020063 pager = getenv("PAGER");
64 if (!pager)
Junio C Hamanoa3d023d2009-10-30 20:45:34 -050065 pager = DEFAULT_PAGER;
Junio C Hamanocaef71a2006-04-16 01:46:08 -070066 else if (!*pager || !strcmp(pager, "cat"))
Jonathan Nieder63618242009-10-30 20:41:27 -050067 pager = NULL;
68
69 return pager;
70}
71
72void setup_pager(void)
73{
Jonathan Nieder64778d22010-02-14 05:59:59 -060074 const char *pager = git_pager(isatty(1));
Jonathan Nieder63618242009-10-30 20:41:27 -050075
Nguyễn Thái Ngọc Duy88e8f902012-04-13 17:54:34 +070076 if (!pager || pager_in_use())
Johannes Schindelin402461a2006-04-16 04:44:25 +020077 return;
78
Zbigniew Jędrzejewski-Szmekad6c3732012-02-12 15:12:32 +010079 /*
80 * force computing the width of the terminal before we redirect
81 * the standard output to the pager.
82 */
83 (void) term_columns();
84
Jeff King2e6c0122011-08-17 22:02:29 -070085 setenv("GIT_PAGER_IN_USE", "true", 1);
Junio C Hamano85fb65e2006-06-06 16:58:40 -070086
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010087 /* spawn the pager */
Jeff Kingac0ba182009-12-30 05:53:57 -050088 pager_argv[0] = pager;
89 pager_process.use_shell = 1;
Jeff Kingea27a182008-07-22 03:14:12 -040090 pager_process.argv = pager_argv;
91 pager_process.in = -1;
Johannes Sixt25fc1782009-09-11 19:45:07 +020092 if (!getenv("LESS")) {
93 static const char *env[] = { "LESS=FRSX", NULL };
94 pager_process.env = env;
95 }
Frank Li71064e32009-09-16 10:20:22 +020096#ifndef WIN32
Jeff Kingea27a182008-07-22 03:14:12 -040097 pager_process.preexec_cb = pager_preexec;
98#endif
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010099 if (start_command(&pager_process))
100 return;
101
102 /* original process continues, but writes to the pipe */
103 dup2(pager_process.in, 1);
Junio C Hamanoa8335022008-12-15 00:33:34 -0800104 if (isatty(2))
105 dup2(pager_process.in, 2);
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +0100106 close(pager_process.in);
107
108 /* this makes sure that the parent terminates after the pager */
Jeff Kinga3da8822009-01-22 01:03:28 -0500109 sigchain_push_common(wait_for_pager_signal);
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +0100110 atexit(wait_for_pager);
Linus Torvaldsf67b45f2006-02-28 11:26:21 -0800111}
Jeff King6e9af862007-12-11 01:27:33 -0500112
113int pager_in_use(void)
114{
115 const char *env;
Jeff King6e9af862007-12-11 01:27:33 -0500116 env = getenv("GIT_PAGER_IN_USE");
117 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
118}
Zbigniew Jędrzejewski-Szmekad6c3732012-02-12 15:12:32 +0100119
120/*
121 * Return cached value (if set) or $COLUMNS environment variable (if
122 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
123 * and default to 80 if all else fails.
124 */
125int term_columns(void)
126{
127 static int term_columns_at_startup;
128
129 char *col_string;
130 int n_cols;
131
132 if (term_columns_at_startup)
133 return term_columns_at_startup;
134
135 term_columns_at_startup = 80;
136
137 col_string = getenv("COLUMNS");
138 if (col_string && (n_cols = atoi(col_string)) > 0)
139 term_columns_at_startup = n_cols;
140#ifdef TIOCGWINSZ
141 else {
142 struct winsize ws;
143 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
144 term_columns_at_startup = ws.ws_col;
145 }
146#endif
147
148 return term_columns_at_startup;
149}
Junio C Hamano4d9e0792012-02-20 00:15:11 -0800150
151/*
Zbigniew Jędrzejewski-Szmekec7ff5b2012-02-12 15:16:20 +0100152 * How many columns do we need to show this number in decimal?
153 */
154int decimal_width(int number)
155{
156 int i, width;
157
158 for (width = 1, i = 10; i <= number; width++)
159 i *= 10;
160 return width;
161}