more plugin support
[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 General Public License for more details.
13 */
14
15 #ifndef __UCI_INTERNAL_H
16 #define __UCI_INTERNAL_H
17
18 #define __public
19 #ifdef UCI_PLUGIN_SUPPORT
20 #define __plugin extern
21 #else
22 #define __plugin static
23 #endif
24
25 struct uci_parse_context
26 {
27 /* error context */
28 const char *reason;
29 int line;
30 int byte;
31
32 /* private: */
33 struct uci_package *package;
34 struct uci_section *section;
35 bool merge;
36 FILE *file;
37 const char *name;
38 char *buf;
39 int bufsz;
40 };
41
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 void uci_add_history(struct uci_context *ctx, struct uci_list *list, int cmd, char *section, char *option, char *value);
46 __plugin void uci_free_history(struct uci_history *h);
47 __plugin struct uci_package *uci_alloc_package(struct uci_context *ctx, const char *name);
48
49 #ifdef UCI_PLUGIN_SUPPORT
50 /**
51 * uci_add_backend: add an extra backend
52 * @ctx: uci context
53 * @name: name of the backend
54 *
55 * The default backend is "file", which uses /etc/config for config storage
56 */
57 __plugin int uci_add_backend(struct uci_context *ctx, struct uci_backend *b);
58
59 /**
60 * uci_add_backend: add an extra backend
61 * @ctx: uci context
62 * @name: name of the backend
63 *
64 * The default backend is "file", which uses /etc/config for config storage
65 */
66 __plugin int uci_del_backend(struct uci_context *ctx, struct uci_backend *b);
67 #endif
68
69 #define UCI_BACKEND(_var, _name, ...) \
70 struct uci_backend _var = { \
71 .e.list = { \
72 .next = &_var.e.list, \
73 .prev = &_var.e.list, \
74 }, \
75 .e.name = _name, \
76 .e.type = UCI_TYPE_BACKEND, \
77 .ptr = &_var, \
78 __VA_ARGS__ \
79 }
80
81
82 /*
83 * functions for debug and error handling, for internal use only
84 */
85
86 #ifdef UCI_DEBUG
87 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
88 #else
89 #define DPRINTF(...)
90 #endif
91
92 /*
93 * throw an uci exception and store the error number
94 * in the context.
95 */
96 #define UCI_THROW(ctx, err) do { \
97 DPRINTF("Exception: %s in %s, %s:%d\n", #err, __func__, __FILE__, __LINE__); \
98 longjmp(ctx->trap, err); \
99 } while (0)
100
101 /*
102 * store the return address for handling exceptions
103 * needs to be called in every externally visible library function
104 *
105 * NB: this does not handle recursion at all. Calling externally visible
106 * functions from other uci functions is only allowed at the end of the
107 * calling function, or by wrapping the function call in UCI_TRAP_SAVE
108 * and UCI_TRAP_RESTORE.
109 */
110 #define UCI_HANDLE_ERR(ctx) do { \
111 DPRINTF("ENTER: %s\n", __func__); \
112 int __val = 0; \
113 ctx->errno = 0; \
114 if (!ctx) \
115 return UCI_ERR_INVAL; \
116 if (!ctx->internal) \
117 __val = setjmp(ctx->trap); \
118 ctx->internal = false; \
119 if (__val) { \
120 DPRINTF("LEAVE: %s, ret=%d\n", __func__, __val); \
121 ctx->errno = __val; \
122 return __val; \
123 } \
124 } while (0)
125
126 /*
127 * In a block enclosed by UCI_TRAP_SAVE and UCI_TRAP_RESTORE, all exceptions
128 * are intercepted and redirected to the label specified in 'handler'
129 * after UCI_TRAP_RESTORE, or when reaching the 'handler' label, the old
130 * exception handler is restored
131 */
132 #define UCI_TRAP_SAVE(ctx, handler) do { \
133 jmp_buf __old_trap; \
134 int __val; \
135 memcpy(__old_trap, ctx->trap, sizeof(ctx->trap)); \
136 __val = setjmp(ctx->trap); \
137 if (__val) { \
138 ctx->errno = __val; \
139 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
140 goto handler; \
141 }
142 #define UCI_TRAP_RESTORE(ctx) \
143 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
144 } while(0)
145
146 /**
147 * UCI_INTERNAL: Do an internal call of a public API function
148 *
149 * Sets Exception handling to passthrough mode.
150 * Allows API functions to change behavior compared to public use
151 */
152 #define UCI_INTERNAL(func, ctx, ...) do { \
153 ctx->internal = true; \
154 func(ctx, __VA_ARGS__); \
155 } while (0)
156
157 /*
158 * check the specified condition.
159 * throw an invalid argument exception if it's false
160 */
161 #define UCI_ASSERT(ctx, expr) do { \
162 if (!(expr)) { \
163 DPRINTF("[%s:%d] Assertion failed\n", __FILE__, __LINE__); \
164 UCI_THROW(ctx, UCI_ERR_INVAL); \
165 } \
166 } while (0)
167
168 #endif