summaryrefslogtreecommitdiff
path: root/win32/perlhost.h
blob: cc5b5e5cd45cf4fdf2b515c6575cb6c820b7a6dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945

#include "iperlsys.h"

extern CPerlObj *pPerl;

#define CALLFUNC0RET(x)\
    int ret = x;\
    if (ret < 0)\
	err = errno;\
    return ret;

#define PROCESS_AND_RETURN  \
    if (errno)		    \
	err = errno;	    \
    return r

#define CALLFUNCRET(x)\
    int ret = x;\
    if (ret)\
	err = errno;\
    return ret;

#define CALLFUNCERR(x)\
    int ret = x;\
    if (errno)\
	err = errno;\
    return ret;

#define LCALLFUNCERR(x)\
    long ret = x;\
    if (errno)\
	err = errno;\
    return ret;

class CPerlDir : public IPerlDir
{
public:
    CPerlDir() {};
    virtual int Makedir(const char *dirname, int mode, int &err)
    {
	CALLFUNC0RET(win32_mkdir(dirname, mode));
    };
    virtual int Chdir(const char *dirname, int &err)
    {
	CALLFUNC0RET(win32_chdir(dirname));
    };
    virtual int Rmdir(const char *dirname, int &err)
    {
	CALLFUNC0RET(win32_rmdir(dirname));
    };
    virtual int Close(DIR *dirp, int &err)
    {
	return win32_closedir(dirp);
    };
    virtual DIR *Open(char *filename, int &err)
    {
	return win32_opendir(filename);
    };
    virtual struct direct *Read(DIR *dirp, int &err)
    {
	return win32_readdir(dirp);
    };
    virtual void Rewind(DIR *dirp, int &err)
    {
	win32_rewinddir(dirp);
    };
    virtual void Seek(DIR *dirp, long loc, int &err)
    {
	win32_seekdir(dirp, loc);
    };
    virtual long Tell(DIR *dirp, int &err)
    {
	return win32_telldir(dirp);
    };
};


extern char *		g_win32_get_privlib(char *pl);
extern char *		g_win32_get_sitelib(char *pl);

class CPerlEnv : public IPerlEnv
{
public:
    CPerlEnv() {};
    virtual char *Getenv(const char *varname, int &err)
    {
	return win32_getenv(varname);
    };
    virtual int Putenv(const char *envstring, int &err)
    {
	return win32_putenv(envstring);
    };
    virtual char* LibPath(char *pl)
    {
	return g_win32_get_privlib(pl);
    };
    virtual char* SiteLibPath(char *pl)
    {
	return g_win32_get_sitelib(pl);
    };
    virtual int Uname(struct utsname *name, int &err)
    {
	return win32_uname(name);
    };
};

class CPerlSock : public IPerlSock
{
public:
    CPerlSock() {};
    virtual u_long Htonl(u_long hostlong)
    {
	return win32_htonl(hostlong);
    };
    virtual u_short Htons(u_short hostshort)
    {
	return win32_htons(hostshort);
    };
    virtual u_long Ntohl(u_long netlong)
    {
	return win32_ntohl(netlong);
    };
    virtual u_short Ntohs(u_short netshort)
    {
	return win32_ntohs(netshort);
    }

