pager.c 868 字节
Newer Older
1 2 3 4 5 6 7
#include "cache.h"

/*
 * This is split up from the rest of git so that we might do
 * something different on Windows, for example.
 */

8
static void run_pager(const char *pager)
9
{
10
	execlp(pager, pager, NULL);
L
Linus Torvalds 已提交
11
	execl("/bin/sh", "sh", "-c", pager, NULL);
12 13 14 15 16 17
}

void setup_pager(void)
{
	pid_t pid;
	int fd[2];
18
	const char *pager = getenv("PAGER");
19 20 21

	if (!isatty(1))
		return;
22 23
	if (!pager)
		pager = "less";
J
Junio C Hamano 已提交
24
	else if (!*pager || !strcmp(pager, "cat"))
25 26
		return;

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
	if (pipe(fd) < 0)
		return;
	pid = fork();
	if (pid < 0) {
		close(fd[0]);
		close(fd[1]);
		return;
	}

	/* return in the child */
	if (!pid) {
		dup2(fd[1], 1);
		close(fd[0]);
		close(fd[1]);
		return;
	}

	/* The original process turns into the PAGER */
	dup2(fd[0], 0);
	close(fd[0]);
	close(fd[1]);

49 50
	setenv("LESS", "-S", 0);
	run_pager(pager);
L
Linus Torvalds 已提交
51
	die("unable to execute pager '%s'", pager);
52 53
	exit(255);
}