]> git.ipfire.org Git - thirdparty/git.git/blame - pager.c
Provide a build time default-editor setting
[thirdparty/git.git] / pager.c
CommitLineData
f67b45f8 1#include "cache.h"
ea27a18c 2#include "run-command.h"
a3da8821 3#include "sigchain.h"
f67b45f8
LT
4
5/*
bfdd9ffd
JS
6 * This is split up from the rest of git so that we can do
7 * something different on Windows.
f67b45f8
LT
8 */
9
6e9af863
JK
10static int spawned_pager;
11
71064e3f 12#ifndef WIN32
ea27a18c 13static void pager_preexec(void)
f67b45f8 14{
35ce8622
LT
15 /*
16 * Work around bug in "less" by not starting it until we
17 * have real input
18 */
19 fd_set in;
20
21 FD_ZERO(&in);
22 FD_SET(0, &in);
23 select(1, &in, NULL, &in, NULL);
f67b45f8 24}
ea27a18c 25#endif
bfdd9ffd
JS
26
27static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
ea27a18c
JK
28static struct child_process pager_process;
29
bfdd9ffd
JS
30static void wait_for_pager(void)
31{
32 fflush(stdout);
33 fflush(stderr);
34 /* signal EOF to pager */
35 close(1);
36 close(2);
37 finish_command(&pager_process);
38}
f67b45f8 39
a3da8821
JK
40static void wait_for_pager_signal(int signo)
41{
42 wait_for_pager();
43 sigchain_pop(signo);
44 raise(signo);
45}
46
63618245 47const char *git_pager(void)
f67b45f8 48{
63618245 49 const char *pager;
f67b45f8
LT
50
51 if (!isatty(1))
63618245
JN
52 return NULL;
53
54 pager = getenv("GIT_PAGER");
cad3a205
JH
55 if (!pager) {
56 if (!pager_program)
ef90d6d4 57 git_config(git_default_config, NULL);
54adf370 58 pager = pager_program;
cad3a205 59 }
c27d205a
ML
60 if (!pager)
61 pager = getenv("PAGER");
402461aa
JS
62 if (!pager)
63 pager = "less";
caef71a5 64 else if (!*pager || !strcmp(pager, "cat"))
63618245
JN
65 pager = NULL;
66
67 return pager;
68}
69
70void setup_pager(void)
71{
72 const char *pager = git_pager();
73
74 if (!pager)
402461aa
JS
75 return;
76
6e9af863 77 spawned_pager = 1; /* means we are emitting to terminal */
85fb65ed 78
bfdd9ffd
JS
79 /* spawn the pager */
80 pager_argv[2] = pager;
ea27a18c
JK
81 pager_process.argv = pager_argv;
82 pager_process.in = -1;
25fc1786
JS
83 if (!getenv("LESS")) {
84 static const char *env[] = { "LESS=FRSX", NULL };
85 pager_process.env = env;
86 }
71064e3f 87#ifndef WIN32
ea27a18c
JK
88 pager_process.preexec_cb = pager_preexec;
89#endif
bfdd9ffd
JS
90 if (start_command(&pager_process))
91 return;
92
93 /* original process continues, but writes to the pipe */
94 dup2(pager_process.in, 1);
a8335024
JH
95 if (isatty(2))
96 dup2(pager_process.in, 2);
bfdd9ffd
JS
97 close(pager_process.in);
98
99 /* this makes sure that the parent terminates after the pager */
a3da8821 100 sigchain_push_common(wait_for_pager_signal);
bfdd9ffd 101 atexit(wait_for_pager);
f67b45f8 102}
6e9af863
JK
103
104int pager_in_use(void)
105{
106 const char *env;
107
108 if (spawned_pager)
109 return 1;
110
111 env = getenv("GIT_PAGER_IN_USE");
112 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
113}