]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: move \n vs. \r\n handling into vty code
authorDavid Lamparter <equinox@opensourcerouting.org>
Thu, 13 Jul 2017 15:34:08 +0000 (17:34 +0200)
committerDavid Lamparter <equinox@opensourcerouting.org>
Thu, 13 Jul 2017 18:29:20 +0000 (20:29 +0200)
Instead of having an ?: expression embedded in every single caller of
vty_out, just expand \n to \r\n in the vty code if neccessary.

(Deprecation warnings will be enabled in the next commits which will do
the search-and-replace over the codebase.)

[This reverts commit 4d5f445 "lib: add vty_outln()"]

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
lib/buffer.c
lib/buffer.h
lib/vty.c
lib/vty.h
ospf6d/ospf6d.h

index 649677fc93ea237e2c55c9f1272d6e324cbf4bd4..5d458901ee0c7cf15f08f017532e6396741a5b33 100644 (file)
@@ -199,6 +199,50 @@ buffer_putstr (struct buffer *b, const char *c)
   buffer_put(b, c, strlen(c));
 }
 
+/* Expand \n to \r\n */
+void
+buffer_put_crlf(struct buffer *b, const void *origp, size_t origsize)
+{
+  struct buffer_data *data = b->tail;
+  const char *p = origp, *end = p + origsize, *lf;
+  size_t size;
+
+  lf = memchr(p, '\n', end - p);
+
+  /* We use even last one byte of data buffer. */
+  while (p < end)
+    {
+      size_t avail, chunk;
+
+      /* If there is no data buffer add it. */
+      if (data == NULL || data->cp == b->size)
+        data = buffer_add (b);
+
+      size = (lf ? lf : end) - p;
+      avail = b->size - data->cp;
+
+      chunk = (size <= avail) ? size : avail;
+      memcpy (data->data + data->cp, p, chunk);
+
+      p += chunk;
+      data->cp += chunk;
+
+      if (lf && size <= avail)
+        {
+          /* we just copied up to (including) a '\n' */
+          if (data->cp == b->size)
+            data = buffer_add (b);
+          data->data[data->cp++] = '\r';
+          if (data->cp == b->size)
+            data = buffer_add (b);
+          data->data[data->cp++] = '\n';
+
+          p++;
+          lf = memchr(p, '\n', end - p);
+        }
+    }
+}
+
 /* Keep flushing data to the fd until the buffer is empty or an error is
    encountered or the operation would block. */
 buffer_status_t
index 67ac71cad8b4e6c15e9fd7bbe46536ea5d1a7e1f..059f2cf3388db2aad3bc908a7e341f2a84e8761b 100644 (file)
@@ -41,6 +41,8 @@ extern void buffer_put (struct buffer *, const void *, size_t);
 extern void buffer_putc (struct buffer *, u_char);
 /* Add a NUL-terminated string to the end of the buffer. */
 extern void buffer_putstr (struct buffer *, const char *);
