common.h 3.21 KB
Newer Older
1 2 3
#ifndef _COMMON_H_
#define _COMMON_H_

4 5
#define _POSIX_C_SOURCE 200809L

6
#include <stdio.h>
7
#include <stdarg.h>
8 9 10 11 12 13 14 15 16 17 18 19 20
#include <stdbool.h>
#include <errno.h>

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

#ifdef MINGW
#include <winsock2.h>
#include <windows.h>
#endif


/**
21 22 23 24
 * Compiler attributes.
 */
#define PACKED __attribute__ ((__packed__))

25
#if defined(WHOLE) && __has_attribute(externally_visible)
26 27 28 29 30 31 32 33 34
#define VISIBLE __attribute__ ((externally_visible))
#else
#define VISIBLE
#endif



/**
 * Returns the size of an array in array elements.
35 36 37 38 39
 */
#define sizeof_array(array) \
	(sizeof (array) / sizeof ((array) [0]))


40 41 42 43 44 45 46
/**
 * Returns the size of a structure member.
 */
#define sizeof_member(type, member) \
	(sizeof (((type *) 0)->member))


47
/**
48
 * Prints a warning message to stderr.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
 *
 * @format	format string for printf
 * @args	arguments associated with the format string
 *
 */
#define warn(args...) { \
	fprintf (stderr, "warning: "); \
	fprintf (stderr, args); \
}

//

int find_value_index (const char * strval, const char * values [], int nvals);

//

#define ERROR_GENERIC 10000
#define ERROR_SERVER 10003
#define ERROR_STD 10002
#define ERROR_JVMTI 10003

70
#define AGENT_NAME "disl-agent"
71 72 73

//

74 75
void die_with_error (const char * format, va_list args);
void die_with_std_error (int errnum, const char * format, va_list args);
76 77 78 79 80 81


/**
 * Reports a general error and terminates the program if the provided
 * error condition is true.
 */
82
static inline void
83
check_error (bool error, const char * format, ...) {
84
	if (error) {
85 86 87 88 89 90
		va_list args;
		va_start (args, format);

		die_with_error (format, args);

		va_end (args);
91 92 93 94 95 96 97 98
	}
}


/**
 * Reports a standard library error and terminates the program if the provided
 * error condition is true.
 */
99
static inline void
100
check_std_error (bool error, const char * format, ...) {
101
	if (error) {
102 103 104 105 106 107
		va_list args;
		va_start (args, format);

		die_with_std_error (errno, format, args);

		va_end (args);
108 109 110
	}
}

111 112 113 114 115 116 117 118 119 120 121 122

/**
 * Warns about a standard library error if the provided
 * error condition is true.
 */
static inline void
warn_std_error (bool error, const char * message) {
	if (error) {
		warn (message, errno);
	}
}

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

// ****************************************************************************
// STRING UTILS SECTION
// ****************************************************************************

/**
 * Count the occurrences of substr in string
 * @param string
 * @param substr
 * @return
 */
size_t
substr_count(const char * string, const char * restrict substr);


/**
 * Split the string into tokens
 * Remember to free the result.
 *
 * @param string The string to split
 * @param separator
 * @param tokens Pointer to the size_t variable to store the number of tokens
 * @return
 */
char **
split_string(char * string, const char * restrict separator, size_t * tokens);

150 151 152 153
//

#ifdef MINGW

154
void die_with_win_error (DWORD errnum, const char * message, va_list args);
155 156 157 158 159 160


/**
 * Reports a windows error and terminates the program if the provided
 * error condition is true.
 */
161
static inline void
162
check_win_error (bool error, const char * format, ...) {
163
	if (error) {
164 165 166 167 168 169
		va_list args;
		va_start (args, format);

		die_with_win_error (GetLastError (), format, args);

		va_end (args);
170 171 172 173 174 175
	}
}

#endif /* MINGW */

#endif /* _COMMON_H_ */