home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 December / PCpro_2006_12.ISO / ossdvd / server / Perl2 / lib / core / win32.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-18  |  15.5 KB  |  561 lines

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