1 #ifndef COMMON_H 2 #define COMMON_H 3 4 #include <stdlib.h> 5 #include <stdarg.h> 6 #include <ctype.h> 7 #include <limits.h> 8 9 #ifdef __cplusplus 10 extern "C" { 11 #endif 12 13 #define CONFIG_DIR ".moc" 14 15 /* Maximal string length sent/received. */ 16 #define MAX_SEND_STRING 4096 17 18 /* Maximum path length, we don't consider exceptions like mounted NFS */ 19 #ifndef PATH_MAX 20 # if defined(_POSIX_PATH_MAX) 21 # define PATH_MAX _POSIX_PATH_MAX /* Posix */ 22 # elif defined(MAXPATHLEN) 23 # define PATH_MAX MAXPATHLEN /* Solaris? Also linux...*/ 24 # else 25 # define PATH_MAX 4096 /* Suppose, we have 4096 */ 26 # endif 27 #endif 28 29 /* Exit status on fatal exit. */ 30 #define EXIT_FATAL 2 31 32 #define LOCK(mutex) pthread_mutex_lock (&mutex) 33 #define UNLOCK(mutex) pthread_mutex_unlock (&mutex) 34 35 #ifndef MIN 36 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 37 #endif 38 39 #ifndef MAX 40 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 41 #endif 42 43 #ifndef LIMIT 44 #define LIMIT(val, lim) ((val) >= 0 && (val) < (lim)) 45 #endif 46 47 #ifndef RANGE 48 #define RANGE(min, val, max) ((val) >= (min) && (val) <= (max)) 49 #endif 50 51 #ifndef CLAMP 52 #define CLAMP(min, val, max) ((val) < (min) ? (min) : \ 53 (val) > (max) ? (max) : (val)) 54 #endif 55 56 #ifdef HAVE__ATTRIBUTE__ 57 # define ATTR_UNUSED __attribute__((unused)) 58 #else 59 # define ATTR_UNUSED 60 #endif 61 62 #if HAVE_STDBOOL_H 63 # include <stdbool.h> 64 #else 65 # if ! HAVE__BOOL 66 # ifdef __cplusplus 67 typedef bool _Bool; 68 # else 69 typedef unsigned char _Bool; 70 # endif 71 # endif 72 # define bool _Bool 73 # define false 0 74 # define true 1 75 # define __bool_true_false_are_defined 1 76 #endif 77 78 /* isblank() is a GNU extension */ 79 #ifndef isblank 80 #define isblank(c) ((c) == ' ' || (c) == '\t') 81 #endif 82 83 /* __FUNCTION__ is a gcc extension */ 84 #ifndef HAVE__FUNCTION__ 85 # define __FUNCTION__ "UNKNOWN_FUNC" 86 #endif 87 88 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) 89 90 void *xmalloc (size_t size); 91 void *xcalloc (size_t nmemb, size_t size); 92 void *xrealloc (void *ptr, const size_t size); 93 char *xstrdup (const char *s); 94 95 96 #ifdef NDEBUG 97 #define fatal(format, ...) \ 98 internal_fatal (NULL, 0, NULL, format, ## __VA_ARGS__) 99 #else 100 #define fatal(format, ...) \ 101 internal_fatal (__FILE__, __LINE__, __FUNCTION__, format, \ 102 ## __VA_ARGS__) 103 #endif 104 105 #ifdef HAVE__ATTRIBUTE__ 106 void internal_fatal (const char *file, int line, const char *function, 107 const char *format, ...) 108 __attribute__ ((format (printf, 4, 5), noreturn)); 109 void error (const char *format, ...) __attribute__((format (printf, 1, 2))); 110 #else 111 void internal_fatal (const char *file, int line, const char *function, 112 const char *format, ...); 113 void error (const char *format, ...); 114 #endif 115 116 void set_me_server (); 117 char *str_repl (char *target, const char *oldstr, const char *newstr); 118 char *trim (const char *src, size_t len); 119 char *format_msg (const char *format, ...); 120 char *format_msg_va (const char *format, va_list va); 121 bool is_valid_symbol (const char *candidate); 122 char *create_file_name (const char *file); 123 void sec_to_min (char *buff, const int seconds); 124 const char *get_home (); 125 126 #ifdef __cplusplus 127 } 128 #endif 129 130 #endif 131