3 #define USE_SOCKETS_AS_HANDLES
13 _CRTIMP int * __cdecl _errno(void);
14 #define errno (*_errno())
16 _CRTIMP extern int errno;
30 class IPerlStdIOWin : public IPerlStdIO
33 virtual int OpenOSfhandle(long osfhandle, int flags) = 0;
34 virtual int GetOSfhandle(int filenum) = 0;
37 extern int g_closedir(DIR *dirp);
38 extern DIR *g_opendir(char *filename);
39 extern struct direct *g_readdir(DIR *dirp);
40 extern void g_rewinddir(DIR *dirp);
41 extern void g_seekdir(DIR *dirp, long loc);
42 extern long g_telldir(DIR *dirp);
43 class CPerlDir : public IPerlDir
47 virtual int Makedir(const char *dirname, int mode, int &err)
49 return win32_mkdir(dirname, mode);
51 virtual int Chdir(const char *dirname, int &err)
53 return win32_chdir(dirname);
55 virtual int Rmdir(const char *dirname, int &err)
57 return win32_rmdir(dirname);
59 virtual int Close(DIR *dirp, int &err)
61 return g_closedir(dirp);
63 virtual DIR *Open(char *filename, int &err)
65 return g_opendir(filename);
67 virtual struct direct *Read(DIR *dirp, int &err)
69 return g_readdir(dirp);
71 virtual void Rewind(DIR *dirp, int &err)
75 virtual void Seek(DIR *dirp, long loc, int &err)
79 virtual long Tell(DIR *dirp, int &err)
81 return g_telldir(dirp);
86 extern char * g_win32_perllib_path(char *sfx,...);
87 class CPerlEnv : public IPerlEnv
91 virtual char *Getenv(const char *varname, int &err)
93 return win32_getenv(varname);
95 virtual int Putenv(const char *envstring, int &err)
97 return _putenv(envstring);
99 virtual char* LibPath(char *sfx, ...)
101 LPSTR ptr1, ptr2, ptr3, ptr4, ptr5;
104 ptr1 = va_arg(ap,char *);
105 ptr2 = va_arg(ap,char *);
106 ptr3 = va_arg(ap,char *);
107 ptr4 = va_arg(ap,char *);
108 ptr5 = va_arg(ap,char *);
109 return g_win32_perllib_path(sfx, ptr1, ptr2, ptr3, ptr4, ptr5);
113 #define PROCESS_AND_RETURN \
118 class CPerlSock : public IPerlSock
122 virtual u_long Htonl(u_long hostlong)
124 return win32_htonl(hostlong);
126 virtual u_short Htons(u_short hostshort)
128 return win32_htons(hostshort);
130 virtual u_long Ntohl(u_long netlong)
132 return win32_ntohl(netlong);
134 virtual u_short Ntohs(u_short netshort)
136 return win32_ntohs(netshort);
139 virtual SOCKET Accept(SOCKET s, struct sockaddr* addr, int* addrlen, int &err)
141 SOCKET r = win32_accept(s, addr, addrlen);
144 virtual int Bind(SOCKET s, const struct sockaddr* name, int namelen, int &err)
146 int r = win32_bind(s, name, namelen);
149 virtual int Connect(SOCKET s, const struct sockaddr* name, int namelen, int &err)
151 int r = win32_connect(s, name, namelen);
154 virtual void Endhostent(int &err)
158 virtual void Endnetent(int &err)
162 virtual void Endprotoent(int &err)
166 virtual void Endservent(int &err)
170 virtual struct hostent* Gethostbyaddr(const char* addr, int len, int type, int &err)
172 struct hostent *r = win32_gethostbyaddr(addr, len, type);
175 virtual struct hostent* Gethostbyname(const char* name, int &err)
177 struct hostent *r = win32_gethostbyname(name);
180 virtual struct hostent* Gethostent(int &err)
182 croak("gethostent not implemented!\n");
185 virtual int Gethostname(char* name, int namelen, int &err)
187 int r = win32_gethostname(name, namelen);
190 virtual struct netent *Getnetbyaddr(long net, int type, int &err)
192 struct netent *r = win32_getnetbyaddr(net, type);
195 virtual struct netent *Getnetbyname(const char *name, int &err)
197 struct netent *r = win32_getnetbyname((char*)name);
200 virtual struct netent *Getnetent(int &err)
202 struct netent *r = win32_getnetent();
205 virtual int Getpeername(SOCKET s, struct sockaddr* name, int* namelen, int &err)
207 int r = win32_getpeername(s, name, namelen);
210 virtual struct protoent* Getprotobyname(const char* name, int &err)
212 struct protoent *r = win32_getprotobyname(name);
215 virtual struct protoent* Getprotobynumber(int number, int &err)
217 struct protoent *r = win32_getprotobynumber(number);
220 virtual struct protoent* Getprotoent(int &err)
222 struct protoent *r = win32_getprotoent();
225 virtual struct servent* Getservbyname(const char* name, const char* proto, int &err)
227 struct servent *r = win32_getservbyname(name, proto);
230 virtual struct servent* Getservbyport(int port, const char* proto, int &err)
232 struct servent *r = win32_getservbyport(port, proto);
235 virtual struct servent* Getservent(int &err)
237 struct servent *r = win32_getservent();
240 virtual int Getsockname(SOCKET s, struct sockaddr* name, int* namelen, int &err)
242 int r = win32_getsockname(s, name, namelen);
245 virtual int Getsockopt(SOCKET s, int level, int optname, char* optval, int* optlen, int &err)
247 int r = win32_getsockopt(s, level, optname, optval, optlen);
250 virtual unsigned long InetAddr(const char* cp, int &err)
252 unsigned long r = win32_inet_addr(cp);
255 virtual char* InetNtoa(struct in_addr in, int &err)
257 char *r = win32_inet_ntoa(in);
260 virtual int IoctlSocket(SOCKET s, long cmd, u_long *argp, int& err)
262 int r = win32_ioctlsocket(s, cmd, argp);
265 virtual int Listen(SOCKET s, int backlog, int &err)
267 int r = win32_listen(s, backlog);
270 virtual int Recvfrom(SOCKET s, char* buffer, int len, int flags, struct sockaddr* from, int* fromlen, int &err)
272 int r = win32_recvfrom(s, buffer, len, flags, from, fromlen);
275 virtual int Select(int nfds, char* readfds, char* writefds, char* exceptfds, const struct timeval* timeout, int &err)
277 int r = win32_select(nfds, (Perl_fd_set*)readfds, (Perl_fd_set*)writefds, (Perl_fd_set*)exceptfds, timeout);
280 virtual int Send(SOCKET s, const char* buffer, int len, int flags, int &err)
282 int r = win32_send(s, buffer, len, flags);
285 virtual int Sendto(SOCKET s, const char* buffer, int len, int flags, const struct sockaddr* to, int tolen, int &err)
287 int r = win32_sendto(s, buffer, len, flags, to, tolen);
290 virtual void Sethostent(int stayopen, int &err)
292 win32_sethostent(stayopen);
294 virtual void Setnetent(int stayopen, int &err)
296 win32_setnetent(stayopen);
298 virtual void Setprotoent(int stayopen, int &err)
300 win32_setprotoent(stayopen);
302 virtual void Setservent(int stayopen, int &err)
304 win32_setservent(stayopen);
306 virtual int Setsockopt(SOCKET s, int level, int optname, const char* optval, int optlen, int &err)
308 int r = win32_setsockopt(s, level, optname, optval, optlen);
311 virtual int Shutdown(SOCKET s, int how, int &err)
313 int r = win32_shutdown(s, how);
316 virtual SOCKET Socket(int af, int type, int protocol, int &err)
318 SOCKET r = win32_socket(af, type, protocol);
321 virtual int Socketpair(int domain, int type, int protocol, int* fds, int &err)
323 croak("socketpair not implemented!\n");
329 #define CALLFUNCRET(x)\
335 #define CALLFUNCERR(x)\
341 #define LCALLFUNCERR(x)\
347 class CPerlLIO : public IPerlLIO
351 virtual int Access(const char *path, int mode, int &err)
353 CALLFUNCRET(access(path, mode))
355 virtual int Chmod(const char *filename, int pmode, int &err)
357 CALLFUNCRET(chmod(filename, pmode))
359 virtual int Chsize(int handle, long size, int &err)
361 CALLFUNCRET(chsize(handle, size))
363 virtual int Close(int handle, int &err)
365 CALLFUNCRET(win32_close(handle))
367 virtual int Dup(int handle, int &err)
369 CALLFUNCERR(win32_dup(handle))
371 virtual int Dup2(int handle1, int handle2, int &err)
373 CALLFUNCERR(win32_dup2(handle1, handle2))
375 virtual int Flock(int fd, int oper, int &err)
377 CALLFUNCERR(win32_flock(fd, oper))
379 virtual int FileStat(int handle, struct stat *buffer, int &err)
381 CALLFUNCERR(fstat(handle, buffer))
383 virtual int IOCtl(int i, unsigned int u, char *data, int &err)
385 CALLFUNCERR(win32_ioctlsocket((SOCKET)i, (long)u, (u_long*)data))
387 virtual int Isatty(int fd, int &err)
391 virtual long Lseek(int handle, long offset, int origin, int &err)
393 LCALLFUNCERR(win32_lseek(handle, offset, origin))
395 virtual int Lstat(const char *path, struct stat *buffer, int &err)
397 return NameStat(path, buffer, err);
399 virtual char *Mktemp(char *Template, int &err)
401 return mktemp(Template);
403 virtual int Open(const char *filename, int oflag, int &err)
405 CALLFUNCERR(win32_open(filename, oflag))
407 virtual int Open(const char *filename, int oflag, int pmode, int &err)
410 if(stricmp(filename, "/dev/null") == 0)
411 ret = open("NUL", oflag, pmode);
413 ret = open(filename, oflag, pmode);
419 virtual int Read(int handle, void *buffer, unsigned int count, int &err)
421 CALLFUNCERR(win32_read(handle, buffer, count))
423 virtual int Rename(const char *OldFileName, const char *newname, int &err)
425 char szNewWorkName[MAX_PATH+1];
426 WIN32_FIND_DATA fdOldFile, fdNewFile;
430 if((strchr(OldFileName, '\\') || strchr(OldFileName, '/'))
431 && strchr(newname, '\\') == NULL
432 && strchr(newname, '/') == NULL)
434 strcpy(szNewWorkName, OldFileName);
435 if((ptr = strrchr(szNewWorkName, '\\')) == NULL)
436 ptr = strrchr(szNewWorkName, '/');
437 strcpy(++ptr, newname);
440 strcpy(szNewWorkName, newname);
442 if(stricmp(OldFileName, szNewWorkName) != 0)
443 { // check that we're not being fooled by relative paths
444 // and only delete the new file
446 // 2) it is not the same file as the old file
448 // GetFullPathName does not return the long file name on some systems
449 handle = FindFirstFile(OldFileName, &fdOldFile);
450 if(handle != INVALID_HANDLE_VALUE)
454 handle = FindFirstFile(szNewWorkName, &fdNewFile);
456 if(handle != INVALID_HANDLE_VALUE)
459 fdNewFile.cFileName[0] = '\0';
461 if(strcmp(fdOldFile.cAlternateFileName, fdNewFile.cAlternateFileName) != 0
462 && strcmp(fdOldFile.cFileName, fdNewFile.cFileName) != 0)
463 { // file exists and not same file
464 DeleteFile(szNewWorkName);
468 int ret = rename(OldFileName, szNewWorkName);
474 virtual int Setmode(int handle, int mode, int &err)
476 CALLFUNCRET(win32_setmode(handle, mode))
478 virtual int NameStat(const char *path, struct stat *buffer, int &err)
480 return win32_stat(path, buffer);
482 virtual char *Tmpnam(char *string, int &err)
484 return tmpnam(string);
486 virtual int Umask(int pmode, int &err)
490 virtual int Unlink(const char *filename, int &err)
492 chmod(filename, _S_IREAD | _S_IWRITE);
493 CALLFUNCRET(unlink(filename))
495 virtual int Utime(char *filename, struct utimbuf *times, int &err)
497 CALLFUNCRET(win32_utime(filename, times))
499 virtual int Write(int handle, const void *buffer, unsigned int count, int &err)
501 CALLFUNCERR(win32_write(handle, buffer, count))
505 class CPerlMem : public IPerlMem
509 virtual void* Malloc(size_t size)
511 return win32_malloc(size);
513 virtual void* Realloc(void* ptr, size_t size)
515 return win32_realloc(ptr, size);
517 virtual void Free(void* ptr)
524 #define EXECF_SPAWN 2
526 extern char *g_getlogin(void);
527 extern int do_spawn2(char *cmd, int exectype);
528 extern int g_do_aspawn(void *vreally, void **vmark, void **vsp);
529 class CPerlProc : public IPerlProc
533 virtual void Abort(void)
537 virtual void Exit(int status)
541 virtual void _Exit(int status)
545 virtual int Execl(const char *cmdname, const char *arg0, const char *arg1, const char *arg2, const char *arg3)
547 return execl(cmdname, arg0, arg1, arg2, arg3);
549 virtual int Execv(const char *cmdname, const char *const *argv)
551 return win32_execvp(cmdname, argv);
553 virtual int Execvp(const char *cmdname, const char *const *argv)
555 return win32_execvp(cmdname, argv);
557 virtual uid_t Getuid(void)
561 virtual uid_t Geteuid(void)
565 virtual gid_t Getgid(void)
569 virtual gid_t Getegid(void)
573 virtual char *Getlogin(void)
577 virtual int Kill(int pid, int sig)
579 return kill(pid, sig);
581 virtual int Killpg(int pid, int sig)
583 croak("killpg not implemented!\n");
586 virtual int PauseProc(void)
588 return win32_sleep((32767L << 16) + 32767);
590 virtual PerlIO* Popen(const char *command, const char *mode)
592 return (PerlIO*)win32_popen(command, mode);
594 virtual int Pclose(PerlIO *stream)
596 return win32_pclose((FILE*)stream);
598 virtual int Pipe(int *phandles)
600 return win32_pipe(phandles, 512, _O_BINARY);
602 virtual int Setuid(uid_t u)
606 virtual int Setgid(gid_t g)
610 virtual int Sleep(unsigned int s)
612 return win32_sleep(s);
614 virtual int Times(struct tms *timebuf)
616 return win32_times(timebuf);
618 virtual int Wait(int *status)
620 return win32_wait(status);
622 virtual Sighandler_t Signal(int sig, Sighandler_t subcode)
626 virtual void GetSysMsg(char*& sMsg, DWORD& dwLen, DWORD dwErr)
628 dwLen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
629 |FORMAT_MESSAGE_IGNORE_INSERTS
630 |FORMAT_MESSAGE_FROM_SYSTEM, NULL,
631 dwErr, 0, (char *)&sMsg, 1, NULL);
633 while (0 < dwLen && isspace(sMsg[--dwLen]))
635 if ('.' != sMsg[dwLen])
640 sMsg = (char*)LocalAlloc(0, 64/**sizeof(TCHAR)*/);
641 dwLen = sprintf(sMsg,
642 "Unknown error #0x%lX (lookup 0x%lX)",
643 dwErr, GetLastError());
646 virtual void FreeBuf(char* sMsg)
650 virtual BOOL DoCmd(char *cmd)
652 do_spawn2(cmd, EXECF_EXEC);
655 virtual int Spawn(char* cmds)
657 return do_spawn2(cmds, EXECF_SPAWN);
659 virtual int Spawnvp(int mode, const char *cmdname, const char *const *argv)
661 return win32_spawnvp(mode, cmdname, argv);
663 virtual int ASpawn(void *vreally, void **vmark, void **vsp)
665 return g_do_aspawn(vreally, vmark, vsp);
670 class CPerlStdIO : public IPerlStdIOWin
674 virtual PerlIO* Stdin(void)
676 return (PerlIO*)win32_stdin();
678 virtual PerlIO* Stdout(void)
680 return (PerlIO*)win32_stdout();
682 virtual PerlIO* Stderr(void)
684 return (PerlIO*)win32_stderr();
686 virtual PerlIO* Open(const char *path, const char *mode, int &err)
688 PerlIO*pf = (PerlIO*)win32_fopen(path, mode);
693 virtual int Close(PerlIO* pf, int &err)
695 CALLFUNCERR(win32_fclose(((FILE*)pf)))
697 virtual int Eof(PerlIO* pf, int &err)
699 CALLFUNCERR(win32_feof((FILE*)pf))
701 virtual int Error(PerlIO* pf, int &err)
703 CALLFUNCERR(win32_ferror((FILE*)pf))
705 virtual void Clearerr(PerlIO* pf, int &err)
707 win32_clearerr((FILE*)pf);
709 virtual int Getc(PerlIO* pf, int &err)
711 CALLFUNCERR(win32_getc((FILE*)pf))
713 virtual char* GetBase(PerlIO* pf, int &err)
715 return ((FILE*)pf)->_base;
717 virtual int GetBufsiz(PerlIO* pf, int &err)
719 return ((FILE*)pf)->_bufsiz;
721 virtual int GetCnt(PerlIO* pf, int &err)
723 return ((FILE*)pf)->_cnt;
725 virtual char* GetPtr(PerlIO* pf, int &err)
727 return ((FILE*)pf)->_ptr;
729 virtual int Putc(PerlIO* pf, int c, int &err)
731 CALLFUNCERR(win32_fputc(c, (FILE*)pf))
733 virtual int Puts(PerlIO* pf, const char *s, int &err)
735 CALLFUNCERR(win32_fputs(s, (FILE*)pf))
737 virtual int Flush(PerlIO* pf, int &err)
739 CALLFUNCERR(win32_fflush((FILE*)pf))
741 virtual int Ungetc(PerlIO* pf,int c, int &err)
743 CALLFUNCERR(win32_ungetc(c, (FILE*)pf))
745 virtual int Fileno(PerlIO* pf, int &err)
747 CALLFUNCERR(win32_fileno((FILE*)pf))
749 virtual PerlIO* Fdopen(int fd, const char *mode, int &err)
751 PerlIO* pf = (PerlIO*)win32_fdopen(fd, mode);
756 virtual PerlIO* Reopen(const char*path, const char*mode, PerlIO* pf, int &err)
758 PerlIO* newPf = (PerlIO*)win32_freopen(path, mode, (FILE*)pf);
763 virtual SSize_t Read(PerlIO* pf, void *buffer, Size_t size, int &err)
765 SSize_t i = win32_fread(buffer, size, 1, (FILE*)pf);
770 virtual SSize_t Write(PerlIO* pf, const void *buffer, Size_t size, int &err)
772 SSize_t i = win32_fwrite(buffer, size, 1, (FILE*)pf);
777 virtual void SetBuf(PerlIO* pf, char* buffer, int &err)
779 win32_setbuf((FILE*)pf, buffer);
781 virtual int SetVBuf(PerlIO* pf, char* buffer, int type, Size_t size, int &err)
783 int i = win32_setvbuf((FILE*)pf, buffer, type, size);
788 virtual void SetCnt(PerlIO* pf, int n, int &err)
790 ((FILE*)pf)->_cnt = n;
792 virtual void SetPtrCnt(PerlIO* pf, char * ptr, int n, int& err)
794 ((FILE*)pf)->_ptr = ptr;
795 ((FILE*)pf)->_cnt = n;
797 virtual void Setlinebuf(PerlIO* pf, int &err)
799 win32_setvbuf((FILE*)pf, NULL, _IOLBF, 0);
801 virtual int Printf(PerlIO* pf, int &err, const char *format,...)
804 va_start(arglist, format);
805 int i = win32_vfprintf((FILE*)pf, format, arglist);
810 virtual int Vprintf(PerlIO* pf, int &err, const char *format, va_list arglist)
812 int i = win32_vfprintf((FILE*)pf, format, arglist);
817 virtual long Tell(PerlIO* pf, int &err)
819 long l = win32_ftell((FILE*)pf);
824 virtual int Seek(PerlIO* pf, off_t offset, int origin, int &err)
826 int i = win32_fseek((FILE*)pf, offset, origin);
831 virtual void Rewind(PerlIO* pf, int &err)
833 win32_rewind((FILE*)pf);
835 virtual PerlIO* Tmpfile(int &err)
837 PerlIO* pf = (PerlIO*)win32_tmpfile();
842 virtual int Getpos(PerlIO* pf, Fpos_t *p, int &err)
844 int i = win32_fgetpos((FILE*)pf, p);
849 virtual int Setpos(PerlIO* pf, const Fpos_t *p, int &err)
851 int i = win32_fsetpos((FILE*)pf, p);
856 virtual void Init(int &err)
859 virtual void InitOSExtras(void* p)
861 Perl_init_os_extras();
863 virtual int OpenOSfhandle(long osfhandle, int flags)
865 return win32_open_osfhandle(osfhandle, flags);
867 virtual int GetOSfhandle(int filenum)
869 return win32_get_osfhandle(filenum);
874 static void xs_init _((CPERLarg));
875 #define stderr (&_iob[2])
882 CPerlHost() { pPerl = NULL; };
883 inline BOOL PerlCreate(void)
887 pPerl = perl_alloc(&perlMem, &perlEnv, &perlStdIO, &perlLIO, &perlDir, &perlSock, &perlProc);
892 pPerl->perl_construct();
896 fprintf(stderr, "%s\n", "Error: Unable to construct data structures");
904 fprintf(stderr, "%s\n", "Error: Unable to allocate memory");
907 return (pPerl != NULL);
909 inline int PerlParse(int argc, char** argv, char** env)
911 char* environ = NULL;
915 retVal = pPerl->perl_parse(xs_init, argc, argv, (env == NULL || *env == NULL ? &environ : env));
919 // this is where exit() should arrive
924 fprintf(stderr, "Error: Parse exception\n");
929 inline int PerlRun(void)
934 retVal = pPerl->perl_run();
938 // this is where exit() should arrive
943 fprintf(stderr, "Error: Runtime exception\n");
948 inline void PerlDestroy(void)
952 pPerl->perl_destruct();
967 CPerlStdIO perlStdIO;
971 #define PERL_SYS_INIT(a, c)
974 main(int argc, char **argv, char **env)
979 if(!host.PerlCreate())
983 exitstatus = host.PerlParse(argc, argv, env);
987 exitstatus = host.PerlRun();
995 char *staticlinkmodules[] = {
1000 EXTERN_C void boot_DynaLoader _((CPERLarg_ CV* cv));
1005 char *file = __FILE__;
1007 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1010 #else /* PERL_OBJECT */
1012 /* Say NO to CPP! Hallelujah! */
1015 * GNU C does not do __declspec()
1017 #define __declspec(foo)
1019 /* Mingw32 defaults to globing command line
1020 * This is inconsistent with other Win32 ports and
1021 * seems to cause trouble with passing -DXSVERSION=\"1.6\"
1022 * So we turn it off like this:
1029 __declspec(dllimport) int RunPerl(int argc, char **argv, char **env, void *ios);
1032 main(int argc, char **argv, char **env)
1034 return RunPerl(argc, argv, env, (void*)0);
1037 #endif /* PERL_OBJECT */