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