Commit | Line | Data |
---|---|---|
76e3520e GS |
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 GS |
9 | #include "Win32iop.h" |
10 | ||
ac4c12e7 GS |
11 | #define errno (*win32_errno()) |
12 | #define stdout (win32_stdout()) | |
13 | #define stderr (win32_stderr()) | |
76e3520e | 14 | |
c69f6586 GS |
15 | CPerlObj *pPerl; |
16 | ||
17 | #include <fcntl.h> | |
76e3520e GS |
18 | #include <ipdir.h> |
19 | #include <ipenv.h> | |
20 | #include <ipsock.h> | |
21 | #include <iplio.h> | |
22 | #include <ipmem.h> | |
23 | #include <ipproc.h> | |
c69f6586 GS |
24 | #include <ipstdio.h> |
25 | ||
26 | class IPerlStdIOWin : public IPerlStdIO | |
27 | { | |
28 | public: | |
29 | virtual int OpenOSfhandle(long osfhandle, int flags) = 0; | |
30 | virtual int GetOSfhandle(int filenum) = 0; | |
31 | }; | |
32 | ||
33 | extern int g_closedir(DIR *dirp); | |
34 | extern DIR *g_opendir(char *filename); | |
35 | extern struct direct *g_readdir(DIR *dirp); | |
36 | extern void g_rewinddir(DIR *dirp); | |
37 | extern void g_seekdir(DIR *dirp, long loc); | |
38 | extern long g_telldir(DIR *dirp); | |
39 | class CPerlDir : public IPerlDir | |
40 | { | |
41 | public: | |
42 | CPerlDir() {}; | |
43 | virtual int Makedir(const char *dirname, int mode, int &err) | |
44 | { | |
45 | return win32_mkdir(dirname, mode); | |
46 | }; | |
47 | virtual int Chdir(const char *dirname, int &err) | |
48 | { | |
49 | return win32_chdir(dirname); | |
50 | }; | |
51 | virtual int Rmdir(const char *dirname, int &err) | |
52 | { | |
53 | return win32_rmdir(dirname); | |
54 | }; | |
55 | virtual int Close(DIR *dirp, int &err) | |
56 | { | |
57 | return g_closedir(dirp); | |
58 | }; | |
59 | virtual DIR *Open(char *filename, int &err) | |
60 | { | |
61 | return g_opendir(filename); | |
62 | }; | |
63 | virtual struct direct *Read(DIR *dirp, int &err) | |
64 | { | |
65 | return g_readdir(dirp); | |
66 | }; | |
67 | virtual void Rewind(DIR *dirp, int &err) | |
68 | { | |
69 | g_rewinddir(dirp); | |
70 | }; | |
71 | virtual void Seek(DIR *dirp, long loc, int &err) | |
72 | { | |
73 | g_seekdir(dirp, loc); | |
74 | }; | |
75 | virtual long Tell(DIR *dirp, int &err) | |
76 | { | |
77 | return g_telldir(dirp); | |
78 | }; | |
79 | }; | |
80 | ||
81 | ||
82 | extern char * g_win32_perllib_path(char *sfx,...); | |
83 | class CPerlEnv : public IPerlEnv | |
84 | { | |
85 | public: | |
86 | CPerlEnv() {}; | |
87 | virtual char *Getenv(const char *varname, int &err) | |
88 | { | |
89 | return win32_getenv(varname); | |
90 | }; | |
91 | virtual int Putenv(const char *envstring, int &err) | |
92 | { | |
ac4c12e7 | 93 | return putenv(envstring); |
c69f6586 GS |
94 | }; |
95 | virtual char* LibPath(char *sfx, ...) | |
96 | { | |
97 | LPSTR ptr1, ptr2, ptr3, ptr4, ptr5; | |
98 | va_list ap; | |
99 | va_start(ap,sfx); | |
100 | ptr1 = va_arg(ap,char *); | |
101 | ptr2 = va_arg(ap,char *); | |
102 | ptr3 = va_arg(ap,char *); | |
103 | ptr4 = va_arg(ap,char *); | |
104 | ptr5 = va_arg(ap,char *); | |
105 | return g_win32_perllib_path(sfx, ptr1, ptr2, ptr3, ptr4, ptr5); | |
106 | }; | |
107 | }; | |
108 | ||
109 | #define PROCESS_AND_RETURN \ | |
110 | if(errno) \ | |
111 | err = errno; \ | |
112 | return r | |
113 | ||
114 | class CPerlSock : public IPerlSock | |
115 | { | |
116 | public: | |
117 | CPerlSock() {}; | |
118 | virtual u_long Htonl(u_long hostlong) | |
119 | { | |
120 | return win32_htonl(hostlong); | |
121 | }; | |
122 | virtual u_short Htons(u_short hostshort) | |
123 | { | |
124 | return win32_htons(hostshort); | |
125 | }; | |
126 | virtual u_long Ntohl(u_long netlong) | |
127 | { | |
128 | return win32_ntohl(netlong); | |
129 | }; | |
130 | virtual u_short Ntohs(u_short netshort) | |
131 | { | |
132 | return win32_ntohs(netshort); | |
133 | } | |
134 | ||
135 | virtual SOCKET Accept(SOCKET s, struct sockaddr* addr, int* addrlen, int &err) | |
136 | { | |
137 | SOCKET r = win32_accept(s, addr, addrlen); | |
138 | PROCESS_AND_RETURN; | |
139 | }; | |
140 | virtual int Bind(SOCKET s, const struct sockaddr* name, int namelen, int &err) | |
141 | { | |
142 | int r = win32_bind(s, name, namelen); | |
143 | PROCESS_AND_RETURN; | |
144 | }; | |
145 | virtual int Connect(SOCKET s, const struct sockaddr* name, int namelen, int &err) | |
146 | { | |
147 | int r = win32_connect(s, name, namelen); | |
148 | PROCESS_AND_RETURN; | |
149 | }; | |
150 | virtual void Endhostent(int &err) | |
151 | { | |
152 | win32_endhostent(); | |
153 | }; | |
154 | virtual void Endnetent(int &err) | |
155 | { | |
156 | win32_endnetent(); | |
157 | }; | |
158 | virtual void Endprotoent(int &err) | |
159 | { | |
160 | win32_endprotoent(); | |
161 | }; | |
162 | virtual void Endservent(int &err) | |
163 | { | |
164 | win32_endservent(); | |
165 | }; | |
166 | virtual struct hostent* Gethostbyaddr(const char* addr, int len, int type, int &err) | |
167 | { | |
168 | struct hostent *r = win32_gethostbyaddr(addr, len, type); | |
169 | PROCESS_AND_RETURN; | |
170 | }; | |
171 | virtual struct hostent* Gethostbyname(const char* name, int &err) | |
172 | { | |
173 | struct hostent *r = win32_gethostbyname(name); | |
174 | PROCESS_AND_RETURN; | |
175 | }; | |
176 | virtual struct hostent* Gethostent(int &err) | |
177 | { | |
178 | croak("gethostent not implemented!\n"); | |
179 | return NULL; | |
180 | }; | |
181 | virtual int Gethostname(char* name, int namelen, int &err) | |
182 | { | |
183 | int r = win32_gethostname(name, namelen); | |
184 | PROCESS_AND_RETURN; | |
185 | }; | |
186 | virtual struct netent *Getnetbyaddr(long net, int type, int &err) | |
187 | { | |
188 | struct netent *r = win32_getnetbyaddr(net, type); | |
189 | PROCESS_AND_RETURN; | |
190 | }; | |
191 | virtual struct netent *Getnetbyname(const char *name, int &err) | |
192 | { | |
193 | struct netent *r = win32_getnetbyname((char*)name); | |
194 | PROCESS_AND_RETURN; | |
195 | }; | |
196 | virtual struct netent *Getnetent(int &err) | |
197 | { | |
198 | struct netent *r = win32_getnetent(); | |
199 | PROCESS_AND_RETURN; | |
200 | }; | |
201 | virtual int Getpeername(SOCKET s, struct sockaddr* name, int* namelen, int &err) | |
202 | { | |
203 | int r = win32_getpeername(s, name, namelen); | |
204 | PROCESS_AND_RETURN; | |
205 | }; | |
206 | virtual struct protoent* Getprotobyname(const char* name, int &err) | |
207 | { | |
208 | struct protoent *r = win32_getprotobyname(name); | |
209 | PROCESS_AND_RETURN; | |
210 | }; | |
211 | virtual struct protoent* Getprotobynumber(int number, int &err) | |
212 | { | |
213 | struct protoent *r = win32_getprotobynumber(number); | |
214 | PROCESS_AND_RETURN; | |
215 | }; | |
216 | virtual struct protoent* Getprotoent(int &err) | |
217 | { | |
218 | struct protoent *r = win32_getprotoent(); | |
219 | PROCESS_AND_RETURN; | |
220 | }; | |
221 | virtual struct servent* Getservbyname(const char* name, const char* proto, int &err) | |
222 | { | |
223 | struct servent *r = win32_getservbyname(name, proto); | |
224 | PROCESS_AND_RETURN; | |
225 | }; | |
226 | virtual struct servent* Getservbyport(int port, const char* proto, int &err) | |
227 | { | |
228 | struct servent *r = win32_getservbyport(port, proto); | |
229 | PROCESS_AND_RETURN; | |
230 | }; | |
231 | virtual struct servent* Getservent(int &err) | |
232 | { | |
233 | struct servent *r = win32_getservent(); | |
234 | PROCESS_AND_RETURN; | |
235 | }; | |
236 | virtual int Getsockname(SOCKET s, struct sockaddr* name, int* namelen, int &err) | |
237 | { | |
238 | int r = win32_getsockname(s, name, namelen); | |
239 | PROCESS_AND_RETURN; | |
240 | }; | |
241 | virtual int Getsockopt(SOCKET s, int level, int optname, char* optval, int* optlen, int &err) | |
242 | { | |
243 | int r = win32_getsockopt(s, level, optname, optval, optlen); | |
244 | PROCESS_AND_RETURN; | |
245 | }; | |
246 | virtual unsigned long InetAddr(const char* cp, int &err) | |
247 | { | |
248 | unsigned long r = win32_inet_addr(cp); | |
249 | PROCESS_AND_RETURN; | |
250 | }; | |
251 | virtual char* InetNtoa(struct in_addr in, int &err) | |
252 | { | |
253 | char *r = win32_inet_ntoa(in); | |
254 | PROCESS_AND_RETURN; | |
255 | }; | |
256 | virtual int IoctlSocket(SOCKET s, long cmd, u_long *argp, int& err) | |
257 | { | |
258 | int r = win32_ioctlsocket(s, cmd, argp); | |
259 | PROCESS_AND_RETURN; | |
260 | }; | |
261 | virtual int Listen(SOCKET s, int backlog, int &err) | |
262 | { | |
263 | int r = win32_listen(s, backlog); | |
264 | PROCESS_AND_RETURN; | |
265 | }; | |
266 | virtual int Recvfrom(SOCKET s, char* buffer, int len, int flags, struct sockaddr* from, int* fromlen, int &err) | |
267 | { | |
268 | int r = win32_recvfrom(s, buffer, len, flags, from, fromlen); | |
269 | PROCESS_AND_RETURN; | |
270 | }; | |
271 | virtual int Select(int nfds, char* readfds, char* writefds, char* exceptfds, const struct timeval* timeout, int &err) | |
272 | { | |
273 | int r = win32_select(nfds, (Perl_fd_set*)readfds, (Perl_fd_set*)writefds, (Perl_fd_set*)exceptfds, timeout); | |
274 | PROCESS_AND_RETURN; | |
275 | }; | |
276 | virtual int Send(SOCKET s, const char* buffer, int len, int flags, int &err) | |
277 | { | |
278 | int r = win32_send(s, buffer, len, flags); | |
279 | PROCESS_AND_RETURN; | |
280 | }; | |
281 | virtual int Sendto(SOCKET s, const char* buffer, int len, int flags, const struct sockaddr* to, int tolen, int &err) | |
282 | { | |
283 | int r = win32_sendto(s, buffer, len, flags, to, tolen); | |
284 | PROCESS_AND_RETURN; | |
285 | }; | |
286 | virtual void Sethostent(int stayopen, int &err) | |
287 | { | |
288 | win32_sethostent(stayopen); | |
289 | }; | |
290 | virtual void Setnetent(int stayopen, int &err) | |
291 | { | |
292 | win32_setnetent(stayopen); | |
293 | }; | |
294 | virtual void Setprotoent(int stayopen, int &err) | |
295 | { | |
296 | win32_setprotoent(stayopen); | |
297 | }; | |
298 | virtual void Setservent(int stayopen, int &err) | |
299 | { | |
300 | win32_setservent(stayopen); | |
301 | }; | |
302 | virtual int Setsockopt(SOCKET s, int level, int optname, const char* optval, int optlen, int &err) | |
303 | { | |
304 | int r = win32_setsockopt(s, level, optname, optval, optlen); | |
305 | PROCESS_AND_RETURN; | |
306 | }; | |
307 | virtual int Shutdown(SOCKET s, int how, int &err) | |
308 | { | |
309 | int r = win32_shutdown(s, how); | |
310 | PROCESS_AND_RETURN; | |
311 | }; | |
312 | virtual SOCKET Socket(int af, int type, int protocol, int &err) | |
313 | { | |
314 | SOCKET r = win32_socket(af, type, protocol); | |
315 | PROCESS_AND_RETURN; | |
316 | }; | |
317 | virtual int Socketpair(int domain, int type, int protocol, int* fds, int &err) | |
318 | { | |
319 | croak("socketpair not implemented!\n"); | |
320 | return 0; | |
321 | }; | |
322 | }; | |
323 | ||
324 | ||
325 | #define CALLFUNCRET(x)\ | |
326 | int ret = x;\ | |
327 | if(ret)\ | |
328 | err = errno;\ | |
329 | return ret; | |
330 | ||
331 | #define CALLFUNCERR(x)\ | |
332 | int ret = x;\ | |
333 | if(errno)\ | |
334 | err = errno;\ | |
335 | return ret; | |
336 | ||
337 | #define LCALLFUNCERR(x)\ | |
338 | long ret = x;\ | |
339 | if(errno)\ | |
340 | err = errno;\ | |
341 | return ret; | |
342 | ||
343 | class CPerlLIO : public IPerlLIO | |
344 | { | |
345 | public: | |
346 | CPerlLIO() {}; | |
347 | virtual int Access(const char *path, int mode, int &err) | |
348 | { | |
349 | CALLFUNCRET(access(path, mode)) | |
350 | }; | |
351 | virtual int Chmod(const char *filename, int pmode, int &err) | |
352 | { | |
353 | CALLFUNCRET(chmod(filename, pmode)) | |
354 | }; | |
01f988be GS |
355 | virtual int Chown(const char *filename, uid_t owner, gid_t group, int &err) |
356 | { | |
357 | CALLFUNCERR(chown(filename, owner, group)) | |
358 | }; | |
c69f6586 GS |
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 | { | |
ac4c12e7 | 492 | chmod(filename, S_IREAD | S_IWRITE); |
c69f6586 GS |
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 | { | |
ac4c12e7 GS |
592 | #ifdef __BORLANDC__ |
593 | win32_fflush(stdout); | |
594 | win32_fflush(stderr); | |
595 | #endif | |
c69f6586 GS |
596 | return (PerlIO*)win32_popen(command, mode); |
597 | }; | |
598 | virtual int Pclose(PerlIO *stream) | |
599 | { | |
600 | return win32_pclose((FILE*)stream); | |
601 | }; | |
602 | virtual int Pipe(int *phandles) | |
603 | { | |
ac4c12e7 | 604 | return win32_pipe(phandles, 512, O_BINARY); |
c69f6586 GS |
605 | }; |
606 | virtual int Setuid(uid_t u) | |
607 | { | |
608 | return setuid(u); | |
609 | }; | |
610 | virtual int Setgid(gid_t g) | |
611 | { | |
612 | return setgid(g); | |
613 | }; | |
614 | virtual int Sleep(unsigned int s) | |
615 | { | |
616 | return win32_sleep(s); | |
617 | }; | |
618 | virtual int Times(struct tms *timebuf) | |
619 | { | |
620 | return win32_times(timebuf); | |
621 | }; | |
622 | virtual int Wait(int *status) | |
623 | { | |
624 | return win32_wait(status); | |
625 | }; | |
626 | virtual Sighandler_t Signal(int sig, Sighandler_t subcode) | |
627 | { | |
628 | return 0; | |
629 | }; | |
630 | virtual void GetSysMsg(char*& sMsg, DWORD& dwLen, DWORD dwErr) | |
631 | { | |
632 | dwLen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
633 | |FORMAT_MESSAGE_IGNORE_INSERTS | |
634 | |FORMAT_MESSAGE_FROM_SYSTEM, NULL, | |
635 | dwErr, 0, (char *)&sMsg, 1, NULL); | |
636 | if (0 < dwLen) { | |
637 | while (0 < dwLen && isspace(sMsg[--dwLen])) | |
638 | ; | |
639 | if ('.' != sMsg[dwLen]) | |
640 | dwLen++; | |
641 | sMsg[dwLen]= '\0'; | |
642 | } | |
643 | if (0 == dwLen) { | |
644 | sMsg = (char*)LocalAlloc(0, 64/**sizeof(TCHAR)*/); | |
645 | dwLen = sprintf(sMsg, | |
646 | "Unknown error #0x%lX (lookup 0x%lX)", | |
647 | dwErr, GetLastError()); | |
648 | } | |
649 | }; | |
650 | virtual void FreeBuf(char* sMsg) | |
651 | { | |
652 | LocalFree(sMsg); | |
653 | }; | |
654 | virtual BOOL DoCmd(char *cmd) | |
655 | { | |
656 | do_spawn2(cmd, EXECF_EXEC); | |
657 | return FALSE; | |
658 | }; | |
659 | virtual int Spawn(char* cmds) | |
660 | { | |
661 | return do_spawn2(cmds, EXECF_SPAWN); | |
662 | }; | |
663 | virtual int Spawnvp(int mode, const char *cmdname, const char *const *argv) | |
664 | { | |
665 | return win32_spawnvp(mode, cmdname, argv); | |
666 | }; | |
667 | virtual int ASpawn(void *vreally, void **vmark, void **vsp) | |
668 | { | |
669 | return g_do_aspawn(vreally, vmark, vsp); | |
670 | }; | |
671 | }; | |
672 | ||
673 | ||
674 | class CPerlStdIO : public IPerlStdIOWin | |
675 | { | |
676 | public: | |
677 | CPerlStdIO() {}; | |
678 | virtual PerlIO* Stdin(void) | |
679 | { | |
680 | return (PerlIO*)win32_stdin(); | |
681 | }; | |
682 | virtual PerlIO* Stdout(void) | |
683 | { | |
684 | return (PerlIO*)win32_stdout(); | |
685 | }; | |
686 | virtual PerlIO* Stderr(void) | |
687 | { | |
688 | return (PerlIO*)win32_stderr(); | |
689 | }; | |
690 | virtual PerlIO* Open(const char *path, const char *mode, int &err) | |
691 | { | |
692 | PerlIO*pf = (PerlIO*)win32_fopen(path, mode); | |
693 | if(errno) | |
694 | err = errno; | |
695 | return pf; | |
696 | }; | |
697 | virtual int Close(PerlIO* pf, int &err) | |
698 | { | |
699 | CALLFUNCERR(win32_fclose(((FILE*)pf))) | |
700 | }; | |
701 | virtual int Eof(PerlIO* pf, int &err) | |
702 | { | |
703 | CALLFUNCERR(win32_feof((FILE*)pf)) | |
704 | }; | |
705 | virtual int Error(PerlIO* pf, int &err) | |
706 | { | |
707 | CALLFUNCERR(win32_ferror((FILE*)pf)) | |
708 | }; | |
709 | virtual void Clearerr(PerlIO* pf, int &err) | |
710 | { | |
711 | win32_clearerr((FILE*)pf); | |
712 | }; | |
713 | virtual int Getc(PerlIO* pf, int &err) | |
714 | { | |
715 | CALLFUNCERR(win32_getc((FILE*)pf)) | |
716 | }; | |
717 | virtual char* GetBase(PerlIO* pf, int &err) | |
718 | { | |
ac4c12e7 GS |
719 | FILE *f = (FILE*)pf; |
720 | return FILE_base(f); | |
c69f6586 GS |
721 | }; |
722 | virtual int GetBufsiz(PerlIO* pf, int &err) | |
723 | { | |
ac4c12e7 GS |
724 | FILE *f = (FILE*)pf; |
725 | return FILE_bufsiz(f); | |
c69f6586 GS |
726 | }; |
727 | virtual int GetCnt(PerlIO* pf, int &err) | |
728 | { | |
ac4c12e7 GS |
729 | FILE *f = (FILE*)pf; |
730 | return FILE_cnt(f); | |
c69f6586 GS |
731 | }; |
732 | virtual char* GetPtr(PerlIO* pf, int &err) | |
733 | { | |
ac4c12e7 GS |
734 | FILE *f = (FILE*)pf; |
735 | return FILE_ptr(f); | |
c69f6586 GS |
736 | }; |
737 | virtual int Putc(PerlIO* pf, int c, int &err) | |
738 | { | |
739 | CALLFUNCERR(win32_fputc(c, (FILE*)pf)) | |
740 | }; | |
741 | virtual int Puts(PerlIO* pf, const char *s, int &err) | |
742 | { | |
743 | CALLFUNCERR(win32_fputs(s, (FILE*)pf)) | |
744 | }; | |
745 | virtual int Flush(PerlIO* pf, int &err) | |
746 | { | |
747 | CALLFUNCERR(win32_fflush((FILE*)pf)) | |
748 | }; | |
749 | virtual int Ungetc(PerlIO* pf,int c, int &err) | |
750 | { | |
751 | CALLFUNCERR(win32_ungetc(c, (FILE*)pf)) | |
752 | }; | |
753 | virtual int Fileno(PerlIO* pf, int &err) | |
754 | { | |
755 | CALLFUNCERR(win32_fileno((FILE*)pf)) | |
756 | }; | |
757 | virtual PerlIO* Fdopen(int fd, const char *mode, int &err) | |
758 | { | |
759 | PerlIO* pf = (PerlIO*)win32_fdopen(fd, mode); | |
760 | if(errno) | |
761 | err = errno; | |
762 | return pf; | |
763 | }; | |
764 | virtual PerlIO* Reopen(const char*path, const char*mode, PerlIO* pf, int &err) | |
765 | { | |
766 | PerlIO* newPf = (PerlIO*)win32_freopen(path, mode, (FILE*)pf); | |
767 | if(errno) | |
768 | err = errno; | |
769 | return newPf; | |
770 | }; | |
771 | virtual SSize_t Read(PerlIO* pf, void *buffer, Size_t size, int &err) | |
772 | { | |
fe9f1ed5 | 773 | SSize_t i = win32_fread(buffer, 1, size, (FILE*)pf); |
c69f6586 GS |
774 | if(errno) |
775 | err = errno; | |
776 | return i; | |
777 | }; | |
778 | virtual SSize_t Write(PerlIO* pf, const void *buffer, Size_t size, int &err) | |
779 | { | |
fe9f1ed5 | 780 | SSize_t i = win32_fwrite(buffer, 1, size, (FILE*)pf); |
c69f6586 GS |
781 | if(errno) |
782 | err = errno; | |
783 | return i; | |
784 | }; | |
785 | virtual void SetBuf(PerlIO* pf, char* buffer, int &err) | |
786 | { | |
787 | win32_setbuf((FILE*)pf, buffer); | |
788 | }; | |
789 | virtual int SetVBuf(PerlIO* pf, char* buffer, int type, Size_t size, int &err) | |
790 | { | |
791 | int i = win32_setvbuf((FILE*)pf, buffer, type, size); | |
792 | if(errno) | |
793 | err = errno; | |
794 | return i; | |
795 | }; | |
796 | virtual void SetCnt(PerlIO* pf, int n, int &err) | |
797 | { | |
ac4c12e7 GS |
798 | FILE *f = (FILE*)pf; |
799 | FILE_cnt(f) = n; | |
c69f6586 GS |
800 | }; |
801 | virtual void SetPtrCnt(PerlIO* pf, char * ptr, int n, int& err) | |
802 | { | |
ac4c12e7 GS |
803 | FILE *f = (FILE*)pf; |
804 | FILE_ptr(f) = ptr; | |
805 | FILE_cnt(f) = n; | |
c69f6586 GS |
806 | }; |
807 | virtual void Setlinebuf(PerlIO* pf, int &err) | |
808 | { | |
809 | win32_setvbuf((FILE*)pf, NULL, _IOLBF, 0); | |
810 | }; | |
811 | virtual int Printf(PerlIO* pf, int &err, const char *format,...) | |
812 | { | |
813 | va_list(arglist); | |
814 | va_start(arglist, format); | |
815 | int i = win32_vfprintf((FILE*)pf, format, arglist); | |
816 | if(errno) | |
817 | err = errno; | |
818 | return i; | |
819 | }; | |
820 | virtual int Vprintf(PerlIO* pf, int &err, const char *format, va_list arglist) | |
821 | { | |
822 | int i = win32_vfprintf((FILE*)pf, format, arglist); | |
823 | if(errno) | |
824 | err = errno; | |
825 | return i; | |
826 | }; | |
827 | virtual long Tell(PerlIO* pf, int &err) | |
828 | { | |
829 | long l = win32_ftell((FILE*)pf); | |
830 | if(errno) | |
831 | err = errno; | |
832 | return l; | |
833 | }; | |
834 | virtual int Seek(PerlIO* pf, off_t offset, int origin, int &err) | |
835 | { | |
836 | int i = win32_fseek((FILE*)pf, offset, origin); | |
837 | if(errno) | |
838 | err = errno; | |
839 | return i; | |
840 | }; | |
841 | virtual void Rewind(PerlIO* pf, int &err) | |
842 | { | |
843 | win32_rewind((FILE*)pf); | |
844 | }; | |
845 | virtual PerlIO* Tmpfile(int &err) | |
846 | { | |
847 | PerlIO* pf = (PerlIO*)win32_tmpfile(); | |
848 | if(errno) | |
849 | err = errno; | |
850 | return pf; | |
851 | }; | |
852 | virtual int Getpos(PerlIO* pf, Fpos_t *p, int &err) | |
853 | { | |
854 | int i = win32_fgetpos((FILE*)pf, p); | |
855 | if(errno) | |
856 | err = errno; | |
857 | return i; | |
858 | }; | |
859 | virtual int Setpos(PerlIO* pf, const Fpos_t *p, int &err) | |
860 | { | |
861 | int i = win32_fsetpos((FILE*)pf, p); | |
862 | if(errno) | |
863 | err = errno; | |
864 | return i; | |
865 | }; | |
866 | virtual void Init(int &err) | |
867 | { | |
868 | }; | |
869 | virtual void InitOSExtras(void* p) | |
870 | { | |
871 | Perl_init_os_extras(); | |
872 | }; | |
873 | virtual int OpenOSfhandle(long osfhandle, int flags) | |
874 | { | |
875 | return win32_open_osfhandle(osfhandle, flags); | |
876 | } | |
877 | virtual int GetOSfhandle(int filenum) | |
878 | { | |
879 | return win32_get_osfhandle(filenum); | |
880 | } | |
881 | }; | |
76e3520e | 882 | |
76e3520e GS |
883 | |
884 | static void xs_init _((CPERLarg)); | |
76e3520e GS |
885 | |
886 | class CPerlHost | |
887 | { | |
888 | public: | |
c69f6586 GS |
889 | CPerlHost() { pPerl = NULL; }; |
890 | inline BOOL PerlCreate(void) | |
891 | { | |
892 | try | |
76e3520e | 893 | { |
c69f6586 GS |
894 | pPerl = perl_alloc(&perlMem, &perlEnv, &perlStdIO, &perlLIO, &perlDir, &perlSock, &perlProc); |
895 | if(pPerl != NULL) | |
896 | { | |
76e3520e GS |
897 | try |
898 | { | |
c69f6586 | 899 | pPerl->perl_construct(); |
76e3520e GS |
900 | } |
901 | catch(...) | |
902 | { | |
ac4c12e7 | 903 | win32_fprintf(stderr, "%s\n", "Error: Unable to construct data structures"); |
c69f6586 GS |
904 | pPerl->perl_free(); |
905 | pPerl = NULL; | |
76e3520e | 906 | } |
c69f6586 GS |
907 | } |
908 | } | |
909 | catch(...) | |
76e3520e | 910 | { |
ac4c12e7 | 911 | win32_fprintf(stderr, "%s\n", "Error: Unable to allocate memory"); |
c69f6586 GS |
912 | pPerl = NULL; |
913 | } | |
914 | return (pPerl != NULL); | |
915 | }; | |
916 | inline int PerlParse(int argc, char** argv, char** env) | |
917 | { | |
918 | char* environ = NULL; | |
919 | int retVal; | |
920 | try | |
76e3520e | 921 | { |
c69f6586 GS |
922 | retVal = pPerl->perl_parse(xs_init, argc, argv, (env == NULL || *env == NULL ? &environ : env)); |
923 | } | |
924 | catch(int x) | |
76e3520e | 925 | { |
c69f6586 GS |
926 | // this is where exit() should arrive |
927 | retVal = x; | |
928 | } | |
929 | catch(...) | |
930 | { | |
ac4c12e7 | 931 | win32_fprintf(stderr, "Error: Parse exception\n"); |
c69f6586 GS |
932 | retVal = -1; |
933 | } | |
934 | return retVal; | |
935 | }; | |
936 | inline int PerlRun(void) | |
937 | { | |
938 | int retVal; | |
939 | try | |
940 | { | |
941 | retVal = pPerl->perl_run(); | |
942 | } | |
943 | catch(int x) | |
944 | { | |
945 | // this is where exit() should arrive | |
946 | retVal = x; | |
947 | } | |
948 | catch(...) | |
949 | { | |
ac4c12e7 | 950 | win32_fprintf(stderr, "Error: Runtime exception\n"); |
c69f6586 GS |
951 | retVal = -1; |
952 | } | |
953 | return retVal; | |
954 | }; | |
955 | inline void PerlDestroy(void) | |
956 | { | |
957 | try | |
958 | { | |
959 | pPerl->perl_destruct(); | |
960 | pPerl->perl_free(); | |
961 | } | |
962 | catch(...) | |
963 | { | |
964 | } | |
965 | }; | |
76e3520e GS |
966 | |
967 | protected: | |
c69f6586 GS |
968 | CPerlDir perlDir; |
969 | CPerlEnv perlEnv; | |
970 | CPerlLIO perlLIO; | |
971 | CPerlMem perlMem; | |
972 | CPerlProc perlProc; | |
973 | CPerlSock perlSock; | |
974 | CPerlStdIO perlStdIO; | |
76e3520e GS |
975 | }; |
976 | ||
977 | #undef PERL_SYS_INIT | |
978 | #define PERL_SYS_INIT(a, c) | |
979 | ||
980 | int | |
981 | main(int argc, char **argv, char **env) | |
982 | { | |
c69f6586 GS |
983 | CPerlHost host; |
984 | int exitstatus = 1; | |
76e3520e | 985 | |
c69f6586 GS |
986 | if(!host.PerlCreate()) |
987 | exit(exitstatus); | |
76e3520e GS |
988 | |
989 | ||
c69f6586 | 990 | exitstatus = host.PerlParse(argc, argv, env); |
76e3520e | 991 | |
c69f6586 GS |
992 | if (!exitstatus) |
993 | { | |
994 | exitstatus = host.PerlRun(); | |
76e3520e GS |
995 | } |
996 | ||
c69f6586 | 997 | host.PerlDestroy(); |
76e3520e GS |
998 | |
999 | return exitstatus; | |
1000 | } | |
1001 | ||
c69f6586 GS |
1002 | char *staticlinkmodules[] = { |
1003 | "DynaLoader", | |
1004 | NULL, | |
1005 | }; | |
76e3520e | 1006 | |
c69f6586 | 1007 | EXTERN_C void boot_DynaLoader _((CPERLarg_ CV* cv)); |
76e3520e | 1008 | |
c69f6586 GS |
1009 | static void |
1010 | xs_init(CPERLarg) | |
9d8a25dc | 1011 | { |
c69f6586 GS |
1012 | char *file = __FILE__; |
1013 | dXSUB_SYS; | |
1014 | newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); | |
9d8a25dc DL |
1015 | } |
1016 | ||
76e3520e GS |
1017 | #else /* PERL_OBJECT */ |
1018 | ||
390b85e7 | 1019 | /* Say NO to CPP! Hallelujah! */ |
a835ef8a | 1020 | #ifdef __GNUC__ |
5b0d9cbe NIS |
1021 | /* |
1022 | * GNU C does not do __declspec() | |
1023 | */ | |
a835ef8a | 1024 | #define __declspec(foo) |
5b0d9cbe NIS |
1025 | |
1026 | /* Mingw32 defaults to globing command line | |
1027 | * This is inconsistent with other Win32 ports and | |
1028 | * seems to cause trouble with passing -DXSVERSION=\"1.6\" | |
1029 | * So we turn it off like this: | |
1030 | */ | |
1031 | int _CRT_glob = 0; | |
1032 | ||
a835ef8a | 1033 | #endif |
0a753a76 | 1034 | |
5b0d9cbe | 1035 | |
390b85e7 | 1036 | __declspec(dllimport) int RunPerl(int argc, char **argv, char **env, void *ios); |
0a753a76 | 1037 | |
137443ea | 1038 | int |
0a753a76 | 1039 | main(int argc, char **argv, char **env) |
1040 | { | |
390b85e7 | 1041 | return RunPerl(argc, argv, env, (void*)0); |
0a753a76 | 1042 | } |
76e3520e GS |
1043 | |
1044 | #endif /* PERL_OBJECT */ |