]> err.no Git - varnish/commitdiff
Add a cli_quote() function for quoting a string properly when reporting
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Tue, 22 Jul 2008 07:37:32 +0000 (07:37 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Tue, 22 Jul 2008 07:37:32 +0000 (07:37 +0000)
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
varnish-cache/include/cli_priv.h
varnish-cache/lib/libvarnish/cli_common.c

index c982471a6ec6cdeb83c12a34ab7b31110ef3c0e4..d1f29e84b9b5a3f6efbfd859ee4f20cbdd6a8045 100644 (file)
@@ -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);
index 2ff5677cf289d0d5e9610f44c669e6f5aa668163..f13a4883b72ad4dc81875d2b78adb13a5c1dde7b 100644 (file)
@@ -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);
 
index dd8d6d9237918a413c2bd77bc790f1aa5a023579..def3da893a19b8907d12b463ff54adc4b05de01b 100644 (file)
@@ -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)
 {