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