2013-02-21 04:01:56 +08:00
|
|
|
#include "cache.h"
|
2006-09-10 16:06:33 +08:00
|
|
|
#include "pkt-line.h"
|
|
|
|
#include "sideband.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive multiplexed output stream over git native protocol.
|
|
|
|
* in_stream is the input stream from the remote, which carries data
|
|
|
|
* in pkt_line format with band designator. Demultiplex it into out
|
|
|
|
* and err and return error appropriately. Band #1 carries the
|
|
|
|
* primary payload. Things coming over band #2 is not necessarily
|
|
|
|
* error; they are usually informative message on the standard error
|
|
|
|
* stream, aka "verbose"). A message over band #3 is a signal that
|
|
|
|
* the remote died unexpectedly. A flush() concludes the stream.
|
|
|
|
*/
|
2007-11-04 12:22:42 +08:00
|
|
|
|
2016-06-28 12:35:26 +08:00
|
|
|
#define PREFIX "remote: "
|
2008-01-09 00:24:53 +08:00
|
|
|
|
|
|
|
#define ANSI_SUFFIX "\033[K"
|
|
|
|
#define DUMB_SUFFIX " "
|
|
|
|
|
2009-03-11 05:54:17 +08:00
|
|
|
int recv_sideband(const char *me, int in_stream, int out)
|
2006-09-10 16:06:33 +08:00
|
|
|
{
|
2017-11-29 22:37:51 +08:00
|
|
|
const char *suffix;
|
2016-06-28 12:35:26 +08:00
|
|
|
char buf[LARGE_PACKET_MAX + 1];
|
|
|
|
struct strbuf outbuf = STRBUF_INIT;
|
|
|
|
int retval = 0;
|
2008-01-09 00:24:53 +08:00
|
|
|
|
2017-11-29 22:37:51 +08:00
|
|
|
if (isatty(2) && !is_terminal_dumb())
|
2008-01-09 00:24:53 +08:00
|
|
|
suffix = ANSI_SUFFIX;
|
|
|
|
else
|
|
|
|
suffix = DUMB_SUFFIX;
|
|
|
|
|
2016-06-28 12:35:26 +08:00
|
|
|
while (!retval) {
|
|
|
|
const char *b, *brk;
|
2006-10-11 23:49:15 +08:00
|
|
|
int band, len;
|
2016-06-28 12:35:26 +08:00
|
|
|
len = packet_read(in_stream, NULL, NULL, buf, LARGE_PACKET_MAX, 0);
|
2006-09-10 16:06:33 +08:00
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
if (len < 1) {
|
2016-06-28 12:35:26 +08:00
|
|
|
strbuf_addf(&outbuf,
|
|
|
|
"%s%s: protocol error: no band designator",
|
|
|
|
outbuf.len ? "\n" : "", me);
|
|
|
|
retval = SIDEBAND_PROTOCOL_ERROR;
|
|
|
|
break;
|
2006-09-10 16:06:33 +08:00
|
|
|
}
|
2016-06-28 12:35:26 +08:00
|
|
|
band = buf[0] & 0xff;
|
|
|
|
buf[len] = '\0';
|
2006-09-10 16:06:33 +08:00
|
|
|
len--;
|
2006-10-11 23:49:15 +08:00
|
|
|
switch (band) {
|
2006-09-10 16:06:33 +08:00
|
|
|
case 3:
|
2016-06-28 12:35:26 +08:00
|
|
|
strbuf_addf(&outbuf, "%s%s%s", outbuf.len ? "\n" : "",
|
|
|
|
PREFIX, buf + 1);
|
|
|
|
retval = SIDEBAND_REMOTE_ERROR;
|
|
|
|
break;
|
2006-09-10 16:06:33 +08:00
|
|
|
case 2:
|
2016-06-28 12:35:26 +08:00
|
|
|
b = buf + 1;
|
2007-11-04 12:22:42 +08:00
|
|
|
|
2016-06-28 12:35:26 +08:00
|
|
|
/*
|
|
|
|
* Append a suffix to each nonempty line to clear the
|
|
|
|
* end of the screen line.
|
|
|
|
*
|
|
|
|
* The output is accumulated in a buffer and
|
|
|
|
* each line is printed to stderr using
|
|
|
|
* write(2) to ensure inter-process atomicity.
|
|
|
|
*/
|
|
|
|
while ((brk = strpbrk(b, "\n\r"))) {
|
|
|
|
int linelen = brk - b;
|
2007-11-04 12:22:42 +08:00
|
|
|
|
2016-06-28 12:35:26 +08:00
|
|
|
if (!outbuf.len)
|
2016-07-06 04:35:50 +08:00
|
|
|
strbuf_addstr(&outbuf, PREFIX);
|
2016-06-28 12:35:26 +08:00
|
|
|
if (linelen > 0) {
|
|
|
|
strbuf_addf(&outbuf, "%.*s%s%c",
|
|
|
|
linelen, b, suffix, *brk);
|
2008-09-04 03:13:42 +08:00
|
|
|
} else {
|
2016-07-06 04:35:50 +08:00
|
|
|
strbuf_addch(&outbuf, *brk);
|
2008-09-04 03:13:42 +08:00
|
|
|
}
|
2016-06-28 12:35:26 +08:00
|
|
|
xwrite(2, outbuf.buf, outbuf.len);
|
|
|
|
strbuf_reset(&outbuf);
|
2007-11-04 12:22:42 +08:00
|
|
|
|
2016-06-28 12:35:26 +08:00
|
|
|
b = brk + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*b)
|
|
|
|
strbuf_addf(&outbuf, "%s%s",
|
|
|
|
outbuf.len ? "" : PREFIX, b);
|
|
|
|
break;
|
2006-09-10 16:06:33 +08:00
|
|
|
case 1:
|
2016-06-28 12:35:26 +08:00
|
|
|
write_or_die(out, buf + 1, len);
|
|
|
|
break;
|
2006-09-10 16:06:33 +08:00
|
|
|
default:
|
2016-06-28 12:35:26 +08:00
|
|
|
strbuf_addf(&outbuf, "%s%s: protocol error: bad band #%d",
|
|
|
|
outbuf.len ? "\n" : "", me, band);
|
|
|
|
retval = SIDEBAND_PROTOCOL_ERROR;
|
|
|
|
break;
|
2006-09-10 16:06:33 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-28 12:35:26 +08:00
|
|
|
|
|
|
|
if (outbuf.len) {
|
2016-07-06 04:35:50 +08:00
|
|
|
strbuf_addch(&outbuf, '\n');
|
2016-06-28 12:35:26 +08:00
|
|
|
xwrite(2, outbuf.buf, outbuf.len);
|
|
|
|
}
|
|
|
|
strbuf_release(&outbuf);
|
|
|
|
return retval;
|
2006-09-10 16:06:33 +08:00
|
|
|
}
|
2006-09-10 18:20:24 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* fd is connected to the remote side; send the sideband data
|
|
|
|
* over multiplexed packet stream.
|
|
|
|
*/
|
2016-06-14 22:49:16 +08:00
|
|
|
void send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
|
2006-09-10 18:20:24 +08:00
|
|
|
{
|
|
|
|
const char *p = data;
|
|
|
|
|
|
|
|
while (sz) {
|
|
|
|
unsigned n;
|
|
|
|
char hdr[5];
|
|
|
|
|
|
|
|
n = sz;
|
|
|
|
if (packet_max - 5 < n)
|
|
|
|
n = packet_max - 5;
|
2009-10-31 08:47:41 +08:00
|
|
|
if (0 <= band) {
|
2015-09-25 05:06:08 +08:00
|
|
|
xsnprintf(hdr, sizeof(hdr), "%04x", n + 5);
|
2009-10-31 08:47:41 +08:00
|
|
|
hdr[4] = band;
|
2013-02-21 04:01:56 +08:00
|
|
|
write_or_die(fd, hdr, 5);
|
2009-10-31 08:47:41 +08:00
|
|
|
} else {
|
2015-09-25 05:06:08 +08:00
|
|
|
xsnprintf(hdr, sizeof(hdr), "%04x", n + 4);
|
2013-02-21 04:01:56 +08:00
|
|
|
write_or_die(fd, hdr, 4);
|
2009-10-31 08:47:41 +08:00
|
|
|
}
|
2013-02-21 04:01:56 +08:00
|
|
|
write_or_die(fd, p, n);
|
2006-09-10 18:20:24 +08:00
|
|
|
p += n;
|
|
|
|
sz -= n;
|
|
|
|
}
|
|
|
|
}
|