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

/*
5 6
 * This is split up from the rest of git so that we can do
 * something different on Windows.
7 8
 */

9 10
static int spawned_pager;

11
#ifndef __MINGW32__
12
static void pager_preexec(void)
13
{
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
	setenv("LESS", "FRSX", 0);
25
}
26
#endif
27 28

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

31 32 33 34 35 36 37 38 39
static void wait_for_pager(void)
{
	fflush(stdout);
	fflush(stderr);
	/* signal EOF to pager */
	close(1);
	close(2);
	finish_command(&pager_process);
}
40 41 42

void setup_pager(void)
{
43
	const char *pager = getenv("GIT_PAGER");
44 45 46

	if (!isatty(1))
		return;
47 48
	if (!pager) {
		if (!pager_program)
49
			git_config(git_default_config, NULL);
50
		pager = pager_program;
51
	}
52 53
	if (!pager)
		pager = getenv("PAGER");
54 55
	if (!pager)
		pager = "less";
Junio C Hamano's avatar
Junio C Hamano committed
56
	else if (!*pager || !strcmp(pager, "cat"))
57 58
		return;

59
	spawned_pager = 1; /* means we are emitting to terminal */
60

61 62
	/* spawn the pager */
	pager_argv[2] = pager;
63 64 65 66 67
	pager_process.argv = pager_argv;
	pager_process.in = -1;
#ifndef __MINGW32__
	pager_process.preexec_cb = pager_preexec;
#endif
68 69 70 71 72
	if (start_command(&pager_process))
		return;

	/* original process continues, but writes to the pipe */
	dup2(pager_process.in, 1);
73 74
	if (isatty(2))
		dup2(pager_process.in, 2);
75 76 77 78
	close(pager_process.in);

	/* this makes sure that the parent terminates after the pager */
	atexit(wait_for_pager);
79
}
80 81 82 83 84 85 86 87 88 89 90

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;
}