quote: implement "sq_dequote_many" to unwrap many args in one string
[git/gitweb.git] / quote.c
blobea49c7a99fb1fc28f786d53f528addf4dcbf5a02
1 #include "cache.h"
2 #include "quote.h"
4 int quote_path_fully = 1;
6 /* Help to copy the thing properly quoted for the shell safety.
7 * any single quote is replaced with '\'', any exclamation point
8 * is replaced with '\!', and the whole thing is enclosed in a
10 * E.g.
11 * original sq_quote result
12 * name ==> name ==> 'name'
13 * a b ==> a b ==> 'a b'
14 * a'b ==> a'\''b ==> 'a'\''b'
15 * a!b ==> a'\!'b ==> 'a'\!'b'
17 static inline int need_bs_quote(char c)
19 return (c == '\'' || c == '!');
22 void sq_quote_buf(struct strbuf *dst, const char *src)
24 char *to_free = NULL;
26 if (dst->buf == src)
27 to_free = strbuf_detach(dst, NULL);
29 strbuf_addch(dst, '\'');
30 while (*src) {
31 size_t len = strcspn(src, "'!");
32 strbuf_add(dst, src, len);
33 src += len;
34 while (need_bs_quote(*src)) {
35 strbuf_addstr(dst, "'\\");
36 strbuf_addch(dst, *src++);
37 strbuf_addch(dst, '\'');
40 strbuf_addch(dst, '\'');
41 free(to_free);
44 void sq_quote_print(FILE *stream, const char *src)
46 char c;
48 fputc('\'', stream);
49 while ((c = *src++)) {
50 if (need_bs_quote(c)) {
51 fputs("'\\", stream);
52 fputc(c, stream);
53 fputc('\'', stream);
54 } else {
55 fputc(c, stream);
58 fputc('\'', stream);
61 void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
63 int i;
65 /* Copy into destination buffer. */
66 strbuf_grow(dst, 255);
67 for (i = 0; argv[i]; ++i) {
68 strbuf_addch(dst, ' ');
69 sq_quote_buf(dst, argv[i]);
70 if (maxlen && dst->len > maxlen)
71 die("Too many or long arguments");
75 char *sq_dequote_step(char *arg, char **next)
77 char *dst = arg;
78 char *src = arg;
79 char c;
81 if (*src != '\'')
82 return NULL;
83 for (;;) {
84 c = *++src;
85 if (!c)
86 return NULL;
87 if (c != '\'') {
88 *dst++ = c;
89 continue;
91 /* We stepped out of sq */
92 switch (*++src) {
93 case '\0':
94 *dst = 0;
95 if (next)
96 *next = NULL;
97 return arg;
98 case '\\':
99 c = *++src;
100 if (need_bs_quote(c) && *++src == '\'') {
101 *dst++ = c;
102 continue;
104 /* Fallthrough */
105 default:
106 if (!next || !isspace(*src))
107 return NULL;
108 do {
109 c = *++src;
110 } while (isspace(c));
111 *dst = 0;
112 *next = src;
113 return arg;
118 char *sq_dequote(char *arg)
120 return sq_dequote_step(arg, NULL);
123 /* 1 means: quote as octal
124 * 0 means: quote as octal if (quote_path_fully)
125 * -1 means: never quote
126 * c: quote as "\\c"
128 #define X8(x) x, x, x, x, x, x, x, x
129 #define X16(x) X8(x), X8(x)
130 static signed char const sq_lookup[256] = {
131 /* 0 1 2 3 4 5 6 7 */
132 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
133 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
134 /* 0x10 */ X16(1),
135 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
136 /* 0x28 */ X16(-1), X16(-1), X16(-1),
137 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
138 /* 0x60 */ X16(-1), X8(-1),
139 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
140 /* 0x80 */ /* set to 0 */
143 static inline int sq_must_quote(char c)
145 return sq_lookup[(unsigned char)c] + quote_path_fully > 0;
148 /* returns the longest prefix not needing a quote up to maxlen if positive.
149 This stops at the first \0 because it's marked as a character needing an
150 escape */
151 static size_t next_quote_pos(const char *s, ssize_t maxlen)
153 size_t len;
154 if (maxlen < 0) {
155 for (len = 0; !sq_must_quote(s[len]); len++);
156 } else {
157 for (len = 0; len < maxlen && !sq_must_quote(s[len]); len++);
159 return len;
163 * C-style name quoting.
165 * (1) if sb and fp are both NULL, inspect the input name and counts the
166 * number of bytes that are needed to hold c_style quoted version of name,
167 * counting the double quotes around it but not terminating NUL, and
168 * returns it.
169 * However, if name does not need c_style quoting, it returns 0.
171 * (2) if sb or fp are not NULL, it emits the c_style quoted version
172 * of name, enclosed with double quotes if asked and needed only.
173 * Return value is the same as in (1).
175 static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
176 struct strbuf *sb, FILE *fp, int no_dq)
178 #undef EMIT
179 #define EMIT(c) \
180 do { \
181 if (sb) strbuf_addch(sb, (c)); \
182 if (fp) fputc((c), fp); \
183 count++; \
184 } while (0)
185 #define EMITBUF(s, l) \
186 do { \
187 if (sb) strbuf_add(sb, (s), (l)); \
188 if (fp) fwrite((s), (l), 1, fp); \
189 count += (l); \
190 } while (0)
192 size_t len, count = 0;
193 const char *p = name;
195 for (;;) {
196 int ch;
198 len = next_quote_pos(p, maxlen);
199 if (len == maxlen || !p[len])
200 break;
202 if (!no_dq && p == name)
203 EMIT('"');
205 EMITBUF(p, len);
206 EMIT('\\');
207 p += len;
208 ch = (unsigned char)*p++;
209 if (sq_lookup[ch] >= ' ') {
210 EMIT(sq_lookup[ch]);
211 } else {
212 EMIT(((ch >> 6) & 03) + '0');
213 EMIT(((ch >> 3) & 07) + '0');
214 EMIT(((ch >> 0) & 07) + '0');
218 EMITBUF(p, len);
219 if (p == name) /* no ending quote needed */
220 return 0;
222 if (!no_dq)
223 EMIT('"');
224 return count;
227 size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
229 return quote_c_style_counted(name, -1, sb, fp, nodq);
232 void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, int nodq)
234 if (quote_c_style(prefix, NULL, NULL, 0) ||
235 quote_c_style(path, NULL, NULL, 0)) {
236 if (!nodq)
237 strbuf_addch(sb, '"');
238 quote_c_style(prefix, sb, NULL, 1);
239 quote_c_style(path, sb, NULL, 1);
240 if (!nodq)
241 strbuf_addch(sb, '"');
242 } else {
243 strbuf_addstr(sb, prefix);
244 strbuf_addstr(sb, path);
248 void write_name_quoted(const char *name, FILE *fp, int terminator)
250 if (terminator) {
251 quote_c_style(name, NULL, fp, 0);
252 } else {
253 fputs(name, fp);
255 fputc(terminator, fp);
258 extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
259 const char *name, FILE *fp, int terminator)
261 int needquote = 0;
263 if (terminator) {
264 needquote = next_quote_pos(pfx, pfxlen) < pfxlen
265 || name[next_quote_pos(name, -1)];
267 if (needquote) {
268 fputc('"', fp);
269 quote_c_style_counted(pfx, pfxlen, NULL, fp, 1);
270 quote_c_style(name, NULL, fp, 1);
271 fputc('"', fp);
272 } else {
273 fwrite(pfx, pfxlen, 1, fp);
274 fputs(name, fp);
276 fputc(terminator, fp);
279 /* quote path as relative to the given prefix */
280 char *quote_path_relative(const char *in, int len,
281 struct strbuf *out, const char *prefix)
283 int needquote;
285 if (len < 0)
286 len = strlen(in);
288 /* "../" prefix itself does not need quoting, but "in" might. */
289 needquote = next_quote_pos(in, len) < len;
290 strbuf_setlen(out, 0);
291 strbuf_grow(out, len);
293 if (needquote)
294 strbuf_addch(out, '"');
295 if (prefix) {
296 int off = 0;
297 while (prefix[off] && off < len && prefix[off] == in[off])
298 if (prefix[off] == '/') {
299 prefix += off + 1;
300 in += off + 1;
301 len -= off + 1;
302 off = 0;
303 } else
304 off++;
306 for (; *prefix; prefix++)
307 if (*prefix == '/')
308 strbuf_addstr(out, "../");
311 quote_c_style_counted (in, len, out, NULL, 1);
313 if (needquote)
314 strbuf_addch(out, '"');
315 if (!out->len)
316 strbuf_addstr(out, "./");
318 return out->buf;
322 * C-style name unquoting.
324 * Quoted should point at the opening double quote.
325 * + Returns 0 if it was able to unquote the string properly, and appends the
326 * result in the strbuf `sb'.
327 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
328 * that this function will allocate memory in the strbuf, so calling
329 * strbuf_release is mandatory whichever result unquote_c_style returns.
331 * Updates endp pointer to point at one past the ending double quote if given.
333 int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
335 size_t oldlen = sb->len, len;
336 int ch, ac;
338 if (*quoted++ != '"')
339 return -1;
341 for (;;) {
342 len = strcspn(quoted, "\"\\");
343 strbuf_add(sb, quoted, len);
344 quoted += len;
346 switch (*quoted++) {
347 case '"':
348 if (endp)
349 *endp = quoted;
350 return 0;
351 case '\\':
352 break;
353 default:
354 goto error;
357 switch ((ch = *quoted++)) {
358 case 'a': ch = '\a'; break;
359 case 'b': ch = '\b'; break;
360 case 'f': ch = '\f'; break;
361 case 'n': ch = '\n'; break;
362 case 'r': ch = '\r'; break;
363 case 't': ch = '\t'; break;
364 case 'v': ch = '\v'; break;
366 case '\\': case '"':
367 break; /* verbatim */
369 /* octal values with first digit over 4 overflow */
370 case '0': case '1': case '2': case '3':
371 ac = ((ch - '0') << 6);
372 if ((ch = *quoted++) < '0' || '7' < ch)
373 goto error;
374 ac |= ((ch - '0') << 3);
375 if ((ch = *quoted++) < '0' || '7' < ch)
376 goto error;
377 ac |= (ch - '0');
378 ch = ac;
379 break;
380 default:
381 goto error;
383 strbuf_addch(sb, ch);
386 error:
387 strbuf_setlen(sb, oldlen);
388 return -1;
391 /* quoting as a string literal for other languages */
393 void perl_quote_print(FILE *stream, const char *src)
395 const char sq = '\'';
396 const char bq = '\\';
397 char c;
399 fputc(sq, stream);
400 while ((c = *src++)) {
401 if (c == sq || c == bq)
402 fputc(bq, stream);
403 fputc(c, stream);
405 fputc(sq, stream);
408 void python_quote_print(FILE *stream, const char *src)
410 const char sq = '\'';
411 const char bq = '\\';
412 const char nl = '\n';
413 char c;
415 fputc(sq, stream);
416 while ((c = *src++)) {
417 if (c == nl) {
418 fputc(bq, stream);
419 fputc('n', stream);
420 continue;
422 if (c == sq || c == bq)
423 fputc(bq, stream);
424 fputc(c, stream);
426 fputc(sq, stream);
429 void tcl_quote_print(FILE *stream, const char *src)
431 char c;
433 fputc('"', stream);
434 while ((c = *src++)) {
435 switch (c) {
436 case '[': case ']':
437 case '{': case '}':
438 case '$': case '\\': case '"':
439 fputc('\\', stream);
440 default:
441 fputc(c, stream);
442 break;
443 case '\f':
444 fputs("\\f", stream);
445 break;
446 case '\r':
447 fputs("\\r", stream);
448 break;
449 case '\n':
450 fputs("\\n", stream);
451 break;
452 case '\t':
453 fputs("\\t", stream);
454 break;
455 case '\v':
456 fputs("\\v", stream);
457 break;
460 fputc('"', stream);