filter-branch: fix variable export logic
[git/git.git] / pager.c
CommitLineData
f67b45f8
LT
1#include "cache.h"
2
3/*
4 * This is split up from the rest of git so that we might do
5 * something different on Windows, for example.
6 */
7
6e9af863
JK
8static int spawned_pager;
9
402461aa 10static void run_pager(const char *pager)
f67b45f8 11{
35ce8622
LT
12 /*
13 * Work around bug in "less" by not starting it until we
14 * have real input
15 */
16 fd_set in;
17
18 FD_ZERO(&in);
19 FD_SET(0, &in);
20 select(1, &in, NULL, &in, NULL);
21
402461aa 22 execlp(pager, pager, NULL);
34fd1c9a 23 execl("/bin/sh", "sh", "-c", pager, NULL);
f67b45f8
LT
24}
25
26void setup_pager(void)
27{
28 pid_t pid;
29 int fd[2];
c27d205a 30 const char *pager = getenv("GIT_PAGER");
f67b45f8
LT
31
32 if (!isatty(1))
33 return;
cad3a205
JH
34 if (!pager) {
35 if (!pager_program)
36 git_config(git_default_config);
54adf370 37 pager = pager_program;
cad3a205 38 }
c27d205a
ML
39 if (!pager)
40 pager = getenv("PAGER");
402461aa
JS
41 if (!pager)
42 pager = "less";
caef71a5 43 else if (!*pager || !strcmp(pager, "cat"))
402461aa
JS
44 return;
45
6e9af863 46 spawned_pager = 1; /* means we are emitting to terminal */
85fb65ed 47
f67b45f8
LT
48 if (pipe(fd) < 0)
49 return;
50 pid = fork();
51 if (pid < 0) {
52 close(fd[0]);
53 close(fd[1]);
54 return;
55 }
56
57 /* return in the child */
58 if (!pid) {
59 dup2(fd[1], 1);
61b80509 60 dup2(fd[1], 2);
f67b45f8
LT
61 close(fd[0]);
62 close(fd[1]);
63 return;
64 }
65
66 /* The original process turns into the PAGER */
67 dup2(fd[0], 0);
68 close(fd[0]);
69 close(fd[1]);
70
0abc0260 71 setenv("LESS", "FRSX", 0);
402461aa 72 run_pager(pager);
34fd1c9a 73 die("unable to execute pager '%s'", pager);
f67b45f8
LT
74 exit(255);
75}
6e9af863
JK
76
77int pager_in_use(void)
78{
79 const char *env;
80
81 if (spawned_pager)
82 return 1;
83
84 env = getenv("GIT_PAGER_IN_USE");
85 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
86}