00c8baace0f9bf73c8f809639babce775310237b
   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                        git_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
  69void prepare_pager_args(struct child_process *pager_process, const char *pager)
  70{
  71        argv_array_push(&pager_process->args, pager);
  72        pager_process->use_shell = 1;
  73        if (!getenv("LESS"))
  74                argv_array_push(&pager_process->env_array, "LESS=FRX");
  75        if (!getenv("LV"))
  76                argv_array_push(&pager_process->env_array, "LV=-c");
  77}
  78
  79void setup_pager(void)
  80{
  81        const char *pager = git_pager(isatty(1));
  82
  83        if (!pager)
  84                return;
  85
  86        /*
  87         * force computing the width of the terminal before we redirect
  88         * the standard output to the pager.
  89         */
  90        (void) term_columns();
  91
  92        setenv("GIT_PAGER_IN_USE", "true", 1);
  93
  94        /* spawn the pager */
  95        prepare_pager_args(&pager_process, pager);
  96        pager_process.in = -1;
  97        argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
  98        if (start_command(&pager_process))
  99                return;
 100
 101        /* original process continues, but writes to the pipe */
 102        dup2(pager_process.in, 1);
 103        if (isatty(2))
 104                dup2(pager_process.in, 2);
 105        close(pager_process.in);
 106
 107        /* this makes sure that the parent terminates after the pager */
 108        sigchain_push_common(wait_for_pager_signal);
 109        atexit(wait_for_pager_atexit);
 110}
 111
 112int pager_in_use(void)
 113{
 114        const char *env;
 115        env = getenv("GIT_PAGER_IN_USE");
 116        return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
 117}
 118
 119/*
 120 * Return cached value (if set) or $COLUMNS environment variable (if
 121 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
 122 * and default to 80 if all else fails.
 123 */
 124int term_columns(void)
 125{
 126        static int term_columns_at_startup;
 127
 128        char *col_string;
 129        int n_cols;
 130
 131        if (term_columns_at_startup)
 132                return term_columns_at_startup;
 133
 134        term_columns_at_startup = 80;
 135
 136        col_string = getenv("COLUMNS");
 137        if (col_string && (n_cols = atoi(col_string)) > 0)
 138                term_columns_at_startup = n_cols;
 139#ifdef TIOCGWINSZ
 140        else {
 141                struct winsize ws;
 142                if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
 143                        term_columns_at_startup = ws.ws_col;
 144        }
 145#endif
 146
 147        return term_columns_at_startup;
 148}
 149
 150/*
 151 * How many columns do we need to show this number in decimal?
 152 */
 153int decimal_width(uintmax_t number)
 154{
 155        int width;
 156
 157        for (width = 1; number >= 10; width++)
 158                number /= 10;
 159        return width;
 160}
 161
 162/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
 163int check_pager_config(const char *cmd)
 164{
 165        int want = -1;
 166        struct strbuf key = STRBUF_INIT;
 167        const char *value = NULL;
 168        strbuf_addf(&key, "pager.%s", cmd);
 169        if (git_config_key_is_valid(key.buf) &&
 170            !git_config_get_value(key.buf, &value)) {
 171                int b = git_config_maybe_bool(key.buf, value);
 172                if (b >= 0)
 173                        want = b;
 174                else {
 175                        want = 1;
 176                        pager_program = xstrdup(value);
 177                }
 178        }
 179        strbuf_release(&key);
 180        return want;
 181}