dir.h: move DTYPE defines from cache.h
[git.git] / quote.c
blob43c739671ed0cb33cd246a98737136b2154c2a57
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "path.h"
4 #include "quote.h"
5 #include "strbuf.h"
6 #include "strvec.h"
8 int quote_path_fully = 1;
10 static inline int need_bs_quote(char c)
12 return (c == '\'' || c == '!');
15 /* Help to copy the thing properly quoted for the shell safety.
16 * any single quote is replaced with '\'', any exclamation point
17 * is replaced with '\!', and the whole thing is enclosed in a
18 * single quote pair.
20 * E.g.
21 * original sq_quote result
22 * name ==> name ==> 'name'
23 * a b ==> a b ==> 'a b'
24 * a'b ==> a'\''b ==> 'a'\''b'
25 * a!b ==> a'\!'b ==> 'a'\!'b'
27 void sq_quote_buf(struct strbuf *dst, const char *src)
29 char *to_free = NULL;
31 if (dst->buf == src)
32 to_free = strbuf_detach(dst, NULL);
34 strbuf_addch(dst, '\'');
35 while (*src) {
36 size_t len = strcspn(src, "'!");
37 strbuf_add(dst, src, len);
38 src += len;
39 while (need_bs_quote(*src)) {
40 strbuf_addstr(dst, "'\\");
41 strbuf_addch(dst, *src++);
42 strbuf_addch(dst, '\'');
45 strbuf_addch(dst, '\'');
46 free(to_free);
49 void sq_quote_buf_pretty(struct strbuf *dst, const char *src)
51 static const char ok_punct[] = "+,-./:=@_^";
52 const char *p;
54 /* Avoid losing a zero-length string by adding '' */
55 if (!*src) {
56 strbuf_addstr(dst, "''");
57 return;
60 for (p = src; *p; p++) {
61 if (!isalnum(*p) && !strchr(ok_punct, *p)) {
62 sq_quote_buf(dst, src);
63 return;
67 /* if we get here, we did not need quoting */
68 strbuf_addstr(dst, src);
71 void sq_quotef(struct strbuf *dst, const char *fmt, ...)
73 struct strbuf src = STRBUF_INIT;
75 va_list ap;
76 va_start(ap, fmt);
77 strbuf_vaddf(&src, fmt, ap);
78 va_end(ap);
80 sq_quote_buf(dst, src.buf);
81 strbuf_release(&src);
84 void sq_quote_argv(struct strbuf *dst, const char **argv)
86 int i;
88 /* Copy into destination buffer. */
89 strbuf_grow(dst, 255);
90 for (i = 0; argv[i]; ++i) {
91 strbuf_addch(dst, ' ');
92 sq_quote_buf(dst, argv[i]);
97 * Legacy function to append each argv value, quoted as necessasry,
98 * with whitespace before each value. This results in a leading
99 * space in the result.
101 void sq_quote_argv_pretty(struct strbuf *dst, const char **argv)
103 if (argv[0])
104 strbuf_addch(dst, ' ');
105 sq_append_quote_argv_pretty(dst, argv);
109 * Append each argv value, quoted as necessary, with whitespace between them.
111 void sq_append_quote_argv_pretty(struct strbuf *dst, const char **argv)
113 int i;
115 for (i = 0; argv[i]; i++) {
116 if (i > 0)
117 strbuf_addch(dst, ' ');
118 sq_quote_buf_pretty(dst, argv[i]);
122 char *sq_dequote_step(char *arg, char **next)
124 char *dst = arg;
125 char *src = arg;
126 char c;
128 if (*src != '\'')
129 return NULL;
130 for (;;) {
131 c = *++src;
132 if (!c)
133 return NULL;
134 if (c != '\'') {
135 *dst++ = c;
136 continue;
138 /* We stepped out of sq */
139 switch (*++src) {
140 case '\0':
141 *dst = 0;
142 if (next)
143 *next = NULL;
144 return arg;
145 case '\\':
147 * Allow backslashed characters outside of
148 * single-quotes only if they need escaping,
149 * and only if we resume the single-quoted part
150 * afterward.
152 if (need_bs_quote(src[1]) && src[2] == '\'') {
153 *dst++ = src[1];
154 src += 2;
155 continue;
157 /* Fallthrough */
158 default:
159 if (!next)
160 return NULL;
161 *dst = 0;
162 *next = src;
163 return arg;
168 char *sq_dequote(char *arg)
170 return sq_dequote_step(arg, NULL);
173 static int sq_dequote_to_argv_internal(char *arg,
174 const char ***argv, int *nr, int *alloc,
175 struct strvec *array)
177 char *next = arg;
179 if (!*arg)
180 return 0;
181 do {
182 char *dequoted = sq_dequote_step(next, &next);
183 if (!dequoted)
184 return -1;
185 if (next) {
186 char c;
187 if (!isspace(*next))
188 return -1;
189 do {
190 c = *++next;
191 } while (isspace(c));
193 if (argv) {
194 ALLOC_GROW(*argv, *nr + 1, *alloc);
195 (*argv)[(*nr)++] = dequoted;
197 if (array)
198 strvec_push(array, dequoted);
199 } while (next);
201 return 0;
204 int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
206 return sq_dequote_to_argv_internal(arg, argv, nr, alloc, NULL);
209 int sq_dequote_to_strvec(char *arg, struct strvec *array)
211 return sq_dequote_to_argv_internal(arg, NULL, NULL, NULL, array);
214 /* 1 means: quote as octal
215 * 0 means: quote as octal if (quote_path_fully)
216 * -1 means: never quote
217 * c: quote as "\\c"
219 #define X8(x) x, x, x, x, x, x, x, x
220 #define X16(x) X8(x), X8(x)
221 static signed char const cq_lookup[256] = {
222 /* 0 1 2 3 4 5 6 7 */
223 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
224 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
225 /* 0x10 */ X16(1),
226 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
227 /* 0x28 */ X16(-1), X16(-1), X16(-1),
228 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
229 /* 0x60 */ X16(-1), X8(-1),
230 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
231 /* 0x80 */ /* set to 0 */
234 static inline int cq_must_quote(char c)
236 return cq_lookup[(unsigned char)c] + quote_path_fully > 0;
239 /* returns the longest prefix not needing a quote up to maxlen if positive.
240 This stops at the first \0 because it's marked as a character needing an
241 escape */
242 static size_t next_quote_pos(const char *s, ssize_t maxlen)
244 size_t len;
245 if (maxlen < 0) {
246 for (len = 0; !cq_must_quote(s[len]); len++);
247 } else {
248 for (len = 0; len < maxlen && !cq_must_quote(s[len]); len++);
250 return len;
254 * C-style name quoting.
256 * (1) if sb and fp are both NULL, inspect the input name and counts the
257 * number of bytes that are needed to hold c_style quoted version of name,
258 * counting the double quotes around it but not terminating NUL, and
259 * returns it.
260 * However, if name does not need c_style quoting, it returns 0.
262 * (2) if sb or fp are not NULL, it emits the c_style quoted version
263 * of name, enclosed with double quotes if asked and needed only.
264 * Return value is the same as in (1).
266 static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
267 struct strbuf *sb, FILE *fp, unsigned flags)
269 #undef EMIT
270 #define EMIT(c) \
271 do { \
272 if (sb) strbuf_addch(sb, (c)); \
273 if (fp) fputc((c), fp); \
274 count++; \
275 } while (0)
276 #define EMITBUF(s, l) \
277 do { \
278 if (sb) strbuf_add(sb, (s), (l)); \
279 if (fp) fwrite((s), (l), 1, fp); \
280 count += (l); \
281 } while (0)
283 int no_dq = !!(flags & CQUOTE_NODQ);
284 size_t len, count = 0;
285 const char *p = name;
287 for (;;) {
288 int ch;
290 len = next_quote_pos(p, maxlen);
291 if (len == maxlen || (maxlen < 0 && !p[len]))
292 break;
294 if (!no_dq && p == name)
295 EMIT('"');
297 EMITBUF(p, len);
298 EMIT('\\');
299 p += len;
300 ch = (unsigned char)*p++;
301 if (maxlen >= 0)
302 maxlen -= len + 1;
303 if (cq_lookup[ch] >= ' ') {
304 EMIT(cq_lookup[ch]);
305 } else {
306 EMIT(((ch >> 6) & 03) + '0');
307 EMIT(((ch >> 3) & 07) + '0');
308 EMIT(((ch >> 0) & 07) + '0');
312 EMITBUF(p, len);
313 if (p == name) /* no ending quote needed */
314 return 0;
316 if (!no_dq)
317 EMIT('"');
318 return count;
321 size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, unsigned flags)
323 return quote_c_style_counted(name, -1, sb, fp, flags);
326 void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path,
327 unsigned flags)
329 int nodq = !!(flags & CQUOTE_NODQ);
330 if (quote_c_style(prefix, NULL, NULL, 0) ||
331 quote_c_style(path, NULL, NULL, 0)) {
332 if (!nodq)
333 strbuf_addch(sb, '"');
334 quote_c_style(prefix, sb, NULL, CQUOTE_NODQ);
335 quote_c_style(path, sb, NULL, CQUOTE_NODQ);
336 if (!nodq)
337 strbuf_addch(sb, '"');
338 } else {
339 strbuf_addstr(sb, prefix);
340 strbuf_addstr(sb, path);
344 void write_name_quoted(const char *name, FILE *fp, int terminator)
346 if (terminator) {
347 quote_c_style(name, NULL, fp, 0);
348 } else {
349 fputs(name, fp);
351 fputc(terminator, fp);
354 void write_name_quoted_relative(const char *name, const char *prefix,
355 FILE *fp, int terminator)
357 struct strbuf sb = STRBUF_INIT;
359 name = relative_path(name, prefix, &sb);
360 write_name_quoted(name, fp, terminator);
362 strbuf_release(&sb);
365 /* quote path as relative to the given prefix */
366 char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigned flags)
368 struct strbuf sb = STRBUF_INIT;
369 const char *rel = relative_path(in, prefix, &sb);
370 int force_dq = ((flags & QUOTE_PATH_QUOTE_SP) && strchr(rel, ' '));
372 strbuf_reset(out);
375 * If the caller wants us to enclose the output in a dq-pair
376 * whether quote_c_style_counted() needs to, we do it ourselves
377 * and tell quote_c_style_counted() not to.
379 if (force_dq)
380 strbuf_addch(out, '"');
381 quote_c_style_counted(rel, strlen(rel), out, NULL,
382 force_dq ? CQUOTE_NODQ : 0);
383 if (force_dq)
384 strbuf_addch(out, '"');
385 strbuf_release(&sb);
387 return out->buf;
391 * C-style name unquoting.
393 * Quoted should point at the opening double quote.
394 * + Returns 0 if it was able to unquote the string properly, and appends the
395 * result in the strbuf `sb'.
396 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
397 * that this function will allocate memory in the strbuf, so calling
398 * strbuf_release is mandatory whichever result unquote_c_style returns.
400 * Updates endp pointer to point at one past the ending double quote if given.
402 int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
404 size_t oldlen = sb->len, len;
405 int ch, ac;
407 if (*quoted++ != '"')
408 return -1;
410 for (;;) {
411 len = strcspn(quoted, "\"\\");
412 strbuf_add(sb, quoted, len);
413 quoted += len;
415 switch (*quoted++) {
416 case '"':
417 if (endp)
418 *endp = quoted;
419 return 0;
420 case '\\':
421 break;
422 default:
423 goto error;
426 switch ((ch = *quoted++)) {
427 case 'a': ch = '\a'; break;
428 case 'b': ch = '\b'; break;
429 case 'f': ch = '\f'; break;
430 case 'n': ch = '\n'; break;
431 case 'r': ch = '\r'; break;
432 case 't': ch = '\t'; break;
433 case 'v': ch = '\v'; break;
435 case '\\': case '"':
436 break; /* verbatim */
438 /* octal values with first digit over 4 overflow */
439 case '0': case '1': case '2': case '3':
440 ac = ((ch - '0') << 6);
441 if ((ch = *quoted++) < '0' || '7' < ch)
442 goto error;
443 ac |= ((ch - '0') << 3);
444 if ((ch = *quoted++) < '0' || '7' < ch)
445 goto error;
446 ac |= (ch - '0');
447 ch = ac;
448 break;
449 default:
450 goto error;
452 strbuf_addch(sb, ch);
455 error:
456 strbuf_setlen(sb, oldlen);
457 return -1;
460 /* quoting as a string literal for other languages */
462 void perl_quote_buf(struct strbuf *sb, const char *src)
464 const char sq = '\'';
465 const char bq = '\\';
466 char c;
468 strbuf_addch(sb, sq);
469 while ((c = *src++)) {
470 if (c == sq || c == bq)
471 strbuf_addch(sb, bq);
472 strbuf_addch(sb, c);
474 strbuf_addch(sb, sq);
477 void perl_quote_buf_with_len(struct strbuf *sb, const char *src, size_t len)
479 const char sq = '\'';
480 const char bq = '\\';
481 const char *c = src;
482 const char *end = src + len;
484 strbuf_addch(sb, sq);
485 while (c != end) {
486 if (*c == sq || *c == bq)
487 strbuf_addch(sb, bq);
488 strbuf_addch(sb, *c);
489 c++;
491 strbuf_addch(sb, sq);
494 void python_quote_buf(struct strbuf *sb, const char *src)
496 const char sq = '\'';
497 const char bq = '\\';
498 const char nl = '\n';
499 char c;
501 strbuf_addch(sb, sq);
502 while ((c = *src++)) {
503 if (c == nl) {
504 strbuf_addch(sb, bq);
505 strbuf_addch(sb, 'n');
506 continue;
508 if (c == sq || c == bq)
509 strbuf_addch(sb, bq);
510 strbuf_addch(sb, c);
512 strbuf_addch(sb, sq);
515 void tcl_quote_buf(struct strbuf *sb, const char *src)
517 char c;
519 strbuf_addch(sb, '"');
520 while ((c = *src++)) {
521 switch (c) {
522 case '[': case ']':
523 case '{': case '}':
524 case '$': case '\\': case '"':
525 strbuf_addch(sb, '\\');
526 /* fallthrough */
527 default:
528 strbuf_addch(sb, c);
529 break;
530 case '\f':
531 strbuf_addstr(sb, "\\f");
532 break;
533 case '\r':
534 strbuf_addstr(sb, "\\r");
535 break;
536 case '\n':
537 strbuf_addstr(sb, "\\n");
538 break;
539 case '\t':
540 strbuf_addstr(sb, "\\t");
541 break;
542 case '\v':
543 strbuf_addstr(sb, "\\v");
544 break;
547 strbuf_addch(sb, '"');
550 void basic_regex_quote_buf(struct strbuf *sb, const char *src)
552 char c;
554 if (*src == '^') {
555 /* only beginning '^' is special and needs quoting */
556 strbuf_addch(sb, '\\');
557 strbuf_addch(sb, *src++);
559 if (*src == '*')
560 /* beginning '*' is not special, no quoting */
561 strbuf_addch(sb, *src++);
563 while ((c = *src++)) {
564 switch (c) {
565 case '[':
566 case '.':
567 case '\\':
568 case '*':
569 strbuf_addch(sb, '\\');
570 strbuf_addch(sb, c);
571 break;
573 case '$':
574 /* only the end '$' is special and needs quoting */
575 if (*src == '\0')
576 strbuf_addch(sb, '\\');
577 strbuf_addch(sb, c);
578 break;
580 default:
581 strbuf_addch(sb, c);
582 break;