major code refactoring for more code reuse and smaller code size
[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 between two consecutive entries */
25 static inline void __uci_list_add(struct uci_list *prev, struct uci_list *next, struct uci_list *ptr)
26 {
27         next->prev = ptr;
28         ptr->prev = prev;
29         ptr->next = next;
30         prev->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_add(head->prev, head, 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 static struct uci_element *
54 uci_alloc_generic(struct uci_context *ctx, const char *name, int size)
55 {
56         struct uci_element *e;
57         void *ptr;
58
59         ptr = uci_malloc(ctx, size + strlen(name) + 1);
60         e = (struct uci_element *) ptr;
61         e->name = (char *) ptr + size;
62         strcpy(e->name, name);
63         uci_list_init(&e->list);
64
65         return e;
66 }
67
68 static void
69 uci_free_element(struct uci_element *e)
70 {
71         if (!e)
72                 return;
73
74         if (!uci_list_empty(&e->list))
75                 uci_list_del(&e->list);
76         free(e);
77 }
78
79 static struct uci_option *
80 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
81 {
82         struct uci_package *p = s->package;
83         struct uci_context *ctx = p->ctx;
84         struct uci_option *o;
85
86         o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
87         o->value = uci_dataptr(o);
88         o->section = s;
89         strcpy(o->value, value);
90         uci_list_add(&s->options, &o->e.list);
91
92         return o;
93 }
94
95 static inline void
96 uci_free_option(struct uci_option *o)
97 {
98         uci_free_element(&o->e);
99 }
100
101 static struct uci_section *
102 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
103 {
104         struct uci_context *ctx = p->ctx;
105         struct uci_section *s;
106         char buf[16];
107
108         if (!name || !name[0]) {
109                 snprintf(buf, 16, "cfg%d", p->n_section);
110                 name = buf;
111         }
112
113         s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
114         s->type = uci_dataptr(s);
115         s->package = p;
116         strcpy(s->type, type);
117         uci_list_init(&s->options);
118         uci_list_add(&p->sections, &s->e.list);
119
120         return s;
121 }
122
123 static void
124 uci_free_section(struct uci_section *s)
125 {
126         struct uci_element *o, *tmp;
127
128         uci_foreach_element_safe(&s->options, tmp, o) {
129                 uci_free_option(uci_to_option(o));
130         }
131         uci_free_element(&s->e);
132 }
133
134 static struct uci_package *
135 uci_alloc_package(struct uci_context *ctx, const char *name)
136 {
137         struct uci_package *p;
138
139         p = uci_alloc_element(ctx, package, name, 0);
140         p->ctx = ctx;
141         uci_list_init(&p->sections);
142         return p;
143 }
144
145 static void
146 uci_free_package(struct uci_package *p)
147 {
148         struct uci_element *e, *tmp;
149
150         if(!p)
151                 return;
152
153         uci_foreach_element_safe(&p->sections, tmp, e) {
154                 uci_free_section(uci_to_section(e));
155         }
156         uci_free_element(&p->e);
157 }
158
159
160 int uci_unload(struct uci_context *ctx, const char *name)
161 {
162         struct uci_element *e;
163
164         UCI_HANDLE_ERR(ctx);
165         UCI_ASSERT(ctx, name != NULL);
166
167         uci_foreach_element(&ctx->root, e) {
168                 if (!strcmp(e->name, name))
169                         goto found;
170         }
171         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
172
173 found:
174         uci_free_package(uci_to_package(e));
175
176         return 0;
177 }
178
179 static inline char *get_filename(char *path)
180 {
181         char *p;
182
183         p = strrchr(path, '/');
184         p++;
185         if (!*p)
186                 return NULL;
187         return p;
188 }
189
190 char **uci_list_configs(struct uci_context *ctx)
191 {
192         char **configs;
193         glob_t globbuf;
194         int size, i;
195         char *buf;
196
197         if (glob(UCI_CONFDIR "/*", GLOB_MARK, NULL, &globbuf) != 0)
198                 return NULL;
199
200         size = sizeof(char *) * (globbuf.gl_pathc + 1);
201         for(i = 0; i < globbuf.gl_pathc; i++) {
202                 char *p;
203
204                 p = get_filename(globbuf.gl_pathv[i]);
205                 if (!p)
206                         continue;
207
208                 size += strlen(p) + 1;
209         }
210
211         configs = malloc(size);
212         if (!configs)
213                 return NULL;
214
215         memset(configs, 0, size);
216         buf = (char *) &configs[globbuf.gl_pathc + 1];
217         for(i = 0; i < globbuf.gl_pathc; i++) {
218                 char *p;
219
220                 p = get_filename(globbuf.gl_pathv[i]);
221                 if (!p)
222                         continue;
223
224                 configs[i] = buf;
225                 strcpy(buf, p);
226                 buf += strlen(buf) + 1;
227         }
228         return configs;
229 }
230
231