118289b3eb77b72e6ac864b28e9dbdb5c7a93354
[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 #include "uci_internal.h"
43 #include "util.c"
44 #include "list.c"
45 #include "history.c"
46 #include "file.c"
47
48 /* exported functions */
49 struct uci_context *uci_alloc_context(void)
50 {
51         struct uci_context *ctx;
52
53         ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
54         memset(ctx, 0, sizeof(struct uci_context));
55         uci_list_init(&ctx->root);
56         uci_list_init(&ctx->history_path);
57         ctx->flags = UCI_FLAG_STRICT;
58
59         ctx->confdir = (char *) uci_confdir;
60         ctx->savedir = (char *) uci_savedir;
61
62         return ctx;
63 }
64
65 void uci_free_context(struct uci_context *ctx)
66 {
67         struct uci_element *e, *tmp;
68
69         if (ctx->confdir != uci_confdir)
70                 free(ctx->confdir);
71         if (ctx->savedir != uci_savedir)
72                 free(ctx->savedir);
73
74         UCI_TRAP_SAVE(ctx, ignore);
75         ctx->internal = true;
76         uci_cleanup(ctx);
77         uci_foreach_element_safe(&ctx->root, tmp, e) {
78                 struct uci_package *p = uci_to_package(e);
79                 uci_free_package(&p);
80         }
81         uci_foreach_element_safe(&ctx->history_path, tmp, e) {
82                 uci_free_element(e);
83         }
84         free(ctx);
85         UCI_TRAP_RESTORE(ctx);
86
87 ignore:
88         return;
89 }
90
91 int uci_set_confdir(struct uci_context *ctx, const char *dir)
92 {
93         char *cdir;
94
95         UCI_HANDLE_ERR(ctx);
96         UCI_ASSERT(ctx, dir != NULL);
97
98         cdir = uci_strdup(ctx, dir);
99         if (ctx->confdir != uci_confdir)
100                 free(ctx->confdir);
101         ctx->confdir = cdir;
102         return 0;
103 }
104
105 int uci_cleanup(struct uci_context *ctx)
106 {
107         struct uci_parse_context *pctx;
108         UCI_HANDLE_ERR(ctx);
109
110         if (ctx->buf) {
111                 free(ctx->buf);
112                 ctx->buf = NULL;
113                 ctx->bufsz = 0;
114         }
115
116         pctx = ctx->pctx;
117         if (!pctx)
118                 goto done;
119
120         ctx->pctx = NULL;
121         if (pctx->package)
122                 uci_free_package(&pctx->package);
123
124         if (pctx->buf)
125                 free(pctx->buf);
126
127         free(pctx);
128 done:
129         return 0;
130 }
131
132 void uci_perror(struct uci_context *ctx, const char *prefix)
133 {
134         int err;
135
136         if (!ctx)
137                 err = UCI_ERR_INVAL;
138         else
139                 err = ctx->errno;
140
141         if ((err < 0) || (err >= UCI_ERR_LAST))
142                 err = UCI_ERR_UNKNOWN;
143
144         if (prefix)
145                 fprintf(stderr, "%s: ", prefix);
146         if (ctx->func)
147                 fprintf(stderr, "%s: ", ctx->func);
148
149         switch (err) {
150         case UCI_ERR_PARSE:
151                 if (ctx->pctx) {
152                         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);
153                         break;
154                 }
155                 /* fall through */
156         default:
157                 fprintf(stderr, "%s\n", uci_errstr[err]);
158                 break;
159         }
160 }
161
162