This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Free temporary scalars later, because they might be still needed
[perl5.git] / dump.c
diff --git a/dump.c b/dump.c
index 2d01693..aa1228b 100644 (file)
--- a/dump.c
+++ b/dump.c
@@ -175,9 +175,9 @@ will also be escaped.
 Normally the SV will be cleared before the escaped string is prepared,
 but when PERL_PV_ESCAPE_NOCLEAR is set this will not occur.
 
-If PERL_PV_ESCAPE_UNI is set then the input string is treated as unicode,
+If PERL_PV_ESCAPE_UNI is set then the input string is treated as Unicode,
 if PERL_PV_ESCAPE_UNI_DETECT is set then the input string is scanned
-using C<is_utf8_string()> to determine if it is unicode.
+using C<is_utf8_string()> to determine if it is Unicode.
 
 If PERL_PV_ESCAPE_ALL is set then all input chars will be output
 using C<\x01F1> style escapes, otherwise only chars above 255 will be
@@ -214,13 +214,15 @@ Perl_pv_escape( pTHX_ SV *dsv, char const * const str,
     STRLEN wrote = 0;    /* chars written so far */
     STRLEN chsize = 0;   /* size of data to be written */
     STRLEN readsize = 1; /* size of data just read */
-    bool isuni= flags & PERL_PV_ESCAPE_UNI ? 1 : 0; /* is this unicode */
+    bool isuni= flags & PERL_PV_ESCAPE_UNI ? 1 : 0; /* is this Unicode */
     const char *pv  = str;
     const char * const end = pv + count; /* end of string */
     octbuf[0] = esc;
 
-    if (!flags & PERL_PV_ESCAPE_NOCLEAR) 
+    if (!(flags & PERL_PV_ESCAPE_NOCLEAR)) {
+           /* This won't alter the UTF-8 flag */
            sv_setpvn(dsv, "", 0);
+    }
     
     if ((flags & PERL_PV_ESCAPE_UNI_DETECT) && is_utf8_string((U8*)pv, count))
         isuni = 1;
@@ -279,6 +281,12 @@ Perl_pv_escape( pTHX_ SV *dsv, char const * const str,
             sv_catpvn(dsv, octbuf, chsize);
             wrote += chsize;
        } else {
+           /* If PERL_PV_ESCAPE_NOBACKSLASH is set then bytes in the range
+              128-255 can be appended raw to the dsv. If dsv happens to be
+              UTF-8 then we need catpvf to upgrade them for us.
+              Or add a new API call sv_catpvc(). Think about that name, and
+              how to keep it clear that it's unlike the s of catpvs, which is
+              really an array octets, not a string.  */
             Perl_sv_catpvf( aTHX_ dsv, "%c", c);
            wrote++;
        }
@@ -296,21 +304,21 @@ Perl_pv_escape( pTHX_ SV *dsv, char const * const str,
            |const U32 flags
 
 Converts a string into something presentable, handling escaping via
-pv_escape() and supporting quoting and elipses. 
+pv_escape() and supporting quoting and ellipses.
 
 If the PERL_PV_PRETTY_QUOTE flag is set then the result will be 
 double quoted with any double quotes in the string escaped. Otherwise
 if the PERL_PV_PRETTY_LTGT flag is set then the result be wrapped in
 angle brackets. 
            
-If the PERL_PV_PRETTY_ELIPSES flag is set and not all characters in
-string were output then an elipses C<...> will be appended to the 
+If the PERL_PV_PRETTY_ELLIPSES flag is set and not all characters in
+string were output then an ellipsis C<...> will be appended to the
 string. Note that this happens AFTER it has been quoted.
            
 If start_color is non-null then it will be inserted after the opening
 quote (if there is one) but before the escaped text. If end_color
 is non-null then it will be inserted after the escaped text but before
-any quotes or elipses.
+any quotes or ellipses.
 
 Returns a pointer to the prettified text as held by dsv.
            
@@ -325,12 +333,15 @@ Perl_pv_pretty( pTHX_ SV *dsv, char const * const str, const STRLEN count,
     const U8 dq = (flags & PERL_PV_PRETTY_QUOTE) ? '"' : '%';
     STRLEN escaped;
     
+    if (!(flags & PERL_PV_PRETTY_NOCLEAR)) {
+           /* This won't alter the UTF-8 flag */
+           sv_setpvn(dsv, "", 0);
+    }
+
     if ( dq == '"' )
-        sv_setpvn(dsv, "\"", 1);
+        sv_catpvn(dsv, "\"", 1);
     else if ( flags & PERL_PV_PRETTY_LTGT )
-        sv_setpvn(dsv, "<", 1);
-    else 
-        sv_setpvn(dsv, "", 0);
+        sv_catpvn(dsv, "<", 1);
         
     if ( start_color != NULL ) 
         Perl_sv_catpv( aTHX_ dsv, start_color);
@@ -345,7 +356,7 @@ Perl_pv_pretty( pTHX_ SV *dsv, char const * const str, const STRLEN count,
     else if ( flags & PERL_PV_PRETTY_LTGT )
         sv_catpvn( dsv, ">", 1);         
     
-    if ( (flags & PERL_PV_PRETTY_ELIPSES) && ( escaped < count ) )
+    if ( (flags & PERL_PV_PRETTY_ELLIPSES) && ( escaped < count ) )
            sv_catpvn( dsv, "...", 3 );
  
     return SvPVX(dsv);
@@ -491,7 +502,7 @@ Perl_sv_peek(pTHX_ SV *sv)
            Perl_sv_catpvf(aTHX_ t, "%s)", pv_display(tmp, SvPVX_const(sv), SvCUR(sv), SvLEN(sv), 127));
            if (SvUTF8(sv))
                Perl_sv_catpvf(aTHX_ t, " [UTF8 \"%s\"]",
-                              sv_uni_display(tmp, sv, 8 * sv_len_utf8(sv),
+                              sv_uni_display(tmp, sv, 6 * SvCUR(sv),
                                              UNI_DISPLAY_QQ));
            SvREFCNT_dec(tmp);
        }
@@ -631,7 +642,7 @@ S_sequence(pTHX_ register const OP *o)
        switch (o->op_type) {
        case OP_STUB:
            if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
-               hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
+               (void)hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
                break;
            }
            goto nothin;
@@ -649,7 +660,7 @@ S_sequence(pTHX_ register const OP *o)
          nothin:
            if (oldop && o->op_next)
                continue;
-           hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
+           (void)hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
            break;
 
        case OP_MAPWHILE:
@@ -662,20 +673,20 @@ S_sequence(pTHX_ register const OP *o)
        case OP_DORASSIGN:
        case OP_COND_EXPR:
        case OP_RANGE:
-           hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
+           (void)hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
            sequence_tail(cLOGOPo->op_other);
            break;
 
        case OP_ENTERLOOP:
        case OP_ENTERITER:
-           hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
+           (void)hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
            sequence_tail(cLOOPo->op_redoop);
            sequence_tail(cLOOPo->op_nextop);
            sequence_tail(cLOOPo->op_lastop);
            break;
 
        case OP_SUBST:
-           hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
+           (void)hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
            sequence_tail(cPMOPo->op_pmstashstartu.op_pmreplstart);
            break;
 
@@ -685,7 +696,7 @@ S_sequence(pTHX_ register const OP *o)
            break;
 
        default:
-           hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
+           (void)hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
            break;
        }
        oldop = o;
@@ -1170,7 +1181,6 @@ static const struct { const char type; const char *name; } magic_names[] = {
        { PERL_MAGIC_isaelem,        "isaelem(i)" },
        { PERL_MAGIC_nkeys,          "nkeys(k)" },
        { PERL_MAGIC_dbline,         "dbline(l)" },
-       { PERL_MAGIC_mutex,          "mutex(m)" },
        { PERL_MAGIC_shared_scalar,  "shared_scalar(n)" },
        { PERL_MAGIC_collxfrm,       "collxfrm(o)" },
        { PERL_MAGIC_tiedelem,       "tiedelem(p)" },
@@ -1277,7 +1287,7 @@ Perl_do_magic_dump(pTHX_ I32 level, PerlIO *file, const MAGIC *mg, I32 nest, I32
                SV * const dsv = sv_newmortal();
                 const char * const s =  pv_pretty(dsv, re->wrapped, re->wraplen, 
                     60, NULL, NULL,
-                    ( PERL_PV_PRETTY_QUOTE | PERL_PV_ESCAPE_RE | PERL_PV_PRETTY_ELIPSES |
+                    ( PERL_PV_PRETTY_QUOTE | PERL_PV_ESCAPE_RE | PERL_PV_PRETTY_ELLIPSES |
                     ((re->extflags & RXf_UTF8) ? PERL_PV_ESCAPE_UNI : 0))
                 );
                Perl_dump_indent(aTHX_ level+1, file, "    PAT = %s\n", s);
@@ -1437,7 +1447,6 @@ Perl_do_sv_dump(pTHX_ I32 level, PerlIO *file, SV *sv, I32 nest, I32 maxnest, bo
        if (CvMETHOD(sv))       sv_catpv(d, "METHOD,");
        if (CvLOCKED(sv))       sv_catpv(d, "LOCKED,");
        if (CvWEAKOUTSIDE(sv))  sv_catpv(d, "WEAKOUTSIDE,");
-       if (CvASSERTION(sv))    sv_catpv(d, "ASSERTION,");
        break;
     case SVt_PVHV:
        if (HvSHAREKEYS(sv))    sv_catpv(d, "SHAREKEYS,");
@@ -1572,8 +1581,8 @@ Perl_do_sv_dump(pTHX_ I32 level, PerlIO *file, SV *sv, I32 nest, I32 maxnest, bo
            if (SvOOK(sv))
                PerlIO_printf(file, "( %s . ) ", pv_display(d, SvPVX_const(sv)-SvIVX(sv), SvIVX(sv), 0, pvlim));
            PerlIO_printf(file, "%s", pv_display(d, SvPVX_const(sv), SvCUR(sv), SvLEN(sv), pvlim));
-           if (SvUTF8(sv)) /* the 8?  \x{....} */
-               PerlIO_printf(file, " [UTF8 \"%s\"]", sv_uni_display(d, sv, 8 * sv_len_utf8(sv), UNI_DISPLAY_QQ));
+           if (SvUTF8(sv)) /* the 6?  \x{....} */
+               PerlIO_printf(file, " [UTF8 \"%s\"]", sv_uni_display(d, sv, 6 * SvCUR(sv), UNI_DISPLAY_QQ));
            PerlIO_printf(file, "\n");
            Perl_dump_indent(aTHX_ level, file, "  CUR = %"IVdf"\n", (IV)SvCUR(sv));
            Perl_dump_indent(aTHX_ level, file, "  LEN = %"IVdf"\n", (IV)SvLEN(sv));
@@ -1718,7 +1727,7 @@ Perl_do_sv_dump(pTHX_ I32 level, PerlIO *file, SV *sv, I32 nest, I32 maxnest, bo
 
                Perl_dump_indent(aTHX_ level+1, file, "Elt %s ", pv_display(d, keypv, len, 0, pvlim));
                if (SvUTF8(keysv))
-                   PerlIO_printf(file, "[UTF8 \"%s\"] ", sv_uni_display(d, keysv, 8 * sv_len_utf8(keysv), UNI_DISPLAY_QQ));
+                   PerlIO_printf(file, "[UTF8 \"%s\"] ", sv_uni_display(d, keysv, 6 * SvCUR(keysv), UNI_DISPLAY_QQ));
                if (HeKREHASH(he))
                    PerlIO_printf(file, "[REHASH] ");
                PerlIO_printf(file, "HASH = 0x%"UVxf"\n", (UV)hash);
@@ -1867,7 +1876,6 @@ Perl_do_sv_dump(pTHX_ I32 level, PerlIO *file, SV *sv, I32 nest, I32 maxnest, bo
            do_sv_dump (level+1, file, (SV *) IoBOTTOM_GV(sv), nest+1, maxnest,
                        dumpops, pvlim);
        }
-       Perl_dump_indent(aTHX_ level, file, "  SUBPROCESS = %"IVdf"\n", (IV)IoSUBPROCESS(sv));
        if (isPRINT(IoTYPE(sv)))
             Perl_dump_indent(aTHX_ level, file, "  TYPE = '%c'\n", IoTYPE(sv));
        else
@@ -1882,7 +1890,10 @@ void
 Perl_sv_dump(pTHX_ SV *sv)
 {
     dVAR;
-    do_sv_dump(0, Perl_debug_log, sv, 0, 0, 0, 0);
+    if (SvROK(sv))
+       do_sv_dump(0, Perl_debug_log, sv, 0, 4, 0, 0);
+    else
+       do_sv_dump(0, Perl_debug_log, sv, 0, 0, 0, 0);
 }
 
 int
@@ -2104,7 +2115,7 @@ Perl_xmldump_sub(pTHX_ const GV *gv)
 {
     SV * const sv = sv_newmortal();
 
-    gv_fullname3(sv, gv, Nullch);
+    gv_fullname3(sv, gv, NULL);
     Perl_xmldump_indent(aTHX_ 0, PL_xmlfp, "\nSUB %s = ", SvPVX(sv));
     if (CvXSUB(GvCV(gv)))
        Perl_xmldump_indent(aTHX_ 0, PL_xmlfp, "(xsub 0x%"UVxf" %d)\n",
@@ -2121,7 +2132,7 @@ Perl_xmldump_form(pTHX_ const GV *gv)
 {
     SV * const sv = sv_newmortal();
 
-    gv_fullname3(sv, gv, Nullch);
+    gv_fullname3(sv, gv, NULL);
     Perl_xmldump_indent(aTHX_ 0, PL_xmlfp, "\nFORMAT %s = ", SvPVX(sv));
     if (CvROOT(GvFORM(gv)))
        op_xmldump(CvROOT(GvFORM(gv)));
@@ -2142,11 +2153,11 @@ Perl_sv_catxmlsv(pTHX_ SV *dsv, SV *ssv)
 }
 
 char *
-Perl_sv_catxmlpvn(pTHX_ SV *dsv, char* pv, STRLEN len, int utf8)
+Perl_sv_catxmlpvn(pTHX_ SV *dsv, const char *pv, STRLEN len, int utf8)
 {
     unsigned int c;
     const char * const e = pv + len;
-    char *start = pv;
+    const char * const start = pv;
     STRLEN dsvcur;
     STRLEN cl;
 
@@ -2226,16 +2237,16 @@ Perl_sv_catxmlpvn(pTHX_ SV *dsv, char* pv, STRLEN len, int utf8)
            Perl_sv_catpvf(aTHX_ dsv, "STUPIDXML(#x%X)", c);
            break;
        case '<':
-           Perl_sv_catpvf(aTHX_ dsv, "&lt;");
+           sv_catpvs(dsv, "&lt;");
            break;
        case '>':
-           Perl_sv_catpvf(aTHX_ dsv, "&gt;");
+           sv_catpvs(dsv, "&gt;");
            break;
        case '&':
-           Perl_sv_catpvf(aTHX_ dsv, "&amp;");
+           sv_catpvs(dsv, "&amp;");
            break;
        case '"':
-           Perl_sv_catpvf(aTHX_ dsv, "&#34;");
+           sv_catpvs(dsv, "&#34;");
            break;
        default:
            if (c < 0xD800) {
@@ -2243,7 +2254,8 @@ Perl_sv_catxmlpvn(pTHX_ SV *dsv, char* pv, STRLEN len, int utf8)
                    Perl_sv_catpvf(aTHX_ dsv, "&#x%X;", c);
                }
                else {
-                   Perl_sv_catpvf(aTHX_ dsv, "%c", c);
+                   const char string = (char) c;
+                   sv_catpvn(dsv, &string, 1);
                }
                break;
            }
@@ -2736,7 +2748,7 @@ Perl_do_op_xmldump(pTHX_ I32 level, PerlIO *file, const OP *o)
            ENTER;
            SAVEFREESV(tmpsv1);
            SAVEFREESV(tmpsv2);
-           gv_fullname3(tmpsv1, (GV*)cSVOPo->op_sv, Nullch);
+           gv_fullname3(tmpsv1, (GV*)cSVOPo->op_sv, NULL);
            s = SvPV(tmpsv1,len);
            sv_catxmlpvn(tmpsv2, s, len, 1);
            S_xmldump_attr(aTHX_ level, file, "gv=\"%s\"", SvPV(tmpsv2, len));
@@ -2819,7 +2831,7 @@ Perl_do_op_xmldump(pTHX_ I32 level, PerlIO *file, const OP *o)
     if (PL_madskills && o->op_madprop) {
        char prevkey = '\0';
        SV * const tmpsv = newSVpvn("", 0);
-       const MADPROP* const mp = o->op_madprop;
+       const MADPROP* mp = o->op_madprop;
 
        sv_utf8_upgrade(tmpsv);
        if (!contents) {