skip t5512 because remote does not yet work
[git/platforms/storm.git] / pager.c
blobde1bc799f5d2d3c8874b47121fd4956421da7107
1 #include "cache.h"
3 /*
4 * This is split up from the rest of git so that we might do
5 * something different on Windows, for example.
6 */
8 #ifndef __MINGW32__
9 static void run_pager(const char *pager)
12 * Work around bug in "less" by not starting it until we
13 * have real input
15 fd_set in;
17 FD_ZERO(&in);
18 FD_SET(0, &in);
19 select(1, &in, NULL, &in, NULL);
21 execlp(pager, pager, NULL);
22 execl("/bin/sh", "sh", "-c", pager, NULL);
24 #else
25 #include "run-command.h"
27 const char *pager_argv[] = { "sh", "-c", NULL, NULL };
28 static struct child_process pager_process = {
29 .argv = pager_argv,
30 .in = -1
32 static void collect_pager(void)
34 fflush(stdout);
35 close(1); /* signals EOF to pager */
36 finish_command(&pager_process);
38 #endif
40 void setup_pager(void)
42 #ifndef __MINGW32__
43 pid_t pid;
44 int fd[2];
45 #endif
46 const char *pager = getenv("GIT_PAGER");
48 if (!isatty(1))
49 return;
50 if (!pager) {
51 if (!pager_program)
52 git_config(git_default_config);
53 pager = pager_program;
55 if (!pager)
56 pager = getenv("PAGER");
57 if (!pager)
58 pager = "less";
59 else if (!*pager || !strcmp(pager, "cat"))
60 return;
62 pager_in_use = 1; /* means we are emitting to terminal */
64 #ifndef __MINGW32__
65 if (pipe(fd) < 0)
66 return;
67 pid = fork();
68 if (pid < 0) {
69 close(fd[0]);
70 close(fd[1]);
71 return;
74 /* return in the child */
75 if (!pid) {
76 dup2(fd[1], 1);
77 close(fd[0]);
78 close(fd[1]);
79 return;
82 /* The original process turns into the PAGER */
83 dup2(fd[0], 0);
84 close(fd[0]);
85 close(fd[1]);
87 setenv("LESS", "FRSX", 0);
88 run_pager(pager);
89 die("unable to execute pager '%s'", pager);
90 exit(255);
91 #else
92 /* spawn the pager */
93 pager_argv[2] = pager;
94 if (start_command(&pager_process))
95 return;
97 /* original process continues, but writes to the pipe */
98 dup2(pager_process.in, 1);
99 close(pager_process.in);
101 /* this makes sure that the parent terminates after the pager */
102 atexit(collect_pager);
103 #endif