5 * Receive multiplexed output stream over git native protocol.
6 * in_stream is the input stream from the remote, which carries data
7 * in pkt_line format with band designator. Demultiplex it into out
8 * and err and return error appropriately. Band #1 carries the
9 * primary payload. Things coming over band #2 is not necessarily
10 * error; they are usually informative message on the standard error
11 * stream, aka "verbose"). A message over band #3 is a signal that
12 * the remote died unexpectedly. A flush() concludes the stream.
15 #define PREFIX "remote:"
17 #define ANSI_SUFFIX "\033[K"
18 #define DUMB_SUFFIX " "
20 #define FIX_SIZE 10 /* large enough for any of the above */
22 int recv_sideband(const char *me, int in_stream, int out)
24 unsigned pf = strlen(PREFIX);
26 char buf[LARGE_PACKET_MAX + 2*FIX_SIZE];
30 memcpy(buf, PREFIX, pf);
31 term = getenv("TERM");
32 if (term && strcmp(term, "dumb"))
40 len = packet_read_line(in_stream, buf + pf, LARGE_PACKET_MAX);
44 fprintf(stderr, "%s: protocol error: no band designator\n", me);
45 return SIDEBAND_PROTOCOL_ERROR;
47 band = buf[pf] & 0xff;
53 fprintf(stderr, "%s\n", buf);
54 return SIDEBAND_REMOTE_ERROR;
62 * If the last buffer didn't end with a line
63 * break then we should not print a prefix
73 /* Look for a line break. */
80 if (b[brk-1] == '\n' ||
86 * Let's insert a suffix to clear the end
87 * of the screen line if a line break was
88 * found. Also, if we don't skip the
89 * prefix, then a non-empty string must be
92 if (brk > (skip_pf ? 0 : (pf+1 + 1))) {
94 memcpy(save, b + brk, sf);
95 b[brk + sf - 1] = b[brk - 1];
96 memcpy(b + brk - 1, suffix, sf);
97 fprintf(stderr, "%.*s", brk + sf, b);
98 memcpy(b + brk, save, sf);
101 int l = brk ? brk : len;
102 fprintf(stderr, "%.*s", l, b);
107 memmove(buf + pf+1, b + brk, len);
111 safe_write(out, buf + pf+1, len);
114 fprintf(stderr, "%s: protocol error: bad band #%d\n",
116 return SIDEBAND_PROTOCOL_ERROR;
123 * fd is connected to the remote side; send the sideband data
124 * over multiplexed packet stream.
126 ssize_t send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
129 const char *p = data;
136 if (packet_max - 5 < n)
139 sprintf(hdr, "%04x", n + 5);
141 safe_write(fd, hdr, 5);
143 sprintf(hdr, "%04x", n + 4);
144 safe_write(fd, hdr, 4);
146 safe_write(fd, p, n);