    virtual SOCKET Accept(SOCKET s, struct sockaddr* addr, int* addrlen, int &err)
    {
	SOCKET r = win32_accept(s, addr, addrlen);
	PROCESS_AND_RETURN;
    };
    virtual int Bind(SOCKET s, const struct sockaddr* name, int namelen, int &err)
    {
	int r = win32_bind(s, name, namelen);
	PROCESS_AND_RETURN;
    };
    virtual int Connect(SOCKET s, const struct sockaddr* name, int namelen, int &err)
    {
	int r = win32_connect(s, name, namelen);
	PROCESS_AND_RETURN;
    };
    virtual void Endhostent(int &err)
    {
	win32_endhostent();
    };
    virtual void Endnetent(int &err)
    {
	win32_endnetent();
    };
    virtual void Endprotoent(int &err)
    {
	win32_endprotoent();
    };
    virtual void Endservent(int &err)
    {
	win32_endservent();
    };
    virtual struct hostent* Gethostbyaddr(const char* addr, int len, int type, int &err)
    {
	struct hostent *r = win32_gethostbyaddr(addr, len, type);
	PROCESS_AND_RETURN;
    };
    virtual struct hostent* Gethostbyname(const char* name, int &err)
    {
	struct hostent *r = win32_gethostbyname(name);
	PROCESS_AND_RETURN;
    };
    virtual struct hostent* Gethostent(int &err)
    {
        croak("gethostent not implemented!\n");
	return NULL;
    };
    virtual int Gethostname(char* name, int namelen, int &err)
    {
	int r = win32_gethostname(name, namelen);
	PROCESS_AND_RETURN;
    };
    virtual struct netent *Getnetbyaddr(long net, int type, int &err)
    {
	struct netent *r = win32_getnetbyaddr(net, type);
	PROCESS_AND_RETURN;
    };
    virtual struct netent *Getnetbyname(const char *name, int &err)
    {
	struct netent *r = win32_getnetbyname((char*)name);
	PROCESS_AND_RETURN;
    };
    virtual struct netent *Getnetent(int &err)
    {
	struct netent *r = win32_getnetent();
	PROCESS_AND_RETURN;
    };
    virtual int Getpeername(SOCKET s, struct sockaddr* name, int* namelen, int &err)
    {
	int r = win32_getpeername(s, name, namelen);
	PROCESS_AND_RETURN;
    };
    virtual struct protoent* Getprotobyname(const char* name, int &err)
    {
	struct protoent *r = win32_getprotobyname(name);
	PROCESS_AND_RETURN;
    };
    virtual struct protoent* Getprotobynumber(int number, int &err)
    {
	struct protoent *r = win32_getprotobynumber(number);
	PROCESS_AND_RETURN;
    };
    virtual struct protoent* Getprotoent(int &err)
    {
	struct protoent *r = win32_getprotoent();
	PROCESS_AND_RETURN;
    };
    virtual struct servent* Getservbyname(const char* name, const char* proto, int &err)
    {
	struct servent *r = win32_getservbyname(name, proto);
	PROCESS_AND_RETURN;
    };
    virtual struct servent* Getservbyport(int port, const char* proto, int &err)
    {
	struct servent *r = win32_getservbyport(port, proto);
	PROCESS_AND_RETURN;
    };
    virtual struct servent* Getservent(int &err)
    {
	struct servent *r = win32_getservent();
	PROCESS_AND_RETURN;
    };
    virtual int Getsockname(SOCKET s, struct sockaddr* name, int* namelen, int &err)
    {
	int r = win32_getsockname(s, name, namelen);
	PROCESS_AND_RETURN;
    };
    virtual int Getsockopt(SOCKET s, int level, int optname, char* optval, int* optlen, int &err)
    {
	int r = win32_getsockopt(s, level, optname, optval, optlen);
	PROCESS_AND_RETURN;
    };
    virtual unsigned long InetAddr(const char* cp, int &err)
    {
	unsigned long r = win32_inet_addr(cp);
	PROCESS_AND_RETURN;
    };
    virtual char* InetNtoa(struct in_addr in, int &err)
    {
	char *r = win32_inet_ntoa(in);
	PROCESS_AND_RETURN;
    };
    virtual int Listen(SOCKET s, int backlog, int &err)
    {
	int r = win32_listen(s, backlog);
	PROCESS_AND_RETURN;
    };
    virtual int Recv(SOCKET s, char* buffer, int len, int flags, int &err)
    {
	int r = win32_recv(s, buffer, len, flags);
	PROCESS_AND_RETURN;
    };
    virtual int Recvfrom(SOCKET s, char* buffer, int len, int flags, struct sockaddr* from, int* fromlen, int &err)
    {
	int r = win32_recvfrom(s, buffer, len, flags, from, fromlen);
	PROCESS_AND_RETURN;
    };
    virtual int Select(int nfds, char* readfds, char* writefds, char* exceptfds, const struct timeval* timeout, int &err)
    {
	int r = win32_select(nfds, (Perl_fd_set*)readfds, (Perl_fd_set*)writefds, (Perl_fd_set*)exceptfds, timeout);
	PROCESS_AND_RETURN;
    };
    virtual int Send(SOCKET s, const char* buffer, int len, int flags, int &err)
    {
	int r = win32_send(s, buffer, len, flags);
	PROCESS_AND_RETURN;
    };
    virtual int Sendto(SOCKET s, const char* buffer, int len, int flags, const struct sockaddr* to, int tolen, int &err)
    {
	int r = win32_sendto(s, buffer, len, flags, to, tolen);
	PROCESS_AND_RETURN;
    };
    virtual void Sethostent(int stayopen, int &err)
    {
	win32_sethostent(stayopen);
    };
    virtual void Setnetent(int stayopen, int &err)
    {
	win32_setnetent(stayopen);
    };
    virtual void Setprotoent(int stayopen, int &err)
    {
	win32_setprotoent(stayopen);
    };
    virtual void Setservent(int stayopen, int &err)
    {
	win32_setservent(stayopen);
    };
    virtual int Setsockopt(SOCKET s, int level, int optname, const char* optval, int optlen, int &err)
    {
	int r = win32_setsockopt(s, level, optname, optval, optlen);
	PROCESS_AND_RETURN;
    };
    virtual int Shutdown(SOCKET s, int how, int &err)
    {
	int r = win32_shutdown(s, how);
	PROCESS_AND_RETURN;
    };
    virtual SOCKET Socket(int af, int type, int protocol, int &err)
    {
	SOCKET r = win32_socket(af, type, protocol);
	PROCESS_AND_RETURN;
    };
    virtual int Socketpair(int domain, int type, int protocol, int* fds, int &err)
    {
        croak("socketpair not implemented!\n");
	return 0;
    };
    virtual int Closesocket(SOCKET s, int& err)
    {
	int r = win32_closesocket(s);
	PROCESS_AND_RETURN;
    };
    virtual int Ioctlsocket(SOCKET s, long cmd, u_long *argp, int& err)
    {
	int r = win32_ioctlsocket(s, cmd, argp);
	PROCESS_AND_RETURN;
    };
};

