2006-02-28 20:26:21 +01:00
|
|
|
#include "cache.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is split up from the rest of git so that we might do
|
|
|
|
* something different on Windows, for example.
|
|
|
|
*/
|
|
|
|
|
2007-12-11 07:27:33 +01:00
|
|
|
static int spawned_pager;
|
|
|
|
|
2006-04-16 04:44:25 +02:00
|
|
|
static void run_pager(const char *pager)
|
2006-02-28 20:26:21 +01:00
|
|
|
{
|
2007-01-24 20:21:10 +01:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
2006-04-16 04:44:25 +02:00
|
|
|
execlp(pager, pager, NULL);
|
2006-04-21 21:25:13 +02:00
|
|
|
execl("/bin/sh", "sh", "-c", pager, NULL);
|
2006-02-28 20:26:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setup_pager(void)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
int fd[2];
|
2006-07-31 15:27:00 +02:00
|
|
|
const char *pager = getenv("GIT_PAGER");
|
2006-02-28 20:26:21 +01:00
|
|
|
|
|
|
|
if (!isatty(1))
|
|
|
|
return;
|
2007-08-07 06:08:43 +02:00
|
|
|
if (!pager) {
|
|
|
|
if (!pager_program)
|
|
|
|
git_config(git_default_config);
|
2007-07-03 20:18:11 +02:00
|
|
|
pager = pager_program;
|
2007-08-07 06:08:43 +02:00
|
|
|
}
|
2006-07-31 15:27:00 +02:00
|
|
|
if (!pager)
|
|
|
|
pager = getenv("PAGER");
|
2006-04-16 04:44:25 +02:00
|
|
|
if (!pager)
|
|
|
|
pager = "less";
|
2006-04-16 10:46:08 +02:00
|
|
|
else if (!*pager || !strcmp(pager, "cat"))
|
2006-04-16 04:44:25 +02:00
|
|
|
return;
|
|
|
|
|
2007-12-11 07:27:33 +01:00
|
|
|
spawned_pager = 1; /* means we are emitting to terminal */
|
2006-06-07 01:58:40 +02:00
|
|
|
|
2006-02-28 20:26:21 +01:00
|
|
|
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]);
|
|
|
|
|
2006-10-23 05:28:10 +02:00
|
|
|
setenv("LESS", "FRSX", 0);
|
2006-04-16 04:44:25 +02:00
|
|
|
run_pager(pager);
|
2006-04-21 21:25:13 +02:00
|
|
|
die("unable to execute pager '%s'", pager);
|
2006-02-28 20:26:21 +01:00
|
|
|
exit(255);
|
|
|
|
}
|
2007-12-11 07:27:33 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|