Windows: Make the pager work.
Since we have neither fork() nor exec(), we have to spawn the pager and feed it with the program's output. Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Johannes Sixt committed
Dec 8, 2007 at 21:28 UTC
bfdd9ffd2f7376ccb8b9d4c4e39e2e0fe97d6b37
1 file changed
+38
-2
pager.c
+38
-2
@@ -1,12 +1,13 @@
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.
4
+ * This is split up from the rest of git so that we can do
5
+ * something different on Windows.
6
*/
7
8
static int spawned_pager;
9
10
+#ifndef __MINGW32__
11
static void run_pager(const char *pager)
12
{
13
/*
@@ -22,11 +23,31 @@ static void run_pager(const char *pager)
23
execlp(pager, pager, NULL);
24
execl("/bin/sh", "sh", "-c", pager, NULL);
25
}
26
+#else
27
+#include "run-command.h"
28
+
29
+static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
30
+static struct child_process pager_process = {
31
+ .argv = pager_argv,
32
+ .in = -1
33
+};
34
+static void wait_for_pager(void)
35
+{
36
+ fflush(stdout);
37
+ fflush(stderr);
38
+ /* signal EOF to pager */
39
+ close(1);
40
+ close(2);
41
+ finish_command(&pager_process);
42
+}
43
+#endif
44
45
void setup_pager(void)
46
{
47
+#ifndef __MINGW32__
48
pid_t pid;
49
int fd[2];
50
+#endif
51
const char *pager = getenv("GIT_PAGER");
52
53
if (!isatty(1))
@@ -45,6 +66,7 @@ void setup_pager(void)
66
67
spawned_pager = 1; /* means we are emitting to terminal */
68
69
+#ifndef __MINGW32__
70
if (pipe(fd) < 0)
71
return;
72
pid = fork();
@@ -72,6 +94,20 @@ void setup_pager(void)
94
run_pager(pager);
95
die("unable to execute pager '%s'", pager);
96
exit(255);
97
+#else
98
+ /* spawn the pager */
99
+ pager_argv[2] = pager;
100
+ if (start_command(&pager_process))
101
+ return;
102
+
103
+ /* original process continues, but writes to the pipe */
104
+ dup2(pager_process.in, 1);
105
+ dup2(pager_process.in, 2);
106
+ close(pager_process.in);
107
+
108
+ /* this makes sure that the parent terminates after the pager */
109
+ atexit(wait_for_pager);
110
+#endif
111
}
112
113
int pager_in_use(void)