class CPerlLIO : public IPerlLIO
{
public:
    CPerlLIO() {};
    virtual int Access(const char *path, int mode, int &err)
    {
	CALLFUNCRET(access(path, mode))
    };
    virtual int Chmod(const char *filename, int pmode, int &err)
    {
	CALLFUNCRET(chmod(filename, pmode))
    };
    virtual int Chown(const char *filename, uid_t owner, gid_t group, int &err)
    {
	CALLFUNCERR(chown(filename, owner, group))
    };
    virtual int Chsize(int handle, long size, int &err)
    {
	CALLFUNCRET(chsize(handle, size))
    };
    virtual int Close(int handle, int &err)
    {
	CALLFUNCRET(win32_close(handle))
    };
    virtual int Dup(int handle, int &err)
    {
	CALLFUNCERR(win32_dup(handle))
    };
    virtual int Dup2(int handle1, int handle2, int &err)
    {
	CALLFUNCERR(win32_dup2(handle1, handle2))
    };
    virtual int Flock(int fd, int oper, int &err)
    {
	CALLFUNCERR(win32_flock(fd, oper))
    };
    virtual int FileStat(int handle, struct stat *buffer, int &err)
    {
	CALLFUNCERR(fstat(handle, buffer))
    };
    virtual int IOCtl(int i, unsigned int u, char *data, int &err)
    {
	CALLFUNCERR(win32_ioctlsocket((SOCKET)i, (long)u, (u_long*)data))
    };
    virtual int Isatty(int fd, int &err)
    {
	return isatty(fd);
    };
    virtual long Lseek(int handle, long offset, int origin, int &err)
    {
	LCALLFUNCERR(win32_lseek(handle, offset, origin))
    };
    virtual int Lstat(const char *path, struct stat *buffer, int &err)
    {
	return NameStat(path, buffer, err);
    };
    virtual char *Mktemp(char *Template, int &err)
    {
	return mktemp(Template);
    };
    virtual int Open(const char *filename, int oflag, int &err)
    {
	CALLFUNCERR(win32_open(filename, oflag))
    };
    virtual int Open(const char *filename, int oflag, int pmode, int &err)
    {
	int ret;
	if(stricmp(filename, "/dev/null") == 0)
	    ret = open("NUL", oflag, pmode);
	else
	    ret = open(filename, oflag, pmode);

	if(errno)
	    err = errno;
	return ret;
    };
    virtual int Read(int handle, void *buffer, unsigned int count, int &err)
    {
	CALLFUNCERR(win32_read(handle, buffer, count))
    };
    virtual int Rename(const char *OldFileName, const char *newname, int &err)
    {
	CALLFUNCRET(win32_rename(OldFileName, newname))
    };
    virtual int Setmode(int handle, int mode, int &err)
    {
	CALLFUNCRET(win32_setmode(handle, mode))
    };
    virtual int NameStat(const char *path, struct stat *buffer, int &err)
    {
	return win32_stat(path, buffer);
    };
    virtual char *Tmpnam(char *string, int &err)
    {
	return tmpnam(string);
    };
    virtual int Umask(int pmode, int &err)
    {
	return umask(pmode);
    };
    virtual int Unlink(const char *filename, int &err)
    {
	chmod(filename, S_IREAD | S_IWRITE);
	CALLFUNCRET(unlink(filename))
    };
    virtual int Utime(char *filename, struct utimbuf *times, int &err)
    {
	CALLFUNCRET(win32_utime(filename, times))
    };
    virtual int Write(int handle, const void *buffer, unsigned int count, int &err)
    {
	CALLFUNCERR(win32_write(handle, buffer, count))
    };
};

