on Windows, avoid potential exception (could happen if MSVCRT isn't
[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_OBJECT) || defined(PERL_IMPLICIT_SYS) || defined(PERL_CAPI)
17 #  define DYNAMIC_ENV_FETCH
18 #  define ENV_HV_NAME "___ENV_HV_NAME___"
19 #  define HAS_GETENV_LEN
20 #  define prime_env_iter()
21 #  define WIN32IO_IS_STDIO              /* don't pull in custom stdio layer */
22 #  define WIN32SCK_IS_STDSCK            /* don't pull in custom wsock layer */
23 #  ifdef PERL_GLOBAL_STRUCT
24 #    error PERL_GLOBAL_STRUCT cannot be defined with PERL_IMPLICIT_SYS
25 #  endif
26 #  define win32_get_privlib PerlEnv_lib_path
27 #  define win32_get_sitelib PerlEnv_sitelib_path
28 #  define win32_get_vendorlib PerlEnv_vendorlib_path
29 #endif
30
31 #ifdef __GNUC__
32 #  ifndef __int64               /* some versions seem to #define it already */
33 #    define __int64 long long
34 #  endif
35 #  define Win32_Winsock
36 #endif
37
38 /* Define DllExport akin to perl's EXT, 
39  * If we are in the DLL or mimicing the DLL for Win95 work round
40  * then Export the symbol, 
41  * otherwise import it.
42  */
43
44 /* now even GCC supports __declspec() */
45
46 #if defined(PERL_OBJECT)
47 #define DllExport
48 #else
49 #if defined(PERLDLL) || defined(WIN95FIX)
50 #define DllExport
51 /*#define DllExport __declspec(dllexport)*/     /* noises with VC5+sp3 */
52 #else 
53 #define DllExport __declspec(dllimport)
54 #endif
55 #endif
56
57 #define  WIN32_LEAN_AND_MEAN
58 #include <windows.h>
59
60 #ifdef   WIN32_LEAN_AND_MEAN            /* C file is NOT a Perl5 original. */
61 #define  CONTEXT        PERL_CONTEXT    /* Avoid conflict of CONTEXT defs. */
62 #endif /*WIN32_LEAN_AND_MEAN */
63
64 #ifndef TLS_OUT_OF_INDEXES
65 #define TLS_OUT_OF_INDEXES (DWORD)0xFFFFFFFF
66 #endif
67
68 #include <dirent.h>
69 #include <io.h>
70 #include <process.h>
71 #include <stdio.h>
72 #include <direct.h>
73 #include <stdlib.h>
74 #include <fcntl.h>
75 #ifndef EXT
76 #include "EXTERN.h"
77 #endif
78
79 struct tms {
80         long    tms_utime;
81         long    tms_stime;
82         long    tms_cutime;
83         long    tms_cstime;
84 };
85
86 #ifndef SYS_NMLN
87 #define SYS_NMLN        257
88 #endif
89
90 struct utsname {
91     char sysname[SYS_NMLN];
92     char nodename[SYS_NMLN];
93     char release[SYS_NMLN];
94     char version[SYS_NMLN];
95     char machine[SYS_NMLN];
96 };
97
98 #ifndef START_EXTERN_C
99 #undef EXTERN_C
100 #ifdef __cplusplus
101 #  define START_EXTERN_C extern "C" {
102 #  define END_EXTERN_C }
103 #  define EXTERN_C extern "C"
104 #else
105 #  define START_EXTERN_C 
106 #  define END_EXTERN_C 
107 #  define EXTERN_C
108 #endif
109 #endif
110
111 #define  STANDARD_C     1
112 #define  DOSISH         1               /* no escaping our roots */
113 #define  OP_BINARY      O_BINARY        /* mistake in in pp_sys.c? */
114
115 /* Define USE_SOCKETS_AS_HANDLES to enable emulation of windows sockets as
116  * real filehandles. XXX Should always be defined (the other version is untested) */
117 #define USE_SOCKETS_AS_HANDLES
118
119 /* read() and write() aren't transparent for socket handles */
120 #define PERL_SOCK_SYSREAD_IS_RECV
121 #define PERL_SOCK_SYSWRITE_IS_SEND
122
123 #define PERL_NO_FORCE_LINK              /* no need for PL_force_link_funcs */
124
125 /* if USE_WIN32_RTL_ENV is not defined, Perl uses direct Win32 calls
126  * to read the environment, bypassing the runtime's (usually broken)
127  * facilities for accessing the same.  See note in util.c/my_setenv(). */
128 /*#define USE_WIN32_RTL_ENV */
129
130 /* Define USE_FIXED_OSFHANDLE to fix MSVCRT's _open_osfhandle() on W95.
131    It now uses some black magic to work seamlessly with the DLL CRT and
132    works with MSVC++ 4.0+ or GCC/Mingw32
133         -- BKS 1-24-2000 */
134 #if (defined(_M_IX86) && _MSC_VER >= 1000) || defined(__MINGW32__)
135 #define USE_FIXED_OSFHANDLE
136 #endif
137
138 /* Define PERL_WIN32_SOCK_DLOAD to have Perl dynamically load the winsock
139    DLL when needed. Don't use if your compiler supports delayloading (ie, VC++ 6.0)
140         -- BKS 5-29-2000 */
141 #if !(defined(_M_IX86) && _MSC_VER >= 1200)
142 #define PERL_WIN32_SOCK_DLOAD
143 #endif
144 #define ENV_IS_CASELESS
145
146 #ifndef VER_PLATFORM_WIN32_WINDOWS      /* VC-2.0 headers don't have this */
147 #define VER_PLATFORM_WIN32_WINDOWS      1
148 #endif
149
150 #ifndef FILE_SHARE_DELETE               /* VC-4.0 headers don't have this */
151 #define FILE_SHARE_DELETE               0x00000004
152 #endif
153
154 /* access() mode bits */
155 #ifndef R_OK
156 #  define       R_OK    4
157 #  define       W_OK    2
158 #  define       X_OK    1
159 #  define       F_OK    0
160 #endif
161
162 /* for waitpid() */
163 #ifndef WNOHANG
164 #  define WNOHANG       1
165 #endif
166
167 #define PERL_GET_CONTEXT_DEFINED
168
169 /* Compiler-specific stuff. */
170
171 #ifdef __BORLANDC__             /* Borland C++ */
172
173 #define _access access
174 #define _chdir chdir
175 #define _getpid getpid
176 #define wcsicmp _wcsicmp
177 #include <sys/types.h>
178
179 #ifndef DllMain
180 #define DllMain DllEntryPoint
181 #endif
182
183 #pragma warn -ccc       /* "condition is always true/false" */
184 #pragma warn -rch       /* "unreachable code" */
185 #pragma warn -sig       /* "conversion may lose significant digits" */
186 #pragma warn -pia       /* "possibly incorrect assignment" */
187 #pragma warn -par       /* "parameter 'foo' is never used" */
188 #pragma warn -aus       /* "'foo' is assigned a value that is never used" */
189 #pragma warn -use       /* "'foo' is declared but never used" */
190 #pragma warn -csu       /* "comparing signed and unsigned values" */
191 #pragma warn -pro       /* "call to function with no prototype" */
192 #pragma warn -stu       /* "undefined structure 'foo'" */
193
194 /* Borland is picky about a bare member function name used as its ptr */
195 #ifdef PERL_OBJECT
196 #  define MEMBER_TO_FPTR(name)  &(name)
197 #endif
198
199 /* Borland C thinks that a pointer to a member variable is 12 bytes in size. */
200 #define PERL_MEMBER_PTR_SIZE    12
201
202 #define isnan           _isnan
203
204 #endif
205
206 #ifdef _MSC_VER                 /* Microsoft Visual C++ */
207
208 typedef long            uid_t;
209 typedef long            gid_t;
210 typedef unsigned short  mode_t;
211 #pragma  warning(disable: 4018 4035 4101 4102 4244 4245 4761)
212
213 /* Visual C thinks that a pointer to a member variable is 16 bytes in size. */
214 #define PERL_MEMBER_PTR_SIZE    16
215
216 #define isnan           _isnan
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 #ifdef PERL_OBJECT
232 #  define MEMBER_TO_FPTR(name)  &(name)
233 #endif
234
235 #ifndef _O_NOINHERIT
236 #  define _O_NOINHERIT  0x0080
237 #  ifndef _NO_OLDNAMES
238 #    define O_NOINHERIT _O_NOINHERIT
239 #  endif
240 #endif
241
242 #endif /* __MINGW32__ */
243
244 /* both GCC/Mingw32 and MSVC++ 4.0 are missing this, so we put it here */
245 #ifndef CP_UTF8
246 #  define CP_UTF8       65001
247 #endif
248
249 /* compatibility stuff for other compilers goes here */
250
251
252 #if !defined(PERL_OBJECT) && defined(PERL_CAPI) && defined(PERL_MEMBER_PTR_SIZE)
253 #  define STRUCT_MGVTBL_DEFINITION \
254 struct mgvtbl {                                                         \
255     union {                                                             \
256         int         (CPERLscope(*svt_get))(pTHX_ SV *sv, MAGIC* mg);    \
257         char        handle_VC_problem1[PERL_MEMBER_PTR_SIZE];           \
258     };                                                                  \
259     union {                                                             \
260         int         (CPERLscope(*svt_set))(pTHX_ SV *sv, MAGIC* mg);    \
261         char        handle_VC_problem2[PERL_MEMBER_PTR_SIZE];           \
262     };                                                                  \
263     union {                                                             \
264         U32         (CPERLscope(*svt_len))(pTHX_ SV *sv, MAGIC* mg);    \
265         char        handle_VC_problem3[PERL_MEMBER_PTR_SIZE];           \
266     };                                                                  \
267     union {                                                             \
268         int         (CPERLscope(*svt_clear))(pTHX_ SV *sv, MAGIC* mg);  \
269         char        handle_VC_problem4[PERL_MEMBER_PTR_SIZE];           \
270     };                                                                  \
271     union {                                                             \
272         int         (CPERLscope(*svt_free))(pTHX_ SV *sv, MAGIC* mg);   \
273         char        handle_VC_problem5[PERL_MEMBER_PTR_SIZE];           \
274     };                                                                  \
275 }
276
277 #  define BASEOP_DEFINITION \
278     OP*         op_next;                                                \
279     OP*         op_sibling;                                             \
280     OP*         (CPERLscope(*op_ppaddr))(pTHX);                         \
281     char        handle_VC_problem[PERL_MEMBER_PTR_SIZE-sizeof(OP*)];    \
282     PADOFFSET   op_targ;                                                \
283     OPCODE      op_type;                                                \
284     U16         op_seq;                                                 \
285     U8          op_flags;                                               \
286     U8          op_private;
287
288 #endif /* !PERL_OBJECT && PERL_CAPI && PERL_MEMBER_PTR_SIZE */
289
290
291 START_EXTERN_C
292
293 /* For UNIX compatibility. */
294
295 extern  uid_t   getuid(void);
296 extern  gid_t   getgid(void);
297 extern  uid_t   geteuid(void);
298 extern  gid_t   getegid(void);
299 extern  int     setuid(uid_t uid);
300 extern  int     setgid(gid_t gid);
301 extern  int     kill(int pid, int sig);
302 extern  void    *sbrk(int need);
303 extern  char *  getlogin(void);
304 extern  int     chown(const char *p, uid_t o, gid_t g);
305
306 #undef   Stat
307 #define  Stat           win32_stat
308
309 #undef   init_os_extras
310 #define  init_os_extras Perl_init_os_extras
311
312 DllExport void          Perl_win32_init(int *argcp, char ***argvp);
313 DllExport void          Perl_init_os_extras();
314 DllExport void          win32_str_os_error(void *sv, DWORD err);
315 DllExport int           RunPerl(int argc, char **argv, char **env);
316
317 typedef struct {
318     HANDLE      childStdIn;
319     HANDLE      childStdOut;
320     HANDLE      childStdErr;
321     /*
322      * the following correspond to the fields of the same name
323      * in the STARTUPINFO structure. Embedders can use these to
324      * control the spawning process' look.
325      * Example - to hide the window of the spawned process:
326      *    dwFlags = STARTF_USESHOWWINDOW;
327      *    wShowWindow = SW_HIDE;
328      */
329     DWORD       dwFlags;
330     DWORD       dwX; 
331     DWORD       dwY; 
332     DWORD       dwXSize; 
333     DWORD       dwYSize; 
334     DWORD       dwXCountChars; 
335     DWORD       dwYCountChars; 
336     DWORD       dwFillAttribute;
337     WORD        wShowWindow; 
338 } child_IO_table;
339
340 DllExport void          win32_get_child_IO(child_IO_table* ptr);
341
342 #ifndef USE_SOCKETS_AS_HANDLES
343 extern FILE *           my_fdopen(int, char *);
344 #endif
345 extern int              my_fclose(FILE *);
346 extern int              do_aspawn(void *really, void **mark, void **sp);
347 extern int              do_spawn(char *cmd);
348 extern int              do_spawn_nowait(char *cmd);
349 extern char *           win32_get_privlib(const char *pl);
350 extern char *           win32_get_sitelib(const char *pl);
351 extern char *           win32_get_vendorlib(const char *pl);
352 extern int              IsWin95(void);
353 extern int              IsWinNT(void);
354 extern void             win32_argv2utf8(int argc, char** argv);
355
356 #ifdef PERL_IMPLICIT_SYS
357 extern void             win32_delete_internal_host(void *h);
358 #endif
359
360 extern char *           staticlinkmodules[];
361
362 END_EXTERN_C
363
364 typedef  char *         caddr_t;        /* In malloc.c (core address). */
365
366 /*
367  * handle socket stuff, assuming socket is always available
368  */
369 #include <sys/socket.h>
370 #include <netdb.h>
371
372 #ifdef MYMALLOC
373 #define EMBEDMYMALLOC   /**/
374 /* #define USE_PERL_SBRK        /**/
375 /* #define PERL_SBRK_VIA_MALLOC /**/
376 #endif
377
378 #if defined(PERLDLL) && !defined(PERL_CORE)
379 #define PERL_CORE
380 #endif
381
382 #ifdef PERL_TEXTMODE_SCRIPTS
383 #  define PERL_SCRIPT_MODE              "r"
384 #else
385 #  define PERL_SCRIPT_MODE              "rb"
386 #endif
387
388 /* 
389  * Now Win32 specific per-thread data stuff 
390  */
391
392 struct thread_intern {
393     /* XXX can probably use one buffer instead of several */
394     char                Wstrerror_buffer[512];
395     struct servent      Wservent;
396     char                Wgetlogin_buffer[128];
397 #    ifdef USE_SOCKETS_AS_HANDLES
398     int                 Winit_socktype;
399 #    endif
400 #    ifdef HAVE_DES_FCRYPT
401     char                Wcrypt_buffer[30];
402 #    endif
403 #    ifdef USE_RTL_THREAD_API
404     void *              retv;   /* slot for thread return value */
405 #    endif
406 };
407
408 #ifdef USE_THREADS
409 #  ifndef USE_DECLSPEC_THREAD
410 #    define HAVE_THREAD_INTERN
411 #  endif /* !USE_DECLSPEC_THREAD */
412 #endif /* USE_THREADS */
413
414 #define HAVE_INTERP_INTERN
415 typedef struct {
416     long        num;
417     DWORD       pids[MAXIMUM_WAIT_OBJECTS];
418     HANDLE      handles[MAXIMUM_WAIT_OBJECTS];
419 } child_tab;
420
421 struct interp_intern {
422     char *      perlshell_tokens;
423     char **     perlshell_vec;
424     long        perlshell_items;
425     struct av * fdpid;
426     child_tab * children;
427 #ifdef USE_ITHREADS
428     DWORD       pseudo_id;
429     child_tab * pseudo_children;
430 #endif
431     void *      internal_host;
432 #ifndef USE_THREADS
433     struct thread_intern        thr_intern;
434 #endif
435 };
436
437
438 #define w32_perlshell_tokens    (PL_sys_intern.perlshell_tokens)
439 #define w32_perlshell_vec       (PL_sys_intern.perlshell_vec)
440 #define w32_perlshell_items     (PL_sys_intern.perlshell_items)
441 #define w32_fdpid               (PL_sys_intern.fdpid)
442 #define w32_children            (PL_sys_intern.children)
443 #define w32_num_children        (w32_children->num)
444 #define w32_child_pids          (w32_children->pids)
445 #define w32_child_handles       (w32_children->handles)
446 #define w32_pseudo_id           (PL_sys_intern.pseudo_id)
447 #define w32_pseudo_children     (PL_sys_intern.pseudo_children)
448 #define w32_num_pseudo_children         (w32_pseudo_children->num)
449 #define w32_pseudo_child_pids           (w32_pseudo_children->pids)
450 #define w32_pseudo_child_handles        (w32_pseudo_children->handles)
451 #define w32_internal_host               (PL_sys_intern.internal_host)
452 #ifdef USE_THREADS
453 #  define w32_strerror_buffer   (thr->i.Wstrerror_buffer)
454 #  define w32_getlogin_buffer   (thr->i.Wgetlogin_buffer)
455 #  define w32_crypt_buffer      (thr->i.Wcrypt_buffer)
456 #  define w32_servent           (thr->i.Wservent)
457 #  define w32_init_socktype     (thr->i.Winit_socktype)
458 #else
459 #  define w32_strerror_buffer   (PL_sys_intern.thr_intern.Wstrerror_buffer)
460 #  define w32_getlogin_buffer   (PL_sys_intern.thr_intern.Wgetlogin_buffer)
461 #  define w32_crypt_buffer      (PL_sys_intern.thr_intern.Wcrypt_buffer)
462 #  define w32_servent           (PL_sys_intern.thr_intern.Wservent)
463 #  define w32_init_socktype     (PL_sys_intern.thr_intern.Winit_socktype)
464 #endif /* USE_THREADS */
465
466 /* UNICODE<>ANSI translation helpers */
467 /* Use CP_ACP when mode is ANSI */
468 /* Use CP_UTF8 when mode is UTF8 */
469
470 #define A2WHELPER_LEN(lpa, alen, lpw, nBytes)\
471     (lpw[0] = 0, MultiByteToWideChar((IN_BYTE) ? CP_ACP : CP_UTF8, 0, \
472                                     lpa, alen, lpw, (nBytes/sizeof(WCHAR))))
473 #define A2WHELPER(lpa, lpw, nBytes)     A2WHELPER_LEN(lpa, -1, lpw, nBytes)
474
475 #define W2AHELPER_LEN(lpw, wlen, lpa, nChars)\
476     (lpa[0] = '\0', WideCharToMultiByte((IN_BYTE) ? CP_ACP : CP_UTF8, 0, \
477                                        lpw, wlen, (LPSTR)lpa, nChars,NULL,NULL))
478 #define W2AHELPER(lpw, lpa, nChars)     W2AHELPER_LEN(lpw, -1, lpa, nChars)
479
480 #define USING_WIDE() (PL_widesyscalls && PerlEnv_os_id() == VER_PLATFORM_WIN32_NT)
481
482 #ifdef USE_ITHREADS
483 #  define PERL_WAIT_FOR_CHILDREN \
484     STMT_START {                                                        \
485         if (w32_pseudo_children && w32_num_pseudo_children) {           \
486             long children = w32_num_pseudo_children;                    \
487             WaitForMultipleObjects(children,                            \
488                                    w32_pseudo_child_handles,            \
489                                    TRUE, INFINITE);                     \
490             while (children)                                            \
491                 CloseHandle(w32_pseudo_child_handles[--children]);      \
492         }                                                               \
493     } STMT_END
494 #endif
495
496 #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
497 #ifdef PERL_CORE
498
499 /* C doesn't like repeat struct definitions */
500 #ifndef _CRTIMP
501 #define _CRTIMP __declspec(dllimport)
502 #endif
503
504 /*
505  * Control structure for lowio file handles
506  */
507 typedef struct {
508     long osfhnd;    /* underlying OS file HANDLE */
509     char osfile;    /* attributes of file (e.g., open in text mode?) */
510     char pipech;    /* one char buffer for handles opened on pipes */
511     int lockinitflag;
512     CRITICAL_SECTION lock;
513 } ioinfo;
514
515
516 /*
517  * Array of arrays of control structures for lowio files.
518  */
519 EXTERN_C _CRTIMP ioinfo* __pioinfo[];
520
521 /*
522  * Definition of IOINFO_L2E, the log base 2 of the number of elements in each
523  * array of ioinfo structs.
524  */
525 #define IOINFO_L2E          5
526
527 /*
528  * Definition of IOINFO_ARRAY_ELTS, the number of elements in ioinfo array
529  */
530 #define IOINFO_ARRAY_ELTS   (1 << IOINFO_L2E)
531
532 /*
533  * Access macros for getting at an ioinfo struct and its fields from a
534  * file handle
535  */
536 #define _pioinfo(i) (__pioinfo[(i) >> IOINFO_L2E] + ((i) & (IOINFO_ARRAY_ELTS - 1)))
537 #define _osfhnd(i)  (_pioinfo(i)->osfhnd)
538 #define _osfile(i)  (_pioinfo(i)->osfile)
539 #define _pipech(i)  (_pioinfo(i)->pipech)
540
541 /* since we are not doing a dup2(), this works fine */
542 #define _set_osfhnd(fh, osfh) (void)(_osfhnd(fh) = (long)osfh)
543 #endif
544 #endif
545
546 /*
547  * This provides a layer of functions and macros to ensure extensions will
548  * get to use the same RTL functions as the core.
549  */
550 #include "win32iop.h"
551
552 #endif /* _INC_WIN32_PERL5 */
553