]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
client: don't output empty lines on configure commands
authorVincent Bernat <vincent@bernat.ch>
Fri, 21 Apr 2023 20:31:26 +0000 (22:31 +0200)
committerVincent Bernat <vincent@bernat.ch>
Fri, 21 Apr 2023 20:31:45 +0000 (22:31 +0200)
NEWS
src/client/text_writer.c

diff --git a/NEWS b/NEWS
index ffc8e8047e9d436d91a7a6f22d7560cef49cf294..6fda933e5afd6ac64ff0e689fa2d8fec07a15422 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,7 @@
 lldpd (1.0.17)
  * Fix:
    + Read overflow when parsing CDP addresses. Thanks to Matteo Memelli.
+   + Don't output empty lines on configure commands.
 
 lldpd (1.0.16)
  * Fix:
index 46664d090e863f89c50a9c680d69fffaaf938c42..95990e749475d947197dd19c58cb8a345bcae51e 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>
 
 #include "writer.h"
@@ -29,8 +30,27 @@ struct txt_writer_private {
        FILE *fh;
        int level;
        int attrs;
+       int lastnl;
 };
 
+static void
+txt_fprintf(struct txt_writer_private *p, const char *fmt, ...)
+{
+       va_list ap;
+
+       // Don't add a newline if we already had one.
+       while (p->lastnl && fmt[0] == '\n') {
+               fmt += 1;
+       }
+       if (fmt[0] == '\0') return;
+
+       va_start(ap, fmt);
+       vfprintf(p->fh, fmt, ap);
+       va_end(ap);
+
+       p->lastnl = (strlen(fmt) > 0 && fmt[strlen(fmt) - 1] == '\n');
+}
+
 static void
 txt_start(struct writer *w, const char *tag, const char *descr)
 {
@@ -39,19 +59,19 @@ txt_start(struct writer *w, const char *tag, const char *descr)
        char buf[128];
 
        if (p->level == 0) {
-               fprintf(p->fh, "%s\n", sep);
-       } else {
-               fprintf(p->fh, "\n");
+               txt_fprintf(p, "%s\n", sep);
+       } else if (!p->lastnl) {
+               txt_fprintf(p, "\n");
        }
 
        for (i = 1; i < p->level; i++) {
-               fprintf(p->fh, "  ");
+               txt_fprintf(p, "  ");
        }
 
        snprintf(buf, sizeof(buf), "%s:", descr);
-       fprintf(p->fh, "%-13s", buf);
+       txt_fprintf(p, "%-13s", buf);
 
-       if (p->level == 0) fprintf(p->fh, "\n%s", sep);
+       if (p->level == 0) txt_fprintf(p, "\n%s", sep);
 
        p->level++;
        p->attrs = 0;
@@ -63,10 +83,10 @@ txt_attr(struct writer *w, const char *tag, const char *descr, const char *value
        struct txt_writer_private *p = w->priv;
 
        if (descr == NULL || strlen(descr) == 0) {
-               fprintf(p->fh, "%s%s", (p->attrs > 0 ? ", " : " "),
+               txt_fprintf(p, "%s%s", (p->attrs > 0 ? ", " : " "),
                    value ? value : "(none)");
        } else {
-               fprintf(p->fh, "%s%s: %s", (p->attrs > 0 ? ", " : " "), descr,
+               txt_fprintf(p, "%s%s: %s", (p->attrs > 0 ? ", " : " "), descr,
                    value ? value : "(none)");
        }
 
@@ -81,24 +101,24 @@ txt_data(struct writer *w, const char *data)
        char *v = begin = data ? strdup(data) : NULL;
 
        if (v == NULL) {
-               fprintf(p->fh, " %s", data ? data : "(none)");
+               txt_fprintf(p, " %s", data ? data : "(none)");
                return;
        }
 
-       fprintf(p->fh, " ");
+       txt_fprintf(p, " ");
        while ((nl = strchr(v, '\n')) != NULL) {
                *nl = '\0';
-               fprintf(p->fh, "%s\n", v);
+               txt_fprintf(p, "%s\n", v);
                v = nl + 1;
 
                /* Indent */
                int i;
                for (i = 1; i < p->level - 1; i++) {
-                       fprintf(p->fh, "  ");
+                       txt_fprintf(p, "  ");
                }
-               fprintf(p->fh, "%-14s", " ");
+               txt_fprintf(p, "%-14s", " ");
        }
-       fprintf(p->fh, "%s", v);
+       txt_fprintf(p, "%s", v);
        free(begin);
 }
 
@@ -109,7 +129,7 @@ txt_end(struct writer *w)
        p->level--;
 
        if (p->level == 1) {
-               fprintf(p->fh, "\n%s", sep);
+               txt_fprintf(p, "\n%s", sep);
                fflush(p->fh);
        }
 }
@@ -119,7 +139,7 @@ txt_finish(struct writer *w)
 {
        struct txt_writer_private *p = w->priv;
 
-       fprintf(p->fh, "\n");
+       txt_fprintf(p, "\n");
 
        free(w->priv);
        w->priv = NULL;
@@ -143,6 +163,7 @@ txt_init(FILE *fh)
        priv->fh = fh;
        priv->level = 0;
        priv->attrs = 0;
+       priv->lastnl = 1;
 
        result = malloc(sizeof(struct writer));
        if (!result) {