1 #define PERL_NO_GET_CONTEXT
7 #include "patchlevel.h"
8 #define PERL_VERSION PATCHLEVEL
13 # define PL_sv_undef sv_undef
16 # define ERRSV GvSV(errgv)
19 # define newSVpvn newSVpv
23 static I32 num_q (char *s, STRLEN slen);
24 static I32 esc_q (char *dest, char *src, STRLEN slen);
25 static SV *sv_x (pTHX_ SV *sv, char *str, STRLEN len, I32 n);
26 static I32 DD_dump (pTHX_ SV *val, char *name, STRLEN namelen, SV *retval,
27 HV *seenhv, AV *postav, I32 *levelp, I32 indent,
28 SV *pad, SV *xpad, SV *apad, SV *sep,
29 SV *freezer, SV *toaster,
30 I32 purity, I32 deepcopy, I32 quotekeys, SV *bless,
33 /* does a string need to be protected? */
35 needs_quote(register char *s)
59 /* count the number of "'"s and "\"s in string */
61 num_q(register char *s, register STRLEN slen)
66 if (*s == '\'' || *s == '\\')
75 /* returns number of chars added to escape "'"s and "\"s in s */
76 /* slen number of characters in s will be escaped */
77 /* destination must be long enough for additional chars */
79 esc_q(register char *d, register char *s, register STRLEN slen)
98 /* append a repeated string to an SV */
100 sv_x(pTHX_ SV *sv, register char *str, STRLEN len, I32 n)
103 sv = newSVpvn("", 0);
105 assert(SvTYPE(sv) >= SVt_PV);
108 SvGROW(sv, len*n + SvCUR(sv) + 1);
110 char *start = SvPVX(sv) + SvCUR(sv);
118 sv_catpvn(sv, str, len);
126 * This ought to be split into smaller functions. (it is one long function since
127 * it exactly parallels the perl version, which was one long thing for
128 * efficiency raisins.) Ugggh!
131 DD_dump(pTHX_ SV *val, char *name, STRLEN namelen, SV *retval, HV *seenhv,
132 AV *postav, I32 *levelp, I32 indent, SV *pad, SV *xpad,
133 SV *apad, SV *sep, SV *freezer, SV *toaster, I32 purity,
134 I32 deepcopy, I32 quotekeys, SV *bless, I32 maxdepth)
138 char *c, *r, *realpack, id[128];
140 SV *sv, *ipad, *ival;
141 SV *blesspad = Nullsv;
142 AV *seenentry = Nullav;
144 STRLEN inamelen, idlen = 0;
151 flags = SvFLAGS(val);
152 realtype = SvTYPE(val);
158 if (SvOBJECT(SvRV(val)) && freezer &&
159 SvPOK(freezer) && SvCUR(freezer))
161 dSP; ENTER; SAVETMPS; PUSHMARK(sp);
162 XPUSHs(val); PUTBACK;
163 i = perl_call_method(SvPVX(freezer), G_EVAL|G_SCALAR);
166 warn("WARNING(Freezer method call failed): %s",
170 PUTBACK; FREETMPS; LEAVE;
172 (void)sv_2mortal(val);
176 flags = SvFLAGS(ival);
177 realtype = SvTYPE(ival);
178 (void) sprintf(id, "0x%lx", (unsigned long)ival);
181 realpack = HvNAME(SvSTASH(ival));
185 /* if it has a name, we need to either look it up, or keep a tab
186 * on it so we know when we hit it later
189 if ((svp = hv_fetch(seenhv, id, idlen, FALSE))
190 && (sv = *svp) && SvROK(sv) && (seenentry = (AV*)SvRV(sv)))
193 if ((svp = av_fetch(seenentry, 0, FALSE))
194 && (othername = *svp))
196 if (purity && *levelp > 0) {
199 if (realtype == SVt_PVHV)
200 sv_catpvn(retval, "{}", 2);
201 else if (realtype == SVt_PVAV)
202 sv_catpvn(retval, "[]", 2);
204 sv_catpvn(retval, "''", 2);
205 postentry = newSVpvn(name, namelen);
206 sv_catpvn(postentry, " = ", 3);
207 sv_catsv(postentry, othername);
208 av_push(postav, postentry);
211 if (name[0] == '@' || name[0] == '%') {
212 if ((SvPVX(othername))[0] == '\\' &&
213 (SvPVX(othername))[1] == name[0]) {
214 sv_catpvn(retval, SvPVX(othername)+1,
218 sv_catpvn(retval, name, 1);
219 sv_catpvn(retval, "{", 1);
220 sv_catsv(retval, othername);
221 sv_catpvn(retval, "}", 1);
225 sv_catsv(retval, othername);
230 warn("ref name not found for %s", id);
234 else { /* store our name and continue */
236 if (name[0] == '@' || name[0] == '%') {
237 namesv = newSVpvn("\\", 1);
238 sv_catpvn(namesv, name, namelen);
240 else if (realtype == SVt_PVCV && name[0] == '*') {
241 namesv = newSVpvn("\\", 2);
242 sv_catpvn(namesv, name, namelen);
243 (SvPVX(namesv))[1] = '&';
246 namesv = newSVpvn(name, namelen);
248 av_push(seenentry, namesv);
249 (void)SvREFCNT_inc(val);
250 av_push(seenentry, val);
251 (void)hv_store(seenhv, id, strlen(id),
252 newRV((SV*)seenentry), 0);
253 SvREFCNT_dec(seenentry);
257 if (realpack && *realpack == 'R' && strEQ(realpack, "Regexp")) {
259 char *rval = SvPV(val, rlen);
260 char *slash = strchr(rval, '/');
261 sv_catpvn(retval, "qr/", 3);
263 sv_catpvn(retval, rval, slash-rval);
264 sv_catpvn(retval, "\\/", 2);
265 rlen -= slash-rval+1;
267 slash = strchr(rval, '/');
269 sv_catpvn(retval, rval, rlen);
270 sv_catpvn(retval, "/", 1);
274 /* If purity is not set and maxdepth is set, then check depth:
275 * if we have reached maximum depth, return the string
276 * representation of the thing we are currently examining
277 * at this depth (i.e., 'Foo=ARRAY(0xdeadbeef)').
279 if (!purity && maxdepth > 0 && *levelp >= maxdepth) {
281 char *valstr = SvPV(val,vallen);
282 sv_catpvn(retval, "'", 1);
283 sv_catpvn(retval, valstr, vallen);
284 sv_catpvn(retval, "'", 1);
288 if (realpack) { /* we have a blessed ref */
290 char *blessstr = SvPV(bless, blesslen);
291 sv_catpvn(retval, blessstr, blesslen);
292 sv_catpvn(retval, "( ", 2);
295 apad = newSVsv(apad);
296 sv_x(aTHX_ apad, " ", 1, blesslen+2);
301 ipad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp);
303 if (realtype <= SVt_PVBM) { /* scalar ref */
304 SV *namesv = newSVpvn("${", 2);
305 sv_catpvn(namesv, name, namelen);
306 sv_catpvn(namesv, "}", 1);
307 if (realpack) { /* blessed */
308 sv_catpvn(retval, "do{\\(my $o = ", 13);
309 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
310 postav, levelp, indent, pad, xpad, apad, sep,
311 freezer, toaster, purity, deepcopy, quotekeys, bless,
313 sv_catpvn(retval, ")}", 2);
316 sv_catpvn(retval, "\\", 1);
317 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
318 postav, levelp, indent, pad, xpad, apad, sep,
319 freezer, toaster, purity, deepcopy, quotekeys, bless,
322 SvREFCNT_dec(namesv);
324 else if (realtype == SVt_PVGV) { /* glob ref */
325 SV *namesv = newSVpvn("*{", 2);
326 sv_catpvn(namesv, name, namelen);
327 sv_catpvn(namesv, "}", 1);
328 sv_catpvn(retval, "\\", 1);
329 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
330 postav, levelp, indent, pad, xpad, apad, sep,
331 freezer, toaster, purity, deepcopy, quotekeys, bless,
333 SvREFCNT_dec(namesv);
335 else if (realtype == SVt_PVAV) {
338 I32 ixmax = av_len((AV *)ival);
340 SV *ixsv = newSViv(0);
341 /* allowing for a 24 char wide array index */
342 New(0, iname, namelen+28, char);
343 (void)strcpy(iname, name);
345 if (name[0] == '@') {
346 sv_catpvn(retval, "(", 1);
350 sv_catpvn(retval, "[", 1);
351 /* omit "->" in $foo{bar}->[0], but not in ${$foo}->[0] */
353 && name[namelen-1] != ']' && name[namelen-1] != '}'
354 && (namelen < 4 || (name[1] != '{' && name[2] != '{')))*/
356 && name[namelen-1] != ']' && name[namelen-1] != '}')
359 || (name[0] == '\\' && name[2] == '{'))))
361 iname[inamelen++] = '-'; iname[inamelen++] = '>';
362 iname[inamelen] = '\0';
365 if (iname[0] == '*' && iname[inamelen-1] == '}' && inamelen >= 8 &&
366 (instr(iname+inamelen-8, "{SCALAR}") ||
367 instr(iname+inamelen-7, "{ARRAY}") ||
368 instr(iname+inamelen-6, "{HASH}"))) {
369 iname[inamelen++] = '-'; iname[inamelen++] = '>';
371 iname[inamelen++] = '['; iname[inamelen] = '\0';
372 totpad = newSVsv(sep);
373 sv_catsv(totpad, pad);
374 sv_catsv(totpad, apad);
376 for (ix = 0; ix <= ixmax; ++ix) {
379 svp = av_fetch((AV*)ival, ix, FALSE);
387 (void) sprintf(iname+ilen, "%ld", ix);
388 ilen = strlen(iname);
389 iname[ilen++] = ']'; iname[ilen] = '\0';
391 sv_catsv(retval, totpad);
392 sv_catsv(retval, ipad);
393 sv_catpvn(retval, "#", 1);
394 sv_catsv(retval, ixsv);
396 sv_catsv(retval, totpad);
397 sv_catsv(retval, ipad);
398 DD_dump(aTHX_ elem, iname, ilen, retval, seenhv, postav,
399 levelp, indent, pad, xpad, apad, sep,
400 freezer, toaster, purity, deepcopy, quotekeys, bless,
403 sv_catpvn(retval, ",", 1);
406 SV *opad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), (*levelp)-1);
407 sv_catsv(retval, totpad);
408 sv_catsv(retval, opad);
412 sv_catpvn(retval, ")", 1);
414 sv_catpvn(retval, "]", 1);
416 SvREFCNT_dec(totpad);
419 else if (realtype == SVt_PVHV) {
420 SV *totpad, *newapad;
427 iname = newSVpvn(name, namelen);
428 if (name[0] == '%') {
429 sv_catpvn(retval, "(", 1);
430 (SvPVX(iname))[0] = '$';
433 sv_catpvn(retval, "{", 1);
434 /* omit "->" in $foo[0]->{bar}, but not in ${$foo}->{bar} */
436 && name[namelen-1] != ']' && name[namelen-1] != '}')
439 || (name[0] == '\\' && name[2] == '{'))))
441 sv_catpvn(iname, "->", 2);
444 if (name[0] == '*' && name[namelen-1] == '}' && namelen >= 8 &&
445 (instr(name+namelen-8, "{SCALAR}") ||
446 instr(name+namelen-7, "{ARRAY}") ||
447 instr(name+namelen-6, "{HASH}"))) {
448 sv_catpvn(iname, "->", 2);
450 sv_catpvn(iname, "{", 1);
451 totpad = newSVsv(sep);
452 sv_catsv(totpad, pad);
453 sv_catsv(totpad, apad);
455 (void)hv_iterinit((HV*)ival);
457 while ((entry = hv_iternext((HV*)ival))) {
462 sv_catpvn(retval, ",", 1);
464 key = hv_iterkey(entry, &klen);
465 hval = hv_iterval((HV*)ival, entry);
467 if (quotekeys || needs_quote(key)) {
468 nticks = num_q(key, klen);
469 New(0, nkey, klen+nticks+3, char);
472 klen += esc_q(nkey+1, key, klen);
474 (void)Copy(key, nkey+1, klen, char);
479 New(0, nkey, klen, char);
480 (void)Copy(key, nkey, klen, char);
483 sname = newSVsv(iname);
484 sv_catpvn(sname, nkey, klen);
485 sv_catpvn(sname, "}", 1);
487 sv_catsv(retval, totpad);
488 sv_catsv(retval, ipad);
489 sv_catpvn(retval, nkey, klen);
490 sv_catpvn(retval, " => ", 4);
494 newapad = newSVsv(apad);
495 New(0, extra, klen+4+1, char);
496 while (elen < (klen+4))
499 sv_catpvn(newapad, extra, elen);
505 DD_dump(aTHX_ hval, SvPVX(sname), SvCUR(sname), retval, seenhv,
506 postav, levelp, indent, pad, xpad, newapad, sep,
507 freezer, toaster, purity, deepcopy, quotekeys, bless,
512 SvREFCNT_dec(newapad);
515 SV *opad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp-1);
516 sv_catsv(retval, totpad);
517 sv_catsv(retval, opad);
521 sv_catpvn(retval, ")", 1);
523 sv_catpvn(retval, "}", 1);
525 SvREFCNT_dec(totpad);
527 else if (realtype == SVt_PVCV) {
528 sv_catpvn(retval, "sub { \"DUMMY\" }", 15);
530 warn("Encountered CODE ref, using dummy placeholder");
533 warn("cannot handle ref type %ld", realtype);
536 if (realpack) { /* free blessed allocs */
541 sv_catpvn(retval, ", '", 3);
542 sv_catpvn(retval, realpack, strlen(realpack));
543 sv_catpvn(retval, "' )", 3);
544 if (toaster && SvPOK(toaster) && SvCUR(toaster)) {
545 sv_catpvn(retval, "->", 2);
546 sv_catsv(retval, toaster);
547 sv_catpvn(retval, "()", 2);
557 (void) sprintf(id, "0x%lx", (unsigned long)val);
558 if ((svp = hv_fetch(seenhv, id, (idlen = strlen(id)), FALSE)) &&
559 (sv = *svp) && SvROK(sv) &&
560 (seenentry = (AV*)SvRV(sv)))
563 if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)
564 && (svp = av_fetch(seenentry, 2, FALSE)) && *svp && SvIV(*svp) > 0)
566 sv_catpvn(retval, "${", 2);
567 sv_catsv(retval, othername);
568 sv_catpvn(retval, "}", 1);
574 namesv = newSVpvn("\\", 1);
575 sv_catpvn(namesv, name, namelen);
577 av_push(seenentry, namesv);
578 av_push(seenentry, newRV(val));
579 (void)hv_store(seenhv, id, strlen(id), newRV((SV*)seenentry), 0);
580 SvREFCNT_dec(seenentry);
587 (void) sprintf(tmpbuf, "%d", i);
588 len = strlen(tmpbuf);
589 sv_catpvn(retval, tmpbuf, len);
591 else if (realtype == SVt_PVGV) {/* GLOBs can end up with scribbly names */
593 ++c; --i; /* just get the name */
594 if (i >= 6 && strncmp(c, "main::", 6) == 0) {
598 if (needs_quote(c)) {
599 sv_grow(retval, SvCUR(retval)+6+2*i);
600 r = SvPVX(retval)+SvCUR(retval);
601 r[0] = '*'; r[1] = '{'; r[2] = '\'';
602 i += esc_q(r+3, c, i);
604 r[i++] = '\''; r[i++] = '}';
608 sv_grow(retval, SvCUR(retval)+i+2);
609 r = SvPVX(retval)+SvCUR(retval);
610 r[0] = '*'; strcpy(r+1, c);
613 SvCUR_set(retval, SvCUR(retval)+i);
616 static char *entries[] = { "{SCALAR}", "{ARRAY}", "{HASH}" };
617 static STRLEN sizes[] = { 8, 7, 6 };
619 SV *nname = newSVpvn("", 0);
620 SV *newapad = newSVpvn("", 0);
624 for (j=0; j<3; j++) {
625 e = ((j == 0) ? GvSV(gv) : (j == 1) ? (SV*)GvAV(gv) : (SV*)GvHV(gv));
628 if (j == 0 && !SvOK(e))
633 SV *postentry = newSVpvn(r,i);
635 sv_setsv(nname, postentry);
636 sv_catpvn(nname, entries[j], sizes[j]);
637 sv_catpvn(postentry, " = ", 3);
638 av_push(postav, postentry);
643 (void)sv_x(aTHX_ newapad, " ", 1, SvCUR(postentry));
645 DD_dump(aTHX_ e, SvPVX(nname), SvCUR(nname), postentry,
646 seenhv, postav, &nlevel, indent, pad, xpad,
647 newapad, sep, freezer, toaster, purity,
648 deepcopy, quotekeys, bless, maxdepth);
653 SvREFCNT_dec(newapad);
657 else if (val == &PL_sv_undef || !SvOK(val)) {
658 sv_catpvn(retval, "undef", 5);
662 sv_grow(retval, SvCUR(retval)+3+2*i);
663 r = SvPVX(retval)+SvCUR(retval);
665 i += esc_q(r+1, c, i);
669 SvCUR_set(retval, SvCUR(retval)+i);
675 (void)hv_delete(seenhv, id, idlen, G_DISCARD);
676 else if (namelen && seenentry) {
677 SV *mark = *av_fetch(seenentry, 2, TRUE);
685 MODULE = Data::Dumper PACKAGE = Data::Dumper PREFIX = Data_Dumper_
688 # This is the exact equivalent of Dump. Well, almost. The things that are
689 # different as of now (due to Laziness):
690 # * doesnt do double-quotes yet.
694 Data_Dumper_Dumpxs(href, ...)
702 AV *postav, *todumpav, *namesav;
704 I32 indent, terse, useqq, i, imax, postlen;
706 SV *val, *name, *pad, *xpad, *apad, *sep, *tmp, *varname;
707 SV *freezer, *toaster, *bless;
708 I32 purity, deepcopy, quotekeys, maxdepth;
712 if (!SvROK(href)) { /* call new to get an object first */
721 croak("Usage: Data::Dumper::Dumpxs(PACKAGE, VAL_ARY_REF, NAME_ARY_REF)");
728 XPUSHs(sv_2mortal(newSVsv(valarray)));
729 XPUSHs(sv_2mortal(newSVsv(namearray)));
731 i = perl_call_method("new", G_SCALAR);
734 href = newSVsv(POPs);
740 (void)sv_2mortal(href);
743 todumpav = namesav = Nullav;
745 val = pad = xpad = apad = sep = tmp = varname
746 = freezer = toaster = bless = &PL_sv_undef;
747 name = sv_newmortal();
749 terse = useqq = purity = deepcopy = 0;
752 retval = newSVpvn("", 0);
754 && (hv = (HV*)SvRV((SV*)href))
755 && SvTYPE(hv) == SVt_PVHV) {
757 if ((svp = hv_fetch(hv, "seen", 4, FALSE)) && SvROK(*svp))
758 seenhv = (HV*)SvRV(*svp);
759 if ((svp = hv_fetch(hv, "todump", 6, FALSE)) && SvROK(*svp))
760 todumpav = (AV*)SvRV(*svp);
761 if ((svp = hv_fetch(hv, "names", 5, FALSE)) && SvROK(*svp))
762 namesav = (AV*)SvRV(*svp);
763 if ((svp = hv_fetch(hv, "indent", 6, FALSE)))
765 if ((svp = hv_fetch(hv, "purity", 6, FALSE)))
767 if ((svp = hv_fetch(hv, "terse", 5, FALSE)))
768 terse = SvTRUE(*svp);
769 if ((svp = hv_fetch(hv, "useqq", 5, FALSE)))
770 useqq = SvTRUE(*svp);
771 if ((svp = hv_fetch(hv, "pad", 3, FALSE)))
773 if ((svp = hv_fetch(hv, "xpad", 4, FALSE)))
775 if ((svp = hv_fetch(hv, "apad", 4, FALSE)))
777 if ((svp = hv_fetch(hv, "sep", 3, FALSE)))
779 if ((svp = hv_fetch(hv, "varname", 7, FALSE)))
781 if ((svp = hv_fetch(hv, "freezer", 7, FALSE)))
783 if ((svp = hv_fetch(hv, "toaster", 7, FALSE)))
785 if ((svp = hv_fetch(hv, "deepcopy", 8, FALSE)))
786 deepcopy = SvTRUE(*svp);
787 if ((svp = hv_fetch(hv, "quotekeys", 9, FALSE)))
788 quotekeys = SvTRUE(*svp);
789 if ((svp = hv_fetch(hv, "bless", 5, FALSE)))
791 if ((svp = hv_fetch(hv, "maxdepth", 8, FALSE)))
792 maxdepth = SvIV(*svp);
796 imax = av_len(todumpav);
799 valstr = newSVpvn("",0);
800 for (i = 0; i <= imax; ++i) {
804 if ((svp = av_fetch(todumpav, i, FALSE)))
808 if ((svp = av_fetch(namesav, i, TRUE)))
809 sv_setsv(name, *svp);
814 if ((SvPVX(name))[0] == '*') {
816 switch (SvTYPE(SvRV(val))) {
818 (SvPVX(name))[0] = '@';
821 (SvPVX(name))[0] = '%';
824 (SvPVX(name))[0] = '*';
827 (SvPVX(name))[0] = '$';
832 (SvPVX(name))[0] = '$';
834 else if ((SvPVX(name))[0] != '$')
835 sv_insert(name, 0, 0, "$", 1);
839 sv_setpvn(name, "$", 1);
840 sv_catsv(name, varname);
841 (void) sprintf(tmpbuf, "%ld", i+1);
842 nchars = strlen(tmpbuf);
843 sv_catpvn(name, tmpbuf, nchars);
847 SV *tmpsv = sv_x(aTHX_ Nullsv, " ", 1, SvCUR(name)+3);
848 newapad = newSVsv(apad);
849 sv_catsv(newapad, tmpsv);
855 DD_dump(aTHX_ val, SvPVX(name), SvCUR(name), valstr, seenhv,
856 postav, &level, indent, pad, xpad, newapad, sep,
857 freezer, toaster, purity, deepcopy, quotekeys,
861 SvREFCNT_dec(newapad);
863 postlen = av_len(postav);
864 if (postlen >= 0 || !terse) {
865 sv_insert(valstr, 0, 0, " = ", 3);
866 sv_insert(valstr, 0, 0, SvPVX(name), SvCUR(name));
867 sv_catpvn(valstr, ";", 1);
869 sv_catsv(retval, pad);
870 sv_catsv(retval, valstr);
871 sv_catsv(retval, sep);
874 sv_catsv(retval, pad);
875 for (i = 0; i <= postlen; ++i) {
877 svp = av_fetch(postav, i, FALSE);
878 if (svp && (elem = *svp)) {
879 sv_catsv(retval, elem);
881 sv_catpvn(retval, ";", 1);
882 sv_catsv(retval, sep);
883 sv_catsv(retval, pad);
887 sv_catpvn(retval, ";", 1);
888 sv_catsv(retval, sep);
890 sv_setpvn(valstr, "", 0);
891 if (gimme == G_ARRAY) {
892 XPUSHs(sv_2mortal(retval));
893 if (i < imax) /* not the last time thro ? */
894 retval = newSVpvn("",0);
897 SvREFCNT_dec(postav);
898 SvREFCNT_dec(valstr);
901 croak("Call to new() method failed to return HASH ref");
902 if (gimme == G_SCALAR)
903 XPUSHs(sv_2mortal(retval));