]> git.ipfire.org Git - thirdparty/git.git/blame - pager.c
refactor signal handling for cleanup functions
[thirdparty/git.git] / pager.c
CommitLineData
f67b45f8 1#include "cache.h"
ea27a18c 2#include "run-command.h"
f67b45f8
LT
3
4/*
bfdd9ffd
JS
5 * This is split up from the rest of git so that we can do
6 * something different on Windows.
f67b45f8
LT
7 */
8
6e9af863
JK
9static int spawned_pager;
10
bfdd9ffd 11#ifndef __MINGW32__
ea27a18c 12static void pager_preexec(void)
f67b45f8 13{
35ce8622
LT
14 /*
15 * Work around bug in "less" by not starting it until we
16 * have real input
17 */
18 fd_set in;
19
20 FD_ZERO(&in);
21 FD_SET(0, &in);
22 select(1, &in, NULL, &in, NULL);
23
ea27a18c 24 setenv("LESS", "FRSX", 0);
f67b45f8 25}
ea27a18c 26#endif
bfdd9ffd
JS
27
28static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
ea27a18c
JK
29static struct child_process pager_process;
30
bfdd9ffd
JS
31static void wait_for_pager(void)
32{
33 fflush(stdout);
34 fflush(stderr);
35 /* signal EOF to pager */
36 close(1);
37 close(2);
38 finish_command(&pager_process);
39}
f67b45f8
LT
40
41void setup_pager(void)
42{
c27d205a 43 const char *pager = getenv("GIT_PAGER");
f67b45f8
LT
44
45 if (!isatty(1))
46 return;
cad3a205
JH
47 if (!pager) {
48 if (!pager_program)
ef90d6d4 49 git_config(git_default_config, NULL);
54adf370 50 pager = pager_program;
cad3a205 51 }
c27d205a
ML
52 if (!pager)
53 pager = getenv("PAGER");
402461aa
JS
54 if (!pager)
55 pager = "less";
caef71a5 56 else if (!*pager || !strcmp(pager, "cat"))
402461aa
JS
57 return;
58
6e9af863 59 spawned_pager = 1; /* means we are emitting to terminal */
85fb65ed 60
bfdd9ffd
JS
61 /* spawn the pager */
62 pager_argv[2] = pager;
ea27a18c
JK
63 pager_process.argv = pager_argv;
64 pager_process.in = -1;
65#ifndef __MINGW32__
66 pager_process.preexec_cb = pager_preexec;
67#endif
bfdd9ffd
JS
68 if (start_command(&pager_process))
69 return;
70
71 /* original process continues, but writes to the pipe */
72 dup2(pager_process.in, 1);
a8335024
JH
73 if (isatty(2))
74 dup2(pager_process.in, 2);
bfdd9ffd
JS
75 close(pager_process.in);
76
77 /* this makes sure that the parent terminates after the pager */
78 atexit(wait_for_pager);
f67b45f8 79}
6e9af863
JK
80
81int pager_in_use(void)
82{
83 const char *env;
84
85 if (spawned_pager)
86 return 1;
87
88 env = getenv("GIT_PAGER_IN_USE");
89 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
90}