implement uci rename
[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_DUPLICATE] = "Duplicate entry",
35         [UCI_ERR_UNKNOWN] =   "Unknown error",
36 };
37
38 #include "util.c"
39 #include "list.c"
40 #include "file.c"
41
42 /* exported functions */
43 struct uci_context *uci_alloc_context(void)
44 {
45         struct uci_context *ctx;
46
47         ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
48         memset(ctx, 0, sizeof(struct uci_context));
49         uci_list_init(&ctx->root);
50         ctx->flags = UCI_FLAG_STRICT;
51
52         return ctx;
53 }
54
55 void uci_free_context(struct uci_context *ctx)
56 {
57         struct uci_element *e, *tmp;
58
59         UCI_TRAP_SAVE(ctx, ignore);
60         uci_cleanup(ctx);
61         uci_foreach_element_safe(&ctx->root, tmp, e) {
62                 struct uci_package *p = uci_to_package(e);
63                 uci_free_package(&p);
64         }
65         free(ctx);
66         UCI_TRAP_RESTORE(ctx);
67
68 ignore:
69         return;
70 }
71
72 int uci_cleanup(struct uci_context *ctx)
73 {
74         UCI_HANDLE_ERR(ctx);
75         uci_file_cleanup(ctx);
76         return 0;
77 }
78
79 void uci_perror(struct uci_context *ctx, const char *prefix)
80 {
81         int err;
82
83         if (!ctx)
84                 err = UCI_ERR_INVAL;
85         else
86                 err = ctx->errno;
87
88         if ((err < 0) || (err >= UCI_ERR_LAST))
89                 err = UCI_ERR_UNKNOWN;
90
91         if (prefix)
92                 fprintf(stderr, "%s: ", prefix);
93         if (ctx->func)
94                 fprintf(stderr, "%s: ", ctx->func);
95
96         switch (err) {
97         case UCI_ERR_PARSE:
98                 if (ctx->pctx) {
99                         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);
100                         break;
101                 }
102                 /* fall through */
103         default:
104                 fprintf(stderr, "%s\n", uci_errstr[err]);
105                 break;
106         }
107 }
108
109