From 4f02d05931b1f0aa8ac27cc5851f98e26055063d Mon Sep 17 00:00:00 2001 From: phk Date: Tue, 22 Jul 2008 07:37:32 +0000 Subject: [PATCH] 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: svn+ssh://projects.linpro.no/svn/varnish/trunk@2982 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/mgt_param.c | 14 +++---- varnish-cache/include/cli_priv.h | 1 + varnish-cache/lib/libvarnish/cli_common.c | 48 ++++++++++++++++++++++- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/varnish-cache/bin/varnishd/mgt_param.c b/varnish-cache/bin/varnishd/mgt_param.c index c982471a..d1f29e84 100644 --- a/varnish-cache/bin/varnishd/mgt_param.c +++ b/varnish-cache/bin/varnishd/mgt_param.c @@ -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); diff --git a/varnish-cache/include/cli_priv.h b/varnish-cache/include/cli_priv.h index 2ff5677c..f13a4883 100644 --- a/varnish-cache/include/cli_priv.h +++ b/varnish-cache/include/cli_priv.h @@ -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); diff --git a/varnish-cache/lib/libvarnish/cli_common.c b/varnish-cache/lib/libvarnish/cli_common.c index dd8d6d92..def3da89 100644 --- a/varnish-cache/lib/libvarnish/cli_common.c +++ b/varnish-cache/lib/libvarnish/cli_common.c @@ -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) { -- 2.39.5