From: Vincent Bernat Date: Thu, 19 May 2016 18:26:37 +0000 (+0200) Subject: client: flush XML object once we have one X-Git-Tag: 0.9.3~12 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b8dec1b4eeee1a0be3bd1f905e16a5743c73d15e;p=thirdparty%2Flldpd.git client: flush XML object once we have one See previous commit. This fixes `lldpcli -f xml watch`. Fix: #179. --- diff --git a/src/client/xml_writer.c b/src/client/xml_writer.c index a53c6d66..aaa50cc3 100644 --- a/src/client/xml_writer.c +++ b/src/client/xml_writer.c @@ -34,6 +34,7 @@ struct xml_writer_private { FILE *fh; + ssize_t depth; xmlTextWriterPtr xw; xmlDocPtr doc; }; @@ -48,6 +49,8 @@ void xml_start(struct writer *w , const char *tag, const char *descr ) { if (xmlTextWriterWriteFormatAttribute(p->xw, BAD_CAST "label", "%s", descr) < 0) log_warnx("lldpctl", "cannot add attribute 'label' to element %s", tag); } + + p->depth++; } void xml_attr(struct writer *w, const char *tag, const char *descr, const char *value ) { @@ -63,30 +66,49 @@ void xml_data(struct writer *w, const char *data) { log_warnx("lldpctl", "cannot add '%s' as data to element", data?data:"(none)"); } +void xml_new_writer(struct xml_writer_private *priv) +{ + priv->xw = xmlNewTextWriterDoc(&(priv->doc), 0); + if (!priv->xw) + fatalx("lldpctl", "cannot create xml writer"); + + xmlTextWriterSetIndent(priv->xw, 4); + + if (xmlTextWriterStartDocument(priv->xw, NULL, "UTF-8", NULL) < 0 ) + fatalx("lldpctl", "cannot start xml document"); +} + void xml_end(struct writer *w) { struct xml_writer_private *p = w->priv; if (xmlTextWriterEndElement(p->xw) < 0 ) log_warnx("lldpctl", "cannot end element"); -} -#define MY_ENCODING "UTF-8" + if (--p->depth == 0) { + int failed = 0; -void xml_finish(struct writer *w) { - struct xml_writer_private *p = w->priv; - int failed = 0; + if (xmlTextWriterEndDocument(p->xw) < 0 ) { + log_warnx("lldpctl", "cannot finish document"); + failed = 1; + } - if (xmlTextWriterEndDocument(p->xw) < 0 ) { - log_warnx("lldpctl", "cannot finish document"); - failed = 1; - } + xmlFreeTextWriter(p->xw); - xmlFreeTextWriter(p->xw); + if (!failed) + xmlDocDump(p->fh, p->doc); - if (!failed) - xmlDocDump(p->fh, p->doc); + xmlFreeDoc(p->doc); - xmlFreeDoc(p->doc); + xml_new_writer(p); + } +} + +void xml_finish(struct writer *w) { + struct xml_writer_private *p = w->priv; + if (p->depth != 0) { + log_warnx("lldpctl", "unbalanced tags"); + /* memory leak... */ + } free(w->priv); free(w); @@ -98,30 +120,18 @@ struct writer *xml_init(FILE *fh) { struct xml_writer_private *priv; priv = malloc(sizeof(*priv)); - priv->fh = fh; if (!priv) { fatalx("lldpctl", "out of memory"); return NULL; } + priv->fh = fh; + priv->depth = 0; - priv->xw = xmlNewTextWriterDoc(&(priv->doc), 0); - if (!priv->xw) { - fatalx("lldpctl", "cannot create xml writer"); - return NULL; - } - - xmlTextWriterSetIndent(priv->xw, 4); - - if (xmlTextWriterStartDocument(priv->xw, NULL, MY_ENCODING, NULL) < 0 ) { - fatalx("lldpctl", "cannot start xml document"); - return NULL; - } + xml_new_writer(priv); result = malloc(sizeof(struct writer)); - if (!result) { + if (!result) fatalx("lldpctl", "out of memory"); - return NULL; - } result->priv = priv; result->start = xml_start;