Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 151 lines (137 sloc) 3.44 kb
49a52b1d » Junio C Hamano
2006-09-10 Move sideband client side support into reusable form.
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 » Nicolas Pitre
2007-11-04 fix display overlap between remote and local progress
14
15 #define PREFIX "remote:"
13e4760a » Johannes Sixt
2008-01-08 recv_sideband: Do not use ANSI escape sequence on dumb terminals.
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 » Nicolas Pitre
2007-11-04 fix display overlap between remote and local progress
21
34df8aba » Johannes Sixt
2009-03-10 recv_sideband: Bands #2 and #3 always go to stderr
22 int recv_sideband(const char *me, int in_stream, int out)
49a52b1d » Junio C Hamano
2006-09-10 Move sideband client side support into reusable form.
23 {
ebe8fa73 » Nicolas Pitre
2007-11-04 fix display overlap between remote and local progress
24 unsigned pf = strlen(PREFIX);
13e4760a » Johannes Sixt
2008-01-08 recv_sideband: Do not use ANSI escape sequence on dumb terminals.
25 unsigned sf;
26 char buf[LARGE_PACKET_MAX + 2*FIX_SIZE];
27 char *suffix, *term;
6b9c42b4 » Nicolas Pitre
2008-09-03 improve handling of sideband message display
28 int skip_pf = 0;
13e4760a » Johannes Sixt
2008-01-08 recv_sideband: Do not use ANSI escape sequence on dumb terminals.
29
ebe8fa73 » Nicolas Pitre
2007-11-04 fix display overlap between remote and local progress
30 memcpy(buf, PREFIX, pf);
13e4760a » Johannes Sixt
2008-01-08 recv_sideband: Do not use ANSI escape sequence on dumb terminals.
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 » Junio C Hamano
2006-09-10 Move sideband client side support into reusable form.
38 while (1) {
9ac13ec9 » Nicolas Pitre
2006-10-11 atomic write for sideband remote messages
39 int band, len;
ebe8fa73 » Nicolas Pitre
2007-11-04 fix display overlap between remote and local progress
40 len = packet_read_line(in_stream, buf + pf, LARGE_PACKET_MAX);
49a52b1d » Junio C Hamano
2006-09-10 Move sideband client side support into reusable form.
41 if (len == 0)
42 break;
43 if (len < 1) {
34df8aba » Johannes Sixt
2009-03-10 recv_sideband: Bands #2 and #3 always go to stderr
44 fprintf(stderr, "%s: protocol error: no band designator\n", me);
49a52b1d » Junio C Hamano
2006-09-10 Move sideband client side support into reusable form.
45 return SIDEBAND_PROTOCOL_ERROR;
46 }
ebe8fa73 » Nicolas Pitre
2007-11-04 fix display overlap between remote and local progress
47 band = buf[pf] & 0xff;
49a52b1d » Junio C Hamano
2006-09-10 Move sideband client side support into reusable form.
48 len--;
9ac13ec9 » Nicolas Pitre
2006-10-11 atomic write for sideband remote messages
49 switch (band) {
49a52b1d » Junio C Hamano
2006-09-10 Move sideband client side support into reusable form.
50 case 3:
ebe8fa73 » Nicolas Pitre
2007-11-04 fix display overlap between remote and local progress
51 buf[pf] = ' ';
34df8aba » Johannes Sixt
2009-03-10 recv_sideband: Bands #2 and #3 always go to stderr
52 buf[pf+1+len] = '\0';
53 fprintf(stderr, "%s\n", buf);
49a52b1d » Junio C Hamano
2006-09-10 Move sideband client side support into reusable form.
54 return SIDEBAND_REMOTE_ERROR;
55 case 2:
ebe8fa73 » Nicolas Pitre
2007-11-04 fix display overlap between remote and local progress
56 buf[pf] = ' ';
6b9c42b4 » Nicolas Pitre
2008-09-03 improve handling of sideband message display
57 do {
58 char *b = buf;
59 int brk = 0;
ebe8fa73 » Nicolas Pitre
2007-11-04 fix display overlap between remote and local progress
60
6b9c42b4 » Nicolas Pitre
2008-09-03 improve handling of sideband message display
61 /*
62 * If the last buffer didn't end with a line
63 * break then we should not print a prefix
64 * this time around.
65 */
66 if (skip_pf) {
67 b += pf+1;
68 } else {
69 len += pf+1;
70 brk += pf+1;
71 }
72
73 /* Look for a line break. */
74 for (;;) {
ed1902ef » Nicolas Pitre
2007-10-16 cope with multiple line breaks within sideband progress messages
75 brk++;
6b9c42b4 » Nicolas Pitre
2008-09-03 improve handling of sideband message display
76 if (brk > len) {
77 brk = 0;
78 break;
79 }
80 if (b[brk-1] == '\n' ||
81 b[brk-1] == '\r')
ed1902ef » Nicolas Pitre
2007-10-16 cope with multiple line breaks within sideband progress messages
82 break;
83 }
ebe8fa73 » Nicolas Pitre
2007-11-04 fix display overlap between remote and local progress
84
85 /*
86 * Let's insert a suffix to clear the end
6b9c42b4 » Nicolas Pitre
2008-09-03 improve handling of sideband message display
87 * of the screen line if a line break was
88 * found. Also, if we don't skip the
89 * prefix, then a non-empty string must be
90 * present too.
ebe8fa73 » Nicolas Pitre
2007-11-04 fix display overlap between remote and local progress
91 */
6b9c42b4 » Nicolas Pitre
2008-09-03 improve handling of sideband message display
92 if (brk > (skip_pf ? 0 : (pf+1 + 1))) {
13e4760a » Johannes Sixt
2008-01-08 recv_sideband: Do not use ANSI escape sequence on dumb terminals.
93 char save[FIX_SIZE];
6b9c42b4 » Nicolas Pitre
2008-09-03 improve handling of sideband message display
94 memcpy(save, b + brk, sf);
95 b[brk + sf - 1] = b[brk - 1];
96 memcpy(b + brk - 1, suffix, sf);
34df8aba » Johannes Sixt
2009-03-10 recv_sideband: Bands #2 and #3 always go to stderr
97 fprintf(stderr, "%.*s", brk + sf, b);
6b9c42b4 » Nicolas Pitre
2008-09-03 improve handling of sideband message display
98 memcpy(b + brk, save, sf);
99 len -= brk;
100 } else {
101 int l = brk ? brk : len;
34df8aba » Johannes Sixt
2009-03-10 recv_sideband: Bands #2 and #3 always go to stderr
102 fprintf(stderr, "%.*s", l, b);
6b9c42b4 » Nicolas Pitre
2008-09-03 improve handling of sideband message display
103 len -= l;
104 }
ebe8fa73 » Nicolas Pitre
2007-11-04 fix display overlap between remote and local progress
105
6b9c42b4 » Nicolas Pitre
2008-09-03 improve handling of sideband message display
106 skip_pf = !brk;
107 memmove(buf + pf+1, b + brk, len);
108 } while (len);
49a52b1d » Junio C Hamano
2006-09-10 Move sideband client side support into reusable form.
109 continue;
110 case 1:
ebe8fa73 » Nicolas Pitre
2007-11-04 fix display overlap between remote and local progress
111 safe_write(out, buf + pf+1, len);
49a52b1d » Junio C Hamano
2006-09-10 Move sideband client side support into reusable form.
112 continue;
113 default:
34df8aba » Johannes Sixt
2009-03-10 recv_sideband: Bands #2 and #3 always go to stderr
114 fprintf(stderr, "%s: protocol error: bad band #%d\n",
115 me, band);
49a52b1d » Junio C Hamano
2006-09-10 Move sideband client side support into reusable form.
116 return SIDEBAND_PROTOCOL_ERROR;
117 }
118 }
119 return 0;
120 }
958c24b1 » Junio C Hamano
2006-09-10 Move sideband server side support into reusable form.
121
122 /*
123 * fd is connected to the remote side; send the sideband data
124 * over multiplexed packet stream.
125 */
126 ssize_t send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
127 {
128 ssize_t ssz = sz;
129 const char *p = data;
130
131 while (sz) {
132 unsigned n;
133 char hdr[5];
134
135 n = sz;
136 if (packet_max - 5 < n)
137 n = packet_max - 5;
de1a2fdd » spearce
2009-10-30 Smart push over HTTP: client side
138 if (0 <= band) {
139 sprintf(hdr, "%04x", n + 5);
140 hdr[4] = band;
141 safe_write(fd, hdr, 5);
142 } else {
143 sprintf(hdr, "%04x", n + 4);
144 safe_write(fd, hdr, 4);
145 }
958c24b1 » Junio C Hamano
2006-09-10 Move sideband server side support into reusable form.
146 safe_write(fd, p, n);
147 p += n;
148 sz -= n;
149 }
150 return ssz;
151 }
Something went wrong with that request. Please try again.