2800526a93fdb8a98ff1ffbf6d68d1ba834eec5c
[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
17  */
18
19 #ifdef 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         longjmp(ctx->trap, err);        \
31 } while (0)
32
33 /*
34  * store the return address for handling exceptions
35  * needs to be called in every externally visible library function
36  *
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.
40  */
41 #define UCI_HANDLE_ERR(ctx) do {                \
42         int __val;                      \
43         if (!ctx)                       \
44                 return UCI_ERR_INVAL;   \
45         __val = setjmp(ctx->trap);      \
46         if (__val) {                    \
47                 ctx->errno = __val;     \
48                 return __val;           \
49         }                               \
50 } while (0)
51
52 /*
53  * check the specified condition.
54  * throw an invalid argument exception if it's false
55  */
56 #define UCI_ASSERT(ctx, expr) do {      \
57         if (!(expr)) {                  \
58                 DPRINTF("[%s:%d] Assertion failed\n", __FILE__, __LINE__); \
59                 UCI_THROW(ctx, UCI_ERR_INVAL);  \
60         }                               \
61 } while (0)
62
63