Arnaldo Carvalho de Melo | 380a71a | 2016-07-07 11:17:38 -0300 | [diff] [blame^] | 1 | #include <stdlib.h> |
| 2 | #include "strbuf.h" |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 3 | #include "quote.h" |
Arnaldo Carvalho de Melo | 380a71a | 2016-07-07 11:17:38 -0300 | [diff] [blame^] | 4 | #include "util.h" |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 5 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 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 |
| 9 | * |
| 10 | * E.g. |
| 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' |
| 16 | */ |
| 17 | static inline int need_bs_quote(char c) |
| 18 | { |
| 19 | return (c == '\'' || c == '!'); |
| 20 | } |
| 21 | |
Masami Hiramatsu | 70a6898 | 2016-05-10 14:47:26 +0900 | [diff] [blame] | 22 | static int sq_quote_buf(struct strbuf *dst, const char *src) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 23 | { |
| 24 | char *to_free = NULL; |
Masami Hiramatsu | 70a6898 | 2016-05-10 14:47:26 +0900 | [diff] [blame] | 25 | int ret; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 26 | |
| 27 | if (dst->buf == src) |
| 28 | to_free = strbuf_detach(dst, NULL); |
| 29 | |
Masami Hiramatsu | 70a6898 | 2016-05-10 14:47:26 +0900 | [diff] [blame] | 30 | ret = strbuf_addch(dst, '\''); |
| 31 | while (!ret && *src) { |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 32 | size_t len = strcspn(src, "'!"); |
Masami Hiramatsu | 70a6898 | 2016-05-10 14:47:26 +0900 | [diff] [blame] | 33 | ret = strbuf_add(dst, src, len); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 34 | src += len; |
Masami Hiramatsu | 70a6898 | 2016-05-10 14:47:26 +0900 | [diff] [blame] | 35 | while (!ret && need_bs_quote(*src)) |
| 36 | ret = strbuf_addf(dst, "'\\%c\'", *src++); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 37 | } |
Masami Hiramatsu | 70a6898 | 2016-05-10 14:47:26 +0900 | [diff] [blame] | 38 | if (!ret) |
| 39 | ret = strbuf_addch(dst, '\''); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 40 | free(to_free); |
Masami Hiramatsu | 70a6898 | 2016-05-10 14:47:26 +0900 | [diff] [blame] | 41 | |
| 42 | return ret; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Masami Hiramatsu | 70a6898 | 2016-05-10 14:47:26 +0900 | [diff] [blame] | 45 | int sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 46 | { |
Masami Hiramatsu | 70a6898 | 2016-05-10 14:47:26 +0900 | [diff] [blame] | 47 | int i, ret; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 48 | |
| 49 | /* Copy into destination buffer. */ |
Masami Hiramatsu | 70a6898 | 2016-05-10 14:47:26 +0900 | [diff] [blame] | 50 | ret = strbuf_grow(dst, 255); |
| 51 | for (i = 0; !ret && argv[i]; ++i) { |
| 52 | ret = strbuf_addch(dst, ' '); |
| 53 | if (ret) |
| 54 | break; |
| 55 | ret = sq_quote_buf(dst, argv[i]); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 56 | if (maxlen && dst->len > maxlen) |
| 57 | die("Too many or long arguments"); |
| 58 | } |
Masami Hiramatsu | 70a6898 | 2016-05-10 14:47:26 +0900 | [diff] [blame] | 59 | return ret; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 60 | } |