Add multiple free for each call to lookup_args()
[project/uci.git] / uci_internal.h
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 Lesser General Public License for more details.
13  */
14
15 #ifndef __UCI_INTERNAL_H
16 #define __UCI_INTERNAL_H
17
18 #define __private __attribute__((visibility("hidden")))
19 #define __public
20
21 struct uci_parse_context
22 {
23         /* error context */
24         const char *reason;
25         int line;
26         int byte;
27
28         /* private: */
29         struct uci_package *package;
30         struct uci_section *section;
31         bool merge;
32         FILE *file;
33         const char *name;
34         char *buf;
35         int bufsz;
36 };
37
38 extern const char *uci_confdir;
39 extern const char *uci_savedir;
40
41 __private void *uci_malloc(struct uci_context *ctx, size_t size);
42 __private void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size);
43 __private char *uci_strdup(struct uci_context *ctx, const char *str);
44 __private bool uci_validate_str(const char *str, bool name);
45 __private void uci_add_delta(struct uci_context *ctx, struct uci_list *list, int cmd, const char *section, const char *option, const char *value);
46 __private void uci_free_delta(struct uci_delta *h);
47 __private struct uci_package *uci_alloc_package(struct uci_context *ctx, const char *name);
48
49 __private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write, bool create);
50 __private void uci_close_stream(FILE *stream);
51 __private void uci_getln(struct uci_context *ctx, int offset);
52
53 __private void uci_parse_error(struct uci_context *ctx, char *pos, char *reason);
54 __private void uci_alloc_parse_context(struct uci_context *ctx);
55
56 __private void uci_cleanup(struct uci_context *ctx);
57 __private struct uci_element *uci_lookup_list(struct uci_list *list, const char *name);
58 __private void uci_fixup_section(struct uci_context *ctx, struct uci_section *s);
59 __private void uci_free_package(struct uci_package **package);
60 __private struct uci_element *uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size);
61 __private void uci_free_element(struct uci_element *e);
62 __private struct uci_element *uci_expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete);
63
64 __private int uci_load_delta(struct uci_context *ctx, struct uci_package *p, bool flush);
65
66 static inline bool uci_validate_package(const char *str)
67 {
68         return uci_validate_str(str, false);
69 }
70
71 static inline bool uci_validate_type(const char *str)
72 {
73         return uci_validate_str(str, false);
74 }
75
76 static inline bool uci_validate_name(const char *str)
77 {
78         return uci_validate_str(str, true);
79 }
80
81 /* initialize a list head/item */
82 static inline void uci_list_init(struct uci_list *ptr)
83 {
84         ptr->prev = ptr;
85         ptr->next = ptr;
86 }
87
88 /* inserts a new list entry after a given entry */
89 static inline void uci_list_insert(struct uci_list *list, struct uci_list *ptr)
90 {
91         list->next->prev = ptr;
92         ptr->prev = list;
93         ptr->next = list->next;
94         list->next = ptr;
95 }
96
97 /* inserts a new list entry at the tail of the list */
98 static inline void uci_list_add(struct uci_list *head, struct uci_list *ptr)
99 {
100         /* NB: head->prev points at the tail */
101         uci_list_insert(head->prev, ptr);
102 }
103
104 static inline void uci_list_del(struct uci_list *ptr)
105 {
106         struct uci_list *next, *prev;
107
108         next = ptr->next;
109         prev = ptr->prev;
110
111         prev->next = next;
112         next->prev = prev;
113
114         uci_list_init(ptr);
115 }
116
117
118 extern struct uci_backend uci_file_backend;
119
120 #ifdef UCI_PLUGIN_SUPPORT
121 /**
122  * uci_add_backend: add an extra backend
123  * @ctx: uci context
124  * @name: name of the backend
125  *
126  * The default backend is "file", which uses /etc/config for config storage
127  */
128 __private int uci_add_backend(struct uci_context *ctx, struct uci_backend *b);
129
130 /**
131  * uci_add_backend: add an extra backend
132  * @ctx: uci context
133  * @name: name of the backend
134  *
135  * The default backend is "file", which uses /etc/config for config storage
136  */
137 __private int uci_del_backend(struct uci_context *ctx, struct uci_backend *b);
138 #endif
139
140 #define UCI_BACKEND(_var, _name, ...)   \
141 struct uci_backend _var = {             \
142         .e.list = {                     \
143                 .next = &_var.e.list,   \
144                 .prev = &_var.e.list,   \
145         },                              \
146         .e.name = _name,                \
147         .e.type = UCI_TYPE_BACKEND,     \
148         .ptr = &_var,                   \
149         __VA_ARGS__                     \
150 }
151
152
153 /*
154  * functions for debug and error handling, for internal use only
155  */
156
157 #ifdef UCI_DEBUG
158 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
159 #else
160 #define DPRINTF(...)
161 #endif
162
163 /* 
164  * throw an uci exception and store the error number
165  * in the context.
166  */
167 #define UCI_THROW(ctx, err) do {        \
168         DPRINTF("Exception: %s in %s, %s:%d\n", #err, __func__, __FILE__, __LINE__); \
169         longjmp(ctx->trap, err);        \
170 } while (0)
171
172 /*
173  * store the return address for handling exceptions
174  * needs to be called in every externally visible library function
175  *
176  * NB: this does not handle recursion at all. Calling externally visible
177  * functions from other uci functions is only allowed at the end of the
178  * calling function, or by wrapping the function call in UCI_TRAP_SAVE
179  * and UCI_TRAP_RESTORE.
180  */
181 #define UCI_HANDLE_ERR(ctx) do {        \
182         DPRINTF("ENTER: %s\n", __func__); \
183         int __val = 0;                  \
184         if (!ctx)                       \
185                 return UCI_ERR_INVAL;   \
186         ctx->err = 0;                   \
187         if (!ctx->internal && !ctx->nested) \
188                 __val = setjmp(ctx->trap); \
189         ctx->internal = false;          \
190         ctx->nested = false;            \
191         if (__val) {                    \
192                 DPRINTF("LEAVE: %s, ret=%d\n", __func__, __val); \
193                 ctx->err = __val;       \
194                 return __val;           \
195         }                               \
196 } while (0)
197
198 /*
199  * In a block enclosed by UCI_TRAP_SAVE and UCI_TRAP_RESTORE, all exceptions
200  * are intercepted and redirected to the label specified in 'handler'
201  * after UCI_TRAP_RESTORE, or when reaching the 'handler' label, the old
202  * exception handler is restored
203  */
204 #define UCI_TRAP_SAVE(ctx, handler) do {   \
205         jmp_buf __old_trap;             \
206         int __val;                      \
207         memcpy(__old_trap, ctx->trap, sizeof(ctx->trap)); \
208         __val = setjmp(ctx->trap);      \
209         if (__val) {                    \
210                 ctx->err = __val;       \
211                 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
212                 goto handler;           \
213         }
214 #define UCI_TRAP_RESTORE(ctx)           \
215         memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
216 } while(0)
217
218 /**
219  * UCI_INTERNAL: Do an internal call of a public API function
220  * 
221  * Sets Exception handling to passthrough mode.
222  * Allows API functions to change behavior compared to public use
223  */
224 #define UCI_INTERNAL(func, ctx, ...) do { \
225         ctx->internal = true;           \
226         func(ctx, __VA_ARGS__);         \
227 } while (0)
228
229 /**
230  * UCI_NESTED: Do an normal nested call of a public API function
231  * 
232  * Sets Exception handling to passthrough mode.
233  * Allows API functions to change behavior compared to public use
234  */
235 #define UCI_NESTED(func, ctx, ...) do { \
236         ctx->nested = true;             \
237         func(ctx, __VA_ARGS__);         \
238 } while (0)
239
240
241 /*
242  * check the specified condition.
243  * throw an invalid argument exception if it's false
244  */
245 #define UCI_ASSERT(ctx, expr) do {      \
246         if (!(expr)) {                  \
247                 DPRINTF("[%s:%d] Assertion failed\n", __FILE__, __LINE__); \
248                 UCI_THROW(ctx, UCI_ERR_INVAL);  \
249         }                               \
250 } while (0)
251
252 #endif