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 size_t sq_quote_buf(char *dst
, size_t n
, const char *src
)
25 while ((c
= *src
++)) {
26 if (c
== '\'' || c
== '!') {
43 char *sq_quote(const char *src
)
48 cnt
= sq_quote_buf(NULL
, 0, src
) + 1;
50 sq_quote_buf(buf
, cnt
, src
);
56 * C-style name quoting.
58 * Does one of three things:
60 * (1) if outbuf and outfp are both NULL, inspect the input name and
61 * counts the number of bytes that are needed to hold c_style
62 * quoted version of name, counting the double quotes around
63 * it but not terminating NUL, and returns it. However, if name
64 * does not need c_style quoting, it returns 0.
66 * (2) if outbuf is not NULL, it must point at a buffer large enough
67 * to hold the c_style quoted version of name, enclosing double
68 * quotes, and terminating NUL. Fills outbuf with c_style quoted
69 * version of name enclosed in double-quote pair. Return value
72 * (3) if outfp is not NULL, outputs c_style quoted version of name,
73 * but not enclosed in double-quote pair. Return value is undefined.
76 int quote_c_style(const char *name
, char *outbuf
, FILE *outfp
, int no_dq
)
80 (outbuf ? (*outbuf++ = (c)) : outfp ? fputc(c, outfp) : (count++))
82 #define EMITQ() EMIT('\\')
85 int ch
, count
= 0, needquote
= 0;
89 for (sp
= name
; (ch
= *sp
++); ) {
91 if ((ch
< ' ') || (ch
== '"') || (ch
== '\\') ||
95 case '\a': EMITQ(); ch
= 'a'; break;
96 case '\b': EMITQ(); ch
= 'b'; break;
97 case '\f': EMITQ(); ch
= 'f'; break;
98 case '\n': EMITQ(); ch
= 'n'; break;
99 case '\r': EMITQ(); ch
= 'r'; break;
100 case '\t': EMITQ(); ch
= 't'; break;
101 case '\v': EMITQ(); ch
= 'v'; break;
103 case '\\': /* fallthru */
104 case '"': EMITQ(); break;
110 EMIT(((ch
>> 6) & 03) + '0');
111 EMIT(((ch
>> 3) & 07) + '0');
112 ch
= (ch
& 07) + '0';
123 return needquote
? count
: 0;
127 * C-style name unquoting.
129 * Quoted should point at the opening double quote. Returns
130 * an allocated memory that holds unquoted name, which the caller
131 * should free when done. Updates endp pointer to point at
132 * one past the ending double quote if given.
135 char *unquote_c_style(const char *quoted
, const char **endp
)
138 char *name
= NULL
, *outp
= NULL
;
139 int count
= 0, ch
, ac
;
142 #define EMIT(c) (outp ? (*outp++ = (c)) : (count++))
144 if (*quoted
++ != '"')
148 /* first pass counts and allocates, second pass fills */
149 for (sp
= quoted
; (ch
= *sp
++) != '"'; ) {
151 switch (ch
= *sp
++) {
152 case 'a': ch
= '\a'; break;
153 case 'b': ch
= '\b'; break;
154 case 'f': ch
= '\f'; break;
155 case 'n': ch
= '\n'; break;
156 case 'r': ch
= '\r'; break;
157 case 't': ch
= '\t'; break;
158 case 'v': ch
= '\v'; break;
161 break; /* verbatim */
165 ac
= ((ch
- '0') << 6);
166 if ((ch
= *sp
++) < '0' || '7' < ch
)
168 ac
|= ((ch
- '0') << 3);
169 if ((ch
= *sp
++) < '0' || '7' < ch
)
175 return NULL
; /* malformed */
187 outp
= name
= xmalloc(count
+ 1);
191 void write_name_quoted(const char *prefix
, const char *name
,
192 int quote
, FILE *out
)
198 if (prefix
&& prefix
[0])
205 if (prefix
&& prefix
[0])
206 needquote
= quote_c_style(prefix
, NULL
, NULL
, 0);
208 needquote
= quote_c_style(name
, NULL
, NULL
, 0);
211 if (prefix
&& prefix
[0])
212 quote_c_style(prefix
, NULL
, out
, 1);
213 quote_c_style(name
, NULL
, out
, 1);