2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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
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.
15 #ifndef __UCI_INTERNAL_H
16 #define __UCI_INTERNAL_H
18 #define __private __attribute__((visibility("hidden")))
20 #ifdef UCI_PLUGIN_SUPPORT
21 #define __plugin extern
23 #define __plugin __private
26 struct uci_parse_context
34 struct uci_package
*package
;
35 struct uci_section
*section
;
43 extern const char *uci_confdir
;
44 extern const char *uci_savedir
;
46 __plugin
void *uci_malloc(struct uci_context
*ctx
, size_t size
);
47 __plugin
void *uci_realloc(struct uci_context
*ctx
, void *ptr
, size_t size
);
48 __plugin
char *uci_strdup(struct uci_context
*ctx
, const char *str
);
49 __plugin
bool uci_validate_str(const char *str
, bool name
);
50 __plugin
void uci_add_delta(struct uci_context
*ctx
, struct uci_list
*list
, int cmd
, const char *section
, const char *option
, const char *value
);
51 __plugin
void uci_free_delta(struct uci_delta
*h
);
52 __plugin
struct uci_package
*uci_alloc_package(struct uci_context
*ctx
, const char *name
);
54 __private
FILE *uci_open_stream(struct uci_context
*ctx
, const char *filename
, int pos
, bool write
, bool create
);
55 __private
void uci_close_stream(FILE *stream
);
56 __private
void uci_getln(struct uci_context
*ctx
, int offset
);
58 __private
void uci_parse_error(struct uci_context
*ctx
, char *pos
, char *reason
);
59 __private
void uci_alloc_parse_context(struct uci_context
*ctx
);
61 __private
void uci_cleanup(struct uci_context
*ctx
);
62 __private
struct uci_element
*uci_lookup_list(struct uci_list
*list
, const char *name
);
63 __private
void uci_fixup_section(struct uci_context
*ctx
, struct uci_section
*s
);
64 __private
void uci_free_package(struct uci_package
**package
);
65 __private
struct uci_element
*uci_alloc_generic(struct uci_context
*ctx
, int type
, const char *name
, int size
);
66 __private
void uci_free_element(struct uci_element
*e
);
67 __private
struct uci_element
*uci_expand_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, bool complete
);
69 __private
int uci_load_delta(struct uci_context
*ctx
, struct uci_package
*p
, bool flush
);
71 static inline bool uci_validate_package(const char *str
)
73 return uci_validate_str(str
, false);
76 static inline bool uci_validate_type(const char *str
)
78 return uci_validate_str(str
, false);
81 static inline bool uci_validate_name(const char *str
)
83 return uci_validate_str(str
, true);
86 /* initialize a list head/item */
87 static inline void uci_list_init(struct uci_list
*ptr
)
93 /* inserts a new list entry after a given entry */
94 static inline void uci_list_insert(struct uci_list
*list
, struct uci_list
*ptr
)
96 list
->next
->prev
= ptr
;
98 ptr
->next
= list
->next
;
102 /* inserts a new list entry at the tail of the list */
103 static inline void uci_list_add(struct uci_list
*head
, struct uci_list
*ptr
)
105 /* NB: head->prev points at the tail */
106 uci_list_insert(head
->prev
, ptr
);
109 static inline void uci_list_del(struct uci_list
*ptr
)
111 struct uci_list
*next
, *prev
;
123 extern struct uci_backend uci_file_backend
;
125 #ifdef UCI_PLUGIN_SUPPORT
127 * uci_add_backend: add an extra backend
129 * @name: name of the backend
131 * The default backend is "file", which uses /etc/config for config storage
133 __plugin
int uci_add_backend(struct uci_context
*ctx
, struct uci_backend
*b
);
136 * uci_add_backend: add an extra backend
138 * @name: name of the backend
140 * The default backend is "file", which uses /etc/config for config storage
142 __plugin
int uci_del_backend(struct uci_context
*ctx
, struct uci_backend
*b
);
145 #define UCI_BACKEND(_var, _name, ...) \
146 struct uci_backend _var = { \
148 .next = &_var.e.list, \
149 .prev = &_var.e.list, \
152 .e.type = UCI_TYPE_BACKEND, \
159 * functions for debug and error handling, for internal use only
163 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
169 * throw an uci exception and store the error number
172 #define UCI_THROW(ctx, err) do { \
173 DPRINTF("Exception: %s in %s, %s:%d\n", #err, __func__, __FILE__, __LINE__); \
174 longjmp(ctx->trap, err); \
178 * store the return address for handling exceptions
179 * needs to be called in every externally visible library function
181 * NB: this does not handle recursion at all. Calling externally visible
182 * functions from other uci functions is only allowed at the end of the
183 * calling function, or by wrapping the function call in UCI_TRAP_SAVE
184 * and UCI_TRAP_RESTORE.
186 #define UCI_HANDLE_ERR(ctx) do { \
187 DPRINTF("ENTER: %s\n", __func__); \
191 return UCI_ERR_INVAL; \
192 if (!ctx->internal && !ctx->nested) \
193 __val = setjmp(ctx->trap); \
194 ctx->internal = false; \
195 ctx->nested = false; \
197 DPRINTF("LEAVE: %s, ret=%d\n", __func__, __val); \
204 * In a block enclosed by UCI_TRAP_SAVE and UCI_TRAP_RESTORE, all exceptions
205 * are intercepted and redirected to the label specified in 'handler'
206 * after UCI_TRAP_RESTORE, or when reaching the 'handler' label, the old
207 * exception handler is restored
209 #define UCI_TRAP_SAVE(ctx, handler) do { \
210 jmp_buf __old_trap; \
212 memcpy(__old_trap, ctx->trap, sizeof(ctx->trap)); \
213 __val = setjmp(ctx->trap); \
216 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
219 #define UCI_TRAP_RESTORE(ctx) \
220 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
224 * UCI_INTERNAL: Do an internal call of a public API function
226 * Sets Exception handling to passthrough mode.
227 * Allows API functions to change behavior compared to public use
229 #define UCI_INTERNAL(func, ctx, ...) do { \
230 ctx->internal = true; \
231 func(ctx, __VA_ARGS__); \
235 * UCI_NESTED: Do an normal nested call of a public API function
237 * Sets Exception handling to passthrough mode.
238 * Allows API functions to change behavior compared to public use
240 #define UCI_NESTED(func, ctx, ...) do { \
241 ctx->nested = true; \
242 func(ctx, __VA_ARGS__); \
247 * check the specified condition.
248 * throw an invalid argument exception if it's false
250 #define UCI_ASSERT(ctx, expr) do { \
252 DPRINTF("[%s:%d] Assertion failed\n", __FILE__, __LINE__); \
253 UCI_THROW(ctx, UCI_ERR_INVAL); \