4 /* Help to copy the thing properly quoted for the shell safety.
5 * any single quote is replaced with '\'', and the caller is
6 * expected to enclose the result within a single quote pair.
9 * original sq_quote result
10 * name ==> name ==> 'name'
11 * a b ==> a b ==> 'a b'
12 * a'b ==> a'\''b ==> 'a'\''b'
14 char *sq_quote(const char *src
)
16 static char *buf
= NULL
;
21 /* count bytes needed to store the quoted string. */
22 for (cnt
= 3, cp
= src
; *cp
; cnt
++, cp
++)
29 while ((c
= *src
++)) {
33 bp
= strcpy(bp
, "'\\''");
43 * C-style name quoting.
45 * Does one of three things:
47 * (1) if outbuf and outfp are both NULL, inspect the input name and
48 * counts the number of bytes that are needed to hold c_style
49 * quoted version of name, counting the double quotes around
50 * it but not terminating NUL, and returns it. However, if name
51 * does not need c_style quoting, it returns 0.
53 * (2) if outbuf is not NULL, it must point at a buffer large enough
54 * to hold the c_style quoted version of name, enclosing double
55 * quotes, and terminating NUL. Fills outbuf with c_style quoted
56 * version of name enclosed in double-quote pair. Return value
59 * (3) if outfp is not NULL, outputs c_style quoted version of name,
60 * but not enclosed in double-quote pair. Return value is undefined.
63 int quote_c_style(const char *name
, char *outbuf
, FILE *outfp
, int no_dq
)
67 (outbuf ? (*outbuf++ = (c)) : outfp ? fputc(c, outfp) : (count++))
69 #define EMITQ() EMIT('\\')
72 int ch
, count
= 0, needquote
= 0;
76 for (sp
= name
; (ch
= *sp
++); ) {
78 if ((ch
< ' ') || (ch
== '"') || (ch
== '\\') ||
82 case '\a': EMITQ(); ch
= 'a'; break;
83 case '\b': EMITQ(); ch
= 'b'; break;
84 case '\f': EMITQ(); ch
= 'f'; break;
85 case '\n': EMITQ(); ch
= 'n'; break;
86 case '\r': EMITQ(); ch
= 'r'; break;
87 case '\t': EMITQ(); ch
= 't'; break;
88 case '\v': EMITQ(); ch
= 'v'; break;
90 case '\\': /* fallthru */
91 case '"': EMITQ(); break;
97 EMIT(((ch
>> 6) & 03) + '0');
98 EMIT(((ch
>> 3) & 07) + '0');
110 return needquote
? count
: 0;
114 * C-style name unquoting.
116 * Quoted should point at the opening double quote. Returns
117 * an allocated memory that holds unquoted name, which the caller
118 * should free when done. Updates endp pointer to point at
119 * one past the ending double quote if given.
122 char *unquote_c_style(const char *quoted
, const char **endp
)
125 char *name
= NULL
, *outp
= NULL
;
126 int count
= 0, ch
, ac
;
129 #define EMIT(c) (outp ? (*outp++ = (c)) : (count++))
131 if (*quoted
++ != '"')
135 /* first pass counts and allocates, second pass fills */
136 for (sp
= quoted
; (ch
= *sp
++) != '"'; ) {
138 switch (ch
= *sp
++) {
139 case 'a': ch
= '\a'; break;
140 case 'b': ch
= '\b'; break;
141 case 'f': ch
= '\f'; break;
142 case 'n': ch
= '\n'; break;
143 case 'r': ch
= '\r'; break;
144 case 't': ch
= '\t'; break;
145 case 'v': ch
= '\v'; break;
148 break; /* verbatim */
152 ac
= ((ch
- '0') << 6);
153 if ((ch
= *sp
++) < '0' || '7' < ch
)
155 ac
|= ((ch
- '0') << 3);
156 if ((ch
= *sp
++) < '0' || '7' < ch
)
162 return NULL
; /* malformed */
174 outp
= name
= xmalloc(count
+ 1);
178 void write_name_quoted(const char *prefix
, const char *name
,
179 int quote
, FILE *out
)
185 if (prefix
&& prefix
[0])
192 if (prefix
&& prefix
[0])
193 needquote
= quote_c_style(prefix
, NULL
, NULL
, 0);
195 needquote
= quote_c_style(name
, NULL
, NULL
, 0);
198 if (prefix
&& prefix
[0])
199 quote_c_style(prefix
, NULL
, out
, 1);
200 quote_c_style(name
, NULL
, out
, 1);