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