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 General Public License for more details.
15 #ifndef __UCI_INTERNAL_H
16 #define __UCI_INTERNAL_H
19 #ifdef UCI_PLUGIN_SUPPORT
20 #define __plugin extern
22 #define __plugin static
25 struct uci_parse_context
33 struct uci_package
*package
;
34 struct uci_section
*section
;
42 __plugin
void *uci_malloc(struct uci_context
*ctx
, size_t size
);
43 __plugin
void *uci_realloc(struct uci_context
*ctx
, void *ptr
, size_t size
);
44 __plugin
char *uci_strdup(struct uci_context
*ctx
, const char *str
);
45 __plugin
bool uci_validate_str(const char *str
, bool name
);
46 __plugin
void uci_add_history(struct uci_context
*ctx
, struct uci_list
*list
, int cmd
, const char *section
, const char *option
, const char *value
);
47 __plugin
void uci_free_history(struct uci_history
*h
);
48 __plugin
struct uci_package
*uci_alloc_package(struct uci_context
*ctx
, const char *name
);
50 #ifdef UCI_PLUGIN_SUPPORT
52 * uci_add_backend: add an extra backend
54 * @name: name of the backend
56 * The default backend is "file", which uses /etc/config for config storage
58 __plugin
int uci_add_backend(struct uci_context
*ctx
, struct uci_backend
*b
);
61 * uci_add_backend: add an extra backend
63 * @name: name of the backend
65 * The default backend is "file", which uses /etc/config for config storage
67 __plugin
int uci_del_backend(struct uci_context
*ctx
, struct uci_backend
*b
);
70 #define UCI_BACKEND(_var, _name, ...) \
71 struct uci_backend _var = { \
73 .next = &_var.e.list, \
74 .prev = &_var.e.list, \
77 .e.type = UCI_TYPE_BACKEND, \
84 * functions for debug and error handling, for internal use only
88 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
94 * throw an uci exception and store the error number
97 #define UCI_THROW(ctx, err) do { \
98 DPRINTF("Exception: %s in %s, %s:%d\n", #err, __func__, __FILE__, __LINE__); \
99 longjmp(ctx->trap, err); \
103 * store the return address for handling exceptions
104 * needs to be called in every externally visible library function
106 * NB: this does not handle recursion at all. Calling externally visible
107 * functions from other uci functions is only allowed at the end of the
108 * calling function, or by wrapping the function call in UCI_TRAP_SAVE
109 * and UCI_TRAP_RESTORE.
111 #define UCI_HANDLE_ERR(ctx) do { \
112 DPRINTF("ENTER: %s\n", __func__); \
116 return UCI_ERR_INVAL; \
117 if (!ctx->internal && !ctx->nested) \
118 __val = setjmp(ctx->trap); \
119 ctx->internal = false; \
120 ctx->nested = false; \
122 DPRINTF("LEAVE: %s, ret=%d\n", __func__, __val); \
129 * In a block enclosed by UCI_TRAP_SAVE and UCI_TRAP_RESTORE, all exceptions
130 * are intercepted and redirected to the label specified in 'handler'
131 * after UCI_TRAP_RESTORE, or when reaching the 'handler' label, the old
132 * exception handler is restored
134 #define UCI_TRAP_SAVE(ctx, handler) do { \
135 jmp_buf __old_trap; \
137 memcpy(__old_trap, ctx->trap, sizeof(ctx->trap)); \
138 __val = setjmp(ctx->trap); \
141 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
144 #define UCI_TRAP_RESTORE(ctx) \
145 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
149 * UCI_INTERNAL: Do an internal call of a public API function
151 * Sets Exception handling to passthrough mode.
152 * Allows API functions to change behavior compared to public use
154 #define UCI_INTERNAL(func, ctx, ...) do { \
155 ctx->internal = true; \
156 func(ctx, __VA_ARGS__); \
160 * UCI_NESTED: Do an normal nested call of a public API function
162 * Sets Exception handling to passthrough mode.
163 * Allows API functions to change behavior compared to public use
165 #define UCI_NESTED(func, ctx, ...) do { \
166 ctx->nested = true; \
167 func(ctx, __VA_ARGS__); \
172 * check the specified condition.
173 * throw an invalid argument exception if it's false
175 #define UCI_ASSERT(ctx, expr) do { \
177 DPRINTF("[%s:%d] Assertion failed\n", __FILE__, __LINE__); \
178 UCI_THROW(ctx, UCI_ERR_INVAL); \