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, int err)
24 unsigned pf = strlen(PREFIX);
26 char buf[LARGE_PACKET_MAX + 2*FIX_SIZE];
29 memcpy(buf, PREFIX, pf);
30 term = getenv("TERM");
31 if (term && strcmp(term, "dumb"))
39 len = packet_read_line(in_stream, buf + pf, LARGE_PACKET_MAX);
43 len = sprintf(buf, "%s: protocol error: no band designator\n", me);
44 safe_write(err, buf, len);
45 return SIDEBAND_PROTOCOL_ERROR;
47 band = buf[pf] & 0xff;
53 safe_write(err, buf, pf+1+len+1);
54 return SIDEBAND_REMOTE_ERROR;
61 /* Break the buffer into separate lines. */
64 if (buf[brk-1] == '\n' ||
70 * Let's insert a suffix to clear the end
71 * of the screen line, but only if current
72 * line data actually contains something.
76 memcpy(save, buf + brk, sf);
77 buf[brk + sf - 1] = buf[brk - 1];
78 memcpy(buf + brk - 1, suffix, sf);
79 safe_write(err, buf, brk + sf);
80 memcpy(buf + brk, save, sf);
82 safe_write(err, buf, brk);
85 memmove(buf + pf+1, buf + brk, len - brk);
86 len = len - brk + pf+1;
92 safe_write(out, buf + pf+1, len);
96 "%s: protocol error: bad band #%d\n",
98 safe_write(err, buf, len);
99 return SIDEBAND_PROTOCOL_ERROR;
106 * fd is connected to the remote side; send the sideband data
107 * over multiplexed packet stream.
109 ssize_t send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
112 const char *p = data;
119 if (packet_max - 5 < n)
121 sprintf(hdr, "%04x", n + 5);
123 safe_write(fd, hdr, 5);
124 safe_write(fd, p, n);