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