add uci_set_backend()
[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 #include "err.h"
27
28 static const char *uci_confdir = UCI_CONFDIR;
29 static const char *uci_savedir = UCI_SAVEDIR;
30
31 static const char *uci_errstr[] = {
32 [UCI_OK] = "Success",
33 [UCI_ERR_MEM] = "Out of memory",
34 [UCI_ERR_INVAL] = "Invalid argument",
35 [UCI_ERR_NOTFOUND] = "Entry not found",
36 [UCI_ERR_IO] = "I/O error",
37 [UCI_ERR_PARSE] = "Parse error",
38 [UCI_ERR_DUPLICATE] = "Duplicate entry",
39 [UCI_ERR_UNKNOWN] = "Unknown error",
40 };
41
42 static void uci_cleanup(struct uci_context *ctx);
43
44 #include "uci_internal.h"
45 #include "util.c"
46 #include "list.c"
47 #include "history.c"
48 #include "file.c"
49
50 /* exported functions */
51 struct uci_context *uci_alloc_context(void)
52 {
53 struct uci_context *ctx;
54
55 ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
56 memset(ctx, 0, sizeof(struct uci_context));
57 uci_list_init(&ctx->root);
58 uci_list_init(&ctx->history_path);
59 uci_list_init(&ctx->backends);
60 ctx->flags = UCI_FLAG_STRICT;
61
62 ctx->confdir = (char *) uci_confdir;
63 ctx->savedir = (char *) uci_savedir;
64
65 uci_list_add(&ctx->backends, &uci_file_backend.e.list);
66 ctx->backend = &uci_file_backend;
67
68 return ctx;
69 }
70
71 void uci_free_context(struct uci_context *ctx)
72 {
73 struct uci_element *e, *tmp;
74
75 if (ctx->confdir != uci_confdir)
76 free(ctx->confdir);
77 if (ctx->savedir != uci_savedir)
78 free(ctx->savedir);
79
80 uci_cleanup(ctx);
81 UCI_TRAP_SAVE(ctx, ignore);
82 uci_foreach_element_safe(&ctx->root, tmp, e) {
83 struct uci_package *p = uci_to_package(e);
84 uci_free_package(&p);
85 }
86 uci_foreach_element_safe(&ctx->history_path, tmp, e) {
87 uci_free_element(e);
88 }
89 free(ctx);
90 UCI_TRAP_RESTORE(ctx);
91
92 ignore:
93 return;
94 }
95
96 int uci_set_confdir(struct uci_context *ctx, const char *dir)
97 {
98 char *cdir;
99
100 UCI_HANDLE_ERR(ctx);
101 UCI_ASSERT(ctx, dir != NULL);
102
103 cdir = uci_strdup(ctx, dir);
104 if (ctx->confdir != uci_confdir)
105 free(ctx->confdir);
106 ctx->confdir = cdir;
107 return 0;
108 }
109
110 static void uci_cleanup(struct uci_context *ctx)
111 {
112 struct uci_parse_context *pctx;
113
114 if (ctx->buf) {
115 free(ctx->buf);
116 ctx->buf = NULL;
117 ctx->bufsz = 0;
118 }
119
120 pctx = ctx->pctx;
121 if (!pctx)
122 return;
123
124 ctx->pctx = NULL;
125 if (pctx->package)
126 uci_free_package(&pctx->package);
127
128 if (pctx->buf)
129 free(pctx->buf);
130
131 free(pctx);
132 }
133
134 void uci_perror(struct uci_context *ctx, const char *prefix)
135 {
136 int err;
137
138 if (!ctx)
139 err = UCI_ERR_INVAL;
140 else
141 err = ctx->errno;
142
143 if ((err < 0) || (err >= UCI_ERR_LAST))
144 err = UCI_ERR_UNKNOWN;
145
146 if (prefix)
147 fprintf(stderr, "%s: ", prefix);
148 if (ctx->func)
149 fprintf(stderr, "%s: ", ctx->func);
150
151 switch (err) {
152 case UCI_ERR_PARSE:
153 if (ctx->pctx) {
154 fprintf(stderr, "%s (%s) at line %d, byte %d\n", uci_errstr[err], (ctx->pctx->reason ? ctx->pctx->reason : "unknown"), ctx->pctx->line, ctx->pctx->byte);
155 break;
156 }
157 /* fall through */
158 default:
159 fprintf(stderr, "%s\n", uci_errstr[err]);
160 break;
161 }
162 }
163
164 int uci_list_configs(struct uci_context *ctx, char ***list)
165 {
166 UCI_HANDLE_ERR(ctx);
167 UCI_ASSERT(ctx, list != NULL);
168 UCI_ASSERT(ctx, ctx->backend && ctx->backend->list_configs);
169 *list = ctx->backend->list_configs(ctx);
170 return 0;
171 }
172
173 int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
174 {
175 struct uci_package *p;
176 UCI_HANDLE_ERR(ctx);
177 UCI_ASSERT(ctx, package != NULL);
178 p = *package;
179 UCI_ASSERT(ctx, p != NULL);
180 UCI_ASSERT(ctx, p->backend && p->backend->commit);
181 p->backend->commit(ctx, package, overwrite);
182 return 0;
183 }
184
185 int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
186 {
187 struct uci_package *p;
188 UCI_HANDLE_ERR(ctx);
189 UCI_ASSERT(ctx, ctx->backend && ctx->backend->load);
190 p = ctx->backend->load(ctx, name);
191 if (package)
192 *package = p;
193
194 return 0;
195 }
196
197 int uci_set_backend(struct uci_context *ctx, const char *name)
198 {
199 struct uci_element *e;
200
201 UCI_HANDLE_ERR(ctx);
202 UCI_ASSERT(ctx, name != NULL);
203 e = uci_lookup_list(&ctx->backends, name);
204 if (!e)
205 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
206 ctx->backend = uci_to_backend(e);
207 return 0;
208 }