Code cleanup based on turning off the -woffs in IRIX.
[p5sagit/p5-mst-13.2.git] / ext / Data / Dumper / Dumper.xs
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5
6 #ifndef PERL_VERSION
7 #include "patchlevel.h"
8 #define PERL_VERSION PATCHLEVEL
9 #endif
10
11 #if PERL_VERSION < 5
12 #  ifndef PL_sv_undef
13 #    define PL_sv_undef sv_undef
14 #  endif
15 #  ifndef ERRSV
16 #    define ERRSV       GvSV(errgv)
17 #  endif
18 #  ifndef newSVpvn
19 #    define newSVpvn    newSVpv
20 #  endif
21 #endif
22
23 static I32 num_q (char *s, STRLEN slen);
24 static I32 esc_q (char *dest, char *src, STRLEN slen);
25 static I32 esc_q_utf8 (pTHX_ SV *sv, char *src, STRLEN slen);
26 static SV *sv_x (pTHX_ SV *sv, char *str, STRLEN len, I32 n);
27 static I32 DD_dump (pTHX_ SV *val, char *name, STRLEN namelen, SV *retval,
28                     HV *seenhv, AV *postav, I32 *levelp, I32 indent,
29                     SV *pad, SV *xpad, SV *apad, SV *sep,
30                     SV *freezer, SV *toaster,
31                     I32 purity, I32 deepcopy, I32 quotekeys, SV *bless,
32                     I32 maxdepth);
33
34 /* does a string need to be protected? */
35 static I32
36 needs_quote(register char *s)
37 {
38 TOP:
39     if (s[0] == ':') {
40         if (*++s) {
41             if (*s++ != ':')
42                 return 1;
43         }
44         else
45             return 1;
46     }
47     if (isIDFIRST(*s)) {
48         while (*++s)
49             if (!isALNUM(*s)) {
50                 if (*s == ':')
51                     goto TOP;
52                 else
53                     return 1;
54             }
55     }
56     else
57         return 1;
58     return 0;
59 }
60
61 /* count the number of "'"s and "\"s in string */
62 static I32
63 num_q(register char *s, register STRLEN slen)
64 {
65     register I32 ret = 0;
66
67     while (slen > 0) {
68         if (*s == '\'' || *s == '\\')
69             ++ret;
70         ++s;
71         --slen;
72     }
73     return ret;
74 }
75
76
77 /* returns number of chars added to escape "'"s and "\"s in s */
78 /* slen number of characters in s will be escaped */
79 /* destination must be long enough for additional chars */
80 static I32
81 esc_q(register char *d, register char *s, register STRLEN slen)
82 {
83     register I32 ret = 0;
84
85     while (slen > 0) {
86         switch (*s) {
87         case '\'':
88         case '\\':
89             *d = '\\';
90             ++d; ++ret;
91         default:
92             *d = *s;
93             ++d; ++s; --slen;
94             break;
95         }
96     }
97     return ret;
98 }
99
100 static I32
101 esc_q_utf8(pTHX_ SV* sv, register char *src, register STRLEN slen)
102 {
103     char *s, *send, *r;
104     STRLEN grow = 0, j = 1, l;
105     bool dquote = FALSE;
106
107     /* this will need EBCDICification */
108     for (s = src, send = src + slen; s < send; s += UTF8SKIP(s)) {
109         UV k = utf8_to_uvchr((U8*)s, &l);
110
111         grow +=
112           (*s == '"' || *s == '\\') ? 2 :
113           (k < 0x80 ? 1 : UNISKIP(k) + 1 + 4); /* 4: \x{} */
114     }
115     sv_grow(sv, SvCUR(sv)+3+grow); /* 3: ""\0 */
116     r = SvPVX(sv) + SvCUR(sv);
117
118     for (s = src; s < send; s += UTF8SKIP(s)) {
119         UV k = utf8_to_uvchr((U8*)s, &l);
120
121         if (*s == '"' || *s == '\\') {
122             r[j++] = '\\';
123             r[j++] = *s;
124         }
125         else if (k < 0x80)
126             r[j++] = k;
127         else {
128             r[j++] = '\\';
129             r[j++] = 'x';
130             r[j++] = '{';
131             j += sprintf(r + j, "%"UVxf, k);
132             r[j++] = '}';
133             dquote = TRUE;
134         }
135     }
136     if (dquote)
137       r[0] = r[j++] = '"';
138     else
139       r[0] = r[j++] = '\'';
140     r[j++] = '\0';
141     SvCUR_set(sv, SvCUR(sv) + j);
142
143     return j;
144 }
145
146 /* append a repeated string to an SV */
147 static SV *
148 sv_x(pTHX_ SV *sv, register char *str, STRLEN len, I32 n)
149 {
150     if (sv == Nullsv)
151         sv = newSVpvn("", 0);
152     else
153         assert(SvTYPE(sv) >= SVt_PV);
154
155     if (n > 0) {
156         SvGROW(sv, len*n + SvCUR(sv) + 1);
157         if (len == 1) {
158             char *start = SvPVX(sv) + SvCUR(sv);
159             SvCUR(sv) += n;
160             start[n] = '\0';
161             while (n > 0)
162                 start[--n] = str[0];
163         }
164         else
165             while (n > 0) {
166                 sv_catpvn(sv, str, len);
167                 --n;
168             }
169     }
170     return sv;
171 }
172
173 /*
174  * This ought to be split into smaller functions. (it is one long function since
175  * it exactly parallels the perl version, which was one long thing for
176  * efficiency raisins.)  Ugggh!
177  */
178 static I32
179 DD_dump(pTHX_ SV *val, char *name, STRLEN namelen, SV *retval, HV *seenhv,
180         AV *postav, I32 *levelp, I32 indent, SV *pad, SV *xpad,
181         SV *apad, SV *sep, SV *freezer, SV *toaster, I32 purity,
182         I32 deepcopy, I32 quotekeys, SV *bless, I32 maxdepth)
183 {
184     char tmpbuf[128];
185     U32 i;
186     char *c, *r, *realpack, id[128];
187     SV **svp;
188     SV *sv, *ipad, *ival;
189     SV *blesspad = Nullsv;
190     AV *seenentry = Nullav;
191     char *iname;
192     STRLEN inamelen, idlen = 0;
193     U32 realtype;
194
195     if (!val)
196         return 0;
197
198     realtype = SvTYPE(val);
199
200     if (SvGMAGICAL(val))
201         mg_get(val);
202     if (SvROK(val)) {
203
204         if (SvOBJECT(SvRV(val)) && freezer &&
205             SvPOK(freezer) && SvCUR(freezer))
206         {
207             dSP; ENTER; SAVETMPS; PUSHMARK(sp);
208             XPUSHs(val); PUTBACK;
209             i = perl_call_method(SvPVX(freezer), G_EVAL|G_SCALAR);
210             SPAGAIN;
211             if (SvTRUE(ERRSV))
212                 warn("WARNING(Freezer method call failed): %s",
213                      SvPVX(ERRSV));
214             else if (i)
215                 val = newSVsv(POPs);
216             PUTBACK; FREETMPS; LEAVE;
217             if (i)
218                 (void)sv_2mortal(val);
219         }
220         
221         ival = SvRV(val);
222         realtype = SvTYPE(ival);
223         (void) sprintf(id, "0x%lx", (unsigned long)ival);
224         idlen = strlen(id);
225         if (SvOBJECT(ival))
226             realpack = HvNAME(SvSTASH(ival));
227         else
228             realpack = Nullch;
229
230         /* if it has a name, we need to either look it up, or keep a tab
231          * on it so we know when we hit it later
232          */
233         if (namelen) {
234             if ((svp = hv_fetch(seenhv, id, idlen, FALSE))
235                 && (sv = *svp) && SvROK(sv) && (seenentry = (AV*)SvRV(sv)))
236             {
237                 SV *othername;
238                 if ((svp = av_fetch(seenentry, 0, FALSE))
239                     && (othername = *svp))
240                 {
241                     if (purity && *levelp > 0) {
242                         SV *postentry;
243                         
244                         if (realtype == SVt_PVHV)
245                             sv_catpvn(retval, "{}", 2);
246                         else if (realtype == SVt_PVAV)
247                             sv_catpvn(retval, "[]", 2);
248                         else
249                             sv_catpvn(retval, "do{my $o}", 9);
250                         postentry = newSVpvn(name, namelen);
251                         sv_catpvn(postentry, " = ", 3);
252                         sv_catsv(postentry, othername);
253                         av_push(postav, postentry);
254                     }
255                     else {
256                         if (name[0] == '@' || name[0] == '%') {
257                             if ((SvPVX(othername))[0] == '\\' &&
258                                 (SvPVX(othername))[1] == name[0]) {
259                                 sv_catpvn(retval, SvPVX(othername)+1,
260                                           SvCUR(othername)-1);
261                             }
262                             else {
263                                 sv_catpvn(retval, name, 1);
264                                 sv_catpvn(retval, "{", 1);
265                                 sv_catsv(retval, othername);
266                                 sv_catpvn(retval, "}", 1);
267                             }
268                         }
269                         else
270                             sv_catsv(retval, othername);
271                     }
272                     return 1;
273                 }
274                 else {
275                     warn("ref name not found for %s", id);
276                     return 0;
277                 }
278             }
279             else {   /* store our name and continue */
280                 SV *namesv;
281                 if (name[0] == '@' || name[0] == '%') {
282                     namesv = newSVpvn("\\", 1);
283                     sv_catpvn(namesv, name, namelen);
284                 }
285                 else if (realtype == SVt_PVCV && name[0] == '*') {
286                     namesv = newSVpvn("\\", 2);
287                     sv_catpvn(namesv, name, namelen);
288                     (SvPVX(namesv))[1] = '&';
289                 }
290                 else
291                     namesv = newSVpvn(name, namelen);
292                 seenentry = newAV();
293                 av_push(seenentry, namesv);
294                 (void)SvREFCNT_inc(val);
295                 av_push(seenentry, val);
296                 (void)hv_store(seenhv, id, strlen(id),
297                                newRV((SV*)seenentry), 0);
298                 SvREFCNT_dec(seenentry);
299             }
300         }
301
302         if (realpack && *realpack == 'R' && strEQ(realpack, "Regexp")) {
303             STRLEN rlen;
304             char *rval = SvPV(val, rlen);
305             char *slash = strchr(rval, '/');
306             sv_catpvn(retval, "qr/", 3);
307             while (slash) {
308                 sv_catpvn(retval, rval, slash-rval);
309                 sv_catpvn(retval, "\\/", 2);
310                 rlen -= slash-rval+1;
311                 rval = slash+1;
312                 slash = strchr(rval, '/');
313             }
314             sv_catpvn(retval, rval, rlen);
315             sv_catpvn(retval, "/", 1);
316             return 1;
317         }
318
319         /* If purity is not set and maxdepth is set, then check depth:
320          * if we have reached maximum depth, return the string
321          * representation of the thing we are currently examining
322          * at this depth (i.e., 'Foo=ARRAY(0xdeadbeef)').
323          */
324         if (!purity && maxdepth > 0 && *levelp >= maxdepth) {
325             STRLEN vallen;
326             char *valstr = SvPV(val,vallen);
327             sv_catpvn(retval, "'", 1);
328             sv_catpvn(retval, valstr, vallen);
329             sv_catpvn(retval, "'", 1);
330             return 1;
331         }
332
333         if (realpack) {                         /* we have a blessed ref */
334             STRLEN blesslen;
335             char *blessstr = SvPV(bless, blesslen);
336             sv_catpvn(retval, blessstr, blesslen);
337             sv_catpvn(retval, "( ", 2);
338             if (indent >= 2) {
339                 blesspad = apad;
340                 apad = newSVsv(apad);
341                 sv_x(aTHX_ apad, " ", 1, blesslen+2);
342             }
343         }
344
345         (*levelp)++;
346         ipad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp);
347
348         if (realtype <= SVt_PVBM) {                          /* scalar ref */
349             SV *namesv = newSVpvn("${", 2);
350             sv_catpvn(namesv, name, namelen);
351             sv_catpvn(namesv, "}", 1);
352             if (realpack) {                                  /* blessed */
353                 sv_catpvn(retval, "do{\\(my $o = ", 13);
354                 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
355                         postav, levelp, indent, pad, xpad, apad, sep,
356                         freezer, toaster, purity, deepcopy, quotekeys, bless,
357                         maxdepth);
358                 sv_catpvn(retval, ")}", 2);
359             }                                                /* plain */
360             else {
361                 sv_catpvn(retval, "\\", 1);
362                 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
363                         postav, levelp, indent, pad, xpad, apad, sep,
364                         freezer, toaster, purity, deepcopy, quotekeys, bless,
365                         maxdepth);
366             }
367             SvREFCNT_dec(namesv);
368         }
369         else if (realtype == SVt_PVGV) {                     /* glob ref */
370             SV *namesv = newSVpvn("*{", 2);
371             sv_catpvn(namesv, name, namelen);
372             sv_catpvn(namesv, "}", 1);
373             sv_catpvn(retval, "\\", 1);
374             DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
375                     postav, levelp,     indent, pad, xpad, apad, sep,
376                     freezer, toaster, purity, deepcopy, quotekeys, bless,
377                     maxdepth);
378             SvREFCNT_dec(namesv);
379         }
380         else if (realtype == SVt_PVAV) {
381             SV *totpad;
382             I32 ix = 0;
383             I32 ixmax = av_len((AV *)ival);
384         
385             SV *ixsv = newSViv(0);
386             /* allowing for a 24 char wide array index */
387             New(0, iname, namelen+28, char);
388             (void)strcpy(iname, name);
389             inamelen = namelen;
390             if (name[0] == '@') {
391                 sv_catpvn(retval, "(", 1);
392                 iname[0] = '$';
393             }
394             else {
395                 sv_catpvn(retval, "[", 1);
396                 /* omit "->" in $foo{bar}->[0], but not in ${$foo}->[0] */
397                 /*if (namelen > 0
398                     && name[namelen-1] != ']' && name[namelen-1] != '}'
399                     && (namelen < 4 || (name[1] != '{' && name[2] != '{')))*/
400                 if ((namelen > 0
401                      && name[namelen-1] != ']' && name[namelen-1] != '}')
402                     || (namelen > 4
403                         && (name[1] == '{'
404                             || (name[0] == '\\' && name[2] == '{'))))
405                 {
406                     iname[inamelen++] = '-'; iname[inamelen++] = '>';
407                     iname[inamelen] = '\0';
408                 }
409             }
410             if (iname[0] == '*' && iname[inamelen-1] == '}' && inamelen >= 8 &&
411                 (instr(iname+inamelen-8, "{SCALAR}") ||
412                  instr(iname+inamelen-7, "{ARRAY}") ||
413                  instr(iname+inamelen-6, "{HASH}"))) {
414                 iname[inamelen++] = '-'; iname[inamelen++] = '>';
415             }
416             iname[inamelen++] = '['; iname[inamelen] = '\0';
417             totpad = newSVsv(sep);
418             sv_catsv(totpad, pad);
419             sv_catsv(totpad, apad);
420
421             for (ix = 0; ix <= ixmax; ++ix) {
422                 STRLEN ilen;
423                 SV *elem;
424                 svp = av_fetch((AV*)ival, ix, FALSE);
425                 if (svp)
426                     elem = *svp;
427                 else
428                     elem = &PL_sv_undef;
429                 
430                 ilen = inamelen;
431                 sv_setiv(ixsv, ix);
432                 (void) sprintf(iname+ilen, "%"IVdf, (IV)ix);
433                 ilen = strlen(iname);
434                 iname[ilen++] = ']'; iname[ilen] = '\0';
435                 if (indent >= 3) {
436                     sv_catsv(retval, totpad);
437                     sv_catsv(retval, ipad);
438                     sv_catpvn(retval, "#", 1);
439                     sv_catsv(retval, ixsv);
440                 }
441                 sv_catsv(retval, totpad);
442                 sv_catsv(retval, ipad);
443                 DD_dump(aTHX_ elem, iname, ilen, retval, seenhv, postav,
444                         levelp, indent, pad, xpad, apad, sep,
445                         freezer, toaster, purity, deepcopy, quotekeys, bless,
446                         maxdepth);
447                 if (ix < ixmax)
448                     sv_catpvn(retval, ",", 1);
449             }
450             if (ixmax >= 0) {
451                 SV *opad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), (*levelp)-1);
452                 sv_catsv(retval, totpad);
453                 sv_catsv(retval, opad);
454                 SvREFCNT_dec(opad);
455             }
456             if (name[0] == '@')
457                 sv_catpvn(retval, ")", 1);
458             else
459                 sv_catpvn(retval, "]", 1);
460             SvREFCNT_dec(ixsv);
461             SvREFCNT_dec(totpad);
462             Safefree(iname);
463         }
464         else if (realtype == SVt_PVHV) {
465             SV *totpad, *newapad;
466             SV *iname, *sname;
467             HE *entry;
468             char *key;
469             I32 klen;
470             SV *hval;
471         
472             iname = newSVpvn(name, namelen);
473             if (name[0] == '%') {
474                 sv_catpvn(retval, "(", 1);
475                 (SvPVX(iname))[0] = '$';
476             }
477             else {
478                 sv_catpvn(retval, "{", 1);
479                 /* omit "->" in $foo[0]->{bar}, but not in ${$foo}->{bar} */
480                 if ((namelen > 0
481                      && name[namelen-1] != ']' && name[namelen-1] != '}')
482                     || (namelen > 4
483                         && (name[1] == '{'
484                             || (name[0] == '\\' && name[2] == '{'))))
485                 {
486                     sv_catpvn(iname, "->", 2);
487                 }
488             }
489             if (name[0] == '*' && name[namelen-1] == '}' && namelen >= 8 &&
490                 (instr(name+namelen-8, "{SCALAR}") ||
491                  instr(name+namelen-7, "{ARRAY}") ||
492                  instr(name+namelen-6, "{HASH}"))) {
493                 sv_catpvn(iname, "->", 2);
494             }
495             sv_catpvn(iname, "{", 1);
496             totpad = newSVsv(sep);
497             sv_catsv(totpad, pad);
498             sv_catsv(totpad, apad);
499         
500             (void)hv_iterinit((HV*)ival);
501             i = 0;
502             while ((entry = hv_iternext((HV*)ival)))  {
503                 char *nkey = NULL;
504                 I32 nticks = 0;
505                 SV* keysv;
506                 STRLEN keylen;
507                 bool do_utf8 = FALSE;
508                 
509                 if (i)
510                     sv_catpvn(retval, ",", 1);
511                 i++;
512                 keysv = hv_iterkeysv(entry);
513                 hval  = hv_iterval((HV*)ival, entry);
514
515                 do_utf8 = DO_UTF8(keysv);
516                 key = SvPV(keysv, keylen);
517                 klen = keylen;
518
519                 if (do_utf8) {
520                     char *okey = SvPVX(retval) + SvCUR(retval);
521                     I32 nlen;
522
523                     sv_catsv(retval, totpad);
524                     sv_catsv(retval, ipad);
525                     nlen = esc_q_utf8(aTHX_ retval, key, klen);
526
527                     sname = newSVsv(iname);
528                     sv_catpvn(sname, okey, nlen);
529                     sv_catpvn(sname, "}", 1);
530                 }
531                 else {
532                     if (quotekeys || needs_quote(key)) {
533                         nticks = num_q(key, klen);
534                         New(0, nkey, klen+nticks+3, char);
535                         nkey[0] = '\'';
536                         if (nticks)
537                             klen += esc_q(nkey+1, key, klen);
538                         else
539                             (void)Copy(key, nkey+1, klen, char);
540                         nkey[++klen] = '\'';
541                         nkey[++klen] = '\0';
542                     }
543                     else {
544                         New(0, nkey, klen, char);
545                         (void)Copy(key, nkey, klen, char);
546                     }
547
548                     sname = newSVsv(iname);
549                     sv_catpvn(sname, nkey, klen);
550                     sv_catpvn(sname, "}", 1);
551
552                     sv_catsv(retval, totpad);
553                     sv_catsv(retval, ipad);
554                     sv_catpvn(retval, nkey, klen);
555                 }
556                 sv_catpvn(retval, " => ", 4);
557                 if (indent >= 2) {
558                     char *extra;
559                     I32 elen = 0;
560                     newapad = newSVsv(apad);
561                     New(0, extra, klen+4+1, char);
562                     while (elen < (klen+4))
563                         extra[elen++] = ' ';
564                     extra[elen] = '\0';
565                     sv_catpvn(newapad, extra, elen);
566                     Safefree(extra);
567                 }
568                 else
569                     newapad = apad;
570
571                 DD_dump(aTHX_ hval, SvPVX(sname), SvCUR(sname), retval, seenhv,
572                         postav, levelp, indent, pad, xpad, newapad, sep,
573                         freezer, toaster, purity, deepcopy, quotekeys, bless,
574                         maxdepth);
575                 SvREFCNT_dec(sname);
576                 Safefree(nkey);
577                 if (indent >= 2)
578                     SvREFCNT_dec(newapad);
579             }
580             if (i) {
581                 SV *opad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp-1);
582                 sv_catsv(retval, totpad);
583                 sv_catsv(retval, opad);
584                 SvREFCNT_dec(opad);
585             }
586             if (name[0] == '%')
587                 sv_catpvn(retval, ")", 1);
588             else
589                 sv_catpvn(retval, "}", 1);
590             SvREFCNT_dec(iname);
591             SvREFCNT_dec(totpad);
592         }
593         else if (realtype == SVt_PVCV) {
594             sv_catpvn(retval, "sub { \"DUMMY\" }", 15);
595             if (purity)
596                 warn("Encountered CODE ref, using dummy placeholder");
597         }
598         else {
599             warn("cannot handle ref type %ld", realtype);
600         }
601
602         if (realpack) {  /* free blessed allocs */
603             if (indent >= 2) {
604                 SvREFCNT_dec(apad);
605                 apad = blesspad;
606             }
607             sv_catpvn(retval, ", '", 3);
608             sv_catpvn(retval, realpack, strlen(realpack));
609             sv_catpvn(retval, "' )", 3);
610             if (toaster && SvPOK(toaster) && SvCUR(toaster)) {
611                 sv_catpvn(retval, "->", 2);
612                 sv_catsv(retval, toaster);
613                 sv_catpvn(retval, "()", 2);
614             }
615         }
616         SvREFCNT_dec(ipad);
617         (*levelp)--;
618     }
619     else {
620         STRLEN i;
621         
622         if (namelen) {
623             (void) sprintf(id, "0x%lx", (unsigned long)val);
624             if ((svp = hv_fetch(seenhv, id, (idlen = strlen(id)), FALSE)) &&
625                 (sv = *svp) && SvROK(sv) &&
626                 (seenentry = (AV*)SvRV(sv)))
627             {
628                 SV *othername;
629                 if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)
630                     && (svp = av_fetch(seenentry, 2, FALSE)) && *svp && SvIV(*svp) > 0)
631                 {
632                     sv_catpvn(retval, "${", 2);
633                     sv_catsv(retval, othername);
634                     sv_catpvn(retval, "}", 1);
635                     return 1;
636                 }
637             }
638             else {
639                 SV *namesv;
640                 namesv = newSVpvn("\\", 1);
641                 sv_catpvn(namesv, name, namelen);
642                 seenentry = newAV();
643                 av_push(seenentry, namesv);
644                 av_push(seenentry, newRV(val));
645                 (void)hv_store(seenhv, id, strlen(id), newRV((SV*)seenentry), 0);
646                 SvREFCNT_dec(seenentry);
647             }
648         }
649
650         if (SvIOK(val)) {
651             STRLEN len;
652             if (SvIsUV(val))
653               (void) sprintf(tmpbuf, "%"UVuf, SvUV(val));
654             else
655               (void) sprintf(tmpbuf, "%"IVdf, SvIV(val));
656             len = strlen(tmpbuf);
657             sv_catpvn(retval, tmpbuf, len);
658         }
659         else if (realtype == SVt_PVGV) {/* GLOBs can end up with scribbly names */
660             c = SvPV(val, i);
661             ++c; --i;                   /* just get the name */
662             if (i >= 6 && strncmp(c, "main::", 6) == 0) {
663                 c += 4;
664                 i -= 4;
665             }
666             if (needs_quote(c)) {
667                 sv_grow(retval, SvCUR(retval)+6+2*i);
668                 r = SvPVX(retval)+SvCUR(retval);
669                 r[0] = '*'; r[1] = '{'; r[2] = '\'';
670                 i += esc_q(r+3, c, i);
671                 i += 3;
672                 r[i++] = '\''; r[i++] = '}';
673                 r[i] = '\0';
674             }
675             else {
676                 sv_grow(retval, SvCUR(retval)+i+2);
677                 r = SvPVX(retval)+SvCUR(retval);
678                 r[0] = '*'; strcpy(r+1, c);
679                 i++;
680             }
681             SvCUR_set(retval, SvCUR(retval)+i);
682
683             if (purity) {
684                 static char *entries[] = { "{SCALAR}", "{ARRAY}", "{HASH}" };
685                 static STRLEN sizes[] = { 8, 7, 6 };
686                 SV *e;
687                 SV *nname = newSVpvn("", 0);
688                 SV *newapad = newSVpvn("", 0);
689                 GV *gv = (GV*)val;
690                 I32 j;
691                 
692                 for (j=0; j<3; j++) {
693                     e = ((j == 0) ? GvSV(gv) : (j == 1) ? (SV*)GvAV(gv) : (SV*)GvHV(gv));
694                     if (!e)
695                         continue;
696                     if (j == 0 && !SvOK(e))
697                         continue;
698
699                     {
700                         I32 nlevel = 0;
701                         SV *postentry = newSVpvn(r,i);
702                         
703                         sv_setsv(nname, postentry);
704                         sv_catpvn(nname, entries[j], sizes[j]);
705                         sv_catpvn(postentry, " = ", 3);
706                         av_push(postav, postentry);
707                         e = newRV(e);
708                         
709                         SvCUR(newapad) = 0;
710                         if (indent >= 2)
711                             (void)sv_x(aTHX_ newapad, " ", 1, SvCUR(postentry));
712                         
713                         DD_dump(aTHX_ e, SvPVX(nname), SvCUR(nname), postentry,
714                                 seenhv, postav, &nlevel, indent, pad, xpad,
715                                 newapad, sep, freezer, toaster, purity,
716                                 deepcopy, quotekeys, bless, maxdepth);
717                         SvREFCNT_dec(e);
718                     }
719                 }
720                 
721                 SvREFCNT_dec(newapad);
722                 SvREFCNT_dec(nname);
723             }
724         }
725         else if (val == &PL_sv_undef || !SvOK(val)) {
726             sv_catpvn(retval, "undef", 5);
727         }
728         else {
729             c = SvPV(val, i);
730             if (DO_UTF8(val))
731                 i += esc_q_utf8(aTHX_ retval, c, i);
732             else {
733                 sv_grow(retval, SvCUR(retval)+3+2*i); /* 3: ""\0 */
734                 r = SvPVX(retval) + SvCUR(retval);
735                 r[0] = '\'';
736                 i += esc_q(r+1, c, i);
737                 ++i;
738                 r[i++] = '\'';
739                 r[i] = '\0';
740                 SvCUR_set(retval, SvCUR(retval)+i);
741             }
742         }
743     }
744
745     if (idlen) {
746         if (deepcopy)
747             (void)hv_delete(seenhv, id, idlen, G_DISCARD);
748         else if (namelen && seenentry) {
749             SV *mark = *av_fetch(seenentry, 2, TRUE);
750             sv_setiv(mark,1);
751         }
752     }
753     return 1;
754 }
755
756
757 MODULE = Data::Dumper           PACKAGE = Data::Dumper         PREFIX = Data_Dumper_
758
759 #
760 # This is the exact equivalent of Dump.  Well, almost. The things that are
761 # different as of now (due to Laziness):
762 #   * doesnt do double-quotes yet.
763 #
764
765 void
766 Data_Dumper_Dumpxs(href, ...)
767         SV      *href;
768         PROTOTYPE: $;$$
769         PPCODE:
770         {
771             HV *hv;
772             SV *retval, *valstr;
773             HV *seenhv = Nullhv;
774             AV *postav, *todumpav, *namesav;
775             I32 level = 0;
776             I32 indent, terse, i, imax, postlen;
777             SV **svp;
778             SV *val, *name, *pad, *xpad, *apad, *sep, *varname;
779             SV *freezer, *toaster, *bless;
780             I32 purity, deepcopy, quotekeys, maxdepth = 0;
781             char tmpbuf[1024];
782             I32 gimme = GIMME;
783
784             if (!SvROK(href)) {         /* call new to get an object first */
785                 if (items < 2)
786                     croak("Usage: Data::Dumper::Dumpxs(PACKAGE, VAL_ARY_REF, [NAME_ARY_REF])");
787                 
788                 ENTER;
789                 SAVETMPS;
790                 
791                 PUSHMARK(sp);
792                 XPUSHs(href);
793                 XPUSHs(sv_2mortal(newSVsv(ST(1))));
794                 if (items >= 3)
795                     XPUSHs(sv_2mortal(newSVsv(ST(2))));
796                 PUTBACK;
797                 i = perl_call_method("new", G_SCALAR);
798                 SPAGAIN;
799                 if (i)
800                     href = newSVsv(POPs);
801
802                 PUTBACK;
803                 FREETMPS;
804                 LEAVE;
805                 if (i)
806                     (void)sv_2mortal(href);
807             }
808
809             todumpav = namesav = Nullav;
810             seenhv = Nullhv;
811             val = pad = xpad = apad = sep = varname
812                 = freezer = toaster = bless = &PL_sv_undef;
813             name = sv_newmortal();
814             indent = 2;
815             terse = purity = deepcopy = 0;
816             quotekeys = 1;
817         
818             retval = newSVpvn("", 0);
819             if (SvROK(href)
820                 && (hv = (HV*)SvRV((SV*)href))
821                 && SvTYPE(hv) == SVt_PVHV)              {
822
823                 if ((svp = hv_fetch(hv, "seen", 4, FALSE)) && SvROK(*svp))
824                     seenhv = (HV*)SvRV(*svp);
825                 if ((svp = hv_fetch(hv, "todump", 6, FALSE)) && SvROK(*svp))
826                     todumpav = (AV*)SvRV(*svp);
827                 if ((svp = hv_fetch(hv, "names", 5, FALSE)) && SvROK(*svp))
828                     namesav = (AV*)SvRV(*svp);
829                 if ((svp = hv_fetch(hv, "indent", 6, FALSE)))
830                     indent = SvIV(*svp);
831                 if ((svp = hv_fetch(hv, "purity", 6, FALSE)))
832                     purity = SvIV(*svp);
833                 if ((svp = hv_fetch(hv, "terse", 5, FALSE)))
834                     terse = SvTRUE(*svp);
835 #if 0 /* useqq currently unused */
836                 if ((svp = hv_fetch(hv, "useqq", 5, FALSE)))
837                     useqq = SvTRUE(*svp);
838 #endif
839                 if ((svp = hv_fetch(hv, "pad", 3, FALSE)))
840                     pad = *svp;
841                 if ((svp = hv_fetch(hv, "xpad", 4, FALSE)))
842                     xpad = *svp;
843                 if ((svp = hv_fetch(hv, "apad", 4, FALSE)))
844                     apad = *svp;
845                 if ((svp = hv_fetch(hv, "sep", 3, FALSE)))
846                     sep = *svp;
847                 if ((svp = hv_fetch(hv, "varname", 7, FALSE)))
848                     varname = *svp;
849                 if ((svp = hv_fetch(hv, "freezer", 7, FALSE)))
850                     freezer = *svp;
851                 if ((svp = hv_fetch(hv, "toaster", 7, FALSE)))
852                     toaster = *svp;
853                 if ((svp = hv_fetch(hv, "deepcopy", 8, FALSE)))
854                     deepcopy = SvTRUE(*svp);
855                 if ((svp = hv_fetch(hv, "quotekeys", 9, FALSE)))
856                     quotekeys = SvTRUE(*svp);
857                 if ((svp = hv_fetch(hv, "bless", 5, FALSE)))
858                     bless = *svp;
859                 if ((svp = hv_fetch(hv, "maxdepth", 8, FALSE)))
860                     maxdepth = SvIV(*svp);
861                 postav = newAV();
862
863                 if (todumpav)
864                     imax = av_len(todumpav);
865                 else
866                     imax = -1;
867                 valstr = newSVpvn("",0);
868                 for (i = 0; i <= imax; ++i) {
869                     SV *newapad;
870                 
871                     av_clear(postav);
872                     if ((svp = av_fetch(todumpav, i, FALSE)))
873                         val = *svp;
874                     else
875                         val = &PL_sv_undef;
876                     if ((svp = av_fetch(namesav, i, TRUE)))
877                         sv_setsv(name, *svp);
878                     else
879                         (void)SvOK_off(name);
880                 
881                     if (SvOK(name)) {
882                         if ((SvPVX(name))[0] == '*') {
883                             if (SvROK(val)) {
884                                 switch (SvTYPE(SvRV(val))) {
885                                 case SVt_PVAV:
886                                     (SvPVX(name))[0] = '@';
887                                     break;
888                                 case SVt_PVHV:
889                                     (SvPVX(name))[0] = '%';
890                                     break;
891                                 case SVt_PVCV:
892                                     (SvPVX(name))[0] = '*';
893                                     break;
894                                 default:
895                                     (SvPVX(name))[0] = '$';
896                                     break;
897                                 }
898                             }
899                             else
900                                 (SvPVX(name))[0] = '$';
901                         }
902                         else if ((SvPVX(name))[0] != '$')
903                             sv_insert(name, 0, 0, "$", 1);
904                     }
905                     else {
906                         STRLEN nchars = 0;
907                         sv_setpvn(name, "$", 1);
908                         sv_catsv(name, varname);
909                         (void) sprintf(tmpbuf, "%"IVdf, (IV)(i+1));
910                         nchars = strlen(tmpbuf);
911                         sv_catpvn(name, tmpbuf, nchars);
912                     }
913                 
914                     if (indent >= 2) {
915                         SV *tmpsv = sv_x(aTHX_ Nullsv, " ", 1, SvCUR(name)+3);
916                         newapad = newSVsv(apad);
917                         sv_catsv(newapad, tmpsv);
918                         SvREFCNT_dec(tmpsv);
919                     }
920                     else
921                         newapad = apad;
922                 
923                     DD_dump(aTHX_ val, SvPVX(name), SvCUR(name), valstr, seenhv,
924                             postav, &level, indent, pad, xpad, newapad, sep,
925                             freezer, toaster, purity, deepcopy, quotekeys,
926                             bless, maxdepth);
927                 
928                     if (indent >= 2)
929                         SvREFCNT_dec(newapad);
930
931                     postlen = av_len(postav);
932                     if (postlen >= 0 || !terse) {
933                         sv_insert(valstr, 0, 0, " = ", 3);
934                         sv_insert(valstr, 0, 0, SvPVX(name), SvCUR(name));
935                         sv_catpvn(valstr, ";", 1);
936                     }
937                     sv_catsv(retval, pad);
938                     sv_catsv(retval, valstr);
939                     sv_catsv(retval, sep);
940                     if (postlen >= 0) {
941                         I32 i;
942                         sv_catsv(retval, pad);
943                         for (i = 0; i <= postlen; ++i) {
944                             SV *elem;
945                             svp = av_fetch(postav, i, FALSE);
946                             if (svp && (elem = *svp)) {
947                                 sv_catsv(retval, elem);
948                                 if (i < postlen) {
949                                     sv_catpvn(retval, ";", 1);
950                                     sv_catsv(retval, sep);
951                                     sv_catsv(retval, pad);
952                                 }
953                             }
954                         }
955                         sv_catpvn(retval, ";", 1);
956                             sv_catsv(retval, sep);
957                     }
958                     sv_setpvn(valstr, "", 0);
959                     if (gimme == G_ARRAY) {
960                         XPUSHs(sv_2mortal(retval));
961                         if (i < imax)   /* not the last time thro ? */
962                             retval = newSVpvn("",0);
963                     }
964                 }
965                 SvREFCNT_dec(postav);
966                 SvREFCNT_dec(valstr);
967             }
968             else
969                 croak("Call to new() method failed to return HASH ref");
970             if (gimme == G_SCALAR)
971                 XPUSHs(sv_2mortal(retval));
972         }