]> git.ipfire.org Git - thirdparty/git.git/blame - quote.c
quote.c: substitute path_relative with relative_path
[thirdparty/git.git] / quote.c
CommitLineData
6fb737be
JH
1#include "cache.h"
2#include "quote.h"
37e8161a 3#include "argv-array.h"
6fb737be 4
5b8e6f85
DP
5int quote_path_fully = 1;
6
6fb737be 7/* Help to copy the thing properly quoted for the shell safety.
77d604c3
PA
8 * any single quote is replaced with '\'', any exclamation point
9 * is replaced with '\!', and the whole thing is enclosed in a
6fb737be
JH
10 *
11 * E.g.
12 * original sq_quote result
13 * name ==> name ==> 'name'
14 * a b ==> a b ==> 'a b'
15 * a'b ==> a'\''b ==> 'a'\''b'
77d604c3 16 * a!b ==> a'\!'b ==> 'a'\!'b'
6fb737be 17 */
35eb2d36
LT
18static inline int need_bs_quote(char c)
19{
20 return (c == '\'' || c == '!');
21}
22
7a33bcbe 23void sq_quote_buf(struct strbuf *dst, const char *src)
77d604c3 24{
7a33bcbe
PH
25 char *to_free = NULL;
26
27 if (dst->buf == src)
b315c5c0 28 to_free = strbuf_detach(dst, NULL);
7a33bcbe
PH
29
30 strbuf_addch(dst, '\'');
31 while (*src) {
c2015b3a 32 size_t len = strcspn(src, "'!");
7a33bcbe
PH
33 strbuf_add(dst, src, len);
34 src += len;
35 while (need_bs_quote(*src)) {
36 strbuf_addstr(dst, "'\\");
37 strbuf_addch(dst, *src++);
38 strbuf_addch(dst, '\'');
6fb737be
JH
39 }
40 }
7a33bcbe
PH
41 strbuf_addch(dst, '\'');
42 free(to_free);
77d604c3
PA
43}
44
575ba9d6
ML
45void sq_quote_print(FILE *stream, const char *src)
46{
47 char c;
48
49 fputc('\'', stream);
50 while ((c = *src++)) {
51 if (need_bs_quote(c)) {
52 fputs("'\\", stream);
53 fputc(c, stream);
54 fputc('\'', stream);
55 } else {
56 fputc(c, stream);
57 }
58 }
59 fputc('\'', stream);
60}
61
b319ce4c 62void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
7cf67205 63{
7cf67205 64 int i;
7cf67205 65
7cf67205 66 /* Copy into destination buffer. */
b319ce4c
CC
67 strbuf_grow(dst, 255);
68 for (i = 0; argv[i]; ++i) {
7a33bcbe
PH
69 strbuf_addch(dst, ' ');
70 sq_quote_buf(dst, argv[i]);
71 if (maxlen && dst->len > maxlen)
72 die("Too many or long arguments");
7cf67205 73 }
86257aa3
CC
74}
75
758e915b 76static char *sq_dequote_step(char *arg, char **next)
35eb2d36
LT
77{
78 char *dst = arg;
79 char *src = arg;
80 char c;
81
82 if (*src != '\'')
83 return NULL;
84 for (;;) {
85 c = *++src;
86 if (!c)
87 return NULL;
88 if (c != '\'') {
89 *dst++ = c;
90 continue;
91 }
92 /* We stepped out of sq */
93 switch (*++src) {
94 case '\0':
95 *dst = 0;
ebbc088e
CC
96 if (next)
97 *next = NULL;
35eb2d36
LT
98 return arg;
99 case '\\':
100 c = *++src;
101 if (need_bs_quote(c) && *++src == '\'') {
102 *dst++ = c;
103 continue;
104 }
105 /* Fallthrough */
106 default:
ebbc088e
CC
107 if (!next || !isspace(*src))
108 return NULL;
109 do {
110 c = *++src;
111 } while (isspace(c));
112 *dst = 0;
113 *next = src;
114 return arg;
35eb2d36
LT
115 }
116 }
117}
118
ebbc088e
CC
119char *sq_dequote(char *arg)
120{
121 return sq_dequote_step(arg, NULL);
122}
123
37e8161a
JK
124static int sq_dequote_to_argv_internal(char *arg,
125 const char ***argv, int *nr, int *alloc,
126 struct argv_array *array)
eaa759b9
CC
127{
128 char *next = arg;
129
130 if (!*arg)
131 return 0;
132 do {
133 char *dequoted = sq_dequote_step(next, &next);
134 if (!dequoted)
135 return -1;
37e8161a
JK
136 if (argv) {
137 ALLOC_GROW(*argv, *nr + 1, *alloc);
138 (*argv)[(*nr)++] = dequoted;
139 }
140 if (array)
141 argv_array_push(array, dequoted);
eaa759b9
CC
142 } while (next);
143
144 return 0;
145}
146
37e8161a
JK
147int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
148{
149 return sq_dequote_to_argv_internal(arg, argv, nr, alloc, NULL);
150}
151
152int sq_dequote_to_argv_array(char *arg, struct argv_array *array)
153{
154 return sq_dequote_to_argv_internal(arg, NULL, NULL, NULL, array);
155}
156
663af342
PH
157/* 1 means: quote as octal
158 * 0 means: quote as octal if (quote_path_fully)
159 * -1 means: never quote
160 * c: quote as "\\c"
161 */
162#define X8(x) x, x, x, x, x, x, x, x
163#define X16(x) X8(x), X8(x)
164static signed char const sq_lookup[256] = {
165 /* 0 1 2 3 4 5 6 7 */
166 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
167 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
168 /* 0x10 */ X16(1),
169 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
170 /* 0x28 */ X16(-1), X16(-1), X16(-1),
171 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
172 /* 0x60 */ X16(-1), X8(-1),
173 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
174 /* 0x80 */ /* set to 0 */
175};
176
f3fa1838
JH
177static inline int sq_must_quote(char c)
178{
663af342
PH
179 return sq_lookup[(unsigned char)c] + quote_path_fully > 0;
180}
181
182/* returns the longest prefix not needing a quote up to maxlen if positive.
183 This stops at the first \0 because it's marked as a character needing an
184 escape */
185static size_t next_quote_pos(const char *s, ssize_t maxlen)
186{
187 size_t len;
188 if (maxlen < 0) {
189 for (len = 0; !sq_must_quote(s[len]); len++);
190 } else {
191 for (len = 0; len < maxlen && !sq_must_quote(s[len]); len++);
192 }
193 return len;
194}
195
4f6fbcdc
JH
196/*
197 * C-style name quoting.
198 *
663af342
PH
199 * (1) if sb and fp are both NULL, inspect the input name and counts the
200 * number of bytes that are needed to hold c_style quoted version of name,
201 * counting the double quotes around it but not terminating NUL, and
202 * returns it.
203 * However, if name does not need c_style quoting, it returns 0.
4f6fbcdc 204 *
663af342
PH
205 * (2) if sb or fp are not NULL, it emits the c_style quoted version
206 * of name, enclosed with double quotes if asked and needed only.
207 * Return value is the same as in (1).
4f6fbcdc 208 */
663af342
PH
209static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
210 struct strbuf *sb, FILE *fp, int no_dq)
4f6fbcdc
JH
211{
212#undef EMIT
663af342
PH
213#define EMIT(c) \
214 do { \
215 if (sb) strbuf_addch(sb, (c)); \
216 if (fp) fputc((c), fp); \
217 count++; \
218 } while (0)
219#define EMITBUF(s, l) \
220 do { \
221 if (sb) strbuf_add(sb, (s), (l)); \
222 if (fp) fwrite((s), (l), 1, fp); \
223 count += (l); \
224 } while (0)
225
226 size_t len, count = 0;
227 const char *p = name;
4f6fbcdc 228
663af342
PH
229 for (;;) {
230 int ch;
4f6fbcdc 231
663af342 232 len = next_quote_pos(p, maxlen);
84249819 233 if (len == maxlen || (maxlen < 0 && !p[len]))
50e7b067 234 break;
663af342
PH
235
236 if (!no_dq && p == name)
237 EMIT('"');
238
239 EMITBUF(p, len);
240 EMIT('\\');
241 p += len;
242 ch = (unsigned char)*p++;
84249819
JK
243 if (maxlen >= 0)
244 maxlen -= len + 1;
663af342
PH
245 if (sq_lookup[ch] >= ' ') {
246 EMIT(sq_lookup[ch]);
247 } else {
248 EMIT(((ch >> 6) & 03) + '0');
249 EMIT(((ch >> 3) & 07) + '0');
250 EMIT(((ch >> 0) & 07) + '0');
4f6fbcdc 251 }
4f6fbcdc 252 }
663af342
PH
253
254 EMITBUF(p, len);
255 if (p == name) /* no ending quote needed */
256 return 0;
257
4f6fbcdc
JH
258 if (!no_dq)
259 EMIT('"');
663af342
PH
260 return count;
261}
4f6fbcdc 262
663af342
PH
263size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
264{
265 return quote_c_style_counted(name, -1, sb, fp, nodq);
4f6fbcdc
JH
266}
267
d5625091
JH
268void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, int nodq)
269{
270 if (quote_c_style(prefix, NULL, NULL, 0) ||
271 quote_c_style(path, NULL, NULL, 0)) {
272 if (!nodq)
273 strbuf_addch(sb, '"');
274 quote_c_style(prefix, sb, NULL, 1);
275 quote_c_style(path, sb, NULL, 1);
276 if (!nodq)
277 strbuf_addch(sb, '"');
278 } else {
279 strbuf_addstr(sb, prefix);
280 strbuf_addstr(sb, path);
281 }
282}
283
663af342 284void write_name_quoted(const char *name, FILE *fp, int terminator)
9ef2b3cb 285{
663af342
PH
286 if (terminator) {
287 quote_c_style(name, NULL, fp, 0);
288 } else {
289 fputs(name, fp);
290 }
291 fputc(terminator, fp);
292}
293
2af202be
LT
294void write_name_quotedpfx(const char *pfx, size_t pfxlen,
295 const char *name, FILE *fp, int terminator)
663af342
PH
296{
297 int needquote = 0;
298
299 if (terminator) {
300 needquote = next_quote_pos(pfx, pfxlen) < pfxlen
301 || name[next_quote_pos(name, -1)];
302 }
303 if (needquote) {
304 fputc('"', fp);
305 quote_c_style_counted(pfx, pfxlen, NULL, fp, 1);
306 quote_c_style(name, NULL, fp, 1);
307 fputc('"', fp);
308 } else {
309 fwrite(pfx, pfxlen, 1, fp);
310 fputs(name, fp);
311 }
312 fputc(terminator, fp);
9ef2b3cb
JH
313}
314
b167cffb
CB
315void write_name_quoted_relative(const char *name, size_t len,
316 const char *prefix, size_t prefix_len,
317 FILE *fp, int terminator)
a734d0b1 318{
b167cffb
CB
319 struct strbuf sb = STRBUF_INIT;
320
ad66df2d 321 name = relative_path(name, prefix, &sb);
b167cffb
CB
322 write_name_quoted(name, fp, terminator);
323
324 strbuf_release(&sb);
325}
326
b167cffb
CB
327/* quote path as relative to the given prefix */
328char *quote_path_relative(const char *in, int len,
329 struct strbuf *out, const char *prefix)
330{
331 struct strbuf sb = STRBUF_INIT;
ad66df2d 332 const char *rel = relative_path(in, prefix, &sb);
b167cffb
CB
333 strbuf_reset(out);
334 quote_c_style_counted(rel, strlen(rel), out, NULL, 0);
335 strbuf_release(&sb);
a734d0b1 336
a734d0b1
DP
337 if (!out->len)
338 strbuf_addstr(out, "./");
339
340 return out->buf;
341}
342
4f6fbcdc
JH
343/*
344 * C-style name unquoting.
345 *
7fb1011e
PH
346 * Quoted should point at the opening double quote.
347 * + Returns 0 if it was able to unquote the string properly, and appends the
348 * result in the strbuf `sb'.
349 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
350 * that this function will allocate memory in the strbuf, so calling
351 * strbuf_release is mandatory whichever result unquote_c_style returns.
352 *
353 * Updates endp pointer to point at one past the ending double quote if given.
4f6fbcdc 354 */
7fb1011e 355int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
4f6fbcdc 356{
7fb1011e
PH
357 size_t oldlen = sb->len, len;
358 int ch, ac;
4f6fbcdc
JH
359
360 if (*quoted++ != '"')
7fb1011e
PH
361 return -1;
362
363 for (;;) {
364 len = strcspn(quoted, "\"\\");
365 strbuf_add(sb, quoted, len);
366 quoted += len;
4f6fbcdc 367
7fb1011e
PH
368 switch (*quoted++) {
369 case '"':
370 if (endp)
c8744d6a 371 *endp = quoted;
7fb1011e
PH
372 return 0;
373 case '\\':
374 break;
375 default:
376 goto error;
377 }
378
379 switch ((ch = *quoted++)) {
380 case 'a': ch = '\a'; break;
381 case 'b': ch = '\b'; break;
382 case 'f': ch = '\f'; break;
383 case 'n': ch = '\n'; break;
384 case 'r': ch = '\r'; break;
385 case 't': ch = '\t'; break;
386 case 'v': ch = '\v'; break;
387
388 case '\\': case '"':
389 break; /* verbatim */
390
391 /* octal values with first digit over 4 overflow */
392 case '0': case '1': case '2': case '3':
4f6fbcdc 393 ac = ((ch - '0') << 6);
7fb1011e
PH
394 if ((ch = *quoted++) < '0' || '7' < ch)
395 goto error;
4f6fbcdc 396 ac |= ((ch - '0') << 3);
7fb1011e
PH
397 if ((ch = *quoted++) < '0' || '7' < ch)
398 goto error;
4f6fbcdc
JH
399 ac |= (ch - '0');
400 ch = ac;
401 break;
402 default:
7fb1011e 403 goto error;
4f6fbcdc 404 }
7fb1011e 405 strbuf_addch(sb, ch);
4f6fbcdc
JH
406 }
407
7fb1011e
PH
408 error:
409 strbuf_setlen(sb, oldlen);
410 return -1;
4f6fbcdc
JH
411}
412
9f613ddd
JH
413/* quoting as a string literal for other languages */
414
415void perl_quote_print(FILE *stream, const char *src)
416{
417 const char sq = '\'';
418 const char bq = '\\';
419 char c;
420
421 fputc(sq, stream);
422 while ((c = *src++)) {
423 if (c == sq || c == bq)
424 fputc(bq, stream);
425 fputc(c, stream);
426 }
427 fputc(sq, stream);
428}
429
430void python_quote_print(FILE *stream, const char *src)
431{
432 const char sq = '\'';
433 const char bq = '\\';
434 const char nl = '\n';
435 char c;
436
437 fputc(sq, stream);
438 while ((c = *src++)) {
439 if (c == nl) {
440 fputc(bq, stream);
441 fputc('n', stream);
442 continue;
443 }
444 if (c == sq || c == bq)
445 fputc(bq, stream);
446 fputc(c, stream);
447 }
448 fputc(sq, stream);
449}
5558e55c
SP
450
451void tcl_quote_print(FILE *stream, const char *src)
452{
453 char c;
454
455 fputc('"', stream);
456 while ((c = *src++)) {
457 switch (c) {
458 case '[': case ']':
459 case '{': case '}':
460 case '$': case '\\': case '"':
461 fputc('\\', stream);
462 default:
463 fputc(c, stream);
464 break;
465 case '\f':
466 fputs("\\f", stream);
467 break;
468 case '\r':
469 fputs("\\r", stream);
470 break;
471 case '\n':
472 fputs("\\n", stream);
473 break;
474 case '\t':
475 fputs("\\t", stream);
476 break;
477 case '\v':
478 fputs("\\v", stream);
479 break;
480 }
481 }
482 fputc('"', stream);
483}