3 * (c) 1999 Microsoft Corporation. All rights reserved.
4 * Portions (c) 1999 ActiveState Tool Corp, http://www.ActiveState.com/
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
11 #define CHECK_HOST_INTERP
14 #ifndef ___PerlHost_H___
15 #define ___PerlHost_H___
24 #ifndef WC_NO_BEST_FIT_CHARS
25 # define WC_NO_BEST_FIT_CHARS 0x00000400
29 extern char * g_win32_get_privlib(const char *pl);
30 extern char * g_win32_get_sitelib(const char *pl);
31 extern char * g_win32_get_vendorlib(const char *pl);
32 extern char * g_getlogin(void);
40 CPerlHost(struct IPerlMem** ppMem, struct IPerlMem** ppMemShared,
41 struct IPerlMem** ppMemParse, struct IPerlEnv** ppEnv,
42 struct IPerlStdIO** ppStdIO, struct IPerlLIO** ppLIO,
43 struct IPerlDir** ppDir, struct IPerlSock** ppSock,
44 struct IPerlProc** ppProc);
45 CPerlHost(CPerlHost& host);
48 static CPerlHost* IPerlMem2Host(struct IPerlMem* piPerl);
49 static CPerlHost* IPerlMemShared2Host(struct IPerlMem* piPerl);
50 static CPerlHost* IPerlMemParse2Host(struct IPerlMem* piPerl);
51 static CPerlHost* IPerlEnv2Host(struct IPerlEnv* piPerl);
52 static CPerlHost* IPerlStdIO2Host(struct IPerlStdIO* piPerl);
53 static CPerlHost* IPerlLIO2Host(struct IPerlLIO* piPerl);
54 static CPerlHost* IPerlDir2Host(struct IPerlDir* piPerl);
55 static CPerlHost* IPerlSock2Host(struct IPerlSock* piPerl);
56 static CPerlHost* IPerlProc2Host(struct IPerlProc* piPerl);
58 BOOL PerlCreate(void);
59 int PerlParse(int argc, char** argv, char** env);
61 void PerlDestroy(void);
64 /* Locks provided but should be unnecessary as this is private pool */
65 inline void* Malloc(size_t size) { return m_pVMem->Malloc(size); };
66 inline void* Realloc(void* ptr, size_t size) { return m_pVMem->Realloc(ptr, size); };
67 inline void Free(void* ptr) { m_pVMem->Free(ptr); };
68 inline void* Calloc(size_t num, size_t size)
70 size_t count = num*size;
71 void* lpVoid = Malloc(count);
73 ZeroMemory(lpVoid, count);
76 inline void GetLock(void) { m_pVMem->GetLock(); };
77 inline void FreeLock(void) { m_pVMem->FreeLock(); };
78 inline int IsLocked(void) { return m_pVMem->IsLocked(); };
81 /* Locks used to serialize access to the pool */
82 inline void GetLockShared(void) { m_pVMemShared->GetLock(); };
83 inline void FreeLockShared(void) { m_pVMemShared->FreeLock(); };
84 inline int IsLockedShared(void) { return m_pVMemShared->IsLocked(); };
85 inline void* MallocShared(size_t size)
89 result = m_pVMemShared->Malloc(size);
93 inline void* ReallocShared(void* ptr, size_t size)
97 result = m_pVMemShared->Realloc(ptr, size);
101 inline void FreeShared(void* ptr)
104 m_pVMemShared->Free(ptr);
107 inline void* CallocShared(size_t num, size_t size)
109 size_t count = num*size;
110 void* lpVoid = MallocShared(count);
112 ZeroMemory(lpVoid, count);
117 /* Assume something else is using locks to mangaging serialize
120 inline void GetLockParse(void) { m_pVMemParse->GetLock(); };
121 inline void FreeLockParse(void) { m_pVMemParse->FreeLock(); };
122 inline int IsLockedParse(void) { return m_pVMemParse->IsLocked(); };
123 inline void* MallocParse(size_t size) { return m_pVMemParse->Malloc(size); };
124 inline void* ReallocParse(void* ptr, size_t size) { return m_pVMemParse->Realloc(ptr, size); };
125 inline void FreeParse(void* ptr) { m_pVMemParse->Free(ptr); };
126 inline void* CallocParse(size_t num, size_t size)
128 size_t count = num*size;
129 void* lpVoid = MallocParse(count);
131 ZeroMemory(lpVoid, count);
136 char *Getenv(const char *varname);
137 int Putenv(const char *envstring);
138 inline char *Getenv(const char *varname, unsigned long *len)
141 char *e = Getenv(varname);
146 void* CreateChildEnv(void) { return CreateLocalEnvironmentStrings(*m_pvDir); };
147 void FreeChildEnv(void* pStr) { FreeLocalEnvironmentStrings((char*)pStr); };
148 char* GetChildDir(void);
149 void FreeChildDir(char* pStr);
153 inline LPSTR GetIndex(DWORD &dwIndex)
155 if(dwIndex < m_dwEnvCount)
158 return m_lppEnvList[dwIndex-1];
164 LPSTR Find(LPCSTR lpStr);
165 void Add(LPCSTR lpStr);
167 LPSTR CreateLocalEnvironmentStrings(VDir &vDir);
168 void FreeLocalEnvironmentStrings(LPSTR lpStr);
169 LPSTR* Lookup(LPCSTR lpStr);
170 DWORD CalculateEnvironmentSpace(void);
175 virtual int Chdir(const char *dirname);
179 void Exit(int status);
180 void _Exit(int status);
181 int Execl(const char *cmdname, const char *arg0, const char *arg1, const char *arg2, const char *arg3);
182 int Execv(const char *cmdname, const char *const *argv);
183 int Execvp(const char *cmdname, const char *const *argv);
185 inline VMem* GetMemShared(void) { m_pVMemShared->AddRef(); return m_pVMemShared; };
186 inline VMem* GetMemParse(void) { m_pVMemParse->AddRef(); return m_pVMemParse; };
187 inline VDir* GetDir(void) { return m_pvDir; };
191 struct IPerlMem m_hostperlMem;
192 struct IPerlMem m_hostperlMemShared;
193 struct IPerlMem m_hostperlMemParse;
194 struct IPerlEnv m_hostperlEnv;
195 struct IPerlStdIO m_hostperlStdIO;
196 struct IPerlLIO m_hostperlLIO;
197 struct IPerlDir m_hostperlDir;
198 struct IPerlSock m_hostperlSock;
199 struct IPerlProc m_hostperlProc;
201 struct IPerlMem* m_pHostperlMem;
202 struct IPerlMem* m_pHostperlMemShared;
203 struct IPerlMem* m_pHostperlMemParse;
204 struct IPerlEnv* m_pHostperlEnv;
205 struct IPerlStdIO* m_pHostperlStdIO;
206 struct IPerlLIO* m_pHostperlLIO;
207 struct IPerlDir* m_pHostperlDir;
208 struct IPerlSock* m_pHostperlSock;
209 struct IPerlProc* m_pHostperlProc;
211 inline char* MapPathA(const char *pInName) { return m_pvDir->MapPathA(pInName); };
212 inline WCHAR* MapPathW(const WCHAR *pInName) { return m_pvDir->MapPathW(pInName); };
222 BOOL m_bTopLevel; // is this a toplevel host?
223 static long num_hosts;
225 inline int LastHost(void) { return num_hosts == 1L; };
226 struct interpreter *host_perl;
229 long CPerlHost::num_hosts = 0L;
231 extern "C" void win32_checkTLS(struct interpreter *host_perl);
233 #define STRUCT2RAWPTR(x, y) (CPerlHost*)(((LPBYTE)x)-offsetof(CPerlHost, y))
234 #ifdef CHECK_HOST_INTERP
235 inline CPerlHost* CheckInterp(CPerlHost *host)
237 win32_checkTLS(host->host_perl);
240 #define STRUCT2PTR(x, y) CheckInterp(STRUCT2RAWPTR(x, y))
242 #define STRUCT2PTR(x, y) STRUCT2RAWPTR(x, y)
245 inline CPerlHost* IPerlMem2Host(struct IPerlMem* piPerl)
247 return STRUCT2RAWPTR(piPerl, m_hostperlMem);
250 inline CPerlHost* IPerlMemShared2Host(struct IPerlMem* piPerl)
252 return STRUCT2RAWPTR(piPerl, m_hostperlMemShared);
255 inline CPerlHost* IPerlMemParse2Host(struct IPerlMem* piPerl)
257 return STRUCT2RAWPTR(piPerl, m_hostperlMemParse);
260 inline CPerlHost* IPerlEnv2Host(struct IPerlEnv* piPerl)
262 return STRUCT2PTR(piPerl, m_hostperlEnv);
265 inline CPerlHost* IPerlStdIO2Host(struct IPerlStdIO* piPerl)
267 return STRUCT2PTR(piPerl, m_hostperlStdIO);
270 inline CPerlHost* IPerlLIO2Host(struct IPerlLIO* piPerl)
272 return STRUCT2PTR(piPerl, m_hostperlLIO);
275 inline CPerlHost* IPerlDir2Host(struct IPerlDir* piPerl)
277 return STRUCT2PTR(piPerl, m_hostperlDir);
280 inline CPerlHost* IPerlSock2Host(struct IPerlSock* piPerl)
282 return STRUCT2PTR(piPerl, m_hostperlSock);
285 inline CPerlHost* IPerlProc2Host(struct IPerlProc* piPerl)
287 return STRUCT2PTR(piPerl, m_hostperlProc);
293 #define IPERL2HOST(x) IPerlMem2Host(x)
297 PerlMemMalloc(struct IPerlMem* piPerl, size_t size)
299 return IPERL2HOST(piPerl)->Malloc(size);
302 PerlMemRealloc(struct IPerlMem* piPerl, void* ptr, size_t size)
304 return IPERL2HOST(piPerl)->Realloc(ptr, size);
307 PerlMemFree(struct IPerlMem* piPerl, void* ptr)
309 IPERL2HOST(piPerl)->Free(ptr);
312 PerlMemCalloc(struct IPerlMem* piPerl, size_t num, size_t size)
314 return IPERL2HOST(piPerl)->Calloc(num, size);
318 PerlMemGetLock(struct IPerlMem* piPerl)
320 IPERL2HOST(piPerl)->GetLock();
324 PerlMemFreeLock(struct IPerlMem* piPerl)
326 IPERL2HOST(piPerl)->FreeLock();
330 PerlMemIsLocked(struct IPerlMem* piPerl)
332 return IPERL2HOST(piPerl)->IsLocked();
335 struct IPerlMem perlMem =
347 #define IPERL2HOST(x) IPerlMemShared2Host(x)
351 PerlMemSharedMalloc(struct IPerlMem* piPerl, size_t size)
353 return IPERL2HOST(piPerl)->MallocShared(size);
356 PerlMemSharedRealloc(struct IPerlMem* piPerl, void* ptr, size_t size)
358 return IPERL2HOST(piPerl)->ReallocShared(ptr, size);
361 PerlMemSharedFree(struct IPerlMem* piPerl, void* ptr)
363 IPERL2HOST(piPerl)->FreeShared(ptr);
366 PerlMemSharedCalloc(struct IPerlMem* piPerl, size_t num, size_t size)
368 return IPERL2HOST(piPerl)->CallocShared(num, size);
372 PerlMemSharedGetLock(struct IPerlMem* piPerl)
374 IPERL2HOST(piPerl)->GetLockShared();
378 PerlMemSharedFreeLock(struct IPerlMem* piPerl)
380 IPERL2HOST(piPerl)->FreeLockShared();
384 PerlMemSharedIsLocked(struct IPerlMem* piPerl)
386 return IPERL2HOST(piPerl)->IsLockedShared();
389 struct IPerlMem perlMemShared =
392 PerlMemSharedRealloc,
395 PerlMemSharedGetLock,
396 PerlMemSharedFreeLock,
397 PerlMemSharedIsLocked,
401 #define IPERL2HOST(x) IPerlMemParse2Host(x)
405 PerlMemParseMalloc(struct IPerlMem* piPerl, size_t size)
407 return IPERL2HOST(piPerl)->MallocParse(size);
410 PerlMemParseRealloc(struct IPerlMem* piPerl, void* ptr, size_t size)
412 return IPERL2HOST(piPerl)->ReallocParse(ptr, size);
415 PerlMemParseFree(struct IPerlMem* piPerl, void* ptr)
417 IPERL2HOST(piPerl)->FreeParse(ptr);
420 PerlMemParseCalloc(struct IPerlMem* piPerl, size_t num, size_t size)
422 return IPERL2HOST(piPerl)->CallocParse(num, size);
426 PerlMemParseGetLock(struct IPerlMem* piPerl)
428 IPERL2HOST(piPerl)->GetLockParse();
432 PerlMemParseFreeLock(struct IPerlMem* piPerl)
434 IPERL2HOST(piPerl)->FreeLockParse();
438 PerlMemParseIsLocked(struct IPerlMem* piPerl)
440 return IPERL2HOST(piPerl)->IsLockedParse();
443 struct IPerlMem perlMemParse =
450 PerlMemParseFreeLock,
451 PerlMemParseIsLocked,
456 #define IPERL2HOST(x) IPerlEnv2Host(x)
460 PerlEnvGetenv(struct IPerlEnv* piPerl, const char *varname)
462 return IPERL2HOST(piPerl)->Getenv(varname);
466 PerlEnvPutenv(struct IPerlEnv* piPerl, const char *envstring)
468 return IPERL2HOST(piPerl)->Putenv(envstring);
472 PerlEnvGetenv_len(struct IPerlEnv* piPerl, const char* varname, unsigned long* len)
474 return IPERL2HOST(piPerl)->Getenv(varname, len);
478 PerlEnvUname(struct IPerlEnv* piPerl, struct utsname *name)
480 return win32_uname(name);
484 PerlEnvClearenv(struct IPerlEnv* piPerl)
486 IPERL2HOST(piPerl)->Clearenv();
490 PerlEnvGetChildenv(struct IPerlEnv* piPerl)
492 return IPERL2HOST(piPerl)->CreateChildEnv();
496 PerlEnvFreeChildenv(struct IPerlEnv* piPerl, void* childEnv)
498 IPERL2HOST(piPerl)->FreeChildEnv(childEnv);
502 PerlEnvGetChilddir(struct IPerlEnv* piPerl)
504 return IPERL2HOST(piPerl)->GetChildDir();
508 PerlEnvFreeChilddir(struct IPerlEnv* piPerl, char* childDir)
510 IPERL2HOST(piPerl)->FreeChildDir(childDir);
514 PerlEnvOsId(struct IPerlEnv* piPerl)
516 return win32_os_id();
520 PerlEnvLibPath(struct IPerlEnv* piPerl, const char *pl)
522 return g_win32_get_privlib(pl);
526 PerlEnvSiteLibPath(struct IPerlEnv* piPerl, const char *pl)
528 return g_win32_get_sitelib(pl);
532 PerlEnvVendorLibPath(struct IPerlEnv* piPerl, const char *pl)
534 return g_win32_get_vendorlib(pl);
538 PerlEnvGetChildIO(struct IPerlEnv* piPerl, child_IO_table* ptr)
540 win32_get_child_IO(ptr);
543 struct IPerlEnv perlEnv =
557 PerlEnvVendorLibPath,
562 #define IPERL2HOST(x) IPerlStdIO2Host(x)
566 PerlStdIOStdin(struct IPerlStdIO* piPerl)
568 return win32_stdin();
572 PerlStdIOStdout(struct IPerlStdIO* piPerl)
574 return win32_stdout();
578 PerlStdIOStderr(struct IPerlStdIO* piPerl)
580 return win32_stderr();
584 PerlStdIOOpen(struct IPerlStdIO* piPerl, const char *path, const char *mode)
586 return win32_fopen(path, mode);
590 PerlStdIOClose(struct IPerlStdIO* piPerl, FILE* pf)
592 return win32_fclose((pf));
596 PerlStdIOEof(struct IPerlStdIO* piPerl, FILE* pf)
598 return win32_feof(pf);
602 PerlStdIOError(struct IPerlStdIO* piPerl, FILE* pf)
604 return win32_ferror(pf);
608 PerlStdIOClearerr(struct IPerlStdIO* piPerl, FILE* pf)
614 PerlStdIOGetc(struct IPerlStdIO* piPerl, FILE* pf)
616 return win32_getc(pf);
620 PerlStdIOGetBase(struct IPerlStdIO* piPerl, FILE* pf)
631 PerlStdIOGetBufsiz(struct IPerlStdIO* piPerl, FILE* pf)
635 return FILE_bufsiz(f);
642 PerlStdIOGetCnt(struct IPerlStdIO* piPerl, FILE* pf)
653 PerlStdIOGetPtr(struct IPerlStdIO* piPerl, FILE* pf)
664 PerlStdIOGets(struct IPerlStdIO* piPerl, FILE* pf, char* s, int n)
666 return win32_fgets(s, n, pf);
670 PerlStdIOPutc(struct IPerlStdIO* piPerl, FILE* pf, int c)
672 return win32_fputc(c, pf);
676 PerlStdIOPuts(struct IPerlStdIO* piPerl, FILE* pf, const char *s)
678 return win32_fputs(s, pf);
682 PerlStdIOFlush(struct IPerlStdIO* piPerl, FILE* pf)
684 return win32_fflush(pf);
688 PerlStdIOUngetc(struct IPerlStdIO* piPerl,int c, FILE* pf)
690 return win32_ungetc(c, pf);
694 PerlStdIOFileno(struct IPerlStdIO* piPerl, FILE* pf)
696 return win32_fileno(pf);
700 PerlStdIOFdopen(struct IPerlStdIO* piPerl, int fd, const char *mode)
702 return win32_fdopen(fd, mode);
706 PerlStdIOReopen(struct IPerlStdIO* piPerl, const char*path, const char*mode, FILE* pf)
708 return win32_freopen(path, mode, (FILE*)pf);
712 PerlStdIORead(struct IPerlStdIO* piPerl, void *buffer, Size_t size, Size_t count, FILE* pf)
714 return win32_fread(buffer, size, count, pf);
718 PerlStdIOWrite(struct IPerlStdIO* piPerl, const void *buffer, Size_t size, Size_t count, FILE* pf)
720 return win32_fwrite(buffer, size, count, pf);
724 PerlStdIOSetBuf(struct IPerlStdIO* piPerl, FILE* pf, char* buffer)
726 win32_setbuf(pf, buffer);
730 PerlStdIOSetVBuf(struct IPerlStdIO* piPerl, FILE* pf, char* buffer, int type, Size_t size)
732 return win32_setvbuf(pf, buffer, type, size);
736 PerlStdIOSetCnt(struct IPerlStdIO* piPerl, FILE* pf, int n)
738 #ifdef STDIO_CNT_LVALUE
745 PerlStdIOSetPtr(struct IPerlStdIO* piPerl, FILE* pf, char * ptr)
747 #ifdef STDIO_PTR_LVALUE
754 PerlStdIOSetlinebuf(struct IPerlStdIO* piPerl, FILE* pf)
756 win32_setvbuf(pf, NULL, _IOLBF, 0);
760 PerlStdIOPrintf(struct IPerlStdIO* piPerl, FILE* pf, const char *format,...)
763 va_start(arglist, format);
764 return win32_vfprintf(pf, format, arglist);
768 PerlStdIOVprintf(struct IPerlStdIO* piPerl, FILE* pf, const char *format, va_list arglist)
770 return win32_vfprintf(pf, format, arglist);
774 PerlStdIOTell(struct IPerlStdIO* piPerl, FILE* pf)
776 return win32_ftell(pf);
780 PerlStdIOSeek(struct IPerlStdIO* piPerl, FILE* pf, Off_t offset, int origin)
782 return win32_fseek(pf, offset, origin);
786 PerlStdIORewind(struct IPerlStdIO* piPerl, FILE* pf)
792 PerlStdIOTmpfile(struct IPerlStdIO* piPerl)
794 return win32_tmpfile();
798 PerlStdIOGetpos(struct IPerlStdIO* piPerl, FILE* pf, Fpos_t *p)
800 return win32_fgetpos(pf, p);
804 PerlStdIOSetpos(struct IPerlStdIO* piPerl, FILE* pf, const Fpos_t *p)
806 return win32_fsetpos(pf, p);
809 PerlStdIOInit(struct IPerlStdIO* piPerl)
814 PerlStdIOInitOSExtras(struct IPerlStdIO* piPerl)
816 Perl_init_os_extras();
820 PerlStdIOOpenOSfhandle(struct IPerlStdIO* piPerl, intptr_t osfhandle, int flags)
822 return win32_open_osfhandle(osfhandle, flags);
826 PerlStdIOGetOSfhandle(struct IPerlStdIO* piPerl, int filenum)
828 return win32_get_osfhandle(filenum);
832 PerlStdIOFdupopen(struct IPerlStdIO* piPerl, FILE* pf)
838 int fileno = win32_dup(win32_fileno(pf));
840 /* open the file in the same mode */
842 if((pf)->flags & _F_READ) {
846 else if((pf)->flags & _F_WRIT) {
850 else if((pf)->flags & _F_RDWR) {
856 if((pf)->_flag & _IOREAD) {
860 else if((pf)->_flag & _IOWRT) {
864 else if((pf)->_flag & _IORW) {
871 /* it appears that the binmode is attached to the
872 * file descriptor so binmode files will be handled
875 pfdup = win32_fdopen(fileno, mode);
877 /* move the file pointer to the same position */
878 if (!fgetpos(pf, &pos)) {
879 fsetpos(pfdup, &pos);
887 struct IPerlStdIO perlStdIO =
926 PerlStdIOInitOSExtras,
932 #define IPERL2HOST(x) IPerlLIO2Host(x)
936 PerlLIOAccess(struct IPerlLIO* piPerl, const char *path, int mode)
938 return win32_access(path, mode);
942 PerlLIOChmod(struct IPerlLIO* piPerl, const char *filename, int pmode)
944 return win32_chmod(filename, pmode);
948 PerlLIOChown(struct IPerlLIO* piPerl, const char *filename, uid_t owner, gid_t group)
950 return chown(filename, owner, group);
954 PerlLIOChsize(struct IPerlLIO* piPerl, int handle, Off_t size)
956 return win32_chsize(handle, size);
960 PerlLIOClose(struct IPerlLIO* piPerl, int handle)
962 return win32_close(handle);
966 PerlLIODup(struct IPerlLIO* piPerl, int handle)
968 return win32_dup(handle);
972 PerlLIODup2(struct IPerlLIO* piPerl, int handle1, int handle2)
974 return win32_dup2(handle1, handle2);
978 PerlLIOFlock(struct IPerlLIO* piPerl, int fd, int oper)
980 return win32_flock(fd, oper);
984 PerlLIOFileStat(struct IPerlLIO* piPerl, int handle, Stat_t *buffer)
986 return win32_fstat(handle, buffer);
990 PerlLIOIOCtl(struct IPerlLIO* piPerl, int i, unsigned int u, char *data)
995 /* mauke says using memcpy avoids alignment issues */
996 memcpy(&u_long_arg, data, sizeof u_long_arg);
997 retval = win32_ioctlsocket((SOCKET)i, (long)u, &u_long_arg);
998 memcpy(data, &u_long_arg, sizeof u_long_arg);
1003 PerlLIOIsatty(struct IPerlLIO* piPerl, int fd)
1009 PerlLIOLink(struct IPerlLIO* piPerl, const char*oldname, const char *newname)
1011 return win32_link(oldname, newname);
1015 PerlLIOLseek(struct IPerlLIO* piPerl, int handle, Off_t offset, int origin)
1017 return win32_lseek(handle, offset, origin);
1021 PerlLIOLstat(struct IPerlLIO* piPerl, const char *path, Stat_t *buffer)
1023 return win32_stat(path, buffer);
1027 PerlLIOMktemp(struct IPerlLIO* piPerl, char *Template)
1029 return mktemp(Template);
1033 PerlLIOOpen(struct IPerlLIO* piPerl, const char *filename, int oflag)
1035 return win32_open(filename, oflag);
1039 PerlLIOOpen3(struct IPerlLIO* piPerl, const char *filename, int oflag, int pmode)
1041 return win32_open(filename, oflag, pmode);
1045 PerlLIORead(struct IPerlLIO* piPerl, int handle, void *buffer, unsigned int count)
1047 return win32_read(handle, buffer, count);
1051 PerlLIORename(struct IPerlLIO* piPerl, const char *OldFileName, const char *newname)
1053 return win32_rename(OldFileName, newname);
1057 PerlLIOSetmode(struct IPerlLIO* piPerl, int handle, int mode)
1059 return win32_setmode(handle, mode);
1063 PerlLIONameStat(struct IPerlLIO* piPerl, const char *path, Stat_t *buffer)
1065 return win32_stat(path, buffer);
1069 PerlLIOTmpnam(struct IPerlLIO* piPerl, char *string)
1071 return tmpnam(string);
1075 PerlLIOUmask(struct IPerlLIO* piPerl, int pmode)
1077 return umask(pmode);
1081 PerlLIOUnlink(struct IPerlLIO* piPerl, const char *filename)
1083 return win32_unlink(filename);
1087 PerlLIOUtime(struct IPerlLIO* piPerl, const char *filename, struct utimbuf *times)
1089 return win32_utime(filename, times);
1093 PerlLIOWrite(struct IPerlLIO* piPerl, int handle, const void *buffer, unsigned int count)
1095 return win32_write(handle, buffer, count);
1098 struct IPerlLIO perlLIO =
1130 #define IPERL2HOST(x) IPerlDir2Host(x)
1134 PerlDirMakedir(struct IPerlDir* piPerl, const char *dirname, int mode)
1136 return win32_mkdir(dirname, mode);
1140 PerlDirChdir(struct IPerlDir* piPerl, const char *dirname)
1142 return IPERL2HOST(piPerl)->Chdir(dirname);
1146 PerlDirRmdir(struct IPerlDir* piPerl, const char *dirname)
1148 return win32_rmdir(dirname);
1152 PerlDirClose(struct IPerlDir* piPerl, DIR *dirp)
1154 return win32_closedir(dirp);
1158 PerlDirOpen(struct IPerlDir* piPerl, const char *filename)
1160 return win32_opendir(filename);
1164 PerlDirRead(struct IPerlDir* piPerl, DIR *dirp)
1166 return win32_readdir(dirp);
1170 PerlDirRewind(struct IPerlDir* piPerl, DIR *dirp)
1172 win32_rewinddir(dirp);
1176 PerlDirSeek(struct IPerlDir* piPerl, DIR *dirp, long loc)
1178 win32_seekdir(dirp, loc);
1182 PerlDirTell(struct IPerlDir* piPerl, DIR *dirp)
1184 return win32_telldir(dirp);
1188 PerlDirMapPathA(struct IPerlDir* piPerl, const char* path)
1190 return IPERL2HOST(piPerl)->MapPathA(path);
1194 PerlDirMapPathW(struct IPerlDir* piPerl, const WCHAR* path)
1196 return IPERL2HOST(piPerl)->MapPathW(path);
1199 struct IPerlDir perlDir =
1217 PerlSockHtonl(struct IPerlSock* piPerl, u_long hostlong)
1219 return win32_htonl(hostlong);
1223 PerlSockHtons(struct IPerlSock* piPerl, u_short hostshort)
1225 return win32_htons(hostshort);
1229 PerlSockNtohl(struct IPerlSock* piPerl, u_long netlong)
1231 return win32_ntohl(netlong);
1235 PerlSockNtohs(struct IPerlSock* piPerl, u_short netshort)
1237 return win32_ntohs(netshort);
1240 SOCKET PerlSockAccept(struct IPerlSock* piPerl, SOCKET s, struct sockaddr* addr, int* addrlen)
1242 return win32_accept(s, addr, addrlen);
1246 PerlSockBind(struct IPerlSock* piPerl, SOCKET s, const struct sockaddr* name, int namelen)
1248 return win32_bind(s, name, namelen);
1252 PerlSockConnect(struct IPerlSock* piPerl, SOCKET s, const struct sockaddr* name, int namelen)
1254 return win32_connect(s, name, namelen);
1258 PerlSockEndhostent(struct IPerlSock* piPerl)
1264 PerlSockEndnetent(struct IPerlSock* piPerl)
1270 PerlSockEndprotoent(struct IPerlSock* piPerl)
1272 win32_endprotoent();
1276 PerlSockEndservent(struct IPerlSock* piPerl)
1282 PerlSockGethostbyaddr(struct IPerlSock* piPerl, const char* addr, int len, int type)
1284 return win32_gethostbyaddr(addr, len, type);
1288 PerlSockGethostbyname(struct IPerlSock* piPerl, const char* name)
1290 return win32_gethostbyname(name);
1294 PerlSockGethostent(struct IPerlSock* piPerl)
1297 Perl_croak(aTHX_ "gethostent not implemented!\n");
1302 PerlSockGethostname(struct IPerlSock* piPerl, char* name, int namelen)
1304 return win32_gethostname(name, namelen);
1308 PerlSockGetnetbyaddr(struct IPerlSock* piPerl, long net, int type)
1310 return win32_getnetbyaddr(net, type);
1314 PerlSockGetnetbyname(struct IPerlSock* piPerl, const char *name)
1316 return win32_getnetbyname((char*)name);
1320 PerlSockGetnetent(struct IPerlSock* piPerl)
1322 return win32_getnetent();
1325 int PerlSockGetpeername(struct IPerlSock* piPerl, SOCKET s, struct sockaddr* name, int* namelen)
1327 return win32_getpeername(s, name, namelen);
1331 PerlSockGetprotobyname(struct IPerlSock* piPerl, const char* name)
1333 return win32_getprotobyname(name);
1337 PerlSockGetprotobynumber(struct IPerlSock* piPerl, int number)
1339 return win32_getprotobynumber(number);
1343 PerlSockGetprotoent(struct IPerlSock* piPerl)
1345 return win32_getprotoent();
1349 PerlSockGetservbyname(struct IPerlSock* piPerl, const char* name, const char* proto)
1351 return win32_getservbyname(name, proto);
1355 PerlSockGetservbyport(struct IPerlSock* piPerl, int port, const char* proto)
1357 return win32_getservbyport(port, proto);
1361 PerlSockGetservent(struct IPerlSock* piPerl)
1363 return win32_getservent();
1367 PerlSockGetsockname(struct IPerlSock* piPerl, SOCKET s, struct sockaddr* name, int* namelen)
1369 return win32_getsockname(s, name, namelen);
1373 PerlSockGetsockopt(struct IPerlSock* piPerl, SOCKET s, int level, int optname, char* optval, int* optlen)
1375 return win32_getsockopt(s, level, optname, optval, optlen);
1379 PerlSockInetAddr(struct IPerlSock* piPerl, const char* cp)
1381 return win32_inet_addr(cp);
1385 PerlSockInetNtoa(struct IPerlSock* piPerl, struct in_addr in)
1387 return win32_inet_ntoa(in);
1391 PerlSockListen(struct IPerlSock* piPerl, SOCKET s, int backlog)
1393 return win32_listen(s, backlog);
1397 PerlSockRecv(struct IPerlSock* piPerl, SOCKET s, char* buffer, int len, int flags)
1399 return win32_recv(s, buffer, len, flags);
1403 PerlSockRecvfrom(struct IPerlSock* piPerl, SOCKET s, char* buffer, int len, int flags, struct sockaddr* from, int* fromlen)
1405 return win32_recvfrom(s, buffer, len, flags, from, fromlen);
1409 PerlSockSelect(struct IPerlSock* piPerl, int nfds, char* readfds, char* writefds, char* exceptfds, const struct timeval* timeout)
1411 return win32_select(nfds, (Perl_fd_set*)readfds, (Perl_fd_set*)writefds, (Perl_fd_set*)exceptfds, timeout);
1415 PerlSockSend(struct IPerlSock* piPerl, SOCKET s, const char* buffer, int len, int flags)
1417 return win32_send(s, buffer, len, flags);
1421 PerlSockSendto(struct IPerlSock* piPerl, SOCKET s, const char* buffer, int len, int flags, const struct sockaddr* to, int tolen)
1423 return win32_sendto(s, buffer, len, flags, to, tolen);
1427 PerlSockSethostent(struct IPerlSock* piPerl, int stayopen)
1429 win32_sethostent(stayopen);
1433 PerlSockSetnetent(struct IPerlSock* piPerl, int stayopen)
1435 win32_setnetent(stayopen);
1439 PerlSockSetprotoent(struct IPerlSock* piPerl, int stayopen)
1441 win32_setprotoent(stayopen);
1445 PerlSockSetservent(struct IPerlSock* piPerl, int stayopen)
1447 win32_setservent(stayopen);
1451 PerlSockSetsockopt(struct IPerlSock* piPerl, SOCKET s, int level, int optname, const char* optval, int optlen)
1453 return win32_setsockopt(s, level, optname, optval, optlen);
1457 PerlSockShutdown(struct IPerlSock* piPerl, SOCKET s, int how)
1459 return win32_shutdown(s, how);
1463 PerlSockSocket(struct IPerlSock* piPerl, int af, int type, int protocol)
1465 return win32_socket(af, type, protocol);
1469 PerlSockSocketpair(struct IPerlSock* piPerl, int domain, int type, int protocol, int* fds)
1471 return Perl_my_socketpair(domain, type, protocol, fds);
1475 PerlSockClosesocket(struct IPerlSock* piPerl, SOCKET s)
1477 return win32_closesocket(s);
1481 PerlSockIoctlsocket(struct IPerlSock* piPerl, SOCKET s, long cmd, u_long *argp)
1483 return win32_ioctlsocket(s, cmd, argp);
1486 struct IPerlSock perlSock =
1497 PerlSockEndprotoent,
1499 PerlSockGethostname,
1500 PerlSockGetpeername,
1501 PerlSockGethostbyaddr,
1502 PerlSockGethostbyname,
1504 PerlSockGetnetbyaddr,
1505 PerlSockGetnetbyname,
1507 PerlSockGetprotobyname,
1508 PerlSockGetprotobynumber,
1509 PerlSockGetprotoent,
1510 PerlSockGetservbyname,
1511 PerlSockGetservbyport,
1513 PerlSockGetsockname,
1525 PerlSockSetprotoent,
1531 PerlSockClosesocket,
1537 #define EXECF_EXEC 1
1538 #define EXECF_SPAWN 2
1541 PerlProcAbort(struct IPerlProc* piPerl)
1547 PerlProcCrypt(struct IPerlProc* piPerl, const char* clear, const char* salt)
1549 return win32_crypt(clear, salt);
1553 PerlProcExit(struct IPerlProc* piPerl, int status)
1559 PerlProc_Exit(struct IPerlProc* piPerl, int status)
1565 PerlProcExecl(struct IPerlProc* piPerl, const char *cmdname, const char *arg0, const char *arg1, const char *arg2, const char *arg3)
1567 return execl(cmdname, arg0, arg1, arg2, arg3);
1571 PerlProcExecv(struct IPerlProc* piPerl, const char *cmdname, const char *const *argv)
1573 return win32_execvp(cmdname, argv);
1577 PerlProcExecvp(struct IPerlProc* piPerl, const char *cmdname, const char *const *argv)
1579 return win32_execvp(cmdname, argv);
1583 PerlProcGetuid(struct IPerlProc* piPerl)
1589 PerlProcGeteuid(struct IPerlProc* piPerl)
1595 PerlProcGetgid(struct IPerlProc* piPerl)
1601 PerlProcGetegid(struct IPerlProc* piPerl)
1607 PerlProcGetlogin(struct IPerlProc* piPerl)
1609 return g_getlogin();
1613 PerlProcKill(struct IPerlProc* piPerl, int pid, int sig)
1615 return win32_kill(pid, sig);
1619 PerlProcKillpg(struct IPerlProc* piPerl, int pid, int sig)
1621 return win32_kill(pid, -sig);
1625 PerlProcPauseProc(struct IPerlProc* piPerl)
1627 return win32_sleep((32767L << 16) + 32767);
1631 PerlProcPopen(struct IPerlProc* piPerl, const char *command, const char *mode)
1634 PERL_FLUSHALL_FOR_CHILD;
1635 return win32_popen(command, mode);
1639 PerlProcPopenList(struct IPerlProc* piPerl, const char *mode, IV narg, SV **args)
1642 PERL_FLUSHALL_FOR_CHILD;
1643 return win32_popenlist(mode, narg, args);
1647 PerlProcPclose(struct IPerlProc* piPerl, PerlIO *stream)
1649 return win32_pclose(stream);
1653 PerlProcPipe(struct IPerlProc* piPerl, int *phandles)
1655 return win32_pipe(phandles, 512, O_BINARY);
1659 PerlProcSetuid(struct IPerlProc* piPerl, uid_t u)
1665 PerlProcSetgid(struct IPerlProc* piPerl, gid_t g)
1671 PerlProcSleep(struct IPerlProc* piPerl, unsigned int s)
1673 return win32_sleep(s);
1677 PerlProcTimes(struct IPerlProc* piPerl, struct tms *timebuf)
1679 return win32_times(timebuf);
1683 PerlProcWait(struct IPerlProc* piPerl, int *status)
1685 return win32_wait(status);
1689 PerlProcWaitpid(struct IPerlProc* piPerl, int pid, int *status, int flags)
1691 return win32_waitpid(pid, status, flags);
1695 PerlProcSignal(struct IPerlProc* piPerl, int sig, Sighandler_t subcode)
1697 return win32_signal(sig, subcode);
1701 PerlProcGetTimeOfDay(struct IPerlProc* piPerl, struct timeval *t, void *z)
1703 return win32_gettimeofday(t, z);
1707 static THREAD_RET_TYPE
1708 win32_start_child(LPVOID arg)
1710 PerlInterpreter *my_perl = (PerlInterpreter*)arg;
1713 HWND parent_message_hwnd;
1714 #ifdef PERL_SYNC_FORK
1715 static long sync_fork_id = 0;
1716 long id = ++sync_fork_id;
1720 PERL_SET_THX(my_perl);
1721 win32_checkTLS(my_perl);
1723 /* set $$ to pseudo id */
1724 #ifdef PERL_SYNC_FORK
1727 w32_pseudo_id = GetCurrentThreadId();
1729 int pid = (int)w32_pseudo_id;
1731 w32_pseudo_id = -pid;
1734 if (tmpgv = gv_fetchpv("$", TRUE, SVt_PV)) {
1735 SV *sv = GvSV(tmpgv);
1737 sv_setiv(sv, -(IV)w32_pseudo_id);
1740 #ifdef PERL_USES_PL_PIDSTATUS
1741 hv_clear(PL_pidstatus);
1744 /* create message window and tell parent about it */
1745 parent_message_hwnd = w32_message_hwnd;
1746 w32_message_hwnd = win32_create_message_window();
1747 if (parent_message_hwnd != NULL)
1748 PostMessage(parent_message_hwnd, WM_USER_MESSAGE, w32_pseudo_id, (LONG)w32_message_hwnd);
1750 /* push a zero on the stack (we are the child) */
1758 /* continue from next op */
1759 PL_op = PL_op->op_next;
1763 volatile int oldscope = PL_scopestack_ix;
1766 JMPENV_PUSH(status);
1773 while (PL_scopestack_ix > oldscope)
1776 PL_curstash = PL_defstash;
1777 if (PL_endav && !PL_minus_c)
1778 call_list(oldscope, PL_endav);
1779 status = STATUS_EXIT;
1783 POPSTACK_TO(PL_mainstack);
1784 PL_op = PL_restartop;
1785 PL_restartop = Nullop;
1788 PerlIO_printf(Perl_error_log, "panic: restartop\n");
1795 /* XXX hack to avoid perl_destruct() freeing optree */
1796 win32_checkTLS(my_perl);
1797 PL_main_root = Nullop;
1800 win32_checkTLS(my_perl);
1801 /* close the std handles to avoid fd leaks */
1803 do_close(PL_stdingv, FALSE);
1804 do_close(gv_fetchpv("STDOUT", TRUE, SVt_PVIO), FALSE); /* PL_stdoutgv - ISAGN */
1805 do_close(PL_stderrgv, FALSE);
1808 /* destroy everything (waits for any pseudo-forked children) */
1809 win32_checkTLS(my_perl);
1810 perl_destruct(my_perl);
1811 win32_checkTLS(my_perl);
1814 #ifdef PERL_SYNC_FORK
1817 return (DWORD)status;
1820 #endif /* USE_ITHREADS */
1823 PerlProcFork(struct IPerlProc* piPerl)
1831 if (w32_num_pseudo_children >= MAXIMUM_WAIT_OBJECTS) {
1835 h = new CPerlHost(*(CPerlHost*)w32_internal_host);
1836 PerlInterpreter *new_perl = perl_clone_using((PerlInterpreter*)aTHX, 1,
1838 h->m_pHostperlMemShared,
1839 h->m_pHostperlMemParse,
1841 h->m_pHostperlStdIO,
1847 new_perl->Isys_intern.internal_host = h;
1848 h->host_perl = new_perl;
1849 # ifdef PERL_SYNC_FORK
1850 id = win32_start_child((LPVOID)new_perl);
1853 if (w32_message_hwnd == INVALID_HANDLE_VALUE)
1854 w32_message_hwnd = win32_create_message_window();
1855 new_perl->Isys_intern.message_hwnd = w32_message_hwnd;
1856 w32_pseudo_child_message_hwnds[w32_num_pseudo_children] =
1857 (w32_message_hwnd == NULL) ? (HWND)NULL : (HWND)INVALID_HANDLE_VALUE;
1858 # ifdef USE_RTL_THREAD_API
1859 handle = (HANDLE)_beginthreadex((void*)NULL, 0, win32_start_child,
1860 (void*)new_perl, 0, (unsigned*)&id);
1862 handle = CreateThread(NULL, 0, win32_start_child,
1863 (LPVOID)new_perl, 0, &id);
1865 PERL_SET_THX(aTHX); /* XXX perl_clone*() set TLS */
1875 w32_pseudo_child_handles[w32_num_pseudo_children] = handle;
1876 w32_pseudo_child_pids[w32_num_pseudo_children] = id;
1877 ++w32_num_pseudo_children;
1881 Perl_croak(aTHX_ "fork() not implemented!\n");
1883 #endif /* USE_ITHREADS */
1887 PerlProcGetpid(struct IPerlProc* piPerl)
1889 return win32_getpid();
1893 PerlProcDynaLoader(struct IPerlProc* piPerl, const char* filename)
1895 return win32_dynaload(filename);
1899 PerlProcGetOSError(struct IPerlProc* piPerl, SV* sv, DWORD dwErr)
1901 win32_str_os_error(sv, dwErr);
1905 PerlProcSpawnvp(struct IPerlProc* piPerl, int mode, const char *cmdname, const char *const *argv)
1907 return win32_spawnvp(mode, cmdname, argv);
1911 PerlProcLastHost(struct IPerlProc* piPerl)
1914 CPerlHost *h = (CPerlHost*)w32_internal_host;
1915 return h->LastHost();
1918 struct IPerlProc perlProc =
1952 PerlProcGetTimeOfDay
1960 CPerlHost::CPerlHost(void)
1962 /* Construct a host from scratch */
1963 InterlockedIncrement(&num_hosts);
1964 m_pvDir = new VDir();
1965 m_pVMem = new VMem();
1966 m_pVMemShared = new VMem();
1967 m_pVMemParse = new VMem();
1969 m_pvDir->Init(NULL, m_pVMem);
1972 m_lppEnvList = NULL;
1975 CopyMemory(&m_hostperlMem, &perlMem, sizeof(perlMem));
1976 CopyMemory(&m_hostperlMemShared, &perlMemShared, sizeof(perlMemShared));
1977 CopyMemory(&m_hostperlMemParse, &perlMemParse, sizeof(perlMemParse));
1978 CopyMemory(&m_hostperlEnv, &perlEnv, sizeof(perlEnv));
1979 CopyMemory(&m_hostperlStdIO, &perlStdIO, sizeof(perlStdIO));
1980 CopyMemory(&m_hostperlLIO, &perlLIO, sizeof(perlLIO));
1981 CopyMemory(&m_hostperlDir, &perlDir, sizeof(perlDir));
1982 CopyMemory(&m_hostperlSock, &perlSock, sizeof(perlSock));
1983 CopyMemory(&m_hostperlProc, &perlProc, sizeof(perlProc));
1985 m_pHostperlMem = &m_hostperlMem;
1986 m_pHostperlMemShared = &m_hostperlMemShared;
1987 m_pHostperlMemParse = &m_hostperlMemParse;
1988 m_pHostperlEnv = &m_hostperlEnv;
1989 m_pHostperlStdIO = &m_hostperlStdIO;
1990 m_pHostperlLIO = &m_hostperlLIO;
1991 m_pHostperlDir = &m_hostperlDir;
1992 m_pHostperlSock = &m_hostperlSock;
1993 m_pHostperlProc = &m_hostperlProc;
1996 #define SETUPEXCHANGE(xptr, iptr, table) \
2007 CPerlHost::CPerlHost(struct IPerlMem** ppMem, struct IPerlMem** ppMemShared,
2008 struct IPerlMem** ppMemParse, struct IPerlEnv** ppEnv,
2009 struct IPerlStdIO** ppStdIO, struct IPerlLIO** ppLIO,
2010 struct IPerlDir** ppDir, struct IPerlSock** ppSock,
2011 struct IPerlProc** ppProc)
2013 InterlockedIncrement(&num_hosts);
2014 m_pvDir = new VDir(0);
2015 m_pVMem = new VMem();
2016 m_pVMemShared = new VMem();
2017 m_pVMemParse = new VMem();
2019 m_pvDir->Init(NULL, m_pVMem);
2022 m_lppEnvList = NULL;
2023 m_bTopLevel = FALSE;
2025 CopyMemory(&m_hostperlMem, &perlMem, sizeof(perlMem));
2026 CopyMemory(&m_hostperlMemShared, &perlMemShared, sizeof(perlMemShared));
2027 CopyMemory(&m_hostperlMemParse, &perlMemParse, sizeof(perlMemParse));
2028 CopyMemory(&m_hostperlEnv, &perlEnv, sizeof(perlEnv));
2029 CopyMemory(&m_hostperlStdIO, &perlStdIO, sizeof(perlStdIO));
2030 CopyMemory(&m_hostperlLIO, &perlLIO, sizeof(perlLIO));
2031 CopyMemory(&m_hostperlDir, &perlDir, sizeof(perlDir));
2032 CopyMemory(&m_hostperlSock, &perlSock, sizeof(perlSock));
2033 CopyMemory(&m_hostperlProc, &perlProc, sizeof(perlProc));
2035 SETUPEXCHANGE(ppMem, m_pHostperlMem, m_hostperlMem);
2036 SETUPEXCHANGE(ppMemShared, m_pHostperlMemShared, m_hostperlMemShared);
2037 SETUPEXCHANGE(ppMemParse, m_pHostperlMemParse, m_hostperlMemParse);
2038 SETUPEXCHANGE(ppEnv, m_pHostperlEnv, m_hostperlEnv);
2039 SETUPEXCHANGE(ppStdIO, m_pHostperlStdIO, m_hostperlStdIO);
2040 SETUPEXCHANGE(ppLIO, m_pHostperlLIO, m_hostperlLIO);
2041 SETUPEXCHANGE(ppDir, m_pHostperlDir, m_hostperlDir);
2042 SETUPEXCHANGE(ppSock, m_pHostperlSock, m_hostperlSock);
2043 SETUPEXCHANGE(ppProc, m_pHostperlProc, m_hostperlProc);
2045 #undef SETUPEXCHANGE
2047 CPerlHost::CPerlHost(CPerlHost& host)
2049 /* Construct a host from another host */
2050 InterlockedIncrement(&num_hosts);
2051 m_pVMem = new VMem();
2052 m_pVMemShared = host.GetMemShared();
2053 m_pVMemParse = host.GetMemParse();
2055 /* duplicate directory info */
2056 m_pvDir = new VDir(0);
2057 m_pvDir->Init(host.GetDir(), m_pVMem);
2059 CopyMemory(&m_hostperlMem, &perlMem, sizeof(perlMem));
2060 CopyMemory(&m_hostperlMemShared, &perlMemShared, sizeof(perlMemShared));
2061 CopyMemory(&m_hostperlMemParse, &perlMemParse, sizeof(perlMemParse));
2062 CopyMemory(&m_hostperlEnv, &perlEnv, sizeof(perlEnv));
2063 CopyMemory(&m_hostperlStdIO, &perlStdIO, sizeof(perlStdIO));
2064 CopyMemory(&m_hostperlLIO, &perlLIO, sizeof(perlLIO));
2065 CopyMemory(&m_hostperlDir, &perlDir, sizeof(perlDir));
2066 CopyMemory(&m_hostperlSock, &perlSock, sizeof(perlSock));
2067 CopyMemory(&m_hostperlProc, &perlProc, sizeof(perlProc));
2068 m_pHostperlMem = &m_hostperlMem;
2069 m_pHostperlMemShared = &m_hostperlMemShared;
2070 m_pHostperlMemParse = &m_hostperlMemParse;
2071 m_pHostperlEnv = &m_hostperlEnv;
2072 m_pHostperlStdIO = &m_hostperlStdIO;
2073 m_pHostperlLIO = &m_hostperlLIO;
2074 m_pHostperlDir = &m_hostperlDir;
2075 m_pHostperlSock = &m_hostperlSock;
2076 m_pHostperlProc = &m_hostperlProc;
2079 m_lppEnvList = NULL;
2080 m_bTopLevel = FALSE;
2082 /* duplicate environment info */
2085 while(lpPtr = host.GetIndex(dwIndex))
2089 CPerlHost::~CPerlHost(void)
2092 InterlockedDecrement(&num_hosts);
2094 m_pVMemParse->Release();
2095 m_pVMemShared->Release();
2100 CPerlHost::Find(LPCSTR lpStr)
2103 LPSTR* lppPtr = Lookup(lpStr);
2104 if(lppPtr != NULL) {
2105 for(lpPtr = *lppPtr; *lpPtr != '\0' && *lpPtr != '='; ++lpPtr)
2117 lookup(const void *arg1, const void *arg2)
2118 { // Compare strings
2122 ptr1 = *(char**)arg1;
2123 ptr2 = *(char**)arg2;
2127 if(c1 == '\0' || c1 == '=') {
2128 if(c2 == '\0' || c2 == '=')
2131 return -1; // string 1 < string 2
2133 else if(c2 == '\0' || c2 == '=')
2134 return 1; // string 1 > string 2
2140 return -1; // string 1 < string 2
2142 return 1; // string 1 > string 2
2150 CPerlHost::Lookup(LPCSTR lpStr)
2153 if (!m_lppEnvList || !m_dwEnvCount)
2158 return (LPSTR*)bsearch(&lpStr, m_lppEnvList, m_dwEnvCount, sizeof(LPSTR), lookup);
2162 compare(const void *arg1, const void *arg2)
2163 { // Compare strings
2167 ptr1 = *(char**)arg1;
2168 ptr2 = *(char**)arg2;
2172 if(c1 == '\0' || c1 == '=') {
2176 return -1; // string 1 < string 2
2178 else if(c2 == '\0' || c2 == '=')
2179 return 1; // string 1 > string 2
2185 return -1; // string 1 < string 2
2187 return 1; // string 1 > string 2
2195 CPerlHost::Add(LPCSTR lpStr)
2198 char szBuffer[1024];
2200 int index, length = strlen(lpStr)+1;
2202 for(index = 0; lpStr[index] != '\0' && lpStr[index] != '='; ++index)
2203 szBuffer[index] = lpStr[index];
2205 szBuffer[index] = '\0';
2208 lpPtr = Lookup(szBuffer);
2209 if (lpPtr != NULL) {
2210 // must allocate things via host memory allocation functions
2211 // rather than perl's Renew() et al, as the perl interpreter
2212 // may either not be initialized enough when we allocate these,
2213 // or may already be dead when we go to free these
2214 *lpPtr = (char*)Realloc(*lpPtr, length * sizeof(char));
2215 strcpy(*lpPtr, lpStr);
2218 m_lppEnvList = (LPSTR*)Realloc(m_lppEnvList, (m_dwEnvCount+1) * sizeof(LPSTR));
2220 m_lppEnvList[m_dwEnvCount] = (char*)Malloc(length * sizeof(char));
2221 if (m_lppEnvList[m_dwEnvCount] != NULL) {
2222 strcpy(m_lppEnvList[m_dwEnvCount], lpStr);
2224 qsort(m_lppEnvList, m_dwEnvCount, sizeof(LPSTR), compare);
2231 CPerlHost::CalculateEnvironmentSpace(void)
2235 for(index = 0; index < m_dwEnvCount; ++index)
2236 dwSize += strlen(m_lppEnvList[index]) + 1;
2242 CPerlHost::FreeLocalEnvironmentStrings(LPSTR lpStr)
2249 CPerlHost::GetChildDir(void)
2255 Newx(ptr, MAX_PATH+1, char);
2256 m_pvDir->GetCurrentDirectoryA(MAX_PATH+1, ptr);
2257 length = strlen(ptr);
2259 if ((ptr[length-1] == '\\') || (ptr[length-1] == '/'))
2266 CPerlHost::FreeChildDir(char* pStr)
2273 CPerlHost::CreateLocalEnvironmentStrings(VDir &vDir)
2276 LPSTR lpStr, lpPtr, lpEnvPtr, lpTmp, lpLocalEnv, lpAllocPtr;
2277 DWORD dwSize, dwEnvIndex;
2278 int nLength, compVal;
2280 // get the process environment strings
2281 lpAllocPtr = lpTmp = (LPSTR)GetEnvironmentStrings();
2283 // step over current directory stuff
2284 while(*lpTmp == '=')
2285 lpTmp += strlen(lpTmp) + 1;
2287 // save the start of the environment strings
2289 for(dwSize = 1; *lpTmp != '\0'; lpTmp += strlen(lpTmp) + 1) {
2290 // calculate the size of the environment strings
2291 dwSize += strlen(lpTmp) + 1;
2294 // add the size of current directories
2295 dwSize += vDir.CalculateEnvironmentSpace();
2297 // add the additional space used by changes made to the environment
2298 dwSize += CalculateEnvironmentSpace();
2300 Newx(lpStr, dwSize, char);
2303 // build the local environment
2304 lpStr = vDir.BuildEnvironmentSpace(lpStr);
2307 lpLocalEnv = GetIndex(dwEnvIndex);
2308 while(*lpEnvPtr != '\0') {
2310 // all environment overrides have been added
2311 // so copy string into place
2312 strcpy(lpStr, lpEnvPtr);
2313 nLength = strlen(lpEnvPtr) + 1;
2315 lpEnvPtr += nLength;
2318 // determine which string to copy next
2319 compVal = compare(&lpEnvPtr, &lpLocalEnv);
2321 strcpy(lpStr, lpEnvPtr);
2322 nLength = strlen(lpEnvPtr) + 1;
2324 lpEnvPtr += nLength;
2327 char *ptr = strchr(lpLocalEnv, '=');
2329 strcpy(lpStr, lpLocalEnv);
2330 lpStr += strlen(lpLocalEnv) + 1;
2332 lpLocalEnv = GetIndex(dwEnvIndex);
2334 // this string was replaced
2335 lpEnvPtr += strlen(lpEnvPtr) + 1;
2342 // still have environment overrides to add
2343 // so copy the strings into place if not an override
2344 char *ptr = strchr(lpLocalEnv, '=');
2346 strcpy(lpStr, lpLocalEnv);
2347 lpStr += strlen(lpLocalEnv) + 1;
2349 lpLocalEnv = GetIndex(dwEnvIndex);
2356 // release the process environment strings
2357 FreeEnvironmentStrings(lpAllocPtr);
2363 CPerlHost::Reset(void)
2366 if(m_lppEnvList != NULL) {
2367 for(DWORD index = 0; index < m_dwEnvCount; ++index) {
2368 Free(m_lppEnvList[index]);
2369 m_lppEnvList[index] = NULL;
2374 m_lppEnvList = NULL;
2378 CPerlHost::Clearenv(void)
2382 LPSTR lpPtr, lpStr, lpEnvPtr;
2383 if (m_lppEnvList != NULL) {
2384 /* set every entry to an empty string */
2385 for(DWORD index = 0; index < m_dwEnvCount; ++index) {
2386 char* ptr = strchr(m_lppEnvList[index], '=');
2393 /* get the process environment strings */
2394 lpStr = lpEnvPtr = (LPSTR)GetEnvironmentStrings();
2396 /* step over current directory stuff */
2397 while(*lpStr == '=')
2398 lpStr += strlen(lpStr) + 1;
2401 lpPtr = strchr(lpStr, '=');
2407 (void)win32_putenv(lpStr);
2410 lpStr += strlen(lpStr) + 1;
2413 FreeEnvironmentStrings(lpEnvPtr);
2418 CPerlHost::Getenv(const char *varname)
2422 char *pEnv = Find(varname);
2426 return win32_getenv(varname);
2430 CPerlHost::Putenv(const char *envstring)
2435 return win32_putenv(envstring);
2441 CPerlHost::Chdir(const char *dirname)
2449 ret = m_pvDir->SetCurrentDirectoryA((char*)dirname);
2456 #endif /* ___PerlHost_H___ */