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