]>
Commit | Line | Data |
---|---|---|
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:" | |
13e4760a JS |
16 | |
17 | #define ANSI_SUFFIX "\033[K" | |
18 | #define DUMB_SUFFIX " " | |
19 | ||
20 | #define FIX_SIZE 10 /* large enough for any of the above */ | |
ebe8fa73 | 21 | |
9ac13ec9 | 22 | int recv_sideband(const char *me, int in_stream, int out, int err) |
49a52b1d | 23 | { |
ebe8fa73 | 24 | unsigned pf = strlen(PREFIX); |
13e4760a JS |
25 | unsigned sf; |
26 | char buf[LARGE_PACKET_MAX + 2*FIX_SIZE]; | |
27 | char *suffix, *term; | |
6b9c42b4 | 28 | int skip_pf = 0; |
13e4760a | 29 | |
ebe8fa73 | 30 | memcpy(buf, PREFIX, pf); |
13e4760a JS |
31 | term = getenv("TERM"); |
32 | if (term && strcmp(term, "dumb")) | |
33 | suffix = ANSI_SUFFIX; | |
34 | else | |
35 | suffix = DUMB_SUFFIX; | |
36 | sf = strlen(suffix); | |
37 | ||
49a52b1d | 38 | while (1) { |
9ac13ec9 | 39 | int band, len; |
ebe8fa73 | 40 | len = packet_read_line(in_stream, buf + pf, LARGE_PACKET_MAX); |
49a52b1d JH |
41 | if (len == 0) |
42 | break; | |
43 | if (len < 1) { | |
44 | len = sprintf(buf, "%s: protocol error: no band designator\n", me); | |
45 | safe_write(err, buf, len); | |
46 | return SIDEBAND_PROTOCOL_ERROR; | |
47 | } | |
ebe8fa73 | 48 | band = buf[pf] & 0xff; |
49a52b1d | 49 | len--; |
9ac13ec9 | 50 | switch (band) { |
49a52b1d | 51 | case 3: |
ebe8fa73 NP |
52 | buf[pf] = ' '; |
53 | buf[pf+1+len] = '\n'; | |
54 | safe_write(err, buf, pf+1+len+1); | |
49a52b1d JH |
55 | return SIDEBAND_REMOTE_ERROR; |
56 | case 2: | |
ebe8fa73 | 57 | buf[pf] = ' '; |
6b9c42b4 NP |
58 | do { |
59 | char *b = buf; | |
60 | int brk = 0; | |
ebe8fa73 | 61 | |
6b9c42b4 NP |
62 | /* |
63 | * If the last buffer didn't end with a line | |
64 | * break then we should not print a prefix | |
65 | * this time around. | |
66 | */ | |
67 | if (skip_pf) { | |
68 | b += pf+1; | |
69 | } else { | |
70 | len += pf+1; | |
71 | brk += pf+1; | |
72 | } | |
73 | ||
74 | /* Look for a line break. */ | |
75 | for (;;) { | |
ed1902ef | 76 | brk++; |
6b9c42b4 NP |
77 | if (brk > len) { |
78 | brk = 0; | |
79 | break; | |
80 | } | |
81 | if (b[brk-1] == '\n' || | |
82 | b[brk-1] == '\r') | |
ed1902ef NP |
83 | break; |
84 | } | |
ebe8fa73 NP |
85 | |
86 | /* | |
87 | * Let's insert a suffix to clear the end | |
6b9c42b4 NP |
88 | * of the screen line if a line break was |
89 | * found. Also, if we don't skip the | |
90 | * prefix, then a non-empty string must be | |
91 | * present too. | |
ebe8fa73 | 92 | */ |
6b9c42b4 | 93 | if (brk > (skip_pf ? 0 : (pf+1 + 1))) { |
13e4760a | 94 | char save[FIX_SIZE]; |
6b9c42b4 NP |
95 | memcpy(save, b + brk, sf); |
96 | b[brk + sf - 1] = b[brk - 1]; | |
97 | memcpy(b + brk - 1, suffix, sf); | |
98 | safe_write(err, b, brk + sf); | |
99 | memcpy(b + brk, save, sf); | |
100 | len -= brk; | |
101 | } else { | |
102 | int l = brk ? brk : len; | |
103 | safe_write(err, b, l); | |
104 | len -= l; | |
105 | } | |
ebe8fa73 | 106 | |
6b9c42b4 NP |
107 | skip_pf = !brk; |
108 | memmove(buf + pf+1, b + brk, len); | |
109 | } while (len); | |
49a52b1d JH |
110 | continue; |
111 | case 1: | |
ebe8fa73 | 112 | safe_write(out, buf + pf+1, len); |
49a52b1d JH |
113 | continue; |
114 | default: | |
9ac13ec9 | 115 | len = sprintf(buf, |
49a52b1d | 116 | "%s: protocol error: bad band #%d\n", |
9ac13ec9 NP |
117 | me, band); |
118 | safe_write(err, buf, len); | |
49a52b1d JH |
119 | return SIDEBAND_PROTOCOL_ERROR; |
120 | } | |
121 | } | |
122 | return 0; | |
123 | } | |
958c24b1 JH |
124 | |
125 | /* | |
126 | * fd is connected to the remote side; send the sideband data | |
127 | * over multiplexed packet stream. | |
128 | */ | |
129 | ssize_t send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max) | |
130 | { | |
131 | ssize_t ssz = sz; | |
132 | const char *p = data; | |
133 | ||
134 | while (sz) { | |
135 | unsigned n; | |
136 | char hdr[5]; | |
137 | ||
138 | n = sz; | |
139 | if (packet_max - 5 < n) | |
140 | n = packet_max - 5; | |
141 | sprintf(hdr, "%04x", n + 5); | |
142 | hdr[4] = band; | |
143 | safe_write(fd, hdr, 5); | |
144 | safe_write(fd, p, n); | |
145 | p += n; | |
146 | sz -= n; | |
147 | } | |
148 | return ssz; | |
149 | } |