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