quote.con commit Merge branch 'js/diffstat' into next (4d2909e)
   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) ( (++len < n) && (*bp++ = (x)) )
  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
  48char *sq_quote(const char *src)
  49{
  50        char *buf;
  51        size_t cnt;
  52
  53        cnt = sq_quote_buf(NULL, 0, src) + 1;
  54        buf = xmalloc(cnt);
  55        sq_quote_buf(buf, cnt, src);
  56
  57        return buf;
  58}
  59
  60char *sq_dequote(char *arg)
  61{
  62        char *dst = arg;
  63        char *src = arg;
  64        char c;
  65
  66        if (*src != '\'')
  67                return NULL;
  68        for (;;) {
  69                c = *++src;
  70                if (!c)
  71                        return NULL;
  72                if (c != '\'') {
  73                        *dst++ = c;
  74                        continue;
  75                }
  76                /* We stepped out of sq */
  77                switch (*++src) {
  78                case '\0':
  79                        *dst = 0;
  80                        return arg;
  81                case '\\':
  82                        c = *++src;
  83                        if (need_bs_quote(c) && *++src == '\'') {
  84                                *dst++ = c;
  85                                continue;
  86                        }
  87                /* Fallthrough */
  88                default:
  89                        return NULL;
  90                }
  91        }
  92}
  93
  94/*
  95 * C-style name quoting.
  96 *
  97 * Does one of three things:
  98 *
  99 * (1) if outbuf and outfp are both NULL, inspect the input name and
 100 *     counts the number of bytes that are needed to hold c_style
 101 *     quoted version of name, counting the double quotes around
 102 *     it but not terminating NUL, and returns it.  However, if name
 103 *     does not need c_style quoting, it returns 0.
 104 *
 105 * (2) if outbuf is not NULL, it must point at a buffer large enough
 106 *     to hold the c_style quoted version of name, enclosing double
 107 *     quotes, and terminating NUL.  Fills outbuf with c_style quoted
 108 *     version of name enclosed in double-quote pair.  Return value
 109 *     is undefined.
 110 *
 111 * (3) if outfp is not NULL, outputs c_style quoted version of name,
 112 *     but not enclosed in double-quote pair.  Return value is undefined.
 113 */
 114
 115static int quote_c_style_counted(const char *name, int namelen,
 116                                 char *outbuf, FILE *outfp, int no_dq)
 117{
 118#undef EMIT
 119#define EMIT(c) \
 120        (outbuf ? (*outbuf++ = (c)) : outfp ? fputc(c, outfp) : (count++))
 121
 122#define EMITQ() EMIT('\\')
 123
 124        const char *sp;
 125        int ch, count = 0, needquote = 0;
 126
 127        if (!no_dq)
 128                EMIT('"');
 129        for (sp = name; sp < name + namelen; sp++) {
 130                ch = *sp;
 131                if (!ch)
 132                        break;
 133                if ((ch < ' ') || (ch == '"') || (ch == '\\') ||
 134                    (ch == 0177)) {
 135                        needquote = 1;
 136                        switch (ch) {
 137                        case '\a': EMITQ(); ch = 'a'; break;
 138                        case '\b': EMITQ(); ch = 'b'; break;
 139                        case '\f': EMITQ(); ch = 'f'; break;
 140                        case '\n': EMITQ(); ch = 'n'; break;
 141                        case '\r': EMITQ(); ch = 'r'; break;
 142                        case '\t': EMITQ(); ch = 't'; break;
 143                        case '\v': EMITQ(); ch = 'v'; break;
 144
 145                        case '\\': /* fallthru */
 146                        case '"': EMITQ(); break;
 147                        case ' ':
 148                                break;
 149                        default:
 150                                /* octal */
 151                                EMITQ();
 152                                EMIT(((ch >> 6) & 03) + '0');
 153                                EMIT(((ch >> 3) & 07) + '0');
 154                                ch = (ch & 07) + '0';
 155                                break;
 156                        }
 157                }
 158                EMIT(ch);
 159        }
 160        if (!no_dq)
 161                EMIT('"');
 162        if (outbuf)
 163                *outbuf = 0;
 164
 165        return needquote ? count : 0;
 166}
 167
 168int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq)
 169{
 170        int cnt = strlen(name);
 171        return quote_c_style_counted(name, cnt, outbuf, outfp, no_dq);
 172}
 173
 174/*
 175 * C-style name unquoting.
 176 *
 177 * Quoted should point at the opening double quote.  Returns
 178 * an allocated memory that holds unquoted name, which the caller
 179 * should free when done.  Updates endp pointer to point at
 180 * one past the ending double quote if given.
 181 */
 182
 183char *unquote_c_style(const char *quoted, const char **endp)
 184{
 185        const char *sp;
 186        char *name = NULL, *outp = NULL;
 187        int count = 0, ch, ac;
 188
 189#undef EMIT
 190#define EMIT(c) (outp ? (*outp++ = (c)) : (count++))
 191
 192        if (*quoted++ != '"')
 193                return NULL;
 194
 195        while (1) {
 196                /* first pass counts and allocates, second pass fills */
 197                for (sp = quoted; (ch = *sp++) != '"'; ) {
 198                        if (ch == '\\') {
 199                                switch (ch = *sp++) {
 200                                case 'a': ch = '\a'; break;
 201                                case 'b': ch = '\b'; break;
 202                                case 'f': ch = '\f'; break;
 203                                case 'n': ch = '\n'; break;
 204                                case 'r': ch = '\r'; break;
 205                                case 't': ch = '\t'; break;
 206                                case 'v': ch = '\v'; break;
 207
 208                                case '\\': case '"':
 209                                        break; /* verbatim */
 210
 211                                case '0'...'7':
 212                                        /* octal */
 213                                        ac = ((ch - '0') << 6);
 214                                        if ((ch = *sp++) < '0' || '7' < ch)
 215                                                return NULL;
 216                                        ac |= ((ch - '0') << 3);
 217                                        if ((ch = *sp++) < '0' || '7' < ch)
 218                                                return NULL;
 219                                        ac |= (ch - '0');
 220                                        ch = ac;
 221                                        break;
 222                                default:
 223                                        return NULL; /* malformed */
 224                                }
 225                        }
 226                        EMIT(ch);
 227                }
 228
 229                if (name) {
 230                        *outp = 0;
 231                        if (endp)
 232                                *endp = sp;
 233                        return name;
 234                }
 235                outp = name = xmalloc(count + 1);
 236        }
 237}
 238
 239void write_name_quoted(const char *prefix, int prefix_len,
 240                       const char *name, int quote, FILE *out)
 241{
 242        int needquote;
 243
 244        if (!quote) {
 245        no_quote:
 246                if (prefix_len)
 247                        fprintf(out, "%.*s", prefix_len, prefix);
 248                fputs(name, out);
 249                return;
 250        }
 251
 252        needquote = 0;
 253        if (prefix_len)
 254                needquote = quote_c_style_counted(prefix, prefix_len,
 255                                                  NULL, NULL, 0);
 256        if (!needquote)
 257                needquote = quote_c_style(name, NULL, NULL, 0);
 258        if (needquote) {
 259                fputc('"', out);
 260                if (prefix_len)
 261                        quote_c_style_counted(prefix, prefix_len,
 262                                              NULL, out, 1);
 263                quote_c_style(name, NULL, out, 1);
 264                fputc('"', out);
 265        }
 266        else
 267                goto no_quote;
 268}