Junio C Hamano | 6fb737b | 2005-07-07 23:58:32 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "quote.h" |
| 3 | |
| 4 | /* Help to copy the thing properly quoted for the shell safety. |
H. Peter Anvin | 77d604c | 2005-10-10 14:46:10 -0700 | [diff] [blame] | 5 | * any single quote is replaced with '\'', any exclamation point |
| 6 | * is replaced with '\!', and the whole thing is enclosed in a |
Junio C Hamano | 6fb737b | 2005-07-07 23:58:32 -0700 | [diff] [blame] | 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' |
H. Peter Anvin | 77d604c | 2005-10-10 14:46:10 -0700 | [diff] [blame] | 13 | * a!b ==> a'\!'b ==> 'a'\!'b' |
Junio C Hamano | 6fb737b | 2005-07-07 23:58:32 -0700 | [diff] [blame] | 14 | */ |
Linus Torvalds | 35eb2d3 | 2005-10-23 14:30:45 -0700 | [diff] [blame] | 15 | static inline int need_bs_quote(char c) |
| 16 | { |
| 17 | return (c == '\'' || c == '!'); |
| 18 | } |
| 19 | |
Pierre Habouzit | 7a33bcb | 2007-09-20 00:42:13 +0200 | [diff] [blame] | 20 | void sq_quote_buf(struct strbuf *dst, const char *src) |
Junio C Hamano | 6fb737b | 2005-07-07 23:58:32 -0700 | [diff] [blame] | 21 | { |
Pierre Habouzit | 7a33bcb | 2007-09-20 00:42:13 +0200 | [diff] [blame] | 22 | char *to_free = NULL; |
Junio C Hamano | 6fb737b | 2005-07-07 23:58:32 -0700 | [diff] [blame] | 23 | |
Pierre Habouzit | 7a33bcb | 2007-09-20 00:42:13 +0200 | [diff] [blame] | 24 | if (dst->buf == src) |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 25 | to_free = strbuf_detach(dst, NULL); |
Pierre Habouzit | 7a33bcb | 2007-09-20 00:42:13 +0200 | [diff] [blame] | 26 | |
| 27 | strbuf_addch(dst, '\''); |
| 28 | while (*src) { |
| 29 | size_t len = strcspn(src, "'\\"); |
| 30 | strbuf_add(dst, src, len); |
| 31 | src += len; |
| 32 | while (need_bs_quote(*src)) { |
| 33 | strbuf_addstr(dst, "'\\"); |
| 34 | strbuf_addch(dst, *src++); |
| 35 | strbuf_addch(dst, '\''); |
Junio C Hamano | 6fb737b | 2005-07-07 23:58:32 -0700 | [diff] [blame] | 36 | } |
| 37 | } |
Pierre Habouzit | 7a33bcb | 2007-09-20 00:42:13 +0200 | [diff] [blame] | 38 | strbuf_addch(dst, '\''); |
| 39 | free(to_free); |
H. Peter Anvin | 77d604c | 2005-10-10 14:46:10 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Matthias Lederhofer | 575ba9d | 2006-06-25 15:56:18 +0200 | [diff] [blame] | 42 | void sq_quote_print(FILE *stream, const char *src) |
| 43 | { |
| 44 | char c; |
| 45 | |
| 46 | fputc('\'', stream); |
| 47 | while ((c = *src++)) { |
| 48 | if (need_bs_quote(c)) { |
| 49 | fputs("'\\", stream); |
| 50 | fputc(c, stream); |
| 51 | fputc('\'', stream); |
| 52 | } else { |
| 53 | fputc(c, stream); |
| 54 | } |
| 55 | } |
| 56 | fputc('\'', stream); |
| 57 | } |
| 58 | |
Pierre Habouzit | 7a33bcb | 2007-09-20 00:42:13 +0200 | [diff] [blame] | 59 | void sq_quote_argv(struct strbuf *dst, const char** argv, int count, |
| 60 | size_t maxlen) |
Christian Couder | 7cf6720 | 2006-08-31 08:42:11 +0200 | [diff] [blame] | 61 | { |
Christian Couder | 7cf6720 | 2006-08-31 08:42:11 +0200 | [diff] [blame] | 62 | int i; |
Christian Couder | 7cf6720 | 2006-08-31 08:42:11 +0200 | [diff] [blame] | 63 | |
| 64 | /* Count argv if needed. */ |
| 65 | if (count < 0) { |
| 66 | for (count = 0; argv[count]; count++) |
| 67 | ; /* just counting */ |
| 68 | } |
| 69 | |
Christian Couder | 7cf6720 | 2006-08-31 08:42:11 +0200 | [diff] [blame] | 70 | /* Copy into destination buffer. */ |
Pierre Habouzit | 7a33bcb | 2007-09-20 00:42:13 +0200 | [diff] [blame] | 71 | strbuf_grow(dst, 32 * count); |
Christian Couder | 7cf6720 | 2006-08-31 08:42:11 +0200 | [diff] [blame] | 72 | for (i = 0; i < count; ++i) { |
Pierre Habouzit | 7a33bcb | 2007-09-20 00:42:13 +0200 | [diff] [blame] | 73 | strbuf_addch(dst, ' '); |
| 74 | sq_quote_buf(dst, argv[i]); |
| 75 | if (maxlen && dst->len > maxlen) |
| 76 | die("Too many or long arguments"); |
Christian Couder | 7cf6720 | 2006-08-31 08:42:11 +0200 | [diff] [blame] | 77 | } |
Christian Couder | 86257aa | 2006-09-11 06:59:22 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Linus Torvalds | 35eb2d3 | 2005-10-23 14:30:45 -0700 | [diff] [blame] | 80 | char *sq_dequote(char *arg) |
| 81 | { |
| 82 | char *dst = arg; |
| 83 | char *src = arg; |
| 84 | char c; |
| 85 | |
| 86 | if (*src != '\'') |
| 87 | return NULL; |
| 88 | for (;;) { |
| 89 | c = *++src; |
| 90 | if (!c) |
| 91 | return NULL; |
| 92 | if (c != '\'') { |
| 93 | *dst++ = c; |
| 94 | continue; |
| 95 | } |
| 96 | /* We stepped out of sq */ |
| 97 | switch (*++src) { |
| 98 | case '\0': |
| 99 | *dst = 0; |
| 100 | return arg; |
| 101 | case '\\': |
| 102 | c = *++src; |
| 103 | if (need_bs_quote(c) && *++src == '\'') { |
| 104 | *dst++ = c; |
| 105 | continue; |
| 106 | } |
| 107 | /* Fallthrough */ |
| 108 | default: |
| 109 | return NULL; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 114 | /* 1 means: quote as octal |
| 115 | * 0 means: quote as octal if (quote_path_fully) |
| 116 | * -1 means: never quote |
| 117 | * c: quote as "\\c" |
| 118 | */ |
| 119 | #define X8(x) x, x, x, x, x, x, x, x |
| 120 | #define X16(x) X8(x), X8(x) |
| 121 | static signed char const sq_lookup[256] = { |
| 122 | /* 0 1 2 3 4 5 6 7 */ |
| 123 | /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a', |
| 124 | /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1, |
| 125 | /* 0x10 */ X16(1), |
| 126 | /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1, |
| 127 | /* 0x28 */ X16(-1), X16(-1), X16(-1), |
| 128 | /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1, |
| 129 | /* 0x60 */ X16(-1), X8(-1), |
| 130 | /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1, |
| 131 | /* 0x80 */ /* set to 0 */ |
| 132 | }; |
| 133 | |
| 134 | static inline int sq_must_quote(char c) { |
| 135 | return sq_lookup[(unsigned char)c] + quote_path_fully > 0; |
| 136 | } |
| 137 | |
| 138 | /* returns the longest prefix not needing a quote up to maxlen if positive. |
| 139 | This stops at the first \0 because it's marked as a character needing an |
| 140 | escape */ |
| 141 | static size_t next_quote_pos(const char *s, ssize_t maxlen) |
| 142 | { |
| 143 | size_t len; |
| 144 | if (maxlen < 0) { |
| 145 | for (len = 0; !sq_must_quote(s[len]); len++); |
| 146 | } else { |
| 147 | for (len = 0; len < maxlen && !sq_must_quote(s[len]); len++); |
| 148 | } |
| 149 | return len; |
| 150 | } |
| 151 | |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 152 | /* |
| 153 | * C-style name quoting. |
| 154 | * |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 155 | * (1) if sb and fp are both NULL, inspect the input name and counts the |
| 156 | * number of bytes that are needed to hold c_style quoted version of name, |
| 157 | * counting the double quotes around it but not terminating NUL, and |
| 158 | * returns it. |
| 159 | * However, if name does not need c_style quoting, it returns 0. |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 160 | * |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 161 | * (2) if sb or fp are not NULL, it emits the c_style quoted version |
| 162 | * of name, enclosed with double quotes if asked and needed only. |
| 163 | * Return value is the same as in (1). |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 164 | */ |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 165 | static size_t quote_c_style_counted(const char *name, ssize_t maxlen, |
| 166 | struct strbuf *sb, FILE *fp, int no_dq) |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 167 | { |
| 168 | #undef EMIT |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 169 | #define EMIT(c) \ |
| 170 | do { \ |
| 171 | if (sb) strbuf_addch(sb, (c)); \ |
| 172 | if (fp) fputc((c), fp); \ |
| 173 | count++; \ |
| 174 | } while (0) |
| 175 | #define EMITBUF(s, l) \ |
| 176 | do { \ |
| 177 | if (sb) strbuf_add(sb, (s), (l)); \ |
| 178 | if (fp) fwrite((s), (l), 1, fp); \ |
| 179 | count += (l); \ |
| 180 | } while (0) |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 181 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 182 | size_t len, count = 0; |
| 183 | const char *p = name; |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 184 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 185 | for (;;) { |
| 186 | int ch; |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 187 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 188 | len = next_quote_pos(p, maxlen); |
| 189 | if (len == maxlen || !p[len]) |
Pavel Roskin | 50e7b06 | 2005-12-21 15:35:48 -0500 | [diff] [blame] | 190 | break; |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 191 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 192 | if (!no_dq && p == name) |
| 193 | EMIT('"'); |
| 194 | |
| 195 | EMITBUF(p, len); |
| 196 | EMIT('\\'); |
| 197 | p += len; |
| 198 | ch = (unsigned char)*p++; |
| 199 | if (sq_lookup[ch] >= ' ') { |
| 200 | EMIT(sq_lookup[ch]); |
| 201 | } else { |
| 202 | EMIT(((ch >> 6) & 03) + '0'); |
| 203 | EMIT(((ch >> 3) & 07) + '0'); |
| 204 | EMIT(((ch >> 0) & 07) + '0'); |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 205 | } |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 206 | } |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 207 | |
| 208 | EMITBUF(p, len); |
| 209 | if (p == name) /* no ending quote needed */ |
| 210 | return 0; |
| 211 | |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 212 | if (!no_dq) |
| 213 | EMIT('"'); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 214 | return count; |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 217 | size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq) |
Junio C Hamano | 9ef2b3c | 2005-11-28 22:55:25 -0800 | [diff] [blame] | 218 | { |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 219 | return quote_c_style_counted(name, -1, sb, fp, nodq); |
| 220 | } |
| 221 | |
| 222 | void write_name_quoted(const char *name, FILE *fp, int terminator) |
| 223 | { |
| 224 | if (terminator) { |
| 225 | quote_c_style(name, NULL, fp, 0); |
| 226 | } else { |
| 227 | fputs(name, fp); |
| 228 | } |
| 229 | fputc(terminator, fp); |
| 230 | } |
| 231 | |
| 232 | extern void write_name_quotedpfx(const char *pfx, size_t pfxlen, |
| 233 | const char *name, FILE *fp, int terminator) |
| 234 | { |
| 235 | int needquote = 0; |
| 236 | |
| 237 | if (terminator) { |
| 238 | needquote = next_quote_pos(pfx, pfxlen) < pfxlen |
| 239 | || name[next_quote_pos(name, -1)]; |
| 240 | } |
| 241 | if (needquote) { |
| 242 | fputc('"', fp); |
| 243 | quote_c_style_counted(pfx, pfxlen, NULL, fp, 1); |
| 244 | quote_c_style(name, NULL, fp, 1); |
| 245 | fputc('"', fp); |
| 246 | } else { |
| 247 | fwrite(pfx, pfxlen, 1, fp); |
| 248 | fputs(name, fp); |
| 249 | } |
| 250 | fputc(terminator, fp); |
Junio C Hamano | 9ef2b3c | 2005-11-28 22:55:25 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 253 | /* |
| 254 | * C-style name unquoting. |
| 255 | * |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 256 | * Quoted should point at the opening double quote. |
| 257 | * + Returns 0 if it was able to unquote the string properly, and appends the |
| 258 | * result in the strbuf `sb'. |
| 259 | * + Returns -1 in case of error, and doesn't touch the strbuf. Though note |
| 260 | * that this function will allocate memory in the strbuf, so calling |
| 261 | * strbuf_release is mandatory whichever result unquote_c_style returns. |
| 262 | * |
| 263 | * Updates endp pointer to point at one past the ending double quote if given. |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 264 | */ |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 265 | int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp) |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 266 | { |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 267 | size_t oldlen = sb->len, len; |
| 268 | int ch, ac; |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 269 | |
| 270 | if (*quoted++ != '"') |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 271 | return -1; |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 272 | |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 273 | for (;;) { |
| 274 | len = strcspn(quoted, "\"\\"); |
| 275 | strbuf_add(sb, quoted, len); |
| 276 | quoted += len; |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 277 | |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 278 | switch (*quoted++) { |
| 279 | case '"': |
| 280 | if (endp) |
| 281 | *endp = quoted + 1; |
| 282 | return 0; |
| 283 | case '\\': |
| 284 | break; |
| 285 | default: |
| 286 | goto error; |
| 287 | } |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 288 | |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 289 | switch ((ch = *quoted++)) { |
| 290 | case 'a': ch = '\a'; break; |
| 291 | case 'b': ch = '\b'; break; |
| 292 | case 'f': ch = '\f'; break; |
| 293 | case 'n': ch = '\n'; break; |
| 294 | case 'r': ch = '\r'; break; |
| 295 | case 't': ch = '\t'; break; |
| 296 | case 'v': ch = '\v'; break; |
| 297 | |
| 298 | case '\\': case '"': |
| 299 | break; /* verbatim */ |
| 300 | |
| 301 | /* octal values with first digit over 4 overflow */ |
| 302 | case '0': case '1': case '2': case '3': |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 303 | ac = ((ch - '0') << 6); |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 304 | if ((ch = *quoted++) < '0' || '7' < ch) |
| 305 | goto error; |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 306 | ac |= ((ch - '0') << 3); |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 307 | if ((ch = *quoted++) < '0' || '7' < ch) |
| 308 | goto error; |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 309 | ac |= (ch - '0'); |
| 310 | ch = ac; |
| 311 | break; |
| 312 | default: |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 313 | goto error; |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 314 | } |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 315 | strbuf_addch(sb, ch); |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 318 | error: |
| 319 | strbuf_setlen(sb, oldlen); |
| 320 | return -1; |
Junio C Hamano | 4f6fbcd | 2005-10-14 21:54:47 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 323 | /* quoting as a string literal for other languages */ |
| 324 | |
| 325 | void perl_quote_print(FILE *stream, const char *src) |
| 326 | { |
| 327 | const char sq = '\''; |
| 328 | const char bq = '\\'; |
| 329 | char c; |
| 330 | |
| 331 | fputc(sq, stream); |
| 332 | while ((c = *src++)) { |
| 333 | if (c == sq || c == bq) |
| 334 | fputc(bq, stream); |
| 335 | fputc(c, stream); |
| 336 | } |
| 337 | fputc(sq, stream); |
| 338 | } |
| 339 | |
| 340 | void python_quote_print(FILE *stream, const char *src) |
| 341 | { |
| 342 | const char sq = '\''; |
| 343 | const char bq = '\\'; |
| 344 | const char nl = '\n'; |
| 345 | char c; |
| 346 | |
| 347 | fputc(sq, stream); |
| 348 | while ((c = *src++)) { |
| 349 | if (c == nl) { |
| 350 | fputc(bq, stream); |
| 351 | fputc('n', stream); |
| 352 | continue; |
| 353 | } |
| 354 | if (c == sq || c == bq) |
| 355 | fputc(bq, stream); |
| 356 | fputc(c, stream); |
| 357 | } |
| 358 | fputc(sq, stream); |
| 359 | } |
Shawn O. Pearce | 5558e55 | 2007-01-28 02:39:13 -0500 | [diff] [blame] | 360 | |
| 361 | void tcl_quote_print(FILE *stream, const char *src) |
| 362 | { |
| 363 | char c; |
| 364 | |
| 365 | fputc('"', stream); |
| 366 | while ((c = *src++)) { |
| 367 | switch (c) { |
| 368 | case '[': case ']': |
| 369 | case '{': case '}': |
| 370 | case '$': case '\\': case '"': |
| 371 | fputc('\\', stream); |
| 372 | default: |
| 373 | fputc(c, stream); |
| 374 | break; |
| 375 | case '\f': |
| 376 | fputs("\\f", stream); |
| 377 | break; |
| 378 | case '\r': |
| 379 | fputs("\\r", stream); |
| 380 | break; |
| 381 | case '\n': |
| 382 | fputs("\\n", stream); |
| 383 | break; |
| 384 | case '\t': |
| 385 | fputs("\\t", stream); |
| 386 | break; |
| 387 | case '\v': |
| 388 | fputs("\\v", stream); |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | fputc('"', stream); |
| 393 | } |