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