2 * SPDX-License-Identifier: BSD-3-Clause-FreeBSD
4 * Copyright (c) 2003 Poul-Henning Kamp
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the authors may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 #include <sys/queue.h>
47 #include <sys/sysctl.h>
56 struct gprovider *provider;
57 struct gconsumer *consumer;
59 struct sbuf *sbuf[20];
67 StartElement(void *userData, const char *name, const char **attr)
76 mt->sbuf[mt->level] = sbuf_new_auto();
79 for (i = 0; attr[i] != NULL; i += 2) {
80 if (!strcmp(attr[i], "id")) {
81 id = (void *)strtoul(attr[i + 1], NULL, 0);
83 } else if (!strcmp(attr[i], "ref")) {
84 ref = (void *)strtoul(attr[i + 1], NULL, 0);
86 printf("%*.*s[%s = %s]\n",
87 mt->level + 1, mt->level + 1, "",
88 attr[i], attr[i + 1]);
90 if (!strcmp(name, "class") && mt->class == NULL) {
91 mt->class = calloc(1, sizeof *mt->class);
92 if (mt->class == NULL) {
94 XML_StopParser(mt->parser, 0);
95 warn("Cannot allocate memory during processing of '%s' "
99 mt->class->lg_id = id;
100 LIST_INSERT_HEAD(&mt->mesh->lg_class, mt->class, lg_class);
101 LIST_INIT(&mt->class->lg_geom);
102 LIST_INIT(&mt->class->lg_config);
105 if (!strcmp(name, "geom") && mt->geom == NULL) {
106 mt->geom = calloc(1, sizeof *mt->geom);
107 if (mt->geom == NULL) {
109 XML_StopParser(mt->parser, 0);
110 warn("Cannot allocate memory during processing of '%s' "
114 mt->geom->lg_id = id;
115 LIST_INSERT_HEAD(&mt->class->lg_geom, mt->geom, lg_geom);
116 LIST_INIT(&mt->geom->lg_provider);
117 LIST_INIT(&mt->geom->lg_consumer);
118 LIST_INIT(&mt->geom->lg_config);
121 if (!strcmp(name, "class") && mt->geom != NULL) {
122 mt->geom->lg_class = ref;
125 if (!strcmp(name, "consumer") && mt->consumer == NULL) {
126 mt->consumer = calloc(1, sizeof *mt->consumer);
127 if (mt->consumer == NULL) {
129 XML_StopParser(mt->parser, 0);
130 warn("Cannot allocate memory during processing of '%s' "
134 mt->consumer->lg_id = id;
135 LIST_INSERT_HEAD(&mt->geom->lg_consumer, mt->consumer,
137 LIST_INIT(&mt->consumer->lg_config);
140 if (!strcmp(name, "geom") && mt->consumer != NULL) {
141 mt->consumer->lg_geom = ref;
144 if (!strcmp(name, "provider") && mt->consumer != NULL) {
145 mt->consumer->lg_provider = ref;
148 if (!strcmp(name, "provider") && mt->provider == NULL) {
149 mt->provider = calloc(1, sizeof *mt->provider);
150 if (mt->provider == NULL) {
152 XML_StopParser(mt->parser, 0);
153 warn("Cannot allocate memory during processing of '%s' "
157 mt->provider->lg_id = id;
158 LIST_INSERT_HEAD(&mt->geom->lg_provider, mt->provider,
160 LIST_INIT(&mt->provider->lg_consumers);
161 LIST_INIT(&mt->provider->lg_config);
164 if (!strcmp(name, "geom") && mt->provider != NULL) {
165 mt->provider->lg_geom = ref;
168 if (!strcmp(name, "config")) {
169 if (mt->provider != NULL) {
170 mt->config = &mt->provider->lg_config;
173 if (mt->consumer != NULL) {
174 mt->config = &mt->consumer->lg_config;
177 if (mt->geom != NULL) {
178 mt->config = &mt->geom->lg_config;
181 if (mt->class != NULL) {
182 mt->config = &mt->class->lg_config;
189 EndElement(void *userData, const char *name)
198 if (sbuf_finish(mt->sbuf[mt->level]) == 0)
199 p = strdup(sbuf_data(mt->sbuf[mt->level]));
200 sbuf_delete(mt->sbuf[mt->level]);
201 mt->sbuf[mt->level] = NULL;
205 XML_StopParser(mt->parser, 0);
206 warn("Cannot allocate memory during processing of '%s' "
210 if (strlen(p) == 0) {
215 if (!strcmp(name, "name")) {
216 if (mt->provider != NULL) {
217 mt->provider->lg_name = p;
219 } else if (mt->geom != NULL) {
220 mt->geom->lg_name = p;
222 } else if (mt->class != NULL) {
223 mt->class->lg_name = p;
227 if (!strcmp(name, "rank") && mt->geom != NULL) {
228 mt->geom->lg_rank = strtoul(p, NULL, 0);
232 if (!strcmp(name, "mode") && mt->provider != NULL) {
233 mt->provider->lg_mode = p;
236 if (!strcmp(name, "mode") && mt->consumer != NULL) {
237 mt->consumer->lg_mode = p;
240 if (!strcmp(name, "mediasize") && mt->provider != NULL) {
241 mt->provider->lg_mediasize = strtoumax(p, NULL, 0);
245 if (!strcmp(name, "sectorsize") && mt->provider != NULL) {
246 mt->provider->lg_sectorsize = strtoul(p, NULL, 0);
250 if (!strcmp(name, "stripesize") && mt->provider != NULL) {
251 mt->provider->lg_stripesize = strtoumax(p, NULL, 0);
255 if (!strcmp(name, "stripeoffset") && mt->provider != NULL) {
256 mt->provider->lg_stripeoffset = strtoumax(p, NULL, 0);
261 if (!strcmp(name, "config")) {
267 if (mt->config != NULL || (!strcmp(name, "wither") &&
268 (mt->provider != NULL || mt->geom != NULL))) {
269 if (mt->config != NULL)
271 else if (mt->provider != NULL)
272 c = &mt->provider->lg_config;
274 c = &mt->geom->lg_config;
275 gc = calloc(1, sizeof *gc);
278 XML_StopParser(mt->parser, 0);
279 warn("Cannot allocate memory during processing of '%s' "
284 gc->lg_name = strdup(name);
285 if (gc->lg_name == NULL) {
287 XML_StopParser(mt->parser, 0);
288 warn("Cannot allocate memory during processing of '%s' "
295 LIST_INSERT_HEAD(c, gc, lg_config);
300 #if DEBUG_LIBGEOM > 0
301 printf("Unexpected XML: name=%s data=\"%s\"\n", name, p);
306 if (!strcmp(name, "consumer") && mt->consumer != NULL) {
310 if (!strcmp(name, "provider") && mt->provider != NULL) {
314 if (!strcmp(name, "geom") && mt->consumer != NULL) {
317 if (!strcmp(name, "geom") && mt->provider != NULL) {
320 if (!strcmp(name, "geom") && mt->geom != NULL) {
324 if (!strcmp(name, "class") && mt->geom != NULL) {
327 if (!strcmp(name, "class") && mt->class != NULL) {
334 CharData(void *userData , const XML_Char *s , int len)
343 while (isspace(*b) && b < e)
345 while (isspace(*e) && e > b)
347 if (e != b || (*b && !isspace(*b)))
348 sbuf_bcat(mt->sbuf[mt->level], b, e - b + 1);
352 geom_lookupid(struct gmesh *gmp, const void *id)
356 for (gip = gmp->lg_ident; gip->lg_id != NULL; gip++)
357 if (gip->lg_id == id)
363 geom_lookupidptr(struct gmesh *gmp, const void *id)
367 gip = geom_lookupid(gmp, id);
369 return (gip->lg_ptr);
374 geom_xml2tree(struct gmesh *gmp, char *p)
380 struct gprovider *pr;
381 struct gconsumer *co;
384 memset(gmp, 0, sizeof *gmp);
385 LIST_INIT(&gmp->lg_class);
386 parser = XML_ParserCreate(NULL);
389 mt = calloc(1, sizeof *mt);
391 XML_ParserFree(parser);
397 XML_SetUserData(parser, mt);
398 XML_SetElementHandler(parser, StartElement, EndElement);
399 XML_SetCharacterDataHandler(parser, CharData);
400 i = XML_Parse(parser, p, strlen(p), 1);
404 error = XML_GetErrorCode(parser) == XML_ERROR_NO_MEMORY ?
407 XML_ParserFree(parser);
412 gmp->lg_ident = calloc(sizeof *gmp->lg_ident, mt->nident + 1);
414 if (gmp->lg_ident == NULL)
417 /* Collect all identifiers */
418 LIST_FOREACH(cl, &gmp->lg_class, lg_class) {
419 gmp->lg_ident[i].lg_id = cl->lg_id;
420 gmp->lg_ident[i].lg_ptr = cl;
421 gmp->lg_ident[i].lg_what = ISCLASS;
423 LIST_FOREACH(ge, &cl->lg_geom, lg_geom) {
424 gmp->lg_ident[i].lg_id = ge->lg_id;
425 gmp->lg_ident[i].lg_ptr = ge;
426 gmp->lg_ident[i].lg_what = ISGEOM;
428 LIST_FOREACH(pr, &ge->lg_provider, lg_provider) {
429 gmp->lg_ident[i].lg_id = pr->lg_id;
430 gmp->lg_ident[i].lg_ptr = pr;
431 gmp->lg_ident[i].lg_what = ISPROVIDER;
434 LIST_FOREACH(co, &ge->lg_consumer, lg_consumer) {
435 gmp->lg_ident[i].lg_id = co->lg_id;
436 gmp->lg_ident[i].lg_ptr = co;
437 gmp->lg_ident[i].lg_what = ISCONSUMER;
442 /* Substitute all identifiers */
443 LIST_FOREACH(cl, &gmp->lg_class, lg_class) {
444 LIST_FOREACH(ge, &cl->lg_geom, lg_geom) {
445 ge->lg_class = geom_lookupidptr(gmp, ge->lg_class);
446 LIST_FOREACH(pr, &ge->lg_provider, lg_provider)
447 pr->lg_geom = geom_lookupidptr(gmp, pr->lg_geom);
448 LIST_FOREACH(co, &ge->lg_consumer, lg_consumer) {
449 co->lg_geom = geom_lookupidptr(gmp, co->lg_geom);
450 if (co->lg_provider != NULL) {
451 co->lg_provider = geom_lookupidptr(gmp,
453 if (co->lg_provider != NULL) {
455 &co->lg_provider->lg_consumers,
466 geom_gettree(struct gmesh *gmp)
474 error = geom_xml2tree(gmp, p);
480 geom_gettree_geom(struct gmesh *gmp, const char *c, const char *g, int parents)
485 if (g != NULL && strncmp(g, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
486 g += sizeof(_PATH_DEV) - 1;
487 p = geom_getxml_geom(c, g, parents);
490 error = geom_xml2tree(gmp, p);
496 delete_config(struct gconf *gp)
504 LIST_REMOVE(cf, lg_config);
512 geom_deletetree(struct gmesh *gmp)
516 struct gprovider *pr;
517 struct gconsumer *co;
520 gmp->lg_ident = NULL;
522 cl = LIST_FIRST(&gmp->lg_class);
525 LIST_REMOVE(cl, lg_class);
526 delete_config(&cl->lg_config);
527 if (cl->lg_name) free(cl->lg_name);
529 ge = LIST_FIRST(&cl->lg_geom);
532 LIST_REMOVE(ge, lg_geom);
533 delete_config(&ge->lg_config);
534 if (ge->lg_name) free(ge->lg_name);
536 pr = LIST_FIRST(&ge->lg_provider);
539 LIST_REMOVE(pr, lg_provider);
540 delete_config(&pr->lg_config);
541 if (pr->lg_name) free(pr->lg_name);
542 if (pr->lg_mode) free(pr->lg_mode);
546 co = LIST_FIRST(&ge->lg_consumer);
549 LIST_REMOVE(co, lg_consumer);
550 delete_config(&co->lg_config);
551 if (co->lg_mode) free(co->lg_mode);