Code

Sync with 1.7.9.5
[git.git] / pager.c
1 #include "cache.h"
2 #include "run-command.h"
3 #include "sigchain.h"
5 #ifndef DEFAULT_PAGER
6 #define DEFAULT_PAGER "less"
7 #endif
9 /*
10  * This is split up from the rest of git so that we can do
11  * something different on Windows.
12  */
14 #ifndef WIN32
15 static void pager_preexec(void)
16 {
17         /*
18          * Work around bug in "less" by not starting it until we
19          * have real input
20          */
21         fd_set in;
23         FD_ZERO(&in);
24         FD_SET(0, &in);
25         select(1, &in, NULL, &in, NULL);
26 }
27 #endif
29 static const char *pager_argv[] = { NULL, NULL };
30 static struct child_process pager_process;
32 static void wait_for_pager(void)
33 {
34         fflush(stdout);
35         fflush(stderr);
36         /* signal EOF to pager */
37         close(1);
38         close(2);
39         finish_command(&pager_process);
40 }
42 static void wait_for_pager_signal(int signo)
43 {
44         wait_for_pager();
45         sigchain_pop(signo);
46         raise(signo);
47 }
49 const char *git_pager(int stdout_is_tty)
50 {
51         const char *pager;
53         if (!stdout_is_tty)
54                 return NULL;
56         pager = getenv("GIT_PAGER");
57         if (!pager) {
58                 if (!pager_program)
59                         git_config(git_default_config, NULL);
60                 pager = pager_program;
61         }
62         if (!pager)
63                 pager = getenv("PAGER");
64         if (!pager)
65                 pager = DEFAULT_PAGER;
66         else if (!*pager || !strcmp(pager, "cat"))
67                 pager = NULL;
69         return pager;
70 }
72 void setup_pager(void)
73 {
74         const char *pager = git_pager(isatty(1));
76         if (!pager)
77                 return;
79         /*
80          * force computing the width of the terminal before we redirect
81          * the standard output to the pager.
82          */
83         (void) term_columns();
85         setenv("GIT_PAGER_IN_USE", "true", 1);
87         /* spawn the pager */
88         pager_argv[0] = pager;
89         pager_process.use_shell = 1;
90         pager_process.argv = pager_argv;
91         pager_process.in = -1;
92         if (!getenv("LESS")) {
93                 static const char *env[] = { "LESS=FRSX", NULL };
94                 pager_process.env = env;
95         }
96 #ifndef WIN32
97         pager_process.preexec_cb = pager_preexec;
98 #endif
99         if (start_command(&pager_process))
100                 return;
102         /* original process continues, but writes to the pipe */
103         dup2(pager_process.in, 1);
104         if (isatty(2))
105                 dup2(pager_process.in, 2);
106         close(pager_process.in);
108         /* this makes sure that the parent terminates after the pager */
109         sigchain_push_common(wait_for_pager_signal);
110         atexit(wait_for_pager);
113 int pager_in_use(void)
115         const char *env;
116         env = getenv("GIT_PAGER_IN_USE");
117         return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
120 /*
121  * Return cached value (if set) or $COLUMNS environment variable (if
122  * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
123  * and default to 80 if all else fails.
124  */
125 int term_columns(void)
127         static int term_columns_at_startup;
129         char *col_string;
130         int n_cols;
132         if (term_columns_at_startup)
133                 return term_columns_at_startup;
135         term_columns_at_startup = 80;
137         col_string = getenv("COLUMNS");
138         if (col_string && (n_cols = atoi(col_string)) > 0)
139                 term_columns_at_startup = n_cols;
140 #ifdef TIOCGWINSZ
141         else {
142                 struct winsize ws;
143                 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
144                         term_columns_at_startup = ws.ws_col;
145         }
146 #endif
148         return term_columns_at_startup;
151 /*
152  * How many columns do we need to show this number in decimal?
153  */
154 int decimal_width(int number)
156         int i, width;
158         for (width = 1, i = 10; i <= number; width++)
159                 i *= 10;
160         return width;