1#include"cache.h" 2 3#include <sys/select.h> 4 5/* 6 * This is split up from the rest of git so that we might do 7 * something different on Windows, for example. 8 */ 9 10static voidrun_pager(const char*pager) 11{ 12/* 13 * Work around bug in "less" by not starting it until we 14 * have real input 15 */ 16 fd_set in; 17 18FD_ZERO(&in); 19FD_SET(0, &in); 20select(1, &in, NULL, &in, NULL); 21 22execlp(pager, pager, NULL); 23execl("/bin/sh","sh","-c", pager, NULL); 24} 25 26voidsetup_pager(void) 27{ 28 pid_t pid; 29int fd[2]; 30const char*pager =getenv("GIT_PAGER"); 31 32if(!isatty(1)) 33return; 34if(!pager) 35 pager = pager_program; 36if(!pager) 37 pager =getenv("PAGER"); 38if(!pager) 39 pager ="less"; 40else if(!*pager || !strcmp(pager,"cat")) 41return; 42 43 pager_in_use =1;/* means we are emitting to terminal */ 44 45if(pipe(fd) <0) 46return; 47 pid =fork(); 48if(pid <0) { 49close(fd[0]); 50close(fd[1]); 51return; 52} 53 54/* return in the child */ 55if(!pid) { 56dup2(fd[1],1); 57close(fd[0]); 58close(fd[1]); 59return; 60} 61 62/* The original process turns into the PAGER */ 63dup2(fd[0],0); 64close(fd[0]); 65close(fd[1]); 66 67setenv("LESS","FRSX",0); 68run_pager(pager); 69die("unable to execute pager '%s'", pager); 70exit(255); 71}