5 int quote_path_fully
= 1;
7 static inline int need_bs_quote(char c
)
9 return (c
== '\'' || c
== '!');
12 /* Help to copy the thing properly quoted for the shell safety.
13 * any single quote is replaced with '\'', any exclamation point
14 * is replaced with '\!', and the whole thing is enclosed in a
18 * original sq_quote result
19 * name ==> name ==> 'name'
20 * a b ==> a b ==> 'a b'
21 * a'b ==> a'\''b ==> 'a'\''b'
22 * a!b ==> a'\!'b ==> 'a'\!'b'
24 void sq_quote_buf(struct strbuf
*dst
, const char *src
)
29 to_free
= strbuf_detach(dst
, NULL
);
31 strbuf_addch(dst
, '\'');
33 size_t len
= strcspn(src
, "'!");
34 strbuf_add(dst
, src
, len
);
36 while (need_bs_quote(*src
)) {
37 strbuf_addstr(dst
, "'\\");
38 strbuf_addch(dst
, *src
++);
39 strbuf_addch(dst
, '\'');
42 strbuf_addch(dst
, '\'');
46 void sq_quote_buf_pretty(struct strbuf
*dst
, const char *src
)
48 static const char ok_punct
[] = "+,-./:=@_^";
51 /* Avoid losing a zero-length string by adding '' */
53 strbuf_addstr(dst
, "''");
57 for (p
= src
; *p
; p
++) {
58 if (!isalnum(*p
) && !strchr(ok_punct
, *p
)) {
59 sq_quote_buf(dst
, src
);
64 /* if we get here, we did not need quoting */
65 strbuf_addstr(dst
, src
);
68 void sq_quotef(struct strbuf
*dst
, const char *fmt
, ...)
70 struct strbuf src
= STRBUF_INIT
;
74 strbuf_vaddf(&src
, fmt
, ap
);
77 sq_quote_buf(dst
, src
.buf
);
81 void sq_quote_argv(struct strbuf
*dst
, const char **argv
)
85 /* Copy into destination buffer. */
86 strbuf_grow(dst
, 255);
87 for (i
= 0; argv
[i
]; ++i
) {
88 strbuf_addch(dst
, ' ');
89 sq_quote_buf(dst
, argv
[i
]);
94 * Legacy function to append each argv value, quoted as necessasry,
95 * with whitespace before each value. This results in a leading
96 * space in the result.
98 void sq_quote_argv_pretty(struct strbuf
*dst
, const char **argv
)
101 strbuf_addch(dst
, ' ');
102 sq_append_quote_argv_pretty(dst
, argv
);
106 * Append each argv value, quoted as necessary, with whitespace between them.
108 void sq_append_quote_argv_pretty(struct strbuf
*dst
, const char **argv
)
112 for (i
= 0; argv
[i
]; i
++) {
114 strbuf_addch(dst
, ' ');
115 sq_quote_buf_pretty(dst
, argv
[i
]);
119 char *sq_dequote_step(char *arg
, char **next
)
135 /* We stepped out of sq */
144 * Allow backslashed characters outside of
145 * single-quotes only if they need escaping,
146 * and only if we resume the single-quoted part
149 if (need_bs_quote(src
[1]) && src
[2] == '\'') {
165 char *sq_dequote(char *arg
)
167 return sq_dequote_step(arg
, NULL
);
170 static int sq_dequote_to_argv_internal(char *arg
,
171 const char ***argv
, int *nr
, int *alloc
,
172 struct strvec
*array
)
179 char *dequoted
= sq_dequote_step(next
, &next
);
188 } while (isspace(c
));
191 ALLOC_GROW(*argv
, *nr
+ 1, *alloc
);
192 (*argv
)[(*nr
)++] = dequoted
;
195 strvec_push(array
, dequoted
);
201 int sq_dequote_to_argv(char *arg
, const char ***argv
, int *nr
, int *alloc
)
203 return sq_dequote_to_argv_internal(arg
, argv
, nr
, alloc
, NULL
);
206 int sq_dequote_to_strvec(char *arg
, struct strvec
*array
)
208 return sq_dequote_to_argv_internal(arg
, NULL
, NULL
, NULL
, array
);
211 /* 1 means: quote as octal
212 * 0 means: quote as octal if (quote_path_fully)
213 * -1 means: never quote
216 #define X8(x) x, x, x, x, x, x, x, x
217 #define X16(x) X8(x), X8(x)
218 static signed char const cq_lookup
[256] = {
219 /* 0 1 2 3 4 5 6 7 */
220 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
221 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
223 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
224 /* 0x28 */ X16(-1), X16(-1), X16(-1),
225 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
226 /* 0x60 */ X16(-1), X8(-1),
227 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
228 /* 0x80 */ /* set to 0 */
231 static inline int cq_must_quote(char c
)
233 return cq_lookup
[(unsigned char)c
] + quote_path_fully
> 0;
236 /* returns the longest prefix not needing a quote up to maxlen if positive.
237 This stops at the first \0 because it's marked as a character needing an
239 static size_t next_quote_pos(const char *s
, ssize_t maxlen
)
243 for (len
= 0; !cq_must_quote(s
[len
]); len
++);
245 for (len
= 0; len
< maxlen
&& !cq_must_quote(s
[len
]); len
++);
251 * C-style name quoting.
253 * (1) if sb and fp are both NULL, inspect the input name and counts the
254 * number of bytes that are needed to hold c_style quoted version of name,
255 * counting the double quotes around it but not terminating NUL, and
257 * However, if name does not need c_style quoting, it returns 0.
259 * (2) if sb or fp are not NULL, it emits the c_style quoted version
260 * of name, enclosed with double quotes if asked and needed only.
261 * Return value is the same as in (1).
263 static size_t quote_c_style_counted(const char *name
, ssize_t maxlen
,
264 struct strbuf
*sb
, FILE *fp
, unsigned flags
)
269 if (sb) strbuf_addch(sb, (c)); \
270 if (fp) fputc((c), fp); \
273 #define EMITBUF(s, l) \
275 if (sb) strbuf_add(sb, (s), (l)); \
276 if (fp) fwrite((s), (l), 1, fp); \
280 int no_dq
= !!(flags
& CQUOTE_NODQ
);
281 size_t len
, count
= 0;
282 const char *p
= name
;
287 len
= next_quote_pos(p
, maxlen
);
288 if (len
== maxlen
|| (maxlen
< 0 && !p
[len
]))
291 if (!no_dq
&& p
== name
)
297 ch
= (unsigned char)*p
++;
300 if (cq_lookup
[ch
] >= ' ') {
303 EMIT(((ch
>> 6) & 03) + '0');
304 EMIT(((ch
>> 3) & 07) + '0');
305 EMIT(((ch
>> 0) & 07) + '0');
310 if (p
== name
) /* no ending quote needed */
318 size_t quote_c_style(const char *name
, struct strbuf
*sb
, FILE *fp
, unsigned flags
)
320 return quote_c_style_counted(name
, -1, sb
, fp
, flags
);
323 void quote_two_c_style(struct strbuf
*sb
, const char *prefix
, const char *path
,
326 int nodq
= !!(flags
& CQUOTE_NODQ
);
327 if (quote_c_style(prefix
, NULL
, NULL
, 0) ||
328 quote_c_style(path
, NULL
, NULL
, 0)) {
330 strbuf_addch(sb
, '"');
331 quote_c_style(prefix
, sb
, NULL
, CQUOTE_NODQ
);
332 quote_c_style(path
, sb
, NULL
, CQUOTE_NODQ
);
334 strbuf_addch(sb
, '"');
336 strbuf_addstr(sb
, prefix
);
337 strbuf_addstr(sb
, path
);
341 void write_name_quoted(const char *name
, FILE *fp
, int terminator
)
344 quote_c_style(name
, NULL
, fp
, 0);
348 fputc(terminator
, fp
);
351 void write_name_quoted_relative(const char *name
, const char *prefix
,
352 FILE *fp
, int terminator
)
354 struct strbuf sb
= STRBUF_INIT
;
356 name
= relative_path(name
, prefix
, &sb
);
357 write_name_quoted(name
, fp
, terminator
);
362 /* quote path as relative to the given prefix */
363 char *quote_path(const char *in
, const char *prefix
, struct strbuf
*out
, unsigned flags
)
365 struct strbuf sb
= STRBUF_INIT
;
366 const char *rel
= relative_path(in
, prefix
, &sb
);
367 int force_dq
= ((flags
& QUOTE_PATH_QUOTE_SP
) && strchr(rel
, ' '));
372 * If the caller wants us to enclose the output in a dq-pair
373 * whether quote_c_style_counted() needs to, we do it ourselves
374 * and tell quote_c_style_counted() not to.
377 strbuf_addch(out
, '"');
378 quote_c_style_counted(rel
, strlen(rel
), out
, NULL
,
379 force_dq
? CQUOTE_NODQ
: 0);
381 strbuf_addch(out
, '"');
388 * C-style name unquoting.
390 * Quoted should point at the opening double quote.
391 * + Returns 0 if it was able to unquote the string properly, and appends the
392 * result in the strbuf `sb'.
393 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
394 * that this function will allocate memory in the strbuf, so calling
395 * strbuf_release is mandatory whichever result unquote_c_style returns.
397 * Updates endp pointer to point at one past the ending double quote if given.
399 int unquote_c_style(struct strbuf
*sb
, const char *quoted
, const char **endp
)
401 size_t oldlen
= sb
->len
, len
;
404 if (*quoted
++ != '"')
408 len
= strcspn(quoted
, "\"\\");
409 strbuf_add(sb
, quoted
, len
);
423 switch ((ch
= *quoted
++)) {
424 case 'a': ch
= '\a'; break;
425 case 'b': ch
= '\b'; break;
426 case 'f': ch
= '\f'; break;
427 case 'n': ch
= '\n'; break;
428 case 'r': ch
= '\r'; break;
429 case 't': ch
= '\t'; break;
430 case 'v': ch
= '\v'; break;
433 break; /* verbatim */
435 /* octal values with first digit over 4 overflow */
436 case '0': case '1': case '2': case '3':
437 ac
= ((ch
- '0') << 6);
438 if ((ch
= *quoted
++) < '0' || '7' < ch
)
440 ac
|= ((ch
- '0') << 3);
441 if ((ch
= *quoted
++) < '0' || '7' < ch
)
449 strbuf_addch(sb
, ch
);
453 strbuf_setlen(sb
, oldlen
);
457 /* quoting as a string literal for other languages */
459 void perl_quote_buf(struct strbuf
*sb
, const char *src
)
461 const char sq
= '\'';
462 const char bq
= '\\';
465 strbuf_addch(sb
, sq
);
466 while ((c
= *src
++)) {
467 if (c
== sq
|| c
== bq
)
468 strbuf_addch(sb
, bq
);
471 strbuf_addch(sb
, sq
);
474 void python_quote_buf(struct strbuf
*sb
, const char *src
)
476 const char sq
= '\'';
477 const char bq
= '\\';
478 const char nl
= '\n';
481 strbuf_addch(sb
, sq
);
482 while ((c
= *src
++)) {
484 strbuf_addch(sb
, bq
);
485 strbuf_addch(sb
, 'n');
488 if (c
== sq
|| c
== bq
)
489 strbuf_addch(sb
, bq
);
492 strbuf_addch(sb
, sq
);
495 void tcl_quote_buf(struct strbuf
*sb
, const char *src
)
499 strbuf_addch(sb
, '"');
500 while ((c
= *src
++)) {
504 case '$': case '\\': case '"':
505 strbuf_addch(sb
, '\\');
511 strbuf_addstr(sb
, "\\f");
514 strbuf_addstr(sb
, "\\r");
517 strbuf_addstr(sb
, "\\n");
520 strbuf_addstr(sb
, "\\t");
523 strbuf_addstr(sb
, "\\v");
527 strbuf_addch(sb
, '"');
530 void basic_regex_quote_buf(struct strbuf
*sb
, const char *src
)
535 /* only beginning '^' is special and needs quoting */
536 strbuf_addch(sb
, '\\');
537 strbuf_addch(sb
, *src
++);
540 /* beginning '*' is not special, no quoting */
541 strbuf_addch(sb
, *src
++);
543 while ((c
= *src
++)) {
549 strbuf_addch(sb
, '\\');
554 /* only the end '$' is special and needs quoting */
556 strbuf_addch(sb
, '\\');