Defer inclusion of VMS-specific code to runtime
[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 */
4
0a753a76 5
6#include "EXTERN.h"
7#include "perl.h"
0cb96387 8
9#ifdef PERL_OBJECT
10#define NO_XSLOCKS
11#endif
12
96e4d5b1 13#include "XSUB.h"
0a753a76 14
146174a9 15#ifdef PERL_IMPLICIT_SYS
16#include "win32iop.h"
17#include <fcntl.h>
18#endif /* PERL_IMPLICIT_SYS */
0cb96387 19
0cb96387 20
146174a9 21/* Register any extra external extensions */
22char *staticlinkmodules[] = {
23 "DynaLoader",
24 NULL,
0cb96387 25};
26
146174a9 27EXTERN_C void boot_DynaLoader (pTHXo_ CV* cv);
0cb96387 28
146174a9 29static void
30xs_init(pTHXo)
0cb96387 31{
146174a9 32 char *file = __FILE__;
33 dXSUB_SYS;
34 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
0cb96387 35}
36
146174a9 37#ifdef PERL_IMPLICIT_SYS
0cb96387 38
146174a9 39#include "perlhost.h"
0cb96387 40
146174a9 41EXTERN_C void
42perl_get_host_info(struct IPerlMemInfo* perlMemInfo,
43 struct IPerlMemInfo* perlMemSharedInfo,
44 struct IPerlMemInfo* perlMemParseInfo,
45 struct IPerlEnvInfo* perlEnvInfo,
46 struct IPerlStdIOInfo* perlStdIOInfo,
47 struct IPerlLIOInfo* perlLIOInfo,
48 struct IPerlDirInfo* perlDirInfo,
49 struct IPerlSockInfo* perlSockInfo,
50 struct IPerlProcInfo* perlProcInfo)
0cb96387 51{
146174a9 52 if (perlMemInfo) {
0cb96387 53 Copy(&perlMem, &perlMemInfo->perlMemList, perlMemInfo->nCount, void*);
54 perlMemInfo->nCount = (sizeof(struct IPerlMem)/sizeof(void*));
55 }
146174a9 56 if (perlMemSharedInfo) {
57 Copy(&perlMem, &perlMemSharedInfo->perlMemList, perlMemSharedInfo->nCount, void*);
58 perlMemSharedInfo->nCount = (sizeof(struct IPerlMem)/sizeof(void*));
59 }
60 if (perlMemParseInfo) {
61 Copy(&perlMem, &perlMemParseInfo->perlMemList, perlMemParseInfo->nCount, void*);
62 perlMemParseInfo->nCount = (sizeof(struct IPerlMem)/sizeof(void*));
63 }
64 if (perlEnvInfo) {
0cb96387 65 Copy(&perlEnv, &perlEnvInfo->perlEnvList, perlEnvInfo->nCount, void*);
66 perlEnvInfo->nCount = (sizeof(struct IPerlEnv)/sizeof(void*));
67 }
146174a9 68 if (perlStdIOInfo) {
0cb96387 69 Copy(&perlStdIO, &perlStdIOInfo->perlStdIOList, perlStdIOInfo->nCount, void*);
70 perlStdIOInfo->nCount = (sizeof(struct IPerlStdIO)/sizeof(void*));
71 }
146174a9 72 if (perlLIOInfo) {
0cb96387 73 Copy(&perlLIO, &perlLIOInfo->perlLIOList, perlLIOInfo->nCount, void*);
74 perlLIOInfo->nCount = (sizeof(struct IPerlLIO)/sizeof(void*));
75 }
146174a9 76 if (perlDirInfo) {
0cb96387 77 Copy(&perlDir, &perlDirInfo->perlDirList, perlDirInfo->nCount, void*);
78 perlDirInfo->nCount = (sizeof(struct IPerlDir)/sizeof(void*));
79 }
146174a9 80 if (perlSockInfo) {
0cb96387 81 Copy(&perlSock, &perlSockInfo->perlSockList, perlSockInfo->nCount, void*);
82 perlSockInfo->nCount = (sizeof(struct IPerlSock)/sizeof(void*));
83 }
146174a9 84 if (perlProcInfo) {
0cb96387 85 Copy(&perlProc, &perlProcInfo->perlProcList, perlProcInfo->nCount, void*);
86 perlProcInfo->nCount = (sizeof(struct IPerlProc)/sizeof(void*));
87 }
88}
89
146174a9 90EXTERN_C PerlInterpreter*
91perl_alloc_override(struct IPerlMem** ppMem, struct IPerlMem** ppMemShared,
92 struct IPerlMem** ppMemParse, struct IPerlEnv** ppEnv,
93 struct IPerlStdIO** ppStdIO, struct IPerlLIO** ppLIO,
94 struct IPerlDir** ppDir, struct IPerlSock** ppSock,
95 struct IPerlProc** ppProc)
0cb96387 96{
146174a9 97 PerlInterpreter *my_perl = NULL;
cb50131a 98 CPerlHost* pHost = new CPerlHost(ppMem, ppMemShared, ppMemParse, ppEnv,
99 ppStdIO, ppLIO, ppDir, ppSock, ppProc);
100
101 if (pHost) {
102 my_perl = perl_alloc_using(pHost->m_pHostperlMem,
103 pHost->m_pHostperlMemShared,
104 pHost->m_pHostperlMemParse,
105 pHost->m_pHostperlEnv,
106 pHost->m_pHostperlStdIO,
107 pHost->m_pHostperlLIO,
108 pHost->m_pHostperlDir,
109 pHost->m_pHostperlSock,
110 pHost->m_pHostperlProc);
111 if (my_perl) {
112#ifdef PERL_OBJECT
113 CPerlObj* pPerl = (CPerlObj*)my_perl;
114#endif
115 w32_internal_host = pHost;
146174a9 116 }
0cb96387 117 }
146174a9 118 return my_perl;
0cb96387 119}
120
146174a9 121EXTERN_C PerlInterpreter*
122perl_alloc(void)
0cb96387 123{
146174a9 124 PerlInterpreter* my_perl = NULL;
cb50131a 125 CPerlHost* pHost = new CPerlHost();
126 if (pHost) {
127 my_perl = perl_alloc_using(pHost->m_pHostperlMem,
128 pHost->m_pHostperlMemShared,
129 pHost->m_pHostperlMemParse,
130 pHost->m_pHostperlEnv,
131 pHost->m_pHostperlStdIO,
132 pHost->m_pHostperlLIO,
133 pHost->m_pHostperlDir,
134 pHost->m_pHostperlSock,
135 pHost->m_pHostperlProc);
136 if (my_perl) {
137#ifdef PERL_OBJECT
138 CPerlObj* pPerl = (CPerlObj*)my_perl;
139#endif
140 w32_internal_host = pHost;
146174a9 141 }
0cb96387 142 }
146174a9 143 return my_perl;
0cb96387 144}
145
cb50131a 146#ifdef PERL_OBJECT
147
146174a9 148EXTERN_C void
149perl_construct(PerlInterpreter* my_perl)
0cb96387 150{
146174a9 151 CPerlObj* pPerl = (CPerlObj*)my_perl;
0cb96387 152 try
153 {
146174a9 154 Perl_construct();
0cb96387 155 }
156 catch(...)
157 {
158 win32_fprintf(stderr, "%s\n",
159 "Error: Unable to construct data structures");
146174a9 160 CPerlHost* pHost = (CPerlHost*)w32_internal_host;
161 Perl_free();
162 delete pHost;
0cb96387 163 SetPerlInterpreter(NULL);
164 }
165}
166
146174a9 167EXTERN_C void
168perl_destruct(PerlInterpreter* my_perl)
0cb96387 169{
146174a9 170 CPerlObj* pPerl = (CPerlObj*)my_perl;
171#ifdef DEBUGGING
172 Perl_destruct();
173#else
0cb96387 174 try
175 {
146174a9 176 Perl_destruct();
0cb96387 177 }
178 catch(...)
179 {
180 }
146174a9 181#endif
0cb96387 182}
183
146174a9 184EXTERN_C void
185perl_free(PerlInterpreter* my_perl)
0cb96387 186{
146174a9 187 CPerlObj* pPerl = (CPerlObj*)my_perl;
188#ifdef DEBUGGING
189 CPerlHost* pHost = (CPerlHost*)w32_internal_host;
190 Perl_free();
191 delete pHost;
192#else
0cb96387 193 try
194 {
146174a9 195 CPerlHost* pHost = (CPerlHost*)w32_internal_host;
196 Perl_free();
197 delete pHost;
0cb96387 198 }
199 catch(...)
200 {
201 }
146174a9 202#endif
0cb96387 203 SetPerlInterpreter(NULL);
204}
205
146174a9 206EXTERN_C int
207perl_run(PerlInterpreter* my_perl)
0cb96387 208{
146174a9 209 CPerlObj* pPerl = (CPerlObj*)my_perl;
210#ifdef DEBUGGING
211 return Perl_run();
212#else
0cb96387 213 int retVal;
214 try
215 {
146174a9 216 retVal = Perl_run();
0cb96387 217 }
0cb96387 218 catch(...)
219 {
220 win32_fprintf(stderr, "Error: Runtime exception\n");
221 retVal = -1;
222 }
223 return retVal;
146174a9 224#endif
0cb96387 225}
226
146174a9 227EXTERN_C int
228perl_parse(PerlInterpreter* my_perl, void (*xsinit)(CPerlObj*), int argc, char** argv, char** env)
0cb96387 229{
230 int retVal;
146174a9 231 CPerlObj* pPerl = (CPerlObj*)my_perl;
232#ifdef DEBUGGING
233 retVal = Perl_parse(xsinit, argc, argv, env);
234#else
0cb96387 235 try
236 {
146174a9 237 retVal = Perl_parse(xsinit, argc, argv, env);
0cb96387 238 }
0cb96387 239 catch(...)
240 {
241 win32_fprintf(stderr, "Error: Parse exception\n");
242 retVal = -1;
243 }
146174a9 244#endif
0cb96387 245 *win32_errno() = 0;
246 return retVal;
247}
248
249#undef PL_perl_destruct_level
250#define PL_perl_destruct_level int dummy
146174a9 251
0cb96387 252#endif /* PERL_OBJECT */
146174a9 253#endif /* PERL_IMPLICIT_SYS */
254
255EXTERN_C HANDLE w32_perldll_handle;
0a753a76 256
c5be433b 257static DWORD g_TlsAllocIndex;
258
259EXTERN_C DllExport bool
260SetPerlInterpreter(void *interp)
261{
cb50131a 262 DWORD dwErr = GetLastError();
263 bool bResult = TlsSetValue(g_TlsAllocIndex, interp);
264 SetLastError(dwErr);
265 return bResult;
c5be433b 266}
267
268EXTERN_C DllExport void*
269GetPerlInterpreter(void)
270{
cb50131a 271 DWORD dwErr = GetLastError();
272 LPVOID pResult = TlsGetValue(g_TlsAllocIndex);
273 SetLastError(dwErr);
274 return pResult;
c5be433b 275}
276
277EXTERN_C DllExport int
0cb96387 278RunPerl(int argc, char **argv, char **env)
0a753a76 279{
68dc0745 280 int exitstatus;
146174a9 281 PerlInterpreter *my_perl, *new_perl = NULL;
4f63d024 282 struct perl_thread *thr;
0a753a76 283
0cb96387 284#ifndef __BORLANDC__
285 /* XXX this _may_ be a problem on some compilers (e.g. Borland) that
286 * want to free() argv after main() returns. As luck would have it,
287 * Borland's CRT does the right thing to argv[0] already. */
288 char szModuleName[MAX_PATH];
289 char *ptr;
290
291 GetModuleFileName(NULL, szModuleName, sizeof(szModuleName));
292 (void)win32_longpath(szModuleName);
293 argv[0] = szModuleName;
294#endif
295
22239a37 296#ifdef PERL_GLOBAL_STRUCT
297#define PERLVAR(var,type) /**/
51371543 298#define PERLVARA(var,type) /**/
533c011a 299#define PERLVARI(var,type,init) PL_Vars.var = init;
300#define PERLVARIC(var,type,init) PL_Vars.var = init;
22239a37 301#include "perlvars.h"
302#undef PERLVAR
51371543 303#undef PERLVARA
22239a37 304#undef PERLVARI
3fe35a81 305#undef PERLVARIC
22239a37 306#endif
307
0a753a76 308 PERL_SYS_INIT(&argc,&argv);
309
68dc0745 310 if (!(my_perl = perl_alloc()))
311 return (1);
312 perl_construct( my_perl );
b28d0864 313 PL_perl_destruct_level = 0;
0a753a76 314
4f63d024 315 exitstatus = perl_parse(my_perl, xs_init, argc, argv, env);
0a753a76 316 if (!exitstatus) {
146174a9 317#if defined(TOP_CLONE) && defined(USE_ITHREADS) /* XXXXXX testing */
318# ifdef PERL_OBJECT
319 CPerlHost *h = new CPerlHost();
320 new_perl = perl_clone_using(my_perl, 1,
321 h->m_pHostperlMem,
322 h->m_pHostperlMemShared,
323 h->m_pHostperlMemParse,
324 h->m_pHostperlEnv,
325 h->m_pHostperlStdIO,
326 h->m_pHostperlLIO,
327 h->m_pHostperlDir,
328 h->m_pHostperlSock,
329 h->m_pHostperlProc
330 );
331 CPerlObj *pPerl = (CPerlObj*)new_perl;
332# else
333 new_perl = perl_clone(my_perl, 1);
334# endif
335 exitstatus = perl_run( new_perl );
336 SetPerlInterpreter(my_perl);
337#else
0a753a76 338 exitstatus = perl_run( my_perl );
146174a9 339#endif
0a753a76 340 }
341
0a753a76 342 perl_destruct( my_perl );
343 perl_free( my_perl );
146174a9 344#ifdef USE_ITHREADS
345 if (new_perl) {
346 SetPerlInterpreter(new_perl);
347 perl_destruct(new_perl);
348 perl_free(new_perl);
349 }
350#endif
0a753a76 351
352 PERL_SYS_TERM();
353
68dc0745 354 return (exitstatus);
0a753a76 355}
356
68dc0745 357BOOL APIENTRY
358DllMain(HANDLE hModule, /* DLL module handle */
359 DWORD fdwReason, /* reason called */
360 LPVOID lpvReserved) /* reserved */
0a753a76 361{
68dc0745 362 switch (fdwReason) {
363 /* The DLL is attaching to a process due to process
364 * initialization or a call to LoadLibrary.
365 */
366 case DLL_PROCESS_ATTACH:
367/* #define DEFAULT_BINMODE */
0a753a76 368#ifdef DEFAULT_BINMODE
3e3baf6d 369 setmode( fileno( stdin ), O_BINARY );
370 setmode( fileno( stdout ), O_BINARY );
371 setmode( fileno( stderr ), O_BINARY );
372 _fmode = O_BINARY;
0a753a76 373#endif
0cb96387 374 g_TlsAllocIndex = TlsAlloc();
146174a9 375 DisableThreadLibraryCalls((HMODULE)hModule);
2d7a9237 376 w32_perldll_handle = hModule;
68dc0745 377 break;
0a753a76 378
68dc0745 379 /* The DLL is detaching from a process due to
380 * process termination or call to FreeLibrary.
381 */
382 case DLL_PROCESS_DETACH:
0cb96387 383 TlsFree(g_TlsAllocIndex);
68dc0745 384 break;
0a753a76 385
68dc0745 386 /* The attached process creates a new thread. */
387 case DLL_THREAD_ATTACH:
388 break;
0a753a76 389
68dc0745 390 /* The thread of the attached process terminates. */
391 case DLL_THREAD_DETACH:
392 break;
0a753a76 393
68dc0745 394 default:
395 break;
396 }
397 return TRUE;
0a753a76 398}