]> git.ipfire.org Git - thirdparty/git.git/blame - quote.c
quote: implement "sq_dequote_many" to unwrap many args in one string
[thirdparty/git.git] / quote.c
CommitLineData
6fb737be
JH
1#include "cache.h"
2#include "quote.h"
3
5b8e6f85
DP
4int quote_path_fully = 1;
5
6fb737be 6/* Help to copy the thing properly quoted for the shell safety.
77d604c3
PA
7 * any single quote is replaced with '\'', any exclamation point
8 * is replaced with '\!', and the whole thing is enclosed in a
6fb737be
JH
9 *
10 * E.g.
11 * original sq_quote result
12 * name ==> name ==> 'name'
13 * a b ==> a b ==> 'a b'
14 * a'b ==> a'\''b ==> 'a'\''b'
77d604c3 15 * a!b ==> a'\!'b ==> 'a'\!'b'
6fb737be 16 */
35eb2d36
LT
17static inline int need_bs_quote(char c)
18{
19 return (c == '\'' || c == '!');
20}
21
7a33bcbe 22void sq_quote_buf(struct strbuf *dst, const char *src)
77d604c3 23{
7a33bcbe
PH
24 char *to_free = NULL;
25
26 if (dst->buf == src)
b315c5c0 27 to_free = strbuf_detach(dst, NULL);
7a33bcbe
PH
28
29 strbuf_addch(dst, '\'');
30 while (*src) {
c2015b3a 31 size_t len = strcspn(src, "'!");
7a33bcbe
PH
32 strbuf_add(dst, src, len);
33 src += len;
34 while (need_bs_quote(*src)) {
35 strbuf_addstr(dst, "'\\");
36 strbuf_addch(dst, *src++);
37 strbuf_addch(dst, '\'');
6fb737be
JH
38 }
39 }
7a33bcbe
PH
40 strbuf_addch(dst, '\'');
41 free(to_free);
77d604c3
PA
42}
43
575ba9d6
ML
44void sq_quote_print(FILE *stream, const char *src)
45{
46 char c;
47
48 fputc('\'', stream);
49 while ((c = *src++)) {
50 if (need_bs_quote(c)) {
51 fputs("'\\", stream);
52 fputc(c, stream);
53 fputc('\'', stream);
54 } else {
55 fputc(c, stream);
56 }
57 }
58 fputc('\'', stream);
59}
60
b319ce4c 61void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
7cf67205 62{
7cf67205 63 int i;
7cf67205 64
7cf67205 65 /* Copy into destination buffer. */
b319ce4c
CC
66 strbuf_grow(dst, 255);
67 for (i = 0; argv[i]; ++i) {
7a33bcbe
PH
68 strbuf_addch(dst, ' ');
69 sq_quote_buf(dst, argv[i]);
70 if (maxlen && dst->len > maxlen)
71 die("Too many or long arguments");
7cf67205 72 }
86257aa3
CC
73}
74
ebbc088e 75char *sq_dequote_step(char *arg, char **next)
35eb2d36
LT
76{
77 char *dst = arg;
78 char *src = arg;
79 char c;
80
81 if (*src != '\'')
82 return NULL;
83 for (;;) {
84 c = *++src;
85 if (!c)
86 return NULL;
87 if (c != '\'') {
88 *dst++ = c;
89 continue;
90 }
91 /* We stepped out of sq */
92 switch (*++src) {
93 case '\0':
94 *dst = 0;
ebbc088e
CC
95 if (next)
96 *next = NULL;
35eb2d36
LT
97 return arg;
98 case '\\':
99 c = *++src;
100 if (need_bs_quote(c) && *++src == '\'') {
101 *dst++ = c;
102 continue;
103 }
104 /* Fallthrough */
105 default:
ebbc088e
CC
106 if (!next || !isspace(*src))
107 return NULL;
108 do {
109 c = *++src;
110 } while (isspace(c));
111 *dst = 0;
112 *next = src;
113 return arg;
35eb2d36
LT
114 }
115 }
116}
117
ebbc088e
CC
118char *sq_dequote(char *arg)
119{
120 return sq_dequote_step(arg, NULL);
121}
122
663af342
PH
123/* 1 means: quote as octal
124 * 0 means: quote as octal if (quote_path_fully)
125 * -1 means: never quote
126 * c: quote as "\\c"
127 */
128#define X8(x) x, x, x, x, x, x, x, x
129#define X16(x) X8(x), X8(x)
130static signed char const sq_lookup[256] = {
131 /* 0 1 2 3 4 5 6 7 */
132 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
133 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
134 /* 0x10 */ X16(1),
135 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
136 /* 0x28 */ X16(-1), X16(-1), X16(-1),
137 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
138 /* 0x60 */ X16(-1), X8(-1),
139 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
140 /* 0x80 */ /* set to 0 */
141};
142
f3fa1838
JH
143static inline int sq_must_quote(char c)
144{
663af342
PH
145 return sq_lookup[(unsigned char)c] + quote_path_fully > 0;
146}
147
148/* returns the longest prefix not needing a quote up to maxlen if positive.
149 This stops at the first \0 because it's marked as a character needing an
150 escape */
151static size_t next_quote_pos(const char *s, ssize_t maxlen)
152{
153 size_t len;
154 if (maxlen < 0) {
155 for (len = 0; !sq_must_quote(s[len]); len++);
156 } else {
157 for (len = 0; len < maxlen && !sq_must_quote(s[len]); len++);
158 }
159 return len;
160}
161
4f6fbcdc
JH
162/*
163 * C-style name quoting.
164 *
663af342
PH
165 * (1) if sb and fp are both NULL, inspect the input name and counts the
166 * number of bytes that are needed to hold c_style quoted version of name,
167 * counting the double quotes around it but not terminating NUL, and
168 * returns it.
169 * However, if name does not need c_style quoting, it returns 0.
4f6fbcdc 170 *
663af342
PH
171 * (2) if sb or fp are not NULL, it emits the c_style quoted version
172 * of name, enclosed with double quotes if asked and needed only.
173 * Return value is the same as in (1).
4f6fbcdc 174 */
663af342
PH
175static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
176 struct strbuf *sb, FILE *fp, int no_dq)
4f6fbcdc
JH
177{
178#undef EMIT
663af342
PH
179#define EMIT(c) \
180 do { \
181 if (sb) strbuf_addch(sb, (c)); \
182 if (fp) fputc((c), fp); \
183 count++; \
184 } while (0)
185#define EMITBUF(s, l) \
186 do { \
187 if (sb) strbuf_add(sb, (s), (l)); \
188 if (fp) fwrite((s), (l), 1, fp); \
189 count += (l); \
190 } while (0)
191
192 size_t len, count = 0;
193 const char *p = name;
4f6fbcdc 194
663af342
PH
195 for (;;) {
196 int ch;
4f6fbcdc 197
663af342
PH
198 len = next_quote_pos(p, maxlen);
199 if (len == maxlen || !p[len])
50e7b067 200 break;
663af342
PH
201
202 if (!no_dq && p == name)
203 EMIT('"');
204
205 EMITBUF(p, len);
206 EMIT('\\');
207 p += len;
208 ch = (unsigned char)*p++;
209 if (sq_lookup[ch] >= ' ') {
210 EMIT(sq_lookup[ch]);
211 } else {
212 EMIT(((ch >> 6) & 03) + '0');
213 EMIT(((ch >> 3) & 07) + '0');
214 EMIT(((ch >> 0) & 07) + '0');
4f6fbcdc 215 }
4f6fbcdc 216 }
663af342
PH
217
218 EMITBUF(p, len);
219 if (p == name) /* no ending quote needed */
220 return 0;
221
4f6fbcdc
JH
222 if (!no_dq)
223 EMIT('"');
663af342
PH
224 return count;
225}
4f6fbcdc 226
663af342
PH
227size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
228{
229 return quote_c_style_counted(name, -1, sb, fp, nodq);
4f6fbcdc
JH
230}
231
d5625091
JH
232void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, int nodq)
233{
234 if (quote_c_style(prefix, NULL, NULL, 0) ||
235 quote_c_style(path, NULL, NULL, 0)) {
236 if (!nodq)
237 strbuf_addch(sb, '"');
238 quote_c_style(prefix, sb, NULL, 1);
239 quote_c_style(path, sb, NULL, 1);
240 if (!nodq)
241 strbuf_addch(sb, '"');
242 } else {
243 strbuf_addstr(sb, prefix);
244 strbuf_addstr(sb, path);
245 }
246}
247
663af342 248void write_name_quoted(const char *name, FILE *fp, int terminator)
9ef2b3cb 249{
663af342
PH
250 if (terminator) {
251 quote_c_style(name, NULL, fp, 0);
252 } else {
253 fputs(name, fp);
254 }
255 fputc(terminator, fp);
256}
257
258extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
259 const char *name, FILE *fp, int terminator)
260{
261 int needquote = 0;
262
263 if (terminator) {
264 needquote = next_quote_pos(pfx, pfxlen) < pfxlen
265 || name[next_quote_pos(name, -1)];
266 }
267 if (needquote) {
268 fputc('"', fp);
269 quote_c_style_counted(pfx, pfxlen, NULL, fp, 1);
270 quote_c_style(name, NULL, fp, 1);
271 fputc('"', fp);
272 } else {
273 fwrite(pfx, pfxlen, 1, fp);
274 fputs(name, fp);
275 }
276 fputc(terminator, fp);
9ef2b3cb
JH
277}
278
a734d0b1
DP
279/* quote path as relative to the given prefix */
280char *quote_path_relative(const char *in, int len,
281 struct strbuf *out, const char *prefix)
282{
283 int needquote;
284
285 if (len < 0)
286 len = strlen(in);
287
288 /* "../" prefix itself does not need quoting, but "in" might. */
289 needquote = next_quote_pos(in, len) < len;
290 strbuf_setlen(out, 0);
291 strbuf_grow(out, len);
292
293 if (needquote)
294 strbuf_addch(out, '"');
295 if (prefix) {
296 int off = 0;
297 while (prefix[off] && off < len && prefix[off] == in[off])
298 if (prefix[off] == '/') {
299 prefix += off + 1;
300 in += off + 1;
301 len -= off + 1;
302 off = 0;
303 } else
304 off++;
305
306 for (; *prefix; prefix++)
307 if (*prefix == '/')
308 strbuf_addstr(out, "../");
309 }
310
311 quote_c_style_counted (in, len, out, NULL, 1);
312
313 if (needquote)
314 strbuf_addch(out, '"');
315 if (!out->len)
316 strbuf_addstr(out, "./");
317
318 return out->buf;
319}
320
4f6fbcdc
JH
321/*
322 * C-style name unquoting.
323 *
7fb1011e
PH
324 * Quoted should point at the opening double quote.
325 * + Returns 0 if it was able to unquote the string properly, and appends the
326 * result in the strbuf `sb'.
327 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
328 * that this function will allocate memory in the strbuf, so calling
329 * strbuf_release is mandatory whichever result unquote_c_style returns.
330 *
331 * Updates endp pointer to point at one past the ending double quote if given.
4f6fbcdc 332 */
7fb1011e 333int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
4f6fbcdc 334{
7fb1011e
PH
335 size_t oldlen = sb->len, len;
336 int ch, ac;
4f6fbcdc
JH
337
338 if (*quoted++ != '"')
7fb1011e
PH
339 return -1;
340
341 for (;;) {
342 len = strcspn(quoted, "\"\\");
343 strbuf_add(sb, quoted, len);
344 quoted += len;
4f6fbcdc 345
7fb1011e
PH
346 switch (*quoted++) {
347 case '"':
348 if (endp)
c8744d6a 349 *endp = quoted;
7fb1011e
PH
350 return 0;
351 case '\\':
352 break;
353 default:
354 goto error;
355 }
356
357 switch ((ch = *quoted++)) {
358 case 'a': ch = '\a'; break;
359 case 'b': ch = '\b'; break;
360 case 'f': ch = '\f'; break;
361 case 'n': ch = '\n'; break;
362 case 'r': ch = '\r'; break;
363 case 't': ch = '\t'; break;
364 case 'v': ch = '\v'; break;
365
366 case '\\': case '"':
367 break; /* verbatim */
368
369 /* octal values with first digit over 4 overflow */
370 case '0': case '1': case '2': case '3':
4f6fbcdc 371 ac = ((ch - '0') << 6);
7fb1011e
PH
372 if ((ch = *quoted++) < '0' || '7' < ch)
373 goto error;
4f6fbcdc 374 ac |= ((ch - '0') << 3);
7fb1011e
PH
375 if ((ch = *quoted++) < '0' || '7' < ch)
376 goto error;
4f6fbcdc
JH
377 ac |= (ch - '0');
378 ch = ac;
379 break;
380 default:
7fb1011e 381 goto error;
4f6fbcdc 382 }
7fb1011e 383 strbuf_addch(sb, ch);
4f6fbcdc
JH
384 }
385
7fb1011e
PH
386 error:
387 strbuf_setlen(sb, oldlen);
388 return -1;
4f6fbcdc
JH
389}
390
9f613ddd
JH
391/* quoting as a string literal for other languages */
392
393void perl_quote_print(FILE *stream, const char *src)
394{
395 const char sq = '\'';
396 const char bq = '\\';
397 char c;
398
399 fputc(sq, stream);
400 while ((c = *src++)) {
401 if (c == sq || c == bq)
402 fputc(bq, stream);
403 fputc(c, stream);
404 }
405 fputc(sq, stream);
406}
407
408void python_quote_print(FILE *stream, const char *src)
409{
410 const char sq = '\'';
411 const char bq = '\\';
412 const char nl = '\n';
413 char c;
414
415 fputc(sq, stream);
416 while ((c = *src++)) {
417 if (c == nl) {
418 fputc(bq, stream);
419 fputc('n', stream);
420 continue;
421 }
422 if (c == sq || c == bq)
423 fputc(bq, stream);
424 fputc(c, stream);
425 }
426 fputc(sq, stream);
427}
5558e55c
SP
428
429void tcl_quote_print(FILE *stream, const char *src)
430{
431 char c;
432
433 fputc('"', stream);
434 while ((c = *src++)) {
435 switch (c) {
436 case '[': case ']':
437 case '{': case '}':
438 case '$': case '\\': case '"':
439 fputc('\\', stream);
440 default:
441 fputc(c, stream);
442 break;
443 case '\f':
444 fputs("\\f", stream);
445 break;
446 case '\r':
447 fputs("\\r", stream);
448 break;
449 case '\n':
450 fputs("\\n", stream);
451 break;
452 case '\t':
453 fputs("\\t", stream);
454 break;
455 case '\v':
456 fputs("\\v", stream);
457 break;
458 }
459 }
460 fputc('"', stream);
461}