perllib.c revision 1.10
191586Smarkm/*
212099Sjoerg *      The Road goes ever on and on
312099Sjoerg *          Down from the door where it began.
491586Smarkm *
512099Sjoerg *     [Bilbo on p.35 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
612099Sjoerg *     [Frodo on p.73 of _The Lord of the Rings_, I/iii: "Three Is Company"]
712099Sjoerg */
812099Sjoerg#define PERLIO_NOT_STDIO 0
912099Sjoerg#include "EXTERN.h"
1012099Sjoerg#include "perl.h"
1112099Sjoerg
1212099Sjoerg#include "XSUB.h"
1312099Sjoerg
1412099Sjoerg#ifdef PERL_IMPLICIT_SYS
1512099Sjoerg#include "win32iop.h"
1612099Sjoerg#include <fcntl.h>
1712099Sjoerg#endif /* PERL_IMPLICIT_SYS */
1812099Sjoerg
1912099Sjoerg
2012099Sjoerg/* Register any extra external extensions */
2112099Sjoergchar *staticlinkmodules[] = {
2212099Sjoerg    "DynaLoader",
2312099Sjoerg    /* other similar records will be included from "perllibst.h" */
2412099Sjoerg#define STATIC1
2512099Sjoerg#include "perllibst.h"
2612099Sjoerg    NULL,
2712099Sjoerg};
2812099Sjoerg
2912099SjoergEXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
3012099Sjoerg/* other similar records will be included from "perllibst.h" */
3112099Sjoerg#define STATIC2
3212099Sjoerg#include "perllibst.h"
3312099Sjoerg
3412099Sjoergstatic void
3512099Sjoergxs_init(pTHX)
3612099Sjoerg{
3712099Sjoerg    char *file = __FILE__;
3812099Sjoerg    dXSUB_SYS;
3912099Sjoerg    newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
4012099Sjoerg    /* other similar records will be included from "perllibst.h" */
4112099Sjoerg#define STATIC3
4212099Sjoerg#include "perllibst.h"
4312099Sjoerg}
4412099Sjoerg
4512099Sjoerg#ifdef PERL_IMPLICIT_SYS
4612099Sjoerg
4712099Sjoerg/* WINCE: include replaced by:
4812099Sjoergextern "C" void win32_checkTLS(PerlInterpreter *host_perl);
4991586Smarkm*/
5012099Sjoerg#include "perlhost.h"
5112099Sjoerg
5212099Sjoergvoid
5312099Sjoergwin32_checkTLS(PerlInterpreter *host_perl)
5412099Sjoerg{
5512099Sjoerg    dTHX;
5612099Sjoerg    if (host_perl != my_perl) {
5791586Smarkm	int *nowhere = NULL;
5891586Smarkm#ifdef UNDER_CE
5991586Smarkm	printf(" ... bad in win32_checkTLS\n");
6091586Smarkm	printf("  %08X ne %08X\n",host_perl,my_perl);
6191586Smarkm#endif
6291586Smarkm	abort();
6391586Smarkm    }
6412099Sjoerg}
6512099Sjoerg
6612099Sjoerg#ifdef UNDER_CE
6712099Sjoergint GetLogicalDrives() {
6812099Sjoerg    return 0; /* no logical drives on CE */
6912099Sjoerg}
7012099Sjoergint GetLogicalDriveStrings(int size, char addr[]) {
7191586Smarkm    return 0; /* no logical drives on CE */
7291586Smarkm}
7391586Smarkm/* TBD */
7491586SmarkmDWORD GetFullPathNameA(LPCSTR fn, DWORD blen, LPTSTR buf,  LPSTR *pfile) {
7591586Smarkm    return 0;
7612099Sjoerg}
7712099Sjoerg/* TBD */
7812099SjoergDWORD GetFullPathNameW(CONST WCHAR *fn, DWORD blen, WCHAR * buf,  WCHAR **pfile) {
7912099Sjoerg    return 0;
8012099Sjoerg}
8112099Sjoerg/* TBD */
8212099SjoergDWORD SetCurrentDirectoryA(LPSTR pPath) {
8312099Sjoerg    return 0;
8412099Sjoerg}
8512099Sjoerg/* TBD */
8612099SjoergDWORD SetCurrentDirectoryW(CONST WCHAR *pPath) {
8712099Sjoerg    return 0;
8812099Sjoerg}
8912099Sjoergint xcesetuid(uid_t id){return 0;}
9012099Sjoergint xceseteuid(uid_t id){  return 0;}
9112099Sjoergint xcegetuid() {return 0;}
9212099Sjoergint xcegeteuid(){ return 0;}
9312099Sjoerg#endif
9412099Sjoerg
9512099Sjoerg/* WINCE??: include "perlhost.h" */
9612099Sjoerg
9712099SjoergEXTERN_C void
9812099Sjoergperl_get_host_info(struct IPerlMemInfo* perlMemInfo,
9912099Sjoerg		   struct IPerlMemInfo* perlMemSharedInfo,
10012099Sjoerg		   struct IPerlMemInfo* perlMemParseInfo,
10112099Sjoerg		   struct IPerlEnvInfo* perlEnvInfo,
10212099Sjoerg		   struct IPerlStdIOInfo* perlStdIOInfo,
10391586Smarkm		   struct IPerlLIOInfo* perlLIOInfo,
10412099Sjoerg		   struct IPerlDirInfo* perlDirInfo,
10512099Sjoerg		   struct IPerlSockInfo* perlSockInfo,
10612099Sjoerg		   struct IPerlProcInfo* perlProcInfo)
10712099Sjoerg{
10812099Sjoerg    if (perlMemInfo) {
10912099Sjoerg	Copy(&perlMem, &perlMemInfo->perlMemList, perlMemInfo->nCount, void*);
11012099Sjoerg	perlMemInfo->nCount = (sizeof(struct IPerlMem)/sizeof(void*));
11112099Sjoerg    }
11212099Sjoerg    if (perlMemSharedInfo) {
11312099Sjoerg	Copy(&perlMem, &perlMemSharedInfo->perlMemList, perlMemSharedInfo->nCount, void*);
11412099Sjoerg	perlMemSharedInfo->nCount = (sizeof(struct IPerlMem)/sizeof(void*));
11512099Sjoerg    }
11612099Sjoerg    if (perlMemParseInfo) {
11712099Sjoerg	Copy(&perlMem, &perlMemParseInfo->perlMemList, perlMemParseInfo->nCount, void*);
11812099Sjoerg	perlMemParseInfo->nCount = (sizeof(struct IPerlMem)/sizeof(void*));
11912099Sjoerg    }
12091586Smarkm    if (perlEnvInfo) {
12112099Sjoerg	Copy(&perlEnv, &perlEnvInfo->perlEnvList, perlEnvInfo->nCount, void*);
12212099Sjoerg	perlEnvInfo->nCount = (sizeof(struct IPerlEnv)/sizeof(void*));
12312099Sjoerg    }
12412099Sjoerg    if (perlStdIOInfo) {
12512099Sjoerg	Copy(&perlStdIO, &perlStdIOInfo->perlStdIOList, perlStdIOInfo->nCount, void*);
12612099Sjoerg	perlStdIOInfo->nCount = (sizeof(struct IPerlStdIO)/sizeof(void*));
12712099Sjoerg    }
12812099Sjoerg    if (perlLIOInfo) {
12912099Sjoerg	Copy(&perlLIO, &perlLIOInfo->perlLIOList, perlLIOInfo->nCount, void*);
13012099Sjoerg	perlLIOInfo->nCount = (sizeof(struct IPerlLIO)/sizeof(void*));
13112099Sjoerg    }
13212099Sjoerg    if (perlDirInfo) {
13312099Sjoerg	Copy(&perlDir, &perlDirInfo->perlDirList, perlDirInfo->nCount, void*);
13412099Sjoerg	perlDirInfo->nCount = (sizeof(struct IPerlDir)/sizeof(void*));
13512099Sjoerg    }
13612099Sjoerg    if (perlSockInfo) {
13712099Sjoerg	Copy(&perlSock, &perlSockInfo->perlSockList, perlSockInfo->nCount, void*);
13812099Sjoerg	perlSockInfo->nCount = (sizeof(struct IPerlSock)/sizeof(void*));
13912099Sjoerg    }
14012099Sjoerg    if (perlProcInfo) {
14112099Sjoerg	Copy(&perlProc, &perlProcInfo->perlProcList, perlProcInfo->nCount, void*);
14212099Sjoerg	perlProcInfo->nCount = (sizeof(struct IPerlProc)/sizeof(void*));
14312099Sjoerg    }
14412099Sjoerg}
14512099Sjoerg
14612099SjoergEXTERN_C PerlInterpreter*
14712099Sjoergperl_alloc_override(struct IPerlMem** ppMem, struct IPerlMem** ppMemShared,
14812099Sjoerg		 struct IPerlMem** ppMemParse, struct IPerlEnv** ppEnv,
14912099Sjoerg		 struct IPerlStdIO** ppStdIO, struct IPerlLIO** ppLIO,
15012099Sjoerg		 struct IPerlDir** ppDir, struct IPerlSock** ppSock,
15112099Sjoerg		 struct IPerlProc** ppProc)
15212099Sjoerg{
15312099Sjoerg    PerlInterpreter *my_perl = NULL;
15412099Sjoerg    CPerlHost* pHost = new CPerlHost(ppMem, ppMemShared, ppMemParse, ppEnv,
15512099Sjoerg				     ppStdIO, ppLIO, ppDir, ppSock, ppProc);
15612099Sjoerg
15712099Sjoerg    if (pHost) {
15812099Sjoerg	my_perl = perl_alloc_using(pHost->m_pHostperlMem,
15912099Sjoerg				   pHost->m_pHostperlMemShared,
16012099Sjoerg				   pHost->m_pHostperlMemParse,
16112099Sjoerg				   pHost->m_pHostperlEnv,
16212099Sjoerg				   pHost->m_pHostperlStdIO,
16312099Sjoerg				   pHost->m_pHostperlLIO,
16412099Sjoerg				   pHost->m_pHostperlDir,
16512099Sjoerg				   pHost->m_pHostperlSock,
16612099Sjoerg				   pHost->m_pHostperlProc);
16712099Sjoerg	if (my_perl) {
16812099Sjoerg	    w32_internal_host = pHost;
16912099Sjoerg	    pHost->host_perl  = my_perl;
17012099Sjoerg	}
17112099Sjoerg    }
17212099Sjoerg    return my_perl;
17312099Sjoerg}
17412099Sjoerg
17512099SjoergEXTERN_C PerlInterpreter*
17612099Sjoergperl_alloc(void)
17712099Sjoerg{
17812099Sjoerg    PerlInterpreter* my_perl = NULL;
17912099Sjoerg    CPerlHost* pHost = new CPerlHost();
18012099Sjoerg    if (pHost) {
18112099Sjoerg	my_perl = perl_alloc_using(pHost->m_pHostperlMem,
18291586Smarkm				   pHost->m_pHostperlMemShared,
18312099Sjoerg				   pHost->m_pHostperlMemParse,
18412099Sjoerg				   pHost->m_pHostperlEnv,
18512099Sjoerg				   pHost->m_pHostperlStdIO,
18612099Sjoerg				   pHost->m_pHostperlLIO,
18712099Sjoerg				   pHost->m_pHostperlDir,
18812099Sjoerg				   pHost->m_pHostperlSock,
189				   pHost->m_pHostperlProc);
190	if (my_perl) {
191	    w32_internal_host = pHost;
192            pHost->host_perl  = my_perl;
193	}
194    }
195    return my_perl;
196}
197
198EXTERN_C void
199win32_delete_internal_host(void *h)
200{
201    CPerlHost *host = (CPerlHost*)h;
202    delete host;
203}
204
205#endif /* PERL_IMPLICIT_SYS */
206
207EXTERN_C HANDLE w32_perldll_handle;
208
209EXTERN_C DllExport int
210RunPerl(int argc, char **argv, char **env)
211{
212    int exitstatus;
213    PerlInterpreter *my_perl, *new_perl = NULL;
214    OSVERSIONINFO osver;
215    char szModuleName[MAX_PATH];
216    char *arg0 = argv[0];
217    char *ansi = NULL;
218    bool use_environ = (env == environ);
219
220    osver.dwOSVersionInfoSize = sizeof(osver);
221    GetVersionEx(&osver);
222
223    if (osver.dwMajorVersion > 4) {
224        WCHAR widename[MAX_PATH];
225        GetModuleFileNameW(NULL, widename, sizeof(widename)/sizeof(WCHAR));
226        argv[0] = ansi = win32_ansipath(widename);
227    }
228    else {
229        Win_GetModuleFileName(NULL, szModuleName, sizeof(szModuleName));
230        (void)win32_longpath(szModuleName);
231        argv[0] = szModuleName;
232    }
233
234#ifdef PERL_GLOBAL_STRUCT
235#define PERLVAR(prefix,var,type) /**/
236#define PERLVARA(prefix,var,type) /**/
237#define PERLVARI(prefix,var,type,init) PL_Vars.prefix##var = init;
238#define PERLVARIC(prefix,var,type,init) PL_Vars.prefix##var = init;
239#include "perlvars.h"
240#undef PERLVAR
241#undef PERLVARA
242#undef PERLVARI
243#undef PERLVARIC
244#endif
245
246    PERL_SYS_INIT(&argc,&argv);
247
248    if (!(my_perl = perl_alloc()))
249	return (1);
250    perl_construct(my_perl);
251    PL_perl_destruct_level = 0;
252
253    /* PERL_SYS_INIT() may update the environment, e.g. via ansify_path().
254     * This may reallocate the RTL environment block. Therefore we need
255     * to make sure that `env` continues to have the same value as `environ`
256     * if we have been called this way.  If we have been called with any
257     * other value for `env` then all environment munging by PERL_SYS_INIT()
258     * will be lost again.
259     */
260    if (use_environ)
261        env = environ;
262
263    exitstatus = perl_parse(my_perl, xs_init, argc, argv, env);
264    if (!exitstatus) {
265#if defined(TOP_CLONE) && defined(USE_ITHREADS)		/* XXXXXX testing */
266	new_perl = perl_clone(my_perl, 1);
267	exitstatus = perl_run(new_perl);
268	PERL_SET_THX(my_perl);
269#else
270	exitstatus = perl_run(my_perl);
271#endif
272    }
273
274    perl_destruct(my_perl);
275    perl_free(my_perl);
276#ifdef USE_ITHREADS
277    if (new_perl) {
278	PERL_SET_THX(new_perl);
279	perl_destruct(new_perl);
280	perl_free(new_perl);
281    }
282#endif
283
284    /* Some RTLs may want to free argv[] after main() returns. */
285    argv[0] = arg0;
286    if (ansi)
287        win32_free(ansi);
288
289    PERL_SYS_TERM();
290
291    return (exitstatus);
292}
293
294EXTERN_C void
295set_w32_module_name(void);
296
297EXTERN_C void
298EndSockets(void);
299
300
301#ifdef __MINGW32__
302EXTERN_C		/* GCC in C++ mode mangles the name, otherwise */
303#endif
304BOOL APIENTRY
305DllMain(HANDLE hModule,		/* DLL module handle */
306	DWORD fdwReason,	/* reason called */
307	LPVOID lpvReserved)	/* reserved */
308{
309    switch (fdwReason) {
310	/* The DLL is attaching to a process due to process
311	 * initialization or a call to LoadLibrary.
312	 */
313    case DLL_PROCESS_ATTACH:
314/* #define DEFAULT_BINMODE */
315#ifdef DEFAULT_BINMODE
316	setmode( fileno( stdin  ), O_BINARY );
317	setmode( fileno( stdout ), O_BINARY );
318	setmode( fileno( stderr ), O_BINARY );
319	_fmode = O_BINARY;
320#endif
321
322#ifndef UNDER_CE
323	DisableThreadLibraryCalls((HMODULE)hModule);
324#endif
325
326	w32_perldll_handle = hModule;
327	set_w32_module_name();
328	break;
329
330	/* The DLL is detaching from a process due to
331	 * process termination or call to FreeLibrary.
332	 */
333    case DLL_PROCESS_DETACH:
334        /* As long as we use TerminateProcess()/TerminateThread() etc. for mimicing kill()
335           anything here had better be harmless if:
336            A. Not called at all.
337            B. Called after memory allocation for Heap has been forcibly removed by OS.
338            PerlIO_cleanup() was done here but fails (B).
339         */
340	EndSockets();
341#if defined(USE_ITHREADS)
342	if (PL_curinterp)
343	    FREE_THREAD_KEY;
344#endif
345	break;
346
347	/* The attached process creates a new thread. */
348    case DLL_THREAD_ATTACH:
349	break;
350
351	/* The thread of the attached process terminates. */
352    case DLL_THREAD_DETACH:
353	break;
354
355    default:
356	break;
357    }
358    return TRUE;
359}
360
361
362#if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
363EXTERN_C PerlInterpreter *
364perl_clone_host(PerlInterpreter* proto_perl, UV flags) {
365    dTHX;
366    CPerlHost *h;
367    h = new CPerlHost(*(CPerlHost*)PL_sys_intern.internal_host);
368    proto_perl = perl_clone_using(proto_perl, flags,
369                        h->m_pHostperlMem,
370                        h->m_pHostperlMemShared,
371                        h->m_pHostperlMemParse,
372                        h->m_pHostperlEnv,
373                        h->m_pHostperlStdIO,
374                        h->m_pHostperlLIO,
375                        h->m_pHostperlDir,
376                        h->m_pHostperlSock,
377                        h->m_pHostperlProc
378    );
379    proto_perl->Isys_intern.internal_host = h;
380    h->host_perl  = proto_perl;
381    return proto_perl;
382
383}
384#endif
385