4 extern CPerlObj *pPerl;
6 #define CALLFUNC0RET(x)\
12 #define PROCESS_AND_RETURN \
17 #define CALLFUNCRET(x)\
23 #define CALLFUNCERR(x)\
29 #define LCALLFUNCERR(x)\
35 class CPerlDir : public IPerlDir
39 virtual int Makedir(const char *dirname, int mode, int &err)
41 CALLFUNC0RET(win32_mkdir(dirname, mode));
43 virtual int Chdir(const char *dirname, int &err)
45 CALLFUNC0RET(win32_chdir(dirname));
47 virtual int Rmdir(const char *dirname, int &err)
49 CALLFUNC0RET(win32_rmdir(dirname));
51 virtual int Close(DIR *dirp, int &err)
53 return win32_closedir(dirp);
55 virtual DIR *Open(char *filename, int &err)
57 return win32_opendir(filename);
59 virtual struct direct *Read(DIR *dirp, int &err)
61 return win32_readdir(dirp);
63 virtual void Rewind(DIR *dirp, int &err)
65 win32_rewinddir(dirp);
67 virtual void Seek(DIR *dirp, long loc, int &err)
69 win32_seekdir(dirp, loc);
71 virtual long Tell(DIR *dirp, int &err)
73 return win32_telldir(dirp);
78 extern char * g_win32_get_privlib(char *pl);
79 extern char * g_win32_get_sitelib(char *pl);
81 class CPerlEnv : public IPerlEnv
85 virtual char *Getenv(const char *varname, int &err)
87 return win32_getenv(varname);
89 virtual int Putenv(const char *envstring, int &err)
91 return putenv(envstring);
93 virtual char* LibPath(char *pl)
95 return g_win32_get_privlib(pl);
97 virtual char* SiteLibPath(char *pl)
99 return g_win32_get_sitelib(pl);
103 class CPerlSock : public IPerlSock
107 virtual u_long Htonl(u_long hostlong)
109 return win32_htonl(hostlong);
111 virtual u_short Htons(u_short hostshort)
113 return win32_htons(hostshort);
115 virtual u_long Ntohl(u_long netlong)
117 return win32_ntohl(netlong);
119 virtual u_short Ntohs(u_short netshort)
121 return win32_ntohs(netshort);
124 virtual SOCKET Accept(SOCKET s, struct sockaddr* addr, int* addrlen, int &err)
126 SOCKET r = win32_accept(s, addr, addrlen);
129 virtual int Bind(SOCKET s, const struct sockaddr* name, int namelen, int &err)
131 int r = win32_bind(s, name, namelen);
134 virtual int Connect(SOCKET s, const struct sockaddr* name, int namelen, int &err)
136 int r = win32_connect(s, name, namelen);
139 virtual void Endhostent(int &err)
143 virtual void Endnetent(int &err)
147 virtual void Endprotoent(int &err)
151 virtual void Endservent(int &err)
155 virtual struct hostent* Gethostbyaddr(const char* addr, int len, int type, int &err)
157 struct hostent *r = win32_gethostbyaddr(addr, len, type);
160 virtual struct hostent* Gethostbyname(const char* name, int &err)
162 struct hostent *r = win32_gethostbyname(name);
165 virtual struct hostent* Gethostent(int &err)
167 croak("gethostent not implemented!\n");
170 virtual int Gethostname(char* name, int namelen, int &err)
172 int r = win32_gethostname(name, namelen);
175 virtual struct netent *Getnetbyaddr(long net, int type, int &err)
177 struct netent *r = win32_getnetbyaddr(net, type);
180 virtual struct netent *Getnetbyname(const char *name, int &err)
182 struct netent *r = win32_getnetbyname((char*)name);
185 virtual struct netent *Getnetent(int &err)
187 struct netent *r = win32_getnetent();
190 virtual int Getpeername(SOCKET s, struct sockaddr* name, int* namelen, int &err)
192 int r = win32_getpeername(s, name, namelen);
195 virtual struct protoent* Getprotobyname(const char* name, int &err)
197 struct protoent *r = win32_getprotobyname(name);
200 virtual struct protoent* Getprotobynumber(int number, int &err)
202 struct protoent *r = win32_getprotobynumber(number);
205 virtual struct protoent* Getprotoent(int &err)
207 struct protoent *r = win32_getprotoent();
210 virtual struct servent* Getservbyname(const char* name, const char* proto, int &err)
212 struct servent *r = win32_getservbyname(name, proto);
215 virtual struct servent* Getservbyport(int port, const char* proto, int &err)
217 struct servent *r = win32_getservbyport(port, proto);
220 virtual struct servent* Getservent(int &err)
222 struct servent *r = win32_getservent();
225 virtual int Getsockname(SOCKET s, struct sockaddr* name, int* namelen, int &err)
227 int r = win32_getsockname(s, name, namelen);
230 virtual int Getsockopt(SOCKET s, int level, int optname, char* optval, int* optlen, int &err)
232 int r = win32_getsockopt(s, level, optname, optval, optlen);
235 virtual unsigned long InetAddr(const char* cp, int &err)
237 unsigned long r = win32_inet_addr(cp);
240 virtual char* InetNtoa(struct in_addr in, int &err)
242 char *r = win32_inet_ntoa(in);
245 virtual int Listen(SOCKET s, int backlog, int &err)
247 int r = win32_listen(s, backlog);
250 virtual int Recv(SOCKET s, char* buffer, int len, int flags, int &err)
252 int r = win32_recv(s, buffer, len, flags);
255 virtual int Recvfrom(SOCKET s, char* buffer, int len, int flags, struct sockaddr* from, int* fromlen, int &err)
257 int r = win32_recvfrom(s, buffer, len, flags, from, fromlen);
260 virtual int Select(int nfds, char* readfds, char* writefds, char* exceptfds, const struct timeval* timeout, int &err)
262 int r = win32_select(nfds, (Perl_fd_set*)readfds, (Perl_fd_set*)writefds, (Perl_fd_set*)exceptfds, timeout);
265 virtual int Send(SOCKET s, const char* buffer, int len, int flags, int &err)
267 int r = win32_send(s, buffer, len, flags);
270 virtual int Sendto(SOCKET s, const char* buffer, int len, int flags, const struct sockaddr* to, int tolen, int &err)
272 int r = win32_sendto(s, buffer, len, flags, to, tolen);
275 virtual void Sethostent(int stayopen, int &err)
277 win32_sethostent(stayopen);
279 virtual void Setnetent(int stayopen, int &err)
281 win32_setnetent(stayopen);
283 virtual void Setprotoent(int stayopen, int &err)
285 win32_setprotoent(stayopen);
287 virtual void Setservent(int stayopen, int &err)
289 win32_setservent(stayopen);
291 virtual int Setsockopt(SOCKET s, int level, int optname, const char* optval, int optlen, int &err)
293 int r = win32_setsockopt(s, level, optname, optval, optlen);
296 virtual int Shutdown(SOCKET s, int how, int &err)
298 int r = win32_shutdown(s, how);
301 virtual SOCKET Socket(int af, int type, int protocol, int &err)
303 SOCKET r = win32_socket(af, type, protocol);
306 virtual int Socketpair(int domain, int type, int protocol, int* fds, int &err)
308 croak("socketpair not implemented!\n");
311 virtual int Closesocket(SOCKET s, int& err)
313 int r = win32_closesocket(s);
316 virtual int Ioctlsocket(SOCKET s, long cmd, u_long *argp, int& err)
318 int r = win32_ioctlsocket(s, cmd, argp);
323 class CPerlLIO : public IPerlLIO
327 virtual int Access(const char *path, int mode, int &err)
329 CALLFUNCRET(access(path, mode))
331 virtual int Chmod(const char *filename, int pmode, int &err)
333 CALLFUNCRET(chmod(filename, pmode))
335 virtual int Chown(const char *filename, uid_t owner, gid_t group, int &err)
337 CALLFUNCERR(chown(filename, owner, group))
339 virtual int Chsize(int handle, long size, int &err)
341 CALLFUNCRET(chsize(handle, size))
343 virtual int Close(int handle, int &err)
345 CALLFUNCRET(win32_close(handle))
347 virtual int Dup(int handle, int &err)
349 CALLFUNCERR(win32_dup(handle))
351 virtual int Dup2(int handle1, int handle2, int &err)
353 CALLFUNCERR(win32_dup2(handle1, handle2))
355 virtual int Flock(int fd, int oper, int &err)
357 CALLFUNCERR(win32_flock(fd, oper))
359 virtual int FileStat(int handle, struct stat *buffer, int &err)
361 CALLFUNCERR(fstat(handle, buffer))
363 virtual int IOCtl(int i, unsigned int u, char *data, int &err)
365 CALLFUNCERR(win32_ioctlsocket((SOCKET)i, (long)u, (u_long*)data))
367 virtual int Isatty(int fd, int &err)
371 virtual long Lseek(int handle, long offset, int origin, int &err)
373 LCALLFUNCERR(win32_lseek(handle, offset, origin))
375 virtual int Lstat(const char *path, struct stat *buffer, int &err)
377 return NameStat(path, buffer, err);
379 virtual char *Mktemp(char *Template, int &err)
381 return mktemp(Template);
383 virtual int Open(const char *filename, int oflag, int &err)
385 CALLFUNCERR(win32_open(filename, oflag))
387 virtual int Open(const char *filename, int oflag, int pmode, int &err)
390 if(stricmp(filename, "/dev/null") == 0)
391 ret = open("NUL", oflag, pmode);
393 ret = open(filename, oflag, pmode);
399 virtual int Read(int handle, void *buffer, unsigned int count, int &err)
401 CALLFUNCERR(win32_read(handle, buffer, count))
403 virtual int Rename(const char *OldFileName, const char *newname, int &err)
405 CALLFUNCRET(win32_rename(OldFileName, newname))
407 virtual int Setmode(int handle, int mode, int &err)
409 CALLFUNCRET(win32_setmode(handle, mode))
411 virtual int NameStat(const char *path, struct stat *buffer, int &err)
413 return win32_stat(path, buffer);
415 virtual char *Tmpnam(char *string, int &err)
417 return tmpnam(string);
419 virtual int Umask(int pmode, int &err)
423 virtual int Unlink(const char *filename, int &err)
425 chmod(filename, S_IREAD | S_IWRITE);
426 CALLFUNCRET(unlink(filename))
428 virtual int Utime(char *filename, struct utimbuf *times, int &err)
430 CALLFUNCRET(win32_utime(filename, times))
432 virtual int Write(int handle, const void *buffer, unsigned int count, int &err)
434 CALLFUNCERR(win32_write(handle, buffer, count))
438 class CPerlMem : public IPerlMem
442 virtual void* Malloc(size_t size)
444 return win32_malloc(size);
446 virtual void* Realloc(void* ptr, size_t size)
448 return win32_realloc(ptr, size);
450 virtual void Free(void* ptr)
457 #define EXECF_SPAWN 2
459 extern char * g_getlogin(void);
460 extern int do_spawn2(char *cmd, int exectype);
461 extern int g_do_aspawn(void *vreally, void **vmark, void **vsp);
463 class CPerlProc : public IPerlProc
467 virtual void Abort(void)
471 virtual void Exit(int status)
475 virtual void _Exit(int status)
479 virtual int Execl(const char *cmdname, const char *arg0, const char *arg1, const char *arg2, const char *arg3)
481 return execl(cmdname, arg0, arg1, arg2, arg3);
483 virtual int Execv(const char *cmdname, const char *const *argv)
485 return win32_execvp(cmdname, argv);
487 virtual int Execvp(const char *cmdname, const char *const *argv)
489 return win32_execvp(cmdname, argv);
491 virtual uid_t Getuid(void)
495 virtual uid_t Geteuid(void)
499 virtual gid_t Getgid(void)
503 virtual gid_t Getegid(void)
507 virtual char *Getlogin(void)
511 virtual int Kill(int pid, int sig)
513 return win32_kill(pid, sig);
515 virtual int Killpg(int pid, int sig)
517 croak("killpg not implemented!\n");
520 virtual int PauseProc(void)
522 return win32_sleep((32767L << 16) + 32767);
524 virtual PerlIO* Popen(const char *command, const char *mode)
526 win32_fflush(stdout);
527 win32_fflush(stderr);
528 return (PerlIO*)win32_popen(command, mode);
530 virtual int Pclose(PerlIO *stream)
532 return win32_pclose((FILE*)stream);
534 virtual int Pipe(int *phandles)
536 return win32_pipe(phandles, 512, O_BINARY);
538 virtual int Setuid(uid_t u)
542 virtual int Setgid(gid_t g)
546 virtual int Sleep(unsigned int s)
548 return win32_sleep(s);
550 virtual int Times(struct tms *timebuf)
552 return win32_times(timebuf);
554 virtual int Wait(int *status)
556 return win32_wait(status);
558 virtual int Waitpid(int pid, int *status, int flags)
560 return win32_waitpid(pid, status, flags);
562 virtual Sighandler_t Signal(int sig, Sighandler_t subcode)
566 virtual void GetSysMsg(char*& sMsg, DWORD& dwLen, DWORD dwErr)
568 dwLen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
569 |FORMAT_MESSAGE_IGNORE_INSERTS
570 |FORMAT_MESSAGE_FROM_SYSTEM, NULL,
571 dwErr, 0, (char *)&sMsg, 1, NULL);
573 while (0 < dwLen && isspace(sMsg[--dwLen]))
575 if ('.' != sMsg[dwLen])
580 sMsg = (char*)LocalAlloc(0, 64/**sizeof(TCHAR)*/);
581 dwLen = sprintf(sMsg,
582 "Unknown error #0x%lX (lookup 0x%lX)",
583 dwErr, GetLastError());
586 virtual void FreeBuf(char* sMsg)
590 virtual BOOL DoCmd(char *cmd)
592 do_spawn2(cmd, EXECF_EXEC);
595 virtual int Spawn(char* cmds)
597 return do_spawn2(cmds, EXECF_SPAWN);
599 virtual int Spawnvp(int mode, const char *cmdname, const char *const *argv)
601 return win32_spawnvp(mode, cmdname, argv);
603 virtual int ASpawn(void *vreally, void **vmark, void **vsp)
605 return g_do_aspawn(vreally, vmark, vsp);
610 class CPerlStdIO : public IPerlStdIO
614 virtual PerlIO* Stdin(void)
616 return (PerlIO*)win32_stdin();
618 virtual PerlIO* Stdout(void)
620 return (PerlIO*)win32_stdout();
622 virtual PerlIO* Stderr(void)
624 return (PerlIO*)win32_stderr();
626 virtual PerlIO* Open(const char *path, const char *mode, int &err)
628 PerlIO*pf = (PerlIO*)win32_fopen(path, mode);
633 virtual int Close(PerlIO* pf, int &err)
635 CALLFUNCERR(win32_fclose(((FILE*)pf)))
637 virtual int Eof(PerlIO* pf, int &err)
639 CALLFUNCERR(win32_feof((FILE*)pf))
641 virtual int Error(PerlIO* pf, int &err)
643 CALLFUNCERR(win32_ferror((FILE*)pf))
645 virtual void Clearerr(PerlIO* pf, int &err)
647 win32_clearerr((FILE*)pf);
649 virtual int Getc(PerlIO* pf, int &err)
651 CALLFUNCERR(win32_getc((FILE*)pf))
653 virtual char* GetBase(PerlIO* pf, int &err)
655 return (PerlIO_has_base(pf) ? PerlIO_get_base(pf) : Nullch);
657 virtual int GetBufsiz(PerlIO* pf, int &err)
659 return PerlIO_get_bufsiz(pf);
661 virtual int GetCnt(PerlIO* pf, int &err)
663 return PerlIO_get_cnt(pf);
665 virtual char* GetPtr(PerlIO* pf, int &err)
667 return PerlIO_get_ptr(pf);
669 virtual char* Gets(PerlIO* pf, char* s, int n, int& err)
671 char* ret = win32_fgets(s, n, (FILE*)pf);
676 virtual int Putc(PerlIO* pf, int c, int &err)
678 CALLFUNCERR(win32_fputc(c, (FILE*)pf))
680 virtual int Puts(PerlIO* pf, const char *s, int &err)
682 CALLFUNCERR(win32_fputs(s, (FILE*)pf))
684 virtual int Flush(PerlIO* pf, int &err)
686 CALLFUNCERR(win32_fflush((FILE*)pf))
688 virtual int Ungetc(PerlIO* pf,int c, int &err)
690 CALLFUNCERR(win32_ungetc(c, (FILE*)pf))
692 virtual int Fileno(PerlIO* pf, int &err)
694 CALLFUNCERR(win32_fileno((FILE*)pf))
696 virtual PerlIO* Fdopen(int fd, const char *mode, int &err)
698 PerlIO* pf = (PerlIO*)win32_fdopen(fd, mode);
703 virtual PerlIO* Reopen(const char*path, const char*mode, PerlIO* pf, int &err)
705 PerlIO* newPf = (PerlIO*)win32_freopen(path, mode, (FILE*)pf);
710 virtual SSize_t Read(PerlIO* pf, void *buffer, Size_t size, int &err)
712 SSize_t i = win32_fread(buffer, 1, size, (FILE*)pf);
717 virtual SSize_t Write(PerlIO* pf, const void *buffer, Size_t size, int &err)
719 SSize_t i = win32_fwrite(buffer, 1, size, (FILE*)pf);
724 virtual void SetBuf(PerlIO* pf, char* buffer, int &err)
726 win32_setbuf((FILE*)pf, buffer);
728 virtual int SetVBuf(PerlIO* pf, char* buffer, int type, Size_t size, int &err)
730 int i = win32_setvbuf((FILE*)pf, buffer, type, size);
735 virtual void SetCnt(PerlIO* pf, int n, int &err)
737 if (PerlIO_canset_cnt(pf)) {
738 PerlIO_set_cnt(pf,n);
741 virtual void SetPtrCnt(PerlIO* pf, char * ptr, int n, int& err)
743 if (PerlIO_canset_cnt(pf)) {
744 PerlIO_set_ptrcnt(pf,ptr,n);
747 virtual void Setlinebuf(PerlIO* pf, int &err)
749 win32_setvbuf((FILE*)pf, NULL, _IOLBF, 0);
751 virtual int Printf(PerlIO* pf, int &err, const char *format,...)
754 va_start(arglist, format);
755 int i = win32_vfprintf((FILE*)pf, format, arglist);
760 virtual int Vprintf(PerlIO* pf, int &err, const char *format, va_list arglist)
762 int i = win32_vfprintf((FILE*)pf, format, arglist);
767 virtual long Tell(PerlIO* pf, int &err)
769 long l = win32_ftell((FILE*)pf);
774 virtual int Seek(PerlIO* pf, off_t offset, int origin, int &err)
776 int i = win32_fseek((FILE*)pf, offset, origin);
781 virtual void Rewind(PerlIO* pf, int &err)
783 win32_rewind((FILE*)pf);
785 virtual PerlIO* Tmpfile(int &err)
787 PerlIO* pf = (PerlIO*)win32_tmpfile();
792 virtual int Getpos(PerlIO* pf, Fpos_t *p, int &err)
794 int i = win32_fgetpos((FILE*)pf, p);
799 virtual int Setpos(PerlIO* pf, const Fpos_t *p, int &err)
801 int i = win32_fsetpos((FILE*)pf, p);
806 virtual void Init(int &err)
809 virtual void InitOSExtras(void* p)
811 Perl_init_os_extras();
813 virtual int OpenOSfhandle(long osfhandle, int flags)
815 return win32_open_osfhandle(osfhandle, flags);
817 virtual int GetOSfhandle(int filenum)
819 return win32_get_osfhandle(filenum);
826 CPerlHost() { pPerl = NULL; };
827 inline BOOL PerlCreate(void)
831 pPerl = perl_alloc(&perlMem, &perlEnv, &perlStdIO, &perlLIO,
832 &perlDir, &perlSock, &perlProc);
837 pPerl->perl_construct();
841 win32_fprintf(stderr, "%s\n",
842 "Error: Unable to construct data structures");
850 win32_fprintf(stderr, "%s\n", "Error: Unable to allocate memory");
853 return (pPerl != NULL);
855 inline int PerlParse(void (*xs_init)(CPerlObj*), int argc, char** argv, char** env)
860 retVal = pPerl->perl_parse(xs_init, argc, argv, env);
864 // this is where exit() should arrive
869 win32_fprintf(stderr, "Error: Parse exception\n");
875 inline int PerlRun(void)
880 retVal = pPerl->perl_run();
884 // this is where exit() should arrive
889 win32_fprintf(stderr, "Error: Runtime exception\n");
894 inline void PerlDestroy(void)
898 pPerl->perl_destruct();
913 CPerlStdIO perlStdIO;