extend api to be able to get a full error string instead of printing it
[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 "uci.h"
26
27 static const char *uci_confdir = UCI_CONFDIR;
28 static const char *uci_savedir = UCI_SAVEDIR;
29
30 static const char *uci_errstr[] = {
31         [UCI_OK] =            "Success",
32         [UCI_ERR_MEM] =       "Out of memory",
33         [UCI_ERR_INVAL] =     "Invalid argument",
34         [UCI_ERR_NOTFOUND] =  "Entry not found",
35         [UCI_ERR_IO] =        "I/O error",
36         [UCI_ERR_PARSE] =     "Parse error",
37         [UCI_ERR_DUPLICATE] = "Duplicate entry",
38         [UCI_ERR_UNKNOWN] =   "Unknown error",
39 };
40
41 static void uci_cleanup(struct uci_context *ctx);
42
43 #include "uci_internal.h"
44 #include "util.c"
45 #include "list.c"
46 #include "history.c"
47 #include "file.c"
48
49 /* exported functions */
50 struct uci_context *uci_alloc_context(void)
51 {
52         struct uci_context *ctx;
53
54         ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
55         memset(ctx, 0, sizeof(struct uci_context));
56         uci_list_init(&ctx->root);
57         uci_list_init(&ctx->history_path);
58         uci_list_init(&ctx->backends);
59         ctx->flags = UCI_FLAG_STRICT | UCI_FLAG_SAVED_HISTORY;
60
61         ctx->confdir = (char *) uci_confdir;
62         ctx->savedir = (char *) uci_savedir;
63
64         uci_list_add(&ctx->backends, &uci_file_backend.e.list);
65         ctx->backend = &uci_file_backend;
66
67         return ctx;
68 }
69
70 void uci_free_context(struct uci_context *ctx)
71 {
72         struct uci_element *e, *tmp;
73
74         if (ctx->confdir != uci_confdir)
75                 free(ctx->confdir);
76         if (ctx->savedir != uci_savedir)
77                 free(ctx->savedir);
78
79         uci_cleanup(ctx);
80         UCI_TRAP_SAVE(ctx, ignore);
81         uci_foreach_element_safe(&ctx->root, tmp, e) {
82                 struct uci_package *p = uci_to_package(e);
83                 uci_free_package(&p);
84         }
85         uci_foreach_element_safe(&ctx->history_path, tmp, e) {
86                 uci_free_element(e);
87         }
88         UCI_TRAP_RESTORE(ctx);
89         free(ctx);
90
91 ignore:
92         return;
93 }
94
95 int uci_set_confdir(struct uci_context *ctx, const char *dir)
96 {
97         char *cdir;
98
99         UCI_HANDLE_ERR(ctx);
100         UCI_ASSERT(ctx, dir != NULL);
101
102         cdir = uci_strdup(ctx, dir);
103         if (ctx->confdir != uci_confdir)
104                 free(ctx->confdir);
105         ctx->confdir = cdir;
106         return 0;
107 }
108
109 static void uci_cleanup(struct uci_context *ctx)
110 {
111         struct uci_parse_context *pctx;
112
113         if (ctx->buf) {
114                 free(ctx->buf);
115                 ctx->buf = NULL;
116                 ctx->bufsz = 0;
117         }
118
119         pctx = ctx->pctx;
120         if (!pctx)
121                 return;
122
123         ctx->pctx = NULL;
124         if (pctx->package)
125                 uci_free_package(&pctx->package);
126
127         if (pctx->buf)
128                 free(pctx->buf);
129
130         free(pctx);
131 }
132
133 void
134 uci_perror(struct uci_context *ctx, const char *str)
135 {
136         uci_get_errorstr(ctx, NULL, str);
137 }
138
139 void
140 uci_get_errorstr(struct uci_context *ctx, char **dest, const char *prefix)
141 {
142         static char error_info[128];
143         int err;
144         const char *format =
145                 "%s%s" /* prefix */
146                 "%s%s" /* function */
147                 "%s" /* error */
148                 "%s\n"; /* details */
149
150         error_info[0] = 0;
151
152         if (!ctx)
153                 err = UCI_ERR_INVAL;
154         else
155                 err = ctx->err;
156
157         if ((err < 0) || (err >= UCI_ERR_LAST))
158                 err = UCI_ERR_UNKNOWN;
159
160         switch (err) {
161         case UCI_ERR_PARSE:
162                 if (ctx->pctx) {
163                         snprintf(error_info, sizeof(error_info), " (%s) at line %d, byte %d", (ctx->pctx->reason ? ctx->pctx->reason : "unknown"), ctx->pctx->line, ctx->pctx->byte);
164                         break;
165                 }
166                 break;
167         default:
168                 break;
169         }
170         if (dest)
171                 asprintf(dest, format,
172                         (prefix ? prefix : ""), (prefix ? ": " : ""),
173                         (ctx->func ? ctx->func : ""), (ctx->func ? ": " : ""),
174                         uci_errstr[err],
175                         error_info);
176         else
177                 fprintf(stderr, format,
178                         (prefix ? prefix : ""), (prefix ? ": " : ""),
179                         (ctx->func ? ctx->func : ""), (ctx->func ? ": " : ""),
180                         uci_errstr[err],
181                         error_info);
182 }
183
184 int uci_list_configs(struct uci_context *ctx, char ***list)
185 {
186         UCI_HANDLE_ERR(ctx);
187         UCI_ASSERT(ctx, list != NULL);
188         UCI_ASSERT(ctx, ctx->backend && ctx->backend->list_configs);
189         *list = ctx->backend->list_configs(ctx);
190         return 0;
191 }
192
193 int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
194 {
195         struct uci_package *p;
196         UCI_HANDLE_ERR(ctx);
197         UCI_ASSERT(ctx, package != NULL);
198         p = *package;
199         UCI_ASSERT(ctx, p != NULL);
200         UCI_ASSERT(ctx, p->backend && p->backend->commit);
201         p->backend->commit(ctx, package, overwrite);
202         return 0;
203 }
204
205 int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
206 {
207         struct uci_package *p;
208         UCI_HANDLE_ERR(ctx);
209         UCI_ASSERT(ctx, ctx->backend && ctx->backend->load);
210         p = ctx->backend->load(ctx, name);
211         if (package)
212                 *package = p;
213
214         return 0;
215 }
216
217 #ifdef UCI_PLUGIN_SUPPORT
218
219 __plugin int uci_add_backend(struct uci_context *ctx, struct uci_backend *b)
220 {
221         struct uci_element *e;
222         UCI_HANDLE_ERR(ctx);
223
224         e = uci_lookup_list(&ctx->backends, b->e.name);
225         if (e)
226                 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
227
228         e = uci_malloc(ctx, sizeof(struct uci_backend));
229         memcpy(e, b, sizeof(struct uci_backend));
230
231         uci_list_add(&ctx->backends, &e->list);
232         return 0;
233 }
234
235 __plugin int uci_del_backend(struct uci_context *ctx, struct uci_backend *b)
236 {
237         struct uci_element *e, *tmp;
238
239         UCI_HANDLE_ERR(ctx);
240
241         e = uci_lookup_list(&ctx->backends, b->e.name);
242         if (!e || uci_to_backend(e)->ptr != b->ptr)
243                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
244         b = uci_to_backend(e);
245
246         if (ctx->backend && ctx->backend->ptr == b->ptr)
247                 ctx->backend = &uci_file_backend;
248
249         uci_foreach_element_safe(&ctx->root, tmp, e) {
250                 struct uci_package *p = uci_to_package(e);
251
252                 if (!p->backend)
253                         continue;
254
255                 if (p->backend->ptr == b->ptr)
256                         UCI_INTERNAL(uci_unload, ctx, p);
257         }
258
259         uci_list_del(&b->e.list);
260         free(b);
261
262         return 0;
263 }
264
265 #endif
266
267 int uci_set_backend(struct uci_context *ctx, const char *name)
268 {
269         struct uci_element *e;
270
271         UCI_HANDLE_ERR(ctx);
272         UCI_ASSERT(ctx, name != NULL);
273         e = uci_lookup_list(&ctx->backends, name);
274         if (!e)
275                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
276         ctx->backend = uci_to_backend(e);
277         return 0;
278 }