1 #include "git-compat-util.h"
8 int quote_path_fully
= 1;
10 static inline int need_bs_quote(char c
)
12 return (c
== '\'' || c
== '!');
15 /* Help to copy the thing properly quoted for the shell safety.
16 * any single quote is replaced with '\'', any exclamation point
17 * is replaced with '\!', and the whole thing is enclosed in a
21 * original sq_quote result
22 * name ==> name ==> 'name'
23 * a b ==> a b ==> 'a b'
24 * a'b ==> a'\''b ==> 'a'\''b'
25 * a!b ==> a'\!'b ==> 'a'\!'b'
27 void sq_quote_buf(struct strbuf
*dst
, const char *src
)
32 to_free
= strbuf_detach(dst
, NULL
);
34 strbuf_addch(dst
, '\'');
36 size_t len
= strcspn(src
, "'!");
37 strbuf_add(dst
, src
, len
);
39 while (need_bs_quote(*src
)) {
40 strbuf_addstr(dst
, "'\\");
41 strbuf_addch(dst
, *src
++);
42 strbuf_addch(dst
, '\'');
45 strbuf_addch(dst
, '\'');
49 void sq_quote_buf_pretty(struct strbuf
*dst
, const char *src
)
51 static const char ok_punct
[] = "+,-./:=@_^";
54 /* Avoid losing a zero-length string by adding '' */
56 strbuf_addstr(dst
, "''");
60 for (p
= src
; *p
; p
++) {
61 if (!isalnum(*p
) && !strchr(ok_punct
, *p
)) {
62 sq_quote_buf(dst
, src
);
67 /* if we get here, we did not need quoting */
68 strbuf_addstr(dst
, src
);
71 void sq_quotef(struct strbuf
*dst
, const char *fmt
, ...)
73 struct strbuf src
= STRBUF_INIT
;
77 strbuf_vaddf(&src
, fmt
, ap
);
80 sq_quote_buf(dst
, src
.buf
);
84 void sq_quote_argv(struct strbuf
*dst
, const char **argv
)
88 /* Copy into destination buffer. */
89 strbuf_grow(dst
, 255);
90 for (i
= 0; argv
[i
]; ++i
) {
91 strbuf_addch(dst
, ' ');
92 sq_quote_buf(dst
, argv
[i
]);
97 * Legacy function to append each argv value, quoted as necessasry,
98 * with whitespace before each value. This results in a leading
99 * space in the result.
101 void sq_quote_argv_pretty(struct strbuf
*dst
, const char **argv
)
104 strbuf_addch(dst
, ' ');
105 sq_append_quote_argv_pretty(dst
, argv
);
109 * Append each argv value, quoted as necessary, with whitespace between them.
111 void sq_append_quote_argv_pretty(struct strbuf
*dst
, const char **argv
)
115 for (i
= 0; argv
[i
]; i
++) {
117 strbuf_addch(dst
, ' ');
118 sq_quote_buf_pretty(dst
, argv
[i
]);
122 char *sq_dequote_step(char *arg
, char **next
)
138 /* We stepped out of sq */
147 * Allow backslashed characters outside of
148 * single-quotes only if they need escaping,
149 * and only if we resume the single-quoted part
152 if (need_bs_quote(src
[1]) && src
[2] == '\'') {
168 char *sq_dequote(char *arg
)
170 return sq_dequote_step(arg
, NULL
);
173 static int sq_dequote_to_argv_internal(char *arg
,
174 const char ***argv
, int *nr
, int *alloc
,
175 struct strvec
*array
)
182 char *dequoted
= sq_dequote_step(next
, &next
);
191 } while (isspace(c
));
194 ALLOC_GROW(*argv
, *nr
+ 1, *alloc
);
195 (*argv
)[(*nr
)++] = dequoted
;
198 strvec_push(array
, dequoted
);
204 int sq_dequote_to_argv(char *arg
, const char ***argv
, int *nr
, int *alloc
)
206 return sq_dequote_to_argv_internal(arg
, argv
, nr
, alloc
, NULL
);
209 int sq_dequote_to_strvec(char *arg
, struct strvec
*array
)
211 return sq_dequote_to_argv_internal(arg
, NULL
, NULL
, NULL
, array
);
214 /* 1 means: quote as octal
215 * 0 means: quote as octal if (quote_path_fully)
216 * -1 means: never quote
219 #define X8(x) x, x, x, x, x, x, x, x
220 #define X16(x) X8(x), X8(x)
221 static signed char const cq_lookup
[256] = {
222 /* 0 1 2 3 4 5 6 7 */
223 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
224 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
226 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
227 /* 0x28 */ X16(-1), X16(-1), X16(-1),
228 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
229 /* 0x60 */ X16(-1), X8(-1),
230 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
231 /* 0x80 */ /* set to 0 */
234 static inline int cq_must_quote(char c
)
236 return cq_lookup
[(unsigned char)c
] + quote_path_fully
> 0;
239 /* returns the longest prefix not needing a quote up to maxlen if positive.
240 This stops at the first \0 because it's marked as a character needing an
242 static size_t next_quote_pos(const char *s
, ssize_t maxlen
)
246 for (len
= 0; !cq_must_quote(s
[len
]); len
++);
248 for (len
= 0; len
< maxlen
&& !cq_must_quote(s
[len
]); len
++);
254 * C-style name quoting.
256 * (1) if sb and fp are both NULL, inspect the input name and counts the
257 * number of bytes that are needed to hold c_style quoted version of name,
258 * counting the double quotes around it but not terminating NUL, and
260 * However, if name does not need c_style quoting, it returns 0.
262 * (2) if sb or fp are not NULL, it emits the c_style quoted version
263 * of name, enclosed with double quotes if asked and needed only.
264 * Return value is the same as in (1).
266 static size_t quote_c_style_counted(const char *name
, ssize_t maxlen
,
267 struct strbuf
*sb
, FILE *fp
, unsigned flags
)
272 if (sb) strbuf_addch(sb, (c)); \
273 if (fp) fputc((c), fp); \
276 #define EMITBUF(s, l) \
278 if (sb) strbuf_add(sb, (s), (l)); \
279 if (fp) fwrite((s), (l), 1, fp); \
283 int no_dq
= !!(flags
& CQUOTE_NODQ
);
284 size_t len
, count
= 0;
285 const char *p
= name
;
290 len
= next_quote_pos(p
, maxlen
);
291 if (len
== maxlen
|| (maxlen
< 0 && !p
[len
]))
294 if (!no_dq
&& p
== name
)
300 ch
= (unsigned char)*p
++;
303 if (cq_lookup
[ch
] >= ' ') {
306 EMIT(((ch
>> 6) & 03) + '0');
307 EMIT(((ch
>> 3) & 07) + '0');
308 EMIT(((ch
>> 0) & 07) + '0');
313 if (p
== name
) /* no ending quote needed */
321 size_t quote_c_style(const char *name
, struct strbuf
*sb
, FILE *fp
, unsigned flags
)
323 return quote_c_style_counted(name
, -1, sb
, fp
, flags
);
326 void quote_two_c_style(struct strbuf
*sb
, const char *prefix
, const char *path
,
329 int nodq
= !!(flags
& CQUOTE_NODQ
);
330 if (quote_c_style(prefix
, NULL
, NULL
, 0) ||
331 quote_c_style(path
, NULL
, NULL
, 0)) {
333 strbuf_addch(sb
, '"');
334 quote_c_style(prefix
, sb
, NULL
, CQUOTE_NODQ
);
335 quote_c_style(path
, sb
, NULL
, CQUOTE_NODQ
);
337 strbuf_addch(sb
, '"');
339 strbuf_addstr(sb
, prefix
);
340 strbuf_addstr(sb
, path
);
344 void write_name_quoted(const char *name
, FILE *fp
, int terminator
)
347 quote_c_style(name
, NULL
, fp
, 0);
351 fputc(terminator
, fp
);
354 void write_name_quoted_relative(const char *name
, const char *prefix
,
355 FILE *fp
, int terminator
)
357 struct strbuf sb
= STRBUF_INIT
;
359 name
= relative_path(name
, prefix
, &sb
);
360 write_name_quoted(name
, fp
, terminator
);
365 /* quote path as relative to the given prefix */
366 char *quote_path(const char *in
, const char *prefix
, struct strbuf
*out
, unsigned flags
)
368 struct strbuf sb
= STRBUF_INIT
;
369 const char *rel
= relative_path(in
, prefix
, &sb
);
370 int force_dq
= ((flags
& QUOTE_PATH_QUOTE_SP
) && strchr(rel
, ' '));
375 * If the caller wants us to enclose the output in a dq-pair
376 * whether quote_c_style_counted() needs to, we do it ourselves
377 * and tell quote_c_style_counted() not to.
380 strbuf_addch(out
, '"');
381 quote_c_style_counted(rel
, strlen(rel
), out
, NULL
,
382 force_dq
? CQUOTE_NODQ
: 0);
384 strbuf_addch(out
, '"');
391 * C-style name unquoting.
393 * Quoted should point at the opening double quote.
394 * + Returns 0 if it was able to unquote the string properly, and appends the
395 * result in the strbuf `sb'.
396 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
397 * that this function will allocate memory in the strbuf, so calling
398 * strbuf_release is mandatory whichever result unquote_c_style returns.
400 * Updates endp pointer to point at one past the ending double quote if given.
402 int unquote_c_style(struct strbuf
*sb
, const char *quoted
, const char **endp
)
404 size_t oldlen
= sb
->len
, len
;
407 if (*quoted
++ != '"')
411 len
= strcspn(quoted
, "\"\\");
412 strbuf_add(sb
, quoted
, len
);
426 switch ((ch
= *quoted
++)) {
427 case 'a': ch
= '\a'; break;
428 case 'b': ch
= '\b'; break;
429 case 'f': ch
= '\f'; break;
430 case 'n': ch
= '\n'; break;
431 case 'r': ch
= '\r'; break;
432 case 't': ch
= '\t'; break;
433 case 'v': ch
= '\v'; break;
436 break; /* verbatim */
438 /* octal values with first digit over 4 overflow */
439 case '0': case '1': case '2': case '3':
440 ac
= ((ch
- '0') << 6);
441 if ((ch
= *quoted
++) < '0' || '7' < ch
)
443 ac
|= ((ch
- '0') << 3);
444 if ((ch
= *quoted
++) < '0' || '7' < ch
)
452 strbuf_addch(sb
, ch
);
456 strbuf_setlen(sb
, oldlen
);
460 /* quoting as a string literal for other languages */
462 void perl_quote_buf(struct strbuf
*sb
, const char *src
)
464 const char sq
= '\'';
465 const char bq
= '\\';
468 strbuf_addch(sb
, sq
);
469 while ((c
= *src
++)) {
470 if (c
== sq
|| c
== bq
)
471 strbuf_addch(sb
, bq
);
474 strbuf_addch(sb
, sq
);
477 void perl_quote_buf_with_len(struct strbuf
*sb
, const char *src
, size_t len
)
479 const char sq
= '\'';
480 const char bq
= '\\';
482 const char *end
= src
+ len
;
484 strbuf_addch(sb
, sq
);
486 if (*c
== sq
|| *c
== bq
)
487 strbuf_addch(sb
, bq
);
488 strbuf_addch(sb
, *c
);
491 strbuf_addch(sb
, sq
);
494 void python_quote_buf(struct strbuf
*sb
, const char *src
)
496 const char sq
= '\'';
497 const char bq
= '\\';
498 const char nl
= '\n';
501 strbuf_addch(sb
, sq
);
502 while ((c
= *src
++)) {
504 strbuf_addch(sb
, bq
);
505 strbuf_addch(sb
, 'n');
508 if (c
== sq
|| c
== bq
)
509 strbuf_addch(sb
, bq
);
512 strbuf_addch(sb
, sq
);
515 void tcl_quote_buf(struct strbuf
*sb
, const char *src
)
519 strbuf_addch(sb
, '"');
520 while ((c
= *src
++)) {
524 case '$': case '\\': case '"':
525 strbuf_addch(sb
, '\\');
531 strbuf_addstr(sb
, "\\f");
534 strbuf_addstr(sb
, "\\r");
537 strbuf_addstr(sb
, "\\n");
540 strbuf_addstr(sb
, "\\t");
543 strbuf_addstr(sb
, "\\v");
547 strbuf_addch(sb
, '"');
550 void basic_regex_quote_buf(struct strbuf
*sb
, const char *src
)
555 /* only beginning '^' is special and needs quoting */
556 strbuf_addch(sb
, '\\');
557 strbuf_addch(sb
, *src
++);
560 /* beginning '*' is not special, no quoting */
561 strbuf_addch(sb
, *src
++);
563 while ((c
= *src
++)) {
569 strbuf_addch(sb
, '\\');
574 /* only the end '$' is special and needs quoting */
576 strbuf_addch(sb
, '\\');