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, size_t maxlen)
63 /* Copy into destination buffer. */
64 strbuf_grow(dst, 255);
65 for (i = 0; argv[i]; ++i) {
66 strbuf_addch(dst, ' ');
67 sq_quote_buf(dst, argv[i]);
68 if (maxlen && dst->len > maxlen)
69 die("Too many or long arguments");
73 char *sq_dequote(char *arg)
89 /* We stepped out of sq */
96 if (need_bs_quote(c) && *++src == '\'') {
107 /* 1 means: quote as octal
108 * 0 means: quote as octal if (quote_path_fully)
109 * -1 means: never quote
112 #define X8(x) x, x, x, x, x, x, x, x
113 #define X16(x) X8(x), X8(x)
114 static signed char const sq_lookup[256] = {
115 /* 0 1 2 3 4 5 6 7 */
116 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
117 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
119 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
120 /* 0x28 */ X16(-1), X16(-1), X16(-1),
121 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
122 /* 0x60 */ X16(-1), X8(-1),
123 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
124 /* 0x80 */ /* set to 0 */
127 static inline int sq_must_quote(char c)
129 return sq_lookup[(unsigned char)c] + quote_path_fully > 0;
132 /* returns the longest prefix not needing a quote up to maxlen if positive.
133 This stops at the first \0 because it's marked as a character needing an
135 static size_t next_quote_pos(const char *s, ssize_t maxlen)
139 for (len = 0; !sq_must_quote(s[len]); len++);
141 for (len = 0; len < maxlen && !sq_must_quote(s[len]); len++);
147 * C-style name quoting.
149 * (1) if sb and fp are both NULL, inspect the input name and counts the
150 * number of bytes that are needed to hold c_style quoted version of name,
151 * counting the double quotes around it but not terminating NUL, and
153 * However, if name does not need c_style quoting, it returns 0.
155 * (2) if sb or fp are not NULL, it emits the c_style quoted version
156 * of name, enclosed with double quotes if asked and needed only.
157 * Return value is the same as in (1).
159 static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
160 struct strbuf *sb, FILE *fp, int no_dq)
165 if (sb) strbuf_addch(sb, (c)); \
166 if (fp) fputc((c), fp); \
169 #define EMITBUF(s, l) \
171 if (sb) strbuf_add(sb, (s), (l)); \
172 if (fp) fwrite((s), (l), 1, fp); \
176 size_t len, count = 0;
177 const char *p = name;
182 len = next_quote_pos(p, maxlen);
183 if (len == maxlen || !p[len])
186 if (!no_dq && p == name)
192 ch = (unsigned char)*p++;
193 if (sq_lookup[ch] >= ' ') {
196 EMIT(((ch >> 6) & 03) + '0');
197 EMIT(((ch >> 3) & 07) + '0');
198 EMIT(((ch >> 0) & 07) + '0');
203 if (p == name) /* no ending quote needed */
211 size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
213 return quote_c_style_counted(name, -1, sb, fp, nodq);
216 void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, int nodq)
218 if (quote_c_style(prefix, NULL, NULL, 0) ||
219 quote_c_style(path, NULL, NULL, 0)) {
221 strbuf_addch(sb, '"');
222 quote_c_style(prefix, sb, NULL, 1);
223 quote_c_style(path, sb, NULL, 1);
225 strbuf_addch(sb, '"');
227 strbuf_addstr(sb, prefix);
228 strbuf_addstr(sb, path);
232 void write_name_quoted(const char *name, FILE *fp, int terminator)
235 quote_c_style(name, NULL, fp, 0);
239 fputc(terminator, fp);
242 extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
243 const char *name, FILE *fp, int terminator)
248 needquote = next_quote_pos(pfx, pfxlen) < pfxlen
249 || name[next_quote_pos(name, -1)];
253 quote_c_style_counted(pfx, pfxlen, NULL, fp, 1);
254 quote_c_style(name, NULL, fp, 1);
257 fwrite(pfx, pfxlen, 1, fp);
260 fputc(terminator, fp);
263 /* quote path as relative to the given prefix */
264 char *quote_path_relative(const char *in, int len,
265 struct strbuf *out, const char *prefix)
272 /* "../" prefix itself does not need quoting, but "in" might. */
273 needquote = next_quote_pos(in, len) < len;
274 strbuf_setlen(out, 0);
275 strbuf_grow(out, len);
278 strbuf_addch(out, '"');
281 while (prefix[off] && off < len && prefix[off] == in[off])
282 if (prefix[off] == '/') {
290 for (; *prefix; prefix++)
292 strbuf_addstr(out, "../");
295 quote_c_style_counted (in, len, out, NULL, 1);
298 strbuf_addch(out, '"');
300 strbuf_addstr(out, "./");
306 * C-style name unquoting.
308 * Quoted should point at the opening double quote.
309 * + Returns 0 if it was able to unquote the string properly, and appends the
310 * result in the strbuf `sb'.
311 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
312 * that this function will allocate memory in the strbuf, so calling
313 * strbuf_release is mandatory whichever result unquote_c_style returns.
315 * Updates endp pointer to point at one past the ending double quote if given.
317 int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
319 size_t oldlen = sb->len, len;
322 if (*quoted++ != '"')
326 len = strcspn(quoted, "\"\\");
327 strbuf_add(sb, quoted, len);
341 switch ((ch = *quoted++)) {
342 case 'a': ch = '\a'; break;
343 case 'b': ch = '\b'; break;
344 case 'f': ch = '\f'; break;
345 case 'n': ch = '\n'; break;
346 case 'r': ch = '\r'; break;
347 case 't': ch = '\t'; break;
348 case 'v': ch = '\v'; break;
351 break; /* verbatim */
353 /* octal values with first digit over 4 overflow */
354 case '0': case '1': case '2': case '3':
355 ac = ((ch - '0') << 6);
356 if ((ch = *quoted++) < '0' || '7' < ch)
358 ac |= ((ch - '0') << 3);
359 if ((ch = *quoted++) < '0' || '7' < ch)
367 strbuf_addch(sb, ch);
371 strbuf_setlen(sb, oldlen);
375 /* quoting as a string literal for other languages */
377 void perl_quote_print(FILE *stream, const char *src)
379 const char sq = '\'';
380 const char bq = '\\';
384 while ((c = *src++)) {
385 if (c == sq || c == bq)
392 void python_quote_print(FILE *stream, const char *src)
394 const char sq = '\'';
395 const char bq = '\\';
396 const char nl = '\n';
400 while ((c = *src++)) {
406 if (c == sq || c == bq)
413 void tcl_quote_print(FILE *stream, const char *src)
418 while ((c = *src++)) {
422 case '$': case '\\': case '"':
428 fputs("\\f", stream);
431 fputs("\\r", stream);
434 fputs("\\n", stream);
437 fputs("\\t", stream);
440 fputs("\\v", stream);