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 =getenv("PAGER"); 36if(!pager) 37 pager ="less"; 38else if(!*pager || !strcmp(pager,"cat")) 39return; 40 41 pager_in_use =1;/* means we are emitting to terminal */ 42 43if(pipe(fd) <0) 44return; 45 pid =fork(); 46if(pid <0) { 47close(fd[0]); 48close(fd[1]); 49return; 50} 51 52/* return in the child */ 53if(!pid) { 54dup2(fd[1],1); 55close(fd[0]); 56close(fd[1]); 57return; 58} 59 60/* The original process turns into the PAGER */ 61dup2(fd[0],0); 62close(fd[0]); 63close(fd[1]); 64 65setenv("LESS","FRSX",0); 66run_pager(pager); 67die("unable to execute pager '%s'", pager); 68exit(255); 69}