8 static I32 num_q _((char *s));
9 static I32 esc_q _((char *dest, char *src, STRLEN slen));
10 static SV *sv_x _((SV *sv, char *str, STRLEN len, I32 n));
11 static I32 DD_dump _((SV *val, char *name, STRLEN namelen, SV *retval,
12 HV *seenhv, AV *postav, I32 *levelp, I32 indent,
13 SV *pad, SV *xpad, SV *apad, SV *sep,
14 SV *freezer, SV *toaster,
15 I32 purity, I32 deepcopy, I32 quotekeys, SV *bless));
17 /* does a string need to be protected? */
19 needs_quote(register char *s)
43 /* count the number of "'"s and "\"s in string */
45 num_q(register char *s)
50 if (*s == '\'' || *s == '\\')
58 /* returns number of chars added to escape "'"s and "\"s in s */
59 /* slen number of characters in s will be escaped */
60 /* destination must be long enough for additional chars */
62 esc_q(register char *d, register char *s, register STRLEN slen)
81 /* append a repeated string to an SV */
83 sv_x(SV *sv, register char *str, STRLEN len, I32 n)
88 assert(SvTYPE(sv) >= SVt_PV);
91 SvGROW(sv, len*n + SvCUR(sv) + 1);
93 char *start = SvPVX(sv) + SvCUR(sv);
101 sv_catpvn(sv, str, len);
109 * This ought to be split into smaller functions. (it is one long function since
110 * it exactly parallels the perl version, which was one long thing for
111 * efficiency raisins.) Ugggh!
114 DD_dump(SV *val, char *name, STRLEN namelen, SV *retval, HV *seenhv,
115 AV *postav, I32 *levelp, I32 indent, SV *pad, SV *xpad,
116 SV *apad, SV *sep, SV *freezer, SV *toaster, I32 purity,
117 I32 deepcopy, I32 quotekeys, SV *bless)
121 char *c, *r, *realpack, id[128];
124 SV *blesspad = Nullsv;
129 STRLEN inamelen, idlen = 0;
136 flags = SvFLAGS(val);
137 realtype = SvTYPE(val);
141 if (val == &PL_sv_undef || !SvOK(val)) {
142 sv_catpvn(retval, "undef", 5);
147 if (SvOBJECT(SvRV(val)) && freezer &&
148 SvPOK(freezer) && SvCUR(freezer))
150 dSP; ENTER; SAVETMPS; PUSHMARK(sp);
151 XPUSHs(val); PUTBACK;
152 i = perl_call_method(SvPVX(freezer), G_EVAL|G_SCALAR);
154 if (SvTRUE(GvSV(PL_errgv)))
155 warn("WARNING(Freezer method call failed): %s",
156 SvPVX(GvSV(PL_errgv)));
159 PUTBACK; FREETMPS; LEAVE;
161 (void)sv_2mortal(val);
165 flags = SvFLAGS(ival);
166 realtype = SvTYPE(ival);
167 (void) sprintf(id, "0x%lx", (unsigned long)ival);
170 realpack = HvNAME(SvSTASH(ival));
173 if ((svp = hv_fetch(seenhv, id, idlen, FALSE)) &&
174 (sv = *svp) && SvROK(sv) &&
175 (seenentry = (AV*)SvRV(sv))) {
177 if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)) {
178 if (purity && *levelp > 0) {
181 if (realtype == SVt_PVHV)
182 sv_catpvn(retval, "{}", 2);
183 else if (realtype == SVt_PVAV)
184 sv_catpvn(retval, "[]", 2);
186 sv_catpvn(retval, "''", 2);
187 postentry = newSVpv(name, namelen);
188 sv_catpvn(postentry, " = ", 3);
189 sv_catsv(postentry, othername);
190 av_push(postav, postentry);
193 if (name[0] == '@' || name[0] == '%') {
194 if ((SvPVX(othername))[0] == '\\' &&
195 (SvPVX(othername))[1] == name[0]) {
196 sv_catpvn(retval, SvPVX(othername)+1, SvCUR(othername)-1);
199 sv_catpvn(retval, name, 1);
200 sv_catpvn(retval, "{", 1);
201 sv_catsv(retval, othername);
202 sv_catpvn(retval, "}", 1);
206 sv_catsv(retval, othername);
211 warn("ref name not found for %s", id);
215 else { /* store our name and continue */
217 if (name[0] == '@' || name[0] == '%') {
218 namesv = newSVpv("\\", 1);
219 sv_catpvn(namesv, name, namelen);
221 else if (realtype == SVt_PVCV && name[0] == '*') {
222 namesv = newSVpv("\\", 2);
223 sv_catpvn(namesv, name, namelen);
224 (SvPVX(namesv))[1] = '&';
227 namesv = newSVpv(name, namelen);
229 av_push(seenentry, namesv);
230 (void)SvREFCNT_inc(val);
231 av_push(seenentry, val);
232 (void)hv_store(seenhv, id, strlen(id), newRV((SV*)seenentry), 0);
233 SvREFCNT_dec(seenentry);
237 ipad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp);
239 if (realpack) { /* we have a blessed ref */
241 char *blessstr = SvPV(bless, blesslen);
242 sv_catpvn(retval, blessstr, blesslen);
243 sv_catpvn(retval, "( ", 2);
246 apad = newSVsv(apad);
247 sv_x(apad, " ", 1, blesslen+2);
251 if (realtype <= SVt_PVBM || realtype == SVt_PVGV) { /* scalars */
252 if (realpack && realtype != SVt_PVGV) { /* blessed */
253 sv_catpvn(retval, "do{\\(my $o = ", 13);
254 DD_dump(ival, "", 0, retval, seenhv, postav,
255 levelp, indent, pad, xpad, apad, sep,
256 freezer, toaster, purity, deepcopy, quotekeys, bless);
257 sv_catpvn(retval, ")}", 2);
260 sv_catpvn(retval, "\\", 1);
261 DD_dump(ival, "", 0, retval, seenhv, postav,
262 levelp, indent, pad, xpad, apad, sep,
263 freezer, toaster, purity, deepcopy, quotekeys, bless);
266 else if (realtype == SVt_PVAV) {
269 I32 ixmax = av_len((AV *)ival);
271 SV *ixsv = newSViv(0);
272 /* allowing for a 24 char wide array index */
273 New(0, iname, namelen+28, char);
274 (void)strcpy(iname, name);
276 if (name[0] == '@') {
277 sv_catpvn(retval, "(", 1);
281 sv_catpvn(retval, "[", 1);
282 if (namelen > 0 && name[namelen-1] != ']' && name[namelen-1] != '}') {
283 iname[inamelen++] = '-'; iname[inamelen++] = '>';
284 iname[inamelen] = '\0';
287 if (iname[0] == '*' && iname[inamelen-1] == '}' && inamelen >= 8 &&
288 (instr(iname+inamelen-8, "{SCALAR}") ||
289 instr(iname+inamelen-7, "{ARRAY}") ||
290 instr(iname+inamelen-6, "{HASH}"))) {
291 iname[inamelen++] = '-'; iname[inamelen++] = '>';
293 iname[inamelen++] = '['; iname[inamelen] = '\0';
294 totpad = newSVsv(sep);
295 sv_catsv(totpad, pad);
296 sv_catsv(totpad, apad);
298 for (ix = 0; ix <= ixmax; ++ix) {
301 svp = av_fetch((AV*)ival, ix, FALSE);
309 (void) sprintf(iname+ilen, "%ld", ix);
310 ilen = strlen(iname);
311 iname[ilen++] = ']'; iname[ilen] = '\0';
313 sv_catsv(retval, totpad);
314 sv_catsv(retval, ipad);
315 sv_catpvn(retval, "#", 1);
316 sv_catsv(retval, ixsv);
318 sv_catsv(retval, totpad);
319 sv_catsv(retval, ipad);
320 DD_dump(elem, iname, ilen, retval, seenhv, postav,
321 levelp, indent, pad, xpad, apad, sep,
322 freezer, toaster, purity, deepcopy, quotekeys, bless);
324 sv_catpvn(retval, ",", 1);
327 SV *opad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), (*levelp)-1);
328 sv_catsv(retval, totpad);
329 sv_catsv(retval, opad);
333 sv_catpvn(retval, ")", 1);
335 sv_catpvn(retval, "]", 1);
337 SvREFCNT_dec(totpad);
340 else if (realtype == SVt_PVHV) {
341 SV *totpad, *newapad;
348 iname = newSVpv(name, namelen);
349 if (name[0] == '%') {
350 sv_catpvn(retval, "(", 1);
351 (SvPVX(iname))[0] = '$';
354 sv_catpvn(retval, "{", 1);
355 if (namelen > 0 && name[namelen-1] != ']' && name[namelen-1] != '}') {
356 sv_catpvn(iname, "->", 2);
359 if (name[0] == '*' && name[namelen-1] == '}' && namelen >= 8 &&
360 (instr(name+namelen-8, "{SCALAR}") ||
361 instr(name+namelen-7, "{ARRAY}") ||
362 instr(name+namelen-6, "{HASH}"))) {
363 sv_catpvn(iname, "->", 2);
365 sv_catpvn(iname, "{", 1);
366 totpad = newSVsv(sep);
367 sv_catsv(totpad, pad);
368 sv_catsv(totpad, apad);
370 (void)hv_iterinit((HV*)ival);
372 while ((entry = hv_iternext((HV*)ival))) {
377 sv_catpvn(retval, ",", 1);
379 key = hv_iterkey(entry, &klen);
380 hval = hv_iterval((HV*)ival, entry);
382 if (quotekeys || needs_quote(key)) {
384 New(0, nkey, klen+nticks+3, char);
387 klen += esc_q(nkey+1, key, klen);
389 (void)Copy(key, nkey+1, klen, char);
394 New(0, nkey, klen, char);
395 (void)Copy(key, nkey, klen, char);
398 sname = newSVsv(iname);
399 sv_catpvn(sname, nkey, klen);
400 sv_catpvn(sname, "}", 1);
402 sv_catsv(retval, totpad);
403 sv_catsv(retval, ipad);
404 sv_catpvn(retval, nkey, klen);
405 sv_catpvn(retval, " => ", 4);
409 newapad = newSVsv(apad);
410 New(0, extra, klen+4+1, char);
411 while (elen < (klen+4))
414 sv_catpvn(newapad, extra, elen);
420 DD_dump(hval, SvPVX(sname), SvCUR(sname), retval, seenhv,
421 postav, levelp, indent, pad, xpad, newapad, sep,
422 freezer, toaster, purity, deepcopy, quotekeys, bless);
426 SvREFCNT_dec(newapad);
429 SV *opad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp-1);
430 sv_catsv(retval, totpad);
431 sv_catsv(retval, opad);
435 sv_catpvn(retval, ")", 1);
437 sv_catpvn(retval, "}", 1);
439 SvREFCNT_dec(totpad);
441 else if (realtype == SVt_PVCV) {
442 sv_catpvn(retval, "sub { \"DUMMY\" }", 15);
444 warn("Encountered CODE ref, using dummy placeholder");
447 warn("cannot handle ref type %ld", realtype);
450 if (realpack) { /* free blessed allocs */
455 sv_catpvn(retval, ", '", 3);
456 sv_catpvn(retval, realpack, strlen(realpack));
457 sv_catpvn(retval, "' )", 3);
458 if (toaster && SvPOK(toaster) && SvCUR(toaster)) {
459 sv_catpvn(retval, "->", 2);
460 sv_catsv(retval, toaster);
461 sv_catpvn(retval, "()", 2);
471 (void) sprintf(id, "0x%lx", (unsigned long)val);
472 if ((svp = hv_fetch(seenhv, id, (idlen = strlen(id)), FALSE)) &&
473 (sv = *svp) && SvROK(sv) &&
474 (seenentry = (AV*)SvRV(sv))) {
476 if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)) {
477 sv_catsv(retval, othername);
483 namesv = newSVpv("\\", 1);
484 sv_catpvn(namesv, name, namelen);
486 av_push(seenentry, namesv);
487 (void)SvREFCNT_inc(val);
488 av_push(seenentry, val);
489 (void)hv_store(seenhv, id, strlen(id), newRV((SV*)seenentry), 0);
490 SvREFCNT_dec(seenentry);
497 (void) sprintf(tmpbuf, "%d", i);
498 len = strlen(tmpbuf);
499 sv_catpvn(retval, tmpbuf, len);
502 else if (realtype == SVt_PVGV) {/* GLOBs can end up with scribbly names */
504 ++c; --i; /* just get the name */
505 if (i >= 6 && strncmp(c, "main::", 6) == 0) {
509 if (needs_quote(c)) {
510 sv_grow(retval, SvCUR(retval)+6+2*i);
511 r = SvPVX(retval)+SvCUR(retval);
512 r[0] = '*'; r[1] = '{'; r[2] = '\'';
513 i += esc_q(r+3, c, i);
515 r[i++] = '\''; r[i++] = '}';
519 sv_grow(retval, SvCUR(retval)+i+2);
520 r = SvPVX(retval)+SvCUR(retval);
521 r[0] = '*'; strcpy(r+1, c);
526 static char *entries[] = { "{SCALAR}", "{ARRAY}", "{HASH}" };
527 static STRLEN sizes[] = { 8, 7, 6 };
529 SV *nname = newSVpv("", 0);
530 SV *newapad = newSVpv("", 0);
534 for (j=0; j<3; j++) {
535 e = ((j == 0) ? GvSV(gv) : (j == 1) ? (SV*)GvAV(gv) : (SV*)GvHV(gv));
538 SV *postentry = newSVpv(r,i);
540 sv_setsv(nname, postentry);
541 sv_catpvn(nname, entries[j], sizes[j]);
542 sv_catpvn(postentry, " = ", 3);
543 av_push(postav, postentry);
548 (void)sv_x(newapad, " ", 1, SvCUR(postentry));
550 DD_dump(e, SvPVX(nname), SvCUR(nname), postentry,
551 seenhv, postav, &nlevel, indent, pad, xpad,
552 newapad, sep, freezer, toaster, purity,
553 deepcopy, quotekeys, bless);
558 SvREFCNT_dec(newapad);
564 sv_grow(retval, SvCUR(retval)+3+2*i);
565 r = SvPVX(retval)+SvCUR(retval);
567 i += esc_q(r+1, c, i);
572 SvCUR_set(retval, SvCUR(retval)+i);
575 if (deepcopy && idlen)
576 (void)hv_delete(seenhv, id, idlen, G_DISCARD);
582 MODULE = Data::Dumper PACKAGE = Data::Dumper PREFIX = Data_Dumper_
585 # This is the exact equivalent of Dump. Well, almost. The things that are
586 # different as of now (due to Laziness):
587 # * doesnt do double-quotes yet.
591 Data_Dumper_Dumpxs(href, ...)
599 AV *postav, *todumpav, *namesav;
601 I32 indent, terse, useqq, i, imax, postlen;
603 SV *val, *name, *pad, *xpad, *apad, *sep, *tmp, *varname;
604 SV *freezer, *toaster, *bless;
605 I32 purity, deepcopy, quotekeys;
609 if (!SvROK(href)) { /* call new to get an object first */
618 croak("Usage: Data::Dumper::Dumpxs(PACKAGE, VAL_ARY_REF, NAME_ARY_REF)");
625 XPUSHs(sv_2mortal(newSVsv(valarray)));
626 XPUSHs(sv_2mortal(newSVsv(namearray)));
628 i = perl_call_method("new", G_SCALAR);
631 href = newSVsv(POPs);
637 (void)sv_2mortal(href);
640 todumpav = namesav = Nullav;
642 val = pad = xpad = apad = sep = tmp = varname
643 = freezer = toaster = bless = &PL_sv_undef;
644 name = sv_newmortal();
646 terse = useqq = purity = deepcopy = 0;
649 retval = newSVpv("", 0);
651 && (hv = (HV*)SvRV((SV*)href))
652 && SvTYPE(hv) == SVt_PVHV) {
654 if ((svp = hv_fetch(hv, "seen", 4, FALSE)) && SvROK(*svp))
655 seenhv = (HV*)SvRV(*svp);
656 if ((svp = hv_fetch(hv, "todump", 6, FALSE)) && SvROK(*svp))
657 todumpav = (AV*)SvRV(*svp);
658 if ((svp = hv_fetch(hv, "names", 5, FALSE)) && SvROK(*svp))
659 namesav = (AV*)SvRV(*svp);
660 if ((svp = hv_fetch(hv, "indent", 6, FALSE)))
662 if ((svp = hv_fetch(hv, "purity", 6, FALSE)))
664 if ((svp = hv_fetch(hv, "terse", 5, FALSE)))
665 terse = SvTRUE(*svp);
666 if ((svp = hv_fetch(hv, "useqq", 5, FALSE)))
667 useqq = SvTRUE(*svp);
668 if ((svp = hv_fetch(hv, "pad", 3, FALSE)))
670 if ((svp = hv_fetch(hv, "xpad", 4, FALSE)))
672 if ((svp = hv_fetch(hv, "apad", 4, FALSE)))
674 if ((svp = hv_fetch(hv, "sep", 3, FALSE)))
676 if ((svp = hv_fetch(hv, "varname", 7, FALSE)))
678 if ((svp = hv_fetch(hv, "freezer", 7, FALSE)))
680 if ((svp = hv_fetch(hv, "toaster", 7, FALSE)))
682 if ((svp = hv_fetch(hv, "deepcopy", 8, FALSE)))
683 deepcopy = SvTRUE(*svp);
684 if ((svp = hv_fetch(hv, "quotekeys", 9, FALSE)))
685 quotekeys = SvTRUE(*svp);
686 if ((svp = hv_fetch(hv, "bless", 5, FALSE)))
691 imax = av_len(todumpav);
694 valstr = newSVpv("",0);
695 for (i = 0; i <= imax; ++i) {
699 if ((svp = av_fetch(todumpav, i, FALSE)))
703 if ((svp = av_fetch(namesav, i, TRUE)))
704 sv_setsv(name, *svp);
709 if ((SvPVX(name))[0] == '*') {
711 switch (SvTYPE(SvRV(val))) {
713 (SvPVX(name))[0] = '@';
716 (SvPVX(name))[0] = '%';
719 (SvPVX(name))[0] = '*';
722 (SvPVX(name))[0] = '$';
727 (SvPVX(name))[0] = '$';
729 else if ((SvPVX(name))[0] != '$')
730 sv_insert(name, 0, 0, "$", 1);
734 sv_setpvn(name, "$", 1);
735 sv_catsv(name, varname);
736 (void) sprintf(tmpbuf, "%ld", i+1);
737 nchars = strlen(tmpbuf);
738 sv_catpvn(name, tmpbuf, nchars);
742 SV *tmpsv = sv_x(Nullsv, " ", 1, SvCUR(name)+3);
743 newapad = newSVsv(apad);
744 sv_catsv(newapad, tmpsv);
750 DD_dump(val, SvPVX(name), SvCUR(name), valstr, seenhv,
751 postav, &level, indent, pad, xpad, newapad, sep,
752 freezer, toaster, purity, deepcopy, quotekeys,
756 SvREFCNT_dec(newapad);
758 postlen = av_len(postav);
759 if (postlen >= 0 || !terse) {
760 sv_insert(valstr, 0, 0, " = ", 3);
761 sv_insert(valstr, 0, 0, SvPVX(name), SvCUR(name));
762 sv_catpvn(valstr, ";", 1);
764 sv_catsv(retval, pad);
765 sv_catsv(retval, valstr);
766 sv_catsv(retval, sep);
769 sv_catsv(retval, pad);
770 for (i = 0; i <= postlen; ++i) {
772 svp = av_fetch(postav, i, FALSE);
773 if (svp && (elem = *svp)) {
774 sv_catsv(retval, elem);
776 sv_catpvn(retval, ";", 1);
777 sv_catsv(retval, sep);
778 sv_catsv(retval, pad);
782 sv_catpvn(retval, ";", 1);
783 sv_catsv(retval, sep);
785 sv_setpvn(valstr, "", 0);
786 if (gimme == G_ARRAY) {
787 XPUSHs(sv_2mortal(retval));
788 if (i < imax) /* not the last time thro ? */
789 retval = newSVpv("",0);
792 SvREFCNT_dec(postav);
793 SvREFCNT_dec(valstr);
796 croak("Call to new() method failed to return HASH ref");
797 if (gimme == G_SCALAR)
798 XPUSHs(sv_2mortal(retval));