pager.con commit Merge branch 'nd/doc-header' (6b0f1d9)
   1#include "cache.h"
   2#include "config.h"
   3#include "run-command.h"
   4#include "sigchain.h"
   5
   6#ifndef DEFAULT_PAGER
   7#define DEFAULT_PAGER "less"
   8#endif
   9
  10static struct child_process pager_process = CHILD_PROCESS_INIT;
  11static const char *pager_program;
  12
  13static void wait_for_pager(int in_signal)
  14{
  15        if (!in_signal) {
  16                fflush(stdout);
  17                fflush(stderr);
  18        }
  19        /* signal EOF to pager */
  20        close(1);
  21        close(2);
  22        if (in_signal)
  23                finish_command_in_signal(&pager_process);
  24        else
  25                finish_command(&pager_process);
  26}
  27
  28static void wait_for_pager_atexit(void)
  29{
  30        wait_for_pager(0);
  31}
  32
  33static void wait_for_pager_signal(int signo)
  34{
  35        wait_for_pager(1);
  36        sigchain_pop(signo);
  37        raise(signo);
  38}
  39
  40static int core_pager_config(const char *var, const char *value, void *data)
  41{
  42        if (!strcmp(var, "core.pager"))
  43                return git_config_string(&pager_program, var, value);
  44        return 0;
  45}
  46
  47const char *git_pager(int stdout_is_tty)
  48{
  49        const char *pager;
  50
  51        if (!stdout_is_tty)
  52                return NULL;
  53
  54        pager = getenv("GIT_PAGER");
  55        if (!pager) {
  56                if (!pager_program)
  57                        read_early_config(core_pager_config, NULL);
  58                pager = pager_program;
  59        }
  60        if (!pager)
  61                pager = getenv("PAGER");
  62        if (!pager)
  63                pager = DEFAULT_PAGER;
  64        if (!*pager || !strcmp(pager, "cat"))
  65                pager = NULL;
  66
  67        return pager;
  68}
  69
  70static void setup_pager_env(struct argv_array *env)
  71{
  72        const char **argv;
  73        int i;
  74        char *pager_env = xstrdup(PAGER_ENV);
  75        int n = split_cmdline(pager_env, &argv);
  76
  77        if (n < 0)
  78                die("malformed build-time PAGER_ENV: %s",
  79                        split_cmdline_strerror(n));
  80
  81        for (i = 0; i < n; i++) {
  82                char *cp = strchr(argv[i], '=');
  83
  84                if (!cp)
  85                        die("malformed build-time PAGER_ENV");
  86
  87                *cp = '\0';
  88                if (!getenv(argv[i])) {
  89                        *cp = '=';
  90                        argv_array_push(env, argv[i]);
  91                }
  92        }
  93        free(pager_env);
  94        free(argv);
  95}
  96
  97void prepare_pager_args(struct child_process *pager_process, const char *pager)
  98{
  99        argv_array_push(&pager_process->args, pager);
 100        pager_process->use_shell = 1;
 101        setup_pager_env(&pager_process->env_array);
 102}
 103
 104void setup_pager(void)
 105{
 106        const char *pager = git_pager(isatty(1));
 107
 108        if (!pager)
 109                return;
 110
 111        /*
 112         * After we redirect standard output, we won't be able to use an ioctl
 113         * to get the terminal size. Let's grab it now, and then set $COLUMNS
 114         * to communicate it to any sub-processes.
 115         */
 116        {
 117                char buf[64];
 118                xsnprintf(buf, sizeof(buf), "%d", term_columns());
 119                setenv("COLUMNS", buf, 0);
 120        }
 121
 122        setenv("GIT_PAGER_IN_USE", "true", 1);
 123
 124        /* spawn the pager */
 125        prepare_pager_args(&pager_process, pager);
 126        pager_process.in = -1;
 127        argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
 128        if (start_command(&pager_process))
 129                return;
 130
 131        /* original process continues, but writes to the pipe */
 132        dup2(pager_process.in, 1);
 133        if (isatty(2))
 134                dup2(pager_process.in, 2);
 135        close(pager_process.in);
 136
 137        /* this makes sure that the parent terminates after the pager */
 138        sigchain_push_common(wait_for_pager_signal);
 139        atexit(wait_for_pager_atexit);
 140}
 141
 142int pager_in_use(void)
 143{
 144        return git_env_bool("GIT_PAGER_IN_USE", 0);
 145}
 146
 147/*
 148 * Return cached value (if set) or $COLUMNS environment variable (if
 149 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
 150 * and default to 80 if all else fails.
 151 */
 152int term_columns(void)
 153{
 154        static int term_columns_at_startup;
 155
 156        char *col_string;
 157        int n_cols;
 158
 159        if (term_columns_at_startup)
 160                return term_columns_at_startup;
 161
 162        term_columns_at_startup = 80;
 163
 164        col_string = getenv("COLUMNS");
 165        if (col_string && (n_cols = atoi(col_string)) > 0)
 166                term_columns_at_startup = n_cols;
 167#ifdef TIOCGWINSZ
 168        else {
 169                struct winsize ws;
 170                if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
 171                        term_columns_at_startup = ws.ws_col;
 172        }
 173#endif
 174
 175        return term_columns_at_startup;
 176}
 177
 178/*
 179 * How many columns do we need to show this number in decimal?
 180 */
 181int decimal_width(uintmax_t number)
 182{
 183        int width;
 184
 185        for (width = 1; number >= 10; width++)
 186                number /= 10;
 187        return width;
 188}
 189
 190struct pager_command_config_data {
 191        const char *cmd;
 192        int want;
 193        char *value;
 194};
 195
 196static int pager_command_config(const char *var, const char *value, void *vdata)
 197{
 198        struct pager_command_config_data *data = vdata;
 199        const char *cmd;
 200
 201        if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
 202                int b = git_parse_maybe_bool(value);
 203                if (b >= 0)
 204                        data->want = b;
 205                else {
 206                        data->want = 1;
 207                        data->value = xstrdup(value);
 208                }
 209        }
 210
 211        return 0;
 212}
 213
 214/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
 215int check_pager_config(const char *cmd)
 216{
 217        struct pager_command_config_data data;
 218
 219        data.cmd = cmd;
 220        data.want = -1;
 221        data.value = NULL;
 222
 223        read_early_config(pager_command_config, &data);
 224
 225        if (data.value)
 226                pager_program = data.value;
 227        return data.want;
 228}