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