This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Don’t leak deleted iterator when tying hash
[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 __declspec(dllexport)
52 #else
53 #define DllExport __declspec(dllimport)
54 #endif
55
56 /* The Perl APIs can only be called directly inside the perl5xx.dll.
57  * All other code has to import them.  By declaring them as "dllimport"
58  * we tell the compiler to generate an indirect call instruction and
59  * avoid redirection through a call thunk.
60  *
61  * The XS code in the re extension is special, in that it redefines
62  * core APIs locally, so don't mark them as "dllimport" because GCC
63  * cannot handle this situation.
64  */
65 #if !defined(PERLDLL) && !defined(PERL_EXT_RE_BUILD)
66 #  ifdef __cplusplus
67 #    define PERL_CALLCONV extern "C" __declspec(dllimport)
68 #    ifdef _MSC_VER
69 #      define PERL_CALLCONV_NO_RET extern "C" __declspec(dllimport) __declspec(noreturn)
70 #    endif
71 #  else
72 #    define PERL_CALLCONV __declspec(dllimport)
73 #    ifdef _MSC_VER
74 #      define PERL_CALLCONV_NO_RET __declspec(dllimport) __declspec(noreturn)
75 #    endif
76 #  endif
77 #else /* MSVC noreturn support inside the interp */
78 #  ifdef _MSC_VER
79 #    define PERL_CALLCONV_NO_RET __declspec(noreturn)
80 #  endif
81 #endif
82
83 #define  WIN32_LEAN_AND_MEAN
84 #include <windows.h>
85
86 /*
87  * Bug in winbase.h in mingw-w64 4.4.0-1 at least... they
88  * do #define GetEnvironmentStringsA GetEnvironmentStrings and fail
89  * to declare GetEnvironmentStringsA.
90  */
91 #if defined(__MINGW64__) && defined(GetEnvironmentStringsA) && !defined(UNICODE)
92 #ifdef __cplusplus
93 extern "C" {
94 #endif
95 #undef GetEnvironmentStringsA
96 WINBASEAPI LPCH WINAPI GetEnvironmentStringsA(VOID);
97 #define GetEnvironmentStrings GetEnvironmentStringsA
98 #ifdef __cplusplus
99 }
100 #endif
101 #endif
102
103 #ifdef   WIN32_LEAN_AND_MEAN            /* C file is NOT a Perl5 original. */
104 #define  CONTEXT        PERL_CONTEXT    /* Avoid conflict of CONTEXT defs. */
105 #endif /*WIN32_LEAN_AND_MEAN */
106
107 #ifndef TLS_OUT_OF_INDEXES
108 #define TLS_OUT_OF_INDEXES (DWORD)0xFFFFFFFF
109 #endif
110
111 #include <dirent.h>
112 #include <io.h>
113 #include <process.h>
114 #include <stdio.h>
115 #include <direct.h>
116 #include <stdlib.h>
117 #include <stddef.h>
118 #include <fcntl.h>
119 #ifndef EXT
120 #include "EXTERN.h"
121 #endif
122
123 struct tms {
124         long    tms_utime;
125         long    tms_stime;
126         long    tms_cutime;
127         long    tms_cstime;
128 };
129
130 #ifndef SYS_NMLN
131 #define SYS_NMLN        257
132 #endif
133
134 struct utsname {
135     char sysname[SYS_NMLN];
136     char nodename[SYS_NMLN];
137     char release[SYS_NMLN];
138     char version[SYS_NMLN];
139     char machine[SYS_NMLN];
140 };
141
142 #ifndef START_EXTERN_C
143 #undef EXTERN_C
144 #ifdef __cplusplus
145 #  define START_EXTERN_C extern "C" {
146 #  define END_EXTERN_C }
147 #  define EXTERN_C extern "C"
148 #else
149 #  define START_EXTERN_C
150 #  define END_EXTERN_C
151 #  define EXTERN_C
152 #endif
153 #endif
154
155 #define  STANDARD_C     1
156 #define  DOSISH         1               /* no escaping our roots */
157 #define  OP_BINARY      O_BINARY        /* mistake in in pp_sys.c? */
158
159 /* Define USE_SOCKETS_AS_HANDLES to enable emulation of windows sockets as
160  * real filehandles. XXX Should always be defined (the other version is untested) */
161 #define USE_SOCKETS_AS_HANDLES
162
163 /* read() and write() aren't transparent for socket handles */
164 #define PERL_SOCK_SYSREAD_IS_RECV
165 #define PERL_SOCK_SYSWRITE_IS_SEND
166
167 #define PERL_NO_FORCE_LINK              /* no need for PL_force_link_funcs */
168
169 #define ENV_IS_CASELESS
170
171 #define PIPESOCK_MODE   "b"             /* pipes, sockets default to binmode */
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 /* VC uses non-standard way to determine the size and alignment if bit-fields */
191 /* MinGW will compile with -mms-bitfields, so should use the same types */
192 #define PERL_BITFIELD8  unsigned char
193 #define PERL_BITFIELD16 unsigned short
194 #define PERL_BITFIELD32 unsigned int
195
196 #ifdef _MSC_VER                 /* Microsoft Visual C++ */
197
198 #ifndef UNDER_CE
199 typedef long            uid_t;
200 typedef long            gid_t;
201 typedef unsigned short  mode_t;
202 #endif
203
204 #pragma  warning(disable: 4102) /* "unreferenced label" */
205
206 #define isnan           _isnan
207 #define snprintf        _snprintf
208 #define vsnprintf       _vsnprintf
209
210 #if _MSC_VER < 1300
211 /* VC6 has broken NaN semantics: NaN == NaN returns true instead of false */
212 #define NAN_COMPARE_BROKEN 1
213 #endif
214
215 #endif /* _MSC_VER */
216
217 #ifdef __MINGW32__              /* Minimal Gnu-Win32 */
218
219 typedef long            uid_t;
220 typedef long            gid_t;
221 #ifndef _environ
222 #define _environ        environ
223 #endif
224 #define flushall        _flushall
225 #define fcloseall       _fcloseall
226 #ifndef isnan
227 #define isnan           _isnan  /* ...same libraries as MSVC */
228 #endif
229
230 #ifndef _O_NOINHERIT
231 #  define _O_NOINHERIT  0x0080
232 #  ifndef _NO_OLDNAMES
233 #    define O_NOINHERIT _O_NOINHERIT
234 #  endif
235 #endif
236
237 /* <stdint.h>, pulled in by <io.h> as of mingw-runtime-3.3, typedef's
238  * (u)intptr_t but doesn't set the _(U)INTPTR_T_DEFINED defines */
239 #ifdef _STDINT_H
240 #  ifndef _INTPTR_T_DEFINED
241 #    define _INTPTR_T_DEFINED
242 #  endif
243 #  ifndef _UINTPTR_T_DEFINED
244 #    define _UINTPTR_T_DEFINED
245 #  endif
246 #endif
247
248 #ifndef CP_UTF8
249 #  define CP_UTF8       65001
250 #endif
251
252 #endif /* __MINGW32__ */
253
254 #ifndef _INTPTR_T_DEFINED
255 typedef int             intptr_t;
256 #  define _INTPTR_T_DEFINED
257 #endif
258
259 #ifndef _UINTPTR_T_DEFINED
260 typedef unsigned int    uintptr_t;
261 #  define _UINTPTR_T_DEFINED
262 #endif
263
264 START_EXTERN_C
265
266 /* For UNIX compatibility. */
267
268 extern  uid_t   getuid(void);
269 extern  gid_t   getgid(void);
270 extern  uid_t   geteuid(void);
271 extern  gid_t   getegid(void);
272 extern  int     setuid(uid_t uid);
273 extern  int     setgid(gid_t gid);
274 extern  int     kill(int pid, int sig);
275 extern  int     killpg(int pid, int sig);
276 #ifndef USE_PERL_SBRK
277 extern  void    *sbrk(ptrdiff_t need);
278 #  define HAS_SBRK_PROTO
279 #endif
280 extern  char *  getlogin(void);
281 extern  int     chown(const char *p, uid_t o, gid_t g);
282 extern  int     mkstemp(const char *path);
283
284 #undef   Stat
285 #define  Stat           win32_stat
286
287 #undef   init_os_extras
288 #define  init_os_extras Perl_init_os_extras
289
290 DllExport void          Perl_win32_init(int *argcp, char ***argvp);
291 DllExport void          Perl_win32_term(void);
292 DllExport void          Perl_init_os_extras(void);
293 DllExport void          win32_str_os_error(void *sv, DWORD err);
294 DllExport int           RunPerl(int argc, char **argv, char **env);
295
296 typedef struct {
297     HANDLE      childStdIn;
298     HANDLE      childStdOut;
299     HANDLE      childStdErr;
300     /*
301      * the following correspond to the fields of the same name
302      * in the STARTUPINFO structure. Embedders can use these to
303      * control the spawning process' look.
304      * Example - to hide the window of the spawned process:
305      *    dwFlags = STARTF_USESHOWWINDOW;
306      *    wShowWindow = SW_HIDE;
307      */
308     DWORD       dwFlags;
309     DWORD       dwX;
310     DWORD       dwY;
311     DWORD       dwXSize;
312     DWORD       dwYSize;
313     DWORD       dwXCountChars;
314     DWORD       dwYCountChars;
315     DWORD       dwFillAttribute;
316     WORD        wShowWindow;
317 } child_IO_table;
318
319 DllExport void          win32_get_child_IO(child_IO_table* ptr);
320 DllExport HWND          win32_create_message_window(void);
321
322 #ifndef USE_SOCKETS_AS_HANDLES
323 extern FILE *           my_fdopen(int, char *);
324 #endif
325 extern int              my_fclose(FILE *);
326 extern char *           win32_get_privlib(const char *pl, STRLEN *const len);
327 extern char *           win32_get_sitelib(const char *pl, STRLEN *const len);
328 extern char *           win32_get_vendorlib(const char *pl, STRLEN *const len);
329
330 #ifdef PERL_IMPLICIT_SYS
331 extern void             win32_delete_internal_host(void *h);
332 #endif
333
334 extern char *           staticlinkmodules[];
335
336 END_EXTERN_C
337
338 typedef  char *         caddr_t;        /* In malloc.c (core address). */
339
340 /*
341  * handle socket stuff, assuming socket is always available
342  */
343 #include <sys/socket.h>
344 #include <netdb.h>
345
346 #ifdef MYMALLOC
347 #define EMBEDMYMALLOC   /**/
348 /* #define USE_PERL_SBRK        /**/
349 /* #define PERL_SBRK_VIA_MALLOC /**/
350 #endif
351
352 #ifdef PERL_TEXTMODE_SCRIPTS
353 #  define PERL_SCRIPT_MODE              "r"
354 #else
355 #  define PERL_SCRIPT_MODE              "rb"
356 #endif
357
358 /*
359  * Now Win32 specific per-thread data stuff
360  */
361
362 /* Leave the first couple ids after WM_USER unused because they
363  * might be used by an embedding application, and on Windows
364  * version before 2000 we might end up eating those messages
365  * if they were not meant for us.
366  */
367 #define WM_USER_MIN     (WM_USER+30)
368 #define WM_USER_MESSAGE (WM_USER_MIN)
369 #define WM_USER_KILL    (WM_USER_MIN+1)
370 #define WM_USER_MAX     (WM_USER_MIN+1)
371
372 struct thread_intern {
373     /* XXX can probably use one buffer instead of several */
374     char                Wstrerror_buffer[512];
375     struct servent      Wservent;
376     char                Wgetlogin_buffer[128];
377 #    ifdef USE_SOCKETS_AS_HANDLES
378     int                 Winit_socktype;
379 #    endif
380     char                Wcrypt_buffer[30];
381 #    ifdef USE_RTL_THREAD_API
382     void *              retv;   /* slot for thread return value */
383 #    endif
384     BOOL               Wuse_showwindow;
385     WORD               Wshowwindow;
386 };
387
388 #define HAVE_INTERP_INTERN
389 typedef struct {
390     long        num;
391     DWORD       pids[MAXIMUM_WAIT_OBJECTS];
392     HANDLE      handles[MAXIMUM_WAIT_OBJECTS];
393 } child_tab;
394
395 #ifdef USE_ITHREADS
396 typedef struct {
397     long        num;
398     DWORD       pids[MAXIMUM_WAIT_OBJECTS];
399     HANDLE      handles[MAXIMUM_WAIT_OBJECTS];
400     HWND        message_hwnds[MAXIMUM_WAIT_OBJECTS];
401     char        sigterm[MAXIMUM_WAIT_OBJECTS];
402 } pseudo_child_tab;
403 #endif
404
405 #ifndef Sighandler_t
406 typedef Signal_t (*Sighandler_t) (int);
407 #define Sighandler_t    Sighandler_t
408 #endif
409
410 struct interp_intern {
411     char *      perlshell_tokens;
412     char **     perlshell_vec;
413     long        perlshell_items;
414     struct av * fdpid;
415     child_tab * children;
416 #ifdef USE_ITHREADS
417     DWORD       pseudo_id;
418     pseudo_child_tab * pseudo_children;
419 #endif
420     void *      internal_host;
421     struct thread_intern        thr_intern;
422     HWND        message_hwnd;
423     UINT        timerid;
424     unsigned    poll_count;
425     Sighandler_t sigtable[SIG_SIZE];
426 };
427
428 DllExport int win32_async_check(pTHX);
429
430 #define WIN32_POLL_INTERVAL 32768
431 #define PERL_ASYNC_CHECK() if (w32_do_async || PL_sig_pending) win32_async_check(aTHX)
432
433 #define w32_perlshell_tokens    (PL_sys_intern.perlshell_tokens)
434 #define w32_perlshell_vec       (PL_sys_intern.perlshell_vec)
435 #define w32_perlshell_items     (PL_sys_intern.perlshell_items)
436 #define w32_fdpid               (PL_sys_intern.fdpid)
437 #define w32_children            (PL_sys_intern.children)
438 #define w32_num_children        (w32_children->num)
439 #define w32_child_pids          (w32_children->pids)
440 #define w32_child_handles       (w32_children->handles)
441 #define w32_pseudo_id           (PL_sys_intern.pseudo_id)
442 #define w32_pseudo_children     (PL_sys_intern.pseudo_children)
443 #define w32_num_pseudo_children         (w32_pseudo_children->num)
444 #define w32_pseudo_child_pids           (w32_pseudo_children->pids)
445 #define w32_pseudo_child_handles        (w32_pseudo_children->handles)
446 #define w32_pseudo_child_message_hwnds  (w32_pseudo_children->message_hwnds)
447 #define w32_pseudo_child_sigterm        (w32_pseudo_children->sigterm)
448 #define w32_internal_host               (PL_sys_intern.internal_host)
449 #define w32_timerid                     (PL_sys_intern.timerid)
450 #define w32_message_hwnd                (PL_sys_intern.message_hwnd)
451 #define w32_sighandler                  (PL_sys_intern.sigtable)
452 #define w32_poll_count                  (PL_sys_intern.poll_count)
453 #define w32_do_async                    (w32_poll_count++ > WIN32_POLL_INTERVAL)
454 #define w32_strerror_buffer     (PL_sys_intern.thr_intern.Wstrerror_buffer)
455 #define w32_getlogin_buffer     (PL_sys_intern.thr_intern.Wgetlogin_buffer)
456 #define w32_crypt_buffer        (PL_sys_intern.thr_intern.Wcrypt_buffer)
457 #define w32_servent             (PL_sys_intern.thr_intern.Wservent)
458 #define w32_init_socktype       (PL_sys_intern.thr_intern.Winit_socktype)
459 #define w32_use_showwindow      (PL_sys_intern.thr_intern.Wuse_showwindow)
460 #define w32_showwindow  (PL_sys_intern.thr_intern.Wshowwindow)
461
462 #ifdef USE_ITHREADS
463 void win32_wait_for_children(pTHX);
464 #  define PERL_WAIT_FOR_CHILDREN win32_wait_for_children(aTHX)
465 #endif
466
467 /* IO.xs and POSIX.xs define PERLIO_NOT_STDIO to 1 */
468 #if defined(PERL_EXT_IO) || defined(PERL_EXT_POSIX)
469 #undef  PERLIO_NOT_STDIO
470 #endif
471 #define PERLIO_NOT_STDIO 0
472
473 #include "perlio.h"
474
475 /*
476  * This provides a layer of functions and macros to ensure extensions will
477  * get to use the same RTL functions as the core.
478  */
479 #include "win32iop.h"
480
481 #define EXEC_ARGV_CAST(x) ((const char *const *) x)
482
483 DllExport void *win32_signal_context(void);
484 #define PERL_GET_SIG_CONTEXT win32_signal_context()
485
486 #ifdef UNDER_CE
487 #define Win_GetModuleHandle   XCEGetModuleHandleA
488 #define Win_GetProcAddress    XCEGetProcAddressA
489 #define Win_GetModuleFileName XCEGetModuleFileNameA
490 #define Win_CreateSemaphore   CreateSemaphoreW
491 #else
492 #define Win_GetModuleHandle   GetModuleHandle
493 #define Win_GetProcAddress    GetProcAddress
494 #define Win_GetModuleFileName GetModuleFileName
495 #define Win_CreateSemaphore   CreateSemaphore
496 #endif
497
498 #endif /* _INC_WIN32_PERL5 */
499