Code

git-diff --numstat -z: make it machine readable
[git.git] / pager.c
1 #include "cache.h"
3 /*
4  * This is split up from the rest of git so that we might do
5  * something different on Windows, for example.
6  */
8 static void run_pager(const char *pager)
9 {
10         /*
11          * Work around bug in "less" by not starting it until we
12          * have real input
13          */
14         fd_set in;
16         FD_ZERO(&in);
17         FD_SET(0, &in);
18         select(1, &in, NULL, &in, NULL);
20         execlp(pager, pager, NULL);
21         execl("/bin/sh", "sh", "-c", pager, NULL);
22 }
24 void setup_pager(void)
25 {
26         pid_t pid;
27         int fd[2];
28         const char *pager = getenv("GIT_PAGER");
30         if (!isatty(1))
31                 return;
32         if (!pager) {
33                 if (!pager_program)
34                         git_config(git_default_config);
35                 pager = pager_program;
36         }
37         if (!pager)
38                 pager = getenv("PAGER");
39         if (!pager)
40                 pager = "less";
41         else if (!*pager || !strcmp(pager, "cat"))
42                 return;
44         pager_in_use = 1; /* means we are emitting to terminal */
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         }
55         /* return in the child */
56         if (!pid) {
57                 dup2(fd[1], 1);
58                 close(fd[0]);
59                 close(fd[1]);
60                 return;
61         }
63         /* The original process turns into the PAGER */
64         dup2(fd[0], 0);
65         close(fd[0]);
66         close(fd[1]);
68         setenv("LESS", "FRSX", 0);
69         run_pager(pager);
70         die("unable to execute pager '%s'", pager);
71         exit(255);
72 }