Update draft Release Notes for 1.5.4
[git/git.git] / pager.c
CommitLineData
f67b45f8
LT
1#include "cache.h"
2
3/*
4 * This is split up from the rest of git so that we might do
5 * something different on Windows, for example.
6 */
7
402461aa 8static void run_pager(const char *pager)
f67b45f8 9{
35ce8622
LT
10 /*
11 * Work around bug in "less" by not starting it until we
12 * have real input
13 */
14 fd_set in;
15
16 FD_ZERO(&in);
17 FD_SET(0, &in);
18 select(1, &in, NULL, &in, NULL);
19
402461aa 20 execlp(pager, pager, NULL);
34fd1c9a 21 execl("/bin/sh", "sh", "-c", pager, NULL);
f67b45f8
LT
22}
23
24void setup_pager(void)
25{
26 pid_t pid;
27 int fd[2];
c27d205a 28 const char *pager = getenv("GIT_PAGER");
f67b45f8
LT
29
30 if (!isatty(1))
31 return;
cad3a205
JH
32 if (!pager) {
33 if (!pager_program)
34 git_config(git_default_config);
54adf370 35 pager = pager_program;
cad3a205 36 }
c27d205a
ML
37 if (!pager)
38 pager = getenv("PAGER");
402461aa
JS
39 if (!pager)
40 pager = "less";
caef71a5 41 else if (!*pager || !strcmp(pager, "cat"))
402461aa
JS
42 return;
43
85fb65ed
JH
44 pager_in_use = 1; /* means we are emitting to terminal */
45
f67b45f8
LT
46 if (pipe(fd) < 0)
47 return;
48 pid = fork();
49 if (pid < 0) {
50 close(fd[0]);
51 close(fd[1]);
52 return;
53 }
54
55 /* return in the child */
56 if (!pid) {
57 dup2(fd[1], 1);
58 close(fd[0]);
59 close(fd[1]);
60 return;
61 }
62
63 /* The original process turns into the PAGER */
64 dup2(fd[0], 0);
65 close(fd[0]);
66 close(fd[1]);
67
0abc0260 68 setenv("LESS", "FRSX", 0);
402461aa 69 run_pager(pager);
34fd1c9a 70 die("unable to execute pager '%s'", pager);
f67b45f8
LT
71 exit(255);
72}