pager.con commit Merge branch 'master' of git://repo.or.cz/git-gui (d9f4059)
   1#include "cache.h"
   2
   3/*
   4 * This is split up from the rest of git so that we might do
   5 * something different on Windows, for example.
   6 */
   7
   8static void run_pager(const char *pager)
   9{
  10        /*
  11         * Work around bug in "less" by not starting it until we
  12         * have real input
  13         */
  14        fd_set in;
  15
  16        FD_ZERO(&in);
  17        FD_SET(0, &in);
  18        select(1, &in, NULL, &in, NULL);
  19
  20        execlp(pager, pager, NULL);
  21        execl("/bin/sh", "sh", "-c", pager, NULL);
  22}
  23
  24void setup_pager(void)
  25{
  26        pid_t pid;
  27        int fd[2];
  28        const char *pager = getenv("GIT_PAGER");
  29
  30        if (!isatty(1))
  31                return;
  32        if (!pager) {
  33                if (!pager_program)
  34                        git_config(git_default_config);
  35                pager = pager_program;
  36        }
  37        if (!pager)
  38                pager = getenv("PAGER");
  39        if (!pager)
  40                pager = "less";
  41        else if (!*pager || !strcmp(pager, "cat"))
  42                return;
  43
  44        pager_in_use = 1; /* means we are emitting to terminal */
  45
  46        if (pipe(fd) < 0)
  47                return;
  48        pid = fork();
  49        if (pid < 0) {
  50                close(fd[0]);
  51                close(fd[1]);
  52                return;
  53        }
  54
  55        /* return in the child */
  56        if (!pid) {
  57                dup2(fd[1], 1);
  58                close(fd[0]);
  59                close(fd[1]);
  60                return;
  61        }
  62
  63        /* The original process turns into the PAGER */
  64        dup2(fd[0], 0);
  65        close(fd[0]);
  66        close(fd[1]);
  67
  68        setenv("LESS", "FRSX", 0);
  69        run_pager(pager);
  70        die("unable to execute pager '%s'", pager);
  71        exit(255);
  72}