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) do { if (++len < n) *bp++ = (x); } while(0)
18 static inline int need_bs_quote(char c)
20 return (c == '\'' || c == '!');
23 static size_t sq_quote_buf(char *dst, size_t n, const char *src)
30 while ((c = *src++)) {
31 if (need_bs_quote(c)) {
48 void sq_quote_print(FILE *stream, const char *src)
53 while ((c = *src++)) {
54 if (need_bs_quote(c)) {
65 char *sq_quote_argv(const char** argv, int count)
71 /* Count argv if needed. */
73 for (count = 0; argv[count]; count++)
77 /* Special case: no argv. */
81 /* Get destination buffer length. */
82 for (i = 0; i < count; i++)
83 len += sq_quote_buf(NULL, 0, argv[i]) + 1;
85 /* Alloc destination buffer. */
86 to = buf = xmalloc(len + 1);
88 /* Copy into destination buffer. */
89 for (i = 0; i < count; ++i) {
91 to += sq_quote_buf(to, len, argv[i]);
98 * Append a string to a string buffer, with or without shell quoting.
99 * Return true if the buffer overflowed.
101 int add_to_string(char **ptrp, int *sizep, const char *str, int quote)
109 oc = sq_quote_buf(p, size, str);
112 memcpy(p, str, (size <= oc) ? size - 1 : oc);
126 char *sq_dequote(char *arg)
142 /* We stepped out of sq */
149 if (need_bs_quote(c) && *++src == '\'') {
161 * C-style name quoting.
163 * Does one of three things:
165 * (1) if outbuf and outfp are both NULL, inspect the input name and
166 * counts the number of bytes that are needed to hold c_style
167 * quoted version of name, counting the double quotes around
168 * it but not terminating NUL, and returns it. However, if name
169 * does not need c_style quoting, it returns 0.
171 * (2) if outbuf is not NULL, it must point at a buffer large enough
172 * to hold the c_style quoted version of name, enclosing double
173 * quotes, and terminating NUL. Fills outbuf with c_style quoted
174 * version of name enclosed in double-quote pair. Return value
177 * (3) if outfp is not NULL, outputs c_style quoted version of name,
178 * but not enclosed in double-quote pair. Return value is undefined.
181 static int quote_c_style_counted(const char *name, int namelen,
182 char *outbuf, FILE *outfp, int no_dq)
186 (outbuf ? (*outbuf++ = (c)) : outfp ? fputc(c, outfp) : (count++))
188 #define EMITQ() EMIT('\\')
191 int ch, count = 0, needquote = 0;
195 for (sp = name; sp < name + namelen; sp++) {
199 if ((ch < ' ') || (ch == '"') || (ch == '\\') ||
203 case '\a': EMITQ(); ch = 'a'; break;
204 case '\b': EMITQ(); ch = 'b'; break;
205 case '\f': EMITQ(); ch = 'f'; break;
206 case '\n': EMITQ(); ch = 'n'; break;
207 case '\r': EMITQ(); ch = 'r'; break;
208 case '\t': EMITQ(); ch = 't'; break;
209 case '\v': EMITQ(); ch = 'v'; break;
211 case '\\': /* fallthru */
212 case '"': EMITQ(); break;
216 EMIT(((ch >> 6) & 03) + '0');
217 EMIT(((ch >> 3) & 07) + '0');
218 ch = (ch & 07) + '0';
229 return needquote ? count : 0;
232 int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq)
234 int cnt = strlen(name);
235 return quote_c_style_counted(name, cnt, outbuf, outfp, no_dq);
239 * C-style name unquoting.
241 * Quoted should point at the opening double quote. Returns
242 * an allocated memory that holds unquoted name, which the caller
243 * should free when done. Updates endp pointer to point at
244 * one past the ending double quote if given.
247 char *unquote_c_style(const char *quoted, const char **endp)
250 char *name = NULL, *outp = NULL;
251 int count = 0, ch, ac;
254 #define EMIT(c) (outp ? (*outp++ = (c)) : (count++))
256 if (*quoted++ != '"')
260 /* first pass counts and allocates, second pass fills */
261 for (sp = quoted; (ch = *sp++) != '"'; ) {
263 switch (ch = *sp++) {
264 case 'a': ch = '\a'; break;
265 case 'b': ch = '\b'; break;
266 case 'f': ch = '\f'; break;
267 case 'n': ch = '\n'; break;
268 case 'r': ch = '\r'; break;
269 case 't': ch = '\t'; break;
270 case 'v': ch = '\v'; break;
273 break; /* verbatim */
284 ac = ((ch - '0') << 6);
285 if ((ch = *sp++) < '0' || '7' < ch)
287 ac |= ((ch - '0') << 3);
288 if ((ch = *sp++) < '0' || '7' < ch)
294 return NULL; /* malformed */
306 outp = name = xmalloc(count + 1);
310 void write_name_quoted(const char *prefix, int prefix_len,
311 const char *name, int quote, FILE *out)
318 fprintf(out, "%.*s", prefix_len, prefix);
325 needquote = quote_c_style_counted(prefix, prefix_len,
328 needquote = quote_c_style(name, NULL, NULL, 0);
332 quote_c_style_counted(prefix, prefix_len,
334 quote_c_style(name, NULL, out, 1);
341 /* quoting as a string literal for other languages */
343 void perl_quote_print(FILE *stream, const char *src)
345 const char sq = '\'';
346 const char bq = '\\';
350 while ((c = *src++)) {
351 if (c == sq || c == bq)
358 void python_quote_print(FILE *stream, const char *src)
360 const char sq = '\'';
361 const char bq = '\\';
362 const char nl = '\n';
366 while ((c = *src++)) {
372 if (c == sq || c == bq)
379 void tcl_quote_print(FILE *stream, const char *src)
384 while ((c = *src++)) {
388 case '$': case '\\': case '"':
394 fputs("\\f", stream);
397 fputs("\\r", stream);
400 fputs("\\n", stream);
403 fputs("\\t", stream);
406 fputs("\\v", stream);