quote.con commit Merge branch 'jc/daemon' (7bbf88c)
   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
 109char *sq_dequote(char *arg)
 110{
 111        char *dst = arg;
 112        char *src = arg;
 113        char c;
 114
 115        if (*src != '\'')
 116                return NULL;
 117        for (;;) {
 118                c = *++src;
 119                if (!c)
 120                        return NULL;
 121                if (c != '\'') {
 122                        *dst++ = c;
 123                        continue;
 124                }
 125                /* We stepped out of sq */
 126                switch (*++src) {
 127                case '\0':
 128                        *dst = 0;
 129                        return arg;
 130                case '\\':
 131                        c = *++src;
 132                        if (need_bs_quote(c) && *++src == '\'') {
 133                                *dst++ = c;
 134                                continue;
 135                        }
 136                /* Fallthrough */
 137                default:
 138                        return NULL;
 139                }
 140        }
 141}
 142
 143/*
 144 * C-style name quoting.
 145 *
 146 * Does one of three things:
 147 *
 148 * (1) if outbuf and outfp are both NULL, inspect the input name and
 149 *     counts the number of bytes that are needed to hold c_style
 150 *     quoted version of name, counting the double quotes around
 151 *     it but not terminating NUL, and returns it.  However, if name
 152 *     does not need c_style quoting, it returns 0.
 153 *
 154 * (2) if outbuf is not NULL, it must point at a buffer large enough
 155 *     to hold the c_style quoted version of name, enclosing double
 156 *     quotes, and terminating NUL.  Fills outbuf with c_style quoted
 157 *     version of name enclosed in double-quote pair.  Return value
 158 *     is undefined.
 159 *
 160 * (3) if outfp is not NULL, outputs c_style quoted version of name,
 161 *     but not enclosed in double-quote pair.  Return value is undefined.
 162 */
 163
 164static int quote_c_style_counted(const char *name, int namelen,
 165                                 char *outbuf, FILE *outfp, int no_dq)
 166{
 167#undef EMIT
 168#define EMIT(c) \
 169        (outbuf ? (*outbuf++ = (c)) : outfp ? fputc(c, outfp) : (count++))
 170
 171#define EMITQ() EMIT('\\')
 172
 173        const char *sp;
 174        int ch, count = 0, needquote = 0;
 175
 176        if (!no_dq)
 177                EMIT('"');
 178        for (sp = name; sp < name + namelen; sp++) {
 179                ch = *sp;
 180                if (!ch)
 181                        break;
 182                if ((ch < ' ') || (ch == '"') || (ch == '\\') ||
 183                    (ch == 0177)) {
 184                        needquote = 1;
 185                        switch (ch) {
 186                        case '\a': EMITQ(); ch = 'a'; break;
 187                        case '\b': EMITQ(); ch = 'b'; break;
 188                        case '\f': EMITQ(); ch = 'f'; break;
 189                        case '\n': EMITQ(); ch = 'n'; break;
 190                        case '\r': EMITQ(); ch = 'r'; break;
 191                        case '\t': EMITQ(); ch = 't'; break;
 192                        case '\v': EMITQ(); ch = 'v'; break;
 193
 194                        case '\\': /* fallthru */
 195                        case '"': EMITQ(); break;
 196                        default:
 197                                /* octal */
 198                                EMITQ();
 199                                EMIT(((ch >> 6) & 03) + '0');
 200                                EMIT(((ch >> 3) & 07) + '0');
 201                                ch = (ch & 07) + '0';
 202                                break;
 203                        }
 204                }
 205                EMIT(ch);
 206        }
 207        if (!no_dq)
 208                EMIT('"');
 209        if (outbuf)
 210                *outbuf = 0;
 211
 212        return needquote ? count : 0;
 213}
 214
 215int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq)
 216{
 217        int cnt = strlen(name);
 218        return quote_c_style_counted(name, cnt, outbuf, outfp, no_dq);
 219}
 220
 221/*
 222 * C-style name unquoting.
 223 *
 224 * Quoted should point at the opening double quote.  Returns
 225 * an allocated memory that holds unquoted name, which the caller
 226 * should free when done.  Updates endp pointer to point at
 227 * one past the ending double quote if given.
 228 */
 229
 230char *unquote_c_style(const char *quoted, const char **endp)
 231{
 232        const char *sp;
 233        char *name = NULL, *outp = NULL;
 234        int count = 0, ch, ac;
 235
 236#undef EMIT
 237#define EMIT(c) (outp ? (*outp++ = (c)) : (count++))
 238
 239        if (*quoted++ != '"')
 240                return NULL;
 241
 242        while (1) {
 243                /* first pass counts and allocates, second pass fills */
 244                for (sp = quoted; (ch = *sp++) != '"'; ) {
 245                        if (ch == '\\') {
 246                                switch (ch = *sp++) {
 247                                case 'a': ch = '\a'; break;
 248                                case 'b': ch = '\b'; break;
 249                                case 'f': ch = '\f'; break;
 250                                case 'n': ch = '\n'; break;
 251                                case 'r': ch = '\r'; break;
 252                                case 't': ch = '\t'; break;
 253                                case 'v': ch = '\v'; break;
 254
 255                                case '\\': case '"':
 256                                        break; /* verbatim */
 257
 258                                case '0':
 259                                case '1':
 260                                case '2':
 261                                case '3':
 262                                case '4':
 263                                case '5':
 264                                case '6':
 265                                case '7':
 266                                        /* octal */
 267                                        ac = ((ch - '0') << 6);
 268                                        if ((ch = *sp++) < '0' || '7' < ch)
 269                                                return NULL;
 270                                        ac |= ((ch - '0') << 3);
 271                                        if ((ch = *sp++) < '0' || '7' < ch)
 272                                                return NULL;
 273                                        ac |= (ch - '0');
 274                                        ch = ac;
 275                                        break;
 276                                default:
 277                                        return NULL; /* malformed */
 278                                }
 279                        }
 280                        EMIT(ch);
 281                }
 282
 283                if (name) {
 284                        *outp = 0;
 285                        if (endp)
 286                                *endp = sp;
 287                        return name;
 288                }
 289                outp = name = xmalloc(count + 1);
 290        }
 291}
 292
 293void write_name_quoted(const char *prefix, int prefix_len,
 294                       const char *name, int quote, FILE *out)
 295{
 296        int needquote;
 297
 298        if (!quote) {
 299        no_quote:
 300                if (prefix_len)
 301                        fprintf(out, "%.*s", prefix_len, prefix);
 302                fputs(name, out);
 303                return;
 304        }
 305
 306        needquote = 0;
 307        if (prefix_len)
 308                needquote = quote_c_style_counted(prefix, prefix_len,
 309                                                  NULL, NULL, 0);
 310        if (!needquote)
 311                needquote = quote_c_style(name, NULL, NULL, 0);
 312        if (needquote) {
 313                fputc('"', out);
 314                if (prefix_len)
 315                        quote_c_style_counted(prefix, prefix_len,
 316                                              NULL, out, 1);
 317                quote_c_style(name, NULL, out, 1);
 318                fputc('"', out);
 319        }
 320        else
 321                goto no_quote;
 322}