This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update to latest blead embed.fnc
[perl5.git] / win32 / perllib.c
1 /*
2  *      The Road goes ever on and on
3  *          Down from the door where it began.
4  *
5  *     [Bilbo on p.35 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
6  *     [Frodo on p.73 of _The Lord of the Rings_, I/iii: "Three Is Company"]
7  */
8 #define PERLIO_NOT_STDIO 0
9 #include "EXTERN.h"
10 #include "perl.h"
11
12 #include "XSUB.h"
13
14 #ifdef PERL_IMPLICIT_SYS
15 #include "win32iop.h"
16 #include <fcntl.h>
17 #endif /* PERL_IMPLICIT_SYS */
18
19
20 /* Register any extra external extensions */
21 const char * const staticlinkmodules[] = {
22     "DynaLoader",
23     /* other similar records will be included from "perllibst.h" */
24 #define STATIC1
25 #include "perllibst.h"
26     NULL,
27 };
28
29 EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
30 /* other similar records will be included from "perllibst.h" */
31 #define STATIC2
32 #include "perllibst.h"
33
34 static void
35 xs_init(pTHX)
36 {
37     char *file = __FILE__;
38     dXSUB_SYS;
39     newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
40     /* other similar records will be included from "perllibst.h" */
41 #define STATIC3
42 #include "perllibst.h"
43 }
44
45 #ifdef PERL_IMPLICIT_SYS
46
47 #include "perlhost.h"
48
49 void
50 win32_checkTLS(PerlInterpreter *host_perl)
51 {
52     dTHX;
53     if (host_perl != my_perl) {
54         int *nowhere = NULL;
55         abort();
56     }
57 }
58
59 EXTERN_C void
60 perl_get_host_info(struct IPerlMemInfo* perlMemInfo,
61                    struct IPerlMemInfo* perlMemSharedInfo,
62                    struct IPerlMemInfo* perlMemParseInfo,
63                    struct IPerlEnvInfo* perlEnvInfo,
64                    struct IPerlStdIOInfo* perlStdIOInfo,
65                    struct IPerlLIOInfo* perlLIOInfo,
66                    struct IPerlDirInfo* perlDirInfo,
67                    struct IPerlSockInfo* perlSockInfo,
68                    struct IPerlProcInfo* perlProcInfo)
69 {
70     if (perlMemInfo) {
71         Copy(&perlMem, &perlMemInfo->perlMemList, perlMemInfo->nCount, void*);
72         perlMemInfo->nCount = (sizeof(struct IPerlMem)/sizeof(void*));
73     }
74     if (perlMemSharedInfo) {
75         Copy(&perlMem, &perlMemSharedInfo->perlMemList, perlMemSharedInfo->nCount, void*);
76         perlMemSharedInfo->nCount = (sizeof(struct IPerlMem)/sizeof(void*));
77     }
78     if (perlMemParseInfo) {
79         Copy(&perlMem, &perlMemParseInfo->perlMemList, perlMemParseInfo->nCount, void*);
80         perlMemParseInfo->nCount = (sizeof(struct IPerlMem)/sizeof(void*));
81     }
82     if (perlEnvInfo) {
83         Copy(&perlEnv, &perlEnvInfo->perlEnvList, perlEnvInfo->nCount, void*);
84         perlEnvInfo->nCount = (sizeof(struct IPerlEnv)/sizeof(void*));
85     }
86     if (perlStdIOInfo) {
87         Copy(&perlStdIO, &perlStdIOInfo->perlStdIOList, perlStdIOInfo->nCount, void*);
88         perlStdIOInfo->nCount = (sizeof(struct IPerlStdIO)/sizeof(void*));
89     }
90     if (perlLIOInfo) {
91         Copy(&perlLIO, &perlLIOInfo->perlLIOList, perlLIOInfo->nCount, void*);
92         perlLIOInfo->nCount = (sizeof(struct IPerlLIO)/sizeof(void*));
93     }
94     if (perlDirInfo) {
95         Copy(&perlDir, &perlDirInfo->perlDirList, perlDirInfo->nCount, void*);
96         perlDirInfo->nCount = (sizeof(struct IPerlDir)/sizeof(void*));
97     }
98     if (perlSockInfo) {
99         Copy(&perlSock, &perlSockInfo->perlSockList, perlSockInfo->nCount, void*);
100         perlSockInfo->nCount = (sizeof(struct IPerlSock)/sizeof(void*));
101     }
102     if (perlProcInfo) {
103         Copy(&perlProc, &perlProcInfo->perlProcList, perlProcInfo->nCount, void*);
104         perlProcInfo->nCount = (sizeof(struct IPerlProc)/sizeof(void*));
105     }
106 }
107
108 EXTERN_C PerlInterpreter*
109 perl_alloc_override(struct IPerlMem** ppMem, struct IPerlMem** ppMemShared,
110                  struct IPerlMem** ppMemParse, struct IPerlEnv** ppEnv,
111                  struct IPerlStdIO** ppStdIO, struct IPerlLIO** ppLIO,
112                  struct IPerlDir** ppDir, struct IPerlSock** ppSock,
113                  struct IPerlProc** ppProc)
114 {
115     PerlInterpreter *my_perl = NULL;
116     CPerlHost* pHost = new CPerlHost(ppMem, ppMemShared, ppMemParse, ppEnv,
117                                      ppStdIO, ppLIO, ppDir, ppSock, ppProc);
118
119     if (pHost) {
120         my_perl = perl_alloc_using(pHost->m_pHostperlMem,
121                                    pHost->m_pHostperlMemShared,
122                                    pHost->m_pHostperlMemParse,
123                                    pHost->m_pHostperlEnv,
124                                    pHost->m_pHostperlStdIO,
125                                    pHost->m_pHostperlLIO,
126                                    pHost->m_pHostperlDir,
127                                    pHost->m_pHostperlSock,
128                                    pHost->m_pHostperlProc);
129         if (my_perl) {
130             w32_internal_host = pHost;
131             pHost->host_perl  = my_perl;
132         }
133     }
134     return my_perl;
135 }
136
137 EXTERN_C PerlInterpreter*
138 perl_alloc(void)
139 {
140     PerlInterpreter* my_perl = NULL;
141     CPerlHost* pHost = new CPerlHost();
142     if (pHost) {
143         my_perl = perl_alloc_using(pHost->m_pHostperlMem,
144                                    pHost->m_pHostperlMemShared,
145                                    pHost->m_pHostperlMemParse,
146                                    pHost->m_pHostperlEnv,
147                                    pHost->m_pHostperlStdIO,
148                                    pHost->m_pHostperlLIO,
149                                    pHost->m_pHostperlDir,
150                                    pHost->m_pHostperlSock,
151                                    pHost->m_pHostperlProc);
152         if (my_perl) {
153             w32_internal_host = pHost;
154             pHost->host_perl  = my_perl;
155         }
156     }
157     return my_perl;
158 }
159
160 EXTERN_C void
161 win32_delete_internal_host(void *h)
162 {
163     CPerlHost *host = (CPerlHost*)h;
164     delete host;
165 }
166
167 #endif /* PERL_IMPLICIT_SYS */
168
169 EXTERN_C HANDLE w32_perldll_handle;
170
171 EXTERN_C DllExport int
172 RunPerl(int argc, char **argv, char **env)
173 {
174     int exitstatus;
175     PerlInterpreter *my_perl, *new_perl = NULL;
176     bool use_environ = (env == environ);
177
178 #ifdef PERL_GLOBAL_STRUCT
179 #define PERLVAR(prefix,var,type) /**/
180 #define PERLVARA(prefix,var,type) /**/
181 #define PERLVARI(prefix,var,type,init) PL_Vars.prefix##var = init;
182 #define PERLVARIC(prefix,var,type,init) PL_Vars.prefix##var = init;
183 #include "perlvars.h"
184 #undef PERLVAR
185 #undef PERLVARA
186 #undef PERLVARI
187 #undef PERLVARIC
188 #endif
189
190     PERL_SYS_INIT(&argc,&argv);
191
192     if (!(my_perl = perl_alloc()))
193         return (1);
194     perl_construct(my_perl);
195     PL_perl_destruct_level = 0;
196
197     /* PERL_SYS_INIT() may update the environment, e.g. via ansify_path().
198      * This may reallocate the RTL environment block. Therefore we need
199      * to make sure that `env` continues to have the same value as `environ`
200      * if we have been called this way.  If we have been called with any
201      * other value for `env` then all environment munging by PERL_SYS_INIT()
202      * will be lost again.
203      */
204     if (use_environ)
205         env = environ;
206
207     if (!perl_parse(my_perl, xs_init, argc, argv, env)) {
208 #if defined(TOP_CLONE) && defined(USE_ITHREADS)         /* XXXXXX testing */
209         new_perl = perl_clone(my_perl, 1);
210         (void) perl_run(new_perl);
211         PERL_SET_THX(my_perl);
212 #else
213         (void) perl_run(my_perl);
214 #endif
215     }
216
217     exitstatus = perl_destruct(my_perl);
218     perl_free(my_perl);
219 #ifdef USE_ITHREADS
220     if (new_perl) {
221         PERL_SET_THX(new_perl);
222         exitstatus = perl_destruct(new_perl);
223         perl_free(new_perl);
224     }
225 #endif
226
227     PERL_SYS_TERM();
228
229     return (exitstatus);
230 }
231
232 EXTERN_C void
233 set_w32_module_name(void);
234
235 EXTERN_C void
236 EndSockets(void);
237
238
239 #ifdef __MINGW32__
240 EXTERN_C                /* GCC in C++ mode mangles the name, otherwise */
241 #endif
242 BOOL APIENTRY
243 DllMain(HINSTANCE hModule,      /* DLL module handle */
244         DWORD fdwReason,        /* reason called */
245         LPVOID lpvReserved)     /* reserved */
246
247     switch (fdwReason) {
248         /* The DLL is attaching to a process due to process
249          * initialization or a call to LoadLibrary.
250          */
251     case DLL_PROCESS_ATTACH:
252         DisableThreadLibraryCalls((HMODULE)hModule);
253
254         w32_perldll_handle = hModule;
255         set_w32_module_name();
256         break;
257
258         /* The DLL is detaching from a process due to
259          * process termination or call to FreeLibrary.
260          */
261     case DLL_PROCESS_DETACH:
262         /* As long as we use TerminateProcess()/TerminateThread() etc. for mimicing kill()
263            anything here had better be harmless if:
264             A. Not called at all.
265             B. Called after memory allocation for Heap has been forcibly removed by OS.
266             PerlIO_cleanup() was done here but fails (B).
267          */     
268         EndSockets();
269 #if defined(USE_ITHREADS)
270         if (PL_curinterp)
271             FREE_THREAD_KEY;
272 #endif
273         break;
274
275         /* The attached process creates a new thread. */
276     case DLL_THREAD_ATTACH:
277         break;
278
279         /* The thread of the attached process terminates. */
280     case DLL_THREAD_DETACH:
281         break;
282
283     default:
284         break;
285     }
286     return TRUE;
287 }
288
289
290 #if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
291 EXTERN_C PerlInterpreter *
292 perl_clone_host(PerlInterpreter* proto_perl, UV flags) {
293     dTHX;
294     CPerlHost *h;
295     h = new CPerlHost(*(CPerlHost*)PL_sys_intern.internal_host);
296     proto_perl = perl_clone_using(proto_perl, flags,
297                         h->m_pHostperlMem,
298                         h->m_pHostperlMemShared,
299                         h->m_pHostperlMemParse,
300                         h->m_pHostperlEnv,
301                         h->m_pHostperlStdIO,
302                         h->m_pHostperlLIO,
303                         h->m_pHostperlDir,
304                         h->m_pHostperlSock,
305                         h->m_pHostperlProc
306     );
307     proto_perl->Isys_intern.internal_host = h;
308     h->host_perl  = proto_perl;
309     return proto_perl;
310         
311 }
312 #endif