1 #include "git-compat-util.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
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
)
31 to_free
= strbuf_detach(dst
, NULL
);
33 strbuf_addch(dst
, '\'');
35 size_t len
= strcspn(src
, "'!");
36 strbuf_add(dst
, 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
, '\'');
48 void sq_quote_buf_pretty(struct strbuf
*dst
, const char *src
)
50 static const char ok_punct
[] = "+,-./:=@_^";
53 /* Avoid losing a zero-length string by adding '' */
55 strbuf_addstr(dst
, "''");
59 for (p
= src
; *p
; p
++) {
60 if (!isalnum(*p
) && !strchr(ok_punct
, *p
)) {
61 sq_quote_buf(dst
, src
);
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
;
76 strbuf_vaddf(&src
, fmt
, ap
);
79 sq_quote_buf(dst
, src
.buf
);
83 void sq_quote_argv(struct strbuf
*dst
, const char **argv
)
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
)
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
)
114 for (i
= 0; argv
[i
]; i
++) {
116 strbuf_addch(dst
, ' ');
117 sq_quote_buf_pretty(dst
, argv
[i
]);
121 char *sq_dequote_step(char *arg
, char **next
)
137 /* We stepped out of sq */
146 * Allow backslashed characters outside of
147 * single-quotes only if they need escaping,
148 * and only if we resume the single-quoted part
151 if (need_bs_quote(src
[1]) && src
[2] == '\'') {
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
)
181 char *dequoted
= sq_dequote_step(next
, &next
);
190 } while (isspace(c
));
193 ALLOC_GROW(*argv
, *nr
+ 1, *alloc
);
194 (*argv
)[(*nr
)++] = dequoted
;
197 strvec_push(array
, dequoted
);
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
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,
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
241 static size_t next_quote_pos(const char *s
, ssize_t maxlen
)
245 for (len
= 0; !cq_must_quote(s
[len
]); len
++);
247 for (len
= 0; len
< maxlen
&& !cq_must_quote(s
[len
]); 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
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
)
271 if (sb) strbuf_addch(sb, (c)); \
272 if (fp) fputc((c), fp); \
275 #define EMITBUF(s, l) \
277 if (sb) strbuf_add(sb, (s), (l)); \
278 if (fp) fwrite((s), (l), 1, fp); \
282 int no_dq
= !!(flags
& CQUOTE_NODQ
);
283 size_t len
, count
= 0;
284 const char *p
= name
;
289 len
= next_quote_pos(p
, maxlen
);
290 if (len
== maxlen
|| (maxlen
< 0 && !p
[len
]))
293 if (!no_dq
&& p
== name
)
299 ch
= (unsigned char)*p
++;
302 if (cq_lookup
[ch
] >= ' ') {
305 EMIT(((ch
>> 6) & 03) + '0');
306 EMIT(((ch
>> 3) & 07) + '0');
307 EMIT(((ch
>> 0) & 07) + '0');
312 if (p
== name
) /* no ending quote needed */
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
,
328 int nodq
= !!(flags
& CQUOTE_NODQ
);
329 if (quote_c_style(prefix
, NULL
, NULL
, 0) ||
330 quote_c_style(path
, NULL
, NULL
, 0)) {
332 strbuf_addch(sb
, '"');
333 quote_c_style(prefix
, sb
, NULL
, CQUOTE_NODQ
);
334 quote_c_style(path
, sb
, NULL
, CQUOTE_NODQ
);
336 strbuf_addch(sb
, '"');
338 strbuf_addstr(sb
, prefix
);
339 strbuf_addstr(sb
, path
);
343 void write_name_quoted(const char *name
, FILE *fp
, int terminator
)
346 quote_c_style(name
, NULL
, fp
, 0);
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
);
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
, ' '));
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.
379 strbuf_addch(out
, '"');
380 quote_c_style_counted(rel
, strlen(rel
), out
, NULL
,
381 force_dq
? CQUOTE_NODQ
: 0);
383 strbuf_addch(out
, '"');
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
;
406 if (*quoted
++ != '"')
410 len
= strcspn(quoted
, "\"\\");
411 strbuf_add(sb
, quoted
, len
);
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;
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
)
442 ac
|= ((ch
- '0') << 3);
443 if ((ch
= *quoted
++) < '0' || '7' < ch
)
451 strbuf_addch(sb
, ch
);
455 strbuf_setlen(sb
, oldlen
);
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
= '\\';
467 strbuf_addch(sb
, sq
);
468 while ((c
= *src
++)) {
469 if (c
== sq
|| c
== bq
)
470 strbuf_addch(sb
, bq
);
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
= '\\';
481 const char *end
= src
+ len
;
483 strbuf_addch(sb
, sq
);
485 if (*c
== sq
|| *c
== bq
)
486 strbuf_addch(sb
, bq
);
487 strbuf_addch(sb
, *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';
500 strbuf_addch(sb
, sq
);
501 while ((c
= *src
++)) {
503 strbuf_addch(sb
, bq
);
504 strbuf_addch(sb
, 'n');
507 if (c
== sq
|| c
== bq
)
508 strbuf_addch(sb
, bq
);
511 strbuf_addch(sb
, sq
);
514 void tcl_quote_buf(struct strbuf
*sb
, const char *src
)
518 strbuf_addch(sb
, '"');
519 while ((c
= *src
++)) {
523 case '$': case '\\': case '"':
524 strbuf_addch(sb
, '\\');
530 strbuf_addstr(sb
, "\\f");
533 strbuf_addstr(sb
, "\\r");
536 strbuf_addstr(sb
, "\\n");
539 strbuf_addstr(sb
, "\\t");
542 strbuf_addstr(sb
, "\\v");
546 strbuf_addch(sb
, '"');
549 void basic_regex_quote_buf(struct strbuf
*sb
, const char *src
)
554 /* only beginning '^' is special and needs quoting */
555 strbuf_addch(sb
, '\\');
556 strbuf_addch(sb
, *src
++);
559 /* beginning '*' is not special, no quoting */
560 strbuf_addch(sb
, *src
++);
562 while ((c
= *src
++)) {
568 strbuf_addch(sb
, '\\');
573 /* only the end '$' is special and needs quoting */
575 strbuf_addch(sb
, '\\');