blob: 5f280ab52720772905cacbcba522ecc9c81bb529 [file] [log] [blame]
Linus Torvaldsf67b45f2006-02-28 11:26:21 -08001#include "cache.h"
2
Linus Torvalds35ce8622007-01-24 11:21:10 -08003#include <sys/select.h>
4
Linus Torvaldsf67b45f2006-02-28 11:26:21 -08005/*
6 * This is split up from the rest of git so that we might do
7 * something different on Windows, for example.
8 */
9
Johannes Schindelin402461a2006-04-16 04:44:25 +020010static void run_pager(const char *pager)
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080011{
Linus Torvalds35ce8622007-01-24 11:21:10 -080012 /*
13 * Work around bug in "less" by not starting it until we
14 * have real input
15 */
16 fd_set in;
17
18 FD_ZERO(&in);
19 FD_SET(0, &in);
20 select(1, &in, NULL, &in, NULL);
21
Johannes Schindelin402461a2006-04-16 04:44:25 +020022 execlp(pager, pager, NULL);
Linus Torvalds34fd1c92006-04-21 12:25:13 -070023 execl("/bin/sh", "sh", "-c", pager, NULL);
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080024}
25
26void setup_pager(void)
27{
28 pid_t pid;
29 int fd[2];
Matthias Lederhoferc27d2052006-07-31 15:27:00 +020030 const char *pager = getenv("GIT_PAGER");
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080031
32 if (!isatty(1))
33 return;
Johannes Schindelin402461a2006-04-16 04:44:25 +020034 if (!pager)
Matthias Lederhoferc27d2052006-07-31 15:27:00 +020035 pager = getenv("PAGER");
36 if (!pager)
Johannes Schindelin402461a2006-04-16 04:44:25 +020037 pager = "less";
Junio C Hamanocaef71a2006-04-16 01:46:08 -070038 else if (!*pager || !strcmp(pager, "cat"))
Johannes Schindelin402461a2006-04-16 04:44:25 +020039 return;
40
Junio C Hamano85fb65e2006-06-06 16:58:40 -070041 pager_in_use = 1; /* means we are emitting to terminal */
42
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080043 if (pipe(fd) < 0)
44 return;
45 pid = fork();
46 if (pid < 0) {
47 close(fd[0]);
48 close(fd[1]);
49 return;
50 }
51
52 /* return in the child */
53 if (!pid) {
54 dup2(fd[1], 1);
55 close(fd[0]);
56 close(fd[1]);
57 return;
58 }
59
60 /* The original process turns into the PAGER */
61 dup2(fd[0], 0);
62 close(fd[0]);
63 close(fd[1]);
64
Junio C Hamano0abc0262006-10-22 20:28:10 -070065 setenv("LESS", "FRSX", 0);
Johannes Schindelin402461a2006-04-16 04:44:25 +020066 run_pager(pager);
Linus Torvalds34fd1c92006-04-21 12:25:13 -070067 die("unable to execute pager '%s'", pager);
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080068 exit(255);
69}