| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 3 | |
| 4 | #include "git-compat-util.h" |
| 5 | #include "color.h" |
| 6 | #include "config.h" |
| 7 | #include "editor.h" |
| 8 | #include "gettext.h" |
| 9 | #include "sideband.h" |
| 10 | #include "help.h" |
| 11 | #include "pkt-line.h" |
| 12 | #include "write-or-die.h" |
| 13 | #include "urlmatch.h" |
| 14 | |
| 15 | struct keyword_entry { |
| 16 | /* |
| 17 | * We use keyword as config key so it should be a single alphanumeric word. |
| 18 | */ |
| 19 | const char *keyword; |
| 20 | char color[COLOR_MAXLEN]; |
| 21 | }; |
| 22 | |
| 23 | static struct keyword_entry keywords[] = { |
| 24 | { "hint", GIT_COLOR_YELLOW }, |
| 25 | { "warning", GIT_COLOR_BOLD_YELLOW }, |
| 26 | { "success", GIT_COLOR_BOLD_GREEN }, |
| 27 | { "error", GIT_COLOR_BOLD_RED }, |
| 28 | }; |
| 29 | |
| 30 | static enum { |
| 31 | ALLOW_CONTROL_SEQUENCES_UNSET = -1, |
| 32 | ALLOW_NO_CONTROL_CHARACTERS = 0, |
| 33 | ALLOW_ANSI_COLOR_SEQUENCES = 1<<0, |
| 34 | ALLOW_ANSI_CURSOR_MOVEMENTS = 1<<1, |
| 35 | ALLOW_ANSI_ERASE = 1<<2, |
| 36 | ALLOW_ALL_CONTROL_CHARACTERS = 1<<3, |
| 37 | ALLOW_DEFAULT_ANSI_SEQUENCES = ALLOW_ANSI_COLOR_SEQUENCES |
| 38 | } allow_control_characters = ALLOW_CONTROL_SEQUENCES_UNSET; |
| 39 | |
| 40 | static inline int skip_prefix_in_csv(const char *value, const char *prefix, |
| 41 | const char **out) |
| 42 | { |
| 43 | if (!skip_prefix(value, prefix, &value) || |
| 44 | (*value && *value != ',')) |
| 45 | return 0; |
| 46 | *out = value + !!*value; |
| 47 | return 1; |
| 48 | } |
| 49 | |
| 50 | int sideband_allow_control_characters_config(const char *var, const char *value) |
| 51 | { |
| 52 | switch (git_parse_maybe_bool(value)) { |
| 53 | case 0: |
| 54 | allow_control_characters = ALLOW_NO_CONTROL_CHARACTERS; |
| 55 | return 0; |
| 56 | case 1: |
| 57 | allow_control_characters = ALLOW_ALL_CONTROL_CHARACTERS; |
| 58 | return 0; |
| 59 | default: |
| 60 | break; |
| 61 | } |
| 62 | |
| 63 | allow_control_characters = ALLOW_NO_CONTROL_CHARACTERS; |
| 64 | while (*value) { |
| 65 | if (skip_prefix_in_csv(value, "color", &value)) |
| 66 | allow_control_characters |= ALLOW_ANSI_COLOR_SEQUENCES; |
| 67 | else if (skip_prefix_in_csv(value, "cursor", &value)) |
| 68 | allow_control_characters |= ALLOW_ANSI_CURSOR_MOVEMENTS; |
| 69 | else if (skip_prefix_in_csv(value, "erase", &value)) |
| 70 | allow_control_characters |= ALLOW_ANSI_ERASE; |
| 71 | else if (skip_prefix_in_csv(value, "true", &value)) |
| 72 | allow_control_characters = ALLOW_ALL_CONTROL_CHARACTERS; |
| 73 | else if (skip_prefix_in_csv(value, "false", &value)) |
| 74 | allow_control_characters = ALLOW_NO_CONTROL_CHARACTERS; |
| 75 | else |
| 76 | warning(_("unrecognized value for '%s': '%s'"), var, value); |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int sideband_config_callback(const char *var, const char *value, |
| 82 | const struct config_context *ctx UNUSED, |
| 83 | void *data UNUSED) |
| 84 | { |
| 85 | if (!strcmp(var, "sideband.allowcontrolcharacters")) |
| 86 | return sideband_allow_control_characters_config(var, value); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | void sideband_apply_url_config(const char *url) |
| 92 | { |
| 93 | struct urlmatch_config config = URLMATCH_CONFIG_INIT; |
| 94 | char *normalized_url; |
| 95 | |
| 96 | if (!url) |
| 97 | BUG("must not call sideband_apply_url_config(NULL)"); |
| 98 | |
| 99 | config.section = "sideband"; |
| 100 | config.collect_fn = sideband_config_callback; |
| 101 | |
| 102 | normalized_url = url_normalize(url, &config.url); |
| 103 | repo_config(the_repository, urlmatch_config_entry, &config); |
| 104 | free(normalized_url); |
| 105 | string_list_clear(&config.vars, 1); |
| 106 | urlmatch_config_release(&config); |
| 107 | } |
| 108 | |
| 109 | /* Returns a color setting (GIT_COLOR_NEVER, etc). */ |
| 110 | static enum git_colorbool use_sideband_colors(void) |
| 111 | { |
| 112 | static enum git_colorbool use_sideband_colors_cached = GIT_COLOR_UNKNOWN; |
| 113 | |
| 114 | const char *key = "color.remote"; |
| 115 | struct strbuf sb = STRBUF_INIT; |
| 116 | const char *value; |
| 117 | int i; |
| 118 | |
| 119 | if (use_sideband_colors_cached != GIT_COLOR_UNKNOWN) |
| 120 | return use_sideband_colors_cached; |
| 121 | |
| 122 | if (allow_control_characters == ALLOW_CONTROL_SEQUENCES_UNSET) { |
| 123 | if (!repo_config_get_value(the_repository, "sideband.allowcontrolcharacters", &value)) |
| 124 | sideband_allow_control_characters_config("sideband.allowcontrolcharacters", value); |
| 125 | |
| 126 | if (allow_control_characters == ALLOW_CONTROL_SEQUENCES_UNSET) |
| 127 | allow_control_characters = ALLOW_DEFAULT_ANSI_SEQUENCES; |
| 128 | } |
| 129 | |
| 130 | if (!repo_config_get_string_tmp(the_repository, key, &value)) |
| 131 | use_sideband_colors_cached = git_config_colorbool(key, value); |
| 132 | else if (!repo_config_get_string_tmp(the_repository, "color.ui", &value)) |
| 133 | use_sideband_colors_cached = git_config_colorbool("color.ui", value); |
| 134 | else |
| 135 | use_sideband_colors_cached = GIT_COLOR_AUTO; |
| 136 | |
| 137 | for (i = 0; i < ARRAY_SIZE(keywords); i++) { |
| 138 | strbuf_reset(&sb); |
| 139 | strbuf_addf(&sb, "%s.%s", key, keywords[i].keyword); |
| 140 | if (repo_config_get_string_tmp(the_repository, sb.buf, &value)) |
| 141 | continue; |
| 142 | color_parse(value, keywords[i].color); |
| 143 | } |
| 144 | |
| 145 | strbuf_release(&sb); |
| 146 | return use_sideband_colors_cached; |
| 147 | } |
| 148 | |
| 149 | void list_config_color_sideband_slots(struct string_list *list, const char *prefix) |
| 150 | { |
| 151 | int i; |
| 152 | |
| 153 | for (i = 0; i < ARRAY_SIZE(keywords); i++) |
| 154 | list_config_item(list, prefix, keywords[i].keyword); |
| 155 | } |
| 156 | |
| 157 | static int handle_ansi_sequence(struct strbuf *dest, const char *src, int n) |
| 158 | { |
| 159 | int i; |
| 160 | |
| 161 | /* |
| 162 | * Valid ANSI color sequences are of the form |
| 163 | * |
| 164 | * ESC [ [<n> [; <n>]*] m |
| 165 | * |
| 166 | * where <n> can be either zero-length, or a decimal number, or a |
| 167 | * series of decimal numbers separated by a colon (for 256-color or |
| 168 | * true-color codes). |
| 169 | * |
| 170 | * These are part of the Select Graphic Rendition sequences which |
| 171 | * contain more than just color sequences, for more details see |
| 172 | * https://en.wikipedia.org/wiki/ANSI_escape_code#SGR. |
| 173 | * |
| 174 | * The cursor movement sequences are: |
| 175 | * |
| 176 | * ESC [ n A - Cursor up n lines (CUU) |
| 177 | * ESC [ n B - Cursor down n lines (CUD) |
| 178 | * ESC [ n C - Cursor forward n columns (CUF) |
| 179 | * ESC [ n D - Cursor back n columns (CUB) |
| 180 | * ESC [ n E - Cursor next line, beginning (CNL) |
| 181 | * ESC [ n F - Cursor previous line, beginning (CPL) |
| 182 | * ESC [ n G - Cursor to column n (CHA) |
| 183 | * ESC [ n ; m H - Cursor position (row n, col m) (CUP) |
| 184 | * ESC [ n ; m f - Same as H (HVP) |
| 185 | * |
| 186 | * The sequences to erase characters are: |
| 187 | * |
| 188 | * |
| 189 | * ESC [ 0 J - Clear from cursor to end of screen (ED) |
| 190 | * ESC [ 1 J - Clear from cursor to beginning of screen (ED) |
| 191 | * ESC [ 2 J - Clear entire screen (ED) |
| 192 | * ESC [ 3 J - Clear entire screen + scrollback (ED) - xterm extension |
| 193 | * ESC [ 0 K - Clear from cursor to end of line (EL) |
| 194 | * ESC [ 1 K - Clear from cursor to beginning of line (EL) |
| 195 | * ESC [ 2 K - Clear entire line (EL) |
| 196 | * ESC [ n M - Delete n lines (DL) |
| 197 | * ESC [ n P - Delete n characters (DCH) |
| 198 | * ESC [ n X - Erase n characters (ECH) |
| 199 | * |
| 200 | * For a comprehensive list of common ANSI Escape sequences, see |
| 201 | * https://www.xfree86.org/current/ctlseqs.html |
| 202 | */ |
| 203 | |
| 204 | if (n < 3 || src[0] != '\x1b' || src[1] != '[') |
| 205 | return 0; |
| 206 | |
| 207 | for (i = 2; i < n; i++) { |
| 208 | if (((allow_control_characters & ALLOW_ANSI_COLOR_SEQUENCES) && |
| 209 | src[i] == 'm') || |
| 210 | ((allow_control_characters & ALLOW_ANSI_CURSOR_MOVEMENTS) && |
| 211 | strchr("ABCDEFGHf", src[i])) || |
| 212 | ((allow_control_characters & ALLOW_ANSI_ERASE) && |
| 213 | strchr("JKMPX", src[i]))) { |
| 214 | strbuf_add(dest, src, i + 1); |
| 215 | return i; |
| 216 | } |
| 217 | if (!isdigit(src[i]) && src[i] != ':' && src[i] != ';') |
| 218 | break; |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static void strbuf_add_sanitized(struct strbuf *dest, const char *src, int n) |
| 225 | { |
| 226 | int i; |
| 227 | |
| 228 | if ((allow_control_characters & ALLOW_ALL_CONTROL_CHARACTERS)) { |
| 229 | strbuf_add(dest, src, n); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | strbuf_grow(dest, n); |
| 234 | for (; n && *src; src++, n--) { |
| 235 | if (!iscntrl(*src) || *src == '\t' || *src == '\n') { |
| 236 | strbuf_addch(dest, *src); |
| 237 | } else if (allow_control_characters != ALLOW_NO_CONTROL_CHARACTERS && |
| 238 | (i = handle_ansi_sequence(dest, src, n))) { |
| 239 | src += i; |
| 240 | n -= i; |
| 241 | } else { |
| 242 | strbuf_addch(dest, '^'); |
| 243 | strbuf_addch(dest, *src == 0x7f ? '?' : 0x40 + *src); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Optionally highlight one keyword in remote output if it appears at the start |
| 250 | * of the line. This should be called for a single line only, which is |
| 251 | * passed as the first N characters of the SRC array. |
| 252 | * |
| 253 | * It is fine to use "int n" here instead of "size_t n" as all calls to this |
| 254 | * function pass an 'int' parameter. Additionally, the buffer involved in |
| 255 | * storing these 'int' values takes input from a packet via the pkt-line |
| 256 | * interface, which is capable of transferring only 64kB at a time. |
| 257 | */ |
| 258 | static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n) |
| 259 | { |
| 260 | int i; |
| 261 | |
| 262 | if (!want_color_stderr(use_sideband_colors())) { |
| 263 | strbuf_add_sanitized(dest, src, n); |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | while (0 < n && isspace(*src)) { |
| 268 | strbuf_addch(dest, *src); |
| 269 | src++; |
| 270 | n--; |
| 271 | } |
| 272 | |
| 273 | for (i = 0; i < ARRAY_SIZE(keywords); i++) { |
| 274 | struct keyword_entry *p = keywords + i; |
| 275 | int len = strlen(p->keyword); |
| 276 | |
| 277 | if (n < len) |
| 278 | continue; |
| 279 | /* |
| 280 | * Match case insensitively, so we colorize output from existing |
| 281 | * servers regardless of the case that they use for their |
| 282 | * messages. We only highlight the word precisely, so |
| 283 | * "successful" stays uncolored. |
| 284 | */ |
| 285 | if (!strncasecmp(p->keyword, src, len) && |
| 286 | (len == n || !isalnum(src[len]))) { |
| 287 | strbuf_addstr(dest, p->color); |
| 288 | strbuf_add(dest, src, len); |
| 289 | strbuf_addstr(dest, GIT_COLOR_RESET); |
| 290 | n -= len; |
| 291 | src += len; |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | strbuf_add_sanitized(dest, src, n); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | #define DISPLAY_PREFIX "remote: " |
| 301 | |
| 302 | #define ANSI_PREFIX "\033[K" |
| 303 | #define DUMB_SUFFIX " " |
| 304 | |
| 305 | int demultiplex_sideband(const char *me, int status, |
| 306 | char *buf, int len, |
| 307 | int die_on_error, |
| 308 | struct strbuf *scratch, |
| 309 | enum sideband_type *sideband_type) |
| 310 | { |
| 311 | static const char *prefix, *suffix; |
| 312 | const char *b, *brk; |
| 313 | int band; |
| 314 | |
| 315 | if (!suffix) { |
| 316 | if (isatty(2) && !is_terminal_dumb()) { |
| 317 | prefix = ANSI_PREFIX DISPLAY_PREFIX; |
| 318 | suffix = ""; |
| 319 | } else { |
| 320 | prefix = DISPLAY_PREFIX; |
| 321 | suffix = DUMB_SUFFIX; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | if (status == PACKET_READ_EOF) { |
| 326 | strbuf_addf(scratch, |
| 327 | "%s%s: unexpected disconnect while reading sideband packet", |
| 328 | scratch->len ? "\n" : "", me); |
| 329 | *sideband_type = SIDEBAND_PROTOCOL_ERROR; |
| 330 | goto cleanup; |
| 331 | } |
| 332 | |
| 333 | if (len < 0) |
| 334 | BUG("negative length on non-eof packet read"); |
| 335 | |
| 336 | if (len == 0) { |
| 337 | if (status == PACKET_READ_NORMAL) { |
| 338 | strbuf_addf(scratch, |
| 339 | "%s%s: protocol error: missing sideband designator", |
| 340 | scratch->len ? "\n" : "", me); |
| 341 | *sideband_type = SIDEBAND_PROTOCOL_ERROR; |
| 342 | } else { |
| 343 | /* covers flush, delim, etc */ |
| 344 | *sideband_type = SIDEBAND_FLUSH; |
| 345 | } |
| 346 | goto cleanup; |
| 347 | } |
| 348 | |
| 349 | band = buf[0] & 0xff; |
| 350 | buf[len] = '\0'; |
| 351 | len--; |
| 352 | switch (band) { |
| 353 | case 3: |
| 354 | if (die_on_error) |
| 355 | die(_("remote error: %s"), buf + 1); |
| 356 | strbuf_addf(scratch, "%s%s", scratch->len ? "\n" : "", prefix); |
| 357 | maybe_colorize_sideband(scratch, buf + 1, len); |
| 358 | |
| 359 | *sideband_type = SIDEBAND_REMOTE_ERROR; |
| 360 | break; |
| 361 | case 2: |
| 362 | b = buf + 1; |
| 363 | |
| 364 | /* |
| 365 | * Append a suffix to each nonempty line to clear the |
| 366 | * end of the screen line. |
| 367 | * |
| 368 | * The output is accumulated in a buffer and |
| 369 | * each line is printed to stderr using |
| 370 | * write(2) to ensure inter-process atomicity. |
| 371 | */ |
| 372 | while ((brk = strpbrk(b, "\n\r"))) { |
| 373 | int linelen = brk - b; |
| 374 | |
| 375 | /* |
| 376 | * For message across packet boundary, there would have |
| 377 | * a nonempty "scratch" buffer from last call of this |
| 378 | * function, and there may have a leading CR/LF in "buf". |
| 379 | * For this case we should add a clear-to-eol suffix to |
| 380 | * clean leftover letters we previously have written on |
| 381 | * the same line. |
| 382 | */ |
| 383 | if (scratch->len && !linelen) |
| 384 | strbuf_addstr(scratch, suffix); |
| 385 | |
| 386 | if (!scratch->len) |
| 387 | strbuf_addstr(scratch, prefix); |
| 388 | |
| 389 | /* |
| 390 | * A use case that we should not add clear-to-eol suffix |
| 391 | * to empty lines: |
| 392 | * |
| 393 | * For progress reporting we may receive a bunch of |
| 394 | * percentage updates followed by '\r' to remain on the |
| 395 | * same line, and at the end receive a single '\n' to |
| 396 | * move to the next line. We should preserve the final |
| 397 | * status report line by not appending clear-to-eol |
| 398 | * suffix to this single line break. |
| 399 | */ |
| 400 | if (linelen > 0) { |
| 401 | maybe_colorize_sideband(scratch, b, linelen); |
| 402 | strbuf_addstr(scratch, suffix); |
| 403 | } |
| 404 | |
| 405 | strbuf_addch(scratch, *brk); |
| 406 | write_in_full(2, scratch->buf, scratch->len); |
| 407 | strbuf_reset(scratch); |
| 408 | |
| 409 | b = brk + 1; |
| 410 | } |
| 411 | |
| 412 | if (*b) { |
| 413 | if (!scratch->len) |
| 414 | strbuf_addstr(scratch, prefix); |
| 415 | maybe_colorize_sideband(scratch, b, strlen(b)); |
| 416 | } |
| 417 | return 0; |
| 418 | case 1: |
| 419 | *sideband_type = SIDEBAND_PRIMARY; |
| 420 | return 1; |
| 421 | default: |
| 422 | strbuf_addf(scratch, "%s%s: protocol error: bad band #%d", |
| 423 | scratch->len ? "\n" : "", me, band); |
| 424 | *sideband_type = SIDEBAND_PROTOCOL_ERROR; |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | cleanup: |
| 429 | if (die_on_error && *sideband_type == SIDEBAND_PROTOCOL_ERROR) |
| 430 | die("%s", scratch->buf); |
| 431 | if (scratch->len) { |
| 432 | strbuf_addch(scratch, '\n'); |
| 433 | write_in_full(2, scratch->buf, scratch->len); |
| 434 | } |
| 435 | strbuf_release(scratch); |
| 436 | return 1; |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * fd is connected to the remote side; send the sideband data |
| 441 | * over multiplexed packet stream. |
| 442 | */ |
| 443 | void send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max) |
| 444 | { |
| 445 | const char *p = data; |
| 446 | |
| 447 | while (sz) { |
| 448 | unsigned n; |
| 449 | char hdr[5]; |
| 450 | |
| 451 | n = sz; |
| 452 | if (packet_max - 5 < n) |
| 453 | n = packet_max - 5; |
| 454 | if (0 <= band) { |
| 455 | xsnprintf(hdr, sizeof(hdr), "%04x", n + 5); |
| 456 | hdr[4] = band; |
| 457 | write_or_die(fd, hdr, 5); |
| 458 | } else { |
| 459 | xsnprintf(hdr, sizeof(hdr), "%04x", n + 4); |
| 460 | write_or_die(fd, hdr, 4); |
| 461 | } |
| 462 | write_or_die(fd, p, n); |
| 463 | p += n; |
| 464 | sz -= n; |
| 465 | } |
| 466 | } |