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)
60 /* count the number of "'"s and "\"s in string */
62 num_q(register char *s, register STRLEN slen)
67 if (*s == '\'' || *s == '\\')
76 /* returns number of chars added to escape "'"s and "\"s in s */
77 /* slen number of characters in s will be escaped */
78 /* destination must be long enough for additional chars */
80 esc_q(register char *d, register char *s, register STRLEN slen)
99 /* append a repeated string to an SV */
101 sv_x(pTHX_ SV *sv, register char *str, STRLEN len, I32 n)
104 sv = newSVpvn("", 0);
106 assert(SvTYPE(sv) >= SVt_PV);
109 SvGROW(sv, len*n + SvCUR(sv) + 1);
111 char *start = SvPVX(sv) + SvCUR(sv);
119 sv_catpvn(sv, str, len);
127 * This ought to be split into smaller functions. (it is one long function since
128 * it exactly parallels the perl version, which was one long thing for
129 * efficiency raisins.) Ugggh!
132 DD_dump(pTHX_ SV *val, char *name, STRLEN namelen, SV *retval, HV *seenhv,
133 AV *postav, I32 *levelp, I32 indent, SV *pad, SV *xpad,
134 SV *apad, SV *sep, SV *freezer, SV *toaster, I32 purity,
135 I32 deepcopy, I32 quotekeys, SV *bless, I32 maxdepth)
139 char *c, *r, *realpack, id[128];
141 SV *sv, *ipad, *ival;
142 SV *blesspad = Nullsv;
143 AV *seenentry = Nullav;
145 STRLEN inamelen, idlen = 0;
152 flags = SvFLAGS(val);
153 realtype = SvTYPE(val);
159 if (SvOBJECT(SvRV(val)) && freezer &&
160 SvPOK(freezer) && SvCUR(freezer))
162 dSP; ENTER; SAVETMPS; PUSHMARK(sp);
163 XPUSHs(val); PUTBACK;
164 i = perl_call_method(SvPVX(freezer), G_EVAL|G_SCALAR);
167 warn("WARNING(Freezer method call failed): %s",
171 PUTBACK; FREETMPS; LEAVE;
173 (void)sv_2mortal(val);
177 flags = SvFLAGS(ival);
178 realtype = SvTYPE(ival);
179 (void) sprintf(id, "0x%lx", (unsigned long)ival);
182 realpack = HvNAME(SvSTASH(ival));
186 /* if it has a name, we need to either look it up, or keep a tab
187 * on it so we know when we hit it later
190 if ((svp = hv_fetch(seenhv, id, idlen, FALSE))
191 && (sv = *svp) && SvROK(sv) && (seenentry = (AV*)SvRV(sv)))
194 if ((svp = av_fetch(seenentry, 0, FALSE))
195 && (othername = *svp))
197 if (purity && *levelp > 0) {
200 if (realtype == SVt_PVHV)
201 sv_catpvn(retval, "{}", 2);
202 else if (realtype == SVt_PVAV)
203 sv_catpvn(retval, "[]", 2);
205 sv_catpvn(retval, "''", 2);
206 postentry = newSVpvn(name, namelen);
207 sv_catpvn(postentry, " = ", 3);
208 sv_catsv(postentry, othername);
209 av_push(postav, postentry);
212 if (name[0] == '@' || name[0] == '%') {
213 if ((SvPVX(othername))[0] == '\\' &&
214 (SvPVX(othername))[1] == name[0]) {
215 sv_catpvn(retval, SvPVX(othername)+1,
219 sv_catpvn(retval, name, 1);
220 sv_catpvn(retval, "{", 1);
221 sv_catsv(retval, othername);
222 sv_catpvn(retval, "}", 1);
226 sv_catsv(retval, othername);
231 warn("ref name not found for %s", id);
235 else { /* store our name and continue */
237 if (name[0] == '@' || name[0] == '%') {
238 namesv = newSVpvn("\\", 1);
239 sv_catpvn(namesv, name, namelen);
241 else if (realtype == SVt_PVCV && name[0] == '*') {
242 namesv = newSVpvn("\\", 2);
243 sv_catpvn(namesv, name, namelen);
244 (SvPVX(namesv))[1] = '&';
247 namesv = newSVpvn(name, namelen);
249 av_push(seenentry, namesv);
250 (void)SvREFCNT_inc(val);
251 av_push(seenentry, val);
252 (void)hv_store(seenhv, id, strlen(id),
253 newRV((SV*)seenentry), 0);
254 SvREFCNT_dec(seenentry);
258 if (realpack && *realpack == 'R' && strEQ(realpack, "Regexp")) {
260 char *rval = SvPV(val, rlen);
261 char *slash = strchr(rval, '/');
262 sv_catpvn(retval, "qr/", 3);
264 sv_catpvn(retval, rval, slash-rval);
265 sv_catpvn(retval, "\\/", 2);
266 rlen -= slash-rval+1;
268 slash = strchr(rval, '/');
270 sv_catpvn(retval, rval, rlen);
271 sv_catpvn(retval, "/", 1);
275 /* If purity is not set and maxdepth is set, then check depth:
276 * if we have reached maximum depth, return the string
277 * representation of the thing we are currently examining
278 * at this depth (i.e., 'Foo=ARRAY(0xdeadbeef)').
280 if (!purity && maxdepth > 0 && *levelp >= maxdepth) {
282 char *valstr = SvPV(val,vallen);
283 sv_catpvn(retval, "'", 1);
284 sv_catpvn(retval, valstr, vallen);
285 sv_catpvn(retval, "'", 1);
289 if (realpack) { /* we have a blessed ref */
291 char *blessstr = SvPV(bless, blesslen);
292 sv_catpvn(retval, blessstr, blesslen);
293 sv_catpvn(retval, "( ", 2);
296 apad = newSVsv(apad);
297 sv_x(aTHX_ apad, " ", 1, blesslen+2);
302 ipad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp);
304 if (realtype <= SVt_PVBM) { /* scalar ref */
305 SV *namesv = newSVpvn("${", 2);
306 sv_catpvn(namesv, name, namelen);
307 sv_catpvn(namesv, "}", 1);
308 if (realpack) { /* blessed */
309 sv_catpvn(retval, "do{\\(my $o = ", 13);
310 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
311 postav, levelp, indent, pad, xpad, apad, sep,
312 freezer, toaster, purity, deepcopy, quotekeys, bless,
314 sv_catpvn(retval, ")}", 2);
317 sv_catpvn(retval, "\\", 1);
318 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
319 postav, levelp, indent, pad, xpad, apad, sep,
320 freezer, toaster, purity, deepcopy, quotekeys, bless,
323 SvREFCNT_dec(namesv);
325 else if (realtype == SVt_PVGV) { /* glob ref */
326 SV *namesv = newSVpvn("*{", 2);
327 sv_catpvn(namesv, name, namelen);
328 sv_catpvn(namesv, "}", 1);
329 sv_catpvn(retval, "\\", 1);
330 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
331 postav, levelp, indent, pad, xpad, apad, sep,
332 freezer, toaster, purity, deepcopy, quotekeys, bless,
334 SvREFCNT_dec(namesv);
336 else if (realtype == SVt_PVAV) {
339 I32 ixmax = av_len((AV *)ival);
341 SV *ixsv = newSViv(0);
342 /* allowing for a 24 char wide array index */
343 New(0, iname, namelen+28, char);
344 (void)strcpy(iname, name);
346 if (name[0] == '@') {
347 sv_catpvn(retval, "(", 1);
351 sv_catpvn(retval, "[", 1);
352 /* omit "->" in $foo{bar}->[0], but not in ${$foo}->[0] */
354 && name[namelen-1] != ']' && name[namelen-1] != '}'
355 && (namelen < 4 || (name[1] != '{' && name[2] != '{')))*/
357 && name[namelen-1] != ']' && name[namelen-1] != '}')
360 || (name[0] == '\\' && name[2] == '{'))))
362 iname[inamelen++] = '-'; iname[inamelen++] = '>';
363 iname[inamelen] = '\0';
366 if (iname[0] == '*' && iname[inamelen-1] == '}' && inamelen >= 8 &&
367 (instr(iname+inamelen-8, "{SCALAR}") ||
368 instr(iname+inamelen-7, "{ARRAY}") ||
369 instr(iname+inamelen-6, "{HASH}"))) {
370 iname[inamelen++] = '-'; iname[inamelen++] = '>';
372 iname[inamelen++] = '['; iname[inamelen] = '\0';
373 totpad = newSVsv(sep);
374 sv_catsv(totpad, pad);
375 sv_catsv(totpad, apad);
377 for (ix = 0; ix <= ixmax; ++ix) {
380 svp = av_fetch((AV*)ival, ix, FALSE);
388 (void) sprintf(iname+ilen, "%"IVdf, (IV)ix);
389 ilen = strlen(iname);
390 iname[ilen++] = ']'; iname[ilen] = '\0';
392 sv_catsv(retval, totpad);
393 sv_catsv(retval, ipad);
394 sv_catpvn(retval, "#", 1);
395 sv_catsv(retval, ixsv);
397 sv_catsv(retval, totpad);
398 sv_catsv(retval, ipad);
399 DD_dump(aTHX_ elem, iname, ilen, retval, seenhv, postav,
400 levelp, indent, pad, xpad, apad, sep,
401 freezer, toaster, purity, deepcopy, quotekeys, bless,
404 sv_catpvn(retval, ",", 1);
407 SV *opad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), (*levelp)-1);
408 sv_catsv(retval, totpad);
409 sv_catsv(retval, opad);
413 sv_catpvn(retval, ")", 1);
415 sv_catpvn(retval, "]", 1);
417 SvREFCNT_dec(totpad);
420 else if (realtype == SVt_PVHV) {
421 SV *totpad, *newapad;
428 iname = newSVpvn(name, namelen);
429 if (name[0] == '%') {
430 sv_catpvn(retval, "(", 1);
431 (SvPVX(iname))[0] = '$';
434 sv_catpvn(retval, "{", 1);
435 /* omit "->" in $foo[0]->{bar}, but not in ${$foo}->{bar} */
437 && name[namelen-1] != ']' && name[namelen-1] != '}')
440 || (name[0] == '\\' && name[2] == '{'))))
442 sv_catpvn(iname, "->", 2);
445 if (name[0] == '*' && name[namelen-1] == '}' && namelen >= 8 &&
446 (instr(name+namelen-8, "{SCALAR}") ||
447 instr(name+namelen-7, "{ARRAY}") ||
448 instr(name+namelen-6, "{HASH}"))) {
449 sv_catpvn(iname, "->", 2);
451 sv_catpvn(iname, "{", 1);
452 totpad = newSVsv(sep);
453 sv_catsv(totpad, pad);
454 sv_catsv(totpad, apad);
456 (void)hv_iterinit((HV*)ival);
458 while ((entry = hv_iternext((HV*)ival))) {
463 sv_catpvn(retval, ",", 1);
465 key = hv_iterkey(entry, &klen);
466 hval = hv_iterval((HV*)ival, entry);
468 if (quotekeys || needs_quote(key)) {
469 nticks = num_q(key, klen);
470 New(0, nkey, klen+nticks+3, char);
473 klen += esc_q(nkey+1, key, klen);
475 (void)Copy(key, nkey+1, klen, char);
480 New(0, nkey, klen, char);
481 (void)Copy(key, nkey, klen, char);
484 sname = newSVsv(iname);
485 sv_catpvn(sname, nkey, klen);
486 sv_catpvn(sname, "}", 1);
488 sv_catsv(retval, totpad);
489 sv_catsv(retval, ipad);
490 sv_catpvn(retval, nkey, klen);
491 sv_catpvn(retval, " => ", 4);
495 newapad = newSVsv(apad);
496 New(0, extra, klen+4+1, char);
497 while (elen < (klen+4))
500 sv_catpvn(newapad, extra, elen);
506 DD_dump(aTHX_ hval, SvPVX(sname), SvCUR(sname), retval, seenhv,
507 postav, levelp, indent, pad, xpad, newapad, sep,
508 freezer, toaster, purity, deepcopy, quotekeys, bless,
513 SvREFCNT_dec(newapad);
516 SV *opad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp-1);
517 sv_catsv(retval, totpad);
518 sv_catsv(retval, opad);
522 sv_catpvn(retval, ")", 1);
524 sv_catpvn(retval, "}", 1);
526 SvREFCNT_dec(totpad);
528 else if (realtype == SVt_PVCV) {
529 sv_catpvn(retval, "sub { \"DUMMY\" }", 15);
531 warn("Encountered CODE ref, using dummy placeholder");
534 warn("cannot handle ref type %ld", realtype);
537 if (realpack) { /* free blessed allocs */
542 sv_catpvn(retval, ", '", 3);
543 sv_catpvn(retval, realpack, strlen(realpack));
544 sv_catpvn(retval, "' )", 3);
545 if (toaster && SvPOK(toaster) && SvCUR(toaster)) {
546 sv_catpvn(retval, "->", 2);
547 sv_catsv(retval, toaster);
548 sv_catpvn(retval, "()", 2);
558 (void) sprintf(id, "0x%lx", (unsigned long)val);
559 if ((svp = hv_fetch(seenhv, id, (idlen = strlen(id)), FALSE)) &&
560 (sv = *svp) && SvROK(sv) &&
561 (seenentry = (AV*)SvRV(sv)))
564 if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)
565 && (svp = av_fetch(seenentry, 2, FALSE)) && *svp && SvIV(*svp) > 0)
567 sv_catpvn(retval, "${", 2);
568 sv_catsv(retval, othername);
569 sv_catpvn(retval, "}", 1);
575 namesv = newSVpvn("\\", 1);
576 sv_catpvn(namesv, name, namelen);
578 av_push(seenentry, namesv);
579 av_push(seenentry, newRV(val));
580 (void)hv_store(seenhv, id, strlen(id), newRV((SV*)seenentry), 0);
581 SvREFCNT_dec(seenentry);
588 (void) sprintf(tmpbuf, "%"IVdf, (IV)i);
589 len = strlen(tmpbuf);
590 sv_catpvn(retval, tmpbuf, len);
592 else if (realtype == SVt_PVGV) {/* GLOBs can end up with scribbly names */
594 ++c; --i; /* just get the name */
595 if (i >= 6 && strncmp(c, "main::", 6) == 0) {
599 if (needs_quote(c)) {
600 sv_grow(retval, SvCUR(retval)+6+2*i);
601 r = SvPVX(retval)+SvCUR(retval);
602 r[0] = '*'; r[1] = '{'; r[2] = '\'';
603 i += esc_q(r+3, c, i);
605 r[i++] = '\''; r[i++] = '}';
609 sv_grow(retval, SvCUR(retval)+i+2);
610 r = SvPVX(retval)+SvCUR(retval);
611 r[0] = '*'; strcpy(r+1, c);
614 SvCUR_set(retval, SvCUR(retval)+i);
617 static char *entries[] = { "{SCALAR}", "{ARRAY}", "{HASH}" };
618 static STRLEN sizes[] = { 8, 7, 6 };
620 SV *nname = newSVpvn("", 0);
621 SV *newapad = newSVpvn("", 0);
625 for (j=0; j<3; j++) {
626 e = ((j == 0) ? GvSV(gv) : (j == 1) ? (SV*)GvAV(gv) : (SV*)GvHV(gv));
629 if (j == 0 && !SvOK(e))
634 SV *postentry = newSVpvn(r,i);
636 sv_setsv(nname, postentry);
637 sv_catpvn(nname, entries[j], sizes[j]);
638 sv_catpvn(postentry, " = ", 3);
639 av_push(postav, postentry);
644 (void)sv_x(aTHX_ newapad, " ", 1, SvCUR(postentry));
646 DD_dump(aTHX_ e, SvPVX(nname), SvCUR(nname), postentry,
647 seenhv, postav, &nlevel, indent, pad, xpad,
648 newapad, sep, freezer, toaster, purity,
649 deepcopy, quotekeys, bless, maxdepth);
654 SvREFCNT_dec(newapad);
658 else if (val == &PL_sv_undef || !SvOK(val)) {
659 sv_catpvn(retval, "undef", 5);
663 sv_grow(retval, SvCUR(retval)+3+2*i);
664 r = SvPVX(retval)+SvCUR(retval);
666 i += esc_q(r+1, c, i);
670 SvCUR_set(retval, SvCUR(retval)+i);
676 (void)hv_delete(seenhv, id, idlen, G_DISCARD);
677 else if (namelen && seenentry) {
678 SV *mark = *av_fetch(seenentry, 2, TRUE);
686 MODULE = Data::Dumper PACKAGE = Data::Dumper PREFIX = Data_Dumper_
689 # This is the exact equivalent of Dump. Well, almost. The things that are
690 # different as of now (due to Laziness):
691 # * doesnt do double-quotes yet.
695 Data_Dumper_Dumpxs(href, ...)
703 AV *postav, *todumpav, *namesav;
705 I32 indent, terse, useqq, i, imax, postlen;
707 SV *val, *name, *pad, *xpad, *apad, *sep, *tmp, *varname;
708 SV *freezer, *toaster, *bless;
709 I32 purity, deepcopy, quotekeys, maxdepth = 0;
713 if (!SvROK(href)) { /* call new to get an object first */
722 croak("Usage: Data::Dumper::Dumpxs(PACKAGE, VAL_ARY_REF, NAME_ARY_REF)");
729 XPUSHs(sv_2mortal(newSVsv(valarray)));
730 XPUSHs(sv_2mortal(newSVsv(namearray)));
732 i = perl_call_method("new", G_SCALAR);
735 href = newSVsv(POPs);
741 (void)sv_2mortal(href);
744 todumpav = namesav = Nullav;
746 val = pad = xpad = apad = sep = tmp = varname
747 = freezer = toaster = bless = &PL_sv_undef;
748 name = sv_newmortal();
750 terse = useqq = purity = deepcopy = 0;
753 retval = newSVpvn("", 0);
755 && (hv = (HV*)SvRV((SV*)href))
756 && SvTYPE(hv) == SVt_PVHV) {
758 if ((svp = hv_fetch(hv, "seen", 4, FALSE)) && SvROK(*svp))
759 seenhv = (HV*)SvRV(*svp);
760 if ((svp = hv_fetch(hv, "todump", 6, FALSE)) && SvROK(*svp))
761 todumpav = (AV*)SvRV(*svp);
762 if ((svp = hv_fetch(hv, "names", 5, FALSE)) && SvROK(*svp))
763 namesav = (AV*)SvRV(*svp);
764 if ((svp = hv_fetch(hv, "indent", 6, FALSE)))
766 if ((svp = hv_fetch(hv, "purity", 6, FALSE)))
768 if ((svp = hv_fetch(hv, "terse", 5, FALSE)))
769 terse = SvTRUE(*svp);
770 if ((svp = hv_fetch(hv, "useqq", 5, FALSE)))
771 useqq = SvTRUE(*svp);
772 if ((svp = hv_fetch(hv, "pad", 3, FALSE)))
774 if ((svp = hv_fetch(hv, "xpad", 4, FALSE)))
776 if ((svp = hv_fetch(hv, "apad", 4, FALSE)))
778 if ((svp = hv_fetch(hv, "sep", 3, FALSE)))
780 if ((svp = hv_fetch(hv, "varname", 7, FALSE)))
782 if ((svp = hv_fetch(hv, "freezer", 7, FALSE)))
784 if ((svp = hv_fetch(hv, "toaster", 7, FALSE)))
786 if ((svp = hv_fetch(hv, "deepcopy", 8, FALSE)))
787 deepcopy = SvTRUE(*svp);
788 if ((svp = hv_fetch(hv, "quotekeys", 9, FALSE)))
789 quotekeys = SvTRUE(*svp);
790 if ((svp = hv_fetch(hv, "bless", 5, FALSE)))
792 if ((svp = hv_fetch(hv, "maxdepth", 8, FALSE)))
793 maxdepth = SvIV(*svp);
797 imax = av_len(todumpav);
800 valstr = newSVpvn("",0);
801 for (i = 0; i <= imax; ++i) {
805 if ((svp = av_fetch(todumpav, i, FALSE)))
809 if ((svp = av_fetch(namesav, i, TRUE)))
810 sv_setsv(name, *svp);
815 if ((SvPVX(name))[0] == '*') {
817 switch (SvTYPE(SvRV(val))) {
819 (SvPVX(name))[0] = '@';
822 (SvPVX(name))[0] = '%';
825 (SvPVX(name))[0] = '*';
828 (SvPVX(name))[0] = '$';
833 (SvPVX(name))[0] = '$';
835 else if ((SvPVX(name))[0] != '$')
836 sv_insert(name, 0, 0, "$", 1);
840 sv_setpvn(name, "$", 1);
841 sv_catsv(name, varname);
842 (void) sprintf(tmpbuf, "%"IVdf, (IV)(i+1));
843 nchars = strlen(tmpbuf);
844 sv_catpvn(name, tmpbuf, nchars);
848 SV *tmpsv = sv_x(aTHX_ Nullsv, " ", 1, SvCUR(name)+3);
849 newapad = newSVsv(apad);
850 sv_catsv(newapad, tmpsv);
856 DD_dump(aTHX_ val, SvPVX(name), SvCUR(name), valstr, seenhv,
857 postav, &level, indent, pad, xpad, newapad, sep,
858 freezer, toaster, purity, deepcopy, quotekeys,
862 SvREFCNT_dec(newapad);
864 postlen = av_len(postav);
865 if (postlen >= 0 || !terse) {
866 sv_insert(valstr, 0, 0, " = ", 3);
867 sv_insert(valstr, 0, 0, SvPVX(name), SvCUR(name));
868 sv_catpvn(valstr, ";", 1);
870 sv_catsv(retval, pad);
871 sv_catsv(retval, valstr);
872 sv_catsv(retval, sep);
875 sv_catsv(retval, pad);
876 for (i = 0; i <= postlen; ++i) {
878 svp = av_fetch(postav, i, FALSE);
879 if (svp && (elem = *svp)) {
880 sv_catsv(retval, elem);
882 sv_catpvn(retval, ";", 1);
883 sv_catsv(retval, sep);
884 sv_catsv(retval, pad);
888 sv_catpvn(retval, ";", 1);
889 sv_catsv(retval, sep);
891 sv_setpvn(valstr, "", 0);
892 if (gimme == G_ARRAY) {
893 XPUSHs(sv_2mortal(retval));
894 if (i < imax) /* not the last time thro ? */
895 retval = newSVpvn("",0);
898 SvREFCNT_dec(postav);
899 SvREFCNT_dec(valstr);
902 croak("Call to new() method failed to return HASH ref");
903 if (gimme == G_SCALAR)
904 XPUSHs(sv_2mortal(retval));