Skip to content

Commit e8bd888

Browse files
rjustogitster
authored andcommitted
pager: introduce wait_for_pager
Since f67b45f (Introduce trivial new pager.c helper infrastructure, 2006-02-28) we have the machinery to send our output to a pager. That machinery, once set up, does not allow us to regain the original stdio streams. In the interactive commands (i.e.: add -p) we want to use the pager for some output, while maintaining the interaction with the user. Modify the pager machinery so that we can use `setup_pager()` and, once we've finished sending the desired output for the pager, wait for the pager termination using a new function `wait_for_pager()`. Make this function reset the pager machinery before returning. One specific point to note is that we avoid forking the pager in `setup_pager()` if the configured pager is an empty string [*1*] or simply "cat" [*2*]. In these cases, `setup_pager()` does nothing and therefore `wait_for_pager()` should not be called. We could modify `setup_pager()` to return an indication of these situations, so we could avoid calling `wait_for_pager()`. However, let's avoid transferring that responsibility to the caller and instead treat the call to `wait_for_pager()` as a no-op when we know we haven't forked the pager. 1.- 402461a (pager: do not fork a pager if PAGER is set to empty., 2006-04-16) 2.- caef71a (Do not fork PAGER=cat, 2006-04-16) Signed-off-by: Rubén Justo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent da9ef60 commit e8bd888

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

pager.c

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ int pager_use_color = 1;
1414

1515
static struct child_process pager_process;
1616
static char *pager_program;
17-
static int close_fd2;
17+
static int old_fd1 = -1, old_fd2 = -1;
1818

1919
/* Is the value coming back from term_columns() just a guess? */
2020
static int term_columns_guessed;
@@ -24,20 +24,49 @@ static void close_pager_fds(void)
2424
{
2525
/* signal EOF to pager */
2626
close(1);
27-
if (close_fd2)
27+
if (old_fd2 != -1)
2828
close(2);
2929
}
3030

31-
static void wait_for_pager_atexit(void)
31+
static void finish_pager(void)
3232
{
3333
fflush(stdout);
3434
fflush(stderr);
3535
close_pager_fds();
3636
finish_command(&pager_process);
3737
}
3838

39+
static void wait_for_pager_atexit(void)
40+
{
41+
if (old_fd1 == -1)
42+
return;
43+
44+
finish_pager();
45+
}
46+
47+
void wait_for_pager(void)
48+
{
49+
if (old_fd1 == -1)
50+
return;
51+
52+
finish_pager();
53+
sigchain_pop_common();
54+
unsetenv("GIT_PAGER_IN_USE");
55+
dup2(old_fd1, 1);
56+
close(old_fd1);
57+
old_fd1 = -1;
58+
if (old_fd2 != -1) {
59+
dup2(old_fd2, 2);
60+
close(old_fd2);
61+
old_fd2 = -1;
62+
}
63+
}
64+
3965
static void wait_for_pager_signal(int signo)
4066
{
67+
if (old_fd1 == -1)
68+
return;
69+
4170
close_pager_fds();
4271
finish_command_in_signal(&pager_process);
4372
sigchain_pop(signo);
@@ -113,6 +142,7 @@ void prepare_pager_args(struct child_process *pager_process, const char *pager)
113142

114143
void setup_pager(void)
115144
{
145+
static int once = 0;
116146
const char *pager = git_pager(isatty(1));
117147

118148
if (!pager)
@@ -142,16 +172,20 @@ void setup_pager(void)
142172
die("unable to execute pager '%s'", pager);
143173

144174
/* original process continues, but writes to the pipe */
175+
old_fd1 = dup(1);
145176
dup2(pager_process.in, 1);
146177
if (isatty(2)) {
147-
close_fd2 = 1;
178+
old_fd2 = dup(2);
148179
dup2(pager_process.in, 2);
149180
}
150181
close(pager_process.in);
151182

152-
/* this makes sure that the parent terminates after the pager */
153183
sigchain_push_common(wait_for_pager_signal);
154-
atexit(wait_for_pager_atexit);
184+
185+
if (!once) {
186+
once++;
187+
atexit(wait_for_pager_atexit);
188+
}
155189
}
156190

157191
int pager_in_use(void)

pager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ struct child_process;
55

66
const char *git_pager(int stdout_is_tty);
77
void setup_pager(void);
8+
void wait_for_pager(void);
89
int pager_in_use(void);
910
int term_columns(void);
1011
void term_clear_line(void);

0 commit comments

Comments
 (0)