some backend abstraction
[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         ctx->flags = UCI_FLAG_STRICT;
60
61         ctx->confdir = (char *) uci_confdir;
62         ctx->savedir = (char *) uci_savedir;
63         ctx->backend = &uci_file_backend;
64
65         return ctx;
66 }
67
68 void uci_free_context(struct uci_context *ctx)
69 {
70         struct uci_element *e, *tmp;
71
72         if (ctx->confdir != uci_confdir)
73                 free(ctx->confdir);
74         if (ctx->savedir != uci_savedir)
75                 free(ctx->savedir);
76
77         uci_cleanup(ctx);
78         UCI_TRAP_SAVE(ctx, ignore);
79         uci_foreach_element_safe(&ctx->root, tmp, e) {
80                 struct uci_package *p = uci_to_package(e);
81                 uci_free_package(&p);
82         }
83         uci_foreach_element_safe(&ctx->history_path, tmp, e) {
84                 uci_free_element(e);
85         }
86         free(ctx);
87         UCI_TRAP_RESTORE(ctx);
88
89 ignore:
90         return;
91 }
92
93 int uci_set_confdir(struct uci_context *ctx, const char *dir)
94 {
95         char *cdir;
96
97         UCI_HANDLE_ERR(ctx);
98         UCI_ASSERT(ctx, dir != NULL);
99
100         cdir = uci_strdup(ctx, dir);
101         if (ctx->confdir != uci_confdir)
102                 free(ctx->confdir);
103         ctx->confdir = cdir;
104         return 0;
105 }
106
107 static void uci_cleanup(struct uci_context *ctx)
108 {
109         struct uci_parse_context *pctx;
110
111         if (ctx->buf) {
112                 free(ctx->buf);
113                 ctx->buf = NULL;
114                 ctx->bufsz = 0;
115         }
116
117         pctx = ctx->pctx;
118         if (!pctx)
119                 return;
120
121         ctx->pctx = NULL;
122         if (pctx->package)
123                 uci_free_package(&pctx->package);
124
125         if (pctx->buf)
126                 free(pctx->buf);
127
128         free(pctx);
129 }
130
131 void uci_perror(struct uci_context *ctx, const char *prefix)
132 {
133         int err;
134
135         if (!ctx)
136                 err = UCI_ERR_INVAL;
137         else
138                 err = ctx->errno;
139
140         if ((err < 0) || (err >= UCI_ERR_LAST))
141                 err = UCI_ERR_UNKNOWN;
142
143         if (prefix)
144                 fprintf(stderr, "%s: ", prefix);
145         if (ctx->func)
146                 fprintf(stderr, "%s: ", ctx->func);
147
148         switch (err) {
149         case UCI_ERR_PARSE:
150                 if (ctx->pctx) {
151                         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);
152                         break;
153                 }
154                 /* fall through */
155         default:
156                 fprintf(stderr, "%s\n", uci_errstr[err]);
157                 break;
158         }
159 }
160
161 int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
162 {
163         UCI_HANDLE_ERR(ctx);
164         UCI_ASSERT(ctx, package != NULL);
165         UCI_ASSERT(ctx, *package != NULL);
166         UCI_ASSERT(ctx, ctx->backend && ctx->backend->commit);
167         ctx->backend->commit(ctx, package, overwrite);
168         return 0;
169 }
170
171 int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
172 {
173         struct uci_package *p;
174         UCI_HANDLE_ERR(ctx);
175         UCI_ASSERT(ctx, ctx->backend && ctx->backend->load);
176         p = ctx->backend->load(ctx, name);
177         if (package)
178                 *package = p;
179
180         return 0;
181 }
182
183