]>
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 | ||
77d604c3 PA |
23 | 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; | |
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 | ||
77d604c3 PA |
65 | char *sq_quote(const char *src) |
66 | { | |
67 | char *buf; | |
68 | size_t cnt; | |
69 | ||
70 | cnt = sq_quote_buf(NULL, 0, src) + 1; | |
71 | buf = xmalloc(cnt); | |
72 | sq_quote_buf(buf, cnt, src); | |
73 | ||
6fb737be JH |
74 | return buf; |
75 | } | |
76 | ||
35eb2d36 LT |
77 | char *sq_dequote(char *arg) |
78 | { | |
79 | char *dst = arg; | |
80 | char *src = arg; | |
81 | char c; | |
82 | ||
83 | if (*src != '\'') | |
84 | return NULL; | |
85 | for (;;) { | |
86 | c = *++src; | |
87 | if (!c) | |
88 | return NULL; | |
89 | if (c != '\'') { | |
90 | *dst++ = c; | |
91 | continue; | |
92 | } | |
93 | /* We stepped out of sq */ | |
94 | switch (*++src) { | |
95 | case '\0': | |
96 | *dst = 0; | |
97 | return arg; | |
98 | case '\\': | |
99 | c = *++src; | |
100 | if (need_bs_quote(c) && *++src == '\'') { | |
101 | *dst++ = c; | |
102 | continue; | |
103 | } | |
104 | /* Fallthrough */ | |
105 | default: | |
106 | return NULL; | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
4f6fbcdc JH |
111 | /* |
112 | * C-style name quoting. | |
113 | * | |
114 | * Does one of three things: | |
115 | * | |
116 | * (1) if outbuf and outfp are both NULL, inspect the input name and | |
117 | * counts the number of bytes that are needed to hold c_style | |
118 | * quoted version of name, counting the double quotes around | |
119 | * it but not terminating NUL, and returns it. However, if name | |
120 | * does not need c_style quoting, it returns 0. | |
121 | * | |
122 | * (2) if outbuf is not NULL, it must point at a buffer large enough | |
123 | * to hold the c_style quoted version of name, enclosing double | |
124 | * quotes, and terminating NUL. Fills outbuf with c_style quoted | |
125 | * version of name enclosed in double-quote pair. Return value | |
126 | * is undefined. | |
127 | * | |
128 | * (3) if outfp is not NULL, outputs c_style quoted version of name, | |
129 | * but not enclosed in double-quote pair. Return value is undefined. | |
130 | */ | |
131 | ||
9ef2b3cb JH |
132 | static int quote_c_style_counted(const char *name, int namelen, |
133 | char *outbuf, FILE *outfp, int no_dq) | |
4f6fbcdc JH |
134 | { |
135 | #undef EMIT | |
136 | #define EMIT(c) \ | |
137 | (outbuf ? (*outbuf++ = (c)) : outfp ? fputc(c, outfp) : (count++)) | |
138 | ||
139 | #define EMITQ() EMIT('\\') | |
140 | ||
141 | const char *sp; | |
142 | int ch, count = 0, needquote = 0; | |
143 | ||
144 | if (!no_dq) | |
145 | EMIT('"'); | |
50e7b067 PR |
146 | for (sp = name; sp < name + namelen; sp++) { |
147 | ch = *sp; | |
148 | if (!ch) | |
149 | break; | |
28fba290 JH |
150 | if ((ch < ' ') || (ch == '"') || (ch == '\\') || |
151 | (ch == 0177)) { | |
4f6fbcdc JH |
152 | needquote = 1; |
153 | switch (ch) { | |
154 | case '\a': EMITQ(); ch = 'a'; break; | |
155 | case '\b': EMITQ(); ch = 'b'; break; | |
156 | case '\f': EMITQ(); ch = 'f'; break; | |
157 | case '\n': EMITQ(); ch = 'n'; break; | |
158 | case '\r': EMITQ(); ch = 'r'; break; | |
159 | case '\t': EMITQ(); ch = 't'; break; | |
160 | case '\v': EMITQ(); ch = 'v'; break; | |
161 | ||
162 | case '\\': /* fallthru */ | |
163 | case '"': EMITQ(); break; | |
4f6fbcdc JH |
164 | default: |
165 | /* octal */ | |
166 | EMITQ(); | |
167 | EMIT(((ch >> 6) & 03) + '0'); | |
168 | EMIT(((ch >> 3) & 07) + '0'); | |
169 | ch = (ch & 07) + '0'; | |
170 | break; | |
171 | } | |
172 | } | |
173 | EMIT(ch); | |
174 | } | |
175 | if (!no_dq) | |
176 | EMIT('"'); | |
177 | if (outbuf) | |
178 | *outbuf = 0; | |
179 | ||
180 | return needquote ? count : 0; | |
181 | } | |
182 | ||
9ef2b3cb JH |
183 | int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq) |
184 | { | |
185 | int cnt = strlen(name); | |
186 | return quote_c_style_counted(name, cnt, outbuf, outfp, no_dq); | |
187 | } | |
188 | ||
4f6fbcdc JH |
189 | /* |
190 | * C-style name unquoting. | |
191 | * | |
192 | * Quoted should point at the opening double quote. Returns | |
193 | * an allocated memory that holds unquoted name, which the caller | |
194 | * should free when done. Updates endp pointer to point at | |
195 | * one past the ending double quote if given. | |
196 | */ | |
197 | ||
198 | char *unquote_c_style(const char *quoted, const char **endp) | |
199 | { | |
200 | const char *sp; | |
201 | char *name = NULL, *outp = NULL; | |
202 | int count = 0, ch, ac; | |
203 | ||
204 | #undef EMIT | |
205 | #define EMIT(c) (outp ? (*outp++ = (c)) : (count++)) | |
206 | ||
207 | if (*quoted++ != '"') | |
208 | return NULL; | |
209 | ||
210 | while (1) { | |
211 | /* first pass counts and allocates, second pass fills */ | |
212 | for (sp = quoted; (ch = *sp++) != '"'; ) { | |
213 | if (ch == '\\') { | |
214 | switch (ch = *sp++) { | |
215 | case 'a': ch = '\a'; break; | |
216 | case 'b': ch = '\b'; break; | |
217 | case 'f': ch = '\f'; break; | |
218 | case 'n': ch = '\n'; break; | |
219 | case 'r': ch = '\r'; break; | |
220 | case 't': ch = '\t'; break; | |
221 | case 'v': ch = '\v'; break; | |
222 | ||
223 | case '\\': case '"': | |
224 | break; /* verbatim */ | |
225 | ||
cfd432e6 FF |
226 | case '0': |
227 | case '1': | |
228 | case '2': | |
229 | case '3': | |
230 | case '4': | |
231 | case '5': | |
232 | case '6': | |
233 | case '7': | |
4f6fbcdc JH |
234 | /* octal */ |
235 | ac = ((ch - '0') << 6); | |
236 | if ((ch = *sp++) < '0' || '7' < ch) | |
237 | return NULL; | |
238 | ac |= ((ch - '0') << 3); | |
239 | if ((ch = *sp++) < '0' || '7' < ch) | |
240 | return NULL; | |
241 | ac |= (ch - '0'); | |
242 | ch = ac; | |
243 | break; | |
244 | default: | |
245 | return NULL; /* malformed */ | |
246 | } | |
247 | } | |
248 | EMIT(ch); | |
249 | } | |
250 | ||
251 | if (name) { | |
252 | *outp = 0; | |
253 | if (endp) | |
254 | *endp = sp; | |
255 | return name; | |
256 | } | |
257 | outp = name = xmalloc(count + 1); | |
258 | } | |
259 | } | |
260 | ||
9ef2b3cb JH |
261 | void write_name_quoted(const char *prefix, int prefix_len, |
262 | const char *name, int quote, FILE *out) | |
4f6fbcdc JH |
263 | { |
264 | int needquote; | |
265 | ||
266 | if (!quote) { | |
267 | no_quote: | |
9ef2b3cb JH |
268 | if (prefix_len) |
269 | fprintf(out, "%.*s", prefix_len, prefix); | |
4f6fbcdc JH |
270 | fputs(name, out); |
271 | return; | |
272 | } | |
273 | ||
274 | needquote = 0; | |
9ef2b3cb JH |
275 | if (prefix_len) |
276 | needquote = quote_c_style_counted(prefix, prefix_len, | |
277 | NULL, NULL, 0); | |
4f6fbcdc JH |
278 | if (!needquote) |
279 | needquote = quote_c_style(name, NULL, NULL, 0); | |
280 | if (needquote) { | |
281 | fputc('"', out); | |
9ef2b3cb JH |
282 | if (prefix_len) |
283 | quote_c_style_counted(prefix, prefix_len, | |
284 | NULL, out, 1); | |
4f6fbcdc JH |
285 | quote_c_style(name, NULL, out, 1); |
286 | fputc('"', out); | |
287 | } | |
288 | else | |
289 | goto no_quote; | |
290 | } |