Revert #29307. It doesn't fix the XXX after all. Using "$(shell @type )"
[p5sagit/p5-mst-13.2.git] / win32 / perllib.c
CommitLineData
0a753a76 1/*
2 * "The Road goes ever on and on, down from the door where it began."
3 */
adb71456 4#define PERLIO_NOT_STDIO 0
0a753a76 5#include "EXTERN.h"
6#include "perl.h"
0cb96387 7
96e4d5b1 8#include "XSUB.h"
0a753a76 9
32e30700 10#ifdef PERL_IMPLICIT_SYS
0cb96387 11#include "win32iop.h"
12#include <fcntl.h>
7766f137 13#endif /* PERL_IMPLICIT_SYS */
0cb96387 14
0cb96387 15
7766f137 16/* Register any extra external extensions */
17char *staticlinkmodules[] = {
18 "DynaLoader",
d2b25974 19 /* other similar records will be included from "perllibst.h" */
d2b25974 20#define STATIC1
21#include "perllibst.h"
7766f137 22 NULL,
0cb96387 23};
24
acfe0abc 25EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
d2b25974 26/* other similar records will be included from "perllibst.h" */
d2b25974 27#define STATIC2
28#include "perllibst.h"
0cb96387 29
7766f137 30static void
acfe0abc 31xs_init(pTHX)
0cb96387 32{
7766f137 33 char *file = __FILE__;
34 dXSUB_SYS;
35 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
d2b25974 36 /* other similar records will be included from "perllibst.h" */
d2b25974 37#define STATIC3
38#include "perllibst.h"
0cb96387 39}
40
7766f137 41#ifdef PERL_IMPLICIT_SYS
0cb96387 42
7bd379e8 43/* WINCE: include replaced by:
44extern "C" void win32_checkTLS(PerlInterpreter *host_perl);
45*/
7766f137 46#include "perlhost.h"
0cb96387 47
222c300a 48void
49win32_checkTLS(PerlInterpreter *host_perl)
50{
51 dTHX;
52 if (host_perl != my_perl) {
53 int *nowhere = NULL;
45496817 54#ifdef UNDER_CE
7bd379e8 55 printf(" ... bad in win32_checkTLS\n");
56 printf(" %08X ne %08X\n",host_perl,my_perl);
57#endif
222c300a 58 abort();
59 }
60}
61
7bd379e8 62#ifdef UNDER_CE
63int GetLogicalDrives() {
64 return 0; /* no logical drives on CE */
65}
66int GetLogicalDriveStrings(int size, char addr[]) {
67 return 0; /* no logical drives on CE */
68}
69/* TBD */
70DWORD GetFullPathNameA(LPCSTR fn, DWORD blen, LPTSTR buf, LPSTR *pfile) {
71 return 0;
72}
73/* TBD */
74DWORD GetFullPathNameW(CONST WCHAR *fn, DWORD blen, WCHAR * buf, WCHAR **pfile) {
75 return 0;
76}
77/* TBD */
78DWORD SetCurrentDirectoryA(LPSTR pPath) {
79 return 0;
80}
81/* TBD */
82DWORD SetCurrentDirectoryW(CONST WCHAR *pPath) {
83 return 0;
84}
85int xcesetuid(uid_t id){return 0;}
86int xceseteuid(uid_t id){ return 0;}
87int xcegetuid() {return 0;}
88int xcegeteuid(){ return 0;}
89#endif
90
91/* WINCE??: include "perlhost.h" */
92
32e30700 93EXTERN_C void
94perl_get_host_info(struct IPerlMemInfo* perlMemInfo,
7766f137 95 struct IPerlMemInfo* perlMemSharedInfo,
96 struct IPerlMemInfo* perlMemParseInfo,
32e30700 97 struct IPerlEnvInfo* perlEnvInfo,
98 struct IPerlStdIOInfo* perlStdIOInfo,
99 struct IPerlLIOInfo* perlLIOInfo,
100 struct IPerlDirInfo* perlDirInfo,
101 struct IPerlSockInfo* perlSockInfo,
102 struct IPerlProcInfo* perlProcInfo)
0cb96387 103{
7766f137 104 if (perlMemInfo) {
0cb96387 105 Copy(&perlMem, &perlMemInfo->perlMemList, perlMemInfo->nCount, void*);
106 perlMemInfo->nCount = (sizeof(struct IPerlMem)/sizeof(void*));
107 }
7766f137 108 if (perlMemSharedInfo) {
109 Copy(&perlMem, &perlMemSharedInfo->perlMemList, perlMemSharedInfo->nCount, void*);
110 perlMemSharedInfo->nCount = (sizeof(struct IPerlMem)/sizeof(void*));
111 }
112 if (perlMemParseInfo) {
113 Copy(&perlMem, &perlMemParseInfo->perlMemList, perlMemParseInfo->nCount, void*);
114 perlMemParseInfo->nCount = (sizeof(struct IPerlMem)/sizeof(void*));
115 }
116 if (perlEnvInfo) {
0cb96387 117 Copy(&perlEnv, &perlEnvInfo->perlEnvList, perlEnvInfo->nCount, void*);
118 perlEnvInfo->nCount = (sizeof(struct IPerlEnv)/sizeof(void*));
119 }
7766f137 120 if (perlStdIOInfo) {
0cb96387 121 Copy(&perlStdIO, &perlStdIOInfo->perlStdIOList, perlStdIOInfo->nCount, void*);
122 perlStdIOInfo->nCount = (sizeof(struct IPerlStdIO)/sizeof(void*));
123 }
7766f137 124 if (perlLIOInfo) {
0cb96387 125 Copy(&perlLIO, &perlLIOInfo->perlLIOList, perlLIOInfo->nCount, void*);
126 perlLIOInfo->nCount = (sizeof(struct IPerlLIO)/sizeof(void*));
127 }
7766f137 128 if (perlDirInfo) {
0cb96387 129 Copy(&perlDir, &perlDirInfo->perlDirList, perlDirInfo->nCount, void*);
130 perlDirInfo->nCount = (sizeof(struct IPerlDir)/sizeof(void*));
131 }
7766f137 132 if (perlSockInfo) {
0cb96387 133 Copy(&perlSock, &perlSockInfo->perlSockList, perlSockInfo->nCount, void*);
134 perlSockInfo->nCount = (sizeof(struct IPerlSock)/sizeof(void*));
135 }
7766f137 136 if (perlProcInfo) {
0cb96387 137 Copy(&perlProc, &perlProcInfo->perlProcList, perlProcInfo->nCount, void*);
138 perlProcInfo->nCount = (sizeof(struct IPerlProc)/sizeof(void*));
139 }
140}
141
7766f137 142EXTERN_C PerlInterpreter*
143perl_alloc_override(struct IPerlMem** ppMem, struct IPerlMem** ppMemShared,
144 struct IPerlMem** ppMemParse, struct IPerlEnv** ppEnv,
145 struct IPerlStdIO** ppStdIO, struct IPerlLIO** ppLIO,
146 struct IPerlDir** ppDir, struct IPerlSock** ppSock,
147 struct IPerlProc** ppProc)
0cb96387 148{
7766f137 149 PerlInterpreter *my_perl = NULL;
8a85dc4e 150 CPerlHost* pHost = new CPerlHost(ppMem, ppMemShared, ppMemParse, ppEnv,
151 ppStdIO, ppLIO, ppDir, ppSock, ppProc);
7766f137 152
8a85dc4e 153 if (pHost) {
154 my_perl = perl_alloc_using(pHost->m_pHostperlMem,
155 pHost->m_pHostperlMemShared,
156 pHost->m_pHostperlMemParse,
157 pHost->m_pHostperlEnv,
158 pHost->m_pHostperlStdIO,
159 pHost->m_pHostperlLIO,
160 pHost->m_pHostperlDir,
161 pHost->m_pHostperlSock,
162 pHost->m_pHostperlProc);
163 if (my_perl) {
8a85dc4e 164 w32_internal_host = pHost;
222c300a 165 pHost->host_perl = my_perl;
7766f137 166 }
0cb96387 167 }
7766f137 168 return my_perl;
0cb96387 169}
170
7766f137 171EXTERN_C PerlInterpreter*
172perl_alloc(void)
0cb96387 173{
7766f137 174 PerlInterpreter* my_perl = NULL;
8a85dc4e 175 CPerlHost* pHost = new CPerlHost();
176 if (pHost) {
177 my_perl = perl_alloc_using(pHost->m_pHostperlMem,
178 pHost->m_pHostperlMemShared,
179 pHost->m_pHostperlMemParse,
180 pHost->m_pHostperlEnv,
181 pHost->m_pHostperlStdIO,
182 pHost->m_pHostperlLIO,
183 pHost->m_pHostperlDir,
184 pHost->m_pHostperlSock,
185 pHost->m_pHostperlProc);
186 if (my_perl) {
8a85dc4e 187 w32_internal_host = pHost;
222c300a 188 pHost->host_perl = my_perl;
7766f137 189 }
0cb96387 190 }
7766f137 191 return my_perl;
0cb96387 192}
193
1c0ca838 194EXTERN_C void
195win32_delete_internal_host(void *h)
196{
197 CPerlHost *host = (CPerlHost*)h;
198 delete host;
199}
200
32e30700 201#endif /* PERL_IMPLICIT_SYS */
202
7766f137 203EXTERN_C HANDLE w32_perldll_handle;
204
c5be433b 205EXTERN_C DllExport int
0cb96387 206RunPerl(int argc, char **argv, char **env)
0a753a76 207{
68dc0745 208 int exitstatus;
ed094faf 209 PerlInterpreter *my_perl, *new_perl = NULL;
0a753a76 210
0cb96387 211#ifndef __BORLANDC__
212 /* XXX this _may_ be a problem on some compilers (e.g. Borland) that
213 * want to free() argv after main() returns. As luck would have it,
214 * Borland's CRT does the right thing to argv[0] already. */
215 char szModuleName[MAX_PATH];
0cb96387 216
7bd379e8 217 Win_GetModuleFileName(NULL, szModuleName, sizeof(szModuleName));
0cb96387 218 (void)win32_longpath(szModuleName);
219 argv[0] = szModuleName;
220#endif
221
22239a37 222#ifdef PERL_GLOBAL_STRUCT
223#define PERLVAR(var,type) /**/
51371543 224#define PERLVARA(var,type) /**/
533c011a 225#define PERLVARI(var,type,init) PL_Vars.var = init;
226#define PERLVARIC(var,type,init) PL_Vars.var = init;
22239a37 227#include "perlvars.h"
228#undef PERLVAR
51371543 229#undef PERLVARA
22239a37 230#undef PERLVARI
3fe35a81 231#undef PERLVARIC
22239a37 232#endif
233
0a753a76 234 PERL_SYS_INIT(&argc,&argv);
235
68dc0745 236 if (!(my_perl = perl_alloc()))
237 return (1);
642f9deb 238 perl_construct(my_perl);
b28d0864 239 PL_perl_destruct_level = 0;
0a753a76 240
4f63d024 241 exitstatus = perl_parse(my_perl, xs_init, argc, argv, env);
0a753a76 242 if (!exitstatus) {
7766f137 243#if defined(TOP_CLONE) && defined(USE_ITHREADS) /* XXXXXX testing */
7766f137 244 new_perl = perl_clone(my_perl, 1);
642f9deb 245 exitstatus = perl_run(new_perl);
ba869deb 246 PERL_SET_THX(my_perl);
d18c6117 247#else
642f9deb 248 exitstatus = perl_run(my_perl);
d18c6117 249#endif
0a753a76 250 }
251
642f9deb 252 perl_destruct(my_perl);
253 perl_free(my_perl);
ed094faf 254#ifdef USE_ITHREADS
255 if (new_perl) {
ba869deb 256 PERL_SET_THX(new_perl);
ed094faf 257 perl_destruct(new_perl);
258 perl_free(new_perl);
259 }
260#endif
0a753a76 261
262 PERL_SYS_TERM();
263
68dc0745 264 return (exitstatus);
0a753a76 265}
266
2fa86c13 267EXTERN_C void
268set_w32_module_name(void);
269
b73db59c 270EXTERN_C void
271EndSockets(void);
272
273
f8fb7c90 274#ifdef __MINGW32__
275EXTERN_C /* GCC in C++ mode mangles the name, otherwise */
276#endif
68dc0745 277BOOL APIENTRY
278DllMain(HANDLE hModule, /* DLL module handle */
279 DWORD fdwReason, /* reason called */
280 LPVOID lpvReserved) /* reserved */
0a753a76 281{
68dc0745 282 switch (fdwReason) {
283 /* The DLL is attaching to a process due to process
284 * initialization or a call to LoadLibrary.
285 */
286 case DLL_PROCESS_ATTACH:
287/* #define DEFAULT_BINMODE */
0a753a76 288#ifdef DEFAULT_BINMODE
3e3baf6d 289 setmode( fileno( stdin ), O_BINARY );
290 setmode( fileno( stdout ), O_BINARY );
291 setmode( fileno( stderr ), O_BINARY );
292 _fmode = O_BINARY;
0a753a76 293#endif
7bd379e8 294
295#ifndef UNDER_CE
5db10396 296 DisableThreadLibraryCalls((HMODULE)hModule);
7bd379e8 297#endif
298
2d7a9237 299 w32_perldll_handle = hModule;
2fa86c13 300 set_w32_module_name();
68dc0745 301 break;
0a753a76 302
68dc0745 303 /* The DLL is detaching from a process due to
304 * process termination or call to FreeLibrary.
305 */
306 case DLL_PROCESS_DETACH:
ce3e5b80 307 /* As long as we use TerminateProcess()/TerminateThread() etc. for mimicing kill()
308 anything here had better be harmless if:
309 A. Not called at all.
310 B. Called after memory allocation for Heap has been forcibly removed by OS.
311 PerlIO_cleanup() was done here but fails (B).
312 */
b73db59c 313 EndSockets();
3db8f154 314#if defined(USE_ITHREADS)
e1b5da64 315 if (PL_curinterp)
316 FREE_THREAD_KEY;
317#endif
68dc0745 318 break;
0a753a76 319
68dc0745 320 /* The attached process creates a new thread. */
321 case DLL_THREAD_ATTACH:
322 break;
0a753a76 323
68dc0745 324 /* The thread of the attached process terminates. */
325 case DLL_THREAD_DETACH:
326 break;
0a753a76 327
68dc0745 328 default:
329 break;
330 }
331 return TRUE;
0a753a76 332}
c43294b8 333
7bd379e8 334
9613994f 335#if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
c43294b8 336EXTERN_C PerlInterpreter *
337perl_clone_host(PerlInterpreter* proto_perl, UV flags) {
acfe0abc 338 dTHX;
c43294b8 339 CPerlHost *h;
340 h = new CPerlHost(*(CPerlHost*)PL_sys_intern.internal_host);
341 proto_perl = perl_clone_using(proto_perl, flags,
342 h->m_pHostperlMem,
343 h->m_pHostperlMemShared,
344 h->m_pHostperlMemParse,
345 h->m_pHostperlEnv,
346 h->m_pHostperlStdIO,
347 h->m_pHostperlLIO,
348 h->m_pHostperlDir,
349 h->m_pHostperlSock,
350 h->m_pHostperlProc
351 );
352 proto_perl->Isys_intern.internal_host = h;
222c300a 353 h->host_perl = proto_perl;
c43294b8 354 return proto_perl;
355
356}
357#endif