run-command: provide in_async query function
[git/git.git] / pkt-line.c
CommitLineData
f3a3214e
LT
1#include "cache.h"
2#include "pkt-line.h"
3
74543a04 4char packet_buffer[LARGE_PACKET_MAX];
1e4cd68c 5static const char *packet_trace_prefix = "git";
6aa30857 6static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
32359838 7static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);
bbc30f99
JK
8
9void packet_trace_identity(const char *prog)
10{
11 packet_trace_prefix = xstrdup(prog);
12}
13
32359838
JK
14static int packet_trace_pack(const char *buf, unsigned int len, int sideband)
15{
16 if (!sideband) {
17 trace_verbatim(&trace_pack, buf, len);
18 return 1;
19 } else if (len && *buf == '\1') {
20 trace_verbatim(&trace_pack, buf + 1, len - 1);
21 return 1;
22 } else {
23 /* it's another non-pack sideband */
24 return 0;
25 }
26}
27
bbc30f99
JK
28static void packet_trace(const char *buf, unsigned int len, int write)
29{
30 int i;
31 struct strbuf out;
32359838
JK
32 static int in_pack, sideband;
33
34 if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
35 return;
36
37 if (in_pack) {
38 if (packet_trace_pack(buf, len, sideband))
39 return;
40 } else if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) {
41 in_pack = 1;
42 sideband = *buf == '\1';
43 packet_trace_pack(buf, len, sideband);
44
45 /*
46 * Make a note in the human-readable trace that the pack data
47 * started.
48 */
49 buf = "PACK ...";
50 len = strlen(buf);
51 }
bbc30f99 52
6aa30857 53 if (!trace_want(&trace_packet))
bbc30f99
JK
54 return;
55
56 /* +32 is just a guess for header + quoting */
57 strbuf_init(&out, len+32);
58
59 strbuf_addf(&out, "packet: %12s%c ",
60 packet_trace_prefix, write ? '>' : '<');
61
32359838
JK
62 /* XXX we should really handle printable utf8 */
63 for (i = 0; i < len; i++) {
64 /* suppress newlines */
65 if (buf[i] == '\n')
66 continue;
67 if (buf[i] >= 0x20 && buf[i] <= 0x7e)
68 strbuf_addch(&out, buf[i]);
69 else
70 strbuf_addf(&out, "\\%o", buf[i]);
bbc30f99
JK
71 }
72
73 strbuf_addch(&out, '\n');
6aa30857 74 trace_strbuf(&trace_packet, &out);
bbc30f99
JK
75 strbuf_release(&out);
76}
77
f3a3214e
LT
78/*
79 * If we buffered things up above (we don't, but we should),
80 * we'd flush it here
81 */
82void packet_flush(int fd)
83{
bbc30f99 84 packet_trace("0000", 4, 1);
cdf4fb8e 85 write_or_die(fd, "0000", 4);
f3a3214e
LT
86}
87
f5615d24
SP
88void packet_buf_flush(struct strbuf *buf)
89{
bbc30f99 90 packet_trace("0000", 4, 1);
f5615d24
SP
91 strbuf_add(buf, "0000", 4);
92}
93
f3a3214e 94#define hex(a) (hexchar[(a) & 15])
8e9faf27 95static void format_packet(struct strbuf *out, const char *fmt, va_list args)
f3a3214e 96{
f3a3214e 97 static char hexchar[] = "0123456789abcdef";
8e9faf27 98 size_t orig_len, n;
f3a3214e 99
8e9faf27
JK
100 orig_len = out->len;
101 strbuf_addstr(out, "0000");
102 strbuf_vaddf(out, fmt, args);
103 n = out->len - orig_len;
104
105 if (n > LARGE_PACKET_MAX)
f3a3214e 106 die("protocol error: impossibly long line");
8e9faf27
JK
107
108 out->buf[orig_len + 0] = hex(n >> 12);
109 out->buf[orig_len + 1] = hex(n >> 8);
110 out->buf[orig_len + 2] = hex(n >> 4);
111 out->buf[orig_len + 3] = hex(n);
112 packet_trace(out->buf + orig_len + 4, n - 4, 1);
f5615d24
SP
113}
114
115void packet_write(int fd, const char *fmt, ...)
116{
8e9faf27 117 static struct strbuf buf = STRBUF_INIT;
f5615d24 118 va_list args;
f5615d24 119
8e9faf27 120 strbuf_reset(&buf);
f5615d24 121 va_start(args, fmt);
8e9faf27 122 format_packet(&buf, fmt, args);
f5615d24 123 va_end(args);
8e9faf27 124 write_or_die(fd, buf.buf, buf.len);
f3a3214e
LT
125}
126
f5615d24
SP
127void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
128{
129 va_list args;
f5615d24
SP
130
131 va_start(args, fmt);
8e9faf27 132 format_packet(buf, fmt, args);
f5615d24 133 va_end(args);
f5615d24
SP
134}
135
4981fe75
JK
136static int get_packet_data(int fd, char **src_buf, size_t *src_size,
137 void *dst, unsigned size, int options)
f3a3214e 138{
4981fe75
JK
139 ssize_t ret;
140
141 if (fd >= 0 && src_buf && *src_buf)
142 die("BUG: multiple sources given to packet_read");
143
144 /* Read up to "size" bytes from our source, whatever it is. */
145 if (src_buf && *src_buf) {
146 ret = size < *src_size ? size : *src_size;
147 memcpy(dst, *src_buf, ret);
148 *src_buf += ret;
149 *src_size -= ret;
150 } else {
151 ret = read_in_full(fd, dst, size);
152 if (ret < 0)
153 die_errno("read error");
154 }
155
156 /* And complain if we didn't get enough bytes to satisfy the read. */
157 if (ret < size) {
03809429 158 if (options & PACKET_READ_GENTLE_ON_EOF)
46284dd1
HV
159 return -1;
160
c697ad14 161 die("The remote end hung up unexpectedly");
46284dd1
HV
162 }
163
164 return ret;
f3a3214e
LT
165}
166
f5615d24 167static int packet_length(const char *linelen)
f3a3214e
LT
168{
169 int n;
f5615d24 170 int len = 0;
f3a3214e 171
f3a3214e
LT
172 for (n = 0; n < 4; n++) {
173 unsigned char c = linelen[n];
174 len <<= 4;
175 if (c >= '0' && c <= '9') {
176 len += c - '0';
177 continue;
178 }
179 if (c >= 'a' && c <= 'f') {
180 len += c - 'a' + 10;
181 continue;
182 }
183 if (c >= 'A' && c <= 'F') {
184 len += c - 'A' + 10;
185 continue;
186 }
f5615d24 187 return -1;
f3a3214e 188 }
f5615d24
SP
189 return len;
190}
191
4981fe75
JK
192int packet_read(int fd, char **src_buf, size_t *src_len,
193 char *buffer, unsigned size, int options)
f5615d24 194{
46284dd1 195 int len, ret;
f5615d24
SP
196 char linelen[4];
197
4981fe75 198 ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
03809429 199 if (ret < 0)
46284dd1 200 return ret;
f5615d24
SP
201 len = packet_length(linelen);
202 if (len < 0)
743c4b7b 203 die("protocol error: bad line length character: %.4s", linelen);
bbc30f99
JK
204 if (!len) {
205 packet_trace("0000", 4, 0);
f3a3214e 206 return 0;
bbc30f99 207 }
f3a3214e
LT
208 len -= 4;
209 if (len >= size)
210 die("protocol error: bad line length %d", len);
4981fe75 211 ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
03809429 212 if (ret < 0)
46284dd1 213 return ret;
819b929d
JK
214
215 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
216 len && buffer[len-1] == '\n')
217 len--;
218
f3a3214e 219 buffer[len] = 0;
bbc30f99 220 packet_trace(buffer, len, 0);
f3a3214e
LT
221 return len;
222}
f5615d24 223
4981fe75
JK
224static char *packet_read_line_generic(int fd,
225 char **src, size_t *src_len,
226 int *dst_len)
46284dd1 227{
4981fe75
JK
228 int len = packet_read(fd, src, src_len,
229 packet_buffer, sizeof(packet_buffer),
74543a04 230 PACKET_READ_CHOMP_NEWLINE);
4981fe75
JK
231 if (dst_len)
232 *dst_len = len;
74543a04 233 return len ? packet_buffer : NULL;
46284dd1
HV
234}
235
4981fe75 236char *packet_read_line(int fd, int *len_p)
f5615d24 237{
4981fe75
JK
238 return packet_read_line_generic(fd, NULL, NULL, len_p);
239}
f5615d24 240
4981fe75
JK
241char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
242{
243 return packet_read_line_generic(-1, src, src_len, dst_len);
f5615d24 244}