6 #include "patchlevel.h"
7 #define PERL_VERSION PATCHLEVEL
12 # define PL_sv_undef sv_undef
15 # define ERRSV GvSV(errgv)
18 # define newSVpvn newSVpv
22 static I32 num_q (char *s, STRLEN slen);
23 static I32 esc_q (char *dest, char *src, STRLEN slen);
24 static SV *sv_x (pTHX_ SV *sv, char *str, STRLEN len, I32 n);
25 static I32 DD_dump (pTHX_ SV *val, char *name, STRLEN namelen, SV *retval,
26 HV *seenhv, AV *postav, I32 *levelp, I32 indent,
27 SV *pad, SV *xpad, SV *apad, SV *sep,
28 SV *freezer, SV *toaster,
29 I32 purity, I32 deepcopy, I32 quotekeys, SV *bless);
31 /* does a string need to be protected? */
33 needs_quote(register char *s)
57 /* count the number of "'"s and "\"s in string */
59 num_q(register char *s, register STRLEN slen)
64 if (*s == '\'' || *s == '\\')
73 /* returns number of chars added to escape "'"s and "\"s in s */
74 /* slen number of characters in s will be escaped */
75 /* destination must be long enough for additional chars */
77 esc_q(register char *d, register char *s, register STRLEN slen)
96 /* append a repeated string to an SV */
98 sv_x(pTHX_ SV *sv, register char *str, STRLEN len, I32 n)
101 sv = newSVpvn("", 0);
103 assert(SvTYPE(sv) >= SVt_PV);
106 SvGROW(sv, len*n + SvCUR(sv) + 1);
108 char *start = SvPVX(sv) + SvCUR(sv);
116 sv_catpvn(sv, str, len);
124 * This ought to be split into smaller functions. (it is one long function since
125 * it exactly parallels the perl version, which was one long thing for
126 * efficiency raisins.) Ugggh!
129 DD_dump(pTHX_ SV *val, char *name, STRLEN namelen, SV *retval, HV *seenhv,
130 AV *postav, I32 *levelp, I32 indent, SV *pad, SV *xpad,
131 SV *apad, SV *sep, SV *freezer, SV *toaster, I32 purity,
132 I32 deepcopy, I32 quotekeys, SV *bless)
136 char *c, *r, *realpack, id[128];
138 SV *sv, *ipad, *ival;
139 SV *blesspad = Nullsv;
140 AV *seenentry = Nullav;
142 STRLEN inamelen, idlen = 0;
149 flags = SvFLAGS(val);
150 realtype = SvTYPE(val);
156 if (SvOBJECT(SvRV(val)) && freezer &&
157 SvPOK(freezer) && SvCUR(freezer))
159 dSP; ENTER; SAVETMPS; PUSHMARK(sp);
160 XPUSHs(val); PUTBACK;
161 i = perl_call_method(SvPVX(freezer), G_EVAL|G_SCALAR);
164 warn("WARNING(Freezer method call failed): %s",
168 PUTBACK; FREETMPS; LEAVE;
170 (void)sv_2mortal(val);
174 flags = SvFLAGS(ival);
175 realtype = SvTYPE(ival);
176 (void) sprintf(id, "0x%lx", (unsigned long)ival);
179 realpack = HvNAME(SvSTASH(ival));
183 /* if it has a name, we need to either look it up, or keep a tab
184 * on it so we know when we hit it later
187 if ((svp = hv_fetch(seenhv, id, idlen, FALSE))
188 && (sv = *svp) && SvROK(sv) && (seenentry = (AV*)SvRV(sv)))
191 if ((svp = av_fetch(seenentry, 0, FALSE))
192 && (othername = *svp))
194 if (purity && *levelp > 0) {
197 if (realtype == SVt_PVHV)
198 sv_catpvn(retval, "{}", 2);
199 else if (realtype == SVt_PVAV)
200 sv_catpvn(retval, "[]", 2);
202 sv_catpvn(retval, "''", 2);
203 postentry = newSVpvn(name, namelen);
204 sv_catpvn(postentry, " = ", 3);
205 sv_catsv(postentry, othername);
206 av_push(postav, postentry);
209 if (name[0] == '@' || name[0] == '%') {
210 if ((SvPVX(othername))[0] == '\\' &&
211 (SvPVX(othername))[1] == name[0]) {
212 sv_catpvn(retval, SvPVX(othername)+1,
216 sv_catpvn(retval, name, 1);
217 sv_catpvn(retval, "{", 1);
218 sv_catsv(retval, othername);
219 sv_catpvn(retval, "}", 1);
223 sv_catsv(retval, othername);
228 warn("ref name not found for %s", id);
232 else { /* store our name and continue */
234 if (name[0] == '@' || name[0] == '%') {
235 namesv = newSVpvn("\\", 1);
236 sv_catpvn(namesv, name, namelen);
238 else if (realtype == SVt_PVCV && name[0] == '*') {
239 namesv = newSVpvn("\\", 2);
240 sv_catpvn(namesv, name, namelen);
241 (SvPVX(namesv))[1] = '&';
244 namesv = newSVpvn(name, namelen);
246 av_push(seenentry, namesv);
247 (void)SvREFCNT_inc(val);
248 av_push(seenentry, val);
249 (void)hv_store(seenhv, id, strlen(id),
250 newRV((SV*)seenentry), 0);
251 SvREFCNT_dec(seenentry);
256 if (*realpack == 'R' && strEQ(realpack, "Regexp")) {
258 char *rval = SvPV(val, rlen);
259 char *slash = strchr(rval, '/');
260 sv_catpvn(retval, "qr/", 3);
262 sv_catpvn(retval, rval, slash-rval);
263 sv_catpvn(retval, "\\/", 2);
264 rlen -= slash-rval+1;
266 slash = strchr(rval, '/');
268 sv_catpvn(retval, rval, rlen);
269 sv_catpvn(retval, "/", 1);
272 else { /* we have a blessed ref */
274 char *blessstr = SvPV(bless, blesslen);
275 sv_catpvn(retval, blessstr, blesslen);
276 sv_catpvn(retval, "( ", 2);
279 apad = newSVsv(apad);
280 sv_x(aTHX_ apad, " ", 1, blesslen+2);
286 ipad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp);
288 if (realtype <= SVt_PVBM) { /* scalar ref */
289 SV *namesv = newSVpvn("${", 2);
290 sv_catpvn(namesv, name, namelen);
291 sv_catpvn(namesv, "}", 1);
292 if (realpack) { /* blessed */
293 sv_catpvn(retval, "do{\\(my $o = ", 13);
294 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
295 postav, levelp, indent, pad, xpad, apad, sep,
296 freezer, toaster, purity, deepcopy, quotekeys, bless);
297 sv_catpvn(retval, ")}", 2);
300 sv_catpvn(retval, "\\", 1);
301 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
302 postav, levelp, indent, pad, xpad, apad, sep,
303 freezer, toaster, purity, deepcopy, quotekeys, bless);
305 SvREFCNT_dec(namesv);
307 else if (realtype == SVt_PVGV) { /* glob ref */
308 SV *namesv = newSVpvn("*{", 2);
309 sv_catpvn(namesv, name, namelen);
310 sv_catpvn(namesv, "}", 1);
311 sv_catpvn(retval, "\\", 1);
312 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
313 postav, levelp, indent, pad, xpad, apad, sep,
314 freezer, toaster, purity, deepcopy, quotekeys, bless);
315 SvREFCNT_dec(namesv);
317 else if (realtype == SVt_PVAV) {
320 I32 ixmax = av_len((AV *)ival);
322 SV *ixsv = newSViv(0);
323 /* allowing for a 24 char wide array index */
324 New(0, iname, namelen+28, char);
325 (void)strcpy(iname, name);
327 if (name[0] == '@') {
328 sv_catpvn(retval, "(", 1);
332 sv_catpvn(retval, "[", 1);
333 /* omit "->" in $foo{bar}->[0], but not in ${$foo}->[0] */
335 && name[namelen-1] != ']' && name[namelen-1] != '}'
336 && (namelen < 4 || (name[1] != '{' && name[2] != '{')))*/
338 && name[namelen-1] != ']' && name[namelen-1] != '}')
341 || (name[0] == '\\' && name[2] == '{'))))
343 iname[inamelen++] = '-'; iname[inamelen++] = '>';
344 iname[inamelen] = '\0';
347 if (iname[0] == '*' && iname[inamelen-1] == '}' && inamelen >= 8 &&
348 (instr(iname+inamelen-8, "{SCALAR}") ||
349 instr(iname+inamelen-7, "{ARRAY}") ||
350 instr(iname+inamelen-6, "{HASH}"))) {
351 iname[inamelen++] = '-'; iname[inamelen++] = '>';
353 iname[inamelen++] = '['; iname[inamelen] = '\0';
354 totpad = newSVsv(sep);
355 sv_catsv(totpad, pad);
356 sv_catsv(totpad, apad);
358 for (ix = 0; ix <= ixmax; ++ix) {
361 svp = av_fetch((AV*)ival, ix, FALSE);
369 (void) sprintf(iname+ilen, "%ld", ix);
370 ilen = strlen(iname);
371 iname[ilen++] = ']'; iname[ilen] = '\0';
373 sv_catsv(retval, totpad);
374 sv_catsv(retval, ipad);
375 sv_catpvn(retval, "#", 1);
376 sv_catsv(retval, ixsv);
378 sv_catsv(retval, totpad);
379 sv_catsv(retval, ipad);
380 DD_dump(aTHX_ elem, iname, ilen, retval, seenhv, postav,
381 levelp, indent, pad, xpad, apad, sep,
382 freezer, toaster, purity, deepcopy, quotekeys, bless);
384 sv_catpvn(retval, ",", 1);
387 SV *opad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), (*levelp)-1);
388 sv_catsv(retval, totpad);
389 sv_catsv(retval, opad);
393 sv_catpvn(retval, ")", 1);
395 sv_catpvn(retval, "]", 1);
397 SvREFCNT_dec(totpad);
400 else if (realtype == SVt_PVHV) {
401 SV *totpad, *newapad;
408 iname = newSVpvn(name, namelen);
409 if (name[0] == '%') {
410 sv_catpvn(retval, "(", 1);
411 (SvPVX(iname))[0] = '$';
414 sv_catpvn(retval, "{", 1);
415 /* omit "->" in $foo[0]->{bar}, but not in ${$foo}->{bar} */
417 && name[namelen-1] != ']' && name[namelen-1] != '}')
420 || (name[0] == '\\' && name[2] == '{'))))
422 sv_catpvn(iname, "->", 2);
425 if (name[0] == '*' && name[namelen-1] == '}' && namelen >= 8 &&
426 (instr(name+namelen-8, "{SCALAR}") ||
427 instr(name+namelen-7, "{ARRAY}") ||
428 instr(name+namelen-6, "{HASH}"))) {
429 sv_catpvn(iname, "->", 2);
431 sv_catpvn(iname, "{", 1);
432 totpad = newSVsv(sep);
433 sv_catsv(totpad, pad);
434 sv_catsv(totpad, apad);
436 (void)hv_iterinit((HV*)ival);
438 while ((entry = hv_iternext((HV*)ival))) {
443 sv_catpvn(retval, ",", 1);
445 key = hv_iterkey(entry, &klen);
446 hval = hv_iterval((HV*)ival, entry);
448 if (quotekeys || needs_quote(key)) {
449 nticks = num_q(key, klen);
450 New(0, nkey, klen+nticks+3, char);
453 klen += esc_q(nkey+1, key, klen);
455 (void)Copy(key, nkey+1, klen, char);
460 New(0, nkey, klen, char);
461 (void)Copy(key, nkey, klen, char);
464 sname = newSVsv(iname);
465 sv_catpvn(sname, nkey, klen);
466 sv_catpvn(sname, "}", 1);
468 sv_catsv(retval, totpad);
469 sv_catsv(retval, ipad);
470 sv_catpvn(retval, nkey, klen);
471 sv_catpvn(retval, " => ", 4);
475 newapad = newSVsv(apad);
476 New(0, extra, klen+4+1, char);
477 while (elen < (klen+4))
480 sv_catpvn(newapad, extra, elen);
486 DD_dump(aTHX_ hval, SvPVX(sname), SvCUR(sname), retval, seenhv,
487 postav, levelp, indent, pad, xpad, newapad, sep,
488 freezer, toaster, purity, deepcopy, quotekeys, bless);
492 SvREFCNT_dec(newapad);
495 SV *opad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp-1);
496 sv_catsv(retval, totpad);
497 sv_catsv(retval, opad);
501 sv_catpvn(retval, ")", 1);
503 sv_catpvn(retval, "}", 1);
505 SvREFCNT_dec(totpad);
507 else if (realtype == SVt_PVCV) {
508 sv_catpvn(retval, "sub { \"DUMMY\" }", 15);
510 warn("Encountered CODE ref, using dummy placeholder");
513 warn("cannot handle ref type %ld", realtype);
516 if (realpack) { /* free blessed allocs */
521 sv_catpvn(retval, ", '", 3);
522 sv_catpvn(retval, realpack, strlen(realpack));
523 sv_catpvn(retval, "' )", 3);
524 if (toaster && SvPOK(toaster) && SvCUR(toaster)) {
525 sv_catpvn(retval, "->", 2);
526 sv_catsv(retval, toaster);
527 sv_catpvn(retval, "()", 2);
537 (void) sprintf(id, "0x%lx", (unsigned long)val);
538 if ((svp = hv_fetch(seenhv, id, (idlen = strlen(id)), FALSE)) &&
539 (sv = *svp) && SvROK(sv) &&
540 (seenentry = (AV*)SvRV(sv)))
543 if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)
544 && (svp = av_fetch(seenentry, 2, FALSE)) && *svp && SvIV(*svp) > 0)
546 sv_catpvn(retval, "${", 2);
547 sv_catsv(retval, othername);
548 sv_catpvn(retval, "}", 1);
554 namesv = newSVpvn("\\", 1);
555 sv_catpvn(namesv, name, namelen);
557 av_push(seenentry, namesv);
558 av_push(seenentry, newRV(val));
559 (void)hv_store(seenhv, id, strlen(id), newRV((SV*)seenentry), 0);
560 SvREFCNT_dec(seenentry);
567 (void) sprintf(tmpbuf, "%d", i);
568 len = strlen(tmpbuf);
569 sv_catpvn(retval, tmpbuf, len);
571 else if (realtype == SVt_PVGV) {/* GLOBs can end up with scribbly names */
573 ++c; --i; /* just get the name */
574 if (i >= 6 && strncmp(c, "main::", 6) == 0) {
578 if (needs_quote(c)) {
579 sv_grow(retval, SvCUR(retval)+6+2*i);
580 r = SvPVX(retval)+SvCUR(retval);
581 r[0] = '*'; r[1] = '{'; r[2] = '\'';
582 i += esc_q(r+3, c, i);
584 r[i++] = '\''; r[i++] = '}';
588 sv_grow(retval, SvCUR(retval)+i+2);
589 r = SvPVX(retval)+SvCUR(retval);
590 r[0] = '*'; strcpy(r+1, c);
593 SvCUR_set(retval, SvCUR(retval)+i);
596 static char *entries[] = { "{SCALAR}", "{ARRAY}", "{HASH}" };
597 static STRLEN sizes[] = { 8, 7, 6 };
599 SV *nname = newSVpvn("", 0);
600 SV *newapad = newSVpvn("", 0);
604 for (j=0; j<3; j++) {
605 e = ((j == 0) ? GvSV(gv) : (j == 1) ? (SV*)GvAV(gv) : (SV*)GvHV(gv));
608 if (j == 0 && !SvOK(e))
613 SV *postentry = newSVpvn(r,i);
615 sv_setsv(nname, postentry);
616 sv_catpvn(nname, entries[j], sizes[j]);
617 sv_catpvn(postentry, " = ", 3);
618 av_push(postav, postentry);
623 (void)sv_x(aTHX_ newapad, " ", 1, SvCUR(postentry));
625 DD_dump(aTHX_ e, SvPVX(nname), SvCUR(nname), postentry,
626 seenhv, postav, &nlevel, indent, pad, xpad,
627 newapad, sep, freezer, toaster, purity,
628 deepcopy, quotekeys, bless);
633 SvREFCNT_dec(newapad);
637 else if (val == &PL_sv_undef || !SvOK(val)) {
638 sv_catpvn(retval, "undef", 5);
642 sv_grow(retval, SvCUR(retval)+3+2*i);
643 r = SvPVX(retval)+SvCUR(retval);
645 i += esc_q(r+1, c, i);
649 SvCUR_set(retval, SvCUR(retval)+i);
655 (void)hv_delete(seenhv, id, idlen, G_DISCARD);
656 else if (namelen && seenentry) {
657 SV *mark = *av_fetch(seenentry, 2, TRUE);
665 MODULE = Data::Dumper PACKAGE = Data::Dumper PREFIX = Data_Dumper_
668 # This is the exact equivalent of Dump. Well, almost. The things that are
669 # different as of now (due to Laziness):
670 # * doesnt do double-quotes yet.
674 Data_Dumper_Dumpxs(href, ...)
682 AV *postav, *todumpav, *namesav;
684 I32 indent, terse, useqq, i, imax, postlen;
686 SV *val, *name, *pad, *xpad, *apad, *sep, *tmp, *varname;
687 SV *freezer, *toaster, *bless;
688 I32 purity, deepcopy, quotekeys;
692 if (!SvROK(href)) { /* call new to get an object first */
701 croak("Usage: Data::Dumper::Dumpxs(PACKAGE, VAL_ARY_REF, NAME_ARY_REF)");
708 XPUSHs(sv_2mortal(newSVsv(valarray)));
709 XPUSHs(sv_2mortal(newSVsv(namearray)));
711 i = perl_call_method("new", G_SCALAR);
714 href = newSVsv(POPs);
720 (void)sv_2mortal(href);
723 todumpav = namesav = Nullav;
725 val = pad = xpad = apad = sep = tmp = varname
726 = freezer = toaster = bless = &PL_sv_undef;
727 name = sv_newmortal();
729 terse = useqq = purity = deepcopy = 0;
732 retval = newSVpvn("", 0);
734 && (hv = (HV*)SvRV((SV*)href))
735 && SvTYPE(hv) == SVt_PVHV) {
737 if ((svp = hv_fetch(hv, "seen", 4, FALSE)) && SvROK(*svp))
738 seenhv = (HV*)SvRV(*svp);
739 if ((svp = hv_fetch(hv, "todump", 6, FALSE)) && SvROK(*svp))
740 todumpav = (AV*)SvRV(*svp);
741 if ((svp = hv_fetch(hv, "names", 5, FALSE)) && SvROK(*svp))
742 namesav = (AV*)SvRV(*svp);
743 if ((svp = hv_fetch(hv, "indent", 6, FALSE)))
745 if ((svp = hv_fetch(hv, "purity", 6, FALSE)))
747 if ((svp = hv_fetch(hv, "terse", 5, FALSE)))
748 terse = SvTRUE(*svp);
749 if ((svp = hv_fetch(hv, "useqq", 5, FALSE)))
750 useqq = SvTRUE(*svp);
751 if ((svp = hv_fetch(hv, "pad", 3, FALSE)))
753 if ((svp = hv_fetch(hv, "xpad", 4, FALSE)))
755 if ((svp = hv_fetch(hv, "apad", 4, FALSE)))
757 if ((svp = hv_fetch(hv, "sep", 3, FALSE)))
759 if ((svp = hv_fetch(hv, "varname", 7, FALSE)))
761 if ((svp = hv_fetch(hv, "freezer", 7, FALSE)))
763 if ((svp = hv_fetch(hv, "toaster", 7, FALSE)))
765 if ((svp = hv_fetch(hv, "deepcopy", 8, FALSE)))
766 deepcopy = SvTRUE(*svp);
767 if ((svp = hv_fetch(hv, "quotekeys", 9, FALSE)))
768 quotekeys = SvTRUE(*svp);
769 if ((svp = hv_fetch(hv, "bless", 5, FALSE)))
774 imax = av_len(todumpav);
777 valstr = newSVpvn("",0);
778 for (i = 0; i <= imax; ++i) {
782 if ((svp = av_fetch(todumpav, i, FALSE)))
786 if ((svp = av_fetch(namesav, i, TRUE)))
787 sv_setsv(name, *svp);
792 if ((SvPVX(name))[0] == '*') {
794 switch (SvTYPE(SvRV(val))) {
796 (SvPVX(name))[0] = '@';
799 (SvPVX(name))[0] = '%';
802 (SvPVX(name))[0] = '*';
805 (SvPVX(name))[0] = '$';
810 (SvPVX(name))[0] = '$';
812 else if ((SvPVX(name))[0] != '$')
813 sv_insert(name, 0, 0, "$", 1);
817 sv_setpvn(name, "$", 1);
818 sv_catsv(name, varname);
819 (void) sprintf(tmpbuf, "%ld", i+1);
820 nchars = strlen(tmpbuf);
821 sv_catpvn(name, tmpbuf, nchars);
825 SV *tmpsv = sv_x(aTHX_ Nullsv, " ", 1, SvCUR(name)+3);
826 newapad = newSVsv(apad);
827 sv_catsv(newapad, tmpsv);
833 DD_dump(aTHX_ val, SvPVX(name), SvCUR(name), valstr, seenhv,
834 postav, &level, indent, pad, xpad, newapad, sep,
835 freezer, toaster, purity, deepcopy, quotekeys,
839 SvREFCNT_dec(newapad);
841 postlen = av_len(postav);
842 if (postlen >= 0 || !terse) {
843 sv_insert(valstr, 0, 0, " = ", 3);
844 sv_insert(valstr, 0, 0, SvPVX(name), SvCUR(name));
845 sv_catpvn(valstr, ";", 1);
847 sv_catsv(retval, pad);
848 sv_catsv(retval, valstr);
849 sv_catsv(retval, sep);
852 sv_catsv(retval, pad);
853 for (i = 0; i <= postlen; ++i) {
855 svp = av_fetch(postav, i, FALSE);
856 if (svp && (elem = *svp)) {
857 sv_catsv(retval, elem);
859 sv_catpvn(retval, ";", 1);
860 sv_catsv(retval, sep);
861 sv_catsv(retval, pad);
865 sv_catpvn(retval, ";", 1);
866 sv_catsv(retval, sep);
868 sv_setpvn(valstr, "", 0);
869 if (gimme == G_ARRAY) {
870 XPUSHs(sv_2mortal(retval));
871 if (i < imax) /* not the last time thro ? */
872 retval = newSVpvn("",0);
875 SvREFCNT_dec(postav);
876 SvREFCNT_dec(valstr);
879 croak("Call to new() method failed to return HASH ref");
880 if (gimme == G_SCALAR)
881 XPUSHs(sv_2mortal(retval));