2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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
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.
16 * functions for debug and error handling, for internal use only
20 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
26 * throw an uci exception and store the error number
29 #define UCI_THROW(ctx, err) do { \
30 longjmp(ctx->trap, err); \
34 * store the return address for handling exceptions
35 * needs to be called in every externally visible library function
37 * NB: this does not handle recursion at all. Calling externally visible
38 * functions from other uci functions is only allowed at the end of the
39 * calling function, or by wrapping the function call in UCI_TRAP_SAVE
40 * and UCI_TRAP_RESTORE.
42 #define UCI_HANDLE_ERR(ctx) do { \
45 return UCI_ERR_INVAL; \
46 __val = setjmp(ctx->trap); \
54 * In a block enclosed by UCI_TRAP_SAVE and UCI_TRAP_RESTORE, all exceptions
55 * are intercepted and redirected to the label specified in 'handler'
56 * after UCI_TRAP_RESTORE, or when reaching the 'handler' label, the old
57 * exception handler is restored
59 #define UCI_TRAP_SAVE(ctx, handler) do { \
62 memcpy(__old_trap, ctx->trap, sizeof(ctx->trap)); \
63 __val = setjmp(ctx->trap); \
66 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
69 #define UCI_TRAP_RESTORE(ctx) \
70 memcpy(ctx->trap, __old_trap, sizeof(ctx->trap)); \
74 * check the specified condition.
75 * throw an invalid argument exception if it's false
77 #define UCI_ASSERT(ctx, expr) do { \
79 DPRINTF("[%s:%d] Assertion failed\n", __FILE__, __LINE__); \
80 UCI_THROW(ctx, UCI_ERR_INVAL); \