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