blob: dac358f047550a07dd8d90b2e410feab3eebd534 [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 King6e9af862007-12-11 01:27:33 -050014static int spawned_pager;
15
Frank Li71064e32009-09-16 10:20:22 +020016#ifndef WIN32
Jeff Kingea27a182008-07-22 03:14:12 -040017static void pager_preexec(void)
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080018{
Linus Torvalds35ce8622007-01-24 11:21:10 -080019 /*
20 * Work around bug in "less" by not starting it until we
21 * have real input
22 */
23 fd_set in;
24
25 FD_ZERO(&in);
26 FD_SET(0, &in);
27 select(1, &in, NULL, &in, NULL);
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080028}
Jeff Kingea27a182008-07-22 03:14:12 -040029#endif
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010030
Jeff Kingac0ba182009-12-30 05:53:57 -050031static const char *pager_argv[] = { NULL, NULL };
Jeff Kingea27a182008-07-22 03:14:12 -040032static struct child_process pager_process;
33
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010034static void wait_for_pager(void)
35{
36 fflush(stdout);
37 fflush(stderr);
38 /* signal EOF to pager */
39 close(1);
40 close(2);
41 finish_command(&pager_process);
42}
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080043
Jeff Kinga3da8822009-01-22 01:03:28 -050044static void wait_for_pager_signal(int signo)
45{
46 wait_for_pager();
47 sigchain_pop(signo);
48 raise(signo);
49}
50
Jonathan Nieder64778d22010-02-14 05:59:59 -060051const char *git_pager(int stdout_is_tty)
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080052{
Jonathan Nieder63618242009-10-30 20:41:27 -050053 const char *pager;
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080054
Jonathan Nieder64778d22010-02-14 05:59:59 -060055 if (!stdout_is_tty)
Jonathan Nieder63618242009-10-30 20:41:27 -050056 return NULL;
57
58 pager = getenv("GIT_PAGER");
Junio C Hamanocad3a202007-08-06 21:08:43 -070059 if (!pager) {
60 if (!pager_program)
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010061 git_config(git_default_config, NULL);
Brian Gernhardt54adf372007-07-03 14:18:11 -040062 pager = pager_program;
Junio C Hamanocad3a202007-08-06 21:08:43 -070063 }
Brian Gernhardt54adf372007-07-03 14:18:11 -040064 if (!pager)
Matthias Lederhoferc27d2052006-07-31 15:27:00 +020065 pager = getenv("PAGER");
66 if (!pager)
Junio C Hamanoa3d023d2009-10-30 20:45:34 -050067 pager = DEFAULT_PAGER;
Junio C Hamanocaef71a2006-04-16 01:46:08 -070068 else if (!*pager || !strcmp(pager, "cat"))
Jonathan Nieder63618242009-10-30 20:41:27 -050069 pager = NULL;
70
71 return pager;
72}
73
74void setup_pager(void)
75{
Jonathan Nieder64778d22010-02-14 05:59:59 -060076 const char *pager = git_pager(isatty(1));
Jonathan Nieder63618242009-10-30 20:41:27 -050077
78 if (!pager)
Johannes Schindelin402461a2006-04-16 04:44:25 +020079 return;
80
Jeff King6e9af862007-12-11 01:27:33 -050081 spawned_pager = 1; /* means we are emitting to terminal */
Junio C Hamano85fb65e2006-06-06 16:58:40 -070082
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010083 /* spawn the pager */
Jeff Kingac0ba182009-12-30 05:53:57 -050084 pager_argv[0] = pager;
85 pager_process.use_shell = 1;
Jeff Kingea27a182008-07-22 03:14:12 -040086 pager_process.argv = pager_argv;
87 pager_process.in = -1;
Johannes Sixt25fc1782009-09-11 19:45:07 +020088 if (!getenv("LESS")) {
89 static const char *env[] = { "LESS=FRSX", NULL };
90 pager_process.env = env;
91 }
Frank Li71064e32009-09-16 10:20:22 +020092#ifndef WIN32
Jeff Kingea27a182008-07-22 03:14:12 -040093 pager_process.preexec_cb = pager_preexec;
94#endif
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010095 if (start_command(&pager_process))
96 return;
97
98 /* original process continues, but writes to the pipe */
99 dup2(pager_process.in, 1);
Junio C Hamanoa8335022008-12-15 00:33:34 -0800100 if (isatty(2))
101 dup2(pager_process.in, 2);
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +0100102 close(pager_process.in);
103
104 /* this makes sure that the parent terminates after the pager */
Jeff Kinga3da8822009-01-22 01:03:28 -0500105 sigchain_push_common(wait_for_pager_signal);
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +0100106 atexit(wait_for_pager);
Linus Torvaldsf67b45f2006-02-28 11:26:21 -0800107}
Jeff King6e9af862007-12-11 01:27:33 -0500108
109int pager_in_use(void)
110{
111 const char *env;
112
113 if (spawned_pager)
114 return 1;
115
116 env = getenv("GIT_PAGER_IN_USE");
117 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
118}