1 #define PERL_NO_GET_CONTEXT
11 struct _PerlIO base; /* Base "class" info */
40 #define MYMethod(x) #x,&s->x
43 PerlIOVia_fetchmethod(pTHX_ PerlIOVia * s, char *method, CV ** save)
45 GV *gv = gv_fetchmeth(s->stash, method, strlen(method), 0);
47 Perl_warn(aTHX_ "Lookup %s::%s => %p", HvNAME(s->stash), method, gv);
50 return *save = GvCV(gv);
53 return *save = (CV *) - 1;
58 * Try and call method, possibly via cached lookup.
59 * If method does not exist return Nullsv (caller may fallback to another approach
60 * If method does exist call it with flags passing variable number of args
61 * Last arg is a "filehandle" to layer below (if present)
62 * Returns scalar returned by method (if any) otherwise sv_undef
66 PerlIOVia_method(pTHX_ PerlIO * f, char *method, CV ** save, int flags,
69 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
71 (*save) ? *save : PerlIOVia_fetchmethod(aTHX_ s, method, save);
75 if (cv != (CV *) - 1) {
79 PUSHSTACKi(PERLSI_MAGIC);
84 while ((arg = va_arg(ap, SV *))) {
89 GV *gv = newGVgen(HvNAME(s->stash));
91 s->fh = newRV_noinc((SV *) gv);
94 IoIFP(s->io) = PerlIONext(f);
95 IoOFP(s->io) = PerlIONext(f);
99 PerlIO_debug("No next\n");
100 /* FIXME: How should this work for OPEN etc? */
103 count = call_sv((SV *) cv, flags);
110 result = &PL_sv_undef;
120 PerlIOVia_pushed(pTHX_ PerlIO * f, const char *mode, SV * arg,
123 IV code = PerlIOBase_pushed(aTHX_ f, mode, Nullsv, tab);
125 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
127 if (ckWARN(WARN_LAYER))
128 Perl_warner(aTHX_ packWARN(WARN_LAYER),
129 "No package specified");
135 char *pkg = SvPV(arg, pkglen);
136 s->obj = SvREFCNT_inc(arg);
137 s->stash = gv_stashpvn(pkg, pkglen, FALSE);
140 newSVpvn(Perl_form(aTHX_ "PerlIO::via::%s", pkg),
143 s->stash = gv_stashpvn(SvPVX(s->obj), pkglen + 13, FALSE);
150 /* binmode() passes NULL - so find out what mode is */
151 mode = PerlIO_modestr(f,lmode);
153 modesv = sv_2mortal(newSVpvn(mode, strlen(mode)));
154 result = PerlIOVia_method(aTHX_ f, MYMethod(PUSHED), G_SCALAR,
157 if (sv_isobject(result)) {
158 s->obj = SvREFCNT_inc(result);
161 else if (SvIV(result) != 0)
167 if (PerlIOVia_fetchmethod(aTHX_ s, MYMethod(FILL)) ==
169 PerlIOBase(f)->flags &= ~PERLIO_F_FASTGETS;
171 PerlIOBase(f)->flags |= PERLIO_F_FASTGETS;
174 if (ckWARN(WARN_LAYER))
175 Perl_warner(aTHX_ packWARN(WARN_LAYER),
176 "Cannot find package '%.*s'", (int) pkglen,
194 PerlIOVia_open(pTHX_ PerlIO_funcs * self, PerlIO_list_t * layers,
195 IV n, const char *mode, int fd, int imode, int perm,
196 PerlIO * f, int narg, SV ** args)
199 f = PerlIO_push(aTHX_ PerlIO_allocate(aTHX), self, mode,
204 if (!PerlIO_push(aTHX_ f, self, mode, PerlIOArg))
208 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
211 SV *fdsv = sv_2mortal(newSViv(fd));
213 PerlIOVia_method(aTHX_ f, MYMethod(FDOPEN), G_SCALAR, fdsv,
218 SV *imodesv = sv_2mortal(newSViv(imode));
219 SV *permsv = sv_2mortal(newSViv(perm));
221 PerlIOVia_method(aTHX_ f, MYMethod(SYSOPEN), G_SCALAR,
222 *args, imodesv, permsv, Nullsv);
226 PerlIOVia_method(aTHX_ f, MYMethod(OPEN), G_SCALAR,
231 if (sv_isobject(result))
232 s->obj = SvREFCNT_inc(result);
233 else if (!SvTRUE(result)) {
238 /* Required open method not present */
239 PerlIO_funcs *tab = NULL;
243 PerlIO_layer_fetch(aTHX_ layers, m, NULL);
251 if ((*tab->Open) (aTHX_ tab, layers, m, mode, fd, imode,
252 perm, PerlIONext(f), narg, args)) {
253 PerlIO_debug("Opened with %s => %p->%p\n", tab->name,
254 PerlIONext(f), *PerlIONext(f));
257 * More layers above the one that we used to open -
260 if (PerlIO_apply_layera
261 (aTHX_ PerlIONext(f), mode, layers, m + 1,
263 /* If pushing layers fails close the file */
268 /* FIXME - Call an OPENED method here ? */
272 PerlIO_debug("Open fail %s => %p->%p\n", tab->name,
273 PerlIONext(f), *PerlIONext(f));
274 /* Sub-layer open failed */
278 PerlIO_debug("Nothing to open with");
279 /* Nothing to do the open */
289 PerlIOVia_popped(pTHX_ PerlIO * f)
291 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
292 PerlIOVia_method(aTHX_ f, MYMethod(POPPED), G_VOID, Nullsv);
294 SvREFCNT_dec(s->var);
308 SvREFCNT_dec(s->obj);
315 PerlIOVia_close(pTHX_ PerlIO * f)
317 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
318 IV code = PerlIOBase_close(aTHX_ f);
320 PerlIOVia_method(aTHX_ f, MYMethod(CLOSE), G_SCALAR, Nullsv);
321 if (result && SvIV(result) != 0)
323 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
328 PerlIOVia_fileno(pTHX_ PerlIO * f)
330 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
332 PerlIOVia_method(aTHX_ f, MYMethod(FILENO), G_SCALAR, Nullsv);
333 return (result) ? SvIV(result) : PerlIO_fileno(PerlIONext(f));
337 PerlIOVia_binmode(pTHX_ PerlIO * f)
339 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
341 PerlIOVia_method(aTHX_ f, MYMethod(BINMODE), G_SCALAR, Nullsv);
342 if (!result || !SvOK(result)) {
350 PerlIOVia_seek(pTHX_ PerlIO * f, Off_t offset, int whence)
352 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
353 SV *offsv = sv_2mortal(sizeof(Off_t) > sizeof(IV)
354 ? newSVnv((NV)offset) : newSViv((IV)offset));
355 SV *whsv = sv_2mortal(newSViv(whence));
357 PerlIOVia_method(aTHX_ f, MYMethod(SEEK), G_SCALAR, offsv, whsv,
359 return (result) ? SvIV(result) : -1;
363 PerlIOVia_tell(pTHX_ PerlIO * f)
365 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
367 PerlIOVia_method(aTHX_ f, MYMethod(TELL), G_SCALAR, Nullsv);
369 ? (SvNOK(result) ? (Off_t)SvNV(result) : (Off_t)SvIV(result))
374 PerlIOVia_unread(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
376 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
377 SV *buf = sv_2mortal(newSVpvn((char *) vbuf, count));
379 PerlIOVia_method(aTHX_ f, MYMethod(UNREAD), G_SCALAR, buf, Nullsv);
381 return (SSize_t) SvIV(result);
383 return PerlIOBase_unread(aTHX_ f, vbuf, count);
388 PerlIOVia_read(pTHX_ PerlIO * f, void *vbuf, Size_t count)
391 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
392 if (PerlIOBase(f)->flags & PERLIO_F_FASTGETS) {
393 rd = PerlIOBase_read(aTHX_ f, vbuf, count);
396 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
397 SV *buf = sv_2mortal(newSV(count));
398 SV *n = sv_2mortal(newSViv(count));
400 PerlIOVia_method(aTHX_ f, MYMethod(READ), G_SCALAR, buf, n,
403 rd = (SSize_t) SvIV(result);
404 Move(SvPVX(buf), vbuf, rd, char);
413 PerlIOVia_write(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
415 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
416 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
417 SV *buf = newSVpvn((char *) vbuf, count);
419 PerlIOVia_method(aTHX_ f, MYMethod(WRITE), G_SCALAR, buf,
423 return (SSize_t) SvIV(result);
430 PerlIOVia_fill(pTHX_ PerlIO * f)
432 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
433 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
435 PerlIOVia_method(aTHX_ f, MYMethod(FILL), G_SCALAR, Nullsv);
437 SvREFCNT_dec(s->var);
440 if (result && SvOK(result)) {
442 char *p = SvPV(result, len);
443 s->var = newSVpvn(p, len);
444 s->cnt = SvCUR(s->var);
448 PerlIOBase(f)->flags |= PERLIO_F_EOF;
454 PerlIOVia_flush(pTHX_ PerlIO * f)
456 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
458 PerlIOVia_method(aTHX_ f, MYMethod(FLUSH), G_SCALAR, Nullsv);
459 if (s->var && s->cnt > 0) {
460 SvREFCNT_dec(s->var);
463 return (result) ? SvIV(result) : 0;
467 PerlIOVia_get_base(pTHX_ PerlIO * f)
469 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
470 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
472 return (STDCHAR *) SvPVX(s->var);
475 return (STDCHAR *) Nullch;
479 PerlIOVia_get_ptr(pTHX_ PerlIO * f)
481 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
482 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
484 STDCHAR *p = (STDCHAR *) (SvEND(s->var) - s->cnt);
488 return (STDCHAR *) Nullch;
492 PerlIOVia_get_cnt(pTHX_ PerlIO * f)
494 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
495 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
504 PerlIOVia_bufsiz(pTHX_ PerlIO * f)
506 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
507 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
509 return SvCUR(s->var);
515 PerlIOVia_set_ptrcnt(pTHX_ PerlIO * f, STDCHAR * ptr, SSize_t cnt)
517 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
522 PerlIOVia_setlinebuf(pTHX_ PerlIO * f)
524 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
525 PerlIOVia_method(aTHX_ f, MYMethod(SETLINEBUF), G_VOID, Nullsv);
526 PerlIOBase_setlinebuf(aTHX_ f);
530 PerlIOVia_clearerr(pTHX_ PerlIO * f)
532 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
533 PerlIOVia_method(aTHX_ f, MYMethod(CLEARERR), G_VOID, Nullsv);
534 PerlIOBase_clearerr(aTHX_ f);
538 PerlIOVia_error(pTHX_ PerlIO * f)
540 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
542 PerlIOVia_method(aTHX_ f, "ERROR", &s->mERROR, G_SCALAR, Nullsv);
543 return (result) ? SvIV(result) : PerlIOBase_error(aTHX_ f);
547 PerlIOVia_eof(pTHX_ PerlIO * f)
549 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
551 PerlIOVia_method(aTHX_ f, "EOF", &s->mEOF, G_SCALAR, Nullsv);
552 return (result) ? SvIV(result) : PerlIOBase_eof(aTHX_ f);
556 PerlIOVia_getarg(pTHX_ PerlIO * f, CLONE_PARAMS * param, int flags)
558 PerlIOVia *s = PerlIOSelf(f, PerlIOVia);
559 return PerlIOVia_method(aTHX_ f, MYMethod(GETARG), G_SCALAR, Nullsv);
563 PerlIOVia_dup(pTHX_ PerlIO * f, PerlIO * o, CLONE_PARAMS * param,
566 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
567 /* Most of the fields will lazily set themselves up as needed
568 stash and obj have been set up by the implied push
576 PerlIO_funcs PerlIO_object = {
577 sizeof(PerlIO_funcs),
580 PERLIO_K_BUFFERED|PERLIO_K_DESTRUCT,
583 PerlIOVia_open, /* NULL, */
584 PerlIOVia_binmode, /* NULL, */
599 PerlIOVia_setlinebuf,
604 PerlIOVia_set_ptrcnt,
608 #endif /* Layers available */
610 MODULE = PerlIO::via PACKAGE = PerlIO::via
616 PerlIO_define_layer(aTHX_ &PerlIO_object);