pager.con commit describe: Break annotated tag ties by tagger date (03e8b54)
   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
   9/*
  10 * This is split up from the rest of git so that we can do
  11 * something different on Windows.
  12 */
  13
  14static int spawned_pager;
  15
  16#ifndef WIN32
  17static void pager_preexec(void)
  18{
  19        /*
  20         * Work around bug in "less" by not starting it until we
  21         * have real input
  22         */
  23        fd_set in;
  24
  25        FD_ZERO(&in);
  26        FD_SET(0, &in);
  27        select(1, &in, NULL, &in, NULL);
  28}
  29#endif
  30
  31static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
  32static struct child_process pager_process;
  33
  34static void wait_for_pager(void)
  35{
  36        fflush(stdout);
  37        fflush(stderr);
  38        /* signal EOF to pager */
  39        close(1);
  40        close(2);
  41        finish_command(&pager_process);
  42}
  43
  44static void wait_for_pager_signal(int signo)
  45{
  46        wait_for_pager();
  47        sigchain_pop(signo);
  48        raise(signo);
  49}
  50
  51const char *git_pager(void)
  52{
  53        const char *pager;
  54
  55        if (!isatty(1))
  56                return NULL;
  57
  58        pager = getenv("GIT_PAGER");
  59        if (!pager) {
  60                if (!pager_program)
  61                        git_config(git_default_config, NULL);
  62                pager = pager_program;
  63        }
  64        if (!pager)
  65                pager = getenv("PAGER");
  66        if (!pager)
  67                pager = DEFAULT_PAGER;
  68        else if (!*pager || !strcmp(pager, "cat"))
  69                pager = NULL;
  70
  71        return pager;
  72}
  73
  74void setup_pager(void)
  75{
  76        const char *pager = git_pager();
  77
  78        if (!pager)
  79                return;
  80
  81        spawned_pager = 1; /* means we are emitting to terminal */
  82
  83        /* spawn the pager */
  84        pager_argv[2] = pager;
  85        pager_process.argv = pager_argv;
  86        pager_process.in = -1;
  87        if (!getenv("LESS")) {
  88                static const char *env[] = { "LESS=FRSX", NULL };
  89                pager_process.env = env;
  90        }
  91#ifndef WIN32
  92        pager_process.preexec_cb = pager_preexec;
  93#endif
  94        if (start_command(&pager_process))
  95                return;
  96
  97        /* original process continues, but writes to the pipe */
  98        dup2(pager_process.in, 1);
  99        if (isatty(2))
 100                dup2(pager_process.in, 2);
 101        close(pager_process.in);
 102
 103        /* this makes sure that the parent terminates after the pager */
 104        sigchain_push_common(wait_for_pager_signal);
 105        atexit(wait_for_pager);
 106}
 107
 108int pager_in_use(void)
 109{
 110        const char *env;
 111
 112        if (spawned_pager)
 113                return 1;
 114
 115        env = getenv("GIT_PAGER_IN_USE");
 116        return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
 117}