Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "run-command.h" |
| 3 | #include "sigchain.h" |
| 4 | |
| 5 | /* |
| 6 | * This is split up from the rest of git so that we can do |
| 7 | * something different on Windows. |
| 8 | */ |
| 9 | |
| 10 | static int spawned_pager; |
| 11 | |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 12 | static void pager_preexec(void) |
| 13 | { |
| 14 | /* |
| 15 | * Work around bug in "less" by not starting it until we |
| 16 | * have real input |
| 17 | */ |
| 18 | fd_set in; |
| 19 | |
| 20 | FD_ZERO(&in); |
| 21 | FD_SET(0, &in); |
| 22 | select(1, &in, NULL, &in, NULL); |
| 23 | |
| 24 | setenv("LESS", "FRSX", 0); |
| 25 | } |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 26 | |
| 27 | static const char *pager_argv[] = { "sh", "-c", NULL, NULL }; |
| 28 | static struct child_process pager_process; |
| 29 | |
| 30 | static void wait_for_pager(void) |
| 31 | { |
| 32 | fflush(stdout); |
| 33 | fflush(stderr); |
| 34 | /* signal EOF to pager */ |
| 35 | close(1); |
| 36 | close(2); |
| 37 | finish_command(&pager_process); |
| 38 | } |
| 39 | |
| 40 | static void wait_for_pager_signal(int signo) |
| 41 | { |
| 42 | wait_for_pager(); |
| 43 | sigchain_pop(signo); |
| 44 | raise(signo); |
| 45 | } |
| 46 | |
| 47 | void setup_pager(void) |
| 48 | { |
| 49 | const char *pager = getenv("PERF_PAGER"); |
| 50 | |
| 51 | if (!isatty(1)) |
| 52 | return; |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 53 | if (!pager) |
| 54 | pager = getenv("PAGER"); |
Michael Lentine | 21cfc5e | 2014-05-20 11:48:49 +0200 | [diff] [blame] | 55 | if (!(pager || access("/usr/bin/pager", X_OK))) |
| 56 | pager = "/usr/bin/pager"; |
| 57 | if (!(pager || access("/usr/bin/less", X_OK))) |
| 58 | pager = "/usr/bin/less"; |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 59 | if (!pager) |
Michael Lentine | 21cfc5e | 2014-05-20 11:48:49 +0200 | [diff] [blame] | 60 | pager = "cat"; |
| 61 | if (!*pager || !strcmp(pager, "cat")) |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 62 | return; |
| 63 | |
| 64 | spawned_pager = 1; /* means we are emitting to terminal */ |
| 65 | |
| 66 | /* spawn the pager */ |
| 67 | pager_argv[2] = pager; |
| 68 | pager_process.argv = pager_argv; |
| 69 | pager_process.in = -1; |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 70 | pager_process.preexec_cb = pager_preexec; |
Ingo Molnar | fde953c | 2009-06-27 06:06:39 +0200 | [diff] [blame] | 71 | |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 72 | if (start_command(&pager_process)) |
| 73 | return; |
| 74 | |
| 75 | /* original process continues, but writes to the pipe */ |
| 76 | dup2(pager_process.in, 1); |
| 77 | if (isatty(2)) |
| 78 | dup2(pager_process.in, 2); |
| 79 | close(pager_process.in); |
| 80 | |
| 81 | /* this makes sure that the parent terminates after the pager */ |
| 82 | sigchain_push_common(wait_for_pager_signal); |
| 83 | atexit(wait_for_pager); |
| 84 | } |
| 85 | |
| 86 | int pager_in_use(void) |
| 87 | { |
| 88 | const char *env; |
| 89 | |
| 90 | if (spawned_pager) |
| 91 | return 1; |
| 92 | |
| 93 | env = getenv("PERF_PAGER_IN_USE"); |
| 94 | return env ? perf_config_bool("PERF_PAGER_IN_USE", env) : 0; |
| 95 | } |