class CPerlMem : public IPerlMem
{
public:
    CPerlMem() {};
    virtual void* Malloc(size_t size)
    {
	return win32_malloc(size);
    };
    virtual void* Realloc(void* ptr, size_t size)
    {
	return win32_realloc(ptr, size);
    };
    virtual void Free(void* ptr)
    {
	win32_free(ptr);
    };
};

#define EXECF_EXEC 1
#define EXECF_SPAWN 2

extern char *		g_getlogin(void);
extern int		do_spawn2(char *cmd, int exectype);
extern int		g_do_aspawn(void *vreally, void **vmark, void **vsp);

class CPerlProc : public IPerlProc
{
public:
    CPerlProc() {};
    virtual void Abort(void)
    {
	win32_abort();
    };
    virtual char * Crypt(const char* clear, const char* salt)
    {
	return win32_crypt(clear, salt);
    };
    virtual void Exit(int status)
    {
	exit(status);
    };
    virtual void _Exit(int status)
    {
	_exit(status);
    };
    virtual int Execl(const char *cmdname, const char *arg0, const char *arg1, const char *arg2, const char *arg3)
    {
	return execl(cmdname, arg0, arg1, arg2, arg3);
    };
    virtual int Execv(const char *cmdname, const char *const *argv)
    {
	return win32_execvp(cmdname, argv);
    };
    virtual int Execvp(const char *cmdname, const char *const *argv)
    {
	return win32_execvp(cmdname, argv);
    };
    virtual uid_t Getuid(void)
    {
	return getuid();
    };
    virtual uid_t Geteuid(void)
    {
	return geteuid();
    };
    virtual gid_t Getgid(void)
    {
	return getgid();
    };
    virtual gid_t Getegid(void)
    {
	return getegid();
    };
    virtual char *Getlogin(void)
    {
	return g_getlogin();
    };
    virtual int Kill(int pid, int sig)
    {
	return win32_kill(pid, sig);
    };
    virtual int Killpg(int pid, int sig)
    {
	croak("killpg not implemented!\n");
	return 0;
    };
    virtual int PauseProc(void)
    {
	return win32_sleep((32767L << 16) + 32767);
    };
    virtual PerlIO* Popen(const char *command, const char *mode)
    {
	win32_fflush(stdout);
	win32_fflush(stderr);
	return (PerlIO*)win32_popen(command, mode);
    };
    virtual int Pclose(PerlIO *stream)
    {
	return win32_pclose((FILE*)stream);
    };
    virtual int Pipe(int *phandles)
    {
	return win32_pipe(phandles, 512, O_BINARY);
    };
    virtual int Setuid(uid_t u)
    {
	return setuid(u);
    };
    virtual int Setgid(gid_t g)
    {
	return setgid(g);
    };
    virtual int Sleep(unsigned int s)
    {
	return win32_sleep(s);
    };
    virtual int Times(struct tms *timebuf)
    {
	return win32_times(timebuf);
    };
    virtual int Wait(int *status)
    {
	return win32_wait(status);
    };
    virtual int Waitpid(int pid, int *status, int flags)
    {
	return win32_waitpid(pid, status, flags);
    };
    virtual Sighandler_t Signal(int sig, Sighandler_t subcode)
    {
	return 0;
    };
    virtual void GetSysMsg(char*& sMsg, DWORD& dwLen, DWORD dwErr)
    {
	dwLen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
			  |FORMAT_MESSAGE_IGNORE_INSERTS
			  |FORMAT_MESSAGE_FROM_SYSTEM, NULL,
			   dwErr, 0, (char *)&sMsg, 1, NULL);
	if (0 < dwLen) {
	    while (0 < dwLen  &&  isspace(sMsg[--dwLen]))
		;
	    if ('.' != sMsg[dwLen])
		dwLen++;
	    sMsg[dwLen]= '\0';
	}
	if (0 == dwLen) {
	    sMsg = (char*)LocalAlloc(0, 64/**sizeof(TCHAR)*/);
	    dwLen = sprintf(sMsg,
			"Unknown error #0x%lX (lookup 0x%lX)",
			dwErr, GetLastError());
	}
    };
    virtual void FreeBuf(char* sMsg)
    {
	LocalFree(sMsg);
    };
    virtual BOOL DoCmd(char *cmd)
    {
	do_spawn2(cmd, EXECF_EXEC);
        return FALSE;
    };
    virtual int Spawn(char* cmds)
    {
	return do_spawn2(cmds, EXECF_SPAWN);
    };
    virtual int Spawnvp(int mode, const char *cmdname, const char *const *argv)
    {
	return win32_spawnvp(mode, cmdname, argv);
    };
    virtual int ASpawn(void *vreally, void **vmark, void **vsp)
    {
	return g_do_aspawn(vreally, vmark, vsp);
    };
};


