]>
Commit | Line | Data |
---|---|---|
f67b45f8 LT |
1 | #include "cache.h" |
2 | ||
35ce8622 LT |
3 | #include <sys/select.h> |
4 | ||
f67b45f8 LT |
5 | /* |
6 | * This is split up from the rest of git so that we might do | |
7 | * something different on Windows, for example. | |
8 | */ | |
9 | ||
402461aa | 10 | static void run_pager(const char *pager) |
f67b45f8 | 11 | { |
35ce8622 LT |
12 | /* |
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 | ||
402461aa | 22 | execlp(pager, pager, NULL); |
34fd1c9a | 23 | execl("/bin/sh", "sh", "-c", pager, NULL); |
f67b45f8 LT |
24 | } |
25 | ||
26 | void setup_pager(void) | |
27 | { | |
28 | pid_t pid; | |
29 | int fd[2]; | |
c27d205a | 30 | const char *pager = getenv("GIT_PAGER"); |
f67b45f8 LT |
31 | |
32 | if (!isatty(1)) | |
33 | return; | |
c27d205a ML |
34 | if (!pager) |
35 | pager = getenv("PAGER"); | |
402461aa JS |
36 | if (!pager) |
37 | pager = "less"; | |
caef71a5 | 38 | else if (!*pager || !strcmp(pager, "cat")) |
402461aa JS |
39 | return; |
40 | ||
85fb65ed JH |
41 | pager_in_use = 1; /* means we are emitting to terminal */ |
42 | ||
f67b45f8 LT |
43 | 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 | ||
0abc0260 | 65 | setenv("LESS", "FRSX", 0); |
402461aa | 66 | run_pager(pager); |
34fd1c9a | 67 | die("unable to execute pager '%s'", pager); |
f67b45f8 LT |
68 | exit(255); |
69 | } |