This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Various core-dump preventions for cases Craig found on VMS.
[perl5.git] / perlio.c
CommitLineData
14a5cf38 1/*
be3c0a43 2 * perlio.c Copyright (c) 1996-2002, Nick Ing-Simmons You may distribute
14a5cf38 3 * under the terms of either the GNU General Public License or the
71200d45 4 * Artistic License, as specified in the README file.
760ac839
LW
5 */
6
14a5cf38 7/*
d31a8517
AT
8 * Hour after hour for nearly three weary days he had jogged up and down,
9 * over passes, and through long dales, and across many streams.
10 */
11
12/*
71200d45 13 * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get
14a5cf38 14 * at the dispatch tables, even when we do not need it for other reasons.
71200d45 15 * Invent a dSYS macro to abstract this out
14a5cf38 16 */
7bcba3d4
NIS
17#ifdef PERL_IMPLICIT_SYS
18#define dSYS dTHX
19#else
20#define dSYS dNOOP
21#endif
22
760ac839 23#define VOIDUSED 1
12ae5dfc
JH
24#ifdef PERL_MICRO
25# include "uconfig.h"
26#else
27# include "config.h"
28#endif
760ac839 29
6f9d8c32 30#define PERLIO_NOT_STDIO 0
760ac839 31#if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
14a5cf38 32/*
71200d45 33 * #define PerlIO FILE
14a5cf38 34 */
760ac839
LW
35#endif
36/*
6f9d8c32 37 * This file provides those parts of PerlIO abstraction
88b61e10 38 * which are not #defined in perlio.h.
6f9d8c32 39 * Which these are depends on various Configure #ifdef's
760ac839
LW
40 */
41
42#include "EXTERN.h"
864dbfa3 43#define PERL_IN_PERLIO_C
760ac839
LW
44#include "perl.h"
45
32af7c23
CL
46#ifdef PERL_IMPLICIT_CONTEXT
47#undef dSYS
48#define dSYS dTHX
49#endif
50
0c4f7ff0
NIS
51#include "XSUB.h"
52
60382766 53int
f5b9d040 54perlsio_binmode(FILE *fp, int iotype, int mode)
60382766 55{
14a5cf38 56 /*
71200d45 57 * This used to be contents of do_binmode in doio.c
14a5cf38 58 */
60382766
NIS
59#ifdef DOSISH
60# if defined(atarist) || defined(__MINT__)
f5b9d040 61 if (!fflush(fp)) {
60382766 62 if (mode & O_BINARY)
14a5cf38 63 ((FILE *) fp)->_flag |= _IOBIN;
60382766 64 else
14a5cf38 65 ((FILE *) fp)->_flag &= ~_IOBIN;
60382766
NIS
66 return 1;
67 }
68 return 0;
69# else
eb73beca 70 dTHX;
14a5cf38
JH
71#ifdef NETWARE
72 if (PerlLIO_setmode(fp, mode) != -1) {
73#else
f5b9d040 74 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
14a5cf38 75#endif
60382766 76# if defined(WIN32) && defined(__BORLANDC__)
14a5cf38 77 /*
71200d45 78 * The translation mode of the stream is maintained independent of
14a5cf38 79 * the translation mode of the fd in the Borland RTL (heavy
71200d45 80 * digging through their runtime sources reveal). User has to set
14a5cf38 81 * the mode explicitly for the stream (though they don't document
71200d45 82 * this anywhere). GSAR 97-5-24
60382766 83 */
14a5cf38 84 fseek(fp, 0L, 0);
60382766 85 if (mode & O_BINARY)
f5b9d040 86 fp->flags |= _F_BIN;
60382766 87 else
14a5cf38 88 fp->flags &= ~_F_BIN;
60382766
NIS
89# endif
90 return 1;
91 }
92 else
93 return 0;
94# endif
95#else
96# if defined(USEMYBINMODE)
04a0f00b 97 dTHX;
60382766
NIS
98 if (my_binmode(fp, iotype, mode) != FALSE)
99 return 1;
100 else
101 return 0;
102# else
103 return 1;
104# endif
105#endif
106}
107
06c7082d 108#ifndef O_ACCMODE
22569500 109#define O_ACCMODE 3 /* Assume traditional implementation */
06c7082d
NIS
110#endif
111
112int
113PerlIO_intmode2str(int rawmode, char *mode, int *writing)
114{
115 int result = rawmode & O_ACCMODE;
116 int ix = 0;
117 int ptype;
118 switch (result) {
119 case O_RDONLY:
120 ptype = IoTYPE_RDONLY;
121 break;
122 case O_WRONLY:
123 ptype = IoTYPE_WRONLY;
124 break;
125 case O_RDWR:
126 default:
127 ptype = IoTYPE_RDWR;
128 break;
129 }
130 if (writing)
131 *writing = (result != O_RDONLY);
132
133 if (result == O_RDONLY) {
134 mode[ix++] = 'r';
135 }
136#ifdef O_APPEND
137 else if (rawmode & O_APPEND) {
138 mode[ix++] = 'a';
139 if (result != O_WRONLY)
140 mode[ix++] = '+';
141 }
142#endif
143 else {
144 if (result == O_WRONLY)
145 mode[ix++] = 'w';
146 else {
147 mode[ix++] = 'r';
148 mode[ix++] = '+';
149 }
150 }
151 if (rawmode & O_BINARY)
152 mode[ix++] = 'b';
153 mode[ix] = '\0';
154 return ptype;
155}
156
eb73beca
NIS
157#ifndef PERLIO_LAYERS
158int
159PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
160{
6874a2de
NIS
161 if (!names || !*names
162 || strEQ(names, ":crlf")
163 || strEQ(names, ":raw")
164 || strEQ(names, ":bytes")
165 ) {
14a5cf38
JH
166 return 0;
167 }
168 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
169 /*
71200d45 170 * NOTREACHED
14a5cf38
JH
171 */
172 return -1;
eb73beca
NIS
173}
174
13621cfb
NIS
175void
176PerlIO_destruct(pTHX)
177{
178}
179
f5b9d040
NIS
180int
181PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
182{
92bff44d 183#ifdef USE_SFIO
14a5cf38 184 return 1;
92bff44d 185#else
14a5cf38 186 return perlsio_binmode(fp, iotype, mode);
92bff44d 187#endif
f5b9d040 188}
60382766 189
e0fa5af2 190PerlIO *
ecdeb87c 191PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
e0fa5af2 192{
0553478e
NIS
193#ifdef PERL_MICRO
194 return NULL;
195#else
196#ifdef PERL_IMPLICIT_SYS
22569500 197 return PerlSIO_fdupopen(f);
0553478e 198#else
30753f56
NIS
199#ifdef WIN32
200 return win32_fdupopen(f);
201#else
e0fa5af2
NIS
202 if (f) {
203 int fd = PerlLIO_dup(PerlIO_fileno(f));
204 if (fd >= 0) {
06c7082d
NIS
205 char mode[8];
206 int omode = fcntl(fd, F_GETFL);
a5936e02
JH
207#ifdef DJGPP
208 omode = djgpp_get_stream_mode(f);
209#endif
06c7082d 210 PerlIO_intmode2str(omode,mode,NULL);
e0fa5af2 211 /* the r+ is a hack */
06c7082d 212 return PerlIO_fdopen(fd, mode);
e0fa5af2
NIS
213 }
214 return NULL;
215 }
216 else {
217 SETERRNO(EBADF, SS$_IVCHAN);
218 }
7114a2d2 219#endif
e0fa5af2 220 return NULL;
0553478e 221#endif
30753f56 222#endif
e0fa5af2
NIS
223}
224
225
14a5cf38 226/*
71200d45 227 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
14a5cf38 228 */
ee518936
NIS
229
230PerlIO *
14a5cf38
JH
231PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
232 int imode, int perm, PerlIO *old, int narg, SV **args)
233{
7cf31beb
NIS
234 if (narg) {
235 if (narg > 1) {
3b8752bb 236 Perl_croak(aTHX_ "More than one argument to open");
7cf31beb 237 }
14a5cf38
JH
238 if (*args == &PL_sv_undef)
239 return PerlIO_tmpfile();
240 else {
241 char *name = SvPV_nolen(*args);
242 if (*mode == '#') {
243 fd = PerlLIO_open3(name, imode, perm);
244 if (fd >= 0)
245 return PerlIO_fdopen(fd, (char *) mode + 1);
246 }
247 else if (old) {
248 return PerlIO_reopen(name, mode, old);
249 }
250 else {
251 return PerlIO_open(name, mode);
252 }
253 }
254 }
255 else {
256 return PerlIO_fdopen(fd, (char *) mode);
257 }
258 return NULL;
ee518936
NIS
259}
260
0c4f7ff0
NIS
261XS(XS_PerlIO__Layer__find)
262{
14a5cf38
JH
263 dXSARGS;
264 if (items < 2)
265 Perl_croak(aTHX_ "Usage class->find(name[,load])");
266 else {
267 char *name = SvPV_nolen(ST(1));
268 ST(0) = (strEQ(name, "crlf")
269 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
270 XSRETURN(1);
271 }
0c4f7ff0
NIS
272}
273
274
275void
276Perl_boot_core_PerlIO(pTHX)
277{
14a5cf38 278 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
0c4f7ff0
NIS
279}
280
ac27b0f5
NIS
281#endif
282
32e30700 283
6f9d8c32 284#ifdef PERLIO_IS_STDIO
760ac839
LW
285
286void
e8632036 287PerlIO_init(pTHX)
760ac839 288{
14a5cf38
JH
289 /*
290 * Does nothing (yet) except force this file to be included in perl
71200d45 291 * binary. That allows this file to force inclusion of other functions
14a5cf38 292 * that may be required by loadable extensions e.g. for
71200d45 293 * FileHandle::tmpfile
14a5cf38 294 */
760ac839
LW
295}
296
33dcbb9a
PP
297#undef PerlIO_tmpfile
298PerlIO *
8ac85365 299PerlIO_tmpfile(void)
33dcbb9a 300{
14a5cf38 301 return tmpfile();
33dcbb9a
PP
302}
303
22569500 304#else /* PERLIO_IS_STDIO */
760ac839
LW
305
306#ifdef USE_SFIO
307
308#undef HAS_FSETPOS
309#undef HAS_FGETPOS
310
14a5cf38
JH
311/*
312 * This section is just to make sure these functions get pulled in from
71200d45 313 * libsfio.a
14a5cf38 314 */
760ac839
LW
315
316#undef PerlIO_tmpfile
317PerlIO *
c78749f2 318PerlIO_tmpfile(void)
760ac839 319{
14a5cf38 320 return sftmp(0);
760ac839
LW
321}
322
323void
e8632036 324PerlIO_init(pTHX)
760ac839 325{
14a5cf38
JH
326 /*
327 * Force this file to be included in perl binary. Which allows this
328 * file to force inclusion of other functions that may be required by
71200d45 329 * loadable extensions e.g. for FileHandle::tmpfile
14a5cf38 330 */
760ac839 331
14a5cf38 332 /*
71200d45 333 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
14a5cf38 334 * results in a lot of lseek()s to regular files and lot of small
71200d45 335 * writes to pipes.
14a5cf38
JH
336 */
337 sfset(sfstdout, SF_SHARE, 0);
760ac839
LW
338}
339
92bff44d 340PerlIO *
4b069b44 341PerlIO_importFILE(FILE *stdio, const char *mode)
92bff44d 342{
14a5cf38 343 int fd = fileno(stdio);
4b069b44 344 if (!mode || !*mode) {
81428673 345 mode = "r+";
4b069b44
NIS
346 }
347 return PerlIO_fdopen(fd, mode);
92bff44d
NIS
348}
349
350FILE *
351PerlIO_findFILE(PerlIO *pio)
352{
14a5cf38
JH
353 int fd = PerlIO_fileno(pio);
354 FILE *f = fdopen(fd, "r+");
355 PerlIO_flush(pio);
356 if (!f && errno == EINVAL)
357 f = fdopen(fd, "w");
358 if (!f && errno == EINVAL)
359 f = fdopen(fd, "r");
360 return f;
92bff44d
NIS
361}
362
363
22569500 364#else /* USE_SFIO */
6f9d8c32 365/*======================================================================================*/
14a5cf38 366/*
71200d45 367 * Implement all the PerlIO interface ourselves.
9e353e3b 368 */
760ac839 369
76ced9ad
NIS
370#include "perliol.h"
371
14a5cf38
JH
372/*
373 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
71200d45 374 * files
14a5cf38 375 */
02f66e2f
NIS
376#ifdef I_UNISTD
377#include <unistd.h>
378#endif
06da4f11
NIS
379#ifdef HAS_MMAP
380#include <sys/mman.h>
381#endif
382
02f66e2f 383
14a5cf38
JH
384void PerlIO_debug(const char *fmt, ...)
385 __attribute__ ((format(__printf__, 1, 2)));
6f9d8c32 386
6f9d8c32 387void
14a5cf38
JH
388PerlIO_debug(const char *fmt, ...)
389{
390 static int dbg = 0;
391 va_list ap;
392 dSYS;
393 va_start(ap, fmt);
394 if (!dbg) {
395 char *s = PerlEnv_getenv("PERLIO_DEBUG");
396 if (s && *s)
397 dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
398 else
399 dbg = -1;
400 }
401 if (dbg > 0) {
402 dTHX;
70ace5da
NIS
403#ifdef USE_ITHREADS
404 /* Use fixed buffer as sv_catpvf etc. needs SVs */
405 char buffer[1024];
406 char *s;
407 STRLEN len;
408 s = CopFILE(PL_curcop);
409 if (!s)
410 s = "(none)";
411 sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop));
22569500 412 len = strlen(buffer);
70ace5da
NIS
413 vsprintf(buffer+len, fmt, ap);
414 PerlLIO_write(dbg, buffer, strlen(buffer));
415#else
14a5cf38
JH
416 SV *sv = newSVpvn("", 0);
417 char *s;
418 STRLEN len;
419 s = CopFILE(PL_curcop);
420 if (!s)
421 s = "(none)";
422 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s,
423 (IV) CopLINE(PL_curcop));
424 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
425
426 s = SvPV(sv, len);
427 PerlLIO_write(dbg, s, len);
428 SvREFCNT_dec(sv);
70ace5da 429#endif
14a5cf38
JH
430 }
431 va_end(ap);
6f9d8c32
NIS
432}
433
9e353e3b
NIS
434/*--------------------------------------------------------------------------------------*/
435
14a5cf38 436/*
71200d45 437 * Inner level routines
14a5cf38 438 */
9e353e3b 439
14a5cf38 440/*
71200d45 441 * Table of pointers to the PerlIO structs (malloc'ed)
14a5cf38 442 */
05d1247b 443#define PERLIO_TABLE_SIZE 64
6f9d8c32 444
760ac839 445PerlIO *
5f1a76d0 446PerlIO_allocate(pTHX)
6f9d8c32 447{
14a5cf38 448 /*
71200d45 449 * Find a free slot in the table, allocating new table as necessary
14a5cf38
JH
450 */
451 PerlIO **last;
452 PerlIO *f;
a1ea730d 453 last = &PL_perlio;
14a5cf38
JH
454 while ((f = *last)) {
455 int i;
456 last = (PerlIO **) (f);
457 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
458 if (!*++f) {
459 return f;
460 }
461 }
462 }
3a1ee7e8 463 Newz('I',f,PERLIO_TABLE_SIZE,PerlIO);
14a5cf38
JH
464 if (!f) {
465 return NULL;
466 }
467 *last = f;
468 return f + 1;
05d1247b
NIS
469}
470
a1ea730d
NIS
471#undef PerlIO_fdupopen
472PerlIO *
ecdeb87c 473PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
a1ea730d 474{
04892f78 475 if (PerlIOValid(f)) {
a1ea730d
NIS
476 PerlIO_funcs *tab = PerlIOBase(f)->tab;
477 PerlIO *new;
fe5a182c 478 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
22569500 479 new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags);
a1ea730d
NIS
480 return new;
481 }
482 else {
483 SETERRNO(EBADF, SS$_IVCHAN);
484 return NULL;
485 }
486}
487
488void
5f1a76d0 489PerlIO_cleantable(pTHX_ PerlIO **tablep)
05d1247b 490{
14a5cf38
JH
491 PerlIO *table = *tablep;
492 if (table) {
493 int i;
494 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
495 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
496 PerlIO *f = table + i;
497 if (*f) {
498 PerlIO_close(f);
499 }
500 }
3a1ee7e8 501 Safefree(table);
14a5cf38 502 *tablep = NULL;
05d1247b 503 }
05d1247b
NIS
504}
505
fcf2db38
NIS
506
507PerlIO_list_t *
3a1ee7e8 508PerlIO_list_alloc(pTHX)
fcf2db38 509{
14a5cf38
JH
510 PerlIO_list_t *list;
511 Newz('L', list, 1, PerlIO_list_t);
512 list->refcnt = 1;
513 return list;
fcf2db38
NIS
514}
515
516void
3a1ee7e8 517PerlIO_list_free(pTHX_ PerlIO_list_t *list)
fcf2db38 518{
14a5cf38
JH
519 if (list) {
520 if (--list->refcnt == 0) {
521 if (list->array) {
14a5cf38
JH
522 IV i;
523 for (i = 0; i < list->cur; i++) {
524 if (list->array[i].arg)
525 SvREFCNT_dec(list->array[i].arg);
526 }
527 Safefree(list->array);
528 }
529 Safefree(list);
530 }
531 }
fcf2db38
NIS
532}
533
534void
3a1ee7e8 535PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
14a5cf38 536{
14a5cf38
JH
537 PerlIO_pair_t *p;
538 if (list->cur >= list->len) {
539 list->len += 8;
540 if (list->array)
541 Renew(list->array, list->len, PerlIO_pair_t);
542 else
543 New('l', list->array, list->len, PerlIO_pair_t);
544 }
545 p = &(list->array[list->cur++]);
546 p->funcs = funcs;
547 if ((p->arg = arg)) {
548 SvREFCNT_inc(arg);
549 }
fcf2db38
NIS
550}
551
3a1ee7e8
NIS
552PerlIO_list_t *
553PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
554{
694c95cf
JH
555 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
556 if (proto) {
557 int i;
558 list = PerlIO_list_alloc(aTHX);
559 for (i=0; i < proto->cur; i++) {
560 SV *arg = Nullsv;
561 if (proto->array[i].arg)
562 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
563 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
564 }
3a1ee7e8
NIS
565 }
566 return list;
567}
4a4a6116 568
05d1247b 569void
3a1ee7e8 570PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
9a6404c5 571{
3aaf42a7 572#ifdef USE_ITHREADS
3a1ee7e8
NIS
573 PerlIO **table = &proto->Iperlio;
574 PerlIO *f;
575 PL_perlio = NULL;
576 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
577 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
578 PerlIO_allocate(aTHX); /* root slot is never used */
694c95cf 579 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
3a1ee7e8
NIS
580 while ((f = *table)) {
581 int i;
582 table = (PerlIO **) (f++);
583 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
584 if (*f) {
93a8090d 585 (void) fp_dup(f, 0, param);
3a1ee7e8
NIS
586 }
587 f++;
588 }
589 }
3aaf42a7 590#endif
9a6404c5
DM
591}
592
593void
13621cfb
NIS
594PerlIO_destruct(pTHX)
595{
a1ea730d 596 PerlIO **table = &PL_perlio;
14a5cf38 597 PerlIO *f;
694c95cf
JH
598#ifdef USE_ITHREADS
599 PerlIO_debug("Destruct %p\n",aTHX);
600#endif
14a5cf38
JH
601 while ((f = *table)) {
602 int i;
603 table = (PerlIO **) (f++);
604 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
605 PerlIO *x = f;
606 PerlIOl *l;
607 while ((l = *x)) {
608 if (l->tab->kind & PERLIO_K_DESTRUCT) {
609 PerlIO_debug("Destruct popping %s\n", l->tab->name);
610 PerlIO_flush(x);
611 PerlIO_pop(aTHX_ x);
612 }
613 else {
614 x = PerlIONext(x);
615 }
616 }
617 f++;
618 }
619 }
13621cfb
NIS
620}
621
622void
a999f61b 623PerlIO_pop(pTHX_ PerlIO *f)
760ac839 624{
14a5cf38
JH
625 PerlIOl *l = *f;
626 if (l) {
fe5a182c 627 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
14a5cf38
JH
628 if (l->tab->Popped) {
629 /*
630 * If popped returns non-zero do not free its layer structure
631 * it has either done so itself, or it is shared and still in
71200d45 632 * use
14a5cf38 633 */
f62ce20a 634 if ((*l->tab->Popped) (aTHX_ f) != 0)
14a5cf38
JH
635 return;
636 }
b47cad08 637 *f = l->next;
3a1ee7e8 638 Safefree(l);
a8c08ecd 639 }
6f9d8c32
NIS
640}
641
9e353e3b 642/*--------------------------------------------------------------------------------------*/
14a5cf38 643/*
71200d45 644 * XS Interface for perl code
14a5cf38 645 */
9e353e3b 646
fcf2db38 647PerlIO_funcs *
2edd7e44 648PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
f3862f8b 649{
14a5cf38
JH
650 IV i;
651 if ((SSize_t) len <= 0)
652 len = strlen(name);
3a1ee7e8
NIS
653 for (i = 0; i < PL_known_layers->cur; i++) {
654 PerlIO_funcs *f = PL_known_layers->array[i].funcs;
14a5cf38 655 if (memEQ(f->name, name, len)) {
fe5a182c 656 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
14a5cf38
JH
657 return f;
658 }
659 }
3a1ee7e8
NIS
660 if (load && PL_subname && PL_def_layerlist
661 && PL_def_layerlist->cur >= 2) {
14a5cf38
JH
662 SV *pkgsv = newSVpvn("PerlIO", 6);
663 SV *layer = newSVpvn(name, len);
664 ENTER;
665 /*
71200d45 666 * The two SVs are magically freed by load_module
14a5cf38
JH
667 */
668 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
669 LEAVE;
670 return PerlIO_find_layer(aTHX_ name, len, 0);
671 }
672 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
673 return NULL;
f3862f8b
NIS
674}
675
2a1bc955 676#ifdef USE_ATTRIBUTES_FOR_PERLIO
b13b2135
NIS
677
678static int
679perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
680{
14a5cf38
JH
681 if (SvROK(sv)) {
682 IO *io = GvIOn((GV *) SvRV(sv));
683 PerlIO *ifp = IoIFP(io);
684 PerlIO *ofp = IoOFP(io);
685 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
686 }
687 return 0;
b13b2135
NIS
688}
689
690static int
691perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
692{
14a5cf38
JH
693 if (SvROK(sv)) {
694 IO *io = GvIOn((GV *) SvRV(sv));
695 PerlIO *ifp = IoIFP(io);
696 PerlIO *ofp = IoOFP(io);
697 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
698 }
699 return 0;
b13b2135
NIS
700}
701
702static int
703perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
704{
14a5cf38
JH
705 Perl_warn(aTHX_ "clear %" SVf, sv);
706 return 0;
b13b2135
NIS
707}
708
709static int
710perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
711{
14a5cf38
JH
712 Perl_warn(aTHX_ "free %" SVf, sv);
713 return 0;
b13b2135
NIS
714}
715
716MGVTBL perlio_vtab = {
14a5cf38
JH
717 perlio_mg_get,
718 perlio_mg_set,
22569500 719 NULL, /* len */
14a5cf38
JH
720 perlio_mg_clear,
721 perlio_mg_free
b13b2135
NIS
722};
723
724XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
725{
14a5cf38
JH
726 dXSARGS;
727 SV *sv = SvRV(ST(1));
728 AV *av = newAV();
729 MAGIC *mg;
730 int count = 0;
731 int i;
732 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
733 SvRMAGICAL_off(sv);
734 mg = mg_find(sv, PERL_MAGIC_ext);
735 mg->mg_virtual = &perlio_vtab;
736 mg_magical(sv);
737 Perl_warn(aTHX_ "attrib %" SVf, sv);
738 for (i = 2; i < items; i++) {
739 STRLEN len;
740 const char *name = SvPV(ST(i), len);
741 SV *layer = PerlIO_find_layer(aTHX_ name, len, 1);
742 if (layer) {
743 av_push(av, SvREFCNT_inc(layer));
744 }
745 else {
746 ST(count) = ST(i);
747 count++;
748 }
749 }
750 SvREFCNT_dec(av);
751 XSRETURN(count);
752}
753
22569500 754#endif /* USE_ATTIBUTES_FOR_PERLIO */
2a1bc955 755
e3f3bf95
NIS
756SV *
757PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
f3862f8b 758{
14a5cf38
JH
759 HV *stash = gv_stashpv("PerlIO::Layer", TRUE);
760 SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
761 return sv;
e3f3bf95
NIS
762}
763
0c4f7ff0
NIS
764XS(XS_PerlIO__Layer__find)
765{
14a5cf38
JH
766 dXSARGS;
767 if (items < 2)
768 Perl_croak(aTHX_ "Usage class->find(name[,load])");
769 else {
770 STRLEN len = 0;
771 char *name = SvPV(ST(1), len);
772 bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
773 PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ name, len, load);
774 ST(0) =
775 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
776 &PL_sv_undef;
777 XSRETURN(1);
778 }
0c4f7ff0
NIS
779}
780
e3f3bf95
NIS
781void
782PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
783{
3a1ee7e8
NIS
784 if (!PL_known_layers)
785 PL_known_layers = PerlIO_list_alloc(aTHX);
786 PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
fe5a182c 787 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
f3862f8b
NIS
788}
789
1141d9f8 790int
fcf2db38 791PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
1141d9f8 792{
14a5cf38
JH
793 if (names) {
794 const char *s = names;
795 while (*s) {
796 while (isSPACE(*s) || *s == ':')
797 s++;
798 if (*s) {
799 STRLEN llen = 0;
800 const char *e = s;
801 const char *as = Nullch;
802 STRLEN alen = 0;
803 if (!isIDFIRST(*s)) {
804 /*
805 * Message is consistent with how attribute lists are
806 * passed. Even though this means "foo : : bar" is
71200d45 807 * seen as an invalid separator character.
14a5cf38
JH
808 */
809 char q = ((*s == '\'') ? '"' : '\'');
99ef548b 810 if (ckWARN(WARN_LAYER))
22569500 811 Perl_warner(aTHX_ packWARN(WARN_LAYER),
1e616cf5
JH
812 "perlio: invalid separator character %c%c%c in layer specification list %s",
813 q, *s, q, s);
0cff2cf3 814 SETERRNO(EINVAL, LIB$_INVARG);
14a5cf38
JH
815 return -1;
816 }
817 do {
818 e++;
819 } while (isALNUM(*e));
820 llen = e - s;
821 if (*e == '(') {
822 int nesting = 1;
823 as = ++e;
824 while (nesting) {
825 switch (*e++) {
826 case ')':
827 if (--nesting == 0)
828 alen = (e - 1) - as;
829 break;
830 case '(':
831 ++nesting;
832 break;
833 case '\\':
834 /*
835 * It's a nul terminated string, not allowed
836 * to \ the terminating null. Anything other
71200d45 837 * character is passed over.
14a5cf38
JH
838 */
839 if (*e++) {
840 break;
841 }
842 /*
71200d45 843 * Drop through
14a5cf38
JH
844 */
845 case '\0':
846 e--;
22569500
NIS
847 if (ckWARN(WARN_LAYER))
848 Perl_warner(aTHX_ packWARN(WARN_LAYER),
14a5cf38
JH
849 "perlio: argument list not closed for layer \"%.*s\"",
850 (int) (e - s), s);
851 return -1;
852 default:
853 /*
71200d45 854 * boring.
14a5cf38
JH
855 */
856 break;
857 }
858 }
859 }
860 if (e > s) {
99ef548b 861 bool warn_layer = ckWARN(WARN_LAYER);
14a5cf38
JH
862 PerlIO_funcs *layer =
863 PerlIO_find_layer(aTHX_ s, llen, 1);
864 if (layer) {
3a1ee7e8 865 PerlIO_list_push(aTHX_ av, layer,
14a5cf38
JH
866 (as) ? newSVpvn(as,
867 alen) :
868 &PL_sv_undef);
869 }
870 else {
22569500 871 if (warn_layer)
99ef548b 872 Perl_warner(aTHX_ packWARN(WARN_LAYER), "perlio: unknown layer \"%.*s\"",
14a5cf38
JH
873 (int) llen, s);
874 return -1;
875 }
876 }
877 s = e;
878 }
879 }
880 }
881 return 0;
1141d9f8
NIS
882}
883
dfebf958 884void
fcf2db38 885PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
dfebf958 886{
14a5cf38 887 PerlIO_funcs *tab = &PerlIO_perlio;
35990314 888#ifdef PERLIO_USING_CRLF
6ce75a77 889 tab = &PerlIO_crlf;
846be114 890#else
6ce75a77 891 if (PerlIO_stdio.Set_ptrcnt)
22569500 892 tab = &PerlIO_stdio;
846be114 893#endif
14a5cf38 894 PerlIO_debug("Pushing %s\n", tab->name);
3a1ee7e8 895 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
14a5cf38 896 &PL_sv_undef);
dfebf958
NIS
897}
898
e3f3bf95 899SV *
14a5cf38 900PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
e3f3bf95 901{
14a5cf38 902 return av->array[n].arg;
e3f3bf95
NIS
903}
904
f3862f8b 905PerlIO_funcs *
14a5cf38 906PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
f3862f8b 907{
14a5cf38
JH
908 if (n >= 0 && n < av->cur) {
909 PerlIO_debug("Layer %" IVdf " is %s\n", n,
910 av->array[n].funcs->name);
911 return av->array[n].funcs;
912 }
913 if (!def)
914 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
915 return def;
e3f3bf95
NIS
916}
917
fcf2db38 918PerlIO_list_t *
e3f3bf95
NIS
919PerlIO_default_layers(pTHX)
920{
3a1ee7e8 921 if (!PL_def_layerlist) {
14a5cf38
JH
922 const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
923 PerlIO_funcs *osLayer = &PerlIO_unix;
3a1ee7e8 924 PL_def_layerlist = PerlIO_list_alloc(aTHX);
14a5cf38 925 PerlIO_define_layer(aTHX_ & PerlIO_unix);
e1caacb4 926#if defined(WIN32) && !defined(UNDER_CE)
14a5cf38 927 PerlIO_define_layer(aTHX_ & PerlIO_win32);
2f8118af 928#if 0
14a5cf38 929 osLayer = &PerlIO_win32;
0c4128ad 930#endif
2f8118af 931#endif
14a5cf38
JH
932 PerlIO_define_layer(aTHX_ & PerlIO_raw);
933 PerlIO_define_layer(aTHX_ & PerlIO_perlio);
934 PerlIO_define_layer(aTHX_ & PerlIO_stdio);
935 PerlIO_define_layer(aTHX_ & PerlIO_crlf);
06da4f11 936#ifdef HAS_MMAP
14a5cf38 937 PerlIO_define_layer(aTHX_ & PerlIO_mmap);
06da4f11 938#endif
14a5cf38
JH
939 PerlIO_define_layer(aTHX_ & PerlIO_utf8);
940 PerlIO_define_layer(aTHX_ & PerlIO_byte);
3a1ee7e8 941 PerlIO_list_push(aTHX_ PL_def_layerlist,
14a5cf38
JH
942 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
943 &PL_sv_undef);
944 if (s) {
3a1ee7e8 945 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
14a5cf38
JH
946 }
947 else {
3a1ee7e8 948 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
14a5cf38 949 }
1141d9f8 950 }
3a1ee7e8
NIS
951 if (PL_def_layerlist->cur < 2) {
952 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
f3862f8b 953 }
3a1ee7e8 954 return PL_def_layerlist;
e3f3bf95
NIS
955}
956
0c4f7ff0
NIS
957void
958Perl_boot_core_PerlIO(pTHX)
959{
960#ifdef USE_ATTRIBUTES_FOR_PERLIO
14a5cf38
JH
961 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
962 __FILE__);
0c4f7ff0 963#endif
14a5cf38 964 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
0c4f7ff0 965}
e3f3bf95
NIS
966
967PerlIO_funcs *
968PerlIO_default_layer(pTHX_ I32 n)
969{
14a5cf38
JH
970 PerlIO_list_t *av = PerlIO_default_layers(aTHX);
971 if (n < 0)
972 n += av->cur;
973 return PerlIO_layer_fetch(aTHX_ av, n, &PerlIO_stdio);
f3862f8b
NIS
974}
975
a999f61b
NIS
976#define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
977#define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
60382766
NIS
978
979void
1141d9f8 980PerlIO_stdstreams(pTHX)
60382766 981{
a1ea730d 982 if (!PL_perlio) {
14a5cf38
JH
983 PerlIO_allocate(aTHX);
984 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
985 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
986 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
987 }
60382766
NIS
988}
989
990PerlIO *
14a5cf38
JH
991PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg)
992{
2dc2558e
NIS
993 if (tab->fsize != sizeof(PerlIO_funcs)) {
994 mismatch:
995 Perl_croak(aTHX_ "Layer does not match this perl");
996 }
997 if (tab->size) {
998 PerlIOl *l = NULL;
999 if (tab->size < sizeof(PerlIOl)) {
1000 goto mismatch;
1001 }
1002 /* Real layer with a data area */
1003 Newc('L',l,tab->size,char,PerlIOl);
1004 if (l && f) {
1005 Zero(l, tab->size, char);
1006 l->next = *f;
1007 l->tab = tab;
1008 *f = l;
1009 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1010 (mode) ? mode : "(Null)", (void*)arg);
1011 if ((*l->tab->Pushed) (aTHX_ f, mode, arg, tab) != 0) {
1012 PerlIO_pop(aTHX_ f);
1013 return NULL;
1014 }
1015 }
1016 }
1017 else if (f) {
1018 /* Pseudo-layer where push does its own stack adjust */
00f51856
NIS
1019 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1020 (mode) ? mode : "(Null)", (void*)arg);
2dc2558e 1021 if ((*tab->Pushed) (aTHX_ f, mode, arg, tab) != 0) {
14a5cf38
JH
1022 return NULL;
1023 }
1024 }
1025 return f;
60382766
NIS
1026}
1027
dfebf958 1028IV
2dc2558e 1029PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4b803d04 1030{
14a5cf38
JH
1031 PerlIO_pop(aTHX_ f);
1032 if (*f) {
1033 PerlIO_flush(f);
1034 PerlIO_pop(aTHX_ f);
1035 return 0;
1036 }
1037 return -1;
4b803d04
NIS
1038}
1039
1040IV
86e05cf2
NIS
1041PerlIOBase_binmode(pTHX_ PerlIO *f)
1042{
1043 if (PerlIOValid(f)) {
1044 /* Is layer suitable for raw stream ? */
1045 if (PerlIOBase(f)->tab->kind & PERLIO_K_RAW) {
1046 /* Yes - turn off UTF-8-ness, to undo UTF-8 locale effects */
1047 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1048 }
1049 else {
1050 /* Not suitable - pop it */
1051 PerlIO_pop(aTHX_ f);
1052 }
1053 return 0;
1054 }
1055 return -1;
1056}
1057
1058IV
2dc2558e 1059PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
dfebf958 1060{
86e05cf2 1061
04892f78 1062 if (PerlIOValid(f)) {
86e05cf2
NIS
1063 PerlIO *t;
1064 PerlIOl *l;
14a5cf38 1065 PerlIO_flush(f);
86e05cf2
NIS
1066 /*
1067 * Strip all layers that are not suitable for a raw stream
1068 */
1069 t = f;
1070 while (t && (l = *t)) {
1071 if (l->tab->Binmode) {
1072 /* Has a handler - normal case */
1073 if ((*l->tab->Binmode)(aTHX_ f) == 0) {
1074 if (*t == l) {
1075 /* Layer still there - move down a layer */
1076 t = PerlIONext(t);
1077 }
1078 }
1079 else {
1080 return -1;
1081 }
14a5cf38
JH
1082 }
1083 else {
86e05cf2
NIS
1084 /* No handler - pop it */
1085 PerlIO_pop(aTHX_ t);
14a5cf38
JH
1086 }
1087 }
86e05cf2
NIS
1088 if (PerlIOValid(f)) {
1089 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1090 return 0;
1091 }
14a5cf38
JH
1092 }
1093 return -1;
dfebf958
NIS
1094}
1095
ac27b0f5 1096int
14a5cf38 1097PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
d9dac8cd 1098 PerlIO_list_t *layers, IV n, IV max)
14a5cf38 1099{
14a5cf38
JH
1100 int code = 0;
1101 while (n < max) {
1102 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
1103 if (tab) {
1104 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1105 code = -1;
1106 break;
1107 }
1108 }
1109 n++;
1110 }
1111 return code;
e3f3bf95
NIS
1112}
1113
1114int
ac27b0f5
NIS
1115PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1116{
14a5cf38 1117 int code = 0;
53f1b6d2 1118 if (f && names) {
3a1ee7e8 1119 PerlIO_list_t *layers = PerlIO_list_alloc(aTHX);
14a5cf38
JH
1120 code = PerlIO_parse_layers(aTHX_ layers, names);
1121 if (code == 0) {
d9dac8cd 1122 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
14a5cf38 1123 }
3a1ee7e8 1124 PerlIO_list_free(aTHX_ layers);
ac27b0f5 1125 }
14a5cf38 1126 return code;
ac27b0f5
NIS
1127}
1128
f3862f8b 1129
60382766 1130/*--------------------------------------------------------------------------------------*/
14a5cf38 1131/*
71200d45 1132 * Given the abstraction above the public API functions
14a5cf38 1133 */
60382766
NIS
1134
1135int
f5b9d040 1136PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
76ced9ad 1137{
14a5cf38 1138 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
fe5a182c 1139 (void*)f, PerlIOBase(f)->tab->name, iotype, mode,
14a5cf38 1140 (names) ? names : "(Null)");
03c0554d
NIS
1141 if (names) {
1142 /* Do not flush etc. if (e.g.) switching encodings.
1143 if a pushed layer knows it needs to flush lower layers
1144 (for example :unix which is never going to call them)
1145 it can do the flush when it is pushed.
1146 */
1147 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1148 }
1149 else {
86e05cf2 1150 /* Fake 5.6 legacy of using this call to turn ON O_TEXT */
35990314 1151#ifdef PERLIO_USING_CRLF
03c0554d
NIS
1152 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1153 O_BINARY so we can look for it in mode.
1154 */
1155 if (!(mode & O_BINARY)) {
1156 /* Text mode */
86e05cf2
NIS
1157 /* FIXME?: Looking down the layer stack seems wrong,
1158 but is a way of reaching past (say) an encoding layer
1159 to flip CRLF-ness of the layer(s) below
1160 */
03c0554d
NIS
1161 while (*f) {
1162 /* Perhaps we should turn on bottom-most aware layer
1163 e.g. Ilya's idea that UNIX TTY could serve
1164 */
1165 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1166 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1167 /* Not in text mode - flush any pending stuff and flip it */
1168 PerlIO_flush(f);
1169 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1170 }
1171 /* Only need to turn it on in one layer so we are done */
1172 return TRUE;
ed53a2bb 1173 }
03c0554d 1174 f = PerlIONext(f);
14a5cf38 1175 }
03c0554d
NIS
1176 /* Not finding a CRLF aware layer presumably means we are binary
1177 which is not what was requested - so we failed
1178 We _could_ push :crlf layer but so could caller
1179 */
1180 return FALSE;
14a5cf38 1181 }
6ce75a77 1182#endif
86e05cf2
NIS
1183 /* Legacy binmode is now _defined_ as being equivalent to pushing :raw
1184 So code that used to be here is now in PerlIORaw_pushed().
03c0554d 1185 */
86e05cf2 1186 return PerlIO_push(aTHX_ f, &PerlIO_raw, Nullch, Nullsv) ? TRUE : FALSE;
14a5cf38 1187 }
f5b9d040
NIS
1188}
1189
f5b9d040 1190int
e87a358a 1191PerlIO__close(pTHX_ PerlIO *f)
f5b9d040 1192{
04892f78 1193 if (PerlIOValid(f))
f62ce20a 1194 return (*PerlIOBase(f)->tab->Close) (aTHX_ f);
14a5cf38
JH
1195 else {
1196 SETERRNO(EBADF, SS$_IVCHAN);
1197 return -1;
1198 }
76ced9ad
NIS
1199}
1200
b931b1d9 1201int
e87a358a 1202Perl_PerlIO_close(pTHX_ PerlIO *f)
b931b1d9 1203{
14a5cf38 1204 int code = -1;
04892f78 1205 if (PerlIOValid(f)) {
f62ce20a 1206 code = (*PerlIOBase(f)->tab->Close) (aTHX_ f);
14a5cf38
JH
1207 while (*f) {
1208 PerlIO_pop(aTHX_ f);
1209 }
f6c77cf1 1210 }
14a5cf38 1211 return code;
b931b1d9
NIS
1212}
1213
b931b1d9 1214int
e87a358a 1215Perl_PerlIO_fileno(pTHX_ PerlIO *f)
b931b1d9 1216{
04892f78 1217 if (PerlIOValid(f))
f62ce20a 1218 return (*PerlIOBase(f)->tab->Fileno) (aTHX_ f);
14a5cf38
JH
1219 else {
1220 SETERRNO(EBADF, SS$_IVCHAN);
1221 return -1;
1222 }
b931b1d9
NIS
1223}
1224
1141d9f8
NIS
1225static const char *
1226PerlIO_context_layers(pTHX_ const char *mode)
1227{
14a5cf38
JH
1228 const char *type = NULL;
1229 /*
71200d45 1230 * Need to supply default layer info from open.pm
14a5cf38
JH
1231 */
1232 if (PL_curcop) {
1233 SV *layers = PL_curcop->cop_io;
1234 if (layers) {
1235 STRLEN len;
1236 type = SvPV(layers, len);
1237 if (type && mode[0] != 'r') {
1238 /*
71200d45 1239 * Skip to write part
14a5cf38
JH
1240 */
1241 const char *s = strchr(type, 0);
eb160463 1242 if (s && (STRLEN)(s - type) < len) {
14a5cf38
JH
1243 type = s + 1;
1244 }
1245 }
1246 }
1247 }
1248 return type;
1141d9f8
NIS
1249}
1250
fcf2db38 1251static PerlIO_funcs *
2edd7e44
NIS
1252PerlIO_layer_from_ref(pTHX_ SV *sv)
1253{
14a5cf38 1254 /*
71200d45 1255 * For any scalar type load the handler which is bundled with perl
14a5cf38
JH
1256 */
1257 if (SvTYPE(sv) < SVt_PVAV)
e934609f 1258 return PerlIO_find_layer(aTHX_ "scalar", 6, 1);
14a5cf38
JH
1259
1260 /*
71200d45 1261 * For other types allow if layer is known but don't try and load it
14a5cf38
JH
1262 */
1263 switch (SvTYPE(sv)) {
1264 case SVt_PVAV:
1265 return PerlIO_find_layer(aTHX_ "Array", 5, 0);
1266 case SVt_PVHV:
1267 return PerlIO_find_layer(aTHX_ "Hash", 4, 0);
1268 case SVt_PVCV:
1269 return PerlIO_find_layer(aTHX_ "Code", 4, 0);
1270 case SVt_PVGV:
1271 return PerlIO_find_layer(aTHX_ "Glob", 4, 0);
1272 }
1273 return NULL;
2edd7e44
NIS
1274}
1275
fcf2db38 1276PerlIO_list_t *
14a5cf38
JH
1277PerlIO_resolve_layers(pTHX_ const char *layers,
1278 const char *mode, int narg, SV **args)
1279{
1280 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1281 int incdef = 1;
a1ea730d 1282 if (!PL_perlio)
14a5cf38
JH
1283 PerlIO_stdstreams(aTHX);
1284 if (narg) {
1285 SV *arg = *args;
1286 /*
71200d45
NIS
1287 * If it is a reference but not an object see if we have a handler
1288 * for it
14a5cf38
JH
1289 */
1290 if (SvROK(arg) && !sv_isobject(arg)) {
1291 PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
1292 if (handler) {
3a1ee7e8
NIS
1293 def = PerlIO_list_alloc(aTHX);
1294 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
14a5cf38
JH
1295 incdef = 0;
1296 }
1297 /*
e934609f 1298 * Don't fail if handler cannot be found :via(...) etc. may do
14a5cf38 1299 * something sensible else we will just stringfy and open
71200d45 1300 * resulting string.
14a5cf38
JH
1301 */
1302 }
1303 }
1304 if (!layers)
1305 layers = PerlIO_context_layers(aTHX_ mode);
1306 if (layers && *layers) {
1307 PerlIO_list_t *av;
1308 if (incdef) {
1309 IV i = def->cur;
3a1ee7e8 1310 av = PerlIO_list_alloc(aTHX);
14a5cf38 1311 for (i = 0; i < def->cur; i++) {
3a1ee7e8 1312 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
14a5cf38
JH
1313 def->array[i].arg);
1314 }
1315 }
1316 else {
1317 av = def;
1318 }
0cff2cf3
NIS
1319 if (PerlIO_parse_layers(aTHX_ av, layers) == 0) {
1320 return av;
1321 }
1322 else {
1323 PerlIO_list_free(aTHX_ av);
1324 return (PerlIO_list_t *) NULL;
1325 }
14a5cf38
JH
1326 }
1327 else {
1328 if (incdef)
1329 def->refcnt++;
1330 return def;
1331 }
ee518936
NIS
1332}
1333
1334PerlIO *
14a5cf38
JH
1335PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1336 int imode, int perm, PerlIO *f, int narg, SV **args)
1337{
1338 if (!f && narg == 1 && *args == &PL_sv_undef) {
1339 if ((f = PerlIO_tmpfile())) {
1340 if (!layers)
1341 layers = PerlIO_context_layers(aTHX_ mode);
1342 if (layers && *layers)
1343 PerlIO_apply_layers(aTHX_ f, mode, layers);
1344 }
1345 }
1346 else {
1347 PerlIO_list_t *layera = NULL;
1348 IV n;
1349 PerlIO_funcs *tab = NULL;
04892f78 1350 if (PerlIOValid(f)) {
14a5cf38 1351 /*
71200d45
NIS
1352 * This is "reopen" - it is not tested as perl does not use it
1353 * yet
14a5cf38
JH
1354 */
1355 PerlIOl *l = *f;
3a1ee7e8 1356 layera = PerlIO_list_alloc(aTHX);
14a5cf38 1357 while (l) {
04892f78
NIS
1358 SV *arg = (l->tab->Getarg)
1359 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1360 : &PL_sv_undef;
3a1ee7e8 1361 PerlIO_list_push(aTHX_ layera, l->tab, arg);
14a5cf38
JH
1362 l = *PerlIONext(&l);
1363 }
1364 }
1365 else {
1366 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
0cff2cf3
NIS
1367 if (!layera) {
1368 return NULL;
1369 }
14a5cf38
JH
1370 }
1371 /*
71200d45 1372 * Start at "top" of layer stack
14a5cf38
JH
1373 */
1374 n = layera->cur - 1;
1375 while (n >= 0) {
1376 PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
1377 if (t && t->Open) {
1378 tab = t;
1379 break;
1380 }
1381 n--;
1382 }
1383 if (tab) {
1384 /*
71200d45 1385 * Found that layer 'n' can do opens - call it
14a5cf38 1386 */
7cf31beb 1387 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
3b8752bb 1388 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
7cf31beb 1389 }
14a5cf38 1390 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
fe5a182c
JH
1391 tab->name, layers, mode, fd, imode, perm,
1392 (void*)f, narg, (void*)args);
14a5cf38
JH
1393 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1394 f, narg, args);
1395 if (f) {
1396 if (n + 1 < layera->cur) {
1397 /*
1398 * More layers above the one that we used to open -
71200d45 1399 * apply them now
14a5cf38 1400 */
d9dac8cd
NIS
1401 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1402 /* If pushing layers fails close the file */
1403 PerlIO_close(f);
14a5cf38
JH
1404 f = NULL;
1405 }
1406 }
1407 }
1408 }
3a1ee7e8 1409 PerlIO_list_free(aTHX_ layera);
14a5cf38
JH
1410 }
1411 return f;
ee518936 1412}
b931b1d9
NIS
1413
1414
9e353e3b 1415SSize_t
e87a358a 1416Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
6f9d8c32 1417{
04892f78 1418 if (PerlIOValid(f))
f62ce20a 1419 return (*PerlIOBase(f)->tab->Read) (aTHX_ f, vbuf, count);
14a5cf38
JH
1420 else {
1421 SETERRNO(EBADF, SS$_IVCHAN);
1422 return -1;
1423 }
760ac839
LW
1424}
1425
313ca112 1426SSize_t
e87a358a 1427Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 1428{
04892f78 1429 if (PerlIOValid(f))
f62ce20a 1430 return (*PerlIOBase(f)->tab->Unread) (aTHX_ f, vbuf, count);
14a5cf38
JH
1431 else {
1432 SETERRNO(EBADF, SS$_IVCHAN);
1433 return -1;
1434 }
760ac839
LW
1435}
1436
9e353e3b 1437SSize_t
e87a358a 1438Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 1439{
04892f78 1440 if (PerlIOValid(f))
f62ce20a 1441 return (*PerlIOBase(f)->tab->Write) (aTHX_ f, vbuf, count);
14a5cf38
JH
1442 else {
1443 SETERRNO(EBADF, SS$_IVCHAN);
1444 return -1;
1445 }
760ac839
LW
1446}
1447
6f9d8c32 1448int
e87a358a 1449Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
760ac839 1450{
04892f78 1451 if (PerlIOValid(f))
f62ce20a 1452 return (*PerlIOBase(f)->tab->Seek) (aTHX_ f, offset, whence);
14a5cf38
JH
1453 else {
1454 SETERRNO(EBADF, SS$_IVCHAN);
1455 return -1;
1456 }
760ac839
LW
1457}
1458
9e353e3b 1459Off_t
e87a358a 1460Perl_PerlIO_tell(pTHX_ PerlIO *f)
760ac839 1461{
04892f78 1462 if (PerlIOValid(f))
f62ce20a 1463 return (*PerlIOBase(f)->tab->Tell) (aTHX_ f);
14a5cf38
JH
1464 else {
1465 SETERRNO(EBADF, SS$_IVCHAN);
1466 return -1;
1467 }
760ac839
LW
1468}
1469
6f9d8c32 1470int
e87a358a 1471Perl_PerlIO_flush(pTHX_ PerlIO *f)
760ac839 1472{
14a5cf38
JH
1473 if (f) {
1474 if (*f) {
1475 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1476 if (tab && tab->Flush) {
f62ce20a 1477 return (*tab->Flush) (aTHX_ f);
14a5cf38
JH
1478 }
1479 else {
fe5a182c 1480 PerlIO_debug("Cannot flush f=%p :%s\n", (void*)f, tab->name);
14a5cf38
JH
1481 SETERRNO(EBADF, SS$_IVCHAN);
1482 return -1;
1483 }
1484 }
1485 else {
fe5a182c 1486 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
14a5cf38
JH
1487 SETERRNO(EBADF, SS$_IVCHAN);
1488 return -1;
1489 }
1490 }
1491 else {
1492 /*
1493 * Is it good API design to do flush-all on NULL, a potentially
1494 * errorneous input? Maybe some magical value (PerlIO*
1495 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1496 * things on fflush(NULL), but should we be bound by their design
71200d45 1497 * decisions? --jhi
14a5cf38 1498 */
a1ea730d 1499 PerlIO **table = &PL_perlio;
14a5cf38
JH
1500 int code = 0;
1501 while ((f = *table)) {
1502 int i;
1503 table = (PerlIO **) (f++);
1504 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1505 if (*f && PerlIO_flush(f) != 0)
1506 code = -1;
1507 f++;
1508 }
1509 }
1510 return code;
1511 }
760ac839
LW
1512}
1513
a9c883f6 1514void
f62ce20a 1515PerlIOBase_flush_linebuf(pTHX)
a9c883f6 1516{
a1ea730d 1517 PerlIO **table = &PL_perlio;
14a5cf38
JH
1518 PerlIO *f;
1519 while ((f = *table)) {
1520 int i;
1521 table = (PerlIO **) (f++);
1522 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1523 if (*f
1524 && (PerlIOBase(f)->
1525 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1526 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1527 PerlIO_flush(f);
1528 f++;
1529 }
a9c883f6 1530 }
a9c883f6
NIS
1531}
1532
06da4f11 1533int
e87a358a 1534Perl_PerlIO_fill(pTHX_ PerlIO *f)
06da4f11 1535{
04892f78 1536 if (PerlIOValid(f))
f62ce20a 1537 return (*PerlIOBase(f)->tab->Fill) (aTHX_ f);
14a5cf38
JH
1538 else {
1539 SETERRNO(EBADF, SS$_IVCHAN);
1540 return -1;
1541 }
06da4f11
NIS
1542}
1543
f3862f8b
NIS
1544int
1545PerlIO_isutf8(PerlIO *f)
1546{
04892f78 1547 if (PerlIOValid(f))
14a5cf38
JH
1548 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1549 else {
1550 SETERRNO(EBADF, SS$_IVCHAN);
1551 return -1;
1552 }
f3862f8b
NIS
1553}
1554
6f9d8c32 1555int
e87a358a 1556Perl_PerlIO_eof(pTHX_ PerlIO *f)
760ac839 1557{
04892f78 1558 if (PerlIOValid(f))
f62ce20a 1559 return (*PerlIOBase(f)->tab->Eof) (aTHX_ f);
14a5cf38
JH
1560 else {
1561 SETERRNO(EBADF, SS$_IVCHAN);
1562 return -1;
1563 }
9e353e3b
NIS
1564}
1565
9e353e3b 1566int
e87a358a 1567Perl_PerlIO_error(pTHX_ PerlIO *f)
9e353e3b 1568{
04892f78 1569 if (PerlIOValid(f))
f62ce20a 1570 return (*PerlIOBase(f)->tab->Error) (aTHX_ f);
14a5cf38
JH
1571 else {
1572 SETERRNO(EBADF, SS$_IVCHAN);
1573 return -1;
1574 }
9e353e3b
NIS
1575}
1576
9e353e3b 1577void
e87a358a 1578Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
9e353e3b 1579{
04892f78 1580 if (PerlIOValid(f))
f62ce20a 1581 (*PerlIOBase(f)->tab->Clearerr) (aTHX_ f);
14a5cf38
JH
1582 else
1583 SETERRNO(EBADF, SS$_IVCHAN);
9e353e3b
NIS
1584}
1585
9e353e3b 1586void
e87a358a 1587Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
9e353e3b 1588{
04892f78 1589 if (PerlIOValid(f))
f62ce20a 1590 (*PerlIOBase(f)->tab->Setlinebuf) (aTHX_ f);
14a5cf38
JH
1591 else
1592 SETERRNO(EBADF, SS$_IVCHAN);
9e353e3b
NIS
1593}
1594
9e353e3b
NIS
1595int
1596PerlIO_has_base(PerlIO *f)
1597{
04892f78 1598 if (PerlIOValid(f)) {
14a5cf38
JH
1599 return (PerlIOBase(f)->tab->Get_base != NULL);
1600 }
1601 return 0;
760ac839
LW
1602}
1603
9e353e3b
NIS
1604int
1605PerlIO_fast_gets(PerlIO *f)
760ac839 1606{
04892f78 1607 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
14a5cf38
JH
1608 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1609 return (tab->Set_ptrcnt != NULL);
1610 }
1611 return 0;
9e353e3b
NIS
1612}
1613
9e353e3b
NIS
1614int
1615PerlIO_has_cntptr(PerlIO *f)
1616{
04892f78 1617 if (PerlIOValid(f)) {
14a5cf38
JH
1618 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1619 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1620 }
1621 return 0;
9e353e3b
NIS
1622}
1623
9e353e3b
NIS
1624int
1625PerlIO_canset_cnt(PerlIO *f)
1626{
04892f78 1627 if (PerlIOValid(f)) {
14a5cf38
JH
1628 PerlIOl *l = PerlIOBase(f);
1629 return (l->tab->Set_ptrcnt != NULL);
1630 }
1631 return 0;
760ac839
LW
1632}
1633
888911fc 1634STDCHAR *
e87a358a 1635Perl_PerlIO_get_base(pTHX_ PerlIO *f)
760ac839 1636{
04892f78 1637 if (PerlIOValid(f))
f62ce20a 1638 return (*PerlIOBase(f)->tab->Get_base) (aTHX_ f);
14a5cf38 1639 return NULL;
9e353e3b
NIS
1640}
1641
9e353e3b 1642int
e87a358a 1643Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
9e353e3b 1644{
04892f78 1645 if (PerlIOValid(f))
f62ce20a 1646 return (*PerlIOBase(f)->tab->Get_bufsiz) (aTHX_ f);
14a5cf38 1647 return 0;
9e353e3b
NIS
1648}
1649
9e353e3b 1650STDCHAR *
e87a358a 1651Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
9e353e3b 1652{
04892f78
NIS
1653 if (PerlIOValid(f)) {
1654 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1655 if (tab->Get_ptr == NULL)
1656 return NULL;
1657 return (*tab->Get_ptr) (aTHX_ f);
1658 }
1659 return NULL;
9e353e3b
NIS
1660}
1661
05d1247b 1662int
e87a358a 1663Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
9e353e3b 1664{
04892f78
NIS
1665 if (PerlIOValid(f)) {
1666 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1667 if (tab->Get_cnt == NULL)
1668 return 0;
1669 return (*tab->Get_cnt) (aTHX_ f);
1670 }
1671 return 0;
9e353e3b
NIS
1672}
1673
9e353e3b 1674void
e87a358a 1675Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
9e353e3b 1676{
04892f78
NIS
1677 if (PerlIOValid(f)) {
1678 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, NULL, cnt);
1679 }
9e353e3b
NIS
1680}
1681
9e353e3b 1682void
e87a358a 1683Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
9e353e3b 1684{
04892f78
NIS
1685 if (PerlIOValid(f)) {
1686 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1687 if (tab->Set_ptrcnt == NULL) {
1688 Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
1689 }
1690 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, ptr, cnt);
14a5cf38 1691 }
9e353e3b
NIS
1692}
1693
1694/*--------------------------------------------------------------------------------------*/
14a5cf38 1695/*
71200d45 1696 * utf8 and raw dummy layers
14a5cf38 1697 */
dfebf958 1698
26fb694e 1699IV
2dc2558e 1700PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
26fb694e 1701{
00f51856 1702 if (PerlIOValid(f)) {
14a5cf38
JH
1703 if (tab->kind & PERLIO_K_UTF8)
1704 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1705 else
1706 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1707 return 0;
1708 }
1709 return -1;
26fb694e
NIS
1710}
1711
dfebf958 1712PerlIO_funcs PerlIO_utf8 = {
2dc2558e 1713 sizeof(PerlIO_funcs),
14a5cf38 1714 "utf8",
2dc2558e 1715 0,
6874a2de 1716 PERLIO_K_DUMMY | PERLIO_K_UTF8,
14a5cf38
JH
1717 PerlIOUtf8_pushed,
1718 NULL,
1719 NULL,
1720 NULL,
1721 NULL,
1722 NULL,
1723 NULL,
1724 NULL,
1725 NULL,
1726 NULL,
1727 NULL,
22569500
NIS
1728 NULL, /* flush */
1729 NULL, /* fill */
14a5cf38
JH
1730 NULL,
1731 NULL,
1732 NULL,
1733 NULL,
22569500
NIS
1734 NULL, /* get_base */
1735 NULL, /* get_bufsiz */
1736 NULL, /* get_ptr */
1737 NULL, /* get_cnt */
1738 NULL, /* set_ptrcnt */
26fb694e
NIS
1739};
1740
1741PerlIO_funcs PerlIO_byte = {
2dc2558e 1742 sizeof(PerlIO_funcs),
14a5cf38 1743 "bytes",
2dc2558e 1744 0,
14a5cf38
JH
1745 PERLIO_K_DUMMY,
1746 PerlIOUtf8_pushed,
1747 NULL,
1748 NULL,
1749 NULL,
1750 NULL,
1751 NULL,
1752 NULL,
1753 NULL,
1754 NULL,
1755 NULL,
1756 NULL,
22569500
NIS
1757 NULL, /* flush */
1758 NULL, /* fill */
14a5cf38
JH
1759 NULL,
1760 NULL,
1761 NULL,
1762 NULL,
22569500
NIS
1763 NULL, /* get_base */
1764 NULL, /* get_bufsiz */
1765 NULL, /* get_ptr */
1766 NULL, /* get_cnt */
1767 NULL, /* set_ptrcnt */
dfebf958
NIS
1768};
1769
1770PerlIO *
14a5cf38
JH
1771PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1772 IV n, const char *mode, int fd, int imode, int perm,
1773 PerlIO *old, int narg, SV **args)
dfebf958 1774{
14a5cf38
JH
1775 PerlIO_funcs *tab = PerlIO_default_btm();
1776 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1777 old, narg, args);
dfebf958
NIS
1778}
1779
1780PerlIO_funcs PerlIO_raw = {
2dc2558e 1781 sizeof(PerlIO_funcs),
14a5cf38 1782 "raw",
2dc2558e 1783 0,
14a5cf38
JH
1784 PERLIO_K_DUMMY,
1785 PerlIORaw_pushed,
1786 PerlIOBase_popped,
1787 PerlIORaw_open,
1788 NULL,
1789 NULL,
1790 NULL,
1791 NULL,
1792 NULL,
1793 NULL,
1794 NULL,
1795 NULL,
22569500
NIS
1796 NULL, /* flush */
1797 NULL, /* fill */
14a5cf38
JH
1798 NULL,
1799 NULL,
1800 NULL,
1801 NULL,
22569500
NIS
1802 NULL, /* get_base */
1803 NULL, /* get_bufsiz */
1804 NULL, /* get_ptr */
1805 NULL, /* get_cnt */
1806 NULL, /* set_ptrcnt */
dfebf958
NIS
1807};
1808/*--------------------------------------------------------------------------------------*/
1809/*--------------------------------------------------------------------------------------*/
14a5cf38 1810/*
71200d45 1811 * "Methods" of the "base class"
14a5cf38 1812 */
9e353e3b
NIS
1813
1814IV
f62ce20a 1815PerlIOBase_fileno(pTHX_ PerlIO *f)
9e353e3b 1816{
04892f78 1817 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
9e353e3b
NIS
1818}
1819
f5b9d040 1820char *
81428673 1821PerlIO_modestr(PerlIO * f, char *buf)
14a5cf38
JH
1822{
1823 char *s = buf;
81428673
NIS
1824 if (PerlIOValid(f)) {
1825 IV flags = PerlIOBase(f)->flags;
1826 if (flags & PERLIO_F_APPEND) {
1827 *s++ = 'a';
1828 if (flags & PERLIO_F_CANREAD) {
1829 *s++ = '+';
1830 }
14a5cf38 1831 }
81428673
NIS
1832 else if (flags & PERLIO_F_CANREAD) {
1833 *s++ = 'r';
1834 if (flags & PERLIO_F_CANWRITE)
1835 *s++ = '+';
1836 }
1837 else if (flags & PERLIO_F_CANWRITE) {
1838 *s++ = 'w';
1839 if (flags & PERLIO_F_CANREAD) {
1840 *s++ = '+';
1841 }
14a5cf38 1842 }
35990314 1843#ifdef PERLIO_USING_CRLF
81428673
NIS
1844 if (!(flags & PERLIO_F_CRLF))
1845 *s++ = 'b';
5f1a76d0 1846#endif
81428673 1847 }
14a5cf38
JH
1848 *s = '\0';
1849 return buf;
f5b9d040
NIS
1850}
1851
81428673 1852
76ced9ad 1853IV
2dc2558e 1854PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
9e353e3b 1855{
14a5cf38 1856 PerlIOl *l = PerlIOBase(f);
b7953727 1857#if 0
14a5cf38
JH
1858 const char *omode = mode;
1859 char temp[8];
b7953727 1860#endif
14a5cf38
JH
1861 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1862 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
1863 if (tab->Set_ptrcnt != NULL)
1864 l->flags |= PERLIO_F_FASTGETS;
1865 if (mode) {
1866 if (*mode == '#' || *mode == 'I')
1867 mode++;
1868 switch (*mode++) {
1869 case 'r':
1870 l->flags |= PERLIO_F_CANREAD;
1871 break;
1872 case 'a':
1873 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
1874 break;
1875 case 'w':
1876 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
1877 break;
1878 default:
1879 SETERRNO(EINVAL, LIB$_INVARG);
1880 return -1;
1881 }
1882 while (*mode) {
1883 switch (*mode++) {
1884 case '+':
1885 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
1886 break;
1887 case 'b':
1888 l->flags &= ~PERLIO_F_CRLF;
1889 break;
1890 case 't':
1891 l->flags |= PERLIO_F_CRLF;
1892 break;
1893 default:
1894 SETERRNO(EINVAL, LIB$_INVARG);
1895 return -1;
1896 }
1897 }
1898 }
1899 else {
1900 if (l->next) {
1901 l->flags |= l->next->flags &
1902 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
1903 PERLIO_F_APPEND);
1904 }
1905 }
5e2ab84b 1906#if 0
14a5cf38
JH
1907 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
1908 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
1909 l->flags, PerlIO_modestr(f, temp));
5e2ab84b 1910#endif
14a5cf38 1911 return 0;
76ced9ad
NIS
1912}
1913
1914IV
f62ce20a 1915PerlIOBase_popped(pTHX_ PerlIO *f)
76ced9ad 1916{
14a5cf38 1917 return 0;
760ac839
LW
1918}
1919
9e353e3b 1920SSize_t
f62ce20a 1921PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 1922{
14a5cf38 1923 /*
71200d45 1924 * Save the position as current head considers it
14a5cf38
JH
1925 */
1926 Off_t old = PerlIO_tell(f);
1927 SSize_t done;
1928 PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv);
1929 PerlIOSelf(f, PerlIOBuf)->posn = old;
f62ce20a 1930 done = PerlIOBuf_unread(aTHX_ f, vbuf, count);
14a5cf38 1931 return done;
9e353e3b
NIS
1932}
1933
f6c77cf1 1934SSize_t
f62ce20a 1935PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
f6c77cf1 1936{
14a5cf38
JH
1937 STDCHAR *buf = (STDCHAR *) vbuf;
1938 if (f) {
1939 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1940 return 0;
1941 while (count > 0) {
1942 SSize_t avail = PerlIO_get_cnt(f);
1943 SSize_t take = 0;
1944 if (avail > 0)
eb160463 1945 take = ((SSize_t)count < avail) ? count : avail;
14a5cf38
JH
1946 if (take > 0) {
1947 STDCHAR *ptr = PerlIO_get_ptr(f);
1948 Copy(ptr, buf, take, STDCHAR);
1949 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
1950 count -= take;
1951 buf += take;
1952 }
1953 if (count > 0 && avail <= 0) {
1954 if (PerlIO_fill(f) != 0)
1955 break;
1956 }
1957 }
1958 return (buf - (STDCHAR *) vbuf);
1959 }
f6c77cf1 1960 return 0;
f6c77cf1
NIS
1961}
1962
9e353e3b 1963IV
f62ce20a 1964PerlIOBase_noop_ok(pTHX_ PerlIO *f)
9e353e3b 1965{
14a5cf38 1966 return 0;
9e353e3b
NIS
1967}
1968
1969IV
f62ce20a 1970PerlIOBase_noop_fail(pTHX_ PerlIO *f)
06da4f11 1971{
14a5cf38 1972 return -1;
06da4f11
NIS
1973}
1974
1975IV
f62ce20a 1976PerlIOBase_close(pTHX_ PerlIO *f)
9e353e3b 1977{
14a5cf38
JH
1978 IV code = 0;
1979 PerlIO *n = PerlIONext(f);
1980 if (PerlIO_flush(f) != 0)
1981 code = -1;
04892f78 1982 if (PerlIOValid(n) && (*PerlIOBase(n)->tab->Close)(aTHX_ n) != 0)
14a5cf38
JH
1983 code = -1;
1984 PerlIOBase(f)->flags &=
1985 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
1986 return code;
9e353e3b
NIS
1987}
1988
1989IV
f62ce20a 1990PerlIOBase_eof(pTHX_ PerlIO *f)
9e353e3b 1991{
04892f78 1992 if (PerlIOValid(f)) {
14a5cf38
JH
1993 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
1994 }
1995 return 1;
9e353e3b
NIS
1996}
1997
1998IV
f62ce20a 1999PerlIOBase_error(pTHX_ PerlIO *f)
9e353e3b 2000{
04892f78 2001 if (PerlIOValid(f)) {
14a5cf38
JH
2002 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
2003 }
2004 return 1;
9e353e3b
NIS
2005}
2006
2007void
f62ce20a 2008PerlIOBase_clearerr(pTHX_ PerlIO *f)
9e353e3b 2009{
04892f78 2010 if (PerlIOValid(f)) {
14a5cf38
JH
2011 PerlIO *n = PerlIONext(f);
2012 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
04892f78 2013 if (PerlIOValid(n))
14a5cf38
JH
2014 PerlIO_clearerr(n);
2015 }
9e353e3b
NIS
2016}
2017
2018void
f62ce20a 2019PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
9e353e3b 2020{
04892f78 2021 if (PerlIOValid(f)) {
14a5cf38
JH
2022 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
2023 }
9e353e3b
NIS
2024}
2025
93a8090d
NIS
2026SV *
2027PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
2028{
2029 if (!arg)
2030 return Nullsv;
2031#ifdef sv_dup
2032 if (param) {
2033 return sv_dup(arg, param);
2034 }
2035 else {
2036 return newSVsv(arg);
2037 }
2038#else
2039 return newSVsv(arg);
2040#endif
2041}
2042
2043PerlIO *
ecdeb87c 2044PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
93a8090d
NIS
2045{
2046 PerlIO *nexto = PerlIONext(o);
04892f78 2047 if (PerlIOValid(nexto)) {
93a8090d 2048 PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
ecdeb87c 2049 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
93a8090d
NIS
2050 }
2051 if (f) {
2052 PerlIO_funcs *self = PerlIOBase(o)->tab;
2053 SV *arg = Nullsv;
2054 char buf[8];
fe5a182c
JH
2055 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2056 self->name, (void*)f, (void*)o, (void*)param);
93a8090d 2057 if (self->Getarg) {
ecdeb87c 2058 arg = (*self->Getarg)(aTHX_ o,param,flags);
93a8090d
NIS
2059 }
2060 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
ecdeb87c 2061 if (arg) {
93a8090d
NIS
2062 SvREFCNT_dec(arg);
2063 }
2064 }
2065 return f;
2066}
2067
2068#define PERLIO_MAX_REFCOUNTABLE_FD 2048
168d5872 2069#ifdef USE_THREADS
93a8090d 2070perl_mutex PerlIO_mutex;
93a8090d 2071#endif
168d5872 2072int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD];
93a8090d
NIS
2073
2074void
2075PerlIO_init(pTHX)
2076{
2077 /* Place holder for stdstreams call ??? */
168d5872 2078#ifdef USE_THREADS
93a8090d
NIS
2079 MUTEX_INIT(&PerlIO_mutex);
2080#endif
2081}
2082
168d5872
NIS
2083void
2084PerlIOUnix_refcnt_inc(int fd)
2085{
2086 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2087#ifdef USE_THREADS
2088 MUTEX_LOCK(&PerlIO_mutex);
2089#endif
2090 PerlIO_fd_refcnt[fd]++;
2091 PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]);
2092#ifdef USE_THREADS
2093 MUTEX_UNLOCK(&PerlIO_mutex);
2094#endif
2095 }
2096}
2097
168d5872
NIS
2098int
2099PerlIOUnix_refcnt_dec(int fd)
2100{
2101 int cnt = 0;
2102 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2103#ifdef USE_THREADS
2104 MUTEX_LOCK(&PerlIO_mutex);
2105#endif
2106 cnt = --PerlIO_fd_refcnt[fd];
2107 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2108#ifdef USE_THREADS
2109 MUTEX_UNLOCK(&PerlIO_mutex);
2110#endif
2111 }
2112 return cnt;
2113}
2114
694c95cf
JH
2115void
2116PerlIO_cleanup(pTHX)
2117{
2118 int i;
2119#ifdef USE_ITHREADS
9f4bd222
NIS
2120 PerlIO_debug("Cleanup layers for %p\n",aTHX);
2121#else
2122 PerlIO_debug("Cleanup layers\n");
694c95cf
JH
2123#endif
2124 /* Raise STDIN..STDERR refcount so we don't close them */
2125 for (i=0; i < 3; i++)
2126 PerlIOUnix_refcnt_inc(i);
2127 PerlIO_cleantable(aTHX_ &PL_perlio);
2128 /* Restore STDIN..STDERR refcount */
2129 for (i=0; i < 3; i++)
2130 PerlIOUnix_refcnt_dec(i);
9f4bd222
NIS
2131
2132 if (PL_known_layers) {
2133 PerlIO_list_free(aTHX_ PL_known_layers);
2134 PL_known_layers = NULL;
2135 }
2136 if(PL_def_layerlist) {
2137 PerlIO_list_free(aTHX_ PL_def_layerlist);
2138 PL_def_layerlist = NULL;
2139 }
694c95cf
JH
2140}
2141
2142
2143
9e353e3b 2144/*--------------------------------------------------------------------------------------*/
14a5cf38 2145/*
71200d45 2146 * Bottom-most level for UNIX-like case
14a5cf38 2147 */
9e353e3b 2148
14a5cf38 2149typedef struct {
22569500
NIS
2150 struct _PerlIO base; /* The generic part */
2151 int fd; /* UNIX like file descriptor */
2152 int oflags; /* open/fcntl flags */
9e353e3b
NIS
2153} PerlIOUnix;
2154
6f9d8c32 2155int
9e353e3b 2156PerlIOUnix_oflags(const char *mode)
760ac839 2157{
14a5cf38
JH
2158 int oflags = -1;
2159 if (*mode == 'I' || *mode == '#')
2160 mode++;
2161 switch (*mode) {
2162 case 'r':
2163 oflags = O_RDONLY;
2164 if (*++mode == '+') {
2165 oflags = O_RDWR;
2166 mode++;
2167 }
2168 break;
2169
2170 case 'w':
2171 oflags = O_CREAT | O_TRUNC;
2172 if (*++mode == '+') {
2173 oflags |= O_RDWR;
2174 mode++;
2175 }
2176 else
2177 oflags |= O_WRONLY;
2178 break;
2179
2180 case 'a':
2181 oflags = O_CREAT | O_APPEND;
2182 if (*++mode == '+') {
2183 oflags |= O_RDWR;
2184 mode++;
2185 }
2186 else
2187 oflags |= O_WRONLY;
2188 break;
2189 }
2190 if (*mode == 'b') {
2191 oflags |= O_BINARY;
2192 oflags &= ~O_TEXT;
2193 mode++;
2194 }
2195 else if (*mode == 't') {
2196 oflags |= O_TEXT;
2197 oflags &= ~O_BINARY;
2198 mode++;
2199 }
2200 /*
71200d45 2201 * Always open in binary mode
14a5cf38
JH
2202 */
2203 oflags |= O_BINARY;
2204 if (*mode || oflags == -1) {
2205 SETERRNO(EINVAL, LIB$_INVARG);
2206 oflags = -1;
2207 }
2208 return oflags;
9e353e3b
NIS
2209}
2210
2211IV
f62ce20a 2212PerlIOUnix_fileno(pTHX_ PerlIO *f)
9e353e3b 2213{
14a5cf38 2214 return PerlIOSelf(f, PerlIOUnix)->fd;
9e353e3b
NIS
2215}
2216
4b803d04 2217IV
2dc2558e 2218PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4b803d04 2219{
2dc2558e 2220 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
93a8090d 2221 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
14a5cf38 2222 if (*PerlIONext(f)) {
4b069b44 2223 /* We never call down so do any pending stuff now */
03c0554d 2224 PerlIO_flush(PerlIONext(f));
14a5cf38
JH
2225 s->fd = PerlIO_fileno(PerlIONext(f));
2226 /*
71200d45 2227 * XXX could (or should) we retrieve the oflags from the open file
14a5cf38 2228 * handle rather than believing the "mode" we are passed in? XXX
71200d45 2229 * Should the value on NULL mode be 0 or -1?
14a5cf38
JH
2230 */
2231 s->oflags = mode ? PerlIOUnix_oflags(mode) : -1;
2232 }
2233 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2234 return code;
4b803d04
NIS
2235}
2236
9e353e3b 2237PerlIO *
14a5cf38
JH
2238PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2239 IV n, const char *mode, int fd, int imode,
2240 int perm, PerlIO *f, int narg, SV **args)
2241{
d9dac8cd 2242 if (PerlIOValid(f)) {
14a5cf38 2243 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
f62ce20a 2244 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
14a5cf38
JH
2245 }
2246 if (narg > 0) {
2247 char *path = SvPV_nolen(*args);
2248 if (*mode == '#')
2249 mode++;
2250 else {
2251 imode = PerlIOUnix_oflags(mode);
2252 perm = 0666;
2253 }
2254 if (imode != -1) {
2255 fd = PerlLIO_open3(path, imode, perm);
2256 }
2257 }
2258 if (fd >= 0) {
2259 PerlIOUnix *s;
2260 if (*mode == 'I')
2261 mode++;
2262 if (!f) {
2263 f = PerlIO_allocate(aTHX);
d9dac8cd
NIS
2264 }
2265 if (!PerlIOValid(f)) {
a33cf58c
NIS
2266 if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2267 return NULL;
2268 }
d9dac8cd 2269 }
a33cf58c 2270 s = PerlIOSelf(f, PerlIOUnix);
14a5cf38
JH
2271 s->fd = fd;
2272 s->oflags = imode;
2273 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
22569500 2274 PerlIOUnix_refcnt_inc(fd);
14a5cf38
JH
2275 return f;
2276 }
2277 else {
2278 if (f) {
2279 /*
71200d45 2280 * FIXME: pop layers ???
14a5cf38
JH
2281 */
2282 }
2283 return NULL;
2284 }
9e353e3b
NIS
2285}
2286
71200d45 2287PerlIO *
ecdeb87c 2288PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
71200d45
NIS
2289{
2290 PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix);
93a8090d 2291 int fd = os->fd;
ecdeb87c
NIS
2292 if (flags & PERLIO_DUP_FD) {
2293 fd = PerlLIO_dup(fd);
2294 }
93a8090d 2295 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
ecdeb87c 2296 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
71200d45
NIS
2297 if (f) {
2298 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2299 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2300 s->fd = fd;
93a8090d 2301 PerlIOUnix_refcnt_inc(fd);
71200d45
NIS
2302 return f;
2303 }
71200d45
NIS
2304 }
2305 return NULL;
2306}
2307
2308
9e353e3b 2309SSize_t
f62ce20a 2310PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
9e353e3b 2311{
14a5cf38 2312 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
81428673 2313 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) ||
1fd8f4ce 2314 PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) {
14a5cf38 2315 return 0;
1fd8f4ce 2316 }
14a5cf38
JH
2317 while (1) {
2318 SSize_t len = PerlLIO_read(fd, vbuf, count);
2319 if (len >= 0 || errno != EINTR) {
2320 if (len < 0)
2321 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2322 else if (len == 0 && count != 0)
2323 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2324 return len;
2325 }
2326 PERL_ASYNC_CHECK();
2327 }
9e353e3b
NIS
2328}
2329
2330SSize_t
f62ce20a 2331PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 2332{
14a5cf38
JH
2333 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2334 while (1) {
2335 SSize_t len = PerlLIO_write(fd, vbuf, count);
2336 if (len >= 0 || errno != EINTR) {
2337 if (len < 0)
2338 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2339 return len;
2340 }
2341 PERL_ASYNC_CHECK();
06da4f11 2342 }
9e353e3b
NIS
2343}
2344
2345IV
f62ce20a 2346PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
9e353e3b 2347{
14a5cf38
JH
2348 Off_t new =
2349 PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, offset, whence);
2350 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2351 return (new == (Off_t) - 1) ? -1 : 0;
9e353e3b
NIS
2352}
2353
2354Off_t
f62ce20a 2355PerlIOUnix_tell(pTHX_ PerlIO *f)
9e353e3b 2356{
14a5cf38 2357 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
9e353e3b
NIS
2358}
2359
71200d45 2360
9e353e3b 2361IV
f62ce20a 2362PerlIOUnix_close(pTHX_ PerlIO *f)
9e353e3b 2363{
14a5cf38
JH
2364 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2365 int code = 0;
168d5872
NIS
2366 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2367 if (PerlIOUnix_refcnt_dec(fd) > 0) {
93a8090d
NIS
2368 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2369 return 0;
22569500 2370 }
93a8090d
NIS
2371 }
2372 else {
2373 SETERRNO(EBADF,SS$_IVCHAN);
2374 return -1;
2375 }
14a5cf38
JH
2376 while (PerlLIO_close(fd) != 0) {
2377 if (errno != EINTR) {
2378 code = -1;
2379 break;
2380 }
2381 PERL_ASYNC_CHECK();
2382 }
2383 if (code == 0) {
2384 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2385 }
2386 return code;
9e353e3b
NIS
2387}
2388
2389PerlIO_funcs PerlIO_unix = {
2dc2558e 2390 sizeof(PerlIO_funcs),
14a5cf38
JH
2391 "unix",
2392 sizeof(PerlIOUnix),
2393 PERLIO_K_RAW,
2394 PerlIOUnix_pushed,
44798d05 2395 PerlIOBase_popped,
14a5cf38 2396 PerlIOUnix_open,
86e05cf2 2397 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
2398 NULL,
2399 PerlIOUnix_fileno,
71200d45 2400 PerlIOUnix_dup,
14a5cf38
JH
2401 PerlIOUnix_read,
2402 PerlIOBase_unread,
2403 PerlIOUnix_write,
2404 PerlIOUnix_seek,
2405 PerlIOUnix_tell,
2406 PerlIOUnix_close,
22569500
NIS
2407 PerlIOBase_noop_ok, /* flush */
2408 PerlIOBase_noop_fail, /* fill */
14a5cf38
JH
2409 PerlIOBase_eof,
2410 PerlIOBase_error,
2411 PerlIOBase_clearerr,
2412 PerlIOBase_setlinebuf,
22569500
NIS
2413 NULL, /* get_base */
2414 NULL, /* get_bufsiz */
2415 NULL, /* get_ptr */
2416 NULL, /* get_cnt */
2417 NULL, /* set_ptrcnt */
9e353e3b
NIS
2418};
2419
2420/*--------------------------------------------------------------------------------------*/
14a5cf38 2421/*
71200d45 2422 * stdio as a layer
14a5cf38 2423 */
9e353e3b 2424
313e59c8
NIS
2425#if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE)
2426/* perl5.8 - This ensures the last minute VMS ungetc fix is not
2427 broken by the last second glibc 2.3 fix
2428 */
2429#define STDIO_BUFFER_WRITABLE
2430#endif
2431
2432
14a5cf38
JH
2433typedef struct {
2434 struct _PerlIO base;
22569500 2435 FILE *stdio; /* The stream */
9e353e3b
NIS
2436} PerlIOStdio;
2437
2438IV
f62ce20a 2439PerlIOStdio_fileno(pTHX_ PerlIO *f)
9e353e3b 2440{
439ba545 2441 FILE *s;
81428673 2442 if (PerlIOValid(f) && (s = PerlIOSelf(f, PerlIOStdio)->stdio)) {
439ba545
NIS
2443 return PerlSIO_fileno(s);
2444 }
2445 errno = EBADF;
2446 return -1;
9e353e3b
NIS
2447}
2448
766a733e 2449char *
14a5cf38
JH
2450PerlIOStdio_mode(const char *mode, char *tmode)
2451{
2452 char *ret = tmode;
2453 while (*mode) {
2454 *tmode++ = *mode++;
2455 }
35990314 2456#ifdef PERLIO_USING_CRLF
6ce75a77
JH
2457 *tmode++ = 'b';
2458#endif
14a5cf38
JH
2459 *tmode = '\0';
2460 return ret;
2461}
2462
4b803d04 2463IV
2dc2558e 2464PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4b803d04 2465{
1fd8f4ce
NIS
2466 PerlIO *n;
2467 if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
2468 PerlIO_funcs *toptab = PerlIOBase(n)->tab;
2469 if (toptab == tab) {
2470 /* Top is already stdio - pop self (duplicate) and use original */
2471 PerlIO_pop(aTHX_ f);
2472 return 0;
2473 } else {
2474 int fd = PerlIO_fileno(n);
2475 char tmode[8];
2476 FILE *stdio;
81428673 2477 if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
1fd8f4ce
NIS
2478 mode = PerlIOStdio_mode(mode, tmode)))) {
2479 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2480 /* We never call down so do any pending stuff now */
2481 PerlIO_flush(PerlIONext(f));
81428673 2482 }
1fd8f4ce
NIS
2483 else {
2484 return -1;
2485 }
2486 }
14a5cf38 2487 }
2dc2558e 2488 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
4b803d04
NIS
2489}
2490
22569500 2491
9e353e3b 2492PerlIO *
4b069b44 2493PerlIO_importFILE(FILE *stdio, const char *mode)
9e353e3b 2494{
14a5cf38
JH
2495 dTHX;
2496 PerlIO *f = NULL;
2497 if (stdio) {
22569500 2498 PerlIOStdio *s;
4b069b44
NIS
2499 if (!mode || !*mode) {
2500 /* We need to probe to see how we can open the stream
2501 so start with read/write and then try write and read
2502 we dup() so that we can fclose without loosing the fd.
2503
2504 Note that the errno value set by a failing fdopen
2505 varies between stdio implementations.
2506 */
2507 int fd = PerlLIO_dup(fileno(stdio));
a33cf58c 2508 FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+"));
4b069b44 2509 if (!f2) {
a33cf58c 2510 f2 = PerlSIO_fdopen(fd, (mode = "w"));
4b069b44
NIS
2511 }
2512 if (!f2) {
a33cf58c 2513 f2 = PerlSIO_fdopen(fd, (mode = "r"));
4b069b44
NIS
2514 }
2515 if (!f2) {
2516 /* Don't seem to be able to open */
2517 PerlLIO_close(fd);
2518 return f;
2519 }
2520 fclose(f2);
22569500 2521 }
a33cf58c
NIS
2522 if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio, mode, Nullsv))) {
2523 s = PerlIOSelf(f, PerlIOStdio);
2524 s->stdio = stdio;
2525 }
14a5cf38
JH
2526 }
2527 return f;
9e353e3b
NIS
2528}
2529
2530PerlIO *
14a5cf38
JH
2531PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2532 IV n, const char *mode, int fd, int imode,
2533 int perm, PerlIO *f, int narg, SV **args)
2534{
2535 char tmode[8];
d9dac8cd 2536 if (PerlIOValid(f)) {
14a5cf38
JH
2537 char *path = SvPV_nolen(*args);
2538 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
1751d015
NIS
2539 FILE *stdio;
2540 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2541 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
14a5cf38
JH
2542 s->stdio);
2543 if (!s->stdio)
2544 return NULL;
2545 s->stdio = stdio;
1751d015 2546 PerlIOUnix_refcnt_inc(fileno(s->stdio));
14a5cf38
JH
2547 return f;
2548 }
2549 else {
2550 if (narg > 0) {
2551 char *path = SvPV_nolen(*args);
2552 if (*mode == '#') {
2553 mode++;
2554 fd = PerlLIO_open3(path, imode, perm);
2555 }
2556 else {
2557 FILE *stdio = PerlSIO_fopen(path, mode);
2558 if (stdio) {
d9dac8cd
NIS
2559 PerlIOStdio *s;
2560 if (!f) {
2561 f = PerlIO_allocate(aTHX);
2562 }
a33cf58c 2563 if ((f = PerlIO_push(aTHX_ f, self,
14a5cf38 2564 (mode = PerlIOStdio_mode(mode, tmode)),
a33cf58c
NIS
2565 PerlIOArg))) {
2566 s = PerlIOSelf(f, PerlIOStdio);
2567 s->stdio = stdio;
2568 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2569 }
602c3c4b
NIS
2570 return f;
2571 }
2572 else {
2573 return NULL;
14a5cf38 2574 }
14a5cf38
JH
2575 }
2576 }
2577 if (fd >= 0) {
2578 FILE *stdio = NULL;
2579 int init = 0;
2580 if (*mode == 'I') {
2581 init = 1;
2582 mode++;
2583 }
2584 if (init) {
2585 switch (fd) {
2586 case 0:
2587 stdio = PerlSIO_stdin;
2588 break;
2589 case 1:
2590 stdio = PerlSIO_stdout;
2591 break;
2592 case 2:
2593 stdio = PerlSIO_stderr;
2594 break;
2595 }
2596 }
2597 else {
2598 stdio = PerlSIO_fdopen(fd, mode =
2599 PerlIOStdio_mode(mode, tmode));
2600 }
2601 if (stdio) {
d9dac8cd
NIS
2602 PerlIOStdio *s;
2603 if (!f) {
2604 f = PerlIO_allocate(aTHX);
2605 }
a33cf58c
NIS
2606 if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2607 s = PerlIOSelf(f, PerlIOStdio);
2608 s->stdio = stdio;
2609 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2610 }
14a5cf38
JH
2611 return f;
2612 }
2613 }
2614 }
ee518936 2615 return NULL;
9e353e3b
NIS
2616}
2617
1751d015 2618PerlIO *
ecdeb87c 2619PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
1751d015
NIS
2620{
2621 /* This assumes no layers underneath - which is what
2622 happens, but is not how I remember it. NI-S 2001/10/16
2623 */
ecdeb87c 2624 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
694c95cf 2625 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
ecdeb87c
NIS
2626 if (flags & PERLIO_DUP_FD) {
2627 int fd = PerlLIO_dup(fileno(stdio));
2628 if (fd >= 0) {
2629 char mode[8];
a33cf58c 2630 stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
ecdeb87c
NIS
2631 }
2632 else {
2633 /* FIXME: To avoid messy error recovery if dup fails
2634 re-use the existing stdio as though flag was not set
2635 */
2636 }
2637 }
694c95cf
JH
2638 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2639 PerlIOUnix_refcnt_inc(fileno(stdio));
1751d015
NIS
2640 }
2641 return f;
2642}
2643
2644IV
f62ce20a 2645PerlIOStdio_close(pTHX_ PerlIO *f)
1751d015 2646{
1751d015
NIS
2647#ifdef SOCKS5_VERSION_NAME
2648 int optval;
2649 Sock_size_t optlen = sizeof(int);
2650#endif
2651 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
439ba545
NIS
2652 if (!stdio) {
2653 errno = EBADF;
2654 return -1;
2655 }
1751d015 2656 if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) {
694c95cf 2657 /* Do not close it but do flush any buffers */
22569500 2658 return PerlIO_flush(f);
1751d015
NIS
2659 }
2660 return (
2661#ifdef SOCKS5_VERSION_NAME
2662 (getsockopt
2663 (PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *) &optval,
2664 &optlen) <
2665 0) ? PerlSIO_fclose(stdio) : close(PerlIO_fileno(f))
2666#else
2667 PerlSIO_fclose(stdio)
2668#endif
2669 );
2670
2671}
2672
2673
2674
9e353e3b 2675SSize_t
f62ce20a 2676PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
9e353e3b 2677{
14a5cf38
JH
2678 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2679 SSize_t got = 0;
2680 if (count == 1) {
2681 STDCHAR *buf = (STDCHAR *) vbuf;
2682 /*
2683 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
71200d45 2684 * stdio does not do that for fread()
14a5cf38
JH
2685 */
2686 int ch = PerlSIO_fgetc(s);
2687 if (ch != EOF) {
2688 *buf = ch;
2689 got = 1;
2690 }
2691 }
2692 else
2693 got = PerlSIO_fread(vbuf, 1, count, s);
2694 return got;
9e353e3b
NIS
2695}
2696
2697SSize_t
f62ce20a 2698PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 2699{
14a5cf38 2700 SSize_t unread = 0;
93679785
NIS
2701 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2702
313e59c8 2703#ifdef STDIO_BUFFER_WRITABLE
9f7cd136 2704 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
93679785
NIS
2705 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
2706 STDCHAR *base = PerlIO_get_base(f);
2707 SSize_t cnt = PerlIO_get_cnt(f);
2708 STDCHAR *ptr = PerlIO_get_ptr(f);
2709 SSize_t avail = ptr - base;
2710 if (avail > 0) {
2711 if (avail > count) {
2712 avail = count;
2713 }
2714 ptr -= avail;
2715 Move(buf-avail,ptr,avail,STDCHAR);
2716 count -= avail;
2717 unread += avail;
2718 PerlIO_set_ptrcnt(f,ptr,cnt+avail);
9f7cd136
NIS
2719 if (PerlSIO_feof(s) && unread >= 0)
2720 PerlSIO_clearerr(s);
2721 }
2722 }
313e59c8
NIS
2723 else
2724#endif
2725 if (PerlIO_has_cntptr(f)) {
9f7cd136
NIS
2726 /* We can get pointer to buffer but not its base
2727 Do ungetc() but check chars are ending up in the
2728 buffer
2729 */
2730 STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
2731 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
2732 while (count > 0) {
2733 int ch = *--buf & 0xFF;
2734 if (ungetc(ch,s) != ch) {
2735 /* ungetc did not work */
2736 break;
2737 }
2738 if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) {
2739 /* Did not change pointer as expected */
2740 fgetc(s); /* get char back again */
2741 break;
2742 }
2743 /* It worked ! */
2744 count--;
2745 unread++;
93679785
NIS
2746 }
2747 }
2748
2749 if (count > 0) {
2750 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
14a5cf38
JH
2751 }
2752 return unread;
9e353e3b
NIS
2753}
2754
2755SSize_t
f62ce20a 2756PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 2757{
14a5cf38
JH
2758 return PerlSIO_fwrite(vbuf, 1, count,
2759 PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
2760}
2761
2762IV
f62ce20a 2763PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
9e353e3b 2764{
14a5cf38
JH
2765 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2766 return PerlSIO_fseek(stdio, offset, whence);
9e353e3b
NIS
2767}
2768
2769Off_t
f62ce20a 2770PerlIOStdio_tell(pTHX_ PerlIO *f)
9e353e3b 2771{
14a5cf38
JH
2772 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2773 return PerlSIO_ftell(stdio);
9e353e3b
NIS
2774}
2775
2776IV
f62ce20a 2777PerlIOStdio_flush(pTHX_ PerlIO *f)
9e353e3b 2778{
14a5cf38
JH
2779 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2780 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
2781 return PerlSIO_fflush(stdio);
2782 }
2783 else {
88b61e10 2784#if 0
14a5cf38
JH
2785 /*
2786 * FIXME: This discards ungetc() and pre-read stuff which is not
71200d45 2787 * right if this is just a "sync" from a layer above Suspect right
14a5cf38 2788 * design is to do _this_ but not have layer above flush this
71200d45 2789 * layer read-to-read
14a5cf38
JH
2790 */
2791 /*
71200d45 2792 * Not writeable - sync by attempting a seek
14a5cf38
JH
2793 */
2794 int err = errno;
2795 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
2796 errno = err;
88b61e10 2797#endif
14a5cf38
JH
2798 }
2799 return 0;
9e353e3b
NIS
2800}
2801
2802IV
f62ce20a 2803PerlIOStdio_eof(pTHX_ PerlIO *f)
9e353e3b 2804{
14a5cf38 2805 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
2806}
2807
2808IV
f62ce20a 2809PerlIOStdio_error(pTHX_ PerlIO *f)
9e353e3b 2810{
14a5cf38 2811 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
2812}
2813
2814void
f62ce20a 2815PerlIOStdio_clearerr(pTHX_ PerlIO *f)
9e353e3b 2816{
14a5cf38 2817 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
2818}
2819
2820void
f62ce20a 2821PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
9e353e3b
NIS
2822{
2823#ifdef HAS_SETLINEBUF
14a5cf38 2824 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b 2825#else
14a5cf38 2826 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
9e353e3b
NIS
2827#endif
2828}
2829
2830#ifdef FILE_base
2831STDCHAR *
f62ce20a 2832PerlIOStdio_get_base(pTHX_ PerlIO *f)
9e353e3b 2833{
14a5cf38 2834 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
cc00df79 2835 return (STDCHAR*)PerlSIO_get_base(stdio);
9e353e3b
NIS
2836}
2837
2838Size_t
f62ce20a 2839PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
9e353e3b 2840{
14a5cf38
JH
2841 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2842 return PerlSIO_get_bufsiz(stdio);
9e353e3b
NIS
2843}
2844#endif
2845
2846#ifdef USE_STDIO_PTR
2847STDCHAR *
f62ce20a 2848PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
9e353e3b 2849{
14a5cf38 2850 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
cc00df79 2851 return (STDCHAR*)PerlSIO_get_ptr(stdio);
9e353e3b
NIS
2852}
2853
2854SSize_t
f62ce20a 2855PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
9e353e3b 2856{
14a5cf38
JH
2857 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2858 return PerlSIO_get_cnt(stdio);
9e353e3b
NIS
2859}
2860
2861void
f62ce20a 2862PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
9e353e3b 2863{
14a5cf38 2864 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 2865 if (ptr != NULL) {
9e353e3b 2866#ifdef STDIO_PTR_LVALUE
22569500 2867 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
9e353e3b 2868#ifdef STDIO_PTR_LVAL_SETS_CNT
14a5cf38 2869 if (PerlSIO_get_cnt(stdio) != (cnt)) {
14a5cf38
JH
2870 assert(PerlSIO_get_cnt(stdio) == (cnt));
2871 }
9e353e3b
NIS
2872#endif
2873#if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
14a5cf38 2874 /*
71200d45 2875 * Setting ptr _does_ change cnt - we are done
14a5cf38
JH
2876 */
2877 return;
9e353e3b 2878#endif
22569500 2879#else /* STDIO_PTR_LVALUE */
14a5cf38 2880 PerlProc_abort();
22569500 2881#endif /* STDIO_PTR_LVALUE */
14a5cf38
JH
2882 }
2883 /*
71200d45 2884 * Now (or only) set cnt
14a5cf38 2885 */
9e353e3b 2886#ifdef STDIO_CNT_LVALUE
14a5cf38 2887 PerlSIO_set_cnt(stdio, cnt);
22569500 2888#else /* STDIO_CNT_LVALUE */
9e353e3b 2889#if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
14a5cf38
JH
2890 PerlSIO_set_ptr(stdio,
2891 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
2892 cnt));
22569500 2893#else /* STDIO_PTR_LVAL_SETS_CNT */
14a5cf38 2894 PerlProc_abort();
22569500
NIS
2895#endif /* STDIO_PTR_LVAL_SETS_CNT */
2896#endif /* STDIO_CNT_LVALUE */
9e353e3b
NIS
2897}
2898
93679785 2899
9e353e3b
NIS
2900#endif
2901
93679785
NIS
2902IV
2903PerlIOStdio_fill(pTHX_ PerlIO *f)
2904{
2905 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2906 int c;
2907 /*
2908 * fflush()ing read-only streams can cause trouble on some stdio-s
2909 */
2910 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
2911 if (PerlSIO_fflush(stdio) != 0)
2912 return EOF;
2913 }
2914 c = PerlSIO_fgetc(stdio);
2915 if (c == EOF)
2916 return EOF;
2917
2918#if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
313e59c8
NIS
2919
2920#ifdef STDIO_BUFFER_WRITABLE
9f7cd136 2921 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
93679785
NIS
2922 /* Fake ungetc() to the real buffer in case system's ungetc
2923 goes elsewhere
2924 */
2925 STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio);
2926 SSize_t cnt = PerlSIO_get_cnt(stdio);
2927 STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio);
2928 if (ptr == base+1) {
2929 *--ptr = (STDCHAR) c;
2930 PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1);
2931 if (PerlSIO_feof(stdio))
2932 PerlSIO_clearerr(stdio);
2933 return 0;
2934 }
2935 }
313e59c8
NIS
2936 else
2937#endif
2938 if (PerlIO_has_cntptr(f)) {
9f7cd136
NIS
2939 STDCHAR ch = c;
2940 if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) {
2941 return 0;
2942 }
2943 }
93679785
NIS
2944#endif
2945
2946#if defined(VMS)
2947 /* An ungetc()d char is handled separately from the regular
2948 * buffer, so we stuff it in the buffer ourselves.
2949 * Should never get called as should hit code above
2950 */
bad9695d
NIS
2951 *(--((*stdio)->_ptr)) = (unsigned char) c;
2952 (*stdio)->_cnt++;
93679785
NIS
2953#else
2954 /* If buffer snoop scheme above fails fall back to
9f7cd136 2955 using ungetc().
93679785
NIS
2956 */
2957 if (PerlSIO_ungetc(c, stdio) != c)
2958 return EOF;
2959#endif
2960 return 0;
2961}
2962
2963
2964
9e353e3b 2965PerlIO_funcs PerlIO_stdio = {
2dc2558e 2966 sizeof(PerlIO_funcs),
14a5cf38
JH
2967 "stdio",
2968 sizeof(PerlIOStdio),
86e05cf2 2969 PERLIO_K_BUFFERED|PERLIO_K_RAW,
1fd8f4ce 2970 PerlIOStdio_pushed,
44798d05 2971 PerlIOBase_popped,
14a5cf38 2972 PerlIOStdio_open,
86e05cf2 2973 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
2974 NULL,
2975 PerlIOStdio_fileno,
71200d45 2976 PerlIOStdio_dup,
14a5cf38
JH
2977 PerlIOStdio_read,
2978 PerlIOStdio_unread,
2979 PerlIOStdio_write,
2980 PerlIOStdio_seek,
2981 PerlIOStdio_tell,
2982 PerlIOStdio_close,
2983 PerlIOStdio_flush,
2984 PerlIOStdio_fill,
2985 PerlIOStdio_eof,
2986 PerlIOStdio_error,
2987 PerlIOStdio_clearerr,
2988 PerlIOStdio_setlinebuf,
9e353e3b 2989#ifdef FILE_base
14a5cf38
JH
2990 PerlIOStdio_get_base,
2991 PerlIOStdio_get_bufsiz,
9e353e3b 2992#else
14a5cf38
JH
2993 NULL,
2994 NULL,
9e353e3b
NIS
2995#endif
2996#ifdef USE_STDIO_PTR
14a5cf38
JH
2997 PerlIOStdio_get_ptr,
2998 PerlIOStdio_get_cnt,
0eb1d8a4 2999#if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
14a5cf38 3000 PerlIOStdio_set_ptrcnt
22569500 3001#else /* STDIO_PTR_LVALUE */
14a5cf38 3002 NULL
22569500
NIS
3003#endif /* STDIO_PTR_LVALUE */
3004#else /* USE_STDIO_PTR */
14a5cf38
JH
3005 NULL,
3006 NULL,
3007 NULL
22569500 3008#endif /* USE_STDIO_PTR */
9e353e3b
NIS
3009};
3010
9e353e3b 3011FILE *
81428673 3012PerlIO_exportFILE(PerlIO * f, const char *mode)
9e353e3b 3013{
e87a358a 3014 dTHX;
81428673
NIS
3015 FILE *stdio = NULL;
3016 if (PerlIOValid(f)) {
3017 char buf[8];
3018 PerlIO_flush(f);
3019 if (!mode || !*mode) {
3020 mode = PerlIO_modestr(f, buf);
3021 }
3022 stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
3023 if (stdio) {
3024 PerlIOl *l = *f;
3025 /* De-link any lower layers so new :stdio sticks */
3026 *f = NULL;
3027 if ((f = PerlIO_push(aTHX_ f, &PerlIO_stdio, buf, Nullsv))) {
3028 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
3029 s->stdio = stdio;
3030 /* Link previous lower layers under new one */
3031 *PerlIONext(f) = l;
3032 }
3033 else {
3034 /* restore layers list */
3035 *f = l;
3036 }
a33cf58c 3037 }
14a5cf38
JH
3038 }
3039 return stdio;
9e353e3b
NIS
3040}
3041
81428673 3042
9e353e3b
NIS
3043FILE *
3044PerlIO_findFILE(PerlIO *f)
3045{
14a5cf38
JH
3046 PerlIOl *l = *f;
3047 while (l) {
3048 if (l->tab == &PerlIO_stdio) {
3049 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3050 return s->stdio;
3051 }
3052 l = *PerlIONext(&l);
f7e7eb72 3053 }
4b069b44
NIS
3054 /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
3055 return PerlIO_exportFILE(f, Nullch);
9e353e3b
NIS
3056}
3057
9e353e3b
NIS
3058void
3059PerlIO_releaseFILE(PerlIO *p, FILE *f)
3060{
22569500
NIS
3061 PerlIOl *l;
3062 while ((l = *p)) {
3063 if (l->tab == &PerlIO_stdio) {
3064 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3065 if (s->stdio == f) {
3066 dTHX;
3067 PerlIO_pop(aTHX_ p);
3068 return;
3069 }
3070 }
3071 p = PerlIONext(p);
3072 }
3073 return;
9e353e3b
NIS
3074}
3075
3076/*--------------------------------------------------------------------------------------*/
14a5cf38 3077/*
71200d45 3078 * perlio buffer layer
14a5cf38 3079 */
9e353e3b 3080
5e2ab84b 3081IV
2dc2558e 3082PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
5e2ab84b 3083{
14a5cf38
JH
3084 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3085 int fd = PerlIO_fileno(f);
14a5cf38
JH
3086 if (fd >= 0 && PerlLIO_isatty(fd)) {
3087 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
3088 }
4b069b44
NIS
3089 if (*PerlIONext(f)) {
3090 Off_t posn = PerlIO_tell(PerlIONext(f));
3091 if (posn != (Off_t) - 1) {
3092 b->posn = posn;
3093 }
14a5cf38 3094 }
2dc2558e 3095 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
5e2ab84b
NIS
3096}
3097
9e353e3b 3098PerlIO *
14a5cf38
JH
3099PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3100 IV n, const char *mode, int fd, int imode, int perm,
3101 PerlIO *f, int narg, SV **args)
3102{
04892f78 3103 if (PerlIOValid(f)) {
14a5cf38 3104 PerlIO *next = PerlIONext(f);
04892f78
NIS
3105 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
3106 next = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
14a5cf38 3107 next, narg, args);
2dc2558e 3108 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
14a5cf38
JH
3109 return NULL;
3110 }
3111 }
3112 else {
04892f78 3113 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
14a5cf38
JH
3114 int init = 0;
3115 if (*mode == 'I') {
3116 init = 1;
3117 /*
71200d45 3118 * mode++;
14a5cf38
JH
3119 */
3120 }
3121 f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
d9dac8cd 3122 f, narg, args);
14a5cf38 3123 if (f) {
22569500 3124 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
b26b1ab5
NC
3125 /*
3126 * if push fails during open, open fails. close will pop us.
3127 */
3128 PerlIO_close (f);
3129 return NULL;
3130 } else {
3131 fd = PerlIO_fileno(f);
b26b1ab5
NC
3132 if (init && fd == 2) {
3133 /*
3134 * Initial stderr is unbuffered
3135 */
3136 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
3137 }
23b84778
IZ
3138#ifdef PERLIO_USING_CRLF
3139# ifdef PERLIO_IS_BINMODE_FD
3140 if (PERLIO_IS_BINMODE_FD(fd))
3141 PerlIO_binmode(f, '<'/*not used*/, O_BINARY, Nullch);
3142 else
3143# endif
3144 /*
3145 * do something about failing setmode()? --jhi
3146 */
3147 PerlLIO_setmode(fd, O_BINARY);
3148#endif
14a5cf38
JH
3149 }
3150 }
ee518936 3151 }
14a5cf38 3152 return f;
9e353e3b
NIS
3153}
3154
14a5cf38
JH
3155/*
3156 * This "flush" is akin to sfio's sync in that it handles files in either
71200d45 3157 * read or write state
14a5cf38 3158 */
9e353e3b 3159IV
f62ce20a 3160PerlIOBuf_flush(pTHX_ PerlIO *f)
6f9d8c32 3161{
14a5cf38
JH
3162 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3163 int code = 0;
04892f78 3164 PerlIO *n = PerlIONext(f);
14a5cf38
JH
3165 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
3166 /*
71200d45 3167 * write() the buffer
14a5cf38
JH
3168 */
3169 STDCHAR *buf = b->buf;
3170 STDCHAR *p = buf;
14a5cf38
JH
3171 while (p < b->ptr) {
3172 SSize_t count = PerlIO_write(n, p, b->ptr - p);
3173 if (count > 0) {
3174 p += count;
3175 }
3176 else if (count < 0 || PerlIO_error(n)) {
3177 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3178 code = -1;
3179 break;
3180 }
3181 }
3182 b->posn += (p - buf);
3183 }
3184 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3185 STDCHAR *buf = PerlIO_get_base(f);
3186 /*
71200d45 3187 * Note position change
14a5cf38
JH
3188 */
3189 b->posn += (b->ptr - buf);
3190 if (b->ptr < b->end) {
4b069b44
NIS
3191 /* We did not consume all of it - try and seek downstream to
3192 our logical position
14a5cf38 3193 */
4b069b44 3194 if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
04892f78
NIS
3195 /* Reload n as some layers may pop themselves on seek */
3196 b->posn = PerlIO_tell(n = PerlIONext(f));
14a5cf38 3197 }
ba5c3fe9 3198 else {
4b069b44
NIS
3199 /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read
3200 data is lost for good - so return saying "ok" having undone
3201 the position adjust
3202 */
3203 b->posn -= (b->ptr - buf);
ba5c3fe9
NIS
3204 return code;
3205 }
14a5cf38
JH
3206 }
3207 }
3208 b->ptr = b->end = b->buf;
3209 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
04892f78 3210 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
04892f78 3211 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
14a5cf38
JH
3212 code = -1;
3213 return code;
6f9d8c32
NIS
3214}
3215
06da4f11 3216IV
f62ce20a 3217PerlIOBuf_fill(pTHX_ PerlIO *f)
06da4f11 3218{
14a5cf38
JH
3219 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3220 PerlIO *n = PerlIONext(f);
3221 SSize_t avail;
3222 /*
4b069b44
NIS
3223 * Down-stream flush is defined not to loose read data so is harmless.
3224 * we would not normally be fill'ing if there was data left in anycase.
14a5cf38
JH
3225 */
3226 if (PerlIO_flush(f) != 0)
3227 return -1;
3228 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
f62ce20a 3229 PerlIOBase_flush_linebuf(aTHX);
14a5cf38
JH
3230
3231 if (!b->buf)
22569500 3232 PerlIO_get_base(f); /* allocate via vtable */
14a5cf38
JH
3233
3234 b->ptr = b->end = b->buf;
4b069b44
NIS
3235
3236 if (!PerlIOValid(n)) {
3237 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3238 return -1;
3239 }
3240
14a5cf38
JH
3241 if (PerlIO_fast_gets(n)) {
3242 /*
04892f78 3243 * Layer below is also buffered. We do _NOT_ want to call its
14a5cf38
JH
3244 * ->Read() because that will loop till it gets what we asked for
3245 * which may hang on a pipe etc. Instead take anything it has to
71200d45 3246 * hand, or ask it to fill _once_.
14a5cf38
JH
3247 */
3248 avail = PerlIO_get_cnt(n);
3249 if (avail <= 0) {
3250 avail = PerlIO_fill(n);
3251 if (avail == 0)
3252 avail = PerlIO_get_cnt(n);
3253 else {
3254 if (!PerlIO_error(n) && PerlIO_eof(n))
3255 avail = 0;
3256 }
3257 }
3258 if (avail > 0) {
3259 STDCHAR *ptr = PerlIO_get_ptr(n);
3260 SSize_t cnt = avail;
eb160463 3261 if (avail > (SSize_t)b->bufsiz)
14a5cf38
JH
3262 avail = b->bufsiz;
3263 Copy(ptr, b->buf, avail, STDCHAR);
3264 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3265 }
3266 }
3267 else {
3268 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3269 }
3270 if (avail <= 0) {
3271 if (avail == 0)
3272 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3273 else
3274 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3275 return -1;
3276 }
3277 b->end = b->buf + avail;
3278 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3279 return 0;
06da4f11
NIS
3280}
3281
6f9d8c32 3282SSize_t
f62ce20a 3283PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
6f9d8c32 3284{
14a5cf38 3285 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
04892f78 3286 if (PerlIOValid(f)) {
14a5cf38
JH
3287 if (!b->ptr)
3288 PerlIO_get_base(f);
f62ce20a 3289 return PerlIOBase_read(aTHX_ f, vbuf, count);
14a5cf38
JH
3290 }
3291 return 0;
6f9d8c32
NIS
3292}
3293
9e353e3b 3294SSize_t
f62ce20a 3295PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 3296{
14a5cf38
JH
3297 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3298 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3299 SSize_t unread = 0;
3300 SSize_t avail;
3301 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3302 PerlIO_flush(f);
3303 if (!b->buf)
3304 PerlIO_get_base(f);
3305 if (b->buf) {
3306 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3307 /*
3308 * Buffer is already a read buffer, we can overwrite any chars
71200d45 3309 * which have been read back to buffer start
14a5cf38
JH
3310 */
3311 avail = (b->ptr - b->buf);
3312 }
3313 else {
3314 /*
3315 * Buffer is idle, set it up so whole buffer is available for
71200d45 3316 * unread
14a5cf38
JH
3317 */
3318 avail = b->bufsiz;
3319 b->end = b->buf + avail;
3320 b->ptr = b->end;
3321 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3322 /*
71200d45 3323 * Buffer extends _back_ from where we are now
14a5cf38
JH
3324 */
3325 b->posn -= b->bufsiz;
3326 }
3327 if (avail > (SSize_t) count) {
3328 /*
71200d45 3329 * If we have space for more than count, just move count
14a5cf38
JH
3330 */
3331 avail = count;
3332 }
3333 if (avail > 0) {
3334 b->ptr -= avail;
3335 buf -= avail;
3336 /*
3337 * In simple stdio-like ungetc() case chars will be already
71200d45 3338 * there
14a5cf38
JH
3339 */
3340 if (buf != b->ptr) {
3341 Copy(buf, b->ptr, avail, STDCHAR);
3342 }
3343 count -= avail;
3344 unread += avail;
3345 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3346 }
3347 }
93679785
NIS
3348 if (count > 0) {
3349 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3350 }
14a5cf38 3351 return unread;
760ac839
LW
3352}
3353
9e353e3b 3354SSize_t
f62ce20a 3355PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 3356{
14a5cf38
JH
3357 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3358 const STDCHAR *buf = (const STDCHAR *) vbuf;
3359 Size_t written = 0;
3360 if (!b->buf)
3361 PerlIO_get_base(f);
3362 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3363 return 0;
3364 while (count > 0) {
3365 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3366 if ((SSize_t) count < avail)
3367 avail = count;
3368 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3369 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3370 while (avail > 0) {
3371 int ch = *buf++;
3372 *(b->ptr)++ = ch;
3373 count--;
3374 avail--;
3375 written++;
3376 if (ch == '\n') {
3377 PerlIO_flush(f);
3378 break;
3379 }
3380 }
3381 }
3382 else {
3383 if (avail) {
3384 Copy(buf, b->ptr, avail, STDCHAR);
3385 count -= avail;
3386 buf += avail;
3387 written += avail;
3388 b->ptr += avail;
3389 }
3390 }
3391 if (b->ptr >= (b->buf + b->bufsiz))
3392 PerlIO_flush(f);
3393 }
3394 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3395 PerlIO_flush(f);
3396 return written;
9e353e3b
NIS
3397}
3398
3399IV
f62ce20a 3400PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
9e353e3b 3401{
14a5cf38
JH
3402 IV code;
3403 if ((code = PerlIO_flush(f)) == 0) {
3404 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3405 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3406 code = PerlIO_seek(PerlIONext(f), offset, whence);
3407 if (code == 0) {
3408 b->posn = PerlIO_tell(PerlIONext(f));
3409 }
9e353e3b 3410 }
14a5cf38 3411 return code;
9e353e3b
NIS
3412}
3413
3414Off_t
f62ce20a 3415PerlIOBuf_tell(pTHX_ PerlIO *f)
9e353e3b 3416{
14a5cf38
JH
3417 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3418 /*
71200d45 3419 * b->posn is file position where b->buf was read, or will be written
14a5cf38
JH
3420 */
3421 Off_t posn = b->posn;
3422 if (b->buf) {
3423 /*
71200d45 3424 * If buffer is valid adjust position by amount in buffer
14a5cf38
JH
3425 */
3426 posn += (b->ptr - b->buf);
3427 }
3428 return posn;
9e353e3b
NIS
3429}
3430
3431IV
44798d05
NIS
3432PerlIOBuf_popped(pTHX_ PerlIO *f)
3433{
3434 IV code = PerlIOBase_popped(aTHX_ f);
3435 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3436 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3437 Safefree(b->buf);
3438 }
3439 b->buf = NULL;
3440 b->ptr = b->end = b->buf;
3441 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3442 return code;
3443}
3444
3445IV
f62ce20a 3446PerlIOBuf_close(pTHX_ PerlIO *f)
9e353e3b 3447{
f62ce20a 3448 IV code = PerlIOBase_close(aTHX_ f);
14a5cf38
JH
3449 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3450 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3a1ee7e8 3451 Safefree(b->buf);
14a5cf38
JH
3452 }
3453 b->buf = NULL;
3454 b->ptr = b->end = b->buf;
3455 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3456 return code;
760ac839
LW
3457}
3458
9e353e3b 3459STDCHAR *
f62ce20a 3460PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
9e353e3b 3461{
14a5cf38
JH
3462 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3463 if (!b->buf)
3464 PerlIO_get_base(f);
3465 return b->ptr;
9e353e3b
NIS
3466}
3467
05d1247b 3468SSize_t
f62ce20a 3469PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
9e353e3b 3470{
14a5cf38
JH
3471 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3472 if (!b->buf)
3473 PerlIO_get_base(f);
3474 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3475 return (b->end - b->ptr);
3476 return 0;
9e353e3b
NIS
3477}
3478
3479STDCHAR *
f62ce20a 3480PerlIOBuf_get_base(pTHX_ PerlIO *f)
9e353e3b 3481{
14a5cf38
JH
3482 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3483 if (!b->buf) {
3484 if (!b->bufsiz)
3485 b->bufsiz = 4096;
a1ea730d 3486 b->buf =
7fcdafbd 3487 Newz('B',b->buf,b->bufsiz, STDCHAR);
14a5cf38
JH
3488 if (!b->buf) {
3489 b->buf = (STDCHAR *) & b->oneword;
3490 b->bufsiz = sizeof(b->oneword);
3491 }
3492 b->ptr = b->buf;
3493 b->end = b->ptr;
06da4f11 3494 }
14a5cf38 3495 return b->buf;
9e353e3b
NIS
3496}
3497
3498Size_t
f62ce20a 3499PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
9e353e3b 3500{
14a5cf38
JH
3501 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3502 if (!b->buf)
3503 PerlIO_get_base(f);
3504 return (b->end - b->buf);
9e353e3b
NIS
3505}
3506
3507void
f62ce20a 3508PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
9e353e3b 3509{
14a5cf38
JH
3510 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3511 if (!b->buf)
3512 PerlIO_get_base(f);
3513 b->ptr = ptr;
3514 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
14a5cf38
JH
3515 assert(PerlIO_get_cnt(f) == cnt);
3516 assert(b->ptr >= b->buf);
3517 }
3518 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
760ac839
LW
3519}
3520
71200d45 3521PerlIO *
ecdeb87c 3522PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
71200d45 3523{
ecdeb87c 3524 return PerlIOBase_dup(aTHX_ f, o, param, flags);
71200d45
NIS
3525}
3526
3527
3528
9e353e3b 3529PerlIO_funcs PerlIO_perlio = {
2dc2558e 3530 sizeof(PerlIO_funcs),
14a5cf38
JH
3531 "perlio",
3532 sizeof(PerlIOBuf),
86e05cf2 3533 PERLIO_K_BUFFERED|PERLIO_K_RAW,
14a5cf38 3534 PerlIOBuf_pushed,
44798d05 3535 PerlIOBuf_popped,
14a5cf38 3536 PerlIOBuf_open,
86e05cf2 3537 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
3538 NULL,
3539 PerlIOBase_fileno,
71200d45 3540 PerlIOBuf_dup,
14a5cf38
JH
3541 PerlIOBuf_read,
3542 PerlIOBuf_unread,
3543 PerlIOBuf_write,
3544 PerlIOBuf_seek,
3545 PerlIOBuf_tell,
3546 PerlIOBuf_close,
3547 PerlIOBuf_flush,
3548 PerlIOBuf_fill,
3549 PerlIOBase_eof,
3550 PerlIOBase_error,
3551 PerlIOBase_clearerr,
3552 PerlIOBase_setlinebuf,
3553 PerlIOBuf_get_base,
3554 PerlIOBuf_bufsiz,
3555 PerlIOBuf_get_ptr,
3556 PerlIOBuf_get_cnt,
3557 PerlIOBuf_set_ptrcnt,
9e353e3b
NIS
3558};
3559
66ecd56b 3560/*--------------------------------------------------------------------------------------*/
14a5cf38 3561/*
71200d45 3562 * Temp layer to hold unread chars when cannot do it any other way
14a5cf38 3563 */
5e2ab84b
NIS
3564
3565IV
f62ce20a 3566PerlIOPending_fill(pTHX_ PerlIO *f)
5e2ab84b 3567{
14a5cf38 3568 /*
71200d45 3569 * Should never happen
14a5cf38
JH
3570 */
3571 PerlIO_flush(f);
3572 return 0;
5e2ab84b
NIS
3573}
3574
3575IV
f62ce20a 3576PerlIOPending_close(pTHX_ PerlIO *f)
5e2ab84b 3577{
14a5cf38 3578 /*
71200d45 3579 * A tad tricky - flush pops us, then we close new top
14a5cf38
JH
3580 */
3581 PerlIO_flush(f);
3582 return PerlIO_close(f);
5e2ab84b
NIS
3583}
3584
3585IV
f62ce20a 3586PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
5e2ab84b 3587{
14a5cf38 3588 /*
71200d45 3589 * A tad tricky - flush pops us, then we seek new top
14a5cf38
JH
3590 */
3591 PerlIO_flush(f);
3592 return PerlIO_seek(f, offset, whence);
5e2ab84b
NIS
3593}
3594
3595
3596IV
f62ce20a 3597PerlIOPending_flush(pTHX_ PerlIO *f)
5e2ab84b 3598{
14a5cf38
JH
3599 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3600 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3a1ee7e8 3601 Safefree(b->buf);
14a5cf38
JH
3602 b->buf = NULL;
3603 }
3604 PerlIO_pop(aTHX_ f);
3605 return 0;
5e2ab84b
NIS
3606}
3607
3608void
f62ce20a 3609PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
5e2ab84b 3610{
14a5cf38
JH
3611 if (cnt <= 0) {
3612 PerlIO_flush(f);
3613 }
3614 else {
f62ce20a 3615 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
14a5cf38 3616 }
5e2ab84b
NIS
3617}
3618
3619IV
2dc2558e 3620PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
5e2ab84b 3621{
2dc2558e 3622 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
14a5cf38
JH
3623 PerlIOl *l = PerlIOBase(f);
3624 /*
71200d45
NIS
3625 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
3626 * etc. get muddled when it changes mid-string when we auto-pop.
14a5cf38
JH
3627 */
3628 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
3629 (PerlIOBase(PerlIONext(f))->
3630 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
3631 return code;
5e2ab84b
NIS
3632}
3633
3634SSize_t
f62ce20a 3635PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
5e2ab84b 3636{
14a5cf38
JH
3637 SSize_t avail = PerlIO_get_cnt(f);
3638 SSize_t got = 0;
eb160463 3639 if ((SSize_t)count < avail)
14a5cf38
JH
3640 avail = count;
3641 if (avail > 0)
f62ce20a 3642 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
eb160463 3643 if (got >= 0 && got < (SSize_t)count) {
14a5cf38
JH
3644 SSize_t more =
3645 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
3646 if (more >= 0 || got == 0)
3647 got += more;
3648 }
3649 return got;
5e2ab84b
NIS
3650}
3651
5e2ab84b 3652PerlIO_funcs PerlIO_pending = {
2dc2558e 3653 sizeof(PerlIO_funcs),
14a5cf38
JH
3654 "pending",
3655 sizeof(PerlIOBuf),
86e05cf2 3656 PERLIO_K_BUFFERED|PERLIO_K_RAW, /* not sure about RAW here */
14a5cf38 3657 PerlIOPending_pushed,
44798d05 3658 PerlIOBuf_popped,
14a5cf38 3659 NULL,
86e05cf2 3660 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
3661 NULL,
3662 PerlIOBase_fileno,
71200d45 3663 PerlIOBuf_dup,
14a5cf38
JH
3664 PerlIOPending_read,
3665 PerlIOBuf_unread,
3666 PerlIOBuf_write,
3667 PerlIOPending_seek,
3668 PerlIOBuf_tell,
3669 PerlIOPending_close,
3670 PerlIOPending_flush,
3671 PerlIOPending_fill,
3672 PerlIOBase_eof,
3673 PerlIOBase_error,
3674 PerlIOBase_clearerr,
3675 PerlIOBase_setlinebuf,
3676 PerlIOBuf_get_base,
3677 PerlIOBuf_bufsiz,
3678 PerlIOBuf_get_ptr,
3679 PerlIOBuf_get_cnt,
3680 PerlIOPending_set_ptrcnt,
5e2ab84b
NIS
3681};
3682
3683
3684
3685/*--------------------------------------------------------------------------------------*/
14a5cf38
JH
3686/*
3687 * crlf - translation On read translate CR,LF to "\n" we do this by
3688 * overriding ptr/cnt entries to hand back a line at a time and keeping a
71200d45 3689 * record of which nl we "lied" about. On write translate "\n" to CR,LF
66ecd56b
NIS
3690 */
3691
14a5cf38 3692typedef struct {
22569500
NIS
3693 PerlIOBuf base; /* PerlIOBuf stuff */
3694 STDCHAR *nl; /* Position of crlf we "lied" about in the
14a5cf38 3695 * buffer */
99efab12
NIS
3696} PerlIOCrlf;
3697
f5b9d040 3698IV
2dc2558e 3699PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
f5b9d040 3700{
14a5cf38
JH
3701 IV code;
3702 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
2dc2558e 3703 code = PerlIOBuf_pushed(aTHX_ f, mode, arg, tab);
5e2ab84b 3704#if 0
14a5cf38
JH
3705 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
3706 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
3707 PerlIOBase(f)->flags);
5e2ab84b 3708#endif
14a5cf38 3709 return code;
f5b9d040
NIS
3710}
3711
3712
99efab12 3713SSize_t
f62ce20a 3714PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
99efab12 3715{
14a5cf38
JH
3716 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3717 if (c->nl) {
3718 *(c->nl) = 0xd;
3719 c->nl = NULL;
3720 }
3721 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
f62ce20a 3722 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
14a5cf38
JH
3723 else {
3724 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3725 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3726 SSize_t unread = 0;