This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
support POSIX::uname() via win32_uname()
[perl5.git] / win32 / perlhost.h
CommitLineData
0f4eea8f
DL
1
2#include "iperlsys.h"
3
4extern CPerlObj *pPerl;
5
6#define CALLFUNC0RET(x)\
7 int ret = x;\
8 if (ret < 0)\
9 err = errno;\
10 return ret;
11
12#define PROCESS_AND_RETURN \
13 if (errno) \
14 err = errno; \
15 return r
16
17#define CALLFUNCRET(x)\
18 int ret = x;\
19 if (ret)\
20 err = errno;\
21 return ret;
22
23#define CALLFUNCERR(x)\
24 int ret = x;\
25 if (errno)\
26 err = errno;\
27 return ret;
28
29#define LCALLFUNCERR(x)\
30 long ret = x;\
31 if (errno)\
32 err = errno;\
33 return ret;
34
0f4eea8f
DL
35class CPerlDir : public IPerlDir
36{
37public:
38 CPerlDir() {};
39 virtual int Makedir(const char *dirname, int mode, int &err)
40 {
41 CALLFUNC0RET(win32_mkdir(dirname, mode));
42 };
43 virtual int Chdir(const char *dirname, int &err)
44 {
45 CALLFUNC0RET(win32_chdir(dirname));
46 };
47 virtual int Rmdir(const char *dirname, int &err)
48 {
49 CALLFUNC0RET(win32_rmdir(dirname));
50 };
51 virtual int Close(DIR *dirp, int &err)
52 {
72311751 53 return win32_closedir(dirp);
0f4eea8f
DL
54 };
55 virtual DIR *Open(char *filename, int &err)
56 {
72311751 57 return win32_opendir(filename);
0f4eea8f
DL
58 };
59 virtual struct direct *Read(DIR *dirp, int &err)
60 {
72311751 61 return win32_readdir(dirp);
0f4eea8f
DL
62 };
63 virtual void Rewind(DIR *dirp, int &err)
64 {
72311751 65 win32_rewinddir(dirp);
0f4eea8f
DL
66 };
67 virtual void Seek(DIR *dirp, long loc, int &err)
68 {
72311751 69 win32_seekdir(dirp, loc);
0f4eea8f
DL
70 };
71 virtual long Tell(DIR *dirp, int &err)
72 {
72311751 73 return win32_telldir(dirp);
0f4eea8f
DL
74 };
75};
76
77
78extern char * g_win32_get_privlib(char *pl);
79extern char * g_win32_get_sitelib(char *pl);
80
81class CPerlEnv : public IPerlEnv
82{
83public:
84 CPerlEnv() {};
85 virtual char *Getenv(const char *varname, int &err)
86 {
87 return win32_getenv(varname);
88 };
89 virtual int Putenv(const char *envstring, int &err)
90 {
ac5c734f 91 return win32_putenv(envstring);
0f4eea8f
DL
92 };
93 virtual char* LibPath(char *pl)
94 {
95 return g_win32_get_privlib(pl);
96 };
97 virtual char* SiteLibPath(char *pl)
98 {
99 return g_win32_get_sitelib(pl);
100 };
b2af26b1
GS
101 virtual int Uname(struct utsname *name, int &err)
102 {
103 return win32_uname(name);
104 };
0f4eea8f
DL
105};
106
107class CPerlSock : public IPerlSock
108{
109public:
110 CPerlSock() {};
111 virtual u_long Htonl(u_long hostlong)
112 {
113 return win32_htonl(hostlong);
114 };
115 virtual u_short Htons(u_short hostshort)
116 {
117 return win32_htons(hostshort);
118 };
119 virtual u_long Ntohl(u_long netlong)
120 {
121 return win32_ntohl(netlong);
122 };
123 virtual u_short Ntohs(u_short netshort)
124 {
125 return win32_ntohs(netshort);
126 }
127
128 virtual SOCKET Accept(SOCKET s, struct sockaddr* addr, int* addrlen, int &err)
129 {
130 SOCKET r = win32_accept(s, addr, addrlen);
131 PROCESS_AND_RETURN;
132 };
133 virtual int Bind(SOCKET s, const struct sockaddr* name, int namelen, int &err)
134 {
135 int r = win32_bind(s, name, namelen);
136 PROCESS_AND_RETURN;
137 };
138 virtual int Connect(SOCKET s, const struct sockaddr* name, int namelen, int &err)
139 {
140 int r = win32_connect(s, name, namelen);
141 PROCESS_AND_RETURN;
142 };
143 virtual void Endhostent(int &err)
144 {
145 win32_endhostent();
146 };
147 virtual void Endnetent(int &err)
148 {
149 win32_endnetent();
150 };
151 virtual void Endprotoent(int &err)
152 {
153 win32_endprotoent();
154 };
155 virtual void Endservent(int &err)
156 {
157 win32_endservent();
158 };
159 virtual struct hostent* Gethostbyaddr(const char* addr, int len, int type, int &err)
160 {
161 struct hostent *r = win32_gethostbyaddr(addr, len, type);
162 PROCESS_AND_RETURN;
163 };
164 virtual struct hostent* Gethostbyname(const char* name, int &err)
165 {
166 struct hostent *r = win32_gethostbyname(name);
167 PROCESS_AND_RETURN;
168 };
169 virtual struct hostent* Gethostent(int &err)
170 {
171 croak("gethostent not implemented!\n");
172 return NULL;
173 };
174 virtual int Gethostname(char* name, int namelen, int &err)
175 {
176 int r = win32_gethostname(name, namelen);
177 PROCESS_AND_RETURN;
178 };
179 virtual struct netent *Getnetbyaddr(long net, int type, int &err)
180 {
181 struct netent *r = win32_getnetbyaddr(net, type);
182 PROCESS_AND_RETURN;
183 };
184 virtual struct netent *Getnetbyname(const char *name, int &err)
185 {
186 struct netent *r = win32_getnetbyname((char*)name);
187 PROCESS_AND_RETURN;
188 };
189 virtual struct netent *Getnetent(int &err)
190 {
191 struct netent *r = win32_getnetent();
192 PROCESS_AND_RETURN;
193 };
194 virtual int Getpeername(SOCKET s, struct sockaddr* name, int* namelen, int &err)
195 {
196 int r = win32_getpeername(s, name, namelen);
197 PROCESS_AND_RETURN;
198 };
199 virtual struct protoent* Getprotobyname(const char* name, int &err)
200 {
201 struct protoent *r = win32_getprotobyname(name);
202 PROCESS_AND_RETURN;
203 };
204 virtual struct protoent* Getprotobynumber(int number, int &err)
205 {
206 struct protoent *r = win32_getprotobynumber(number);
207 PROCESS_AND_RETURN;
208 };
209 virtual struct protoent* Getprotoent(int &err)
210 {
211 struct protoent *r = win32_getprotoent();
212 PROCESS_AND_RETURN;
213 };
214 virtual struct servent* Getservbyname(const char* name, const char* proto, int &err)
215 {
216 struct servent *r = win32_getservbyname(name, proto);
217 PROCESS_AND_RETURN;
218 };
219 virtual struct servent* Getservbyport(int port, const char* proto, int &err)
220 {
221 struct servent *r = win32_getservbyport(port, proto);
222 PROCESS_AND_RETURN;
223 };
224 virtual struct servent* Getservent(int &err)
225 {
226 struct servent *r = win32_getservent();
227 PROCESS_AND_RETURN;
228 };
229 virtual int Getsockname(SOCKET s, struct sockaddr* name, int* namelen, int &err)
230 {
231 int r = win32_getsockname(s, name, namelen);
232 PROCESS_AND_RETURN;
233 };
234 virtual int Getsockopt(SOCKET s, int level, int optname, char* optval, int* optlen, int &err)
235 {
236 int r = win32_getsockopt(s, level, optname, optval, optlen);
237 PROCESS_AND_RETURN;
238 };
239 virtual unsigned long InetAddr(const char* cp, int &err)
240 {
241 unsigned long r = win32_inet_addr(cp);
242 PROCESS_AND_RETURN;
243 };
244 virtual char* InetNtoa(struct in_addr in, int &err)
245 {
246 char *r = win32_inet_ntoa(in);
247 PROCESS_AND_RETURN;
248 };
249 virtual int Listen(SOCKET s, int backlog, int &err)
250 {
251 int r = win32_listen(s, backlog);
252 PROCESS_AND_RETURN;
253 };
254 virtual int Recv(SOCKET s, char* buffer, int len, int flags, int &err)
255 {
256 int r = win32_recv(s, buffer, len, flags);
257 PROCESS_AND_RETURN;
258 };
259 virtual int Recvfrom(SOCKET s, char* buffer, int len, int flags, struct sockaddr* from, int* fromlen, int &err)
260 {
261 int r = win32_recvfrom(s, buffer, len, flags, from, fromlen);
262 PROCESS_AND_RETURN;
263 };
264 virtual int Select(int nfds, char* readfds, char* writefds, char* exceptfds, const struct timeval* timeout, int &err)
265 {
266 int r = win32_select(nfds, (Perl_fd_set*)readfds, (Perl_fd_set*)writefds, (Perl_fd_set*)exceptfds, timeout);
267 PROCESS_AND_RETURN;
268 };
269 virtual int Send(SOCKET s, const char* buffer, int len, int flags, int &err)
270 {
271 int r = win32_send(s, buffer, len, flags);
272 PROCESS_AND_RETURN;
273 };
274 virtual int Sendto(SOCKET s, const char* buffer, int len, int flags, const struct sockaddr* to, int tolen, int &err)
275 {
276 int r = win32_sendto(s, buffer, len, flags, to, tolen);
277 PROCESS_AND_RETURN;
278 };
279 virtual void Sethostent(int stayopen, int &err)
280 {
281 win32_sethostent(stayopen);
282 };
283 virtual void Setnetent(int stayopen, int &err)
284 {
285 win32_setnetent(stayopen);
286 };
287 virtual void Setprotoent(int stayopen, int &err)
288 {
289 win32_setprotoent(stayopen);
290 };
291 virtual void Setservent(int stayopen, int &err)
292 {
293 win32_setservent(stayopen);
294 };
295 virtual int Setsockopt(SOCKET s, int level, int optname, const char* optval, int optlen, int &err)
296 {
297 int r = win32_setsockopt(s, level, optname, optval, optlen);
298 PROCESS_AND_RETURN;
299 };
300 virtual int Shutdown(SOCKET s, int how, int &err)
301 {
302 int r = win32_shutdown(s, how);
303 PROCESS_AND_RETURN;
304 };
305 virtual SOCKET Socket(int af, int type, int protocol, int &err)
306 {
307 SOCKET r = win32_socket(af, type, protocol);
308 PROCESS_AND_RETURN;
309 };
310 virtual int Socketpair(int domain, int type, int protocol, int* fds, int &err)
311 {
312 croak("socketpair not implemented!\n");
313 return 0;
314 };
315 virtual int Closesocket(SOCKET s, int& err)
316 {
317 int r = win32_closesocket(s);
318 PROCESS_AND_RETURN;
319 };
320 virtual int Ioctlsocket(SOCKET s, long cmd, u_long *argp, int& err)
321 {
322 int r = win32_ioctlsocket(s, cmd, argp);
323 PROCESS_AND_RETURN;
324 };
325};
326
327class CPerlLIO : public IPerlLIO
328{
329public:
330 CPerlLIO() {};
331 virtual int Access(const char *path, int mode, int &err)
332 {
333 CALLFUNCRET(access(path, mode))
334 };
335 virtual int Chmod(const char *filename, int pmode, int &err)
336 {
337 CALLFUNCRET(chmod(filename, pmode))
338 };
339 virtual int Chown(const char *filename, uid_t owner, gid_t group, int &err)
340 {
341 CALLFUNCERR(chown(filename, owner, group))
342 };
343 virtual int Chsize(int handle, long size, int &err)
344 {
345 CALLFUNCRET(chsize(handle, size))
346 };
347 virtual int Close(int handle, int &err)
348 {
349 CALLFUNCRET(win32_close(handle))
350 };
351 virtual int Dup(int handle, int &err)
352 {
353 CALLFUNCERR(win32_dup(handle))
354 };
355 virtual int Dup2(int handle1, int handle2, int &err)
356 {
357 CALLFUNCERR(win32_dup2(handle1, handle2))
358 };
359 virtual int Flock(int fd, int oper, int &err)
360 {
361 CALLFUNCERR(win32_flock(fd, oper))
362 };
363 virtual int FileStat(int handle, struct stat *buffer, int &err)
364 {
365 CALLFUNCERR(fstat(handle, buffer))
366 };
367 virtual int IOCtl(int i, unsigned int u, char *data, int &err)
368 {
369 CALLFUNCERR(win32_ioctlsocket((SOCKET)i, (long)u, (u_long*)data))
370 };
371 virtual int Isatty(int fd, int &err)
372 {
373 return isatty(fd);
374 };
375 virtual long Lseek(int handle, long offset, int origin, int &err)
376 {
377 LCALLFUNCERR(win32_lseek(handle, offset, origin))
378 };
379 virtual int Lstat(const char *path, struct stat *buffer, int &err)
380 {
381 return NameStat(path, buffer, err);
382 };
383 virtual char *Mktemp(char *Template, int &err)
384 {
385 return mktemp(Template);
386 };
387 virtual int Open(const char *filename, int oflag, int &err)
388 {
389 CALLFUNCERR(win32_open(filename, oflag))
390 };
391 virtual int Open(const char *filename, int oflag, int pmode, int &err)
392 {
393 int ret;
394 if(stricmp(filename, "/dev/null") == 0)
395 ret = open("NUL", oflag, pmode);
396 else
397 ret = open(filename, oflag, pmode);
398
399 if(errno)
400 err = errno;
401 return ret;
402 };
403 virtual int Read(int handle, void *buffer, unsigned int count, int &err)
404 {
405 CALLFUNCERR(win32_read(handle, buffer, count))
406 };
407 virtual int Rename(const char *OldFileName, const char *newname, int &err)
408 {
e24c7c18 409 CALLFUNCRET(win32_rename(OldFileName, newname))
0f4eea8f
DL
410 };
411 virtual int Setmode(int handle, int mode, int &err)
412 {
413 CALLFUNCRET(win32_setmode(handle, mode))
414 };
415 virtual int NameStat(const char *path, struct stat *buffer, int &err)
416 {
417 return win32_stat(path, buffer);
418 };
419 virtual char *Tmpnam(char *string, int &err)
420 {
421 return tmpnam(string);
422 };
423 virtual int Umask(int pmode, int &err)
424 {
425 return umask(pmode);
426 };
427 virtual int Unlink(const char *filename, int &err)
428 {
429 chmod(filename, S_IREAD | S_IWRITE);
430 CALLFUNCRET(unlink(filename))
431 };
432 virtual int Utime(char *filename, struct utimbuf *times, int &err)
433 {
434 CALLFUNCRET(win32_utime(filename, times))
435 };
436 virtual int Write(int handle, const void *buffer, unsigned int count, int &err)
437 {
438 CALLFUNCERR(win32_write(handle, buffer, count))
439 };
440};
441
442class CPerlMem : public IPerlMem
443{
444public:
445 CPerlMem() {};
446 virtual void* Malloc(size_t size)
447 {
448 return win32_malloc(size);
449 };
450 virtual void* Realloc(void* ptr, size_t size)
451 {
452 return win32_realloc(ptr, size);
453 };
454 virtual void Free(void* ptr)
455 {
456 win32_free(ptr);
457 };
458};
459
460#define EXECF_EXEC 1
461#define EXECF_SPAWN 2
462
463extern char * g_getlogin(void);
464extern int do_spawn2(char *cmd, int exectype);
465extern int g_do_aspawn(void *vreally, void **vmark, void **vsp);
466
467class CPerlProc : public IPerlProc
468{
469public:
470 CPerlProc() {};
471 virtual void Abort(void)
472 {
473 win32_abort();
474 };
ff95b63e
DL
475 virtual char * Crypt(const char* clear, const char* salt)
476 {
477 return win32_crypt(clear, salt);
478 };
0f4eea8f
DL
479 virtual void Exit(int status)
480 {
481 exit(status);
482 };
483 virtual void _Exit(int status)
484 {
485 _exit(status);
486 };
487 virtual int Execl(const char *cmdname, const char *arg0, const char *arg1, const char *arg2, const char *arg3)
488 {
489 return execl(cmdname, arg0, arg1, arg2, arg3);
490 };
491 virtual int Execv(const char *cmdname, const char *const *argv)
492 {
493 return win32_execvp(cmdname, argv);
494 };
495 virtual int Execvp(const char *cmdname, const char *const *argv)
496 {
497 return win32_execvp(cmdname, argv);
498 };
499 virtual uid_t Getuid(void)
500 {
501 return getuid();
502 };
503 virtual uid_t Geteuid(void)
504 {
505 return geteuid();
506 };
507 virtual gid_t Getgid(void)
508 {
509 return getgid();
510 };
511 virtual gid_t Getegid(void)
512 {
513 return getegid();
514 };
515 virtual char *Getlogin(void)
516 {
517 return g_getlogin();
518 };
519 virtual int Kill(int pid, int sig)
520 {
521 return win32_kill(pid, sig);
522 };
523 virtual int Killpg(int pid, int sig)
524 {
525 croak("killpg not implemented!\n");
526 return 0;
527 };
528 virtual int PauseProc(void)
529 {
530 return win32_sleep((32767L << 16) + 32767);
531 };
532 virtual PerlIO* Popen(const char *command, const char *mode)
533 {
534 win32_fflush(stdout);
535 win32_fflush(stderr);
536 return (PerlIO*)win32_popen(command, mode);
537 };
538 virtual int Pclose(PerlIO *stream)
539 {
540 return win32_pclose((FILE*)stream);
541 };
542 virtual int Pipe(int *phandles)
543 {
544 return win32_pipe(phandles, 512, O_BINARY);
545 };
546 virtual int Setuid(uid_t u)
547 {
548 return setuid(u);
549 };
550 virtual int Setgid(gid_t g)
551 {
552 return setgid(g);
553 };
554 virtual int Sleep(unsigned int s)
555 {
556 return win32_sleep(s);
557 };
558 virtual int Times(struct tms *timebuf)
559 {
560 return win32_times(timebuf);
561 };
562 virtual int Wait(int *status)
563 {
564 return win32_wait(status);
565 };
566 virtual int Waitpid(int pid, int *status, int flags)
567 {
568 return win32_waitpid(pid, status, flags);
569 };
570 virtual Sighandler_t Signal(int sig, Sighandler_t subcode)
571 {
572 return 0;
573 };
574 virtual void GetSysMsg(char*& sMsg, DWORD& dwLen, DWORD dwErr)
575 {
576 dwLen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
577 |FORMAT_MESSAGE_IGNORE_INSERTS
578 |FORMAT_MESSAGE_FROM_SYSTEM, NULL,
579 dwErr, 0, (char *)&sMsg, 1, NULL);
580 if (0 < dwLen) {
581 while (0 < dwLen && isspace(sMsg[--dwLen]))
582 ;
583 if ('.' != sMsg[dwLen])
584 dwLen++;
585 sMsg[dwLen]= '\0';
586 }
587 if (0 == dwLen) {
588 sMsg = (char*)LocalAlloc(0, 64/**sizeof(TCHAR)*/);
589 dwLen = sprintf(sMsg,
590 "Unknown error #0x%lX (lookup 0x%lX)",
591 dwErr, GetLastError());
592 }
593 };
594 virtual void FreeBuf(char* sMsg)
595 {
596 LocalFree(sMsg);
597 };
598 virtual BOOL DoCmd(char *cmd)
599 {
600 do_spawn2(cmd, EXECF_EXEC);
601 return FALSE;
602 };
603 virtual int Spawn(char* cmds)
604 {
605 return do_spawn2(cmds, EXECF_SPAWN);
606 };
607 virtual int Spawnvp(int mode, const char *cmdname, const char *const *argv)
608 {
609 return win32_spawnvp(mode, cmdname, argv);
610 };
611 virtual int ASpawn(void *vreally, void **vmark, void **vsp)
612 {
613 return g_do_aspawn(vreally, vmark, vsp);
614 };
615};
616
617
618class CPerlStdIO : public IPerlStdIO
619{
620public:
621 CPerlStdIO() {};
622 virtual PerlIO* Stdin(void)
623 {
624 return (PerlIO*)win32_stdin();
625 };
626 virtual PerlIO* Stdout(void)
627 {
628 return (PerlIO*)win32_stdout();
629 };
630 virtual PerlIO* Stderr(void)
631 {
632 return (PerlIO*)win32_stderr();
633 };
634 virtual PerlIO* Open(const char *path, const char *mode, int &err)
635 {
636 PerlIO*pf = (PerlIO*)win32_fopen(path, mode);
637 if(errno)
638 err = errno;
639 return pf;
640 };
641 virtual int Close(PerlIO* pf, int &err)
642 {
643 CALLFUNCERR(win32_fclose(((FILE*)pf)))
644 };
645 virtual int Eof(PerlIO* pf, int &err)
646 {
647 CALLFUNCERR(win32_feof((FILE*)pf))
648 };
649 virtual int Error(PerlIO* pf, int &err)
650 {
651 CALLFUNCERR(win32_ferror((FILE*)pf))
652 };
653 virtual void Clearerr(PerlIO* pf, int &err)
654 {
655 win32_clearerr((FILE*)pf);
656 };
657 virtual int Getc(PerlIO* pf, int &err)
658 {
659 CALLFUNCERR(win32_getc((FILE*)pf))
660 };
661 virtual char* GetBase(PerlIO* pf, int &err)
662 {
2bd2b9e0
GS
663#ifdef FILE_base
664 FILE *f = (FILE*)pf;
665 return FILE_base(f);
666#else
667 return Nullch;
668#endif
0f4eea8f
DL
669 };
670 virtual int GetBufsiz(PerlIO* pf, int &err)
671 {
7a5f8e82 672#ifdef FILE_bufsiz
2bd2b9e0
GS
673 FILE *f = (FILE*)pf;
674 return FILE_bufsiz(f);
675#else
676 return (-1);
677#endif
0f4eea8f
DL
678 };
679 virtual int GetCnt(PerlIO* pf, int &err)
680 {
2bd2b9e0
GS
681#ifdef USE_STDIO_PTR
682 FILE *f = (FILE*)pf;
683 return FILE_cnt(f);
684#else
685 return (-1);
686#endif
0f4eea8f
DL
687 };
688 virtual char* GetPtr(PerlIO* pf, int &err)
689 {
2bd2b9e0
GS
690#ifdef USE_STDIO_PTR
691 FILE *f = (FILE*)pf;
692 return FILE_ptr(f);
693#else
694 return Nullch;
695#endif
0f4eea8f
DL
696 };
697 virtual char* Gets(PerlIO* pf, char* s, int n, int& err)
698 {
699 char* ret = win32_fgets(s, n, (FILE*)pf);
700 if(errno)
701 err = errno;
702 return ret;
703 };
704 virtual int Putc(PerlIO* pf, int c, int &err)
705 {
706 CALLFUNCERR(win32_fputc(c, (FILE*)pf))
707 };
708 virtual int Puts(PerlIO* pf, const char *s, int &err)
709 {
710 CALLFUNCERR(win32_fputs(s, (FILE*)pf))
711 };
712 virtual int Flush(PerlIO* pf, int &err)
713 {
714 CALLFUNCERR(win32_fflush((FILE*)pf))
715 };
716 virtual int Ungetc(PerlIO* pf,int c, int &err)
717 {
718 CALLFUNCERR(win32_ungetc(c, (FILE*)pf))
719 };
720 virtual int Fileno(PerlIO* pf, int &err)
721 {
722 CALLFUNCERR(win32_fileno((FILE*)pf))
723 };
724 virtual PerlIO* Fdopen(int fd, const char *mode, int &err)
725 {
726 PerlIO* pf = (PerlIO*)win32_fdopen(fd, mode);
727 if(errno)
728 err = errno;
729 return pf;
730 };
731 virtual PerlIO* Reopen(const char*path, const char*mode, PerlIO* pf, int &err)
732 {
733 PerlIO* newPf = (PerlIO*)win32_freopen(path, mode, (FILE*)pf);
734 if(errno)
735 err = errno;
736 return newPf;
737 };
738 virtual SSize_t Read(PerlIO* pf, void *buffer, Size_t size, int &err)
739 {
740 SSize_t i = win32_fread(buffer, 1, size, (FILE*)pf);
741 if(errno)
742 err = errno;
743 return i;
744 };
745 virtual SSize_t Write(PerlIO* pf, const void *buffer, Size_t size, int &err)
746 {
747 SSize_t i = win32_fwrite(buffer, 1, size, (FILE*)pf);
748 if(errno)
749 err = errno;
750 return i;
751 };
752 virtual void SetBuf(PerlIO* pf, char* buffer, int &err)
753 {
754 win32_setbuf((FILE*)pf, buffer);
755 };
756 virtual int SetVBuf(PerlIO* pf, char* buffer, int type, Size_t size, int &err)
757 {
758 int i = win32_setvbuf((FILE*)pf, buffer, type, size);
759 if(errno)
760 err = errno;
761 return i;
762 };
763 virtual void SetCnt(PerlIO* pf, int n, int &err)
764 {
2bd2b9e0
GS
765#ifdef STDIO_CNT_LVALUE
766 FILE *f = (FILE*)pf;
767 FILE_cnt(f) = n;
768#endif
0f4eea8f
DL
769 };
770 virtual void SetPtrCnt(PerlIO* pf, char * ptr, int n, int& err)
771 {
2bd2b9e0
GS
772#ifdef STDIO_PTR_LVALUE
773 FILE *f = (FILE*)pf;
774 FILE_ptr(f) = ptr;
775 FILE_cnt(f) = n;
776#endif
0f4eea8f
DL
777 };
778 virtual void Setlinebuf(PerlIO* pf, int &err)
779 {
780 win32_setvbuf((FILE*)pf, NULL, _IOLBF, 0);
781 };
782 virtual int Printf(PerlIO* pf, int &err, const char *format,...)
783 {
784 va_list(arglist);
785 va_start(arglist, format);
786 int i = win32_vfprintf((FILE*)pf, format, arglist);
787 if(errno)
788 err = errno;
789 return i;
790 };
791 virtual int Vprintf(PerlIO* pf, int &err, const char *format, va_list arglist)
792 {
793 int i = win32_vfprintf((FILE*)pf, format, arglist);
794 if(errno)
795 err = errno;
796 return i;
797 };
798 virtual long Tell(PerlIO* pf, int &err)
799 {
800 long l = win32_ftell((FILE*)pf);
801 if(errno)
802 err = errno;
803 return l;
804 };
805 virtual int Seek(PerlIO* pf, off_t offset, int origin, int &err)
806 {
807 int i = win32_fseek((FILE*)pf, offset, origin);
808 if(errno)
809 err = errno;
810 return i;
811 };
812 virtual void Rewind(PerlIO* pf, int &err)
813 {
814 win32_rewind((FILE*)pf);
815 };
816 virtual PerlIO* Tmpfile(int &err)
817 {
818 PerlIO* pf = (PerlIO*)win32_tmpfile();
819 if(errno)
820 err = errno;
821 return pf;
822 };
823 virtual int Getpos(PerlIO* pf, Fpos_t *p, int &err)
824 {
825 int i = win32_fgetpos((FILE*)pf, p);
826 if(errno)
827 err = errno;
828 return i;
829 };
830 virtual int Setpos(PerlIO* pf, const Fpos_t *p, int &err)
831 {
832 int i = win32_fsetpos((FILE*)pf, p);
833 if(errno)
834 err = errno;
835 return i;
836 };
837 virtual void Init(int &err)
838 {
839 };
840 virtual void InitOSExtras(void* p)
841 {
842 Perl_init_os_extras();
843 };
844 virtual int OpenOSfhandle(long osfhandle, int flags)
845 {
846 return win32_open_osfhandle(osfhandle, flags);
847 }
848 virtual int GetOSfhandle(int filenum)
849 {
850 return win32_get_osfhandle(filenum);
851 }
852};
853
854class CPerlHost
855{
856public:
857 CPerlHost() { pPerl = NULL; };
858 inline BOOL PerlCreate(void)
859 {
860 try
861 {
862 pPerl = perl_alloc(&perlMem, &perlEnv, &perlStdIO, &perlLIO,
863 &perlDir, &perlSock, &perlProc);
864 if(pPerl != NULL)
865 {
866 try
867 {
22c35a8c 868 perl_construct();
0f4eea8f
DL
869 }
870 catch(...)
871 {
872 win32_fprintf(stderr, "%s\n",
873 "Error: Unable to construct data structures");
22c35a8c 874 perl_free();
0f4eea8f
DL
875 pPerl = NULL;
876 }
877 }
878 }
879 catch(...)
880 {
881 win32_fprintf(stderr, "%s\n", "Error: Unable to allocate memory");
882 pPerl = NULL;
883 }
884 return (pPerl != NULL);
885 };
886 inline int PerlParse(void (*xs_init)(CPerlObj*), int argc, char** argv, char** env)
887 {
888 int retVal;
889 try
890 {
22c35a8c 891 retVal = perl_parse(xs_init, argc, argv, env);
0f4eea8f
DL
892 }
893 catch(int x)
894 {
895 // this is where exit() should arrive
896 retVal = x;
897 }
898 catch(...)
899 {
900 win32_fprintf(stderr, "Error: Parse exception\n");
901 retVal = -1;
902 }
903 *win32_errno() = 0;
904 return retVal;
905 };
906 inline int PerlRun(void)
907 {
908 int retVal;
909 try
910 {
22c35a8c 911 retVal = perl_run();
0f4eea8f
DL
912 }
913 catch(int x)
914 {
915 // this is where exit() should arrive
916 retVal = x;
917 }
918 catch(...)
919 {
920 win32_fprintf(stderr, "Error: Runtime exception\n");
921 retVal = -1;
922 }
923 return retVal;
924 };
925 inline void PerlDestroy(void)
926 {
927 try
928 {
22c35a8c
GS
929 perl_destruct();
930 perl_free();
0f4eea8f
DL
931 }
932 catch(...)
933 {
934 }
935 };
936
937protected:
938 CPerlDir perlDir;
939 CPerlEnv perlEnv;
940 CPerlLIO perlLIO;
941 CPerlMem perlMem;
942 CPerlProc perlProc;
943 CPerlSock perlSock;
944 CPerlStdIO perlStdIO;
945};