]>
git.ipfire.org Git - thirdparty/lldpd.git/blob - src/client/text_writer.c
1 /* -*- mode: c; c-file-style: "openbsd" -*- */
3 * Copyright (c) 2010 Andreas Hofmeister <andi@collax.com>
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.
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.
25 static char sep
[] = "-------------------------------------------------------------------------------";
27 struct txt_writer_private
{
34 txt_start(struct writer
*w
, const char *tag
, const char *descr
) {
35 struct txt_writer_private
*p
= w
->priv
;
40 fprintf(p
->fh
, "%s\n", sep
);
45 for (i
= 1; i
< p
->level
; i
++) {
49 snprintf(buf
, sizeof(buf
), "%s:", descr
);
50 fprintf(p
->fh
, "%-13s", buf
);
53 fprintf(p
->fh
, "\n%s", sep
);
60 txt_attr(struct writer
*w
, const char *tag
, const char *descr
, const char *value
) {
61 struct txt_writer_private
*p
= w
->priv
;
63 if (descr
== NULL
|| strlen(descr
) == 0) {
64 fprintf(p
->fh
, "%s%s", (p
->attrs
> 0 ? ", " : " "), value
?value
:"(none)");
66 fprintf(p
->fh
, "%s%s: %s", (p
->attrs
> 0 ? ", " : " "), descr
, value
?value
:"(none)");
73 txt_data(struct writer
*w
, const char *data
) {
74 struct txt_writer_private
*p
= w
->priv
;
76 char *v
= begin
= data
?strdup(data
):NULL
;
79 fprintf(p
->fh
, " %s", data
?data
:"(none)");
84 while ((nl
= strchr(v
, '\n')) != NULL
) {
86 fprintf(p
->fh
, "%s\n", v
);
91 for (i
= 1; i
< p
->level
- 1; i
++) {
94 fprintf(p
->fh
, "%-14s", " ");
96 fprintf(p
->fh
, "%s", v
);
101 txt_end(struct writer
*w
) {
102 struct txt_writer_private
*p
= w
->priv
;
106 fprintf(p
->fh
, "\n%s", sep
);
112 txt_finish(struct writer
*w
) {
113 struct txt_writer_private
*p
= w
->priv
;
115 fprintf(p
->fh
, "\n");
126 struct writer
*result
;
127 struct txt_writer_private
*priv
;
129 priv
= malloc(sizeof(*priv
));
131 fatalx("lldpctl", "out of memory");
139 result
= malloc(sizeof(struct writer
));
141 fatalx("llpctl", "out of memory");
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
;