@matheusbsbs / matheus-bagliano / commits / f67b45f862
Fork 0

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 <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Feb 28, 2006 at 11:26 UTC f67b45f862d17b54037b9b59eefc385624d1fa83
3 files changed +52 -1
Makefile
+1 -1
@@ -205,7 +205,7 @@ LIB_OBJS = \
205 quote.o read-cache.o refs.o run-command.o \
206 server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
207 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 \
209 $(DIFF_OBJS)
210
211 LIBS = $(LIB_FILE)
cache.h
+3
@@ -352,4 +352,7 @@ extern int copy_fd(int ifd, int ofd);
352 extern int receive_unpack_pack(int fd[2], const char *me, int quiet);
353 extern int receive_keep_pack(int fd[2], const char *me, int quiet);
354
355 +/* pager.c */
356 +extern void setup_pager(void);
357 +
358 #endif /* CACHE_H */
pager.c new
+48
@@ -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 +}