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
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'
16 #define EMIT(x) ( (++len < n) && (*bp++ = (x)) )
18 static inline int need_bs_quote(char c)
20 return (c == '\'' || c == '!');
23 size_t sq_quote_buf(char *dst, size_t n, const char *src)
30 while ((c = *src++)) {
31 if (need_bs_quote(c)) {
48 char *sq_quote(const char *src)
53 cnt = sq_quote_buf(NULL, 0, src) + 1;
55 sq_quote_buf(buf, cnt, src);
60 char *sq_dequote(char *arg)
76 /* We stepped out of sq */
83 if (need_bs_quote(c) && *++src == '\'') {
95 * C-style name quoting.
97 * Does one of three things:
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.
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
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.
115 static int quote_c_style_counted(const char *name, int namelen,
116 char *outbuf, FILE *outfp, int no_dq)
120 (outbuf ? (*outbuf++ = (c)) : outfp ? fputc(c, outfp) : (count++))
122 #define EMITQ() EMIT('\\')
125 int ch, count = 0, needquote = 0;
129 for (sp = name; sp < name + namelen; sp++) {
133 if ((ch < ' ') || (ch == '"') || (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;
145 case '\\': /* fallthru */
146 case '"': EMITQ(); break;
150 EMIT(((ch >> 6) & 03) + '0');
151 EMIT(((ch >> 3) & 07) + '0');
152 ch = (ch & 07) + '0';
163 return needquote ? count : 0;
166 int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq)
168 int cnt = strlen(name);
169 return quote_c_style_counted(name, cnt, outbuf, outfp, no_dq);
173 * C-style name unquoting.
175 * Quoted should point at the opening double quote. Returns
176 * an allocated memory that holds unquoted name, which the caller
177 * should free when done. Updates endp pointer to point at
178 * one past the ending double quote if given.
181 char *unquote_c_style(const char *quoted, const char **endp)
184 char *name = NULL, *outp = NULL;
185 int count = 0, ch, ac;
188 #define EMIT(c) (outp ? (*outp++ = (c)) : (count++))
190 if (*quoted++ != '"')
194 /* first pass counts and allocates, second pass fills */
195 for (sp = quoted; (ch = *sp++) != '"'; ) {
197 switch (ch = *sp++) {
198 case 'a': ch = '\a'; break;
199 case 'b': ch = '\b'; break;
200 case 'f': ch = '\f'; break;
201 case 'n': ch = '\n'; break;
202 case 'r': ch = '\r'; break;
203 case 't': ch = '\t'; break;
204 case 'v': ch = '\v'; break;
207 break; /* verbatim */
218 ac = ((ch - '0') << 6);
219 if ((ch = *sp++) < '0' || '7' < ch)
221 ac |= ((ch - '0') << 3);
222 if ((ch = *sp++) < '0' || '7' < ch)
228 return NULL; /* malformed */
240 outp = name = xmalloc(count + 1);
244 void write_name_quoted(const char *prefix, int prefix_len,
245 const char *name, int quote, FILE *out)
252 fprintf(out, "%.*s", prefix_len, prefix);
259 needquote = quote_c_style_counted(prefix, prefix_len,
262 needquote = quote_c_style(name, NULL, NULL, 0);
266 quote_c_style_counted(prefix, prefix_len,
268 quote_c_style(name, NULL, out, 1);