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