Merge branch 'master' of git://github.com/peff/git
[git/mingw.git] / pager.c
blobc0b4387d969476232a4e00a7acf9b06dc4ef6edc
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.
14 static const char *pager_argv[] = { NULL, NULL };
15 static struct child_process pager_process;
17 static void wait_for_pager(void)
19 fflush(stdout);
20 fflush(stderr);
21 /* signal EOF to pager */
22 close(1);
23 close(2);
24 finish_command(&pager_process);
27 static void wait_for_pager_signal(int signo)
29 wait_for_pager();
30 sigchain_pop(signo);
31 raise(signo);
34 const char *git_pager(int stdout_is_tty)
36 const char *pager;
38 if (!stdout_is_tty)
39 return NULL;
41 pager = getenv("GIT_PAGER");
42 if (!pager) {
43 if (!pager_program)
44 git_config(git_default_config, NULL);
45 pager = pager_program;
47 if (!pager)
48 pager = getenv("PAGER");
49 if (!pager)
50 pager = DEFAULT_PAGER;
51 else if (!*pager || !strcmp(pager, "cat"))
52 pager = NULL;
54 return pager;
57 void setup_pager(void)
59 const char *pager = git_pager(isatty(1));
61 if (!pager || pager_in_use())
62 return;
65 * force computing the width of the terminal before we redirect
66 * the standard output to the pager.
68 (void) term_columns();
70 setenv("GIT_PAGER_IN_USE", "true", 1);
72 /* spawn the pager */
73 pager_argv[0] = pager;
74 pager_process.use_shell = 1;
75 pager_process.argv = pager_argv;
76 pager_process.in = -1;
77 if (!getenv("LESS")) {
78 static const char *env[] = { "LESS=FRSX", NULL };
79 pager_process.env = env;
81 if (start_command(&pager_process))
82 return;
84 /* original process continues, but writes to the pipe */
85 dup2(pager_process.in, 1);
86 if (isatty(2))
87 dup2(pager_process.in, 2);
88 close(pager_process.in);
90 /* this makes sure that the parent terminates after the pager */
91 sigchain_push_common(wait_for_pager_signal);
92 atexit(wait_for_pager);
95 int pager_in_use(void)
97 const char *env;
98 env = getenv("GIT_PAGER_IN_USE");
99 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
103 * Return cached value (if set) or $COLUMNS environment variable (if
104 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
105 * and default to 80 if all else fails.
107 int term_columns(void)
109 static int term_columns_at_startup;
111 char *col_string;
112 int n_cols;
114 if (term_columns_at_startup)
115 return term_columns_at_startup;
117 term_columns_at_startup = 80;
119 col_string = getenv("COLUMNS");
120 if (col_string && (n_cols = atoi(col_string)) > 0)
121 term_columns_at_startup = n_cols;
122 #ifdef TIOCGWINSZ
123 else {
124 struct winsize ws;
125 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
126 term_columns_at_startup = ws.ws_col;
128 #endif
130 return term_columns_at_startup;
134 * How many columns do we need to show this number in decimal?
136 int decimal_width(int number)
138 int i, width;
140 for (width = 1, i = 10; i <= number; width++)
141 i *= 10;
142 return width;