2 #include "run-command.h"
4 #include "subcmd-config.h"
7 * This is split up from the rest of git so that we can do
8 * something different on Windows.
11 static int spawned_pager
;
13 void pager_init(const char *pager_env
)
15 subcmd_config
.pager_env
= pager_env
;
18 static void pager_preexec(void)
21 * Work around bug in "less" by not starting it until we
28 select(1, &in
, NULL
, &in
, NULL
);
30 setenv("LESS", "FRSX", 0);
33 static const char *pager_argv
[] = { "sh", "-c", NULL
, NULL
};
34 static struct child_process pager_process
;
36 static void wait_for_pager(void)
40 /* signal EOF to pager */
43 finish_command(&pager_process
);
46 static void wait_for_pager_signal(int signo
)
53 void setup_pager(void)
55 const char *pager
= getenv(subcmd_config
.pager_env
);
60 pager
= getenv("PAGER");
61 if (!(pager
|| access("/usr/bin/pager", X_OK
)))
62 pager
= "/usr/bin/pager";
63 if (!(pager
|| access("/usr/bin/less", X_OK
)))
64 pager
= "/usr/bin/less";
67 if (!*pager
|| !strcmp(pager
, "cat"))
70 spawned_pager
= 1; /* means we are emitting to terminal */
73 pager_argv
[2] = pager
;
74 pager_process
.argv
= pager_argv
;
75 pager_process
.in
= -1;
76 pager_process
.preexec_cb
= pager_preexec
;
78 if (start_command(&pager_process
))
81 /* original process continues, but writes to the pipe */
82 dup2(pager_process
.in
, 1);
84 dup2(pager_process
.in
, 2);
85 close(pager_process
.in
);
87 /* this makes sure that the parent terminates after the pager */
88 sigchain_push_common(wait_for_pager_signal
);
89 atexit(wait_for_pager
);
92 int pager_in_use(void)