RE: [PATCH, no, really!] Re: [perl #38779] NAN's on Win32
[p5sagit/p5-mst-13.2.git] / win32 / win32.h
1 /* WIN32.H
2  *
3  * (c) 1995 Microsoft Corporation. All rights reserved.
4  *              Developed by hip communications inc., http://info.hip.com/info/
5  *
6  *    You may distribute under the terms of either the GNU General Public
7  *    License or the Artistic License, as specified in the README file.
8  */
9 #ifndef  _INC_WIN32_PERL5
10 #define  _INC_WIN32_PERL5
11
12 #ifndef _WIN32_WINNT
13 #  define _WIN32_WINNT 0x0400     /* needed for TryEnterCriticalSection() etc. */
14 #endif
15
16 #if defined(PERL_IMPLICIT_SYS)
17 #  define DYNAMIC_ENV_FETCH
18 #  define HAS_GETENV_LEN
19 #  define prime_env_iter()
20 #  define WIN32IO_IS_STDIO              /* don't pull in custom stdio layer */
21 #  define WIN32SCK_IS_STDSCK            /* don't pull in custom wsock layer */
22 #  ifdef PERL_GLOBAL_STRUCT
23 #    error PERL_GLOBAL_STRUCT cannot be defined with PERL_IMPLICIT_SYS
24 #  endif
25 #  define win32_get_privlib PerlEnv_lib_path
26 #  define win32_get_sitelib PerlEnv_sitelib_path
27 #  define win32_get_vendorlib PerlEnv_vendorlib_path
28 #endif
29
30 #ifdef __GNUC__
31 #  ifndef __int64               /* some versions seem to #define it already */
32 #    define __int64 long long
33 #  endif
34 #  define Win32_Winsock
35 #ifdef __cplusplus
36 /* Mingw32 gcc -xc++ objects to __attribute((unused)) at least */
37 #undef  PERL_UNUSED_DECL
38 #define PERL_UNUSED_DECL
39 #endif
40 #endif
41
42
43 /* Define DllExport akin to perl's EXT,
44  * If we are in the DLL or mimicing the DLL for Win95 work round
45  * then Export the symbol,
46  * otherwise import it.
47  */
48
49 /* now even GCC supports __declspec() */
50
51 #if defined(PERLDLL) || defined(WIN95FIX)
52 #define DllExport
53 /*#define DllExport __declspec(dllexport)*/     /* noises with VC5+sp3 */
54 #else
55 #define DllExport __declspec(dllimport)
56 #endif
57
58 #define  WIN32_LEAN_AND_MEAN
59 #include <windows.h>
60
61 #ifdef   WIN32_LEAN_AND_MEAN            /* C file is NOT a Perl5 original. */
62 #define  CONTEXT        PERL_CONTEXT    /* Avoid conflict of CONTEXT defs. */
63 #endif /*WIN32_LEAN_AND_MEAN */
64
65 #ifndef TLS_OUT_OF_INDEXES
66 #define TLS_OUT_OF_INDEXES (DWORD)0xFFFFFFFF
67 #endif
68
69 #include <dirent.h>
70 #include <io.h>
71 #include <process.h>
72 #include <stdio.h>
73 #include <direct.h>
74 #include <stdlib.h>
75 #include <stddef.h>
76 #include <fcntl.h>
77 #ifndef EXT
78 #include "EXTERN.h"
79 #endif
80
81 struct tms {
82         long    tms_utime;
83         long    tms_stime;
84         long    tms_cutime;
85         long    tms_cstime;
86 };
87
88 #ifndef SYS_NMLN
89 #define SYS_NMLN        257
90 #endif
91
92 struct utsname {
93     char sysname[SYS_NMLN];
94     char nodename[SYS_NMLN];
95     char release[SYS_NMLN];
96     char version[SYS_NMLN];
97     char machine[SYS_NMLN];
98 };
99
100 #ifndef START_EXTERN_C
101 #undef EXTERN_C
102 #ifdef __cplusplus
103 #  define START_EXTERN_C extern "C" {
104 #  define END_EXTERN_C }
105 #  define EXTERN_C extern "C"
106 #else
107 #  define START_EXTERN_C
108 #  define END_EXTERN_C
109 #  define EXTERN_C
110 #endif
111 #endif
112
113 #define  STANDARD_C     1
114 #define  DOSISH         1               /* no escaping our roots */
115 #define  OP_BINARY      O_BINARY        /* mistake in in pp_sys.c? */
116
117 /* Define USE_SOCKETS_AS_HANDLES to enable emulation of windows sockets as
118  * real filehandles. XXX Should always be defined (the other version is untested) */
119 #define USE_SOCKETS_AS_HANDLES
120
121 /* read() and write() aren't transparent for socket handles */
122 #define PERL_SOCK_SYSREAD_IS_RECV
123 #define PERL_SOCK_SYSWRITE_IS_SEND
124
125 #define PERL_NO_FORCE_LINK              /* no need for PL_force_link_funcs */
126
127 /* Define USE_FIXED_OSFHANDLE to fix MSVCRT's _open_osfhandle() on W95.
128    It now uses some black magic to work seamlessly with the DLL CRT and
129    works with MSVC++ 4.0+ or GCC/Mingw32
130         -- BKS 1-24-2000 */
131 #if (defined(_M_IX86) && _MSC_VER >= 1000) || defined(__MINGW32__)
132 #define USE_FIXED_OSFHANDLE
133 #endif
134
135 /* Define PERL_WIN32_SOCK_DLOAD to have Perl dynamically load the winsock
136    DLL when needed. Don't use if your compiler supports delayloading (ie, VC++ 6.0)
137         -- BKS 5-29-2000 */
138 #if !(defined(_M_IX86) && _MSC_VER >= 1200)
139 #define PERL_WIN32_SOCK_DLOAD
140 #endif
141 #define ENV_IS_CASELESS
142
143 #define PIPESOCK_MODE   "b"             /* pipes, sockets default to binmode */
144
145 #ifndef VER_PLATFORM_WIN32_WINDOWS      /* VC-2.0 headers don't have this */
146 #define VER_PLATFORM_WIN32_WINDOWS      1
147 #endif
148
149 #ifndef FILE_SHARE_DELETE               /* VC-4.0 headers don't have this */
150 #define FILE_SHARE_DELETE               0x00000004
151 #endif
152
153 /* access() mode bits */
154 #ifndef R_OK
155 #  define       R_OK    4
156 #  define       W_OK    2
157 #  define       X_OK    1
158 #  define       F_OK    0
159 #endif
160
161 /* for waitpid() */
162 #ifndef WNOHANG
163 #  define WNOHANG       1
164 #endif
165
166 #define PERL_GET_CONTEXT_DEFINED
167
168 /* Compiler-specific stuff. */
169
170 #ifdef __BORLANDC__             /* Borland C++ */
171
172 #if (__BORLANDC__ <= 0x520)
173 #define _access access
174 #define _chdir chdir
175 #endif
176
177 #define _getpid getpid
178 #define wcsicmp _wcsicmp
179 #include <sys/types.h>
180
181 #ifndef DllMain
182 #define DllMain DllEntryPoint
183 #endif
184
185 #pragma warn -ccc       /* "condition is always true/false" */
186 #pragma warn -rch       /* "unreachable code" */
187 #pragma warn -sig       /* "conversion may lose significant digits" */
188 #pragma warn -pia       /* "possibly incorrect assignment" */
189 #pragma warn -par       /* "parameter 'foo' is never used" */
190 #pragma warn -aus       /* "'foo' is assigned a value that is never used" */
191 #pragma warn -use       /* "'foo' is declared but never used" */
192 #pragma warn -csu       /* "comparing signed and unsigned values" */
193
194 /* Borland C thinks that a pointer to a member variable is 12 bytes in size. */
195 #define PERL_MEMBER_PTR_SIZE    12
196
197 #define isnan           _isnan
198
199 #endif
200
201 #ifdef _MSC_VER                 /* Microsoft Visual C++ */
202
203 typedef long            uid_t;
204 typedef long            gid_t;
205 typedef unsigned short  mode_t;
206 #pragma  warning(disable: 4102) /* "unreferenced label" */
207
208 /* Visual C thinks that a pointer to a member variable is 16 bytes in size. */
209 #define PERL_MEMBER_PTR_SIZE    16
210
211 #define isnan           _isnan
212
213 #if _MSC_VER < 1300
214 /* VC6 has broken NaN semantics: NaN == NaN returns true instead of false */
215 #define NAN_COMPARE_BROKEN 1
216 #endif
217
218 #endif /* _MSC_VER */
219
220 #ifdef __MINGW32__              /* Minimal Gnu-Win32 */
221
222 typedef long            uid_t;
223 typedef long            gid_t;
224 #ifndef _environ
225 #define _environ        environ
226 #endif
227 #define flushall        _flushall
228 #define fcloseall       _fcloseall
229 #define isnan           _isnan  /* ...same libraries as MSVC */
230
231 #ifndef _O_NOINHERIT
232 #  define _O_NOINHERIT  0x0080
233 #  ifndef _NO_OLDNAMES
234 #    define O_NOINHERIT _O_NOINHERIT
235 #  endif
236 #endif
237
238 /* <stdint.h>, pulled in by <io.h> as of mingw-runtime-3.3, typedef's
239  * (u)intptr_t but doesn't set the _(U)INTPTR_T_DEFINED defines */
240 #ifdef _STDINT_H
241 #  ifndef _INTPTR_T_DEFINED
242 #    define _INTPTR_T_DEFINED
243 #  endif
244 #  ifndef _UINTPTR_T_DEFINED
245 #    define _UINTPTR_T_DEFINED
246 #  endif
247 #endif
248
249 #endif /* __MINGW32__ */
250
251 /* both GCC/Mingw32 and MSVC++ 4.0 are missing this, so we put it here */
252 #ifndef CP_UTF8
253 #  define CP_UTF8       65001
254 #endif
255
256 /* compatibility stuff for other compilers goes here */
257
258 #ifndef _INTPTR_T_DEFINED
259 typedef int             intptr_t;
260 #  define _INTPTR_T_DEFINED
261 #endif
262
263 #ifndef _UINTPTR_T_DEFINED
264 typedef unsigned int    uintptr_t;
265 #  define _UINTPTR_T_DEFINED
266 #endif
267
268 START_EXTERN_C
269
270 /* For UNIX compatibility. */
271
272 extern  uid_t   getuid(void);
273 extern  gid_t   getgid(void);
274 extern  uid_t   geteuid(void);
275 extern  gid_t   getegid(void);
276 extern  int     setuid(uid_t uid);
277 extern  int     setgid(gid_t gid);
278 extern  int     kill(int pid, int sig);
279 #ifndef USE_PERL_SBRK
280 extern  void    *sbrk(ptrdiff_t need);
281 #  define HAS_SBRK_PROTO
282 #endif
283 extern  char *  getlogin(void);
284 extern  int     chown(const char *p, uid_t o, gid_t g);
285 extern  int     mkstemp(const char *path);
286
287 #undef   Stat
288 #define  Stat           win32_stat
289
290 #undef   init_os_extras
291 #define  init_os_extras Perl_init_os_extras
292
293 DllExport void          Perl_win32_init(int *argcp, char ***argvp);
294 DllExport void          Perl_win32_term(void);
295 DllExport void          Perl_init_os_extras(void);
296 DllExport void          win32_str_os_error(void *sv, DWORD err);
297 DllExport int           RunPerl(int argc, char **argv, char **env);
298
299 typedef struct {
300     HANDLE      childStdIn;
301     HANDLE      childStdOut;
302     HANDLE      childStdErr;
303     /*
304      * the following correspond to the fields of the same name
305      * in the STARTUPINFO structure. Embedders can use these to
306      * control the spawning process' look.
307      * Example - to hide the window of the spawned process:
308      *    dwFlags = STARTF_USESHOWWINDOW;
309      *    wShowWindow = SW_HIDE;
310      */
311     DWORD       dwFlags;
312     DWORD       dwX;
313     DWORD       dwY;
314     DWORD       dwXSize;
315     DWORD       dwYSize;
316     DWORD       dwXCountChars;
317     DWORD       dwYCountChars;
318     DWORD       dwFillAttribute;
319     WORD        wShowWindow;
320 } child_IO_table;
321
322 DllExport void          win32_get_child_IO(child_IO_table* ptr);
323 DllExport HWND          win32_create_message_window();
324
325 #ifndef USE_SOCKETS_AS_HANDLES
326 extern FILE *           my_fdopen(int, char *);
327 #endif
328 extern int              my_fclose(FILE *);
329 extern int              my_fstat(int fd, Stat_t *sbufptr);
330 extern char *           win32_get_privlib(const char *pl);
331 extern char *           win32_get_sitelib(const char *pl);
332 extern char *           win32_get_vendorlib(const char *pl);
333 extern int              IsWin95(void);
334 extern int              IsWinNT(void);
335 extern void             win32_argv2utf8(int argc, char** argv);
336
337 #ifdef PERL_IMPLICIT_SYS
338 extern void             win32_delete_internal_host(void *h);
339 #endif
340
341 extern char *           staticlinkmodules[];
342
343 END_EXTERN_C
344
345 typedef  char *         caddr_t;        /* In malloc.c (core address). */
346
347 /*
348  * handle socket stuff, assuming socket is always available
349  */
350 #include <sys/socket.h>
351 #include <netdb.h>
352
353 #ifdef MYMALLOC
354 #define EMBEDMYMALLOC   /**/
355 /* #define USE_PERL_SBRK        /**/
356 /* #define PERL_SBRK_VIA_MALLOC /**/
357 #endif
358
359 #if defined(PERLDLL) && !defined(PERL_CORE)
360 #define PERL_CORE
361 #endif
362
363 #ifdef PERL_TEXTMODE_SCRIPTS
364 #  define PERL_SCRIPT_MODE              "r"
365 #else
366 #  define PERL_SCRIPT_MODE              "rb"
367 #endif
368
369 /*
370  * Now Win32 specific per-thread data stuff
371  */
372
373 /* Leave the first couple ids after WM_USER unused because they
374  * might be used by an embedding application, and on Windows
375  * version before 2000 we might end up eating those messages
376  * if they were not meant for us.
377  */
378 #define WM_USER_MIN     (WM_USER+30)
379 #define WM_USER_MESSAGE (WM_USER_MIN)
380 #define WM_USER_KILL    (WM_USER_MIN+1)
381 #define WM_USER_MAX     (WM_USER_MIN+1)
382
383 struct thread_intern {
384     /* XXX can probably use one buffer instead of several */
385     char                Wstrerror_buffer[512];
386     struct servent      Wservent;
387     char                Wgetlogin_buffer[128];
388 #    ifdef HAVE_DES_FCRYPT
389     char                Wcrypt_buffer[30];
390 #    endif
391 #    ifdef USE_RTL_THREAD_API
392     void *              retv;   /* slot for thread return value */
393 #    endif
394     BOOL               Wuse_showwindow;
395     WORD               Wshowwindow;
396 };
397
398 #define HAVE_INTERP_INTERN
399 typedef struct {
400     long        num;
401     DWORD       pids[MAXIMUM_WAIT_OBJECTS];
402     HANDLE      handles[MAXIMUM_WAIT_OBJECTS];
403 } child_tab;
404
405 #ifdef USE_ITHREADS
406 typedef struct {
407     long        num;
408     DWORD       pids[MAXIMUM_WAIT_OBJECTS];
409     HANDLE      handles[MAXIMUM_WAIT_OBJECTS];
410     HWND        message_hwnds[MAXIMUM_WAIT_OBJECTS];
411 } pseudo_child_tab;
412 #endif
413
414 #ifndef Sighandler_t
415 typedef Signal_t (*Sighandler_t) (int);
416 #define Sighandler_t    Sighandler_t
417 #endif
418
419 struct interp_intern {
420     char *      perlshell_tokens;
421     char **     perlshell_vec;
422     long        perlshell_items;
423     struct av * fdpid;
424     child_tab * children;
425 #ifdef USE_ITHREADS
426     DWORD       pseudo_id;
427     pseudo_child_tab * pseudo_children;
428 #endif
429     void *      internal_host;
430     struct thread_intern        thr_intern;
431     HWND        message_hwnd;
432     UINT        timerid;
433     unsigned    poll_count;
434     Sighandler_t sigtable[SIG_SIZE];
435 };
436
437 DllExport int win32_async_check(pTHX);
438
439 #define WIN32_POLL_INTERVAL 32768
440 #define PERL_ASYNC_CHECK() if (w32_do_async || PL_sig_pending) win32_async_check(aTHX)
441
442 #define w32_perlshell_tokens    (PL_sys_intern.perlshell_tokens)
443 #define w32_perlshell_vec       (PL_sys_intern.perlshell_vec)
444 #define w32_perlshell_items     (PL_sys_intern.perlshell_items)
445 #define w32_fdpid               (PL_sys_intern.fdpid)
446 #define w32_children            (PL_sys_intern.children)
447 #define w32_num_children        (w32_children->num)
448 #define w32_child_pids          (w32_children->pids)
449 #define w32_child_handles       (w32_children->handles)
450 #define w32_pseudo_id           (PL_sys_intern.pseudo_id)
451 #define w32_pseudo_children     (PL_sys_intern.pseudo_children)
452 #define w32_num_pseudo_children         (w32_pseudo_children->num)
453 #define w32_pseudo_child_pids           (w32_pseudo_children->pids)
454 #define w32_pseudo_child_handles        (w32_pseudo_children->handles)
455 #define w32_pseudo_child_message_hwnds  (w32_pseudo_children->message_hwnds)
456 #define w32_internal_host               (PL_sys_intern.internal_host)
457 #define w32_timerid                     (PL_sys_intern.timerid)
458 #define w32_message_hwnd                (PL_sys_intern.message_hwnd)
459 #define w32_sighandler                  (PL_sys_intern.sigtable)
460 #define w32_poll_count                  (PL_sys_intern.poll_count)
461 #define w32_do_async                    (w32_poll_count++ > WIN32_POLL_INTERVAL)
462 #define w32_strerror_buffer     (PL_sys_intern.thr_intern.Wstrerror_buffer)
463 #define w32_getlogin_buffer     (PL_sys_intern.thr_intern.Wgetlogin_buffer)
464 #define w32_crypt_buffer        (PL_sys_intern.thr_intern.Wcrypt_buffer)
465 #define w32_servent             (PL_sys_intern.thr_intern.Wservent)
466 #define w32_use_showwindow      (PL_sys_intern.thr_intern.Wuse_showwindow)
467 #define w32_showwindow  (PL_sys_intern.thr_intern.Wshowwindow)
468
469 #ifdef USE_ITHREADS
470 #  define PERL_WAIT_FOR_CHILDREN \
471     STMT_START {                                                        \
472         if (w32_pseudo_children && w32_num_pseudo_children) {           \
473             long children = w32_num_pseudo_children;                    \
474             WaitForMultipleObjects(children,                            \
475                                    w32_pseudo_child_handles,            \
476                                    TRUE, INFINITE);                     \
477             while (children)                                            \
478                 CloseHandle(w32_pseudo_child_handles[--children]);      \
479         }                                                               \
480     } STMT_END
481 #endif
482
483 #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
484 #ifdef PERL_CORE
485
486 /* C doesn't like repeat struct definitions */
487 #if defined(__MINGW32__) && (__MINGW32_MAJOR_VERSION>=3)
488 #undef _CRTIMP
489 #endif
490 #ifndef _CRTIMP
491 #define _CRTIMP __declspec(dllimport)
492 #endif
493
494 /*
495  * Control structure for lowio file handles
496  */
497 typedef struct {
498     intptr_t osfhnd;/* underlying OS file HANDLE */
499     char osfile;    /* attributes of file (e.g., open in text mode?) */
500     char pipech;    /* one char buffer for handles opened on pipes */
501     int lockinitflag;
502     CRITICAL_SECTION lock;
503 } ioinfo;
504
505
506 /*
507  * Array of arrays of control structures for lowio files.
508  */
509 EXTERN_C _CRTIMP ioinfo* __pioinfo[];
510
511 /*
512  * Definition of IOINFO_L2E, the log base 2 of the number of elements in each
513  * array of ioinfo structs.
514  */
515 #define IOINFO_L2E          5
516
517 /*
518  * Definition of IOINFO_ARRAY_ELTS, the number of elements in ioinfo array
519  */
520 #define IOINFO_ARRAY_ELTS   (1 << IOINFO_L2E)
521
522 /*
523  * Access macros for getting at an ioinfo struct and its fields from a
524  * file handle
525  */
526 #define _pioinfo(i) (__pioinfo[(i) >> IOINFO_L2E] + ((i) & (IOINFO_ARRAY_ELTS - 1)))
527 #define _osfhnd(i)  (_pioinfo(i)->osfhnd)
528 #define _osfile(i)  (_pioinfo(i)->osfile)
529 #define _pipech(i)  (_pioinfo(i)->pipech)
530
531 /* since we are not doing a dup2(), this works fine */
532 #define _set_osfhnd(fh, osfh) (void)(_osfhnd(fh) = (intptr_t)osfh)
533 #endif
534 #endif
535
536 /* IO.xs and POSIX.xs define PERLIO_NOT_STDIO to 1 */
537 #if defined(PERL_EXT_IO) || defined(PERL_EXT_POSIX)
538 #undef  PERLIO_NOT_STDIO
539 #endif
540 #define PERLIO_NOT_STDIO 0
541
542 #include "perlio.h"
543
544 /*
545  * This provides a layer of functions and macros to ensure extensions will
546  * get to use the same RTL functions as the core.
547  */
548 #include "win32iop.h"
549
550 #define EXEC_ARGV_CAST(x) ((const char *const *) x)
551
552 #if !defined(ECONNABORTED) && defined(WSAECONNABORTED)
553 #define ECONNABORTED WSAECONNABORTED
554 #endif
555 #if !defined(ECONNRESET) && defined(WSAECONNRESET)
556 #define ECONNRESET WSAECONNRESET
557 #endif
558 #if !defined(EAFNOSUPPORT) && defined(WSAEAFNOSUPPORT)
559 #define EAFNOSUPPORT WSAEAFNOSUPPORT
560 #endif
561 /* Why not needed for ECONNREFUSED? --abe */
562
563 DllExport void *win32_signal_context(void);
564 #define PERL_GET_SIG_CONTEXT win32_signal_context()
565
566 #endif /* _INC_WIN32_PERL5 */
567