This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Enough changes to Configure and metaunits warrant an update.
[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];
0cb96387
GS
170
171 GetModuleFileName(NULL, szModuleName, sizeof(szModuleName));
172 (void)win32_longpath(szModuleName);
173 argv[0] = szModuleName;
174#endif
175
22239a37
NIS
176#ifdef PERL_GLOBAL_STRUCT
177#define PERLVAR(var,type) /**/
51371543 178#define PERLVARA(var,type) /**/
533c011a
NIS
179#define PERLVARI(var,type,init) PL_Vars.var = init;
180#define PERLVARIC(var,type,init) PL_Vars.var = init;
22239a37
NIS
181#include "perlvars.h"
182#undef PERLVAR
51371543 183#undef PERLVARA
22239a37 184#undef PERLVARI
3fe35a81 185#undef PERLVARIC
22239a37
NIS
186#endif
187
0a753a76 188 PERL_SYS_INIT(&argc,&argv);
189
68dc0745 190 if (!(my_perl = perl_alloc()))
191 return (1);
642f9deb 192 perl_construct(my_perl);
b28d0864 193 PL_perl_destruct_level = 0;
0a753a76 194
4f63d024 195 exitstatus = perl_parse(my_perl, xs_init, argc, argv, env);
0a753a76 196 if (!exitstatus) {
7766f137 197#if defined(TOP_CLONE) && defined(USE_ITHREADS) /* XXXXXX testing */
7766f137 198 new_perl = perl_clone(my_perl, 1);
642f9deb 199 exitstatus = perl_run(new_perl);
ba869deb 200 PERL_SET_THX(my_perl);
d18c6117 201#else
642f9deb 202 exitstatus = perl_run(my_perl);
d18c6117 203#endif
0a753a76 204 }
205
642f9deb
GS
206 perl_destruct(my_perl);
207 perl_free(my_perl);
ed094faf
GS
208#ifdef USE_ITHREADS
209 if (new_perl) {
ba869deb 210 PERL_SET_THX(new_perl);
ed094faf
GS
211 perl_destruct(new_perl);
212 perl_free(new_perl);
213 }
214#endif
0a753a76 215
216 PERL_SYS_TERM();
217
68dc0745 218 return (exitstatus);
0a753a76 219}
220
2fa86c13
GS
221EXTERN_C void
222set_w32_module_name(void);
223
b73db59c
GS
224EXTERN_C void
225EndSockets(void);
226
227
f8fb7c90
GS
228#ifdef __MINGW32__
229EXTERN_C /* GCC in C++ mode mangles the name, otherwise */
230#endif
68dc0745 231BOOL APIENTRY
232DllMain(HANDLE hModule, /* DLL module handle */
233 DWORD fdwReason, /* reason called */
234 LPVOID lpvReserved) /* reserved */
0a753a76 235{
68dc0745 236 switch (fdwReason) {
237 /* The DLL is attaching to a process due to process
238 * initialization or a call to LoadLibrary.
239 */
240 case DLL_PROCESS_ATTACH:
241/* #define DEFAULT_BINMODE */
0a753a76 242#ifdef DEFAULT_BINMODE
3e3baf6d
TB
243 setmode( fileno( stdin ), O_BINARY );
244 setmode( fileno( stdout ), O_BINARY );
245 setmode( fileno( stderr ), O_BINARY );
246 _fmode = O_BINARY;
0a753a76 247#endif
5db10396 248 DisableThreadLibraryCalls((HMODULE)hModule);
2d7a9237 249 w32_perldll_handle = hModule;
2fa86c13 250 set_w32_module_name();
68dc0745 251 break;
0a753a76 252
68dc0745 253 /* The DLL is detaching from a process due to
254 * process termination or call to FreeLibrary.
255 */
256 case DLL_PROCESS_DETACH:
ce3e5b80
NIS
257 /* As long as we use TerminateProcess()/TerminateThread() etc. for mimicing kill()
258 anything here had better be harmless if:
259 A. Not called at all.
260 B. Called after memory allocation for Heap has been forcibly removed by OS.
261 PerlIO_cleanup() was done here but fails (B).
262 */
b73db59c 263 EndSockets();
3db8f154 264#if defined(USE_ITHREADS)
e1b5da64
GS
265 if (PL_curinterp)
266 FREE_THREAD_KEY;
267#endif
68dc0745 268 break;
0a753a76 269
68dc0745 270 /* The attached process creates a new thread. */
271 case DLL_THREAD_ATTACH:
272 break;
0a753a76 273
68dc0745 274 /* The thread of the attached process terminates. */
275 case DLL_THREAD_DETACH:
276 break;
0a753a76 277
68dc0745 278 default:
279 break;
280 }
281 return TRUE;
0a753a76 282}
c43294b8 283
9613994f 284#if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
c43294b8
AB
285EXTERN_C PerlInterpreter *
286perl_clone_host(PerlInterpreter* proto_perl, UV flags) {
acfe0abc 287 dTHX;
c43294b8
AB
288 CPerlHost *h;
289 h = new CPerlHost(*(CPerlHost*)PL_sys_intern.internal_host);
290 proto_perl = perl_clone_using(proto_perl, flags,
291 h->m_pHostperlMem,
292 h->m_pHostperlMemShared,
293 h->m_pHostperlMemParse,
294 h->m_pHostperlEnv,
295 h->m_pHostperlStdIO,
296 h->m_pHostperlLIO,
297 h->m_pHostperlDir,
298 h->m_pHostperlSock,
299 h->m_pHostperlProc
300 );
301 proto_perl->Isys_intern.internal_host = h;
222c300a 302 h->host_perl = proto_perl;
c43294b8
AB
303 return proto_perl;
304
305}
306#endif