pager.con commit t5551: do not assume the "matching" push is the default (bd7ac59)
   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
   9struct pager_config {
  10        const char *cmd;
  11        int want;
  12        char *value;
  13};
  14
  15/*
  16 * This is split up from the rest of git so that we can do
  17 * something different on Windows.
  18 */
  19
  20static const char *pager_argv[] = { NULL, NULL };
  21static struct child_process pager_process;
  22
  23static void wait_for_pager(void)
  24{
  25        fflush(stdout);
  26        fflush(stderr);
  27        /* signal EOF to pager */
  28        close(1);
  29        close(2);
  30        finish_command(&pager_process);
  31}
  32
  33static void wait_for_pager_signal(int signo)
  34{
  35        wait_for_pager();
  36        sigchain_pop(signo);
  37        raise(signo);
  38}
  39
  40const char *git_pager(int stdout_is_tty)
  41{
  42        const char *pager;
  43
  44        if (!stdout_is_tty)
  45                return NULL;
  46
  47        pager = getenv("GIT_PAGER");
  48        if (!pager) {
  49                if (!pager_program)
  50                        git_config(git_default_config, NULL);
  51                pager = pager_program;
  52        }
  53        if (!pager)
  54                pager = getenv("PAGER");
  55        if (!pager)
  56                pager = DEFAULT_PAGER;
  57        else if (!*pager || !strcmp(pager, "cat"))
  58                pager = NULL;
  59
  60        return pager;
  61}
  62
  63void setup_pager(void)
  64{
  65        const char *pager = git_pager(isatty(1));
  66
  67        if (!pager || pager_in_use())
  68                return;
  69
  70        /*
  71         * force computing the width of the terminal before we redirect
  72         * the standard output to the pager.
  73         */
  74        (void) term_columns();
  75
  76        setenv("GIT_PAGER_IN_USE", "true", 1);
  77
  78        /* spawn the pager */
  79        pager_argv[0] = pager;
  80        pager_process.use_shell = 1;
  81        pager_process.argv = pager_argv;
  82        pager_process.in = -1;
  83        if (!getenv("LESS")) {
  84                static const char *env[] = { "LESS=FRSX", NULL };
  85                pager_process.env = env;
  86        }
  87        if (start_command(&pager_process))
  88                return;
  89
  90        /* original process continues, but writes to the pipe */
  91        dup2(pager_process.in, 1);
  92        if (isatty(2))
  93                dup2(pager_process.in, 2);
  94        close(pager_process.in);
  95
  96        /* this makes sure that the parent terminates after the pager */
  97        sigchain_push_common(wait_for_pager_signal);
  98        atexit(wait_for_pager);
  99}
 100
 101int pager_in_use(void)
 102{
 103        const char *env;
 104        env = getenv("GIT_PAGER_IN_USE");
 105        return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
 106}
 107
 108/*
 109 * Return cached value (if set) or $COLUMNS environment variable (if
 110 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
 111 * and default to 80 if all else fails.
 112 */
 113int term_columns(void)
 114{
 115        static int term_columns_at_startup;
 116
 117        char *col_string;
 118        int n_cols;
 119
 120        if (term_columns_at_startup)
 121                return term_columns_at_startup;
 122
 123        term_columns_at_startup = 80;
 124
 125        col_string = getenv("COLUMNS");
 126        if (col_string && (n_cols = atoi(col_string)) > 0)
 127                term_columns_at_startup = n_cols;
 128#ifdef TIOCGWINSZ
 129        else {
 130                struct winsize ws;
 131                if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
 132                        term_columns_at_startup = ws.ws_col;
 133        }
 134#endif
 135
 136        return term_columns_at_startup;
 137}
 138
 139/*
 140 * How many columns do we need to show this number in decimal?
 141 */
 142int decimal_width(int number)
 143{
 144        int i, width;
 145
 146        for (width = 1, i = 10; i <= number; width++)
 147                i *= 10;
 148        return width;
 149}
 150
 151static int pager_command_config(const char *var, const char *value, void *data)
 152{
 153        struct pager_config *c = data;
 154        if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd)) {
 155                int b = git_config_maybe_bool(var, value);
 156                if (b >= 0)
 157                        c->want = b;
 158                else {
 159                        c->want = 1;
 160                        c->value = xstrdup(value);
 161                }
 162        }
 163        return 0;
 164}
 165
 166/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
 167int check_pager_config(const char *cmd)
 168{
 169        struct pager_config c;
 170        c.cmd = cmd;
 171        c.want = -1;
 172        c.value = NULL;
 173        git_config(pager_command_config, &c);
 174        if (c.value)
 175                pager_program = c.value;
 176        return c.want;
 177}