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];
64 StartElement(void *userData, const char *name, const char **attr)
73 mt->sbuf[mt->level] = sbuf_new_auto();
76 for (i = 0; attr[i] != NULL; i += 2) {
77 if (!strcmp(attr[i], "id")) {
78 id = (void *)strtoul(attr[i + 1], NULL, 0);
80 } else if (!strcmp(attr[i], "ref")) {
81 ref = (void *)strtoul(attr[i + 1], NULL, 0);
83 printf("%*.*s[%s = %s]\n",
84 mt->level + 1, mt->level + 1, "",
85 attr[i], attr[i + 1]);
87 if (!strcmp(name, "class") && mt->class == NULL) {
88 mt->class = calloc(1, sizeof *mt->class);
89 if (mt->class == NULL) {
91 XML_StopParser(mt->parser, 0);
92 warn("Cannot allocate memory during processing of '%s' "
96 mt->class->lg_id = id;
97 LIST_INSERT_HEAD(&mt->mesh->lg_class, mt->class, lg_class);
98 LIST_INIT(&mt->class->lg_geom);
99 LIST_INIT(&mt->class->lg_config);
102 if (!strcmp(name, "geom") && mt->geom == NULL) {
103 mt->geom = calloc(1, sizeof *mt->geom);
104 if (mt->geom == NULL) {
106 XML_StopParser(mt->parser, 0);
107 warn("Cannot allocate memory during processing of '%s' "
111 mt->geom->lg_id = id;
112 LIST_INSERT_HEAD(&mt->class->lg_geom, mt->geom, lg_geom);
113 LIST_INIT(&mt->geom->lg_provider);
114 LIST_INIT(&mt->geom->lg_consumer);
115 LIST_INIT(&mt->geom->lg_config);
118 if (!strcmp(name, "class") && mt->geom != NULL) {
119 mt->geom->lg_class = ref;
122 if (!strcmp(name, "consumer") && mt->consumer == NULL) {
123 mt->consumer = calloc(1, sizeof *mt->consumer);
124 if (mt->consumer == NULL) {
126 XML_StopParser(mt->parser, 0);
127 warn("Cannot allocate memory during processing of '%s' "
131 mt->consumer->lg_id = id;
132 LIST_INSERT_HEAD(&mt->geom->lg_consumer, mt->consumer,
134 LIST_INIT(&mt->consumer->lg_config);
137 if (!strcmp(name, "geom") && mt->consumer != NULL) {
138 mt->consumer->lg_geom = ref;
141 if (!strcmp(name, "provider") && mt->consumer != NULL) {
142 mt->consumer->lg_provider = ref;
145 if (!strcmp(name, "provider") && mt->provider == NULL) {
146 mt->provider = calloc(1, sizeof *mt->provider);
147 if (mt->provider == NULL) {
149 XML_StopParser(mt->parser, 0);
150 warn("Cannot allocate memory during processing of '%s' "
154 mt->provider->lg_id = id;
155 LIST_INSERT_HEAD(&mt->geom->lg_provider, mt->provider,
157 LIST_INIT(&mt->provider->lg_consumers);
158 LIST_INIT(&mt->provider->lg_config);
161 if (!strcmp(name, "geom") && mt->provider != NULL) {
162 mt->provider->lg_geom = ref;
165 if (!strcmp(name, "config")) {
166 if (mt->provider != NULL) {
167 mt->config = &mt->provider->lg_config;
170 if (mt->consumer != NULL) {
171 mt->config = &mt->consumer->lg_config;
174 if (mt->geom != NULL) {
175 mt->config = &mt->geom->lg_config;
178 if (mt->class != NULL) {
179 mt->config = &mt->class->lg_config;
186 EndElement(void *userData, const char *name)
194 if (sbuf_finish(mt->sbuf[mt->level]) == 0)
195 p = strdup(sbuf_data(mt->sbuf[mt->level]));
196 sbuf_delete(mt->sbuf[mt->level]);
197 mt->sbuf[mt->level] = NULL;
201 XML_StopParser(mt->parser, 0);
202 warn("Cannot allocate memory during processing of '%s' "
206 if (strlen(p) == 0) {
211 if (!strcmp(name, "name")) {
212 if (mt->provider != NULL) {
213 mt->provider->lg_name = p;
215 } else if (mt->geom != NULL) {
216 mt->geom->lg_name = p;
218 } else if (mt->class != NULL) {
219 mt->class->lg_name = p;
223 if (!strcmp(name, "rank") && mt->geom != NULL) {
224 mt->geom->lg_rank = strtoul(p, NULL, 0);
228 if (!strcmp(name, "mode") && mt->provider != NULL) {
229 mt->provider->lg_mode = p;
232 if (!strcmp(name, "mode") && mt->consumer != NULL) {
233 mt->consumer->lg_mode = p;
236 if (!strcmp(name, "mediasize") && mt->provider != NULL) {
237 mt->provider->lg_mediasize = strtoumax(p, NULL, 0);
241 if (!strcmp(name, "sectorsize") && mt->provider != NULL) {
242 mt->provider->lg_sectorsize = strtoul(p, NULL, 0);
246 if (!strcmp(name, "stripesize") && mt->provider != NULL) {
247 mt->provider->lg_stripesize = strtoumax(p, NULL, 0);
251 if (!strcmp(name, "stripeoffset") && mt->provider != NULL) {
252 mt->provider->lg_stripeoffset = strtoumax(p, NULL, 0);
257 if (!strcmp(name, "config")) {
262 if (mt->config != NULL) {
263 gc = calloc(1, sizeof *gc);
266 XML_StopParser(mt->parser, 0);
267 warn("Cannot allocate memory during processing of '%s' "
271 gc->lg_name = strdup(name);
272 if (gc->lg_name == NULL) {
274 XML_StopParser(mt->parser, 0);
275 warn("Cannot allocate memory during processing of '%s' "
280 LIST_INSERT_HEAD(mt->config, gc, lg_config);
285 printf("Unexpected XML: name=%s data=\"%s\"\n", name, p);
289 if (!strcmp(name, "consumer") && mt->consumer != NULL) {
293 if (!strcmp(name, "provider") && mt->provider != NULL) {
297 if (!strcmp(name, "geom") && mt->consumer != NULL) {
300 if (!strcmp(name, "geom") && mt->provider != NULL) {
303 if (!strcmp(name, "geom") && mt->geom != NULL) {
307 if (!strcmp(name, "class") && mt->geom != NULL) {
310 if (!strcmp(name, "class") && mt->class != NULL) {
317 CharData(void *userData , const XML_Char *s , int len)
326 while (isspace(*b) && b < e)
328 while (isspace(*e) && e > b)
330 if (e != b || (*b && !isspace(*b)))
331 sbuf_bcat(mt->sbuf[mt->level], b, e - b + 1);
335 geom_lookupid(struct gmesh *gmp, const void *id)
339 for (gip = gmp->lg_ident; gip->lg_id != NULL; gip++)
340 if (gip->lg_id == id)
346 geom_xml2tree(struct gmesh *gmp, char *p)
352 struct gprovider *pr;
353 struct gconsumer *co;
356 memset(gmp, 0, sizeof *gmp);
357 LIST_INIT(&gmp->lg_class);
358 parser = XML_ParserCreate(NULL);
361 mt = calloc(1, sizeof *mt);
363 XML_ParserFree(parser);
369 XML_SetUserData(parser, mt);
370 XML_SetElementHandler(parser, StartElement, EndElement);
371 XML_SetCharacterDataHandler(parser, CharData);
372 i = XML_Parse(parser, p, strlen(p), 1);
376 error = XML_GetErrorCode(parser) == XML_ERROR_NO_MEMORY ?
379 XML_ParserFree(parser);
384 gmp->lg_ident = calloc(sizeof *gmp->lg_ident, mt->nident + 1);
386 if (gmp->lg_ident == NULL)
389 /* Collect all identifiers */
390 LIST_FOREACH(cl, &gmp->lg_class, lg_class) {
391 gmp->lg_ident[i].lg_id = cl->lg_id;
392 gmp->lg_ident[i].lg_ptr = cl;
393 gmp->lg_ident[i].lg_what = ISCLASS;
395 LIST_FOREACH(ge, &cl->lg_geom, lg_geom) {
396 gmp->lg_ident[i].lg_id = ge->lg_id;
397 gmp->lg_ident[i].lg_ptr = ge;
398 gmp->lg_ident[i].lg_what = ISGEOM;
400 LIST_FOREACH(pr, &ge->lg_provider, lg_provider) {
401 gmp->lg_ident[i].lg_id = pr->lg_id;
402 gmp->lg_ident[i].lg_ptr = pr;
403 gmp->lg_ident[i].lg_what = ISPROVIDER;
406 LIST_FOREACH(co, &ge->lg_consumer, lg_consumer) {
407 gmp->lg_ident[i].lg_id = co->lg_id;
408 gmp->lg_ident[i].lg_ptr = co;
409 gmp->lg_ident[i].lg_what = ISCONSUMER;
414 /* Substitute all identifiers */
415 LIST_FOREACH(cl, &gmp->lg_class, lg_class) {
416 LIST_FOREACH(ge, &cl->lg_geom, lg_geom) {
418 geom_lookupid(gmp, ge->lg_class)->lg_ptr;
419 LIST_FOREACH(pr, &ge->lg_provider, lg_provider) {
421 geom_lookupid(gmp, pr->lg_geom)->lg_ptr;
423 LIST_FOREACH(co, &ge->lg_consumer, lg_consumer) {
425 geom_lookupid(gmp, co->lg_geom)->lg_ptr;
426 if (co->lg_provider != NULL) {
429 co->lg_provider)->lg_ptr;
431 &co->lg_provider->lg_consumers,
441 geom_gettree(struct gmesh *gmp)
449 error = geom_xml2tree(gmp, p);
455 delete_config(struct gconf *gp)
463 LIST_REMOVE(cf, lg_config);
471 geom_deletetree(struct gmesh *gmp)
475 struct gprovider *pr;
476 struct gconsumer *co;
479 gmp->lg_ident = NULL;
481 cl = LIST_FIRST(&gmp->lg_class);
484 LIST_REMOVE(cl, lg_class);
485 delete_config(&cl->lg_config);
486 if (cl->lg_name) free(cl->lg_name);
488 ge = LIST_FIRST(&cl->lg_geom);
491 LIST_REMOVE(ge, lg_geom);
492 delete_config(&ge->lg_config);
493 if (ge->lg_name) free(ge->lg_name);
495 pr = LIST_FIRST(&ge->lg_provider);
498 LIST_REMOVE(pr, lg_provider);
499 delete_config(&pr->lg_config);
500 if (pr->lg_name) free(pr->lg_name);
501 if (pr->lg_mode) free(pr->lg_mode);
505 co = LIST_FIRST(&ge->lg_consumer);
508 LIST_REMOVE(co, lg_consumer);
509 delete_config(&co->lg_config);
510 if (co->lg_mode) free(co->lg_mode);