i18n: send-email: mark string with interpolation for translation
[git/debian.git] / pkt-line.c
blob30489c60b1d3f8acc002fad31aa68b528757c8da
1 #include "cache.h"
2 #include "pkt-line.h"
3 #include "run-command.h"
5 char packet_buffer[LARGE_PACKET_MAX];
6 static const char *packet_trace_prefix = "git";
7 static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
8 static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);
10 void packet_trace_identity(const char *prog)
12 packet_trace_prefix = xstrdup(prog);
15 static const char *get_trace_prefix(void)
17 return in_async() ? "sideband" : packet_trace_prefix;
20 static int packet_trace_pack(const char *buf, unsigned int len, int sideband)
22 if (!sideband) {
23 trace_verbatim(&trace_pack, buf, len);
24 return 1;
25 } else if (len && *buf == '\1') {
26 trace_verbatim(&trace_pack, buf + 1, len - 1);
27 return 1;
28 } else {
29 /* it's another non-pack sideband */
30 return 0;
34 static void packet_trace(const char *buf, unsigned int len, int write)
36 int i;
37 struct strbuf out;
38 static int in_pack, sideband;
40 if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
41 return;
43 if (in_pack) {
44 if (packet_trace_pack(buf, len, sideband))
45 return;
46 } else if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) {
47 in_pack = 1;
48 sideband = *buf == '\1';
49 packet_trace_pack(buf, len, sideband);
52 * Make a note in the human-readable trace that the pack data
53 * started.
55 buf = "PACK ...";
56 len = strlen(buf);
59 if (!trace_want(&trace_packet))
60 return;
62 /* +32 is just a guess for header + quoting */
63 strbuf_init(&out, len+32);
65 strbuf_addf(&out, "packet: %12s%c ",
66 get_trace_prefix(), write ? '>' : '<');
68 /* XXX we should really handle printable utf8 */
69 for (i = 0; i < len; i++) {
70 /* suppress newlines */
71 if (buf[i] == '\n')
72 continue;
73 if (buf[i] >= 0x20 && buf[i] <= 0x7e)
74 strbuf_addch(&out, buf[i]);
75 else
76 strbuf_addf(&out, "\\%o", buf[i]);
79 strbuf_addch(&out, '\n');
80 trace_strbuf(&trace_packet, &out);
81 strbuf_release(&out);
85 * If we buffered things up above (we don't, but we should),
86 * we'd flush it here
88 void packet_flush(int fd)
90 packet_trace("0000", 4, 1);
91 write_or_die(fd, "0000", 4);
94 void packet_buf_flush(struct strbuf *buf)
96 packet_trace("0000", 4, 1);
97 strbuf_add(buf, "0000", 4);
100 #define hex(a) (hexchar[(a) & 15])
101 static void format_packet(struct strbuf *out, const char *fmt, va_list args)
103 static char hexchar[] = "0123456789abcdef";
104 size_t orig_len, n;
106 orig_len = out->len;
107 strbuf_addstr(out, "0000");
108 strbuf_vaddf(out, fmt, args);
109 n = out->len - orig_len;
111 if (n > LARGE_PACKET_MAX)
112 die("protocol error: impossibly long line");
114 out->buf[orig_len + 0] = hex(n >> 12);
115 out->buf[orig_len + 1] = hex(n >> 8);
116 out->buf[orig_len + 2] = hex(n >> 4);
117 out->buf[orig_len + 3] = hex(n);
118 packet_trace(out->buf + orig_len + 4, n - 4, 1);
121 void packet_write(int fd, const char *fmt, ...)
123 static struct strbuf buf = STRBUF_INIT;
124 va_list args;
126 strbuf_reset(&buf);
127 va_start(args, fmt);
128 format_packet(&buf, fmt, args);
129 va_end(args);
130 write_or_die(fd, buf.buf, buf.len);
133 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
135 va_list args;
137 va_start(args, fmt);
138 format_packet(buf, fmt, args);
139 va_end(args);
142 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
143 void *dst, unsigned size, int options)
145 ssize_t ret;
147 if (fd >= 0 && src_buf && *src_buf)
148 die("BUG: multiple sources given to packet_read");
150 /* Read up to "size" bytes from our source, whatever it is. */
151 if (src_buf && *src_buf) {
152 ret = size < *src_size ? size : *src_size;
153 memcpy(dst, *src_buf, ret);
154 *src_buf += ret;
155 *src_size -= ret;
156 } else {
157 ret = read_in_full(fd, dst, size);
158 if (ret < 0)
159 die_errno("read error");
162 /* And complain if we didn't get enough bytes to satisfy the read. */
163 if (ret < size) {
164 if (options & PACKET_READ_GENTLE_ON_EOF)
165 return -1;
167 die("The remote end hung up unexpectedly");
170 return ret;
173 static int packet_length(const char *linelen)
175 int val = hex2chr(linelen);
176 return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
179 int packet_read(int fd, char **src_buf, size_t *src_len,
180 char *buffer, unsigned size, int options)
182 int len, ret;
183 char linelen[4];
185 ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
186 if (ret < 0)
187 return ret;
188 len = packet_length(linelen);
189 if (len < 0)
190 die("protocol error: bad line length character: %.4s", linelen);
191 if (!len) {
192 packet_trace("0000", 4, 0);
193 return 0;
195 len -= 4;
196 if (len >= size)
197 die("protocol error: bad line length %d", len);
198 ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
199 if (ret < 0)
200 return ret;
202 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
203 len && buffer[len-1] == '\n')
204 len--;
206 buffer[len] = 0;
207 packet_trace(buffer, len, 0);
208 return len;
211 static char *packet_read_line_generic(int fd,
212 char **src, size_t *src_len,
213 int *dst_len)
215 int len = packet_read(fd, src, src_len,
216 packet_buffer, sizeof(packet_buffer),
217 PACKET_READ_CHOMP_NEWLINE);
218 if (dst_len)
219 *dst_len = len;
220 return len ? packet_buffer : NULL;
223 char *packet_read_line(int fd, int *len_p)
225 return packet_read_line_generic(fd, NULL, NULL, len_p);
228 char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
230 return packet_read_line_generic(-1, src, src_len, dst_len);