Imported Upstream version 1.12.0
[platform/upstream/augeas.git] / src / xml.c
1 /*
2  * xml.c: the implementation of aug_to_xml and supporting functions
3  *
4  * Copyright (C) 2017 David Lutterkort
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
19  *
20  * Author: David Lutterkort <lutter@watzmann.net>
21  */
22
23 #include <config.h>
24 #include "augeas.h"
25 #include "internal.h"
26 #include "memory.h"
27 #include "info.h"
28 #include "errcode.h"
29
30 #include <libxml/tree.h>
31
32 static int to_xml_span(xmlNodePtr elem, const char *pfor, int start, int end) {
33     int r;
34     char *buf;
35     xmlAttrPtr prop;
36     xmlNodePtr span_elem;
37
38     span_elem = xmlNewChild(elem, NULL, BAD_CAST "span", NULL);
39     if (span_elem == NULL)
40         return -1;
41
42     prop = xmlSetProp(span_elem, BAD_CAST "for", BAD_CAST pfor);
43     if (prop == NULL)
44         return -1;
45
46     /* Format and set the start property */
47     r = xasprintf(&buf, "%d", start);
48     if (r < 0)
49         return -1;
50
51     prop = xmlSetProp(span_elem, BAD_CAST "start", BAD_CAST buf);
52     FREE(buf);
53     if (prop == NULL)
54         return -1;
55
56     /* Format and set the end property */
57     r = xasprintf(&buf, "%d", end);
58     if (r < 0)
59         return -1;
60
61     prop = xmlSetProp(span_elem, BAD_CAST "end", BAD_CAST buf);
62     FREE(buf);
63     if (prop == NULL)
64         return -1;
65
66     return 0;
67 }
68
69 static int to_xml_one(xmlNodePtr elem, const struct tree *tree,
70                       const char *pathin) {
71     xmlNodePtr value;
72     xmlAttrPtr prop;
73     int r;
74
75     prop = xmlSetProp(elem, BAD_CAST "label", BAD_CAST tree->label);
76     if (prop == NULL)
77         goto error;
78
79     if (tree->span) {
80         struct span *span = tree->span;
81
82         prop = xmlSetProp(elem, BAD_CAST "file",
83                           BAD_CAST span->filename->str);
84         if (prop == NULL)
85             goto error;
86
87         r = to_xml_span(elem, "label", span->label_start, span->label_end);
88         if (r < 0)
89             goto error;
90
91         r = to_xml_span(elem, "value", span->value_start, span->value_end);
92         if (r < 0)
93             goto error;
94
95         r = to_xml_span(elem, "node", span->span_start, span->span_end);
96         if (r < 0)
97             goto error;
98     }
99
100     if (pathin != NULL) {
101         prop = xmlSetProp(elem, BAD_CAST "path", BAD_CAST pathin);
102         if (prop == NULL)
103             goto error;
104     }
105     if (tree->value != NULL) {
106         value = xmlNewTextChild(elem, NULL, BAD_CAST "value",
107                                 BAD_CAST tree->value);
108         if (value == NULL)
109             goto error;
110     }
111     return 0;
112  error:
113     return -1;
114 }
115
116 static int to_xml_rec(xmlNodePtr pnode, struct tree *start,
117                       const char *pathin) {
118     int r;
119     xmlNodePtr elem;
120
121     elem = xmlNewChild(pnode, NULL, BAD_CAST "node", NULL);
122     if (elem == NULL)
123         goto error;
124     r = to_xml_one(elem, start, pathin);
125     if (r < 0)
126         goto error;
127
128     list_for_each(tree, start->children) {
129         if (TREE_HIDDEN(tree))
130             continue;
131         r = to_xml_rec(elem, tree, NULL);
132         if (r < 0)
133             goto error;
134     }
135
136     return 0;
137  error:
138     return -1;
139 }
140
141 static int tree_to_xml(struct pathx *p, xmlNode **xml, const char *pathin) {
142     char *path = NULL;
143     struct tree *tree;
144     xmlAttrPtr expr;
145     int r;
146
147     *xml = xmlNewNode(NULL, BAD_CAST "augeas");
148     if (*xml == NULL)
149         goto error;
150     expr = xmlSetProp(*xml, BAD_CAST "match", BAD_CAST pathin);
151     if (expr == NULL)
152         goto error;
153
154     for (tree = pathx_first(p); tree != NULL; tree = pathx_next(p)) {
155         if (TREE_HIDDEN(tree))
156             continue;
157         path = path_of_tree(tree);
158         if (path == NULL)
159             goto error;
160         r = to_xml_rec(*xml, tree, path);
161         if (r < 0)
162             goto error;
163         FREE(path);
164     }
165     return 0;
166  error:
167     free(path);
168     xmlFree(*xml);
169     *xml = NULL;
170     return -1;
171 }
172
173 int aug_to_xml(const struct augeas *aug, const char *pathin,
174                xmlNode **xmldoc, unsigned int flags) {
175     struct pathx *p = NULL;
176     int result = -1;
177
178     api_entry(aug);
179
180     ARG_CHECK(flags != 0, aug, "aug_to_xml: FLAGS must be 0");
181     ARG_CHECK(xmldoc == NULL, aug, "aug_to_xml: XMLDOC must be non-NULL");
182
183     *xmldoc = NULL;
184
185     if (pathin == NULL || strlen(pathin) == 0 || strcmp(pathin, "/") == 0) {
186         pathin = "/*";
187     }
188
189     p = pathx_aug_parse(aug, aug->origin, tree_root_ctx(aug), pathin, true);
190     ERR_BAIL(aug);
191     result = tree_to_xml(p, xmldoc, pathin);
192     ERR_THROW(result < 0, aug, AUG_ENOMEM, NULL);
193 error:
194     free_pathx(p);
195     api_exit(aug);
196
197     return result;
198 }