5 #include "patchlevel.h"
9 # define PL_sv_undef sv_undef
12 # define ERRSV GvSV(errgv)
15 # define newSVpvn newSVpv
19 static I32 num_q _((char *s, STRLEN slen));
20 static I32 esc_q _((char *dest, char *src, STRLEN slen));
21 static SV *sv_x _((SV *sv, char *str, STRLEN len, I32 n));
22 static I32 DD_dump _((SV *val, char *name, STRLEN namelen, SV *retval,
23 HV *seenhv, AV *postav, I32 *levelp, I32 indent,
24 SV *pad, SV *xpad, SV *apad, SV *sep,
25 SV *freezer, SV *toaster,
26 I32 purity, I32 deepcopy, I32 quotekeys, SV *bless));
28 /* does a string need to be protected? */
30 needs_quote(register char *s)
54 /* count the number of "'"s and "\"s in string */
56 num_q(register char *s, register STRLEN slen)
61 if (*s == '\'' || *s == '\\')
70 /* returns number of chars added to escape "'"s and "\"s in s */
71 /* slen number of characters in s will be escaped */
72 /* destination must be long enough for additional chars */
74 esc_q(register char *d, register char *s, register STRLEN slen)
93 /* append a repeated string to an SV */
95 sv_x(SV *sv, register char *str, STRLEN len, I32 n)
100 assert(SvTYPE(sv) >= SVt_PV);
103 SvGROW(sv, len*n + SvCUR(sv) + 1);
105 char *start = SvPVX(sv) + SvCUR(sv);
113 sv_catpvn(sv, str, len);
121 * This ought to be split into smaller functions. (it is one long function since
122 * it exactly parallels the perl version, which was one long thing for
123 * efficiency raisins.) Ugggh!
126 DD_dump(SV *val, char *name, STRLEN namelen, SV *retval, HV *seenhv,
127 AV *postav, I32 *levelp, I32 indent, SV *pad, SV *xpad,
128 SV *apad, SV *sep, SV *freezer, SV *toaster, I32 purity,
129 I32 deepcopy, I32 quotekeys, SV *bless)
133 char *c, *r, *realpack, id[128];
135 SV *sv, *ipad, *ival;
136 SV *blesspad = Nullsv;
137 AV *seenentry = Nullav;
139 STRLEN inamelen, idlen = 0;
146 flags = SvFLAGS(val);
147 realtype = SvTYPE(val);
153 if (SvOBJECT(SvRV(val)) && freezer &&
154 SvPOK(freezer) && SvCUR(freezer))
156 dSP; ENTER; SAVETMPS; PUSHMARK(sp);
157 XPUSHs(val); PUTBACK;
158 i = perl_call_method(SvPVX(freezer), G_EVAL|G_SCALAR);
161 warn("WARNING(Freezer method call failed): %s",
165 PUTBACK; FREETMPS; LEAVE;
167 (void)sv_2mortal(val);
171 flags = SvFLAGS(ival);
172 realtype = SvTYPE(ival);
173 (void) sprintf(id, "0x%lx", (unsigned long)ival);
176 realpack = HvNAME(SvSTASH(ival));
180 /* if it has a name, we need to either look it up, or keep a tab
181 * on it so we know when we hit it later
184 if ((svp = hv_fetch(seenhv, id, idlen, FALSE))
185 && (sv = *svp) && SvROK(sv) && (seenentry = (AV*)SvRV(sv)))
188 if ((svp = av_fetch(seenentry, 0, FALSE))
189 && (othername = *svp))
191 if (purity && *levelp > 0) {
194 if (realtype == SVt_PVHV)
195 sv_catpvn(retval, "{}", 2);
196 else if (realtype == SVt_PVAV)
197 sv_catpvn(retval, "[]", 2);
199 sv_catpvn(retval, "''", 2);
200 postentry = newSVpvn(name, namelen);
201 sv_catpvn(postentry, " = ", 3);
202 sv_catsv(postentry, othername);
203 av_push(postav, postentry);
206 if (name[0] == '@' || name[0] == '%') {
207 if ((SvPVX(othername))[0] == '\\' &&
208 (SvPVX(othername))[1] == name[0]) {
209 sv_catpvn(retval, SvPVX(othername)+1,
213 sv_catpvn(retval, name, 1);
214 sv_catpvn(retval, "{", 1);
215 sv_catsv(retval, othername);
216 sv_catpvn(retval, "}", 1);
220 sv_catsv(retval, othername);
225 warn("ref name not found for %s", id);
229 else { /* store our name and continue */
231 if (name[0] == '@' || name[0] == '%') {
232 namesv = newSVpvn("\\", 1);
233 sv_catpvn(namesv, name, namelen);
235 else if (realtype == SVt_PVCV && name[0] == '*') {
236 namesv = newSVpvn("\\", 2);
237 sv_catpvn(namesv, name, namelen);
238 (SvPVX(namesv))[1] = '&';
241 namesv = newSVpvn(name, namelen);
243 av_push(seenentry, namesv);
244 (void)SvREFCNT_inc(val);
245 av_push(seenentry, val);
246 (void)hv_store(seenhv, id, strlen(id),
247 newRV((SV*)seenentry), 0);
248 SvREFCNT_dec(seenentry);
253 ipad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp);
255 if (realpack) { /* we have a blessed ref */
257 char *blessstr = SvPV(bless, blesslen);
258 sv_catpvn(retval, blessstr, blesslen);
259 sv_catpvn(retval, "( ", 2);
262 apad = newSVsv(apad);
263 sv_x(apad, " ", 1, blesslen+2);
267 if (realtype <= SVt_PVBM) { /* scalar ref */
268 SV *namesv = newSVpvn("${", 2);
269 sv_catpvn(namesv, name, namelen);
270 sv_catpvn(namesv, "}", 1);
271 if (realpack) { /* blessed */
272 sv_catpvn(retval, "do{\\(my $o = ", 13);
273 DD_dump(ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
274 postav, levelp, indent, pad, xpad, apad, sep,
275 freezer, toaster, purity, deepcopy, quotekeys, bless);
276 sv_catpvn(retval, ")}", 2);
279 sv_catpvn(retval, "\\", 1);
280 DD_dump(ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
281 postav, levelp, indent, pad, xpad, apad, sep,
282 freezer, toaster, purity, deepcopy, quotekeys, bless);
284 SvREFCNT_dec(namesv);
286 else if (realtype == SVt_PVGV) { /* glob ref */
287 SV *namesv = newSVpvn("*{", 2);
288 sv_catpvn(namesv, name, namelen);
289 sv_catpvn(namesv, "}", 1);
290 sv_catpvn(retval, "\\", 1);
291 DD_dump(ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
292 postav, levelp, indent, pad, xpad, apad, sep,
293 freezer, toaster, purity, deepcopy, quotekeys, bless);
294 SvREFCNT_dec(namesv);
296 else if (realtype == SVt_PVAV) {
299 I32 ixmax = av_len((AV *)ival);
301 SV *ixsv = newSViv(0);
302 /* allowing for a 24 char wide array index */
303 New(0, iname, namelen+28, char);
304 (void)strcpy(iname, name);
306 if (name[0] == '@') {
307 sv_catpvn(retval, "(", 1);
311 sv_catpvn(retval, "[", 1);
312 /* omit "->" in $foo{bar}->[0], but not in ${$foo}->[0] */
314 && name[namelen-1] != ']' && name[namelen-1] != '}'
315 && (namelen < 4 || (name[1] != '{' && name[2] != '{')))*/
317 && name[namelen-1] != ']' && name[namelen-1] != '}')
320 || (name[0] == '\\' && name[2] == '{'))))
322 iname[inamelen++] = '-'; iname[inamelen++] = '>';
323 iname[inamelen] = '\0';
326 if (iname[0] == '*' && iname[inamelen-1] == '}' && inamelen >= 8 &&
327 (instr(iname+inamelen-8, "{SCALAR}") ||
328 instr(iname+inamelen-7, "{ARRAY}") ||
329 instr(iname+inamelen-6, "{HASH}"))) {
330 iname[inamelen++] = '-'; iname[inamelen++] = '>';
332 iname[inamelen++] = '['; iname[inamelen] = '\0';
333 totpad = newSVsv(sep);
334 sv_catsv(totpad, pad);
335 sv_catsv(totpad, apad);
337 for (ix = 0; ix <= ixmax; ++ix) {
340 svp = av_fetch((AV*)ival, ix, FALSE);
348 (void) sprintf(iname+ilen, "%ld", ix);
349 ilen = strlen(iname);
350 iname[ilen++] = ']'; iname[ilen] = '\0';
352 sv_catsv(retval, totpad);
353 sv_catsv(retval, ipad);
354 sv_catpvn(retval, "#", 1);
355 sv_catsv(retval, ixsv);
357 sv_catsv(retval, totpad);
358 sv_catsv(retval, ipad);
359 DD_dump(elem, iname, ilen, retval, seenhv, postav,
360 levelp, indent, pad, xpad, apad, sep,
361 freezer, toaster, purity, deepcopy, quotekeys, bless);
363 sv_catpvn(retval, ",", 1);
366 SV *opad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), (*levelp)-1);
367 sv_catsv(retval, totpad);
368 sv_catsv(retval, opad);
372 sv_catpvn(retval, ")", 1);
374 sv_catpvn(retval, "]", 1);
376 SvREFCNT_dec(totpad);
379 else if (realtype == SVt_PVHV) {
380 SV *totpad, *newapad;
387 iname = newSVpvn(name, namelen);
388 if (name[0] == '%') {
389 sv_catpvn(retval, "(", 1);
390 (SvPVX(iname))[0] = '$';
393 sv_catpvn(retval, "{", 1);
394 /* omit "->" in $foo[0]->{bar}, but not in ${$foo}->{bar} */
396 && name[namelen-1] != ']' && name[namelen-1] != '}')
399 || (name[0] == '\\' && name[2] == '{'))))
401 sv_catpvn(iname, "->", 2);
404 if (name[0] == '*' && name[namelen-1] == '}' && namelen >= 8 &&
405 (instr(name+namelen-8, "{SCALAR}") ||
406 instr(name+namelen-7, "{ARRAY}") ||
407 instr(name+namelen-6, "{HASH}"))) {
408 sv_catpvn(iname, "->", 2);
410 sv_catpvn(iname, "{", 1);
411 totpad = newSVsv(sep);
412 sv_catsv(totpad, pad);
413 sv_catsv(totpad, apad);
415 (void)hv_iterinit((HV*)ival);
417 while ((entry = hv_iternext((HV*)ival))) {
422 sv_catpvn(retval, ",", 1);
424 key = hv_iterkey(entry, &klen);
425 hval = hv_iterval((HV*)ival, entry);
427 if (quotekeys || needs_quote(key)) {
428 nticks = num_q(key, klen);
429 New(0, nkey, klen+nticks+3, char);
432 klen += esc_q(nkey+1, key, klen);
434 (void)Copy(key, nkey+1, klen, char);
439 New(0, nkey, klen, char);
440 (void)Copy(key, nkey, klen, char);
443 sname = newSVsv(iname);
444 sv_catpvn(sname, nkey, klen);
445 sv_catpvn(sname, "}", 1);
447 sv_catsv(retval, totpad);
448 sv_catsv(retval, ipad);
449 sv_catpvn(retval, nkey, klen);
450 sv_catpvn(retval, " => ", 4);
454 newapad = newSVsv(apad);
455 New(0, extra, klen+4+1, char);
456 while (elen < (klen+4))
459 sv_catpvn(newapad, extra, elen);
465 DD_dump(hval, SvPVX(sname), SvCUR(sname), retval, seenhv,
466 postav, levelp, indent, pad, xpad, newapad, sep,
467 freezer, toaster, purity, deepcopy, quotekeys, bless);
471 SvREFCNT_dec(newapad);
474 SV *opad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp-1);
475 sv_catsv(retval, totpad);
476 sv_catsv(retval, opad);
480 sv_catpvn(retval, ")", 1);
482 sv_catpvn(retval, "}", 1);
484 SvREFCNT_dec(totpad);
486 else if (realtype == SVt_PVCV) {
487 sv_catpvn(retval, "sub { \"DUMMY\" }", 15);
489 warn("Encountered CODE ref, using dummy placeholder");
492 warn("cannot handle ref type %ld", realtype);
495 if (realpack) { /* free blessed allocs */
500 sv_catpvn(retval, ", '", 3);
501 sv_catpvn(retval, realpack, strlen(realpack));
502 sv_catpvn(retval, "' )", 3);
503 if (toaster && SvPOK(toaster) && SvCUR(toaster)) {
504 sv_catpvn(retval, "->", 2);
505 sv_catsv(retval, toaster);
506 sv_catpvn(retval, "()", 2);
516 (void) sprintf(id, "0x%lx", (unsigned long)val);
517 if ((svp = hv_fetch(seenhv, id, (idlen = strlen(id)), FALSE)) &&
518 (sv = *svp) && SvROK(sv) &&
519 (seenentry = (AV*)SvRV(sv)))
522 if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)
523 && (svp = av_fetch(seenentry, 2, FALSE)) && *svp && SvIV(*svp) > 0)
525 sv_catpvn(retval, "${", 2);
526 sv_catsv(retval, othername);
527 sv_catpvn(retval, "}", 1);
533 namesv = newSVpvn("\\", 1);
534 sv_catpvn(namesv, name, namelen);
536 av_push(seenentry, namesv);
537 av_push(seenentry, newRV(val));
538 (void)hv_store(seenhv, id, strlen(id), newRV((SV*)seenentry), 0);
539 SvREFCNT_dec(seenentry);
546 (void) sprintf(tmpbuf, "%d", i);
547 len = strlen(tmpbuf);
548 sv_catpvn(retval, tmpbuf, len);
550 else if (realtype == SVt_PVGV) {/* GLOBs can end up with scribbly names */
552 ++c; --i; /* just get the name */
553 if (i >= 6 && strncmp(c, "main::", 6) == 0) {
557 if (needs_quote(c)) {
558 sv_grow(retval, SvCUR(retval)+6+2*i);
559 r = SvPVX(retval)+SvCUR(retval);
560 r[0] = '*'; r[1] = '{'; r[2] = '\'';
561 i += esc_q(r+3, c, i);
563 r[i++] = '\''; r[i++] = '}';
567 sv_grow(retval, SvCUR(retval)+i+2);
568 r = SvPVX(retval)+SvCUR(retval);
569 r[0] = '*'; strcpy(r+1, c);
572 SvCUR_set(retval, SvCUR(retval)+i);
575 static char *entries[] = { "{SCALAR}", "{ARRAY}", "{HASH}" };
576 static STRLEN sizes[] = { 8, 7, 6 };
578 SV *nname = newSVpvn("", 0);
579 SV *newapad = newSVpvn("", 0);
583 for (j=0; j<3; j++) {
584 e = ((j == 0) ? GvSV(gv) : (j == 1) ? (SV*)GvAV(gv) : (SV*)GvHV(gv));
587 if (j == 0 && !SvOK(e))
592 SV *postentry = newSVpvn(r,i);
594 sv_setsv(nname, postentry);
595 sv_catpvn(nname, entries[j], sizes[j]);
596 sv_catpvn(postentry, " = ", 3);
597 av_push(postav, postentry);
602 (void)sv_x(newapad, " ", 1, SvCUR(postentry));
604 DD_dump(e, SvPVX(nname), SvCUR(nname), postentry,
605 seenhv, postav, &nlevel, indent, pad, xpad,
606 newapad, sep, freezer, toaster, purity,
607 deepcopy, quotekeys, bless);
612 SvREFCNT_dec(newapad);
616 else if (val == &PL_sv_undef || !SvOK(val)) {
617 sv_catpvn(retval, "undef", 5);
621 sv_grow(retval, SvCUR(retval)+3+2*i);
622 r = SvPVX(retval)+SvCUR(retval);
624 i += esc_q(r+1, c, i);
628 SvCUR_set(retval, SvCUR(retval)+i);
634 (void)hv_delete(seenhv, id, idlen, G_DISCARD);
635 else if (namelen && seenentry) {
636 SV *mark = *av_fetch(seenentry, 2, TRUE);
644 MODULE = Data::Dumper PACKAGE = Data::Dumper PREFIX = Data_Dumper_
647 # This is the exact equivalent of Dump. Well, almost. The things that are
648 # different as of now (due to Laziness):
649 # * doesnt do double-quotes yet.
653 Data_Dumper_Dumpxs(href, ...)
661 AV *postav, *todumpav, *namesav;
663 I32 indent, terse, useqq, i, imax, postlen;
665 SV *val, *name, *pad, *xpad, *apad, *sep, *tmp, *varname;
666 SV *freezer, *toaster, *bless;
667 I32 purity, deepcopy, quotekeys;
671 if (!SvROK(href)) { /* call new to get an object first */
680 croak("Usage: Data::Dumper::Dumpxs(PACKAGE, VAL_ARY_REF, NAME_ARY_REF)");
687 XPUSHs(sv_2mortal(newSVsv(valarray)));
688 XPUSHs(sv_2mortal(newSVsv(namearray)));
690 i = perl_call_method("new", G_SCALAR);
693 href = newSVsv(POPs);
699 (void)sv_2mortal(href);
702 todumpav = namesav = Nullav;
704 val = pad = xpad = apad = sep = tmp = varname
705 = freezer = toaster = bless = &PL_sv_undef;
706 name = sv_newmortal();
708 terse = useqq = purity = deepcopy = 0;
711 retval = newSVpvn("", 0);
713 && (hv = (HV*)SvRV((SV*)href))
714 && SvTYPE(hv) == SVt_PVHV) {
716 if ((svp = hv_fetch(hv, "seen", 4, FALSE)) && SvROK(*svp))
717 seenhv = (HV*)SvRV(*svp);
718 if ((svp = hv_fetch(hv, "todump", 6, FALSE)) && SvROK(*svp))
719 todumpav = (AV*)SvRV(*svp);
720 if ((svp = hv_fetch(hv, "names", 5, FALSE)) && SvROK(*svp))
721 namesav = (AV*)SvRV(*svp);
722 if ((svp = hv_fetch(hv, "indent", 6, FALSE)))
724 if ((svp = hv_fetch(hv, "purity", 6, FALSE)))
726 if ((svp = hv_fetch(hv, "terse", 5, FALSE)))
727 terse = SvTRUE(*svp);
728 if ((svp = hv_fetch(hv, "useqq", 5, FALSE)))
729 useqq = SvTRUE(*svp);
730 if ((svp = hv_fetch(hv, "pad", 3, FALSE)))
732 if ((svp = hv_fetch(hv, "xpad", 4, FALSE)))
734 if ((svp = hv_fetch(hv, "apad", 4, FALSE)))
736 if ((svp = hv_fetch(hv, "sep", 3, FALSE)))
738 if ((svp = hv_fetch(hv, "varname", 7, FALSE)))
740 if ((svp = hv_fetch(hv, "freezer", 7, FALSE)))
742 if ((svp = hv_fetch(hv, "toaster", 7, FALSE)))
744 if ((svp = hv_fetch(hv, "deepcopy", 8, FALSE)))
745 deepcopy = SvTRUE(*svp);
746 if ((svp = hv_fetch(hv, "quotekeys", 9, FALSE)))
747 quotekeys = SvTRUE(*svp);
748 if ((svp = hv_fetch(hv, "bless", 5, FALSE)))
753 imax = av_len(todumpav);
756 valstr = newSVpvn("",0);
757 for (i = 0; i <= imax; ++i) {
761 if ((svp = av_fetch(todumpav, i, FALSE)))
765 if ((svp = av_fetch(namesav, i, TRUE)))
766 sv_setsv(name, *svp);
771 if ((SvPVX(name))[0] == '*') {
773 switch (SvTYPE(SvRV(val))) {
775 (SvPVX(name))[0] = '@';
778 (SvPVX(name))[0] = '%';
781 (SvPVX(name))[0] = '*';
784 (SvPVX(name))[0] = '$';
789 (SvPVX(name))[0] = '$';
791 else if ((SvPVX(name))[0] != '$')
792 sv_insert(name, 0, 0, "$", 1);
796 sv_setpvn(name, "$", 1);
797 sv_catsv(name, varname);
798 (void) sprintf(tmpbuf, "%ld", i+1);
799 nchars = strlen(tmpbuf);
800 sv_catpvn(name, tmpbuf, nchars);
804 SV *tmpsv = sv_x(Nullsv, " ", 1, SvCUR(name)+3);
805 newapad = newSVsv(apad);
806 sv_catsv(newapad, tmpsv);
812 DD_dump(val, SvPVX(name), SvCUR(name), valstr, seenhv,
813 postav, &level, indent, pad, xpad, newapad, sep,
814 freezer, toaster, purity, deepcopy, quotekeys,
818 SvREFCNT_dec(newapad);
820 postlen = av_len(postav);
821 if (postlen >= 0 || !terse) {
822 sv_insert(valstr, 0, 0, " = ", 3);
823 sv_insert(valstr, 0, 0, SvPVX(name), SvCUR(name));
824 sv_catpvn(valstr, ";", 1);
826 sv_catsv(retval, pad);
827 sv_catsv(retval, valstr);
828 sv_catsv(retval, sep);
831 sv_catsv(retval, pad);
832 for (i = 0; i <= postlen; ++i) {
834 svp = av_fetch(postav, i, FALSE);
835 if (svp && (elem = *svp)) {
836 sv_catsv(retval, elem);
838 sv_catpvn(retval, ";", 1);
839 sv_catsv(retval, sep);
840 sv_catsv(retval, pad);
844 sv_catpvn(retval, ";", 1);
845 sv_catsv(retval, sep);
847 sv_setpvn(valstr, "", 0);
848 if (gimme == G_ARRAY) {
849 XPUSHs(sv_2mortal(retval));
850 if (i < imax) /* not the last time thro ? */
851 retval = newSVpvn("",0);
854 SvREFCNT_dec(postav);
855 SvREFCNT_dec(valstr);
858 croak("Call to new() method failed to return HASH ref");
859 if (gimme == G_SCALAR)
860 XPUSHs(sv_2mortal(retval));