TAILQ_ENTRY(json_element) next;
json_t *el;
};
-TAILQ_HEAD(json_writer_private, json_element);
+TAILQ_HEAD(json_element_list, json_element);
+struct json_writer_private {
+ FILE *fh;
+ struct json_element_list els;
+};
static void
jansson_start(struct writer *w, const char *tag, const char *descr)
{
struct json_writer_private *p = w->priv;
- struct json_element *current = TAILQ_LAST(p, json_writer_private);
+ struct json_element *current = TAILQ_LAST(&p->els, json_element_list);
struct json_element *new;
json_t *exist;
if (new == NULL) fatal(NULL, NULL);
new->el = json_object();
json_array_append_new(exist, new->el);
- TAILQ_INSERT_TAIL(p, new, next);
+ TAILQ_INSERT_TAIL(&p->els, new, next);
}
static void
const char *descr, const char *value)
{
struct json_writer_private *p = w->priv;
- struct json_element *current = TAILQ_LAST(p, json_writer_private);
+ struct json_element *current = TAILQ_LAST(&p->els, json_element_list);
json_t *jvalue;
if (value && (!strcmp(value, "yes") || !strcmp(value, "on")))
jvalue = json_true();
jansson_data(struct writer *w, const char *data)
{
struct json_writer_private *p = w->priv;
- struct json_element *current = TAILQ_LAST(p, json_writer_private);
+ struct json_element *current = TAILQ_LAST(&p->els, json_element_list);
json_object_set_new(current->el, "value",
json_string(data?data:""));
}
jansson_end(struct writer *w)
{
struct json_writer_private *p = w->priv;
- struct json_element *current = TAILQ_LAST(p, json_writer_private);
+ struct json_element *current = TAILQ_LAST(&p->els, json_element_list);
if (current == NULL) {
log_warnx("lldpctl", "unbalanced tags");
return;
}
- TAILQ_REMOVE(p, current, next);
+ TAILQ_REMOVE(&p->els, current, next);
free(current);
}
jansson_finish(struct writer *w)
{
struct json_writer_private *p = w->priv;
- if (TAILQ_EMPTY(p)) {
+ if (TAILQ_EMPTY(&p->els)) {
log_warnx("lldpctl", "nothing to output");
- } else if (TAILQ_NEXT(TAILQ_FIRST(p), next) != NULL) {
+ } else if (TAILQ_NEXT(TAILQ_FIRST(&p->els), next) != NULL) {
log_warnx("lldpctl", "unbalanced tags");
/* memory will leak... */
} else {
- struct json_element *first = TAILQ_FIRST(p);
+ struct json_element *first = TAILQ_FIRST(&p->els);
json_t *export = jansson_cleanup(first->el);
if (json_dumpf(export,
- stdout,
+ p->fh,
JSON_INDENT(2) | JSON_PRESERVE_ORDER) == -1)
log_warnx("lldpctl", "unable to output JSON");
json_decref(first->el);
json_decref(export);
- TAILQ_REMOVE(p, first, next);
+ TAILQ_REMOVE(&p->els, first, next);
free(first);
}
free(p);
priv = malloc(sizeof(*priv));
root = malloc(sizeof(*root));
if (priv == NULL || root == NULL) fatal(NULL, NULL);
- TAILQ_INIT(priv);
- TAILQ_INSERT_TAIL(priv, root, next);
+
+ priv->fh = fh;
+ TAILQ_INIT(&priv->els);
+ TAILQ_INSERT_TAIL(&priv->els, root, next);
root->el = json_object();
if (root->el == NULL)
fatalx("lldpctl", "cannot create JSON root object");
TAILQ_ENTRY(json_element) next;
json_object *el;
};
-TAILQ_HEAD(json_writer_private, json_element);
+TAILQ_HEAD(json_element_list, json_element);
+struct json_writer_private {
+ FILE *fh;
+ struct json_element_list els;
+};
static void
jsonc_start(struct writer *w, const char *tag, const char *descr)
{
struct json_writer_private *p = w->priv;
- struct json_element *current = TAILQ_LAST(p, json_writer_private);
+ struct json_element *current = TAILQ_LAST(&p->els, json_element_list);
struct json_element *new;
json_object *exist = NULL;
if (new == NULL) fatal(NULL, NULL);
new->el = json_object_new_object();
json_object_array_add(exist, new->el);
- TAILQ_INSERT_TAIL(p, new, next);
+ TAILQ_INSERT_TAIL(&p->els, new, next);
}
static void
const char *descr, const char *value)
{
struct json_writer_private *p = w->priv;
- struct json_element *current = TAILQ_LAST(p, json_writer_private);
+ struct json_element *current = TAILQ_LAST(&p->els, json_element_list);
json_object *jvalue;
if (value && (!strcmp(value, "yes") || !strcmp(value, "on")))
jvalue = json_object_new_boolean(1);
jsonc_data(struct writer *w, const char *data)
{
struct json_writer_private *p = w->priv;
- struct json_element *current = TAILQ_LAST(p, json_writer_private);
+ struct json_element *current = TAILQ_LAST(&p->els, json_element_list);
json_object_object_add(current->el, "value",
json_object_new_string(data?data:""));
}
jsonc_end(struct writer *w)
{
struct json_writer_private *p = w->priv;
- struct json_element *current = TAILQ_LAST(p, json_writer_private);
+ struct json_element *current = TAILQ_LAST(&p->els, json_element_list);
if (current == NULL) {
log_warnx("lldpctl", "unbalanced tags");
return;
}
- TAILQ_REMOVE(p, current, next);
+ TAILQ_REMOVE(&p->els, current, next);
free(current);
}
jsonc_finish(struct writer *w)
{
struct json_writer_private *p = w->priv;
- if (TAILQ_EMPTY(p)) {
+ if (TAILQ_EMPTY(&p->els)) {
log_warnx("lldpctl", "nothing to output");
- } else if (TAILQ_NEXT(TAILQ_FIRST(p), next) != NULL) {
+ } else if (TAILQ_NEXT(TAILQ_FIRST(&p->els), next) != NULL) {
log_warnx("lldpctl", "unbalanced tags");
/* memory will leak... */
} else {
- struct json_element *first = TAILQ_FIRST(p);
+ struct json_element *first = TAILQ_FIRST(&p->els);
int json_flags = (JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED);
- fprintf(stdout, "%s", json_object_to_json_string_ext(first->el, json_flags));
+ const char *s = json_object_to_json_string_ext(first->el, json_flags);
+ fprintf(p->fh, "%s", s?s:"{}");
json_object_put(first->el);
- TAILQ_REMOVE(p, first, next);
+ TAILQ_REMOVE(&p->els, first, next);
free(first);
}
- fprintf(stdout, "\n");
+ fprintf(p->fh, "\n");
free(p);
free(w);
}
priv = malloc(sizeof(*priv));
root = malloc(sizeof(*root));
if (priv == NULL || root == NULL) fatal(NULL, NULL);
- TAILQ_INIT(priv);
- TAILQ_INSERT_TAIL(priv, root, next);
+
+ priv->fh = fh;
+ TAILQ_INIT(&priv->els);
+ TAILQ_INSERT_TAIL(&priv->els, root, next);
root->el = json_object_new_object();
if (root->el == NULL)
fatalx("lldpctl", "cannot create JSON root object");