Skip to content

Commit f67b45f

Browse files
Linus TorvaldsJunio C Hamano
authored andcommitted
Introduce trivial new pager.c helper infrastructure
This introduces the new function void setup_pager(void); to set up output to be written through a pager applocation. All in preparation for doing the simple scripts in C. Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a4a88b2 commit f67b45f

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ LIB_OBJS = \
205205
quote.o read-cache.o refs.o run-command.o \
206206
server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
207207
tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
208-
fetch-clone.o revision.o \
208+
fetch-clone.o revision.o pager.o \
209209
$(DIFF_OBJS)
210210

211211
LIBS = $(LIB_FILE)

cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,7 @@ extern int copy_fd(int ifd, int ofd);
352352
extern int receive_unpack_pack(int fd[2], const char *me, int quiet);
353353
extern int receive_keep_pack(int fd[2], const char *me, int quiet);
354354

355+
/* pager.c */
356+
extern void setup_pager(void);
357+
355358
#endif /* CACHE_H */

pager.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.
6+
*/
7+
8+
static void run_pager(void)
9+
{
10+
const char *prog = getenv("PAGER");
11+
if (!prog)
12+
prog = "less";
13+
setenv("LESS", "-S", 0);
14+
execlp(prog, prog, NULL);
15+
}
16+
17+
void setup_pager(void)
18+
{
19+
pid_t pid;
20+
int fd[2];
21+
22+
if (!isatty(1))
23+
return;
24+
if (pipe(fd) < 0)
25+
return;
26+
pid = fork();
27+
if (pid < 0) {
28+
close(fd[0]);
29+
close(fd[1]);
30+
return;
31+
}
32+
33+
/* return in the child */
34+
if (!pid) {
35+
dup2(fd[1], 1);
36+
close(fd[0]);
37+
close(fd[1]);
38+
return;
39+
}
40+
41+
/* The original process turns into the PAGER */
42+
dup2(fd[0], 0);
43+
close(fd[0]);
44+
close(fd[1]);
45+
46+
run_pager();
47+
exit(255);
48+
}

0 commit comments

Comments
 (0)