quote_path: rename quote_path_relative() to quote_path()
[git] / quote.c
1 #include "cache.h"
2 #include "quote.h"
3 #include "strvec.h"
4
5 int quote_path_fully = 1;
6
7 static inline int need_bs_quote(char c)
8 {
9         return (c == '\'' || c == '!');
10 }
11
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
15  * single quote pair.
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'
22  *  a!b      ==> a'\!'b    ==> 'a'\!'b'
23  */
24 void sq_quote_buf(struct strbuf *dst, const char *src)
25 {
26         char *to_free = NULL;
27
28         if (dst->buf == src)
29                 to_free = strbuf_detach(dst, NULL);
30
31         strbuf_addch(dst, '\'');
32         while (*src) {
33                 size_t len = strcspn(src, "'!");
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, '\'');
40                 }
41         }
42         strbuf_addch(dst, '\'');
43         free(to_free);
44 }
45
46 void sq_quote_buf_pretty(struct strbuf *dst, const char *src)
47 {
48         static const char ok_punct[] = "+,-./:=@_^";
49         const char *p;
50
51         /* Avoid losing a zero-length string by adding '' */
52         if (!*src) {
53                 strbuf_addstr(dst, "''");
54                 return;
55         }
56
57         for (p = src; *p; p++) {
58                 if (!isalnum(*p) && !strchr(ok_punct, *p)) {
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
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
81 void sq_quote_argv(struct strbuf *dst, const char **argv)
82 {
83         int i;
84
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]);
90         }
91 }
92
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  */
98 void sq_quote_argv_pretty(struct strbuf *dst, const char **argv)
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)
109 {
110         int i;
111
112         for (i = 0; argv[i]; i++) {
113                 if (i > 0)
114                         strbuf_addch(dst, ' ');
115                 sq_quote_buf_pretty(dst, argv[i]);
116         }
117 }
118
119 static char *sq_dequote_step(char *arg, char **next)
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;
139                         if (next)
140                                 *next = NULL;
141                         return arg;
142                 case '\\':
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;
152                                 continue;
153                         }
154                 /* Fallthrough */
155                 default:
156                         if (!next || !isspace(*src))
157                                 return NULL;
158                         do {
159                                 c = *++src;
160                         } while (isspace(c));
161                         *dst = 0;
162                         *next = src;
163                         return arg;
164                 }
165         }
166 }
167
168 char *sq_dequote(char *arg)
169 {
170         return sq_dequote_step(arg, NULL);
171 }
172
173 static int sq_dequote_to_argv_internal(char *arg,
174                                        const char ***argv, int *nr, int *alloc,
175                                        struct strvec *array)
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;
185                 if (argv) {
186                         ALLOC_GROW(*argv, *nr + 1, *alloc);
187                         (*argv)[(*nr)++] = dequoted;
188                 }
189                 if (array)
190                         strvec_push(array, dequoted);
191         } while (next);
192
193         return 0;
194 }
195
196 int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
197 {
198         return sq_dequote_to_argv_internal(arg, argv, nr, alloc, NULL);
199 }
200
201 int sq_dequote_to_strvec(char *arg, struct strvec *array)
202 {
203         return sq_dequote_to_argv_internal(arg, NULL, NULL, NULL, array);
204 }
205
206 /* 1 means: quote as octal
207  * 0 means: quote as octal if (quote_path_fully)
208  * -1 means: never quote
209  * c: quote as "\\c"
210  */
211 #define X8(x)   x, x, x, x, x, x, x, x
212 #define X16(x)  X8(x), X8(x)
213 static signed char const sq_lookup[256] = {
214         /*           0    1    2    3    4    5    6    7 */
215         /* 0x00 */   1,   1,   1,   1,   1,   1,   1, 'a',
216         /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r',   1,   1,
217         /* 0x10 */ X16(1),
218         /* 0x20 */  -1,  -1, '"',  -1,  -1,  -1,  -1,  -1,
219         /* 0x28 */ X16(-1), X16(-1), X16(-1),
220         /* 0x58 */  -1,  -1,  -1,  -1,'\\',  -1,  -1,  -1,
221         /* 0x60 */ X16(-1), X8(-1),
222         /* 0x78 */  -1,  -1,  -1,  -1,  -1,  -1,  -1,   1,
223         /* 0x80 */ /* set to 0 */
224 };
225
226 static inline int sq_must_quote(char c)
227 {
228         return sq_lookup[(unsigned char)c] + quote_path_fully > 0;
229 }
230
231 /* returns the longest prefix not needing a quote up to maxlen if positive.
232    This stops at the first \0 because it's marked as a character needing an
233    escape */
234 static size_t next_quote_pos(const char *s, ssize_t maxlen)
235 {
236         size_t len;
237         if (maxlen < 0) {
238                 for (len = 0; !sq_must_quote(s[len]); len++);
239         } else {
240                 for (len = 0; len < maxlen && !sq_must_quote(s[len]); len++);
241         }
242         return len;
243 }
244
245 /*
246  * C-style name quoting.
247  *
248  * (1) if sb and fp are both NULL, inspect the input name and counts the
249  *     number of bytes that are needed to hold c_style quoted version of name,
250  *     counting the double quotes around it but not terminating NUL, and
251  *     returns it.
252  *     However, if name does not need c_style quoting, it returns 0.
253  *
254  * (2) if sb or fp are not NULL, it emits the c_style quoted version
255  *     of name, enclosed with double quotes if asked and needed only.
256  *     Return value is the same as in (1).
257  */
258 static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
259                                     struct strbuf *sb, FILE *fp, int no_dq)
260 {
261 #undef EMIT
262 #define EMIT(c)                                 \
263         do {                                        \
264                 if (sb) strbuf_addch(sb, (c));          \
265                 if (fp) fputc((c), fp);                 \
266                 count++;                                \
267         } while (0)
268 #define EMITBUF(s, l)                           \
269         do {                                        \
270                 if (sb) strbuf_add(sb, (s), (l));       \
271                 if (fp) fwrite((s), (l), 1, fp);        \
272                 count += (l);                           \
273         } while (0)
274
275         size_t len, count = 0;
276         const char *p = name;
277
278         for (;;) {
279                 int ch;
280
281                 len = next_quote_pos(p, maxlen);
282                 if (len == maxlen || (maxlen < 0 && !p[len]))
283                         break;
284
285                 if (!no_dq && p == name)
286                         EMIT('"');
287
288                 EMITBUF(p, len);
289                 EMIT('\\');
290                 p += len;
291                 ch = (unsigned char)*p++;
292                 if (maxlen >= 0)
293                         maxlen -= len + 1;
294                 if (sq_lookup[ch] >= ' ') {
295                         EMIT(sq_lookup[ch]);
296                 } else {
297                         EMIT(((ch >> 6) & 03) + '0');
298                         EMIT(((ch >> 3) & 07) + '0');
299                         EMIT(((ch >> 0) & 07) + '0');
300                 }
301         }
302
303         EMITBUF(p, len);
304         if (p == name)   /* no ending quote needed */
305                 return 0;
306
307         if (!no_dq)
308                 EMIT('"');
309         return count;
310 }
311
312 size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
313 {
314         return quote_c_style_counted(name, -1, sb, fp, nodq);
315 }
316
317 void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, int nodq)
318 {
319         if (quote_c_style(prefix, NULL, NULL, 0) ||
320             quote_c_style(path, NULL, NULL, 0)) {
321                 if (!nodq)
322                         strbuf_addch(sb, '"');
323                 quote_c_style(prefix, sb, NULL, 1);
324                 quote_c_style(path, sb, NULL, 1);
325                 if (!nodq)
326                         strbuf_addch(sb, '"');
327         } else {
328                 strbuf_addstr(sb, prefix);
329                 strbuf_addstr(sb, path);
330         }
331 }
332
333 void write_name_quoted(const char *name, FILE *fp, int terminator)
334 {
335         if (terminator) {
336                 quote_c_style(name, NULL, fp, 0);
337         } else {
338                 fputs(name, fp);
339         }
340         fputc(terminator, fp);
341 }
342
343 void write_name_quoted_relative(const char *name, const char *prefix,
344                                 FILE *fp, int terminator)
345 {
346         struct strbuf sb = STRBUF_INIT;
347
348         name = relative_path(name, prefix, &sb);
349         write_name_quoted(name, fp, terminator);
350
351         strbuf_release(&sb);
352 }
353
354 /* quote path as relative to the given prefix */
355 char *quote_path(const char *in, const char *prefix, struct strbuf *out)
356 {
357         struct strbuf sb = STRBUF_INIT;
358         const char *rel = relative_path(in, prefix, &sb);
359         strbuf_reset(out);
360         quote_c_style_counted(rel, strlen(rel), out, NULL, 0);
361         strbuf_release(&sb);
362
363         return out->buf;
364 }
365
366 /*
367  * C-style name unquoting.
368  *
369  * Quoted should point at the opening double quote.
370  * + Returns 0 if it was able to unquote the string properly, and appends the
371  *   result in the strbuf `sb'.
372  * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
373  *   that this function will allocate memory in the strbuf, so calling
374  *   strbuf_release is mandatory whichever result unquote_c_style returns.
375  *
376  * Updates endp pointer to point at one past the ending double quote if given.
377  */
378 int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
379 {
380         size_t oldlen = sb->len, len;
381         int ch, ac;
382
383         if (*quoted++ != '"')
384                 return -1;
385
386         for (;;) {
387                 len = strcspn(quoted, "\"\\");
388                 strbuf_add(sb, quoted, len);
389                 quoted += len;
390
391                 switch (*quoted++) {
392                   case '"':
393                         if (endp)
394                                 *endp = quoted;
395                         return 0;
396                   case '\\':
397                         break;
398                   default:
399                         goto error;
400                 }
401
402                 switch ((ch = *quoted++)) {
403                 case 'a': ch = '\a'; break;
404                 case 'b': ch = '\b'; break;
405                 case 'f': ch = '\f'; break;
406                 case 'n': ch = '\n'; break;
407                 case 'r': ch = '\r'; break;
408                 case 't': ch = '\t'; break;
409                 case 'v': ch = '\v'; break;
410
411                 case '\\': case '"':
412                         break; /* verbatim */
413
414                 /* octal values with first digit over 4 overflow */
415                 case '0': case '1': case '2': case '3':
416                                         ac = ((ch - '0') << 6);
417                         if ((ch = *quoted++) < '0' || '7' < ch)
418                                 goto error;
419                                         ac |= ((ch - '0') << 3);
420                         if ((ch = *quoted++) < '0' || '7' < ch)
421                                 goto error;
422                                         ac |= (ch - '0');
423                                         ch = ac;
424                                         break;
425                                 default:
426                         goto error;
427                         }
428                 strbuf_addch(sb, ch);
429                 }
430
431   error:
432         strbuf_setlen(sb, oldlen);
433         return -1;
434 }
435
436 /* quoting as a string literal for other languages */
437
438 void perl_quote_buf(struct strbuf *sb, const char *src)
439 {
440         const char sq = '\'';
441         const char bq = '\\';
442         char c;
443
444         strbuf_addch(sb, sq);
445         while ((c = *src++)) {
446                 if (c == sq || c == bq)
447                         strbuf_addch(sb, bq);
448                 strbuf_addch(sb, c);
449         }
450         strbuf_addch(sb, sq);
451 }
452
453 void python_quote_buf(struct strbuf *sb, const char *src)
454 {
455         const char sq = '\'';
456         const char bq = '\\';
457         const char nl = '\n';
458         char c;
459
460         strbuf_addch(sb, sq);
461         while ((c = *src++)) {
462                 if (c == nl) {
463                         strbuf_addch(sb, bq);
464                         strbuf_addch(sb, 'n');
465                         continue;
466                 }
467                 if (c == sq || c == bq)
468                         strbuf_addch(sb, bq);
469                 strbuf_addch(sb, c);
470         }
471         strbuf_addch(sb, sq);
472 }
473
474 void tcl_quote_buf(struct strbuf *sb, const char *src)
475 {
476         char c;
477
478         strbuf_addch(sb, '"');
479         while ((c = *src++)) {
480                 switch (c) {
481                 case '[': case ']':
482                 case '{': case '}':
483                 case '$': case '\\': case '"':
484                         strbuf_addch(sb, '\\');
485                         /* fallthrough */
486                 default:
487                         strbuf_addch(sb, c);
488                         break;
489                 case '\f':
490                         strbuf_addstr(sb, "\\f");
491                         break;
492                 case '\r':
493                         strbuf_addstr(sb, "\\r");
494                         break;
495                 case '\n':
496                         strbuf_addstr(sb, "\\n");
497                         break;
498                 case '\t':
499                         strbuf_addstr(sb, "\\t");
500                         break;
501                 case '\v':
502                         strbuf_addstr(sb, "\\v");
503                         break;
504                 }
505         }
506         strbuf_addch(sb, '"');
507 }
508
509 void basic_regex_quote_buf(struct strbuf *sb, const char *src)
510 {
511         char c;
512
513         if (*src == '^') {
514                 /* only beginning '^' is special and needs quoting */
515                 strbuf_addch(sb, '\\');
516                 strbuf_addch(sb, *src++);
517         }
518         if (*src == '*')
519                 /* beginning '*' is not special, no quoting */
520                 strbuf_addch(sb, *src++);
521
522         while ((c = *src++)) {
523                 switch (c) {
524                 case '[':
525                 case '.':
526                 case '\\':
527                 case '*':
528                         strbuf_addch(sb, '\\');
529                         strbuf_addch(sb, c);
530                         break;
531
532                 case '$':
533                         /* only the end '$' is special and needs quoting */
534                         if (*src == '\0')
535                                 strbuf_addch(sb, '\\');
536                         strbuf_addch(sb, c);
537                         break;
538
539                 default:
540                         strbuf_addch(sb, c);
541                         break;
542                 }
543         }
544 }