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