quote.con commit Merge branch 'ph/describe-match' (3960a95)
   1#include "cache.h"
   2#include "quote.h"
   3
   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
   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'
  13 *  a!b      ==> a'\!'b    ==> 'a'\!'b'
  14 */
  15static inline int need_bs_quote(char c)
  16{
  17        return (c == '\'' || c == '!');
  18}
  19
  20void sq_quote_buf(struct strbuf *dst, const char *src)
  21{
  22        char *to_free = NULL;
  23
  24        if (dst->buf == src)
  25                to_free = strbuf_detach(dst, NULL);
  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, '\'');
  36                }
  37        }
  38        strbuf_addch(dst, '\'');
  39        free(to_free);
  40}
  41
  42void 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
  59void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
  60{
  61        int i;
  62
  63        /* Copy into destination buffer. */
  64        strbuf_grow(dst, 255);
  65        for (i = 0; argv[i]; ++i) {
  66                strbuf_addch(dst, ' ');
  67                sq_quote_buf(dst, argv[i]);
  68                if (maxlen && dst->len > maxlen)
  69                        die("Too many or long arguments");
  70        }
  71}
  72
  73char *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
 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)
 114static 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
 127static inline int sq_must_quote(char c)
 128{
 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 */
 135static 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
 146/*
 147 * C-style name quoting.
 148 *
 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.
 154 *
 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).
 158 */
 159static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
 160                                    struct strbuf *sb, FILE *fp, int no_dq)
 161{
 162#undef EMIT
 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)
 175
 176        size_t len, count = 0;
 177        const char *p = name;
 178
 179        for (;;) {
 180                int ch;
 181
 182                len = next_quote_pos(p, maxlen);
 183                if (len == maxlen || !p[len])
 184                        break;
 185
 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');
 199                }
 200        }
 201
 202        EMITBUF(p, len);
 203        if (p == name)   /* no ending quote needed */
 204                return 0;
 205
 206        if (!no_dq)
 207                EMIT('"');
 208        return count;
 209}
 210
 211size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
 212{
 213        return quote_c_style_counted(name, -1, sb, fp, nodq);
 214}
 215
 216void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, int nodq)
 217{
 218        if (quote_c_style(prefix, NULL, NULL, 0) ||
 219            quote_c_style(path, NULL, NULL, 0)) {
 220                if (!nodq)
 221                        strbuf_addch(sb, '"');
 222                quote_c_style(prefix, sb, NULL, 1);
 223                quote_c_style(path, sb, NULL, 1);
 224                if (!nodq)
 225                        strbuf_addch(sb, '"');
 226        } else {
 227                strbuf_addstr(sb, prefix);
 228                strbuf_addstr(sb, path);
 229        }
 230}
 231
 232void write_name_quoted(const char *name, FILE *fp, int terminator)
 233{
 234        if (terminator) {
 235                quote_c_style(name, NULL, fp, 0);
 236        } else {
 237                fputs(name, fp);
 238        }
 239        fputc(terminator, fp);
 240}
 241
 242extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
 243                                 const char *name, FILE *fp, int terminator)
 244{
 245        int needquote = 0;
 246
 247        if (terminator) {
 248                needquote = next_quote_pos(pfx, pfxlen) < pfxlen
 249                        || name[next_quote_pos(name, -1)];
 250        }
 251        if (needquote) {
 252                fputc('"', fp);
 253                quote_c_style_counted(pfx, pfxlen, NULL, fp, 1);
 254                quote_c_style(name, NULL, fp, 1);
 255                fputc('"', fp);
 256        } else {
 257                fwrite(pfx, pfxlen, 1, fp);
 258                fputs(name, fp);
 259        }
 260        fputc(terminator, fp);
 261}
 262
 263/*
 264 * C-style name unquoting.
 265 *
 266 * Quoted should point at the opening double quote.
 267 * + Returns 0 if it was able to unquote the string properly, and appends the
 268 *   result in the strbuf `sb'.
 269 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
 270 *   that this function will allocate memory in the strbuf, so calling
 271 *   strbuf_release is mandatory whichever result unquote_c_style returns.
 272 *
 273 * Updates endp pointer to point at one past the ending double quote if given.
 274 */
 275int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
 276{
 277        size_t oldlen = sb->len, len;
 278        int ch, ac;
 279
 280        if (*quoted++ != '"')
 281                return -1;
 282
 283        for (;;) {
 284                len = strcspn(quoted, "\"\\");
 285                strbuf_add(sb, quoted, len);
 286                quoted += len;
 287
 288                switch (*quoted++) {
 289                  case '"':
 290                        if (endp)
 291                                *endp = quoted + 1;
 292                        return 0;
 293                  case '\\':
 294                        break;
 295                  default:
 296                        goto error;
 297                }
 298
 299                switch ((ch = *quoted++)) {
 300                case 'a': ch = '\a'; break;
 301                case 'b': ch = '\b'; break;
 302                case 'f': ch = '\f'; break;
 303                case 'n': ch = '\n'; break;
 304                case 'r': ch = '\r'; break;
 305                case 't': ch = '\t'; break;
 306                case 'v': ch = '\v'; break;
 307
 308                case '\\': case '"':
 309                        break; /* verbatim */
 310
 311                /* octal values with first digit over 4 overflow */
 312                case '0': case '1': case '2': case '3':
 313                                        ac = ((ch - '0') << 6);
 314                        if ((ch = *quoted++) < '0' || '7' < ch)
 315                                goto error;
 316                                        ac |= ((ch - '0') << 3);
 317                        if ((ch = *quoted++) < '0' || '7' < ch)
 318                                goto error;
 319                                        ac |= (ch - '0');
 320                                        ch = ac;
 321                                        break;
 322                                default:
 323                        goto error;
 324                        }
 325                strbuf_addch(sb, ch);
 326                }
 327
 328  error:
 329        strbuf_setlen(sb, oldlen);
 330        return -1;
 331}
 332
 333/* quoting as a string literal for other languages */
 334
 335void perl_quote_print(FILE *stream, const char *src)
 336{
 337        const char sq = '\'';
 338        const char bq = '\\';
 339        char c;
 340
 341        fputc(sq, stream);
 342        while ((c = *src++)) {
 343                if (c == sq || c == bq)
 344                        fputc(bq, stream);
 345                fputc(c, stream);
 346        }
 347        fputc(sq, stream);
 348}
 349
 350void python_quote_print(FILE *stream, const char *src)
 351{
 352        const char sq = '\'';
 353        const char bq = '\\';
 354        const char nl = '\n';
 355        char c;
 356
 357        fputc(sq, stream);
 358        while ((c = *src++)) {
 359                if (c == nl) {
 360                        fputc(bq, stream);
 361                        fputc('n', stream);
 362                        continue;
 363                }
 364                if (c == sq || c == bq)
 365                        fputc(bq, stream);
 366                fputc(c, stream);
 367        }
 368        fputc(sq, stream);
 369}
 370
 371void tcl_quote_print(FILE *stream, const char *src)
 372{
 373        char c;
 374
 375        fputc('"', stream);
 376        while ((c = *src++)) {
 377                switch (c) {
 378                case '[': case ']':
 379                case '{': case '}':
 380                case '$': case '\\': case '"':
 381                        fputc('\\', stream);
 382                default:
 383                        fputc(c, stream);
 384                        break;
 385                case '\f':
 386                        fputs("\\f", stream);
 387                        break;
 388                case '\r':
 389                        fputs("\\r", stream);
 390                        break;
 391                case '\n':
 392                        fputs("\\n", stream);
 393                        break;
 394                case '\t':
 395                        fputs("\\t", stream);
 396                        break;
 397                case '\v':
 398                        fputs("\\v", stream);
 399                        break;
 400                }
 401        }
 402        fputc('"', stream);
 403}