Signedness nit, found by Jarkko
[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 -8004      /* "'foo' is assigned a value that is never used" */
186 #pragma warn -8008      /* "condition is always true/false" */
187 #pragma warn -8012      /* "comparing signed and unsigned values" */
188 #pragma warn -8027      /* "functions containing %s are not expanded inline" */
189 #pragma warn -8057      /* "parameter 'foo' is never used" */
190 #pragma warn -8060      /* "possibly incorrect assignment" */
191 #pragma warn -8066      /* "unreachable code" */
192 #pragma warn -8071      /* "conversion may lose significant digits" */
193 #pragma warn -8080      /* "'foo' is declared but never used" */
194
195 /* Borland C thinks that a pointer to a member variable is 12 bytes in size. */
196 #define PERL_MEMBER_PTR_SIZE    12
197
198 #define isnan           _isnan
199
200 #endif
201
202 #ifdef _MSC_VER                 /* Microsoft Visual C++ */
203
204 #ifndef UNDER_CE
205 typedef long            uid_t;
206 typedef long            gid_t;
207 typedef unsigned short  mode_t;
208 #endif
209
210 #pragma  warning(disable: 4102) /* "unreferenced label" */
211
212 /* Visual C thinks that a pointer to a member variable is 16 bytes in size. */
213 #define PERL_MEMBER_PTR_SIZE    16
214
215 #define isnan           _isnan
216 #define snprintf        _snprintf
217 #define vsnprintf       _vsnprintf
218
219 #if _MSC_VER < 1300
220 /* VC6 has broken NaN semantics: NaN == NaN returns true instead of false */
221 #define NAN_COMPARE_BROKEN 1
222 #endif
223
224 #endif /* _MSC_VER */
225
226 #ifdef __MINGW32__              /* Minimal Gnu-Win32 */
227
228 typedef long            uid_t;
229 typedef long            gid_t;
230 #ifndef _environ
231 #define _environ        environ
232 #endif
233 #define flushall        _flushall
234 #define fcloseall       _fcloseall
235 #define isnan           _isnan  /* ...same libraries as MSVC */
236
237 #ifndef _O_NOINHERIT
238 #  define _O_NOINHERIT  0x0080
239 #  ifndef _NO_OLDNAMES
240 #    define O_NOINHERIT _O_NOINHERIT
241 #  endif
242 #endif
243
244 /* <stdint.h>, pulled in by <io.h> as of mingw-runtime-3.3, typedef's
245  * (u)intptr_t but doesn't set the _(U)INTPTR_T_DEFINED defines */
246 #ifdef _STDINT_H
247 #  ifndef _INTPTR_T_DEFINED
248 #    define _INTPTR_T_DEFINED
249 #  endif
250 #  ifndef _UINTPTR_T_DEFINED
251 #    define _UINTPTR_T_DEFINED
252 #  endif
253 #endif
254
255 #endif /* __MINGW32__ */
256
257 /* both GCC/Mingw32 and MSVC++ 4.0 are missing this, so we put it here */
258 #ifndef CP_UTF8
259 #  define CP_UTF8       65001
260 #endif
261
262 /* compatibility stuff for other compilers goes here */
263
264 #ifndef _INTPTR_T_DEFINED
265 typedef int             intptr_t;
266 #  define _INTPTR_T_DEFINED
267 #endif
268
269 #ifndef _UINTPTR_T_DEFINED
270 typedef unsigned int    uintptr_t;
271 #  define _UINTPTR_T_DEFINED
272 #endif
273
274 START_EXTERN_C
275
276 /* For UNIX compatibility. */
277
278 extern  uid_t   getuid(void);
279 extern  gid_t   getgid(void);
280 extern  uid_t   geteuid(void);
281 extern  gid_t   getegid(void);
282 extern  int     setuid(uid_t uid);
283 extern  int     setgid(gid_t gid);
284 extern  int     kill(int pid, int sig);
285 extern  int     killpg(int pid, int sig);
286 #ifndef USE_PERL_SBRK
287 extern  void    *sbrk(ptrdiff_t need);
288 #  define HAS_SBRK_PROTO
289 #endif
290 extern  char *  getlogin(void);
291 extern  int     chown(const char *p, uid_t o, gid_t g);
292 extern  int     mkstemp(const char *path);
293
294 #undef   Stat
295 #define  Stat           win32_stat
296
297 #undef   init_os_extras
298 #define  init_os_extras Perl_init_os_extras
299
300 DllExport void          Perl_win32_init(int *argcp, char ***argvp);
301 DllExport void          Perl_win32_term(void);
302 DllExport void          Perl_init_os_extras(void);
303 DllExport void          win32_str_os_error(void *sv, DWORD err);
304 DllExport int           RunPerl(int argc, char **argv, char **env);
305
306 typedef struct {
307     HANDLE      childStdIn;
308     HANDLE      childStdOut;
309     HANDLE      childStdErr;
310     /*
311      * the following correspond to the fields of the same name
312      * in the STARTUPINFO structure. Embedders can use these to
313      * control the spawning process' look.
314      * Example - to hide the window of the spawned process:
315      *    dwFlags = STARTF_USESHOWWINDOW;
316      *    wShowWindow = SW_HIDE;
317      */
318     DWORD       dwFlags;
319     DWORD       dwX;
320     DWORD       dwY;
321     DWORD       dwXSize;
322     DWORD       dwYSize;
323     DWORD       dwXCountChars;
324     DWORD       dwYCountChars;
325     DWORD       dwFillAttribute;
326     WORD        wShowWindow;
327 } child_IO_table;
328
329 DllExport void          win32_get_child_IO(child_IO_table* ptr);
330 DllExport HWND          win32_create_message_window(void);
331
332 #ifndef USE_SOCKETS_AS_HANDLES
333 extern FILE *           my_fdopen(int, char *);
334 #endif
335 extern int              my_fclose(FILE *);
336 extern int              my_fstat(int fd, Stat_t *sbufptr);
337 extern char *           win32_get_privlib(const char *pl);
338 extern char *           win32_get_sitelib(const char *pl);
339 extern char *           win32_get_vendorlib(const char *pl);
340 extern int              IsWin95(void);
341 extern int              IsWinNT(void);
342 extern void             win32_argv2utf8(int argc, char** argv);
343
344 #ifdef PERL_IMPLICIT_SYS
345 extern void             win32_delete_internal_host(void *h);
346 #endif
347
348 extern char *           staticlinkmodules[];
349
350 END_EXTERN_C
351
352 typedef  char *         caddr_t;        /* In malloc.c (core address). */
353
354 /*
355  * handle socket stuff, assuming socket is always available
356  */
357 #include <sys/socket.h>
358 #include <netdb.h>
359
360 #ifdef MYMALLOC
361 #define EMBEDMYMALLOC   /**/
362 /* #define USE_PERL_SBRK        /**/
363 /* #define PERL_SBRK_VIA_MALLOC /**/
364 #endif
365
366 #if defined(PERLDLL) && !defined(PERL_CORE)
367 #define PERL_CORE
368 #endif
369
370 #ifdef PERL_TEXTMODE_SCRIPTS
371 #  define PERL_SCRIPT_MODE              "r"
372 #else
373 #  define PERL_SCRIPT_MODE              "rb"
374 #endif
375
376 /*
377  * Now Win32 specific per-thread data stuff
378  */
379
380 /* Leave the first couple ids after WM_USER unused because they
381  * might be used by an embedding application, and on Windows
382  * version before 2000 we might end up eating those messages
383  * if they were not meant for us.
384  */
385 #define WM_USER_MIN     (WM_USER+30)
386 #define WM_USER_MESSAGE (WM_USER_MIN)
387 #define WM_USER_KILL    (WM_USER_MIN+1)
388 #define WM_USER_MAX     (WM_USER_MIN+1)
389
390 struct thread_intern {
391     /* XXX can probably use one buffer instead of several */
392     char                Wstrerror_buffer[512];
393     struct servent      Wservent;
394     char                Wgetlogin_buffer[128];
395 #    ifdef USE_SOCKETS_AS_HANDLES
396     int                 Winit_socktype;
397 #    endif
398 #    ifdef HAVE_DES_FCRYPT
399     char                Wcrypt_buffer[30];
400 #    endif
401 #    ifdef USE_RTL_THREAD_API
402     void *              retv;   /* slot for thread return value */
403 #    endif
404     BOOL               Wuse_showwindow;
405     WORD               Wshowwindow;
406 };
407
408 #define HAVE_INTERP_INTERN
409 typedef struct {
410     long        num;
411     DWORD       pids[MAXIMUM_WAIT_OBJECTS];
412     HANDLE      handles[MAXIMUM_WAIT_OBJECTS];
413 } child_tab;
414
415 #ifdef USE_ITHREADS
416 typedef struct {
417     long        num;
418     DWORD       pids[MAXIMUM_WAIT_OBJECTS];
419     HANDLE      handles[MAXIMUM_WAIT_OBJECTS];
420     HWND        message_hwnds[MAXIMUM_WAIT_OBJECTS];
421 } pseudo_child_tab;
422 #endif
423
424 #ifndef Sighandler_t
425 typedef Signal_t (*Sighandler_t) (int);
426 #define Sighandler_t    Sighandler_t
427 #endif
428
429 struct interp_intern {
430     char *      perlshell_tokens;
431     char **     perlshell_vec;
432     long        perlshell_items;
433     struct av * fdpid;
434     child_tab * children;
435 #ifdef USE_ITHREADS
436     DWORD       pseudo_id;
437     pseudo_child_tab * pseudo_children;
438 #endif
439     void *      internal_host;
440     struct thread_intern        thr_intern;
441     HWND        message_hwnd;
442     UINT        timerid;
443     unsigned    poll_count;
444     Sighandler_t sigtable[SIG_SIZE];
445 };
446
447 DllExport int win32_async_check(pTHX);
448
449 #define WIN32_POLL_INTERVAL 32768
450 #define PERL_ASYNC_CHECK() if (w32_do_async || PL_sig_pending) win32_async_check(aTHX)
451
452 #define w32_perlshell_tokens    (PL_sys_intern.perlshell_tokens)
453 #define w32_perlshell_vec       (PL_sys_intern.perlshell_vec)
454 #define w32_perlshell_items     (PL_sys_intern.perlshell_items)
455 #define w32_fdpid               (PL_sys_intern.fdpid)
456 #define w32_children            (PL_sys_intern.children)
457 #define w32_num_children        (w32_children->num)
458 #define w32_child_pids          (w32_children->pids)
459 #define w32_child_handles       (w32_children->handles)
460 #define w32_pseudo_id           (PL_sys_intern.pseudo_id)
461 #define w32_pseudo_children     (PL_sys_intern.pseudo_children)
462 #define w32_num_pseudo_children         (w32_pseudo_children->num)
463 #define w32_pseudo_child_pids           (w32_pseudo_children->pids)
464 #define w32_pseudo_child_handles        (w32_pseudo_children->handles)
465 #define w32_pseudo_child_message_hwnds  (w32_pseudo_children->message_hwnds)
466 #define w32_internal_host               (PL_sys_intern.internal_host)
467 #define w32_timerid                     (PL_sys_intern.timerid)
468 #define w32_message_hwnd                (PL_sys_intern.message_hwnd)
469 #define w32_sighandler                  (PL_sys_intern.sigtable)
470 #define w32_poll_count                  (PL_sys_intern.poll_count)
471 #define w32_do_async                    (w32_poll_count++ > WIN32_POLL_INTERVAL)
472 #define w32_strerror_buffer     (PL_sys_intern.thr_intern.Wstrerror_buffer)
473 #define w32_getlogin_buffer     (PL_sys_intern.thr_intern.Wgetlogin_buffer)
474 #define w32_crypt_buffer        (PL_sys_intern.thr_intern.Wcrypt_buffer)
475 #define w32_servent             (PL_sys_intern.thr_intern.Wservent)
476 #define w32_init_socktype       (PL_sys_intern.thr_intern.Winit_socktype)
477 #define w32_use_showwindow      (PL_sys_intern.thr_intern.Wuse_showwindow)
478 #define w32_showwindow  (PL_sys_intern.thr_intern.Wshowwindow)
479
480 #ifdef USE_ITHREADS
481 #  define PERL_WAIT_FOR_CHILDREN \
482     STMT_START {                                                        \
483         if (w32_pseudo_children && w32_num_pseudo_children) {           \
484             long children = w32_num_pseudo_children;                    \
485             WaitForMultipleObjects(children,                            \
486                                    w32_pseudo_child_handles,            \
487                                    TRUE, INFINITE);                     \
488             while (children)                                            \
489                 CloseHandle(w32_pseudo_child_handles[--children]);      \
490         }                                                               \
491     } STMT_END
492 #endif
493
494 #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
495 #ifdef PERL_CORE
496
497 /* C doesn't like repeat struct definitions */
498 #if defined(__MINGW32__) && (__MINGW32_MAJOR_VERSION>=3)
499 #undef _CRTIMP
500 #endif
501 #ifndef _CRTIMP
502 #define _CRTIMP __declspec(dllimport)
503 #endif
504
505 /*
506  * Control structure for lowio file handles
507  */
508 typedef struct {
509     intptr_t osfhnd;/* underlying OS file HANDLE */
510     char osfile;    /* attributes of file (e.g., open in text mode?) */
511     char pipech;    /* one char buffer for handles opened on pipes */
512     int lockinitflag;
513     CRITICAL_SECTION lock;
514 } ioinfo;
515
516
517 /*
518  * Array of arrays of control structures for lowio files.
519  */
520 EXTERN_C _CRTIMP ioinfo* __pioinfo[];
521
522 /*
523  * Definition of IOINFO_L2E, the log base 2 of the number of elements in each
524  * array of ioinfo structs.
525  */
526 #define IOINFO_L2E          5
527
528 /*
529  * Definition of IOINFO_ARRAY_ELTS, the number of elements in ioinfo array
530  */
531 #define IOINFO_ARRAY_ELTS   (1 << IOINFO_L2E)
532
533 /*
534  * Access macros for getting at an ioinfo struct and its fields from a
535  * file handle
536  */
537 #define _pioinfo(i) (__pioinfo[(i) >> IOINFO_L2E] + ((i) & (IOINFO_ARRAY_ELTS - 1)))
538 #define _osfhnd(i)  (_pioinfo(i)->osfhnd)
539 #define _osfile(i)  (_pioinfo(i)->osfile)
540 #define _pipech(i)  (_pioinfo(i)->pipech)
541
542 /* since we are not doing a dup2(), this works fine */
543 #define _set_osfhnd(fh, osfh) (void)(_osfhnd(fh) = (intptr_t)osfh)
544 #endif
545 #endif
546
547 /* IO.xs and POSIX.xs define PERLIO_NOT_STDIO to 1 */
548 #if defined(PERL_EXT_IO) || defined(PERL_EXT_POSIX)
549 #undef  PERLIO_NOT_STDIO
550 #endif
551 #define PERLIO_NOT_STDIO 0
552
553 #include "perlio.h"
554
555 /*
556  * This provides a layer of functions and macros to ensure extensions will
557  * get to use the same RTL functions as the core.
558  */
559 #include "win32iop.h"
560
561 #define EXEC_ARGV_CAST(x) ((const char *const *) x)
562
563 #if !defined(ECONNABORTED) && defined(WSAECONNABORTED)
564 #define ECONNABORTED WSAECONNABORTED
565 #endif
566 #if !defined(ECONNRESET) && defined(WSAECONNRESET)
567 #define ECONNRESET WSAECONNRESET
568 #endif
569 #if !defined(EAFNOSUPPORT) && defined(WSAEAFNOSUPPORT)
570 #define EAFNOSUPPORT WSAEAFNOSUPPORT
571 #endif
572 /* Why not needed for ECONNREFUSED? --abe */
573
574 DllExport void *win32_signal_context(void);
575 #define PERL_GET_SIG_CONTEXT win32_signal_context()
576
577 #ifdef UNDER_CE
578 #define Win_GetModuleHandle   XCEGetModuleHandleA
579 #define Win_GetProcAddress    XCEGetProcAddressA
580 #define Win_GetModuleFileName XCEGetModuleFileNameA
581 #define Win_CreateSemaphore   CreateSemaphoreW
582 #else
583 #define Win_GetModuleHandle   GetModuleHandle
584 #define Win_GetProcAddress    GetProcAddress
585 #define Win_GetModuleFileName GetModuleFileName
586 #define Win_CreateSemaphore   CreateSemaphore
587 #endif
588
589 #endif /* _INC_WIN32_PERL5 */
590