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