Uninline prefixcmp()
[git.git] / sideband.c
blob756bbc28d71448781294151cbacd30f68b2bb97b
1 #include "pkt-line.h"
2 #include "sideband.h"
4 /*
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);
24 while (1) {
25 int band, len;
26 len = packet_read_line(in_stream, buf + pf, LARGE_PACKET_MAX);
27 if (len == 0)
28 break;
29 if (len < 1) {
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;
35 len--;
36 switch (band) {
37 case 3:
38 buf[pf] = ' ';
39 buf[pf+1+len] = '\n';
40 safe_write(err, buf, pf+1+len+1);
41 return SIDEBAND_REMOTE_ERROR;
42 case 2:
43 buf[pf] = ' ';
44 len += pf+1;
45 while (1) {
46 int brk = pf+1;
48 /* Break the buffer into separate lines. */
49 while (brk < len) {
50 brk++;
51 if (buf[brk-1] == '\n' ||
52 buf[brk-1] == '\r')
53 break;
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.
61 if (brk > pf+1 + 1) {
62 char save[sf];
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);
68 } else
69 safe_write(err, buf, brk);
71 if (brk < len) {
72 memmove(buf + pf+1, buf + brk, len - brk);
73 len = len - brk + pf+1;
74 } else
75 break;
77 continue;
78 case 1:
79 safe_write(out, buf + pf+1, len);
80 continue;
81 default:
82 len = sprintf(buf,
83 "%s: protocol error: bad band #%d\n",
84 me, band);
85 safe_write(err, buf, len);
86 return SIDEBAND_PROTOCOL_ERROR;
89 return 0;
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)
98 ssize_t ssz = sz;
99 const char *p = data;
101 while (sz) {
102 unsigned n;
103 char hdr[5];
105 n = sz;
106 if (packet_max - 5 < n)
107 n = packet_max - 5;
108 sprintf(hdr, "%04x", n + 5);
109 hdr[4] = band;
110 safe_write(fd, hdr, 5);
111 safe_write(fd, p, n);
112 p += n;
113 sz -= n;
115 return ssz;