fix uci_parse_tuple
[project/uci.git] / err.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 /*
16 * functions for debug and error handling, for internal use only
17 */
18
19 #ifdef UCI_DEBUG
20 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
21 #else
22 #define DPRINTF(...)
23 #endif
24
25 /*
26 * throw an uci exception and store the error number
27 * in the context.
28 */
29 #define UCI_THROW(ctx, err) do { \
30 DPRINTF("Exception: %s in %s, %s:%d\n", #err, __func__, __FILE__, __LINE__); \
31 longjmp(ctx->trap, err); \
32 } while (0)
33
34 /*
35 * store the return address for handling exceptions
36 * needs to be called in every externally visible library function
37 *
38 * NB: this does not handle recursion at all. Calling externally visible
39 * functions from other uci functions is only allowed at the end of the
40 * calling function, or by wrapping the function call in UCI_TRAP_SAVE
41 * and UCI_TRAP_RESTORE.
42 */
43 #define UCI_HANDLE_ERR(ctx) do { \
44 DPRINTF("ENTER: %s\n", __func__); \
45 int __val = 0; \
46 ctx->errno = 0; \
47 if (!ctx) \
48 return UCI_ERR_INVAL; \
49 if (!ctx->internal) \
50 __val = setjmp(ctx->trap); \
51 ctx->internal = false; \
52 if (__val) { \
53 ctx->errno = __val; \
54 return __val; \
55 } \
56 } while (0)
57
58 /*
59 * In a block enclosed by UCI_TRAP_SAVE and UCI_TRAP_RESTORE, all exceptions
60 * are intercepted and redirected to the label specified in 'handler'
61 * after UCI_TRAP_RESTORE, or when reaching the 'handler' label, the old
62 * exception handler is restored
63 */
64 #define UCI_TRAP_SAVE(ctx, handler) do { \
65 jmp_buf __old_trap; \
66 int __val; \
67 memcpy(__old_trap, ctx->trap, sizeof(ctx->trap)); \
68 __val = setjmp(ctx->trap); \
69 if (__val) { \
70 ctx->errno = __val; \
71 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
72 goto handler; \
73 }
74 #define UCI_TRAP_RESTORE(ctx) \
75 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
76 } while(0)
77
78 /**
79 * UCI_INTERNAL: Do an internal call of a public API function
80 *
81 * Sets Exception handling to passthrough mode.
82 * Allows API functions to change behavior compared to public use
83 */
84 #define UCI_INTERNAL(func, ctx, ...) do { \
85 ctx->internal = true; \
86 func(ctx, __VA_ARGS__); \
87 } while (0)
88
89 /*
90 * check the specified condition.
91 * throw an invalid argument exception if it's false
92 */
93 #define UCI_ASSERT(ctx, expr) do { \
94 if (!(expr)) { \
95 DPRINTF("[%s:%d] Assertion failed\n", __FILE__, __LINE__); \
96 UCI_THROW(ctx, UCI_ERR_INVAL); \
97 } \
98 } while (0)
99