-
Notifications
You must be signed in to change notification settings - Fork 38
/
common.h
167 lines (140 loc) · 4.61 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* The purpose of this header is to provide common functions and macros
* used throughout MOC code. It also provides (x-prefixed) functions
* which augment or adapt their respective system functions with error
* checking and the like.
*/
#ifndef COMMON_H
#define COMMON_H
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <limits.h>
#include "compat.h"
/* Suppress overly-enthusiastic GNU variadic macro extensions warning. */
#if defined(__clang__) && HAVE_VARIADIC_MACRO_WARNING
# pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#endif
struct timespec;
#ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
# define ATTR_PRINTF(x,y) __attribute__ ((format (printf, x, y)))
#else
# define ATTR_PRINTF(...)
#endif
#ifdef HAVE_FUNC_ATTRIBUTE_NORETURN
# define ATTR_NORETURN __attribute__((noreturn))
#else
# define ATTR_NORETURN
#endif
#ifdef HAVE_VAR_ATTRIBUTE_UNUSED
# define ATTR_UNUSED __attribute__((unused))
#else
# define ATTR_UNUSED
#endif
#ifndef GCC_VERSION
#define GCC_VERSION (__GNUC__ * 10000 + \
__GNUC_MINOR__ * 100 + \
__GNUC_PATCHLEVEL__)
#endif
/* These macros allow us to use the appropriate method for manipulating
* GCC's diagnostic pragmas depending on the compiler's version. */
#if GCC_VERSION >= 40200
# define GCC_DIAG_STR(s) #s
# define GCC_DIAG_JOINSTR(x,y) GCC_DIAG_STR(x ## y)
# define GCC_DIAG_DO_PRAGMA(x) _Pragma (#x)
# define GCC_DIAG_PRAGMA(x) GCC_DIAG_DO_PRAGMA(GCC diagnostic x)
# if GCC_VERSION >= 40600
# define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(push) \
GCC_DIAG_PRAGMA(ignored GCC_DIAG_JOINSTR(-W,x))
# define GCC_DIAG_ON(x) GCC_DIAG_PRAGMA(pop)
# else
# define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(ignored GCC_DIAG_JOINSTR(-W,x))
# define GCC_DIAG_ON(x) GCC_DIAG_PRAGMA(warning GCC_DIAG_JOINSTR(-W,x))
# endif
#else
# define GCC_DIAG_OFF(x)
# define GCC_DIAG_ON(x)
#endif
#ifdef HAVE_FORMAT_TRUNCATION_WARNING
# define SUPPRESS_FORMAT_TRUNCATION_WARNING GCC_DIAG_OFF(format-truncation)
# define UNSUPPRESS_FORMAT_TRUNCATION_WARNING GCC_DIAG_ON(format-truncation)
#else
# define SUPPRESS_FORMAT_TRUNCATION_WARNING
# define UNSUPPRESS_FORMAT_TRUNCATION_WARNING
#endif
#define CONFIG_DIR ".moc"
#define LOCK(mutex) pthread_mutex_lock (&mutex)
#define UNLOCK(mutex) pthread_mutex_unlock (&mutex)
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#define ssizeof(x) ((ssize_t) sizeof(x))
/* Maximal string length sent/received. */
#define MAX_SEND_STRING 4096
/* Exit status on fatal error. */
#define EXIT_FATAL 2
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef LIMIT
#define LIMIT(val, lim) ((val) >= 0 && (val) < (lim))
#endif
#ifndef RANGE
#define RANGE(min, val, max) ((val) >= (min) && (val) <= (max))
#endif
#ifndef CLAMP
#define CLAMP(min, val, max) ((val) < (min) ? (min) : \
(val) > (max) ? (max) : (val))
#endif
#ifdef NDEBUG
#define error(...) \
internal_error (NULL, 0, NULL, ## __VA_ARGS__)
#define fatal(...) \
internal_fatal (NULL, 0, NULL, ## __VA_ARGS__)
#define ASSERT_ONLY ATTR_UNUSED
#else
#define error(...) \
internal_error (__FILE__, __LINE__, __func__, ## __VA_ARGS__)
#define fatal(...) \
internal_fatal (__FILE__, __LINE__, __func__, ## __VA_ARGS__)
#define ASSERT_ONLY
#endif
#ifndef STRERROR_FN
# define STRERROR_FN xstrerror
#endif
#define error_errno(format, errnum) \
do { \
char *err##__LINE__ = STRERROR_FN (errnum); \
error (format ": %s", err##__LINE__); \
free (err##__LINE__); \
} while (0)
#ifdef __cplusplus
extern "C" {
#endif
void *xmalloc (size_t size);
void *xcalloc (size_t nmemb, size_t size);
void *xrealloc (void *ptr, const size_t size);
char *xstrdup (const char *s);
void xsleep (size_t ticks, size_t ticks_per_sec);
char *xstrerror (int errnum);
void xsignal (int signum, void (*func)(int));
void internal_error (const char *file, int line, const char *function,
const char *format, ...) ATTR_PRINTF(4, 5);
void internal_fatal (const char *file, int line, const char *function,
const char *format, ...) ATTR_NORETURN ATTR_PRINTF(4, 5);
void set_me_server ();
char *str_repl (char *target, const char *oldstr, const char *newstr);
char *trim (const char *src, size_t len);
char *format_msg (const char *format, ...);
char *format_msg_va (const char *format, va_list va);
bool is_valid_symbol (const char *candidate);
char *create_file_name (const char *file);
int get_realtime (struct timespec *ts);
void sec_to_min (char *buff, const int seconds);
const char *get_home ();
void common_cleanup ();
#ifdef __cplusplus
}
#endif
#endif