file: fix segfault in uci_parse_option
[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_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);
68
69 __private int uci_load_delta(struct uci_context *ctx, struct uci_package *p, bool flush);
70
71 static inline bool uci_validate_package(const char *str)
72 {
73 return uci_validate_str(str, false, true);
74 }
75
76 static inline bool uci_validate_type(const char *str)
77 {
78 return uci_validate_str(str, false, false);
79 }
80
81 static inline bool uci_validate_name(const char *str)
82 {
83 return uci_validate_str(str, true, false);
84 }
85
86 /* initialize a list head/item */
87 static inline void uci_list_init(struct uci_list *ptr)
88 {
89 ptr->prev = ptr;
90 ptr->next = ptr;
91 }
92
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)
95 {
96 list->next->prev = ptr;
97 ptr->prev = list;
98 ptr->next = list->next;
99 list->next = ptr;
100 }
101
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)
104 {
105 /* NB: head->prev points at the tail */
106 uci_list_insert(head->prev, ptr);
107 }
108
109 static inline void uci_list_del(struct uci_list *ptr)
110 {
111 struct uci_list *next, *prev;
112
113 next = ptr->next;
114 prev = ptr->prev;
115
116 prev->next = next;
117 next->prev = prev;
118
119 uci_list_init(ptr);
120 }
121
122
123 extern struct uci_backend uci_file_backend;
124
125 #ifdef UCI_PLUGIN_SUPPORT
126 /**
127 * uci_add_backend: add an extra backend
128 * @ctx: uci context
129 * @name: name of the backend
130 *
131 * The default backend is "file", which uses /etc/config for config storage
132 */
133 __private int uci_add_backend(struct uci_context *ctx, struct uci_backend *b);
134
135 /**
136 * uci_add_backend: add an extra backend
137 * @ctx: uci context
138 * @name: name of the backend
139 *
140 * The default backend is "file", which uses /etc/config for config storage
141 */
142 __private int uci_del_backend(struct uci_context *ctx, struct uci_backend *b);
143 #endif
144
145 #define UCI_BACKEND(_var, _name, ...) \
146 struct uci_backend _var = { \
147 .e.list = { \
148 .next = &_var.e.list, \
149 .prev = &_var.e.list, \
150 }, \
151 .e.name = _name, \
152 .e.type = UCI_TYPE_BACKEND, \
153 .ptr = &_var, \
154 __VA_ARGS__ \
155 }
156
157
158 /*
159 * functions for debug and error handling, for internal use only
160 */
161
162 #ifdef UCI_DEBUG
163 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
164 #else
165 #define DPRINTF(...)
166 #endif
167
168 /*
169 * throw an uci exception and store the error number
170 * in the context.
171 */
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); \
175 } while (0)
176
177 /*
178 * store the return address for handling exceptions
179 * needs to be called in every externally visible library function
180 *
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.
185 */
186 #define UCI_HANDLE_ERR(ctx) do { \
187 DPRINTF("ENTER: %s\n", __func__); \
188 int __val = 0; \
189 if (!ctx) \
190 return UCI_ERR_INVAL; \
191 ctx->err = 0; \
192 if (!ctx->internal && !ctx->nested) \
193 __val = setjmp(ctx->trap); \
194 ctx->internal = false; \
195 ctx->nested = false; \
196 if (__val) { \
197 DPRINTF("LEAVE: %s, ret=%d\n", __func__, __val); \
198 ctx->err = __val; \
199 return __val; \
200 } \
201 } while (0)
202
203 /*
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
208 */
209 #define UCI_TRAP_SAVE(ctx, handler) do { \
210 jmp_buf __old_trap; \
211 int __val; \
212 memcpy(__old_trap, ctx->trap, sizeof(ctx->trap)); \
213 __val = setjmp(ctx->trap); \
214 if (__val) { \
215 ctx->err = __val; \
216 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
217 goto handler; \
218 } while(0)
219 #define UCI_TRAP_RESTORE(ctx) \
220 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
221 } while(0)
222
223 /**
224 * UCI_INTERNAL: Do an internal call of a public API function
225 *
226 * Sets Exception handling to passthrough mode.
227 * Allows API functions to change behavior compared to public use
228 */
229 #define UCI_INTERNAL(func, ctx, ...) do { \
230 ctx->internal = true; \
231 func(ctx, __VA_ARGS__); \
232 } while (0)
233
234 /**
235 * UCI_NESTED: Do an normal nested call of a public API function
236 *
237 * Sets Exception handling to passthrough mode.
238 * Allows API functions to change behavior compared to public use
239 */
240 #define UCI_NESTED(func, ctx, ...) do { \
241 ctx->nested = true; \
242 func(ctx, __VA_ARGS__); \
243 } while (0)
244
245
246 /*
247 * check the specified condition.
248 * throw an invalid argument exception if it's false
249 */
250 #define UCI_ASSERT(ctx, expr) do { \
251 if (!(expr)) { \
252 DPRINTF("[%s:%d] Assertion failed\n", __FILE__, __LINE__); \
253 UCI_THROW(ctx, UCI_ERR_INVAL); \
254 } \
255 } while (0)
256
257 #endif