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