]> git.ipfire.org Git - thirdparty/git.git/blame - quote.c
quote: turn 'nodq' parameter into a set of flags
[thirdparty/git.git] / quote.c
CommitLineData
6fb737be
JH
1#include "cache.h"
2#include "quote.h"
dbbcd44f 3#include "strvec.h"
6fb737be 4
5b8e6f85
DP
5int quote_path_fully = 1;
6
44cd91ea
CC
7static inline int need_bs_quote(char c)
8{
9 return (c == '\'' || c == '!');
10}
11
6fb737be 12/* Help to copy the thing properly quoted for the shell safety.
77d604c3
PA
13 * any single quote is replaced with '\'', any exclamation point
14 * is replaced with '\!', and the whole thing is enclosed in a
ca9da0d8 15 * single quote pair.
6fb737be
JH
16 *
17 * E.g.
18 * original sq_quote result
19 * name ==> name ==> 'name'
20 * a b ==> a b ==> 'a b'
21 * a'b ==> a'\''b ==> 'a'\''b'
77d604c3 22 * a!b ==> a'\!'b ==> 'a'\!'b'
6fb737be 23 */
7a33bcbe 24void sq_quote_buf(struct strbuf *dst, const char *src)
77d604c3 25{
7a33bcbe
PH
26 char *to_free = NULL;
27
28 if (dst->buf == src)
b315c5c0 29 to_free = strbuf_detach(dst, NULL);
7a33bcbe
PH
30
31 strbuf_addch(dst, '\'');
32 while (*src) {
c2015b3a 33 size_t len = strcspn(src, "'!");
7a33bcbe
PH
34 strbuf_add(dst, src, len);
35 src += len;
36 while (need_bs_quote(*src)) {
37 strbuf_addstr(dst, "'\\");
38 strbuf_addch(dst, *src++);
39 strbuf_addch(dst, '\'');
6fb737be
JH
40 }
41 }
7a33bcbe
PH
42 strbuf_addch(dst, '\'');
43 free(to_free);
77d604c3
PA
44}
45
1fbdab21
JK
46void sq_quote_buf_pretty(struct strbuf *dst, const char *src)
47{
48 static const char ok_punct[] = "+,-./:=@_^";
49 const char *p;
50
ce2d7ed2
GS
51 /* Avoid losing a zero-length string by adding '' */
52 if (!*src) {
53 strbuf_addstr(dst, "''");
54 return;
55 }
56
1fbdab21 57 for (p = src; *p; p++) {
2b3c430b 58 if (!isalnum(*p) && !strchr(ok_punct, *p)) {
1fbdab21
JK
59 sq_quote_buf(dst, src);
60 return;
61 }
62 }
63
64 /* if we get here, we did not need quoting */
65 strbuf_addstr(dst, src);
66}
67
e70986d7
JK
68void sq_quotef(struct strbuf *dst, const char *fmt, ...)
69{
70 struct strbuf src = STRBUF_INIT;
71
72 va_list ap;
73 va_start(ap, fmt);
74 strbuf_vaddf(&src, fmt, ap);
75 va_end(ap);
76
77 sq_quote_buf(dst, src.buf);
78 strbuf_release(&src);
79}
80
e35f11c2 81void sq_quote_argv(struct strbuf *dst, const char **argv)
7cf67205 82{
7cf67205 83 int i;
7cf67205 84
7cf67205 85 /* Copy into destination buffer. */
b319ce4c
CC
86 strbuf_grow(dst, 255);
87 for (i = 0; argv[i]; ++i) {
7a33bcbe
PH
88 strbuf_addch(dst, ' ');
89 sq_quote_buf(dst, argv[i]);
7cf67205 90 }
86257aa3
CC
91}
92
c2b890ac
JH
93/*
94 * Legacy function to append each argv value, quoted as necessasry,
95 * with whitespace before each value. This results in a leading
96 * space in the result.
97 */
1fbdab21 98void sq_quote_argv_pretty(struct strbuf *dst, const char **argv)
c2b890ac
JH
99{
100 if (argv[0])
101 strbuf_addch(dst, ' ');
102 sq_append_quote_argv_pretty(dst, argv);
103}
104
105/*
106 * Append each argv value, quoted as necessary, with whitespace between them.
107 */
108void sq_append_quote_argv_pretty(struct strbuf *dst, const char **argv)
1fbdab21
JK
109{
110 int i;
111
112 for (i = 0; argv[i]; i++) {
c2b890ac
JH
113 if (i > 0)
114 strbuf_addch(dst, ' ');
1fbdab21
JK
115 sq_quote_buf_pretty(dst, argv[i]);
116 }
117}
118
758e915b 119static char *sq_dequote_step(char *arg, char **next)
35eb2d36
LT
120{
121 char *dst = arg;
122 char *src = arg;
123 char c;
124
125 if (*src != '\'')
126 return NULL;
127 for (;;) {
128 c = *++src;
129 if (!c)
130 return NULL;
131 if (c != '\'') {
132 *dst++ = c;
133 continue;
134 }
135 /* We stepped out of sq */
136 switch (*++src) {
137 case '\0':
138 *dst = 0;
ebbc088e
CC
139 if (next)
140 *next = NULL;
35eb2d36
LT
141 return arg;
142 case '\\':
ddbbf8eb
JK
143 /*
144 * Allow backslashed characters outside of
145 * single-quotes only if they need escaping,
146 * and only if we resume the single-quoted part
147 * afterward.
148 */
149 if (need_bs_quote(src[1]) && src[2] == '\'') {
150 *dst++ = src[1];
151 src += 2;
35eb2d36
LT
152 continue;
153 }
154 /* Fallthrough */
155 default:
ebbc088e
CC
156 if (!next || !isspace(*src))
157 return NULL;
158 do {
159 c = *++src;
160 } while (isspace(c));
161 *dst = 0;
162 *next = src;
163 return arg;
35eb2d36
LT
164 }
165 }
166}
167
ebbc088e
CC
168char *sq_dequote(char *arg)
169{
170 return sq_dequote_step(arg, NULL);
171}
172
37e8161a
JK
173static int sq_dequote_to_argv_internal(char *arg,
174 const char ***argv, int *nr, int *alloc,
c972bf4c 175 struct strvec *array)
eaa759b9
CC
176{
177 char *next = arg;
178
179 if (!*arg)
180 return 0;
181 do {
182 char *dequoted = sq_dequote_step(next, &next);
183 if (!dequoted)
184 return -1;
37e8161a
JK
185 if (argv) {
186 ALLOC_GROW(*argv, *nr + 1, *alloc);
187 (*argv)[(*nr)++] = dequoted;
188 }
189 if (array)
c972bf4c 190 strvec_push(array, dequoted);
eaa759b9
CC
191 } while (next);
192
193 return 0;
194}
195
37e8161a
JK
196int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
197{
198 return sq_dequote_to_argv_internal(arg, argv, nr, alloc, NULL);
199}
200
2745b6b4 201int sq_dequote_to_strvec(char *arg, struct strvec *array)
37e8161a
JK
202{
203 return sq_dequote_to_argv_internal(arg, NULL, NULL, NULL, array);
204}
205
663af342
PH
206/* 1 means: quote as octal
207 * 0 means: quote as octal if (quote_path_fully)
208 * -1 means: never quote
209 * c: quote as "\\c"
210 */
211#define X8(x) x, x, x, x, x, x, x, x
212#define X16(x) X8(x), X8(x)
dfc7f65c 213static signed char const cq_lookup[256] = {
663af342
PH
214 /* 0 1 2 3 4 5 6 7 */
215 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
216 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
217 /* 0x10 */ X16(1),
218 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
219 /* 0x28 */ X16(-1), X16(-1), X16(-1),
220 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
221 /* 0x60 */ X16(-1), X8(-1),
222 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
223 /* 0x80 */ /* set to 0 */
224};
225
dfc7f65c 226static inline int cq_must_quote(char c)
f3fa1838 227{
dfc7f65c 228 return cq_lookup[(unsigned char)c] + quote_path_fully > 0;
663af342
PH
229}
230
231/* returns the longest prefix not needing a quote up to maxlen if positive.
232 This stops at the first \0 because it's marked as a character needing an
233 escape */
234static size_t next_quote_pos(const char *s, ssize_t maxlen)
235{
236 size_t len;
237 if (maxlen < 0) {
dfc7f65c 238 for (len = 0; !cq_must_quote(s[len]); len++);
663af342 239 } else {
dfc7f65c 240 for (len = 0; len < maxlen && !cq_must_quote(s[len]); len++);
663af342
PH
241 }
242 return len;
243}
244
4f6fbcdc
JH
245/*
246 * C-style name quoting.
247 *
663af342
PH
248 * (1) if sb and fp are both NULL, inspect the input name and counts the
249 * number of bytes that are needed to hold c_style quoted version of name,
250 * counting the double quotes around it but not terminating NUL, and
251 * returns it.
252 * However, if name does not need c_style quoting, it returns 0.
4f6fbcdc 253 *
663af342
PH
254 * (2) if sb or fp are not NULL, it emits the c_style quoted version
255 * of name, enclosed with double quotes if asked and needed only.
256 * Return value is the same as in (1).
4f6fbcdc 257 */
663af342 258static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
7c37c975 259 struct strbuf *sb, FILE *fp, unsigned flags)
4f6fbcdc
JH
260{
261#undef EMIT
663af342
PH
262#define EMIT(c) \
263 do { \
264 if (sb) strbuf_addch(sb, (c)); \
265 if (fp) fputc((c), fp); \
266 count++; \
267 } while (0)
268#define EMITBUF(s, l) \
269 do { \
270 if (sb) strbuf_add(sb, (s), (l)); \
271 if (fp) fwrite((s), (l), 1, fp); \
272 count += (l); \
273 } while (0)
274
7c37c975 275 int no_dq = !!(flags & CQUOTE_NODQ);
663af342
PH
276 size_t len, count = 0;
277 const char *p = name;
4f6fbcdc 278
663af342
PH
279 for (;;) {
280 int ch;
4f6fbcdc 281
663af342 282 len = next_quote_pos(p, maxlen);
84249819 283 if (len == maxlen || (maxlen < 0 && !p[len]))
50e7b067 284 break;
663af342
PH
285
286 if (!no_dq && p == name)
287 EMIT('"');
288
289 EMITBUF(p, len);
290 EMIT('\\');
291 p += len;
292 ch = (unsigned char)*p++;
84249819
JK
293 if (maxlen >= 0)
294 maxlen -= len + 1;
dfc7f65c
JH
295 if (cq_lookup[ch] >= ' ') {
296 EMIT(cq_lookup[ch]);
663af342
PH
297 } else {
298 EMIT(((ch >> 6) & 03) + '0');
299 EMIT(((ch >> 3) & 07) + '0');
300 EMIT(((ch >> 0) & 07) + '0');
4f6fbcdc 301 }
4f6fbcdc 302 }
663af342
PH
303
304 EMITBUF(p, len);
305 if (p == name) /* no ending quote needed */
306 return 0;
307
4f6fbcdc
JH
308 if (!no_dq)
309 EMIT('"');
663af342
PH
310 return count;
311}
4f6fbcdc 312
7c37c975 313size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, unsigned flags)
663af342 314{
7c37c975 315 return quote_c_style_counted(name, -1, sb, fp, flags);
4f6fbcdc
JH
316}
317
7c37c975
JH
318void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path,
319 unsigned flags)
d5625091 320{
7c37c975 321 int nodq = !!(flags & CQUOTE_NODQ);
d5625091
JH
322 if (quote_c_style(prefix, NULL, NULL, 0) ||
323 quote_c_style(path, NULL, NULL, 0)) {
324 if (!nodq)
325 strbuf_addch(sb, '"');
7c37c975
JH
326 quote_c_style(prefix, sb, NULL, CQUOTE_NODQ);
327 quote_c_style(path, sb, NULL, CQUOTE_NODQ);
d5625091
JH
328 if (!nodq)
329 strbuf_addch(sb, '"');
330 } else {
331 strbuf_addstr(sb, prefix);
332 strbuf_addstr(sb, path);
333 }
334}
335
663af342 336void write_name_quoted(const char *name, FILE *fp, int terminator)
9ef2b3cb 337{
663af342
PH
338 if (terminator) {
339 quote_c_style(name, NULL, fp, 0);
340 } else {
341 fputs(name, fp);
342 }
343 fputc(terminator, fp);
344}
345
e9a820ce 346void write_name_quoted_relative(const char *name, const char *prefix,
b167cffb 347 FILE *fp, int terminator)
a734d0b1 348{
b167cffb
CB
349 struct strbuf sb = STRBUF_INIT;
350
ad66df2d 351 name = relative_path(name, prefix, &sb);
b167cffb
CB
352 write_name_quoted(name, fp, terminator);
353
354 strbuf_release(&sb);
355}
356
b167cffb 357/* quote path as relative to the given prefix */
88910c99 358char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigned flags)
b167cffb
CB
359{
360 struct strbuf sb = STRBUF_INIT;
ad66df2d 361 const char *rel = relative_path(in, prefix, &sb);
e2773aa4
JH
362 int force_dq = ((flags & QUOTE_PATH_QUOTE_SP) && strchr(rel, ' '));
363
b167cffb 364 strbuf_reset(out);
a734d0b1 365
e2773aa4
JH
366 /*
367 * If the caller wants us to enclose the output in a dq-pair
368 * whether quote_c_style_counted() needs to, we do it ourselves
369 * and tell quote_c_style_counted() not to.
370 */
371 if (force_dq)
f3fc4a1b 372 strbuf_addch(out, '"');
7c37c975
JH
373 quote_c_style_counted(rel, strlen(rel), out, NULL,
374 force_dq ? CQUOTE_NODQ : 0);
e2773aa4
JH
375 if (force_dq)
376 strbuf_addch(out, '"');
377 strbuf_release(&sb);
f3fc4a1b 378
a734d0b1
DP
379 return out->buf;
380}
381
4f6fbcdc
JH
382/*
383 * C-style name unquoting.
384 *
7fb1011e
PH
385 * Quoted should point at the opening double quote.
386 * + Returns 0 if it was able to unquote the string properly, and appends the
387 * result in the strbuf `sb'.
388 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
389 * that this function will allocate memory in the strbuf, so calling
390 * strbuf_release is mandatory whichever result unquote_c_style returns.
391 *
392 * Updates endp pointer to point at one past the ending double quote if given.
4f6fbcdc 393 */
7fb1011e 394int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
4f6fbcdc 395{
7fb1011e
PH
396 size_t oldlen = sb->len, len;
397 int ch, ac;
4f6fbcdc
JH
398
399 if (*quoted++ != '"')
7fb1011e
PH
400 return -1;
401
402 for (;;) {
403 len = strcspn(quoted, "\"\\");
404 strbuf_add(sb, quoted, len);
405 quoted += len;
4f6fbcdc 406
7fb1011e
PH
407 switch (*quoted++) {
408 case '"':
409 if (endp)
c8744d6a 410 *endp = quoted;
7fb1011e
PH
411 return 0;
412 case '\\':
413 break;
414 default:
415 goto error;
416 }
417
418 switch ((ch = *quoted++)) {
419 case 'a': ch = '\a'; break;
420 case 'b': ch = '\b'; break;
421 case 'f': ch = '\f'; break;
422 case 'n': ch = '\n'; break;
423 case 'r': ch = '\r'; break;
424 case 't': ch = '\t'; break;
425 case 'v': ch = '\v'; break;
426
427 case '\\': case '"':
428 break; /* verbatim */
429
430 /* octal values with first digit over 4 overflow */
431 case '0': case '1': case '2': case '3':
4f6fbcdc 432 ac = ((ch - '0') << 6);
7fb1011e
PH
433 if ((ch = *quoted++) < '0' || '7' < ch)
434 goto error;
4f6fbcdc 435 ac |= ((ch - '0') << 3);
7fb1011e
PH
436 if ((ch = *quoted++) < '0' || '7' < ch)
437 goto error;
4f6fbcdc
JH
438 ac |= (ch - '0');
439 ch = ac;
440 break;
441 default:
7fb1011e 442 goto error;
4f6fbcdc 443 }
7fb1011e 444 strbuf_addch(sb, ch);
4f6fbcdc
JH
445 }
446
7fb1011e
PH
447 error:
448 strbuf_setlen(sb, oldlen);
449 return -1;
4f6fbcdc
JH
450}
451
9f613ddd
JH
452/* quoting as a string literal for other languages */
453
10d0167f 454void perl_quote_buf(struct strbuf *sb, const char *src)
9f613ddd
JH
455{
456 const char sq = '\'';
457 const char bq = '\\';
458 char c;
459
10d0167f 460 strbuf_addch(sb, sq);
9f613ddd
JH
461 while ((c = *src++)) {
462 if (c == sq || c == bq)
10d0167f
NTND
463 strbuf_addch(sb, bq);
464 strbuf_addch(sb, c);
9f613ddd 465 }
10d0167f 466 strbuf_addch(sb, sq);
9f613ddd
JH
467}
468
10d0167f 469void python_quote_buf(struct strbuf *sb, const char *src)
9f613ddd
JH
470{
471 const char sq = '\'';
472 const char bq = '\\';
473 const char nl = '\n';
474 char c;
475
10d0167f 476 strbuf_addch(sb, sq);
9f613ddd
JH
477 while ((c = *src++)) {
478 if (c == nl) {
10d0167f
NTND
479 strbuf_addch(sb, bq);
480 strbuf_addch(sb, 'n');
9f613ddd
JH
481 continue;
482 }
483 if (c == sq || c == bq)
10d0167f
NTND
484 strbuf_addch(sb, bq);
485 strbuf_addch(sb, c);
9f613ddd 486 }
10d0167f 487 strbuf_addch(sb, sq);
9f613ddd 488}
5558e55c 489
10d0167f 490void tcl_quote_buf(struct strbuf *sb, const char *src)
5558e55c
SP
491{
492 char c;
493
10d0167f 494 strbuf_addch(sb, '"');
5558e55c
SP
495 while ((c = *src++)) {
496 switch (c) {
497 case '[': case ']':
498 case '{': case '}':
499 case '$': case '\\': case '"':
10d0167f 500 strbuf_addch(sb, '\\');
1cf01a34 501 /* fallthrough */
5558e55c 502 default:
10d0167f 503 strbuf_addch(sb, c);
5558e55c
SP
504 break;
505 case '\f':
10d0167f 506 strbuf_addstr(sb, "\\f");
5558e55c
SP
507 break;
508 case '\r':
10d0167f 509 strbuf_addstr(sb, "\\r");
5558e55c
SP
510 break;
511 case '\n':
10d0167f 512 strbuf_addstr(sb, "\\n");
5558e55c
SP
513 break;
514 case '\t':
10d0167f 515 strbuf_addstr(sb, "\\t");
5558e55c
SP
516 break;
517 case '\v':
10d0167f 518 strbuf_addstr(sb, "\\v");
5558e55c
SP
519 break;
520 }
521 }
10d0167f 522 strbuf_addch(sb, '"');
5558e55c 523}
793dc676
NTND
524
525void basic_regex_quote_buf(struct strbuf *sb, const char *src)
526{
527 char c;
528
529 if (*src == '^') {
530 /* only beginning '^' is special and needs quoting */
531 strbuf_addch(sb, '\\');
532 strbuf_addch(sb, *src++);
533 }
534 if (*src == '*')
535 /* beginning '*' is not special, no quoting */
536 strbuf_addch(sb, *src++);
537
538 while ((c = *src++)) {
539 switch (c) {
540 case '[':
541 case '.':
542 case '\\':
543 case '*':
544 strbuf_addch(sb, '\\');
545 strbuf_addch(sb, c);
546 break;
547
548 case '$':
549 /* only the end '$' is special and needs quoting */
550 if (*src == '\0')
551 strbuf_addch(sb, '\\');
552 strbuf_addch(sb, c);
553 break;
554
555 default:
556 strbuf_addch(sb, c);
557 break;
558 }
559 }
560}