1 #define DISABLE_SIGN_COMPARE_WARNINGS
3 #include "git-compat-util.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
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
)
33 to_free
= strbuf_detach(dst
, NULL
);
35 strbuf_addch(dst
, '\'');
37 size_t len
= strcspn(src
, "'!");
38 strbuf_add(dst
, 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
, '\'');
50 void sq_quote_buf_pretty(struct strbuf
*dst
, const char *src
)
52 static const char ok_punct
[] = "+,-./:=@_^";
55 /* Avoid losing a zero-length string by adding '' */
57 strbuf_addstr(dst
, "''");
61 for (p
= src
; *p
; p
++) {
62 if (!isalnum(*p
) && !strchr(ok_punct
, *p
)) {
63 sq_quote_buf(dst
, src
);
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
;
78 strbuf_vaddf(&src
, fmt
, ap
);
81 sq_quote_buf(dst
, src
.buf
);
85 void sq_quote_argv(struct strbuf
*dst
, const char **argv
)
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
)
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
)
116 for (i
= 0; argv
[i
]; i
++) {
118 strbuf_addch(dst
, ' ');
119 sq_quote_buf_pretty(dst
, argv
[i
]);
123 char *sq_dequote_step(char *arg
, char **next
)
139 /* We stepped out of sq */
148 * Allow backslashed characters outside of
149 * single-quotes only if they need escaping,
150 * and only if we resume the single-quoted part
153 if (need_bs_quote(src
[1]) && src
[2] == '\'') {
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
)
183 char *dequoted
= sq_dequote_step(next
, &next
);
192 } while (isspace(c
));
195 ALLOC_GROW(*argv
, *nr
+ 1, *alloc
);
196 (*argv
)[(*nr
)++] = dequoted
;
199 strvec_push(array
, dequoted
);
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
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,
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
243 static size_t next_quote_pos(const char *s
, ssize_t maxlen
)
247 for (len
= 0; !cq_must_quote(s
[len
]); len
++);
249 for (len
= 0; len
< maxlen
&& !cq_must_quote(s
[len
]); 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
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
)
273 if (sb) strbuf_addch(sb, (c)); \
274 if (fp) fputc((c), fp); \
277 #define EMITBUF(s, l) \
279 if (sb) strbuf_add(sb, (s), (l)); \
280 if (fp) fwrite((s), (l), 1, fp); \
284 int no_dq
= !!(flags
& CQUOTE_NODQ
);
285 size_t len
, count
= 0;
286 const char *p
= name
;
291 len
= next_quote_pos(p
, maxlen
);
292 if (len
== maxlen
|| (maxlen
< 0 && !p
[len
]))
295 if (!no_dq
&& p
== name
)
301 ch
= (unsigned char)*p
++;
304 if (cq_lookup
[ch
] >= ' ') {
307 EMIT(((ch
>> 6) & 03) + '0');
308 EMIT(((ch
>> 3) & 07) + '0');
309 EMIT(((ch
>> 0) & 07) + '0');
314 if (p
== name
) /* no ending quote needed */
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
,
330 int nodq
= !!(flags
& CQUOTE_NODQ
);
331 if (quote_c_style(prefix
, NULL
, NULL
, 0) ||
332 quote_c_style(path
, NULL
, NULL
, 0)) {
334 strbuf_addch(sb
, '"');
335 quote_c_style(prefix
, sb
, NULL
, CQUOTE_NODQ
);
336 quote_c_style(path
, sb
, NULL
, CQUOTE_NODQ
);
338 strbuf_addch(sb
, '"');
340 strbuf_addstr(sb
, prefix
);
341 strbuf_addstr(sb
, path
);
345 void write_name_quoted(const char *name
, FILE *fp
, int terminator
)
348 quote_c_style(name
, NULL
, fp
, 0);
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
);
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
, ' '));
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.
381 strbuf_addch(out
, '"');
382 quote_c_style_counted(rel
, strlen(rel
), out
, NULL
,
383 force_dq
? CQUOTE_NODQ
: 0);
385 strbuf_addch(out
, '"');
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
;
408 if (*quoted
++ != '"')
412 len
= strcspn(quoted
, "\"\\");
413 strbuf_add(sb
, quoted
, len
);
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;
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
)
444 ac
|= ((ch
- '0') << 3);
445 if ((ch
= *quoted
++) < '0' || '7' < ch
)
453 strbuf_addch(sb
, ch
);
457 strbuf_setlen(sb
, oldlen
);
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
= '\\';
469 strbuf_addch(sb
, sq
);
470 while ((c
= *src
++)) {
471 if (c
== sq
|| c
== bq
)
472 strbuf_addch(sb
, bq
);
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
= '\\';
483 const char *end
= src
+ len
;
485 strbuf_addch(sb
, sq
);
487 if (*c
== sq
|| *c
== bq
)
488 strbuf_addch(sb
, bq
);
489 strbuf_addch(sb
, *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';
502 strbuf_addch(sb
, sq
);
503 while ((c
= *src
++)) {
505 strbuf_addch(sb
, bq
);
506 strbuf_addch(sb
, 'n');
509 if (c
== sq
|| c
== bq
)
510 strbuf_addch(sb
, bq
);
513 strbuf_addch(sb
, sq
);
516 void tcl_quote_buf(struct strbuf
*sb
, const char *src
)
520 strbuf_addch(sb
, '"');
521 while ((c
= *src
++)) {
525 case '$': case '\\': case '"':
526 strbuf_addch(sb
, '\\');
532 strbuf_addstr(sb
, "\\f");
535 strbuf_addstr(sb
, "\\r");
538 strbuf_addstr(sb
, "\\n");
541 strbuf_addstr(sb
, "\\t");
544 strbuf_addstr(sb
, "\\v");
548 strbuf_addch(sb
, '"');
551 void basic_regex_quote_buf(struct strbuf
*sb
, const char *src
)
556 /* only beginning '^' is special and needs quoting */
557 strbuf_addch(sb
, '\\');
558 strbuf_addch(sb
, *src
++);
561 /* beginning '*' is not special, no quoting */
562 strbuf_addch(sb
, *src
++);
564 while ((c
= *src
++)) {
570 strbuf_addch(sb
, '\\');
575 /* only the end '$' is special and needs quoting */
577 strbuf_addch(sb
, '\\');