66e83ec0be51151defd8475d0e5d99608a78d4a2
[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 #include <glob.h>
16
17 /* initialize a list head/item */
18 static inline void uci_list_init(struct uci_list *ptr)
19 {
20         ptr->prev = ptr;
21         ptr->next = ptr;
22 }
23
24 /* inserts a new list entry after a given entry */
25 static inline void uci_list_insert(struct uci_list *list, struct uci_list *ptr)
26 {
27         list->next->prev = ptr;
28         ptr->prev = list;
29         ptr->next = list->next;
30         list->next = ptr;
31 }
32
33 /* inserts a new list entry at the tail of the list */
34 static inline void uci_list_add(struct uci_list *head, struct uci_list *ptr)
35 {
36         /* NB: head->prev points at the tail */
37         uci_list_insert(head->prev, ptr);
38 }
39
40 static inline void uci_list_del(struct uci_list *ptr)
41 {
42         struct uci_list *next, *prev;
43
44         next = ptr->next;
45         prev = ptr->prev;
46
47         prev->next = next;
48         next->prev = prev;
49
50         uci_list_init(ptr);
51 }
52
53 /* 
54  * uci_alloc_generic allocates a new uci_element with payload
55  * payload is appended to the struct to save memory and reduce fragmentation
56  */
57 static struct uci_element *
58 uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
59 {
60         struct uci_element *e;
61         int datalen = size;
62         void *ptr;
63
64         ptr = uci_malloc(ctx, datalen);
65         e = (struct uci_element *) ptr;
66         e->type = type;
67         if (name) {
68                 UCI_TRAP_SAVE(ctx, error);
69                 e->name = uci_strdup(ctx, name);
70                 UCI_TRAP_RESTORE(ctx);
71         }
72         uci_list_init(&e->list);
73         goto done;
74
75 error:
76         free(ptr);
77         UCI_THROW(ctx, ctx->errno);
78
79 done:
80         return e;
81 }
82
83 static void
84 uci_free_element(struct uci_element *e)
85 {
86         if (e->name)
87                 free(e->name);
88         if (!uci_list_empty(&e->list))
89                 uci_list_del(&e->list);
90         free(e);
91 }
92
93 static struct uci_option *
94 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
95 {
96         struct uci_package *p = s->package;
97         struct uci_context *ctx = p->ctx;
98         struct uci_option *o;
99
100         o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
101         o->value = uci_dataptr(o);
102         o->section = s;
103         strcpy(o->value, value);
104         uci_list_add(&s->options, &o->e.list);
105
106         return o;
107 }
108
109 static inline void
110 uci_free_option(struct uci_option *o)
111 {
112         if ((o->value != uci_dataptr(o)) &&
113                 (o->value != NULL))
114                 free(o->value);
115         uci_free_element(&o->e);
116 }
117
118 static struct uci_section *
119 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
120 {
121         struct uci_context *ctx = p->ctx;
122         struct uci_section *s;
123
124         if (name && !name[0])
125                 name = NULL;
126
127         s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
128         uci_list_init(&s->options);
129         s->type = uci_dataptr(s);
130         s->package = p;
131         strcpy(s->type, type);
132         if (name == NULL)
133                 s->anonymous = true;
134
135         uci_list_add(&p->sections, &s->e.list);
136
137         return s;
138 }
139
140 static void
141 uci_free_section(struct uci_section *s)
142 {
143         struct uci_element *o, *tmp;
144
145         uci_foreach_element_safe(&s->options, tmp, o) {
146                 uci_free_option(uci_to_option(o));
147         }
148         if ((s->type != uci_dataptr(s)) &&
149                 (s->type != NULL))
150                 free(s->type);
151         uci_free_element(&s->e);
152 }
153
154 /* record a change that was done to a package */
155 static void
156 uci_add_history(struct uci_context *ctx, struct uci_package *p, int cmd, char *section, char *option, char *value)
157 {
158         struct uci_history *h;
159         int size = strlen(section) + 1;
160         char *ptr;
161
162         if (!p->confdir)
163                 return;
164
165         if (value)
166                 size += strlen(section) + 1;
167
168         h = uci_alloc_element(ctx, history, option, size);
169         ptr = uci_dataptr(h);
170         h->cmd = cmd;
171         h->section = strcpy(ptr, section);
172         if (value) {
173                 ptr += strlen(ptr) + 1;
174                 h->value = strcpy(ptr, value);
175         }
176         uci_list_add(&p->history, &h->e.list);
177 }
178
179 static void
180 uci_free_history(struct uci_history *h)
181 {
182         if (!h)
183                 return;
184         if ((h->section != NULL) &&
185                 (h->section != uci_dataptr(h))) {
186                 free(h->section);
187                 free(h->value);
188         }
189         uci_free_element(&h->e);
190 }
191
192 static struct uci_package *
193 uci_alloc_package(struct uci_context *ctx, const char *name)
194 {
195         struct uci_package *p;
196
197         p = uci_alloc_element(ctx, package, name, 0);
198         p->ctx = ctx;
199         uci_list_init(&p->sections);
200         uci_list_init(&p->history);
201         return p;
202 }
203
204 static void
205 uci_free_package(struct uci_package **package)
206 {
207         struct uci_element *e, *tmp;
208         struct uci_package *p = *package;
209
210         if(!p)
211                 return;
212
213         if (p->path)
214                 free(p->path);
215         uci_foreach_element_safe(&p->sections, tmp, e) {
216                 uci_free_section(uci_to_section(e));
217         }
218         uci_foreach_element_safe(&p->history, tmp, e) {
219                 uci_free_history(uci_to_history(e));
220         }
221         uci_free_element(&p->e);
222         *package = NULL;
223 }
224
225 static struct uci_element *uci_lookup_list(struct uci_context *ctx, struct uci_list *list, const char *name)
226 {
227         struct uci_element *e;
228
229         uci_foreach_element(list, e) {
230                 if (!strcmp(e->name, name))
231                         return e;
232         }
233         return NULL;
234 }
235
236 int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *p, char *section, char *option)
237 {
238         struct uci_element *e;
239         struct uci_section *s;
240
241         UCI_HANDLE_ERR(ctx);
242         UCI_ASSERT(ctx, res != NULL);
243         UCI_ASSERT(ctx, p != NULL);
244         UCI_ASSERT(ctx, section != NULL);
245
246         e = uci_lookup_list(ctx, &p->sections, section);
247         if (!e)
248                 goto notfound;
249
250         if (option) {
251                 s = uci_to_section(e);
252                 e = uci_lookup_list(ctx, &s->options, option);
253         }
254
255         *res = e;
256         return 0;
257
258 notfound:
259         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
260         return 0;
261 }
262
263 int uci_del_element(struct uci_context *ctx, struct uci_element *e)
264 {
265         /* NB: UCI_INTERNAL use means without history tracking */
266         bool internal = ctx->internal;
267         struct uci_package *p = NULL;
268         struct uci_section *s = NULL;
269         struct uci_option *o = NULL;
270         struct uci_element *i, *tmp;
271         char *option = NULL;
272
273         UCI_HANDLE_ERR(ctx);
274         UCI_ASSERT(ctx, e != NULL);
275
276         switch(e->type) {
277         case UCI_TYPE_SECTION:
278                 s = uci_to_section(e);
279                 uci_foreach_element_safe(&s->options, tmp, i) {
280                         uci_del_element(ctx, i);
281                 }
282                 break;
283         case UCI_TYPE_OPTION:
284                 o = uci_to_option(e);
285                 s = o->section;
286                 p = s->package;
287                 option = e->name;
288                 break;
289         default:
290                 UCI_THROW(ctx, UCI_ERR_INVAL);
291                 break;
292         }
293
294         p = s->package;
295         if (!internal)
296                 uci_add_history(ctx, p, UCI_CMD_REMOVE, s->e.name, option, NULL);
297
298         switch(e->type) {
299         case UCI_TYPE_SECTION:
300                 uci_free_section(s);
301                 break;
302         case UCI_TYPE_OPTION:
303                 uci_free_option(o);
304                 break;
305         default:
306                 break;
307         }
308         return 0;
309 }
310
311 int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, char *value)
312 {
313         /* NB: UCI_INTERNAL use means without history tracking */
314         bool internal = ctx->internal;
315         struct uci_list *list;
316         struct uci_element *e;
317         struct uci_package *p;
318         struct uci_section *s;
319         char *section;
320         char *option;
321         char *str;
322         int size;
323
324         UCI_HANDLE_ERR(ctx);
325         UCI_ASSERT(ctx, value != NULL);
326         UCI_ASSERT(ctx, element != NULL);
327         UCI_ASSERT(ctx, *element != NULL);
328
329         /* what the 'value' of an element means depends on the type
330          * for a section, the 'value' means its type
331          * for an option, the 'value' means its value string
332          * when changing the value, shrink the element to its actual size
333          * (it may have been allocated with a bigger size, to include
334          *  its buffer)
335          * then duplicate the string passed on the command line and
336          * insert it into the structure.
337          */
338         e = *element;
339         list = e->list.prev;
340         switch(e->type) {
341         case UCI_TYPE_SECTION:
342                 size = sizeof(struct uci_section);
343                 s = uci_to_section(e);
344                 section = e->name;
345                 option = NULL;
346                 break;
347         case UCI_TYPE_OPTION:
348                 size = sizeof(struct uci_option);
349                 s = uci_to_option(e)->section;
350                 section = s->e.name;
351                 option = e->name;
352                 break;
353         default:
354                 UCI_THROW(ctx, UCI_ERR_INVAL);
355                 return 0;
356         }
357         p = s->package;
358         if (!internal)
359                 uci_add_history(ctx, p, UCI_CMD_CHANGE, section, option, value);
360
361         uci_list_del(&e->list);
362         e = uci_realloc(ctx, e, size);
363         str = uci_strdup(ctx, value);
364         uci_list_insert(list, &e->list);
365         *element = e;
366
367         switch(e->type) {
368         case UCI_TYPE_SECTION:
369                 uci_to_section(e)->type = str;
370                 break;
371         case UCI_TYPE_OPTION:
372                 uci_to_option(e)->value = str;
373                 break;
374         default:
375                 break;
376         }
377
378         return 0;
379 }
380
381 int uci_rename(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *name)
382 {
383         /* NB: UCI_INTERNAL use means without history tracking */
384         bool internal = ctx->internal;
385         struct uci_element *e;
386
387         UCI_HANDLE_ERR(ctx);
388         UCI_ASSERT(ctx, p != NULL);
389         UCI_ASSERT(ctx, section != NULL);
390
391         UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
392
393         if (!internal)
394                 uci_add_history(ctx, p, UCI_CMD_RENAME, section, option, name);
395
396         name = uci_strdup(ctx, name);
397         if (e->name)
398                 free(e->name);
399         e->name = name;
400
401         return 0;
402 }
403
404
405 int uci_delete(struct uci_context *ctx, struct uci_package *p, char *section, char *option)
406 {
407         /* NB: pass on internal flag to uci_del_element */
408         bool internal = ctx->internal;
409         struct uci_element *e;
410
411         UCI_HANDLE_ERR(ctx);
412         UCI_ASSERT(ctx, p != NULL);
413         UCI_ASSERT(ctx, section != NULL);
414
415         UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
416
417         if (!internal)
418                 return uci_del_element(ctx, e);
419         UCI_INTERNAL(uci_del_element, ctx, e);
420
421         return 0;
422 }
423
424 int uci_set(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *value)
425 {
426         /* NB: UCI_INTERNAL use means without history tracking */
427         bool internal = ctx->internal;
428         struct uci_element *e = NULL;
429         struct uci_section *s = NULL;
430         struct uci_option *o = NULL;
431
432         UCI_HANDLE_ERR(ctx);
433         UCI_ASSERT(ctx, p != NULL);
434         UCI_ASSERT(ctx, section != NULL);
435         UCI_ASSERT(ctx, value != NULL);
436
437         /*
438          * look up the package, section and option (if set)
439          * if the section/option is to be modified and it is not found
440          * create a new element in the appropriate list
441          */
442         e = uci_lookup_list(ctx, &p->sections, section);
443         if (!e)
444                 goto notfound;
445
446         s = uci_to_section(e);
447         if (ctx->pctx)
448                 ctx->pctx->section = s;
449
450         if (option) {
451                 e = uci_lookup_list(ctx, &s->options, option);
452                 if (!e)
453                         goto notfound;
454                 o = uci_to_option(e);
455         }
456
457         /* 
458          * no unknown element was supplied, assume that we can just update 
459          * an existing entry
460          */
461         if (o)
462                 e = &o->e;
463         else
464                 e = &s->e;
465
466         if (!internal)
467                 return uci_set_element_value(ctx, &e, value);
468
469         UCI_INTERNAL(uci_set_element_value, ctx, &e, value);
470         return 0;
471
472 notfound:
473         /* 
474          * the entry that we need to update was not found,
475          * check if the search failed prematurely.
476          * this can happen if the package was not found, or if
477          * an option was supplied, but the section wasn't found
478          */
479         if (!p || (!s && option))
480                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
481
482         /* now add the missing entry */
483         if (!internal)
484                 uci_add_history(ctx, p, UCI_CMD_ADD, section, option, value);
485         if (s)
486                 uci_alloc_option(s, option, value);
487         else {
488                 s = uci_alloc_section(p, value, section);
489                 if (ctx->pctx)
490                         ctx->pctx->section = s;
491         }
492
493         return 0;
494 }
495
496 int uci_unload(struct uci_context *ctx, struct uci_package *p)
497 {
498         UCI_HANDLE_ERR(ctx);
499         UCI_ASSERT(ctx, p != NULL);
500
501         uci_free_package(&p);
502         return 0;
503 }
504