/* perlio.c
*
- * Copyright (c) 1996-2000, Nick Ing-Simmons
+ * Copyright (c) 1996-2001, 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.
#define PERL_IN_PERLIO_C
#include "perl.h"
-#ifndef PERLIO_LAYERS
-int
-PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
-{
- if (!names || !*names || strEQ(names,":crlf") || strEQ(names,":raw"))
- {
- return 0;
- }
- Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl",names);
- /* NOTREACHED */
- return -1;
-}
+#undef PerlMemShared_calloc
+#define PerlMemShared_calloc(x,y) calloc(x,y)
+#undef PerlMemShared_free
+#define PerlMemShared_free(x) free(x)
int
perlsio_binmode(FILE *fp, int iotype, int mode)
}
return 0;
# else
+ dTHX;
if (PerlLIO_setmode(fileno(fp), mode) != -1) {
# if defined(WIN32) && defined(__BORLANDC__)
/* The translation mode of the stream is maintained independent
#endif
}
+#ifndef PERLIO_LAYERS
+int
+PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
+{
+ if (!names || !*names || strEQ(names,":crlf") || strEQ(names,":raw"))
+ {
+ return 0;
+ }
+ Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl",names);
+ /* NOTREACHED */
+ return -1;
+}
+
int
PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
{
#endif
-#if !defined(PERL_IMPLICIT_SYS)
#ifdef PERLIO_IS_STDIO
void
PerlIO_debug(const char *fmt,...)
{
+ dTHX;
static int dbg = 0;
va_list ap;
va_start(ap,fmt);
#define PERLIO_TABLE_SIZE 64
PerlIO *
-PerlIO_allocate(void)
+PerlIO_allocate(pTHX)
{
/* Find a free slot in the table, allocating new table as necessary */
- PerlIO **last = &_perlio;
+ PerlIO **last;
PerlIO *f;
+ last = &_perlio;
while ((f = *last))
{
int i;
}
}
}
- Newz('I',f,PERLIO_TABLE_SIZE,PerlIO);
+ f = PerlMemShared_calloc(PERLIO_TABLE_SIZE,sizeof(PerlIO));
if (!f)
- return NULL;
+ {
+ return NULL;
+ }
*last = f;
return f+1;
}
void
-PerlIO_cleantable(PerlIO **tablep)
+PerlIO_cleantable(pTHX_ PerlIO **tablep)
{
PerlIO *table = *tablep;
if (table)
{
int i;
- PerlIO_cleantable((PerlIO **) &(table[0]));
+ PerlIO_cleantable(aTHX_ (PerlIO **) &(table[0]));
for (i=PERLIO_TABLE_SIZE-1; i > 0; i--)
{
PerlIO *f = table+i;
PerlIO_close(f);
}
}
- Safefree(table);
+ PerlMemShared_free(table);
*tablep = NULL;
}
}
AV *PerlIO_layer_av;
void
-PerlIO_cleanup(void)
+PerlIO_cleanup()
{
- PerlIO_cleantable(&_perlio);
+ dTHX;
+ PerlIO_cleantable(aTHX_ &_perlio);
}
void
PerlIO_pop(PerlIO *f)
{
+ dTHX;
PerlIOl *l = *f;
if (l)
{
- PerlIO_debug(__FUNCTION__ " f=%p %s\n",f,l->tab->name);
- (*l->tab->Popped)(f);
+ PerlIO_debug("PerlIO_pop f=%p %s\n",f,l->tab->name);
+ if (l->tab->Popped)
+ (*l->tab->Popped)(f);
*f = l->next;
- Safefree(l);
+ PerlMemShared_free(l);
}
}
dTHX;
SV **svp;
SV *sv;
- if (len <= 0)
+ if ((SSize_t) len <= 0)
len = strlen(name);
svp = hv_fetch(PerlIO_layer_hv,name,len,0);
if (svp && (sv = *svp) && SvROK(sv))
PerlIO *ifp = IoIFP(io);
PerlIO *ofp = IoOFP(io);
AV *av = (AV *) mg->mg_obj;
- Perl_warn(aTHX_ "set %_ %p %p %p",sv,io,ifp,ofp);
+ Perl_warn(aTHX_ "set %"SVf" %p %p %p",sv,io,ifp,ofp);
}
return 0;
}
PerlIO *ifp = IoIFP(io);
PerlIO *ofp = IoOFP(io);
AV *av = (AV *) mg->mg_obj;
- Perl_warn(aTHX_ "get %_ %p %p %p",sv,io,ifp,ofp);
+ Perl_warn(aTHX_ "get %"SVf" %p %p %p",sv,io,ifp,ofp);
}
return 0;
}
static int
perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
{
- Perl_warn(aTHX_ "clear %_",sv);
+ Perl_warn(aTHX_ "clear %"SVf,sv);
return 0;
}
static int
perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
{
- Perl_warn(aTHX_ "free %_",sv);
+ Perl_warn(aTHX_ "free %"SVf,sv);
return 0;
}
mg = mg_find(sv,'~');
mg->mg_virtual = &perlio_vtab;
mg_magical(sv);
- Perl_warn(aTHX_ "attrib %_",sv);
+ Perl_warn(aTHX_ "attrib %"SVf,sv);
for (i=2; i < items; i++)
{
STRLEN len;
dTHX;
HV *stash = gv_stashpv("perlio::Layer", TRUE);
SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))),stash);
+ if (!PerlIO_layer_hv)
+ {
+ PerlIO_layer_hv = get_hv("open::layers",GV_ADD|GV_ADDMULTI);
+ }
hv_store(PerlIO_layer_hv,tab->name,strlen(tab->name),sv,0);
+ PerlIO_debug("define %s %p\n",tab->name,tab);
+}
+
+void
+PerlIO_default_buffer(pTHX)
+{
+ PerlIO_funcs *tab = &PerlIO_perlio;
+ if (O_BINARY != O_TEXT)
+ {
+ tab = &PerlIO_crlf;
+ }
+ else
+ {
+ if (PerlIO_stdio.Set_ptrcnt)
+ {
+ tab = &PerlIO_stdio;
+ }
+ }
+ PerlIO_debug("Pushing %s\n",tab->name);
+ av_push(PerlIO_layer_av,SvREFCNT_inc(PerlIO_find_layer(tab->name,0)));
}
+
PerlIO_funcs *
PerlIO_default_layer(I32 n)
{
SV *layer;
PerlIO_funcs *tab = &PerlIO_stdio;
int len;
- if (!PerlIO_layer_hv)
+ if (!PerlIO_layer_av)
{
const char *s = PerlEnv_getenv("PERLIO");
+ PerlIO_layer_av = get_av("open::layers",GV_ADD|GV_ADDMULTI);
newXS("perlio::import",XS_perlio_import,__FILE__);
newXS("perlio::unimport",XS_perlio_unimport,__FILE__);
#if 0
newXS("io::MODIFY_SCALAR_ATTRIBUTES",XS_io_MODIFY_SCALAR_ATTRIBUTES,__FILE__);
#endif
- PerlIO_layer_hv = get_hv("open::layers",GV_ADD|GV_ADDMULTI);
- PerlIO_layer_av = get_av("open::layers",GV_ADD|GV_ADDMULTI);
+ PerlIO_define_layer(&PerlIO_raw);
PerlIO_define_layer(&PerlIO_unix);
PerlIO_define_layer(&PerlIO_perlio);
PerlIO_define_layer(&PerlIO_stdio);
#ifdef HAS_MMAP
PerlIO_define_layer(&PerlIO_mmap);
#endif
+ PerlIO_define_layer(&PerlIO_utf8);
+ PerlIO_define_layer(&PerlIO_byte);
av_push(PerlIO_layer_av,SvREFCNT_inc(PerlIO_find_layer(PerlIO_unix.name,0)));
if (s)
{
+ IV buffered = 0;
while (*s)
{
while (*s && isSPACE((unsigned char)*s))
layer = PerlIO_find_layer(s,e-s);
if (layer)
{
+ PerlIO_funcs *tab = INT2PTR(PerlIO_funcs *, SvIV(SvRV(layer)));
+ if ((tab->kind & PERLIO_K_DUMMY) && (tab->kind & PERLIO_K_BUFFERED))
+ {
+ if (!buffered)
+ PerlIO_default_buffer(aTHX);
+ }
PerlIO_debug("Pushing %.*s\n",(e-s),s);
av_push(PerlIO_layer_av,SvREFCNT_inc(layer));
+ buffered |= (tab->kind & PERLIO_K_BUFFERED);
}
else
Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"",(e-s),s);
len = av_len(PerlIO_layer_av);
if (len < 1)
{
- if (O_BINARY != O_TEXT)
- {
- av_push(PerlIO_layer_av,SvREFCNT_inc(PerlIO_find_layer(PerlIO_crlf.name,0)));
- }
- else
- {
- if (PerlIO_stdio.Set_ptrcnt)
- {
- av_push(PerlIO_layer_av,SvREFCNT_inc(PerlIO_find_layer(PerlIO_stdio.name,0)));
- }
- else
- {
- av_push(PerlIO_layer_av,SvREFCNT_inc(PerlIO_find_layer(PerlIO_perlio.name,0)));
- }
- }
+ PerlIO_default_buffer(aTHX);
len = av_len(PerlIO_layer_av);
}
if (n < 0)
{
if (!_perlio)
{
- PerlIO_allocate();
+ dTHX;
+ PerlIO_allocate(aTHX);
PerlIO_fdopen(0,"Ir" PERLIO_STDTEXT);
PerlIO_fdopen(1,"Iw" PERLIO_STDTEXT);
PerlIO_fdopen(2,"Iw" PERLIO_STDTEXT);
}
PerlIO *
-PerlIO_push(PerlIO *f,PerlIO_funcs *tab,const char *mode)
+PerlIO_push(PerlIO *f,PerlIO_funcs *tab,const char *mode,const char *arg,STRLEN len)
{
+ dTHX;
PerlIOl *l = NULL;
- Newc('L',l,tab->size,char,PerlIOl);
+ l = PerlMemShared_calloc(tab->size,sizeof(char));
if (l)
{
Zero(l,tab->size,char);
l->next = *f;
l->tab = tab;
*f = l;
- PerlIO_debug(__FUNCTION__ " f=%p %s %s\n",f,tab->name,(mode) ? mode : "(Null)");
- if ((*l->tab->Pushed)(f,mode) != 0)
+ PerlIO_debug("PerlIO_push f=%p %s %s '%.*s'\n",
+ f,tab->name,(mode) ? mode : "(Null)",(int) len,arg);
+ if ((*l->tab->Pushed)(f,mode,arg,len) != 0)
{
PerlIO_pop(f);
return NULL;
return f;
}
+IV
+PerlIOPop_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
+{
+ PerlIO_pop(f);
+ if (*f)
+ {
+ PerlIO_flush(f);
+ PerlIO_pop(f);
+ return 0;
+ }
+ return -1;
+}
+
+IV
+PerlIORaw_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
+{
+ /* Remove the dummy layer */
+ PerlIO_pop(f);
+ /* Pop back to bottom layer */
+ if (f && *f)
+ {
+ int code = 0;
+ PerlIO_flush(f);
+ while (!(PerlIOBase(f)->tab->kind & PERLIO_K_RAW))
+ {
+ if (*PerlIONext(f))
+ {
+ PerlIO_pop(f);
+ }
+ else
+ {
+ /* Nothing bellow - push unix on top then remove it */
+ if (PerlIO_push(f,PerlIO_default_btm(),mode,arg,len))
+ {
+ PerlIO_pop(PerlIONext(f));
+ }
+ break;
+ }
+ }
+ PerlIO_debug(":raw f=%p :%s\n",f,PerlIOBase(f)->tab->name);
+ return 0;
+ }
+ return -1;
+}
+
int
PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
{
const char *s = names;
while (*s)
{
- while (isSPACE(*s))
- s++;
- if (*s == ':')
+ while (isSPACE(*s) || *s == ':')
s++;
if (*s)
{
+ STRLEN llen = 0;
const char *e = s;
- while (*e && *e != ':' && !isSPACE(*e))
- e++;
- if (e > s)
+ const char *as = Nullch;
+ STRLEN alen = 0;
+ if (!isIDFIRST(*s))
+ {
+ /* Message is consistent with how attribute lists are passed.
+ Even though this means "foo : : bar" is seen as an invalid separator
+ character. */
+ char q = ((*s == '\'') ? '"' : '\'');
+ Perl_warn(aTHX_ "perlio: invalid separator character %c%c%c in layer specification list", q, *s, q);
+ return -1;
+ }
+ do
+ {
+ e++;
+ } while (isALNUM(*e));
+ llen = e-s;
+ if (*e == '(')
{
- if ((e - s) == 3 && strncmp(s,"raw",3) == 0)
+ int nesting = 1;
+ as = ++e;
+ while (nesting)
{
- /* Pop back to bottom layer */
- if (PerlIONext(f))
+ switch (*e++)
{
- PerlIO_flush(f);
- while (PerlIONext(f))
- {
- PerlIO_pop(f);
- }
+ case ')':
+ if (--nesting == 0)
+ alen = (e-1)-as;
+ break;
+ case '(':
+ ++nesting;
+ break;
+ case '\\':
+ /* It's a nul terminated string, not allowed to \ the terminating null.
+ Anything other character is passed over. */
+ if (*e++)
+ {
+ break;
+ }
+ /* Drop through */
+ case '\0':
+ e--;
+ Perl_warn(aTHX_ "perlio: argument list not closed for layer \"%.*s\"",(int)(e - s),s);
+ return -1;
+ default:
+ /* boring. */
+ break;
}
}
- else
+ }
+ if (e > s)
+ {
+ SV *layer = PerlIO_find_layer(s,llen);
+ if (layer)
{
- SV *layer = PerlIO_find_layer(s,e-s);
- if (layer)
+ PerlIO_funcs *tab = INT2PTR(PerlIO_funcs *, SvIV(SvRV(layer)));
+ if (tab)
{
- PerlIO_funcs *tab = INT2PTR(PerlIO_funcs *, SvIV(SvRV(layer)));
- if (tab)
- {
- PerlIO *new = PerlIO_push(f,tab,mode);
- if (!new)
- return -1;
- }
+ if (!PerlIO_push(f,tab,mode,as,alen))
+ return -1;
}
- else
- Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"",(int)(e-s),s);
}
+ else
+ Perl_warn(aTHX_ "perlio: unknown layer \"%.*s\"",(int)llen,s);
}
s = e;
}
int
PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
{
- PerlIO_debug(__FUNCTION__ " f=%p %s %c %x %s\n",
- f,PerlIOBase(f)->tab->name,iotype,mode, (names) ? names : "(Null)");
- if (!names || (O_TEXT != O_BINARY && mode & O_BINARY))
+ PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n",
+ f,PerlIOBase(f)->tab->name,iotype,mode, (names) ? names : "(Null)");
+ if (!names && (O_TEXT != O_BINARY && (mode & O_BINARY)))
{
PerlIO *top = f;
PerlIOl *l;
if (PerlIOBase(top)->tab == &PerlIO_crlf)
{
PerlIO_flush(top);
- PerlIOBase(top)->flags &= ~PERLIO_F_CRLF;
+ PerlIOBase(top)->flags &= ~PERLIO_F_CRLF;
break;
}
top = PerlIONext(top);
return (*PerlIOBase(f)->tab->Close)(f);
}
+#undef PerlIO_fdupopen
+PerlIO *
+PerlIO_fdupopen(pTHX_ PerlIO *f)
+{
+ char buf[8];
+ int fd = PerlLIO_dup(PerlIO_fileno(f));
+ PerlIO *new = PerlIO_fdopen(fd,PerlIO_modestr(f,buf));
+ if (new)
+ {
+ Off_t posn = PerlIO_tell(f);
+ PerlIO_seek(new,posn,SEEK_SET);
+ }
+ return new;
+}
#undef PerlIO_close
int
PerlIO_flush(f);
if ((*PerlIOBase(f)->tab->Reopen)(path,mode,f) == 0)
{
- if ((*PerlIOBase(f)->tab->Pushed)(f,mode) == 0)
+ if ((*PerlIOBase(f)->tab->Pushed)(f,mode,Nullch,0) == 0)
return f;
}
return NULL;
{
if (f)
{
- return (*PerlIOBase(f)->tab->Flush)(f);
+ PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ if (tab && tab->Flush)
+ {
+ return (*tab->Flush)(f);
+ }
+ else
+ {
+ PerlIO_debug("Cannot flush f=%p :%s\n",f,tab->name);
+ errno = EINVAL;
+ return -1;
+ }
}
else
{
int
PerlIO_fast_gets(PerlIO *f)
{
- if (f && *f)
+ if (f && *f && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS))
{
- PerlIOl *l = PerlIOBase(f);
- return (l->tab->Set_ptrcnt != NULL);
+ PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ return (tab->Set_ptrcnt != NULL);
}
return 0;
}
STDCHAR *
PerlIO_get_ptr(PerlIO *f)
{
- return (*PerlIOBase(f)->tab->Get_ptr)(f);
+ PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ if (tab->Get_ptr == NULL)
+ return NULL;
+ return (*tab->Get_ptr)(f);
}
#undef PerlIO_get_cnt
int
PerlIO_get_cnt(PerlIO *f)
{
- return (*PerlIOBase(f)->tab->Get_cnt)(f);
+ PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ if (tab->Get_cnt == NULL)
+ return 0;
+ return (*tab->Get_cnt)(f);
}
#undef PerlIO_set_cnt
void
PerlIO_set_ptrcnt(PerlIO *f, STDCHAR *ptr, int cnt)
{
+ PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ if (tab->Set_ptrcnt == NULL)
+ {
+ dTHX;
+ Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
+ }
(*PerlIOBase(f)->tab->Set_ptrcnt)(f,ptr,cnt);
}
/*--------------------------------------------------------------------------------------*/
+/* utf8 and raw dummy layers */
+
+IV
+PerlIOUtf8_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
+{
+ if (PerlIONext(f))
+ {
+ PerlIO_funcs *tab = PerlIOBase(f)->tab;
+ PerlIO_pop(f);
+ if (tab->kind & PERLIO_K_UTF8)
+ PerlIOBase(f)->flags |= PERLIO_F_UTF8;
+ else
+ PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
+ return 0;
+ }
+ return -1;
+}
+
+PerlIO *
+PerlIOUtf8_fdopen(PerlIO_funcs *self, int fd,const char *mode)
+{
+ PerlIO_funcs *tab = PerlIO_default_layer(-2);
+ PerlIO *f = (*tab->Fdopen)(tab,fd,mode);
+ if (f)
+ {
+ PerlIOl *l = PerlIOBase(f);
+ if (tab->kind & PERLIO_K_UTF8)
+ l->flags |= PERLIO_F_UTF8;
+ else
+ l->flags &= ~PERLIO_F_UTF8;
+ }
+ return f;
+}
+
+PerlIO *
+PerlIOUtf8_open(PerlIO_funcs *self, const char *path,const char *mode)
+{
+ PerlIO_funcs *tab = PerlIO_default_layer(-2);
+ PerlIO *f = (*tab->Open)(tab,path,mode);
+ if (f)
+ {
+ PerlIOl *l = PerlIOBase(f);
+ if (tab->kind & PERLIO_K_UTF8)
+ l->flags |= PERLIO_F_UTF8;
+ else
+ l->flags &= ~PERLIO_F_UTF8;
+ }
+ return f;
+}
+
+PerlIO_funcs PerlIO_utf8 = {
+ "utf8",
+ sizeof(PerlIOl),
+ PERLIO_K_DUMMY|PERLIO_F_UTF8,
+ NULL,
+ PerlIOUtf8_fdopen,
+ PerlIOUtf8_open,
+ NULL,
+ PerlIOUtf8_pushed,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL, /* flush */
+ NULL, /* fill */
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL, /* get_base */
+ NULL, /* get_bufsiz */
+ NULL, /* get_ptr */
+ NULL, /* get_cnt */
+ NULL, /* set_ptrcnt */
+};
+
+PerlIO_funcs PerlIO_byte = {
+ "bytes",
+ sizeof(PerlIOl),
+ PERLIO_K_DUMMY,
+ NULL,
+ PerlIOUtf8_fdopen,
+ PerlIOUtf8_open,
+ NULL,
+ PerlIOUtf8_pushed,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL, /* flush */
+ NULL, /* fill */
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL, /* get_base */
+ NULL, /* get_bufsiz */
+ NULL, /* get_ptr */
+ NULL, /* get_cnt */
+ NULL, /* set_ptrcnt */
+};
+
+PerlIO *
+PerlIORaw_fdopen(PerlIO_funcs *self, int fd,const char *mode)
+{
+ PerlIO_funcs *tab = PerlIO_default_btm();
+ return (*tab->Fdopen)(tab,fd,mode);
+}
+
+PerlIO *
+PerlIORaw_open(PerlIO_funcs *self, const char *path,const char *mode)
+{
+ PerlIO_funcs *tab = PerlIO_default_btm();
+ return (*tab->Open)(tab,path,mode);
+}
+
+PerlIO_funcs PerlIO_raw = {
+ "raw",
+ sizeof(PerlIOl),
+ PERLIO_K_DUMMY,
+ NULL,
+ PerlIORaw_fdopen,
+ PerlIORaw_open,
+ NULL,
+ PerlIORaw_pushed,
+ PerlIOBase_popped,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL, /* flush */
+ NULL, /* fill */
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL, /* get_base */
+ NULL, /* get_bufsiz */
+ NULL, /* get_ptr */
+ NULL, /* get_cnt */
+ NULL, /* set_ptrcnt */
+};
+/*--------------------------------------------------------------------------------------*/
+/*--------------------------------------------------------------------------------------*/
/* "Methods" of the "base class" */
IV
{
char *s = buf;
IV flags = PerlIOBase(f)->flags;
- if (flags & PERLIO_F_CANREAD)
- *s++ = 'r';
- if (flags & PERLIO_F_CANWRITE)
- *s++ = 'w';
- if (flags & PERLIO_F_CRLF)
- *s++ = 't';
- else
- *s++ = 'b';
+ if (flags & PERLIO_F_APPEND)
+ {
+ *s++ = 'a';
+ if (flags & PERLIO_F_CANREAD)
+ {
+ *s++ = '+';
+ }
+ }
+ else if (flags & PERLIO_F_CANREAD)
+ {
+ *s++ = 'r';
+ if (flags & PERLIO_F_CANWRITE)
+ *s++ = '+';
+ }
+ else if (flags & PERLIO_F_CANWRITE)
+ {
+ *s++ = 'w';
+ if (flags & PERLIO_F_CANREAD)
+ {
+ *s++ = '+';
+ }
+ }
+#if O_TEXT != O_BINARY
+ if (!(flags & PERLIO_F_CRLF))
+ *s++ = 'b';
+#endif
*s = '\0';
return buf;
}
IV
-PerlIOBase_pushed(PerlIO *f, const char *mode)
+PerlIOBase_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
{
PerlIOl *l = PerlIOBase(f);
const char *omode = mode;
char temp[8];
+ PerlIO_funcs *tab = PerlIOBase(f)->tab;
l->flags &= ~(PERLIO_F_CANREAD|PERLIO_F_CANWRITE|
PERLIO_F_TRUNCATE|PERLIO_F_APPEND);
+ if (tab->Set_ptrcnt != NULL)
+ l->flags |= PERLIO_F_FASTGETS;
if (mode)
{
switch (*mode++)
(PERLIO_F_CANREAD|PERLIO_F_CANWRITE|PERLIO_F_TRUNCATE|PERLIO_F_APPEND);
}
}
- PerlIO_debug(__FUNCTION__ " f=%p %s %s fl=%08x (%s)\n",
+#if 0
+ PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08"UVxf" (%s)\n",
f,PerlIOBase(f)->tab->name,(omode) ? omode : "(Null)",
- l->flags,PerlIO_modestr(f,temp));
+ l->flags,PerlIO_modestr(f,temp));
+#endif
return 0;
}
PerlIOBase_unread(PerlIO *f, const void *vbuf, Size_t count)
{
Off_t old = PerlIO_tell(f);
- if (PerlIO_seek(f,-((Off_t)count),SEEK_CUR) == 0)
- {
- Off_t new = PerlIO_tell(f);
- return old - new;
- }
- return 0;
+ SSize_t done;
+ PerlIO_push(f,&PerlIO_pending,"r",Nullch,0);
+ done = PerlIOBuf_unread(f,vbuf,count);
+ PerlIOSelf(f,PerlIOBuf)->posn = old - done;
+ return done;
}
IV
return PerlIOSelf(f,PerlIOUnix)->fd;
}
+IV
+PerlIOUnix_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
+{
+ IV code = PerlIOBase_pushed(f,mode,arg,len);
+ if (*PerlIONext(f))
+ {
+ PerlIOUnix *s = PerlIOSelf(f,PerlIOUnix);
+ s->fd = PerlIO_fileno(PerlIONext(f));
+ s->oflags = PerlIOUnix_oflags(mode);
+ }
+ PerlIOBase(f)->flags |= PERLIO_F_OPEN;
+ return code;
+}
+
PerlIO *
PerlIOUnix_fdopen(PerlIO_funcs *self, int fd,const char *mode)
{
+ dTHX;
PerlIO *f = NULL;
if (*mode == 'I')
mode++;
int oflags = PerlIOUnix_oflags(mode);
if (oflags != -1)
{
- PerlIOUnix *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(),self,mode),PerlIOUnix);
+ PerlIOUnix *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),self,mode,Nullch,0),PerlIOUnix);
s->fd = fd;
s->oflags = oflags;
- PerlIOBase(f)->flags |= PERLIO_F_OPEN;
}
}
return f;
PerlIO *
PerlIOUnix_open(PerlIO_funcs *self, const char *path,const char *mode)
{
+ dTHX;
PerlIO *f = NULL;
int oflags = PerlIOUnix_oflags(mode);
if (oflags != -1)
int fd = PerlLIO_open3(path,oflags,0666);
if (fd >= 0)
{
- PerlIOUnix *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(),self,mode),PerlIOUnix);
+ PerlIOUnix *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),self,mode,Nullch,0),PerlIOUnix);
s->fd = fd;
s->oflags = oflags;
PerlIOBase(f)->flags |= PERLIO_F_OPEN;
(*PerlIOBase(f)->tab->Close)(f);
if (oflags != -1)
{
+ dTHX;
int fd = PerlLIO_open3(path,oflags,0666);
if (fd >= 0)
{
SSize_t
PerlIOUnix_read(PerlIO *f, void *vbuf, Size_t count)
{
+ dTHX;
int fd = PerlIOSelf(f,PerlIOUnix)->fd;
if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
return 0;
PerlIOBase(f)->flags |= PERLIO_F_EOF;
return len;
}
+ PERL_ASYNC_CHECK();
}
}
SSize_t
PerlIOUnix_write(PerlIO *f, const void *vbuf, Size_t count)
{
+ dTHX;
int fd = PerlIOSelf(f,PerlIOUnix)->fd;
while (1)
{
PerlIOBase(f)->flags |= PERLIO_F_ERROR;
return len;
}
+ PERL_ASYNC_CHECK();
}
}
IV
PerlIOUnix_seek(PerlIO *f, Off_t offset, int whence)
{
+ dTHX;
Off_t new = PerlLIO_lseek(PerlIOSelf(f,PerlIOUnix)->fd,offset,whence);
PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
return (new == (Off_t) -1) ? -1 : 0;
Off_t
PerlIOUnix_tell(PerlIO *f)
{
+ dTHX;
+ Off_t posn = PerlLIO_lseek(PerlIOSelf(f,PerlIOUnix)->fd,0,SEEK_CUR);
return PerlLIO_lseek(PerlIOSelf(f,PerlIOUnix)->fd,0,SEEK_CUR);
}
IV
PerlIOUnix_close(PerlIO *f)
{
+ dTHX;
int fd = PerlIOSelf(f,PerlIOUnix)->fd;
int code = 0;
while (PerlLIO_close(fd) != 0)
code = -1;
break;
}
+ PERL_ASYNC_CHECK();
}
if (code == 0)
{
PerlIOUnix_fdopen,
PerlIOUnix_open,
PerlIOUnix_reopen,
- PerlIOBase_pushed,
+ PerlIOUnix_pushed,
PerlIOBase_noop_ok,
PerlIOUnix_read,
PerlIOBase_unread,
IV
PerlIOStdio_fileno(PerlIO *f)
{
- return fileno(PerlIOSelf(f,PerlIOStdio)->stdio);
+ dTHX;
+ return PerlSIO_fileno(PerlIOSelf(f,PerlIOStdio)->stdio);
}
-const char *
+char *
PerlIOStdio_mode(const char *mode,char *tmode)
{
- const char *ret = mode;
+ char *ret = tmode;
+ while (*mode)
+ {
+ *tmode++ = *mode++;
+ }
if (O_BINARY != O_TEXT)
{
- ret = (const char *) tmode;
- while (*mode)
- {
- *tmode++ = *mode++;
- }
*tmode++ = 'b';
- *tmode = '\0';
}
+ *tmode = '\0';
return ret;
}
PerlIO *
PerlIOStdio_fdopen(PerlIO_funcs *self, int fd,const char *mode)
{
+ dTHX;
PerlIO *f = NULL;
int init = 0;
char tmode[8];
switch(fd)
{
case 0:
- stdio = stdin;
+ stdio = PerlSIO_stdin;
break;
case 1:
- stdio = stdout;
+ stdio = PerlSIO_stdout;
break;
case 2:
- stdio = stderr;
+ stdio = PerlSIO_stderr;
break;
}
}
else
{
- stdio = fdopen(fd,mode = PerlIOStdio_mode(mode,tmode));
+ stdio = PerlSIO_fdopen(fd,mode = PerlIOStdio_mode(mode,tmode));
}
if (stdio)
{
- PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(),self,mode),PerlIOStdio);
+ PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),self,mode,Nullch,0),PerlIOStdio);
s->stdio = stdio;
}
}
return f;
}
+/* This isn't used yet ... */
+IV
+PerlIOStdio_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
+{
+ if (*PerlIONext(f))
+ {
+ PerlIOStdio *s = PerlIOSelf(f,PerlIOStdio);
+ char tmode[8];
+ FILE *stdio = PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)),mode = PerlIOStdio_mode(mode,tmode));
+ if (stdio)
+ s->stdio = stdio;
+ else
+ return -1;
+ }
+ return PerlIOBase_pushed(f,mode,arg,len);
+}
+
#undef PerlIO_importFILE
PerlIO *
PerlIO_importFILE(FILE *stdio, int fl)
{
+ dTHX;
PerlIO *f = NULL;
if (stdio)
{
- PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(),&PerlIO_stdio,"r+"),PerlIOStdio);
+ PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),&PerlIO_stdio,"r+",Nullch,0),PerlIOStdio);
s->stdio = stdio;
}
return f;
PerlIO *
PerlIOStdio_open(PerlIO_funcs *self, const char *path,const char *mode)
{
+ dTHX;
PerlIO *f = NULL;
- FILE *stdio = fopen(path,mode);
+ FILE *stdio = PerlSIO_fopen(path,mode);
if (stdio)
{
char tmode[8];
- PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(), self,
- (mode = PerlIOStdio_mode(mode,tmode))),
+ PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX), self,
+ (mode = PerlIOStdio_mode(mode,tmode)),Nullch,0),
PerlIOStdio);
s->stdio = stdio;
}
int
PerlIOStdio_reopen(const char *path, const char *mode, PerlIO *f)
{
+ dTHX;
PerlIOStdio *s = PerlIOSelf(f,PerlIOStdio);
char tmode[8];
- FILE *stdio = freopen(path,(mode = PerlIOStdio_mode(mode,tmode)),s->stdio);
+ FILE *stdio = PerlSIO_freopen(path,(mode = PerlIOStdio_mode(mode,tmode)),s->stdio);
if (!s->stdio)
return -1;
s->stdio = stdio;
SSize_t
PerlIOStdio_read(PerlIO *f, void *vbuf, Size_t count)
{
+ dTHX;
FILE *s = PerlIOSelf(f,PerlIOStdio)->stdio;
SSize_t got = 0;
if (count == 1)
/* Perl is expecting PerlIO_getc() to fill the buffer
* Linux's stdio does not do that for fread()
*/
- int ch = fgetc(s);
+ int ch = PerlSIO_fgetc(s);
if (ch != EOF)
{
*buf = ch;
}
}
else
- got = fread(vbuf,1,count,s);
+ got = PerlSIO_fread(vbuf,1,count,s);
return got;
}
SSize_t
PerlIOStdio_unread(PerlIO *f, const void *vbuf, Size_t count)
{
+ dTHX;
FILE *s = PerlIOSelf(f,PerlIOStdio)->stdio;
STDCHAR *buf = ((STDCHAR *)vbuf)+count-1;
SSize_t unread = 0;
while (count > 0)
{
int ch = *buf-- & 0xff;
- if (ungetc(ch,s) != ch)
+ if (PerlSIO_ungetc(ch,s) != ch)
break;
unread++;
count--;
SSize_t
PerlIOStdio_write(PerlIO *f, const void *vbuf, Size_t count)
{
- return fwrite(vbuf,1,count,PerlIOSelf(f,PerlIOStdio)->stdio);
+ dTHX;
+ return PerlSIO_fwrite(vbuf,1,count,PerlIOSelf(f,PerlIOStdio)->stdio);
}
IV
PerlIOStdio_seek(PerlIO *f, Off_t offset, int whence)
{
+ dTHX;
FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
- return fseek(stdio,offset,whence);
+ return PerlSIO_fseek(stdio,offset,whence);
}
Off_t
PerlIOStdio_tell(PerlIO *f)
{
+ dTHX;
FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
- return ftell(stdio);
+ return PerlSIO_ftell(stdio);
}
IV
PerlIOStdio_close(PerlIO *f)
{
+ dTHX;
+#ifdef HAS_SOCKET
+ int optval, optlen = sizeof(int);
+#endif
FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
- return fclose(stdio);
+ return(
+#ifdef HAS_SOCKET
+ (getsockopt(PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (char *)&optval, &optlen) < 0) ?
+ PerlSIO_fclose(stdio) :
+ close(PerlIO_fileno(f))
+#else
+ PerlSIO_fclose(stdio)
+#endif
+ );
+
}
IV
PerlIOStdio_flush(PerlIO *f)
{
+ dTHX;
FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE)
{
- return fflush(stdio);
+ return PerlSIO_fflush(stdio);
}
else
{
*/
/* Not writeable - sync by attempting a seek */
int err = errno;
- if (fseek(stdio,(Off_t) 0, SEEK_CUR) != 0)
+ if (PerlSIO_fseek(stdio,(Off_t) 0, SEEK_CUR) != 0)
errno = err;
#endif
}
IV
PerlIOStdio_fill(PerlIO *f)
{
+ dTHX;
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 (fflush(stdio) != 0)
+ if (PerlSIO_fflush(stdio) != 0)
return EOF;
}
- c = fgetc(stdio);
- if (c == EOF || ungetc(c,stdio) != c)
+ c = PerlSIO_fgetc(stdio);
+ if (c == EOF || PerlSIO_ungetc(c,stdio) != c)
return EOF;
return 0;
}
IV
PerlIOStdio_eof(PerlIO *f)
{
- return feof(PerlIOSelf(f,PerlIOStdio)->stdio);
+ dTHX;
+ return PerlSIO_feof(PerlIOSelf(f,PerlIOStdio)->stdio);
}
IV
PerlIOStdio_error(PerlIO *f)
{
- return ferror(PerlIOSelf(f,PerlIOStdio)->stdio);
+ dTHX;
+ return PerlSIO_ferror(PerlIOSelf(f,PerlIOStdio)->stdio);
}
void
PerlIOStdio_clearerr(PerlIO *f)
{
- clearerr(PerlIOSelf(f,PerlIOStdio)->stdio);
+ dTHX;
+ PerlSIO_clearerr(PerlIOSelf(f,PerlIOStdio)->stdio);
}
void
PerlIOStdio_setlinebuf(PerlIO *f)
{
+ dTHX;
#ifdef HAS_SETLINEBUF
- setlinebuf(PerlIOSelf(f,PerlIOStdio)->stdio);
+ PerlSIO_setlinebuf(PerlIOSelf(f,PerlIOStdio)->stdio);
#else
- setvbuf(PerlIOSelf(f,PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
+ PerlSIO_setvbuf(PerlIOSelf(f,PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
#endif
}
STDCHAR *
PerlIOStdio_get_base(PerlIO *f)
{
+ dTHX;
FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
- return FILE_base(stdio);
+ return PerlSIO_get_base(stdio);
}
Size_t
PerlIOStdio_get_bufsiz(PerlIO *f)
{
+ dTHX;
FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
- return FILE_bufsiz(stdio);
+ return PerlSIO_get_bufsiz(stdio);
}
#endif
STDCHAR *
PerlIOStdio_get_ptr(PerlIO *f)
{
+ dTHX;
FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
- return FILE_ptr(stdio);
+ return PerlSIO_get_ptr(stdio);
}
SSize_t
PerlIOStdio_get_cnt(PerlIO *f)
{
+ dTHX;
FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
- return FILE_cnt(stdio);
+ return PerlSIO_get_cnt(stdio);
}
void
PerlIOStdio_set_ptrcnt(PerlIO *f,STDCHAR *ptr,SSize_t cnt)
{
+ dTHX;
FILE *stdio = PerlIOSelf(f,PerlIOStdio)->stdio;
if (ptr != NULL)
{
#ifdef STDIO_PTR_LVALUE
- FILE_ptr(stdio) = ptr;
+ PerlSIO_set_ptr(stdio,ptr);
#ifdef STDIO_PTR_LVAL_SETS_CNT
- if (FILE_cnt(stdio) != (cnt))
+ if (PerlSIO_get_cnt(stdio) != (cnt))
{
dTHX;
- assert(FILE_cnt(stdio) == (cnt));
+ assert(PerlSIO_get_cnt(stdio) == (cnt));
}
#endif
#if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
return;
#endif
#else /* STDIO_PTR_LVALUE */
- abort();
+ PerlProc_abort();
#endif /* STDIO_PTR_LVALUE */
}
/* Now (or only) set cnt */
#ifdef STDIO_CNT_LVALUE
- FILE_cnt(stdio) = cnt;
+ PerlSIO_set_cnt(stdio,cnt);
#else /* STDIO_CNT_LVALUE */
#if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
- FILE_ptr(stdio) = FILE_ptr(stdio)+(FILE_cnt(stdio)-cnt);
+ PerlSIO_set_ptr(stdio,PerlSIO_get_ptr(stdio)+(PerlSIO_get_cnt(stdio)-cnt));
#else /* STDIO_PTR_LVAL_SETS_CNT */
- abort();
+ PerlProc_abort();
#endif /* STDIO_PTR_LVAL_SETS_CNT */
#endif /* STDIO_CNT_LVALUE */
}
FILE *
PerlIO_exportFILE(PerlIO *f, int fl)
{
+ FILE *stdio;
PerlIO_flush(f);
- /* Should really push stdio discipline when we have them */
- return fdopen(PerlIO_fileno(f),"r+");
+ stdio = fdopen(PerlIO_fileno(f),"r+");
+ if (stdio)
+ {
+ PerlIOStdio *s = PerlIOSelf(PerlIO_push(f,&PerlIO_stdio,"r+",Nullch,0),PerlIOStdio);
+ s->stdio = stdio;
+ }
+ return stdio;
}
#undef PerlIO_findFILE
FILE *
PerlIO_findFILE(PerlIO *f)
{
+ PerlIOl *l = *f;
+ while (l)
+ {
+ if (l->tab == &PerlIO_stdio)
+ {
+ PerlIOStdio *s = PerlIOSelf(&l,PerlIOStdio);
+ return s->stdio;
+ }
+ l = *PerlIONext(&l);
+ }
return PerlIO_exportFILE(f,0);
}
/*--------------------------------------------------------------------------------------*/
/* perlio buffer layer */
+IV
+PerlIOBuf_pushed(PerlIO *f, const char *mode, const char *arg, STRLEN len)
+{
+ PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
+ b->posn = PerlIO_tell(PerlIONext(f));
+ return PerlIOBase_pushed(f,mode,arg,len);
+}
+
PerlIO *
PerlIOBuf_fdopen(PerlIO_funcs *self, int fd, const char *mode)
{
+ dTHX;
PerlIO_funcs *tab = PerlIO_default_btm();
int init = 0;
PerlIO *f;
init = 1;
mode++;
}
- if (O_BINARY != O_TEXT)
- {
- int code = PerlLIO_setmode(fd, O_BINARY);
- PerlIO_debug(__FUNCTION__ " %s fd=%d m=%s c=%d\n",tab->name,fd,mode,code);
- }
+#if O_BINARY != O_TEXT
+ /* do something about failing setmode()? --jhi */
+ PerlLIO_setmode(fd, O_BINARY);
+#endif
f = (*tab->Fdopen)(tab,fd,mode);
if (f)
{
- PerlIOBuf *b = PerlIOSelf(PerlIO_push(f,self,mode),PerlIOBuf);
- b->posn = PerlIO_tell(PerlIONext(f));
+ PerlIOBuf *b = PerlIOSelf(PerlIO_push(f,self,mode,Nullch,0),PerlIOBuf);
if (init && fd == 2)
{
/* Initial stderr is unbuffered */
PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
- }
- PerlIO_debug(__FUNCTION__ " %s f=%p fd=%d m=%s fl=%08x\n",
+ }
+#if 0
+ PerlIO_debug("PerlIOBuf_fdopen %s f=%p fd=%d m=%s fl=%08"UVxf"\n",
self->name,f,fd,mode,PerlIOBase(f)->flags);
+#endif
}
return f;
}
PerlIO *f = (*tab->Open)(tab,path,mode);
if (f)
{
- PerlIOBuf *b = PerlIOSelf(PerlIO_push(f,self,mode),PerlIOBuf);
- b->posn = PerlIO_tell(PerlIONext(f));
+ PerlIO_push(f,self,mode,Nullch,0);
}
return f;
}
PerlIO *next = PerlIONext(f);
int code = (*PerlIOBase(next)->tab->Reopen)(path,mode,next);
if (code = 0)
- code = (*PerlIOBase(f)->tab->Pushed)(f,mode);
- if (code == 0)
- {
- PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
- b->posn = PerlIO_tell(PerlIONext(f));
- }
+ code = (*PerlIOBase(f)->tab->Pushed)(f,mode,Nullch,0);
return code;
}
if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
{
/* write() the buffer */
- STDCHAR *p = b->buf;
- int count;
+ STDCHAR *buf = b->buf;
+ STDCHAR *p = buf;
PerlIO *n = PerlIONext(f);
while (p < b->ptr)
{
- count = PerlIO_write(n,p,b->ptr - p);
+ SSize_t count = PerlIO_write(n,p,b->ptr - p);
if (count > 0)
{
p += count;
break;
}
}
- b->posn += (p - b->buf);
+ b->posn += (p - buf);
}
else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
{
+ STDCHAR *buf = PerlIO_get_base(f);
/* Note position change */
- b->posn += (b->ptr - b->buf);
+ b->posn += (b->ptr - buf);
if (b->ptr < b->end)
{
/* We did not consume all of it */
if (PerlIO_flush(f) != 0)
return -1;
+ if (!b->buf)
+ PerlIO_get_base(f); /* allocate via vtable */
+
b->ptr = b->end = b->buf;
if (PerlIO_fast_gets(n))
{
if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
{
avail = (b->ptr - b->buf);
- if (avail > (SSize_t) count)
- avail = count;
- b->ptr -= avail;
}
else
{
avail = b->bufsiz;
- if (avail > (SSize_t) count)
- avail = count;
- b->end = b->ptr + avail;
+ b->end = b->buf + avail;
+ b->ptr = b->end;
+ PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
+ b->posn -= b->bufsiz;
}
+ if (avail > (SSize_t) count)
+ avail = count;
if (avail > 0)
{
+ b->ptr -= avail;
buf -= avail;
if (buf != b->ptr)
{
IV
PerlIOBuf_seek(PerlIO *f, Off_t offset, int whence)
{
- PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
- int code = PerlIO_flush(f);
- if (code == 0)
+ IV code;
+ if ((code = PerlIO_flush(f)) == 0)
{
+ PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
code = PerlIO_seek(PerlIONext(f),offset,whence);
if (code == 0)
IV
PerlIOBuf_close(PerlIO *f)
{
+ dTHX;
IV code = PerlIOBase_close(f);
PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
if (b->buf && b->buf != (STDCHAR *) &b->oneword)
{
- Safefree(b->buf);
+ PerlMemShared_free(b->buf);
}
b->buf = NULL;
b->ptr = b->end = b->buf;
PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
if (!b->buf)
{
+ dTHX;
if (!b->bufsiz)
b->bufsiz = 4096;
- New('B',b->buf,b->bufsiz,STDCHAR);
+ b->buf = PerlMemShared_calloc(b->bufsiz,sizeof(STDCHAR));
if (!b->buf)
{
b->buf = (STDCHAR *)&b->oneword;
PerlIOBuf_fdopen,
PerlIOBuf_open,
PerlIOBuf_reopen,
- PerlIOBase_pushed,
+ PerlIOBuf_pushed,
PerlIOBase_noop_ok,
PerlIOBuf_read,
PerlIOBuf_unread,
};
/*--------------------------------------------------------------------------------------*/
+/* Temp layer to hold unread chars when cannot do it any other way */
+
+IV
+PerlIOPending_fill(PerlIO *f)
+{
+ /* Should never happen */
+ PerlIO_flush(f);
+ return 0;
+}
+
+IV
+PerlIOPending_close(PerlIO *f)
+{
+ /* A tad tricky - flush pops us, then we close new top */
+ PerlIO_flush(f);
+ return PerlIO_close(f);
+}
+
+IV
+PerlIOPending_seek(PerlIO *f, Off_t offset, int whence)
+{
+ /* A tad tricky - flush pops us, then we seek new top */
+ PerlIO_flush(f);
+ return PerlIO_seek(f,offset,whence);
+}
+
+
+IV
+PerlIOPending_flush(PerlIO *f)
+{
+ PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
+ if (b->buf && b->buf != (STDCHAR *) &b->oneword)
+ {
+ dTHX;
+ PerlMemShared_free(b->buf);
+ b->buf = NULL;
+ }
+ PerlIO_pop(f);
+ return 0;
+}
+
+void
+PerlIOPending_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t cnt)
+{
+ if (cnt <= 0)
+ {
+ PerlIO_flush(f);
+ }
+ else
+ {
+ PerlIOBuf_set_ptrcnt(f,ptr,cnt);
+ }
+}
+
+IV
+PerlIOPending_pushed(PerlIO *f,const char *mode,const char *arg,STRLEN len)
+{
+ IV code = PerlIOBase_pushed(f,mode,arg,len);
+ PerlIOl *l = PerlIOBase(f);
+ /* Our PerlIO_fast_gets must match what we are pushed on,
+ or sv_gets() etc. get muddled when it changes mid-string
+ when we auto-pop.
+ */
+ l->flags = (l->flags & ~(PERLIO_F_FASTGETS|PERLIO_F_UTF8)) |
+ (PerlIOBase(PerlIONext(f))->flags & (PERLIO_F_FASTGETS|PERLIO_F_UTF8));
+ return code;
+}
+
+SSize_t
+PerlIOPending_read(PerlIO *f, void *vbuf, Size_t count)
+{
+ SSize_t avail = PerlIO_get_cnt(f);
+ SSize_t got = 0;
+ if (count < avail)
+ avail = count;
+ if (avail > 0)
+ got = PerlIOBuf_read(f,vbuf,avail);
+ if (got < count)
+ got += PerlIO_read(f,((STDCHAR *) vbuf)+got,count-got);
+ return got;
+}
+
+
+PerlIO_funcs PerlIO_pending = {
+ "pending",
+ sizeof(PerlIOBuf),
+ PERLIO_K_BUFFERED,
+ PerlIOBase_fileno,
+ NULL,
+ NULL,
+ NULL,
+ PerlIOPending_pushed,
+ PerlIOBase_noop_ok,
+ PerlIOPending_read,
+ PerlIOBuf_unread,
+ PerlIOBuf_write,
+ PerlIOPending_seek,
+ PerlIOBuf_tell,
+ PerlIOPending_close,
+ PerlIOPending_flush,
+ PerlIOPending_fill,
+ PerlIOBase_eof,
+ PerlIOBase_error,
+ PerlIOBase_clearerr,
+ PerlIOBuf_setlinebuf,
+ PerlIOBuf_get_base,
+ PerlIOBuf_bufsiz,
+ PerlIOBuf_get_ptr,
+ PerlIOBuf_get_cnt,
+ PerlIOPending_set_ptrcnt,
+};
+
+
+
+/*--------------------------------------------------------------------------------------*/
/* crlf - translation
On read translate CR,LF to "\n" we do this by overriding ptr/cnt entries
to hand back a line at a time and keeping a record of which nl we "lied" about.
} PerlIOCrlf;
IV
-PerlIOCrlf_pushed(PerlIO *f, const char *mode)
+PerlIOCrlf_pushed(PerlIO *f, const char *mode,const char *arg,STRLEN len)
{
IV code;
PerlIOBase(f)->flags |= PERLIO_F_CRLF;
- code = PerlIOBase_pushed(f,mode);
- PerlIO_debug(__FUNCTION__ " f=%p %s %s fl=%08x\n",
+ code = PerlIOBuf_pushed(f,mode,arg,len);
+#if 0
+ PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08"UVxf"\n",
f,PerlIOBase(f)->tab->name,(mode) ? mode : "(Null)",
- PerlIOBase(f)->flags);
+ PerlIOBase(f)->flags);
+#endif
return code;
}
return PerlIOBuf_unread(f,vbuf,count);
else
{
- const STDCHAR *buf = (const STDCHAR *) vbuf+count;
+ const STDCHAR *buf = (const STDCHAR *) vbuf+count;
PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
SSize_t unread = 0;
if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
{
b->end = b->ptr = b->buf + b->bufsiz;
PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
+ b->posn -= b->bufsiz;
}
while (count > 0 && b->ptr > b->buf)
{
chk--;
}
chk -= cnt;
-
+
if (ptr != chk)
{
dTHX;
- Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08x nl=%p e=%p for %d",
- ptr, chk, flags, c->nl, b->end, cnt);
+ Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08"UVxf" nl=%p e=%p for %d",
+ ptr, chk, flags, c->nl, b->end, cnt);
}
}
if (c->nl)
return PerlIOBuf_write(f,vbuf,count);
else
{
- PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
+ PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
const STDCHAR *buf = (const STDCHAR *) vbuf;
const STDCHAR *ebuf = buf+count;
if (!b->buf)
}
posn = (b->posn / page_size) * page_size;
len = st.st_size - posn;
- m->mptr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, posn);
+ m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
if (m->mptr && m->mptr != (Mmap_t) -1)
{
-#if defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
+#if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
madvise(m->mptr, len, MADV_SEQUENTIAL);
#endif
+#if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
+ madvise(m->mptr, len, MADV_WILLNEED);
+#endif
PerlIOBase(f)->flags = (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
b->end = ((STDCHAR *)m->mptr) + len;
b->buf = ((STDCHAR *)m->mptr) + (b->posn - posn);
m->bbuf = b->buf;
}
}
- return PerlIOBuf_unread(f,vbuf,count);
+return PerlIOBuf_unread(f,vbuf,count);
}
SSize_t
PerlIOBuf_fdopen,
PerlIOBuf_open,
PerlIOBuf_reopen,
- PerlIOBase_pushed,
+ PerlIOBuf_pushed,
PerlIOBase_noop_ok,
PerlIOBuf_read,
PerlIOMmap_unread,
{
if (!_perlio)
{
+#ifndef WIN32
atexit(&PerlIO_cleanup);
+#endif
}
}
+
+
#undef PerlIO_stdin
PerlIO *
PerlIO_stdin(void)
{
/* I have no idea how portable mkstemp() is ... */
#if defined(WIN32) || !defined(HAVE_MKSTEMP)
+ dTHX;
PerlIO *f = NULL;
- FILE *stdio = tmpfile();
+ FILE *stdio = PerlSIO_tmpfile();
if (stdio)
{
- PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(),&PerlIO_stdio,"w+"),PerlIOStdio);
+ PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),&PerlIO_stdio,"w+",Nullch,0),PerlIOStdio);
s->stdio = stdio;
}
return f;
#ifndef HAS_FSETPOS
#undef PerlIO_setpos
int
-PerlIO_setpos(PerlIO *f, const Fpos_t *pos)
+PerlIO_setpos(PerlIO *f, SV *pos)
{
- return PerlIO_seek(f,*pos,0);
+ dTHX;
+ if (SvOK(pos))
+ {
+ STRLEN len;
+ Off_t *posn = (Off_t *) SvPV(pos,len);
+ if (f && len == sizeof(Off_t))
+ return PerlIO_seek(f,*posn,SEEK_SET);
+ }
+ errno = EINVAL;
+ return -1;
}
#else
-#ifndef PERLIO_IS_STDIO
#undef PerlIO_setpos
int
-PerlIO_setpos(PerlIO *f, const Fpos_t *pos)
+PerlIO_setpos(PerlIO *f, SV *pos)
{
+ dTHX;
+ if (SvOK(pos))
+ {
+ STRLEN len;
+ Fpos_t *fpos = (Fpos_t *) SvPV(pos,len);
+ if (f && len == sizeof(Fpos_t))
+ {
#if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
- return fsetpos64(f, pos);
+ return fsetpos64(f, fpos);
#else
- return fsetpos(f, pos);
+ return fsetpos(f, fpos);
#endif
+ }
+ }
+ errno = EINVAL;
+ return -1;
}
#endif
-#endif
#ifndef HAS_FGETPOS
#undef PerlIO_getpos
int
-PerlIO_getpos(PerlIO *f, Fpos_t *pos)
+PerlIO_getpos(PerlIO *f, SV *pos)
{
- *pos = PerlIO_tell(f);
- return *pos == -1 ? -1 : 0;
+ dTHX;
+ Off_t posn = PerlIO_tell(f);
+ sv_setpvn(pos,(char *)&posn,sizeof(posn));
+ return (posn == (Off_t)-1) ? -1 : 0;
}
#else
-#ifndef PERLIO_IS_STDIO
#undef PerlIO_getpos
int
-PerlIO_getpos(PerlIO *f, Fpos_t *pos)
+PerlIO_getpos(PerlIO *f, SV *pos)
{
+ dTHX;
+ Fpos_t fpos;
+ int code;
#if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
- return fgetpos64(f, pos);
+ code = fgetpos64(f, &fpos);
#else
- return fgetpos(f, pos);
+ code = fgetpos(f, &fpos);
#endif
+ sv_setpvn(pos,(char *)&fpos,sizeof(fpos));
+ return code;
}
#endif
-#endif
#if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
}
#endif
-#endif /* !PERL_IMPLICIT_SYS */