Merge branch 'bw/shortref'
[git/dkf.git] / quote.c
blob6a520855d6c418ecb1384ef9571b122b134af1af
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(char *arg)
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 return arg;
96 case '\\':
97 c = *++src;
98 if (need_bs_quote(c) && *++src == '\'') {
99 *dst++ = c;
100 continue;
102 /* Fallthrough */
103 default:
104 return NULL;
109 /* 1 means: quote as octal
110 * 0 means: quote as octal if (quote_path_fully)
111 * -1 means: never quote
112 * c: quote as "\\c"
114 #define X8(x) x, x, x, x, x, x, x, x
115 #define X16(x) X8(x), X8(x)
116 static signed char const sq_lookup[256] = {
117 /* 0 1 2 3 4 5 6 7 */
118 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
119 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
120 /* 0x10 */ X16(1),
121 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
122 /* 0x28 */ X16(-1), X16(-1), X16(-1),
123 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
124 /* 0x60 */ X16(-1), X8(-1),
125 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
126 /* 0x80 */ /* set to 0 */
129 static inline int sq_must_quote(char c)
131 return sq_lookup[(unsigned char)c] + quote_path_fully > 0;
134 /* returns the longest prefix not needing a quote up to maxlen if positive.
135 This stops at the first \0 because it's marked as a character needing an
136 escape */
137 static size_t next_quote_pos(const char *s, ssize_t maxlen)
139 size_t len;
140 if (maxlen < 0) {
141 for (len = 0; !sq_must_quote(s[len]); len++);
142 } else {
143 for (len = 0; len < maxlen && !sq_must_quote(s[len]); len++);
145 return len;
149 * C-style name quoting.
151 * (1) if sb and fp are both NULL, inspect the input name and counts the
152 * number of bytes that are needed to hold c_style quoted version of name,
153 * counting the double quotes around it but not terminating NUL, and
154 * returns it.
155 * However, if name does not need c_style quoting, it returns 0.
157 * (2) if sb or fp are not NULL, it emits the c_style quoted version
158 * of name, enclosed with double quotes if asked and needed only.
159 * Return value is the same as in (1).
161 static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
162 struct strbuf *sb, FILE *fp, int no_dq)
164 #undef EMIT
165 #define EMIT(c) \
166 do { \
167 if (sb) strbuf_addch(sb, (c)); \
168 if (fp) fputc((c), fp); \
169 count++; \
170 } while (0)
171 #define EMITBUF(s, l) \
172 do { \
173 if (sb) strbuf_add(sb, (s), (l)); \
174 if (fp) fwrite((s), (l), 1, fp); \
175 count += (l); \
176 } while (0)
178 size_t len, count = 0;
179 const char *p = name;
181 for (;;) {
182 int ch;
184 len = next_quote_pos(p, maxlen);
185 if (len == maxlen || !p[len])
186 break;
188 if (!no_dq && p == name)
189 EMIT('"');
191 EMITBUF(p, len);
192 EMIT('\\');
193 p += len;
194 ch = (unsigned char)*p++;
195 if (sq_lookup[ch] >= ' ') {
196 EMIT(sq_lookup[ch]);
197 } else {
198 EMIT(((ch >> 6) & 03) + '0');
199 EMIT(((ch >> 3) & 07) + '0');
200 EMIT(((ch >> 0) & 07) + '0');
204 EMITBUF(p, len);
205 if (p == name) /* no ending quote needed */
206 return 0;
208 if (!no_dq)
209 EMIT('"');
210 return count;
213 size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
215 return quote_c_style_counted(name, -1, sb, fp, nodq);
218 void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, int nodq)
220 if (quote_c_style(prefix, NULL, NULL, 0) ||
221 quote_c_style(path, NULL, NULL, 0)) {
222 if (!nodq)
223 strbuf_addch(sb, '"');
224 quote_c_style(prefix, sb, NULL, 1);
225 quote_c_style(path, sb, NULL, 1);
226 if (!nodq)
227 strbuf_addch(sb, '"');
228 } else {
229 strbuf_addstr(sb, prefix);
230 strbuf_addstr(sb, path);
234 void write_name_quoted(const char *name, FILE *fp, int terminator)
236 if (terminator) {
237 quote_c_style(name, NULL, fp, 0);
238 } else {
239 fputs(name, fp);
241 fputc(terminator, fp);
244 extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
245 const char *name, FILE *fp, int terminator)
247 int needquote = 0;
249 if (terminator) {
250 needquote = next_quote_pos(pfx, pfxlen) < pfxlen
251 || name[next_quote_pos(name, -1)];
253 if (needquote) {
254 fputc('"', fp);
255 quote_c_style_counted(pfx, pfxlen, NULL, fp, 1);
256 quote_c_style(name, NULL, fp, 1);
257 fputc('"', fp);
258 } else {
259 fwrite(pfx, pfxlen, 1, fp);
260 fputs(name, fp);
262 fputc(terminator, fp);
265 /* quote path as relative to the given prefix */
266 char *quote_path_relative(const char *in, int len,
267 struct strbuf *out, const char *prefix)
269 int needquote;
271 if (len < 0)
272 len = strlen(in);
274 /* "../" prefix itself does not need quoting, but "in" might. */
275 needquote = next_quote_pos(in, len) < len;
276 strbuf_setlen(out, 0);
277 strbuf_grow(out, len);
279 if (needquote)
280 strbuf_addch(out, '"');
281 if (prefix) {
282 int off = 0;
283 while (prefix[off] && off < len && prefix[off] == in[off])
284 if (prefix[off] == '/') {
285 prefix += off + 1;
286 in += off + 1;
287 len -= off + 1;
288 off = 0;
289 } else
290 off++;
292 for (; *prefix; prefix++)
293 if (*prefix == '/')
294 strbuf_addstr(out, "../");
297 quote_c_style_counted (in, len, out, NULL, 1);
299 if (needquote)
300 strbuf_addch(out, '"');
301 if (!out->len)
302 strbuf_addstr(out, "./");
304 return out->buf;
308 * C-style name unquoting.
310 * Quoted should point at the opening double quote.
311 * + Returns 0 if it was able to unquote the string properly, and appends the
312 * result in the strbuf `sb'.
313 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
314 * that this function will allocate memory in the strbuf, so calling
315 * strbuf_release is mandatory whichever result unquote_c_style returns.
317 * Updates endp pointer to point at one past the ending double quote if given.
319 int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
321 size_t oldlen = sb->len, len;
322 int ch, ac;
324 if (*quoted++ != '"')
325 return -1;
327 for (;;) {
328 len = strcspn(quoted, "\"\\");
329 strbuf_add(sb, quoted, len);
330 quoted += len;
332 switch (*quoted++) {
333 case '"':
334 if (endp)
335 *endp = quoted;
336 return 0;
337 case '\\':
338 break;
339 default:
340 goto error;
343 switch ((ch = *quoted++)) {
344 case 'a': ch = '\a'; break;
345 case 'b': ch = '\b'; break;
346 case 'f': ch = '\f'; break;
347 case 'n': ch = '\n'; break;
348 case 'r': ch = '\r'; break;
349 case 't': ch = '\t'; break;
350 case 'v': ch = '\v'; break;
352 case '\\': case '"':
353 break; /* verbatim */
355 /* octal values with first digit over 4 overflow */
356 case '0': case '1': case '2': case '3':
357 ac = ((ch - '0') << 6);
358 if ((ch = *quoted++) < '0' || '7' < ch)
359 goto error;
360 ac |= ((ch - '0') << 3);
361 if ((ch = *quoted++) < '0' || '7' < ch)
362 goto error;
363 ac |= (ch - '0');
364 ch = ac;
365 break;
366 default:
367 goto error;
369 strbuf_addch(sb, ch);
372 error:
373 strbuf_setlen(sb, oldlen);
374 return -1;
377 /* quoting as a string literal for other languages */
379 void perl_quote_print(FILE *stream, const char *src)
381 const char sq = '\'';
382 const char bq = '\\';
383 char c;
385 fputc(sq, stream);
386 while ((c = *src++)) {
387 if (c == sq || c == bq)
388 fputc(bq, stream);
389 fputc(c, stream);
391 fputc(sq, stream);
394 void python_quote_print(FILE *stream, const char *src)
396 const char sq = '\'';
397 const char bq = '\\';
398 const char nl = '\n';
399 char c;
401 fputc(sq, stream);
402 while ((c = *src++)) {
403 if (c == nl) {
404 fputc(bq, stream);
405 fputc('n', stream);
406 continue;
408 if (c == sq || c == bq)
409 fputc(bq, stream);
410 fputc(c, stream);
412 fputc(sq, stream);
415 void tcl_quote_print(FILE *stream, const char *src)
417 char c;
419 fputc('"', stream);
420 while ((c = *src++)) {
421 switch (c) {
422 case '[': case ']':
423 case '{': case '}':
424 case '$': case '\\': case '"':
425 fputc('\\', stream);
426 default:
427 fputc(c, stream);
428 break;
429 case '\f':
430 fputs("\\f", stream);
431 break;
432 case '\r':
433 fputs("\\r", stream);
434 break;
435 case '\n':
436 fputs("\\n", stream);
437 break;
438 case '\t':
439 fputs("\\t", stream);
440 break;
441 case '\v':
442 fputs("\\v", stream);
443 break;
446 fputc('"', stream);