pager: introduce wait_for_pager

Since f67b45f862 (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.- 402461aab1 (pager: do not fork a pager if PAGER is set to empty., 2006-04-16) 2.- caef71a535 (Do not fork PAGER=cat, 2006-04-16) Signed-off-by: Rubén Justo <rjusto@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rubén Justo committed Jul 25, 2024 at 15:44 UTC e8bd8883feb7551f7d1870403d212c77cd56071b
2 files changed +41 -6
pager.c
+40 -6
@@ -14,7 +14,7 @@ int pager_use_color = 1;
14
15 static struct child_process pager_process;
16 static char *pager_program;
17 -static int close_fd2;
17 +static int old_fd1 = -1, old_fd2 = -1;
18
19 /* Is the value coming back from term_columns() just a guess? */
20 static int term_columns_guessed;
@@ -24,11 +24,11 @@ static void close_pager_fds(void)
24 {
25 /* signal EOF to pager */
26 close(1);
27 - if (close_fd2)
27 + if (old_fd2 != -1)
28 close(2);
29 }
30
31 -static void wait_for_pager_atexit(void)
31 +static void finish_pager(void)
32 {
33 fflush(stdout);
34 fflush(stderr);
@@ -36,8 +36,37 @@ static void wait_for_pager_atexit(void)
36 finish_command(&pager_process);
37 }
38
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 +
65 static void wait_for_pager_signal(int signo)
66 {
67 + if (old_fd1 == -1)
68 + return;
69 +
70 close_pager_fds();
71 finish_command_in_signal(&pager_process);
72 sigchain_pop(signo);
@@ -113,6 +142,7 @@ void prepare_pager_args(struct child_process *pager_process, const char *pager)
142
143 void setup_pager(void)
144 {
145 + static int once = 0;
146 const char *pager = git_pager(isatty(1));
147
148 if (!pager)
@@ -142,16 +172,20 @@ void setup_pager(void)
172 die("unable to execute pager '%s'", pager);
173
174 /* original process continues, but writes to the pipe */
175 + old_fd1 = dup(1);
176 dup2(pager_process.in, 1);
177 if (isatty(2)) {
147 - close_fd2 = 1;
178 + old_fd2 = dup(2);
179 dup2(pager_process.in, 2);
180 }
181 close(pager_process.in);
182
152 - /* this makes sure that the parent terminates after the pager */
183 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 + }
189 }
190
191 int pager_in_use(void)
pager.h
+1
@@ -5,6 +5,7 @@ struct child_process;
5
6 const char *git_pager(int stdout_is_tty);
7 void setup_pager(void);
8 +void wait_for_pager(void);
9 int pager_in_use(void);
10 int term_columns(void);
11 void term_clear_line(void);