2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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
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.
16 * This file contains some common code for the uci library
20 #include <sys/types.h>
27 static const char *uci_confdir
= UCI_CONFDIR
;
28 static const char *uci_savedir
= UCI_SAVEDIR
;
30 static const char *uci_errstr
[] = {
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",
41 static void uci_cleanup(struct uci_context
*ctx
);
43 #include "uci_internal.h"
49 /* exported functions */
50 struct uci_context
*uci_alloc_context(void)
52 struct uci_context
*ctx
;
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
;
61 ctx
->confdir
= (char *) uci_confdir
;
62 ctx
->savedir
= (char *) uci_savedir
;
64 uci_list_add(&ctx
->backends
, &uci_file_backend
.e
.list
);
65 ctx
->backend
= &uci_file_backend
;
70 void uci_free_context(struct uci_context
*ctx
)
72 struct uci_element
*e
, *tmp
;
74 if (ctx
->confdir
!= uci_confdir
)
76 if (ctx
->savedir
!= uci_savedir
)
80 UCI_TRAP_SAVE(ctx
, ignore
);
81 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
82 struct uci_package
*p
= uci_to_package(e
);
85 uci_foreach_element_safe(&ctx
->history_path
, tmp
, e
) {
88 UCI_TRAP_RESTORE(ctx
);
95 int uci_set_confdir(struct uci_context
*ctx
, const char *dir
)
100 UCI_ASSERT(ctx
, dir
!= NULL
);
102 cdir
= uci_strdup(ctx
, dir
);
103 if (ctx
->confdir
!= uci_confdir
)
109 static void uci_cleanup(struct uci_context
*ctx
)
111 struct uci_parse_context
*pctx
;
125 uci_free_package(&pctx
->package
);
134 uci_perror(struct uci_context
*ctx
, const char *str
)
136 uci_get_errorstr(ctx
, NULL
, str
);
140 uci_get_errorstr(struct uci_context
*ctx
, char **dest
, const char *prefix
)
142 static char error_info
[128];
146 "%s%s" /* function */
157 if ((err
< 0) || (err
>= UCI_ERR_LAST
))
158 err
= UCI_ERR_UNKNOWN
;
163 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
);
171 asprintf(dest
, format
,
172 (prefix
? prefix
: ""), (prefix
? ": " : ""),
173 (ctx
->func
? ctx
->func
: ""), (ctx
->func
? ": " : ""),
177 strcat(error_info
, "\n");
178 fprintf(stderr
, format
,
179 (prefix
? prefix
: ""), (prefix
? ": " : ""),
180 (ctx
->func
? ctx
->func
: ""), (ctx
->func
? ": " : ""),
186 int uci_list_configs(struct uci_context
*ctx
, char ***list
)
189 UCI_ASSERT(ctx
, list
!= NULL
);
190 UCI_ASSERT(ctx
, ctx
->backend
&& ctx
->backend
->list_configs
);
191 *list
= ctx
->backend
->list_configs(ctx
);
195 int uci_commit(struct uci_context
*ctx
, struct uci_package
**package
, bool overwrite
)
197 struct uci_package
*p
;
199 UCI_ASSERT(ctx
, package
!= NULL
);
201 UCI_ASSERT(ctx
, p
!= NULL
);
202 UCI_ASSERT(ctx
, p
->backend
&& p
->backend
->commit
);
203 p
->backend
->commit(ctx
, package
, overwrite
);
207 int uci_load(struct uci_context
*ctx
, const char *name
, struct uci_package
**package
)
209 struct uci_package
*p
;
211 UCI_ASSERT(ctx
, ctx
->backend
&& ctx
->backend
->load
);
212 p
= ctx
->backend
->load(ctx
, name
);
219 #ifdef UCI_PLUGIN_SUPPORT
221 __plugin
int uci_add_backend(struct uci_context
*ctx
, struct uci_backend
*b
)
223 struct uci_element
*e
;
226 e
= uci_lookup_list(&ctx
->backends
, b
->e
.name
);
228 UCI_THROW(ctx
, UCI_ERR_DUPLICATE
);
230 e
= uci_malloc(ctx
, sizeof(struct uci_backend
));
231 memcpy(e
, b
, sizeof(struct uci_backend
));
233 uci_list_add(&ctx
->backends
, &e
->list
);
237 __plugin
int uci_del_backend(struct uci_context
*ctx
, struct uci_backend
*b
)
239 struct uci_element
*e
, *tmp
;
243 e
= uci_lookup_list(&ctx
->backends
, b
->e
.name
);
244 if (!e
|| uci_to_backend(e
)->ptr
!= b
->ptr
)
245 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
246 b
= uci_to_backend(e
);
248 if (ctx
->backend
&& ctx
->backend
->ptr
== b
->ptr
)
249 ctx
->backend
= &uci_file_backend
;
251 uci_foreach_element_safe(&ctx
->root
, tmp
, e
) {
252 struct uci_package
*p
= uci_to_package(e
);
257 if (p
->backend
->ptr
== b
->ptr
)
258 UCI_INTERNAL(uci_unload
, ctx
, p
);
261 uci_list_del(&b
->e
.list
);
269 int uci_set_backend(struct uci_context
*ctx
, const char *name
)
271 struct uci_element
*e
;
274 UCI_ASSERT(ctx
, name
!= NULL
);
275 e
= uci_lookup_list(&ctx
->backends
, name
);
277 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
278 ctx
->backend
= uci_to_backend(e
);