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 int recv_sideband(const char *me
, int in_stream
, int out
)
23 const char *term
, *suffix
;
24 char buf
[LARGE_PACKET_MAX
+ 1];
25 struct strbuf outbuf
= STRBUF_INIT
;
28 term
= getenv("TERM");
29 if (isatty(2) && term
&& strcmp(term
, "dumb"))
37 len
= packet_read(in_stream
, NULL
, NULL
, buf
, LARGE_PACKET_MAX
, 0);
42 "%s%s: protocol error: no band designator",
43 outbuf
.len
? "\n" : "", me
);
44 retval
= SIDEBAND_PROTOCOL_ERROR
;
52 strbuf_addf(&outbuf
, "%s%s%s", outbuf
.len
? "\n" : "",
54 retval
= SIDEBAND_REMOTE_ERROR
;
60 * Append a suffix to each nonempty line to clear the
61 * end of the screen line.
63 * The output is accumulated in a buffer and
64 * each line is printed to stderr using
65 * write(2) to ensure inter-process atomicity.
67 while ((brk
= strpbrk(b
, "\n\r"))) {
68 int linelen
= brk
- b
;
71 strbuf_addstr(&outbuf
, PREFIX
);
73 strbuf_addf(&outbuf
, "%.*s%s%c",
74 linelen
, b
, suffix
, *brk
);
76 strbuf_addch(&outbuf
, *brk
);
78 xwrite(2, outbuf
.buf
, outbuf
.len
);
79 strbuf_reset(&outbuf
);
85 strbuf_addf(&outbuf
, "%s%s",
86 outbuf
.len
? "" : PREFIX
, b
);
89 write_or_die(out
, buf
+ 1, len
);
92 strbuf_addf(&outbuf
, "%s%s: protocol error: bad band #%d",
93 outbuf
.len
? "\n" : "", me
, band
);
94 retval
= SIDEBAND_PROTOCOL_ERROR
;
100 strbuf_addch(&outbuf
, '\n');
101 xwrite(2, outbuf
.buf
, outbuf
.len
);
103 strbuf_release(&outbuf
);
108 * fd is connected to the remote side; send the sideband data
109 * over multiplexed packet stream.
111 void send_sideband(int fd
, int band
, const char *data
, ssize_t sz
, int packet_max
)
113 const char *p
= data
;
120 if (packet_max
- 5 < n
)
123 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 5);
125 write_or_die(fd
, hdr
, 5);
127 xsnprintf(hdr
, sizeof(hdr
), "%04x", n
+ 4);
128 write_or_die(fd
, hdr
, 4);
130 write_or_die(fd
, p
, n
);