From: Vincent Bernat Date: Sun, 11 Apr 2010 08:14:12 +0000 (+0200) Subject: Add a new output for lldpctl: keyvalue. X-Git-Tag: 0.5.1~22 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=999509a3bdf6b1c05fd632c2643415efe349680c;p=thirdparty%2Flldpd.git Add a new output for lldpctl: keyvalue. This output allows is easily parseable through a shell script (sed, grep, awk). There is still room for improvement on how to handle attributes. --- diff --git a/CHANGELOG b/CHANGELOG index b350997a..2755ce75 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -6,7 +6,8 @@ lldpd (0.5.1) Philipp Kempgen. + Allow to set LLDP-MED POE-MDI from lldpctl. + Add a summary of available options in "lldpd -h" and "lldpctl -h", - thanks to a patch from Jorge Boncompte + thanks to a patch from Jorge Boncompte. + + Add a new output (keyvalue) for lldpctl. lldpd (0.5.0) diff --git a/man/lldpctl.8 b/man/lldpctl.8 index f641355a..d83f43d4 100644 --- a/man/lldpctl.8 +++ b/man/lldpctl.8 @@ -45,9 +45,10 @@ The options are as follows: Enable more debugging information. .It Fl f Ar format Choose the output format. Currently -.Em plain -and +.Em plain , .Em xml +and +.Em keyvalue formats are available. The default is .Em plain. .It Fl L Ar location diff --git a/src/Makefile.am b/src/Makefile.am index ec471b50..f3a8182b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,7 +15,7 @@ lldpd_SOURCES = main.c lldpd_LDADD = liblldpd.la ## lldpctl -lldpctl_SOURCES = lldpctl.c display.c writer.h text_writer.c +lldpctl_SOURCES = lldpctl.c display.c writer.h text_writer.c kv_writer.c lldpctl_LDADD = libcommon.la ## With SNMP... diff --git a/src/display.c b/src/display.c index 8267b0d6..c16d69fe 100644 --- a/src/display.c +++ b/src/display.c @@ -939,6 +939,8 @@ display_interfaces(int s, const char * fmt, int argc, char *argv[]) if ( strcmp(fmt,"plain") == 0 ) { w = txt_init( stdout ); + } else if (strcmp(fmt, "keyvalue") == 0) { + w = kv_init( stdout ); } #ifdef USE_XML else if ( strcmp(fmt,"xml") == 0 ) { diff --git a/src/kv_writer.c b/src/kv_writer.c new file mode 100644 index 00000000..ec0b4e5c --- /dev/null +++ b/src/kv_writer.c @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2010 Andreas Hofmeister + * 2010 Vincent Bernat + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include "writer.h" +#include "lldpd.h" + +struct kv_writer_private { + FILE * fh; + char * prefix; +}; + +void +kv_start(struct writer *w , const char *tag, const char *descr) +{ + struct kv_writer_private *p = w->priv; + char *newprefix; + int s; + + s = strlen(p->prefix) + 1 + strlen(tag); + if ((newprefix = malloc(s+1)) == NULL) + fatal(NULL); + if (strlen(p->prefix) > 0) + snprintf(newprefix, s+1, "%s.%s", p->prefix, tag); + else + snprintf(newprefix, s+1, "%s", tag); + free(p->prefix); + p->prefix = newprefix; +} + +void +kv_data(struct writer *w, const char *data) +{ + struct kv_writer_private *p = w->priv; + fprintf(p->fh, "%s=%s\n", p->prefix, data); +} + +void +kv_end(struct writer *w) +{ + struct kv_writer_private *p = w->priv; + char *dot; + + if ((dot = strrchr(p->prefix, '.')) == NULL) + p->prefix[0] = '\0'; + else + *dot = '\0'; +} + +void +kv_attr(struct writer *w, const char *tag, const char *descr, const char *value) +{ + if (!strcmp(tag, "name") || !strcmp(tag, "type")) { + /* Special case for name, replace the last prefix */ + kv_end(w); + kv_start(w, value, NULL); + } else { + kv_start(w, tag, NULL); + kv_data(w, value); + kv_end(w); + } +} + +void +kv_finish(struct writer *w) +{ + struct kv_writer_private *p = w->priv; + + free(p->prefix); + free(w->priv); + w->priv = NULL; + + free(w); +} + +struct writer * +kv_init(FILE *fh) +{ + + struct writer *result; + struct kv_writer_private *priv; + + if ((priv = malloc(sizeof(*priv))) == NULL) + fatal(NULL); + + priv->fh = fh; + priv->prefix = strdup(""); + + if ((result = malloc(sizeof(struct writer))) == NULL) + fatal(NULL); + + result->priv = priv; + result->start = kv_start; + result->attr = kv_attr; + result->data = kv_data; + result->end = kv_end; + result->finish= kv_finish; + + return result; +} diff --git a/src/lldpctl.c b/src/lldpctl.c index 7e710793..ecf919a8 100644 --- a/src/lldpctl.c +++ b/src/lldpctl.c @@ -51,7 +51,7 @@ usage(void) fprintf(stderr, "\n"); fprintf(stderr, "-d Enable more debugging information.\n"); - fprintf(stderr, "-f format Choose output format (plain or xml).\n"); + fprintf(stderr, "-f format Choose output format (plain, keyvalue or xml).\n"); #ifdef ENABLE_LLDPMED fprintf(stderr, "-L location Enable the transmission of LLDP-MED location TLV for the\n"); fprintf(stderr, " given interfaces. Can be repeated to enable the transmission\n"); diff --git a/src/writer.h b/src/writer.h index 4b4b8912..dadc2017 100644 --- a/src/writer.h +++ b/src/writer.h @@ -35,6 +35,7 @@ struct writer { #define tag_datatag(w,t,d,...) { w->start(w,t,d); w->data(w,## __VA_ARGS__); w->end(w); } extern struct writer * txt_init( FILE * ); +extern struct writer * kv_init( FILE * ); #ifdef USE_XML extern struct writer * xml_init( FILE * );