ucimap: free memory allocated for fixups
[project/uci.git] / list.c
1 /*
2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 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 General Public License for more details.
13 */
14
15 /* initialize a list head/item */
16 static inline void uci_list_init(struct uci_list *ptr)
17 {
18 ptr->prev = ptr;
19 ptr->next = ptr;
20 }
21
22 /* inserts a new list entry after a given entry */
23 static inline void uci_list_insert(struct uci_list *list, struct uci_list *ptr)
24 {
25 list->next->prev = ptr;
26 ptr->prev = list;
27 ptr->next = list->next;
28 list->next = ptr;
29 }
30
31 /* inserts a new list entry at the tail of the list */
32 static inline void uci_list_add(struct uci_list *head, struct uci_list *ptr)
33 {
34 /* NB: head->prev points at the tail */
35 uci_list_insert(head->prev, ptr);
36 }
37
38 static inline void uci_list_del(struct uci_list *ptr)
39 {
40 struct uci_list *next, *prev;
41
42 next = ptr->next;
43 prev = ptr->prev;
44
45 prev->next = next;
46 next->prev = prev;
47
48 uci_list_init(ptr);
49 }
50
51 static inline void uci_list_set_pos(struct uci_list *head, struct uci_list *ptr, int pos)
52 {
53 struct uci_list *new_head = head;
54 struct uci_element *p = NULL;
55
56 uci_list_del(ptr);
57 uci_foreach_element(head, p) {
58 new_head = &p->list;
59 if (pos-- <= 0)
60 break;
61 }
62 uci_list_add(new_head, ptr);
63 }
64
65 static inline void uci_list_fixup(struct uci_list *ptr)
66 {
67 ptr->prev->next = ptr;
68 ptr->next->prev = ptr;
69 }
70
71 /*
72 * uci_alloc_generic allocates a new uci_element with payload
73 * payload is appended to the struct to save memory and reduce fragmentation
74 */
75 static struct uci_element *
76 uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
77 {
78 struct uci_element *e;
79 int datalen = size;
80 void *ptr;
81
82 ptr = uci_malloc(ctx, datalen);
83 e = (struct uci_element *) ptr;
84 e->type = type;
85 if (name) {
86 UCI_TRAP_SAVE(ctx, error);
87 e->name = uci_strdup(ctx, name);
88 UCI_TRAP_RESTORE(ctx);
89 }
90 uci_list_init(&e->list);
91 goto done;
92
93 error:
94 free(ptr);
95 UCI_THROW(ctx, ctx->err);
96
97 done:
98 return e;
99 }
100
101 static void
102 uci_free_element(struct uci_element *e)
103 {
104 if (e->name)
105 free(e->name);
106 if (!uci_list_empty(&e->list))
107 uci_list_del(&e->list);
108 free(e);
109 }
110
111 static struct uci_option *
112 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
113 {
114 struct uci_package *p = s->package;
115 struct uci_context *ctx = p->ctx;
116 struct uci_option *o;
117
118 o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
119 o->type = UCI_TYPE_STRING;
120 o->v.string = uci_dataptr(o);
121 o->section = s;
122 strcpy(o->v.string, value);
123 uci_list_add(&s->options, &o->e.list);
124
125 return o;
126 }
127
128 static inline void
129 uci_free_option(struct uci_option *o)
130 {
131 struct uci_element *e, *tmp;
132
133 switch(o->type) {
134 case UCI_TYPE_STRING:
135 if ((o->v.string != uci_dataptr(o)) &&
136 (o->v.string != NULL))
137 free(o->v.string);
138 break;
139 case UCI_TYPE_LIST:
140 uci_foreach_element_safe(&o->v.list, tmp, e) {
141 uci_free_element(e);
142 }
143 break;
144 default:
145 break;
146 }
147 uci_free_element(&o->e);
148 }
149
150 static struct uci_option *
151 uci_alloc_list(struct uci_section *s, const char *name)
152 {
153 struct uci_package *p = s->package;
154 struct uci_context *ctx = p->ctx;
155 struct uci_option *o;
156
157 o = uci_alloc_element(ctx, option, name, 0);
158 o->type = UCI_TYPE_LIST;
159 o->section = s;
160 uci_list_init(&o->v.list);
161 uci_list_add(&s->options, &o->e.list);
162
163 return o;
164 }
165
166 /* fix up an unnamed section, e.g. after adding options to it */
167 static void uci_fixup_section(struct uci_context *ctx, struct uci_section *s)
168 {
169 unsigned int hash = ~0;
170 struct uci_element *e;
171 char buf[16];
172
173 if (!s || s->e.name)
174 return;
175
176 /*
177 * Generate a name for unnamed sections. This is used as reference
178 * when locating or updating the section from apps/scripts.
179 * To make multiple concurrent versions somewhat safe for updating,
180 * the name is generated from a hash of its type and name/value
181 * pairs of its option, and it is prefixed by a counter value.
182 * If the order of the unnamed sections changes for some reason,
183 * updates to them will be rejected.
184 */
185 hash = djbhash(hash, s->type);
186 uci_foreach_element(&s->options, e) {
187 struct uci_option *o;
188 hash = djbhash(hash, e->name);
189 o = uci_to_option(e);
190 switch(o->type) {
191 case UCI_TYPE_STRING:
192 hash = djbhash(hash, o->v.string);
193 break;
194 default:
195 break;
196 }
197 }
198 sprintf(buf, "cfg%02x%04x", ++s->package->n_section, hash % (1 << 16));
199 s->e.name = uci_strdup(ctx, buf);
200 }
201
202 static struct uci_section *
203 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
204 {
205 struct uci_context *ctx = p->ctx;
206 struct uci_section *s;
207
208 if (name && !name[0])
209 name = NULL;
210
211 s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
212 uci_list_init(&s->options);
213 s->type = uci_dataptr(s);
214 s->package = p;
215 strcpy(s->type, type);
216 if (name == NULL)
217 s->anonymous = true;
218 p->n_section++;
219
220 uci_list_add(&p->sections, &s->e.list);
221
222 return s;
223 }
224
225 static void
226 uci_free_section(struct uci_section *s)
227 {
228 struct uci_element *o, *tmp;
229
230 uci_foreach_element_safe(&s->options, tmp, o) {
231 uci_free_option(uci_to_option(o));
232 }
233 if ((s->type != uci_dataptr(s)) &&
234 (s->type != NULL))
235 free(s->type);
236 uci_free_element(&s->e);
237 }
238
239 __plugin struct uci_package *
240 uci_alloc_package(struct uci_context *ctx, const char *name)
241 {
242 struct uci_package *p;
243
244 p = uci_alloc_element(ctx, package, name, 0);
245 p->ctx = ctx;
246 uci_list_init(&p->sections);
247 uci_list_init(&p->history);
248 uci_list_init(&p->saved_history);
249 return p;
250 }
251
252 static void
253 uci_free_package(struct uci_package **package)
254 {
255 struct uci_element *e, *tmp;
256 struct uci_package *p = *package;
257
258 if(!p)
259 return;
260
261 if (p->path)
262 free(p->path);
263 uci_foreach_element_safe(&p->sections, tmp, e) {
264 uci_free_section(uci_to_section(e));
265 }
266 uci_foreach_element_safe(&p->history, tmp, e) {
267 uci_free_history(uci_to_history(e));
268 }
269 uci_foreach_element_safe(&p->saved_history, tmp, e) {
270 uci_free_history(uci_to_history(e));
271 }
272 uci_free_element(&p->e);
273 *package = NULL;
274 }
275
276 static void
277 uci_free_any(struct uci_element **e)
278 {
279 switch((*e)->type) {
280 case UCI_TYPE_SECTION:
281 uci_free_section(uci_to_section(*e));
282 break;
283 case UCI_TYPE_OPTION:
284 uci_free_option(uci_to_option(*e));
285 break;
286 default:
287 break;
288 }
289 *e = NULL;
290 }
291
292 static inline struct uci_element *
293 uci_lookup_list(struct uci_list *list, const char *name)
294 {
295 struct uci_element *e;
296
297 uci_foreach_element(list, e) {
298 if (!strcmp(e->name, name))
299 return e;
300 }
301 return NULL;
302 }
303
304 static struct uci_element *
305 uci_lookup_ext_section(struct uci_context *ctx, struct uci_ptr *ptr)
306 {
307 char *idxstr, *t, *section, *name;
308 struct uci_element *e = NULL;
309 struct uci_section *s;
310 int idx, c;
311
312 section = uci_strdup(ctx, ptr->section);
313 name = idxstr = section + 1;
314
315 if (section[0] != '@')
316 goto error;
317
318 /* parse the section index part */
319 idxstr = strchr(idxstr, '[');
320 if (!idxstr)
321 goto error;
322 *idxstr = 0;
323 idxstr++;
324
325 t = strchr(idxstr, ']');
326 if (!t)
327 goto error;
328 if (t[1] != 0)
329 goto error;
330 *t = 0;
331
332 t = NULL;
333 idx = strtol(idxstr, &t, 10);
334 if (t && *t)
335 goto error;
336
337 if (!*name)
338 name = NULL;
339 else if (!uci_validate_type(name))
340 goto error;
341
342 /* if the given index is negative, it specifies the section number from
343 * the end of the list */
344 if (idx < 0) {
345 c = 0;
346 uci_foreach_element(&ptr->p->sections, e) {
347 s = uci_to_section(e);
348 if (name && (strcmp(s->type, name) != 0))
349 continue;
350
351 c++;
352 }
353 idx += c;
354 }
355
356 c = 0;
357 uci_foreach_element(&ptr->p->sections, e) {
358 s = uci_to_section(e);
359 if (name && (strcmp(s->type, name) != 0))
360 continue;
361
362 if (idx == c)
363 goto done;
364 c++;
365 }
366 e = NULL;
367 goto done;
368
369 error:
370 e = NULL;
371 memset(ptr, 0, sizeof(struct uci_ptr));
372 UCI_THROW(ctx, UCI_ERR_INVAL);
373 done:
374 free(section);
375 if (e)
376 ptr->section = e->name;
377 return e;
378 }
379
380 int
381 uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
382 {
383 struct uci_element *e;
384
385 UCI_HANDLE_ERR(ctx);
386 UCI_ASSERT(ctx, ptr != NULL);
387
388 if (str)
389 UCI_INTERNAL(uci_parse_ptr, ctx, ptr, str);
390
391 ptr->flags |= UCI_LOOKUP_DONE;
392
393 /* look up the package first */
394 e = uci_lookup_list(&ctx->root, ptr->package);
395 if (!e) {
396 UCI_INTERNAL(uci_load, ctx, ptr->package, &ptr->p);
397 if (!ptr->p)
398 goto notfound;
399 ptr->last = &ptr->p->e;
400 } else {
401 ptr->p = uci_to_package(e);
402 ptr->last = e;
403 }
404
405 if (!ptr->section)
406 goto complete;
407
408 /* if the section name validates as a regular name, pass through
409 * to the regular uci_lookup function call */
410 if (ptr->flags & UCI_LOOKUP_EXTENDED)
411 e = uci_lookup_ext_section(ctx, ptr);
412 else
413 e = uci_lookup_list(&ptr->p->sections, ptr->section);
414
415 if (!e)
416 goto abort;
417
418 ptr->last = e;
419 ptr->s = uci_to_section(e);
420
421 if (ptr->option) {
422 e = uci_lookup_list(&ptr->s->options, ptr->option);
423 if (!e)
424 goto abort;
425
426 ptr->o = uci_to_option(e);
427 ptr->last = e;
428 }
429
430 complete:
431 ptr->flags |= UCI_LOOKUP_COMPLETE;
432 abort:
433 return 0;
434
435 notfound:
436 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
437 return 0;
438 }
439
440 int
441 uci_fill_ptr(struct uci_context *ctx, struct uci_ptr *ptr, struct uci_element *e, bool complete)
442 {
443 UCI_HANDLE_ERR(ctx);
444 UCI_ASSERT(ctx, ptr != NULL);
445 UCI_ASSERT(ctx, e != NULL);
446
447 memset(ptr, 0, sizeof(struct uci_ptr));
448 switch(e->type) {
449 case UCI_TYPE_OPTION:
450 ptr->o = uci_to_option(e);
451 goto fill_option;
452 case UCI_TYPE_SECTION:
453 ptr->s = uci_to_section(e);
454 goto fill_section;
455 case UCI_TYPE_PACKAGE:
456 ptr->p = uci_to_package(e);
457 goto fill_package;
458 default:
459 UCI_THROW(ctx, UCI_ERR_INVAL);
460 }
461
462 fill_option:
463 ptr->option = ptr->o->e.name;
464 ptr->s = ptr->o->section;
465 fill_section:
466 ptr->section = ptr->s->e.name;
467 ptr->p = ptr->s->package;
468 fill_package:
469 ptr->package = ptr->p->e.name;
470
471 ptr->flags |= UCI_LOOKUP_DONE;
472 if (complete)
473 ptr->flags |= UCI_LOOKUP_COMPLETE;
474
475 return 0;
476 }
477
478 static struct uci_element *
479 expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete)
480 {
481 UCI_ASSERT(ctx, ptr != NULL);
482
483 if (!(ptr->flags & UCI_LOOKUP_DONE))
484 UCI_INTERNAL(uci_lookup_ptr, ctx, ptr, NULL, 1);
485 if (complete && !(ptr->flags & UCI_LOOKUP_COMPLETE))
486 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
487 UCI_ASSERT(ctx, ptr->p != NULL);
488
489 /* fill in missing string info */
490 if (ptr->p && !ptr->package)
491 ptr->package = ptr->p->e.name;
492 if (ptr->s && !ptr->section)
493 ptr->section = ptr->s->e.name;
494 if (ptr->o && !ptr->option)
495 ptr->option = ptr->o->e.name;
496
497 if (ptr->o)
498 return &ptr->o->e;
499 if (ptr->s)
500 return &ptr->s->e;
501 if (ptr->p)
502 return &ptr->p->e;
503 else
504 return NULL;
505 }
506
507 static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, bool internal)
508 {
509 struct uci_element *e;
510 struct uci_package *p;
511
512 p = ptr->p;
513 if (!internal && p->has_history)
514 uci_add_history(ctx, &p->history, UCI_CMD_LIST_ADD, ptr->section, ptr->option, ptr->value);
515
516 e = uci_alloc_generic(ctx, UCI_TYPE_ITEM, ptr->value, sizeof(struct uci_option));
517 uci_list_add(&ptr->o->v.list, &e->list);
518 }
519
520 int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
521 {
522 /* NB: UCI_INTERNAL use means without history tracking */
523 bool internal = ctx->internal;
524 struct uci_element *e;
525 struct uci_package *p;
526 char *n;
527
528 UCI_HANDLE_ERR(ctx);
529
530 e = expand_ptr(ctx, ptr, true);
531 p = ptr->p;
532
533 UCI_ASSERT(ctx, ptr->s);
534 UCI_ASSERT(ctx, ptr->value);
535
536 if (!internal && p->has_history)
537 uci_add_history(ctx, &p->history, UCI_CMD_RENAME, ptr->section, ptr->option, ptr->value);
538
539 n = uci_strdup(ctx, ptr->value);
540 if (e->name)
541 free(e->name);
542 e->name = n;
543
544 if (e->type == UCI_TYPE_SECTION)
545 uci_to_section(e)->anonymous = false;
546
547 return 0;
548 }
549
550 int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos)
551 {
552 struct uci_package *p = s->package;
553 char order[32];
554
555 UCI_HANDLE_ERR(ctx);
556
557 uci_list_set_pos(&s->package->sections, &s->e.list, pos);
558 if (!ctx->internal && p->has_history) {
559 sprintf(order, "%d", pos);
560 uci_add_history(ctx, &p->history, UCI_CMD_REORDER, s->e.name, NULL, order);
561 }
562
563 return 0;
564 }
565
566 int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
567 {
568 bool internal = ctx->internal;
569 struct uci_section *s;
570
571 UCI_HANDLE_ERR(ctx);
572 UCI_ASSERT(ctx, p != NULL);
573 s = uci_alloc_section(p, type, NULL);
574 uci_fixup_section(ctx, s);
575 *res = s;
576 if (!internal && p->has_history)
577 uci_add_history(ctx, &p->history, UCI_CMD_ADD, s->e.name, NULL, type);
578
579 return 0;
580 }
581
582 int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr)
583 {
584 /* NB: pass on internal flag to uci_del_element */
585 bool internal = ctx->internal;
586 struct uci_package *p;
587 struct uci_element *e;
588
589 UCI_HANDLE_ERR(ctx);
590
591 e = expand_ptr(ctx, ptr, true);
592 p = ptr->p;
593
594 UCI_ASSERT(ctx, ptr->s);
595
596 if (!internal && p->has_history)
597 uci_add_history(ctx, &p->history, UCI_CMD_REMOVE, ptr->section, ptr->option, NULL);
598
599 uci_free_any(&e);
600
601 if (ptr->option)
602 ptr->o = NULL;
603 else if (ptr->section)
604 ptr->s = NULL;
605
606 return 0;
607 }
608
609 int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr)
610 {
611 /* NB: UCI_INTERNAL use means without history tracking */
612 bool internal = ctx->internal;
613 struct uci_option *prev = NULL;
614 const char *value2 = NULL;
615
616 UCI_HANDLE_ERR(ctx);
617
618 expand_ptr(ctx, ptr, false);
619 UCI_ASSERT(ctx, ptr->s);
620 UCI_ASSERT(ctx, ptr->value);
621
622 if (ptr->o) {
623 switch (ptr->o->type) {
624 case UCI_TYPE_STRING:
625 /* we already have a string value, convert that to a list */
626 prev = ptr->o;
627 value2 = ptr->value;
628 ptr->value = ptr->o->v.string;
629 break;
630 case UCI_TYPE_LIST:
631 uci_add_element_list(ctx, ptr, internal);
632 return 0;
633 default:
634 UCI_THROW(ctx, UCI_ERR_INVAL);
635 break;
636 }
637 }
638
639 ptr->o = uci_alloc_list(ptr->s, ptr->option);
640 if (prev) {
641 uci_add_element_list(ctx, ptr, true);
642 uci_free_option(prev);
643 ptr->value = value2;
644 }
645 uci_add_element_list(ctx, ptr, internal);
646
647 return 0;
648 }
649
650 int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
651 {
652 /* NB: UCI_INTERNAL use means without history tracking */
653 bool internal = ctx->internal;
654
655 UCI_HANDLE_ERR(ctx);
656 expand_ptr(ctx, ptr, false);
657 UCI_ASSERT(ctx, ptr->value);
658 UCI_ASSERT(ctx, ptr->s || (!ptr->option && ptr->section));
659 if (!ptr->option && ptr->value[0]) {
660 UCI_ASSERT(ctx, uci_validate_type(ptr->value));
661 }
662
663 if (!ptr->o && ptr->s && ptr->option) {
664 struct uci_element *e;
665 e = uci_lookup_list(&ptr->s->options, ptr->option);
666 if (e)
667 ptr->o = uci_to_option(e);
668 }
669 if (!ptr->value[0]) {
670 /* if setting a nonexistant option/section to a nonexistant value,
671 * exit without errors */
672 if (!(ptr->flags & UCI_LOOKUP_COMPLETE))
673 return 0;
674
675 return uci_delete(ctx, ptr);
676 } else if (!ptr->o && ptr->option) { /* new option */
677 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
678 ptr->last = &ptr->o->e;
679 } else if (!ptr->s && ptr->section) { /* new section */
680 ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section);
681 ptr->last = &ptr->s->e;
682 } else if (ptr->o && ptr->option) { /* update option */
683 if ((ptr->o->type == UCI_TYPE_STRING) &&
684 !strcmp(ptr->o->v.string, ptr->value))
685 return 0;
686 uci_free_option(ptr->o);
687 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
688 ptr->last = &ptr->o->e;
689 } else if (ptr->s && ptr->section) { /* update section */
690 char *s = uci_strdup(ctx, ptr->value);
691
692 if (ptr->s->type == uci_dataptr(ptr->s)) {
693 ptr->last = NULL;
694 ptr->last = uci_realloc(ctx, ptr->s, sizeof(struct uci_section));
695 ptr->s = uci_to_section(ptr->last);
696 uci_list_fixup(&ptr->s->e.list);
697 } else {
698 free(ptr->s->type);
699 }
700 ptr->s->type = s;
701 } else {
702 UCI_THROW(ctx, UCI_ERR_INVAL);
703 }
704
705 if (!internal && ptr->p->has_history)
706 uci_add_history(ctx, &ptr->p->history, UCI_CMD_CHANGE, ptr->section, ptr->option, ptr->value);
707
708 return 0;
709 }
710
711 int uci_unload(struct uci_context *ctx, struct uci_package *p)
712 {
713 UCI_HANDLE_ERR(ctx);
714 UCI_ASSERT(ctx, p != NULL);
715
716 uci_free_package(&p);
717 return 0;
718 }
719