5332800fe32975c98c8b8a14b2b47d378b874d6f
[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         void *ptr;
62
63         ptr = uci_malloc(ctx, size + strlen(name) + 1);
64         e = (struct uci_element *) ptr;
65         e->type = type;
66         e->name = (char *) ptr + size;
67         strcpy(e->name, name);
68         uci_list_init(&e->list);
69
70         return e;
71 }
72
73 static void
74 uci_free_element(struct uci_element *e)
75 {
76         if (!uci_list_empty(&e->list))
77                 uci_list_del(&e->list);
78         free(e);
79 }
80
81 static struct uci_option *
82 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
83 {
84         struct uci_package *p = s->package;
85         struct uci_context *ctx = p->ctx;
86         struct uci_option *o;
87
88         o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
89         o->value = uci_dataptr(o);
90         o->section = s;
91         strcpy(o->value, value);
92         uci_list_add(&s->options, &o->e.list);
93
94         return o;
95 }
96
97 static inline void
98 uci_free_option(struct uci_option *o)
99 {
100         uci_free_element(&o->e);
101 }
102
103 static struct uci_section *
104 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
105 {
106         struct uci_context *ctx = p->ctx;
107         struct uci_section *s;
108         char buf[16];
109
110         if (!name || !name[0]) {
111                 snprintf(buf, 16, "cfg%d", p->n_section);
112                 name = buf;
113         }
114
115         s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
116         s->type = uci_dataptr(s);
117         s->package = p;
118         strcpy(s->type, type);
119         uci_list_init(&s->options);
120         uci_list_add(&p->sections, &s->e.list);
121
122         return s;
123 }
124
125 static void
126 uci_free_section(struct uci_section *s)
127 {
128         struct uci_element *o, *tmp;
129
130         uci_foreach_element_safe(&s->options, tmp, o) {
131                 uci_free_option(uci_to_option(o));
132         }
133         uci_free_element(&s->e);
134 }
135
136 static struct uci_package *
137 uci_alloc_package(struct uci_context *ctx, const char *name)
138 {
139         struct uci_package *p;
140
141         p = uci_alloc_element(ctx, package, name, 0);
142         p->ctx = ctx;
143         uci_list_init(&p->sections);
144         uci_list_init(&p->history);
145         return p;
146 }
147
148 static void
149 uci_free_package(struct uci_package *p)
150 {
151         struct uci_element *e, *tmp;
152
153         if(!p)
154                 return;
155
156         if (p->path)
157                 free(p->path);
158         uci_foreach_element_safe(&p->sections, tmp, e) {
159                 uci_free_section(uci_to_section(e));
160         }
161         uci_free_element(&p->e);
162 }
163
164 /* record a change that was done to a package */
165 static inline void
166 uci_add_history(struct uci_context *ctx, struct uci_package *p, int cmd, char *section, char *option, char *value)
167 {
168         struct uci_history *h = (struct uci_history *) uci_malloc(ctx, sizeof(struct uci_history));
169
170         uci_list_init(&h->list);
171         h->cmd = cmd;
172         h->section = section;
173         h->option = option;
174         h->value = value;
175         uci_list_add(&p->history, &h->list);
176 }
177
178
179 static struct uci_element *uci_lookup_list(struct uci_context *ctx, struct uci_list *list, const char *name)
180 {
181         struct uci_element *e;
182
183         uci_foreach_element(list, e) {
184                 if (!strcmp(e->name, name))
185                         return e;
186         }
187         return NULL;
188 }
189
190 int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *p, char *section, char *option)
191 {
192         struct uci_element *e;
193         struct uci_section *s;
194         struct uci_option *o;
195
196         UCI_HANDLE_ERR(ctx);
197         UCI_ASSERT(ctx, res != NULL);
198         UCI_ASSERT(ctx, p != NULL);
199         UCI_ASSERT(ctx, section != NULL);
200
201         e = uci_lookup_list(ctx, &p->sections, section);
202         if (!e)
203                 goto notfound;
204
205         if (option) {
206                 s = uci_to_section(e);
207                 e = uci_lookup_list(ctx, &s->options, option);
208         }
209
210         *res = e;
211         return 0;
212
213 notfound:
214         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
215         return 0;
216 }
217
218 int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, char *value)
219 {
220         int size;
221         char *str;
222         struct uci_list *list;
223         struct uci_element *e;
224         struct uci_package *p;
225         struct uci_section *s;
226         char *section;
227         char *option;
228
229         UCI_HANDLE_ERR(ctx);
230         UCI_ASSERT(ctx, value != NULL);
231         UCI_ASSERT(ctx, element != NULL);
232         UCI_ASSERT(ctx, *element != NULL);
233
234         /* what the 'value' of an element means depends on the type
235          * for a section, the 'value' means its type
236          * for an option, the 'value' means its value string
237          * when changing the value, shrink the element to its actual size
238          * (it may have been allocated with a bigger size, to include
239          *  its buffer)
240          * then duplicate the string passed on the command line and
241          * insert it into the structure.
242          */
243         e = *element;
244         list = e->list.prev;
245         switch(e->type) {
246         case UCI_TYPE_SECTION:
247                 size = sizeof(struct uci_section);
248                 s = uci_to_section(e);
249                 section = e->name;
250                 option = NULL;
251                 break;
252         case UCI_TYPE_OPTION:
253                 size = sizeof(struct uci_option);
254                 s = uci_to_option(e)->section;
255                 section = s->e.name;
256                 option = e->name;
257                 break;
258         default:
259                 UCI_THROW(ctx, UCI_ERR_INVAL);
260                 return 0;
261         }
262         p = s->package;
263         uci_add_history(ctx, p, UCI_CMD_CHANGE, section, option, value);
264
265         uci_list_del(&e->list);
266         e = uci_realloc(ctx, e, size);
267         str = uci_strdup(ctx, value);
268         uci_list_insert(list, &e->list);
269         *element = e;
270
271         switch(e->type) {
272         case UCI_TYPE_SECTION:
273                 uci_to_section(e)->type = value;
274                 break;
275         case UCI_TYPE_OPTION:
276                 uci_to_option(e)->value = value;
277                 break;
278         default:
279                 break;
280         }
281
282         return 0;
283 }
284
285 int uci_set(struct uci_context *ctx, char *package, char *section, char *option, char *value)
286 {
287         struct uci_element *e;
288         struct uci_package *p = NULL;
289         struct uci_section *s = NULL;
290         struct uci_option *o = NULL;
291         struct uci_history *h;
292
293         UCI_HANDLE_ERR(ctx);
294         UCI_ASSERT(ctx, package != NULL);
295         UCI_ASSERT(ctx, section != NULL);
296
297         /*
298          * look up the package, section and option (if set)
299          * if the section/option is to be modified and it is not found
300          * create a new element in the appropriate list
301          */
302         e = uci_lookup_list(ctx, &ctx->root, package);
303         if (!e)
304                 goto notfound;
305
306         p = uci_to_package(e);
307         e = uci_lookup_list(ctx, &p->sections, section);
308         if (!e)
309                 goto notfound;
310
311         s = uci_to_section(e);
312         if (option) {
313                 e = uci_lookup_list(ctx, &s->options, option);
314                 if (!e)
315                         goto notfound;
316                 o = uci_to_option(e);
317         }
318
319         /* 
320          * no unknown element was supplied, assume that we can just update 
321          * an existing entry
322          */
323         if (o)
324                 e = &o->e;
325         else
326                 e = &s->e;
327
328         return uci_set_element_value(ctx, &e, value);
329
330 notfound:
331         /* 
332          * the entry that we need to update was not found,
333          * check if the search failed prematurely.
334          * this can happen if the package was not found, or if
335          * an option was supplied, but the section wasn't found
336          */
337         if (!p || (!s && option))
338                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
339
340         /* now add the missing entry */
341         uci_add_history(ctx, p, UCI_CMD_ADD, section, option, value);
342         if (s)
343                 uci_alloc_option(s, option, value);
344         else
345                 uci_alloc_section(p, value, section);
346
347         return 0;
348 }
349
350 int uci_unload(struct uci_context *ctx, struct uci_package *p)
351 {
352         struct uci_element *e;
353
354         UCI_HANDLE_ERR(ctx);
355         UCI_ASSERT(ctx, p != NULL);
356
357         uci_free_package(p);
358         return 0;
359 }
360