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