3 #define USE_SOCKETS_AS_HANDLES
11 #define errno (*win32_errno())
12 #define stdout (win32_stdout())
13 #define stderr (win32_stderr())
26 extern int g_closedir(DIR *dirp);
27 extern DIR *g_opendir(char *filename);
28 extern struct direct *g_readdir(DIR *dirp);
29 extern void g_rewinddir(DIR *dirp);
30 extern void g_seekdir(DIR *dirp, long loc);
31 extern long g_telldir(DIR *dirp);
32 class CPerlDir : public IPerlDir
36 virtual int Makedir(const char *dirname, int mode, int &err)
38 return win32_mkdir(dirname, mode);
40 virtual int Chdir(const char *dirname, int &err)
42 return win32_chdir(dirname);
44 virtual int Rmdir(const char *dirname, int &err)
46 return win32_rmdir(dirname);
48 virtual int Close(DIR *dirp, int &err)
50 return g_closedir(dirp);
52 virtual DIR *Open(char *filename, int &err)
54 return g_opendir(filename);
56 virtual struct direct *Read(DIR *dirp, int &err)
58 return g_readdir(dirp);
60 virtual void Rewind(DIR *dirp, int &err)
64 virtual void Seek(DIR *dirp, long loc, int &err)
68 virtual long Tell(DIR *dirp, int &err)
70 return g_telldir(dirp);
75 extern char * g_win32_get_stdlib(char *pl);
76 extern char * g_win32_get_sitelib(char *pl);
77 class CPerlEnv : public IPerlEnv
81 virtual char *Getenv(const char *varname, int &err)
83 return win32_getenv(varname);
85 virtual int Putenv(const char *envstring, int &err)
87 return putenv(envstring);
89 virtual char* LibPath(char *pl)
91 return g_win32_get_stdlib(pl);
93 virtual char* SiteLibPath(char *pl)
95 return g_win32_get_sitelib(pl);
99 #define PROCESS_AND_RETURN \
104 class CPerlSock : public IPerlSock
108 virtual u_long Htonl(u_long hostlong)
110 return win32_htonl(hostlong);
112 virtual u_short Htons(u_short hostshort)
114 return win32_htons(hostshort);
116 virtual u_long Ntohl(u_long netlong)
118 return win32_ntohl(netlong);
120 virtual u_short Ntohs(u_short netshort)
122 return win32_ntohs(netshort);
125 virtual SOCKET Accept(SOCKET s, struct sockaddr* addr, int* addrlen, int &err)
127 SOCKET r = win32_accept(s, addr, addrlen);
130 virtual int Bind(SOCKET s, const struct sockaddr* name, int namelen, int &err)
132 int r = win32_bind(s, name, namelen);
135 virtual int Connect(SOCKET s, const struct sockaddr* name, int namelen, int &err)
137 int r = win32_connect(s, name, namelen);
140 virtual void Endhostent(int &err)
144 virtual void Endnetent(int &err)
148 virtual void Endprotoent(int &err)
152 virtual void Endservent(int &err)
156 virtual struct hostent* Gethostbyaddr(const char* addr, int len, int type, int &err)
158 struct hostent *r = win32_gethostbyaddr(addr, len, type);
161 virtual struct hostent* Gethostbyname(const char* name, int &err)
163 struct hostent *r = win32_gethostbyname(name);
166 virtual struct hostent* Gethostent(int &err)
168 croak("gethostent not implemented!\n");
171 virtual int Gethostname(char* name, int namelen, int &err)
173 int r = win32_gethostname(name, namelen);
176 virtual struct netent *Getnetbyaddr(long net, int type, int &err)
178 struct netent *r = win32_getnetbyaddr(net, type);
181 virtual struct netent *Getnetbyname(const char *name, int &err)
183 struct netent *r = win32_getnetbyname((char*)name);
186 virtual struct netent *Getnetent(int &err)
188 struct netent *r = win32_getnetent();
191 virtual int Getpeername(SOCKET s, struct sockaddr* name, int* namelen, int &err)
193 int r = win32_getpeername(s, name, namelen);
196 virtual struct protoent* Getprotobyname(const char* name, int &err)
198 struct protoent *r = win32_getprotobyname(name);
201 virtual struct protoent* Getprotobynumber(int number, int &err)
203 struct protoent *r = win32_getprotobynumber(number);
206 virtual struct protoent* Getprotoent(int &err)
208 struct protoent *r = win32_getprotoent();
211 virtual struct servent* Getservbyname(const char* name, const char* proto, int &err)
213 struct servent *r = win32_getservbyname(name, proto);
216 virtual struct servent* Getservbyport(int port, const char* proto, int &err)
218 struct servent *r = win32_getservbyport(port, proto);
221 virtual struct servent* Getservent(int &err)
223 struct servent *r = win32_getservent();
226 virtual int Getsockname(SOCKET s, struct sockaddr* name, int* namelen, int &err)
228 int r = win32_getsockname(s, name, namelen);
231 virtual int Getsockopt(SOCKET s, int level, int optname, char* optval, int* optlen, int &err)
233 int r = win32_getsockopt(s, level, optname, optval, optlen);
236 virtual unsigned long InetAddr(const char* cp, int &err)
238 unsigned long r = win32_inet_addr(cp);
241 virtual char* InetNtoa(struct in_addr in, int &err)
243 char *r = win32_inet_ntoa(in);
246 virtual int IoctlSocket(SOCKET s, long cmd, u_long *argp, int& err)
248 int r = win32_ioctlsocket(s, cmd, argp);
251 virtual int Listen(SOCKET s, int backlog, int &err)
253 int r = win32_listen(s, backlog);
256 virtual int Recvfrom(SOCKET s, char* buffer, int len, int flags, struct sockaddr* from, int* fromlen, int &err)
258 int r = win32_recvfrom(s, buffer, len, flags, from, fromlen);
261 virtual int Select(int nfds, char* readfds, char* writefds, char* exceptfds, const struct timeval* timeout, int &err)
263 int r = win32_select(nfds, (Perl_fd_set*)readfds, (Perl_fd_set*)writefds, (Perl_fd_set*)exceptfds, timeout);
266 virtual int Send(SOCKET s, const char* buffer, int len, int flags, int &err)
268 int r = win32_send(s, buffer, len, flags);
271 virtual int Sendto(SOCKET s, const char* buffer, int len, int flags, const struct sockaddr* to, int tolen, int &err)
273 int r = win32_sendto(s, buffer, len, flags, to, tolen);
276 virtual void Sethostent(int stayopen, int &err)
278 win32_sethostent(stayopen);
280 virtual void Setnetent(int stayopen, int &err)
282 win32_setnetent(stayopen);
284 virtual void Setprotoent(int stayopen, int &err)
286 win32_setprotoent(stayopen);
288 virtual void Setservent(int stayopen, int &err)
290 win32_setservent(stayopen);
292 virtual int Setsockopt(SOCKET s, int level, int optname, const char* optval, int optlen, int &err)
294 int r = win32_setsockopt(s, level, optname, optval, optlen);
297 virtual int Shutdown(SOCKET s, int how, int &err)
299 int r = win32_shutdown(s, how);
302 virtual SOCKET Socket(int af, int type, int protocol, int &err)
304 SOCKET r = win32_socket(af, type, protocol);
307 virtual int Socketpair(int domain, int type, int protocol, int* fds, int &err)
309 croak("socketpair not implemented!\n");
315 #define CALLFUNCRET(x)\
321 #define CALLFUNCERR(x)\
327 #define LCALLFUNCERR(x)\
333 class CPerlLIO : public IPerlLIO
337 virtual int Access(const char *path, int mode, int &err)
339 CALLFUNCRET(access(path, mode))
341 virtual int Chmod(const char *filename, int pmode, int &err)
343 CALLFUNCRET(chmod(filename, pmode))
345 virtual int Chown(const char *filename, uid_t owner, gid_t group, int &err)
347 CALLFUNCERR(chown(filename, owner, group))
349 virtual int Chsize(int handle, long size, int &err)
351 CALLFUNCRET(chsize(handle, size))
353 virtual int Close(int handle, int &err)
355 CALLFUNCRET(win32_close(handle))
357 virtual int Dup(int handle, int &err)
359 CALLFUNCERR(win32_dup(handle))
361 virtual int Dup2(int handle1, int handle2, int &err)
363 CALLFUNCERR(win32_dup2(handle1, handle2))
365 virtual int Flock(int fd, int oper, int &err)
367 CALLFUNCERR(win32_flock(fd, oper))
369 virtual int FileStat(int handle, struct stat *buffer, int &err)
371 CALLFUNCERR(fstat(handle, buffer))
373 virtual int IOCtl(int i, unsigned int u, char *data, int &err)
375 CALLFUNCERR(win32_ioctlsocket((SOCKET)i, (long)u, (u_long*)data))
377 virtual int Isatty(int fd, int &err)
381 virtual long Lseek(int handle, long offset, int origin, int &err)
383 LCALLFUNCERR(win32_lseek(handle, offset, origin))
385 virtual int Lstat(const char *path, struct stat *buffer, int &err)
387 return NameStat(path, buffer, err);
389 virtual char *Mktemp(char *Template, int &err)
391 return mktemp(Template);
393 virtual int Open(const char *filename, int oflag, int &err)
395 CALLFUNCERR(win32_open(filename, oflag))
397 virtual int Open(const char *filename, int oflag, int pmode, int &err)
400 if(stricmp(filename, "/dev/null") == 0)
401 ret = open("NUL", oflag, pmode);
403 ret = open(filename, oflag, pmode);
409 virtual int Read(int handle, void *buffer, unsigned int count, int &err)
411 CALLFUNCERR(win32_read(handle, buffer, count))
413 virtual int Rename(const char *OldFileName, const char *newname, int &err)
415 char szNewWorkName[MAX_PATH+1];
416 WIN32_FIND_DATA fdOldFile, fdNewFile;
420 if((strchr(OldFileName, '\\') || strchr(OldFileName, '/'))
421 && strchr(newname, '\\') == NULL
422 && strchr(newname, '/') == NULL)
424 strcpy(szNewWorkName, OldFileName);
425 if((ptr = strrchr(szNewWorkName, '\\')) == NULL)
426 ptr = strrchr(szNewWorkName, '/');
427 strcpy(++ptr, newname);
430 strcpy(szNewWorkName, newname);
432 if(stricmp(OldFileName, szNewWorkName) != 0)
433 { // check that we're not being fooled by relative paths
434 // and only delete the new file
436 // 2) it is not the same file as the old file
438 // GetFullPathName does not return the long file name on some systems
439 handle = FindFirstFile(OldFileName, &fdOldFile);
440 if(handle != INVALID_HANDLE_VALUE)
444 handle = FindFirstFile(szNewWorkName, &fdNewFile);
446 if(handle != INVALID_HANDLE_VALUE)
449 fdNewFile.cFileName[0] = '\0';
451 if(strcmp(fdOldFile.cAlternateFileName, fdNewFile.cAlternateFileName) != 0
452 && strcmp(fdOldFile.cFileName, fdNewFile.cFileName) != 0)
453 { // file exists and not same file
454 DeleteFile(szNewWorkName);
458 int ret = rename(OldFileName, szNewWorkName);
464 virtual int Setmode(int handle, int mode, int &err)
466 CALLFUNCRET(win32_setmode(handle, mode))
468 virtual int NameStat(const char *path, struct stat *buffer, int &err)
470 return win32_stat(path, buffer);
472 virtual char *Tmpnam(char *string, int &err)
474 return tmpnam(string);
476 virtual int Umask(int pmode, int &err)
480 virtual int Unlink(const char *filename, int &err)
482 chmod(filename, S_IREAD | S_IWRITE);
483 CALLFUNCRET(unlink(filename))
485 virtual int Utime(char *filename, struct utimbuf *times, int &err)
487 CALLFUNCRET(win32_utime(filename, times))
489 virtual int Write(int handle, const void *buffer, unsigned int count, int &err)
491 CALLFUNCERR(win32_write(handle, buffer, count))
495 class CPerlMem : public IPerlMem
499 virtual void* Malloc(size_t size)
501 return win32_malloc(size);
503 virtual void* Realloc(void* ptr, size_t size)
505 return win32_realloc(ptr, size);
507 virtual void Free(void* ptr)
514 #define EXECF_SPAWN 2
516 extern char *g_getlogin(void);
517 extern int do_spawn2(char *cmd, int exectype);
518 extern int g_do_aspawn(void *vreally, void **vmark, void **vsp);
519 class CPerlProc : public IPerlProc
523 virtual void Abort(void)
527 virtual void Exit(int status)
531 virtual void _Exit(int status)
535 virtual int Execl(const char *cmdname, const char *arg0, const char *arg1, const char *arg2, const char *arg3)
537 return execl(cmdname, arg0, arg1, arg2, arg3);
539 virtual int Execv(const char *cmdname, const char *const *argv)
541 return win32_execvp(cmdname, argv);
543 virtual int Execvp(const char *cmdname, const char *const *argv)
545 return win32_execvp(cmdname, argv);
547 virtual uid_t Getuid(void)
551 virtual uid_t Geteuid(void)
555 virtual gid_t Getgid(void)
559 virtual gid_t Getegid(void)
563 virtual char *Getlogin(void)
567 virtual int Kill(int pid, int sig)
569 return kill(pid, sig);
571 virtual int Killpg(int pid, int sig)
573 croak("killpg not implemented!\n");
576 virtual int PauseProc(void)
578 return win32_sleep((32767L << 16) + 32767);
580 virtual PerlIO* Popen(const char *command, const char *mode)
583 win32_fflush(stdout);
584 win32_fflush(stderr);
586 return (PerlIO*)win32_popen(command, mode);
588 virtual int Pclose(PerlIO *stream)
590 return win32_pclose((FILE*)stream);
592 virtual int Pipe(int *phandles)
594 return win32_pipe(phandles, 512, O_BINARY);
596 virtual int Setuid(uid_t u)
600 virtual int Setgid(gid_t g)
604 virtual int Sleep(unsigned int s)
606 return win32_sleep(s);
608 virtual int Times(struct tms *timebuf)
610 return win32_times(timebuf);
612 virtual int Wait(int *status)
614 return win32_wait(status);
616 virtual Sighandler_t Signal(int sig, Sighandler_t subcode)
620 virtual void GetSysMsg(char*& sMsg, DWORD& dwLen, DWORD dwErr)
622 dwLen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
623 |FORMAT_MESSAGE_IGNORE_INSERTS
624 |FORMAT_MESSAGE_FROM_SYSTEM, NULL,
625 dwErr, 0, (char *)&sMsg, 1, NULL);
627 while (0 < dwLen && isspace(sMsg[--dwLen]))
629 if ('.' != sMsg[dwLen])
634 sMsg = (char*)LocalAlloc(0, 64/**sizeof(TCHAR)*/);
635 dwLen = sprintf(sMsg,
636 "Unknown error #0x%lX (lookup 0x%lX)",
637 dwErr, GetLastError());
640 virtual void FreeBuf(char* sMsg)
644 virtual BOOL DoCmd(char *cmd)
646 do_spawn2(cmd, EXECF_EXEC);
649 virtual int Spawn(char* cmds)
651 return do_spawn2(cmds, EXECF_SPAWN);
653 virtual int Spawnvp(int mode, const char *cmdname, const char *const *argv)
655 return win32_spawnvp(mode, cmdname, argv);
657 virtual int ASpawn(void *vreally, void **vmark, void **vsp)
659 return g_do_aspawn(vreally, vmark, vsp);
664 class CPerlStdIO : public IPerlStdIO
668 virtual PerlIO* Stdin(void)
670 return (PerlIO*)win32_stdin();
672 virtual PerlIO* Stdout(void)
674 return (PerlIO*)win32_stdout();
676 virtual PerlIO* Stderr(void)
678 return (PerlIO*)win32_stderr();
680 virtual PerlIO* Open(const char *path, const char *mode, int &err)
682 PerlIO*pf = (PerlIO*)win32_fopen(path, mode);
687 virtual int Close(PerlIO* pf, int &err)
689 CALLFUNCERR(win32_fclose(((FILE*)pf)))
691 virtual int Eof(PerlIO* pf, int &err)
693 CALLFUNCERR(win32_feof((FILE*)pf))
695 virtual int Error(PerlIO* pf, int &err)
697 CALLFUNCERR(win32_ferror((FILE*)pf))
699 virtual void Clearerr(PerlIO* pf, int &err)
701 win32_clearerr((FILE*)pf);
703 virtual int Getc(PerlIO* pf, int &err)
705 CALLFUNCERR(win32_getc((FILE*)pf))
707 virtual char* GetBase(PerlIO* pf, int &err)
712 virtual int GetBufsiz(PerlIO* pf, int &err)
715 return FILE_bufsiz(f);
717 virtual int GetCnt(PerlIO* pf, int &err)
722 virtual char* GetPtr(PerlIO* pf, int &err)
727 virtual int Putc(PerlIO* pf, int c, int &err)
729 CALLFUNCERR(win32_fputc(c, (FILE*)pf))
731 virtual int Puts(PerlIO* pf, const char *s, int &err)
733 CALLFUNCERR(win32_fputs(s, (FILE*)pf))
735 virtual int Flush(PerlIO* pf, int &err)
737 CALLFUNCERR(win32_fflush((FILE*)pf))
739 virtual int Ungetc(PerlIO* pf,int c, int &err)
741 CALLFUNCERR(win32_ungetc(c, (FILE*)pf))
743 virtual int Fileno(PerlIO* pf, int &err)
745 CALLFUNCERR(win32_fileno((FILE*)pf))
747 virtual PerlIO* Fdopen(int fd, const char *mode, int &err)
749 PerlIO* pf = (PerlIO*)win32_fdopen(fd, mode);
754 virtual PerlIO* Reopen(const char*path, const char*mode, PerlIO* pf, int &err)
756 PerlIO* newPf = (PerlIO*)win32_freopen(path, mode, (FILE*)pf);
761 virtual SSize_t Read(PerlIO* pf, void *buffer, Size_t size, int &err)
763 SSize_t i = win32_fread(buffer, 1, size, (FILE*)pf);
768 virtual SSize_t Write(PerlIO* pf, const void *buffer, Size_t size, int &err)
770 SSize_t i = win32_fwrite(buffer, 1, size, (FILE*)pf);
775 virtual void SetBuf(PerlIO* pf, char* buffer, int &err)
777 win32_setbuf((FILE*)pf, buffer);
779 virtual int SetVBuf(PerlIO* pf, char* buffer, int type, Size_t size, int &err)
781 int i = win32_setvbuf((FILE*)pf, buffer, type, size);
786 virtual void SetCnt(PerlIO* pf, int n, int &err)
791 virtual void SetPtrCnt(PerlIO* pf, char * ptr, int n, int& err)
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));
879 CPerlHost() { pPerl = NULL; };
880 inline BOOL PerlCreate(void)
884 pPerl = perl_alloc(&perlMem, &perlEnv, &perlStdIO, &perlLIO, &perlDir, &perlSock, &perlProc);
889 pPerl->perl_construct();
893 win32_fprintf(stderr, "%s\n", "Error: Unable to construct data structures");
901 win32_fprintf(stderr, "%s\n", "Error: Unable to allocate memory");
904 return (pPerl != NULL);
906 inline int PerlParse(int argc, char** argv, char** env)
908 char* environ = NULL;
912 retVal = pPerl->perl_parse(xs_init, argc, argv, (env == NULL || *env == NULL ? &environ : env));
916 // this is where exit() should arrive
921 win32_fprintf(stderr, "Error: Parse exception\n");
926 inline int PerlRun(void)
931 retVal = pPerl->perl_run();
935 // this is where exit() should arrive
940 win32_fprintf(stderr, "Error: Runtime exception\n");
945 inline void PerlDestroy(void)
949 pPerl->perl_destruct();
964 CPerlStdIO perlStdIO;
968 #define PERL_SYS_INIT(a, c)
971 main(int argc, char **argv, char **env)
976 if(!host.PerlCreate())
980 exitstatus = host.PerlParse(argc, argv, env);
984 exitstatus = host.PerlRun();
992 char *staticlinkmodules[] = {
997 EXTERN_C void boot_DynaLoader _((CV* cv _CPERLarg));
1002 char *file = __FILE__;
1004 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1007 #else /* PERL_OBJECT */
1009 /* Say NO to CPP! Hallelujah! */
1012 * GNU C does not do __declspec()
1014 #define __declspec(foo)
1016 /* Mingw32 defaults to globing command line
1017 * This is inconsistent with other Win32 ports and
1018 * seems to cause trouble with passing -DXSVERSION=\"1.6\"
1019 * So we turn it off like this:
1026 __declspec(dllimport) int RunPerl(int argc, char **argv, char **env, void *ios);
1029 main(int argc, char **argv, char **env)
1031 return RunPerl(argc, argv, env, (void*)0);
1034 #endif /* PERL_OBJECT */