3 #include "argv-array.h"
5 int quote_path_fully
= 1;
7 /* Help to copy the thing properly quoted for the shell safety.
8 * any single quote is replaced with '\'', any exclamation point
9 * is replaced with '\!', and the whole thing is enclosed in a
12 * original sq_quote result
13 * name ==> name ==> 'name'
14 * a b ==> a b ==> 'a b'
15 * a'b ==> a'\''b ==> 'a'\''b'
16 * a!b ==> a'\!'b ==> 'a'\!'b'
18 static inline int need_bs_quote(char c
)
20 return (c
== '\'' || c
== '!');
23 void sq_quote_buf(struct strbuf
*dst
, const char *src
)
28 to_free
= strbuf_detach(dst
, NULL
);
30 strbuf_addch(dst
, '\'');
32 size_t len
= strcspn(src
, "'!");
33 strbuf_add(dst
, src
, len
);
35 while (need_bs_quote(*src
)) {
36 strbuf_addstr(dst
, "'\\");
37 strbuf_addch(dst
, *src
++);
38 strbuf_addch(dst
, '\'');
41 strbuf_addch(dst
, '\'');
45 void sq_quote_print(FILE *stream
, const char *src
)
50 while ((c
= *src
++)) {
51 if (need_bs_quote(c
)) {
62 void sq_quote_argv(struct strbuf
*dst
, const char** argv
, size_t maxlen
)
66 /* Copy into destination buffer. */
67 strbuf_grow(dst
, 255);
68 for (i
= 0; argv
[i
]; ++i
) {
69 strbuf_addch(dst
, ' ');
70 sq_quote_buf(dst
, argv
[i
]);
71 if (maxlen
&& dst
->len
> maxlen
)
72 die("Too many or long arguments");
76 static char *sq_dequote_step(char *arg
, char **next
)
92 /* We stepped out of sq */
101 if (need_bs_quote(c
) && *++src
== '\'') {
107 if (!next
|| !isspace(*src
))
111 } while (isspace(c
));
119 char *sq_dequote(char *arg
)
121 return sq_dequote_step(arg
, NULL
);
124 static int sq_dequote_to_argv_internal(char *arg
,
125 const char ***argv
, int *nr
, int *alloc
,
126 struct argv_array
*array
)
133 char *dequoted
= sq_dequote_step(next
, &next
);
137 ALLOC_GROW(*argv
, *nr
+ 1, *alloc
);
138 (*argv
)[(*nr
)++] = dequoted
;
141 argv_array_push(array
, dequoted
);
147 int sq_dequote_to_argv(char *arg
, const char ***argv
, int *nr
, int *alloc
)
149 return sq_dequote_to_argv_internal(arg
, argv
, nr
, alloc
, NULL
);
152 int sq_dequote_to_argv_array(char *arg
, struct argv_array
*array
)
154 return sq_dequote_to_argv_internal(arg
, NULL
, NULL
, NULL
, array
);
157 /* 1 means: quote as octal
158 * 0 means: quote as octal if (quote_path_fully)
159 * -1 means: never quote
162 #define X8(x) x, x, x, x, x, x, x, x
163 #define X16(x) X8(x), X8(x)
164 static signed char const sq_lookup
[256] = {
165 /* 0 1 2 3 4 5 6 7 */
166 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
167 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
169 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
170 /* 0x28 */ X16(-1), X16(-1), X16(-1),
171 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
172 /* 0x60 */ X16(-1), X8(-1),
173 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
174 /* 0x80 */ /* set to 0 */
177 static inline int sq_must_quote(char c
)
179 return sq_lookup
[(unsigned char)c
] + quote_path_fully
> 0;
182 /* returns the longest prefix not needing a quote up to maxlen if positive.
183 This stops at the first \0 because it's marked as a character needing an
185 static size_t next_quote_pos(const char *s
, ssize_t maxlen
)
189 for (len
= 0; !sq_must_quote(s
[len
]); len
++);
191 for (len
= 0; len
< maxlen
&& !sq_must_quote(s
[len
]); len
++);
197 * C-style name quoting.
199 * (1) if sb and fp are both NULL, inspect the input name and counts the
200 * number of bytes that are needed to hold c_style quoted version of name,
201 * counting the double quotes around it but not terminating NUL, and
203 * However, if name does not need c_style quoting, it returns 0.
205 * (2) if sb or fp are not NULL, it emits the c_style quoted version
206 * of name, enclosed with double quotes if asked and needed only.
207 * Return value is the same as in (1).
209 static size_t quote_c_style_counted(const char *name
, ssize_t maxlen
,
210 struct strbuf
*sb
, FILE *fp
, int no_dq
)
215 if (sb) strbuf_addch(sb, (c)); \
216 if (fp) fputc((c), fp); \
219 #define EMITBUF(s, l) \
221 if (sb) strbuf_add(sb, (s), (l)); \
222 if (fp) fwrite((s), (l), 1, fp); \
226 size_t len
, count
= 0;
227 const char *p
= name
;
232 len
= next_quote_pos(p
, maxlen
);
233 if (len
== maxlen
|| (maxlen
< 0 && !p
[len
]))
236 if (!no_dq
&& p
== name
)
242 ch
= (unsigned char)*p
++;
245 if (sq_lookup
[ch
] >= ' ') {
248 EMIT(((ch
>> 6) & 03) + '0');
249 EMIT(((ch
>> 3) & 07) + '0');
250 EMIT(((ch
>> 0) & 07) + '0');
255 if (p
== name
) /* no ending quote needed */
263 size_t quote_c_style(const char *name
, struct strbuf
*sb
, FILE *fp
, int nodq
)
265 return quote_c_style_counted(name
, -1, sb
, fp
, nodq
);
268 void quote_two_c_style(struct strbuf
*sb
, const char *prefix
, const char *path
, int nodq
)
270 if (quote_c_style(prefix
, NULL
, NULL
, 0) ||
271 quote_c_style(path
, NULL
, NULL
, 0)) {
273 strbuf_addch(sb
, '"');
274 quote_c_style(prefix
, sb
, NULL
, 1);
275 quote_c_style(path
, sb
, NULL
, 1);
277 strbuf_addch(sb
, '"');
279 strbuf_addstr(sb
, prefix
);
280 strbuf_addstr(sb
, path
);
284 void write_name_quoted(const char *name
, FILE *fp
, int terminator
)
287 quote_c_style(name
, NULL
, fp
, 0);
291 fputc(terminator
, fp
);
294 void write_name_quotedpfx(const char *pfx
, size_t pfxlen
,
295 const char *name
, FILE *fp
, int terminator
)
300 needquote
= next_quote_pos(pfx
, pfxlen
) < pfxlen
301 || name
[next_quote_pos(name
, -1)];
305 quote_c_style_counted(pfx
, pfxlen
, NULL
, fp
, 1);
306 quote_c_style(name
, NULL
, fp
, 1);
309 fwrite(pfx
, pfxlen
, 1, fp
);
312 fputc(terminator
, fp
);
315 static const char *path_relative(const char *in
, int len
,
316 struct strbuf
*sb
, const char *prefix
,
319 void write_name_quoted_relative(const char *name
, size_t len
,
320 const char *prefix
, size_t prefix_len
,
321 FILE *fp
, int terminator
)
323 struct strbuf sb
= STRBUF_INIT
;
325 name
= path_relative(name
, len
, &sb
, prefix
, prefix_len
);
326 write_name_quoted(name
, fp
, terminator
);
332 * Give path as relative to prefix.
334 * The strbuf may or may not be used, so do not assume it contains the
337 static const char *path_relative(const char *in
, int len
,
338 struct strbuf
*sb
, const char *prefix
,
345 if (prefix_len
< 0) {
347 prefix_len
= strlen(prefix
);
354 while (i
< prefix_len
&& i
< len
&& prefix
[i
] == in
[i
]) {
355 if (prefix
[i
] == '/')
366 strbuf_grow(sb
, len
);
368 while (i
< prefix_len
) {
369 if (prefix
[i
] == '/')
370 strbuf_addstr(sb
, "../");
373 strbuf_add(sb
, in
, len
);
378 /* quote path as relative to the given prefix */
379 char *quote_path_relative(const char *in
, int len
,
380 struct strbuf
*out
, const char *prefix
)
382 struct strbuf sb
= STRBUF_INIT
;
383 const char *rel
= path_relative(in
, len
, &sb
, prefix
, -1);
385 quote_c_style_counted(rel
, strlen(rel
), out
, NULL
, 0);
389 strbuf_addstr(out
, "./");
395 * C-style name unquoting.
397 * Quoted should point at the opening double quote.
398 * + Returns 0 if it was able to unquote the string properly, and appends the
399 * result in the strbuf `sb'.
400 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
401 * that this function will allocate memory in the strbuf, so calling
402 * strbuf_release is mandatory whichever result unquote_c_style returns.
404 * Updates endp pointer to point at one past the ending double quote if given.
406 int unquote_c_style(struct strbuf
*sb
, const char *quoted
, const char **endp
)
408 size_t oldlen
= sb
->len
, len
;
411 if (*quoted
++ != '"')
415 len
= strcspn(quoted
, "\"\\");
416 strbuf_add(sb
, quoted
, len
);
430 switch ((ch
= *quoted
++)) {
431 case 'a': ch
= '\a'; break;
432 case 'b': ch
= '\b'; break;
433 case 'f': ch
= '\f'; break;
434 case 'n': ch
= '\n'; break;
435 case 'r': ch
= '\r'; break;
436 case 't': ch
= '\t'; break;
437 case 'v': ch
= '\v'; break;
440 break; /* verbatim */
442 /* octal values with first digit over 4 overflow */
443 case '0': case '1': case '2': case '3':
444 ac
= ((ch
- '0') << 6);
445 if ((ch
= *quoted
++) < '0' || '7' < ch
)
447 ac
|= ((ch
- '0') << 3);
448 if ((ch
= *quoted
++) < '0' || '7' < ch
)
456 strbuf_addch(sb
, ch
);
460 strbuf_setlen(sb
, oldlen
);
464 /* quoting as a string literal for other languages */
466 void perl_quote_print(FILE *stream
, const char *src
)
468 const char sq
= '\'';
469 const char bq
= '\\';
473 while ((c
= *src
++)) {
474 if (c
== sq
|| c
== bq
)
481 void python_quote_print(FILE *stream
, const char *src
)
483 const char sq
= '\'';
484 const char bq
= '\\';
485 const char nl
= '\n';
489 while ((c
= *src
++)) {
495 if (c
== sq
|| c
== bq
)
502 void tcl_quote_print(FILE *stream
, const char *src
)
507 while ((c
= *src
++)) {
511 case '$': case '\\': case '"':
517 fputs("\\f", stream
);
520 fputs("\\r", stream
);
523 fputs("\\n", stream
);
526 fputs("\\t", stream
);
529 fputs("\\v", stream
);