From 4d5f445750e01467898eee47796e80d808500d56 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Wed, 21 Jun 2017 02:47:44 +0000 Subject: [PATCH] lib: add vty_outln() Like *.println() in some other unspeakable languages Signed-off-by: Quentin Young --- lib/vty.c | 45 ++++++++++++++++++++++++++++++++------------- lib/vty.h | 1 + 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/lib/vty.c b/lib/vty.c index 54a5f727e1..c6a22503b9 100644 --- a/lib/vty.c +++ b/lib/vty.c @@ -92,28 +92,23 @@ char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG; static int do_log_commands = 0; -/* VTY standard output function. */ -int -vty_out (struct vty *vty, const char *format, ...) +static int +vty_out_variadic (struct vty *vty, const char *format, va_list args) { - va_list args; int len = 0; int size = 1024; char buf[1024]; char *p = NULL; + va_list cp; if (vty_shell (vty)) - { - va_start (args, format); - vprintf (format, args); - va_end (args); - } + vprintf (format, args); else { /* Try to write to initial buffer. */ - va_start (args, format); + va_copy (cp, args); len = vsnprintf (buf, sizeof(buf), format, args); - va_end (args); + va_end (cp); /* Initial buffer is not enough. */ if (len < 0 || len >= size) @@ -129,9 +124,7 @@ vty_out (struct vty *vty, const char *format, ...) if (! p) return -1; - va_start (args, format); len = vsnprintf (p, size, format, args); - va_end (args); if (len > -1 && len < size) break; @@ -152,6 +145,32 @@ vty_out (struct vty *vty, const char *format, ...) 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", VTY_NEWLINE); +} 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 e658eb236c..ee2aeac675 100644 --- a/lib/vty.h +++ b/lib/vty.h @@ -215,6 +215,7 @@ 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 *); -- 2.39.5