class CPerlStdIO : public IPerlStdIO
{
public:
    CPerlStdIO() {};
    virtual PerlIO* Stdin(void)
    {
	return (PerlIO*)win32_stdin();
    };
    virtual PerlIO* Stdout(void)
    {
	return (PerlIO*)win32_stdout();
    };
    virtual PerlIO* Stderr(void)
    {
	return (PerlIO*)win32_stderr();
    };
    virtual PerlIO* Open(const char *path, const char *mode, int &err)
    {
	PerlIO*pf = (PerlIO*)win32_fopen(path, mode);
	if(errno)
	    err = errno;
	return pf;
    };
    virtual int Close(PerlIO* pf, int &err)
    {
	CALLFUNCERR(win32_fclose(((FILE*)pf)))
    };
    virtual int Eof(PerlIO* pf, int &err)
    {
	CALLFUNCERR(win32_feof((FILE*)pf))
    };
    virtual int Error(PerlIO* pf, int &err)
    {
	CALLFUNCERR(win32_ferror((FILE*)pf))
    };
    virtual void Clearerr(PerlIO* pf, int &err)
    {
	win32_clearerr((FILE*)pf);
    };
    virtual int Getc(PerlIO* pf, int &err)
    {
	CALLFUNCERR(win32_getc((FILE*)pf))
    };
    virtual char* GetBase(PerlIO* pf, int &err)
    {
#ifdef FILE_base
	FILE *f = (FILE*)pf;
	return FILE_base(f);
#else
	return Nullch;
#endif
    };
    virtual int GetBufsiz(PerlIO* pf, int &err)
    {
#ifdef FILE_bufsiz
	FILE *f = (FILE*)pf;
	return FILE_bufsiz(f);
#else
	return (-1);
#endif
    };
    virtual int GetCnt(PerlIO* pf, int &err)
    {
#ifdef USE_STDIO_PTR
	FILE *f = (FILE*)pf;
	return FILE_cnt(f);
#else
	return (-1);
#endif
    };
    virtual char* GetPtr(PerlIO* pf, int &err)
    {
#ifdef USE_STDIO_PTR
	FILE *f = (FILE*)pf;
	return FILE_ptr(f);
#else
	return Nullch;
#endif
    };
    virtual char* Gets(PerlIO* pf, char* s, int n, int& err)
    {
	char* ret = win32_fgets(s, n, (FILE*)pf);
	if(errno)
	    err = errno;
	return ret;
    };
    virtual int Putc(PerlIO* pf, int c, int &err)
    {
	CALLFUNCERR(win32_fputc(c, (FILE*)pf))
    };
    virtual int Puts(PerlIO* pf, const char *s, int &err)
    {
	CALLFUNCERR(win32_fputs(s, (FILE*)pf))
    };
    virtual int Flush(PerlIO* pf, int &err)
    {
	CALLFUNCERR(win32_fflush((FILE*)pf))
    };
    virtual int Ungetc(PerlIO* pf,int c, int &err)
    {
	CALLFUNCERR(win32_ungetc(c, (FILE*)pf))
    };
    virtual int Fileno(PerlIO* pf, int &err)
    {
	CALLFUNCERR(win32_fileno((FILE*)pf))
    };
    virtual PerlIO* Fdopen(int fd, const char *mode, int &err)
    {
	PerlIO* pf = (PerlIO*)win32_fdopen(fd, mode);
	if(errno)
	    err = errno;
	return pf;
    };
    virtual PerlIO* Reopen(const char*path, const char*mode, PerlIO* pf, int &err)
    {
	PerlIO* newPf = (PerlIO*)win32_freopen(path, mode, (FILE*)pf);
	if(errno)
	    err = errno;
	return newPf;
    };
    virtual SSize_t Read(PerlIO* pf, void *buffer, Size_t size, int &err)
    {
	SSize_t i = win32_fread(buffer, 1, size, (FILE*)pf);
	if(errno)
	    err = errno;
	return i;
    };
    virtual SSize_t Write(PerlIO* pf, const void *buffer, Size_t size, int &err)
    {
	SSize_t i = win32_fwrite(buffer, 1, size, (FILE*)pf);
	if(errno)
	    err = errno;
	return i;
    };
    virtual void SetBuf(PerlIO* pf, char* buffer, int &err)
    {
	win32_setbuf((FILE*)pf, buffer);
    };
    virtual int SetVBuf(PerlIO* pf, char* buffer, int type, Size_t size, int &err)
    {
	int i = win32_setvbuf((FILE*)pf, buffer, type, size);
	if(errno)
	    err = errno;
	return i;
    };
    virtual void SetCnt(PerlIO* pf, int n, int &err)
    {
#ifdef STDIO_CNT_LVALUE
	FILE *f = (FILE*)pf;
	FILE_cnt(f) = n;
#endif
    };
    virtual void SetPtrCnt(PerlIO* pf, char * ptr, int n, int& err)
    {
#ifdef STDIO_PTR_LVALUE
	FILE *f = (FILE*)pf;
	FILE_ptr(f) = ptr;
	FILE_cnt(f) = n;
#endif
    };
    virtual void Setlinebuf(PerlIO* pf, int &err)
    {
	win32_setvbuf((FILE*)pf, NULL, _IOLBF, 0);
    };
    virtual int Printf(PerlIO* pf, int &err, const char *format,...)
    {
	va_list(arglist);
	va_start(arglist, format);
	int i = win32_vfprintf((FILE*)pf, format, arglist);
	if(errno)
	    err = errno;
	return i;
    };
    virtual int Vprintf(PerlIO* pf, int &err, const char *format, va_list arglist)
    {
	int i = win32_vfprintf((FILE*)pf, format, arglist);
	if(errno)
	    err = errno;
	return i;
    };
    virtual long Tell(PerlIO* pf, int &err)
    {
	long l = win32_ftell((FILE*)pf);
	if(errno)
	    err = errno;
	return l;
    };
    virtual int Seek(PerlIO* pf, off_t offset, int origin, int &err)
    {
	int i = win32_fseek((FILE*)pf, offset, origin);
	if(errno)
	    err = errno;
	return i;
    };
    virtual void Rewind(PerlIO* pf, int &err)
    {
	win32_rewind((FILE*)pf);
    };
    virtual PerlIO* Tmpfile(int &err)
    {
	PerlIO* pf = (PerlIO*)win32_tmpfile();
	if(errno)
	    err = errno;
	return pf;
    };
    virtual int Getpos(PerlIO* pf, Fpos_t *p, int &err)
    {
	int i = win32_fgetpos((FILE*)pf, p);
	if(errno)
	    err = errno;
	return i;
    };
    virtual int Setpos(PerlIO* pf, const Fpos_t *p, int &err)
    {
	int i = win32_fsetpos((FILE*)pf, p);
	if(errno)
	    err = errno;
	return i;
    };
    virtual void Init(int &err)
    {
    };
    virtual void InitOSExtras(void* p)
    {
	Perl_init_os_extras();
    };
    virtual int OpenOSfhandle(long osfhandle, int flags)
    {
	return win32_open_osfhandle(osfhandle, flags);
    }
    virtual int GetOSfhandle(int filenum)
    {
	return win32_get_osfhandle(filenum);
    }
};

