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:"
16 #define SUFFIX "\033[K" /* change to " " if ANSI sequences don't work */
18 int recv_sideband(const char *me, int in_stream, int out, int err)
20 unsigned pf = strlen(PREFIX);
21 unsigned sf = strlen(SUFFIX);
22 char buf[pf + LARGE_PACKET_MAX + sf + 1];
23 memcpy(buf, PREFIX, pf);
26 len = packet_read_line(in_stream, buf + pf, LARGE_PACKET_MAX);
30 len = sprintf(buf, "%s: protocol error: no band designator\n", me);
31 safe_write(err, buf, len);
32 return SIDEBAND_PROTOCOL_ERROR;
34 band = buf[pf] & 0xff;
40 safe_write(err, buf, pf+1+len+1);
41 return SIDEBAND_REMOTE_ERROR;
48 /* Break the buffer into separate lines. */
51 if (buf[brk-1] == '\n' ||
57 * Let's insert a suffix to clear the end
58 * of the screen line, but only if current
59 * line data actually contains something.
63 memcpy(save, buf + brk, sf);
64 buf[brk + sf - 1] = buf[brk - 1];
65 memcpy(buf + brk - 1, SUFFIX, sf);
66 safe_write(err, buf, brk + sf);
67 memcpy(buf + brk, save, sf);
69 safe_write(err, buf, brk);
72 memmove(buf + pf+1, buf + brk, len - brk);
73 len = len - brk + pf+1;
79 safe_write(out, buf + pf+1, len);
83 "%s: protocol error: bad band #%d\n",
85 safe_write(err, buf, len);
86 return SIDEBAND_PROTOCOL_ERROR;
93 * fd is connected to the remote side; send the sideband data
94 * over multiplexed packet stream.
96 ssize_t send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
106 if (packet_max - 5 < n)
108 sprintf(hdr, "%04x", n + 5);
110 safe_write(fd, hdr, 5);
111 safe_write(fd, p, n);