Merge branch 'master' of git://git.kernel.org/pub/scm/gitk/gitk
[git/git.git] / sideband.c
CommitLineData
49a52b1d
JH
1#include "pkt-line.h"
2#include "sideband.h"
3
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.
13 */
ebe8fa73
NP
14
15#define PREFIX "remote:"
0d8aafd2 16#define SUFFIX "\033[K" /* change to " " if ANSI sequences don't work */
ebe8fa73 17
9ac13ec9 18int recv_sideband(const char *me, int in_stream, int out, int err)
49a52b1d 19{
ebe8fa73
NP
20 unsigned pf = strlen(PREFIX);
21 unsigned sf = strlen(SUFFIX);
22 char buf[pf + LARGE_PACKET_MAX + sf + 1];
23 memcpy(buf, PREFIX, pf);
49a52b1d 24 while (1) {
9ac13ec9 25 int band, len;
ebe8fa73 26 len = packet_read_line(in_stream, buf + pf, LARGE_PACKET_MAX);
49a52b1d
JH
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;
33 }
ebe8fa73 34 band = buf[pf] & 0xff;
49a52b1d 35 len--;
9ac13ec9 36 switch (band) {
49a52b1d 37 case 3:
ebe8fa73
NP
38 buf[pf] = ' ';
39 buf[pf+1+len] = '\n';
40 safe_write(err, buf, pf+1+len+1);
49a52b1d
JH
41 return SIDEBAND_REMOTE_ERROR;
42 case 2:
ebe8fa73
NP
43 buf[pf] = ' ';
44 len += pf+1;
ed1902ef 45 while (1) {
ebe8fa73
NP
46 int brk = pf+1;
47
48 /* Break the buffer into separate lines. */
ed1902ef
NP
49 while (brk < len) {
50 brk++;
51 if (buf[brk-1] == '\n' ||
52 buf[brk-1] == '\r')
53 break;
54 }
ebe8fa73
NP
55
56 /*
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.
60 */
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);
70
ed1902ef 71 if (brk < len) {
ebe8fa73
NP
72 memmove(buf + pf+1, buf + brk, len - brk);
73 len = len - brk + pf+1;
ed1902ef
NP
74 } else
75 break;
76 }
49a52b1d
JH
77 continue;
78 case 1:
ebe8fa73 79 safe_write(out, buf + pf+1, len);
49a52b1d
JH
80 continue;
81 default:
9ac13ec9 82 len = sprintf(buf,
49a52b1d 83 "%s: protocol error: bad band #%d\n",
9ac13ec9
NP
84 me, band);
85 safe_write(err, buf, len);
49a52b1d
JH
86 return SIDEBAND_PROTOCOL_ERROR;
87 }
88 }
89 return 0;
90}
958c24b1
JH
91
92/*
93 * fd is connected to the remote side; send the sideband data
94 * over multiplexed packet stream.
95 */
96ssize_t send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
97{
98 ssize_t ssz = sz;
99 const char *p = data;
100
101 while (sz) {
102 unsigned n;
103 char hdr[5];
104
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;
114 }
115 return ssz;
116}