2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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
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.
16 * This file contains some common code for the uci library
19 #include <sys/types.h>
29 static const char *uci_errstr[] = {
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",
41 * UCI wrapper for malloc, which uses exception handling
43 static void *uci_malloc(struct uci_context *ctx, size_t size)
49 UCI_THROW(ctx, UCI_ERR_MEM);
56 * UCI wrapper for realloc, which uses exception handling
58 static void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size)
60 ptr = realloc(ptr, size);
62 UCI_THROW(ctx, UCI_ERR_MEM);
68 * UCI wrapper for strdup, which uses exception handling
70 static char *uci_strdup(struct uci_context *ctx, const char *str)
76 UCI_THROW(ctx, UCI_ERR_MEM);
84 /* externally visible functions */
86 struct uci_context *uci_alloc(void)
88 struct uci_context *ctx;
90 ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
91 memset(ctx, 0, sizeof(struct uci_context));
92 uci_list_init(&ctx->root);
97 void uci_free(struct uci_context *ctx)
99 struct uci_config *cfg;
102 uci_foreach_entry(config, &ctx->root, cfg) {
103 uci_drop_config(cfg);
109 int uci_cleanup(struct uci_context *ctx)
112 uci_parse_cleanup(ctx);
116 void uci_perror(struct uci_context *ctx, const char *str)
125 if ((err < 0) || (err >= UCI_ERR_LAST))
126 err = UCI_ERR_UNKNOWN;
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);
136 fprintf(stderr, "%s: %s\n", str, uci_errstr[err]);