+/* Add given data, inline-expanding \n to \r\n */
+extern void buffer_put_crlf(struct buffer *b, const void *p, size_t size);
 
 /* Combine all accumulated (and unflushed) data inside the buffer into a
    single NUL-terminated string allocated using XMALLOC(MTYPE_TMP).  Note
index e6497b31002d0dda84ea0c21dba1a871fd20dfae..53328248f8a4c93f2636e9c3bfec2bc7be109506 100644 (file)
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -92,23 +92,28 @@ char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
 
 static int do_log_commands = 0;
 
-static int
-vty_out_variadic (struct vty *vty, const char *format, va_list args)
+/* VTY standard output function. */
+int
+vty_out (struct vty *vty, const char *format, ...)
 {
+  va_list args;
   int len = 0;
   int size = 1024;
   char buf[1024];
   char *p = NULL;
-  va_list cp;
 
   if (vty_shell (vty))
-    vprintf (format, args);
+    {
+      va_start (args, format);
+      vprintf (format, args);
+      va_end (args);
+    }
   else
     {
       /* Try to write to initial buffer.  */
-      va_copy (cp, args);
-      len = vsnprintf (buf, sizeof(buf), format, cp);
-      va_end (cp);
+      va_start (args, format);
+      len = vsnprintf (buf, sizeof(buf), format, args);
+      va_end (args);
 
       /* Initial buffer is not enough.  */
       if (len < 0 || len >= size)
@@ -124,9 +129,9 @@ vty_out_variadic (struct vty *vty, const char *format, va_list args)
               if (! p)
                 return -1;
 
-              va_copy (cp, args);
-              len = vsnprintf (p, size, format, cp);
-              va_end (cp);
+              va_start (args, format);
+              len = vsnprintf (p, size, format, args);
+              va_end (args);
 
               if (len > -1 && len < size)
                 break;
@@ -138,7 +143,10 @@ vty_out_variadic (struct vty *vty, const char *format, va_list args)
         p = buf;
 
       /* Pointer p must point out buffer. */
-      buffer_put (vty->obuf, (u_char *) p, len);
+      if (vty->type != VTY_TERM)
+        buffer_put (vty->obuf, (u_char *) p, len);
+      else
+        buffer_put_crlf (vty->obuf, (u_char *) p, len);
 
       /* If p is not different with buf, it is allocated buffer.  */
       if (p != buf)
@@ -147,32 +155,6 @@ vty_out_variadic (struct vty *vty, const char *format, va_list args)
 
   return len;
 }
-/* VTY standard output function. */
-int
-vty_out (struct vty *vty, const char *format, ...)
-{
-  int len;
-  va_list args;
-
-  va_start (args, format);
-  len = vty_out_variadic (vty, format, args);
-  va_end (args);
-
-  return len;
-}
-
-int
-vty_outln (struct vty *vty, const char *format, ...)
-{
-  int len;
-  va_list args;
-
-  va_start (args, format);
-  len = vty_out_variadic (vty, format, args);
-  va_end (args);
-
-  return len + vty_out (vty, "%s", VTYNL);
-}
 
 static int
 vty_log_out (struct vty *vty, const char *level, const char *proto_str,
index 5cd28c0c42ac8d1e96961cdf0c6f380abcb277c8..ef537b753d2be60f0232743c10fe973346152691 100644 (file)
--- a/lib/vty.h
+++ b/lib/vty.h
@@ -179,9 +179,6 @@ struct vty_arg
 /* Integrated configuration file. */
 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
 
-/* Small macro to determine newline is newline only or linefeed needed. */
-#define VTYNL  ((vty->type == VTY_TERM) ? "\r\n" : "\n")
-
 /* for compatibility */
 #if defined(__ICC)
 #define CPP_WARN_STR(X) #X
@@ -198,8 +195,12 @@ struct vty_arg
 #define CPP_WARN(text)
 #endif
 
-#define VTY_NEWLINE                               VTYNL \
-       CPP_WARN("VTY_NEWLINE has been replaced with VTYNL and/or vty_outln().")
+#define VNL                                       "\n" \
+/*     CPP_WARN("VNL has been replaced with \\n.") */
+#define VTYNL                                     "\n" \
+/*     CPP_WARN("VTYNL has been replaced with \\n.") */
+#define VTY_NEWLINE                               "\n" \
+       CPP_WARN("VTY_NEWLINE has been replaced with \\n.")
 #define VTY_GET_INTEGER(desc,v,str)               {(v)=strtoul ((str), NULL, 10);} \
        CPP_WARN("VTY_GET_INTEGER is no longer useful, use strtoul() or DEFPY.")
 #define VTY_GET_INTEGER_RANGE(desc,v,str,min,max) {(v)=strtoul ((str), NULL, 10);} \
@@ -212,6 +213,9 @@ struct vty_arg
        CPP_WARN("VTY_GET_IPV4_ADDRESS is no longer useful, use inet_aton() or DEFPY.")
 #define VTY_GET_IPV4_PREFIX(desc,v,str)           str2prefix_ipv4 ((str), &(v)) \
        CPP_WARN("VTY_GET_IPV4_PREFIX is no longer useful, use str2prefix_ipv4() or DEFPY.")
+#define vty_outln(vty, str, ...) \
+       vty_out(vty, str "\n", ## __VA_ARGS__) \
+/*     CPP_WARN("vty_outln is no longer useful, use vty_out(...\\n...)") */
 
 /* Default time out value */
 #define VTY_TIMEOUT_DEFAULT 600
@@ -239,7 +243,6 @@ extern void vty_reset (void);
 extern struct vty *vty_new (void);
 extern struct vty *vty_stdio (void (*atclose)(void));
 extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
-extern int vty_outln (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
 extern void vty_read_config (const char *, char *);
 extern void vty_time_print (struct vty *, int);
 extern void vty_serv_sock (const char *, unsigned short, const char *);
index 41082e451b3e591a87a338fbaa5f43d5849684b6..347ad3487181408e2f92178ae62c306f37dde0fa 100644 (file)
@@ -90,7 +90,6 @@ extern struct thread_master *master;
 #define OSPF6_ROUTER_ID_STR "Specify Router-ID\n"
 #define OSPF6_LS_ID_STR     "Specify Link State ID\n"
 
-#define VNL VTYNL
 #define OSPF6_CMD_CHECK_RUNNING() \
   if (ospf6 == NULL) \
     { \