Minor pod nit in perlre.
[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 }
432ce874 251
642f9deb 252 perl_destruct(my_perl);
ed094faf 253#ifdef USE_ITHREADS
254 if (new_perl) {
ba869deb 255 PERL_SET_THX(new_perl);
ed094faf 256 perl_destruct(new_perl);
432ce874 257 PERL_SET_THX(my_perl);
258 }
259#endif
260
261 PERL_SYS_TERM();
262
263 perl_free(my_perl);
264
265#ifdef USE_ITHREADS
266 if (new_perl) {
267 PERL_SET_THX(new_perl);
ed094faf 268 perl_free(new_perl);
269 }
270#endif
0a753a76 271
432ce874 272
0a753a76 273
68dc0745 274 return (exitstatus);
0a753a76 275}
276
2fa86c13 277EXTERN_C void
278set_w32_module_name(void);
279
b73db59c 280EXTERN_C void
281EndSockets(void);
282
283
f8fb7c90 284#ifdef __MINGW32__
285EXTERN_C /* GCC in C++ mode mangles the name, otherwise */
286#endif
68dc0745 287BOOL APIENTRY
288DllMain(HANDLE hModule, /* DLL module handle */
289 DWORD fdwReason, /* reason called */
290 LPVOID lpvReserved) /* reserved */
0a753a76 291{
68dc0745 292 switch (fdwReason) {
293 /* The DLL is attaching to a process due to process
294 * initialization or a call to LoadLibrary.
295 */
296 case DLL_PROCESS_ATTACH:
297/* #define DEFAULT_BINMODE */
0a753a76 298#ifdef DEFAULT_BINMODE
3e3baf6d 299 setmode( fileno( stdin ), O_BINARY );
300 setmode( fileno( stdout ), O_BINARY );
301 setmode( fileno( stderr ), O_BINARY );
302 _fmode = O_BINARY;
0a753a76 303#endif
7bd379e8 304
305#ifndef UNDER_CE
5db10396 306 DisableThreadLibraryCalls((HMODULE)hModule);
7bd379e8 307#endif
308
2d7a9237 309 w32_perldll_handle = hModule;
2fa86c13 310 set_w32_module_name();
68dc0745 311 break;
0a753a76 312
68dc0745 313 /* The DLL is detaching from a process due to
314 * process termination or call to FreeLibrary.
315 */
316 case DLL_PROCESS_DETACH:
ce3e5b80 317 /* As long as we use TerminateProcess()/TerminateThread() etc. for mimicing kill()
318 anything here had better be harmless if:
319 A. Not called at all.
320 B. Called after memory allocation for Heap has been forcibly removed by OS.
321 PerlIO_cleanup() was done here but fails (B).
322 */
b73db59c 323 EndSockets();
3db8f154 324#if defined(USE_ITHREADS)
e1b5da64 325 if (PL_curinterp)
326 FREE_THREAD_KEY;
327#endif
68dc0745 328 break;
0a753a76 329
68dc0745 330 /* The attached process creates a new thread. */
331 case DLL_THREAD_ATTACH:
332 break;
0a753a76 333
68dc0745 334 /* The thread of the attached process terminates. */
335 case DLL_THREAD_DETACH:
336 break;
0a753a76 337
68dc0745 338 default:
339 break;
340 }
341 return TRUE;
0a753a76 342}
c43294b8 343
7bd379e8 344
9613994f 345#if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
c43294b8 346EXTERN_C PerlInterpreter *
347perl_clone_host(PerlInterpreter* proto_perl, UV flags) {
acfe0abc 348 dTHX;
c43294b8 349 CPerlHost *h;
350 h = new CPerlHost(*(CPerlHost*)PL_sys_intern.internal_host);
351 proto_perl = perl_clone_using(proto_perl, flags,
352 h->m_pHostperlMem,
353 h->m_pHostperlMemShared,
354 h->m_pHostperlMemParse,
355 h->m_pHostperlEnv,
356 h->m_pHostperlStdIO,
357 h->m_pHostperlLIO,
358 h->m_pHostperlDir,
359 h->m_pHostperlSock,
360 h->m_pHostperlProc
361 );
362 proto_perl->Isys_intern.internal_host = h;
222c300a 363 h->host_perl = proto_perl;
c43294b8 364 return proto_perl;
365
366}
367#endif