1e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng#include "cache.h" 2e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng#include "quote.h" 3e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng 4e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng/* Help to copy the thing properly quoted for the shell safety. 5e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng * any single quote is replaced with '\'', any exclamation point 6e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng * is replaced with '\!', and the whole thing is enclosed in a 7e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng * 8e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng * E.g. 9e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng * original sq_quote result 10e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng * name ==> name ==> 'name' 11e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng * a b ==> a b ==> 'a b' 12e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng * a'b ==> a'\''b ==> 'a'\''b' 13e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng * a!b ==> a'\!'b ==> 'a'\!'b' 14e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng */ 15e6817ec1d8ab31fc7b01906e305f848542df6413Ben Chengstatic inline int need_bs_quote(char c) 16e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng{ 17e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng return (c == '\'' || c == '!'); 18e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng} 19e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng 20e6817ec1d8ab31fc7b01906e305f848542df6413Ben Chengstatic void sq_quote_buf(struct strbuf *dst, const char *src) 21e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng{ 22e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng char *to_free = NULL; 23e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng 24e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng if (dst->buf == src) 25e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng to_free = strbuf_detach(dst, NULL); 26e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng 27e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng strbuf_addch(dst, '\''); 28e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng while (*src) { 29e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng size_t len = strcspn(src, "'!"); 30e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng strbuf_add(dst, src, len); 31e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng src += len; 32e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng while (need_bs_quote(*src)) { 33e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng strbuf_addstr(dst, "'\\"); 34e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng strbuf_addch(dst, *src++); 35e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng strbuf_addch(dst, '\''); 36e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng } 37e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng } 38e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng strbuf_addch(dst, '\''); 39e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng free(to_free); 40e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng} 41e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng 42e6817ec1d8ab31fc7b01906e305f848542df6413Ben Chengvoid sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen) 43e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng{ 44e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng int i; 45e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng 46e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng /* Copy into destination buffer. */ 47e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng strbuf_grow(dst, 255); 48e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng for (i = 0; argv[i]; ++i) { 49e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng strbuf_addch(dst, ' '); 50e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng sq_quote_buf(dst, argv[i]); 51e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng if (maxlen && dst->len > maxlen) 52e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng die("Too many or long arguments"); 53e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng } 54e6817ec1d8ab31fc7b01906e305f848542df6413Ben Cheng} 55