Fix regexec.c so $^N and $+ are correctly updated so that they work properly inside...
[p5sagit/p5-mst-13.2.git] / universal.c
1 /*    universal.c
2  *
3  *    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4  *    2005, 2006, 2007 by Larry Wall and others
5  *
6  *    You may distribute under the terms of either the GNU General Public
7  *    License or the Artistic License, as specified in the README file.
8  *
9  */
10
11 /*
12  * "The roots of those mountains must be roots indeed; there must be
13  * great secrets buried there which have not been discovered since the
14  * beginning." --Gandalf, relating Gollum's story
15  */
16
17 /* This file contains the code that implements the functions in Perl's
18  * UNIVERSAL package, such as UNIVERSAL->can().
19  *
20  * It is also used to store XS functions that need to be present in
21  * miniperl for a lack of a better place to put them. It might be
22  * clever to move them to seperate XS files which would then be pulled
23  * in by some to-be-written build process.
24  */
25
26 #include "EXTERN.h"
27 #define PERL_IN_UNIVERSAL_C
28 #include "perl.h"
29
30 #ifdef USE_PERLIO
31 #include "perliol.h" /* For the PERLIO_F_XXX */
32 #endif
33
34 /*
35  * Contributed by Graham Barr  <Graham.Barr@tiuk.ti.com>
36  * The main guts of traverse_isa was actually copied from gv_fetchmeth
37  */
38
39 STATIC bool
40 S_isa_lookup(pTHX_ HV *stash, const char * const name, const HV* const name_stash)
41 {
42     dVAR;
43     AV* stash_linear_isa;
44     SV** svp;
45     const char *hvname;
46     I32 items;
47
48     /* A stash/class can go by many names (ie. User == main::User), so 
49        we compare the stash itself just in case */
50     if (name_stash && ((const HV *)stash == name_stash))
51         return TRUE;
52
53     hvname = HvNAME_get(stash);
54
55     if (strEQ(hvname, name))
56         return TRUE;
57
58     if (strEQ(name, "UNIVERSAL"))
59         return TRUE;
60
61     stash_linear_isa = mro_get_linear_isa(stash);
62     svp = AvARRAY(stash_linear_isa) + 1;
63     items = AvFILLp(stash_linear_isa);
64     while (items--) {
65         SV* const basename_sv = *svp++;
66         HV* const basestash = gv_stashsv(basename_sv, 0);
67         if (!basestash) {
68             if (ckWARN(WARN_SYNTAX))
69                 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
70                             "Can't locate package %"SVf" for the parents of %s",
71                             SVfARG(basename_sv), hvname);
72             continue;
73         }
74         if(name_stash == basestash || strEQ(name, SvPVX(basename_sv)))
75             return TRUE;
76     }
77
78     return FALSE;
79 }
80
81 /*
82 =head1 SV Manipulation Functions
83
84 =for apidoc sv_derived_from
85
86 Returns a boolean indicating whether the SV is derived from the specified class
87 I<at the C level>.  To check derivation at the Perl level, call C<isa()> as a
88 normal Perl method.
89
90 =cut
91 */
92
93 bool
94 Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
95 {
96     dVAR;
97     HV *stash;
98
99     SvGETMAGIC(sv);
100
101     if (SvROK(sv)) {
102         const char *type;
103         sv = SvRV(sv);
104         type = sv_reftype(sv,0);
105         if (type && strEQ(type,name))
106             return TRUE;
107         stash = SvOBJECT(sv) ? SvSTASH(sv) : NULL;
108     }
109     else {
110         stash = gv_stashsv(sv, 0);
111     }
112
113     if (stash) {
114         HV * const name_stash = gv_stashpv(name, 0);
115         return isa_lookup(stash, name, name_stash);
116     }
117     else
118         return FALSE;
119
120 }
121
122 /*
123 =for apidoc sv_does
124
125 Returns a boolean indicating whether the SV performs a specific, named role.
126 The SV can be a Perl object or the name of a Perl class.
127
128 =cut
129 */
130
131 #include "XSUB.h"
132
133 bool
134 Perl_sv_does(pTHX_ SV *sv, const char *name)
135 {
136     const char *classname;
137     bool does_it;
138     SV *methodname;
139
140     dSP;
141     ENTER;
142     SAVETMPS;
143
144     SvGETMAGIC(sv);
145
146     if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
147                 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
148         return FALSE;
149
150     if (sv_isobject(sv)) {
151         classname = sv_reftype(SvRV(sv),TRUE);
152     } else {
153         classname = SvPV_nolen(sv);
154     }
155
156     if (strEQ(name,classname))
157         return TRUE;
158
159     PUSHMARK(SP);
160     XPUSHs(sv);
161     mXPUSHs(newSVpv(name, 0));
162     PUTBACK;
163
164     methodname = newSVpvs_flags("isa", SVs_TEMP);
165     /* ugly hack: use the SvSCREAM flag so S_method_common
166      * can figure out we're calling DOES() and not isa(),
167      * and report eventual errors correctly. --rgs */
168     SvSCREAM_on(methodname);
169     call_sv(methodname, G_SCALAR | G_METHOD);
170     SPAGAIN;
171
172     does_it = SvTRUE( TOPs );
173     FREETMPS;
174     LEAVE;
175
176     return does_it;
177 }
178
179 PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv);
180 PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv);
181 PERL_XS_EXPORT_C void XS_UNIVERSAL_DOES(pTHX_ CV *cv);
182 PERL_XS_EXPORT_C void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
183 XS(XS_version_new);
184 XS(XS_version_stringify);
185 XS(XS_version_numify);
186 XS(XS_version_normal);
187 XS(XS_version_vcmp);
188 XS(XS_version_boolean);
189 #ifdef HASATTRIBUTE_NORETURN
190 XS(XS_version_noop) __attribute__noreturn__;
191 #else
192 XS(XS_version_noop);
193 #endif
194 XS(XS_version_is_alpha);
195 XS(XS_version_qv);
196 XS(XS_utf8_is_utf8);
197 XS(XS_utf8_valid);
198 XS(XS_utf8_encode);
199 XS(XS_utf8_decode);
200 XS(XS_utf8_upgrade);
201 XS(XS_utf8_downgrade);
202 XS(XS_utf8_unicode_to_native);
203 XS(XS_utf8_native_to_unicode);
204 XS(XS_Internals_SvREADONLY);
205 XS(XS_Internals_SvREFCNT);
206 XS(XS_Internals_hv_clear_placehold);
207 XS(XS_PerlIO_get_layers);
208 XS(XS_Internals_hash_seed);
209 XS(XS_Internals_rehash_seed);
210 XS(XS_Internals_HvREHASH);
211 XS(XS_Internals_inc_sub_generation);
212 XS(XS_re_is_regexp); 
213 XS(XS_re_regname);
214 XS(XS_re_regnames);
215 XS(XS_re_regnames_count);
216 XS(XS_Tie_Hash_NamedCapture_FETCH);
217 XS(XS_Tie_Hash_NamedCapture_STORE);
218 XS(XS_Tie_Hash_NamedCapture_DELETE);
219 XS(XS_Tie_Hash_NamedCapture_CLEAR);
220 XS(XS_Tie_Hash_NamedCapture_EXISTS);
221 XS(XS_Tie_Hash_NamedCapture_FIRSTK);
222 XS(XS_Tie_Hash_NamedCapture_NEXTK);
223 XS(XS_Tie_Hash_NamedCapture_SCALAR);
224 XS(XS_Tie_Hash_NamedCapture_flags);
225
226 void
227 Perl_boot_core_UNIVERSAL(pTHX)
228 {
229     dVAR;
230     static const char file[] = __FILE__;
231
232     newXS("UNIVERSAL::isa",             XS_UNIVERSAL_isa,         file);
233     newXS("UNIVERSAL::can",             XS_UNIVERSAL_can,         file);
234     newXS("UNIVERSAL::DOES",            XS_UNIVERSAL_DOES,        file);
235     newXS("UNIVERSAL::VERSION",         XS_UNIVERSAL_VERSION,     file);
236     {
237         /* register the overloading (type 'A') magic */
238         PL_amagic_generation++;
239         /* Make it findable via fetchmethod */
240         newXS("version::()", XS_version_noop, file);
241         newXS("version::new", XS_version_new, file);
242         newXS("version::(\"\"", XS_version_stringify, file);
243         newXS("version::stringify", XS_version_stringify, file);
244         newXS("version::(0+", XS_version_numify, file);
245         newXS("version::numify", XS_version_numify, file);
246         newXS("version::normal", XS_version_normal, file);
247         newXS("version::(cmp", XS_version_vcmp, file);
248         newXS("version::(<=>", XS_version_vcmp, file);
249         newXS("version::vcmp", XS_version_vcmp, file);
250         newXS("version::(bool", XS_version_boolean, file);
251         newXS("version::boolean", XS_version_boolean, file);
252         newXS("version::(nomethod", XS_version_noop, file);
253         newXS("version::noop", XS_version_noop, file);
254         newXS("version::is_alpha", XS_version_is_alpha, file);
255         newXS("version::qv", XS_version_qv, file);
256     }
257     newXS("utf8::is_utf8", XS_utf8_is_utf8, file);
258     newXS("utf8::valid", XS_utf8_valid, file);
259     newXS("utf8::encode", XS_utf8_encode, file);
260     newXS("utf8::decode", XS_utf8_decode, file);
261     newXS("utf8::upgrade", XS_utf8_upgrade, file);
262     newXS("utf8::downgrade", XS_utf8_downgrade, file);
263     newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
264     newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
265     newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
266     newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
267     newXSproto("Internals::hv_clear_placeholders",
268                XS_Internals_hv_clear_placehold, file, "\\%");
269     newXSproto("PerlIO::get_layers",
270                XS_PerlIO_get_layers, file, "*;@");
271     newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, "");
272     newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, file, "");
273     newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%");
274     newXSproto("re::is_regexp", XS_re_is_regexp, file, "$");
275     newXSproto("re::regname", XS_re_regname, file, ";$$");
276     newXSproto("re::regnames", XS_re_regnames, file, ";$");
277     newXSproto("re::regnames_count", XS_re_regnames_count, file, "");
278     newXS("Tie::Hash::NamedCapture::FETCH", XS_Tie_Hash_NamedCapture_FETCH, file);
279     newXS("Tie::Hash::NamedCapture::STORE", XS_Tie_Hash_NamedCapture_STORE, file);
280     newXS("Tie::Hash::NamedCapture::DELETE", XS_Tie_Hash_NamedCapture_DELETE, file);
281     newXS("Tie::Hash::NamedCapture::CLEAR", XS_Tie_Hash_NamedCapture_CLEAR, file);
282     newXS("Tie::Hash::NamedCapture::EXISTS", XS_Tie_Hash_NamedCapture_EXISTS, file);
283     newXS("Tie::Hash::NamedCapture::FIRSTKEY", XS_Tie_Hash_NamedCapture_FIRSTK, file);
284     newXS("Tie::Hash::NamedCapture::NEXTKEY", XS_Tie_Hash_NamedCapture_NEXTK, file);
285     newXS("Tie::Hash::NamedCapture::SCALAR", XS_Tie_Hash_NamedCapture_SCALAR, file);
286     newXS("Tie::Hash::NamedCapture::flags", XS_Tie_Hash_NamedCapture_flags, file);
287 }
288
289
290 XS(XS_UNIVERSAL_isa)
291 {
292     dVAR;
293     dXSARGS;
294     PERL_UNUSED_ARG(cv);
295
296     if (items != 2)
297         Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
298     else {
299         SV * const sv = ST(0);
300         const char *name;
301
302         SvGETMAGIC(sv);
303
304         if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
305                     || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
306             XSRETURN_UNDEF;
307
308         name = SvPV_nolen_const(ST(1));
309
310         ST(0) = boolSV(sv_derived_from(sv, name));
311         XSRETURN(1);
312     }
313 }
314
315 XS(XS_UNIVERSAL_can)
316 {
317     dVAR;
318     dXSARGS;
319     SV   *sv;
320     const char *name;
321     SV   *rv;
322     HV   *pkg = NULL;
323     PERL_UNUSED_ARG(cv);
324
325     if (items != 2)
326         Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
327
328     sv = ST(0);
329
330     SvGETMAGIC(sv);
331
332     if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
333                 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
334         XSRETURN_UNDEF;
335
336     name = SvPV_nolen_const(ST(1));
337     rv = &PL_sv_undef;
338
339     if (SvROK(sv)) {
340         sv = (SV*)SvRV(sv);
341         if (SvOBJECT(sv))
342             pkg = SvSTASH(sv);
343     }
344     else {
345         pkg = gv_stashsv(sv, 0);
346     }
347
348     if (pkg) {
349         GV * const gv = gv_fetchmethod_autoload(pkg, name, FALSE);
350         if (gv && isGV(gv))
351             rv = sv_2mortal(newRV((SV*)GvCV(gv)));
352     }
353
354     ST(0) = rv;
355     XSRETURN(1);
356 }
357
358 XS(XS_UNIVERSAL_DOES)
359 {
360     dVAR;
361     dXSARGS;
362     PERL_UNUSED_ARG(cv);
363
364     if (items != 2)
365         Perl_croak(aTHX_ "Usage: invocant->DOES(kind)");
366     else {
367         SV * const sv = ST(0);
368         const char *name;
369
370         name = SvPV_nolen_const(ST(1));
371         if (sv_does( sv, name ))
372             XSRETURN_YES;
373
374         XSRETURN_NO;
375     }
376 }
377
378 XS(XS_UNIVERSAL_VERSION)
379 {
380     dVAR;
381     dXSARGS;
382     HV *pkg;
383     GV **gvp;
384     GV *gv;
385     SV *sv;
386     const char *undef;
387     PERL_UNUSED_ARG(cv);
388
389     if (SvROK(ST(0))) {
390         sv = (SV*)SvRV(ST(0));
391         if (!SvOBJECT(sv))
392             Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
393         pkg = SvSTASH(sv);
394     }
395     else {
396         pkg = gv_stashsv(ST(0), 0);
397     }
398
399     gvp = pkg ? (GV**)hv_fetchs(pkg, "VERSION", FALSE) : NULL;
400
401     if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) {
402         SV * const nsv = sv_newmortal();
403         sv_setsv(nsv, sv);
404         sv = nsv;
405         if ( !sv_derived_from(sv, "version"))
406             upg_version(sv, FALSE);
407         undef = NULL;
408     }
409     else {
410         sv = (SV*)&PL_sv_undef;
411         undef = "(undef)";
412     }
413
414     if (items > 1) {
415         SV *req = ST(1);
416
417         if (undef) {
418             if (pkg) {
419                 const char * const name = HvNAME_get(pkg);
420                 Perl_croak(aTHX_
421                            "%s does not define $%s::VERSION--version check failed",
422                            name, name);
423             } else {
424                 Perl_croak(aTHX_
425                              "%s defines neither package nor VERSION--version check failed",
426                              SvPVx_nolen_const(ST(0)) );
427              }
428         }
429
430         if ( !sv_derived_from(req, "version")) {
431             /* req may very well be R/O, so create a new object */
432             req = sv_2mortal( new_version(req) );
433         }
434
435         if ( vcmp( req, sv ) > 0 ) {
436             if ( hv_exists((HV*)SvRV(req), "qv", 2 ) ) {
437                 Perl_croak(aTHX_ "%s version %"SVf" required--"
438                        "this is only version %"SVf"", HvNAME_get(pkg),
439                        SVfARG(vnormal(req)),
440                        SVfARG(vnormal(sv)));
441             } else {
442                 Perl_croak(aTHX_ "%s version %"SVf" required--"
443                        "this is only version %"SVf"", HvNAME_get(pkg),
444                        SVfARG(vstringify(req)),
445                        SVfARG(vstringify(sv)));
446             }
447         }
448
449     }
450
451     if ( SvOK(sv) && sv_derived_from(sv, "version") ) {
452         ST(0) = vstringify(sv);
453     } else {
454         ST(0) = sv;
455     }
456
457     XSRETURN(1);
458 }
459
460 XS(XS_version_new)
461 {
462     dVAR;
463     dXSARGS;
464     PERL_UNUSED_ARG(cv);
465     if (items > 3)
466         Perl_croak(aTHX_ "Usage: version::new(class, version)");
467     SP -= items;
468     {
469         SV *vs = ST(1);
470         SV *rv;
471         const char * const classname =
472             sv_isobject(ST(0)) /* get the class if called as an object method */
473                 ? HvNAME(SvSTASH(SvRV(ST(0))))
474                 : (char *)SvPV_nolen(ST(0));
475
476         if ( items == 1 || vs == &PL_sv_undef ) { /* no param or explicit undef */
477             /* create empty object */
478             vs = sv_newmortal();
479             sv_setpvn(vs,"",0);
480         }
481         else if ( items == 3 ) {
482             vs = sv_newmortal();
483             Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(ST(2)));
484         }
485
486         rv = new_version(vs);
487         if ( strcmp(classname,"version") != 0 ) /* inherited new() */
488             sv_bless(rv, gv_stashpv(classname, GV_ADD));
489
490         mPUSHs(rv);
491         PUTBACK;
492         return;
493     }
494 }
495
496 XS(XS_version_stringify)
497 {
498      dVAR;
499      dXSARGS;
500      PERL_UNUSED_ARG(cv);
501      if (items < 1)
502           Perl_croak(aTHX_ "Usage: version::stringify(lobj, ...)");
503      SP -= items;
504      {
505           SV *  lobj;
506
507           if (sv_derived_from(ST(0), "version")) {
508                lobj = SvRV(ST(0));
509           }
510           else
511                Perl_croak(aTHX_ "lobj is not of type version");
512
513           mPUSHs(vstringify(lobj));
514
515           PUTBACK;
516           return;
517      }
518 }
519
520 XS(XS_version_numify)
521 {
522      dVAR;
523      dXSARGS;
524      PERL_UNUSED_ARG(cv);
525      if (items < 1)
526           Perl_croak(aTHX_ "Usage: version::numify(lobj, ...)");
527      SP -= items;
528      {
529           SV *  lobj;
530
531           if (sv_derived_from(ST(0), "version")) {
532                lobj = SvRV(ST(0));
533           }
534           else
535                Perl_croak(aTHX_ "lobj is not of type version");
536
537           mPUSHs(vnumify(lobj));
538
539           PUTBACK;
540           return;
541      }
542 }
543
544 XS(XS_version_normal)
545 {
546      dVAR;
547      dXSARGS;
548      PERL_UNUSED_ARG(cv);
549      if (items < 1)
550           Perl_croak(aTHX_ "Usage: version::normal(lobj, ...)");
551      SP -= items;
552      {
553           SV *  lobj;
554
555           if (sv_derived_from(ST(0), "version")) {
556                lobj = SvRV(ST(0));
557           }
558           else
559                Perl_croak(aTHX_ "lobj is not of type version");
560
561           mPUSHs(vnormal(lobj));
562
563           PUTBACK;
564           return;
565      }
566 }
567
568 XS(XS_version_vcmp)
569 {
570      dVAR;
571      dXSARGS;
572      PERL_UNUSED_ARG(cv);
573      if (items < 1)
574           Perl_croak(aTHX_ "Usage: version::vcmp(lobj, ...)");
575      SP -= items;
576      {
577           SV *  lobj;
578
579           if (sv_derived_from(ST(0), "version")) {
580                lobj = SvRV(ST(0));
581           }
582           else
583                Perl_croak(aTHX_ "lobj is not of type version");
584
585           {
586                SV       *rs;
587                SV       *rvs;
588                SV * robj = ST(1);
589                const IV  swap = (IV)SvIV(ST(2));
590
591                if ( ! sv_derived_from(robj, "version") )
592                {
593                     robj = new_version(robj);
594                }
595                rvs = SvRV(robj);
596
597                if ( swap )
598                {
599                     rs = newSViv(vcmp(rvs,lobj));
600                }
601                else
602                {
603                     rs = newSViv(vcmp(lobj,rvs));
604                }
605
606                mPUSHs(rs);
607           }
608
609           PUTBACK;
610           return;
611      }
612 }
613
614 XS(XS_version_boolean)
615 {
616     dVAR;
617     dXSARGS;
618     PERL_UNUSED_ARG(cv);
619     if (items < 1)
620         Perl_croak(aTHX_ "Usage: version::boolean(lobj, ...)");
621     SP -= items;
622     if (sv_derived_from(ST(0), "version")) {
623         SV * const lobj = SvRV(ST(0));
624         SV * const rs = newSViv( vcmp(lobj,new_version(newSVpvs("0"))) );
625         mPUSHs(rs);
626         PUTBACK;
627         return;
628     }
629     else
630         Perl_croak(aTHX_ "lobj is not of type version");
631 }
632
633 XS(XS_version_noop)
634 {
635     dVAR;
636     dXSARGS;
637     PERL_UNUSED_ARG(cv);
638     if (items < 1)
639         Perl_croak(aTHX_ "Usage: version::noop(lobj, ...)");
640     if (sv_derived_from(ST(0), "version"))
641         Perl_croak(aTHX_ "operation not supported with version object");
642     else
643         Perl_croak(aTHX_ "lobj is not of type version");
644 #ifndef HASATTRIBUTE_NORETURN
645     XSRETURN_EMPTY;
646 #endif
647 }
648
649 XS(XS_version_is_alpha)
650 {
651     dVAR;
652     dXSARGS;
653     PERL_UNUSED_ARG(cv);
654     if (items != 1)
655         Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)");
656     SP -= items;
657     if (sv_derived_from(ST(0), "version")) {
658         SV * const lobj = ST(0);
659         if ( hv_exists((HV*)SvRV(lobj), "alpha", 5 ) )
660             XSRETURN_YES;
661         else
662             XSRETURN_NO;
663         PUTBACK;
664         return;
665     }
666     else
667         Perl_croak(aTHX_ "lobj is not of type version");
668 }
669
670 XS(XS_version_qv)
671 {
672     dVAR;
673     dXSARGS;
674     PERL_UNUSED_ARG(cv);
675     if (items != 1)
676         Perl_croak(aTHX_ "Usage: version::qv(ver)");
677     SP -= items;
678     {
679         SV *    ver = ST(0);
680         if ( !SvVOK(ver) ) { /* only need to do with if not already v-string */
681             SV * const rv = sv_newmortal();
682             sv_setsv(rv,ver); /* make a duplicate */
683             upg_version(rv, TRUE);
684             PUSHs(rv);
685         }
686         else
687         {
688             mPUSHs(new_version(ver));
689         }
690
691         PUTBACK;
692         return;
693     }
694 }
695
696 XS(XS_utf8_is_utf8)
697 {
698      dVAR;
699      dXSARGS;
700      PERL_UNUSED_ARG(cv);
701      if (items != 1)
702           Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
703      else {
704         const SV * const sv = ST(0);
705             if (SvUTF8(sv))
706                 XSRETURN_YES;
707             else
708                 XSRETURN_NO;
709      }
710      XSRETURN_EMPTY;
711 }
712
713 XS(XS_utf8_valid)
714 {
715      dVAR;
716      dXSARGS;
717      PERL_UNUSED_ARG(cv);
718      if (items != 1)
719           Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
720     else {
721         SV * const sv = ST(0);
722         STRLEN len;
723         const char * const s = SvPV_const(sv,len);
724         if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
725             XSRETURN_YES;
726         else
727             XSRETURN_NO;
728     }
729      XSRETURN_EMPTY;
730 }
731
732 XS(XS_utf8_encode)
733 {
734     dVAR;
735     dXSARGS;
736     PERL_UNUSED_ARG(cv);
737     if (items != 1)
738         Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
739     sv_utf8_encode(ST(0));
740     XSRETURN_EMPTY;
741 }
742
743 XS(XS_utf8_decode)
744 {
745     dVAR;
746     dXSARGS;
747     PERL_UNUSED_ARG(cv);
748     if (items != 1)
749         Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
750     else {
751         SV * const sv = ST(0);
752         const bool RETVAL = sv_utf8_decode(sv);
753         ST(0) = boolSV(RETVAL);
754         sv_2mortal(ST(0));
755     }
756     XSRETURN(1);
757 }
758
759 XS(XS_utf8_upgrade)
760 {
761     dVAR;
762     dXSARGS;
763     PERL_UNUSED_ARG(cv);
764     if (items != 1)
765         Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
766     else {
767         SV * const sv = ST(0);
768         STRLEN  RETVAL;
769         dXSTARG;
770
771         RETVAL = sv_utf8_upgrade(sv);
772         XSprePUSH; PUSHi((IV)RETVAL);
773     }
774     XSRETURN(1);
775 }
776
777 XS(XS_utf8_downgrade)
778 {
779     dVAR;
780     dXSARGS;
781     PERL_UNUSED_ARG(cv);
782     if (items < 1 || items > 2)
783         Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
784     else {
785         SV * const sv = ST(0);
786         const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
787         const bool RETVAL = sv_utf8_downgrade(sv, failok);
788
789         ST(0) = boolSV(RETVAL);
790         sv_2mortal(ST(0));
791     }
792     XSRETURN(1);
793 }
794
795 XS(XS_utf8_native_to_unicode)
796 {
797  dVAR;
798  dXSARGS;
799  const UV uv = SvUV(ST(0));
800  PERL_UNUSED_ARG(cv);
801
802  if (items > 1)
803      Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
804
805  ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
806  XSRETURN(1);
807 }
808
809 XS(XS_utf8_unicode_to_native)
810 {
811  dVAR;
812  dXSARGS;
813  const UV uv = SvUV(ST(0));
814  PERL_UNUSED_ARG(cv);
815
816  if (items > 1)
817      Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
818
819  ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
820  XSRETURN(1);
821 }
822
823 XS(XS_Internals_SvREADONLY)     /* This is dangerous stuff. */
824 {
825     dVAR;
826     dXSARGS;
827     SV * const sv = SvRV(ST(0));
828     PERL_UNUSED_ARG(cv);
829
830     if (items == 1) {
831          if (SvREADONLY(sv))
832              XSRETURN_YES;
833          else
834              XSRETURN_NO;
835     }
836     else if (items == 2) {
837         if (SvTRUE(ST(1))) {
838             SvREADONLY_on(sv);
839             XSRETURN_YES;
840         }
841         else {
842             /* I hope you really know what you are doing. */
843             SvREADONLY_off(sv);
844             XSRETURN_NO;
845         }
846     }
847     XSRETURN_UNDEF; /* Can't happen. */
848 }
849
850 XS(XS_Internals_SvREFCNT)       /* This is dangerous stuff. */
851 {
852     dVAR;
853     dXSARGS;
854     SV * const sv = SvRV(ST(0));
855     PERL_UNUSED_ARG(cv);
856
857     if (items == 1)
858          XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
859     else if (items == 2) {
860          /* I hope you really know what you are doing. */
861          SvREFCNT(sv) = SvIV(ST(1));
862          XSRETURN_IV(SvREFCNT(sv));
863     }
864     XSRETURN_UNDEF; /* Can't happen. */
865 }
866
867 XS(XS_Internals_hv_clear_placehold)
868 {
869     dVAR;
870     dXSARGS;
871     PERL_UNUSED_ARG(cv);
872
873     if (items != 1)
874         Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
875     else {
876         HV * const hv = (HV *) SvRV(ST(0));
877         hv_clear_placeholders(hv);
878         XSRETURN(0);
879     }
880 }
881
882 XS(XS_PerlIO_get_layers)
883 {
884     dVAR;
885     dXSARGS;
886     PERL_UNUSED_ARG(cv);
887     if (items < 1 || items % 2 == 0)
888         Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
889 #ifdef USE_PERLIO
890     {
891         SV *    sv;
892         GV *    gv;
893         IO *    io;
894         bool    input = TRUE;
895         bool    details = FALSE;
896
897         if (items > 1) {
898              SV * const *svp;
899              for (svp = MARK + 2; svp <= SP; svp += 2) {
900                   SV * const * const varp = svp;
901                   SV * const * const valp = svp + 1;
902                   STRLEN klen;
903                   const char * const key = SvPV_const(*varp, klen);
904
905                   switch (*key) {
906                   case 'i':
907                        if (klen == 5 && memEQ(key, "input", 5)) {
908                             input = SvTRUE(*valp);
909                             break;
910                        }
911                        goto fail;
912                   case 'o': 
913                        if (klen == 6 && memEQ(key, "output", 6)) {
914                             input = !SvTRUE(*valp);
915                             break;
916                        }
917                        goto fail;
918                   case 'd':
919                        if (klen == 7 && memEQ(key, "details", 7)) {
920                             details = SvTRUE(*valp);
921                             break;
922                        }
923                        goto fail;
924                   default:
925                   fail:
926                        Perl_croak(aTHX_
927                                   "get_layers: unknown argument '%s'",
928                                   key);
929                   }
930              }
931
932              SP -= (items - 1);
933         }
934
935         sv = POPs;
936         gv = (GV*)sv;
937
938         if (!isGV(sv)) {
939              if (SvROK(sv) && isGV(SvRV(sv)))
940                   gv = (GV*)SvRV(sv);
941              else if (SvPOKp(sv))
942                   gv = gv_fetchsv(sv, 0, SVt_PVIO);
943         }
944
945         if (gv && (io = GvIO(gv))) {
946              AV* const av = PerlIO_get_layers(aTHX_ input ?
947                                         IoIFP(io) : IoOFP(io));
948              I32 i;
949              const I32 last = av_len(av);
950              I32 nitem = 0;
951              
952              for (i = last; i >= 0; i -= 3) {
953                   SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
954                   SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
955                   SV * const * const flgsvp = av_fetch(av, i,     FALSE);
956
957                   const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
958                   const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
959                   const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
960
961                   if (details) {
962                        XPUSHs(namok
963                               ? sv_2mortal(newSVpvn(SvPVX_const(*namsvp), SvCUR(*namsvp)))
964                               : &PL_sv_undef);
965                        XPUSHs(argok
966                               ? sv_2mortal(newSVpvn(SvPVX_const(*argsvp), SvCUR(*argsvp)))
967                               : &PL_sv_undef);
968                        if (flgok)
969                             mXPUSHi(SvIVX(*flgsvp));
970                        else
971                             XPUSHs(&PL_sv_undef);
972                        nitem += 3;
973                   }
974                   else {
975                        if (namok && argok)
976                             XPUSHs(sv_2mortal(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
977                                                  SVfARG(*namsvp),
978                                                  SVfARG(*argsvp))));
979                        else if (namok)
980                             XPUSHs(sv_2mortal(Perl_newSVpvf(aTHX_ "%"SVf,
981                                                  SVfARG(*namsvp))));
982                        else
983                             XPUSHs(&PL_sv_undef);
984                        nitem++;
985                        if (flgok) {
986                             const IV flags = SvIVX(*flgsvp);
987
988                             if (flags & PERLIO_F_UTF8) {
989                                  XPUSHs(newSVpvs_flags("utf8", SVs_TEMP));
990                                  nitem++;
991                             }
992                        }
993                   }
994              }
995
996              SvREFCNT_dec(av);
997
998              XSRETURN(nitem);
999         }
1000     }
1001 #endif
1002
1003     XSRETURN(0);
1004 }
1005
1006 XS(XS_Internals_hash_seed)
1007 {
1008     dVAR;
1009     /* Using dXSARGS would also have dITEM and dSP,
1010      * which define 2 unused local variables.  */
1011     dAXMARK;
1012     PERL_UNUSED_ARG(cv);
1013     PERL_UNUSED_VAR(mark);
1014     XSRETURN_UV(PERL_HASH_SEED);
1015 }
1016
1017 XS(XS_Internals_rehash_seed)
1018 {
1019     dVAR;
1020     /* Using dXSARGS would also have dITEM and dSP,
1021      * which define 2 unused local variables.  */
1022     dAXMARK;
1023     PERL_UNUSED_ARG(cv);
1024     PERL_UNUSED_VAR(mark);
1025     XSRETURN_UV(PL_rehash_seed);
1026 }
1027
1028 XS(XS_Internals_HvREHASH)       /* Subject to change  */
1029 {
1030     dVAR;
1031     dXSARGS;
1032     PERL_UNUSED_ARG(cv);
1033     if (SvROK(ST(0))) {
1034         const HV * const hv = (HV *) SvRV(ST(0));
1035         if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
1036             if (HvREHASH(hv))
1037                 XSRETURN_YES;
1038             else
1039                 XSRETURN_NO;
1040         }
1041     }
1042     Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
1043 }
1044
1045 XS(XS_re_is_regexp)
1046 {
1047     dVAR; 
1048     dXSARGS;
1049     PERL_UNUSED_VAR(cv);
1050
1051     if (items != 1)
1052        Perl_croak(aTHX_ "Usage: %s(%s)", "re::is_regexp", "sv");
1053
1054     SP -= items;
1055
1056     if (SvRXOK(ST(0))) {
1057         XSRETURN_YES;
1058     } else {
1059         XSRETURN_NO;
1060     }
1061 }
1062
1063 XS(XS_re_regnames_count)
1064 {
1065     REGEXP *rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1066     SV * ret;
1067     dVAR; 
1068     dXSARGS;
1069     PERL_UNUSED_ARG(cv);
1070
1071     if (items != 0)
1072        Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames_count", "");
1073
1074     SP -= items;
1075
1076     if (!rx)
1077         XSRETURN_UNDEF;
1078
1079     ret = CALLREG_NAMED_BUFF_COUNT(rx);
1080
1081     SPAGAIN;
1082
1083     if (ret) {
1084         XPUSHs(ret);
1085         PUTBACK;
1086         return;
1087     } else {
1088         XSRETURN_UNDEF;
1089     }
1090 }
1091
1092 XS(XS_re_regname)
1093 {
1094     dVAR;
1095     dXSARGS;
1096     REGEXP * rx;
1097     U32 flags;
1098     SV * ret;
1099     PERL_UNUSED_ARG(cv);
1100
1101     if (items < 1 || items > 2)
1102         Perl_croak(aTHX_ "Usage: %s(%s)", "re::regname", "name[, all ]");
1103
1104     SP -= items;
1105
1106     rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1107
1108     if (!rx)
1109         XSRETURN_UNDEF;
1110
1111     if (items == 2 && SvTRUE(ST(1))) {
1112         flags = RXapif_ALL;
1113     } else {
1114         flags = RXapif_ONE;
1115     }
1116     ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(0), (flags | RXapif_REGNAME));
1117
1118     if (ret) {
1119         if (SvROK(ret))
1120             XPUSHs(ret);
1121         else
1122             XPUSHs(SvREFCNT_inc(ret));
1123         XSRETURN(1);
1124     }
1125     XSRETURN_UNDEF;    
1126 }
1127
1128
1129 XS(XS_re_regnames)
1130 {
1131     dVAR;
1132     dXSARGS;
1133     REGEXP * rx;
1134     U32 flags;
1135     SV *ret;
1136     AV *av;
1137     I32 length;
1138     I32 i;
1139     SV **entry;
1140     PERL_UNUSED_ARG(cv);
1141
1142     if (items > 1)
1143         Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames", "[all]");
1144
1145     rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1146
1147     if (!rx)
1148         XSRETURN_UNDEF;
1149
1150     if (items == 1 && SvTRUE(ST(0))) {
1151         flags = RXapif_ALL;
1152     } else {
1153         flags = RXapif_ONE;
1154     }
1155
1156     SP -= items;
1157
1158     ret = CALLREG_NAMED_BUFF_ALL(rx, (flags | RXapif_REGNAMES));
1159
1160     SPAGAIN;
1161
1162     SP -= items;
1163
1164     if (!ret)
1165         XSRETURN_UNDEF;
1166
1167     av = (AV*)SvRV(ret);
1168     length = av_len(av);
1169
1170     for (i = 0; i <= length; i++) {
1171         entry = av_fetch(av, i, FALSE);
1172         
1173         if (!entry)
1174             Perl_croak(aTHX_ "NULL array element in re::regnames()");
1175
1176         XPUSHs(*entry);
1177     }
1178     PUTBACK;
1179     return;
1180 }
1181
1182 XS(XS_Tie_Hash_NamedCapture_FETCH)
1183 {
1184     dVAR;
1185     dXSARGS;
1186     REGEXP * rx;
1187     U32 flags;
1188     SV * ret;
1189     PERL_UNUSED_ARG(cv);
1190
1191     if (items != 2)
1192         Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::STORE($key, $flags)");
1193
1194     rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1195
1196     if (!rx)
1197         XSRETURN_UNDEF;
1198
1199     SP -= items;
1200
1201     flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1202     ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(1), flags);
1203
1204     SPAGAIN;
1205
1206     if (ret) {
1207         if (SvROK(ret))
1208             XPUSHs(ret);
1209         else
1210             XPUSHs(SvREFCNT_inc(ret));
1211         PUTBACK;
1212         return;
1213     }
1214     XSRETURN_UNDEF;
1215 }
1216
1217 XS(XS_Tie_Hash_NamedCapture_STORE)
1218 {
1219     dVAR;
1220     dXSARGS;
1221     REGEXP * rx;
1222     U32 flags;
1223     PERL_UNUSED_ARG(cv);
1224
1225     if (items != 3)
1226         Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::STORE($key, $value, $flags)");
1227
1228     rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1229
1230     if (!rx) {
1231         if (!PL_localizing)
1232             Perl_croak(aTHX_ PL_no_modify);
1233         else
1234             XSRETURN_UNDEF;
1235     }
1236
1237     SP -= items;
1238
1239     flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1240     CALLREG_NAMED_BUFF_STORE(rx,ST(1), ST(2), flags);
1241 }
1242
1243 XS(XS_Tie_Hash_NamedCapture_DELETE)
1244 {
1245     dVAR;
1246     dXSARGS;
1247     REGEXP * rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1248     U32 flags;
1249     PERL_UNUSED_ARG(cv);
1250
1251     if (items != 2)
1252         Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::DELETE($key, $flags)");
1253
1254     if (!rx)
1255         Perl_croak(aTHX_ PL_no_modify);
1256
1257     SP -= items;
1258
1259     flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1260     CALLREG_NAMED_BUFF_DELETE(rx, ST(1), flags);
1261 }
1262
1263 XS(XS_Tie_Hash_NamedCapture_CLEAR)
1264 {
1265     dVAR;
1266     dXSARGS;
1267     REGEXP * rx;
1268     U32 flags;
1269     PERL_UNUSED_ARG(cv);
1270
1271     if (items != 1)
1272         Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::CLEAR($flags)");
1273
1274     rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1275
1276     if (!rx)
1277         Perl_croak(aTHX_ PL_no_modify);
1278
1279     SP -= items;
1280
1281     flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1282     CALLREG_NAMED_BUFF_CLEAR(rx, flags);
1283 }
1284
1285 XS(XS_Tie_Hash_NamedCapture_EXISTS)
1286 {
1287     dVAR;
1288     dXSARGS;
1289     REGEXP * rx;
1290     U32 flags;
1291     SV * ret;
1292     PERL_UNUSED_ARG(cv);
1293
1294     if (items != 2)
1295         Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::EXISTS($key, $flags)");
1296
1297     rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1298
1299     if (!rx)
1300         XSRETURN_UNDEF;
1301
1302     SP -= items;
1303
1304     flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1305     ret = CALLREG_NAMED_BUFF_EXISTS(rx, ST(1), flags);
1306
1307     SPAGAIN;
1308
1309         XPUSHs(ret);
1310         PUTBACK;
1311         return;
1312 }
1313
1314 XS(XS_Tie_Hash_NamedCapture_FIRSTK)
1315 {
1316     dVAR;
1317     dXSARGS;
1318     REGEXP * rx;
1319     U32 flags;
1320     SV * ret;
1321     PERL_UNUSED_ARG(cv);
1322
1323     if (items != 1)
1324         Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::FIRSTKEY()");
1325
1326     rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1327
1328     if (!rx)
1329         XSRETURN_UNDEF;
1330
1331     SP -= items;
1332
1333     flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1334     ret = CALLREG_NAMED_BUFF_FIRSTKEY(rx, flags);
1335
1336     SPAGAIN;
1337
1338     if (ret) {
1339         XPUSHs(SvREFCNT_inc(ret));
1340         PUTBACK;
1341     } else {
1342         XSRETURN_UNDEF;
1343     }
1344
1345 }
1346
1347 XS(XS_Tie_Hash_NamedCapture_NEXTK)
1348 {
1349     dVAR;
1350     dXSARGS;
1351     REGEXP * rx;
1352     U32 flags;
1353     SV * ret;
1354     PERL_UNUSED_ARG(cv);
1355
1356     if (items != 2)
1357         Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::NEXTKEY($lastkey)");
1358
1359     rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1360
1361     if (!rx)
1362         XSRETURN_UNDEF;
1363
1364     SP -= items;
1365
1366     flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1367     ret = CALLREG_NAMED_BUFF_NEXTKEY(rx, ST(1), flags);
1368
1369     SPAGAIN;
1370
1371     if (ret) {
1372         XPUSHs(ret);
1373     } else {
1374         XSRETURN_UNDEF;
1375     }  
1376     PUTBACK;
1377 }
1378
1379 XS(XS_Tie_Hash_NamedCapture_SCALAR)
1380 {
1381     dVAR;
1382     dXSARGS;
1383     REGEXP * rx;
1384     U32 flags;
1385     SV * ret;
1386     PERL_UNUSED_ARG(cv);
1387
1388     if (items != 1)
1389         Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::SCALAR()");
1390
1391     rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1392
1393     if (!rx)
1394         XSRETURN_UNDEF;
1395
1396     SP -= items;
1397
1398     flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1399     ret = CALLREG_NAMED_BUFF_SCALAR(rx, flags);
1400
1401     SPAGAIN;
1402
1403     if (ret) {
1404         XPUSHs(ret);
1405         PUTBACK;
1406         return;
1407     } else {
1408         XSRETURN_UNDEF;
1409     }
1410 }
1411
1412 XS(XS_Tie_Hash_NamedCapture_flags)
1413 {
1414     dVAR;
1415     dXSARGS;
1416     PERL_UNUSED_ARG(cv);
1417
1418     if (items != 0)
1419         Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::flags()");
1420
1421         mXPUSHu(RXapif_ONE);
1422         mXPUSHu(RXapif_ALL);
1423         PUTBACK;
1424         return;
1425 }
1426
1427
1428 /*
1429  * Local variables:
1430  * c-indentation-style: bsd
1431  * c-basic-offset: 4
1432  * indent-tabs-mode: t
1433  * End:
1434  *
1435  * ex: set ts=8 sts=4 sw=4 noet:
1436  */