Commit f54228c8 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Add a cli_quote() function for quoting a string properly when reporting

it in the CLI.

Use it for cc_command and listen_address parameters



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@2982 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent e2149b9e
......@@ -315,11 +315,7 @@ tweak_listen_address(struct cli *cli, const struct parspec *par, const char *arg
(void)par;
if (arg == NULL) {
/* Quote the string if we have more than one socket */
if (heritage.nsocks > 1)
cli_out(cli, "\"%s\"", master.listen_address);
else
cli_out(cli, "%s", master.listen_address);
cli_quote(cli, master.listen_address);
return;
}
......@@ -348,7 +344,8 @@ tweak_listen_address(struct cli *cli, const struct parspec *par, const char *arg
int j, n;
if (VSS_parse(av[i], &host, &port) != 0) {
cli_out(cli, "Invalid listen address \"%s\"", av[i]);
cli_out(cli, "Invalid listen address ");
cli_quote(cli, av[i]);
cli_result(cli, CLIS_PARAM);
break;
}
......@@ -356,7 +353,8 @@ tweak_listen_address(struct cli *cli, const struct parspec *par, const char *arg
free(host);
free(port);
if (n == 0) {
cli_out(cli, "Invalid listen address \"%s\"", av[i]);
cli_out(cli, "Invalid listen address ");
cli_quote(cli, av[i]);
cli_result(cli, CLIS_PARAM);
break;
}
......@@ -399,7 +397,7 @@ tweak_cc_command(struct cli *cli, const struct parspec *par, const char *arg)
/* XXX should have tweak_generic_string */
(void)par;
if (arg == NULL) {
cli_out(cli, "%s", mgt_cc_cmd);
cli_quote(cli, mgt_cc_cmd);
} else {
free(mgt_cc_cmd);
mgt_cc_cmd = strdup(arg);
......
......@@ -54,6 +54,7 @@ struct cli_proto {
/* The implementation must provide these functions */
void cli_out(struct cli *cli, const char *fmt, ...);
void cli_quote(struct cli *cli, const char *str);
void cli_param(struct cli *cli);
void cli_result(struct cli *cli, unsigned r);
......
......@@ -68,6 +68,53 @@ cli_out(struct cli *cli, const char *fmt, ...)
va_end(ap);
}
void
cli_quote(struct cli *cli, const char *s)
{
const char *q;
int quote = 0;
for (q = s; *q != '\0'; q++) {
if (!isgraph(*q) || *q == '"') {
quote++;
break;
}
}
if (!quote) {
(void)vsb_cat(cli->sb, s);
return;
}
(void)vsb_putc(cli->sb, '"');
for (q = s; *q != '\0'; q++) {
switch (*q) {
case ' ':
(void)vsb_putc(cli->sb, *q);
break;
case '\\':
case '"':
(void)vsb_putc(cli->sb, '\\');
(void)vsb_putc(cli->sb, *q);
break;
case '\n':
(void)vsb_cat(cli->sb, "\\n");
break;
case '\r':
(void)vsb_cat(cli->sb, "\\r");
break;
case '\t':
(void)vsb_cat(cli->sb, "\\t");
break;
default:
if (isgraph(*q))
(void)vsb_putc(cli->sb, *q);
else
(void)vsb_printf(cli->sb, "\\%o", *q);
break;
}
}
(void)vsb_putc(cli->sb, '"');
}
void
cli_result(struct cli *cli, unsigned res)
{
......@@ -78,7 +125,6 @@ cli_result(struct cli *cli, unsigned res)
printf("CLI result = %d\n", res);
}
void
cli_param(struct cli *cli)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment