Add git-shell.
[git/jnareb-git/bp-gitweb.git] / quote.c
blobe662a7da71e39c1ffc3d1a4bf485fda17df5e907
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
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'
15 #undef EMIT
16 #define EMIT(x) ( (++len < n) && (*bp++ = (x)) )
18 static inline int need_bs_quote(char c)
20 return (c == '\'' || c == '!');
23 size_t sq_quote_buf(char *dst, size_t n, const char *src)
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);
40 EMIT('\'');
42 if ( n )
43 *bp = 0;
45 return len;
48 char *sq_quote(const char *src)
50 char *buf;
51 size_t cnt;
53 cnt = sq_quote_buf(NULL, 0, src) + 1;
54 buf = xmalloc(cnt);
55 sq_quote_buf(buf, cnt, src);
57 return buf;
60 char *sq_dequote(char *arg)
62 char *dst = arg;
63 char *src = arg;
64 char c;
66 if (*src != '\'')
67 return NULL;
68 for (;;) {
69 c = *++src;
70 if (!c)
71 return NULL;
72 if (c != '\'') {
73 *dst++ = c;
74 continue;
76 /* We stepped out of sq */
77 switch (*++src) {
78 case '\0':
79 *dst = 0;
80 return arg;
81 case '\\':
82 c = *++src;
83 if (need_bs_quote(c) && *++src == '\'') {
84 *dst++ = c;
85 continue;
87 /* Fallthrough */
88 default:
89 return NULL;
95 * C-style name quoting.
97 * Does one of three things:
99 * (1) if outbuf and outfp are both NULL, inspect the input name and
100 * counts the number of bytes that are needed to hold c_style
101 * quoted version of name, counting the double quotes around
102 * it but not terminating NUL, and returns it. However, if name
103 * does not need c_style quoting, it returns 0.
105 * (2) if outbuf is not NULL, it must point at a buffer large enough
106 * to hold the c_style quoted version of name, enclosing double
107 * quotes, and terminating NUL. Fills outbuf with c_style quoted
108 * version of name enclosed in double-quote pair. Return value
109 * is undefined.
111 * (3) if outfp is not NULL, outputs c_style quoted version of name,
112 * but not enclosed in double-quote pair. Return value is undefined.
115 int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq)
117 #undef EMIT
118 #define EMIT(c) \
119 (outbuf ? (*outbuf++ = (c)) : outfp ? fputc(c, outfp) : (count++))
121 #define EMITQ() EMIT('\\')
123 const char *sp;
124 int ch, count = 0, needquote = 0;
126 if (!no_dq)
127 EMIT('"');
128 for (sp = name; (ch = *sp++); ) {
130 if ((ch < ' ') || (ch == '"') || (ch == '\\') ||
131 (ch == 0177)) {
132 needquote = 1;
133 switch (ch) {
134 case '\a': EMITQ(); ch = 'a'; break;
135 case '\b': EMITQ(); ch = 'b'; break;
136 case '\f': EMITQ(); ch = 'f'; break;
137 case '\n': EMITQ(); ch = 'n'; break;
138 case '\r': EMITQ(); ch = 'r'; break;
139 case '\t': EMITQ(); ch = 't'; break;
140 case '\v': EMITQ(); ch = 'v'; break;
142 case '\\': /* fallthru */
143 case '"': EMITQ(); break;
144 case ' ':
145 break;
146 default:
147 /* octal */
148 EMITQ();
149 EMIT(((ch >> 6) & 03) + '0');
150 EMIT(((ch >> 3) & 07) + '0');
151 ch = (ch & 07) + '0';
152 break;
155 EMIT(ch);
157 if (!no_dq)
158 EMIT('"');
159 if (outbuf)
160 *outbuf = 0;
162 return needquote ? count : 0;
166 * C-style name unquoting.
168 * Quoted should point at the opening double quote. Returns
169 * an allocated memory that holds unquoted name, which the caller
170 * should free when done. Updates endp pointer to point at
171 * one past the ending double quote if given.
174 char *unquote_c_style(const char *quoted, const char **endp)
176 const char *sp;
177 char *name = NULL, *outp = NULL;
178 int count = 0, ch, ac;
180 #undef EMIT
181 #define EMIT(c) (outp ? (*outp++ = (c)) : (count++))
183 if (*quoted++ != '"')
184 return NULL;
186 while (1) {
187 /* first pass counts and allocates, second pass fills */
188 for (sp = quoted; (ch = *sp++) != '"'; ) {
189 if (ch == '\\') {
190 switch (ch = *sp++) {
191 case 'a': ch = '\a'; break;
192 case 'b': ch = '\b'; break;
193 case 'f': ch = '\f'; break;
194 case 'n': ch = '\n'; break;
195 case 'r': ch = '\r'; break;
196 case 't': ch = '\t'; break;
197 case 'v': ch = '\v'; break;
199 case '\\': case '"':
200 break; /* verbatim */
202 case '0'...'7':
203 /* octal */
204 ac = ((ch - '0') << 6);
205 if ((ch = *sp++) < '0' || '7' < ch)
206 return NULL;
207 ac |= ((ch - '0') << 3);
208 if ((ch = *sp++) < '0' || '7' < ch)
209 return NULL;
210 ac |= (ch - '0');
211 ch = ac;
212 break;
213 default:
214 return NULL; /* malformed */
217 EMIT(ch);
220 if (name) {
221 *outp = 0;
222 if (endp)
223 *endp = sp;
224 return name;
226 outp = name = xmalloc(count + 1);
230 void write_name_quoted(const char *prefix, const char *name,
231 int quote, FILE *out)
233 int needquote;
235 if (!quote) {
236 no_quote:
237 if (prefix && prefix[0])
238 fputs(prefix, out);
239 fputs(name, out);
240 return;
243 needquote = 0;
244 if (prefix && prefix[0])
245 needquote = quote_c_style(prefix, NULL, NULL, 0);
246 if (!needquote)
247 needquote = quote_c_style(name, NULL, NULL, 0);
248 if (needquote) {
249 fputc('"', out);
250 if (prefix && prefix[0])
251 quote_c_style(prefix, NULL, out, 1);
252 quote_c_style(name, NULL, out, 1);
253 fputc('"', out);
255 else
256 goto no_quote;