class CPerlHost
{
public:
    CPerlHost() { pPerl = NULL; };
    inline BOOL PerlCreate(void)
    {
	try
	{
	    pPerl = perl_alloc(&perlMem, &perlEnv, &perlStdIO, &perlLIO,
			       &perlDir, &perlSock, &perlProc);
	    if(pPerl != NULL)
	    {
		try
		{
		    perl_construct();
		}
		catch(...)
		{
		    win32_fprintf(stderr, "%s\n",
				  "Error: Unable to construct data structures");
		    perl_free();
		    pPerl = NULL;
		}
	    }
	}
	catch(...)
	{
	    win32_fprintf(stderr, "%s\n", "Error: Unable to allocate memory");
	    pPerl = NULL;
	}
	return (pPerl != NULL);
    };
    inline int PerlParse(void (*xs_init)(CPerlObj*), int argc, char** argv, char** env)
    {
	int retVal;
	try
	{
	    retVal = perl_parse(xs_init, argc, argv, env);
	}
	catch(int x)
	{
	    // this is where exit() should arrive
	    retVal = x;
	}
	catch(...)
	{
	    win32_fprintf(stderr, "Error: Parse exception\n");
	    retVal = -1;
	}
	*win32_errno() = 0;
	return retVal;
    };
    inline int PerlRun(void)
    {
	int retVal;
	try
	{
	    retVal = perl_run();
	}
	catch(int x)
	{
	    // this is where exit() should arrive
	    retVal = x;
	}
	catch(...)
	{
	    win32_fprintf(stderr, "Error: Runtime exception\n");
	    retVal = -1;
	}
	return retVal;
    };
    inline void PerlDestroy(void)
    {
	try
	{
	    perl_destruct();
	    perl_free();
	}
	catch(...)
	{
	}
    };

protected:
    CPerlDir	perlDir;
    CPerlEnv	perlEnv;
    CPerlLIO	perlLIO;
    CPerlMem	perlMem;
    CPerlProc	perlProc;
    CPerlSock	perlSock;
    CPerlStdIO	perlStdIO;
};