--- /dev/null
+#define PERL_NO_GET_CONTEXT
+#include "EXTERN.h"
+#include "perl.h"
+#include "XSUB.h"
+#ifdef PERLIO_LAYERS
+
+#include "perliol.h"
+
+typedef struct
+{
+ struct _PerlIO base; /* Base "class" info */
+ SV * var;
+ Off_t posn;
+} PerlIOScalar;
+
+IV
+PerlIOScalar_pushed(PerlIO *f, const char *mode, SV *arg)
+{
+ PerlIOScalar *b = PerlIOSelf(f,PerlIOScalar);
+ return PerlIOBase_pushed(f,mode,arg);
+}
+
+IV
+PerlIOScalar_popped(PerlIO *f)
+{
+ PerlIOScalar *s = PerlIOSelf(f,PerlIOScalar);
+ if (s->var)
+ {
+ dTHX;
+ SvREFCNT_dec(s->var);
+ s->var = Nullsv;
+ }
+ return 0;
+}
+
+IV
+PerlIOScalar_close(PerlIO *f)
+{
+ dTHX;
+ IV code = PerlIOBase_close(f);
+ PerlIOScalar *s = PerlIOSelf(f,PerlIOScalar);
+ PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF|PERLIO_F_WRBUF);
+ return code;
+}
+
+IV
+PerlIOScalar_fileno(PerlIO *f)
+{
+ return -1;
+}
+
+IV
+PerlIOScalar_seek(PerlIO *f, Off_t offset, int whence)
+{
+ PerlIOScalar *s = PerlIOSelf(f,PerlIOScalar);
+ switch(whence)
+ {
+ case 0:
+ s->posn = offset;
+ break;
+ case 1:
+ s->posn = offset + s->posn;
+ break;
+ case 2:
+ s->posn = offset + SvCUR(s->var);
+ break;
+ }
+ if (s->posn > SvCUR(s->var))
+ {
+ dTHX;
+ (void) SvGROW(s->var,s->posn);
+ }
+ return 0;
+}
+
+Off_t
+PerlIOScalar_tell(PerlIO *f)
+{
+ PerlIOScalar *s = PerlIOSelf(f,PerlIOScalar);
+ return s->posn;
+}
+
+SSize_t
+PerlIOScalar_unread(PerlIO *f, const void *vbuf, Size_t count)
+{
+ dTHX;
+ PerlIOScalar *s = PerlIOSelf(f,PerlIOScalar);
+ char *dst = SvGROW(s->var,s->posn+count);
+ Move(vbuf,dst,count,char);
+ s->posn += count;
+ SvCUR_set(s->var,s->posn);
+ SvPOK_on(s->var);
+ return count;
+}
+
+SSize_t
+PerlIOScalar_write(PerlIO *f, const void *vbuf, Size_t count)
+{
+ if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE)
+ {
+ return PerlIOScalar_unread(f,vbuf,count);
+ }
+ return 0;
+}
+
+IV
+PerlIOScalar_fill(PerlIO *f)
+{
+ return -1;
+}
+
+IV
+PerlIOScalar_flush(PerlIO *f)
+{
+ return 0;
+}
+
+STDCHAR *
+PerlIOScalar_get_base(PerlIO *f)
+{
+ PerlIOScalar *s = PerlIOSelf(f,PerlIOScalar);
+ if (PerlIOBase(f)->flags & PERLIO_F_CANREAD)
+ {
+ dTHX;
+ return (STDCHAR *)SvPV_nolen(s->var);
+ }
+}
+
+STDCHAR *
+PerlIOScalar_get_ptr(PerlIO *f)
+{
+ if (PerlIOBase(f)->flags & PERLIO_F_CANREAD)
+ {
+ PerlIOScalar *s = PerlIOSelf(f,PerlIOScalar);
+ return PerlIOScalar_get_base(f)+s->posn;
+ }
+ return (STDCHAR *) Nullch;
+}
+
+SSize_t
+PerlIOScalar_get_cnt(PerlIO *f)
+{
+ if (PerlIOBase(f)->flags & PERLIO_F_CANREAD)
+ {
+ PerlIOScalar *s = PerlIOSelf(f,PerlIOScalar);
+ return SvCUR(s->var) - s->posn;
+ }
+ return 0;
+}
+
+Size_t
+PerlIOScalar_bufsiz(PerlIO *f)
+{
+ if (PerlIOBase(f)->flags & PERLIO_F_CANREAD)
+ {
+ PerlIOScalar *s = PerlIOSelf(f,PerlIOScalar);
+ return SvCUR(s->var);
+ }
+ return 0;
+}
+
+void
+PerlIOScalar_set_ptrcnt(PerlIO *f, STDCHAR *ptr, SSize_t cnt)
+{
+ PerlIOScalar *s = PerlIOSelf(f,PerlIOScalar);
+ s->posn = SvCUR(s->var)-cnt;
+}
+
+PerlIO *
+PerlIOScalar_open(pTHX_ PerlIO_funcs *self, AV *layers, IV n, const char *mode, int fd, int imode, int perm, PerlIO *f, int narg, SV **args)
+{
+ PerlIOScalar *s;
+ if (narg > 0)
+ {
+ SV *ref = *args;
+ if (SvROK(ref))
+ {
+ SV *var = SvRV(ref);
+ sv_upgrade(var,SVt_PV);
+ f = PerlIO_allocate(aTHX);
+ s = PerlIOSelf(PerlIO_push(aTHX_ f,self,mode,PerlIOArg),PerlIOScalar);
+ s->var = SvREFCNT_inc(var);
+ s->posn = 0;
+ PerlIOBase(f)->flags |= PERLIO_F_OPEN;
+ return f;
+ }
+ }
+ return NULL;
+}
+
+
+PerlIO_funcs PerlIO_scalar = {
+ "Scalar",
+ sizeof(PerlIOScalar),
+ PERLIO_K_BUFFERED,
+ PerlIOScalar_pushed,
+ PerlIOScalar_popped,
+ PerlIOScalar_open,
+ NULL,
+ PerlIOScalar_fileno,
+ PerlIOBase_read,
+ PerlIOScalar_unread,
+ PerlIOScalar_write,
+ PerlIOScalar_seek,
+ PerlIOScalar_tell,
+ PerlIOScalar_close,
+ PerlIOScalar_flush,
+ PerlIOScalar_fill,
+ PerlIOBase_eof,
+ PerlIOBase_error,
+ PerlIOBase_clearerr,
+ PerlIOBase_setlinebuf,
+ PerlIOScalar_get_base,
+ PerlIOScalar_bufsiz,
+ PerlIOScalar_get_ptr,
+ PerlIOScalar_get_cnt,
+ PerlIOScalar_set_ptrcnt,
+};
+
+
+#endif /* Layers available */
+
+MODULE = PerlIO::Scalar PACKAGE = PerlIO::Scalar
+
+BOOT:
+{
+#ifdef PERLIO_LAYERS
+ PerlIO_define_layer(aTHX_ &PerlIO_scalar);
+#endif
+}
+
{
if (narg == 1)
{
- char *name = SvPV_nolen(*args);
- if (*mode == '#')
- {
- fd = PerlLIO_open3(name,imode,perm);
- if (fd >= 0)
- return PerlIO_fdopen(fd,mode+1);
- }
- else if (old)
- {
- return PerlIO_reopen(name,mode,old);
- }
+ if (*args == &PL_sv_undef)
+ return PerlIO_tmpfile();
else
{
- return PerlIO_open(name,mode);
+ char *name = SvPV_nolen(*args);
+ if (*mode == '#')
+ {
+ fd = PerlLIO_open3(name,imode,perm);
+ if (fd >= 0)
+ return PerlIO_fdopen(fd,mode+1);
+ }
+ else if (old)
+ {
+ return PerlIO_reopen(name,mode,old);
+ }
+ else
+ {
+ return PerlIO_open(name,mode);
+ }
}
}
else
return (svp) ? *svp : Nullsv;
}
-#define MYARG PerlIO_arg_fetch(aTHX_ layers,n+1)
-
-
PerlIO_funcs *
PerlIO_layer_fetch(pTHX_ AV *av,IV n,PerlIO_funcs *def)
{
return INT2PTR(PerlIO_funcs *, SvIV(layer));
}
if (!def)
- Perl_croak(aTHX_ "panic:layer array corrupt");
+ Perl_croak(aTHX_ "panic:PerlIO layer array corrupt");
return def;
}
PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers,n,NULL);
if (tab)
{
- if (!PerlIO_push(aTHX_ f,tab,mode,MYARG))
+ if (!PerlIO_push(aTHX_ f,tab,mode,PerlIOArg))
{
code -1;
break;
PerlIO_close(PerlIO *f)
{
dTHX;
- int code = (*PerlIOBase(f)->tab->Close)(f);
- while (*f)
+ int code = -1;
+ if (f && *f)
{
- PerlIO_pop(aTHX_ f);
+ code = (*PerlIOBase(f)->tab->Close)(f);
+ while (*f)
+ {
+ PerlIO_pop(aTHX_ f);
+ }
}
return code;
}
PerlIO_resolve_layers(pTHX_ const char *layers,const char *mode,int narg, SV **args)
{
AV *def = PerlIO_default_layers(aTHX);
+ int incdef = 1;
if (!_perlio)
PerlIO_stdstreams(aTHX);
- /* FIXME !!! */
+ if (narg)
+ {
+ if (SvROK(*args))
+ {
+ if (sv_isobject(*args))
+ {
+ SV *handler = PerlIO_find_layer(aTHX_ "object",6);
+ if (handler)
+ {
+ def = newAV();
+ av_push(def,handler);
+ av_push(def,&PL_sv_undef);
+ incdef = 0;
+ }
+ }
+ else
+ {
+ if (SvTYPE(SvRV(*args)) < SVt_PVAV)
+ {
+ SV *handler = PerlIO_find_layer(aTHX_ "Scalar",6);
+ if (handler)
+ {
+ def = newAV();
+ av_push(def,handler);
+ av_push(def,&PL_sv_undef);
+ incdef = 0;
+ }
+ }
+ else
+ {
+ Perl_croak(aTHX_ "Unsupported reference arg to open()");
+ }
+ }
+ }
+ }
if (!layers)
layers = PerlIO_context_layers(aTHX_ mode);
if (layers && *layers)
{
- AV *av = newAV();
- IV n = av_len(def)+1;
- while (n-- > 0)
+ AV *av;
+ if (incdef)
{
- SV **svp = av_fetch(def,n,0);
- av_store(av,n,(svp) ? SvREFCNT_inc(*svp) : &PL_sv_undef);
+ IV n = av_len(def)+1;
+ av = newAV();
+ while (n-- > 0)
+ {
+ SV **svp = av_fetch(def,n,0);
+ av_store(av,n,(svp) ? SvREFCNT_inc(*svp) : &PL_sv_undef);
+ }
+ }
+ else
+ {
+ av = def;
}
PerlIO_parse_layers(aTHX_ av,layers);
return av;
}
else
{
- SvREFCNT_inc(def);
+ if (incdef)
+ SvREFCNT_inc(def);
return def;
}
}
PerlIO *
PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd, int imode, int perm, PerlIO *f, int narg, SV **args)
{
- AV *layera;
- IV n;
- PerlIO_funcs *tab;
- if (f && *f)
+ if (!f && narg == 1 && *args == &PL_sv_undef)
{
- PerlIOl *l = *f;
- layera = newAV();
- while (l)
+ if ((f = PerlIO_tmpfile()))
{
- SV *arg = (l->tab->Getarg) ? (*l->tab->Getarg)(&l) : &PL_sv_undef;
- av_unshift(layera,2);
- av_store(layera,0,PerlIO_tab_sv(aTHX_ l->tab));
- av_store(layera,1,arg);
- l = *PerlIONext(&l);
+ if (!layers)
+ layers = PerlIO_context_layers(aTHX_ mode);
+ if (layers && *layers)
+ PerlIO_apply_layers(aTHX_ f,mode,layers);
}
}
else
{
- layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
- }
- n = av_len(layera)-1;
- while (n >= 0)
- {
- PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera,n,NULL);
- if (t && t->Open)
+ AV *layera;
+ IV n;
+ PerlIO_funcs *tab;
+ if (f && *f)
{
- tab = t;
- break;
+ /* This is "reopen" - it is not tested as perl does not use it yet */
+ PerlIOl *l = *f;
+ layera = newAV();
+ while (l)
+ {
+ SV *arg = (l->tab->Getarg) ? (*l->tab->Getarg)(&l) : &PL_sv_undef;
+ av_unshift(layera,2);
+ av_store(layera,0,PerlIO_tab_sv(aTHX_ l->tab));
+ av_store(layera,1,arg);
+ l = *PerlIONext(&l);
+ }
}
- n -= 2;
- }
- if (tab)
- {
- PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
- tab->name,layers,mode,fd,imode,perm,f,narg,args);
- f = (*tab->Open)(aTHX_ tab, layera, n, mode,fd,imode,perm,f,narg,args);
- if (f)
+ else
+ {
+ layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
+ }
+ n = av_len(layera)-1;
+ while (n >= 0)
+ {
+ PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera,n,NULL);
+ if (t && t->Open)
+ {
+ tab = t;
+ break;
+ }
+ n -= 2;
+ }
+ if (tab)
{
- if (n+2 < av_len(layera)+1)
+ PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
+ tab->name,layers,mode,fd,imode,perm,f,narg,args);
+ f = (*tab->Open)(aTHX_ tab, layera, n, mode,fd,imode,perm,f,narg,args);
+ if (f)
{
- if (PerlIO_apply_layera(aTHX_ f, mode, layera, n+2) != 0)
+ if (n+2 < av_len(layera)+1)
{
- f = NULL;
+ if (PerlIO_apply_layera(aTHX_ f, mode, layera, n+2) != 0)
+ {
+ f = NULL;
+ }
}
}
}
+ SvREFCNT_dec(layera);
}
- SvREFCNT_dec(layera);
return f;
}
return done;
}
+SSize_t
+PerlIOBase_read(PerlIO *f, void *vbuf, Size_t count)
+{
+ STDCHAR *buf = (STDCHAR *) vbuf;
+ if (f)
+ {
+ if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
+ return 0;
+ while (count > 0)
+ {
+ SSize_t avail = PerlIO_get_cnt(f);
+ SSize_t take = (count < avail) ? count : avail;
+ if (take > 0)
+ {
+ STDCHAR *ptr = PerlIO_get_ptr(f);
+ Copy(ptr,buf,take,STDCHAR);
+ PerlIO_set_ptrcnt(f,ptr+take,(avail -= take));
+ count -= take;
+ buf += take;
+ }
+ if (count > 0 && avail <= 0)
+ {
+ if (PerlIO_fill(f) != 0)
+ break;
+ }
+ }
+ return (buf - (STDCHAR *) vbuf);
+ }
+ return 0;
+}
+
IV
PerlIOBase_noop_ok(PerlIO *f)
{
PerlIO *n = PerlIONext(f);
if (PerlIO_flush(f) != 0)
code = -1;
- if (n && (*PerlIOBase(n)->tab->Close)(n) != 0)
+ if (n && *n && (*PerlIOBase(n)->tab->Close)(n) != 0)
code = -1;
PerlIOBase(f)->flags &= ~(PERLIO_F_CANREAD|PERLIO_F_CANWRITE|PERLIO_F_OPEN);
return code;
void
PerlIOBase_setlinebuf(PerlIO *f)
{
-
+ if (f)
+ {
+ PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
+ }
}
/*--------------------------------------------------------------------------------------*/
if (!f)
{
f = PerlIO_allocate(aTHX);
- s = PerlIOSelf(PerlIO_push(aTHX_ f,self,mode,MYARG),PerlIOUnix);
+ s = PerlIOSelf(PerlIO_push(aTHX_ f,self,mode,PerlIOArg),PerlIOUnix);
}
else
s = PerlIOSelf(f,PerlIOUnix);
if (stdio)
{
PerlIOStdio *s = PerlIOSelf(PerlIO_push(aTHX_ (f = PerlIO_allocate(aTHX)), self,
- (mode = PerlIOStdio_mode(mode,tmode)),MYARG),
+ (mode = PerlIOStdio_mode(mode,tmode)),PerlIOArg),
PerlIOStdio);
s->stdio = stdio;
}
}
if (stdio)
{
- PerlIOStdio *s = PerlIOSelf(PerlIO_push(aTHX_ (f = PerlIO_allocate(aTHX)),self,mode,MYARG),PerlIOStdio);
+ PerlIOStdio *s = PerlIOSelf(PerlIO_push(aTHX_ (f = PerlIO_allocate(aTHX)),self,mode,PerlIOArg),PerlIOStdio);
s->stdio = stdio;
return f;
}
PerlIO *next = PerlIONext(f);
PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n-2, PerlIOBase(next)->tab);
next = (*tab->Open)(aTHX_ tab, layers, n-2, mode,fd,imode,perm,next,narg,args);
- if (!next || (*PerlIOBase(f)->tab->Pushed)(f,mode,MYARG) != 0)
+ if (!next || (*PerlIOBase(f)->tab->Pushed)(f,mode,PerlIOArg) != 0)
{
return NULL;
}
f = (*tab->Open)(aTHX_ tab, layers, n-2, mode,fd,imode,perm,NULL,narg,args);
if (f)
{
- PerlIOBuf *b = PerlIOSelf(PerlIO_push(aTHX_ f,self,mode,MYARG),PerlIOBuf);
+ PerlIOBuf *b = PerlIOSelf(PerlIO_push(aTHX_ f,self,mode,PerlIOArg),PerlIOBuf);
fd = PerlIO_fileno(f);
#if O_BINARY != O_TEXT
/* do something about failing setmode()? --jhi */
PerlIOBuf_read(PerlIO *f, void *vbuf, Size_t count)
{
PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
- STDCHAR *buf = (STDCHAR *) vbuf;
if (f)
{
if (!b->ptr)
PerlIO_get_base(f);
- if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
- return 0;
- while (count > 0)
- {
- SSize_t avail = PerlIO_get_cnt(f);
- SSize_t take = (count < avail) ? count : avail;
- if (take > 0)
- {
- STDCHAR *ptr = PerlIO_get_ptr(f);
- Copy(ptr,buf,take,STDCHAR);
- PerlIO_set_ptrcnt(f,ptr+take,(avail -= take));
- count -= take;
- buf += take;
- }
- if (count > 0 && avail <= 0)
- {
- if (PerlIO_fill(f) != 0)
- break;
- }
- }
- return (buf - (STDCHAR *) vbuf);
+ return PerlIOBase_read(f,vbuf,count);
}
return 0;
}
return code;
}
-void
-PerlIOBuf_setlinebuf(PerlIO *f)
-{
- if (f)
- {
- PerlIOBase(f)->flags &= ~PERLIO_F_LINEBUF;
- }
-}
-
STDCHAR *
PerlIOBuf_get_ptr(PerlIO *f)
{
PerlIOBase_eof,
PerlIOBase_error,
PerlIOBase_clearerr,
- PerlIOBuf_setlinebuf,
+ PerlIOBase_setlinebuf,
PerlIOBuf_get_base,
PerlIOBuf_bufsiz,
PerlIOBuf_get_ptr,
PerlIOBase_eof,
PerlIOBase_error,
PerlIOBase_clearerr,
- PerlIOBuf_setlinebuf,
+ PerlIOBase_setlinebuf,
PerlIOBuf_get_base,
PerlIOBuf_bufsiz,
PerlIOBuf_get_ptr,
PerlIOBase_eof,
PerlIOBase_error,
PerlIOBase_clearerr,
- PerlIOBuf_setlinebuf,
+ PerlIOBase_setlinebuf,
PerlIOBuf_get_base,
PerlIOBuf_bufsiz,
PerlIOBuf_get_ptr,
PerlIOBase_eof,
PerlIOBase_error,
PerlIOBase_clearerr,
- PerlIOBuf_setlinebuf,
+ PerlIOBase_setlinebuf,
PerlIOMmap_get_base,
PerlIOBuf_bufsiz,
PerlIOBuf_get_ptr,