chiark / gitweb /
tripectl: Quotify the arguments rather than hoping for the best.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 1 Jan 2007 15:46:36 +0000 (15:46 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 1 Jan 2007 15:46:36 +0000 (15:46 +0000)
To make this work, move quotify from admin to the shared utilities.

client/tripectl.c
common/util.c
common/util.h
doc/tripectl.1
server/admin.c

index 7386b304b3a7dd808b4cd46a978fd5073e543498..ea8aadff7599c8f9b6ea91b68a582f274ffb6153 100644 (file)
@@ -532,11 +532,8 @@ int main(int argc, char *argv[])
   if (optind < argc) {
     dstr d = DSTR_INIT;
     setup((f & f_warn) ? "WATCH -A+w" : "WATCH -A");
-    dstr_puts(&d, argv[optind++]);
-    while (optind < argc) {
-      dstr_putc(&d, ' ');
-      dstr_puts(&d, argv[optind++]);
-    }
+    while (optind < argc)
+      u_quotify(&d, argv[optind++]);
     dstr_putc(&d, '\n');
     errno = EIO;
     if (write(fd, d.buf, d.len) != d.len || shutdown(fd, 1))
index ceeb0bd519ca7780e570f92fddeaed9c86ab27ee..4b32985c1f626c777d86e177f078261d4f9c18ef 100644 (file)
 #include <unistd.h>
 #include <fcntl.h>
 
+#include <mLib/dstr.h>
+
 #include "util.h"
 
 #include <sys/ioctl.h>
 
 /*----- Main code ---------------------------------------------------------*/
 
+/* --- @u_quotify@ --- *
+ *
+ * Arguments:  @dstr *d@ = where to write the answer
+ *             @const char *p@ = string to quotify
+ *
+ * Returns:    ---
+ *
+ * Use:                Quotes the given string if necessary, according to our
+ *             quoting rules.
+ */
+
+void u_quotify(dstr *d, const char *p)
+{
+  if (d->len)
+    dstr_putc(d, ' ');
+  if (*p && !p[strcspn(p, "\"' \t\n\v")])
+    dstr_puts(d, p);
+  else {
+    dstr_putc(d, '\"');
+    while (*p) {
+      if (*p == '\\' || *p == '\"')
+       dstr_putc(d, '\\');
+      dstr_putc(d, *p++);
+    }
+    dstr_putc(d, '\"');
+  }
+}
+
 /* --- @u_detach@ --- *
  *
  * Arguments:  ---
index 675f0efed01269ef707a66510bfae9e7f25219f3..0c135480359c14499cef21c02cbda995d8f89aa4 100644 (file)
 
 /*----- Functions provided ------------------------------------------------*/
 
+/* --- @u_quotify@ --- *
+ *
+ * Arguments:  @dstr *d@ = where to write the answer
+ *             @const char *p@ = string to quotify
+ *
+ * Returns:    ---
+ *
+ * Use:                Quotes the given string if necessary, according to our
+ *             quoting rules.
+ */
+
+extern void u_quotify(dstr */*d*/, const char */*p*/);
+
 /* --- @u_detach@ --- *
  *
  * Arguments:  ---
index c0f57a46ebe385b658eb8f24accbc3adf5a5fac3..379fcda18b2c294d80152fb36f6c176ef9dc73b6 100644 (file)
@@ -185,8 +185,9 @@ fatal signal or an end-of-file indication from the server.
 .SS "Use from scripts"
 If arguments are given to
 .BR tripectl ,
-they are concatenated with spaces between and submitted to the server
-after connection.  Any
+they are quoted if necessary to protect spaces and other special
+characters, concatenated with spaces between, and submitted to the
+server after connection.  Any
 .B INFO
 responses returned by the server are written to standard output (without
 the
index 797b47869589b702813b2e48f71ba9c074064a3c..4dbaf33e1e89c4e6a33701f281beb58633f854e5 100644 (file)
@@ -231,34 +231,6 @@ static void a_flush(int fd, unsigned mode, void *v)
 
 /*----- Utility functions -------------------------------------------------*/
 
-/* --- @quotify@ --- *
- *
- * Arguments:  @dstr *d@ = where to write the answer
- *             @const char *p@ = string to quotify
- *
- * Returns:    ---
- *
- * Use:                Quotes the given string if necessary, according to our
- *             quoting rules.
- */
-
-static void quotify(dstr *d, const char *p)
-{
-  if (d->len)
-    dstr_putc(d, ' ');
-  if (*p && !p[strcspn(p, "\"' \t\n\v")])
-    dstr_puts(d, p);
-  else {
-    dstr_putc(d, '\"');
-    while (*p) {
-      if (*p == '\\' || *p == '\"')
-       dstr_putc(d, '\\');
-      dstr_putc(d, *p++);
-    }
-    dstr_putc(d, '\"');
-  }
-}
-
 /* --- @a_vformat@ --- *
  *
  * Arguments:  @dstr *d@ = where to leave the formatted message
@@ -283,8 +255,8 @@ static void a_vformat(dstr *d, const char *fmt, va_list ap)
        const addr *a = va_arg(ap, const addr *);
        switch (a->sa.sa_family) {
          case AF_INET:
-           quotify(d, "INET");
-           quotify(d, inet_ntoa(a->sin.sin_addr));
+           u_quotify(d, "INET");
+           u_quotify(d, inet_ntoa(a->sin.sin_addr));
            dstr_putf(d, " %u", (unsigned)ntohs(a->sin.sin_port));
            break;
          default:
@@ -303,19 +275,19 @@ static void a_vformat(dstr *d, const char *fmt, va_list ap)
        while (d->len && d->buf[d->len - 1] == '=') d->len--;
       } else if (strcmp(fmt, "?TOKENS") == 0) {
        const char *const *av = va_arg(ap, const char *const *);
-       while (*av) quotify(d, *av++);
+       while (*av) u_quotify(d, *av++);
       } else if (strcmp(fmt, "?PEER") == 0)
-       quotify(d, p_name(va_arg(ap, peer *)));
+       u_quotify(d, p_name(va_arg(ap, peer *)));
       else if (strcmp(fmt, "?ERRNO") == 0) {
        dstr_putf(d, " E%d", errno);
-       quotify(d, strerror(errno));
+       u_quotify(d, strerror(errno));
       } else
        abort();
     } else {
       if (*fmt == '!') fmt++;
       DRESET(&dd);
       dstr_vputf(&dd, fmt, &ap);
-      quotify(d, dd.buf);
+      u_quotify(d, dd.buf);
     }
     fmt = va_arg(ap, const char *);
   }
@@ -344,7 +316,7 @@ static void a_vwrite(admin *a, const char *status, const char *tag,
 
   if (tag) dstr_puts(&d, "BG");
   dstr_puts(&d, status);
-  if (tag) quotify(&d, tag);
+  if (tag) u_quotify(&d, tag);
   a_vformat(&d, fmt, ap);
   dstr_putc(&d, '\n');
   dosend(a, d.buf, d.len);