2 * Copyright (c) 2003 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The names of the authors may not be used to endorse or promote
14 * products derived from this software without specific prior written
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 #include <sys/queue.h>
44 #include <sys/sysctl.h>
53 struct gprovider *provider;
54 struct gconsumer *consumer;
56 struct sbuf *sbuf[20];
62 StartElement(void *userData, const char *name, const char **attr)
71 mt->sbuf[mt->level] = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
74 for (i = 0; attr[i] != NULL; i += 2) {
75 if (!strcmp(attr[i], "id")) {
76 id = (void *)strtoul(attr[i + 1], NULL, 0);
78 } else if (!strcmp(attr[i], "ref")) {
79 ref = (void *)strtoul(attr[i + 1], NULL, 0);
81 printf("%*.*s[%s = %s]\n",
82 mt->level + 1, mt->level + 1, "",
83 attr[i], attr[i + 1]);
85 if (!strcmp(name, "class") && mt->class == NULL) {
86 mt->class = calloc(1, sizeof *mt->class);
87 mt->class->lg_id = id;
88 LIST_INSERT_HEAD(&mt->mesh->lg_class, mt->class, lg_class);
89 LIST_INIT(&mt->class->lg_geom);
90 LIST_INIT(&mt->class->lg_config);
93 if (!strcmp(name, "geom") && mt->geom == NULL) {
94 mt->geom = calloc(1, sizeof *mt->geom);
96 LIST_INSERT_HEAD(&mt->class->lg_geom, mt->geom, lg_geom);
97 LIST_INIT(&mt->geom->lg_provider);
98 LIST_INIT(&mt->geom->lg_consumer);
99 LIST_INIT(&mt->geom->lg_config);
102 if (!strcmp(name, "class") && mt->geom != NULL) {
103 mt->geom->lg_class = ref;
106 if (!strcmp(name, "consumer") && mt->consumer == NULL) {
107 mt->consumer = calloc(1, sizeof *mt->consumer);
108 mt->consumer->lg_id = id;
109 LIST_INSERT_HEAD(&mt->geom->lg_consumer, mt->consumer,
111 LIST_INIT(&mt->consumer->lg_config);
114 if (!strcmp(name, "geom") && mt->consumer != NULL) {
115 mt->consumer->lg_geom = ref;
118 if (!strcmp(name, "provider") && mt->consumer != NULL) {
119 mt->consumer->lg_provider = ref;
122 if (!strcmp(name, "provider") && mt->provider == NULL) {
123 mt->provider = calloc(1, sizeof *mt->provider);
124 mt->provider->lg_id = id;
125 LIST_INSERT_HEAD(&mt->geom->lg_provider, mt->provider,
127 LIST_INIT(&mt->provider->lg_consumers);
128 LIST_INIT(&mt->provider->lg_config);
131 if (!strcmp(name, "geom") && mt->provider != NULL) {
132 mt->provider->lg_geom = ref;
135 if (!strcmp(name, "config")) {
136 if (mt->provider != NULL) {
137 mt->config = &mt->provider->lg_config;
140 if (mt->consumer != NULL) {
141 mt->config = &mt->consumer->lg_config;
144 if (mt->geom != NULL) {
145 mt->config = &mt->geom->lg_config;
148 if (mt->class != NULL) {
149 mt->config = &mt->class->lg_config;
156 EndElement(void *userData, const char *name)
163 sbuf_finish(mt->sbuf[mt->level]);
164 p = strdup(sbuf_data(mt->sbuf[mt->level]));
165 sbuf_delete(mt->sbuf[mt->level]);
166 mt->sbuf[mt->level] = NULL;
168 if (strlen(p) == 0) {
173 if (!strcmp(name, "name")) {
174 if (mt->provider != NULL) {
175 mt->provider->lg_name = p;
177 } else if (mt->geom != NULL) {
178 mt->geom->lg_name = p;
180 } else if (mt->class != NULL) {
181 mt->class->lg_name = p;
185 if (!strcmp(name, "rank") && mt->geom != NULL) {
186 mt->geom->lg_rank = strtoul(p, NULL, 0);
190 if (!strcmp(name, "mode") && mt->provider != NULL) {
191 mt->provider->lg_mode = p;
194 if (!strcmp(name, "mode") && mt->consumer != NULL) {
195 mt->consumer->lg_mode = p;
198 if (!strcmp(name, "mediasize") && mt->provider != NULL) {
199 mt->provider->lg_mediasize = strtoumax(p, NULL, 0);
203 if (!strcmp(name, "sectorsize") && mt->provider != NULL) {
204 mt->provider->lg_sectorsize = strtoul(p, NULL, 0);
209 if (!strcmp(name, "config")) {
214 if (mt->config != NULL) {
215 gc = calloc(sizeof *gc, 1);
216 gc->lg_name = strdup(name);
218 LIST_INSERT_HEAD(mt->config, gc, lg_config);
223 printf("Unexpected XML: name=%s data=\"%s\"\n", name, p);
227 if (!strcmp(name, "consumer") && mt->consumer != NULL) {
231 if (!strcmp(name, "provider") && mt->provider != NULL) {
235 if (!strcmp(name, "geom") && mt->consumer != NULL) {
238 if (!strcmp(name, "geom") && mt->provider != NULL) {
241 if (!strcmp(name, "geom") && mt->geom != NULL) {
245 if (!strcmp(name, "class") && mt->geom != NULL) {
248 if (!strcmp(name, "class") && mt->class != NULL) {
255 CharData(void *userData , const XML_Char *s , int len)
264 while (isspace(*b) && b < e)
266 while (isspace(*e) && e > b)
268 if (e != b || (*b && !isspace(*b)))
269 sbuf_bcat(mt->sbuf[mt->level], b, e - b + 1);
273 geom_lookupid(struct gmesh *gmp, const void *id)
277 for (gip = gmp->lg_ident; gip->lg_id != NULL; gip++)
278 if (gip->lg_id == id)
284 geom_xml2tree(struct gmesh *gmp, char *p)
290 struct gprovider *pr;
291 struct gconsumer *co;
294 memset(gmp, 0, sizeof *gmp);
295 LIST_INIT(&gmp->lg_class);
296 parser = XML_ParserCreate(NULL);
297 mt = calloc(1, sizeof *mt);
301 XML_SetUserData(parser, mt);
302 XML_SetElementHandler(parser, StartElement, EndElement);
303 XML_SetCharacterDataHandler(parser, CharData);
304 i = XML_Parse(parser, p, strlen(p), 1);
307 XML_ParserFree(parser);
308 gmp->lg_ident = calloc(sizeof *gmp->lg_ident, mt->nident + 1);
309 if (gmp->lg_ident == NULL)
313 /* Collect all identifiers */
314 LIST_FOREACH(cl, &gmp->lg_class, lg_class) {
315 gmp->lg_ident[i].lg_id = cl->lg_id;
316 gmp->lg_ident[i].lg_ptr = cl;
317 gmp->lg_ident[i].lg_what = ISCLASS;
319 LIST_FOREACH(ge, &cl->lg_geom, lg_geom) {
320 gmp->lg_ident[i].lg_id = ge->lg_id;
321 gmp->lg_ident[i].lg_ptr = ge;
322 gmp->lg_ident[i].lg_what = ISGEOM;
324 LIST_FOREACH(pr, &ge->lg_provider, lg_provider) {
325 gmp->lg_ident[i].lg_id = pr->lg_id;
326 gmp->lg_ident[i].lg_ptr = pr;
327 gmp->lg_ident[i].lg_what = ISPROVIDER;
330 LIST_FOREACH(co, &ge->lg_consumer, lg_consumer) {
331 gmp->lg_ident[i].lg_id = co->lg_id;
332 gmp->lg_ident[i].lg_ptr = co;
333 gmp->lg_ident[i].lg_what = ISCONSUMER;
338 /* Substitute all identifiers */
339 LIST_FOREACH(cl, &gmp->lg_class, lg_class) {
340 LIST_FOREACH(ge, &cl->lg_geom, lg_geom) {
342 geom_lookupid(gmp, ge->lg_class)->lg_ptr;
343 LIST_FOREACH(pr, &ge->lg_provider, lg_provider) {
345 geom_lookupid(gmp, pr->lg_geom)->lg_ptr;
347 LIST_FOREACH(co, &ge->lg_consumer, lg_consumer) {
349 geom_lookupid(gmp, co->lg_geom)->lg_ptr;
350 if (co->lg_provider != NULL) {
353 co->lg_provider)->lg_ptr;
355 &co->lg_provider->lg_consumers,
365 geom_gettree(struct gmesh *gmp)
371 error = geom_xml2tree(gmp, p);
377 delete_config(struct gconf *gp)
385 LIST_REMOVE(cf, lg_config);
393 geom_deletetree(struct gmesh *gmp)
397 struct gprovider *pr;
398 struct gconsumer *co;
401 gmp->lg_ident = NULL;
403 cl = LIST_FIRST(&gmp->lg_class);
406 LIST_REMOVE(cl, lg_class);
407 delete_config(&cl->lg_config);
408 if (cl->lg_name) free(cl->lg_name);
410 ge = LIST_FIRST(&cl->lg_geom);
413 LIST_REMOVE(ge, lg_geom);
414 delete_config(&ge->lg_config);
415 if (ge->lg_name) free(ge->lg_name);
417 pr = LIST_FIRST(&ge->lg_provider);
420 LIST_REMOVE(pr, lg_provider);
421 delete_config(&pr->lg_config);
422 if (pr->lg_name) free(pr->lg_name);
423 if (pr->lg_mode) free(pr->lg_mode);
427 co = LIST_FIRST(&ge->lg_consumer);
430 LIST_REMOVE(co, lg_consumer);
431 delete_config(&co->lg_config);
432 if (co->lg_mode) free(co->lg_mode);