X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=universal.c;h=e7889fb1fd2dea089b97a409e2ae6fcffa92bb7f;hb=b8d5f52139b667ea3276273b5665157310d1a461;hp=7999757f284e85d865ed1a7797b6b3e01e0dca39;hpb=35c1215df9ec9cb54402afdda4ed360fdbf58539;p=p5sagit%2Fp5-mst-13.2.git diff --git a/universal.c b/universal.c index 7999757..e7889fb 100644 --- a/universal.c +++ b/universal.c @@ -1,6 +1,7 @@ /* universal.c * - * Copyright (c) 1997-2002, Larry Wall + * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, + * by Larry Wall and others * * You may distribute under the terms of either the GNU General Public * License or the Artistic License, as specified in the README file. @@ -17,6 +18,10 @@ #define PERL_IN_UNIVERSAL_C #include "perl.h" +#ifdef USE_PERLIO +#include "perliol.h" /* For the PERLIO_F_XXX */ +#endif + /* * Contributed by Graham Barr * The main guts of traverse_isa was actually copied from gv_fetchmeth @@ -176,6 +181,8 @@ XS(XS_utf8_native_to_unicode); XS(XS_Internals_SvREADONLY); XS(XS_Internals_SvREFCNT); XS(XS_Internals_hv_clear_placehold); +XS(XS_PerlIO_get_layers); +XS(XS_Regexp_DESTROY); void Perl_boot_core_UNIVERSAL(pTHX) @@ -214,6 +221,9 @@ Perl_boot_core_UNIVERSAL(pTHX) newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$"); newXSproto("Internals::hv_clear_placeholders", XS_Internals_hv_clear_placehold, file, "\\%"); + newXSproto("PerlIO::get_layers", + XS_PerlIO_get_layers, file, "*;@"); + newXS("Regexp::DESTROY", XS_Regexp_DESTROY, file); } @@ -232,7 +242,8 @@ XS(XS_UNIVERSAL_isa) if (SvGMAGICAL(sv)) mg_get(sv); - if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv)))) + if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv)) + || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv)))) XSRETURN_UNDEF; name = (char *)SvPV(ST(1),n_a); @@ -258,7 +269,8 @@ XS(XS_UNIVERSAL_can) if (SvGMAGICAL(sv)) mg_get(sv); - if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv)))) + if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv)) + || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv)))) XSRETURN_UNDEF; name = (char *)SvPV(ST(1),n_a); @@ -338,7 +350,8 @@ XS(XS_UNIVERSAL_VERSION) req = new_version(req); if ( vcmp( SvRV(req), SvRV(sv) ) > 0 ) - Perl_croak(aTHX_ "%s version %_ required--this is only version %_", + Perl_croak(aTHX_ + "%s version %"SVf" required--this is only version %"SVf, HvNAME(pkg), req, sv); } @@ -350,17 +363,19 @@ XS(XS_UNIVERSAL_VERSION) XS(XS_version_new) { dXSARGS; - if (items != 2) + if (items > 3) Perl_croak(aTHX_ "Usage: version::new(class, version)"); SP -= items; { /* char * class = (char *)SvPV_nolen(ST(0)); */ - SV * version = ST(1); - -{ - PUSHs(new_version(version)); -} + SV *version = ST(1); + if (items == 3 ) + { + char *vs = savepvn(SvPVX(ST(2)),SvCUR(ST(2))); + version = Perl_newSVpvf(aTHX_ "v%s",vs); + } + PUSHs(new_version(version)); PUTBACK; return; } @@ -709,3 +724,134 @@ XS(XS_Internals_hv_clear_placehold) XSRETURN(0); } + +XS(XS_Regexp_DESTROY) +{ + +} + +XS(XS_PerlIO_get_layers) +{ + dXSARGS; + if (items < 1 || items % 2 == 0) + Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])"); +#ifdef USE_PERLIO + { + SV * sv; + GV * gv; + IO * io; + bool input = TRUE; + bool details = FALSE; + + if (items > 1) { + SV **svp; + + for (svp = MARK + 2; svp <= SP; svp += 2) { + SV **varp = svp; + SV **valp = svp + 1; + STRLEN klen; + char *key = SvPV(*varp, klen); + + switch (*key) { + case 'i': + if (klen == 5 && memEQ(key, "input", 5)) { + input = SvTRUE(*valp); + break; + } + goto fail; + case 'o': + if (klen == 6 && memEQ(key, "output", 6)) { + input = !SvTRUE(*valp); + break; + } + goto fail; + case 'd': + if (klen == 7 && memEQ(key, "details", 7)) { + details = SvTRUE(*valp); + break; + } + goto fail; + default: + fail: + Perl_croak(aTHX_ + "get_layers: unknown argument '%s'", + key); + } + } + + SP -= (items - 1); + } + + sv = POPs; + gv = (GV*)sv; + + if (!isGV(sv)) { + if (SvROK(sv) && isGV(SvRV(sv))) + gv = (GV*)SvRV(sv); + else + gv = gv_fetchpv(SvPVX(sv), FALSE, SVt_PVIO); + } + + if (gv && (io = GvIO(gv))) { + dTARGET; + AV* av = PerlIO_get_layers(aTHX_ input ? + IoIFP(io) : IoOFP(io)); + I32 i; + I32 last = av_len(av); + I32 nitem = 0; + + for (i = last; i >= 0; i -= 3) { + SV **namsvp; + SV **argsvp; + SV **flgsvp; + bool namok, argok, flgok; + + namsvp = av_fetch(av, i - 2, FALSE); + argsvp = av_fetch(av, i - 1, FALSE); + flgsvp = av_fetch(av, i, FALSE); + + namok = namsvp && *namsvp && SvPOK(*namsvp); + argok = argsvp && *argsvp && SvPOK(*argsvp); + flgok = flgsvp && *flgsvp && SvIOK(*flgsvp); + + if (details) { + XPUSHs(namok ? + newSVpv(SvPVX(*namsvp), 0) : &PL_sv_undef); + XPUSHs(argok ? + newSVpv(SvPVX(*argsvp), 0) : &PL_sv_undef); + if (flgok) + XPUSHi(SvIVX(*flgsvp)); + else + XPUSHs(&PL_sv_undef); + nitem += 3; + } + else { + if (namok && argok) + XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")", + *namsvp, *argsvp)); + else if (namok) + XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf, *namsvp)); + else + XPUSHs(&PL_sv_undef); + nitem++; + if (flgok) { + IV flags = SvIVX(*flgsvp); + + if (flags & PERLIO_F_UTF8) { + XPUSHs(newSVpvn("utf8", 4)); + nitem++; + } + } + } + } + + SvREFCNT_dec(av); + + XSRETURN(nitem); + } + } +#endif + + XSRETURN(0); +} +