Merge branch 'jk/http-error-messages'
[git/gitweb.git] / sideband.c
blobd1125f5c528b680f915bb2fbf54231cc8c3b2ec0
1 #include "cache.h"
2 #include "pkt-line.h"
3 #include "sideband.h"
5 /*
6 * Receive multiplexed output stream over git native protocol.
7 * in_stream is the input stream from the remote, which carries data
8 * in pkt_line format with band designator. Demultiplex it into out
9 * and err and return error appropriately. Band #1 carries the
10 * primary payload. Things coming over band #2 is not necessarily
11 * error; they are usually informative message on the standard error
12 * stream, aka "verbose"). A message over band #3 is a signal that
13 * the remote died unexpectedly. A flush() concludes the stream.
16 #define PREFIX "remote:"
18 #define ANSI_SUFFIX "\033[K"
19 #define DUMB_SUFFIX " "
21 #define FIX_SIZE 10 /* large enough for any of the above */
23 int recv_sideband(const char *me, int in_stream, int out)
25 unsigned pf = strlen(PREFIX);
26 unsigned sf;
27 char buf[LARGE_PACKET_MAX + 2*FIX_SIZE];
28 char *suffix, *term;
29 int skip_pf = 0;
31 memcpy(buf, PREFIX, pf);
32 term = getenv("TERM");
33 if (term && strcmp(term, "dumb"))
34 suffix = ANSI_SUFFIX;
35 else
36 suffix = DUMB_SUFFIX;
37 sf = strlen(suffix);
39 while (1) {
40 int band, len;
41 len = packet_read(in_stream, NULL, NULL, buf + pf, LARGE_PACKET_MAX, 0);
42 if (len == 0)
43 break;
44 if (len < 1) {
45 fprintf(stderr, "%s: protocol error: no band designator\n", me);
46 return SIDEBAND_PROTOCOL_ERROR;
48 band = buf[pf] & 0xff;
49 len--;
50 switch (band) {
51 case 3:
52 buf[pf] = ' ';
53 buf[pf+1+len] = '\0';
54 fprintf(stderr, "%s\n", buf);
55 return SIDEBAND_REMOTE_ERROR;
56 case 2:
57 buf[pf] = ' ';
58 do {
59 char *b = buf;
60 int brk = 0;
63 * If the last buffer didn't end with a line
64 * break then we should not print a prefix
65 * this time around.
67 if (skip_pf) {
68 b += pf+1;
69 } else {
70 len += pf+1;
71 brk += pf+1;
74 /* Look for a line break. */
75 for (;;) {
76 brk++;
77 if (brk > len) {
78 brk = 0;
79 break;
81 if (b[brk-1] == '\n' ||
82 b[brk-1] == '\r')
83 break;
87 * Let's insert a suffix to clear the end
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.
93 if (brk > (skip_pf ? 0 : (pf+1 + 1))) {
94 char save[FIX_SIZE];
95 memcpy(save, b + brk, sf);
96 b[brk + sf - 1] = b[brk - 1];
97 memcpy(b + brk - 1, suffix, sf);
98 fprintf(stderr, "%.*s", brk + sf, b);
99 memcpy(b + brk, save, sf);
100 len -= brk;
101 } else {
102 int l = brk ? brk : len;
103 fprintf(stderr, "%.*s", l, b);
104 len -= l;
107 skip_pf = !brk;
108 memmove(buf + pf+1, b + brk, len);
109 } while (len);
110 continue;
111 case 1:
112 write_or_die(out, buf + pf+1, len);
113 continue;
114 default:
115 fprintf(stderr, "%s: protocol error: bad band #%d\n",
116 me, band);
117 return SIDEBAND_PROTOCOL_ERROR;
120 return 0;
124 * fd is connected to the remote side; send the sideband data
125 * over multiplexed packet stream.
127 ssize_t send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
129 ssize_t ssz = sz;
130 const char *p = data;
132 while (sz) {
133 unsigned n;
134 char hdr[5];
136 n = sz;
137 if (packet_max - 5 < n)
138 n = packet_max - 5;
139 if (0 <= band) {
140 sprintf(hdr, "%04x", n + 5);
141 hdr[4] = band;
142 write_or_die(fd, hdr, 5);
143 } else {
144 sprintf(hdr, "%04x", n + 4);
145 write_or_die(fd, hdr, 4);
147 write_or_die(fd, p, n);
148 p += n;
149 sz -= n;
151 return ssz;