"git -p cmd" to page anywhere
[git/jrn.git] / pager.c
blobbb14e99735dd08c31c66325eacdcde4f3f2c685c
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 int pager_in_use;
10 static void run_pager(const char *pager)
12 execlp(pager, pager, NULL);
13 execl("/bin/sh", "sh", "-c", pager, NULL);
16 void setup_pager(void)
18 pid_t pid;
19 int fd[2];
20 const char *pager = getenv("PAGER");
22 if (!isatty(1))
23 return;
24 if (!pager)
25 pager = "less";
26 else if (!*pager || !strcmp(pager, "cat"))
27 return;
29 pager_in_use = 1; /* means we are emitting to terminal */
31 if (pipe(fd) < 0)
32 return;
33 pid = fork();
34 if (pid < 0) {
35 close(fd[0]);
36 close(fd[1]);
37 return;
40 /* return in the child */
41 if (!pid) {
42 dup2(fd[1], 1);
43 close(fd[0]);
44 close(fd[1]);
45 return;
48 /* The original process turns into the PAGER */
49 dup2(fd[0], 0);
50 close(fd[0]);
51 close(fd[1]);
53 setenv("LESS", "-RS", 0);
54 run_pager(pager);
55 die("unable to execute pager '%s'", pager);
56 exit(255);