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);
32 /* does a string need to be protected? */
34 needs_quote(register char *s)
58 /* count the number of "'"s and "\"s in string */
60 num_q(register char *s, register STRLEN slen)
65 if (*s == '\'' || *s == '\\')
74 /* returns number of chars added to escape "'"s and "\"s in s */
75 /* slen number of characters in s will be escaped */
76 /* destination must be long enough for additional chars */
78 esc_q(register char *d, register char *s, register STRLEN slen)
97 /* append a repeated string to an SV */
99 sv_x(pTHX_ SV *sv, register char *str, STRLEN len, I32 n)
102 sv = newSVpvn("", 0);
104 assert(SvTYPE(sv) >= SVt_PV);
107 SvGROW(sv, len*n + SvCUR(sv) + 1);
109 char *start = SvPVX(sv) + SvCUR(sv);
117 sv_catpvn(sv, str, len);
125 * This ought to be split into smaller functions. (it is one long function since
126 * it exactly parallels the perl version, which was one long thing for
127 * efficiency raisins.) Ugggh!
130 DD_dump(pTHX_ SV *val, char *name, STRLEN namelen, SV *retval, HV *seenhv,
131 AV *postav, I32 *levelp, I32 indent, SV *pad, SV *xpad,
132 SV *apad, SV *sep, SV *freezer, SV *toaster, I32 purity,
133 I32 deepcopy, I32 quotekeys, SV *bless)
137 char *c, *r, *realpack, id[128];
139 SV *sv, *ipad, *ival;
140 SV *blesspad = Nullsv;
141 AV *seenentry = Nullav;
143 STRLEN inamelen, idlen = 0;
150 flags = SvFLAGS(val);
151 realtype = SvTYPE(val);
157 if (SvOBJECT(SvRV(val)) && freezer &&
158 SvPOK(freezer) && SvCUR(freezer))
160 dSP; ENTER; SAVETMPS; PUSHMARK(sp);
161 XPUSHs(val); PUTBACK;
162 i = perl_call_method(SvPVX(freezer), G_EVAL|G_SCALAR);
165 warn("WARNING(Freezer method call failed): %s",
169 PUTBACK; FREETMPS; LEAVE;
171 (void)sv_2mortal(val);
175 flags = SvFLAGS(ival);
176 realtype = SvTYPE(ival);
177 (void) sprintf(id, "0x%lx", (unsigned long)ival);
180 realpack = HvNAME(SvSTASH(ival));
184 /* if it has a name, we need to either look it up, or keep a tab
185 * on it so we know when we hit it later
188 if ((svp = hv_fetch(seenhv, id, idlen, FALSE))
189 && (sv = *svp) && SvROK(sv) && (seenentry = (AV*)SvRV(sv)))
192 if ((svp = av_fetch(seenentry, 0, FALSE))
193 && (othername = *svp))
195 if (purity && *levelp > 0) {
198 if (realtype == SVt_PVHV)
199 sv_catpvn(retval, "{}", 2);
200 else if (realtype == SVt_PVAV)
201 sv_catpvn(retval, "[]", 2);
203 sv_catpvn(retval, "''", 2);
204 postentry = newSVpvn(name, namelen);
205 sv_catpvn(postentry, " = ", 3);
206 sv_catsv(postentry, othername);
207 av_push(postav, postentry);
210 if (name[0] == '@' || name[0] == '%') {
211 if ((SvPVX(othername))[0] == '\\' &&
212 (SvPVX(othername))[1] == name[0]) {
213 sv_catpvn(retval, SvPVX(othername)+1,
217 sv_catpvn(retval, name, 1);
218 sv_catpvn(retval, "{", 1);
219 sv_catsv(retval, othername);
220 sv_catpvn(retval, "}", 1);
224 sv_catsv(retval, othername);
229 warn("ref name not found for %s", id);
233 else { /* store our name and continue */
235 if (name[0] == '@' || name[0] == '%') {
236 namesv = newSVpvn("\\", 1);
237 sv_catpvn(namesv, name, namelen);
239 else if (realtype == SVt_PVCV && name[0] == '*') {
240 namesv = newSVpvn("\\", 2);
241 sv_catpvn(namesv, name, namelen);
242 (SvPVX(namesv))[1] = '&';
245 namesv = newSVpvn(name, namelen);
247 av_push(seenentry, namesv);
248 (void)SvREFCNT_inc(val);
249 av_push(seenentry, val);
250 (void)hv_store(seenhv, id, strlen(id),
251 newRV((SV*)seenentry), 0);
252 SvREFCNT_dec(seenentry);
257 if (*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);
273 else { /* we have a blessed ref */
275 char *blessstr = SvPV(bless, blesslen);
276 sv_catpvn(retval, blessstr, blesslen);
277 sv_catpvn(retval, "( ", 2);
280 apad = newSVsv(apad);
281 sv_x(aTHX_ apad, " ", 1, blesslen+2);
287 ipad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp);
289 if (realtype <= SVt_PVBM) { /* scalar ref */
290 SV *namesv = newSVpvn("${", 2);
291 sv_catpvn(namesv, name, namelen);
292 sv_catpvn(namesv, "}", 1);
293 if (realpack) { /* blessed */
294 sv_catpvn(retval, "do{\\(my $o = ", 13);
295 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
296 postav, levelp, indent, pad, xpad, apad, sep,
297 freezer, toaster, purity, deepcopy, quotekeys, bless);
298 sv_catpvn(retval, ")}", 2);
301 sv_catpvn(retval, "\\", 1);
302 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
303 postav, levelp, indent, pad, xpad, apad, sep,
304 freezer, toaster, purity, deepcopy, quotekeys, bless);
306 SvREFCNT_dec(namesv);
308 else if (realtype == SVt_PVGV) { /* glob ref */
309 SV *namesv = newSVpvn("*{", 2);
310 sv_catpvn(namesv, name, namelen);
311 sv_catpvn(namesv, "}", 1);
312 sv_catpvn(retval, "\\", 1);
313 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
314 postav, levelp, indent, pad, xpad, apad, sep,
315 freezer, toaster, purity, deepcopy, quotekeys, bless);
316 SvREFCNT_dec(namesv);
318 else if (realtype == SVt_PVAV) {
321 I32 ixmax = av_len((AV *)ival);
323 SV *ixsv = newSViv(0);
324 /* allowing for a 24 char wide array index */
325 New(0, iname, namelen+28, char);
326 (void)strcpy(iname, name);
328 if (name[0] == '@') {
329 sv_catpvn(retval, "(", 1);
333 sv_catpvn(retval, "[", 1);
334 /* omit "->" in $foo{bar}->[0], but not in ${$foo}->[0] */
336 && name[namelen-1] != ']' && name[namelen-1] != '}'
337 && (namelen < 4 || (name[1] != '{' && name[2] != '{')))*/
339 && name[namelen-1] != ']' && name[namelen-1] != '}')
342 || (name[0] == '\\' && name[2] == '{'))))
344 iname[inamelen++] = '-'; iname[inamelen++] = '>';
345 iname[inamelen] = '\0';
348 if (iname[0] == '*' && iname[inamelen-1] == '}' && inamelen >= 8 &&
349 (instr(iname+inamelen-8, "{SCALAR}") ||
350 instr(iname+inamelen-7, "{ARRAY}") ||
351 instr(iname+inamelen-6, "{HASH}"))) {
352 iname[inamelen++] = '-'; iname[inamelen++] = '>';
354 iname[inamelen++] = '['; iname[inamelen] = '\0';
355 totpad = newSVsv(sep);
356 sv_catsv(totpad, pad);
357 sv_catsv(totpad, apad);
359 for (ix = 0; ix <= ixmax; ++ix) {
362 svp = av_fetch((AV*)ival, ix, FALSE);
370 (void) sprintf(iname+ilen, "%ld", ix);
371 ilen = strlen(iname);
372 iname[ilen++] = ']'; iname[ilen] = '\0';
374 sv_catsv(retval, totpad);
375 sv_catsv(retval, ipad);
376 sv_catpvn(retval, "#", 1);
377 sv_catsv(retval, ixsv);
379 sv_catsv(retval, totpad);
380 sv_catsv(retval, ipad);
381 DD_dump(aTHX_ elem, iname, ilen, retval, seenhv, postav,
382 levelp, indent, pad, xpad, apad, sep,
383 freezer, toaster, purity, deepcopy, quotekeys, bless);
385 sv_catpvn(retval, ",", 1);
388 SV *opad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), (*levelp)-1);
389 sv_catsv(retval, totpad);
390 sv_catsv(retval, opad);
394 sv_catpvn(retval, ")", 1);
396 sv_catpvn(retval, "]", 1);
398 SvREFCNT_dec(totpad);
401 else if (realtype == SVt_PVHV) {
402 SV *totpad, *newapad;
409 iname = newSVpvn(name, namelen);
410 if (name[0] == '%') {
411 sv_catpvn(retval, "(", 1);
412 (SvPVX(iname))[0] = '$';
415 sv_catpvn(retval, "{", 1);
416 /* omit "->" in $foo[0]->{bar}, but not in ${$foo}->{bar} */
418 && name[namelen-1] != ']' && name[namelen-1] != '}')
421 || (name[0] == '\\' && name[2] == '{'))))
423 sv_catpvn(iname, "->", 2);
426 if (name[0] == '*' && name[namelen-1] == '}' && namelen >= 8 &&
427 (instr(name+namelen-8, "{SCALAR}") ||
428 instr(name+namelen-7, "{ARRAY}") ||
429 instr(name+namelen-6, "{HASH}"))) {
430 sv_catpvn(iname, "->", 2);
432 sv_catpvn(iname, "{", 1);
433 totpad = newSVsv(sep);
434 sv_catsv(totpad, pad);
435 sv_catsv(totpad, apad);
437 (void)hv_iterinit((HV*)ival);
439 while ((entry = hv_iternext((HV*)ival))) {
444 sv_catpvn(retval, ",", 1);
446 key = hv_iterkey(entry, &klen);
447 hval = hv_iterval((HV*)ival, entry);
449 if (quotekeys || needs_quote(key)) {
450 nticks = num_q(key, klen);
451 New(0, nkey, klen+nticks+3, char);
454 klen += esc_q(nkey+1, key, klen);
456 (void)Copy(key, nkey+1, klen, char);
461 New(0, nkey, klen, char);
462 (void)Copy(key, nkey, klen, char);
465 sname = newSVsv(iname);
466 sv_catpvn(sname, nkey, klen);
467 sv_catpvn(sname, "}", 1);
469 sv_catsv(retval, totpad);
470 sv_catsv(retval, ipad);
471 sv_catpvn(retval, nkey, klen);
472 sv_catpvn(retval, " => ", 4);
476 newapad = newSVsv(apad);
477 New(0, extra, klen+4+1, char);
478 while (elen < (klen+4))
481 sv_catpvn(newapad, extra, elen);
487 DD_dump(aTHX_ hval, SvPVX(sname), SvCUR(sname), retval, seenhv,
488 postav, levelp, indent, pad, xpad, newapad, sep,
489 freezer, toaster, purity, deepcopy, quotekeys, bless);
493 SvREFCNT_dec(newapad);
496 SV *opad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp-1);
497 sv_catsv(retval, totpad);
498 sv_catsv(retval, opad);
502 sv_catpvn(retval, ")", 1);
504 sv_catpvn(retval, "}", 1);
506 SvREFCNT_dec(totpad);
508 else if (realtype == SVt_PVCV) {
509 sv_catpvn(retval, "sub { \"DUMMY\" }", 15);
511 warn("Encountered CODE ref, using dummy placeholder");
514 warn("cannot handle ref type %ld", realtype);
517 if (realpack) { /* free blessed allocs */
522 sv_catpvn(retval, ", '", 3);
523 sv_catpvn(retval, realpack, strlen(realpack));
524 sv_catpvn(retval, "' )", 3);
525 if (toaster && SvPOK(toaster) && SvCUR(toaster)) {
526 sv_catpvn(retval, "->", 2);
527 sv_catsv(retval, toaster);
528 sv_catpvn(retval, "()", 2);
538 (void) sprintf(id, "0x%lx", (unsigned long)val);
539 if ((svp = hv_fetch(seenhv, id, (idlen = strlen(id)), FALSE)) &&
540 (sv = *svp) && SvROK(sv) &&
541 (seenentry = (AV*)SvRV(sv)))
544 if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)
545 && (svp = av_fetch(seenentry, 2, FALSE)) && *svp && SvIV(*svp) > 0)
547 sv_catpvn(retval, "${", 2);
548 sv_catsv(retval, othername);
549 sv_catpvn(retval, "}", 1);
555 namesv = newSVpvn("\\", 1);
556 sv_catpvn(namesv, name, namelen);
558 av_push(seenentry, namesv);
559 av_push(seenentry, newRV(val));
560 (void)hv_store(seenhv, id, strlen(id), newRV((SV*)seenentry), 0);
561 SvREFCNT_dec(seenentry);
568 (void) sprintf(tmpbuf, "%d", i);
569 len = strlen(tmpbuf);
570 sv_catpvn(retval, tmpbuf, len);
572 else if (realtype == SVt_PVGV) {/* GLOBs can end up with scribbly names */
574 ++c; --i; /* just get the name */
575 if (i >= 6 && strncmp(c, "main::", 6) == 0) {
579 if (needs_quote(c)) {
580 sv_grow(retval, SvCUR(retval)+6+2*i);
581 r = SvPVX(retval)+SvCUR(retval);
582 r[0] = '*'; r[1] = '{'; r[2] = '\'';
583 i += esc_q(r+3, c, i);
585 r[i++] = '\''; r[i++] = '}';
589 sv_grow(retval, SvCUR(retval)+i+2);
590 r = SvPVX(retval)+SvCUR(retval);
591 r[0] = '*'; strcpy(r+1, c);
594 SvCUR_set(retval, SvCUR(retval)+i);
597 static char *entries[] = { "{SCALAR}", "{ARRAY}", "{HASH}" };
598 static STRLEN sizes[] = { 8, 7, 6 };
600 SV *nname = newSVpvn("", 0);
601 SV *newapad = newSVpvn("", 0);
605 for (j=0; j<3; j++) {
606 e = ((j == 0) ? GvSV(gv) : (j == 1) ? (SV*)GvAV(gv) : (SV*)GvHV(gv));
609 if (j == 0 && !SvOK(e))
614 SV *postentry = newSVpvn(r,i);
616 sv_setsv(nname, postentry);
617 sv_catpvn(nname, entries[j], sizes[j]);
618 sv_catpvn(postentry, " = ", 3);
619 av_push(postav, postentry);
624 (void)sv_x(aTHX_ newapad, " ", 1, SvCUR(postentry));
626 DD_dump(aTHX_ e, SvPVX(nname), SvCUR(nname), postentry,
627 seenhv, postav, &nlevel, indent, pad, xpad,
628 newapad, sep, freezer, toaster, purity,
629 deepcopy, quotekeys, bless);
634 SvREFCNT_dec(newapad);
638 else if (val == &PL_sv_undef || !SvOK(val)) {
639 sv_catpvn(retval, "undef", 5);
643 sv_grow(retval, SvCUR(retval)+3+2*i);
644 r = SvPVX(retval)+SvCUR(retval);
646 i += esc_q(r+1, c, i);
650 SvCUR_set(retval, SvCUR(retval)+i);
656 (void)hv_delete(seenhv, id, idlen, G_DISCARD);
657 else if (namelen && seenentry) {
658 SV *mark = *av_fetch(seenentry, 2, TRUE);
666 MODULE = Data::Dumper PACKAGE = Data::Dumper PREFIX = Data_Dumper_
669 # This is the exact equivalent of Dump. Well, almost. The things that are
670 # different as of now (due to Laziness):
671 # * doesnt do double-quotes yet.
675 Data_Dumper_Dumpxs(href, ...)
683 AV *postav, *todumpav, *namesav;
685 I32 indent, terse, useqq, i, imax, postlen;
687 SV *val, *name, *pad, *xpad, *apad, *sep, *tmp, *varname;
688 SV *freezer, *toaster, *bless;
689 I32 purity, deepcopy, quotekeys;
693 if (!SvROK(href)) { /* call new to get an object first */
702 croak("Usage: Data::Dumper::Dumpxs(PACKAGE, VAL_ARY_REF, NAME_ARY_REF)");
709 XPUSHs(sv_2mortal(newSVsv(valarray)));
710 XPUSHs(sv_2mortal(newSVsv(namearray)));
712 i = perl_call_method("new", G_SCALAR);
715 href = newSVsv(POPs);
721 (void)sv_2mortal(href);
724 todumpav = namesav = Nullav;
726 val = pad = xpad = apad = sep = tmp = varname
727 = freezer = toaster = bless = &PL_sv_undef;
728 name = sv_newmortal();
730 terse = useqq = purity = deepcopy = 0;
733 retval = newSVpvn("", 0);
735 && (hv = (HV*)SvRV((SV*)href))
736 && SvTYPE(hv) == SVt_PVHV) {
738 if ((svp = hv_fetch(hv, "seen", 4, FALSE)) && SvROK(*svp))
739 seenhv = (HV*)SvRV(*svp);
740 if ((svp = hv_fetch(hv, "todump", 6, FALSE)) && SvROK(*svp))
741 todumpav = (AV*)SvRV(*svp);
742 if ((svp = hv_fetch(hv, "names", 5, FALSE)) && SvROK(*svp))
743 namesav = (AV*)SvRV(*svp);
744 if ((svp = hv_fetch(hv, "indent", 6, FALSE)))
746 if ((svp = hv_fetch(hv, "purity", 6, FALSE)))
748 if ((svp = hv_fetch(hv, "terse", 5, FALSE)))
749 terse = SvTRUE(*svp);
750 if ((svp = hv_fetch(hv, "useqq", 5, FALSE)))
751 useqq = SvTRUE(*svp);
752 if ((svp = hv_fetch(hv, "pad", 3, FALSE)))
754 if ((svp = hv_fetch(hv, "xpad", 4, FALSE)))
756 if ((svp = hv_fetch(hv, "apad", 4, FALSE)))
758 if ((svp = hv_fetch(hv, "sep", 3, FALSE)))
760 if ((svp = hv_fetch(hv, "varname", 7, FALSE)))
762 if ((svp = hv_fetch(hv, "freezer", 7, FALSE)))
764 if ((svp = hv_fetch(hv, "toaster", 7, FALSE)))
766 if ((svp = hv_fetch(hv, "deepcopy", 8, FALSE)))
767 deepcopy = SvTRUE(*svp);
768 if ((svp = hv_fetch(hv, "quotekeys", 9, FALSE)))
769 quotekeys = SvTRUE(*svp);
770 if ((svp = hv_fetch(hv, "bless", 5, FALSE)))
775 imax = av_len(todumpav);
778 valstr = newSVpvn("",0);
779 for (i = 0; i <= imax; ++i) {
783 if ((svp = av_fetch(todumpav, i, FALSE)))
787 if ((svp = av_fetch(namesav, i, TRUE)))
788 sv_setsv(name, *svp);
793 if ((SvPVX(name))[0] == '*') {
795 switch (SvTYPE(SvRV(val))) {
797 (SvPVX(name))[0] = '@';
800 (SvPVX(name))[0] = '%';
803 (SvPVX(name))[0] = '*';
806 (SvPVX(name))[0] = '$';
811 (SvPVX(name))[0] = '$';
813 else if ((SvPVX(name))[0] != '$')
814 sv_insert(name, 0, 0, "$", 1);
818 sv_setpvn(name, "$", 1);
819 sv_catsv(name, varname);
820 (void) sprintf(tmpbuf, "%ld", i+1);
821 nchars = strlen(tmpbuf);
822 sv_catpvn(name, tmpbuf, nchars);
826 SV *tmpsv = sv_x(aTHX_ Nullsv, " ", 1, SvCUR(name)+3);
827 newapad = newSVsv(apad);
828 sv_catsv(newapad, tmpsv);
834 DD_dump(aTHX_ val, SvPVX(name), SvCUR(name), valstr, seenhv,
835 postav, &level, indent, pad, xpad, newapad, sep,
836 freezer, toaster, purity, deepcopy, quotekeys,
840 SvREFCNT_dec(newapad);
842 postlen = av_len(postav);
843 if (postlen >= 0 || !terse) {
844 sv_insert(valstr, 0, 0, " = ", 3);
845 sv_insert(valstr, 0, 0, SvPVX(name), SvCUR(name));
846 sv_catpvn(valstr, ";", 1);
848 sv_catsv(retval, pad);
849 sv_catsv(retval, valstr);
850 sv_catsv(retval, sep);
853 sv_catsv(retval, pad);
854 for (i = 0; i <= postlen; ++i) {
856 svp = av_fetch(postav, i, FALSE);
857 if (svp && (elem = *svp)) {
858 sv_catsv(retval, elem);
860 sv_catpvn(retval, ";", 1);
861 sv_catsv(retval, sep);
862 sv_catsv(retval, pad);
866 sv_catpvn(retval, ";", 1);
867 sv_catsv(retval, sep);
869 sv_setpvn(valstr, "", 0);
870 if (gimme == G_ARRAY) {
871 XPUSHs(sv_2mortal(retval));
872 if (i < imax) /* not the last time thro ? */
873 retval = newSVpvn("",0);
876 SvREFCNT_dec(postav);
877 SvREFCNT_dec(valstr);
880 croak("Call to new() method failed to return HASH ref");
881 if (gimme == G_SCALAR)
882 XPUSHs(sv_2mortal(retval));