ze-filter  (ze-filter-0.8.0-develop-180218)
zeMacros.h
Go to the documentation of this file.
1 /*
2  *
3  * ze-filter - Mail Server Filter for sendmail
4  *
5  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
6  *
7  * Auteur : Jose Marcio Martins da Cruz
8  * jose.marcio.mc@gmail.org
9  *
10  * Historique :
11  * Creation : janvier 2002
12  *
13  * This program is free software, but with restricted license :
14  *
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * More details about ze-filter license can be found at ze-filter
21  * web site : http://foss.jose-marcio.org
22  */
23 
24 
25 #ifndef __ZE_MACROS_H
26 
27 #define ASSERT(a) assert((a))
28 
29 #if !OS_LINUX
30 # define WAIT_NOHANG(pid,stat) waitpid(pid, stat, WNOHANG)
31 # define WAIT_HANG(pid,stat) waitpid(pid, stat, 0)
32 #else
33 # define WAIT_NOHANG(pid,stat) waitpid(pid, stat, WNOHANG | __WALL)
34 # define WAIT_HANG(pid,stat) waitpid(pid, stat, __WALL)
35 #endif
36 
37 #define FREE(x) \
38  do { \
39  if (x != NULL) \
40  free(x); \
41  x = NULL; \
42  } while (0)
43 
44 
45 #define FD_PRINTF(fdp, ...) \
46  do { \
47  if (fdp >= 0) { \
48  char fdp_str[1024]; \
49  (void ) snprintf(fdp_str, sizeof(fdp_str), __VA_ARGS__); \
50  if (write (fdp, fdp_str, strlen(fdp_str)) != strlen(fdp_str)) \
51  ZE_LogSysError("error on FD_PRINTF"); \
52  } else \
53  printf(__VA_ARGS__); \
54  } while (0)
55 
56 #define SD_PRINTF(fdp, ...) \
57  do { \
58  if (fdp >= 0) { \
59  char fdp_str[1024]; \
60  (void ) snprintf(fdp_str, sizeof(fdp_str), __VA_ARGS__); \
61  if (sendto(fdp, fdp_str, strlen(fdp_str), 0, NULL, 0) != strlen(fdp_str)) \
62  ZE_LogSysError("error on SD_PRINTF"); \
63  } else \
64  printf(__VA_ARGS__); \
65  } while (0)
66 
67 #if 0
68 #define STRLCPY(d,s,sz) snprintf(d,sz,"%s",s)
69 #endif
70 
71 #if 1
72 #define STRCASEEQUAL(a,b) \
73  ((a) != NULL && (b) != NULL ? strcasecmp((a),(b)) == 0 : ((a) == (b)))
74 
75 #define STRNCASEEQUAL(a,b,n) \
76  ((a) != NULL && (b) != NULL ? strncasecmp((a),(b),(n)) == 0 : ((a) == (b)))
77 
78 #define STREQUAL(a,b) \
79  ((a) != NULL && (b) != NULL ? strcmp((a),(b)) == 0 : ((a) == (b)))
80 
81 #define STRNULL(x,r) ((x) != NULL ? (x) : (r))
82 #define STREMPTY(x,r) ((x) != NULL && strlen(x) > 0 ? (x) : (r))
83 
84 #define ISSTRNULL(x) ((x) == NULL)
85 #define ISSTREMPTY(x) ((x) == NULL || strlen(x) == 0)
86 
87 #define STRBOOL(x,t,f) ((x) ? t : f)
88 
89 #endif
90 
91 #define SIGN(x) ((x) < 0 ? - 1 : 1)
92 
93 #define MUTEX_LOCK(mutex) \
94  { \
95  int r = 0; \
96  if ((r = pthread_mutex_lock(mutex)) != 0) { \
97  ZE_LogSysError("pthread_mutex_lock : %s", strerror(r)); \
98  } \
99  }
100 
101 #define MUTEX_UNLOCK(mutex) \
102  { \
103  int r = 0; \
104  if ((r = pthread_mutex_unlock(mutex)) != 0) { \
105  ZE_LogSysError("pthread_mutex_unlock : %s", strerror(r)); \
106  } \
107  }
108 
109 #define RWLOCK_RDLOCK(lock) \
110  if (pthread_rwlock_rdlock(lock) != 0) { \
111  ZE_LogSysError("pthread_rwlock_rdlock"); \
112  }
113 
114 #define RWLOCK_WRLOCK(lock) \
115  if (pthread_rwlock_wrlock(lock) != 0) { \
116  ZE_LogSysError("pthread_rwlock_rdlock"); \
117  }
118 
119 #define RWLOCK_UNLOCK(lock) \
120  if (pthread_rwlock_unlock(lock) != 0) { \
121  ZE_LogSysError("pthread_rwlock_rdlock"); \
122  }
123 
124 
125 
126 #ifndef HAVE_MAX
127 #define max(a,b) ((a) > (b) ? (a) : (b))
128 #define min(a,b) ((a) < (b) ? (a) : (b))
129 #endif
130 
131 #ifdef MAX
132 # undef MAX
133 #endif
134 
135 #ifdef MIN
136 # undef MIN
137 #endif
138 
139 #define MAX(a,b) ((a) > (b) ? (a) : (b))
140 #define MIN(a,b) ((a) < (b) ? (a) : (b))
141 
142 #define SECONDS
143 #define MINUTES * 60
144 #define HOURS * 60 MINUTES
145 #define DAYS * 24 HOURS
146 #define WEEKS * 7 DAYS
147 #define MONTHS * 30 DAYS
148 #define YEARS * 365 DAYS
149 
150 #define BYTES
151 #define KBYTES * 1024
152 #define MBYTES * 1024 KBYTES
153 #define GBYTES * 1024 MBYTES
154 
155 
156 #ifndef TRUE
157 # define TRUE 1
158 #endif /* ! TRUE */
159 #ifndef FALSE
160 # define FALSE 0
161 #endif /* ! FALSE */
162 
163 
164 
165 
166 #define SET_BIT(p, i) ((p) |= (1 << (i)))
167 #define CLR_BIT(p, i) ((p) &= ~(1 << (i)))
168 #define GET_BIT(p, i) (((p) & (1 << (i))) != 0 ? TRUE : FALSE)
169 
170 
171 #define SKIP_SPACES(s) \
172  do { \
173  if (s != NULL) \
174  while (*s != '\0' && isspace(*s)) \
175  s++; \
176  } while (0)
177 
178 #define SKIP_ALPHAS(s) \
179  do { \
180  if (s != NULL) \
181  while (*s != '\0' && isalpha(*s)) \
182  s++; \
183  } while (0)
184 
185 #define SKIP_DIGITS(s) \
186  do { \
187  if (s != NULL) \
188  while (*s != '\0' && isdigit(*s)) \
189  s++; \
190  } while (0)
191 
192 #define SKIP_ALPHANUM(s) \
193  do { \
194  if (s != NULL) \
195  while (*s != '\0' && isalnum(*s)) \
196  s++; \
197  } while (0)
198 
199 #define SKIP_KEYCHARS(s) \
200  do { \
201  if (s != NULL) \
202  while (*s != '\0' && (isalnum(*s) || *s == '-')) \
203  s++; \
204  } while (0)
205 
206 #define STRIP_END_SPACES(s) \
207  do { \
208  if (s != NULL) \
209  while (*s != '\0' && isspace(*s)) \
210  s++; \
211  } while (0)
212 
213 
214 #define PATH_REGEX "^(/[-a-z0-9.]+)+$"
215 
216 #define DOMAINNAME_REGEX "^[a-z0-9._-]+\\.[a-z]{2,6}$"
217 
218 #define IPV4_ADDR_REGEX "^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$"
219 #define IPV6_ADDR_REGEX "(ipv6:)?.*:"
220 
221 #define IPV4_ADDR_REGEX_BRACKET "[\\[][0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+[\\]]"
222 
223 
224 #define CRLF "\r\n"
225 
226 #if defined(NULLSENDER)
227 # undef NULLSENDER
228 #endif
229 #define NULLSENDER "<>"
230 
231 #define POSTMASTER_OK 1
232 #define POSTMASTER_FORGED -1
233 #define NOT_POSTMASTER 0
234 
235 
236 #define ADJUST_FILENAME(path, fname, cfdir, defval) \
237  do { \
238  char *name = NULL; \
239  name = (fname != NULL && strlen(fname) > 0) ? fname : defval; \
240  strcpy(path, ""); \
241  if (*name == '/' || *name == '.') \
242  strlcpy(path, name, sizeof(path)); \
243  else \
244  snprintf(path, sizeof(path), "%s/%s", cfdir, name); \
245  ZE_MessageInfo(15, "Adjusted path is %s", path); \
246  } while (FALSE);
247 
248 
249 #define SHOW_CURSOR(zero) \
250  do { \
251  static char xCURSOR[] = "|/-\\"; \
252  static int n = 0, i = 0; \
253  if (zero) \
254  n = 0; \
255  if (++n % 10 == 0) { \
256  char c = xCURSOR[i++ % 4]; \
257  fprintf (stderr, "* %c %6d\r", c, n); \
258  } \
259  } while (FALSE)
260 
261 #define __ZE_MACROS_H
262 #endif