#ifndef GRAS_PORTABLE_H
#define GRAS_PORTABLE_H
-#include "gras_config.h"
+/*
+ * win32 or win64 (__WIN32 is defined for win32 and win64 applications, __TOS_WIN__ is defined by xlC).
+*/
+#ifdef _WIN32
+# include "win32/config.h"
+# include <windows.h>
+#else
+# include "gras_config.h"
+#endif
+
+/* Load this asap to make sure that GNU_SOURCE is defined on need when stdio gets loaded by some random system header */
+#ifdef HAVE_GETLINE
+# define _GNU_SOURCE
+# include <stdio.h>
+#endif
+
+#include <stdarg.h>
#ifdef HAVE_ERRNO_H
# include <errno.h>
# include <netinet/tcp.h> /* TCP_NODELAY */
# include <netdb.h> /* getprotobyname() */
# include <arpa/inet.h> /* inet_ntoa() */
+# include <sys/types.h> /* sometimes required for fd_set */
# endif
-#ifdef HAVE_WINSOCK2_H
-# include <winsock2.h>
-# include <ws2tcpip.h> /* socklen_t, but doubtful */
-# ifndef HAVE_WINSOCK_H
-# define HAVE_WINSOCK_H
-# endif
-#elif HAVE_WINSOCK_H
-# include <winsock.h>
-#endif
-
-#ifdef HAVE_WINSOCK_H
-# define tcp_read( s, buf, len ) recv( s, buf, len, 0 )
-# define tcp_write( s, buf, len ) send( s, buf, len, 0 )
-# define ioctl( s, c, a ) ioctlsocket( (s), (c), (a) )
-# define ioctl_t u_long
-# define AC_SOCKET_INVALID ((unsigned int) ~0)
-# ifdef SD_BOTH
-# define tcp_close( s ) (shutdown( s, SD_BOTH ), closesocket( s ))
-# else
-# define tcp_close( s ) closesocket( s )
-# endif
-
-# define EWOULDBLOCK WSAEWOULDBLOCK
-# define EINPROGRESS WSAEINPROGRESS
-# define ETIMEDOUT WSAETIMEDOUT
-
-# undef sock_errno
-# undef sock_errstr
-# define sock_errno WSAGetLastError()
-# define sock_errstr gras_wsa_err2string(WSAGetLastError())
-
-const char *gras_wsa_err2string(int errcode);
-
-# define S_IRGRP 0
-# define S_IWGRP 0
-
-#else
+#ifndef HAVE_WINSOCK_H
# define tcp_read( s, buf, len) read( s, buf, len )
# define tcp_write( s, buf, len) write( s, buf, len )
-# define sock_errno errno
-# define sock_errstr strerror(errno)
+# define sock_errno errno
+# define sock_errstr(err) strerror(err)
# ifdef SHUT_RDWR
# define tcp_close( s ) (shutdown( s, SHUT_RDWR ), close( s ))
# endif
#endif
-#ifdef _WIN32
-#define sleep _sleep /* else defined in stdlib.h */
+/****
+ **** Signals
+ ****/
+#ifdef HAVE_SIGNAL_H
+# include <signal.h>
#endif
/****
- **** Contexts
+ **** string handling (parts from http://www.ijs.si/software/snprintf/)
****/
-#ifdef HAVE_UCONTEXT_H
-# include <ucontext.h>
+/* prototype of C99 functions */
+#ifdef HAVE_SNPRINTF
+#include <stdio.h>
#else
-# include "xbt/context_win32.h" /* Manual reimplementation for prehistoric platforms */
+# if (defined(_MSC_VER) && defined(DLL_EXPORT))
+ __declspec(dllexport) int snprintf(char *, size_t, const char *, /*args*/ ...);
+ __declspec(dllexport) int vsnprintf(char *, size_t, const char *, va_list);
+# elif (defined(_MSC_VER) && !defined(DLL_EXPORT) && !defined(DLL_STATIC) )
+ __declspec(dllimport) int snprintf(char *, size_t, const char *, /*args*/ ...);
+ __declspec(dllimport) int vsnprintf(char *, size_t, const char *, va_list);
+# else
+ extern int snprintf(char *, size_t, const char *, /*args*/ ...);
+ extern int vsnprintf(char *, size_t, const char *, va_list);
+# endif
+
+#endif
+
+/* use internal functions when OS provided ones are borken */
+#if defined(HAVE_SNPRINTF) && defined(PREFER_PORTABLE_SNPRINTF)
+extern int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...);
+extern int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap);
+#define snprintf portable_snprintf
+#define vsnprintf portable_vsnprintf
+#endif
+
+/* prototype of GNU functions */
+#ifndef __BORLANDC__
+extern int asprintf (char **ptr, const char *fmt, /*args*/ ...);
+extern int vasprintf (char **ptr, const char *fmt, va_list ap);
#endif
+extern int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...);
+extern int vasnprintf(char **ptr, size_t str_m, const char *fmt, va_list ap);
+/*
+ * That's needed to protect solaris's printf from ever seing NULL associated to a %s format
+ * (without adding an extra check on working platforms :)
+ */
+
+#ifdef PRINTF_NULL_WORKING
+# define PRINTF_STR(a) (a)
+#else
+# define PRINTF_STR(a) (a)?:"(null)"
+#endif
+
+/*
+ * What we need to extract the backtrace in exception handling code
+ */
+#ifdef HAVE_EXECINFO_H
+# include <execinfo.h>
+#endif
+
+/****
+ **** Some debugging functions. Can't we find a better place for this??
+ ****/
+void hexa_print(const char*name, unsigned char *data, int size);
+const char *hexa_str(unsigned char *data, int size, int downside);
#endif /* GRAS_PORTABLE_H */