ucimap: add an example for optmap data structure encapsulation
[project/uci.git] / libuci.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 /*
16  * This file contains some common code for the uci library
17  */
18
19 #define _GNU_SOURCE
20 #include <sys/types.h>
21 #include <stdbool.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <dlfcn.h>
26 #include <glob.h>
27 #include "uci.h"
28
29 static const char *uci_confdir = UCI_CONFDIR;
30 static const char *uci_savedir = UCI_SAVEDIR;
31
32 static const char *uci_errstr[] = {
33         [UCI_OK] =            "Success",
34         [UCI_ERR_MEM] =       "Out of memory",
35         [UCI_ERR_INVAL] =     "Invalid argument",
36         [UCI_ERR_NOTFOUND] =  "Entry not found",
37         [UCI_ERR_IO] =        "I/O error",
38         [UCI_ERR_PARSE] =     "Parse error",
39         [UCI_ERR_DUPLICATE] = "Duplicate entry",
40         [UCI_ERR_UNKNOWN] =   "Unknown error",
41 };
42
43 static void uci_cleanup(struct uci_context *ctx);
44 static void uci_unload_plugin(struct uci_context *ctx, struct uci_plugin *p);
45
46 #include "uci_internal.h"
47 #include "util.c"
48 #include "list.c"
49 #include "history.c"
50 #include "file.c"
51
52 /* exported functions */
53 struct uci_context *uci_alloc_context(void)
54 {
55         struct uci_context *ctx;
56
57         ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
58         memset(ctx, 0, sizeof(struct uci_context));
59         uci_list_init(&ctx->root);
60         uci_list_init(&ctx->history_path);
61         uci_list_init(&ctx->backends);
62         uci_list_init(&ctx->hooks);
63         uci_list_init(&ctx->plugins);
64         ctx->flags = UCI_FLAG_STRICT | UCI_FLAG_SAVED_HISTORY;
65
66         ctx->confdir = (char *) uci_confdir;
67         ctx->savedir = (char *) uci_savedir;
68
69         uci_list_add(&ctx->backends, &uci_file_backend.e.list);
70         ctx->backend = &uci_file_backend;
71
72         return ctx;
73 }
74
75 void uci_free_context(struct uci_context *ctx)
76 {
77         struct uci_element *e, *tmp;
78
79         if (ctx->confdir != uci_confdir)
80                 free(ctx->confdir);
81         if (ctx->savedir != uci_savedir)
82                 free(ctx->savedir);
83
84         uci_cleanup(ctx);
85         UCI_TRAP_SAVE(ctx, ignore);
86         uci_foreach_element_safe(&ctx->root, tmp, e) {
87                 struct uci_package *p = uci_to_package(e);
88                 uci_free_package(&p);
89         }
90         uci_foreach_element_safe(&ctx->history_path, tmp, e) {
91                 uci_free_element(e);
92         }
93         UCI_TRAP_RESTORE(ctx);
94         uci_foreach_element_safe(&ctx->root, tmp, e) {
95                 uci_unload_plugin(ctx, uci_to_plugin(e));
96         }
97         free(ctx);
98
99 ignore:
100         return;
101 }
102
103 int uci_set_confdir(struct uci_context *ctx, const char *dir)
104 {
105         char *cdir;
106
107         UCI_HANDLE_ERR(ctx);
108         UCI_ASSERT(ctx, dir != NULL);
109
110         cdir = uci_strdup(ctx, dir);
111         if (ctx->confdir != uci_confdir)
112                 free(ctx->confdir);
113         ctx->confdir = cdir;
114         return 0;
115 }
116
117 static void uci_cleanup(struct uci_context *ctx)
118 {
119         struct uci_parse_context *pctx;
120
121         if (ctx->buf) {
122                 free(ctx->buf);
123                 ctx->buf = NULL;
124                 ctx->bufsz = 0;
125         }
126
127         pctx = ctx->pctx;
128         if (!pctx)
129                 return;
130
131         ctx->pctx = NULL;
132         if (pctx->package)
133                 uci_free_package(&pctx->package);
134
135         if (pctx->buf)
136                 free(pctx->buf);
137
138         free(pctx);
139 }
140
141 void
142 uci_perror(struct uci_context *ctx, const char *str)
143 {
144         uci_get_errorstr(ctx, NULL, str);
145 }
146
147 void
148 uci_get_errorstr(struct uci_context *ctx, char **dest, const char *prefix)
149 {
150         static char error_info[128];
151         int err;
152         const char *format =
153                 "%s%s" /* prefix */
154                 "%s%s" /* function */
155                 "%s" /* error */
156                 "%s"; /* details */
157
158         error_info[0] = 0;
159
160         if (!ctx)
161                 err = UCI_ERR_INVAL;
162         else
163                 err = ctx->err;
164
165         if ((err < 0) || (err >= UCI_ERR_LAST))
166                 err = UCI_ERR_UNKNOWN;
167
168         switch (err) {
169         case UCI_ERR_PARSE:
170                 if (ctx->pctx) {
171                         snprintf(error_info, sizeof(error_info) - 1, " (%s) at line %d, byte %d", (ctx->pctx->reason ? ctx->pctx->reason : "unknown"), ctx->pctx->line, ctx->pctx->byte);
172                         break;
173                 }
174                 break;
175         default:
176                 break;
177         }
178         if (dest) {
179                 err = asprintf(dest, format,
180                         (prefix ? prefix : ""), (prefix ? ": " : ""),
181                         (ctx->func ? ctx->func : ""), (ctx->func ? ": " : ""),
182                         uci_errstr[err],
183                         error_info);
184                 if (err < 0)
185                         *dest = NULL;
186         } else {
187                 strcat(error_info, "\n");
188                 fprintf(stderr, format,
189                         (prefix ? prefix : ""), (prefix ? ": " : ""),
190                         (ctx->func ? ctx->func : ""), (ctx->func ? ": " : ""),
191                         uci_errstr[err],
192                         error_info);
193         }
194 }
195
196 int uci_list_configs(struct uci_context *ctx, char ***list)
197 {
198         UCI_HANDLE_ERR(ctx);
199         UCI_ASSERT(ctx, list != NULL);
200         UCI_ASSERT(ctx, ctx->backend && ctx->backend->list_configs);
201         *list = ctx->backend->list_configs(ctx);
202         return 0;
203 }
204
205 int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
206 {
207         struct uci_package *p;
208         UCI_HANDLE_ERR(ctx);
209         UCI_ASSERT(ctx, package != NULL);
210         p = *package;
211         UCI_ASSERT(ctx, p != NULL);
212         UCI_ASSERT(ctx, p->backend && p->backend->commit);
213         p->backend->commit(ctx, package, overwrite);
214         return 0;
215 }
216
217 int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
218 {
219         struct uci_package *p;
220         struct uci_element *e;
221
222         UCI_HANDLE_ERR(ctx);
223         UCI_ASSERT(ctx, ctx->backend && ctx->backend->load);
224         p = ctx->backend->load(ctx, name);
225         uci_foreach_element(&ctx->hooks, e) {
226                 struct uci_hook *h = uci_to_hook(e);
227                 if (h->ops->load)
228                         h->ops->load(h->ops, p);
229         }
230         if (package)
231                 *package = p;
232
233         return 0;
234 }
235
236 #ifdef UCI_PLUGIN_SUPPORT
237
238 __plugin int uci_add_backend(struct uci_context *ctx, struct uci_backend *b)
239 {
240         struct uci_element *e;
241         UCI_HANDLE_ERR(ctx);
242
243         e = uci_lookup_list(&ctx->backends, b->e.name);
244         if (e)
245                 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
246
247         e = uci_malloc(ctx, sizeof(struct uci_backend));
248         memcpy(e, b, sizeof(struct uci_backend));
249
250         uci_list_add(&ctx->backends, &e->list);
251         return 0;
252 }
253
254 __plugin int uci_del_backend(struct uci_context *ctx, struct uci_backend *b)
255 {
256         struct uci_element *e, *tmp;
257
258         UCI_HANDLE_ERR(ctx);
259
260         e = uci_lookup_list(&ctx->backends, b->e.name);
261         if (!e || uci_to_backend(e)->ptr != b->ptr)
262                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
263         b = uci_to_backend(e);
264
265         if (ctx->backend && ctx->backend->ptr == b->ptr)
266                 ctx->backend = &uci_file_backend;
267
268         uci_foreach_element_safe(&ctx->root, tmp, e) {
269                 struct uci_package *p = uci_to_package(e);
270
271                 if (!p->backend)
272                         continue;
273
274                 if (p->backend->ptr == b->ptr)
275                         UCI_INTERNAL(uci_unload, ctx, p);
276         }
277
278         uci_list_del(&b->e.list);
279         free(b);
280
281         return 0;
282 }
283
284 #endif
285
286 int uci_set_backend(struct uci_context *ctx, const char *name)
287 {
288         struct uci_element *e;
289
290         UCI_HANDLE_ERR(ctx);
291         UCI_ASSERT(ctx, name != NULL);
292         e = uci_lookup_list(&ctx->backends, name);
293         if (!e)
294                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
295         ctx->backend = uci_to_backend(e);
296         return 0;
297 }
298
299 int uci_add_hook(struct uci_context *ctx, const struct uci_hook_ops *ops)
300 {
301         struct uci_element *e;
302         struct uci_hook *h;
303
304         UCI_HANDLE_ERR(ctx);
305
306         /* check for duplicate elements */
307         uci_foreach_element(&ctx->hooks, e) {
308                 h = uci_to_hook(e);
309                 if (h->ops == ops)
310                         return UCI_ERR_INVAL;
311         }
312
313         h = uci_alloc_element(ctx, hook, "", 0);
314         h->ops = ops;
315         uci_list_init(&h->e.list);
316         uci_list_add(&ctx->hooks, &h->e.list);
317
318         return 0;
319 }
320
321 int uci_remove_hook(struct uci_context *ctx, const struct uci_hook_ops *ops)
322 {
323         struct uci_element *e;
324
325         uci_foreach_element(&ctx->hooks, e) {
326                 struct uci_hook *h = uci_to_hook(e);
327                 if (h->ops == ops) {
328                         uci_list_del(&e->list);
329                         return 0;
330                 }
331         }
332         return UCI_ERR_NOTFOUND;
333 }
334
335 int uci_load_plugin(struct uci_context *ctx, const char *filename)
336 {
337         struct uci_plugin *p;
338         const struct uci_plugin_ops *ops;
339         void *dlh;
340
341         UCI_HANDLE_ERR(ctx);
342         dlh = dlopen(filename, RTLD_GLOBAL|RTLD_NOW);
343         if (!dlh)
344                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
345
346         ops = dlsym(dlh, "uci_plugin");
347         if (!ops || !ops->attach || (ops->attach(ctx) != 0)) {
348                 if (!ops)
349                         fprintf(stderr, "No ops\n");
350                 else if (!ops->attach)
351                         fprintf(stderr, "No attach\n");
352                 else
353                         fprintf(stderr, "Other weirdness\n");
354                 dlclose(dlh);
355                 UCI_THROW(ctx, UCI_ERR_INVAL);
356         }
357
358         p = uci_alloc_element(ctx, plugin, filename, 0);
359         p->dlh = dlh;
360         p->ops = ops;
361         uci_list_add(&ctx->plugins, &p->e.list);
362
363         return 0;
364 }
365
366 static void uci_unload_plugin(struct uci_context *ctx, struct uci_plugin *p)
367 {
368         if (p->ops->detach)
369                 p->ops->detach(ctx);
370         dlclose(p->dlh);
371         uci_free_element(&p->e);
372 }
373
374 int uci_load_plugins(struct uci_context *ctx, const char *pattern)
375 {
376         glob_t gl;
377         int i;
378
379         if (!pattern)
380                 pattern = UCI_PREFIX "/lib/uci_*.so";
381
382         memset(&gl, 0, sizeof(gl));
383         glob(pattern, 0, NULL, &gl);
384         for (i = 0; i < gl.gl_pathc; i++)
385                 uci_load_plugin(ctx, gl.gl_pathv[i]);
386
387         return 0;
388 }