Code

Merge branch 'maint'
[git.git] / quote.c
1 #include "cache.h"
2 #include "quote.h"
4 /* Help to copy the thing properly quoted for the shell safety.
5  * any single quote is replaced with '\'', any exclamation point
6  * is replaced with '\!', and the whole thing is enclosed in a
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'
13  *  a!b      ==> a'\!'b    ==> 'a'\!'b'
14  */
15 #undef EMIT
16 #define EMIT(x) do { if (++len < n) *bp++ = (x); } while(0)
18 static inline int need_bs_quote(char c)
19 {
20         return (c == '\'' || c == '!');
21 }
23 static size_t sq_quote_buf(char *dst, size_t n, const char *src)
24 {
25         char c;
26         char *bp = dst;
27         size_t len = 0;
29         EMIT('\'');
30         while ((c = *src++)) {
31                 if (need_bs_quote(c)) {
32                         EMIT('\'');
33                         EMIT('\\');
34                         EMIT(c);
35                         EMIT('\'');
36                 } else {
37                         EMIT(c);
38                 }
39         }
40         EMIT('\'');
42         if ( n )
43                 *bp = 0;
45         return len;
46 }
48 void sq_quote_print(FILE *stream, const char *src)
49 {
50         char c;
52         fputc('\'', stream);
53         while ((c = *src++)) {
54                 if (need_bs_quote(c)) {
55                         fputs("'\\", stream);
56                         fputc(c, stream);
57                         fputc('\'', stream);
58                 } else {
59                         fputc(c, stream);
60                 }
61         }
62         fputc('\'', stream);
63 }
65 char *sq_quote_argv(const char** argv, int count)
66 {
67         char *buf, *to;
68         int i;
69         size_t len = 0;
71         /* Count argv if needed. */
72         if (count < 0) {
73                 for (count = 0; argv[count]; count++)
74                         ; /* just counting */
75         }
77         /* Special case: no argv. */
78         if (!count)
79                 return xcalloc(1,1);
81         /* Get destination buffer length. */
82         for (i = 0; i < count; i++)
83                 len += sq_quote_buf(NULL, 0, argv[i]) + 1;
85         /* Alloc destination buffer. */
86         to = buf = xmalloc(len + 1);
88         /* Copy into destination buffer. */
89         for (i = 0; i < count; ++i) {
90                 *to++ = ' ';
91                 to += sq_quote_buf(to, len, argv[i]);
92         }
94         return buf;
95 }
97 /*
98  * Append a string to a string buffer, with or without shell quoting.
99  * Return true if the buffer overflowed.
100  */
101 int add_to_string(char **ptrp, int *sizep, const char *str, int quote)
103         char *p = *ptrp;
104         int size = *sizep;
105         int oc;
106         int err = 0;
108         if (quote)
109                 oc = sq_quote_buf(p, size, str);
110         else {
111                 oc = strlen(str);
112                 memcpy(p, str, (size <= oc) ? size - 1 : oc);
113         }
115         if (size <= oc) {
116                 err = 1;
117                 oc = size - 1;
118         }
120         *ptrp += oc;
121         **ptrp = '\0';
122         *sizep -= oc;
123         return err;
126 char *sq_dequote(char *arg)
128         char *dst = arg;
129         char *src = arg;
130         char c;
132         if (*src != '\'')
133                 return NULL;
134         for (;;) {
135                 c = *++src;
136                 if (!c)
137                         return NULL;
138                 if (c != '\'') {
139                         *dst++ = c;
140                         continue;
141                 }
142                 /* We stepped out of sq */
143                 switch (*++src) {
144                 case '\0':
145                         *dst = 0;
146                         return arg;
147                 case '\\':
148                         c = *++src;
149                         if (need_bs_quote(c) && *++src == '\'') {
150                                 *dst++ = c;
151                                 continue;
152                         }
153                 /* Fallthrough */
154                 default:
155                         return NULL;
156                 }
157         }
160 /*
161  * C-style name quoting.
162  *
163  * Does one of three things:
164  *
165  * (1) if outbuf and outfp are both NULL, inspect the input name and
166  *     counts the number of bytes that are needed to hold c_style
167  *     quoted version of name, counting the double quotes around
168  *     it but not terminating NUL, and returns it.  However, if name
169  *     does not need c_style quoting, it returns 0.
170  *
171  * (2) if outbuf is not NULL, it must point at a buffer large enough
172  *     to hold the c_style quoted version of name, enclosing double
173  *     quotes, and terminating NUL.  Fills outbuf with c_style quoted
174  *     version of name enclosed in double-quote pair.  Return value
175  *     is undefined.
176  *
177  * (3) if outfp is not NULL, outputs c_style quoted version of name,
178  *     but not enclosed in double-quote pair.  Return value is undefined.
179  */
181 static int quote_c_style_counted(const char *name, int namelen,
182                                  char *outbuf, FILE *outfp, int no_dq)
184 #undef EMIT
185 #define EMIT(c) \
186         (outbuf ? (*outbuf++ = (c)) : outfp ? fputc(c, outfp) : (count++))
188 #define EMITQ() EMIT('\\')
190         const char *sp;
191         int ch, count = 0, needquote = 0;
193         if (!no_dq)
194                 EMIT('"');
195         for (sp = name; sp < name + namelen; sp++) {
196                 ch = *sp;
197                 if (!ch)
198                         break;
199                 if ((ch < ' ') || (ch == '"') || (ch == '\\') ||
200                     (ch >= 0177)) {
201                         needquote = 1;
202                         switch (ch) {
203                         case '\a': EMITQ(); ch = 'a'; break;
204                         case '\b': EMITQ(); ch = 'b'; break;
205                         case '\f': EMITQ(); ch = 'f'; break;
206                         case '\n': EMITQ(); ch = 'n'; break;
207                         case '\r': EMITQ(); ch = 'r'; break;
208                         case '\t': EMITQ(); ch = 't'; break;
209                         case '\v': EMITQ(); ch = 'v'; break;
211                         case '\\': /* fallthru */
212                         case '"': EMITQ(); break;
213                         default:
214                                 /* octal */
215                                 EMITQ();
216                                 EMIT(((ch >> 6) & 03) + '0');
217                                 EMIT(((ch >> 3) & 07) + '0');
218                                 ch = (ch & 07) + '0';
219                                 break;
220                         }
221                 }
222                 EMIT(ch);
223         }
224         if (!no_dq)
225                 EMIT('"');
226         if (outbuf)
227                 *outbuf = 0;
229         return needquote ? count : 0;
232 int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq)
234         int cnt = strlen(name);
235         return quote_c_style_counted(name, cnt, outbuf, outfp, no_dq);
238 /*
239  * C-style name unquoting.
240  *
241  * Quoted should point at the opening double quote.  Returns
242  * an allocated memory that holds unquoted name, which the caller
243  * should free when done.  Updates endp pointer to point at
244  * one past the ending double quote if given.
245  */
247 char *unquote_c_style(const char *quoted, const char **endp)
249         const char *sp;
250         char *name = NULL, *outp = NULL;
251         int count = 0, ch, ac;
253 #undef EMIT
254 #define EMIT(c) (outp ? (*outp++ = (c)) : (count++))
256         if (*quoted++ != '"')
257                 return NULL;
259         while (1) {
260                 /* first pass counts and allocates, second pass fills */
261                 for (sp = quoted; (ch = *sp++) != '"'; ) {
262                         if (ch == '\\') {
263                                 switch (ch = *sp++) {
264                                 case 'a': ch = '\a'; break;
265                                 case 'b': ch = '\b'; break;
266                                 case 'f': ch = '\f'; break;
267                                 case 'n': ch = '\n'; break;
268                                 case 'r': ch = '\r'; break;
269                                 case 't': ch = '\t'; break;
270                                 case 'v': ch = '\v'; break;
272                                 case '\\': case '"':
273                                         break; /* verbatim */
275                                 case '0':
276                                 case '1':
277                                 case '2':
278                                 case '3':
279                                 case '4':
280                                 case '5':
281                                 case '6':
282                                 case '7':
283                                         /* octal */
284                                         ac = ((ch - '0') << 6);
285                                         if ((ch = *sp++) < '0' || '7' < ch)
286                                                 return NULL;
287                                         ac |= ((ch - '0') << 3);
288                                         if ((ch = *sp++) < '0' || '7' < ch)
289                                                 return NULL;
290                                         ac |= (ch - '0');
291                                         ch = ac;
292                                         break;
293                                 default:
294                                         return NULL; /* malformed */
295                                 }
296                         }
297                         EMIT(ch);
298                 }
300                 if (name) {
301                         *outp = 0;
302                         if (endp)
303                                 *endp = sp;
304                         return name;
305                 }
306                 outp = name = xmalloc(count + 1);
307         }
310 void write_name_quoted(const char *prefix, int prefix_len,
311                        const char *name, int quote, FILE *out)
313         int needquote;
315         if (!quote) {
316         no_quote:
317                 if (prefix_len)
318                         fprintf(out, "%.*s", prefix_len, prefix);
319                 fputs(name, out);
320                 return;
321         }
323         needquote = 0;
324         if (prefix_len)
325                 needquote = quote_c_style_counted(prefix, prefix_len,
326                                                   NULL, NULL, 0);
327         if (!needquote)
328                 needquote = quote_c_style(name, NULL, NULL, 0);
329         if (needquote) {
330                 fputc('"', out);
331                 if (prefix_len)
332                         quote_c_style_counted(prefix, prefix_len,
333                                               NULL, out, 1);
334                 quote_c_style(name, NULL, out, 1);
335                 fputc('"', out);
336         }
337         else
338                 goto no_quote;
341 /* quoting as a string literal for other languages */
343 void perl_quote_print(FILE *stream, const char *src)
345         const char sq = '\'';
346         const char bq = '\\';
347         char c;
349         fputc(sq, stream);
350         while ((c = *src++)) {
351                 if (c == sq || c == bq)
352                         fputc(bq, stream);
353                 fputc(c, stream);
354         }
355         fputc(sq, stream);
358 void python_quote_print(FILE *stream, const char *src)
360         const char sq = '\'';
361         const char bq = '\\';
362         const char nl = '\n';
363         char c;
365         fputc(sq, stream);
366         while ((c = *src++)) {
367                 if (c == nl) {
368                         fputc(bq, stream);
369                         fputc('n', stream);
370                         continue;
371                 }
372                 if (c == sq || c == bq)
373                         fputc(bq, stream);
374                 fputc(c, stream);
375         }
376         fputc(sq, stream);
379 void tcl_quote_print(FILE *stream, const char *src)
381         char c;
383         fputc('"', stream);
384         while ((c = *src++)) {
385                 switch (c) {
386                 case '[': case ']':
387                 case '{': case '}':
388                 case '$': case '\\': case '"':
389                         fputc('\\', stream);
390                 default:
391                         fputc(c, stream);
392                         break;
393                 case '\f':
394                         fputs("\\f", stream);
395                         break;
396                 case '\r':
397                         fputs("\\r", stream);
398                         break;
399                 case '\n':
400                         fputs("\\n", stream);
401                         break;
402                 case '\t':
403                         fputs("\\t", stream);
404                         break;
405                 case '\v':
406                         fputs("\\v", stream);
407                         break;
408                 }
409         }
410         fputc('"', stream);