ccf29b7ca708f5d143bff88ca457acae90ed518a
[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
25 #include "libuci.h"
26
27 #define DEBUG
28
29 #ifdef DEBUG
30 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
31 #else
32 #define DPRINTF(...)
33 #endif
34 /* 
35  * throw an uci exception and store the error number
36  * in the context.
37  */
38 #define UCI_THROW(ctx, err) do {        \
39         longjmp(ctx->trap, err);        \
40 } while (0)
41
42 /*
43  * store the return address for handling exceptions
44  * needs to be called in every externally visible library function
45  *
46  * NB: this does not handle recursion at all. Calling externally visible
47  * functions from other uci functions is only allowed at the end of the
48  * calling function.
49  */
50 #define UCI_HANDLE_ERR(ctx) do {                \
51         int __val;                      \
52         if (!ctx)                       \
53                 return UCI_ERR_INVAL;   \
54         __val = setjmp(ctx->trap);      \
55         if (__val) {                    \
56                 ctx->errno = __val;     \
57                 return __val;           \
58         }                               \
59 } while (0)
60
61 /*
62  * check the specified condition.
63  * throw an invalid argument exception if it's false
64  */
65 #define UCI_ASSERT(ctx, expr) do {      \
66         if (!(expr)) {                  \
67                 DPRINTF("[%s:%d] Assertion failed\n", __FILE__, __LINE__); \
68                 UCI_THROW(ctx, UCI_ERR_INVAL);  \
69         }                               \
70 } while (0)
71
72
73 static char *uci_errstr[] = {
74         [UCI_OK] =           "Success",
75         [UCI_ERR_MEM] =      "Out of memory",
76         [UCI_ERR_INVAL] =    "Invalid argument",
77         [UCI_ERR_NOTFOUND] = "Entry not found",
78         [UCI_ERR_PARSE] =    "Parse error",
79         [UCI_ERR_UNKNOWN] =  "Unknown error",
80 };
81
82
83 /*
84  * UCI wrapper for malloc, which uses exception handling
85  */
86 static void *uci_malloc(struct uci_context *ctx, size_t size)
87 {
88         void *ptr;
89         
90         ptr = malloc(size);
91         if (!ptr)
92                 UCI_THROW(ctx, UCI_ERR_MEM);
93         memset(ptr, 0, size);
94
95         return ptr;
96 }
97
98 /*
99  * UCI wrapper for realloc, which uses exception handling
100  */
101 static void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size)
102 {
103         ptr = realloc(ptr, size);
104         if (!ptr)
105                 UCI_THROW(ctx, UCI_ERR_MEM);
106
107         return ptr;
108 }
109
110 #include "hash.c"
111 #include "parse.c"
112
113 /* externally visible functions */
114
115 struct uci_context *uci_alloc(void)
116 {
117         struct uci_context *ctx;
118         
119         ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
120         memset(ctx, 0, sizeof(struct uci_context));
121         
122         return ctx;
123 }
124
125 int uci_cleanup(struct uci_context *ctx)
126 {
127         UCI_HANDLE_ERR(ctx);
128         uci_parse_cleanup(ctx);
129         return 0;
130 }
131
132 void uci_perror(struct uci_context *ctx, const char *str)
133 {
134         int err;
135
136         if (!ctx)
137                 err = UCI_ERR_INVAL;
138         else
139                 err = ctx->errno;
140         
141         if ((err < 0) || (err >= UCI_ERR_LAST))
142                 err = UCI_ERR_UNKNOWN;
143
144         switch (err) {
145         case UCI_ERR_PARSE:
146                 if (ctx->pctx) {
147                         fprintf(stderr, "%s: %s at line %d, byte %d\n", str, uci_errstr[err], ctx->pctx->line, ctx->pctx->byte);
148                         break;
149                 }
150                 /* fall through */
151         default:
152                 fprintf(stderr, "%s: %s\n", str, uci_errstr[err]);
153                 break;
154         }
155 }
156
157