]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
Add a new output for lldpctl: keyvalue.
authorVincent Bernat <bernat@luffy.cx>
Sun, 11 Apr 2010 08:14:12 +0000 (10:14 +0200)
committerVincent Bernat <bernat@luffy.cx>
Sun, 11 Apr 2010 08:17:07 +0000 (10:17 +0200)
This output allows is easily parseable through a shell script (sed,
grep, awk). There is still room for improvement on how to handle
attributes.

CHANGELOG
man/lldpctl.8
src/Makefile.am
src/display.c
src/kv_writer.c [new file with mode: 0644]
src/lldpctl.c
src/writer.h

index b350997a8d7329c725edfdac9b738e409edd8e8a..2755ce75220708a0773c580f5965263693140759 100644 (file)
--- 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)
 
index f641355aa3f0286660c6c7f343ad08da5a195831..d83f43d42a45f87dfae5b77897373744650d5e09 100644 (file)
@@ -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
index ec471b506e7736bd76c716e49916030f62a0a8fa..f3a8182b257a684ae9be8b118064b738117aa8c1 100644 (file)
@@ -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...
index 8267b0d660c1c9f72bddfa9ff358a2b0de6fbc31..c16d69fee0f612f0e7e71b6866e44be596945f9f 100644 (file)
@@ -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 (file)
index 0000000..ec0b4e5
--- /dev/null
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2010 Andreas Hofmeister <andi@collax.com>
+ *               2010 Vincent Bernat <bernat@luffy.cx>
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#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;
+}
index 7e7107930673b7b0c308f2889f052acda310cf2c..ecf919a8910c33c3c9c14f2a198218df762e24fe 100644 (file)
@@ -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");
index 4b4b89125adff2d284d3a1cdd17cf5da3f2f57a0..dadc201734f49370414200b389cf0e096ef3e37a 100644 (file)
@@ -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 * );