#include <stdlib.h>
#include "strbuf.h"
#include "quote.h"
#include "util.h"

/* Help to copy the thing properly quoted for the shell safety.
 * any single quote is replaced with '\'', any exclamation point
 * is replaced with '\!', and the whole thing is enclosed in a
 *
 * E.g.
 *  original     sq_quote     result
 *  name     ==> name      ==> 'name'
 *  a b      ==> a b       ==> 'a b'
 *  a'b      ==> a'\''b    ==> 'a'\''b'
 *  a!b      ==> a'\!'b    ==> 'a'\!'b'
 */
static inline int need_bs_quote(char c)
{
	return (c == '\'' || c == '!');
}

static int sq_quote_buf(struct strbuf *dst, const char *src)
{
	char *to_free = NULL;
	int ret;

	if (dst->buf == src)
		to_free = strbuf_detach(dst, NULL);

	ret = strbuf_addch(dst, '\'');
	while (!ret && *src) {
		size_t len = strcspn(src, "'!");
		ret = strbuf_add(dst, src, len);
		src += len;
		while (!ret && need_bs_quote(*src))
			ret = strbuf_addf(dst, "'\\%c\'", *src++);
	}
	if (!ret)
		ret = strbuf_addch(dst, '\'');
	free(to_free);

	return ret;
}

int sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
{
	int i, ret;

	/* Copy into destination buffer. */
	ret = strbuf_grow(dst, 255);
	for (i = 0; !ret && argv[i]; ++i) {
		ret = strbuf_addch(dst, ' ');
		if (ret)
			break;
		ret = sq_quote_buf(dst, argv[i]);
		if (maxlen && dst->len > maxlen)
			return -ENOSPC;
	}
	return ret;
}

<tr><td class='sub'>net-next plumbings</td><td class='sub right'>Tobias Klauser</td></tr></table>
<table class='tabs'><tr><td>
<a href='/cgit.cgi/linux/net-next.git/'>summary</a><a href='/cgit.cgi/linux/net-next.git/refs/?id=c5d35cb32cffa6e4c2db1cbd9a544e10a8d6fda9'>refs</a><a class='active' href='/cgit.cgi/linux/net-next.git/log/include/dt-bindings/pinctrl/dm814x.h'>log</a><a href='/cgit.cgi/linux/net-next.git/tree/include/dt-bindings/pinctrl/dm814x.h?id=c5d35cb32cffa6e4c2db1cbd9a544e10a8d6fda9'>tree</a><a href='/cgit.cgi/linux/net-next.git/commit/include/dt-bindings/pinctrl/dm814x.h?id=c5d35cb32cffa6e4c2db1cbd9a544e10a8d6fda9'>commit</a><a href='/cgit.cgi/linux/net-next.git/diff/include/dt-bindings/pinctrl/dm814x.h?id=c5d35cb32cffa6e4c2db1cbd9a544e10a8d6fda9'>diff</a></td><td class='form'><form class='right' method='get' action='/cgit.cgi/linux/net-next.git/log/include/dt-bindings/pinctrl/dm814x.h'>
<input type='hidden' name='id' value='c5d35cb32cffa6e4c2db1cbd9a544e10a8d6fda9'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/cgit.cgi/linux/net-next.git/log/?id=c5d35cb32cffa6e4c2db1cbd9a544e10a8d6fda9'>root</a>/<a href='/cgit.cgi/linux/net-next.git/log/include?id=c5d35cb32cffa6e4c2db1cbd9a544e10a8d6fda9'>include</a>/<a href='/cgit.cgi/linux/net-next.git/log/include/dt-bindings?id=c5d35cb32cffa6e4c2db1cbd9a544e10a8d6fda9'>dt-bindings</a>/<a href='/cgit.cgi/linux/net-next.git/log/include/dt-bindings/pinctrl?id=c5d35cb32cffa6e4c2db1cbd9a544e10a8d6fda9'>pinctrl</a>/<a href='/cgit.cgi/linux/net-next.git/log/include/dt-bindings/pinctrl/dm814x.h?id=c5d35cb32cffa6e4c2db1cbd9a544e10a8d6fda9'>dm814x.h</a></div><div class='content'><table class='list nowrap'><tr class='nohover'><th class='left'>Age</th><th class='left'>Commit message (<a href='/cgit.cgi/linux/net-next.git/log/include/dt-bindings/pinctrl/dm814x.h?id=c5d35cb32cffa6e4c2db1cbd9a544e10a8d6fda9&amp;showmsg=1'>Expand</a>)</th><th class='left'>Author</th><th class='left'>Files</th><th class='left'>Lines</th></tr>