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