Merge branch 'cr/tag-options'
[git/git-p4.git] / pager.c
blobfb7a1a625abf07b0d896a270286ee422a700c14c
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)
11 * Work around bug in "less" by not starting it until we
12 * have real input
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);
24 void setup_pager(void)
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;
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;
55 /* return in the child */
56 if (!pid) {
57 dup2(fd[1], 1);
58 close(fd[0]);
59 close(fd[1]);
60 return;
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);