]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - lib/libgeom/geom_xml2tree.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / lib / libgeom / geom_xml2tree.c
1 /*-
2  * Copyright (c) 2003 Poul-Henning Kamp
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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
15  *    permission.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 #include <stdio.h>
33 #include <inttypes.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <ctype.h>
40 #include <sys/stat.h>
41 #include <sys/mman.h>
42 #include <sys/queue.h>
43 #include <sys/sbuf.h>
44 #include <sys/sysctl.h>
45 #include <err.h>
46 #include <bsdxml.h>
47 #include <libgeom.h>
48
49 struct mystate {
50         struct gmesh            *mesh;
51         struct gclass           *class;
52         struct ggeom            *geom;
53         struct gprovider        *provider;
54         struct gconsumer        *consumer;
55         int                     level;
56         struct sbuf             *sbuf[20];
57         struct gconf            *config;
58         int                     nident;
59         XML_Parser              parser;
60         int                     error;
61 };
62
63 static void
64 StartElement(void *userData, const char *name, const char **attr)
65 {
66         struct mystate *mt;
67         void *id;
68         void *ref;
69         int i;
70
71         mt = userData;
72         mt->level++;
73         mt->sbuf[mt->level] = sbuf_new_auto();
74         id = NULL;
75         ref = NULL;
76         for (i = 0; attr[i] != NULL; i += 2) {
77                 if (!strcmp(attr[i], "id")) {
78                         id = (void *)strtoul(attr[i + 1], NULL, 0);
79                         mt->nident++;
80                 } else if (!strcmp(attr[i], "ref")) {
81                         ref = (void *)strtoul(attr[i + 1], NULL, 0);
82                 } else
83                         printf("%*.*s[%s = %s]\n",
84                             mt->level + 1, mt->level + 1, "",
85                             attr[i], attr[i + 1]);
86         }
87         if (!strcmp(name, "class") && mt->class == NULL) {
88                 mt->class = calloc(1, sizeof *mt->class);
89                 if (mt->class == NULL) {
90                         mt->error = errno;
91                         XML_StopParser(mt->parser, 0);
92                         warn("Cannot allocate memory during processing of '%s' "
93                             "element", name);
94                         return;
95                 }
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);
100                 return;
101         }
102         if (!strcmp(name, "geom") && mt->geom == NULL) {
103                 mt->geom = calloc(1, sizeof *mt->geom);
104                 if (mt->geom == NULL) {
105                         mt->error = errno;
106                         XML_StopParser(mt->parser, 0);
107                         warn("Cannot allocate memory during processing of '%s' "
108                             "element", name);
109                         return;
110                 }
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);
116                 return;
117         }
118         if (!strcmp(name, "class") && mt->geom != NULL) {
119                 mt->geom->lg_class = ref;
120                 return;
121         }
122         if (!strcmp(name, "consumer") && mt->consumer == NULL) {
123                 mt->consumer = calloc(1, sizeof *mt->consumer);
124                 if (mt->consumer == NULL) {
125                         mt->error = errno;
126                         XML_StopParser(mt->parser, 0);
127                         warn("Cannot allocate memory during processing of '%s' "
128                             "element", name);
129                         return;
130                 }
131                 mt->consumer->lg_id = id;
132                 LIST_INSERT_HEAD(&mt->geom->lg_consumer, mt->consumer,
133                     lg_consumer);
134                 LIST_INIT(&mt->consumer->lg_config);
135                 return;
136         }
137         if (!strcmp(name, "geom") && mt->consumer != NULL) {
138                 mt->consumer->lg_geom = ref;
139                 return;
140         }
141         if (!strcmp(name, "provider") && mt->consumer != NULL) {
142                 mt->consumer->lg_provider = ref;
143                 return;
144         }
145         if (!strcmp(name, "provider") && mt->provider == NULL) {
146                 mt->provider = calloc(1, sizeof *mt->provider);
147                 if (mt->provider == NULL) {
148                         mt->error = errno;
149                         XML_StopParser(mt->parser, 0);
150                         warn("Cannot allocate memory during processing of '%s' "
151                             "element", name);
152                         return;
153                 }
154                 mt->provider->lg_id = id;
155                 LIST_INSERT_HEAD(&mt->geom->lg_provider, mt->provider,
156                     lg_provider);
157                 LIST_INIT(&mt->provider->lg_consumers);
158                 LIST_INIT(&mt->provider->lg_config);
159                 return;
160         }
161         if (!strcmp(name, "geom") && mt->provider != NULL) {
162                 mt->provider->lg_geom = ref;
163                 return;
164         }
165         if (!strcmp(name, "config")) {
166                 if (mt->provider != NULL) {
167                         mt->config = &mt->provider->lg_config;
168                         return;
169                 }
170                 if (mt->consumer != NULL) {
171                         mt->config = &mt->consumer->lg_config;
172                         return;
173                 }
174                 if (mt->geom != NULL) {
175                         mt->config = &mt->geom->lg_config;
176                         return;
177                 }
178                 if (mt->class != NULL) {
179                         mt->config = &mt->class->lg_config;
180                         return;
181                 }
182         }
183 }
184
185 static void
186 EndElement(void *userData, const char *name)
187 {
188         struct mystate *mt;
189         struct gconfig *gc;
190         char *p;
191
192         mt = userData;
193         p = NULL;
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;
198         mt->level--;
199         if (p == NULL) {
200                 mt->error = errno;
201                 XML_StopParser(mt->parser, 0);
202                 warn("Cannot allocate memory during processing of '%s' "
203                     "element", name);
204                 return;
205         }
206         if (strlen(p) == 0) {
207                 free(p);
208                 p = NULL;
209         }
210
211         if (!strcmp(name, "name")) {
212                 if (mt->provider != NULL) {
213                         mt->provider->lg_name = p;
214                         return;
215                 } else if (mt->geom != NULL) {
216                         mt->geom->lg_name = p;
217                         return;
218                 } else if (mt->class != NULL) {
219                         mt->class->lg_name = p;
220                         return;
221                 }
222         }
223         if (!strcmp(name, "rank") && mt->geom != NULL) {
224                 mt->geom->lg_rank = strtoul(p, NULL, 0);
225                 free(p);
226                 return;
227         }
228         if (!strcmp(name, "mode") && mt->provider != NULL) {
229                 mt->provider->lg_mode = p;
230                 return;
231         }
232         if (!strcmp(name, "mode") && mt->consumer != NULL) {
233                 mt->consumer->lg_mode = p;
234                 return;
235         }
236         if (!strcmp(name, "mediasize") && mt->provider != NULL) {
237                 mt->provider->lg_mediasize = strtoumax(p, NULL, 0);
238                 free(p);
239                 return;
240         }
241         if (!strcmp(name, "sectorsize") && mt->provider != NULL) {
242                 mt->provider->lg_sectorsize = strtoul(p, NULL, 0);
243                 free(p);
244                 return;
245         }
246         if (!strcmp(name, "stripesize") && mt->provider != NULL) {
247                 mt->provider->lg_stripesize = strtoumax(p, NULL, 0);
248                 free(p);
249                 return;
250         }
251         if (!strcmp(name, "stripeoffset") && mt->provider != NULL) {
252                 mt->provider->lg_stripeoffset = strtoumax(p, NULL, 0);
253                 free(p);
254                 return;
255         }
256
257         if (!strcmp(name, "config")) {
258                 mt->config = NULL;
259                 return;
260         }
261
262         if (mt->config != NULL) {
263                 gc = calloc(1, sizeof *gc);
264                 if (gc == NULL) {
265                         mt->error = errno;
266                         XML_StopParser(mt->parser, 0);
267                         warn("Cannot allocate memory during processing of '%s' "
268                             "element", name);
269                         return;
270                 }
271                 gc->lg_name = strdup(name);
272                 if (gc->lg_name == NULL) {
273                         mt->error = errno;
274                         XML_StopParser(mt->parser, 0);
275                         warn("Cannot allocate memory during processing of '%s' "
276                             "element", name);
277                         return;
278                 }
279                 gc->lg_val = p;
280                 LIST_INSERT_HEAD(mt->config, gc, lg_config);
281                 return;
282         }
283
284         if (p != NULL) {
285 #if DEBUG_LIBGEOM > 0
286                 printf("Unexpected XML: name=%s data=\"%s\"\n", name, p);
287 #endif
288                 free(p);
289         }
290
291         if (!strcmp(name, "consumer") && mt->consumer != NULL) {
292                 mt->consumer = NULL;
293                 return;
294         }
295         if (!strcmp(name, "provider") && mt->provider != NULL) {
296                 mt->provider = NULL;
297                 return;
298         }
299         if (!strcmp(name, "geom") && mt->consumer != NULL) {
300                 return;
301         }
302         if (!strcmp(name, "geom") && mt->provider != NULL) {
303                 return;
304         }
305         if (!strcmp(name, "geom") && mt->geom != NULL) {
306                 mt->geom = NULL;
307                 return;
308         }
309         if (!strcmp(name, "class") && mt->geom != NULL) {
310                 return;
311         }
312         if (!strcmp(name, "class") && mt->class != NULL) {
313                 mt->class = NULL;
314                 return;
315         }
316 }
317
318 static void
319 CharData(void *userData , const XML_Char *s , int len)
320 {
321         struct mystate *mt;
322         const char *b, *e;
323
324         mt = userData;
325
326         b = s;
327         e = s + len - 1;
328         while (isspace(*b) && b < e)
329                 b++;
330         while (isspace(*e) && e > b)
331                 e--;
332         if (e != b || (*b && !isspace(*b)))
333                 sbuf_bcat(mt->sbuf[mt->level], b, e - b + 1);
334 }
335
336 struct gident *
337 geom_lookupid(struct gmesh *gmp, const void *id)
338 {
339         struct gident *gip;
340
341         for (gip = gmp->lg_ident; gip->lg_id != NULL; gip++)
342                 if (gip->lg_id == id)
343                         return (gip);
344         return (NULL);
345 }
346
347 int
348 geom_xml2tree(struct gmesh *gmp, char *p)
349 {
350         XML_Parser parser;
351         struct mystate *mt;
352         struct gclass *cl;
353         struct ggeom *ge;
354         struct gprovider *pr;
355         struct gconsumer *co;
356         int error, i;
357
358         memset(gmp, 0, sizeof *gmp);
359         LIST_INIT(&gmp->lg_class);
360         parser = XML_ParserCreate(NULL);
361         if (parser == NULL)
362                 return (ENOMEM);
363         mt = calloc(1, sizeof *mt);
364         if (mt == NULL) {
365                 XML_ParserFree(parser);
366                 return (ENOMEM);
367         }
368         mt->mesh = gmp;
369         mt->parser = parser;
370         error = 0;
371         XML_SetUserData(parser, mt);
372         XML_SetElementHandler(parser, StartElement, EndElement);
373         XML_SetCharacterDataHandler(parser, CharData);
374         i = XML_Parse(parser, p, strlen(p), 1);
375         if (mt->error != 0)
376                 error = mt->error;
377         else if (i != 1) {
378                 error = XML_GetErrorCode(parser) == XML_ERROR_NO_MEMORY ?
379                     ENOMEM : EILSEQ;
380         }
381         XML_ParserFree(parser);
382         if (error != 0) {
383                 free(mt);
384                 return (error);
385         }
386         gmp->lg_ident = calloc(sizeof *gmp->lg_ident, mt->nident + 1);
387         free(mt);
388         if (gmp->lg_ident == NULL)
389                 return (ENOMEM);
390         i = 0;
391         /* Collect all identifiers */
392         LIST_FOREACH(cl, &gmp->lg_class, lg_class) {
393                 gmp->lg_ident[i].lg_id = cl->lg_id;
394                 gmp->lg_ident[i].lg_ptr = cl;
395                 gmp->lg_ident[i].lg_what = ISCLASS;
396                 i++;
397                 LIST_FOREACH(ge, &cl->lg_geom, lg_geom) {
398                         gmp->lg_ident[i].lg_id = ge->lg_id;
399                         gmp->lg_ident[i].lg_ptr = ge;
400                         gmp->lg_ident[i].lg_what = ISGEOM;
401                         i++;
402                         LIST_FOREACH(pr, &ge->lg_provider, lg_provider) {
403                                 gmp->lg_ident[i].lg_id = pr->lg_id;
404                                 gmp->lg_ident[i].lg_ptr = pr;
405                                 gmp->lg_ident[i].lg_what = ISPROVIDER;
406                                 i++;
407                         }
408                         LIST_FOREACH(co, &ge->lg_consumer, lg_consumer) {
409                                 gmp->lg_ident[i].lg_id = co->lg_id;
410                                 gmp->lg_ident[i].lg_ptr = co;
411                                 gmp->lg_ident[i].lg_what = ISCONSUMER;
412                                 i++;
413                         }
414                 }
415         }
416         /* Substitute all identifiers */
417         LIST_FOREACH(cl, &gmp->lg_class, lg_class) {
418                 LIST_FOREACH(ge, &cl->lg_geom, lg_geom) {
419                         ge->lg_class =
420                             geom_lookupid(gmp, ge->lg_class)->lg_ptr;
421                         LIST_FOREACH(pr, &ge->lg_provider, lg_provider) {
422                                 pr->lg_geom =
423                                     geom_lookupid(gmp, pr->lg_geom)->lg_ptr;
424                         }
425                         LIST_FOREACH(co, &ge->lg_consumer, lg_consumer) {
426                                 co->lg_geom =
427                                     geom_lookupid(gmp, co->lg_geom)->lg_ptr;
428                                 if (co->lg_provider != NULL) {
429                                         co->lg_provider = 
430                                             geom_lookupid(gmp,
431                                                 co->lg_provider)->lg_ptr;
432                                         LIST_INSERT_HEAD(
433                                             &co->lg_provider->lg_consumers,
434                                             co, lg_consumers);
435                                 }
436                         }
437                 }
438         }
439         return (0);
440 }
441
442 int
443 geom_gettree(struct gmesh *gmp)
444 {
445         char *p;
446         int error;
447
448         p = geom_getxml();
449         if (p == NULL)
450                 return (errno);
451         error = geom_xml2tree(gmp, p);
452         free(p);
453         return (error);
454 }
455
456 static void 
457 delete_config(struct gconf *gp)
458 {
459         struct gconfig *cf;
460
461         for (;;) {
462                 cf = LIST_FIRST(gp);
463                 if (cf == NULL)
464                         return;
465                 LIST_REMOVE(cf, lg_config);
466                 free(cf->lg_name);
467                 free(cf->lg_val);
468                 free(cf);
469         }
470 }
471
472 void
473 geom_deletetree(struct gmesh *gmp)
474 {
475         struct gclass *cl;
476         struct ggeom *ge;
477         struct gprovider *pr;
478         struct gconsumer *co;
479
480         free(gmp->lg_ident);
481         gmp->lg_ident = NULL;
482         for (;;) {
483                 cl = LIST_FIRST(&gmp->lg_class);
484                 if (cl == NULL) 
485                         break;
486                 LIST_REMOVE(cl, lg_class);
487                 delete_config(&cl->lg_config);
488                 if (cl->lg_name) free(cl->lg_name);
489                 for (;;) {
490                         ge = LIST_FIRST(&cl->lg_geom);
491                         if (ge == NULL) 
492                                 break;
493                         LIST_REMOVE(ge, lg_geom);
494                         delete_config(&ge->lg_config);
495                         if (ge->lg_name) free(ge->lg_name);
496                         for (;;) {
497                                 pr = LIST_FIRST(&ge->lg_provider);
498                                 if (pr == NULL) 
499                                         break;
500                                 LIST_REMOVE(pr, lg_provider);
501                                 delete_config(&pr->lg_config);
502                                 if (pr->lg_name) free(pr->lg_name);
503                                 if (pr->lg_mode) free(pr->lg_mode);
504                                 free(pr);
505                         }
506                         for (;;) {
507                                 co = LIST_FIRST(&ge->lg_consumer);
508                                 if (co == NULL) 
509                                         break;
510                                 LIST_REMOVE(co, lg_consumer);
511                                 delete_config(&co->lg_config);
512                                 if (co->lg_mode) free(co->lg_mode);
513                                 free(co);
514                         }
515                         free(ge);
516                 }
517                 free(cl);
518         }
519 }