]>
Commit | Line | Data |
---|---|---|
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 | */ |
4f6fbcdc | 15 | #undef EMIT |
8dcaefb5 | 16 | #define EMIT(x) do { if (++len < n) *bp++ = (x); } while(0) |
6fb737be | 17 | |
35eb2d36 LT |
18 | static inline int need_bs_quote(char c) |
19 | { | |
20 | return (c == '\'' || c == '!'); | |
21 | } | |
22 | ||
16befb8b | 23 | static size_t sq_quote_buf(char *dst, size_t n, const char *src) |
77d604c3 PA |
24 | { |
25 | char c; | |
26 | char *bp = dst; | |
27 | size_t len = 0; | |
6fb737be | 28 | |
77d604c3 | 29 | EMIT('\''); |
6fb737be | 30 | while ((c = *src++)) { |
35eb2d36 | 31 | if (need_bs_quote(c)) { |
77d604c3 PA |
32 | EMIT('\''); |
33 | EMIT('\\'); | |
34 | EMIT(c); | |
35 | EMIT('\''); | |
36 | } else { | |
37 | EMIT(c); | |
6fb737be JH |
38 | } |
39 | } | |
77d604c3 PA |
40 | EMIT('\''); |
41 | ||
42 | if ( n ) | |
43 | *bp = 0; | |
44 | ||
45 | return len; | |
46 | } | |
47 | ||
575ba9d6 ML |
48 | void sq_quote_print(FILE *stream, const char *src) |
49 | { | |
50 | char c; | |
51 | ||
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 | } | |
64 | ||
7cf67205 CC |
65 | char *sq_quote_argv(const char** argv, int count) |
66 | { | |
67 | char *buf, *to; | |
68 | int i; | |
69 | size_t len = 0; | |
70 | ||
71 | /* Count argv if needed. */ | |
72 | if (count < 0) { | |
73 | for (count = 0; argv[count]; count++) | |
74 | ; /* just counting */ | |
75 | } | |
76 | ||
77 | /* Special case: no argv. */ | |
78 | if (!count) | |
79 | return xcalloc(1,1); | |
80 | ||
81 | /* Get destination buffer length. */ | |
82 | for (i = 0; i < count; i++) | |
83 | len += sq_quote_buf(NULL, 0, argv[i]) + 1; | |
84 | ||
85 | /* Alloc destination buffer. */ | |
86 | to = buf = xmalloc(len + 1); | |
87 | ||
88 | /* Copy into destination buffer. */ | |
89 | for (i = 0; i < count; ++i) { | |
90 | *to++ = ' '; | |
91 | to += sq_quote_buf(to, len, argv[i]); | |
92 | } | |
93 | ||
94 | return buf; | |
95 | } | |
96 | ||
86257aa3 CC |
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) | |
102 | { | |
103 | char *p = *ptrp; | |
104 | int size = *sizep; | |
105 | int oc; | |
106 | int err = 0; | |
107 | ||
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 | } | |
114 | ||
115 | if (size <= oc) { | |
116 | err = 1; | |
117 | oc = size - 1; | |
118 | } | |
119 | ||
120 | *ptrp += oc; | |
121 | **ptrp = '\0'; | |
122 | *sizep -= oc; | |
123 | return err; | |
124 | } | |
125 | ||
35eb2d36 LT |
126 | char *sq_dequote(char *arg) |
127 | { | |
128 | char *dst = arg; | |
129 | char *src = arg; | |
130 | char c; | |
131 | ||
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 | } | |
158 | } | |
159 | ||
4f6fbcdc JH |
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 | */ | |
180 | ||
9ef2b3cb JH |
181 | static int quote_c_style_counted(const char *name, int namelen, |
182 | char *outbuf, FILE *outfp, int no_dq) | |
4f6fbcdc JH |
183 | { |
184 | #undef EMIT | |
185 | #define EMIT(c) \ | |
186 | (outbuf ? (*outbuf++ = (c)) : outfp ? fputc(c, outfp) : (count++)) | |
187 | ||
188 | #define EMITQ() EMIT('\\') | |
189 | ||
190 | const char *sp; | |
9378c161 JH |
191 | unsigned char ch; |
192 | int count = 0, needquote = 0; | |
4f6fbcdc JH |
193 | |
194 | if (!no_dq) | |
195 | EMIT('"'); | |
50e7b067 PR |
196 | for (sp = name; sp < name + namelen; sp++) { |
197 | ch = *sp; | |
198 | if (!ch) | |
199 | break; | |
28fba290 | 200 | if ((ch < ' ') || (ch == '"') || (ch == '\\') || |
9378c161 | 201 | (quote_path_fully && (ch >= 0177))) { |
4f6fbcdc JH |
202 | needquote = 1; |
203 | switch (ch) { | |
204 | case '\a': EMITQ(); ch = 'a'; break; | |
205 | case '\b': EMITQ(); ch = 'b'; break; | |
206 | case '\f': EMITQ(); ch = 'f'; break; | |
207 | case '\n': EMITQ(); ch = 'n'; break; | |
208 | case '\r': EMITQ(); ch = 'r'; break; | |
209 | case '\t': EMITQ(); ch = 't'; break; | |
210 | case '\v': EMITQ(); ch = 'v'; break; | |
211 | ||
212 | case '\\': /* fallthru */ | |
213 | case '"': EMITQ(); break; | |
4f6fbcdc JH |
214 | default: |
215 | /* octal */ | |
216 | EMITQ(); | |
217 | EMIT(((ch >> 6) & 03) + '0'); | |
218 | EMIT(((ch >> 3) & 07) + '0'); | |
219 | ch = (ch & 07) + '0'; | |
220 | break; | |
221 | } | |
222 | } | |
223 | EMIT(ch); | |
224 | } | |
225 | if (!no_dq) | |
226 | EMIT('"'); | |
227 | if (outbuf) | |
228 | *outbuf = 0; | |
229 | ||
230 | return needquote ? count : 0; | |
231 | } | |
232 | ||
9ef2b3cb JH |
233 | int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq) |
234 | { | |
235 | int cnt = strlen(name); | |
236 | return quote_c_style_counted(name, cnt, outbuf, outfp, no_dq); | |
237 | } | |
238 | ||
4f6fbcdc JH |
239 | /* |
240 | * C-style name unquoting. | |
241 | * | |
7fb1011e PH |
242 | * Quoted should point at the opening double quote. |
243 | * + Returns 0 if it was able to unquote the string properly, and appends the | |
244 | * result in the strbuf `sb'. | |
245 | * + Returns -1 in case of error, and doesn't touch the strbuf. Though note | |
246 | * that this function will allocate memory in the strbuf, so calling | |
247 | * strbuf_release is mandatory whichever result unquote_c_style returns. | |
248 | * | |
249 | * Updates endp pointer to point at one past the ending double quote if given. | |
4f6fbcdc | 250 | */ |
7fb1011e | 251 | int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp) |
4f6fbcdc | 252 | { |
7fb1011e PH |
253 | size_t oldlen = sb->len, len; |
254 | int ch, ac; | |
4f6fbcdc JH |
255 | |
256 | if (*quoted++ != '"') | |
7fb1011e PH |
257 | return -1; |
258 | ||
259 | for (;;) { | |
260 | len = strcspn(quoted, "\"\\"); | |
261 | strbuf_add(sb, quoted, len); | |
262 | quoted += len; | |
4f6fbcdc | 263 | |
7fb1011e PH |
264 | switch (*quoted++) { |
265 | case '"': | |
266 | if (endp) | |
267 | *endp = quoted + 1; | |
268 | return 0; | |
269 | case '\\': | |
270 | break; | |
271 | default: | |
272 | goto error; | |
273 | } | |
274 | ||
275 | switch ((ch = *quoted++)) { | |
276 | case 'a': ch = '\a'; break; | |
277 | case 'b': ch = '\b'; break; | |
278 | case 'f': ch = '\f'; break; | |
279 | case 'n': ch = '\n'; break; | |
280 | case 'r': ch = '\r'; break; | |
281 | case 't': ch = '\t'; break; | |
282 | case 'v': ch = '\v'; break; | |
283 | ||
284 | case '\\': case '"': | |
285 | break; /* verbatim */ | |
286 | ||
287 | /* octal values with first digit over 4 overflow */ | |
288 | case '0': case '1': case '2': case '3': | |
4f6fbcdc | 289 | ac = ((ch - '0') << 6); |
7fb1011e PH |
290 | if ((ch = *quoted++) < '0' || '7' < ch) |
291 | goto error; | |
4f6fbcdc | 292 | ac |= ((ch - '0') << 3); |
7fb1011e PH |
293 | if ((ch = *quoted++) < '0' || '7' < ch) |
294 | goto error; | |
4f6fbcdc JH |
295 | ac |= (ch - '0'); |
296 | ch = ac; | |
297 | break; | |
298 | default: | |
7fb1011e | 299 | goto error; |
4f6fbcdc | 300 | } |
7fb1011e | 301 | strbuf_addch(sb, ch); |
4f6fbcdc JH |
302 | } |
303 | ||
7fb1011e PH |
304 | error: |
305 | strbuf_setlen(sb, oldlen); | |
306 | return -1; | |
4f6fbcdc JH |
307 | } |
308 | ||
9ef2b3cb JH |
309 | void write_name_quoted(const char *prefix, int prefix_len, |
310 | const char *name, int quote, FILE *out) | |
4f6fbcdc JH |
311 | { |
312 | int needquote; | |
313 | ||
314 | if (!quote) { | |
315 | no_quote: | |
9ef2b3cb JH |
316 | if (prefix_len) |
317 | fprintf(out, "%.*s", prefix_len, prefix); | |
4f6fbcdc JH |
318 | fputs(name, out); |
319 | return; | |
320 | } | |
321 | ||
322 | needquote = 0; | |
9ef2b3cb JH |
323 | if (prefix_len) |
324 | needquote = quote_c_style_counted(prefix, prefix_len, | |
325 | NULL, NULL, 0); | |
4f6fbcdc JH |
326 | if (!needquote) |
327 | needquote = quote_c_style(name, NULL, NULL, 0); | |
328 | if (needquote) { | |
329 | fputc('"', out); | |
9ef2b3cb JH |
330 | if (prefix_len) |
331 | quote_c_style_counted(prefix, prefix_len, | |
332 | NULL, out, 1); | |
4f6fbcdc JH |
333 | quote_c_style(name, NULL, out, 1); |
334 | fputc('"', out); | |
335 | } | |
336 | else | |
337 | goto no_quote; | |
338 | } | |
9f613ddd JH |
339 | |
340 | /* quoting as a string literal for other languages */ | |
341 | ||
342 | void perl_quote_print(FILE *stream, const char *src) | |
343 | { | |
344 | const char sq = '\''; | |
345 | const char bq = '\\'; | |
346 | char c; | |
347 | ||
348 | fputc(sq, stream); | |
349 | while ((c = *src++)) { | |
350 | if (c == sq || c == bq) | |
351 | fputc(bq, stream); | |
352 | fputc(c, stream); | |
353 | } | |
354 | fputc(sq, stream); | |
355 | } | |
356 | ||
357 | void python_quote_print(FILE *stream, const char *src) | |
358 | { | |
359 | const char sq = '\''; | |
360 | const char bq = '\\'; | |
361 | const char nl = '\n'; | |
362 | char c; | |
363 | ||
364 | fputc(sq, stream); | |
365 | while ((c = *src++)) { | |
366 | if (c == nl) { | |
367 | fputc(bq, stream); | |
368 | fputc('n', stream); | |
369 | continue; | |
370 | } | |
371 | if (c == sq || c == bq) | |
372 | fputc(bq, stream); | |
373 | fputc(c, stream); | |
374 | } | |
375 | fputc(sq, stream); | |
376 | } | |
5558e55c SP |
377 | |
378 | void tcl_quote_print(FILE *stream, const char *src) | |
379 | { | |
380 | char c; | |
381 | ||
382 | fputc('"', stream); | |
383 | while ((c = *src++)) { | |
384 | switch (c) { | |
385 | case '[': case ']': | |
386 | case '{': case '}': | |
387 | case '$': case '\\': case '"': | |
388 | fputc('\\', stream); | |
389 | default: | |
390 | fputc(c, stream); | |
391 | break; | |
392 | case '\f': | |
393 | fputs("\\f", stream); | |
394 | break; | |
395 | case '\r': | |
396 | fputs("\\r", stream); | |
397 | break; | |
398 | case '\n': | |
399 | fputs("\\n", stream); | |
400 | break; | |
401 | case '\t': | |
402 | fputs("\\t", stream); | |
403 | break; | |
404 | case '\v': | |
405 | fputs("\\v", stream); | |
406 | break; | |
407 | } | |
408 | } | |
409 | fputc('"', stream); | |
410 | } |