Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 1 | #include "cache.h" |
Jeff King | ea27a18 | 2008-07-22 03:14:12 -0400 | [diff] [blame] | 2 | #include "run-command.h" |
Jeff King | a3da882 | 2009-01-22 01:03:28 -0500 | [diff] [blame] | 3 | #include "sigchain.h" |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 4 | |
Junio C Hamano | a3d023d | 2009-10-30 20:45:34 -0500 | [diff] [blame] | 5 | #ifndef DEFAULT_PAGER |
| 6 | #define DEFAULT_PAGER "less" |
| 7 | #endif |
| 8 | |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 9 | /* |
Johannes Sixt | bfdd9ff | 2007-12-08 21:28:41 +0100 | [diff] [blame] | 10 | * This is split up from the rest of git so that we can do |
| 11 | * something different on Windows. |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 12 | */ |
| 13 | |
Frank Li | 71064e3 | 2009-09-16 10:20:22 +0200 | [diff] [blame] | 14 | #ifndef WIN32 |
Jeff King | ea27a18 | 2008-07-22 03:14:12 -0400 | [diff] [blame] | 15 | static void pager_preexec(void) |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 16 | { |
Linus Torvalds | 35ce862 | 2007-01-24 11:21:10 -0800 | [diff] [blame] | 17 | /* |
| 18 | * Work around bug in "less" by not starting it until we |
| 19 | * have real input |
| 20 | */ |
| 21 | fd_set in; |
| 22 | |
| 23 | FD_ZERO(&in); |
| 24 | FD_SET(0, &in); |
| 25 | select(1, &in, NULL, &in, NULL); |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 26 | } |
Jeff King | ea27a18 | 2008-07-22 03:14:12 -0400 | [diff] [blame] | 27 | #endif |
Johannes Sixt | bfdd9ff | 2007-12-08 21:28:41 +0100 | [diff] [blame] | 28 | |
Jeff King | ac0ba18 | 2009-12-30 05:53:57 -0500 | [diff] [blame] | 29 | static const char *pager_argv[] = { NULL, NULL }; |
Jeff King | ea27a18 | 2008-07-22 03:14:12 -0400 | [diff] [blame] | 30 | static struct child_process pager_process; |
| 31 | |
Johannes Sixt | bfdd9ff | 2007-12-08 21:28:41 +0100 | [diff] [blame] | 32 | static void wait_for_pager(void) |
| 33 | { |
| 34 | fflush(stdout); |
| 35 | fflush(stderr); |
| 36 | /* signal EOF to pager */ |
| 37 | close(1); |
| 38 | close(2); |
| 39 | finish_command(&pager_process); |
| 40 | } |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 41 | |
Jeff King | a3da882 | 2009-01-22 01:03:28 -0500 | [diff] [blame] | 42 | static void wait_for_pager_signal(int signo) |
| 43 | { |
| 44 | wait_for_pager(); |
| 45 | sigchain_pop(signo); |
| 46 | raise(signo); |
| 47 | } |
| 48 | |
Jonathan Nieder | 64778d2 | 2010-02-14 05:59:59 -0600 | [diff] [blame] | 49 | const char *git_pager(int stdout_is_tty) |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 50 | { |
Jonathan Nieder | 6361824 | 2009-10-30 20:41:27 -0500 | [diff] [blame] | 51 | const char *pager; |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 52 | |
Jonathan Nieder | 64778d2 | 2010-02-14 05:59:59 -0600 | [diff] [blame] | 53 | if (!stdout_is_tty) |
Jonathan Nieder | 6361824 | 2009-10-30 20:41:27 -0500 | [diff] [blame] | 54 | return NULL; |
| 55 | |
| 56 | pager = getenv("GIT_PAGER"); |
Junio C Hamano | cad3a20 | 2007-08-06 21:08:43 -0700 | [diff] [blame] | 57 | if (!pager) { |
| 58 | if (!pager_program) |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 59 | git_config(git_default_config, NULL); |
Brian Gernhardt | 54adf37 | 2007-07-03 14:18:11 -0400 | [diff] [blame] | 60 | pager = pager_program; |
Junio C Hamano | cad3a20 | 2007-08-06 21:08:43 -0700 | [diff] [blame] | 61 | } |
Brian Gernhardt | 54adf37 | 2007-07-03 14:18:11 -0400 | [diff] [blame] | 62 | if (!pager) |
Matthias Lederhofer | c27d205 | 2006-07-31 15:27:00 +0200 | [diff] [blame] | 63 | pager = getenv("PAGER"); |
| 64 | if (!pager) |
Junio C Hamano | a3d023d | 2009-10-30 20:45:34 -0500 | [diff] [blame] | 65 | pager = DEFAULT_PAGER; |
Junio C Hamano | caef71a | 2006-04-16 01:46:08 -0700 | [diff] [blame] | 66 | else if (!*pager || !strcmp(pager, "cat")) |
Jonathan Nieder | 6361824 | 2009-10-30 20:41:27 -0500 | [diff] [blame] | 67 | pager = NULL; |
| 68 | |
| 69 | return pager; |
| 70 | } |
| 71 | |
| 72 | void setup_pager(void) |
| 73 | { |
Jonathan Nieder | 64778d2 | 2010-02-14 05:59:59 -0600 | [diff] [blame] | 74 | const char *pager = git_pager(isatty(1)); |
Jonathan Nieder | 6361824 | 2009-10-30 20:41:27 -0500 | [diff] [blame] | 75 | |
Nguyễn Thái Ngọc Duy | 88e8f90 | 2012-04-13 17:54:34 +0700 | [diff] [blame] | 76 | if (!pager || pager_in_use()) |
Johannes Schindelin | 402461a | 2006-04-16 04:44:25 +0200 | [diff] [blame] | 77 | return; |
| 78 | |
Zbigniew Jędrzejewski-Szmek | ad6c373 | 2012-02-12 15:12:32 +0100 | [diff] [blame] | 79 | /* |
| 80 | * force computing the width of the terminal before we redirect |
| 81 | * the standard output to the pager. |
| 82 | */ |
| 83 | (void) term_columns(); |
| 84 | |
Jeff King | 2e6c012 | 2011-08-17 22:02:29 -0700 | [diff] [blame] | 85 | setenv("GIT_PAGER_IN_USE", "true", 1); |
Junio C Hamano | 85fb65e | 2006-06-06 16:58:40 -0700 | [diff] [blame] | 86 | |
Johannes Sixt | bfdd9ff | 2007-12-08 21:28:41 +0100 | [diff] [blame] | 87 | /* spawn the pager */ |
Jeff King | ac0ba18 | 2009-12-30 05:53:57 -0500 | [diff] [blame] | 88 | pager_argv[0] = pager; |
| 89 | pager_process.use_shell = 1; |
Jeff King | ea27a18 | 2008-07-22 03:14:12 -0400 | [diff] [blame] | 90 | pager_process.argv = pager_argv; |
| 91 | pager_process.in = -1; |
Johannes Sixt | 25fc178 | 2009-09-11 19:45:07 +0200 | [diff] [blame] | 92 | if (!getenv("LESS")) { |
| 93 | static const char *env[] = { "LESS=FRSX", NULL }; |
| 94 | pager_process.env = env; |
| 95 | } |
Frank Li | 71064e3 | 2009-09-16 10:20:22 +0200 | [diff] [blame] | 96 | #ifndef WIN32 |
Jeff King | ea27a18 | 2008-07-22 03:14:12 -0400 | [diff] [blame] | 97 | pager_process.preexec_cb = pager_preexec; |
| 98 | #endif |
Johannes Sixt | bfdd9ff | 2007-12-08 21:28:41 +0100 | [diff] [blame] | 99 | if (start_command(&pager_process)) |
| 100 | return; |
| 101 | |
| 102 | /* original process continues, but writes to the pipe */ |
| 103 | dup2(pager_process.in, 1); |
Junio C Hamano | a833502 | 2008-12-15 00:33:34 -0800 | [diff] [blame] | 104 | if (isatty(2)) |
| 105 | dup2(pager_process.in, 2); |
Johannes Sixt | bfdd9ff | 2007-12-08 21:28:41 +0100 | [diff] [blame] | 106 | close(pager_process.in); |
| 107 | |
| 108 | /* this makes sure that the parent terminates after the pager */ |
Jeff King | a3da882 | 2009-01-22 01:03:28 -0500 | [diff] [blame] | 109 | sigchain_push_common(wait_for_pager_signal); |
Johannes Sixt | bfdd9ff | 2007-12-08 21:28:41 +0100 | [diff] [blame] | 110 | atexit(wait_for_pager); |
Linus Torvalds | f67b45f | 2006-02-28 11:26:21 -0800 | [diff] [blame] | 111 | } |
Jeff King | 6e9af86 | 2007-12-11 01:27:33 -0500 | [diff] [blame] | 112 | |
| 113 | int pager_in_use(void) |
| 114 | { |
| 115 | const char *env; |
Jeff King | 6e9af86 | 2007-12-11 01:27:33 -0500 | [diff] [blame] | 116 | env = getenv("GIT_PAGER_IN_USE"); |
| 117 | return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0; |
| 118 | } |
Zbigniew Jędrzejewski-Szmek | ad6c373 | 2012-02-12 15:12:32 +0100 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * Return cached value (if set) or $COLUMNS environment variable (if |
| 122 | * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive), |
| 123 | * and default to 80 if all else fails. |
| 124 | */ |
| 125 | int term_columns(void) |
| 126 | { |
| 127 | static int term_columns_at_startup; |
| 128 | |
| 129 | char *col_string; |
| 130 | int n_cols; |
| 131 | |
| 132 | if (term_columns_at_startup) |
| 133 | return term_columns_at_startup; |
| 134 | |
| 135 | term_columns_at_startup = 80; |
| 136 | |
| 137 | col_string = getenv("COLUMNS"); |
| 138 | if (col_string && (n_cols = atoi(col_string)) > 0) |
| 139 | term_columns_at_startup = n_cols; |
| 140 | #ifdef TIOCGWINSZ |
| 141 | else { |
| 142 | struct winsize ws; |
| 143 | if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col) |
| 144 | term_columns_at_startup = ws.ws_col; |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | return term_columns_at_startup; |
| 149 | } |
Junio C Hamano | 4d9e079 | 2012-02-20 00:15:11 -0800 | [diff] [blame] | 150 | |
| 151 | /* |
Zbigniew Jędrzejewski-Szmek | ec7ff5b | 2012-02-12 15:16:20 +0100 | [diff] [blame] | 152 | * How many columns do we need to show this number in decimal? |
| 153 | */ |
| 154 | int decimal_width(int number) |
| 155 | { |
| 156 | int i, width; |
| 157 | |
| 158 | for (width = 1, i = 10; i <= number; width++) |
| 159 | i *= 10; |
| 160 | return width; |
| 161 | } |