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