build: remove install prefix override
[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 (!a->ptr)
299 return -ENOMEM;
300
301 if (offset)
302 memset((char *) a->ptr + offset, 0, size - offset);
303 new = a->ptr;
304
305 set:
306 new->size = items;
307 *list = new;
308 return 0;
309 }
310
311 static void
312 ucimap_add_fixup(struct ucimap_section_data *sd, union ucimap_data *data, struct uci_optmap *om, const char *str)
313 {
314 struct ucimap_fixup *f, tmp;
315 struct uci_map *map = sd->map;
316
317 tmp.next = NULL;
318 tmp.sm = om->data.sm;
319 tmp.name = str;
320 tmp.type = om->type;
321 tmp.data = data;
322 if (ucimap_handle_fixup(map, &tmp))
323 return;
324
325 f = malloc(sizeof(struct ucimap_fixup));
326 if (!f)
327 return;
328
329 memcpy(f, &tmp, sizeof(tmp));
330 f->next = NULL;
331 *map->fixup_tail = f;
332 map->fixup_tail = &f->next;
333 }
334
335 static void
336 ucimap_add_custom_alloc(struct ucimap_section_data *sd, struct uci_optmap *om, void *ptr)
337 {
338 struct ucimap_alloc_custom *a = &sd->alloc_custom[sd->alloc_custom_len++];
339
340 a->section = ucimap_section_ptr(sd);
341 a->om = om;
342 a->ptr = ptr;
343 }
344
345 static void
346 ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
347 {
348 union ucimap_data tdata = *data;
349 char *eptr = NULL;
350 long lval;
351 char *s;
352 int val;
353
354 if (ucimap_is_list(om->type) && !ucimap_is_fixup(om->type)) {
355 data = ucimap_list_append(data->list);
356 if (!data)
357 return;
358 }
359
360 switch(om->type & UCIMAP_SUBTYPE) {
361 case UCIMAP_STRING:
362 if ((om->data.s.maxlen > 0) &&
363 (strlen(str) > om->data.s.maxlen))
364 return;
365
366 s = strdup(str);
367 tdata.s = s;
368 ucimap_add_alloc(sd, s);
369 break;
370 case UCIMAP_BOOL:
371 if (!strcmp(str, "on"))
372 val = true;
373 else if (!strcmp(str, "1"))
374 val = true;
375 else if (!strcmp(str, "enabled"))
376 val = true;
377 else if (!strcmp(str, "off"))
378 val = false;
379 else if (!strcmp(str, "0"))
380 val = false;
381 else if (!strcmp(str, "disabled"))
382 val = false;
383 else
384 return;
385
386 tdata.b = val;
387 break;
388 case UCIMAP_INT:
389 lval = strtol(str, &eptr, om->data.i.base);
390 if (lval < INT_MIN || lval > INT_MAX)
391 return;
392
393 if (!eptr || *eptr == '\0')
394 tdata.i = (int) lval;
395 else
396 return;
397 break;
398 case UCIMAP_SECTION:
399 ucimap_add_fixup(sd, data, om, str);
400 return;
401 case UCIMAP_CUSTOM:
402 break;
403 }
404 if (om->parse) {
405 if (om->parse(ucimap_section_ptr(sd), om, data, str) < 0)
406 return;
407 if (ucimap_is_custom(om->type) && om->free) {
408 if (tdata.ptr != data->ptr)
409 ucimap_add_custom_alloc(sd, om, data->ptr);
410 }
411 }
412 if (ucimap_is_custom(om->type))
413 return;
414 memcpy(data, &tdata, sizeof(union ucimap_data));
415 }
416
417
418 static void
419 ucimap_convert_list(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
420 {
421 char *s, *p;
422
423 s = strdup(str);
424 if (!s)
425 return;
426
427 ucimap_add_alloc(sd, s);
428
429 do {
430 while (isspace(*s))
431 s++;
432
433 if (!*s)
434 break;
435
436 p = s;
437 while (*s && !isspace(*s))
438 s++;
439
440 if (isspace(*s)) {
441 *s = 0;
442 s++;
443 }
444
445 ucimap_add_value(data, om, sd, p);
446 } while (*s);
447 }
448
449 static int
450 ucimap_parse_options(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
451 {
452 struct uci_element *e, *l;
453 struct uci_option *o;
454 union ucimap_data *data;
455
456 uci_foreach_element(&s->options, e) {
457 struct uci_optmap *om = NULL, *tmp;
458
459 ucimap_foreach_option(sm, tmp) {
460 if (strcmp(e->name, tmp->name) == 0) {
461 om = tmp;
462 break;
463 }
464 }
465 if (!om)
466 continue;
467
468 data = ucimap_get_data(sd, om);
469 o = uci_to_option(e);
470 if ((o->type == UCI_TYPE_STRING) && ucimap_is_simple(om->type)) {
471 ucimap_add_value(data, om, sd, o->v.string);
472 } else if ((o->type == UCI_TYPE_LIST) && ucimap_is_list(om->type)) {
473 uci_foreach_element(&o->v.list, l) {
474 ucimap_add_value(data, om, sd, l->name);
475 }
476 } else if ((o->type == UCI_TYPE_STRING) && ucimap_is_list_auto(om->type)) {
477 ucimap_convert_list(data, om, sd, o->v.string);
478 }
479 }
480
481 return 0;
482 }
483
484 static void
485 ucimap_add_section_list(struct uci_map *map, struct ucimap_section_data *sd)
486 {
487 sd->ref = map->sdata_tail;
488 *sd->ref = sd;
489 map->sdata_tail = &sd->next;
490 }
491
492 static void
493 ucimap_add_section(struct ucimap_section_data *sd)
494 {
495 struct uci_map *map = sd->map;
496
497 sd->next = NULL;
498 if (sd->sm->add(map, ucimap_section_ptr(sd)) < 0)
499 ucimap_free_section(map, sd);
500 else
501 ucimap_add_section_list(map, sd);
502 }
503
504 #ifdef UCI_DEBUG
505 static const char *ucimap_type_names[] = {
506 [UCIMAP_STRING] = "string",
507 [UCIMAP_INT] = "integer",
508 [UCIMAP_BOOL] = "boolean",
509 [UCIMAP_SECTION] = "section",
510 [UCIMAP_LIST] = "list",
511 };
512
513 static const char *
514 ucimap_get_type_name(int type)
515 {
516 static char buf[32];
517 const char *name;
518
519 if (ucimap_is_list(type))
520 return ucimap_type_names[UCIMAP_LIST];
521
522 name = ucimap_type_names[type & UCIMAP_SUBTYPE];
523 if (!name) {
524 sprintf(buf, "Unknown (%d)", type & UCIMAP_SUBTYPE);
525 name = buf;
526 }
527
528 return name;
529 }
530 #endif
531
532 static bool
533 ucimap_check_optmap_type(struct uci_sectionmap *sm, struct uci_optmap *om)
534 {
535 unsigned int type;
536
537 if (unlikely(sm->type_name != om->type_name) &&
538 unlikely(strcmp(sm->type_name, om->type_name) != 0)) {
539 DPRINTF("Option '%s' of section type '%s' refereces unknown "
540 "section type '%s', should be '%s'.\n",
541 om->name, sm->type, om->type_name, sm->type_name);
542 return false;
543 }
544
545 if (om->detected_type < 0)
546 return true;
547
548 if (ucimap_is_custom(om->type))
549 return true;
550
551 if (ucimap_is_list(om->type) !=
552 ucimap_is_list(om->detected_type))
553 goto failed;
554
555 if (ucimap_is_list(om->type))
556 return true;
557
558 type = om->type & UCIMAP_SUBTYPE;
559 switch(type) {
560 case UCIMAP_STRING:
561 case UCIMAP_INT:
562 case UCIMAP_BOOL:
563 if (type != om->detected_type)
564 goto failed;
565 break;
566 case UCIMAP_SECTION:
567 goto failed;
568 default:
569 break;
570 }
571 return true;
572
573 failed:
574 DPRINTF("Invalid type in option '%s' of section type '%s', "
575 "declared type is %s, detected type is %s\n",
576 om->name, sm->type,
577 ucimap_get_type_name(om->type),
578 ucimap_get_type_name(om->detected_type));
579 return false;
580 }
581
582 static void
583 ucimap_count_alloc(struct uci_optmap *om, int *n_alloc, int *n_custom)
584 {
585 if (ucimap_is_alloc(om->type))
586 (*n_alloc)++;
587 else if (ucimap_is_custom(om->type) && om->free)
588 (*n_custom)++;
589 }
590
591 int
592 ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
593 {
594 struct uci_optmap *om;
595 char *section_name;
596 void *section;
597 int n_alloc = 2;
598 int n_alloc_custom = 0;
599 int err;
600
601 sd->map = map;
602 sd->sm = sm;
603
604 ucimap_foreach_option(sm, om) {
605 if (!ucimap_check_optmap_type(sm, om))
606 continue;
607
608 if (ucimap_is_list(om->type)) {
609 union ucimap_data *data;
610 struct uci_element *e;
611 int n_elements = 0;
612 int n_elements_alloc = 0;
613 int n_elements_custom = 0;
614 int size;
615
616 data = ucimap_get_data(sd, om);
617 uci_foreach_element(&s->options, e) {
618 struct uci_option *o = uci_to_option(e);
619 struct uci_element *tmp;
620
621 if (strcmp(e->name, om->name) != 0)
622 continue;
623
624 if (o->type == UCI_TYPE_LIST) {
625 uci_foreach_element(&o->v.list, tmp) {
626 ucimap_count_alloc(om, &n_elements_alloc, &n_elements_custom);
627 n_elements++;
628 }
629 } else if ((o->type == UCI_TYPE_STRING) &&
630 ucimap_is_list_auto(om->type)) {
631 const char *data = o->v.string;
632 do {
633 while (isspace(*data))
634 data++;
635
636 if (!*data)
637 break;
638
639 n_elements++;
640 ucimap_count_alloc(om, &n_elements_alloc, &n_elements_custom);
641
642 while (*data && !isspace(*data))
643 data++;
644 } while (*data);
645
646 /* for the duplicated data string */
647 if (n_elements)
648 n_alloc++;
649 }
650 break;
651 }
652 /* add one more for the ucimap_list */
653 n_alloc += n_elements_alloc + 1;
654 n_alloc_custom += n_elements_custom;
655 size = sizeof(struct ucimap_list) +
656 n_elements * sizeof(union ucimap_data);
657
658 data->list = malloc(size);
659 if (!data->list)
660 goto error_mem;
661
662 memset(data->list, 0, size);
663 data->list->size = n_elements;
664 } else {
665 ucimap_count_alloc(om, &n_alloc, &n_alloc_custom);
666 }
667 }
668
669 sd->allocmap = calloc(n_alloc, sizeof(struct ucimap_alloc));
670 if (!sd->allocmap)
671 goto error_mem;
672
673 if (n_alloc_custom > 0) {
674 sd->alloc_custom = calloc(n_alloc_custom, sizeof(struct ucimap_alloc_custom));
675 if (!sd->alloc_custom)
676 goto error_mem;
677 }
678
679 section_name = strdup(s->e.name);
680 if (!section_name)
681 goto error_mem;
682
683 sd->section_name = section_name;
684
685 sd->cmap = calloc(1, BITFIELD_SIZE(sm->n_options));
686 if (!sd->cmap)
687 goto error_mem;
688
689 ucimap_add_alloc(sd, (void *)section_name);
690 ucimap_add_alloc(sd, (void *)sd->cmap);
691 ucimap_foreach_option(sm, om) {
692 if (!ucimap_is_list(om->type))
693 continue;
694
695 ucimap_add_alloc(sd, ucimap_get_data(sd, om)->list);
696 }
697
698 section = ucimap_section_ptr(sd);
699 err = sm->init(map, section, s);
700 if (err)
701 goto error;
702
703 if (map->parsed) {
704 ucimap_add_section(sd);
705 } else {
706 ucimap_add_section_list(map, sd);
707 }
708
709 err = ucimap_parse_options(map, sm, sd, s);
710 if (err)
711 goto error;
712
713 return 0;
714
715 error_mem:
716 if (sd->alloc_custom)
717 free(sd->alloc_custom);
718 if (sd->allocmap)
719 free(sd->allocmap);
720 free(sd);
721 return UCI_ERR_MEM;
722
723 error:
724 ucimap_free_section(map, sd);
725 return err;
726 }
727
728 static int
729 ucimap_fill_ptr(struct uci_ptr *ptr, struct uci_section *s, const char *option)
730 {
731 struct uci_package *p = s->package;
732
733 memset(ptr, 0, sizeof(struct uci_ptr));
734
735 ptr->package = p->e.name;
736 ptr->p = p;
737
738 ptr->section = s->e.name;
739 ptr->s = s;
740
741 ptr->option = option;
742 return uci_lookup_ptr(p->ctx, ptr, NULL, false);
743 }
744
745 void
746 ucimap_set_changed(struct ucimap_section_data *sd, void *field)
747 {
748 void *section = ucimap_section_ptr(sd);
749 struct uci_sectionmap *sm = sd->sm;
750 struct uci_optmap *om;
751 int ofs = (char *)field - (char *)section;
752 int i = 0;
753
754 ucimap_foreach_option(sm, om) {
755 if (om->offset == ofs) {
756 SET_BIT(sd->cmap, i);
757 break;
758 }
759 i++;
760 }
761 }
762
763 static char *
764 ucimap_data_to_string(struct ucimap_section_data *sd, struct uci_optmap *om, union ucimap_data *data)
765 {
766 static char buf[32];
767 char *str = NULL;
768
769 switch(om->type & UCIMAP_SUBTYPE) {
770 case UCIMAP_STRING:
771 str = data->s;
772 break;
773 case UCIMAP_INT:
774 sprintf(buf, "%d", data->i);
775 str = buf;
776 break;
777 case UCIMAP_BOOL:
778 sprintf(buf, "%d", !!data->b);
779 str = buf;
780 break;
781 case UCIMAP_SECTION:
782 if (data->ptr)
783 str = (char *) ucimap_ptr_section(om->data.sm, data->ptr)->section_name;
784 else
785 str = "";
786 break;
787 case UCIMAP_CUSTOM:
788 break;
789 default:
790 return NULL;
791 }
792
793 if (om->format) {
794 if (om->format(ucimap_section_ptr(sd), om, data, &str) < 0)
795 return NULL;
796
797 if (!str)
798 str = "";
799 }
800 return str;
801 }
802
803 int
804 ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd)
805 {
806 struct uci_sectionmap *sm = sd->sm;
807 struct uci_section *s = NULL;
808 struct uci_optmap *om;
809 struct uci_element *e;
810 struct uci_ptr ptr;
811 int i = 0;
812 int ret;
813
814 uci_foreach_element(&p->sections, e) {
815 if (!strcmp(e->name, sd->section_name)) {
816 s = uci_to_section(e);
817 break;
818 }
819 }
820 if (!s)
821 return UCI_ERR_NOTFOUND;
822
823 ucimap_foreach_option(sm, om) {
824 union ucimap_data *data;
825
826 i++;
827 data = ucimap_get_data(sd, om);
828 if (!TEST_BIT(sd->cmap, i - 1))
829 continue;
830
831 ucimap_fill_ptr(&ptr, s, om->name);
832 if (ucimap_is_list(om->type)) {
833 struct ucimap_list *list = data->list;
834 bool first = true;
835 int j;
836
837 for (j = 0; j < list->n_items; j++) {
838 ptr.value = ucimap_data_to_string(sd, om, &list->item[j]);
839 if (!ptr.value)
840 continue;
841
842 if (first) {
843 ret = uci_set(s->package->ctx, &ptr);
844 first = false;
845 } else {
846 ret = uci_add_list(s->package->ctx, &ptr);
847 }
848 if (ret)
849 return ret;
850 }
851 } else {
852 ptr.value = ucimap_data_to_string(sd, om, data);
853 if (!ptr.value)
854 continue;
855
856 ret = uci_set(s->package->ctx, &ptr);
857 if (ret)
858 return ret;
859 }
860
861 CLR_BIT(sd->cmap, i - 1);
862 }
863
864 return 0;
865 }
866
867 void
868 ucimap_parse(struct uci_map *map, struct uci_package *pkg)
869 {
870 struct uci_element *e;
871 struct ucimap_section_data *sd, **sd_tail;
872 struct ucimap_fixup *f;
873 int i;
874
875 sd_tail = map->sdata_tail;
876 map->parsed = false;
877 map->sdata_tail = &map->pending;
878 uci_foreach_element(&pkg->sections, e) {
879 struct uci_section *s = uci_to_section(e);
880
881 for (i = 0; i < map->n_sections; i++) {
882 struct uci_sectionmap *sm = map->sections[i];
883 struct ucimap_section_data *sd;
884
885 if (strcmp(s->type, map->sections[i]->type) != 0)
886 continue;
887
888 if (sm->alloc) {
889 sd = sm->alloc(map, sm, s);
890 memset(sd, 0, sizeof(struct ucimap_section_data));
891 } else {
892 sd = malloc(sm->alloc_len);
893 memset(sd, 0, sm->alloc_len);
894 sd = ucimap_ptr_section(sm, sd);
895 }
896 if (!sd)
897 continue;
898
899 ucimap_parse_section(map, sm, sd, s);
900 }
901 }
902 if (!map->parsed) {
903 map->parsed = true;
904 map->sdata_tail = sd_tail;
905 }
906
907 f = map->fixup;
908 while (f) {
909 struct ucimap_fixup *next = f->next;
910 ucimap_handle_fixup(map, f);
911 free(f);
912 f = next;
913 }
914 map->fixup_tail = &map->fixup;
915 map->fixup = NULL;
916
917 sd = map->pending;
918 while (sd) {
919 struct ucimap_section_data *next = sd->next;
920 ucimap_add_section(sd);
921 sd = next;
922 }
923 map->pending = NULL;
924 }