92205e8a0492d995352549b432f961c36701f7e9
[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 #include <sys/types.h>
20 #include <stdbool.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include "uci.h"
25
26 #define DEBUG
27 #include "err.h"
28
29 static const char *uci_errstr[] = {
30         [UCI_OK] =           "Success",
31         [UCI_ERR_MEM] =      "Out of memory",
32         [UCI_ERR_INVAL] =    "Invalid argument",
33         [UCI_ERR_NOTFOUND] = "Entry not found",
34         [UCI_ERR_IO] =       "I/O error",
35         [UCI_ERR_PARSE] =    "Parse error",
36         [UCI_ERR_UNKNOWN] =  "Unknown error",
37 };
38
39
40 /*
41  * UCI wrapper for malloc, which uses exception handling
42  */
43 static void *uci_malloc(struct uci_context *ctx, size_t size)
44 {
45         void *ptr;
46
47         ptr = malloc(size);
48         if (!ptr)
49                 UCI_THROW(ctx, UCI_ERR_MEM);
50         memset(ptr, 0, size);
51
52         return ptr;
53 }
54
55 /*
56  * UCI wrapper for realloc, which uses exception handling
57  */
58 static void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size)
59 {
60         ptr = realloc(ptr, size);
61         if (!ptr)
62                 UCI_THROW(ctx, UCI_ERR_MEM);
63
64         return ptr;
65 }
66
67 /*
68  * UCI wrapper for strdup, which uses exception handling
69  */
70 static char *uci_strdup(struct uci_context *ctx, const char *str)
71 {
72         char *ptr;
73
74         ptr = strdup(str);
75         if (!ptr)
76                 UCI_THROW(ctx, UCI_ERR_MEM);
77
78         return ptr;
79 }
80
81 #include "list.c"
82 #include "file.c"
83
84 /* externally visible functions */
85
86 struct uci_context *uci_alloc(void)
87 {
88         struct uci_context *ctx;
89
90         ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
91         memset(ctx, 0, sizeof(struct uci_context));
92         uci_list_init(&ctx->root);
93
94         return ctx;
95 }
96
97 void uci_free(struct uci_context *ctx)
98 {
99         struct uci_element *e, *tmp;
100
101         uci_cleanup(ctx);
102         uci_foreach_element_safe(&ctx->root, tmp, e) {
103                 uci_free_package(uci_to_package(e));
104         }
105         free(ctx);
106         return;
107 }
108
109 int uci_cleanup(struct uci_context *ctx)
110 {
111         UCI_HANDLE_ERR(ctx);
112         uci_file_cleanup(ctx);
113         return 0;
114 }
115
116 void uci_perror(struct uci_context *ctx, const char *str)
117 {
118         int err;
119
120         if (!ctx)
121                 err = UCI_ERR_INVAL;
122         else
123                 err = ctx->errno;
124
125         if ((err < 0) || (err >= UCI_ERR_LAST))
126                 err = UCI_ERR_UNKNOWN;
127
128         switch (err) {
129         case UCI_ERR_PARSE:
130                 if (ctx->pctx) {
131                         fprintf(stderr, "%s: %s (%s) at line %d, byte %d\n", str, uci_errstr[err], (ctx->pctx->reason ? ctx->pctx->reason : "unknown"), ctx->pctx->line, ctx->pctx->byte);
132                         break;
133                 }
134                 /* fall through */
135         default:
136                 fprintf(stderr, "%s: %s\n", str, uci_errstr[err]);
137                 break;
138         }
139 }
140
141