2 * Copyright © 2012 Ran Benita <ran234@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
37 * We sometimes malloc strings and then expose them as const char*'s. This
38 * macro is used when we free these strings in order to avoid -Wcast-qual
41 #define UNCONSTIFY(const_ptr) ((void *) (uintptr_t) (const_ptr))
44 streq(const char *s1, const char *s2)
46 return strcmp(s1, s2) == 0;
50 streq_not_null(const char *s1, const char *s2)
58 istreq(const char *s1, const char *s2)
60 return strcasecmp(s1, s2) == 0;
64 istreq_prefix(const char *s1, const char *s2)
66 return strncasecmp(s1, s2, strlen(s1)) == 0;
70 strdup_safe(const char *s)
72 return s ? strdup(s) : NULL;
76 strlen_safe(const char *s)
78 return s ? strlen(s) : 0;
82 isempty(const char *s)
84 return s == NULL || s[0] == '\0';
87 static inline const char *
88 strnull(const char *s)
90 return s ? s : "(null)";
93 static inline const char *
94 strempty(const char *s)
100 memdup(const void *mem, size_t nmemb, size_t size)
102 void *p = malloc(nmemb * size);
104 memcpy(p, mem, nmemb * size);
109 min(int misc, int other)
111 return (misc < other) ? misc : other;
115 max(int misc, int other)
117 return (misc > other) ? misc : other;
120 /* ctype.h is locale-dependent and has other oddities. */
124 return ch == ' ' || (ch >= '\t' && ch <= '\r');
130 return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
136 return ch >= '0' && ch <= '9';
142 return is_alpha(ch) || is_digit(ch);
149 (ch >= '0' && ch <= '9') ||
150 (ch >= 'a' && ch <= 'f') ||
151 (ch >= 'A' && ch <= 'F');
157 /* See table in ascii(7). */
158 return ch >= '!' && ch <= '~';
162 * Return the bit position of the most significant bit.
163 * Note: this is 1-based! It's more useful this way, and returns 0 when
167 msb_pos(uint32_t mask)
178 map_file(FILE *file, const char **string_out, size_t *size_out);
181 unmap_file(const char *str, size_t size);
183 #define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
185 #define MIN(a, b) ((a) < (b) ? (a) : (b))
186 #define MIN3(a, b, c) MIN(MIN((a), (b)), (c))
187 #define MAX(a, b) ((a) > (b) ? (a) : (b))
188 #define MAX3(a, b, c) MAX(MAX((a), (b)), (c))
190 #if defined(HAVE_SECURE_GETENV)
191 # define secure_getenv secure_getenv
192 #elif defined(HAVE___SECURE_GETENV)
193 # define secure_getenv __secure_getenv
195 # define secure_getenv getenv
198 /* Compiler Attributes */
200 #if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__CYGWIN__)
201 # define XKB_EXPORT __attribute__((visibility("default")))
202 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
203 # define XKB_EXPORT __global
204 #else /* not gcc >= 4 and not Sun Studio >= 8 */
208 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)
209 # define ATTR_PRINTF(x,y) __attribute__((__format__(__printf__, x, y)))
210 #else /* not gcc >= 2.3 */
211 # define ATTR_PRINTF(x,y)
214 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
215 || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
216 # define ATTR_NORETURN __attribute__((__noreturn__))
218 # define ATTR_NORETURN
221 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 296)
222 #define ATTR_MALLOC __attribute__((__malloc__))
227 #if defined(__GNUC__) && (__GNUC__ >= 4)
228 # define ATTR_NULL_SENTINEL __attribute__((__sentinel__))
230 # define ATTR_NULL_SENTINEL
231 #endif /* GNUC >= 4 */
233 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 295)
234 #define ATTR_PACKED __attribute__((__packed__))