This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Devel::DProf Real Elapsed Times
[perl5.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
GS
11#include "win32iop.h"
12#include <fcntl.h>
7766f137 13#endif /* PERL_IMPLICIT_SYS */
0cb96387 14
0cb96387 15
7766f137
GS
16/* Register any extra external extensions */
17char *staticlinkmodules[] = {
18 "DynaLoader",
19 NULL,
0cb96387
GS
20};
21
acfe0abc 22EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
0cb96387 23
7766f137 24static void
acfe0abc 25xs_init(pTHX)
0cb96387 26{
7766f137
GS
27 char *file = __FILE__;
28 dXSUB_SYS;
29 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
0cb96387
GS
30}
31
7766f137 32#ifdef PERL_IMPLICIT_SYS
0cb96387 33
7766f137 34#include "perlhost.h"
0cb96387 35
222c300a
NIS
36void
37win32_checkTLS(PerlInterpreter *host_perl)
38{
39 dTHX;
40 if (host_perl != my_perl) {
41 int *nowhere = NULL;
42 *nowhere = 0;
43 abort();
44 }
45}
46
32e30700
GS
47EXTERN_C void
48perl_get_host_info(struct IPerlMemInfo* perlMemInfo,
7766f137
GS
49 struct IPerlMemInfo* perlMemSharedInfo,
50 struct IPerlMemInfo* perlMemParseInfo,
32e30700
GS
51 struct IPerlEnvInfo* perlEnvInfo,
52 struct IPerlStdIOInfo* perlStdIOInfo,
53 struct IPerlLIOInfo* perlLIOInfo,
54 struct IPerlDirInfo* perlDirInfo,
55 struct IPerlSockInfo* perlSockInfo,
56 struct IPerlProcInfo* perlProcInfo)
0cb96387 57{
7766f137 58 if (perlMemInfo) {
0cb96387
GS
59 Copy(&perlMem, &perlMemInfo->perlMemList, perlMemInfo->nCount, void*);
60 perlMemInfo->nCount = (sizeof(struct IPerlMem)/sizeof(void*));
61 }
7766f137
GS
62 if (perlMemSharedInfo) {
63 Copy(&perlMem, &perlMemSharedInfo->perlMemList, perlMemSharedInfo->nCount, void*);
64 perlMemSharedInfo->nCount = (sizeof(struct IPerlMem)/sizeof(void*));
65 }
66 if (perlMemParseInfo) {
67 Copy(&perlMem, &perlMemParseInfo->perlMemList, perlMemParseInfo->nCount, void*);
68 perlMemParseInfo->nCount = (sizeof(struct IPerlMem)/sizeof(void*));
69 }
70 if (perlEnvInfo) {
0cb96387
GS
71 Copy(&perlEnv, &perlEnvInfo->perlEnvList, perlEnvInfo->nCount, void*);
72 perlEnvInfo->nCount = (sizeof(struct IPerlEnv)/sizeof(void*));
73 }
7766f137 74 if (perlStdIOInfo) {
0cb96387
GS
75 Copy(&perlStdIO, &perlStdIOInfo->perlStdIOList, perlStdIOInfo->nCount, void*);
76 perlStdIOInfo->nCount = (sizeof(struct IPerlStdIO)/sizeof(void*));
77 }
7766f137 78 if (perlLIOInfo) {
0cb96387
GS
79 Copy(&perlLIO, &perlLIOInfo->perlLIOList, perlLIOInfo->nCount, void*);
80 perlLIOInfo->nCount = (sizeof(struct IPerlLIO)/sizeof(void*));
81 }
7766f137 82 if (perlDirInfo) {
0cb96387
GS
83 Copy(&perlDir, &perlDirInfo->perlDirList, perlDirInfo->nCount, void*);
84 perlDirInfo->nCount = (sizeof(struct IPerlDir)/sizeof(void*));
85 }
7766f137 86 if (perlSockInfo) {
0cb96387
GS
87 Copy(&perlSock, &perlSockInfo->perlSockList, perlSockInfo->nCount, void*);
88 perlSockInfo->nCount = (sizeof(struct IPerlSock)/sizeof(void*));
89 }
7766f137 90 if (perlProcInfo) {
0cb96387
GS
91 Copy(&perlProc, &perlProcInfo->perlProcList, perlProcInfo->nCount, void*);
92 perlProcInfo->nCount = (sizeof(struct IPerlProc)/sizeof(void*));
93 }
94}
95
7766f137
GS
96EXTERN_C PerlInterpreter*
97perl_alloc_override(struct IPerlMem** ppMem, struct IPerlMem** ppMemShared,
98 struct IPerlMem** ppMemParse, struct IPerlEnv** ppEnv,
99 struct IPerlStdIO** ppStdIO, struct IPerlLIO** ppLIO,
100 struct IPerlDir** ppDir, struct IPerlSock** ppSock,
101 struct IPerlProc** ppProc)
0cb96387 102{
7766f137 103 PerlInterpreter *my_perl = NULL;
8a85dc4e
GS
104 CPerlHost* pHost = new CPerlHost(ppMem, ppMemShared, ppMemParse, ppEnv,
105 ppStdIO, ppLIO, ppDir, ppSock, ppProc);
7766f137 106
8a85dc4e
GS
107 if (pHost) {
108 my_perl = perl_alloc_using(pHost->m_pHostperlMem,
109 pHost->m_pHostperlMemShared,
110 pHost->m_pHostperlMemParse,
111 pHost->m_pHostperlEnv,
112 pHost->m_pHostperlStdIO,
113 pHost->m_pHostperlLIO,
114 pHost->m_pHostperlDir,
115 pHost->m_pHostperlSock,
116 pHost->m_pHostperlProc);
117 if (my_perl) {
8a85dc4e 118 w32_internal_host = pHost;
222c300a 119 pHost->host_perl = my_perl;
7766f137 120 }
0cb96387 121 }
7766f137 122 return my_perl;
0cb96387
GS
123}
124
7766f137
GS
125EXTERN_C PerlInterpreter*
126perl_alloc(void)
0cb96387 127{
7766f137 128 PerlInterpreter* my_perl = NULL;
8a85dc4e
GS
129 CPerlHost* pHost = new CPerlHost();
130 if (pHost) {
131 my_perl = perl_alloc_using(pHost->m_pHostperlMem,
132 pHost->m_pHostperlMemShared,
133 pHost->m_pHostperlMemParse,
134 pHost->m_pHostperlEnv,
135 pHost->m_pHostperlStdIO,
136 pHost->m_pHostperlLIO,
137 pHost->m_pHostperlDir,
138 pHost->m_pHostperlSock,
139 pHost->m_pHostperlProc);
140 if (my_perl) {
8a85dc4e 141 w32_internal_host = pHost;
222c300a 142 pHost->host_perl = my_perl;
7766f137 143 }
0cb96387 144 }
7766f137 145 return my_perl;
0cb96387
GS
146}
147
1c0ca838
GS
148EXTERN_C void
149win32_delete_internal_host(void *h)
150{
151 CPerlHost *host = (CPerlHost*)h;
152 delete host;
153}
154
32e30700
GS
155#endif /* PERL_IMPLICIT_SYS */
156
7766f137
GS
157EXTERN_C HANDLE w32_perldll_handle;
158
c5be433b 159EXTERN_C DllExport int
0cb96387 160RunPerl(int argc, char **argv, char **env)
0a753a76 161{
68dc0745 162 int exitstatus;
ed094faf 163 PerlInterpreter *my_perl, *new_perl = NULL;
0a753a76 164
0cb96387
GS
165#ifndef __BORLANDC__
166 /* XXX this _may_ be a problem on some compilers (e.g. Borland) that
167 * want to free() argv after main() returns. As luck would have it,
168 * Borland's CRT does the right thing to argv[0] already. */
169 char szModuleName[MAX_PATH];
170 char *ptr;
171
172 GetModuleFileName(NULL, szModuleName, sizeof(szModuleName));
173 (void)win32_longpath(szModuleName);
174 argv[0] = szModuleName;
175#endif
176
22239a37
NIS
177#ifdef PERL_GLOBAL_STRUCT
178#define PERLVAR(var,type) /**/
51371543 179#define PERLVARA(var,type) /**/
533c011a
NIS
180#define PERLVARI(var,type,init) PL_Vars.var = init;
181#define PERLVARIC(var,type,init) PL_Vars.var = init;
22239a37
NIS
182#include "perlvars.h"
183#undef PERLVAR
51371543 184#undef PERLVARA
22239a37 185#undef PERLVARI
3fe35a81 186#undef PERLVARIC
22239a37
NIS
187#endif
188
0a753a76 189 PERL_SYS_INIT(&argc,&argv);
190
68dc0745 191 if (!(my_perl = perl_alloc()))
192 return (1);
642f9deb 193 perl_construct(my_perl);
b28d0864 194 PL_perl_destruct_level = 0;
0a753a76 195
4f63d024 196 exitstatus = perl_parse(my_perl, xs_init, argc, argv, env);
0a753a76 197 if (!exitstatus) {
7766f137 198#if defined(TOP_CLONE) && defined(USE_ITHREADS) /* XXXXXX testing */
7766f137 199 new_perl = perl_clone(my_perl, 1);
642f9deb 200 exitstatus = perl_run(new_perl);
ba869deb 201 PERL_SET_THX(my_perl);
d18c6117 202#else
642f9deb 203 exitstatus = perl_run(my_perl);
d18c6117 204#endif
0a753a76 205 }
206
642f9deb
GS
207 perl_destruct(my_perl);
208 perl_free(my_perl);
ed094faf
GS
209#ifdef USE_ITHREADS
210 if (new_perl) {
ba869deb 211 PERL_SET_THX(new_perl);
ed094faf
GS
212 perl_destruct(new_perl);
213 perl_free(new_perl);
214 }
215#endif
0a753a76 216
217 PERL_SYS_TERM();
218
68dc0745 219 return (exitstatus);
0a753a76 220}
221
2fa86c13
GS
222EXTERN_C void
223set_w32_module_name(void);
224
b73db59c
GS
225EXTERN_C void
226EndSockets(void);
227
228
f8fb7c90
GS
229#ifdef __MINGW32__
230EXTERN_C /* GCC in C++ mode mangles the name, otherwise */
231#endif
68dc0745 232BOOL APIENTRY
233DllMain(HANDLE hModule, /* DLL module handle */
234 DWORD fdwReason, /* reason called */
235 LPVOID lpvReserved) /* reserved */
0a753a76 236{
68dc0745 237 switch (fdwReason) {
238 /* The DLL is attaching to a process due to process
239 * initialization or a call to LoadLibrary.
240 */
241 case DLL_PROCESS_ATTACH:
242/* #define DEFAULT_BINMODE */
0a753a76 243#ifdef DEFAULT_BINMODE
3e3baf6d
TB
244 setmode( fileno( stdin ), O_BINARY );
245 setmode( fileno( stdout ), O_BINARY );
246 setmode( fileno( stderr ), O_BINARY );
247 _fmode = O_BINARY;
0a753a76 248#endif
5db10396 249 DisableThreadLibraryCalls((HMODULE)hModule);
2d7a9237 250 w32_perldll_handle = hModule;
2fa86c13 251 set_w32_module_name();
68dc0745 252 break;
0a753a76 253
68dc0745 254 /* The DLL is detaching from a process due to
255 * process termination or call to FreeLibrary.
256 */
257 case DLL_PROCESS_DETACH:
ce3e5b80
NIS
258 /* As long as we use TerminateProcess()/TerminateThread() etc. for mimicing kill()
259 anything here had better be harmless if:
260 A. Not called at all.
261 B. Called after memory allocation for Heap has been forcibly removed by OS.
262 PerlIO_cleanup() was done here but fails (B).
263 */
b73db59c 264 EndSockets();
4d1ff10f 265#if defined(USE_5005THREADS) || defined(USE_ITHREADS)
e1b5da64
GS
266 if (PL_curinterp)
267 FREE_THREAD_KEY;
268#endif
68dc0745 269 break;
0a753a76 270
68dc0745 271 /* The attached process creates a new thread. */
272 case DLL_THREAD_ATTACH:
273 break;
0a753a76 274
68dc0745 275 /* The thread of the attached process terminates. */
276 case DLL_THREAD_DETACH:
277 break;
0a753a76 278
68dc0745 279 default:
280 break;
281 }
282 return TRUE;
0a753a76 283}
c43294b8 284
9613994f 285#if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
c43294b8
AB
286EXTERN_C PerlInterpreter *
287perl_clone_host(PerlInterpreter* proto_perl, UV flags) {
acfe0abc 288 dTHX;
c43294b8
AB
289 CPerlHost *h;
290 h = new CPerlHost(*(CPerlHost*)PL_sys_intern.internal_host);
291 proto_perl = perl_clone_using(proto_perl, flags,
292 h->m_pHostperlMem,
293 h->m_pHostperlMemShared,
294 h->m_pHostperlMemParse,
295 h->m_pHostperlEnv,
296 h->m_pHostperlStdIO,
297 h->m_pHostperlLIO,
298 h->m_pHostperlDir,
299 h->m_pHostperlSock,
300 h->m_pHostperlProc
301 );
302 proto_perl->Isys_intern.internal_host = h;
222c300a 303 h->host_perl = proto_perl;
c43294b8
AB
304 return proto_perl;
305
306}
307#endif