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