6 int quote_path_fully
= 1;
8 static inline int need_bs_quote(char c
)
10 return (c
== '\'' || c
== '!');
13 /* Help to copy the thing properly quoted for the shell safety.
14 * any single quote is replaced with '\'', any exclamation point
15 * is replaced with '\!', and the whole thing is enclosed in a
19 * original sq_quote result
20 * name ==> name ==> 'name'
21 * a b ==> a b ==> 'a b'
22 * a'b ==> a'\''b ==> 'a'\''b'
23 * a!b ==> a'\!'b ==> 'a'\!'b'
25 void sq_quote_buf(struct strbuf
*dst
, const char *src
)
30 to_free
= strbuf_detach(dst
, NULL
);
32 strbuf_addch(dst
, '\'');
34 size_t len
= strcspn(src
, "'!");
35 strbuf_add(dst
, src
, len
);
37 while (need_bs_quote(*src
)) {
38 strbuf_addstr(dst
, "'\\");
39 strbuf_addch(dst
, *src
++);
40 strbuf_addch(dst
, '\'');
43 strbuf_addch(dst
, '\'');
47 void sq_quote_buf_pretty(struct strbuf
*dst
, const char *src
)
49 static const char ok_punct
[] = "+,-./:=@_^";
52 /* Avoid losing a zero-length string by adding '' */
54 strbuf_addstr(dst
, "''");
58 for (p
= src
; *p
; p
++) {
59 if (!isalnum(*p
) && !strchr(ok_punct
, *p
)) {
60 sq_quote_buf(dst
, src
);
65 /* if we get here, we did not need quoting */
66 strbuf_addstr(dst
, src
);
69 void sq_quotef(struct strbuf
*dst
, const char *fmt
, ...)
71 struct strbuf src
= STRBUF_INIT
;
75 strbuf_vaddf(&src
, fmt
, ap
);
78 sq_quote_buf(dst
, src
.buf
);
82 void sq_quote_argv(struct strbuf
*dst
, const char **argv
)
86 /* Copy into destination buffer. */
87 strbuf_grow(dst
, 255);
88 for (i
= 0; argv
[i
]; ++i
) {
89 strbuf_addch(dst
, ' ');
90 sq_quote_buf(dst
, argv
[i
]);
95 * Legacy function to append each argv value, quoted as necessasry,
96 * with whitespace before each value. This results in a leading
97 * space in the result.
99 void sq_quote_argv_pretty(struct strbuf
*dst
, const char **argv
)
102 strbuf_addch(dst
, ' ');
103 sq_append_quote_argv_pretty(dst
, argv
);
107 * Append each argv value, quoted as necessary, with whitespace between them.
109 void sq_append_quote_argv_pretty(struct strbuf
*dst
, const char **argv
)
113 for (i
= 0; argv
[i
]; i
++) {
115 strbuf_addch(dst
, ' ');
116 sq_quote_buf_pretty(dst
, argv
[i
]);
120 char *sq_dequote_step(char *arg
, char **next
)
136 /* We stepped out of sq */
145 * Allow backslashed characters outside of
146 * single-quotes only if they need escaping,
147 * and only if we resume the single-quoted part
150 if (need_bs_quote(src
[1]) && src
[2] == '\'') {
166 char *sq_dequote(char *arg
)
168 return sq_dequote_step(arg
, NULL
);
171 static int sq_dequote_to_argv_internal(char *arg
,
172 const char ***argv
, int *nr
, int *alloc
,
173 struct strvec
*array
)
180 char *dequoted
= sq_dequote_step(next
, &next
);
189 } while (isspace(c
));
192 ALLOC_GROW(*argv
, *nr
+ 1, *alloc
);
193 (*argv
)[(*nr
)++] = dequoted
;
196 strvec_push(array
, dequoted
);
202 int sq_dequote_to_argv(char *arg
, const char ***argv
, int *nr
, int *alloc
)
204 return sq_dequote_to_argv_internal(arg
, argv
, nr
, alloc
, NULL
);
207 int sq_dequote_to_strvec(char *arg
, struct strvec
*array
)
209 return sq_dequote_to_argv_internal(arg
, NULL
, NULL
, NULL
, array
);
212 /* 1 means: quote as octal
213 * 0 means: quote as octal if (quote_path_fully)
214 * -1 means: never quote
217 #define X8(x) x, x, x, x, x, x, x, x
218 #define X16(x) X8(x), X8(x)
219 static signed char const cq_lookup
[256] = {
220 /* 0 1 2 3 4 5 6 7 */
221 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
222 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
224 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
225 /* 0x28 */ X16(-1), X16(-1), X16(-1),
226 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
227 /* 0x60 */ X16(-1), X8(-1),
228 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
229 /* 0x80 */ /* set to 0 */
232 static inline int cq_must_quote(char c
)
234 return cq_lookup
[(unsigned char)c
] + quote_path_fully
> 0;
237 /* returns the longest prefix not needing a quote up to maxlen if positive.
238 This stops at the first \0 because it's marked as a character needing an
240 static size_t next_quote_pos(const char *s
, ssize_t maxlen
)
244 for (len
= 0; !cq_must_quote(s
[len
]); len
++);
246 for (len
= 0; len
< maxlen
&& !cq_must_quote(s
[len
]); len
++);
252 * C-style name quoting.
254 * (1) if sb and fp are both NULL, inspect the input name and counts the
255 * number of bytes that are needed to hold c_style quoted version of name,
256 * counting the double quotes around it but not terminating NUL, and
258 * However, if name does not need c_style quoting, it returns 0.
260 * (2) if sb or fp are not NULL, it emits the c_style quoted version
261 * of name, enclosed with double quotes if asked and needed only.
262 * Return value is the same as in (1).
264 static size_t quote_c_style_counted(const char *name
, ssize_t maxlen
,
265 struct strbuf
*sb
, FILE *fp
, unsigned flags
)
270 if (sb) strbuf_addch(sb, (c)); \
271 if (fp) fputc((c), fp); \
274 #define EMITBUF(s, l) \
276 if (sb) strbuf_add(sb, (s), (l)); \
277 if (fp) fwrite((s), (l), 1, fp); \
281 int no_dq
= !!(flags
& CQUOTE_NODQ
);
282 size_t len
, count
= 0;
283 const char *p
= name
;
288 len
= next_quote_pos(p
, maxlen
);
289 if (len
== maxlen
|| (maxlen
< 0 && !p
[len
]))
292 if (!no_dq
&& p
== name
)
298 ch
= (unsigned char)*p
++;
301 if (cq_lookup
[ch
] >= ' ') {
304 EMIT(((ch
>> 6) & 03) + '0');
305 EMIT(((ch
>> 3) & 07) + '0');
306 EMIT(((ch
>> 0) & 07) + '0');
311 if (p
== name
) /* no ending quote needed */
319 size_t quote_c_style(const char *name
, struct strbuf
*sb
, FILE *fp
, unsigned flags
)
321 return quote_c_style_counted(name
, -1, sb
, fp
, flags
);
324 void quote_two_c_style(struct strbuf
*sb
, const char *prefix
, const char *path
,
327 int nodq
= !!(flags
& CQUOTE_NODQ
);
328 if (quote_c_style(prefix
, NULL
, NULL
, 0) ||
329 quote_c_style(path
, NULL
, NULL
, 0)) {
331 strbuf_addch(sb
, '"');
332 quote_c_style(prefix
, sb
, NULL
, CQUOTE_NODQ
);
333 quote_c_style(path
, sb
, NULL
, CQUOTE_NODQ
);
335 strbuf_addch(sb
, '"');
337 strbuf_addstr(sb
, prefix
);
338 strbuf_addstr(sb
, path
);
342 void write_name_quoted(const char *name
, FILE *fp
, int terminator
)
345 quote_c_style(name
, NULL
, fp
, 0);
349 fputc(terminator
, fp
);
352 void write_name_quoted_relative(const char *name
, const char *prefix
,
353 FILE *fp
, int terminator
)
355 struct strbuf sb
= STRBUF_INIT
;
357 name
= relative_path(name
, prefix
, &sb
);
358 write_name_quoted(name
, fp
, terminator
);
363 /* quote path as relative to the given prefix */
364 char *quote_path(const char *in
, const char *prefix
, struct strbuf
*out
, unsigned flags
)
366 struct strbuf sb
= STRBUF_INIT
;
367 const char *rel
= relative_path(in
, prefix
, &sb
);
368 int force_dq
= ((flags
& QUOTE_PATH_QUOTE_SP
) && strchr(rel
, ' '));
373 * If the caller wants us to enclose the output in a dq-pair
374 * whether quote_c_style_counted() needs to, we do it ourselves
375 * and tell quote_c_style_counted() not to.
378 strbuf_addch(out
, '"');
379 quote_c_style_counted(rel
, strlen(rel
), out
, NULL
,
380 force_dq
? CQUOTE_NODQ
: 0);
382 strbuf_addch(out
, '"');
389 * C-style name unquoting.
391 * Quoted should point at the opening double quote.
392 * + Returns 0 if it was able to unquote the string properly, and appends the
393 * result in the strbuf `sb'.
394 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
395 * that this function will allocate memory in the strbuf, so calling
396 * strbuf_release is mandatory whichever result unquote_c_style returns.
398 * Updates endp pointer to point at one past the ending double quote if given.
400 int unquote_c_style(struct strbuf
*sb
, const char *quoted
, const char **endp
)
402 size_t oldlen
= sb
->len
, len
;
405 if (*quoted
++ != '"')
409 len
= strcspn(quoted
, "\"\\");
410 strbuf_add(sb
, quoted
, len
);
424 switch ((ch
= *quoted
++)) {
425 case 'a': ch
= '\a'; break;
426 case 'b': ch
= '\b'; break;
427 case 'f': ch
= '\f'; break;
428 case 'n': ch
= '\n'; break;
429 case 'r': ch
= '\r'; break;
430 case 't': ch
= '\t'; break;
431 case 'v': ch
= '\v'; break;
434 break; /* verbatim */
436 /* octal values with first digit over 4 overflow */
437 case '0': case '1': case '2': case '3':
438 ac
= ((ch
- '0') << 6);
439 if ((ch
= *quoted
++) < '0' || '7' < ch
)
441 ac
|= ((ch
- '0') << 3);
442 if ((ch
= *quoted
++) < '0' || '7' < ch
)
450 strbuf_addch(sb
, ch
);
454 strbuf_setlen(sb
, oldlen
);
458 /* quoting as a string literal for other languages */
460 void perl_quote_buf(struct strbuf
*sb
, const char *src
)
462 const char sq
= '\'';
463 const char bq
= '\\';
466 strbuf_addch(sb
, sq
);
467 while ((c
= *src
++)) {
468 if (c
== sq
|| c
== bq
)
469 strbuf_addch(sb
, bq
);
472 strbuf_addch(sb
, sq
);
475 void perl_quote_buf_with_len(struct strbuf
*sb
, const char *src
, size_t len
)
477 const char sq
= '\'';
478 const char bq
= '\\';
480 const char *end
= src
+ len
;
482 strbuf_addch(sb
, sq
);
484 if (*c
== sq
|| *c
== bq
)
485 strbuf_addch(sb
, bq
);
486 strbuf_addch(sb
, *c
);
489 strbuf_addch(sb
, sq
);
492 void python_quote_buf(struct strbuf
*sb
, const char *src
)
494 const char sq
= '\'';
495 const char bq
= '\\';
496 const char nl
= '\n';
499 strbuf_addch(sb
, sq
);
500 while ((c
= *src
++)) {
502 strbuf_addch(sb
, bq
);
503 strbuf_addch(sb
, 'n');
506 if (c
== sq
|| c
== bq
)
507 strbuf_addch(sb
, bq
);
510 strbuf_addch(sb
, sq
);
513 void tcl_quote_buf(struct strbuf
*sb
, const char *src
)
517 strbuf_addch(sb
, '"');
518 while ((c
= *src
++)) {
522 case '$': case '\\': case '"':
523 strbuf_addch(sb
, '\\');
529 strbuf_addstr(sb
, "\\f");
532 strbuf_addstr(sb
, "\\r");
535 strbuf_addstr(sb
, "\\n");
538 strbuf_addstr(sb
, "\\t");
541 strbuf_addstr(sb
, "\\v");
545 strbuf_addch(sb
, '"');
548 void basic_regex_quote_buf(struct strbuf
*sb
, const char *src
)
553 /* only beginning '^' is special and needs quoting */
554 strbuf_addch(sb
, '\\');
555 strbuf_addch(sb
, *src
++);
558 /* beginning '*' is not special, no quoting */
559 strbuf_addch(sb
, *src
++);
561 while ((c
= *src
++)) {
567 strbuf_addch(sb
, '\\');
572 /* only the end '$' is special and needs quoting */
574 strbuf_addch(sb
, '\\');