From 83eba583d7be5afaf2eba35ce4d391f645af4bfa Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Thu, 13 Jul 2017 17:34:08 +0200 Subject: [PATCH] lib: move \n vs. \r\n handling into vty code 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 --- lib/buffer.c | 44 ++++++++++++++++++++++++++++++++++++++ lib/buffer.h | 2 ++ lib/vty.c | 56 +++++++++++++++++-------------------------------- lib/vty.h | 15 +++++++------ ospf6d/ospf6d.h | 1 - 5 files changed, 74 insertions(+), 44 deletions(-) diff --git a/lib/buffer.c b/lib/buffer.c index 649677fc9..5d458901e 100644 --- a/lib/buffer.c +++ b/lib/buffer.c @@ -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 diff --git a/lib/buffer.h b/lib/buffer.h index 67ac71cad..059f2cf33 100644 --- a/lib/buffer.h +++ b/lib/buffer.h @@ -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 diff --git a/lib/vty.c b/lib/vty.c index e6497b310..53328248f 100644 --- 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, diff --git a/lib/vty.h b/lib/vty.h index 5cd28c0c4..ef537b753 100644 --- 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 *); diff --git a/ospf6d/ospf6d.h b/ospf6d/ospf6d.h index 41082e451..347ad3487 100644 --- a/ospf6d/ospf6d.h +++ b/ospf6d/ospf6d.h @@ -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) \ { \ -- 2.39.2