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