Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | |
| 3 | /* |
| 4 | * This is split up from the rest of git so that we might do |
| 5 | * something different on Windows, for example. |
| 6 | */ |
| 7 | |
Jeff King | 6e9af86 | 2007-12-11 01:27:33 -0500 | [diff] [blame] | 8 | static int spawned_pager; |
| 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; |
Junio C Hamano | cad3a20 | 2007-08-06 21:08:43 -0700 | [diff] [blame] | 34 | if (!pager) { |
| 35 | if (!pager_program) |
| 36 | git_config(git_default_config); |
Brian Gernhardt | 54adf37 | 2007-07-03 14:18:11 -0400 | [diff] [blame] | 37 | pager = pager_program; |
Junio C Hamano | cad3a20 | 2007-08-06 21:08:43 -0700 | [diff] [blame] | 38 | } |
Brian Gernhardt | 54adf37 | 2007-07-03 14:18:11 -0400 | [diff] [blame] | 39 | if (!pager) |
Matthias Lederhofer | c27d205 | 2006-07-31 15:27:00 +0200 | [diff] [blame] | 40 | pager = getenv("PAGER"); |
| 41 | if (!pager) |
Johannes Schindelin | 402461a | 2006-04-16 04:44:25 +0200 | [diff] [blame] | 42 | pager = "less"; |
Junio C Hamano | caef71a | 2006-04-16 01:46:08 -0700 | [diff] [blame] | 43 | else if (!*pager || !strcmp(pager, "cat")) |
Johannes Schindelin | 402461a | 2006-04-16 04:44:25 +0200 | [diff] [blame] | 44 | return; |
| 45 | |
Jeff King | 6e9af86 | 2007-12-11 01:27:33 -0500 | [diff] [blame] | 46 | spawned_pager = 1; /* means we are emitting to terminal */ |
Junio C Hamano | 85fb65e | 2006-06-06 16:58:40 -0700 | [diff] [blame] | 47 | |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 48 | if (pipe(fd) < 0) |
| 49 | return; |
| 50 | pid = fork(); |
| 51 | if (pid < 0) { |
| 52 | close(fd[0]); |
| 53 | close(fd[1]); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | /* return in the child */ |
| 58 | if (!pid) { |
| 59 | dup2(fd[1], 1); |
Junio C Hamano | 61b8050 | 2008-02-16 11:15:41 -0800 | [diff] [blame] | 60 | dup2(fd[1], 2); |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 61 | close(fd[0]); |
| 62 | close(fd[1]); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | /* The original process turns into the PAGER */ |
| 67 | dup2(fd[0], 0); |
| 68 | close(fd[0]); |
| 69 | close(fd[1]); |
| 70 | |
Junio C Hamano | 0abc026 | 2006-10-22 20:28:10 -0700 | [diff] [blame] | 71 | setenv("LESS", "FRSX", 0); |
Johannes Schindelin | 402461a | 2006-04-16 04:44:25 +0200 | [diff] [blame] | 72 | run_pager(pager); |
Linus Torvalds | 34fd1c9 | 2006-04-21 12:25:13 -0700 | [diff] [blame] | 73 | die("unable to execute pager '%s'", pager); |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 74 | exit(255); |
| 75 | } |
Jeff King | 6e9af86 | 2007-12-11 01:27:33 -0500 | [diff] [blame] | 76 | |
| 77 | int pager_in_use(void) |
| 78 | { |
| 79 | const char *env; |
| 80 | |
| 81 | if (spawned_pager) |
| 82 | return 1; |
| 83 | |
| 84 | env = getenv("GIT_PAGER_IN_USE"); |
| 85 | return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0; |
| 86 | } |