check result of memory allocations (patch by Stanislav Fomichev)
[project/uci.git] / ucimap.c
1 /*
2 * ucimap.c - Library for the Unified Configuration Interface
3 * Copyright (C) 2008-2009 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 */
14
15 /*
16 * This file contains ucimap, an API for mapping UCI to C data structures
17 */
18
19 #include <strings.h>
20 #include <stdbool.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include "ucimap.h"
29 #include "uci_internal.h"
30
31 struct ucimap_alloc {
32 void *ptr;
33 };
34
35 struct ucimap_alloc_custom {
36 void *section;
37 struct uci_optmap *om;
38 void *ptr;
39 };
40
41 struct ucimap_fixup {
42 struct ucimap_fixup *next;
43 struct uci_sectionmap *sm;
44 const char *name;
45 enum ucimap_type type;
46 union ucimap_data *data;
47 };
48
49 #define ucimap_foreach_option(_sm, _o) \
50 if (!(_sm)->options_size) \
51 (_sm)->options_size = sizeof(struct uci_optmap); \
52 for (_o = &(_sm)->options[0]; \
53 ((char *)(_o)) < ((char *) &(_sm)->options[0] + \
54 (_sm)->options_size * (_sm)->n_options); \
55 _o = (struct uci_optmap *) ((char *)(_o) + \
56 (_sm)->options_size))
57
58
59 static inline bool
60 ucimap_is_alloc(enum ucimap_type type)
61 {
62 return (type & UCIMAP_SUBTYPE) == UCIMAP_STRING;
63 }
64
65 static inline bool
66 ucimap_is_fixup(enum ucimap_type type)
67 {
68 return (type & UCIMAP_SUBTYPE) == UCIMAP_SECTION;
69 }
70
71 static inline bool
72 ucimap_is_simple(enum ucimap_type type)
73 {
74 return ((type & UCIMAP_TYPE) == UCIMAP_SIMPLE);
75 }
76
77 static inline bool
78 ucimap_is_list(enum ucimap_type type)
79 {
80 return ((type & UCIMAP_TYPE) == UCIMAP_LIST);
81 }
82
83 static inline bool
84 ucimap_is_list_auto(enum ucimap_type type)
85 {
86 return ucimap_is_list(type) && !!(type & UCIMAP_LIST_AUTO);
87 }
88
89 static inline bool
90 ucimap_is_custom(enum ucimap_type type)
91 {
92 return ((type & UCIMAP_SUBTYPE) == UCIMAP_CUSTOM);
93 }
94
95 static inline void *
96 ucimap_section_ptr(struct ucimap_section_data *sd)
97 {
98 return ((char *) sd - sd->sm->smap_offset);
99 }
100
101 static inline struct ucimap_section_data *
102 ucimap_ptr_section(struct uci_sectionmap *sm, void *ptr) {
103 ptr = (char *) ptr + sm->smap_offset;
104 return ptr;
105 }
106
107 static inline union ucimap_data *
108 ucimap_get_data(struct ucimap_section_data *sd, struct uci_optmap *om)
109 {
110 void *data;
111
112 data = (char *) ucimap_section_ptr(sd) + om->offset;
113 return data;
114 }
115
116 int
117 ucimap_init(struct uci_map *map)
118 {
119 map->fixup = NULL;
120 map->sdata = NULL;
121 map->fixup_tail = &map->fixup;
122 map->sdata_tail = &map->sdata;
123 return 0;
124 }
125
126 static void
127 ucimap_add_alloc(struct ucimap_section_data *sd, void *ptr)
128 {
129 struct ucimap_alloc *a = &sd->allocmap[sd->allocmap_len++];
130 a->ptr = ptr;
131 }
132
133 void
134 ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd)
135 {
136 void *section;
137 int i;
138
139 section = ucimap_section_ptr(sd);
140 if (sd->ref)
141 *sd->ref = sd->next;
142
143 if (sd->sm->free)
144 sd->sm->free(map, section);
145
146 for (i = 0; i < sd->allocmap_len; i++) {
147 free(sd->allocmap[i].ptr);
148 }
149
150 if (sd->alloc_custom) {
151 for (i = 0; i < sd->alloc_custom_len; i++) {
152 struct ucimap_alloc_custom *a = &sd->alloc_custom[i];
153 a->om->free(a->section, a->om, a->ptr);
154 }
155 free(sd->alloc_custom);
156 }
157
158 free(sd->allocmap);
159 free(sd);
160 }
161
162 void
163 ucimap_cleanup(struct uci_map *map)
164 {
165 struct ucimap_section_data *sd, *sd_next;
166
167 for (sd = map->sdata; sd; sd = sd_next) {
168 sd_next = sd->next;
169 ucimap_free_section(map, sd);
170 }
171 }
172
173 static void *
174 ucimap_find_section(struct uci_map *map, struct ucimap_fixup *f)
175 {
176 struct ucimap_section_data *sd;
177
178 for (sd = map->sdata; sd; sd = sd->next) {
179 if (sd->sm != f->sm)
180 continue;
181 if (strcmp(f->name, sd->section_name) != 0)
182 continue;
183 return ucimap_section_ptr(sd);
184 }
185 for (sd = map->pending; sd; sd = sd->next) {
186 if (sd->sm != f->sm)
187 continue;
188 if (strcmp(f->name, sd->section_name) != 0)
189 continue;
190 return ucimap_section_ptr(sd);
191 }
192 return NULL;
193 }
194
195 static union ucimap_data *
196 ucimap_list_append(struct ucimap_list *list)
197 {
198 if (unlikely(list->size <= list->n_items)) {
199 /* should not happen */
200 DPRINTF("ERROR: overflow while filling a list (size=%d)\n", list->size);
201 return NULL;
202 }
203 return &list->item[list->n_items++];
204 }
205
206
207 static bool
208 ucimap_handle_fixup(struct uci_map *map, struct ucimap_fixup *f)
209 {
210 void *ptr = ucimap_find_section(map, f);
211 union ucimap_data *data;
212
213 if (!ptr)
214 return false;
215
216 switch(f->type & UCIMAP_TYPE) {
217 case UCIMAP_SIMPLE:
218 f->data->ptr = ptr;
219 break;
220 case UCIMAP_LIST:
221 data = ucimap_list_append(f->data->list);
222 if (!data)
223 return false;
224
225 data->ptr = ptr;
226 break;
227 }
228 return true;
229 }
230
231 void
232 ucimap_free_item(struct ucimap_section_data *sd, void *item)
233 {
234 struct ucimap_alloc_custom *ac;
235 struct ucimap_alloc *a;
236 void *ptr = *((void **) item);
237 int i;
238
239 if (!ptr)
240 return;
241
242 *((void **)item) = NULL;
243 for (i = 0, a = sd->allocmap; i < sd->allocmap_len; i++, a++) {
244 if (a->ptr != ptr)
245 continue;
246
247 if (i != sd->allocmap_len - 1)
248 a->ptr = sd->allocmap[sd->allocmap_len - 1].ptr;
249
250 sd->allocmap_len--;
251 return;
252 }
253
254 for (i = 0, ac = sd->alloc_custom; i < sd->alloc_custom_len; i++, ac++) {
255 if (ac->ptr != ptr)
256 continue;
257
258 if (i != sd->alloc_custom_len - 1)
259 memcpy(ac, &sd->alloc_custom[sd->alloc_custom_len - 1],
260 sizeof(struct ucimap_alloc_custom));
261
262 ac->om->free(ac->section, ac->om, ac->ptr);
263 sd->alloc_custom_len--;
264 return;
265 }
266 }
267
268 int
269 ucimap_resize_list(struct ucimap_section_data *sd, struct ucimap_list **list, int items)
270 {
271 struct ucimap_list *new;
272 struct ucimap_alloc *a;
273 int i, offset = 0;
274 int size = sizeof(struct ucimap_list) + items * sizeof(union ucimap_data);
275
276 if (!*list) {
277 new = calloc(1, size);
278 if (!new)
279 return -ENOMEM;
280
281 ucimap_add_alloc(sd, new);
282 goto set;
283 }
284
285 for (i = 0, a = sd->allocmap; i < sd->allocmap_len; i++, a++) {
286 if (a->ptr != *list)
287 continue;
288
289 goto realloc;
290 }
291 return -ENOENT;
292
293 realloc:
294 if (items > (*list)->size)
295 offset = (items - (*list)->size) * sizeof(union ucimap_data);
296
297 a->ptr = realloc(a->ptr, size);
298 if (offset)
299 memset((char *) a->ptr + offset, 0, size - offset);
300 new = a->ptr;
301
302 set:
303 new->size = items;
304 *list = new;
305 return 0;
306 }
307
308 static void
309 ucimap_add_fixup(struct ucimap_section_data *sd, union ucimap_data *data, struct uci_optmap *om, const char *str)
310 {
311 struct ucimap_fixup *f, tmp;
312 struct uci_map *map = sd->map;
313
314 tmp.sm = om->data.sm;
315 tmp.name = str;
316 tmp.type = om->type;
317 tmp.data = data;
318 if (ucimap_handle_fixup(map, &tmp))
319 return;
320
321 f = malloc(sizeof(struct ucimap_fixup));
322 if (!f)
323 return;
324
325 memcpy(f, &tmp, sizeof(tmp));
326 f->next = NULL;
327 *map->fixup_tail = f;
328 map->fixup_tail = &f->next;
329 }
330
331 static void
332 ucimap_add_custom_alloc(struct ucimap_section_data *sd, struct uci_optmap *om, void *ptr)
333 {
334 struct ucimap_alloc_custom *a = &sd->alloc_custom[sd->alloc_custom_len++];
335
336 a->section = ucimap_section_ptr(sd);
337 a->om = om;
338 a->ptr = ptr;
339 }
340
341 static void
342 ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
343 {
344 union ucimap_data tdata = *data;
345 char *eptr = NULL;
346 long lval;
347 char *s;
348 int val;
349
350 if (ucimap_is_list(om->type) && !ucimap_is_fixup(om->type)) {
351 data = ucimap_list_append(data->list);
352 if (!data)
353 return;
354 }
355
356 switch(om->type & UCIMAP_SUBTYPE) {
357 case UCIMAP_STRING:
358 if ((om->data.s.maxlen > 0) &&
359 (strlen(str) > om->data.s.maxlen))
360 return;
361
362 s = strdup(str);
363 tdata.s = s;
364 ucimap_add_alloc(sd, s);
365 break;
366 case UCIMAP_BOOL:
367 if (!strcmp(str, "on"))
368 val = true;
369 else if (!strcmp(str, "1"))
370 val = true;
371 else if (!strcmp(str, "enabled"))
372 val = true;
373 else if (!strcmp(str, "off"))
374 val = false;
375 else if (!strcmp(str, "0"))
376 val = false;
377 else if (!strcmp(str, "disabled"))
378 val = false;
379 else
380 return;
381
382 tdata.b = val;
383 break;
384 case UCIMAP_INT:
385 lval = strtol(str, &eptr, om->data.i.base);
386 if (lval < INT_MIN || lval > INT_MAX)
387 return;
388
389 if (!eptr || *eptr == '\0')
390 tdata.i = (int) lval;
391 else
392 return;
393 break;
394 case UCIMAP_SECTION:
395 ucimap_add_fixup(sd, data, om, str);
396 return;
397 case UCIMAP_CUSTOM:
398 break;
399 }
400 if (om->parse) {
401 if (om->parse(ucimap_section_ptr(sd), om, data, str) < 0)
402 return;
403 if (ucimap_is_custom(om->type) && om->free) {
404 if (tdata.ptr != data->ptr)
405 ucimap_add_custom_alloc(sd, om, data->ptr);
406 }
407 }
408 if (ucimap_is_custom(om->type))
409 return;
410 memcpy(data, &tdata, sizeof(union ucimap_data));
411 }
412
413
414 static void
415 ucimap_convert_list(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
416 {
417 char *s, *p;
418
419 s = strdup(str);
420 if (!s)
421 return;
422
423 ucimap_add_alloc(sd, s);
424
425 do {
426 while (isspace(*s))
427 s++;
428
429 if (!*s)
430 break;
431
432 p = s;
433 while (*s && !isspace(*s))
434 s++;
435
436 if (isspace(*s)) {
437 *s = 0;
438 s++;
439 }
440
441 ucimap_add_value(data, om, sd, p);
442 } while (*s);
443 }
444
445 static int
446 ucimap_parse_options(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
447 {
448 struct uci_element *e, *l;
449 struct uci_option *o;
450 union ucimap_data *data;
451
452 uci_foreach_element(&s->options, e) {
453 struct uci_optmap *om = NULL, *tmp;
454
455 ucimap_foreach_option(sm, tmp) {
456 if (strcmp(e->name, tmp->name) == 0) {
457 om = tmp;
458 break;
459 }
460 }
461 if (!om)
462 continue;
463
464 data = ucimap_get_data(sd, om);
465 o = uci_to_option(e);
466 if ((o->type == UCI_TYPE_STRING) && ucimap_is_simple(om->type)) {
467 ucimap_add_value(data, om, sd, o->v.string);
468 } else if ((o->type == UCI_TYPE_LIST) && ucimap_is_list(om->type)) {
469 uci_foreach_element(&o->v.list, l) {
470 ucimap_add_value(data, om, sd, l->name);
471 }
472 } else if ((o->type == UCI_TYPE_STRING) && ucimap_is_list_auto(om->type)) {
473 ucimap_convert_list(data, om, sd, o->v.string);
474 }
475 }
476
477 return 0;
478 }
479
480 static void
481 ucimap_add_section_list(struct uci_map *map, struct ucimap_section_data *sd)
482 {
483 sd->ref = map->sdata_tail;
484 *sd->ref = sd;
485 map->sdata_tail = &sd->next;
486 }
487
488 static void
489 ucimap_add_section(struct ucimap_section_data *sd)
490 {
491 struct uci_map *map = sd->map;
492
493 sd->next = NULL;
494 if (sd->sm->add(map, ucimap_section_ptr(sd)) < 0)
495 ucimap_free_section(map, sd);
496 else
497 ucimap_add_section_list(map, sd);
498 }
499
500 #ifdef UCI_DEBUG
501 static const char *ucimap_type_names[] = {
502 [UCIMAP_STRING] = "string",
503 [UCIMAP_INT] = "integer",
504 [UCIMAP_BOOL] = "boolean",
505 [UCIMAP_SECTION] = "section",
506 [UCIMAP_LIST] = "list",
507 };
508
509 static const char *
510 ucimap_get_type_name(int type)
511 {
512 static char buf[32];
513 const char *name;
514
515 if (ucimap_is_list(type))
516 return ucimap_type_names[UCIMAP_LIST];
517
518 name = ucimap_type_names[type & UCIMAP_SUBTYPE];
519 if (!name) {
520 sprintf(buf, "Unknown (%d)", type & UCIMAP_SUBTYPE);
521 name = buf;
522 }
523
524 return name;
525 }
526 #endif
527
528 static bool
529 ucimap_check_optmap_type(struct uci_sectionmap *sm, struct uci_optmap *om)
530 {
531 unsigned int type;
532
533 if (unlikely(sm->type_name != om->type_name) &&
534 unlikely(strcmp(sm->type_name, om->type_name) != 0)) {
535 DPRINTF("Option '%s' of section type '%s' refereces unknown "
536 "section type '%s', should be '%s'.\n",
537 om->name, sm->type, om->type_name, sm->type_name);
538 return false;
539 }
540
541 if (om->detected_type < 0)
542 return true;
543
544 if (ucimap_is_custom(om->type))
545 return true;
546
547 if (ucimap_is_list(om->type) !=
548 ucimap_is_list(om->detected_type))
549 goto failed;
550
551 if (ucimap_is_list(om->type))
552 return true;
553
554 type = om->type & UCIMAP_SUBTYPE;
555 switch(type) {
556 case UCIMAP_STRING:
557 case UCIMAP_INT:
558 case UCIMAP_BOOL:
559 if (type != om->detected_type)
560 goto failed;
561 break;
562 case UCIMAP_SECTION:
563 goto failed;
564 default:
565 break;
566 }
567 return true;
568
569 failed:
570 DPRINTF("Invalid type in option '%s' of section type '%s', "
571 "declared type is %s, detected type is %s\n",
572 om->name, sm->type,
573 ucimap_get_type_name(om->type),
574 ucimap_get_type_name(om->detected_type));
575 return false;
576 }
577
578 static void
579 ucimap_count_alloc(struct uci_optmap *om, int *n_alloc, int *n_custom)
580 {
581 if (ucimap_is_alloc(om->type))
582 (*n_alloc)++;
583 else if (ucimap_is_custom(om->type) && om->free)
584 (*n_custom)++;
585 }
586
587 int
588 ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
589 {
590 struct uci_optmap *om;
591 char *section_name;
592 void *section;
593 int n_alloc = 2;
594 int n_alloc_custom = 0;
595 int err;
596
597 sd->map = map;
598 sd->sm = sm;
599
600 ucimap_foreach_option(sm, om) {
601 if (!ucimap_check_optmap_type(sm, om))
602 continue;
603
604 if (ucimap_is_list(om->type)) {
605 union ucimap_data *data;
606 struct uci_element *e;
607 int n_elements = 0;
608 int n_elements_alloc = 0;
609 int n_elements_custom = 0;
610 int size;
611
612 data = ucimap_get_data(sd, om);
613 uci_foreach_element(&s->options, e) {
614 struct uci_option *o = uci_to_option(e);
615 struct uci_element *tmp;
616
617 if (strcmp(e->name, om->name) != 0)
618 continue;
619
620 if (o->type == UCI_TYPE_LIST) {
621 uci_foreach_element(&o->v.list, tmp) {
622 ucimap_count_alloc(om, &n_elements_alloc, &n_elements_custom);
623 n_elements++;
624 }
625 } else if ((o->type == UCI_TYPE_STRING) &&
626 ucimap_is_list_auto(om->type)) {
627 const char *data = o->v.string;
628 do {
629 while (isspace(*data))
630 data++;
631
632 if (!*data)
633 break;
634
635 n_elements++;
636 ucimap_count_alloc(om, &n_elements_alloc, &n_elements_custom);
637
638 while (*data && !isspace(*data))
639 data++;
640 } while (*data);
641
642 /* for the duplicated data string */
643 if (n_elements)
644 n_alloc++;
645 }
646 break;
647 }
648 /* add one more for the ucimap_list */
649 n_alloc += n_elements_alloc + 1;
650 n_alloc_custom += n_elements_custom;
651 size = sizeof(struct ucimap_list) +
652 n_elements * sizeof(union ucimap_data);
653
654 data->list = malloc(size);
655 if (!data->list)
656 goto error_mem;
657
658 memset(data->list, 0, size);
659 data->list->size = n_elements;
660 } else {
661 ucimap_count_alloc(om, &n_alloc, &n_alloc_custom);
662 }
663 }
664
665 sd->allocmap = calloc(n_alloc, sizeof(struct ucimap_alloc));
666 if (!sd->allocmap)
667 goto error_mem;
668
669 if (n_alloc_custom > 0) {
670 sd->alloc_custom = calloc(n_alloc_custom, sizeof(struct ucimap_alloc_custom));
671 if (!sd->alloc_custom)
672 goto error_mem;
673 }
674
675 section_name = strdup(s->e.name);
676 if (!section_name)
677 goto error_mem;
678
679 sd->section_name = section_name;
680
681 sd->cmap = calloc(1, BITFIELD_SIZE(sm->n_options));
682 if (!sd->cmap)
683 goto error_mem;
684
685 ucimap_add_alloc(sd, (void *)section_name);
686 ucimap_add_alloc(sd, (void *)sd->cmap);
687 ucimap_foreach_option(sm, om) {
688 if (!ucimap_is_list(om->type))
689 continue;
690
691 ucimap_add_alloc(sd, ucimap_get_data(sd, om)->list);
692 }
693
694 section = ucimap_section_ptr(sd);
695 err = sm->init(map, section, s);
696 if (err)
697 goto error;
698
699 if (map->parsed) {
700 ucimap_add_section(sd);
701 } else {
702 ucimap_add_section_list(map, sd);
703 }
704
705 err = ucimap_parse_options(map, sm, sd, s);
706 if (err)
707 goto error;
708
709 return 0;
710
711 error_mem:
712 if (sd->allocmap)
713 free(sd->allocmap);
714 free(sd);
715 return UCI_ERR_MEM;
716
717 error:
718 ucimap_free_section(map, sd);
719 return err;
720 }
721
722 static int
723 ucimap_fill_ptr(struct uci_ptr *ptr, struct uci_section *s, const char *option)
724 {
725 struct uci_package *p = s->package;
726
727 memset(ptr, 0, sizeof(struct uci_ptr));
728
729 ptr->package = p->e.name;
730 ptr->p = p;
731
732 ptr->section = s->e.name;
733 ptr->s = s;
734
735 ptr->option = option;
736 return uci_lookup_ptr(p->ctx, ptr, NULL, false);
737 }
738
739 void
740 ucimap_set_changed(struct ucimap_section_data *sd, void *field)
741 {
742 void *section = ucimap_section_ptr(sd);
743 struct uci_sectionmap *sm = sd->sm;
744 struct uci_optmap *om;
745 int ofs = (char *)field - (char *)section;
746 int i = 0;
747
748 ucimap_foreach_option(sm, om) {
749 if (om->offset == ofs) {
750 SET_BIT(sd->cmap, i);
751 break;
752 }
753 i++;
754 }
755 }
756
757 static char *
758 ucimap_data_to_string(struct ucimap_section_data *sd, struct uci_optmap *om, union ucimap_data *data)
759 {
760 static char buf[32];
761 char *str = NULL;
762
763 switch(om->type & UCIMAP_SUBTYPE) {
764 case UCIMAP_STRING:
765 str = data->s;
766 break;
767 case UCIMAP_INT:
768 sprintf(buf, "%d", data->i);
769 str = buf;
770 break;
771 case UCIMAP_BOOL:
772 sprintf(buf, "%d", !!data->b);
773 str = buf;
774 break;
775 case UCIMAP_SECTION:
776 if (data->ptr)
777 str = (char *) ucimap_ptr_section(om->data.sm, data->ptr)->section_name;
778 else
779 str = "";
780 break;
781 case UCIMAP_CUSTOM:
782 break;
783 default:
784 return NULL;
785 }
786
787 if (om->format) {
788 if (om->format(ucimap_section_ptr(sd), om, data, &str) < 0)
789 return NULL;
790
791 if (!str)
792 str = "";
793 }
794 return str;
795 }
796
797 int
798 ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd)
799 {
800 struct uci_sectionmap *sm = sd->sm;
801 struct uci_section *s = NULL;
802 struct uci_optmap *om;
803 struct uci_element *e;
804 struct uci_ptr ptr;
805 int i = 0;
806 int ret;
807
808 uci_foreach_element(&p->sections, e) {
809 if (!strcmp(e->name, sd->section_name)) {
810 s = uci_to_section(e);
811 break;
812 }
813 }
814 if (!s)
815 return UCI_ERR_NOTFOUND;
816
817 ucimap_foreach_option(sm, om) {
818 union ucimap_data *data;
819
820 i++;
821 data = ucimap_get_data(sd, om);
822 if (!TEST_BIT(sd->cmap, i - 1))
823 continue;
824
825 ucimap_fill_ptr(&ptr, s, om->name);
826 if (ucimap_is_list(om->type)) {
827 struct ucimap_list *list = data->list;
828 bool first = true;
829 int j;
830
831 for (j = 0; j < list->n_items; j++) {
832 ptr.value = ucimap_data_to_string(sd, om, &list->item[j]);
833 if (!ptr.value)
834 continue;
835
836 if (first) {
837 ret = uci_set(s->package->ctx, &ptr);
838 first = false;
839 } else {
840 ret = uci_add_list(s->package->ctx, &ptr);
841 }
842 if (ret)
843 return ret;
844 }
845 } else {
846 ptr.value = ucimap_data_to_string(sd, om, data);
847 if (!ptr.value)
848 continue;
849
850 ret = uci_set(s->package->ctx, &ptr);
851 if (ret)
852 return ret;
853 }
854
855 CLR_BIT(sd->cmap, i - 1);
856 }
857
858 return 0;
859 }
860
861 void
862 ucimap_parse(struct uci_map *map, struct uci_package *pkg)
863 {
864 struct uci_element *e;
865 struct ucimap_section_data *sd, **sd_tail;
866 struct ucimap_fixup *f;
867 int i;
868
869 sd_tail = map->sdata_tail;
870 map->parsed = false;
871 map->sdata_tail = &map->pending;
872 uci_foreach_element(&pkg->sections, e) {
873 struct uci_section *s = uci_to_section(e);
874
875 for (i = 0; i < map->n_sections; i++) {
876 struct uci_sectionmap *sm = map->sections[i];
877 struct ucimap_section_data *sd;
878
879 if (strcmp(s->type, map->sections[i]->type) != 0)
880 continue;
881
882 if (sm->alloc) {
883 sd = sm->alloc(map, sm, s);
884 memset(sd, 0, sizeof(struct ucimap_section_data));
885 } else {
886 sd = malloc(sm->alloc_len);
887 memset(sd, 0, sm->alloc_len);
888 sd = ucimap_ptr_section(sm, sd);
889 }
890 if (!sd)
891 continue;
892
893 ucimap_parse_section(map, sm, sd, s);
894 }
895 }
896 if (!map->parsed) {
897 map->parsed = true;
898 map->sdata_tail = sd_tail;
899 }
900
901 f = map->fixup;
902 while (f) {
903 struct ucimap_fixup *next = f->next;
904 ucimap_handle_fixup(map, f);
905 free(f);
906 f = next;
907 }
908 map->fixup_tail = &map->fixup;
909 map->fixup = NULL;
910
911 sd = map->pending;
912 while (sd) {
913 struct ucimap_section_data *next = sd->next;
914 ucimap_add_section(sd);
915 sd = next;
916 }
917 map->pending = NULL;
918 }