]>
Commit | Line | Data |
---|---|---|
6fb737be JH |
1 | #include "cache.h" |
2 | #include "quote.h" | |
dbbcd44f | 3 | #include "strvec.h" |
6fb737be | 4 | |
5b8e6f85 DP |
5 | int quote_path_fully = 1; |
6 | ||
44cd91ea CC |
7 | static inline int need_bs_quote(char c) |
8 | { | |
9 | return (c == '\'' || c == '!'); | |
10 | } | |
11 | ||
6fb737be | 12 | /* Help to copy the thing properly quoted for the shell safety. |
77d604c3 PA |
13 | * any single quote is replaced with '\'', any exclamation point |
14 | * is replaced with '\!', and the whole thing is enclosed in a | |
ca9da0d8 | 15 | * single quote pair. |
6fb737be JH |
16 | * |
17 | * E.g. | |
18 | * original sq_quote result | |
19 | * name ==> name ==> 'name' | |
20 | * a b ==> a b ==> 'a b' | |
21 | * a'b ==> a'\''b ==> 'a'\''b' | |
77d604c3 | 22 | * a!b ==> a'\!'b ==> 'a'\!'b' |
6fb737be | 23 | */ |
7a33bcbe | 24 | void sq_quote_buf(struct strbuf *dst, const char *src) |
77d604c3 | 25 | { |
7a33bcbe PH |
26 | char *to_free = NULL; |
27 | ||
28 | if (dst->buf == src) | |
b315c5c0 | 29 | to_free = strbuf_detach(dst, NULL); |
7a33bcbe PH |
30 | |
31 | strbuf_addch(dst, '\''); | |
32 | while (*src) { | |
c2015b3a | 33 | size_t len = strcspn(src, "'!"); |
7a33bcbe PH |
34 | strbuf_add(dst, src, len); |
35 | src += len; | |
36 | while (need_bs_quote(*src)) { | |
37 | strbuf_addstr(dst, "'\\"); | |
38 | strbuf_addch(dst, *src++); | |
39 | strbuf_addch(dst, '\''); | |
6fb737be JH |
40 | } |
41 | } | |
7a33bcbe PH |
42 | strbuf_addch(dst, '\''); |
43 | free(to_free); | |
77d604c3 PA |
44 | } |
45 | ||
1fbdab21 JK |
46 | void sq_quote_buf_pretty(struct strbuf *dst, const char *src) |
47 | { | |
48 | static const char ok_punct[] = "+,-./:=@_^"; | |
49 | const char *p; | |
50 | ||
ce2d7ed2 GS |
51 | /* Avoid losing a zero-length string by adding '' */ |
52 | if (!*src) { | |
53 | strbuf_addstr(dst, "''"); | |
54 | return; | |
55 | } | |
56 | ||
1fbdab21 | 57 | for (p = src; *p; p++) { |
2b3c430b | 58 | if (!isalnum(*p) && !strchr(ok_punct, *p)) { |
1fbdab21 JK |
59 | sq_quote_buf(dst, src); |
60 | return; | |
61 | } | |
62 | } | |
63 | ||
64 | /* if we get here, we did not need quoting */ | |
65 | strbuf_addstr(dst, src); | |
66 | } | |
67 | ||
e70986d7 JK |
68 | void sq_quotef(struct strbuf *dst, const char *fmt, ...) |
69 | { | |
70 | struct strbuf src = STRBUF_INIT; | |
71 | ||
72 | va_list ap; | |
73 | va_start(ap, fmt); | |
74 | strbuf_vaddf(&src, fmt, ap); | |
75 | va_end(ap); | |
76 | ||
77 | sq_quote_buf(dst, src.buf); | |
78 | strbuf_release(&src); | |
79 | } | |
80 | ||
e35f11c2 | 81 | void sq_quote_argv(struct strbuf *dst, const char **argv) |
7cf67205 | 82 | { |
7cf67205 | 83 | int i; |
7cf67205 | 84 | |
7cf67205 | 85 | /* Copy into destination buffer. */ |
b319ce4c CC |
86 | strbuf_grow(dst, 255); |
87 | for (i = 0; argv[i]; ++i) { | |
7a33bcbe PH |
88 | strbuf_addch(dst, ' '); |
89 | sq_quote_buf(dst, argv[i]); | |
7cf67205 | 90 | } |
86257aa3 CC |
91 | } |
92 | ||
c2b890ac JH |
93 | /* |
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. | |
97 | */ | |
1fbdab21 | 98 | void sq_quote_argv_pretty(struct strbuf *dst, const char **argv) |
c2b890ac JH |
99 | { |
100 | if (argv[0]) | |
101 | strbuf_addch(dst, ' '); | |
102 | sq_append_quote_argv_pretty(dst, argv); | |
103 | } | |
104 | ||
105 | /* | |
106 | * Append each argv value, quoted as necessary, with whitespace between them. | |
107 | */ | |
108 | void sq_append_quote_argv_pretty(struct strbuf *dst, const char **argv) | |
1fbdab21 JK |
109 | { |
110 | int i; | |
111 | ||
112 | for (i = 0; argv[i]; i++) { | |
c2b890ac JH |
113 | if (i > 0) |
114 | strbuf_addch(dst, ' '); | |
1fbdab21 JK |
115 | sq_quote_buf_pretty(dst, argv[i]); |
116 | } | |
117 | } | |
118 | ||
13c44953 | 119 | char *sq_dequote_step(char *arg, char **next) |
35eb2d36 LT |
120 | { |
121 | char *dst = arg; | |
122 | char *src = arg; | |
123 | char c; | |
124 | ||
125 | if (*src != '\'') | |
126 | return NULL; | |
127 | for (;;) { | |
128 | c = *++src; | |
129 | if (!c) | |
130 | return NULL; | |
131 | if (c != '\'') { | |
132 | *dst++ = c; | |
133 | continue; | |
134 | } | |
135 | /* We stepped out of sq */ | |
136 | switch (*++src) { | |
137 | case '\0': | |
138 | *dst = 0; | |
ebbc088e CC |
139 | if (next) |
140 | *next = NULL; | |
35eb2d36 LT |
141 | return arg; |
142 | case '\\': | |
ddbbf8eb JK |
143 | /* |
144 | * Allow backslashed characters outside of | |
145 | * single-quotes only if they need escaping, | |
146 | * and only if we resume the single-quoted part | |
147 | * afterward. | |
148 | */ | |
149 | if (need_bs_quote(src[1]) && src[2] == '\'') { | |
150 | *dst++ = src[1]; | |
151 | src += 2; | |
35eb2d36 LT |
152 | continue; |
153 | } | |
154 | /* Fallthrough */ | |
155 | default: | |
13c44953 | 156 | if (!next) |
ebbc088e | 157 | return NULL; |
ebbc088e CC |
158 | *dst = 0; |
159 | *next = src; | |
160 | return arg; | |
35eb2d36 LT |
161 | } |
162 | } | |
163 | } | |
164 | ||
ebbc088e CC |
165 | char *sq_dequote(char *arg) |
166 | { | |
167 | return sq_dequote_step(arg, NULL); | |
168 | } | |
169 | ||
37e8161a JK |
170 | static int sq_dequote_to_argv_internal(char *arg, |
171 | const char ***argv, int *nr, int *alloc, | |
c972bf4c | 172 | struct strvec *array) |
eaa759b9 CC |
173 | { |
174 | char *next = arg; | |
175 | ||
176 | if (!*arg) | |
177 | return 0; | |
178 | do { | |
179 | char *dequoted = sq_dequote_step(next, &next); | |
180 | if (!dequoted) | |
181 | return -1; | |
13c44953 JK |
182 | if (next) { |
183 | char c; | |
184 | if (!isspace(*next)) | |
185 | return -1; | |
186 | do { | |
187 | c = *++next; | |
188 | } while (isspace(c)); | |
189 | } | |
37e8161a JK |
190 | if (argv) { |
191 | ALLOC_GROW(*argv, *nr + 1, *alloc); | |
192 | (*argv)[(*nr)++] = dequoted; | |
193 | } | |
194 | if (array) | |
c972bf4c | 195 | strvec_push(array, dequoted); |
eaa759b9 CC |
196 | } while (next); |
197 | ||
198 | return 0; | |
199 | } | |
200 | ||
37e8161a JK |
201 | int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc) |
202 | { | |
203 | return sq_dequote_to_argv_internal(arg, argv, nr, alloc, NULL); | |
204 | } | |
205 | ||
2745b6b4 | 206 | int sq_dequote_to_strvec(char *arg, struct strvec *array) |
37e8161a JK |
207 | { |
208 | return sq_dequote_to_argv_internal(arg, NULL, NULL, NULL, array); | |
209 | } | |
210 | ||
663af342 PH |
211 | /* 1 means: quote as octal |
212 | * 0 means: quote as octal if (quote_path_fully) | |
213 | * -1 means: never quote | |
214 | * c: quote as "\\c" | |
215 | */ | |
216 | #define X8(x) x, x, x, x, x, x, x, x | |
217 | #define X16(x) X8(x), X8(x) | |
dfc7f65c | 218 | static signed char const cq_lookup[256] = { |
663af342 PH |
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, | |
222 | /* 0x10 */ X16(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 */ | |
229 | }; | |
230 | ||
dfc7f65c | 231 | static inline int cq_must_quote(char c) |
f3fa1838 | 232 | { |
dfc7f65c | 233 | return cq_lookup[(unsigned char)c] + quote_path_fully > 0; |
663af342 PH |
234 | } |
235 | ||
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 | |
238 | escape */ | |
239 | static size_t next_quote_pos(const char *s, ssize_t maxlen) | |
240 | { | |
241 | size_t len; | |
242 | if (maxlen < 0) { | |
dfc7f65c | 243 | for (len = 0; !cq_must_quote(s[len]); len++); |
663af342 | 244 | } else { |
dfc7f65c | 245 | for (len = 0; len < maxlen && !cq_must_quote(s[len]); len++); |
663af342 PH |
246 | } |
247 | return len; | |
248 | } | |
249 | ||
4f6fbcdc JH |
250 | /* |
251 | * C-style name quoting. | |
252 | * | |
663af342 PH |
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 | |
256 | * returns it. | |
257 | * However, if name does not need c_style quoting, it returns 0. | |
4f6fbcdc | 258 | * |
663af342 PH |
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). | |
4f6fbcdc | 262 | */ |
663af342 | 263 | static size_t quote_c_style_counted(const char *name, ssize_t maxlen, |
7c37c975 | 264 | struct strbuf *sb, FILE *fp, unsigned flags) |
4f6fbcdc JH |
265 | { |
266 | #undef EMIT | |
663af342 PH |
267 | #define EMIT(c) \ |
268 | do { \ | |
269 | if (sb) strbuf_addch(sb, (c)); \ | |
270 | if (fp) fputc((c), fp); \ | |
271 | count++; \ | |
272 | } while (0) | |
273 | #define EMITBUF(s, l) \ | |
274 | do { \ | |
275 | if (sb) strbuf_add(sb, (s), (l)); \ | |
276 | if (fp) fwrite((s), (l), 1, fp); \ | |
277 | count += (l); \ | |
278 | } while (0) | |
279 | ||
7c37c975 | 280 | int no_dq = !!(flags & CQUOTE_NODQ); |
663af342 PH |
281 | size_t len, count = 0; |
282 | const char *p = name; | |
4f6fbcdc | 283 | |
663af342 PH |
284 | for (;;) { |
285 | int ch; | |
4f6fbcdc | 286 | |
663af342 | 287 | len = next_quote_pos(p, maxlen); |
84249819 | 288 | if (len == maxlen || (maxlen < 0 && !p[len])) |
50e7b067 | 289 | break; |
663af342 PH |
290 | |
291 | if (!no_dq && p == name) | |
292 | EMIT('"'); | |
293 | ||
294 | EMITBUF(p, len); | |
295 | EMIT('\\'); | |
296 | p += len; | |
297 | ch = (unsigned char)*p++; | |
84249819 JK |
298 | if (maxlen >= 0) |
299 | maxlen -= len + 1; | |
dfc7f65c JH |
300 | if (cq_lookup[ch] >= ' ') { |
301 | EMIT(cq_lookup[ch]); | |
663af342 PH |
302 | } else { |
303 | EMIT(((ch >> 6) & 03) + '0'); | |
304 | EMIT(((ch >> 3) & 07) + '0'); | |
305 | EMIT(((ch >> 0) & 07) + '0'); | |
4f6fbcdc | 306 | } |
4f6fbcdc | 307 | } |
663af342 PH |
308 | |
309 | EMITBUF(p, len); | |
310 | if (p == name) /* no ending quote needed */ | |
311 | return 0; | |
312 | ||
4f6fbcdc JH |
313 | if (!no_dq) |
314 | EMIT('"'); | |
663af342 PH |
315 | return count; |
316 | } | |
4f6fbcdc | 317 | |
7c37c975 | 318 | size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, unsigned flags) |
663af342 | 319 | { |
7c37c975 | 320 | return quote_c_style_counted(name, -1, sb, fp, flags); |
4f6fbcdc JH |
321 | } |
322 | ||
7c37c975 JH |
323 | void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, |
324 | unsigned flags) | |
d5625091 | 325 | { |
7c37c975 | 326 | int nodq = !!(flags & CQUOTE_NODQ); |
d5625091 JH |
327 | if (quote_c_style(prefix, NULL, NULL, 0) || |
328 | quote_c_style(path, NULL, NULL, 0)) { | |
329 | if (!nodq) | |
330 | strbuf_addch(sb, '"'); | |
7c37c975 JH |
331 | quote_c_style(prefix, sb, NULL, CQUOTE_NODQ); |
332 | quote_c_style(path, sb, NULL, CQUOTE_NODQ); | |
d5625091 JH |
333 | if (!nodq) |
334 | strbuf_addch(sb, '"'); | |
335 | } else { | |
336 | strbuf_addstr(sb, prefix); | |
337 | strbuf_addstr(sb, path); | |
338 | } | |
339 | } | |
340 | ||
663af342 | 341 | void write_name_quoted(const char *name, FILE *fp, int terminator) |
9ef2b3cb | 342 | { |
663af342 PH |
343 | if (terminator) { |
344 | quote_c_style(name, NULL, fp, 0); | |
345 | } else { | |
346 | fputs(name, fp); | |
347 | } | |
348 | fputc(terminator, fp); | |
349 | } | |
350 | ||
e9a820ce | 351 | void write_name_quoted_relative(const char *name, const char *prefix, |
b167cffb | 352 | FILE *fp, int terminator) |
a734d0b1 | 353 | { |
b167cffb CB |
354 | struct strbuf sb = STRBUF_INIT; |
355 | ||
ad66df2d | 356 | name = relative_path(name, prefix, &sb); |
b167cffb CB |
357 | write_name_quoted(name, fp, terminator); |
358 | ||
359 | strbuf_release(&sb); | |
360 | } | |
361 | ||
b167cffb | 362 | /* quote path as relative to the given prefix */ |
88910c99 | 363 | char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigned flags) |
b167cffb CB |
364 | { |
365 | struct strbuf sb = STRBUF_INIT; | |
ad66df2d | 366 | const char *rel = relative_path(in, prefix, &sb); |
e2773aa4 JH |
367 | int force_dq = ((flags & QUOTE_PATH_QUOTE_SP) && strchr(rel, ' ')); |
368 | ||
b167cffb | 369 | strbuf_reset(out); |
a734d0b1 | 370 | |
e2773aa4 JH |
371 | /* |
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. | |
375 | */ | |
376 | if (force_dq) | |
f3fc4a1b | 377 | strbuf_addch(out, '"'); |
7c37c975 JH |
378 | quote_c_style_counted(rel, strlen(rel), out, NULL, |
379 | force_dq ? CQUOTE_NODQ : 0); | |
e2773aa4 JH |
380 | if (force_dq) |
381 | strbuf_addch(out, '"'); | |
382 | strbuf_release(&sb); | |
f3fc4a1b | 383 | |
a734d0b1 DP |
384 | return out->buf; |
385 | } | |
386 | ||
4f6fbcdc JH |
387 | /* |
388 | * C-style name unquoting. | |
389 | * | |
7fb1011e PH |
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. | |
396 | * | |
397 | * Updates endp pointer to point at one past the ending double quote if given. | |
4f6fbcdc | 398 | */ |
7fb1011e | 399 | int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp) |
4f6fbcdc | 400 | { |
7fb1011e PH |
401 | size_t oldlen = sb->len, len; |
402 | int ch, ac; | |
4f6fbcdc JH |
403 | |
404 | if (*quoted++ != '"') | |
7fb1011e PH |
405 | return -1; |
406 | ||
407 | for (;;) { | |
408 | len = strcspn(quoted, "\"\\"); | |
409 | strbuf_add(sb, quoted, len); | |
410 | quoted += len; | |
4f6fbcdc | 411 | |
7fb1011e PH |
412 | switch (*quoted++) { |
413 | case '"': | |
414 | if (endp) | |
c8744d6a | 415 | *endp = quoted; |
7fb1011e PH |
416 | return 0; |
417 | case '\\': | |
418 | break; | |
419 | default: | |
420 | goto error; | |
421 | } | |
422 | ||
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; | |
431 | ||
432 | case '\\': case '"': | |
433 | break; /* verbatim */ | |
434 | ||
435 | /* octal values with first digit over 4 overflow */ | |
436 | case '0': case '1': case '2': case '3': | |
4f6fbcdc | 437 | ac = ((ch - '0') << 6); |
7fb1011e PH |
438 | if ((ch = *quoted++) < '0' || '7' < ch) |
439 | goto error; | |
4f6fbcdc | 440 | ac |= ((ch - '0') << 3); |
7fb1011e PH |
441 | if ((ch = *quoted++) < '0' || '7' < ch) |
442 | goto error; | |
4f6fbcdc JH |
443 | ac |= (ch - '0'); |
444 | ch = ac; | |
445 | break; | |
446 | default: | |
7fb1011e | 447 | goto error; |
4f6fbcdc | 448 | } |
7fb1011e | 449 | strbuf_addch(sb, ch); |
4f6fbcdc JH |
450 | } |
451 | ||
7fb1011e PH |
452 | error: |
453 | strbuf_setlen(sb, oldlen); | |
454 | return -1; | |
4f6fbcdc JH |
455 | } |
456 | ||
9f613ddd JH |
457 | /* quoting as a string literal for other languages */ |
458 | ||
10d0167f | 459 | void perl_quote_buf(struct strbuf *sb, const char *src) |
9f613ddd JH |
460 | { |
461 | const char sq = '\''; | |
462 | const char bq = '\\'; | |
463 | char c; | |
464 | ||
10d0167f | 465 | strbuf_addch(sb, sq); |
9f613ddd JH |
466 | while ((c = *src++)) { |
467 | if (c == sq || c == bq) | |
10d0167f NTND |
468 | strbuf_addch(sb, bq); |
469 | strbuf_addch(sb, c); | |
9f613ddd | 470 | } |
10d0167f | 471 | strbuf_addch(sb, sq); |
9f613ddd JH |
472 | } |
473 | ||
7121c4d4 ZH |
474 | void perl_quote_buf_with_len(struct strbuf *sb, const char *src, size_t len) |
475 | { | |
476 | const char sq = '\''; | |
477 | const char bq = '\\'; | |
478 | const char *c = src; | |
479 | const char *end = src + len; | |
480 | ||
481 | strbuf_addch(sb, sq); | |
482 | while (c != end) { | |
483 | if (*c == sq || *c == bq) | |
484 | strbuf_addch(sb, bq); | |
485 | strbuf_addch(sb, *c); | |
486 | c++; | |
487 | } | |
488 | strbuf_addch(sb, sq); | |
489 | } | |
490 | ||
10d0167f | 491 | void python_quote_buf(struct strbuf *sb, const char *src) |
9f613ddd JH |
492 | { |
493 | const char sq = '\''; | |
494 | const char bq = '\\'; | |
495 | const char nl = '\n'; | |
496 | char c; | |
497 | ||
10d0167f | 498 | strbuf_addch(sb, sq); |
9f613ddd JH |
499 | while ((c = *src++)) { |
500 | if (c == nl) { | |
10d0167f NTND |
501 | strbuf_addch(sb, bq); |
502 | strbuf_addch(sb, 'n'); | |
9f613ddd JH |
503 | continue; |
504 | } | |
505 | if (c == sq || c == bq) | |
10d0167f NTND |
506 | strbuf_addch(sb, bq); |
507 | strbuf_addch(sb, c); | |
9f613ddd | 508 | } |
10d0167f | 509 | strbuf_addch(sb, sq); |
9f613ddd | 510 | } |
5558e55c | 511 | |
10d0167f | 512 | void tcl_quote_buf(struct strbuf *sb, const char *src) |
5558e55c SP |
513 | { |
514 | char c; | |
515 | ||
10d0167f | 516 | strbuf_addch(sb, '"'); |
5558e55c SP |
517 | while ((c = *src++)) { |
518 | switch (c) { | |
519 | case '[': case ']': | |
520 | case '{': case '}': | |
521 | case '$': case '\\': case '"': | |
10d0167f | 522 | strbuf_addch(sb, '\\'); |
1cf01a34 | 523 | /* fallthrough */ |
5558e55c | 524 | default: |
10d0167f | 525 | strbuf_addch(sb, c); |
5558e55c SP |
526 | break; |
527 | case '\f': | |
10d0167f | 528 | strbuf_addstr(sb, "\\f"); |
5558e55c SP |
529 | break; |
530 | case '\r': | |
10d0167f | 531 | strbuf_addstr(sb, "\\r"); |
5558e55c SP |
532 | break; |
533 | case '\n': | |
10d0167f | 534 | strbuf_addstr(sb, "\\n"); |
5558e55c SP |
535 | break; |
536 | case '\t': | |
10d0167f | 537 | strbuf_addstr(sb, "\\t"); |
5558e55c SP |
538 | break; |
539 | case '\v': | |
10d0167f | 540 | strbuf_addstr(sb, "\\v"); |
5558e55c SP |
541 | break; |
542 | } | |
543 | } | |
10d0167f | 544 | strbuf_addch(sb, '"'); |
5558e55c | 545 | } |
793dc676 NTND |
546 | |
547 | void basic_regex_quote_buf(struct strbuf *sb, const char *src) | |
548 | { | |
549 | char c; | |
550 | ||
551 | if (*src == '^') { | |
552 | /* only beginning '^' is special and needs quoting */ | |
553 | strbuf_addch(sb, '\\'); | |
554 | strbuf_addch(sb, *src++); | |
555 | } | |
556 | if (*src == '*') | |
557 | /* beginning '*' is not special, no quoting */ | |
558 | strbuf_addch(sb, *src++); | |
559 | ||
560 | while ((c = *src++)) { | |
561 | switch (c) { | |
562 | case '[': | |
563 | case '.': | |
564 | case '\\': | |
565 | case '*': | |
566 | strbuf_addch(sb, '\\'); | |
567 | strbuf_addch(sb, c); | |
568 | break; | |
569 | ||
570 | case '$': | |
571 | /* only the end '$' is special and needs quoting */ | |
572 | if (*src == '\0') | |
573 | strbuf_addch(sb, '\\'); | |
574 | strbuf_addch(sb, c); | |
575 | break; | |
576 | ||
577 | default: | |
578 | strbuf_addch(sb, c); | |
579 | break; | |
580 | } | |
581 | } | |
582 | } |