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 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(const char *src)
70 cnt = sq_quote_buf(NULL, 0, src) + 1;
72 sq_quote_buf(buf, cnt, src);
77 char *sq_quote_argv(const char** argv, int count)
83 /* Count argv if needed. */
85 for (count = 0; argv[count]; count++)
89 /* Special case: no argv. */
93 /* Get destination buffer length. */
94 for (i = 0; i < count; i++)
95 len += sq_quote_buf(NULL, 0, argv[i]) + 1;
97 /* Alloc destination buffer. */
98 to = buf = xmalloc(len + 1);
100 /* Copy into destination buffer. */
101 for (i = 0; i < count; ++i) {
103 to += sq_quote_buf(to, len, argv[i]);
110 * Append a string to a string buffer, with or without shell quoting.
111 * Return true if the buffer overflowed.
113 int add_to_string(char **ptrp, int *sizep, const char *str, int quote)
121 oc = sq_quote_buf(p, size, str);
124 memcpy(p, str, (size <= oc) ? size - 1 : oc);
138 char *sq_dequote(char *arg)
154 /* We stepped out of sq */
161 if (need_bs_quote(c) && *++src == '\'') {
173 * C-style name quoting.
175 * Does one of three things:
177 * (1) if outbuf and outfp are both NULL, inspect the input name and
178 * counts the number of bytes that are needed to hold c_style
179 * quoted version of name, counting the double quotes around
180 * it but not terminating NUL, and returns it. However, if name
181 * does not need c_style quoting, it returns 0.
183 * (2) if outbuf is not NULL, it must point at a buffer large enough
184 * to hold the c_style quoted version of name, enclosing double
185 * quotes, and terminating NUL. Fills outbuf with c_style quoted
186 * version of name enclosed in double-quote pair. Return value
189 * (3) if outfp is not NULL, outputs c_style quoted version of name,
190 * but not enclosed in double-quote pair. Return value is undefined.
193 static int quote_c_style_counted(const char *name, int namelen,
194 char *outbuf, FILE *outfp, int no_dq)
198 (outbuf ? (*outbuf++ = (c)) : outfp ? fputc(c, outfp) : (count++))
200 #define EMITQ() EMIT('\\')
203 int ch, count = 0, needquote = 0;
207 for (sp = name; sp < name + namelen; sp++) {
211 if ((ch < ' ') || (ch == '"') || (ch == '\\') ||
215 case '\a': EMITQ(); ch = 'a'; break;
216 case '\b': EMITQ(); ch = 'b'; break;
217 case '\f': EMITQ(); ch = 'f'; break;
218 case '\n': EMITQ(); ch = 'n'; break;
219 case '\r': EMITQ(); ch = 'r'; break;
220 case '\t': EMITQ(); ch = 't'; break;
221 case '\v': EMITQ(); ch = 'v'; break;
223 case '\\': /* fallthru */
224 case '"': EMITQ(); break;
228 EMIT(((ch >> 6) & 03) + '0');
229 EMIT(((ch >> 3) & 07) + '0');
230 ch = (ch & 07) + '0';
241 return needquote ? count : 0;
244 int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq)
246 int cnt = strlen(name);
247 return quote_c_style_counted(name, cnt, outbuf, outfp, no_dq);
251 * C-style name unquoting.
253 * Quoted should point at the opening double quote. Returns
254 * an allocated memory that holds unquoted name, which the caller
255 * should free when done. Updates endp pointer to point at
256 * one past the ending double quote if given.
259 char *unquote_c_style(const char *quoted, const char **endp)
262 char *name = NULL, *outp = NULL;
263 int count = 0, ch, ac;
266 #define EMIT(c) (outp ? (*outp++ = (c)) : (count++))
268 if (*quoted++ != '"')
272 /* first pass counts and allocates, second pass fills */
273 for (sp = quoted; (ch = *sp++) != '"'; ) {
275 switch (ch = *sp++) {
276 case 'a': ch = '\a'; break;
277 case 'b': ch = '\b'; break;
278 case 'f': ch = '\f'; break;
279 case 'n': ch = '\n'; break;
280 case 'r': ch = '\r'; break;
281 case 't': ch = '\t'; break;
282 case 'v': ch = '\v'; break;
285 break; /* verbatim */
296 ac = ((ch - '0') << 6);
297 if ((ch = *sp++) < '0' || '7' < ch)
299 ac |= ((ch - '0') << 3);
300 if ((ch = *sp++) < '0' || '7' < ch)
306 return NULL; /* malformed */
318 outp = name = xmalloc(count + 1);
322 void write_name_quoted(const char *prefix, int prefix_len,
323 const char *name, int quote, FILE *out)
330 fprintf(out, "%.*s", prefix_len, prefix);
337 needquote = quote_c_style_counted(prefix, prefix_len,
340 needquote = quote_c_style(name, NULL, NULL, 0);
344 quote_c_style_counted(prefix, prefix_len,
346 quote_c_style(name, NULL, out, 1);
353 /* quoting as a string literal for other languages */
355 void perl_quote_print(FILE *stream, const char *src)
357 const char sq = '\'';
358 const char bq = '\\';
362 while ((c = *src++)) {
363 if (c == sq || c == bq)
370 void python_quote_print(FILE *stream, const char *src)
372 const char sq = '\'';
373 const char bq = '\\';
374 const char nl = '\n';
378 while ((c = *src++)) {
384 if (c == sq || c == bq)
391 void tcl_quote_print(FILE *stream, const char *src)
396 while ((c = *src++)) {
400 case '$': case '\\': case '"':
406 fputs("\\f", stream);
409 fputs("\\r", stream);
412 fputs("\\n", stream);
415 fputs("\\t", stream);
418 fputs("\\v", stream);