1 /*  -*- Mode: C -*-
2  * --------------------------------------------------------------------
3  * compat.h.in --- verbose but portable cpp defines for snprintfv
4  * Copyright (C) 1999 Gary V. Vaughan
5  * Originally by Gary V. Vaughan, 1999
6  * This file is part of Snprintfv
7  *
8  * Snprintfv is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * Snprintfv program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses>.
20  *
21  * As a special exception to the GNU General Public License, if you
22  * distribute this file as part of a program that also links with and
23  * uses the libopts library from AutoGen, you may include it under
24  * the same distribution terms used by the libopts library.
25  *
26  * Code:  */
27 
28 #ifndef SNPRINTFV_COMPAT_H
29 #define SNPRINTFV_COMPAT_H 1
30 
31 #define  _GNU_SOURCE    1 /* for strsignal in GNU's libc */
32 #define  __USE_GNU      1 // likewise
33 #define  __EXTENSIONS__ 1 /* and another way to call for it */
34 
35 #ifdef __cplusplus
36 #define SNV_START_EXTERN_C extern "C" {
37 SNV_START_EXTERN_C
38 #define SNV_END_EXTERN_C }
39 #else
40 #define SNV_END_EXTERN_C
41 #endif /* __cplusplus */
42 
43 #define NO_FLOAT_PRINTING
44 
45 #ifdef HAVE_SYS_TYPES_H
46 #  include <sys/types.h>
47 #endif
48 
49 #include <stdio.h>
50 
51 #ifdef HAVE_STDLIB_H
52 #  include <stdlib.h>
53 #endif
54 
55 #ifdef HAVE_ERRNO_H
56 #  include <errno.h>
57 #  ifndef errno
58      /* Some sytems #define this! */
59      extern int errno;
60 #  endif
61 #endif
62 
63 #if defined( HAVE_LIMITS_H )
64 #  include <limits.h>
65 
66 #elif defined( HAVE_SYS_LIMITS_H )
67 #  include <sys/limits.h>
68 
69 #elif defined( HAVE_VALUES_H )
70 #  ifndef MAXINT
71 #    include <values.h>
72 #  endif /* MAXINT */
73 #endif
74 
75 #if defined( HAVE_STRING_H )
76 #  include <string.h>
77 
78 #elif defined( HAVE_STRINGS_H )
79 #  include <strings.h>
80 #endif
81 
82 #if defined( HAVE_MEMORY_H )
83 #  include <memory.h>
84 #endif
85 
86 #if defined( HAVE_INTTYPES_H )
87 #  include <inttypes.h>
88 
89 #elif defined( HAVE_STDINT_H )
90 #  include <stdint.h>
91 #endif
92 
93 #ifndef HAVE_UINTMAX_T
94 #  if defined( HAVE_LONG_LONG )
95      typedef long long intmax_t;
96      typedef unsigned long long uintmax_t;
97 #  else
98      typedef long intmax_t;
99      typedef unsigned long uintmax_t;
100 #  endif
101 #endif
102 
103 #if defined( HAVE_STDARG_H )
104 #  include <stdarg.h>
105 #  ifndef   VA_START
106 #    define VA_START(a, f)  va_start(a, f)
107 #    define VA_END(a)	    va_end(a)
108 #  endif /* VA_START */
109 #  define SNV_USING_STDARG_H
110 #elif defined( HAVE_VARARGS_H )
111 #  include <varargs.h>
112 #  ifndef   VA_START
113 #    define VA_START(a, f) va_start(a)
114 #    define VA_END(a)	 va_end(a)
115 #  endif /* VA_START */
116 #  undef  SNV_USING_STDARG_H
117 #else
118 #  include "must-have-stdarg-or-varargs"
119 #endif
120 
121 #if HAVE_RUNETYPE_H
122 # include <runetype.h>
123 #endif
124 
125 #ifdef HAVE_WCHAR_H
126 # include <wchar.h>
127 #endif
128 
129 #ifdef HAVE_WCHAR_T
130 typedef wchar_t snv_wchar_t;
131 #else
132 typedef int snv_wchar_t;
133 #endif
134 
135 #ifdef HAVE_WINT_T
136 typedef wint_t snv_wint_t;
137 #else
138 typedef int snv_wint_t;
139 #endif
140 
141 /* inline and const keywords are (mostly) handled by config.h */
142 #ifdef __GNUC__
143 
144 #  define GCC_VERSION (__GNUC__ * 10000 \
145                     + __GNUC_MINOR__ * 100 \
146                     + __GNUC_PATCHLEVEL__)
147 
148 #  if GCC_VERSION > 40400
149 #    pragma  GCC diagnostic ignored "-Wextra"
150 #    pragma  GCC diagnostic ignored "-Wconversion"
151 #    pragma  GCC diagnostic ignored "-Wsign-conversion"
152 #    pragma  GCC diagnostic ignored "-Wstrict-overflow"
153 #  endif
154 
155 #  ifndef const
156 #    define const	__const
157 #  endif
158 #  ifndef inline
159 #    define inline	__inline
160 #  endif
161 #  ifndef signed
162 #    define signed	__signed
163 #  endif
164 
165 #else
166 #  define GCC_VERSION 0
167 #  ifndef __STDC__
168 #    undef  signed
169 #    define signed
170 #  endif
171 #endif
172 
173 #ifdef __STDC__
174 #  define _SNV_STR(x)		#x
175    typedef void *snv_pointer;
176    typedef const void *snv_constpointer;
177 #else
178 #  define _SNV_STR(x)		"x"
179    typedef char *snv_pointer;
180    typedef char *snv_constpointer;
181 #endif
182 
183 #if defined(HAVE_FPUTC_UNLOCKED) && defined(HAVE_FLOCKFILE)
184 #  define SNV_FPUTC_UNLOCKED fputc_unlocked
185 #  define SNV_PUTC_UNLOCKED putc_unlocked
186 #  define SNV_WITH_LOCKED_FP(fp, tmp_var) \
187      for (flockfile (fp), tmp_var = 1; \
188        tmp_var--; funlockfile (fp))
189 #else
190 #  define SNV_FPUTC_UNLOCKED fputc
191 #  define SNV_PUTC_UNLOCKED putc
192 #  define SNV_WITH_LOCKED_FP(fp, tmp_var) \
193      for (tmp_var = 1; tmp_var--; )
194 #endif
195 
196 /*
197  * Define macros for storing integers inside pointers.
198  * Be aware that it is only safe to use these macros to store `int'
199  * values in `char*' (or `void*') words, and then extract them later.
200  * Although it will work the other way round on many common
201  * architectures, it is not portable to assume a `char*' can be
202  * stored in an `int' and extracted later without loss of the msb's
203  */
204 #define SNV_POINTER_TO_LONG(p)	((long)(p))
205 #define SNV_POINTER_TO_ULONG(p)	((unsigned long)(p))
206 #define SNV_LONG_TO_POINTER(i)	((snv_pointer)(long)(i))
207 #define SNV_ULONG_TO_POINTER(u)	((snv_pointer)(unsigned long)(u))
208 
209 #ifdef HAVE_STDBOOL_H
210 #include <stdbool.h>
211 #else
212 typedef enum {
213     false = 0,
214     true  = 1
215 } bool;
216 #endif
217 
218 #ifdef __CYGWIN32__
219 #  ifndef __CYGWIN__
220 #    define __CYGWIN__
221 #  endif
222 #endif
223 #ifdef __CYGWIN__
224 #  ifndef _WIN32
225 #    define _WIN32
226 #  endif
227 #endif
228 
229 #ifndef PARAMS
230 #  define PARAMS(args)      args
231 #endif
232 
233 #undef SNV_STMT_START
234 #undef SNV_STMT_END
235 #if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
236 #  define SNV_STMT_START	(void)(
237 #  define SNV_STMT_END		)
238 
239 #elif (defined (sun) || defined (__sun__))
240 #  define SNV_STMT_START	if (1)
241 #  define SNV_STMT_END		else (void)0
242 
243 #else
244 #  define SNV_STMT_START	do
245 #  define SNV_STMT_END		while (false)
246 #endif
247 
248 #ifdef _WIN32
249 #  ifdef DLL_EXPORT
250 #    define SNV_SCOPE	extern __declspec(dllexport)
251 #  else
252 #    ifdef LIBSNPRINTFV_DLL_IMPORT
253 #      define SNV_SCOPE	extern __declspec(dllimport)
254 #    endif
255 #  endif
256 #endif
257 #ifndef SNV_SCOPE
258 #  define SNV_SCOPE	extern
259 #endif
260 
261 #undef SNV_GNUC_PRINTF
262 #undef SNV_GNUC_NORETURN
263 #if GCC_VERSION > 20400
264 #  define SNV_GNUC_PRINTF( args, format_idx, arg_idx )		\
265   	args __attribute__((format (printf, format_idx, arg_idx)))
266 #  define SNV_GNUC_NORETURN						\
267 	__attribute__((__noreturn__))
268 #  define SNV_ASSERT_FCN  	 " (", __PRETTY_FUNCTION__, ")"
269 #else /* GCC_VERSION */
270 #  define SNV_GNUC_PRINTF( args, format_idx, arg_idx ) args
271 #  define SNV_GNUC_NORETURN
272 #  define SNV_ASSERT_FCN		"", "", ""
273 #endif /* GCC_VERSION */
274 
275 #define SNV_ASSERT_FMT  "file %s: line %d%s%s%s: assertion \"%s\" failed.\n"
276 
277 #define snv_assert(expr)		snv_fassert(stderr, expr)
278 #define snv_fassert(stream, expr)   SNV_STMT_START {        \
279     if (!(expr))   {                                        \
280     fprintf (stream, SNV_ASSERT_FMT, __FILE__, __LINE__,    \
281              SNV_ASSERT_FCN, _SNV_STR(expr));               \
282     exit(EXIT_FAILURE);                                     \
283     }; } SNV_STMT_END
284 
285 #define return_if_fail(expr)		freturn_if_fail(stderr, expr)
286 #define freturn_if_fail(expr)       SNV_STMT_START {        \
287     if (!(expr))   {                                        \
288     fprintf (stream, SNV_ASSERT_FMT, __FILE__, __LINE__,    \
289              SNV_ASSERT_FCN, _SNV_STR(expr));               \
290     return;                                                 \
291     }; } SNV_STMT_END
292 
293 #define return_val_if_fail(expr, val)	freturn_val_if_fail(stderr, expr, val)
294 #define freturn_val_if_fail(stream, expr, val)  SNV_STMT_START {    \
295     if (!(expr))  {                                                 \
296     fprintf (stream, SNV_ASSERT_FMT, __FILE__, __LINE__,            \
297              SNV_ASSERT_FCN, _SNV_STR(expr));                       \
298     return val;                                                     \
299     }; } SNV_STMT_END
300 
301 #ifndef MAX
302 #define MAX(a,b)	((a) > (b) ? (a) : (b))
303 #endif
304 
305 #ifndef MIN
306 #define MIN(a,b)	((a) < (b) ? (a) : (b))
307 #endif
308 
309 #ifndef ABS
310 #define ABS(a)		((a) < 0 ? -(a) : (a))
311 #endif
312 
313 typedef SNV_LONG_DOUBLE snv_long_double;
314 
315 #ifndef HAVE_STRTOUL
316 extern unsigned long
317 strtoul( const char *nptrm, char **endptr, register int base );
318 #endif
319 
320 SNV_END_EXTERN_C
321 #endif /* SNPRINTFV_COMPAT_H */
322 
323 /*
324  * Local Variables:
325  * mode: C
326  * c-file-style: "gnu"
327  * indent-tabs-mode: nil
328  * End:
329  * end of snprintfv/compat.h */
330