make IMC/IMV pairs independent of libcharon
[strongswan.git] / src / libstrongswan / printf_hook.h
1 /*
2 * Copyright (C) 2009 Tobias Brunner
3 * Copyright (C) 2006-2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 /**
18 * @defgroup printf_hook printf_hook
19 * @{ @ingroup libstrongswan
20 */
21
22 #ifndef PRINTF_HOOK_H_
23 #define PRINTF_HOOK_H_
24
25 typedef struct printf_hook_t printf_hook_t;
26 typedef struct printf_hook_spec_t printf_hook_spec_t;
27 typedef enum printf_hook_argtype_t printf_hook_argtype_t;
28
29 #if !defined(USE_VSTR) && \
30 (defined(HAVE_PRINTF_FUNCTION) || defined(HAVE_PRINTF_SPECIFIER))
31
32 #include <stdio.h>
33 #include <printf.h>
34
35 enum printf_hook_argtype_t {
36 PRINTF_HOOK_ARGTYPE_END = -1,
37 PRINTF_HOOK_ARGTYPE_INT = PA_INT,
38 PRINTF_HOOK_ARGTYPE_POINTER = PA_POINTER,
39 };
40
41 #else
42
43 #include <vstr.h>
44
45 enum printf_hook_argtype_t {
46 PRINTF_HOOK_ARGTYPE_END = VSTR_TYPE_FMT_END,
47 PRINTF_HOOK_ARGTYPE_INT = VSTR_TYPE_FMT_INT,
48 PRINTF_HOOK_ARGTYPE_POINTER = VSTR_TYPE_FMT_PTR_VOID,
49 };
50
51 /**
52 * Redefining printf and alike
53 */
54 #include <stdio.h>
55 #include <stdarg.h>
56
57 int vstr_wrapper_printf(const char *format, ...);
58 int vstr_wrapper_fprintf(FILE *stream, const char *format, ...);
59 int vstr_wrapper_sprintf(char *str, const char *format, ...);
60 int vstr_wrapper_snprintf(char *str, size_t size, const char *format, ...);
61 int vstr_wrapper_asprintf(char **str, const char *format, ...);
62
63 int vstr_wrapper_vprintf(const char *format, va_list ap);
64 int vstr_wrapper_vfprintf(FILE *stream, const char *format, va_list ap);
65 int vstr_wrapper_vsprintf(char *str, const char *format, va_list ap);
66 int vstr_wrapper_vsnprintf(char *str, size_t size, const char *format, va_list ap);
67 int vstr_wrapper_vasprintf(char **str, const char *format, va_list ap);
68
69 #define printf vstr_wrapper_printf
70 #define fprintf vstr_wrapper_fprintf
71 #define sprintf vstr_wrapper_sprintf
72 #define snprintf vstr_wrapper_snprintf
73 #define asprintf vstr_wrapper_asprintf
74
75 #define vprintf vstr_wrapper_vprintf
76 #define vfprintf vstr_wrapper_vfprintf
77 #define vsprintf vstr_wrapper_vsprintf
78 #define vsnprintf vstr_wrapper_vsnprintf
79 #define vasprintf vstr_wrapper_vasprintf
80
81 #endif
82
83 /**
84 * Callback function type for printf hooks.
85 *
86 * @param dst destination buffer
87 * @param len length of the buffer
88 * @param spec format specifier
89 * @param args arguments array
90 * @return number of characters written
91 */
92 typedef int (*printf_hook_function_t)(char *dst, size_t len,
93 printf_hook_spec_t *spec,
94 const void *const *args);
95
96 /**
97 * Helper macro to be used in printf hook callbacks.
98 * buf and buflen get modified.
99 */
100 #define print_in_hook(buf, buflen, fmt, ...) ({\
101 int _written = snprintf(buf, buflen, fmt, ##__VA_ARGS__);\
102 if (_written < 0 || _written >= buflen)\
103 {\
104 _written = buflen - 1;\
105 }\
106 buf += _written;\
107 buflen -= _written;\
108 _written;\
109 })
110
111 /**
112 * Properties of the format specifier
113 */
114 struct printf_hook_spec_t {
115 /**
116 * TRUE if a '#' was used in the format specifier
117 */
118 int hash;
119
120 /**
121 * TRUE if a '-' was used in the format specifier
122 */
123 int minus;
124
125 /**
126 * The width as given in the format specifier.
127 */
128 int width;
129 };
130
131 /**
132 * Printf handler management.
133 */
134 struct printf_hook_t {
135
136 /**
137 * Register a printf handler.
138 *
139 * @param spec printf hook format character
140 * @param hook hook function
141 * @param ... list of PRINTF_HOOK_ARGTYPE_*, MUST end with PRINTF_HOOK_ARGTYPE_END
142 */
143 void (*add_handler)(printf_hook_t *this, char spec,
144 printf_hook_function_t hook, ...);
145
146 /**
147 * Destroy a printf_hook instance.
148 */
149 void (*destroy)(printf_hook_t *this);
150 };
151
152 /**
153 * Create a printf_hook instance.
154 */
155 printf_hook_t *printf_hook_create();
156
157 #endif /** PRINTF_HOOK_H_ @}*/