1 #include <sys/select.h>
8 #include "run-command.h"
10 #include "subcmd-config.h"
13 * This is split up from the rest of git so that we can do
14 * something different on Windows.
17 static int spawned_pager;
18 static int pager_columns;
20 void pager_init(const char *pager_env)
22 subcmd_config.pager_env = pager_env;
25 static void pager_preexec(void)
28 * Work around bug in "less" by not starting it until we
37 FD_SET(0, &exception);
38 select(1, &in, NULL, &exception, NULL);
40 setenv("LESS", "FRSX", 0);
43 static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
44 static struct child_process pager_process;
46 static void wait_for_pager(void)
50 /* signal EOF to pager */
53 finish_command(&pager_process);
56 static void wait_for_pager_signal(int signo)
63 void setup_pager(void)
65 const char *pager = getenv(subcmd_config.pager_env);
70 if (ioctl(1, TIOCGWINSZ, &sz) == 0)
71 pager_columns = sz.ws_col;
73 pager = getenv("PAGER");
74 if (!(pager || access("/usr/bin/pager", X_OK)))
75 pager = "/usr/bin/pager";
76 if (!(pager || access("/usr/bin/less", X_OK)))
77 pager = "/usr/bin/less";
80 if (!*pager || !strcmp(pager, "cat"))
83 spawned_pager = 1; /* means we are emitting to terminal */
86 pager_argv[2] = pager;
87 pager_process.argv = pager_argv;
88 pager_process.in = -1;
89 pager_process.preexec_cb = pager_preexec;
91 if (start_command(&pager_process))
94 /* original process continues, but writes to the pipe */
95 dup2(pager_process.in, 1);
97 dup2(pager_process.in, 2);
98 close(pager_process.in);
100 /* this makes sure that the parent terminates after the pager */
101 sigchain_push_common(wait_for_pager_signal);
102 atexit(wait_for_pager);
105 int pager_in_use(void)
107 return spawned_pager;
110 int pager_get_columns(void)
114 s = getenv("COLUMNS");
118 return (pager_columns ? pager_columns : 80) - 2;