blob: 975955ba82a0dbb128d6733090cd74c2b509ea81 [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
76 if (!pager)
Johannes Schindelin402461a2006-04-16 04:44:25 +020077 return;
78
Jeff King2e6c0122011-08-17 22:02:29 -070079 setenv("GIT_PAGER_IN_USE", "true", 1);
Junio C Hamano85fb65e2006-06-06 16:58:40 -070080
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010081 /* spawn the pager */
Jeff Kingac0ba182009-12-30 05:53:57 -050082 pager_argv[0] = pager;
83 pager_process.use_shell = 1;
Jeff Kingea27a182008-07-22 03:14:12 -040084 pager_process.argv = pager_argv;
85 pager_process.in = -1;
Johannes Sixt25fc1782009-09-11 19:45:07 +020086 if (!getenv("LESS")) {
87 static const char *env[] = { "LESS=FRSX", NULL };
88 pager_process.env = env;
89 }
Frank Li71064e32009-09-16 10:20:22 +020090#ifndef WIN32
Jeff Kingea27a182008-07-22 03:14:12 -040091 pager_process.preexec_cb = pager_preexec;
92#endif
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010093 if (start_command(&pager_process))
94 return;
95
96 /* original process continues, but writes to the pipe */
97 dup2(pager_process.in, 1);
Junio C Hamanoa8335022008-12-15 00:33:34 -080098 if (isatty(2))
99 dup2(pager_process.in, 2);
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +0100100 close(pager_process.in);
101
102 /* this makes sure that the parent terminates after the pager */
Jeff Kinga3da8822009-01-22 01:03:28 -0500103 sigchain_push_common(wait_for_pager_signal);
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +0100104 atexit(wait_for_pager);
Linus Torvaldsf67b45f2006-02-28 11:26:21 -0800105}
Jeff King6e9af862007-12-11 01:27:33 -0500106
107int pager_in_use(void)
108{
109 const char *env;
Jeff King6e9af862007-12-11 01:27:33 -0500110 env = getenv("GIT_PAGER_IN_USE");
111 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
112}