Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | |
Linus Torvalds | 35ce862 | 2007-01-24 11:21:10 -0800 | [diff] [blame] | 3 | #include <sys/select.h> |
| 4 | |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 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 | |
Johannes Schindelin | 402461a | 2006-04-16 04:44:25 +0200 | [diff] [blame] | 10 | static void run_pager(const char *pager) |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 11 | { |
Linus Torvalds | 35ce862 | 2007-01-24 11:21:10 -0800 | [diff] [blame] | 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 | |
Johannes Schindelin | 402461a | 2006-04-16 04:44:25 +0200 | [diff] [blame] | 22 | execlp(pager, pager, NULL); |
Linus Torvalds | 34fd1c9 | 2006-04-21 12:25:13 -0700 | [diff] [blame] | 23 | execl("/bin/sh", "sh", "-c", pager, NULL); |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void setup_pager(void) |
| 27 | { |
| 28 | pid_t pid; |
| 29 | int fd[2]; |
Matthias Lederhofer | c27d205 | 2006-07-31 15:27:00 +0200 | [diff] [blame] | 30 | const char *pager = getenv("GIT_PAGER"); |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 31 | |
| 32 | if (!isatty(1)) |
| 33 | return; |
Johannes Schindelin | 402461a | 2006-04-16 04:44:25 +0200 | [diff] [blame] | 34 | if (!pager) |
Matthias Lederhofer | c27d205 | 2006-07-31 15:27:00 +0200 | [diff] [blame] | 35 | pager = getenv("PAGER"); |
| 36 | if (!pager) |
Johannes Schindelin | 402461a | 2006-04-16 04:44:25 +0200 | [diff] [blame] | 37 | pager = "less"; |
Junio C Hamano | caef71a | 2006-04-16 01:46:08 -0700 | [diff] [blame] | 38 | else if (!*pager || !strcmp(pager, "cat")) |
Johannes Schindelin | 402461a | 2006-04-16 04:44:25 +0200 | [diff] [blame] | 39 | return; |
| 40 | |
Junio C Hamano | 85fb65e | 2006-06-06 16:58:40 -0700 | [diff] [blame] | 41 | pager_in_use = 1; /* means we are emitting to terminal */ |
| 42 | |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 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 | |
Junio C Hamano | 0abc026 | 2006-10-22 20:28:10 -0700 | [diff] [blame] | 65 | setenv("LESS", "FRSX", 0); |
Johannes Schindelin | 402461a | 2006-04-16 04:44:25 +0200 | [diff] [blame] | 66 | run_pager(pager); |
Linus Torvalds | 34fd1c9 | 2006-04-21 12:25:13 -0700 | [diff] [blame] | 67 | die("unable to execute pager '%s'", pager); |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 68 | exit(255); |
| 69 | } |