Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 1 | #include <sys/select.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | #include <signal.h> |
Andi Kleen | 61eb2eb | 2016-09-15 15:24:44 -0700 | [diff] [blame] | 6 | #include <sys/ioctl.h> |
Josh Poimboeuf | 2f4ce5e | 2015-12-15 09:39:38 -0600 | [diff] [blame] | 7 | #include "pager.h" |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 8 | #include "run-command.h" |
| 9 | #include "sigchain.h" |
Josh Poimboeuf | 096d355 | 2015-12-15 09:39:35 -0600 | [diff] [blame] | 10 | #include "subcmd-config.h" |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 11 | |
| 12 | /* |
| 13 | * This is split up from the rest of git so that we can do |
| 14 | * something different on Windows. |
| 15 | */ |
| 16 | |
| 17 | static int spawned_pager; |
Andi Kleen | 61eb2eb | 2016-09-15 15:24:44 -0700 | [diff] [blame] | 18 | static int pager_columns; |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 19 | |
Josh Poimboeuf | 096d355 | 2015-12-15 09:39:35 -0600 | [diff] [blame] | 20 | void pager_init(const char *pager_env) |
| 21 | { |
| 22 | subcmd_config.pager_env = pager_env; |
| 23 | } |
| 24 | |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 25 | static void pager_preexec(void) |
| 26 | { |
| 27 | /* |
| 28 | * Work around bug in "less" by not starting it until we |
| 29 | * have real input |
| 30 | */ |
| 31 | fd_set in; |
Sergey Senozhatsky | d8d8355 | 2018-02-06 15:37:52 -0800 | [diff] [blame] | 32 | fd_set exception; |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 33 | |
| 34 | FD_ZERO(&in); |
Sergey Senozhatsky | d8d8355 | 2018-02-06 15:37:52 -0800 | [diff] [blame] | 35 | FD_ZERO(&exception); |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 36 | FD_SET(0, &in); |
Sergey Senozhatsky | d8d8355 | 2018-02-06 15:37:52 -0800 | [diff] [blame] | 37 | FD_SET(0, &exception); |
| 38 | select(1, &in, NULL, &exception, NULL); |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 39 | |
| 40 | setenv("LESS", "FRSX", 0); |
| 41 | } |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 42 | |
| 43 | static const char *pager_argv[] = { "sh", "-c", NULL, NULL }; |
| 44 | static struct child_process pager_process; |
| 45 | |
| 46 | static void wait_for_pager(void) |
| 47 | { |
| 48 | fflush(stdout); |
| 49 | fflush(stderr); |
| 50 | /* signal EOF to pager */ |
| 51 | close(1); |
| 52 | close(2); |
| 53 | finish_command(&pager_process); |
| 54 | } |
| 55 | |
| 56 | static void wait_for_pager_signal(int signo) |
| 57 | { |
| 58 | wait_for_pager(); |
| 59 | sigchain_pop(signo); |
| 60 | raise(signo); |
| 61 | } |
| 62 | |
| 63 | void setup_pager(void) |
| 64 | { |
Josh Poimboeuf | 096d355 | 2015-12-15 09:39:35 -0600 | [diff] [blame] | 65 | const char *pager = getenv(subcmd_config.pager_env); |
Andi Kleen | 61eb2eb | 2016-09-15 15:24:44 -0700 | [diff] [blame] | 66 | struct winsize sz; |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 67 | |
| 68 | if (!isatty(1)) |
| 69 | return; |
Andi Kleen | 61eb2eb | 2016-09-15 15:24:44 -0700 | [diff] [blame] | 70 | if (ioctl(1, TIOCGWINSZ, &sz) == 0) |
| 71 | pager_columns = sz.ws_col; |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 72 | if (!pager) |
| 73 | pager = getenv("PAGER"); |
Michael Lentine | 21cfc5e | 2014-05-20 11:48:49 +0200 | [diff] [blame] | 74 | if (!(pager || access("/usr/bin/pager", X_OK))) |
| 75 | pager = "/usr/bin/pager"; |
| 76 | if (!(pager || access("/usr/bin/less", X_OK))) |
| 77 | pager = "/usr/bin/less"; |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 78 | if (!pager) |
Michael Lentine | 21cfc5e | 2014-05-20 11:48:49 +0200 | [diff] [blame] | 79 | pager = "cat"; |
| 80 | if (!*pager || !strcmp(pager, "cat")) |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 81 | return; |
| 82 | |
| 83 | spawned_pager = 1; /* means we are emitting to terminal */ |
| 84 | |
| 85 | /* spawn the pager */ |
| 86 | pager_argv[2] = pager; |
| 87 | pager_process.argv = pager_argv; |
| 88 | pager_process.in = -1; |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 89 | pager_process.preexec_cb = pager_preexec; |
Ingo Molnar | fde953c | 2009-06-27 06:06:39 +0200 | [diff] [blame] | 90 | |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 91 | if (start_command(&pager_process)) |
| 92 | return; |
| 93 | |
| 94 | /* original process continues, but writes to the pipe */ |
| 95 | dup2(pager_process.in, 1); |
| 96 | if (isatty(2)) |
| 97 | dup2(pager_process.in, 2); |
| 98 | close(pager_process.in); |
| 99 | |
| 100 | /* this makes sure that the parent terminates after the pager */ |
| 101 | sigchain_push_common(wait_for_pager_signal); |
| 102 | atexit(wait_for_pager); |
| 103 | } |
| 104 | |
| 105 | int pager_in_use(void) |
| 106 | { |
Josh Poimboeuf | a871a77 | 2015-12-13 22:18:08 -0600 | [diff] [blame] | 107 | return spawned_pager; |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 108 | } |
Andi Kleen | 61eb2eb | 2016-09-15 15:24:44 -0700 | [diff] [blame] | 109 | |
| 110 | int pager_get_columns(void) |
| 111 | { |
| 112 | char *s; |
| 113 | |
| 114 | s = getenv("COLUMNS"); |
| 115 | if (s) |
| 116 | return atoi(s); |
| 117 | |
| 118 | return (pager_columns ? pager_columns : 80) - 2; |
| 119 | } |