make more functions available externally
[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 struct uci_parse_context
19 {
20         /* error context */
21         const char *reason;
22         int line;
23         int byte;
24
25         /* private: */
26         struct uci_package *package;
27         struct uci_section *section;
28         bool merge;
29         FILE *file;
30         const char *name;
31         char *buf;
32         int bufsz;
33 };
34
35 int uci_add_backend(struct uci_context *ctx, struct uci_backend *b);
36 void uci_add_history(struct uci_context *ctx, struct uci_list *list, int cmd, char *section, char *option, char *value);
37 void uci_free_history(struct uci_history *h);
38
39 /*
40  * functions for debug and error handling, for internal use only
41  */
42
43 #ifdef UCI_DEBUG
44 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
45 #else
46 #define DPRINTF(...)
47 #endif
48
49 /* 
50  * throw an uci exception and store the error number
51  * in the context.
52  */
53 #define UCI_THROW(ctx, err) do {        \
54         DPRINTF("Exception: %s in %s, %s:%d\n", #err, __func__, __FILE__, __LINE__); \
55         longjmp(ctx->trap, err);        \
56 } while (0)
57
58 /*
59  * store the return address for handling exceptions
60  * needs to be called in every externally visible library function
61  *
62  * NB: this does not handle recursion at all. Calling externally visible
63  * functions from other uci functions is only allowed at the end of the
64  * calling function, or by wrapping the function call in UCI_TRAP_SAVE
65  * and UCI_TRAP_RESTORE.
66  */
67 #define UCI_HANDLE_ERR(ctx) do {        \
68         DPRINTF("ENTER: %s\n", __func__); \
69         int __val = 0;                  \
70         ctx->errno = 0;                 \
71         if (!ctx)                       \
72                 return UCI_ERR_INVAL;   \
73         if (!ctx->internal)             \
74                 __val = setjmp(ctx->trap); \
75         ctx->internal = false;          \
76         if (__val) {                    \
77                 DPRINTF("LEAVE: %s, ret=%d\n", __func__, __val); \
78                 ctx->errno = __val;     \
79                 return __val;           \
80         }                               \
81 } while (0)
82
83 /*
84  * In a block enclosed by UCI_TRAP_SAVE and UCI_TRAP_RESTORE, all exceptions
85  * are intercepted and redirected to the label specified in 'handler'
86  * after UCI_TRAP_RESTORE, or when reaching the 'handler' label, the old
87  * exception handler is restored
88  */
89 #define UCI_TRAP_SAVE(ctx, handler) do {   \
90         jmp_buf __old_trap;             \
91         int __val;                      \
92         memcpy(__old_trap, ctx->trap, sizeof(ctx->trap)); \
93         __val = setjmp(ctx->trap);      \
94         if (__val) {                    \
95                 ctx->errno = __val;     \
96                 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
97                 goto handler;           \
98         }
99 #define UCI_TRAP_RESTORE(ctx)           \
100         memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
101 } while(0)
102
103 /**
104  * UCI_INTERNAL: Do an internal call of a public API function
105  * 
106  * Sets Exception handling to passthrough mode.
107  * Allows API functions to change behavior compared to public use
108  */
109 #define UCI_INTERNAL(func, ctx, ...) do { \
110         ctx->internal = true;           \
111         func(ctx, __VA_ARGS__);         \
112 } while (0)
113
114 /*
115  * check the specified condition.
116  * throw an invalid argument exception if it's false
117  */
118 #define UCI_ASSERT(ctx, expr) do {      \
119         if (!(expr)) {                  \
120                 DPRINTF("[%s:%d] Assertion failed\n", __FILE__, __LINE__); \
121                 UCI_THROW(ctx, UCI_ERR_INVAL);  \
122         }                               \
123 } while (0)
124
125 #endif