]> arthur.barton.de Git - netatalk.git/blob - include/atalk/errchk.h
Prevent error check macro inflation
[netatalk.git] / include / atalk / errchk.h
1 /*
2    Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
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 ERRCHECK_H
16 #define ERRCHECK_H
17
18 #define EC_INIT int ret = 0
19 #define EC_STATUS(a) ret = (a)
20 #define EC_FAIL ret = -1; goto cleanup
21 #define EC_CLEANUP cleanup
22 #define EC_EXIT return ret
23
24 /* 
25  * Check out doc/DEVELOPER for more infos.
26  *
27  * We have these macros:
28  * EC_ZERO, EC_ZERO_LOG, EC_ZERO_LOG_ERR, EC_ZERO_CUSTOM
29  * EC_NEG1, EC_NEG1_LOG, EC_NEG1_LOG_ERR, EC_NEG1_CUSTOM
30  * EC_NULL, EC_NULL_LOG, EC_NULL_LOG_ERR, EC_NULL_CUSTOM
31  *
32  * A boileplate function template is:
33
34    int func(void)
35    {
36        EC_INIT;
37
38        ...your code here...
39
40        EC_STATUS(0);
41
42    EC_CLEANUP:
43        EC_EXIT;
44    }
45  */
46
47 /* check for return val 0 which is ok, every other is an error, prints errno */
48 #define EC_ZERO_LOG(a)                          \
49     do {                                        \
50         if ((a) != 0) {                                                 \
51             LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \
52             ret = -1;                                                   \
53             goto cleanup;                                               \
54         } \
55     } while (0)
56
57 #define EC_ZERO_LOG_ERR(a, b)                   \
58     do { \
59         if ((a) != 0) {                                                 \
60             LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \
61             ret = (b);                                                  \
62             goto cleanup;                                               \
63         }                                                               \
64     } while (0)
65
66 #define EC_ZERO(a) \
67     do { \
68         if ((a) != 0) { \
69             ret = -1; \
70             goto cleanup; \
71         } \
72     } while (0)
73
74 #define EC_ZERO_ERR(a,b )                       \
75     do {                                        \
76         if ((a) != 0) {                         \
77             ret = b;                            \
78             goto cleanup;                       \
79         }                                       \
80     } while (0)
81
82 /* check for return val 0 which is ok, every other is an error, prints errno */
83 #define EC_NEG1_LOG(a) \
84     do { \
85         if ((a) == -1) { \
86             LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \
87             ret = -1; \
88             goto cleanup; \
89         } \
90     } while (0)
91
92 #define EC_NEG1_LOG_ERR(a, b)                   \
93     do {                                        \
94         if ((a) == -1) {                                                \
95             LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \
96             ret = b;                                                    \
97             goto cleanup;                                               \
98         }                                                               \
99     } while (0)
100
101 /* check for return val 0 which is ok, every other is an error */
102 #define EC_NEG1(a) \
103     do { \
104         if ((a) == -1) { \
105             ret = -1; \
106             goto cleanup; \
107         } \
108     } while (0)
109
110 /* check for return val != NULL, prints errno */
111 #define EC_NULL_LOG(a) \
112     do { \
113         if ((a) == NULL) { \
114             LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \
115             ret = -1; \
116             goto cleanup; \
117         } \
118     } while (0)
119
120 #define EC_NULL_LOG_ERR(a, b)                   \
121     do {                                        \
122         if ((a) == NULL) {                                              \
123             LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \
124             ret = b;                                                    \
125             goto cleanup;                                               \
126         }                                                               \
127     } while (0)
128
129 /* check for return val != NULL */
130 #define EC_NULL(a) \
131     do { \
132         if ((a) == NULL) { \
133             ret = -1; \
134             goto cleanup; \
135         } \
136     } while (0)
137
138 #endif /* ERRCHECK_H */