config.c: handle lock file in error case in git_config_rename_...
[git/debian.git] / pager.c
blob4bc048148e043eabf1315bbcbae8ea4c6363b330
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 struct child_process pager_process = CHILD_PROCESS_INIT;
16 static void wait_for_pager(int in_signal)
18 if (!in_signal) {
19 fflush(stdout);
20 fflush(stderr);
22 /* signal EOF to pager */
23 close(1);
24 close(2);
25 if (in_signal)
26 finish_command_in_signal(&pager_process);
27 else
28 finish_command(&pager_process);
31 static void wait_for_pager_atexit(void)
33 wait_for_pager(0);
36 static void wait_for_pager_signal(int signo)
38 wait_for_pager(1);
39 sigchain_pop(signo);
40 raise(signo);
43 const char *git_pager(int stdout_is_tty)
45 const char *pager;
47 if (!stdout_is_tty)
48 return NULL;
50 pager = getenv("GIT_PAGER");
51 if (!pager) {
52 if (!pager_program)
53 git_config(git_default_config, NULL);
54 pager = pager_program;
56 if (!pager)
57 pager = getenv("PAGER");
58 if (!pager)
59 pager = DEFAULT_PAGER;
60 if (!*pager || !strcmp(pager, "cat"))
61 pager = NULL;
63 return pager;
66 void prepare_pager_args(struct child_process *pager_process, const char *pager)
68 argv_array_push(&pager_process->args, pager);
69 pager_process->use_shell = 1;
70 if (!getenv("LESS"))
71 argv_array_push(&pager_process->env_array, "LESS=FRX");
72 if (!getenv("LV"))
73 argv_array_push(&pager_process->env_array, "LV=-c");
76 void setup_pager(void)
78 const char *pager = git_pager(isatty(1));
80 if (!pager)
81 return;
84 * force computing the width of the terminal before we redirect
85 * the standard output to the pager.
87 (void) term_columns();
89 setenv("GIT_PAGER_IN_USE", "true", 1);
91 /* spawn the pager */
92 prepare_pager_args(&pager_process, pager);
93 pager_process.in = -1;
94 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
95 if (start_command(&pager_process))
96 return;
98 /* original process continues, but writes to the pipe */
99 dup2(pager_process.in, 1);
100 if (isatty(2))
101 dup2(pager_process.in, 2);
102 close(pager_process.in);
104 /* this makes sure that the parent terminates after the pager */
105 sigchain_push_common(wait_for_pager_signal);
106 atexit(wait_for_pager_atexit);
109 int pager_in_use(void)
111 const char *env;
112 env = getenv("GIT_PAGER_IN_USE");
113 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
117 * Return cached value (if set) or $COLUMNS environment variable (if
118 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
119 * and default to 80 if all else fails.
121 int term_columns(void)
123 static int term_columns_at_startup;
125 char *col_string;
126 int n_cols;
128 if (term_columns_at_startup)
129 return term_columns_at_startup;
131 term_columns_at_startup = 80;
133 col_string = getenv("COLUMNS");
134 if (col_string && (n_cols = atoi(col_string)) > 0)
135 term_columns_at_startup = n_cols;
136 #ifdef TIOCGWINSZ
137 else {
138 struct winsize ws;
139 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
140 term_columns_at_startup = ws.ws_col;
142 #endif
144 return term_columns_at_startup;
148 * How many columns do we need to show this number in decimal?
150 int decimal_width(uintmax_t number)
152 int width;
154 for (width = 1; number >= 10; width++)
155 number /= 10;
156 return width;
159 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
160 int check_pager_config(const char *cmd)
162 int want = -1;
163 struct strbuf key = STRBUF_INIT;
164 const char *value = NULL;
165 strbuf_addf(&key, "pager.%s", cmd);
166 if (git_config_key_is_valid(key.buf) &&
167 !git_config_get_value(key.buf, &value)) {
168 int b = git_config_maybe_bool(key.buf, value);
169 if (b >= 0)
170 want = b;
171 else {
172 want = 1;
173 pager_program = xstrdup(value);
176 strbuf_release(&key);
177 return want;