Skip tests that fail due to incomplete implementations, missing tools...
[git/mingw/j6t.git] / pager.c
blob750abff22f9535ea3b8147fab27ad65108fbafd3
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 = CHILD_PROCESS_INIT;
17 static void wait_for_pager(int in_signal)
19 if (!in_signal) {
20 fflush(stdout);
21 fflush(stderr);
23 /* signal EOF to pager */
24 close(1);
25 close(2);
26 if (in_signal)
27 finish_command_in_signal(&pager_process);
28 else
29 finish_command(&pager_process);
32 static void wait_for_pager_atexit(void)
34 wait_for_pager(0);
37 static void wait_for_pager_signal(int signo)
39 wait_for_pager(1);
40 sigchain_pop(signo);
41 raise(signo);
44 const char *git_pager(int stdout_is_tty)
46 const char *pager;
48 if (!stdout_is_tty)
49 return NULL;
51 pager = getenv("GIT_PAGER");
52 if (!pager) {
53 if (!pager_program)
54 git_config(git_default_config, NULL);
55 pager = pager_program;
57 if (!pager)
58 pager = getenv("PAGER");
59 if (!pager)
60 pager = DEFAULT_PAGER;
61 if (!*pager || !strcmp(pager, "cat"))
62 pager = NULL;
64 return pager;
67 void setup_pager(void)
69 const char *pager = git_pager(isatty(1));
70 int ret;
72 if (!pager)
73 return;
76 * force computing the width of the terminal before we redirect
77 * the standard output to the pager.
79 (void) term_columns();
81 setenv("GIT_PAGER_IN_USE", "true", 1);
83 /* spawn the pager */
84 pager_argv[0] = pager;
85 pager_process.argv = pager_argv;
86 if (!getenv("LESS"))
87 argv_array_push(&pager_process.env_array, "LESS=FRX");
88 if (!getenv("LV"))
89 argv_array_push(&pager_process.env_array, "LV=-c");
90 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
91 pager_process.in = -1;
92 pager_process.silent_exec_failure = 1;
93 ret = start_command(&pager_process);
94 if (ret < 0 && errno == ENOENT) {
95 pager_process.use_shell = 1;
96 pager_process.in = -1;
97 pager_process.silent_exec_failure = 0;
98 ret = start_command(&pager_process);
100 if (ret)
101 return; /* no pager */
103 /* original process continues, but writes to the pipe */
104 dup2(pager_process.in, 1);
105 if (isatty(2))
106 dup2(pager_process.in, 2);
107 close(pager_process.in);
109 /* this makes sure that the parent terminates after the pager */
110 sigchain_push_common(wait_for_pager_signal);
111 atexit(wait_for_pager_atexit);
114 int pager_in_use(void)
116 const char *env;
117 env = getenv("GIT_PAGER_IN_USE");
118 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
122 * Return cached value (if set) or $COLUMNS environment variable (if
123 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
124 * and default to 80 if all else fails.
126 int term_columns(void)
128 static int term_columns_at_startup;
130 char *col_string;
131 int n_cols;
133 if (term_columns_at_startup)
134 return term_columns_at_startup;
136 term_columns_at_startup = 80;
138 col_string = getenv("COLUMNS");
139 if (col_string && (n_cols = atoi(col_string)) > 0)
140 term_columns_at_startup = n_cols;
141 #ifdef TIOCGWINSZ
142 else {
143 struct winsize ws;
144 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
145 term_columns_at_startup = ws.ws_col;
147 #endif
149 return term_columns_at_startup;
153 * How many columns do we need to show this number in decimal?
155 int decimal_width(uintmax_t number)
157 int width;
159 for (width = 1; number >= 10; width++)
160 number /= 10;
161 return width;
164 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
165 int check_pager_config(const char *cmd)
167 int want = -1;
168 struct strbuf key = STRBUF_INIT;
169 const char *value = NULL;
170 strbuf_addf(&key, "pager.%s", cmd);
171 if (git_config_key_is_valid(key.buf) &&
172 !git_config_get_value(key.buf, &value)) {
173 int b = git_config_maybe_bool(key.buf, value);
174 if (b >= 0)
175 want = b;
176 else {
177 want = 1;
178 pager_program = xstrdup(value);
181 strbuf_release(&key);
182 return want;