checkout: refactor die_if_checked_out() caller
[alt-git.git] / pager.c
blobb8822a9381e49487ecb9bf37f1a42f1414eb8ff3
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "editor.h"
4 #include "pager.h"
5 #include "run-command.h"
6 #include "sigchain.h"
7 #include "alias.h"
9 int pager_use_color = 1;
11 #ifndef DEFAULT_PAGER
12 #define DEFAULT_PAGER "less"
13 #endif
15 static struct child_process pager_process;
16 static const char *pager_program;
18 /* Is the value coming back from term_columns() just a guess? */
19 static int term_columns_guessed;
22 static void close_pager_fds(void)
24 /* signal EOF to pager */
25 close(1);
26 close(2);
29 static void wait_for_pager_atexit(void)
31 fflush(stdout);
32 fflush(stderr);
33 close_pager_fds();
34 finish_command(&pager_process);
37 static void wait_for_pager_signal(int signo)
39 close_pager_fds();
40 finish_command_in_signal(&pager_process);
41 sigchain_pop(signo);
42 raise(signo);
45 static int core_pager_config(const char *var, const char *value,
46 const struct config_context *ctx UNUSED,
47 void *data UNUSED)
49 if (!strcmp(var, "core.pager"))
50 return git_config_string(&pager_program, var, value);
51 return 0;
54 const char *git_pager(int stdout_is_tty)
56 const char *pager;
58 if (!stdout_is_tty)
59 return NULL;
61 pager = getenv("GIT_PAGER");
62 if (!pager) {
63 if (!pager_program)
64 read_early_config(core_pager_config, NULL);
65 pager = pager_program;
67 if (!pager)
68 pager = getenv("PAGER");
69 if (!pager)
70 pager = DEFAULT_PAGER;
71 if (!*pager || !strcmp(pager, "cat"))
72 pager = NULL;
74 return pager;
77 static void setup_pager_env(struct strvec *env)
79 const char **argv;
80 int i;
81 char *pager_env = xstrdup(PAGER_ENV);
82 int n = split_cmdline(pager_env, &argv);
84 if (n < 0)
85 die("malformed build-time PAGER_ENV: %s",
86 split_cmdline_strerror(n));
88 for (i = 0; i < n; i++) {
89 char *cp = strchr(argv[i], '=');
91 if (!cp)
92 die("malformed build-time PAGER_ENV");
94 *cp = '\0';
95 if (!getenv(argv[i])) {
96 *cp = '=';
97 strvec_push(env, argv[i]);
100 free(pager_env);
101 free(argv);
104 void prepare_pager_args(struct child_process *pager_process, const char *pager)
106 strvec_push(&pager_process->args, pager);
107 pager_process->use_shell = 1;
108 setup_pager_env(&pager_process->env);
109 pager_process->trace2_child_class = "pager";
112 void setup_pager(void)
114 const char *pager = git_pager(isatty(1));
116 if (!pager)
117 return;
120 * After we redirect standard output, we won't be able to use an ioctl
121 * to get the terminal size. Let's grab it now, and then set $COLUMNS
122 * to communicate it to any sub-processes.
125 char buf[64];
126 xsnprintf(buf, sizeof(buf), "%d", term_columns());
127 if (!term_columns_guessed)
128 setenv("COLUMNS", buf, 0);
131 setenv("GIT_PAGER_IN_USE", "true", 1);
133 child_process_init(&pager_process);
135 /* spawn the pager */
136 prepare_pager_args(&pager_process, pager);
137 pager_process.in = -1;
138 strvec_push(&pager_process.env, "GIT_PAGER_IN_USE");
139 if (start_command(&pager_process))
140 return;
142 /* original process continues, but writes to the pipe */
143 dup2(pager_process.in, 1);
144 if (isatty(2))
145 dup2(pager_process.in, 2);
146 close(pager_process.in);
148 /* this makes sure that the parent terminates after the pager */
149 sigchain_push_common(wait_for_pager_signal);
150 atexit(wait_for_pager_atexit);
153 int pager_in_use(void)
155 return git_env_bool("GIT_PAGER_IN_USE", 0);
159 * Return cached value (if set) or $COLUMNS environment variable (if
160 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
161 * and default to 80 if all else fails.
163 int term_columns(void)
165 static int term_columns_at_startup;
167 char *col_string;
168 int n_cols;
170 if (term_columns_at_startup)
171 return term_columns_at_startup;
173 term_columns_at_startup = 80;
174 term_columns_guessed = 1;
176 col_string = getenv("COLUMNS");
177 if (col_string && (n_cols = atoi(col_string)) > 0) {
178 term_columns_at_startup = n_cols;
179 term_columns_guessed = 0;
181 #ifdef TIOCGWINSZ
182 else {
183 struct winsize ws;
184 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col) {
185 term_columns_at_startup = ws.ws_col;
186 term_columns_guessed = 0;
189 #endif
191 return term_columns_at_startup;
195 * Clear the entire line, leave cursor in first column.
197 void term_clear_line(void)
199 if (is_terminal_dumb())
201 * Fall back to print a terminal width worth of space
202 * characters (hoping that the terminal is still as wide
203 * as it was upon the first call to term_columns()).
205 fprintf(stderr, "\r%*s\r", term_columns(), "");
206 else
208 * On non-dumb terminals use an escape sequence to clear
209 * the whole line, no matter how wide the terminal.
211 fputs("\r\033[K", stderr);
215 * How many columns do we need to show this number in decimal?
217 int decimal_width(uintmax_t number)
219 int width;
221 for (width = 1; number >= 10; width++)
222 number /= 10;
223 return width;
226 struct pager_command_config_data {
227 const char *cmd;
228 int want;
229 char *value;
232 static int pager_command_config(const char *var, const char *value,
233 const struct config_context *ctx UNUSED,
234 void *vdata)
236 struct pager_command_config_data *data = vdata;
237 const char *cmd;
239 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
240 int b = git_parse_maybe_bool(value);
241 if (b >= 0)
242 data->want = b;
243 else {
244 data->want = 1;
245 data->value = xstrdup(value);
249 return 0;
252 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
253 int check_pager_config(const char *cmd)
255 struct pager_command_config_data data;
257 data.cmd = cmd;
258 data.want = -1;
259 data.value = NULL;
261 read_early_config(pager_command_config, &data);
263 if (data.value)
264 pager_program = data.value;
265 return data.want;