hash.h: move some oid-related declarations from cache.h
[git/debian.git] / quote.c
blob2453397fbbd11062b574bcd8d80449df0f29ed21
1 #include "cache.h"
2 #include "alloc.h"
3 #include "quote.h"
4 #include "strvec.h"
6 int quote_path_fully = 1;
8 static inline int need_bs_quote(char c)
10 return (c == '\'' || c == '!');
13 /* Help to copy the thing properly quoted for the shell safety.
14 * any single quote is replaced with '\'', any exclamation point
15 * is replaced with '\!', and the whole thing is enclosed in a
16 * single quote pair.
18 * E.g.
19 * original sq_quote result
20 * name ==> name ==> 'name'
21 * a b ==> a b ==> 'a b'
22 * a'b ==> a'\''b ==> 'a'\''b'
23 * a!b ==> a'\!'b ==> 'a'\!'b'
25 void sq_quote_buf(struct strbuf *dst, const char *src)
27 char *to_free = NULL;
29 if (dst->buf == src)
30 to_free = strbuf_detach(dst, NULL);
32 strbuf_addch(dst, '\'');
33 while (*src) {
34 size_t len = strcspn(src, "'!");
35 strbuf_add(dst, src, len);
36 src += len;
37 while (need_bs_quote(*src)) {
38 strbuf_addstr(dst, "'\\");
39 strbuf_addch(dst, *src++);
40 strbuf_addch(dst, '\'');
43 strbuf_addch(dst, '\'');
44 free(to_free);
47 void sq_quote_buf_pretty(struct strbuf *dst, const char *src)
49 static const char ok_punct[] = "+,-./:=@_^";
50 const char *p;
52 /* Avoid losing a zero-length string by adding '' */
53 if (!*src) {
54 strbuf_addstr(dst, "''");
55 return;
58 for (p = src; *p; p++) {
59 if (!isalnum(*p) && !strchr(ok_punct, *p)) {
60 sq_quote_buf(dst, src);
61 return;
65 /* if we get here, we did not need quoting */
66 strbuf_addstr(dst, src);
69 void sq_quotef(struct strbuf *dst, const char *fmt, ...)
71 struct strbuf src = STRBUF_INIT;
73 va_list ap;
74 va_start(ap, fmt);
75 strbuf_vaddf(&src, fmt, ap);
76 va_end(ap);
78 sq_quote_buf(dst, src.buf);
79 strbuf_release(&src);
82 void sq_quote_argv(struct strbuf *dst, const char **argv)
84 int i;
86 /* Copy into destination buffer. */
87 strbuf_grow(dst, 255);
88 for (i = 0; argv[i]; ++i) {
89 strbuf_addch(dst, ' ');
90 sq_quote_buf(dst, argv[i]);
95 * Legacy function to append each argv value, quoted as necessasry,
96 * with whitespace before each value. This results in a leading
97 * space in the result.
99 void sq_quote_argv_pretty(struct strbuf *dst, const char **argv)
101 if (argv[0])
102 strbuf_addch(dst, ' ');
103 sq_append_quote_argv_pretty(dst, argv);
107 * Append each argv value, quoted as necessary, with whitespace between them.
109 void sq_append_quote_argv_pretty(struct strbuf *dst, const char **argv)
111 int i;
113 for (i = 0; argv[i]; i++) {
114 if (i > 0)
115 strbuf_addch(dst, ' ');
116 sq_quote_buf_pretty(dst, argv[i]);
120 char *sq_dequote_step(char *arg, char **next)
122 char *dst = arg;
123 char *src = arg;
124 char c;
126 if (*src != '\'')
127 return NULL;
128 for (;;) {
129 c = *++src;
130 if (!c)
131 return NULL;
132 if (c != '\'') {
133 *dst++ = c;
134 continue;
136 /* We stepped out of sq */
137 switch (*++src) {
138 case '\0':
139 *dst = 0;
140 if (next)
141 *next = NULL;
142 return arg;
143 case '\\':
145 * Allow backslashed characters outside of
146 * single-quotes only if they need escaping,
147 * and only if we resume the single-quoted part
148 * afterward.
150 if (need_bs_quote(src[1]) && src[2] == '\'') {
151 *dst++ = src[1];
152 src += 2;
153 continue;
155 /* Fallthrough */
156 default:
157 if (!next)
158 return NULL;
159 *dst = 0;
160 *next = src;
161 return arg;
166 char *sq_dequote(char *arg)
168 return sq_dequote_step(arg, NULL);
171 static int sq_dequote_to_argv_internal(char *arg,
172 const char ***argv, int *nr, int *alloc,
173 struct strvec *array)
175 char *next = arg;
177 if (!*arg)
178 return 0;
179 do {
180 char *dequoted = sq_dequote_step(next, &next);
181 if (!dequoted)
182 return -1;
183 if (next) {
184 char c;
185 if (!isspace(*next))
186 return -1;
187 do {
188 c = *++next;
189 } while (isspace(c));
191 if (argv) {
192 ALLOC_GROW(*argv, *nr + 1, *alloc);
193 (*argv)[(*nr)++] = dequoted;
195 if (array)
196 strvec_push(array, dequoted);
197 } while (next);
199 return 0;
202 int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
204 return sq_dequote_to_argv_internal(arg, argv, nr, alloc, NULL);
207 int sq_dequote_to_strvec(char *arg, struct strvec *array)
209 return sq_dequote_to_argv_internal(arg, NULL, NULL, NULL, array);
212 /* 1 means: quote as octal
213 * 0 means: quote as octal if (quote_path_fully)
214 * -1 means: never quote
215 * c: quote as "\\c"
217 #define X8(x) x, x, x, x, x, x, x, x
218 #define X16(x) X8(x), X8(x)
219 static signed char const cq_lookup[256] = {
220 /* 0 1 2 3 4 5 6 7 */
221 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
222 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
223 /* 0x10 */ X16(1),
224 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
225 /* 0x28 */ X16(-1), X16(-1), X16(-1),
226 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
227 /* 0x60 */ X16(-1), X8(-1),
228 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
229 /* 0x80 */ /* set to 0 */
232 static inline int cq_must_quote(char c)
234 return cq_lookup[(unsigned char)c] + quote_path_fully > 0;
237 /* returns the longest prefix not needing a quote up to maxlen if positive.
238 This stops at the first \0 because it's marked as a character needing an
239 escape */
240 static size_t next_quote_pos(const char *s, ssize_t maxlen)
242 size_t len;
243 if (maxlen < 0) {
244 for (len = 0; !cq_must_quote(s[len]); len++);
245 } else {
246 for (len = 0; len < maxlen && !cq_must_quote(s[len]); len++);
248 return len;
252 * C-style name quoting.
254 * (1) if sb and fp are both NULL, inspect the input name and counts the
255 * number of bytes that are needed to hold c_style quoted version of name,
256 * counting the double quotes around it but not terminating NUL, and
257 * returns it.
258 * However, if name does not need c_style quoting, it returns 0.
260 * (2) if sb or fp are not NULL, it emits the c_style quoted version
261 * of name, enclosed with double quotes if asked and needed only.
262 * Return value is the same as in (1).
264 static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
265 struct strbuf *sb, FILE *fp, unsigned flags)
267 #undef EMIT
268 #define EMIT(c) \
269 do { \
270 if (sb) strbuf_addch(sb, (c)); \
271 if (fp) fputc((c), fp); \
272 count++; \
273 } while (0)
274 #define EMITBUF(s, l) \
275 do { \
276 if (sb) strbuf_add(sb, (s), (l)); \
277 if (fp) fwrite((s), (l), 1, fp); \
278 count += (l); \
279 } while (0)
281 int no_dq = !!(flags & CQUOTE_NODQ);
282 size_t len, count = 0;
283 const char *p = name;
285 for (;;) {
286 int ch;
288 len = next_quote_pos(p, maxlen);
289 if (len == maxlen || (maxlen < 0 && !p[len]))
290 break;
292 if (!no_dq && p == name)
293 EMIT('"');
295 EMITBUF(p, len);
296 EMIT('\\');
297 p += len;
298 ch = (unsigned char)*p++;
299 if (maxlen >= 0)
300 maxlen -= len + 1;
301 if (cq_lookup[ch] >= ' ') {
302 EMIT(cq_lookup[ch]);
303 } else {
304 EMIT(((ch >> 6) & 03) + '0');
305 EMIT(((ch >> 3) & 07) + '0');
306 EMIT(((ch >> 0) & 07) + '0');
310 EMITBUF(p, len);
311 if (p == name) /* no ending quote needed */
312 return 0;
314 if (!no_dq)
315 EMIT('"');
316 return count;
319 size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, unsigned flags)
321 return quote_c_style_counted(name, -1, sb, fp, flags);
324 void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path,
325 unsigned flags)
327 int nodq = !!(flags & CQUOTE_NODQ);
328 if (quote_c_style(prefix, NULL, NULL, 0) ||
329 quote_c_style(path, NULL, NULL, 0)) {
330 if (!nodq)
331 strbuf_addch(sb, '"');
332 quote_c_style(prefix, sb, NULL, CQUOTE_NODQ);
333 quote_c_style(path, sb, NULL, CQUOTE_NODQ);
334 if (!nodq)
335 strbuf_addch(sb, '"');
336 } else {
337 strbuf_addstr(sb, prefix);
338 strbuf_addstr(sb, path);
342 void write_name_quoted(const char *name, FILE *fp, int terminator)
344 if (terminator) {
345 quote_c_style(name, NULL, fp, 0);
346 } else {
347 fputs(name, fp);
349 fputc(terminator, fp);
352 void write_name_quoted_relative(const char *name, const char *prefix,
353 FILE *fp, int terminator)
355 struct strbuf sb = STRBUF_INIT;
357 name = relative_path(name, prefix, &sb);
358 write_name_quoted(name, fp, terminator);
360 strbuf_release(&sb);
363 /* quote path as relative to the given prefix */
364 char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigned flags)
366 struct strbuf sb = STRBUF_INIT;
367 const char *rel = relative_path(in, prefix, &sb);
368 int force_dq = ((flags & QUOTE_PATH_QUOTE_SP) && strchr(rel, ' '));
370 strbuf_reset(out);
373 * If the caller wants us to enclose the output in a dq-pair
374 * whether quote_c_style_counted() needs to, we do it ourselves
375 * and tell quote_c_style_counted() not to.
377 if (force_dq)
378 strbuf_addch(out, '"');
379 quote_c_style_counted(rel, strlen(rel), out, NULL,
380 force_dq ? CQUOTE_NODQ : 0);
381 if (force_dq)
382 strbuf_addch(out, '"');
383 strbuf_release(&sb);
385 return out->buf;
389 * C-style name unquoting.
391 * Quoted should point at the opening double quote.
392 * + Returns 0 if it was able to unquote the string properly, and appends the
393 * result in the strbuf `sb'.
394 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
395 * that this function will allocate memory in the strbuf, so calling
396 * strbuf_release is mandatory whichever result unquote_c_style returns.
398 * Updates endp pointer to point at one past the ending double quote if given.
400 int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
402 size_t oldlen = sb->len, len;
403 int ch, ac;
405 if (*quoted++ != '"')
406 return -1;
408 for (;;) {
409 len = strcspn(quoted, "\"\\");
410 strbuf_add(sb, quoted, len);
411 quoted += len;
413 switch (*quoted++) {
414 case '"':
415 if (endp)
416 *endp = quoted;
417 return 0;
418 case '\\':
419 break;
420 default:
421 goto error;
424 switch ((ch = *quoted++)) {
425 case 'a': ch = '\a'; break;
426 case 'b': ch = '\b'; break;
427 case 'f': ch = '\f'; break;
428 case 'n': ch = '\n'; break;
429 case 'r': ch = '\r'; break;
430 case 't': ch = '\t'; break;
431 case 'v': ch = '\v'; break;
433 case '\\': case '"':
434 break; /* verbatim */
436 /* octal values with first digit over 4 overflow */
437 case '0': case '1': case '2': case '3':
438 ac = ((ch - '0') << 6);
439 if ((ch = *quoted++) < '0' || '7' < ch)
440 goto error;
441 ac |= ((ch - '0') << 3);
442 if ((ch = *quoted++) < '0' || '7' < ch)
443 goto error;
444 ac |= (ch - '0');
445 ch = ac;
446 break;
447 default:
448 goto error;
450 strbuf_addch(sb, ch);
453 error:
454 strbuf_setlen(sb, oldlen);
455 return -1;
458 /* quoting as a string literal for other languages */
460 void perl_quote_buf(struct strbuf *sb, const char *src)
462 const char sq = '\'';
463 const char bq = '\\';
464 char c;
466 strbuf_addch(sb, sq);
467 while ((c = *src++)) {
468 if (c == sq || c == bq)
469 strbuf_addch(sb, bq);
470 strbuf_addch(sb, c);
472 strbuf_addch(sb, sq);
475 void perl_quote_buf_with_len(struct strbuf *sb, const char *src, size_t len)
477 const char sq = '\'';
478 const char bq = '\\';
479 const char *c = src;
480 const char *end = src + len;
482 strbuf_addch(sb, sq);
483 while (c != end) {
484 if (*c == sq || *c == bq)
485 strbuf_addch(sb, bq);
486 strbuf_addch(sb, *c);
487 c++;
489 strbuf_addch(sb, sq);
492 void python_quote_buf(struct strbuf *sb, const char *src)
494 const char sq = '\'';
495 const char bq = '\\';
496 const char nl = '\n';
497 char c;
499 strbuf_addch(sb, sq);
500 while ((c = *src++)) {
501 if (c == nl) {
502 strbuf_addch(sb, bq);
503 strbuf_addch(sb, 'n');
504 continue;
506 if (c == sq || c == bq)
507 strbuf_addch(sb, bq);
508 strbuf_addch(sb, c);
510 strbuf_addch(sb, sq);
513 void tcl_quote_buf(struct strbuf *sb, const char *src)
515 char c;
517 strbuf_addch(sb, '"');
518 while ((c = *src++)) {
519 switch (c) {
520 case '[': case ']':
521 case '{': case '}':
522 case '$': case '\\': case '"':
523 strbuf_addch(sb, '\\');
524 /* fallthrough */
525 default:
526 strbuf_addch(sb, c);
527 break;
528 case '\f':
529 strbuf_addstr(sb, "\\f");
530 break;
531 case '\r':
532 strbuf_addstr(sb, "\\r");
533 break;
534 case '\n':
535 strbuf_addstr(sb, "\\n");
536 break;
537 case '\t':
538 strbuf_addstr(sb, "\\t");
539 break;
540 case '\v':
541 strbuf_addstr(sb, "\\v");
542 break;
545 strbuf_addch(sb, '"');
548 void basic_regex_quote_buf(struct strbuf *sb, const char *src)
550 char c;
552 if (*src == '^') {
553 /* only beginning '^' is special and needs quoting */
554 strbuf_addch(sb, '\\');
555 strbuf_addch(sb, *src++);
557 if (*src == '*')
558 /* beginning '*' is not special, no quoting */
559 strbuf_addch(sb, *src++);
561 while ((c = *src++)) {
562 switch (c) {
563 case '[':
564 case '.':
565 case '\\':
566 case '*':
567 strbuf_addch(sb, '\\');
568 strbuf_addch(sb, c);
569 break;
571 case '$':
572 /* only the end '$' is special and needs quoting */
573 if (*src == '\0')
574 strbuf_addch(sb, '\\');
575 strbuf_addch(sb, c);
576 break;
578 default:
579 strbuf_addch(sb, c);
580 break;