pager.c 1.9 KB
Newer Older
1
#include "cache.h"
2
#include "run-command.h"
3
#include "sigchain.h"
4 5

/*
J
Johannes Sixt 已提交
6 7
 * This is split up from the rest of git so that we can do
 * something different on Windows.
8 9
 */

10 11
static int spawned_pager;

J
Johannes Sixt 已提交
12
#ifndef __MINGW32__
13
static void pager_preexec(void)
14
{
15 16 17 18 19 20 21 22 23
	/*
	 * Work around bug in "less" by not starting it until we
	 * have real input
	 */
	fd_set in;

	FD_ZERO(&in);
	FD_SET(0, &in);
	select(1, &in, NULL, &in, NULL);
24
}
25
#endif
J
Johannes Sixt 已提交
26 27

static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
28 29
static struct child_process pager_process;

J
Johannes Sixt 已提交
30 31 32 33 34 35 36 37 38
static void wait_for_pager(void)
{
	fflush(stdout);
	fflush(stderr);
	/* signal EOF to pager */
	close(1);
	close(2);
	finish_command(&pager_process);
}
39

40 41 42 43 44 45 46
static void wait_for_pager_signal(int signo)
{
	wait_for_pager();
	sigchain_pop(signo);
	raise(signo);
}

47 48
void setup_pager(void)
{
49
	const char *pager = getenv("GIT_PAGER");
50 51 52

	if (!isatty(1))
		return;
53 54
	if (!pager) {
		if (!pager_program)
55
			git_config(git_default_config, NULL);
56
		pager = pager_program;
57
	}
58 59
	if (!pager)
		pager = getenv("PAGER");
60 61
	if (!pager)
		pager = "less";
J
Junio C Hamano 已提交
62
	else if (!*pager || !strcmp(pager, "cat"))
63 64
		return;

65
	spawned_pager = 1; /* means we are emitting to terminal */
J
Junio C Hamano 已提交
66

J
Johannes Sixt 已提交
67 68
	/* spawn the pager */
	pager_argv[2] = pager;
69 70
	pager_process.argv = pager_argv;
	pager_process.in = -1;
71 72 73 74
	if (!getenv("LESS")) {
		static const char *env[] = { "LESS=FRSX", NULL };
		pager_process.env = env;
	}
75 76 77
#ifndef __MINGW32__
	pager_process.preexec_cb = pager_preexec;
#endif
J
Johannes Sixt 已提交
78 79 80 81 82
	if (start_command(&pager_process))
		return;

	/* original process continues, but writes to the pipe */
	dup2(pager_process.in, 1);
83 84
	if (isatty(2))
		dup2(pager_process.in, 2);
J
Johannes Sixt 已提交
85 86 87
	close(pager_process.in);

	/* this makes sure that the parent terminates after the pager */
88
	sigchain_push_common(wait_for_pager_signal);
J
Johannes Sixt 已提交
89
	atexit(wait_for_pager);
90
}
91 92 93 94 95 96 97 98 99 100 101

int pager_in_use(void)
{
	const char *env;

	if (spawned_pager)
		return 1;

	env = getenv("GIT_PAGER_IN_USE");
	return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
}