quote.con commit Merge branch 'maint' (83877f8)
   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 */
  15#undef EMIT
  16#define EMIT(x) do { if (++len < n) *bp++ = (x); } while(0)
  17
  18static inline int need_bs_quote(char c)
  19{
  20        return (c == '\'' || c == '!');
  21}
  22
  23size_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;
  28
  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);
  38                }
  39        }
  40        EMIT('\'');
  41
  42        if ( n )
  43                *bp = 0;
  44
  45        return len;
  46}
  47
  48void 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
  65char *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
  74        return buf;
  75}
  76
  77char *sq_quote_argv(const char** argv, int count)
  78{
  79        char *buf, *to;
  80        int i;
  81        size_t len = 0;
  82
  83        /* Count argv if needed. */
  84        if (count < 0) {
  85                for (count = 0; argv[count]; count++)
  86                        ; /* just counting */
  87        }
  88
  89        /* Special case: no argv. */
  90        if (!count)
  91                return xcalloc(1,1);
  92
  93        /* Get destination buffer length. */
  94        for (i = 0; i < count; i++)
  95                len += sq_quote_buf(NULL, 0, argv[i]) + 1;
  96
  97        /* Alloc destination buffer. */
  98        to = buf = xmalloc(len + 1);
  99
 100        /* Copy into destination buffer. */
 101        for (i = 0; i < count; ++i) {
 102                *to++ = ' ';
 103                to += sq_quote_buf(to, len, argv[i]);
 104        }
 105
 106        return buf;
 107}
 108
 109/*
 110 * Append a string to a string buffer, with or without shell quoting.
 111 * Return true if the buffer overflowed.
 112 */
 113int add_to_string(char **ptrp, int *sizep, const char *str, int quote)
 114{
 115        char *p = *ptrp;
 116        int size = *sizep;
 117        int oc;
 118        int err = 0;
 119
 120        if (quote)
 121                oc = sq_quote_buf(p, size, str);
 122        else {
 123                oc = strlen(str);
 124                memcpy(p, str, (size <= oc) ? size - 1 : oc);
 125        }
 126
 127        if (size <= oc) {
 128                err = 1;
 129                oc = size - 1;
 130        }
 131
 132        *ptrp += oc;
 133        **ptrp = '\0';
 134        *sizep -= oc;
 135        return err;
 136}
 137
 138char *sq_dequote(char *arg)
 139{
 140        char *dst = arg;
 141        char *src = arg;
 142        char c;
 143
 144        if (*src != '\'')
 145                return NULL;
 146        for (;;) {
 147                c = *++src;
 148                if (!c)
 149                        return NULL;
 150                if (c != '\'') {
 151                        *dst++ = c;
 152                        continue;
 153                }
 154                /* We stepped out of sq */
 155                switch (*++src) {
 156                case '\0':
 157                        *dst = 0;
 158                        return arg;
 159                case '\\':
 160                        c = *++src;
 161                        if (need_bs_quote(c) && *++src == '\'') {
 162                                *dst++ = c;
 163                                continue;
 164                        }
 165                /* Fallthrough */
 166                default:
 167                        return NULL;
 168                }
 169        }
 170}
 171
 172/*
 173 * C-style name quoting.
 174 *
 175 * Does one of three things:
 176 *
 177 * (1) if outbuf and outfp are both NULL, inspect the input name and
 178 *     counts the number of bytes that are needed to hold c_style
 179 *     quoted version of name, counting the double quotes around
 180 *     it but not terminating NUL, and returns it.  However, if name
 181 *     does not need c_style quoting, it returns 0.
 182 *
 183 * (2) if outbuf is not NULL, it must point at a buffer large enough
 184 *     to hold the c_style quoted version of name, enclosing double
 185 *     quotes, and terminating NUL.  Fills outbuf with c_style quoted
 186 *     version of name enclosed in double-quote pair.  Return value
 187 *     is undefined.
 188 *
 189 * (3) if outfp is not NULL, outputs c_style quoted version of name,
 190 *     but not enclosed in double-quote pair.  Return value is undefined.
 191 */
 192
 193static int quote_c_style_counted(const char *name, int namelen,
 194                                 char *outbuf, FILE *outfp, int no_dq)
 195{
 196#undef EMIT
 197#define EMIT(c) \
 198        (outbuf ? (*outbuf++ = (c)) : outfp ? fputc(c, outfp) : (count++))
 199
 200#define EMITQ() EMIT('\\')
 201
 202        const char *sp;
 203        int ch, count = 0, needquote = 0;
 204
 205        if (!no_dq)
 206                EMIT('"');
 207        for (sp = name; sp < name + namelen; sp++) {
 208                ch = *sp;
 209                if (!ch)
 210                        break;
 211                if ((ch < ' ') || (ch == '"') || (ch == '\\') ||
 212                    (ch >= 0177)) {
 213                        needquote = 1;
 214                        switch (ch) {
 215                        case '\a': EMITQ(); ch = 'a'; break;
 216                        case '\b': EMITQ(); ch = 'b'; break;
 217                        case '\f': EMITQ(); ch = 'f'; break;
 218                        case '\n': EMITQ(); ch = 'n'; break;
 219                        case '\r': EMITQ(); ch = 'r'; break;
 220                        case '\t': EMITQ(); ch = 't'; break;
 221                        case '\v': EMITQ(); ch = 'v'; break;
 222
 223                        case '\\': /* fallthru */
 224                        case '"': EMITQ(); break;
 225                        default:
 226                                /* octal */
 227                                EMITQ();
 228                                EMIT(((ch >> 6) & 03) + '0');
 229                                EMIT(((ch >> 3) & 07) + '0');
 230                                ch = (ch & 07) + '0';
 231                                break;
 232                        }
 233                }
 234                EMIT(ch);
 235        }
 236        if (!no_dq)
 237                EMIT('"');
 238        if (outbuf)
 239                *outbuf = 0;
 240
 241        return needquote ? count : 0;
 242}
 243
 244int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq)
 245{
 246        int cnt = strlen(name);
 247        return quote_c_style_counted(name, cnt, outbuf, outfp, no_dq);
 248}
 249
 250/*
 251 * C-style name unquoting.
 252 *
 253 * Quoted should point at the opening double quote.  Returns
 254 * an allocated memory that holds unquoted name, which the caller
 255 * should free when done.  Updates endp pointer to point at
 256 * one past the ending double quote if given.
 257 */
 258
 259char *unquote_c_style(const char *quoted, const char **endp)
 260{
 261        const char *sp;
 262        char *name = NULL, *outp = NULL;
 263        int count = 0, ch, ac;
 264
 265#undef EMIT
 266#define EMIT(c) (outp ? (*outp++ = (c)) : (count++))
 267
 268        if (*quoted++ != '"')
 269                return NULL;
 270
 271        while (1) {
 272                /* first pass counts and allocates, second pass fills */
 273                for (sp = quoted; (ch = *sp++) != '"'; ) {
 274                        if (ch == '\\') {
 275                                switch (ch = *sp++) {
 276                                case 'a': ch = '\a'; break;
 277                                case 'b': ch = '\b'; break;
 278                                case 'f': ch = '\f'; break;
 279                                case 'n': ch = '\n'; break;
 280                                case 'r': ch = '\r'; break;
 281                                case 't': ch = '\t'; break;
 282                                case 'v': ch = '\v'; break;
 283
 284                                case '\\': case '"':
 285                                        break; /* verbatim */
 286
 287                                case '0':
 288                                case '1':
 289                                case '2':
 290                                case '3':
 291                                case '4':
 292                                case '5':
 293                                case '6':
 294                                case '7':
 295                                        /* octal */
 296                                        ac = ((ch - '0') << 6);
 297                                        if ((ch = *sp++) < '0' || '7' < ch)
 298                                                return NULL;
 299                                        ac |= ((ch - '0') << 3);
 300                                        if ((ch = *sp++) < '0' || '7' < ch)
 301                                                return NULL;
 302                                        ac |= (ch - '0');
 303                                        ch = ac;
 304                                        break;
 305                                default:
 306                                        return NULL; /* malformed */
 307                                }
 308                        }
 309                        EMIT(ch);
 310                }
 311
 312                if (name) {
 313                        *outp = 0;
 314                        if (endp)
 315                                *endp = sp;
 316                        return name;
 317                }
 318                outp = name = xmalloc(count + 1);
 319        }
 320}
 321
 322void write_name_quoted(const char *prefix, int prefix_len,
 323                       const char *name, int quote, FILE *out)
 324{
 325        int needquote;
 326
 327        if (!quote) {
 328        no_quote:
 329                if (prefix_len)
 330                        fprintf(out, "%.*s", prefix_len, prefix);
 331                fputs(name, out);
 332                return;
 333        }
 334
 335        needquote = 0;
 336        if (prefix_len)
 337                needquote = quote_c_style_counted(prefix, prefix_len,
 338                                                  NULL, NULL, 0);
 339        if (!needquote)
 340                needquote = quote_c_style(name, NULL, NULL, 0);
 341        if (needquote) {
 342                fputc('"', out);
 343                if (prefix_len)
 344                        quote_c_style_counted(prefix, prefix_len,
 345                                              NULL, out, 1);
 346                quote_c_style(name, NULL, out, 1);
 347                fputc('"', out);
 348        }
 349        else
 350                goto no_quote;
 351}
 352
 353/* quoting as a string literal for other languages */
 354
 355void perl_quote_print(FILE *stream, const char *src)
 356{
 357        const char sq = '\'';
 358        const char bq = '\\';
 359        char c;
 360
 361        fputc(sq, stream);
 362        while ((c = *src++)) {
 363                if (c == sq || c == bq)
 364                        fputc(bq, stream);
 365                fputc(c, stream);
 366        }
 367        fputc(sq, stream);
 368}
 369
 370void python_quote_print(FILE *stream, const char *src)
 371{
 372        const char sq = '\'';
 373        const char bq = '\\';
 374        const char nl = '\n';
 375        char c;
 376
 377        fputc(sq, stream);
 378        while ((c = *src++)) {
 379                if (c == nl) {
 380                        fputc(bq, stream);
 381                        fputc('n', stream);
 382                        continue;
 383                }
 384                if (c == sq || c == bq)
 385                        fputc(bq, stream);
 386                fputc(c, stream);
 387        }
 388        fputc(sq, stream);
 389}