2006-02-28 22:26:21 +03:00
|
|
|
#include "cache.h"
|
|
|
|
|
|
|
|
/*
|
2007-12-08 23:28:41 +03:00
|
|
|
* This is split up from the rest of git so that we can do
|
|
|
|
* something different on Windows.
|
2006-02-28 22:26:21 +03:00
|
|
|
*/
|
|
|
|
|
2007-12-11 09:27:33 +03:00
|
|
|
static int spawned_pager;
|
|
|
|
|
2007-12-08 23:28:41 +03:00
|
|
|
#ifndef __MINGW32__
|
2006-04-16 06:44:25 +04:00
|
|
|
static void run_pager(const char *pager)
|
2006-02-28 22:26:21 +03:00
|
|
|
{
|
2007-01-24 22:21:10 +03: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 06:44:25 +04:00
|
|
|
execlp(pager, pager, NULL);
|
2006-04-21 23:25:13 +04:00
|
|
|
execl("/bin/sh", "sh", "-c", pager, NULL);
|
2006-02-28 22:26:21 +03:00
|
|
|
}
|
2007-12-08 23:28:41 +03:00
|
|
|
#else
|
|
|
|
#include "run-command.h"
|
|
|
|
|
|
|
|
static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
|
|
|
|
static struct child_process pager_process = {
|
|
|
|
.argv = pager_argv,
|
|
|
|
.in = -1
|
|
|
|
};
|
|
|
|
static void wait_for_pager(void)
|
|
|
|
{
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
/* signal EOF to pager */
|
|
|
|
close(1);
|
|
|
|
close(2);
|
|
|
|
finish_command(&pager_process);
|
|
|
|
}
|
|
|
|
#endif
|
2006-02-28 22:26:21 +03:00
|
|
|
|
|
|
|
void setup_pager(void)
|
|
|
|
{
|
2007-12-08 23:28:41 +03:00
|
|
|
#ifndef __MINGW32__
|
2006-02-28 22:26:21 +03:00
|
|
|
pid_t pid;
|
|
|
|
int fd[2];
|
2007-12-08 23:28:41 +03:00
|
|
|
#endif
|
2006-07-31 17:27:00 +04:00
|
|
|
const char *pager = getenv("GIT_PAGER");
|
2006-02-28 22:26:21 +03:00
|
|
|
|
|
|
|
if (!isatty(1))
|
|
|
|
return;
|
2007-08-07 08:08:43 +04:00
|
|
|
if (!pager) {
|
|
|
|
if (!pager_program)
|
2008-05-14 21:46:53 +04:00
|
|
|
git_config(git_default_config, NULL);
|
2007-07-03 22:18:11 +04:00
|
|
|
pager = pager_program;
|
2007-08-07 08:08:43 +04:00
|
|
|
}
|
2006-07-31 17:27:00 +04:00
|
|
|
if (!pager)
|
|
|
|
pager = getenv("PAGER");
|
2006-04-16 06:44:25 +04:00
|
|
|
if (!pager)
|
|
|
|
pager = "less";
|
2006-04-16 12:46:08 +04:00
|
|
|
else if (!*pager || !strcmp(pager, "cat"))
|
2006-04-16 06:44:25 +04:00
|
|
|
return;
|
|
|
|
|
2007-12-11 09:27:33 +03:00
|
|
|
spawned_pager = 1; /* means we are emitting to terminal */
|
2006-06-07 03:58:40 +04:00
|
|
|
|
2007-12-08 23:28:41 +03:00
|
|
|
#ifndef __MINGW32__
|
2006-02-28 22:26:21 +03: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);
|
2008-02-16 22:15:41 +03:00
|
|
|
dup2(fd[1], 2);
|
2006-02-28 22:26:21 +03:00
|
|
|
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 07:28:10 +04:00
|
|
|
setenv("LESS", "FRSX", 0);
|
2006-04-16 06:44:25 +04:00
|
|
|
run_pager(pager);
|
2006-04-21 23:25:13 +04:00
|
|
|
die("unable to execute pager '%s'", pager);
|
2006-02-28 22:26:21 +03:00
|
|
|
exit(255);
|
2007-12-08 23:28:41 +03:00
|
|
|
#else
|
|
|
|
/* spawn the pager */
|
|
|
|
pager_argv[2] = pager;
|
|
|
|
if (start_command(&pager_process))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* original process continues, but writes to the pipe */
|
|
|
|
dup2(pager_process.in, 1);
|
|
|
|
dup2(pager_process.in, 2);
|
|
|
|
close(pager_process.in);
|
|
|
|
|
|
|
|
/* this makes sure that the parent terminates after the pager */
|
|
|
|
atexit(wait_for_pager);
|
|
|
|
#endif
|
2006-02-28 22:26:21 +03:00
|
|
|
}
|
2007-12-11 09:27:33 +03: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;
|
|
|
|
}
|