X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=perlio.c;h=304107bcd3f728291020d39b13ec90366a8161dd;hb=65575be5af9681bf691b8b72b0e5b7f432a867bf;hp=d4c2e11dda702d4625fb2520b7bb3be9c0fea925;hpb=f62ce20a4126b1e303e2d4d0a5c1e049ef2cb0c2;p=p5sagit%2Fp5-mst-13.2.git diff --git a/perlio.c b/perlio.c index d4c2e11..304107b 100644 --- a/perlio.c +++ b/perlio.c @@ -1,10 +1,15 @@ /* - * perlio.c Copyright (c) 1996-2001, Nick Ing-Simmons You may distribute + * perlio.c Copyright (c) 1996-2002, Nick Ing-Simmons You may distribute * under the terms of either the GNU General Public License or the * Artistic License, as specified in the README file. */ /* + * Hour after hour for nearly three weary days he had jogged up and down, + * over passes, and through long dales, and across many streams. + */ + +/* * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get * at the dispatch tables, even when we do not need it for other reasons. * Invent a dSYS macro to abstract this out @@ -89,6 +94,7 @@ perlsio_binmode(FILE *fp, int iotype, int mode) # endif #else # if defined(USEMYBINMODE) + dTHX; if (my_binmode(fp, iotype, mode) != FALSE) return 1; else @@ -100,7 +106,7 @@ perlsio_binmode(FILE *fp, int iotype, int mode) } #ifndef O_ACCMODE -#define O_ACCMODE 3 /* Assume traditional implementation */ +#define O_ACCMODE 3 /* Assume traditional implementation */ #endif int @@ -152,7 +158,11 @@ PerlIO_intmode2str(int rawmode, char *mode, int *writing) int PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names) { - if (!names || !*names || strEQ(names, ":crlf") || strEQ(names, ":raw")) { + if (!names || !*names + || strEQ(names, ":crlf") + || strEQ(names, ":raw") + || strEQ(names, ":bytes") + ) { return 0; } Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names); @@ -180,12 +190,23 @@ PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names) PerlIO * PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) { -#ifndef PERL_MICRO +#ifdef PERL_MICRO + return NULL; +#else +#ifdef PERL_IMPLICIT_SYS + return PerlSIO_fdupopen(f); +#else +#ifdef WIN32 + return win32_fdupopen(f); +#else if (f) { int fd = PerlLIO_dup(PerlIO_fileno(f)); if (fd >= 0) { char mode[8]; int omode = fcntl(fd, F_GETFL); +#ifdef DJGPP + omode = djgpp_get_stream_mode(f); +#endif PerlIO_intmode2str(omode,mode,NULL); /* the r+ is a hack */ return PerlIO_fdopen(fd, mode); @@ -197,6 +218,8 @@ PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) } #endif return NULL; +#endif +#endif } @@ -278,7 +301,7 @@ PerlIO_tmpfile(void) return tmpfile(); } -#else /* PERLIO_IS_STDIO */ +#else /* PERLIO_IS_STDIO */ #ifdef USE_SFIO @@ -336,7 +359,7 @@ PerlIO_findFILE(PerlIO *pio) } -#else /* USE_SFIO */ +#else /* USE_SFIO */ /*======================================================================================*/ /* * Implement all the PerlIO interface ourselves. @@ -384,7 +407,7 @@ PerlIO_debug(const char *fmt, ...) if (!s) s = "(none)"; sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop)); - len = strlen(buffer); + len = strlen(buffer); vsprintf(buffer+len, fmt, ap); PerlLIO_write(dbg, buffer, strlen(buffer)); #else @@ -447,11 +470,11 @@ PerlIO_allocate(pTHX) PerlIO * PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) { - if (f && *f) { + if (PerlIOValid(f)) { PerlIO_funcs *tab = PerlIOBase(f)->tab; PerlIO *new; PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param); - new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags); + new = (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX),f,param, flags); return new; } else { @@ -592,10 +615,6 @@ PerlIO_destruct(pTHX) f++; } } - PerlIO_list_free(aTHX_ PL_known_layers); - PL_known_layers = NULL; - PerlIO_list_free(aTHX_ PL_def_layerlist); - PL_def_layerlist = NULL; } void @@ -695,7 +714,7 @@ perlio_mg_free(pTHX_ SV *sv, MAGIC *mg) MGVTBL perlio_vtab = { perlio_mg_get, perlio_mg_set, - NULL, /* len */ + NULL, /* len */ perlio_mg_clear, perlio_mg_free }; @@ -730,7 +749,7 @@ XS(XS_io_MODIFY_SCALAR_ATTRIBUTES) XSRETURN(count); } -#endif /* USE_ATTIBUTES_FOR_PERLIO */ +#endif /* USE_ATTIBUTES_FOR_PERLIO */ SV * PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab) @@ -786,9 +805,11 @@ PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names) * seen as an invalid separator character. */ char q = ((*s == '\'') ? '"' : '\''); - Perl_warn(aTHX_ + if (ckWARN(WARN_LAYER)) + Perl_warner(aTHX_ packWARN(WARN_LAYER), "perlio: invalid separator character %c%c%c in layer specification list %s", q, *s, q, s); + SETERRNO(EINVAL, LIB$_INVARG); return -1; } do { @@ -821,7 +842,8 @@ PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names) */ case '\0': e--; - Perl_warn(aTHX_ + if (ckWARN(WARN_LAYER)) + Perl_warner(aTHX_ packWARN(WARN_LAYER), "perlio: argument list not closed for layer \"%.*s\"", (int) (e - s), s); return -1; @@ -834,6 +856,7 @@ PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names) } } if (e > s) { + bool warn_layer = ckWARN(WARN_LAYER); PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ s, llen, 1); if (layer) { @@ -843,7 +866,8 @@ PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names) &PL_sv_undef); } else { - Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"", + if (warn_layer) + Perl_warner(aTHX_ packWARN(WARN_LAYER), "perlio: unknown layer \"%.*s\"", (int) llen, s); return -1; } @@ -863,7 +887,7 @@ PerlIO_default_buffer(pTHX_ PerlIO_list_t *av) tab = &PerlIO_crlf; #else if (PerlIO_stdio.Set_ptrcnt) - tab = &PerlIO_stdio; + tab = &PerlIO_stdio; #endif PerlIO_debug("Pushing %s\n", tab->name); PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0), @@ -1003,7 +1027,7 @@ PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg) /* * Pop back to bottom layer */ - if (f && *f) { + if (PerlIOValid(f)) { PerlIO_flush(f); while (!(PerlIOBase(f)->tab->kind & PERLIO_K_RAW)) { if (*PerlIONext(f)) { @@ -1027,9 +1051,8 @@ PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg) int PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode, - PerlIO_list_t *layers, IV n) + PerlIO_list_t *layers, IV n, IV max) { - IV max = layers->cur; int code = 0; while (n < max) { PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL); @@ -1052,7 +1075,7 @@ PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names) PerlIO_list_t *layers = PerlIO_list_alloc(aTHX); code = PerlIO_parse_layers(aTHX_ layers, names); if (code == 0) { - code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0); + code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur); } PerlIO_list_free(aTHX_ layers); } @@ -1071,32 +1094,85 @@ PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names) PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n", (void*)f, PerlIOBase(f)->tab->name, iotype, mode, (names) ? names : "(Null)"); - /* Can't flush if switching encodings. */ - if (!(names && memEQ(names, ":encoding(", 10))) { - PerlIO_flush(f); + if (names) { + /* Do not flush etc. if (e.g.) switching encodings. + if a pushed layer knows it needs to flush lower layers + (for example :unix which is never going to call them) + it can do the flush when it is pushed. + */ + return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE; + } + else { + if (*f) { + /* Turn off UTF-8-ness, to undo UTF-8 locale effects + This may be too simplistic! + */ + PerlIOBase(f)->flags &= ~PERLIO_F_UTF8; + } + /* FIXME?: Looking down the layer stack seems wrong, + but is a way of reaching past (say) an encoding layer + to flip CRLF-ness of the layer(s) below + */ #ifdef PERLIO_USING_CRLF - if (!names && (mode & O_BINARY)) { - PerlIO *top = f; - while (*top) { - if (PerlIOBase(top)->tab == &PerlIO_crlf) { - PerlIOBase(top)->flags &= ~PERLIO_F_CRLF; - break; + /* Legacy binmode only has meaning if O_TEXT has a value distinct from + O_BINARY so we can look for it in mode. + */ + if (!(mode & O_BINARY)) { + /* Text mode */ + while (*f) { + /* Perhaps we should turn on bottom-most aware layer + e.g. Ilya's idea that UNIX TTY could serve + */ + if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) { + if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) { + /* Not in text mode - flush any pending stuff and flip it */ + PerlIO_flush(f); + PerlIOBase(f)->flags |= PERLIO_F_CRLF; + } + /* Only need to turn it on in one layer so we are done */ + return TRUE; } - top = PerlIONext(top); - PerlIO_flush(top); + f = PerlIONext(f); } + /* Not finding a CRLF aware layer presumably means we are binary + which is not what was requested - so we failed + We _could_ push :crlf layer but so could caller + */ + return FALSE; } #endif + /* Either asked for BINMODE or that is normal on this platform + see if any CRLF aware layers are present and turn off the flag + and possibly remove layer. + */ + while (*f) { + if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) { + if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) { + /* In text mode - flush any pending stuff and flip it */ + PerlIO_flush(f); + PerlIOBase(f)->flags &= ~PERLIO_F_CRLF; +#ifndef PERLIO_USING_CRLF + /* CRLF is unusual case - if this is just the :crlf layer pop it */ + if (PerlIOBase(f)->tab == &PerlIO_crlf) { + PerlIO_pop(aTHX_ f); + } +#endif + /* Normal case is only one layer doing this, so exit on first + abnormal case can always do multiple binmode calls + */ + return TRUE; + } + } + f = PerlIONext(f); + } + return TRUE; } - return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE; } -#undef PerlIO__close int -PerlIO__close(PerlIO *f) +PerlIO__close(pTHX_ PerlIO *f) { - dTHX; - if (f && *f) + if (PerlIOValid(f)) return (*PerlIOBase(f)->tab->Close) (aTHX_ f); else { SETERRNO(EBADF, SS$_IVCHAN); @@ -1104,13 +1180,11 @@ PerlIO__close(PerlIO *f) } } -#undef PerlIO_close int -PerlIO_close(PerlIO *f) +Perl_PerlIO_close(pTHX_ PerlIO *f) { - dTHX; int code = -1; - if (f && *f) { + if (PerlIOValid(f)) { code = (*PerlIOBase(f)->tab->Close) (aTHX_ f); while (*f) { PerlIO_pop(aTHX_ f); @@ -1119,12 +1193,10 @@ PerlIO_close(PerlIO *f) return code; } -#undef PerlIO_fileno int -PerlIO_fileno(PerlIO *f) +Perl_PerlIO_fileno(pTHX_ PerlIO *f) { - dTHX; - if (f && *f) + if (PerlIOValid(f)) return (*PerlIOBase(f)->tab->Fileno) (aTHX_ f); else { SETERRNO(EBADF, SS$_IVCHAN); @@ -1149,7 +1221,7 @@ PerlIO_context_layers(pTHX_ const char *mode) * Skip to write part */ const char *s = strchr(type, 0); - if (s && (s - type) < len) { + if (s && (STRLEN)(s - type) < len) { type = s + 1; } } @@ -1226,8 +1298,13 @@ PerlIO_resolve_layers(pTHX_ const char *layers, else { av = def; } - PerlIO_parse_layers(aTHX_ av, layers); - return av; + if (PerlIO_parse_layers(aTHX_ av, layers) == 0) { + return av; + } + else { + PerlIO_list_free(aTHX_ av); + return (PerlIO_list_t *) NULL; + } } else { if (incdef) @@ -1252,7 +1329,7 @@ PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd, PerlIO_list_t *layera = NULL; IV n; PerlIO_funcs *tab = NULL; - if (f && *f) { + if (PerlIOValid(f)) { /* * This is "reopen" - it is not tested as perl does not use it * yet @@ -1260,15 +1337,18 @@ PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd, PerlIOl *l = *f; layera = PerlIO_list_alloc(aTHX); while (l) { - SV *arg = - (l->tab->Getarg) ? (*l->tab-> - Getarg) (aTHX_ &l, NULL, 0) : &PL_sv_undef; + SV *arg = (l->tab->Getarg) + ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0) + : &PL_sv_undef; PerlIO_list_push(aTHX_ layera, l->tab, arg); l = *PerlIONext(&l); } } else { layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args); + if (!layera) { + return NULL; + } } /* * Start at "top" of layer stack @@ -1300,8 +1380,9 @@ PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd, * More layers above the one that we used to open - * apply them now */ - if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1) - != 0) { + if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) { + /* If pushing layers fails close the file */ + PerlIO_close(f); f = NULL; } } @@ -1313,38 +1394,10 @@ PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd, } -#undef PerlIO_fdopen -PerlIO * -PerlIO_fdopen(int fd, const char *mode) -{ - dTHX; - return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL); -} - -#undef PerlIO_open -PerlIO * -PerlIO_open(const char *path, const char *mode) -{ - dTHX; - SV *name = sv_2mortal(newSVpvn(path, strlen(path))); - return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name); -} - -#undef PerlIO_reopen -PerlIO * -PerlIO_reopen(const char *path, const char *mode, PerlIO *f) -{ - dTHX; - SV *name = sv_2mortal(newSVpvn(path, strlen(path))); - return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name); -} - -#undef PerlIO_read SSize_t -PerlIO_read(PerlIO *f, void *vbuf, Size_t count) +Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) { - dTHX; - if (f && *f) + if (PerlIOValid(f)) return (*PerlIOBase(f)->tab->Read) (aTHX_ f, vbuf, count); else { SETERRNO(EBADF, SS$_IVCHAN); @@ -1352,12 +1405,10 @@ PerlIO_read(PerlIO *f, void *vbuf, Size_t count) } } -#undef PerlIO_unread SSize_t -PerlIO_unread(PerlIO *f, const void *vbuf, Size_t count) +Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count) { - dTHX; - if (f && *f) + if (PerlIOValid(f)) return (*PerlIOBase(f)->tab->Unread) (aTHX_ f, vbuf, count); else { SETERRNO(EBADF, SS$_IVCHAN); @@ -1365,12 +1416,10 @@ PerlIO_unread(PerlIO *f, const void *vbuf, Size_t count) } } -#undef PerlIO_write SSize_t -PerlIO_write(PerlIO *f, const void *vbuf, Size_t count) +Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count) { - dTHX; - if (f && *f) + if (PerlIOValid(f)) return (*PerlIOBase(f)->tab->Write) (aTHX_ f, vbuf, count); else { SETERRNO(EBADF, SS$_IVCHAN); @@ -1378,12 +1427,10 @@ PerlIO_write(PerlIO *f, const void *vbuf, Size_t count) } } -#undef PerlIO_seek int -PerlIO_seek(PerlIO *f, Off_t offset, int whence) +Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence) { - dTHX; - if (f && *f) + if (PerlIOValid(f)) return (*PerlIOBase(f)->tab->Seek) (aTHX_ f, offset, whence); else { SETERRNO(EBADF, SS$_IVCHAN); @@ -1391,12 +1438,10 @@ PerlIO_seek(PerlIO *f, Off_t offset, int whence) } } -#undef PerlIO_tell Off_t -PerlIO_tell(PerlIO *f) +Perl_PerlIO_tell(pTHX_ PerlIO *f) { - dTHX; - if (f && *f) + if (PerlIOValid(f)) return (*PerlIOBase(f)->tab->Tell) (aTHX_ f); else { SETERRNO(EBADF, SS$_IVCHAN); @@ -1404,11 +1449,9 @@ PerlIO_tell(PerlIO *f) } } -#undef PerlIO_flush int -PerlIO_flush(PerlIO *f) +Perl_PerlIO_flush(pTHX_ PerlIO *f) { - dTHX; if (f) { if (*f) { PerlIO_funcs *tab = PerlIOBase(f)->tab; @@ -1469,12 +1512,10 @@ PerlIOBase_flush_linebuf(pTHX) } } -#undef PerlIO_fill int -PerlIO_fill(PerlIO *f) +Perl_PerlIO_fill(pTHX_ PerlIO *f) { - dTHX; - if (f && *f) + if (PerlIOValid(f)) return (*PerlIOBase(f)->tab->Fill) (aTHX_ f); else { SETERRNO(EBADF, SS$_IVCHAN); @@ -1482,11 +1523,10 @@ PerlIO_fill(PerlIO *f) } } -#undef PerlIO_isutf8 int PerlIO_isutf8(PerlIO *f) { - if (f && *f) + if (PerlIOValid(f)) return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0; else { SETERRNO(EBADF, SS$_IVCHAN); @@ -1494,12 +1534,10 @@ PerlIO_isutf8(PerlIO *f) } } -#undef PerlIO_eof int -PerlIO_eof(PerlIO *f) +Perl_PerlIO_eof(pTHX_ PerlIO *f) { - dTHX; - if (f && *f) + if (PerlIOValid(f)) return (*PerlIOBase(f)->tab->Eof) (aTHX_ f); else { SETERRNO(EBADF, SS$_IVCHAN); @@ -1507,12 +1545,10 @@ PerlIO_eof(PerlIO *f) } } -#undef PerlIO_error int -PerlIO_error(PerlIO *f) +Perl_PerlIO_error(pTHX_ PerlIO *f) { - dTHX; - if (f && *f) + if (PerlIOValid(f)) return (*PerlIOBase(f)->tab->Error) (aTHX_ f); else { SETERRNO(EBADF, SS$_IVCHAN); @@ -1520,131 +1556,121 @@ PerlIO_error(PerlIO *f) } } -#undef PerlIO_clearerr void -PerlIO_clearerr(PerlIO *f) +Perl_PerlIO_clearerr(pTHX_ PerlIO *f) { - dTHX; - if (f && *f) + if (PerlIOValid(f)) (*PerlIOBase(f)->tab->Clearerr) (aTHX_ f); else SETERRNO(EBADF, SS$_IVCHAN); } -#undef PerlIO_setlinebuf void -PerlIO_setlinebuf(PerlIO *f) +Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f) { - dTHX; - if (f && *f) + if (PerlIOValid(f)) (*PerlIOBase(f)->tab->Setlinebuf) (aTHX_ f); else SETERRNO(EBADF, SS$_IVCHAN); } -#undef PerlIO_has_base int PerlIO_has_base(PerlIO *f) { - if (f && *f) { + if (PerlIOValid(f)) { return (PerlIOBase(f)->tab->Get_base != NULL); } return 0; } -#undef PerlIO_fast_gets int PerlIO_fast_gets(PerlIO *f) { - if (f && *f && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) { + if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) { PerlIO_funcs *tab = PerlIOBase(f)->tab; return (tab->Set_ptrcnt != NULL); } return 0; } -#undef PerlIO_has_cntptr int PerlIO_has_cntptr(PerlIO *f) { - if (f && *f) { + if (PerlIOValid(f)) { PerlIO_funcs *tab = PerlIOBase(f)->tab; return (tab->Get_ptr != NULL && tab->Get_cnt != NULL); } return 0; } -#undef PerlIO_canset_cnt int PerlIO_canset_cnt(PerlIO *f) { - if (f && *f) { + if (PerlIOValid(f)) { PerlIOl *l = PerlIOBase(f); return (l->tab->Set_ptrcnt != NULL); } return 0; } -#undef PerlIO_get_base STDCHAR * -PerlIO_get_base(PerlIO *f) +Perl_PerlIO_get_base(pTHX_ PerlIO *f) { - dTHX; - if (f && *f) + if (PerlIOValid(f)) return (*PerlIOBase(f)->tab->Get_base) (aTHX_ f); return NULL; } -#undef PerlIO_get_bufsiz int -PerlIO_get_bufsiz(PerlIO *f) +Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f) { - dTHX; - if (f && *f) + if (PerlIOValid(f)) return (*PerlIOBase(f)->tab->Get_bufsiz) (aTHX_ f); return 0; } -#undef PerlIO_get_ptr STDCHAR * -PerlIO_get_ptr(PerlIO *f) +Perl_PerlIO_get_ptr(pTHX_ PerlIO *f) { - dTHX; - PerlIO_funcs *tab = PerlIOBase(f)->tab; - if (tab->Get_ptr == NULL) - return NULL; - return (*tab->Get_ptr) (aTHX_ f); + if (PerlIOValid(f)) { + PerlIO_funcs *tab = PerlIOBase(f)->tab; + if (tab->Get_ptr == NULL) + return NULL; + return (*tab->Get_ptr) (aTHX_ f); + } + return NULL; } -#undef PerlIO_get_cnt int -PerlIO_get_cnt(PerlIO *f) +Perl_PerlIO_get_cnt(pTHX_ PerlIO *f) { - dTHX; - PerlIO_funcs *tab = PerlIOBase(f)->tab; - if (tab->Get_cnt == NULL) - return 0; - return (*tab->Get_cnt) (aTHX_ f); + if (PerlIOValid(f)) { + PerlIO_funcs *tab = PerlIOBase(f)->tab; + if (tab->Get_cnt == NULL) + return 0; + return (*tab->Get_cnt) (aTHX_ f); + } + return 0; } -#undef PerlIO_set_cnt void -PerlIO_set_cnt(PerlIO *f, int cnt) +Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt) { - dTHX; - (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, NULL, cnt); + if (PerlIOValid(f)) { + (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, NULL, cnt); + } } -#undef PerlIO_set_ptrcnt void -PerlIO_set_ptrcnt(PerlIO *f, STDCHAR * ptr, int cnt) +Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt) { - dTHX; - PerlIO_funcs *tab = PerlIOBase(f)->tab; - if (tab->Set_ptrcnt == NULL) { - Perl_croak(aTHX_ "PerlIO buffer snooping abuse"); + if (PerlIOValid(f)) { + PerlIO_funcs *tab = PerlIOBase(f)->tab; + if (tab->Set_ptrcnt == NULL) { + Perl_croak(aTHX_ "PerlIO buffer snooping abuse"); + } + (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, ptr, cnt); } - (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, ptr, cnt); } /*--------------------------------------------------------------------------------------*/ @@ -1655,7 +1681,7 @@ PerlIO_set_ptrcnt(PerlIO *f, STDCHAR * ptr, int cnt) IV PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg) { - if (PerlIONext(f)) { + if (*PerlIONext(f)) { PerlIO_funcs *tab = PerlIOBase(f)->tab; PerlIO_pop(aTHX_ f); if (tab->kind & PERLIO_K_UTF8) @@ -1670,7 +1696,7 @@ PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg) PerlIO_funcs PerlIO_utf8 = { "utf8", sizeof(PerlIOl), - PERLIO_K_DUMMY | PERLIO_F_UTF8, + PERLIO_K_DUMMY | PERLIO_K_UTF8, PerlIOUtf8_pushed, NULL, NULL, @@ -1682,17 +1708,17 @@ PerlIO_funcs PerlIO_utf8 = { NULL, NULL, NULL, - NULL, /* flush */ - NULL, /* fill */ + NULL, /* flush */ + NULL, /* fill */ NULL, NULL, NULL, NULL, - NULL, /* get_base */ - NULL, /* get_bufsiz */ - NULL, /* get_ptr */ - NULL, /* get_cnt */ - NULL, /* set_ptrcnt */ + NULL, /* get_base */ + NULL, /* get_bufsiz */ + NULL, /* get_ptr */ + NULL, /* get_cnt */ + NULL, /* set_ptrcnt */ }; PerlIO_funcs PerlIO_byte = { @@ -1710,17 +1736,17 @@ PerlIO_funcs PerlIO_byte = { NULL, NULL, NULL, - NULL, /* flush */ - NULL, /* fill */ + NULL, /* flush */ + NULL, /* fill */ NULL, NULL, NULL, NULL, - NULL, /* get_base */ - NULL, /* get_bufsiz */ - NULL, /* get_ptr */ - NULL, /* get_cnt */ - NULL, /* set_ptrcnt */ + NULL, /* get_base */ + NULL, /* get_bufsiz */ + NULL, /* get_ptr */ + NULL, /* get_cnt */ + NULL, /* set_ptrcnt */ }; PerlIO * @@ -1748,17 +1774,17 @@ PerlIO_funcs PerlIO_raw = { NULL, NULL, NULL, - NULL, /* flush */ - NULL, /* fill */ + NULL, /* flush */ + NULL, /* fill */ NULL, NULL, NULL, NULL, - NULL, /* get_base */ - NULL, /* get_bufsiz */ - NULL, /* get_ptr */ - NULL, /* get_cnt */ - NULL, /* set_ptrcnt */ + NULL, /* get_base */ + NULL, /* get_bufsiz */ + NULL, /* get_ptr */ + NULL, /* get_cnt */ + NULL, /* set_ptrcnt */ }; /*--------------------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------------------*/ @@ -1769,7 +1795,7 @@ PerlIO_funcs PerlIO_raw = { IV PerlIOBase_fileno(pTHX_ PerlIO *f) { - return PerlIO_fileno(PerlIONext(f)); + return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1; } char * @@ -1895,7 +1921,7 @@ PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) SSize_t avail = PerlIO_get_cnt(f); SSize_t take = 0; if (avail > 0) - take = (count < avail) ? count : avail; + take = ((SSize_t)count < avail) ? count : avail; if (take > 0) { STDCHAR *ptr = PerlIO_get_ptr(f); Copy(ptr, buf, take, STDCHAR); @@ -1932,7 +1958,7 @@ PerlIOBase_close(pTHX_ PerlIO *f) PerlIO *n = PerlIONext(f); if (PerlIO_flush(f) != 0) code = -1; - if (n && *n && (*PerlIOBase(n)->tab->Close)(aTHX_ n) != 0) + if (PerlIOValid(n) && (*PerlIOBase(n)->tab->Close)(aTHX_ n) != 0) code = -1; PerlIOBase(f)->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN); @@ -1942,7 +1968,7 @@ PerlIOBase_close(pTHX_ PerlIO *f) IV PerlIOBase_eof(pTHX_ PerlIO *f) { - if (f && *f) { + if (PerlIOValid(f)) { return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0; } return 1; @@ -1951,7 +1977,7 @@ PerlIOBase_eof(pTHX_ PerlIO *f) IV PerlIOBase_error(pTHX_ PerlIO *f) { - if (f && *f) { + if (PerlIOValid(f)) { return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0; } return 1; @@ -1960,10 +1986,10 @@ PerlIOBase_error(pTHX_ PerlIO *f) void PerlIOBase_clearerr(pTHX_ PerlIO *f) { - if (f && *f) { + if (PerlIOValid(f)) { PerlIO *n = PerlIONext(f); PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF); - if (n) + if (PerlIOValid(n)) PerlIO_clearerr(n); } } @@ -1971,7 +1997,7 @@ PerlIOBase_clearerr(pTHX_ PerlIO *f) void PerlIOBase_setlinebuf(pTHX_ PerlIO *f) { - if (f) { + if (PerlIOValid(f)) { PerlIOBase(f)->flags |= PERLIO_F_LINEBUF; } } @@ -1997,7 +2023,7 @@ PerlIO * PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags) { PerlIO *nexto = PerlIONext(o); - if (*nexto) { + if (PerlIOValid(nexto)) { PerlIO_funcs *tab = PerlIOBase(nexto)->tab; f = (*tab->Dup)(aTHX_ f, nexto, param, flags); } @@ -2070,7 +2096,9 @@ PerlIO_cleanup(pTHX) { int i; #ifdef USE_ITHREADS - PerlIO_debug("Cleanup %p\n",aTHX); + PerlIO_debug("Cleanup layers for %p\n",aTHX); +#else + PerlIO_debug("Cleanup layers\n"); #endif /* Raise STDIN..STDERR refcount so we don't close them */ for (i=0; i < 3; i++) @@ -2079,6 +2107,15 @@ PerlIO_cleanup(pTHX) /* Restore STDIN..STDERR refcount */ for (i=0; i < 3; i++) PerlIOUnix_refcnt_dec(i); + + if (PL_known_layers) { + PerlIO_list_free(aTHX_ PL_known_layers); + PL_known_layers = NULL; + } + if(PL_def_layerlist) { + PerlIO_list_free(aTHX_ PL_def_layerlist); + PL_def_layerlist = NULL; + } } @@ -2089,9 +2126,9 @@ PerlIO_cleanup(pTHX) */ typedef struct { - struct _PerlIO base; /* The generic part */ - int fd; /* UNIX like file descriptor */ - int oflags; /* open/fcntl flags */ + struct _PerlIO base; /* The generic part */ + int fd; /* UNIX like file descriptor */ + int oflags; /* open/fcntl flags */ } PerlIOUnix; int @@ -2162,6 +2199,8 @@ PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg) IV code = PerlIOBase_pushed(aTHX_ f, mode, arg); PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix); if (*PerlIONext(f)) { + /* We never call down so any pending stuff now */ + PerlIO_flush(PerlIONext(f)); s->fd = PerlIO_fileno(PerlIONext(f)); /* * XXX could (or should) we retrieve the oflags from the open file @@ -2179,7 +2218,7 @@ PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, IV n, const char *mode, int fd, int imode, int perm, PerlIO *f, int narg, SV **args) { - if (f) { + if (PerlIOValid(f)) { if (PerlIOBase(f)->flags & PERLIO_F_OPEN) (*PerlIOBase(f)->tab->Close)(aTHX_ f); } @@ -2201,15 +2240,18 @@ PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, mode++; if (!f) { f = PerlIO_allocate(aTHX); + } + if (!PerlIOValid(f)) { s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg), PerlIOUnix); } - else + else { s = PerlIOSelf(f, PerlIOUnix); + } s->fd = fd; s->oflags = imode; PerlIOBase(f)->flags |= PERLIO_F_OPEN; - PerlIOUnix_refcnt_inc(fd); + PerlIOUnix_refcnt_inc(fd); return f; } else { @@ -2303,7 +2345,7 @@ PerlIOUnix_close(pTHX_ PerlIO *f) if (PerlIOUnix_refcnt_dec(fd) > 0) { PerlIOBase(f)->flags &= ~PERLIO_F_OPEN; return 0; - } + } } else { SETERRNO(EBADF,SS$_IVCHAN); @@ -2327,7 +2369,7 @@ PerlIO_funcs PerlIO_unix = { sizeof(PerlIOUnix), PERLIO_K_RAW, PerlIOUnix_pushed, - PerlIOBase_noop_ok, + PerlIOBase_popped, PerlIOUnix_open, NULL, PerlIOUnix_fileno, @@ -2338,17 +2380,17 @@ PerlIO_funcs PerlIO_unix = { PerlIOUnix_seek, PerlIOUnix_tell, PerlIOUnix_close, - PerlIOBase_noop_ok, /* flush */ - PerlIOBase_noop_fail, /* fill */ + PerlIOBase_noop_ok, /* flush */ + PerlIOBase_noop_fail, /* fill */ PerlIOBase_eof, PerlIOBase_error, PerlIOBase_clearerr, PerlIOBase_setlinebuf, - NULL, /* get_base */ - NULL, /* get_bufsiz */ - NULL, /* get_ptr */ - NULL, /* get_cnt */ - NULL, /* set_ptrcnt */ + NULL, /* get_base */ + NULL, /* get_bufsiz */ + NULL, /* get_ptr */ + NULL, /* get_cnt */ + NULL, /* set_ptrcnt */ }; /*--------------------------------------------------------------------------------------*/ @@ -2356,9 +2398,17 @@ PerlIO_funcs PerlIO_unix = { * stdio as a layer */ +#if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE) +/* perl5.8 - This ensures the last minute VMS ungetc fix is not + broken by the last second glibc 2.3 fix + */ +#define STDIO_BUFFER_WRITABLE +#endif + + typedef struct { struct _PerlIO base; - FILE *stdio; /* The stream */ + FILE *stdio; /* The stream */ } PerlIOStdio; IV @@ -2393,25 +2443,52 @@ PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg) FILE *stdio = PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)), mode = PerlIOStdio_mode(mode, tmode)); - if (stdio) + if (stdio) { s->stdio = stdio; + /* We never call down so any pending stuff now */ + PerlIO_flush(PerlIONext(f)); + } else return -1; } return PerlIOBase_pushed(aTHX_ f, mode, arg); } -#undef PerlIO_importFILE + PerlIO * PerlIO_importFILE(FILE *stdio, int fl) { dTHX; PerlIO *f = NULL; if (stdio) { - PerlIOStdio *s = - PerlIOSelf(PerlIO_push - (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio, - "r+", Nullsv), PerlIOStdio); + /* We need to probe to see how we can open the stream + so start with read/write and then try write and read + we dup() so that we can fclose without loosing the fd. + + Note that the errno value set by a failing fdopen + varies between stdio implementations. + */ + int fd = PerlLIO_dup(fileno(stdio)); + char *mode = "r+"; + FILE *f2 = fdopen(fd, mode); + PerlIOStdio *s; + if (!f2) { + mode = "w"; + f2 = fdopen(fd, mode); + } + if (!f2) { + mode = "r"; + f2 = fdopen(fd, mode); + } + if (!f2) { + /* Don't seem to be able to open */ + PerlLIO_close(fd); + return f; + } + fclose(f2); + s = PerlIOSelf(PerlIO_push + (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio, + mode, Nullsv), PerlIOStdio); s->stdio = stdio; } return f; @@ -2423,7 +2500,7 @@ PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, int perm, PerlIO *f, int narg, SV **args) { char tmode[8]; - if (f) { + if (PerlIOValid(f)) { char *path = SvPV_nolen(*args); PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio); FILE *stdio; @@ -2446,9 +2523,11 @@ PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, else { FILE *stdio = PerlSIO_fopen(path, mode); if (stdio) { - PerlIOStdio *s = - PerlIOSelf(PerlIO_push - (aTHX_(f = PerlIO_allocate(aTHX)), self, + PerlIOStdio *s; + if (!f) { + f = PerlIO_allocate(aTHX); + } + s = PerlIOSelf(PerlIO_push(aTHX_ f, self, (mode = PerlIOStdio_mode(mode, tmode)), PerlIOArg), PerlIOStdio); @@ -2483,10 +2562,11 @@ PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, PerlIOStdio_mode(mode, tmode)); } if (stdio) { - PerlIOStdio *s = - PerlIOSelf(PerlIO_push - (aTHX_(f = PerlIO_allocate(aTHX)), self, - mode, PerlIOArg), PerlIOStdio); + PerlIOStdio *s; + if (!f) { + f = PerlIO_allocate(aTHX); + } + s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg), PerlIOStdio); s->stdio = stdio; PerlIOUnix_refcnt_inc(fileno(s->stdio)); return f; @@ -2532,8 +2612,7 @@ PerlIOStdio_close(pTHX_ PerlIO *f) FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) { /* Do not close it but do flush any buffers */ - PerlIO_flush(f); - return 0; + return PerlIO_flush(f); } return ( #ifdef SOCKS5_VERSION_NAME @@ -2575,15 +2654,57 @@ PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) SSize_t PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count) { - FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio; - STDCHAR *buf = ((STDCHAR *) vbuf) + count - 1; SSize_t unread = 0; - while (count > 0) { - int ch = *buf-- & 0xff; - if (PerlSIO_ungetc(ch, s) != ch) - break; - unread++; - count--; + FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio; + +#ifdef STDIO_BUFFER_WRITABLE + if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) { + STDCHAR *buf = ((STDCHAR *) vbuf) + count; + STDCHAR *base = PerlIO_get_base(f); + SSize_t cnt = PerlIO_get_cnt(f); + STDCHAR *ptr = PerlIO_get_ptr(f); + SSize_t avail = ptr - base; + if (avail > 0) { + if (avail > count) { + avail = count; + } + ptr -= avail; + Move(buf-avail,ptr,avail,STDCHAR); + count -= avail; + unread += avail; + PerlIO_set_ptrcnt(f,ptr,cnt+avail); + if (PerlSIO_feof(s) && unread >= 0) + PerlSIO_clearerr(s); + } + } + else +#endif + if (PerlIO_has_cntptr(f)) { + /* We can get pointer to buffer but not its base + Do ungetc() but check chars are ending up in the + buffer + */ + STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s); + STDCHAR *buf = ((STDCHAR *) vbuf) + count; + while (count > 0) { + int ch = *--buf & 0xFF; + if (ungetc(ch,s) != ch) { + /* ungetc did not work */ + break; + } + if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) { + /* Did not change pointer as expected */ + fgetc(s); /* get char back again */ + break; + } + /* It worked ! */ + count--; + unread++; + } + } + + if (count > 0) { + unread += PerlIOBase_unread(aTHX_ f, vbuf, count); } return unread; } @@ -2636,24 +2757,6 @@ PerlIOStdio_flush(pTHX_ PerlIO *f) } IV -PerlIOStdio_fill(pTHX_ PerlIO *f) -{ - FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; - int c; - /* - * fflush()ing read-only streams can cause trouble on some stdio-s - */ - if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) { - if (PerlSIO_fflush(stdio) != 0) - return EOF; - } - c = PerlSIO_fgetc(stdio); - if (c == EOF || PerlSIO_ungetc(c, stdio) != c) - return EOF; - return 0; -} - -IV PerlIOStdio_eof(pTHX_ PerlIO *f) { return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio); @@ -2718,10 +2821,9 @@ PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt) FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; if (ptr != NULL) { #ifdef STDIO_PTR_LVALUE - PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */ + PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */ #ifdef STDIO_PTR_LVAL_SETS_CNT if (PerlSIO_get_cnt(stdio) != (cnt)) { - dTHX; assert(PerlSIO_get_cnt(stdio) == (cnt)); } #endif @@ -2731,34 +2833,98 @@ PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt) */ return; #endif -#else /* STDIO_PTR_LVALUE */ +#else /* STDIO_PTR_LVALUE */ PerlProc_abort(); -#endif /* STDIO_PTR_LVALUE */ +#endif /* STDIO_PTR_LVALUE */ } /* * Now (or only) set cnt */ #ifdef STDIO_CNT_LVALUE PerlSIO_set_cnt(stdio, cnt); -#else /* STDIO_CNT_LVALUE */ +#else /* STDIO_CNT_LVALUE */ #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT)) PerlSIO_set_ptr(stdio, PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) - cnt)); -#else /* STDIO_PTR_LVAL_SETS_CNT */ +#else /* STDIO_PTR_LVAL_SETS_CNT */ PerlProc_abort(); -#endif /* STDIO_PTR_LVAL_SETS_CNT */ -#endif /* STDIO_CNT_LVALUE */ +#endif /* STDIO_PTR_LVAL_SETS_CNT */ +#endif /* STDIO_CNT_LVALUE */ } + +#endif + +IV +PerlIOStdio_fill(pTHX_ PerlIO *f) +{ + FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; + int c; + /* + * fflush()ing read-only streams can cause trouble on some stdio-s + */ + if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) { + if (PerlSIO_fflush(stdio) != 0) + return EOF; + } + c = PerlSIO_fgetc(stdio); + if (c == EOF) + return EOF; + +#if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT))) + +#ifdef STDIO_BUFFER_WRITABLE + if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) { + /* Fake ungetc() to the real buffer in case system's ungetc + goes elsewhere + */ + STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio); + SSize_t cnt = PerlSIO_get_cnt(stdio); + STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio); + if (ptr == base+1) { + *--ptr = (STDCHAR) c; + PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1); + if (PerlSIO_feof(stdio)) + PerlSIO_clearerr(stdio); + return 0; + } + } + else +#endif + if (PerlIO_has_cntptr(f)) { + STDCHAR ch = c; + if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) { + return 0; + } + } +#endif + +#if defined(VMS) + /* An ungetc()d char is handled separately from the regular + * buffer, so we stuff it in the buffer ourselves. + * Should never get called as should hit code above + */ + *(--((*stdio)->_ptr)) = (unsigned char) c; + (*stdio)->_cnt++; +#else + /* If buffer snoop scheme above fails fall back to + using ungetc(). + */ + if (PerlSIO_ungetc(c, stdio) != c) + return EOF; #endif + return 0; +} + + PerlIO_funcs PerlIO_stdio = { "stdio", sizeof(PerlIOStdio), PERLIO_K_BUFFERED, PerlIOBase_pushed, - PerlIOBase_noop_ok, + PerlIOBase_popped, PerlIOStdio_open, NULL, PerlIOStdio_fileno, @@ -2787,34 +2953,33 @@ PerlIO_funcs PerlIO_stdio = { PerlIOStdio_get_cnt, #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT))) PerlIOStdio_set_ptrcnt -#else /* STDIO_PTR_LVALUE */ +#else /* STDIO_PTR_LVALUE */ NULL -#endif /* STDIO_PTR_LVALUE */ -#else /* USE_STDIO_PTR */ +#endif /* STDIO_PTR_LVALUE */ +#else /* USE_STDIO_PTR */ NULL, NULL, NULL -#endif /* USE_STDIO_PTR */ +#endif /* USE_STDIO_PTR */ }; -#undef PerlIO_exportFILE FILE * PerlIO_exportFILE(PerlIO *f, int fl) { + dTHX; FILE *stdio; + char buf[8]; PerlIO_flush(f); - stdio = fdopen(PerlIO_fileno(f), "r+"); + stdio = fdopen(PerlIO_fileno(f), PerlIO_modestr(f,buf)); if (stdio) { - dTHX; PerlIOStdio *s = - PerlIOSelf(PerlIO_push(aTHX_ f, &PerlIO_stdio, "r+", Nullsv), + PerlIOSelf(PerlIO_push(aTHX_ f, &PerlIO_stdio, buf, Nullsv), PerlIOStdio); s->stdio = stdio; } return stdio; } -#undef PerlIO_findFILE FILE * PerlIO_findFILE(PerlIO *f) { @@ -2829,10 +2994,22 @@ PerlIO_findFILE(PerlIO *f) return PerlIO_exportFILE(f, 0); } -#undef PerlIO_releaseFILE void PerlIO_releaseFILE(PerlIO *p, FILE *f) { + PerlIOl *l; + while ((l = *p)) { + if (l->tab == &PerlIO_stdio) { + PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio); + if (s->stdio == f) { + dTHX; + PerlIO_pop(aTHX_ p); + return; + } + } + p = PerlIONext(p); + } + return; } /*--------------------------------------------------------------------------------------*/ @@ -2861,21 +3038,17 @@ PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, IV n, const char *mode, int fd, int imode, int perm, PerlIO *f, int narg, SV **args) { - if (f) { + if (PerlIOValid(f)) { PerlIO *next = PerlIONext(f); - PerlIO_funcs *tab = - PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab); - next = - (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm, + PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab); + next = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm, next, narg, args); - if (!next - || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg) != 0) { + if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg) != 0) { return NULL; } } else { - PerlIO_funcs *tab = - PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm()); + PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm()); int init = 0; if (*mode == 'I') { init = 1; @@ -2884,9 +3057,9 @@ PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, */ } f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm, - NULL, narg, args); + f, narg, args); if (f) { - if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) { + if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) { /* * if push fails during open, open fails. close will pop us. */ @@ -2894,18 +3067,23 @@ PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, return NULL; } else { fd = PerlIO_fileno(f); -#ifdef PERLIO_USING_CRLF - /* - * do something about failing setmode()? --jhi - */ - PerlLIO_setmode(fd, O_BINARY); -#endif if (init && fd == 2) { /* * Initial stderr is unbuffered */ PerlIOBase(f)->flags |= PERLIO_F_UNBUF; } +#ifdef PERLIO_USING_CRLF +# ifdef PERLIO_IS_BINMODE_FD + if (PERLIO_IS_BINMODE_FD(fd)) + PerlIO_binmode(f, '<'/*not used*/, O_BINARY, Nullch); + else +# endif + /* + * do something about failing setmode()? --jhi + */ + PerlLIO_setmode(fd, O_BINARY); +#endif } } } @@ -2921,13 +3099,13 @@ PerlIOBuf_flush(pTHX_ PerlIO *f) { PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); int code = 0; + PerlIO *n = PerlIONext(f); if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) { /* * write() the buffer */ STDCHAR *buf = b->buf; STDCHAR *p = buf; - PerlIO *n = PerlIONext(f); while (p < b->ptr) { SSize_t count = PerlIO_write(n, p, b->ptr - p); if (count > 0) { @@ -2951,17 +3129,20 @@ PerlIOBuf_flush(pTHX_ PerlIO *f) /* * We did not consume all of it */ - if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) == 0) { - b->posn = PerlIO_tell(PerlIONext(f)); + if (PerlIO_seek(n, b->posn, SEEK_SET) == 0) { + /* Reload n as some layers may pop themselves on seek */ + b->posn = PerlIO_tell(n = PerlIONext(f)); + } + else { + return code; } } } b->ptr = b->end = b->buf; PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF); - /* - * FIXME: Is this right for read case ? - */ - if (PerlIO_flush(PerlIONext(f)) != 0) + /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */ + /* FIXME: Doing downstream flush may be sub-optimal see PerlIOBuf_fill() below */ + if (PerlIOValid(n) && PerlIO_flush(n) != 0) code = -1; return code; } @@ -2973,10 +3154,10 @@ PerlIOBuf_fill(pTHX_ PerlIO *f) PerlIO *n = PerlIONext(f); SSize_t avail; /* - * FIXME: doing the down-stream flush is a bad idea if it causes - * pre-read data in stdio buffer to be discarded but this is too - * simplistic - as it skips _our_ hosekeeping and breaks tell tests. - * if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) { } + * FIXME: doing the down-stream flush maybe sub-optimal if it causes + * pre-read data in stdio buffer to be discarded. + * However, skipping the flush also skips _our_ hosekeeping + * and breaks tell tests. So we do the flush. */ if (PerlIO_flush(f) != 0) return -1; @@ -2984,12 +3165,12 @@ PerlIOBuf_fill(pTHX_ PerlIO *f) PerlIOBase_flush_linebuf(aTHX); if (!b->buf) - PerlIO_get_base(f); /* allocate via vtable */ + PerlIO_get_base(f); /* allocate via vtable */ b->ptr = b->end = b->buf; if (PerlIO_fast_gets(n)) { /* - * Layer below is also buffered We do _NOT_ want to call its + * Layer below is also buffered. We do _NOT_ want to call its * ->Read() because that will loop till it gets what we asked for * which may hang on a pipe etc. Instead take anything it has to * hand, or ask it to fill _once_. @@ -3007,7 +3188,7 @@ PerlIOBuf_fill(pTHX_ PerlIO *f) if (avail > 0) { STDCHAR *ptr = PerlIO_get_ptr(n); SSize_t cnt = avail; - if (avail > b->bufsiz) + if (avail > (SSize_t)b->bufsiz) avail = b->bufsiz; Copy(ptr, b->buf, avail, STDCHAR); PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail); @@ -3032,7 +3213,7 @@ SSize_t PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) { PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); - if (f) { + if (PerlIOValid(f)) { if (!b->ptr) PerlIO_get_base(f); return PerlIOBase_read(aTHX_ f, vbuf, count); @@ -3094,6 +3275,9 @@ PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count) PerlIOBase(f)->flags &= ~PERLIO_F_EOF; } } + if (count > 0) { + unread += PerlIOBase_unread(aTHX_ f, vbuf, count); + } return unread; } @@ -3175,6 +3359,20 @@ PerlIOBuf_tell(pTHX_ PerlIO *f) } IV +PerlIOBuf_popped(pTHX_ PerlIO *f) +{ + IV code = PerlIOBase_popped(aTHX_ f); + PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); + if (b->buf && b->buf != (STDCHAR *) & b->oneword) { + Safefree(b->buf); + } + b->buf = NULL; + b->ptr = b->end = b->buf; + PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF); + return code; +} + +IV PerlIOBuf_close(pTHX_ PerlIO *f) { IV code = PerlIOBase_close(aTHX_ f); @@ -3244,7 +3442,6 @@ PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt) PerlIO_get_base(f); b->ptr = ptr; if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) { - dTHX; assert(PerlIO_get_cnt(f) == cnt); assert(b->ptr >= b->buf); } @@ -3264,7 +3461,7 @@ PerlIO_funcs PerlIO_perlio = { sizeof(PerlIOBuf), PERLIO_K_BUFFERED, PerlIOBuf_pushed, - PerlIOBase_noop_ok, + PerlIOBuf_popped, PerlIOBuf_open, NULL, PerlIOBase_fileno, @@ -3367,11 +3564,11 @@ PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) { SSize_t avail = PerlIO_get_cnt(f); SSize_t got = 0; - if (count < avail) + if ((SSize_t)count < avail) avail = count; if (avail > 0) got = PerlIOBuf_read(aTHX_ f, vbuf, avail); - if (got >= 0 && got < count) { + if (got >= 0 && got < (SSize_t)count) { SSize_t more = PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got); if (more >= 0 || got == 0) @@ -3385,7 +3582,7 @@ PerlIO_funcs PerlIO_pending = { sizeof(PerlIOBuf), PERLIO_K_BUFFERED, PerlIOPending_pushed, - PerlIOBase_noop_ok, + PerlIOBuf_popped, NULL, NULL, PerlIOBase_fileno, @@ -3419,8 +3616,8 @@ PerlIO_funcs PerlIO_pending = { */ typedef struct { - PerlIOBuf base; /* PerlIOBuf stuff */ - STDCHAR *nl; /* Position of crlf we "lied" about in the + PerlIOBuf base; /* PerlIOBuf stuff */ + STDCHAR *nl; /* Position of crlf we "lied" about in the * buffer */ } PerlIOCrlf; @@ -3496,8 +3693,8 @@ PerlIOCrlf_get_cnt(pTHX_ PerlIO *f) PerlIO_get_base(f); if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) { PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf); - if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && !c->nl) { - STDCHAR *nl = b->ptr; + if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) { + STDCHAR *nl = (c->nl) ? c->nl : b->ptr; scan: while (nl < b->end && *nl != 0xd) nl++; @@ -3520,31 +3717,34 @@ PerlIOCrlf_get_cnt(pTHX_ PerlIO *f) /* * Blast - found CR as last char in buffer */ + if (b->ptr < nl) { /* * They may not care, defer work as long as * possible */ + c->nl = nl; return (nl - b->ptr); } else { int code; - b->ptr++; /* say we have read it as far as + b->ptr++; /* say we have read it as far as * flush() is concerned */ - b->buf++; /* Leave space in front of buffer */ - b->bufsiz--; /* Buffer is thus smaller */ - code = PerlIO_fill(f); /* Fetch some more */ - b->bufsiz++; /* Restore size for next time */ - b->buf--; /* Point at space */ - b->ptr = nl = b->buf; /* Which is what we hand + b->buf++; /* Leave space in front of buffer */ + b->bufsiz--; /* Buffer is thus smaller */ + code = PerlIO_fill(f); /* Fetch some more */ + b->bufsiz++; /* Restore size for next time */ + b->buf--; /* Point at space */ + b->ptr = nl = b->buf; /* Which is what we hand * off */ - b->posn--; /* Buffer starts here */ - *nl = 0xd; /* Fill in the CR */ + b->posn--; /* Buffer starts here */ + *nl = 0xd; /* Fill in the CR */ if (code == 0) - goto test; /* fill() call worked */ + goto test; /* fill() call worked */ /* * CR at EOF - just fall through */ + /* Should we clear EOF though ??? */ } } } @@ -3559,39 +3759,40 @@ PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt) { PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf); - IV flags = PerlIOBase(f)->flags; if (!b->buf) PerlIO_get_base(f); if (!ptr) { - if (c->nl) + if (c->nl) { ptr = c->nl + 1; + if (ptr == b->end && *c->nl == 0xd) { + /* Defered CR at end of buffer case - we lied about count */ + ptr--; + } + } else { ptr = b->end; - if ((flags & PERLIO_F_CRLF) && ptr > b->buf && ptr[-1] == 0xd) - ptr--; } ptr -= cnt; } else { +#if 0 /* * Test code - delete when it works ... */ - STDCHAR *chk; - if (c->nl) - chk = c->nl + 1; - else { - chk = b->end; - if ((flags & PERLIO_F_CRLF) && chk > b->buf && chk[-1] == 0xd) - chk--; + IV flags = PerlIOBase(f)->flags; + STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end; + if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) { + /* Defered CR at end of buffer case - we lied about count */ + chk--; } chk -= cnt; - if (ptr != chk) { - dTHX; + if (ptr != chk ) { Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf " nl=%p e=%p for %d", ptr, chk, flags, c->nl, b->end, cnt); } +#endif } if (c->nl) { if (ptr > c->nl) { @@ -3633,8 +3834,8 @@ PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count) break; } else { - *(b->ptr)++ = 0xd; /* CR */ - *(b->ptr)++ = 0xa; /* LF */ + *(b->ptr)++ = 0xd; /* CR */ + *(b->ptr)++ = 0xa; /* LF */ buf++; if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) { PerlIO_flush(f); @@ -3674,15 +3875,15 @@ PerlIO_funcs PerlIO_crlf = { sizeof(PerlIOCrlf), PERLIO_K_BUFFERED | PERLIO_K_CANCRLF, PerlIOCrlf_pushed, - PerlIOBase_noop_ok, /* popped */ + PerlIOBuf_popped, /* popped */ PerlIOBuf_open, NULL, PerlIOBase_fileno, PerlIOBuf_dup, - PerlIOBuf_read, /* generic read works with ptr/cnt lies + PerlIOBuf_read, /* generic read works with ptr/cnt lies * ... */ - PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */ - PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */ + PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */ + PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */ PerlIOBuf_seek, PerlIOBuf_tell, PerlIOBuf_close, @@ -3706,10 +3907,10 @@ PerlIO_funcs PerlIO_crlf = { */ typedef struct { - PerlIOBuf base; /* PerlIOBuf stuff */ - Mmap_t mptr; /* Mapped address */ - Size_t len; /* mapped length */ - STDCHAR *bbuf; /* malloced buffer if map fails */ + PerlIOBuf base; /* PerlIOBuf stuff */ + Mmap_t mptr; /* Mapped address */ + Size_t len; /* mapped length */ + STDCHAR *bbuf; /* malloced buffer if map fails */ } PerlIOMmap; static size_t page_size = 0; @@ -3760,7 +3961,7 @@ PerlIOMmap_map(pTHX_ PerlIO *f) page_size = getpagesize(); # else # if defined(I_SYS_PARAM) && defined(PAGESIZE) - page_size = PAGESIZE; /* compiletime, bad */ + page_size = PAGESIZE; /* compiletime, bad */ # endif # endif #endif @@ -3809,7 +4010,7 @@ PerlIOMmap_map(pTHX_ PerlIO *f) } IV -PerlIOMmap_unmap(PerlIO *f) +PerlIOMmap_unmap(pTHX_ PerlIO *f) { PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap); PerlIOBuf *b = &m->base; @@ -3844,11 +4045,11 @@ PerlIOMmap_get_base(pTHX_ PerlIO *f) /* * We have a write buffer or flushed PerlIOBuf read buffer */ - m->bbuf = b->buf; /* save it in case we need it again */ - b->buf = NULL; /* Clear to trigger below */ + m->bbuf = b->buf; /* save it in case we need it again */ + b->buf = NULL; /* Clear to trigger below */ } if (!b->buf) { - PerlIOMmap_map(aTHX_ f); /* Try and map it */ + PerlIOMmap_map(aTHX_ f); /* Try and map it */ if (!b->buf) { /* * Map did not work - recover PerlIOBuf buffer if we have one @@ -3903,7 +4104,7 @@ PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count) * No, or wrong sort of, buffer */ if (m->len) { - if (PerlIOMmap_unmap(f) != 0) + if (PerlIOMmap_unmap(aTHX_ f) != 0) return 0; } /* @@ -3933,7 +4134,7 @@ PerlIOMmap_flush(pTHX_ PerlIO *f) /* * Unmap the buffer */ - if (PerlIOMmap_unmap(f) != 0) + if (PerlIOMmap_unmap(aTHX_ f) != 0) code = -1; } else { @@ -3989,7 +4190,7 @@ PerlIO_funcs PerlIO_mmap = { sizeof(PerlIOMmap), PERLIO_K_BUFFERED, PerlIOBuf_pushed, - PerlIOBase_noop_ok, + PerlIOBuf_popped, PerlIOBuf_open, NULL, PerlIOBase_fileno, @@ -4013,35 +4214,29 @@ PerlIO_funcs PerlIO_mmap = { PerlIOBuf_set_ptrcnt, }; -#endif /* HAS_MMAP */ +#endif /* HAS_MMAP */ -#undef PerlIO_stdin PerlIO * -PerlIO_stdin(void) +Perl_PerlIO_stdin(pTHX) { - dTHX; if (!PL_perlio) { PerlIO_stdstreams(aTHX); } return &PL_perlio[1]; } -#undef PerlIO_stdout PerlIO * -PerlIO_stdout(void) +Perl_PerlIO_stdout(pTHX) { - dTHX; if (!PL_perlio) { PerlIO_stdstreams(aTHX); } return &PL_perlio[2]; } -#undef PerlIO_stderr PerlIO * -PerlIO_stderr(void) +Perl_PerlIO_stderr(pTHX) { - dTHX; if (!PL_perlio) { PerlIO_stdstreams(aTHX); } @@ -4050,7 +4245,6 @@ PerlIO_stderr(void) /*--------------------------------------------------------------------------------------*/ -#undef PerlIO_getname char * PerlIO_getname(PerlIO *f, char *buf) { @@ -4073,10 +4267,37 @@ PerlIO_getname(PerlIO *f, char *buf) * terms of above */ +#undef PerlIO_fdopen +PerlIO * +PerlIO_fdopen(int fd, const char *mode) +{ + dTHX; + return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL); +} + +#undef PerlIO_open +PerlIO * +PerlIO_open(const char *path, const char *mode) +{ + dTHX; + SV *name = sv_2mortal(newSVpvn(path, strlen(path))); + return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name); +} + +#undef Perlio_reopen +PerlIO * +PerlIO_reopen(const char *path, const char *mode, PerlIO *f) +{ + dTHX; + SV *name = sv_2mortal(newSVpvn(path, strlen(path))); + return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name); +} + #undef PerlIO_getc int PerlIO_getc(PerlIO *f) { + dTHX; STDCHAR buf[1]; SSize_t count = PerlIO_read(f, buf, 1); if (count == 1) { @@ -4089,6 +4310,7 @@ PerlIO_getc(PerlIO *f) int PerlIO_ungetc(PerlIO *f, int ch) { + dTHX; if (ch != EOF) { STDCHAR buf = ch; if (PerlIO_unread(f, &buf, 1) == 1) @@ -4101,6 +4323,7 @@ PerlIO_ungetc(PerlIO *f, int ch) int PerlIO_putc(PerlIO *f, int ch) { + dTHX; STDCHAR buf = ch; return PerlIO_write(f, &buf, 1); } @@ -4109,6 +4332,7 @@ PerlIO_putc(PerlIO *f, int ch) int PerlIO_puts(PerlIO *f, const char *s) { + dTHX; STRLEN len = strlen(s); return PerlIO_write(f, s, len); } @@ -4117,6 +4341,7 @@ PerlIO_puts(PerlIO *f, const char *s) void PerlIO_rewind(PerlIO *f) { + dTHX; PerlIO_seek(f, (Off_t) 0, SEEK_SET); PerlIO_clearerr(f); } @@ -4159,6 +4384,7 @@ PerlIO_printf(PerlIO *f, const char *fmt, ...) int PerlIO_stdoutf(const char *fmt, ...) { + dTHX; va_list ap; int result; va_start(ap, fmt); @@ -4206,8 +4432,8 @@ PerlIO_tmpfile(void) #undef HAS_FSETPOS #undef HAS_FGETPOS -#endif /* USE_SFIO */ -#endif /* PERLIO_IS_STDIO */ +#endif /* USE_SFIO */ +#endif /* PERLIO_IS_STDIO */ /*======================================================================================*/ /* @@ -4286,7 +4512,7 @@ int vprintf(char *pat, char *args) { _doprnt(pat, args, stdout); - return 0; /* wrong, but perl doesn't use the return + return 0; /* wrong, but perl doesn't use the return * value */ } @@ -4294,7 +4520,7 @@ int vfprintf(FILE *fd, char *pat, char *args) { _doprnt(pat, args, fd); - return 0; /* wrong, but perl doesn't use the return + return 0; /* wrong, but perl doesn't use the return * value */ }