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