| 1 | #include "git-compat-util.h" |
| 2 | #include "config.h" |
| 3 | #include "editor.h" |
| 4 | #include "pager.h" |
| 5 | #include "run-command.h" |
| 6 | #include "sigchain.h" |
| 7 | #include "alias.h" |
| 8 | #include "repository.h" |
| 9 | #include "environment.h" |
| 10 | |
| 11 | int pager_use_color = 1; |
| 12 | |
| 13 | #ifndef DEFAULT_PAGER |
| 14 | #define DEFAULT_PAGER "less" |
| 15 | #endif |
| 16 | |
| 17 | static struct child_process pager_process; |
| 18 | static int old_fd1 = -1, old_fd2 = -1; |
| 19 | |
| 20 | /* Is the value coming back from term_columns() just a guess? */ |
| 21 | static int term_columns_guessed; |
| 22 | |
| 23 | |
| 24 | static void close_pager_fds(void) |
| 25 | { |
| 26 | /* signal EOF to pager */ |
| 27 | close(1); |
| 28 | if (old_fd2 != -1) |
| 29 | close(2); |
| 30 | } |
| 31 | |
| 32 | static void finish_pager(void) |
| 33 | { |
| 34 | fflush(stdout); |
| 35 | fflush(stderr); |
| 36 | close_pager_fds(); |
| 37 | finish_command(&pager_process); |
| 38 | } |
| 39 | |
| 40 | static void wait_for_pager_atexit(void) |
| 41 | { |
| 42 | if (old_fd1 == -1) |
| 43 | return; |
| 44 | |
| 45 | finish_pager(); |
| 46 | } |
| 47 | |
| 48 | void wait_for_pager(void) |
| 49 | { |
| 50 | if (old_fd1 == -1) |
| 51 | return; |
| 52 | |
| 53 | finish_pager(); |
| 54 | sigchain_pop_common(); |
| 55 | unsetenv("GIT_PAGER_IN_USE"); |
| 56 | dup2(old_fd1, 1); |
| 57 | close(old_fd1); |
| 58 | old_fd1 = -1; |
| 59 | if (old_fd2 != -1) { |
| 60 | dup2(old_fd2, 2); |
| 61 | close(old_fd2); |
| 62 | old_fd2 = -1; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static void wait_for_pager_signal(int signo) |
| 67 | { |
| 68 | if (old_fd1 == -1) |
| 69 | return; |
| 70 | |
| 71 | close_pager_fds(); |
| 72 | finish_command_in_signal(&pager_process); |
| 73 | sigchain_pop(signo); |
| 74 | raise(signo); |
| 75 | } |
| 76 | |
| 77 | static int core_pager_config(const char *var, const char *value, |
| 78 | const struct config_context *ctx UNUSED, |
| 79 | void *data) |
| 80 | { |
| 81 | struct repository *r = data; |
| 82 | |
| 83 | if (!strcmp(var, "core.pager")) { |
| 84 | struct repo_config_values *cfg = repo_config_values(r); |
| 85 | |
| 86 | FREE_AND_NULL(cfg->pager_program); |
| 87 | return git_config_string(&cfg->pager_program, var, value); |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | const char *git_pager(struct repository *r, int stdout_is_tty) |
| 94 | { |
| 95 | const char *pager; |
| 96 | |
| 97 | if (!stdout_is_tty) |
| 98 | return NULL; |
| 99 | |
| 100 | pager = getenv("GIT_PAGER"); |
| 101 | if (!pager) { |
| 102 | struct repo_config_values *cfg = repo_config_values(r); |
| 103 | |
| 104 | if (!cfg->pager_program) |
| 105 | read_early_config(r, |
| 106 | core_pager_config, r); |
| 107 | pager = cfg->pager_program; |
| 108 | } |
| 109 | if (!pager) |
| 110 | pager = getenv("PAGER"); |
| 111 | if (!pager) |
| 112 | pager = DEFAULT_PAGER; |
| 113 | if (!*pager || !strcmp(pager, "cat")) |
| 114 | pager = NULL; |
| 115 | |
| 116 | return pager; |
| 117 | } |
| 118 | |
| 119 | static void setup_pager_env(struct strvec *env) |
| 120 | { |
| 121 | char **argv; |
| 122 | int i; |
| 123 | char *pager_env = xstrdup(PAGER_ENV); |
| 124 | /* split_cmdline splits in place, so we know the result is writable */ |
| 125 | int n = split_cmdline(pager_env, (const char ***)&argv); |
| 126 | |
| 127 | if (n < 0) |
| 128 | die("malformed build-time PAGER_ENV: %s", |
| 129 | split_cmdline_strerror(n)); |
| 130 | |
| 131 | for (i = 0; i < n; i++) { |
| 132 | char *cp = strchr(argv[i], '='); |
| 133 | |
| 134 | if (!cp) |
| 135 | die("malformed build-time PAGER_ENV"); |
| 136 | |
| 137 | *cp = '\0'; |
| 138 | if (!getenv(argv[i])) { |
| 139 | *cp = '='; |
| 140 | strvec_push(env, argv[i]); |
| 141 | } |
| 142 | } |
| 143 | free(pager_env); |
| 144 | free(argv); |
| 145 | } |
| 146 | |
| 147 | void prepare_pager_args(struct child_process *pager_process, const char *pager) |
| 148 | { |
| 149 | strvec_push(&pager_process->args, pager); |
| 150 | pager_process->use_shell = 1; |
| 151 | setup_pager_env(&pager_process->env); |
| 152 | pager_process->trace2_child_class = "pager"; |
| 153 | } |
| 154 | |
| 155 | void setup_pager(struct repository *r) |
| 156 | { |
| 157 | static int once = 0; |
| 158 | const char *pager = git_pager(r, isatty(1)); |
| 159 | |
| 160 | if (!pager) |
| 161 | return; |
| 162 | |
| 163 | /* |
| 164 | * After we redirect standard output, we won't be able to use an ioctl |
| 165 | * to get the terminal size. Let's grab it now, and then set $COLUMNS |
| 166 | * to communicate it to any sub-processes. |
| 167 | */ |
| 168 | { |
| 169 | char buf[64]; |
| 170 | xsnprintf(buf, sizeof(buf), "%d", term_columns()); |
| 171 | if (!term_columns_guessed) |
| 172 | setenv("COLUMNS", buf, 0); |
| 173 | } |
| 174 | |
| 175 | setenv("GIT_PAGER_IN_USE", "true", 1); |
| 176 | |
| 177 | child_process_init(&pager_process); |
| 178 | |
| 179 | /* spawn the pager */ |
| 180 | prepare_pager_args(&pager_process, pager); |
| 181 | pager_process.in = -1; |
| 182 | strvec_push(&pager_process.env, "GIT_PAGER_IN_USE"); |
| 183 | if (start_command(&pager_process)) |
| 184 | die("unable to execute pager '%s'", pager); |
| 185 | |
| 186 | /* original process continues, but writes to the pipe */ |
| 187 | old_fd1 = dup(1); |
| 188 | dup2(pager_process.in, 1); |
| 189 | if (isatty(2)) { |
| 190 | old_fd2 = dup(2); |
| 191 | dup2(pager_process.in, 2); |
| 192 | } |
| 193 | close(pager_process.in); |
| 194 | |
| 195 | sigchain_push_common(wait_for_pager_signal); |
| 196 | |
| 197 | if (!once) { |
| 198 | once++; |
| 199 | atexit(wait_for_pager_atexit); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | int pager_in_use(void) |
| 204 | { |
| 205 | return git_env_bool("GIT_PAGER_IN_USE", 0); |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Return cached value (if set) or $COLUMNS environment variable (if |
| 210 | * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive), |
| 211 | * and default to 80 if all else fails. |
| 212 | */ |
| 213 | int term_columns(void) |
| 214 | { |
| 215 | static int term_columns_at_startup; |
| 216 | |
| 217 | char *col_string; |
| 218 | int n_cols; |
| 219 | |
| 220 | if (term_columns_at_startup) |
| 221 | return term_columns_at_startup; |
| 222 | |
| 223 | term_columns_at_startup = 80; |
| 224 | term_columns_guessed = 1; |
| 225 | |
| 226 | col_string = getenv("COLUMNS"); |
| 227 | if (col_string && (n_cols = atoi(col_string)) > 0) { |
| 228 | term_columns_at_startup = n_cols; |
| 229 | term_columns_guessed = 0; |
| 230 | } |
| 231 | #ifdef TIOCGWINSZ |
| 232 | else { |
| 233 | struct winsize ws; |
| 234 | if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col) { |
| 235 | term_columns_at_startup = ws.ws_col; |
| 236 | term_columns_guessed = 0; |
| 237 | } |
| 238 | } |
| 239 | #endif |
| 240 | |
| 241 | return term_columns_at_startup; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Clear the entire line, leave cursor in first column. |
| 246 | */ |
| 247 | void term_clear_line(void) |
| 248 | { |
| 249 | if (!isatty(2)) |
| 250 | return; |
| 251 | if (is_terminal_dumb()) |
| 252 | /* |
| 253 | * Fall back to print a terminal width worth of space |
| 254 | * characters (hoping that the terminal is still as wide |
| 255 | * as it was upon the first call to term_columns()). |
| 256 | */ |
| 257 | fprintf(stderr, "\r%*s\r", term_columns(), ""); |
| 258 | else |
| 259 | /* |
| 260 | * On non-dumb terminals use an escape sequence to clear |
| 261 | * the whole line, no matter how wide the terminal. |
| 262 | */ |
| 263 | fputs("\r\033[K", stderr); |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * How many columns do we need to show this number in decimal? |
| 268 | */ |
| 269 | int decimal_width(uintmax_t number) |
| 270 | { |
| 271 | int width; |
| 272 | |
| 273 | for (width = 1; number >= 10; width++) |
| 274 | number /= 10; |
| 275 | return width; |
| 276 | } |
| 277 | |
| 278 | struct pager_command_config_data { |
| 279 | const char *cmd; |
| 280 | int want; |
| 281 | char *value; |
| 282 | }; |
| 283 | |
| 284 | static int pager_command_config(const char *var, const char *value, |
| 285 | const struct config_context *ctx UNUSED, |
| 286 | void *vdata) |
| 287 | { |
| 288 | struct pager_command_config_data *data = vdata; |
| 289 | const char *cmd; |
| 290 | |
| 291 | if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) { |
| 292 | int b = git_parse_maybe_bool(value); |
| 293 | if (b >= 0) |
| 294 | data->want = b; |
| 295 | else { |
| 296 | data->want = 1; |
| 297 | data->value = xstrdup(value); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */ |
| 305 | int check_pager_config(struct repository *r, const char *cmd) |
| 306 | { |
| 307 | struct pager_command_config_data data; |
| 308 | |
| 309 | data.cmd = cmd; |
| 310 | data.want = -1; |
| 311 | data.value = NULL; |
| 312 | |
| 313 | read_early_config(r, pager_command_config, &data); |
| 314 | |
| 315 | if (data.value) { |
| 316 | struct repo_config_values *cfg = repo_config_values(r); |
| 317 | |
| 318 | free(cfg->pager_program); |
| 319 | cfg->pager_program = data.value; |
| 320 | } |
| 321 | return data.want; |
| 322 | } |