cocci: remove 'unused.cocci'
[git.git] / sideband.c
blob0af582858bf4175e8c3047cffc10eb156661a631
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"
8 #include "write-or-die.h"
10 struct keyword_entry {
12 * We use keyword as config key so it should be a single alphanumeric word.
14 const char *keyword;
15 char color[COLOR_MAXLEN];
18 static struct keyword_entry keywords[] = {
19 { "hint", GIT_COLOR_YELLOW },
20 { "warning", GIT_COLOR_BOLD_YELLOW },
21 { "success", GIT_COLOR_BOLD_GREEN },
22 { "error", GIT_COLOR_BOLD_RED },
25 /* Returns a color setting (GIT_COLOR_NEVER, etc). */
26 static int use_sideband_colors(void)
28 static int use_sideband_colors_cached = -1;
30 const char *key = "color.remote";
31 struct strbuf sb = STRBUF_INIT;
32 char *value;
33 int i;
35 if (use_sideband_colors_cached >= 0)
36 return use_sideband_colors_cached;
38 if (!git_config_get_string(key, &value)) {
39 use_sideband_colors_cached = git_config_colorbool(key, value);
40 } else if (!git_config_get_string("color.ui", &value)) {
41 use_sideband_colors_cached = git_config_colorbool("color.ui", value);
42 } else {
43 use_sideband_colors_cached = GIT_COLOR_AUTO;
46 for (i = 0; i < ARRAY_SIZE(keywords); i++) {
47 strbuf_reset(&sb);
48 strbuf_addf(&sb, "%s.%s", key, keywords[i].keyword);
49 if (git_config_get_string(sb.buf, &value))
50 continue;
51 if (color_parse(value, keywords[i].color))
52 continue;
54 strbuf_release(&sb);
55 return use_sideband_colors_cached;
58 void list_config_color_sideband_slots(struct string_list *list, const char *prefix)
60 int i;
62 for (i = 0; i < ARRAY_SIZE(keywords); i++)
63 list_config_item(list, prefix, keywords[i].keyword);
67 * Optionally highlight one keyword in remote output if it appears at the start
68 * of the line. This should be called for a single line only, which is
69 * passed as the first N characters of the SRC array.
71 * NEEDSWORK: use "size_t n" instead for clarity.
73 static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
75 int i;
77 if (!want_color_stderr(use_sideband_colors())) {
78 strbuf_add(dest, src, n);
79 return;
82 while (0 < n && isspace(*src)) {
83 strbuf_addch(dest, *src);
84 src++;
85 n--;
88 for (i = 0; i < ARRAY_SIZE(keywords); i++) {
89 struct keyword_entry *p = keywords + i;
90 int len = strlen(p->keyword);
92 if (n < len)
93 continue;
95 * Match case insensitively, so we colorize output from existing
96 * servers regardless of the case that they use for their
97 * messages. We only highlight the word precisely, so
98 * "successful" stays uncolored.
100 if (!strncasecmp(p->keyword, src, len) &&
101 (len == n || !isalnum(src[len]))) {
102 strbuf_addstr(dest, p->color);
103 strbuf_add(dest, src, len);
104 strbuf_addstr(dest, GIT_COLOR_RESET);
105 n -= len;
106 src += len;
107 break;
111 strbuf_add(dest, src, n);
115 #define DISPLAY_PREFIX "remote: "
117 #define ANSI_SUFFIX "\033[K"
118 #define DUMB_SUFFIX " "
120 int demultiplex_sideband(const char *me, int status,
121 char *buf, int len,
122 int die_on_error,
123 struct strbuf *scratch,
124 enum sideband_type *sideband_type)
126 static const char *suffix;
127 const char *b, *brk;
128 int band;
130 if (!suffix) {
131 if (isatty(2) && !is_terminal_dumb())
132 suffix = ANSI_SUFFIX;
133 else
134 suffix = DUMB_SUFFIX;
137 if (status == PACKET_READ_EOF) {
138 strbuf_addf(scratch,
139 "%s%s: unexpected disconnect while reading sideband packet",
140 scratch->len ? "\n" : "", me);
141 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
142 goto cleanup;
145 if (len < 0)
146 BUG("negative length on non-eof packet read");
148 if (len == 0) {
149 if (status == PACKET_READ_NORMAL) {
150 strbuf_addf(scratch,
151 "%s%s: protocol error: missing sideband designator",
152 scratch->len ? "\n" : "", me);
153 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
154 } else {
155 /* covers flush, delim, etc */
156 *sideband_type = SIDEBAND_FLUSH;
158 goto cleanup;
161 band = buf[0] & 0xff;
162 buf[len] = '\0';
163 len--;
164 switch (band) {
165 case 3:
166 if (die_on_error)
167 die(_("remote error: %s"), buf + 1);
168 strbuf_addf(scratch, "%s%s", scratch->len ? "\n" : "",
169 DISPLAY_PREFIX);
170 maybe_colorize_sideband(scratch, buf + 1, len);
172 *sideband_type = SIDEBAND_REMOTE_ERROR;
173 break;
174 case 2:
175 b = buf + 1;
178 * Append a suffix to each nonempty line to clear the
179 * end of the screen line.
181 * The output is accumulated in a buffer and
182 * each line is printed to stderr using
183 * write(2) to ensure inter-process atomicity.
185 while ((brk = strpbrk(b, "\n\r"))) {
186 int linelen = brk - b;
189 * For message accross packet boundary, there would have
190 * a nonempty "scratch" buffer from last call of this
191 * function, and there may have a leading CR/LF in "buf".
192 * For this case we should add a clear-to-eol suffix to
193 * clean leftover letters we previously have written on
194 * the same line.
196 if (scratch->len && !linelen)
197 strbuf_addstr(scratch, suffix);
199 if (!scratch->len)
200 strbuf_addstr(scratch, DISPLAY_PREFIX);
203 * A use case that we should not add clear-to-eol suffix
204 * to empty lines:
206 * For progress reporting we may receive a bunch of
207 * percentage updates followed by '\r' to remain on the
208 * same line, and at the end receive a single '\n' to
209 * move to the next line. We should preserve the final
210 * status report line by not appending clear-to-eol
211 * suffix to this single line break.
213 if (linelen > 0) {
214 maybe_colorize_sideband(scratch, b, linelen);
215 strbuf_addstr(scratch, suffix);
218 strbuf_addch(scratch, *brk);
219 xwrite(2, scratch->buf, scratch->len);
220 strbuf_reset(scratch);
222 b = brk + 1;
225 if (*b) {
226 strbuf_addstr(scratch, scratch->len ?
227 "" : DISPLAY_PREFIX);
228 maybe_colorize_sideband(scratch, b, strlen(b));
230 return 0;
231 case 1:
232 *sideband_type = SIDEBAND_PRIMARY;
233 return 1;
234 default:
235 strbuf_addf(scratch, "%s%s: protocol error: bad band #%d",
236 scratch->len ? "\n" : "", me, band);
237 *sideband_type = SIDEBAND_PROTOCOL_ERROR;
238 break;
241 cleanup:
242 if (die_on_error && *sideband_type == SIDEBAND_PROTOCOL_ERROR)
243 die("%s", scratch->buf);
244 if (scratch->len) {
245 strbuf_addch(scratch, '\n');
246 xwrite(2, scratch->buf, scratch->len);
248 strbuf_release(scratch);
249 return 1;
253 * fd is connected to the remote side; send the sideband data
254 * over multiplexed packet stream.
256 void send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
258 const char *p = data;
260 while (sz) {
261 unsigned n;
262 char hdr[5];
264 n = sz;
265 if (packet_max - 5 < n)
266 n = packet_max - 5;
267 if (0 <= band) {
268 xsnprintf(hdr, sizeof(hdr), "%04x", n + 5);
269 hdr[4] = band;
270 write_or_die(fd, hdr, 5);
271 } else {
272 xsnprintf(hdr, sizeof(hdr), "%04x", n + 4);
273 write_or_die(fd, hdr, 4);
275 write_or_die(fd, p, n);
276 p += n;
277 sz -= n;