]> git.ipfire.org Git - thirdparty/lldpd.git/blob - src/client/text_writer.c
client: add show interfaces command (#240)
[thirdparty/lldpd.git] / src / client / text_writer.c
1 /* -*- mode: c; c-file-style: "openbsd" -*- */
2 /*
3 * Copyright (c) 2010 Andreas Hofmeister <andi@collax.com>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "writer.h"
23 #include "../log.h"
24
25 static char sep[] = "-------------------------------------------------------------------------------";
26
27 struct txt_writer_private {
28 FILE * fh;
29 int level;
30 int attrs;
31 };
32
33 static void
34 txt_start(struct writer *w , const char *tag, const char *descr) {
35 struct txt_writer_private *p = w->priv;
36 int i = 0;
37 char buf[128];
38
39 if (p->level == 0) {
40 fprintf(p->fh, "%s\n", sep);
41 } else {
42 fprintf(p->fh, "\n");
43 }
44
45 for (i = 1; i < p->level; i++) {
46 fprintf(p->fh, " ");
47 }
48
49 snprintf(buf, sizeof(buf), "%s:", descr);
50 fprintf(p->fh, "%-13s", buf);
51
52 if (p->level == 0)
53 fprintf(p->fh, "\n%s", sep);
54
55 p->level++;
56 p->attrs = 0;
57 }
58
59 static void
60 txt_attr(struct writer *w, const char *tag, const char *descr, const char *value) {
61 struct txt_writer_private *p = w->priv;
62
63 if (descr == NULL || strlen(descr) == 0) {
64 fprintf(p->fh, "%s%s", (p->attrs > 0 ? ", " : " "), value?value:"(none)");
65 } else {
66 fprintf(p->fh, "%s%s: %s", (p->attrs > 0 ? ", " : " "), descr, value?value:"(none)");
67 }
68
69 p->attrs++;
70 }
71
72 static void
73 txt_data(struct writer *w, const char *data) {
74 struct txt_writer_private *p = w->priv;
75 char *nl, *begin;
76 char *v = begin = data?strdup(data):NULL;
77
78 if (v == NULL) {
79 fprintf(p->fh, " %s", data?data:"(none)");
80 return;
81 }
82
83 fprintf(p->fh, " ");
84 while ((nl = strchr(v, '\n')) != NULL) {
85 *nl = '\0';
86 fprintf(p->fh, "%s\n", v);
87 v = nl + 1;
88
89 /* Indent */
90 int i;
91 for (i = 1; i < p->level - 1; i++) {
92 fprintf(p->fh, " ");
93 }
94 fprintf(p->fh, "%-14s", " ");
95 }
96 fprintf(p->fh, "%s", v);
97 free(begin);
98 }
99
100 static void
101 txt_end(struct writer *w) {
102 struct txt_writer_private *p = w->priv;
103 p->level--;
104
105 if (p->level == 1) {
106 fprintf(p->fh, "\n%s", sep);
107 fflush(p->fh);
108 }
109 }
110
111 static void
112 txt_finish(struct writer *w) {
113 struct txt_writer_private *p = w->priv;
114
115 fprintf(p->fh, "\n");
116
117 free(w->priv);
118 w->priv = NULL;
119
120 free(w);
121 }
122
123 struct writer*
124 txt_init(FILE* fh) {
125
126 struct writer *result;
127 struct txt_writer_private *priv;
128
129 priv = malloc(sizeof(*priv));
130 if (!priv) {
131 fatalx("lldpctl", "out of memory");
132 return NULL;
133 }
134
135 priv->fh = fh;
136 priv->level = 0;
137 priv->attrs = 0;
138
139 result = malloc(sizeof(struct writer));
140 if (!result) {
141 fatalx("llpctl", "out of memory");
142 free(priv);
143 return NULL;
144 }
145
146 result->priv = priv;
147 result->start = txt_start;
148 result->attr = txt_attr;
149 result->data = txt_data;
150 result->end = txt_end;
151 result->finish= txt_finish;
152
153 return result;
154 }