cli: do not return 0 if the command was invalid
[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 __private __attribute__((visibility("hidden")))
19 #define __public
20 #ifdef UCI_PLUGIN_SUPPORT
21 #define __plugin extern
22 #else
23 #define __plugin __private
24 #endif
25
26 struct uci_parse_context
27 {
28         /* error context */
29         const char *reason;
30         int line;
31         int byte;
32
33         /* private: */
34         struct uci_package *package;
35         struct uci_section *section;
36         bool merge;
37         FILE *file;
38         const char *name;
39         char *buf;
40         int bufsz;
41 };
42
43 extern const char *uci_confdir;
44 extern const char *uci_savedir;
45
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);
53
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);
57
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);
60
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);
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);
74 }
75
76 static inline bool uci_validate_type(const char *str)
77 {
78         return uci_validate_str(str, false);
79 }
80
81 static inline bool uci_validate_name(const char *str)
82 {
83         return uci_validate_str(str, true);
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 __plugin 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 __plugin 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         ctx->err = 0;                   \
190         if (!ctx)                       \
191                 return UCI_ERR_INVAL;   \
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         }
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