setup.h: move declarations for setup.c functions from cache.h
[git/debian.git] / sideband.c
blob4905cf9b32afbb8437d8cfacb2a7e9eaf3958b5d
1 #include "cache.h"
2 #include "color.h"
3 #include "config.h"
4 #include "gettext.h"
5 #include "sideband.h"
6 #include "help.h"
7 #include "pkt-line.h"
9 struct keyword_entry {
11 * We use keyword as config key so it should be a single alphanumeric word.
13 const char *keyword;
14 char color[COLOR_MAXLEN];
17 static struct keyword_entry keywords[] = {
18 { "hint", GIT_COLOR_YELLOW },
19 { "warning", GIT_COLOR_BOLD_YELLOW },
20 { "success", GIT_COLOR_BOLD_GREEN },
21 { "error", GIT_COLOR_BOLD_RED },
24 /* Returns a color setting (GIT_COLOR_NEVER, etc). */
25 static int use_sideband_colors(void)
27 static int use_sideband_colors_cached = -1;
29 const char *key = "color.remote";
30 struct strbuf sb = STRBUF_INIT;
31 char *value;
32 int i;
34 if (use_sideband_colors_cached >= 0)
35 return use_sideband_colors_cached;
37 if (!git_config_get_string(key, &value)) {
38 use_sideband_colors_cached = git_config_colorbool(key, value);
39 } else if (!git_config_get_string("color.ui", &value)) {
40 use_sideband_colors_cached = git_config_colorbool("color.ui", value);
41 } else {
42 use_sideband_colors_cached = GIT_COLOR_AUTO;
45 for (i = 0; i < ARRAY_SIZE(keywords); i++) {
46 strbuf_reset(&sb);
47 strbuf_addf(&sb, "%s.%s", key, keywords[i].keyword);
48 if (git_config_get_string(sb.buf, &value))
49 continue;
50 if (color_parse(value, keywords[i].color))
51 continue;
53 strbuf_release(&sb);
54 return use_sideband_colors_cached;
57 void list_config_color_sideband_slots(struct string_list *list, const char *prefix)
59 int i;
61 for (i = 0; i < ARRAY_SIZE(keywords); i++)
62 list_config_item(list, prefix, keywords[i].keyword);
66 * Optionally highlight one keyword in remote output if it appears at the start
67 * of the line. This should be called for a single line only, which is
68 * passed as the first N characters of the SRC array.
70 * NEEDSWORK: use "size_t n" instead for clarity.
72 static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
74 int i;
76 if (!want_color_stderr(use_sideband_colors())) {
77 strbuf_add(dest, src, n);
78 return;
81 while (0 < n && isspace(*src)) {
82 strbuf_addch(dest, *src);
83 src++;
84 n--;
87 for (i = 0; i < ARRAY_SIZE(keywords); i++) {
88 struct keyword_entry *p = keywords + i;
89 int len = strlen(p->keyword);
91 if (n < len)
92 continue;
94 * Match case insensitively, so we colorize output from existing
95 * servers regardless of the case that they use for their
96 * messages. We only highlight the word precisely, so
97 * "successful" stays uncolored.
99 if (!strncasecmp(p->keyword, src, len) &&
100 (len == n || !isalnum(src[len]))) {
101 strbuf_addstr(dest, p->color);
102 strbuf_add(dest, src, len);
103 strbuf_addstr(dest, GIT_COLOR_RESET);
104 n -= len;
105 src += len;
106 break;
110 strbuf_add(dest, src, n);
114 #define DISPLAY_PREFIX "remote: "
116 #define ANSI_SUFFIX "\033[K"
117 #define DUMB_SUFFIX " "
119 int demultiplex_sideband(const char *me, int status,
120 char *buf, int len,
121 int die_on_error,
122 struct strbuf *scratch,
123 enum sideband_type *sideband_type)
125 static const char *suffix;
126 const char *b, *brk;
127 int band;
129 if (!suffix) {
130 if (isatty(2) && !is_terminal_dumb())
131 suffix = ANSI_SUFFIX;
132 else
133 suffix = DUMB_SUFFIX;
136 if (status == PACKET_READ_EOF) {
137 strbuf_addf(scratch,
138 "%s%s: unexpected disconnect while reading sideband packet",
139 scratch->len ? "\n" : "", me);
140 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
141 goto cleanup;
144 if (len < 0)
145 BUG("negative length on non-eof packet read");
147 if (len == 0) {
148 if (status == PACKET_READ_NORMAL) {
149 strbuf_addf(scratch,
150 "%s%s: protocol error: missing sideband designator",
151 scratch->len ? "\n" : "", me);
152 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
153 } else {
154 /* covers flush, delim, etc */
155 *sideband_type = SIDEBAND_FLUSH;
157 goto cleanup;
160 band = buf[0] & 0xff;
161 buf[len] = '\0';
162 len--;
163 switch (band) {
164 case 3:
165 if (die_on_error)
166 die(_("remote error: %s"), buf + 1);
167 strbuf_addf(scratch, "%s%s", scratch->len ? "\n" : "",
168 DISPLAY_PREFIX);
169 maybe_colorize_sideband(scratch, buf + 1, len);
171 *sideband_type = SIDEBAND_REMOTE_ERROR;
172 break;
173 case 2:
174 b = buf + 1;
177 * Append a suffix to each nonempty line to clear the
178 * end of the screen line.
180 * The output is accumulated in a buffer and
181 * each line is printed to stderr using
182 * write(2) to ensure inter-process atomicity.
184 while ((brk = strpbrk(b, "\n\r"))) {
185 int linelen = brk - b;
188 * For message accross packet boundary, there would have
189 * a nonempty "scratch" buffer from last call of this
190 * function, and there may have a leading CR/LF in "buf".
191 * For this case we should add a clear-to-eol suffix to
192 * clean leftover letters we previously have written on
193 * the same line.
195 if (scratch->len && !linelen)
196 strbuf_addstr(scratch, suffix);
198 if (!scratch->len)
199 strbuf_addstr(scratch, DISPLAY_PREFIX);
202 * A use case that we should not add clear-to-eol suffix
203 * to empty lines:
205 * For progress reporting we may receive a bunch of
206 * percentage updates followed by '\r' to remain on the
207 * same line, and at the end receive a single '\n' to
208 * move to the next line. We should preserve the final
209 * status report line by not appending clear-to-eol
210 * suffix to this single line break.
212 if (linelen > 0) {
213 maybe_colorize_sideband(scratch, b, linelen);
214 strbuf_addstr(scratch, suffix);
217 strbuf_addch(scratch, *brk);
218 xwrite(2, scratch->buf, scratch->len);
219 strbuf_reset(scratch);
221 b = brk + 1;
224 if (*b) {
225 strbuf_addstr(scratch, scratch->len ?
226 "" : DISPLAY_PREFIX);
227 maybe_colorize_sideband(scratch, b, strlen(b));
229 return 0;
230 case 1:
231 *sideband_type = SIDEBAND_PRIMARY;
232 return 1;
233 default:
234 strbuf_addf(scratch, "%s%s: protocol error: bad band #%d",
235 scratch->len ? "\n" : "", me, band);
236 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
237 break;
240 cleanup:
241 if (die_on_error && *sideband_type == SIDEBAND_PROTOCOL_ERROR)
242 die("%s", scratch->buf);
243 if (scratch->len) {
244 strbuf_addch(scratch, '\n');
245 xwrite(2, scratch->buf, scratch->len);
247 strbuf_release(scratch);
248 return 1;
252 * fd is connected to the remote side; send the sideband data
253 * over multiplexed packet stream.
255 void send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
257 const char *p = data;
259 while (sz) {
260 unsigned n;
261 char hdr[5];
263 n = sz;
264 if (packet_max - 5 < n)
265 n = packet_max - 5;
266 if (0 <= band) {
267 xsnprintf(hdr, sizeof(hdr), "%04x", n + 5);
268 hdr[4] = band;
269 write_or_die(fd, hdr, 5);
270 } else {
271 xsnprintf(hdr, sizeof(hdr), "%04x", n + 4);
272 write_or_die(fd, hdr, 4);
274 write_or_die(fd, p, n);
275 p += n;
276 sz -= n;