The sixteenth batch
[git/gitster.git] / quote.c
blobb9f6bdc775c4686c192b8b339adac7e664af8e0f
1 #define DISABLE_SIGN_COMPARE_WARNINGS
3 #include "git-compat-util.h"
4 #include "path.h"
5 #include "quote.h"
6 #include "strbuf.h"
7 #include "strvec.h"
9 int quote_path_fully = 1;
11 static inline int need_bs_quote(char c)
13 return (c == '\'' || c == '!');
16 /* Help to copy the thing properly quoted for the shell safety.
17 * any single quote is replaced with '\'', any exclamation point
18 * is replaced with '\!', and the whole thing is enclosed in a
19 * single quote pair.
21 * E.g.
22 * original sq_quote result
23 * name ==> name ==> 'name'
24 * a b ==> a b ==> 'a b'
25 * a'b ==> a'\''b ==> 'a'\''b'
26 * a!b ==> a'\!'b ==> 'a'\!'b'
28 void sq_quote_buf(struct strbuf *dst, const char *src)
30 char *to_free = NULL;
32 if (dst->buf == src)
33 to_free = strbuf_detach(dst, NULL);
35 strbuf_addch(dst, '\'');
36 while (*src) {
37 size_t len = strcspn(src, "'!");
38 strbuf_add(dst, src, len);
39 src += len;
40 while (need_bs_quote(*src)) {
41 strbuf_addstr(dst, "'\\");
42 strbuf_addch(dst, *src++);
43 strbuf_addch(dst, '\'');
46 strbuf_addch(dst, '\'');
47 free(to_free);
50 void sq_quote_buf_pretty(struct strbuf *dst, const char *src)
52 static const char ok_punct[] = "+,-./:=@_^";
53 const char *p;
55 /* Avoid losing a zero-length string by adding '' */
56 if (!*src) {
57 strbuf_addstr(dst, "''");
58 return;
61 for (p = src; *p; p++) {
62 if (!isalnum(*p) && !strchr(ok_punct, *p)) {
63 sq_quote_buf(dst, src);
64 return;
68 /* if we get here, we did not need quoting */
69 strbuf_addstr(dst, src);
72 void sq_quotef(struct strbuf *dst, const char *fmt, ...)
74 struct strbuf src = STRBUF_INIT;
76 va_list ap;
77 va_start(ap, fmt);
78 strbuf_vaddf(&src, fmt, ap);
79 va_end(ap);
81 sq_quote_buf(dst, src.buf);
82 strbuf_release(&src);
85 void sq_quote_argv(struct strbuf *dst, const char **argv)
87 int i;
89 /* Copy into destination buffer. */
90 strbuf_grow(dst, 255);
91 for (i = 0; argv[i]; ++i) {
92 strbuf_addch(dst, ' ');
93 sq_quote_buf(dst, argv[i]);
98 * Legacy function to append each argv value, quoted as necessasry,
99 * with whitespace before each value. This results in a leading
100 * space in the result.
102 void sq_quote_argv_pretty(struct strbuf *dst, const char **argv)
104 if (argv[0])
105 strbuf_addch(dst, ' ');
106 sq_append_quote_argv_pretty(dst, argv);
110 * Append each argv value, quoted as necessary, with whitespace between them.
112 void sq_append_quote_argv_pretty(struct strbuf *dst, const char **argv)
114 int i;
116 for (i = 0; argv[i]; i++) {
117 if (i > 0)
118 strbuf_addch(dst, ' ');
119 sq_quote_buf_pretty(dst, argv[i]);
123 char *sq_dequote_step(char *arg, char **next)
125 char *dst = arg;
126 char *src = arg;
127 char c;
129 if (*src != '\'')
130 return NULL;
131 for (;;) {
132 c = *++src;
133 if (!c)
134 return NULL;
135 if (c != '\'') {
136 *dst++ = c;
137 continue;
139 /* We stepped out of sq */
140 switch (*++src) {
141 case '\0':
142 *dst = 0;
143 if (next)
144 *next = NULL;
145 return arg;
146 case '\\':
148 * Allow backslashed characters outside of
149 * single-quotes only if they need escaping,
150 * and only if we resume the single-quoted part
151 * afterward.
153 if (need_bs_quote(src[1]) && src[2] == '\'') {
154 *dst++ = src[1];
155 src += 2;
156 continue;
158 /* Fallthrough */
159 default:
160 if (!next)
161 return NULL;
162 *dst = 0;
163 *next = src;
164 return arg;
169 char *sq_dequote(char *arg)
171 return sq_dequote_step(arg, NULL);
174 static int sq_dequote_to_argv_internal(char *arg,
175 const char ***argv, int *nr, int *alloc,
176 struct strvec *array)
178 char *next = arg;
180 if (!*arg)
181 return 0;
182 do {
183 char *dequoted = sq_dequote_step(next, &next);
184 if (!dequoted)
185 return -1;
186 if (next) {
187 char c;
188 if (!isspace(*next))
189 return -1;
190 do {
191 c = *++next;
192 } while (isspace(c));
194 if (argv) {
195 ALLOC_GROW(*argv, *nr + 1, *alloc);
196 (*argv)[(*nr)++] = dequoted;
198 if (array)
199 strvec_push(array, dequoted);
200 } while (next);
202 return 0;
205 int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
207 return sq_dequote_to_argv_internal(arg, argv, nr, alloc, NULL);
210 int sq_dequote_to_strvec(char *arg, struct strvec *array)
212 return sq_dequote_to_argv_internal(arg, NULL, NULL, NULL, array);
215 /* 1 means: quote as octal
216 * 0 means: quote as octal if (quote_path_fully)
217 * -1 means: never quote
218 * c: quote as "\\c"
220 #define X8(x) x, x, x, x, x, x, x, x
221 #define X16(x) X8(x), X8(x)
222 static signed char const cq_lookup[256] = {
223 /* 0 1 2 3 4 5 6 7 */
224 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
225 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
226 /* 0x10 */ X16(1),
227 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
228 /* 0x28 */ X16(-1), X16(-1), X16(-1),
229 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
230 /* 0x60 */ X16(-1), X8(-1),
231 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
232 /* 0x80 */ /* set to 0 */
235 static inline int cq_must_quote(char c)
237 return cq_lookup[(unsigned char)c] + quote_path_fully > 0;
240 /* returns the longest prefix not needing a quote up to maxlen if positive.
241 This stops at the first \0 because it's marked as a character needing an
242 escape */
243 static size_t next_quote_pos(const char *s, ssize_t maxlen)
245 size_t len;
246 if (maxlen < 0) {
247 for (len = 0; !cq_must_quote(s[len]); len++);
248 } else {
249 for (len = 0; len < maxlen && !cq_must_quote(s[len]); len++);
251 return len;
255 * C-style name quoting.
257 * (1) if sb and fp are both NULL, inspect the input name and counts the
258 * number of bytes that are needed to hold c_style quoted version of name,
259 * counting the double quotes around it but not terminating NUL, and
260 * returns it.
261 * However, if name does not need c_style quoting, it returns 0.
263 * (2) if sb or fp are not NULL, it emits the c_style quoted version
264 * of name, enclosed with double quotes if asked and needed only.
265 * Return value is the same as in (1).
267 static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
268 struct strbuf *sb, FILE *fp, unsigned flags)
270 #undef EMIT
271 #define EMIT(c) \
272 do { \
273 if (sb) strbuf_addch(sb, (c)); \
274 if (fp) fputc((c), fp); \
275 count++; \
276 } while (0)
277 #define EMITBUF(s, l) \
278 do { \
279 if (sb) strbuf_add(sb, (s), (l)); \
280 if (fp) fwrite((s), (l), 1, fp); \
281 count += (l); \
282 } while (0)
284 int no_dq = !!(flags & CQUOTE_NODQ);
285 size_t len, count = 0;
286 const char *p = name;
288 for (;;) {
289 int ch;
291 len = next_quote_pos(p, maxlen);
292 if (len == maxlen || (maxlen < 0 && !p[len]))
293 break;
295 if (!no_dq && p == name)
296 EMIT('"');
298 EMITBUF(p, len);
299 EMIT('\\');
300 p += len;
301 ch = (unsigned char)*p++;
302 if (maxlen >= 0)
303 maxlen -= len + 1;
304 if (cq_lookup[ch] >= ' ') {
305 EMIT(cq_lookup[ch]);
306 } else {
307 EMIT(((ch >> 6) & 03) + '0');
308 EMIT(((ch >> 3) & 07) + '0');
309 EMIT(((ch >> 0) & 07) + '0');
313 EMITBUF(p, len);
314 if (p == name) /* no ending quote needed */
315 return 0;
317 if (!no_dq)
318 EMIT('"');
319 return count;
322 size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, unsigned flags)
324 return quote_c_style_counted(name, -1, sb, fp, flags);
327 void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path,
328 unsigned flags)
330 int nodq = !!(flags & CQUOTE_NODQ);
331 if (quote_c_style(prefix, NULL, NULL, 0) ||
332 quote_c_style(path, NULL, NULL, 0)) {
333 if (!nodq)
334 strbuf_addch(sb, '"');
335 quote_c_style(prefix, sb, NULL, CQUOTE_NODQ);
336 quote_c_style(path, sb, NULL, CQUOTE_NODQ);
337 if (!nodq)
338 strbuf_addch(sb, '"');
339 } else {
340 strbuf_addstr(sb, prefix);
341 strbuf_addstr(sb, path);
345 void write_name_quoted(const char *name, FILE *fp, int terminator)
347 if (terminator) {
348 quote_c_style(name, NULL, fp, 0);
349 } else {
350 fputs(name, fp);
352 fputc(terminator, fp);
355 void write_name_quoted_relative(const char *name, const char *prefix,
356 FILE *fp, int terminator)
358 struct strbuf sb = STRBUF_INIT;
360 name = relative_path(name, prefix, &sb);
361 write_name_quoted(name, fp, terminator);
363 strbuf_release(&sb);
366 /* quote path as relative to the given prefix */
367 char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigned flags)
369 struct strbuf sb = STRBUF_INIT;
370 const char *rel = relative_path(in, prefix, &sb);
371 int force_dq = ((flags & QUOTE_PATH_QUOTE_SP) && strchr(rel, ' '));
373 strbuf_reset(out);
376 * If the caller wants us to enclose the output in a dq-pair
377 * whether quote_c_style_counted() needs to, we do it ourselves
378 * and tell quote_c_style_counted() not to.
380 if (force_dq)
381 strbuf_addch(out, '"');
382 quote_c_style_counted(rel, strlen(rel), out, NULL,
383 force_dq ? CQUOTE_NODQ : 0);
384 if (force_dq)
385 strbuf_addch(out, '"');
386 strbuf_release(&sb);
388 return out->buf;
392 * C-style name unquoting.
394 * Quoted should point at the opening double quote.
395 * + Returns 0 if it was able to unquote the string properly, and appends the
396 * result in the strbuf `sb'.
397 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
398 * that this function will allocate memory in the strbuf, so calling
399 * strbuf_release is mandatory whichever result unquote_c_style returns.
401 * Updates endp pointer to point at one past the ending double quote if given.
403 int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
405 size_t oldlen = sb->len, len;
406 int ch, ac;
408 if (*quoted++ != '"')
409 return -1;
411 for (;;) {
412 len = strcspn(quoted, "\"\\");
413 strbuf_add(sb, quoted, len);
414 quoted += len;
416 switch (*quoted++) {
417 case '"':
418 if (endp)
419 *endp = quoted;
420 return 0;
421 case '\\':
422 break;
423 default:
424 goto error;
427 switch ((ch = *quoted++)) {
428 case 'a': ch = '\a'; break;
429 case 'b': ch = '\b'; break;
430 case 'f': ch = '\f'; break;
431 case 'n': ch = '\n'; break;
432 case 'r': ch = '\r'; break;
433 case 't': ch = '\t'; break;
434 case 'v': ch = '\v'; break;
436 case '\\': case '"':
437 break; /* verbatim */
439 /* octal values with first digit over 4 overflow */
440 case '0': case '1': case '2': case '3':
441 ac = ((ch - '0') << 6);
442 if ((ch = *quoted++) < '0' || '7' < ch)
443 goto error;
444 ac |= ((ch - '0') << 3);
445 if ((ch = *quoted++) < '0' || '7' < ch)
446 goto error;
447 ac |= (ch - '0');
448 ch = ac;
449 break;
450 default:
451 goto error;
453 strbuf_addch(sb, ch);
456 error:
457 strbuf_setlen(sb, oldlen);
458 return -1;
461 /* quoting as a string literal for other languages */
463 void perl_quote_buf(struct strbuf *sb, const char *src)
465 const char sq = '\'';
466 const char bq = '\\';
467 char c;
469 strbuf_addch(sb, sq);
470 while ((c = *src++)) {
471 if (c == sq || c == bq)
472 strbuf_addch(sb, bq);
473 strbuf_addch(sb, c);
475 strbuf_addch(sb, sq);
478 void perl_quote_buf_with_len(struct strbuf *sb, const char *src, size_t len)
480 const char sq = '\'';
481 const char bq = '\\';
482 const char *c = src;
483 const char *end = src + len;
485 strbuf_addch(sb, sq);
486 while (c != end) {
487 if (*c == sq || *c == bq)
488 strbuf_addch(sb, bq);
489 strbuf_addch(sb, *c);
490 c++;
492 strbuf_addch(sb, sq);
495 void python_quote_buf(struct strbuf *sb, const char *src)
497 const char sq = '\'';
498 const char bq = '\\';
499 const char nl = '\n';
500 char c;
502 strbuf_addch(sb, sq);
503 while ((c = *src++)) {
504 if (c == nl) {
505 strbuf_addch(sb, bq);
506 strbuf_addch(sb, 'n');
507 continue;
509 if (c == sq || c == bq)
510 strbuf_addch(sb, bq);
511 strbuf_addch(sb, c);
513 strbuf_addch(sb, sq);
516 void tcl_quote_buf(struct strbuf *sb, const char *src)
518 char c;
520 strbuf_addch(sb, '"');
521 while ((c = *src++)) {
522 switch (c) {
523 case '[': case ']':
524 case '{': case '}':
525 case '$': case '\\': case '"':
526 strbuf_addch(sb, '\\');
527 /* fallthrough */
528 default:
529 strbuf_addch(sb, c);
530 break;
531 case '\f':
532 strbuf_addstr(sb, "\\f");
533 break;
534 case '\r':
535 strbuf_addstr(sb, "\\r");
536 break;
537 case '\n':
538 strbuf_addstr(sb, "\\n");
539 break;
540 case '\t':
541 strbuf_addstr(sb, "\\t");
542 break;
543 case '\v':
544 strbuf_addstr(sb, "\\v");
545 break;
548 strbuf_addch(sb, '"');
551 void basic_regex_quote_buf(struct strbuf *sb, const char *src)
553 char c;
555 if (*src == '^') {
556 /* only beginning '^' is special and needs quoting */
557 strbuf_addch(sb, '\\');
558 strbuf_addch(sb, *src++);
560 if (*src == '*')
561 /* beginning '*' is not special, no quoting */
562 strbuf_addch(sb, *src++);
564 while ((c = *src++)) {
565 switch (c) {
566 case '[':
567 case '.':
568 case '\\':
569 case '*':
570 strbuf_addch(sb, '\\');
571 strbuf_addch(sb, c);
572 break;
574 case '$':
575 /* only the end '$' is special and needs quoting */
576 if (*src == '\0')
577 strbuf_addch(sb, '\\');
578 strbuf_addch(sb, c);
579 break;
581 default:
582 strbuf_addch(sb, c);
583 break;