Skip to content

Commit

Permalink
client: don't output empty lines on configure commands
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentbernat committed Apr 21, 2023
1 parent 74d4d69 commit 14ce099
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
53 changes: 37 additions & 16 deletions src/client/text_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#include "writer.h"
Expand All @@ -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)
{
Expand All @@ -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;
Expand All @@ -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)");
}

Expand All @@ -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);
}

Expand All @@ -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);
}
}
Expand All @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit 14ce099

Please sign in to comment.