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'
15 static inline int need_bs_quote(char c)
17 return (c == '\'' || c == '!');
20 void sq_quote_buf(struct strbuf *dst, const char *src)
25 to_free = strbuf_detach(dst, NULL);
27 strbuf_addch(dst, '\'');
29 size_t len = strcspn(src, "'!");
30 strbuf_add(dst, src, len);
32 while (need_bs_quote(*src)) {
33 strbuf_addstr(dst, "'\\");
34 strbuf_addch(dst, *src++);
35 strbuf_addch(dst, '\'');
38 strbuf_addch(dst, '\'');
42 void sq_quote_print(FILE *stream, const char *src)
47 while ((c = *src++)) {
48 if (need_bs_quote(c)) {
59 void sq_quote_argv(struct strbuf *dst, const char** argv, int count,
64 /* Count argv if needed. */
66 for (count = 0; argv[count]; count++)
70 /* Copy into destination buffer. */
71 strbuf_grow(dst, 32 * count);
72 for (i = 0; i < count; ++i) {
73 strbuf_addch(dst, ' ');
74 sq_quote_buf(dst, argv[i]);
75 if (maxlen && dst->len > maxlen)
76 die("Too many or long arguments");
80 char *sq_dequote(char *arg)
96 /* We stepped out of sq */
103 if (need_bs_quote(c) && *++src == '\'') {
114 /* 1 means: quote as octal
115 * 0 means: quote as octal if (quote_path_fully)
116 * -1 means: never quote
119 #define X8(x) x, x, x, x, x, x, x, x
120 #define X16(x) X8(x), X8(x)
121 static signed char const sq_lookup[256] = {
122 /* 0 1 2 3 4 5 6 7 */
123 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
124 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
126 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
127 /* 0x28 */ X16(-1), X16(-1), X16(-1),
128 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
129 /* 0x60 */ X16(-1), X8(-1),
130 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
131 /* 0x80 */ /* set to 0 */
134 static inline int sq_must_quote(char c)
136 return sq_lookup[(unsigned char)c] + quote_path_fully > 0;
139 /* returns the longest prefix not needing a quote up to maxlen if positive.
140 This stops at the first \0 because it's marked as a character needing an
142 static size_t next_quote_pos(const char *s, ssize_t maxlen)
146 for (len = 0; !sq_must_quote(s[len]); len++);
148 for (len = 0; len < maxlen && !sq_must_quote(s[len]); len++);
154 * C-style name quoting.
156 * (1) if sb and fp are both NULL, inspect the input name and counts the
157 * number of bytes that are needed to hold c_style quoted version of name,
158 * counting the double quotes around it but not terminating NUL, and
160 * However, if name does not need c_style quoting, it returns 0.
162 * (2) if sb or fp are not NULL, it emits the c_style quoted version
163 * of name, enclosed with double quotes if asked and needed only.
164 * Return value is the same as in (1).
166 static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
167 struct strbuf *sb, FILE *fp, int no_dq)
172 if (sb) strbuf_addch(sb, (c)); \
173 if (fp) fputc((c), fp); \
176 #define EMITBUF(s, l) \
178 if (sb) strbuf_add(sb, (s), (l)); \
179 if (fp) fwrite((s), (l), 1, fp); \
183 size_t len, count = 0;
184 const char *p = name;
189 len = next_quote_pos(p, maxlen);
190 if (len == maxlen || !p[len])
193 if (!no_dq && p == name)
199 ch = (unsigned char)*p++;
200 if (sq_lookup[ch] >= ' ') {
203 EMIT(((ch >> 6) & 03) + '0');
204 EMIT(((ch >> 3) & 07) + '0');
205 EMIT(((ch >> 0) & 07) + '0');
210 if (p == name) /* no ending quote needed */
218 size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
220 return quote_c_style_counted(name, -1, sb, fp, nodq);
223 void write_name_quoted(const char *name, FILE *fp, int terminator)
226 quote_c_style(name, NULL, fp, 0);
230 fputc(terminator, fp);
233 extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
234 const char *name, FILE *fp, int terminator)
239 needquote = next_quote_pos(pfx, pfxlen) < pfxlen
240 || name[next_quote_pos(name, -1)];
244 quote_c_style_counted(pfx, pfxlen, NULL, fp, 1);
245 quote_c_style(name, NULL, fp, 1);
248 fwrite(pfx, pfxlen, 1, fp);
251 fputc(terminator, fp);
255 * C-style name unquoting.
257 * Quoted should point at the opening double quote.
258 * + Returns 0 if it was able to unquote the string properly, and appends the
259 * result in the strbuf `sb'.
260 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
261 * that this function will allocate memory in the strbuf, so calling
262 * strbuf_release is mandatory whichever result unquote_c_style returns.
264 * Updates endp pointer to point at one past the ending double quote if given.
266 int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
268 size_t oldlen = sb->len, len;
271 if (*quoted++ != '"')
275 len = strcspn(quoted, "\"\\");
276 strbuf_add(sb, quoted, len);
290 switch ((ch = *quoted++)) {
291 case 'a': ch = '\a'; break;
292 case 'b': ch = '\b'; break;
293 case 'f': ch = '\f'; break;
294 case 'n': ch = '\n'; break;
295 case 'r': ch = '\r'; break;
296 case 't': ch = '\t'; break;
297 case 'v': ch = '\v'; break;
300 break; /* verbatim */
302 /* octal values with first digit over 4 overflow */
303 case '0': case '1': case '2': case '3':
304 ac = ((ch - '0') << 6);
305 if ((ch = *quoted++) < '0' || '7' < ch)
307 ac |= ((ch - '0') << 3);
308 if ((ch = *quoted++) < '0' || '7' < ch)
316 strbuf_addch(sb, ch);
320 strbuf_setlen(sb, oldlen);
324 /* quoting as a string literal for other languages */
326 void perl_quote_print(FILE *stream, const char *src)
328 const char sq = '\'';
329 const char bq = '\\';
333 while ((c = *src++)) {
334 if (c == sq || c == bq)
341 void python_quote_print(FILE *stream, const char *src)
343 const char sq = '\'';
344 const char bq = '\\';
345 const char nl = '\n';
349 while ((c = *src++)) {
355 if (c == sq || c == bq)
362 void tcl_quote_print(FILE *stream, const char *src)
367 while ((c = *src++)) {
371 case '$': case '\\': case '"':
377 fputs("\\f", stream);
380 fputs("\\r", stream);
383 fputs("\\n", stream);
386 fputs("\\t", stream);
389 fputs("\\v", stream);