🌐 AI搜索 & 代理 主页
blob: dac358f047550a07dd8d90b2e410feab3eebd534 [file] [log] [blame]
Linus Torvaldsf67b45f2006-02-28 19:26:211#include "cache.h"
Jeff Kingea27a182008-07-22 07:14:122#include "run-command.h"
Jeff Kinga3da8822009-01-22 06:03:283#include "sigchain.h"
Linus Torvaldsf67b45f2006-02-28 19:26:214
Junio C Hamanoa3d023d2009-10-31 01:45:345#ifndef DEFAULT_PAGER
6#define DEFAULT_PAGER "less"
7#endif
8
Linus Torvaldsf67b45f2006-02-28 19:26:219/*
Johannes Sixtbfdd9ff2007-12-08 20:28:4110 * This is split up from the rest of git so that we can do
11 * something different on Windows.
Linus Torvaldsf67b45f2006-02-28 19:26:2112 */
13
Jeff King6e9af862007-12-11 06:27:3314static int spawned_pager;
15
Frank Li71064e32009-09-16 08:20:2216#ifndef WIN32
Jeff Kingea27a182008-07-22 07:14:1217static void pager_preexec(void)
Linus Torvaldsf67b45f2006-02-28 19:26:2118{
Linus Torvalds35ce8622007-01-24 19:21:1019 /*
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 19:26:2128}
Jeff Kingea27a182008-07-22 07:14:1229#endif
Johannes Sixtbfdd9ff2007-12-08 20:28:4130
Jeff Kingac0ba182009-12-30 10:53:5731static const char *pager_argv[] = { NULL, NULL };
Jeff Kingea27a182008-07-22 07:14:1232static struct child_process pager_process;
33
Johannes Sixtbfdd9ff2007-12-08 20:28:4134static 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 19:26:2143
Jeff Kinga3da8822009-01-22 06:03:2844static 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 11:59:5951const char *git_pager(int stdout_is_tty)
Linus Torvaldsf67b45f2006-02-28 19:26:2152{
Jonathan Nieder63618242009-10-31 01:41:2753 const char *pager;
Linus Torvaldsf67b45f2006-02-28 19:26:2154
Jonathan Nieder64778d22010-02-14 11:59:5955 if (!stdout_is_tty)
Jonathan Nieder63618242009-10-31 01:41:2756 return NULL;
57
58 pager = getenv("GIT_PAGER");
Junio C Hamanocad3a202007-08-07 04:08:4359 if (!pager) {
60 if (!pager_program)
Johannes Schindelinef90d6d2008-05-14 17:46:5361 git_config(git_default_config, NULL);
Brian Gernhardt54adf372007-07-03 18:18:1162 pager = pager_program;
Junio C Hamanocad3a202007-08-07 04:08:4363 }
Brian Gernhardt54adf372007-07-03 18:18:1164 if (!pager)
Matthias Lederhoferc27d2052006-07-31 13:27:0065 pager = getenv("PAGER");
66 if (!pager)
Junio C Hamanoa3d023d2009-10-31 01:45:3467 pager = DEFAULT_PAGER;
Junio C Hamanocaef71a2006-04-16 08:46:0868 else if (!*pager || !strcmp(pager, "cat"))
Jonathan Nieder63618242009-10-31 01:41:2769 pager = NULL;
70
71 return pager;
72}
73
74void setup_pager(void)
75{
Jonathan Nieder64778d22010-02-14 11:59:5976 const char *pager = git_pager(isatty(1));
Jonathan Nieder63618242009-10-31 01:41:2777
78 if (!pager)
Johannes Schindelin402461a2006-04-16 02:44:2579 return;
80
Jeff King6e9af862007-12-11 06:27:3381 spawned_pager = 1; /* means we are emitting to terminal */
Junio C Hamano85fb65e2006-06-06 23:58:4082
Johannes Sixtbfdd9ff2007-12-08 20:28:4183 /* spawn the pager */
Jeff Kingac0ba182009-12-30 10:53:5784 pager_argv[0] = pager;
85 pager_process.use_shell = 1;
Jeff Kingea27a182008-07-22 07:14:1286 pager_process.argv = pager_argv;
87 pager_process.in = -1;
Johannes Sixt25fc1782009-09-11 17:45:0788 if (!getenv("LESS")) {
89 static const char *env[] = { "LESS=FRSX", NULL };
90 pager_process.env = env;
91 }
Frank Li71064e32009-09-16 08:20:2292#ifndef WIN32
Jeff Kingea27a182008-07-22 07:14:1293 pager_process.preexec_cb = pager_preexec;
94#endif
Johannes Sixtbfdd9ff2007-12-08 20:28:4195 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 08:33:34100 if (isatty(2))
101 dup2(pager_process.in, 2);
Johannes Sixtbfdd9ff2007-12-08 20:28:41102 close(pager_process.in);
103
104 /* this makes sure that the parent terminates after the pager */
Jeff Kinga3da8822009-01-22 06:03:28105 sigchain_push_common(wait_for_pager_signal);
Johannes Sixtbfdd9ff2007-12-08 20:28:41106 atexit(wait_for_pager);
Linus Torvaldsf67b45f2006-02-28 19:26:21107}
Jeff King6e9af862007-12-11 06:27:33108
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}