4 int quote_path_fully
= 1;
6 /* Help to copy the thing properly quoted for the shell safety.
7 * any single quote is replaced with '\'', any exclamation point
8 * is replaced with '\!', and the whole thing is enclosed in a
11 * original sq_quote result
12 * name ==> name ==> 'name'
13 * a b ==> a b ==> 'a b'
14 * a'b ==> a'\''b ==> 'a'\''b'
15 * a!b ==> a'\!'b ==> 'a'\!'b'
17 static inline int need_bs_quote(char c
)
19 return (c
== '\'' || c
== '!');
22 void sq_quote_buf(struct strbuf
*dst
, const char *src
)
27 to_free
= strbuf_detach(dst
, NULL
);
29 strbuf_addch(dst
, '\'');
31 size_t len
= strcspn(src
, "'!");
32 strbuf_add(dst
, src
, len
);
34 while (need_bs_quote(*src
)) {
35 strbuf_addstr(dst
, "'\\");
36 strbuf_addch(dst
, *src
++);
37 strbuf_addch(dst
, '\'');
40 strbuf_addch(dst
, '\'');
44 void sq_quote_print(FILE *stream
, const char *src
)
49 while ((c
= *src
++)) {
50 if (need_bs_quote(c
)) {
61 void sq_quote_argv(struct strbuf
*dst
, const char** argv
, size_t maxlen
)
65 /* Copy into destination buffer. */
66 strbuf_grow(dst
, 255);
67 for (i
= 0; argv
[i
]; ++i
) {
68 strbuf_addch(dst
, ' ');
69 sq_quote_buf(dst
, argv
[i
]);
70 if (maxlen
&& dst
->len
> maxlen
)
71 die("Too many or long arguments");
75 char *sq_dequote(char *arg
)
91 /* We stepped out of sq */
98 if (need_bs_quote(c
) && *++src
== '\'') {
109 /* 1 means: quote as octal
110 * 0 means: quote as octal if (quote_path_fully)
111 * -1 means: never quote
114 #define X8(x) x, x, x, x, x, x, x, x
115 #define X16(x) X8(x), X8(x)
116 static signed char const sq_lookup
[256] = {
117 /* 0 1 2 3 4 5 6 7 */
118 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
119 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
121 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
122 /* 0x28 */ X16(-1), X16(-1), X16(-1),
123 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
124 /* 0x60 */ X16(-1), X8(-1),
125 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
126 /* 0x80 */ /* set to 0 */
129 static inline int sq_must_quote(char c
)
131 return sq_lookup
[(unsigned char)c
] + quote_path_fully
> 0;
134 /* returns the longest prefix not needing a quote up to maxlen if positive.
135 This stops at the first \0 because it's marked as a character needing an
137 static size_t next_quote_pos(const char *s
, ssize_t maxlen
)
141 for (len
= 0; !sq_must_quote(s
[len
]); len
++);
143 for (len
= 0; len
< maxlen
&& !sq_must_quote(s
[len
]); len
++);
149 * C-style name quoting.
151 * (1) if sb and fp are both NULL, inspect the input name and counts the
152 * number of bytes that are needed to hold c_style quoted version of name,
153 * counting the double quotes around it but not terminating NUL, and
155 * However, if name does not need c_style quoting, it returns 0.
157 * (2) if sb or fp are not NULL, it emits the c_style quoted version
158 * of name, enclosed with double quotes if asked and needed only.
159 * Return value is the same as in (1).
161 static size_t quote_c_style_counted(const char *name
, ssize_t maxlen
,
162 struct strbuf
*sb
, FILE *fp
, int no_dq
)
167 if (sb) strbuf_addch(sb, (c)); \
168 if (fp) fputc((c), fp); \
171 #define EMITBUF(s, l) \
173 if (sb) strbuf_add(sb, (s), (l)); \
174 if (fp) fwrite((s), (l), 1, fp); \
178 size_t len
, count
= 0;
179 const char *p
= name
;
184 len
= next_quote_pos(p
, maxlen
);
185 if (len
== maxlen
|| !p
[len
])
188 if (!no_dq
&& p
== name
)
194 ch
= (unsigned char)*p
++;
195 if (sq_lookup
[ch
] >= ' ') {
198 EMIT(((ch
>> 6) & 03) + '0');
199 EMIT(((ch
>> 3) & 07) + '0');
200 EMIT(((ch
>> 0) & 07) + '0');
205 if (p
== name
) /* no ending quote needed */
213 size_t quote_c_style(const char *name
, struct strbuf
*sb
, FILE *fp
, int nodq
)
215 return quote_c_style_counted(name
, -1, sb
, fp
, nodq
);
218 void quote_two_c_style(struct strbuf
*sb
, const char *prefix
, const char *path
, int nodq
)
220 if (quote_c_style(prefix
, NULL
, NULL
, 0) ||
221 quote_c_style(path
, NULL
, NULL
, 0)) {
223 strbuf_addch(sb
, '"');
224 quote_c_style(prefix
, sb
, NULL
, 1);
225 quote_c_style(path
, sb
, NULL
, 1);
227 strbuf_addch(sb
, '"');
229 strbuf_addstr(sb
, prefix
);
230 strbuf_addstr(sb
, path
);
234 void write_name_quoted(const char *name
, FILE *fp
, int terminator
)
237 quote_c_style(name
, NULL
, fp
, 0);
241 fputc(terminator
, fp
);
244 extern void write_name_quotedpfx(const char *pfx
, size_t pfxlen
,
245 const char *name
, FILE *fp
, int terminator
)
250 needquote
= next_quote_pos(pfx
, pfxlen
) < pfxlen
251 || name
[next_quote_pos(name
, -1)];
255 quote_c_style_counted(pfx
, pfxlen
, NULL
, fp
, 1);
256 quote_c_style(name
, NULL
, fp
, 1);
259 fwrite(pfx
, pfxlen
, 1, fp
);
262 fputc(terminator
, fp
);
265 /* quote path as relative to the given prefix */
266 char *quote_path_relative(const char *in
, int len
,
267 struct strbuf
*out
, const char *prefix
)
274 /* "../" prefix itself does not need quoting, but "in" might. */
275 needquote
= next_quote_pos(in
, len
) < len
;
276 strbuf_setlen(out
, 0);
277 strbuf_grow(out
, len
);
280 strbuf_addch(out
, '"');
283 while (prefix
[off
] && off
< len
&& prefix
[off
] == in
[off
])
284 if (prefix
[off
] == '/') {
292 for (; *prefix
; prefix
++)
294 strbuf_addstr(out
, "../");
297 quote_c_style_counted (in
, len
, out
, NULL
, 1);
300 strbuf_addch(out
, '"');
302 strbuf_addstr(out
, "./");
308 * C-style name unquoting.
310 * Quoted should point at the opening double quote.
311 * + Returns 0 if it was able to unquote the string properly, and appends the
312 * result in the strbuf `sb'.
313 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
314 * that this function will allocate memory in the strbuf, so calling
315 * strbuf_release is mandatory whichever result unquote_c_style returns.
317 * Updates endp pointer to point at one past the ending double quote if given.
319 int unquote_c_style(struct strbuf
*sb
, const char *quoted
, const char **endp
)
321 size_t oldlen
= sb
->len
, len
;
324 if (*quoted
++ != '"')
328 len
= strcspn(quoted
, "\"\\");
329 strbuf_add(sb
, quoted
, len
);
343 switch ((ch
= *quoted
++)) {
344 case 'a': ch
= '\a'; break;
345 case 'b': ch
= '\b'; break;
346 case 'f': ch
= '\f'; break;
347 case 'n': ch
= '\n'; break;
348 case 'r': ch
= '\r'; break;
349 case 't': ch
= '\t'; break;
350 case 'v': ch
= '\v'; break;
353 break; /* verbatim */
355 /* octal values with first digit over 4 overflow */
356 case '0': case '1': case '2': case '3':
357 ac
= ((ch
- '0') << 6);
358 if ((ch
= *quoted
++) < '0' || '7' < ch
)
360 ac
|= ((ch
- '0') << 3);
361 if ((ch
= *quoted
++) < '0' || '7' < ch
)
369 strbuf_addch(sb
, ch
);
373 strbuf_setlen(sb
, oldlen
);
377 /* quoting as a string literal for other languages */
379 void perl_quote_print(FILE *stream
, const char *src
)
381 const char sq
= '\'';
382 const char bq
= '\\';
386 while ((c
= *src
++)) {
387 if (c
== sq
|| c
== bq
)
394 void python_quote_print(FILE *stream
, const char *src
)
396 const char sq
= '\'';
397 const char bq
= '\\';
398 const char nl
= '\n';
402 while ((c
= *src
++)) {
408 if (c
== sq
|| c
== bq
)
415 void tcl_quote_print(FILE *stream
, const char *src
)
420 while ((c
= *src
++)) {
424 case '$': case '\\': case '"':
430 fputs("\\f", stream
);
433 fputs("\\r", stream
);
436 fputs("\\n", stream
);
439 fputs("\\t", stream
);
442 fputs("\\v", stream
);