2 * Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
3 * This program is free software; you can redistribute it and/or
4 * modify it under the same terms as Perl itself.
9 #define PERL_NO_GET_CONTEXT
11 #define PERLIO_NOT_STDIO 1
15 #define NEED_newCONSTSUB
16 #define NEED_newSVpvn_flags
22 #if defined(I_FCNTL) || defined(HAS_FCNTL)
28 # include <sys/sockio.h>
33 #if defined(MACOS_TRADITIONAL) && defined(USE_SFIO)
34 #define PERLIO_IS_STDIO 1
37 #define setvbuf _stdsetvbuf
38 #define setbuf(f,b) ( __sf_setbuf(f,b) )
41 typedef PerlIO * InputStream;
42 typedef PerlIO * OutputStream;
44 #define PERLIO_IS_STDIO 1
46 typedef FILE * InputStream;
47 typedef FILE * OutputStream;
50 #define MY_start_subparse(fmt,flags) start_subparse(fmt,flags)
53 #define gv_stashpvn(str,len,flags) gv_stashpv(str,flags)
56 #ifndef __attribute__noreturn__
57 # define __attribute__noreturn__
60 #ifndef NORETURN_FUNCTION_END
61 # define NORETURN_FUNCTION_END /* NOT REACHED */ return 0
69 # define OpSIBLING(o) (o)->op_sibling
72 static int not_here(const char *s) __attribute__noreturn__;
74 not_here(const char *s)
76 croak("%s not implemented on this architecture", s);
77 NORETURN_FUNCTION_END;
80 #ifndef UVCHR_IS_INVARIANT /* For use with Perls without this macro */
82 # define UVCHR_IS_INVARIANT(cp) ((cp) < 128)
83 # elif (defined(NATIVE_IS_INVARIANT)) /* EBCDIC on old Perl */
84 # define UVCHR_IS_INVARIANT(cp) ((cp) < 256 && NATIVE_IS_INVARIANT(cp))
85 # elif defined(isASCII) /* EBCDIC on very old Perl */
86 /* In EBCDIC, the invariants are the code points corresponding to ASCII,
87 * plus all the controls. All but one EBCDIC control is below SPACE; it
88 * varies depending on the code page, determined by the ord of '^' */
89 # define UVCHR_IS_INVARIANT(cp) (isASCII(cp) \
91 || (('^' == 106) /* POSIX-BC */ \
93 : (cp) == 0xFF)) /* 1047 or 037 */
94 # else /* EBCDIC on very very old Perl */
95 /* This assumes isascii() is available, but that could be fixed by
96 * having the macro test for each printable ASCII char */
97 # define UVCHR_IS_INVARIANT(cp) (isascii(cp) \
99 || (('^' == 106) /* POSIX-BC */ \
101 : (cp) == 0xFF)) /* 1047 or 037 */
107 #define PerlIO_fileno(f) fileno(f)
111 io_blocking(pTHX_ InputStream f, int block)
114 #if defined(HAS_FCNTL)
120 fd = PerlIO_fileno(f);
125 RETVAL = fcntl(fd, F_GETFL, 0);
133 # define O_NDELAY O_NONBLOCK
135 /* Note: UNICOS and UNICOS/mk a F_GETFL returns an O_NDELAY
136 * after a successful F_SETFL of an O_NONBLOCK. */
137 RETVAL = RETVAL & (O_NONBLOCK | O_NDELAY) ? 0 : 1;
140 newmode &= ~O_NDELAY;
141 newmode |= O_NONBLOCK;
142 } else if (block > 0) {
143 newmode &= ~(O_NDELAY|O_NONBLOCK);
146 /* Not POSIX - better have O_NDELAY or we can't cope.
147 * for BSD-ish machines this is an acceptable alternative
148 * for SysV we can't tell "would block" from EOF but that is
151 RETVAL = RETVAL & O_NDELAY ? 0 : 1;
155 } else if (block > 0) {
156 newmode &= ~O_NDELAY;
159 if (newmode != mode) {
160 const int ret = fcntl(fd, F_SETFL, newmode);
169 unsigned long flags = !block;
170 /* ioctl claims to take char* but really needs a u_long sized buffer */
171 const int ret = ioctl(fd, FIONBIO, (char*)&flags);
174 /* Win32 has no way to get the current blocking status of a socket.
175 * However, we don't want to just return undef, because there's no way
176 * to tell that the ioctl succeeded.
180 /* TODO: Perhaps set $! to ENOTSUP? */
189 MODULE = IO PACKAGE = IO::Seekable PREFIX = f
199 ST(0) = sv_newmortal();
200 if (PerlIO_getpos(handle, &pos) != 0) {
201 ST(0) = &PL_sv_undef;
204 sv_setpvn(ST(0), (char *)&pos, sizeof(Fpos_t));
207 ST(0) = sv_newmortal();
208 if (PerlIO_getpos(handle, ST(0)) != 0) {
209 ST(0) = &PL_sv_undef;
214 if (fgetpos(handle, &pos)) {
215 ST(0) = &PL_sv_undef;
217 # if PERL_VERSION >= 11
218 ST(0) = newSVpvn_flags((char*)&pos, sizeof(Fpos_t), SVs_TEMP);
220 ST(0) = sv_2mortal(newSVpvn((char*)&pos, sizeof(Fpos_t)));
227 ST(0) = &PL_sv_undef;
240 if (SvOK(pos) && (p = SvPV(pos,len)) && len == sizeof(Fpos_t)) {
241 RETVAL = PerlIO_setpos(handle, (Fpos_t*)p);
248 RETVAL = PerlIO_setpos(handle, pos);
253 if ((p = SvPV(pos,len)) && len == sizeof(Fpos_t)) {
254 RETVAL = fsetpos(handle, (Fpos_t*)p);
269 MODULE = IO PACKAGE = IO::File PREFIX = f
272 new_tmpfile(packname = "IO::File")
273 const char * packname
279 fp = PerlIO_tmpfile();
283 gv = (GV*)SvREFCNT_inc(newGVgen(packname));
285 (void) hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
286 if (gv && do_open(gv, "+>&", 3, FALSE, 0, 0, fp)) {
287 ST(0) = sv_2mortal(newRV((SV*)gv));
288 sv_bless(ST(0), gv_stashpv(packname, TRUE));
289 SvREFCNT_dec(gv); /* undo increment in newRV() */
292 ST(0) = &PL_sv_undef;
296 MODULE = IO PACKAGE = IO::Poll
304 const int nfd = (items - 1) / 2;
305 SV *tmpsv = sv_2mortal(NEWSV(999,nfd * sizeof(struct pollfd)));
306 /* We should pass _some_ valid pointer even if nfd is zero, but it
307 * doesn't matter what it is, since we're telling it to not check any fds.
309 struct pollfd *fds = nfd ? (struct pollfd *)SvPVX(tmpsv) : (struct pollfd *)tmpsv;
311 for(i=1, j=0 ; j < nfd ; j++) {
312 fds[j].fd = SvIV(ST(i));
314 fds[j].events = (short)SvIV(ST(i));
318 if((ret = poll(fds,nfd,timeout)) >= 0) {
319 for(i=1, j=0 ; j < nfd ; j++) {
320 sv_setiv(ST(i), fds[j].fd); i++;
321 sv_setiv(ST(i), fds[j].revents); i++;
326 not_here("IO::Poll::poll");
330 MODULE = IO PACKAGE = IO::Handle PREFIX = io_
333 io_blocking(handle,blk=-1)
339 const int ret = io_blocking(aTHX_ handle, items == 1 ? -1 : blk ? 1 : 0);
346 MODULE = IO PACKAGE = IO::Handle PREFIX = f
357 if ((SvIOK_notUV(c) && SvIV(c) < 0) || (SvNOK(c) && SvNV(c) < 0.0))
358 croak("Negative character number in ungetc()");
361 if (UVCHR_IS_INVARIANT(v) || (v <= 0xFF && !PerlIO_isutf8(handle)))
362 RETVAL = PerlIO_ungetc(handle, (int)v);
364 U8 buf[UTF8_MAXBYTES + 1], *end;
367 if (!PerlIO_isutf8(handle))
368 croak("Wide character number in ungetc()");
370 /* This doesn't warn for non-chars, surrogate, and
372 end = uvchr_to_utf8_flags(buf, v, 0);
374 if ((Size_t)PerlIO_unread(handle, &buf, len) == len)
380 RETVAL = ungetc((int)SvIV(c), handle);
396 RETVAL = PerlIO_error(handle);
398 RETVAL = ferror(handle);
413 PerlIO_clearerr(handle);
434 IoFLAGS(io) |= IOf_UNTAINT;
453 RETVAL = PerlIO_flush(handle);
455 RETVAL = Fflush(handle);
469 #ifdef PERLIO_IS_STDIO
471 char *buf = items == 2 && SvPOK(ST(1)) ?
472 sv_grow(ST(1), BUFSIZ) : 0;
476 not_here("IO::Handle::setbuf");
483 Perl_croak(aTHX_ "Usage: IO::Handle::setvbuf(handle, buf, type, size)");
484 #if defined(PERLIO_IS_STDIO) && defined(_IOFBF) && defined(HAS_SETVBUF)
486 OutputStream handle = 0;
487 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
492 handle = IoOFP(sv_2io(ST(0)));
493 buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
494 type = (int)SvIV(ST(2));
495 size = (int)SvIV(ST(3));
497 if (!handle) /* Try input stream. */
498 handle = IoIFP(sv_2io(ST(0)));
499 if (items == 4 && handle)
500 RETVAL = setvbuf(handle, buf, type, size);
507 RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
517 OutputStream handle = NULL;
520 handle = IoOFP(sv_2io(arg));
522 handle = IoIFP(sv_2io(arg));
524 int fd = PerlIO_fileno(handle);
536 RETVAL = (SysRet) not_here("IO::Handle::sync");
541 # To make these two work correctly with the open pragma, the readline op
542 # needs to pick up the lexical hints at the method's callsite. This doesn't
543 # work in pure Perl, because the hints are read from the most recent nextstate,
544 # and the nextstate of the Perl subroutines show *here* hold the lexical state
545 # for the IO package.
547 # There's no clean way to implement this - this approach, while complex, seems
548 # to be the most robust, and avoids manipulating external state (ie op checkers)
551 # @_ == 1 or croak 'usage: $io->getline()';
553 # return scalar <$this>;
557 # @_ == 1 or croak 'usage: $io->getlines()';
559 # croak 'Can\'t call $io->getlines in a scalar context, use $io->getline';
564 # If this is deprecated, should it warn, and should it be removed at some point?
565 # *gets = \&getline; # deprecated
570 IO::Handle::getline = 1
578 Perl_croak(aTHX_ "usage: $io->%s()", ix ? "getline" : "getlines");
579 if (!ix && GIMME_V != G_ARRAY)
580 Perl_croak(aTHX_ "Can't call $io->getlines in a scalar context, use $io->getline");
581 Zero(&myop, 1, UNOP);
582 myop.op_flags = (ix ? OPf_WANT_SCALAR : OPf_WANT_LIST ) | OPf_STACKED;
583 myop.op_ppaddr = PL_ppaddr[OP_READLINE];
584 myop.op_type = OP_READLINE;
585 /* I don't know if we need this, but it's correct as far as the control flow
586 goes. However, if we *do* need it, do we need to set anything else up? */
587 myop.op_next = PL_op->op_next;
588 /* Sigh, because pp_readline calls pp_rv2gv, and *it* has this wonderful
589 state check for PL_op->op_type == OP_READLINE */
590 PL_op = (OP *) &myop;
592 /* Our target (which we need to provide, as we don't have a pad entry.
593 I think that this is only needed for G_SCALAR - maybe we can get away
594 with NULL for list context? */
595 PUSHs(sv_newmortal());
598 /* And effectively we get away with tail calling pp_readline, as it stacks
599 exactly the return value(s) we need to return. */
600 PL_ppaddr[OP_READLINE](aTHX);
602 /* And we don't want to reach the line
604 that xsubpp adds after our body becase PL_stack_sp is correct, not sp */
607 MODULE = IO PACKAGE = IO::Socket
616 fd = PerlIO_fileno(sock);
621 #ifdef HAS_SOCKATMARK
623 RETVAL = sockatmark(fd);
629 # if defined(NETWARE) || defined(WIN32)
630 if (ioctl(fd, SIOCATMARK, (char*)&flag) != 0)
632 if (ioctl(fd, SIOCATMARK, &flag) != 0)
636 not_here("IO::Socket::atmark");
648 * constant subs for IO::Poll
650 stash = gv_stashpvn("IO::Poll", 8, TRUE);
652 newCONSTSUB(stash,"POLLIN",newSViv(POLLIN));
655 newCONSTSUB(stash,"POLLPRI", newSViv(POLLPRI));
658 newCONSTSUB(stash,"POLLOUT", newSViv(POLLOUT));
661 newCONSTSUB(stash,"POLLRDNORM", newSViv(POLLRDNORM));
664 newCONSTSUB(stash,"POLLWRNORM", newSViv(POLLWRNORM));
667 newCONSTSUB(stash,"POLLRDBAND", newSViv(POLLRDBAND));
670 newCONSTSUB(stash,"POLLWRBAND", newSViv(POLLWRBAND));
673 newCONSTSUB(stash,"POLLNORM", newSViv(POLLNORM));
676 newCONSTSUB(stash,"POLLERR", newSViv(POLLERR));
679 newCONSTSUB(stash,"POLLHUP", newSViv(POLLHUP));
682 newCONSTSUB(stash,"POLLNVAL", newSViv(POLLNVAL));
685 * constant subs for IO::Handle
687 stash = gv_stashpvn("IO::Handle", 10, TRUE);
689 newCONSTSUB(stash,"_IOFBF", newSViv(_IOFBF));
692 newCONSTSUB(stash,"_IOLBF", newSViv(_IOLBF));
695 newCONSTSUB(stash,"_IONBF", newSViv(_IONBF));
698 newCONSTSUB(stash,"SEEK_SET", newSViv(SEEK_SET));
701 newCONSTSUB(stash,"SEEK_CUR", newSViv(SEEK_CUR));
704 newCONSTSUB(stash,"SEEK_END", newSViv(SEEK_END));