1 #define PERL_NO_GET_CONTEXT
11 struct _PerlIO base; /* Base "class" info */
41 #define MYMethod(x) #x,&s->x
44 PerlIOVia_fetchmethod(pTHX_ PerlIOVia * s, char *method, CV ** save)
46 GV *gv = gv_fetchmeth(s->stash, method, strlen(method), 0);
48 Perl_warn(aTHX_ "Lookup %s::%s => %p", HvNAME_get(s->stash), method, gv);
51 return *save = GvCV(gv);
54 return *save = (CV *) - 1;
59 * Try and call method, possibly via cached lookup.
60 * If method does not exist return Nullsv (caller may fallback to another approach
61 * If method does exist call it with flags passing variable number of args
62 * Last arg is a "filehandle" to layer below (if present)
63 * Returns scalar returned by method (if any) otherwise sv_undef
67 PerlIOVia_method(pTHX_ PerlIO * f, char *method, CV ** save, int flags,
70 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
72 (*save) ? *save : PerlIOVia_fetchmethod(aTHX_ s, method, save);
76 if (cv != (CV *) - 1) {
80 PUSHSTACKi(PERLSI_MAGIC);
85 while ((arg = va_arg(ap, SV *))) {
90 GV *gv = newGVgen(HvNAME_get(s->stash));
92 s->fh = newRV_noinc((SV *) gv);
95 IoIFP(s->io) = PerlIONext(f);
96 IoOFP(s->io) = PerlIONext(f);
100 PerlIO_debug("No next\n");
101 /* FIXME: How should this work for OPEN etc? */
104 count = call_sv((SV *) cv, flags);
111 result = &PL_sv_undef;
121 PerlIOVia_pushed(pTHX_ PerlIO * f, const char *mode, SV * arg,
124 IV code = PerlIOBase_pushed(aTHX_ f, mode, Nullsv, tab);
126 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
128 if (ckWARN(WARN_LAYER))
129 Perl_warner(aTHX_ packWARN(WARN_LAYER),
130 "No package specified");
136 const char *pkg = SvPV(arg, pkglen);
137 s->obj = SvREFCNT_inc(arg);
138 s->stash = gv_stashpvn(pkg, pkglen, 0);
140 SvREFCNT_dec(s->obj);
142 newSVpvn(Perl_form(aTHX_ "PerlIO::via::%s", pkg),
144 s->stash = gv_stashpvn(SvPVX_const(s->obj), pkglen + 13, 0);
151 /* binmode() passes NULL - so find out what mode is */
152 mode = PerlIO_modestr(f,lmode);
154 modesv = sv_2mortal(newSVpvn(mode, strlen(mode)));
155 result = PerlIOVia_method(aTHX_ f, MYMethod(PUSHED), G_SCALAR,
158 if (sv_isobject(result)) {
159 SvREFCNT_dec(s->obj);
160 s->obj = SvREFCNT_inc(result);
162 else if (SvIV(result) != 0)
168 modesv = (*PerlIONext(f) && (PerlIOBase(PerlIONext(f))->flags & PERLIO_F_UTF8))
169 ? &PL_sv_yes : &PL_sv_no;
170 result = PerlIOVia_method(aTHX_ f, MYMethod(UTF8), G_SCALAR, modesv, Nullsv);
171 if (result && SvTRUE(result)) {
172 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
175 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
177 if (PerlIOVia_fetchmethod(aTHX_ s, MYMethod(FILL)) ==
179 PerlIOBase(f)->flags &= ~PERLIO_F_FASTGETS;
181 PerlIOBase(f)->flags |= PERLIO_F_FASTGETS;
184 if (ckWARN(WARN_LAYER))
185 Perl_warner(aTHX_ packWARN(WARN_LAYER),
186 "Cannot find package '%.*s'", (int) pkglen,
204 PerlIOVia_open(pTHX_ PerlIO_funcs * self, PerlIO_list_t * layers,
205 IV n, const char *mode, int fd, int imode, int perm,
206 PerlIO * f, int narg, SV ** args)
209 f = PerlIO_push(aTHX_ PerlIO_allocate(aTHX), self, mode,
214 if (!PerlIO_push(aTHX_ f, self, mode, PerlIOArg))
218 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
221 SV *fdsv = sv_2mortal(newSViv(fd));
223 PerlIOVia_method(aTHX_ f, MYMethod(FDOPEN), G_SCALAR, fdsv,
228 SV *imodesv = sv_2mortal(newSViv(imode));
229 SV *permsv = sv_2mortal(newSViv(perm));
231 PerlIOVia_method(aTHX_ f, MYMethod(SYSOPEN), G_SCALAR,
232 *args, imodesv, permsv, Nullsv);
236 PerlIOVia_method(aTHX_ f, MYMethod(OPEN), G_SCALAR,
241 if (sv_isobject(result))
242 s->obj = SvREFCNT_inc(result);
243 else if (!SvTRUE(result)) {
248 /* Required open method not present */
249 PerlIO_funcs *tab = NULL;
253 PerlIO_layer_fetch(aTHX_ layers, m, NULL);
261 if ((*tab->Open) (aTHX_ tab, layers, m, mode, fd, imode,
262 perm, PerlIONext(f), narg, args)) {
263 PerlIO_debug("Opened with %s => %p->%p\n", tab->name,
264 PerlIONext(f), *PerlIONext(f));
267 * More layers above the one that we used to open -
270 if (PerlIO_apply_layera
271 (aTHX_ PerlIONext(f), mode, layers, m + 1,
273 /* If pushing layers fails close the file */
278 /* FIXME - Call an OPENED method here ? */
282 PerlIO_debug("Open fail %s => %p->%p\n", tab->name,
283 PerlIONext(f), *PerlIONext(f));
284 /* Sub-layer open failed */
288 PerlIO_debug("Nothing to open with");
289 /* Nothing to do the open */
299 PerlIOVia_popped(pTHX_ PerlIO * f)
301 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
302 PerlIOVia_method(aTHX_ f, MYMethod(POPPED), G_VOID, Nullsv);
304 SvREFCNT_dec(s->var);
318 SvREFCNT_dec(s->obj);
325 PerlIOVia_close(pTHX_ PerlIO * f)
327 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
328 IV code = PerlIOBase_close(aTHX_ f);
330 PerlIOVia_method(aTHX_ f, MYMethod(CLOSE), G_SCALAR, Nullsv);
331 if (result && SvIV(result) != 0)
333 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
338 PerlIOVia_fileno(pTHX_ PerlIO * f)
340 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
342 PerlIOVia_method(aTHX_ f, MYMethod(FILENO), G_SCALAR, Nullsv);
343 return (result) ? SvIV(result) : PerlIO_fileno(PerlIONext(f));
347 PerlIOVia_binmode(pTHX_ PerlIO * f)
349 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
351 PerlIOVia_method(aTHX_ f, MYMethod(BINMODE), G_SCALAR, Nullsv);
352 if (!result || !SvOK(result)) {
360 PerlIOVia_seek(pTHX_ PerlIO * f, Off_t offset, int whence)
362 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
363 SV *offsv = sv_2mortal(sizeof(Off_t) > sizeof(IV)
364 ? newSVnv((NV)offset) : newSViv((IV)offset));
365 SV *whsv = sv_2mortal(newSViv(whence));
367 PerlIOVia_method(aTHX_ f, MYMethod(SEEK), G_SCALAR, offsv, whsv,
369 #if Off_t_size == 8 && defined(CONDOP_SIZE) && CONDOP_SIZE < Off_t_size
371 return (Off_t) SvIV(result);
375 return (result) ? SvIV(result) : -1;
380 PerlIOVia_tell(pTHX_ PerlIO * f)
382 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
384 PerlIOVia_method(aTHX_ f, MYMethod(TELL), G_SCALAR, Nullsv);
386 ? (SvNOK(result) ? (Off_t)SvNV(result) : (Off_t)SvIV(result))
391 PerlIOVia_unread(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
393 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
394 SV *buf = sv_2mortal(newSVpvn((char *) vbuf, count));
396 PerlIOVia_method(aTHX_ f, MYMethod(UNREAD), G_SCALAR, buf, Nullsv);
398 return (SSize_t) SvIV(result);
400 return PerlIOBase_unread(aTHX_ f, vbuf, count);
405 PerlIOVia_read(pTHX_ PerlIO * f, void *vbuf, Size_t count)
408 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
409 if (PerlIOBase(f)->flags & PERLIO_F_FASTGETS) {
410 rd = PerlIOBase_read(aTHX_ f, vbuf, count);
413 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
414 SV *buf = sv_2mortal(newSV(count));
415 SV *n = sv_2mortal(newSViv(count));
417 PerlIOVia_method(aTHX_ f, MYMethod(READ), G_SCALAR, buf, n,
420 rd = (SSize_t) SvIV(result);
421 Move(SvPVX(buf), vbuf, rd, char);
430 PerlIOVia_write(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
432 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
433 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
434 SV *buf = newSVpvn((char *) vbuf, count);
436 PerlIOVia_method(aTHX_ f, MYMethod(WRITE), G_SCALAR, buf,
440 return (SSize_t) SvIV(result);
447 PerlIOVia_fill(pTHX_ PerlIO * f)
449 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
450 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
452 PerlIOVia_method(aTHX_ f, MYMethod(FILL), G_SCALAR, Nullsv);
454 SvREFCNT_dec(s->var);
457 if (result && SvOK(result)) {
459 const char *p = SvPV(result, len);
460 s->var = newSVpvn(p, len);
461 s->cnt = SvCUR(s->var);
465 PerlIOBase(f)->flags |= PERLIO_F_EOF;
471 PerlIOVia_flush(pTHX_ PerlIO * f)
473 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
475 PerlIOVia_method(aTHX_ f, MYMethod(FLUSH), G_SCALAR, Nullsv);
476 if (s->var && s->cnt > 0) {
477 SvREFCNT_dec(s->var);
480 return (result) ? SvIV(result) : 0;
484 PerlIOVia_get_base(pTHX_ PerlIO * f)
486 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
487 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
489 return (STDCHAR *) SvPVX(s->var);
492 return (STDCHAR *) Nullch;
496 PerlIOVia_get_ptr(pTHX_ PerlIO * f)
498 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
499 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
501 STDCHAR *p = (STDCHAR *) (SvEND(s->var) - s->cnt);
505 return (STDCHAR *) Nullch;
509 PerlIOVia_get_cnt(pTHX_ PerlIO * f)
511 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
512 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
521 PerlIOVia_bufsiz(pTHX_ PerlIO * f)
523 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
524 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
526 return SvCUR(s->var);
532 PerlIOVia_set_ptrcnt(pTHX_ PerlIO * f, STDCHAR * ptr, SSize_t cnt)
534 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
539 PerlIOVia_setlinebuf(pTHX_ PerlIO * f)
541 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
542 PerlIOVia_method(aTHX_ f, MYMethod(SETLINEBUF), G_VOID, Nullsv);
543 PerlIOBase_setlinebuf(aTHX_ f);
547 PerlIOVia_clearerr(pTHX_ PerlIO * f)
549 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
550 PerlIOVia_method(aTHX_ f, MYMethod(CLEARERR), G_VOID, Nullsv);
551 PerlIOBase_clearerr(aTHX_ f);
555 PerlIOVia_error(pTHX_ PerlIO * f)
557 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
559 PerlIOVia_method(aTHX_ f, "ERROR", &s->mERROR, G_SCALAR, Nullsv);
560 return (result) ? SvIV(result) : PerlIOBase_error(aTHX_ f);
564 PerlIOVia_eof(pTHX_ PerlIO * f)
566 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
568 PerlIOVia_method(aTHX_ f, "EOF", &s->mEOF, G_SCALAR, Nullsv);
569 return (result) ? SvIV(result) : PerlIOBase_eof(aTHX_ f);
573 PerlIOVia_getarg(pTHX_ PerlIO * f, CLONE_PARAMS * param, int flags)
575 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
576 return PerlIOVia_method(aTHX_ f, MYMethod(GETARG), G_SCALAR, Nullsv);
580 PerlIOVia_dup(pTHX_ PerlIO * f, PerlIO * o, CLONE_PARAMS * param,
583 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
584 /* Most of the fields will lazily set themselves up as needed
585 stash and obj have been set up by the implied push
593 PERLIO_FUNCS_DECL(PerlIO_object) = {
594 sizeof(PerlIO_funcs),
597 PERLIO_K_BUFFERED|PERLIO_K_DESTRUCT,
600 PerlIOVia_open, /* NULL, */
601 PerlIOVia_binmode, /* NULL, */
616 PerlIOVia_setlinebuf,
621 PerlIOVia_set_ptrcnt,
625 #endif /* Layers available */
627 MODULE = PerlIO::via PACKAGE = PerlIO::via
633 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_object));