Merge branch 'ob/test-lib-rebase-fake-editor-updates'
[alt-git.git] / quote.c
blob3c05194496f65f879f437abd29823986ef118f47
1 #include "git-compat-util.h"
2 #include "path.h"
3 #include "quote.h"
4 #include "strbuf.h"
5 #include "strvec.h"
7 int quote_path_fully = 1;
9 static inline int need_bs_quote(char c)
11 return (c == '\'' || c == '!');
14 /* Help to copy the thing properly quoted for the shell safety.
15 * any single quote is replaced with '\'', any exclamation point
16 * is replaced with '\!', and the whole thing is enclosed in a
17 * single quote pair.
19 * E.g.
20 * original sq_quote result
21 * name ==> name ==> 'name'
22 * a b ==> a b ==> 'a b'
23 * a'b ==> a'\''b ==> 'a'\''b'
24 * a!b ==> a'\!'b ==> 'a'\!'b'
26 void sq_quote_buf(struct strbuf *dst, const char *src)
28 char *to_free = NULL;
30 if (dst->buf == src)
31 to_free = strbuf_detach(dst, NULL);
33 strbuf_addch(dst, '\'');
34 while (*src) {
35 size_t len = strcspn(src, "'!");
36 strbuf_add(dst, src, len);
37 src += len;
38 while (need_bs_quote(*src)) {
39 strbuf_addstr(dst, "'\\");
40 strbuf_addch(dst, *src++);
41 strbuf_addch(dst, '\'');
44 strbuf_addch(dst, '\'');
45 free(to_free);
48 void sq_quote_buf_pretty(struct strbuf *dst, const char *src)
50 static const char ok_punct[] = "+,-./:=@_^";
51 const char *p;
53 /* Avoid losing a zero-length string by adding '' */
54 if (!*src) {
55 strbuf_addstr(dst, "''");
56 return;
59 for (p = src; *p; p++) {
60 if (!isalnum(*p) && !strchr(ok_punct, *p)) {
61 sq_quote_buf(dst, src);
62 return;
66 /* if we get here, we did not need quoting */
67 strbuf_addstr(dst, src);
70 void sq_quotef(struct strbuf *dst, const char *fmt, ...)
72 struct strbuf src = STRBUF_INIT;
74 va_list ap;
75 va_start(ap, fmt);
76 strbuf_vaddf(&src, fmt, ap);
77 va_end(ap);
79 sq_quote_buf(dst, src.buf);
80 strbuf_release(&src);
83 void sq_quote_argv(struct strbuf *dst, const char **argv)
85 int i;
87 /* Copy into destination buffer. */
88 strbuf_grow(dst, 255);
89 for (i = 0; argv[i]; ++i) {
90 strbuf_addch(dst, ' ');
91 sq_quote_buf(dst, argv[i]);
96 * Legacy function to append each argv value, quoted as necessasry,
97 * with whitespace before each value. This results in a leading
98 * space in the result.
100 void sq_quote_argv_pretty(struct strbuf *dst, const char **argv)
102 if (argv[0])
103 strbuf_addch(dst, ' ');
104 sq_append_quote_argv_pretty(dst, argv);
108 * Append each argv value, quoted as necessary, with whitespace between them.
110 void sq_append_quote_argv_pretty(struct strbuf *dst, const char **argv)
112 int i;
114 for (i = 0; argv[i]; i++) {
115 if (i > 0)
116 strbuf_addch(dst, ' ');
117 sq_quote_buf_pretty(dst, argv[i]);
121 char *sq_dequote_step(char *arg, char **next)
123 char *dst = arg;
124 char *src = arg;
125 char c;
127 if (*src != '\'')
128 return NULL;
129 for (;;) {
130 c = *++src;
131 if (!c)
132 return NULL;
133 if (c != '\'') {
134 *dst++ = c;
135 continue;
137 /* We stepped out of sq */
138 switch (*++src) {
139 case '\0':
140 *dst = 0;
141 if (next)
142 *next = NULL;
143 return arg;
144 case '\\':
146 * Allow backslashed characters outside of
147 * single-quotes only if they need escaping,
148 * and only if we resume the single-quoted part
149 * afterward.
151 if (need_bs_quote(src[1]) && src[2] == '\'') {
152 *dst++ = src[1];
153 src += 2;
154 continue;
156 /* Fallthrough */
157 default:
158 if (!next)
159 return NULL;
160 *dst = 0;
161 *next = src;
162 return arg;
167 char *sq_dequote(char *arg)
169 return sq_dequote_step(arg, NULL);
172 static int sq_dequote_to_argv_internal(char *arg,
173 const char ***argv, int *nr, int *alloc,
174 struct strvec *array)
176 char *next = arg;
178 if (!*arg)
179 return 0;
180 do {
181 char *dequoted = sq_dequote_step(next, &next);
182 if (!dequoted)
183 return -1;
184 if (next) {
185 char c;
186 if (!isspace(*next))
187 return -1;
188 do {
189 c = *++next;
190 } while (isspace(c));
192 if (argv) {
193 ALLOC_GROW(*argv, *nr + 1, *alloc);
194 (*argv)[(*nr)++] = dequoted;
196 if (array)
197 strvec_push(array, dequoted);
198 } while (next);
200 return 0;
203 int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
205 return sq_dequote_to_argv_internal(arg, argv, nr, alloc, NULL);
208 int sq_dequote_to_strvec(char *arg, struct strvec *array)
210 return sq_dequote_to_argv_internal(arg, NULL, NULL, NULL, array);
213 /* 1 means: quote as octal
214 * 0 means: quote as octal if (quote_path_fully)
215 * -1 means: never quote
216 * c: quote as "\\c"
218 #define X8(x) x, x, x, x, x, x, x, x
219 #define X16(x) X8(x), X8(x)
220 static signed char const cq_lookup[256] = {
221 /* 0 1 2 3 4 5 6 7 */
222 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
223 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
224 /* 0x10 */ X16(1),
225 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
226 /* 0x28 */ X16(-1), X16(-1), X16(-1),
227 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
228 /* 0x60 */ X16(-1), X8(-1),
229 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
230 /* 0x80 */ /* set to 0 */
233 static inline int cq_must_quote(char c)
235 return cq_lookup[(unsigned char)c] + quote_path_fully > 0;
238 /* returns the longest prefix not needing a quote up to maxlen if positive.
239 This stops at the first \0 because it's marked as a character needing an
240 escape */
241 static size_t next_quote_pos(const char *s, ssize_t maxlen)
243 size_t len;
244 if (maxlen < 0) {
245 for (len = 0; !cq_must_quote(s[len]); len++);
246 } else {
247 for (len = 0; len < maxlen && !cq_must_quote(s[len]); len++);
249 return len;
253 * C-style name quoting.
255 * (1) if sb and fp are both NULL, inspect the input name and counts the
256 * number of bytes that are needed to hold c_style quoted version of name,
257 * counting the double quotes around it but not terminating NUL, and
258 * returns it.
259 * However, if name does not need c_style quoting, it returns 0.
261 * (2) if sb or fp are not NULL, it emits the c_style quoted version
262 * of name, enclosed with double quotes if asked and needed only.
263 * Return value is the same as in (1).
265 static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
266 struct strbuf *sb, FILE *fp, unsigned flags)
268 #undef EMIT
269 #define EMIT(c) \
270 do { \
271 if (sb) strbuf_addch(sb, (c)); \
272 if (fp) fputc((c), fp); \
273 count++; \
274 } while (0)
275 #define EMITBUF(s, l) \
276 do { \
277 if (sb) strbuf_add(sb, (s), (l)); \
278 if (fp) fwrite((s), (l), 1, fp); \
279 count += (l); \
280 } while (0)
282 int no_dq = !!(flags & CQUOTE_NODQ);
283 size_t len, count = 0;
284 const char *p = name;
286 for (;;) {
287 int ch;
289 len = next_quote_pos(p, maxlen);
290 if (len == maxlen || (maxlen < 0 && !p[len]))
291 break;
293 if (!no_dq && p == name)
294 EMIT('"');
296 EMITBUF(p, len);
297 EMIT('\\');
298 p += len;
299 ch = (unsigned char)*p++;
300 if (maxlen >= 0)
301 maxlen -= len + 1;
302 if (cq_lookup[ch] >= ' ') {
303 EMIT(cq_lookup[ch]);
304 } else {
305 EMIT(((ch >> 6) & 03) + '0');
306 EMIT(((ch >> 3) & 07) + '0');
307 EMIT(((ch >> 0) & 07) + '0');
311 EMITBUF(p, len);
312 if (p == name) /* no ending quote needed */
313 return 0;
315 if (!no_dq)
316 EMIT('"');
317 return count;
320 size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, unsigned flags)
322 return quote_c_style_counted(name, -1, sb, fp, flags);
325 void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path,
326 unsigned flags)
328 int nodq = !!(flags & CQUOTE_NODQ);
329 if (quote_c_style(prefix, NULL, NULL, 0) ||
330 quote_c_style(path, NULL, NULL, 0)) {
331 if (!nodq)
332 strbuf_addch(sb, '"');
333 quote_c_style(prefix, sb, NULL, CQUOTE_NODQ);
334 quote_c_style(path, sb, NULL, CQUOTE_NODQ);
335 if (!nodq)
336 strbuf_addch(sb, '"');
337 } else {
338 strbuf_addstr(sb, prefix);
339 strbuf_addstr(sb, path);
343 void write_name_quoted(const char *name, FILE *fp, int terminator)
345 if (terminator) {
346 quote_c_style(name, NULL, fp, 0);
347 } else {
348 fputs(name, fp);
350 fputc(terminator, fp);
353 void write_name_quoted_relative(const char *name, const char *prefix,
354 FILE *fp, int terminator)
356 struct strbuf sb = STRBUF_INIT;
358 name = relative_path(name, prefix, &sb);
359 write_name_quoted(name, fp, terminator);
361 strbuf_release(&sb);
364 /* quote path as relative to the given prefix */
365 char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigned flags)
367 struct strbuf sb = STRBUF_INIT;
368 const char *rel = relative_path(in, prefix, &sb);
369 int force_dq = ((flags & QUOTE_PATH_QUOTE_SP) && strchr(rel, ' '));
371 strbuf_reset(out);
374 * If the caller wants us to enclose the output in a dq-pair
375 * whether quote_c_style_counted() needs to, we do it ourselves
376 * and tell quote_c_style_counted() not to.
378 if (force_dq)
379 strbuf_addch(out, '"');
380 quote_c_style_counted(rel, strlen(rel), out, NULL,
381 force_dq ? CQUOTE_NODQ : 0);
382 if (force_dq)
383 strbuf_addch(out, '"');
384 strbuf_release(&sb);
386 return out->buf;
390 * C-style name unquoting.
392 * Quoted should point at the opening double quote.
393 * + Returns 0 if it was able to unquote the string properly, and appends the
394 * result in the strbuf `sb'.
395 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
396 * that this function will allocate memory in the strbuf, so calling
397 * strbuf_release is mandatory whichever result unquote_c_style returns.
399 * Updates endp pointer to point at one past the ending double quote if given.
401 int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
403 size_t oldlen = sb->len, len;
404 int ch, ac;
406 if (*quoted++ != '"')
407 return -1;
409 for (;;) {
410 len = strcspn(quoted, "\"\\");
411 strbuf_add(sb, quoted, len);
412 quoted += len;
414 switch (*quoted++) {
415 case '"':
416 if (endp)
417 *endp = quoted;
418 return 0;
419 case '\\':
420 break;
421 default:
422 goto error;
425 switch ((ch = *quoted++)) {
426 case 'a': ch = '\a'; break;
427 case 'b': ch = '\b'; break;
428 case 'f': ch = '\f'; break;
429 case 'n': ch = '\n'; break;
430 case 'r': ch = '\r'; break;
431 case 't': ch = '\t'; break;
432 case 'v': ch = '\v'; break;
434 case '\\': case '"':
435 break; /* verbatim */
437 /* octal values with first digit over 4 overflow */
438 case '0': case '1': case '2': case '3':
439 ac = ((ch - '0') << 6);
440 if ((ch = *quoted++) < '0' || '7' < ch)
441 goto error;
442 ac |= ((ch - '0') << 3);
443 if ((ch = *quoted++) < '0' || '7' < ch)
444 goto error;
445 ac |= (ch - '0');
446 ch = ac;
447 break;
448 default:
449 goto error;
451 strbuf_addch(sb, ch);
454 error:
455 strbuf_setlen(sb, oldlen);
456 return -1;
459 /* quoting as a string literal for other languages */
461 void perl_quote_buf(struct strbuf *sb, const char *src)
463 const char sq = '\'';
464 const char bq = '\\';
465 char c;
467 strbuf_addch(sb, sq);
468 while ((c = *src++)) {
469 if (c == sq || c == bq)
470 strbuf_addch(sb, bq);
471 strbuf_addch(sb, c);
473 strbuf_addch(sb, sq);
476 void perl_quote_buf_with_len(struct strbuf *sb, const char *src, size_t len)
478 const char sq = '\'';
479 const char bq = '\\';
480 const char *c = src;
481 const char *end = src + len;
483 strbuf_addch(sb, sq);
484 while (c != end) {
485 if (*c == sq || *c == bq)
486 strbuf_addch(sb, bq);
487 strbuf_addch(sb, *c);
488 c++;
490 strbuf_addch(sb, sq);
493 void python_quote_buf(struct strbuf *sb, const char *src)
495 const char sq = '\'';
496 const char bq = '\\';
497 const char nl = '\n';
498 char c;
500 strbuf_addch(sb, sq);
501 while ((c = *src++)) {
502 if (c == nl) {
503 strbuf_addch(sb, bq);
504 strbuf_addch(sb, 'n');
505 continue;
507 if (c == sq || c == bq)
508 strbuf_addch(sb, bq);
509 strbuf_addch(sb, c);
511 strbuf_addch(sb, sq);
514 void tcl_quote_buf(struct strbuf *sb, const char *src)
516 char c;
518 strbuf_addch(sb, '"');
519 while ((c = *src++)) {
520 switch (c) {
521 case '[': case ']':
522 case '{': case '}':
523 case '$': case '\\': case '"':
524 strbuf_addch(sb, '\\');
525 /* fallthrough */
526 default:
527 strbuf_addch(sb, c);
528 break;
529 case '\f':
530 strbuf_addstr(sb, "\\f");
531 break;
532 case '\r':
533 strbuf_addstr(sb, "\\r");
534 break;
535 case '\n':
536 strbuf_addstr(sb, "\\n");
537 break;
538 case '\t':
539 strbuf_addstr(sb, "\\t");
540 break;
541 case '\v':
542 strbuf_addstr(sb, "\\v");
543 break;
546 strbuf_addch(sb, '"');
549 void basic_regex_quote_buf(struct strbuf *sb, const char *src)
551 char c;
553 if (*src == '^') {
554 /* only beginning '^' is special and needs quoting */
555 strbuf_addch(sb, '\\');
556 strbuf_addch(sb, *src++);
558 if (*src == '*')
559 /* beginning '*' is not special, no quoting */
560 strbuf_addch(sb, *src++);
562 while ((c = *src++)) {
563 switch (c) {
564 case '[':
565 case '.':
566 case '\\':
567 case '*':
568 strbuf_addch(sb, '\\');
569 strbuf_addch(sb, c);
570 break;
572 case '$':
573 /* only the end '$' is special and needs quoting */
574 if (*src == '\0')
575 strbuf_addch(sb, '\\');
576 strbuf_addch(sb, c);
577 break;
579 default:
580 strbuf_addch(sb, c);
581 break;