8 static I32 num_q _((char *s, STRLEN slen));
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, register STRLEN slen)
50 if (*s == '\'' || *s == '\\')
59 /* returns number of chars added to escape "'"s and "\"s in s */
60 /* slen number of characters in s will be escaped */
61 /* destination must be long enough for additional chars */
63 esc_q(register char *d, register char *s, register STRLEN slen)
82 /* append a repeated string to an SV */
84 sv_x(SV *sv, register char *str, STRLEN len, I32 n)
89 assert(SvTYPE(sv) >= SVt_PV);
92 SvGROW(sv, len*n + SvCUR(sv) + 1);
94 char *start = SvPVX(sv) + SvCUR(sv);
102 sv_catpvn(sv, str, len);
110 * This ought to be split into smaller functions. (it is one long function since
111 * it exactly parallels the perl version, which was one long thing for
112 * efficiency raisins.) Ugggh!
115 DD_dump(SV *val, char *name, STRLEN namelen, SV *retval, HV *seenhv,
116 AV *postav, I32 *levelp, I32 indent, SV *pad, SV *xpad,
117 SV *apad, SV *sep, SV *freezer, SV *toaster, I32 purity,
118 I32 deepcopy, I32 quotekeys, SV *bless)
122 char *c, *r, *realpack, id[128];
125 SV *blesspad = Nullsv;
130 STRLEN inamelen, idlen = 0;
137 flags = SvFLAGS(val);
138 realtype = SvTYPE(val);
142 if (val == &PL_sv_undef || !SvOK(val)) {
143 sv_catpvn(retval, "undef", 5);
148 if (SvOBJECT(SvRV(val)) && freezer &&
149 SvPOK(freezer) && SvCUR(freezer))
151 dSP; ENTER; SAVETMPS; PUSHMARK(sp);
152 XPUSHs(val); PUTBACK;
153 i = perl_call_method(SvPVX(freezer), G_EVAL|G_SCALAR);
155 if (SvTRUE(GvSV(PL_errgv)))
156 warn("WARNING(Freezer method call failed): %s",
157 SvPVX(GvSV(PL_errgv)));
160 PUTBACK; FREETMPS; LEAVE;
162 (void)sv_2mortal(val);
166 flags = SvFLAGS(ival);
167 realtype = SvTYPE(ival);
168 (void) sprintf(id, "0x%lx", (unsigned long)ival);
171 realpack = HvNAME(SvSTASH(ival));
174 if ((svp = hv_fetch(seenhv, id, idlen, FALSE)) &&
175 (sv = *svp) && SvROK(sv) &&
176 (seenentry = (AV*)SvRV(sv))) {
178 if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)) {
179 if (purity && *levelp > 0) {
182 if (realtype == SVt_PVHV)
183 sv_catpvn(retval, "{}", 2);
184 else if (realtype == SVt_PVAV)
185 sv_catpvn(retval, "[]", 2);
187 sv_catpvn(retval, "''", 2);
188 postentry = newSVpv(name, namelen);
189 sv_catpvn(postentry, " = ", 3);
190 sv_catsv(postentry, othername);
191 av_push(postav, postentry);
194 if (name[0] == '@' || name[0] == '%') {
195 if ((SvPVX(othername))[0] == '\\' &&
196 (SvPVX(othername))[1] == name[0]) {
197 sv_catpvn(retval, SvPVX(othername)+1, SvCUR(othername)-1);
200 sv_catpvn(retval, name, 1);
201 sv_catpvn(retval, "{", 1);
202 sv_catsv(retval, othername);
203 sv_catpvn(retval, "}", 1);
207 sv_catsv(retval, othername);
212 warn("ref name not found for %s", id);
216 else { /* store our name and continue */
218 if (name[0] == '@' || name[0] == '%') {
219 namesv = newSVpv("\\", 1);
220 sv_catpvn(namesv, name, namelen);
222 else if (realtype == SVt_PVCV && name[0] == '*') {
223 namesv = newSVpv("\\", 2);
224 sv_catpvn(namesv, name, namelen);
225 (SvPVX(namesv))[1] = '&';
228 namesv = newSVpv(name, namelen);
230 av_push(seenentry, namesv);
231 (void)SvREFCNT_inc(val);
232 av_push(seenentry, val);
233 (void)hv_store(seenhv, id, strlen(id), newRV((SV*)seenentry), 0);
234 SvREFCNT_dec(seenentry);
238 ipad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp);
240 if (realpack) { /* we have a blessed ref */
242 char *blessstr = SvPV(bless, blesslen);
243 sv_catpvn(retval, blessstr, blesslen);
244 sv_catpvn(retval, "( ", 2);
247 apad = newSVsv(apad);
248 sv_x(apad, " ", 1, blesslen+2);
252 if (realtype <= SVt_PVBM || realtype == SVt_PVGV) { /* scalars */
253 if (realpack && realtype != SVt_PVGV) { /* blessed */
254 sv_catpvn(retval, "do{\\(my $o = ", 13);
255 DD_dump(ival, "", 0, retval, seenhv, postav,
256 levelp, indent, pad, xpad, apad, sep,
257 freezer, toaster, purity, deepcopy, quotekeys, bless);
258 sv_catpvn(retval, ")}", 2);
261 sv_catpvn(retval, "\\", 1);
262 DD_dump(ival, "", 0, retval, seenhv, postav,
263 levelp, indent, pad, xpad, apad, sep,
264 freezer, toaster, purity, deepcopy, quotekeys, bless);
267 else if (realtype == SVt_PVAV) {
270 I32 ixmax = av_len((AV *)ival);
272 SV *ixsv = newSViv(0);
273 /* allowing for a 24 char wide array index */
274 New(0, iname, namelen+28, char);
275 (void)strcpy(iname, name);
277 if (name[0] == '@') {
278 sv_catpvn(retval, "(", 1);
282 sv_catpvn(retval, "[", 1);
283 if (namelen > 0 && name[namelen-1] != ']' && name[namelen-1] != '}') {
284 iname[inamelen++] = '-'; iname[inamelen++] = '>';
285 iname[inamelen] = '\0';
288 if (iname[0] == '*' && iname[inamelen-1] == '}' && inamelen >= 8 &&
289 (instr(iname+inamelen-8, "{SCALAR}") ||
290 instr(iname+inamelen-7, "{ARRAY}") ||
291 instr(iname+inamelen-6, "{HASH}"))) {
292 iname[inamelen++] = '-'; iname[inamelen++] = '>';
294 iname[inamelen++] = '['; iname[inamelen] = '\0';
295 totpad = newSVsv(sep);
296 sv_catsv(totpad, pad);
297 sv_catsv(totpad, apad);
299 for (ix = 0; ix <= ixmax; ++ix) {
302 svp = av_fetch((AV*)ival, ix, FALSE);
310 (void) sprintf(iname+ilen, "%ld", ix);
311 ilen = strlen(iname);
312 iname[ilen++] = ']'; iname[ilen] = '\0';
314 sv_catsv(retval, totpad);
315 sv_catsv(retval, ipad);
316 sv_catpvn(retval, "#", 1);
317 sv_catsv(retval, ixsv);
319 sv_catsv(retval, totpad);
320 sv_catsv(retval, ipad);
321 DD_dump(elem, iname, ilen, retval, seenhv, postav,
322 levelp, indent, pad, xpad, apad, sep,
323 freezer, toaster, purity, deepcopy, quotekeys, bless);
325 sv_catpvn(retval, ",", 1);
328 SV *opad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), (*levelp)-1);
329 sv_catsv(retval, totpad);
330 sv_catsv(retval, opad);
334 sv_catpvn(retval, ")", 1);
336 sv_catpvn(retval, "]", 1);
338 SvREFCNT_dec(totpad);
341 else if (realtype == SVt_PVHV) {
342 SV *totpad, *newapad;
349 iname = newSVpv(name, namelen);
350 if (name[0] == '%') {
351 sv_catpvn(retval, "(", 1);
352 (SvPVX(iname))[0] = '$';
355 sv_catpvn(retval, "{", 1);
356 if (namelen > 0 && name[namelen-1] != ']' && name[namelen-1] != '}') {
357 sv_catpvn(iname, "->", 2);
360 if (name[0] == '*' && name[namelen-1] == '}' && namelen >= 8 &&
361 (instr(name+namelen-8, "{SCALAR}") ||
362 instr(name+namelen-7, "{ARRAY}") ||
363 instr(name+namelen-6, "{HASH}"))) {
364 sv_catpvn(iname, "->", 2);
366 sv_catpvn(iname, "{", 1);
367 totpad = newSVsv(sep);
368 sv_catsv(totpad, pad);
369 sv_catsv(totpad, apad);
371 (void)hv_iterinit((HV*)ival);
373 while ((entry = hv_iternext((HV*)ival))) {
378 sv_catpvn(retval, ",", 1);
380 key = hv_iterkey(entry, &klen);
381 hval = hv_iterval((HV*)ival, entry);
383 if (quotekeys || needs_quote(key)) {
384 nticks = num_q(key, klen);
385 New(0, nkey, klen+nticks+3, char);
388 klen += esc_q(nkey+1, key, klen);
390 (void)Copy(key, nkey+1, klen, char);
395 New(0, nkey, klen, char);
396 (void)Copy(key, nkey, klen, char);
399 sname = newSVsv(iname);
400 sv_catpvn(sname, nkey, klen);
401 sv_catpvn(sname, "}", 1);
403 sv_catsv(retval, totpad);
404 sv_catsv(retval, ipad);
405 sv_catpvn(retval, nkey, klen);
406 sv_catpvn(retval, " => ", 4);
410 newapad = newSVsv(apad);
411 New(0, extra, klen+4+1, char);
412 while (elen < (klen+4))
415 sv_catpvn(newapad, extra, elen);
421 DD_dump(hval, SvPVX(sname), SvCUR(sname), retval, seenhv,
422 postav, levelp, indent, pad, xpad, newapad, sep,
423 freezer, toaster, purity, deepcopy, quotekeys, bless);
427 SvREFCNT_dec(newapad);
430 SV *opad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp-1);
431 sv_catsv(retval, totpad);
432 sv_catsv(retval, opad);
436 sv_catpvn(retval, ")", 1);
438 sv_catpvn(retval, "}", 1);
440 SvREFCNT_dec(totpad);
442 else if (realtype == SVt_PVCV) {
443 sv_catpvn(retval, "sub { \"DUMMY\" }", 15);
445 warn("Encountered CODE ref, using dummy placeholder");
448 warn("cannot handle ref type %ld", realtype);
451 if (realpack) { /* free blessed allocs */
456 sv_catpvn(retval, ", '", 3);
457 sv_catpvn(retval, realpack, strlen(realpack));
458 sv_catpvn(retval, "' )", 3);
459 if (toaster && SvPOK(toaster) && SvCUR(toaster)) {
460 sv_catpvn(retval, "->", 2);
461 sv_catsv(retval, toaster);
462 sv_catpvn(retval, "()", 2);
472 (void) sprintf(id, "0x%lx", (unsigned long)val);
473 if ((svp = hv_fetch(seenhv, id, (idlen = strlen(id)), FALSE)) &&
474 (sv = *svp) && SvROK(sv) &&
475 (seenentry = (AV*)SvRV(sv))) {
477 if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)) {
478 sv_catsv(retval, othername);
484 namesv = newSVpv("\\", 1);
485 sv_catpvn(namesv, name, namelen);
487 av_push(seenentry, namesv);
488 (void)SvREFCNT_inc(val);
489 av_push(seenentry, val);
490 (void)hv_store(seenhv, id, strlen(id), newRV((SV*)seenentry), 0);
491 SvREFCNT_dec(seenentry);
498 (void) sprintf(tmpbuf, "%d", i);
499 len = strlen(tmpbuf);
500 sv_catpvn(retval, tmpbuf, len);
503 else if (realtype == SVt_PVGV) {/* GLOBs can end up with scribbly names */
505 ++c; --i; /* just get the name */
506 if (i >= 6 && strncmp(c, "main::", 6) == 0) {
510 if (needs_quote(c)) {
511 sv_grow(retval, SvCUR(retval)+6+2*i);
512 r = SvPVX(retval)+SvCUR(retval);
513 r[0] = '*'; r[1] = '{'; r[2] = '\'';
514 i += esc_q(r+3, c, i);
516 r[i++] = '\''; r[i++] = '}';
520 sv_grow(retval, SvCUR(retval)+i+2);
521 r = SvPVX(retval)+SvCUR(retval);
522 r[0] = '*'; strcpy(r+1, c);
527 static char *entries[] = { "{SCALAR}", "{ARRAY}", "{HASH}" };
528 static STRLEN sizes[] = { 8, 7, 6 };
530 SV *nname = newSVpv("", 0);
531 SV *newapad = newSVpv("", 0);
535 for (j=0; j<3; j++) {
536 e = ((j == 0) ? GvSV(gv) : (j == 1) ? (SV*)GvAV(gv) : (SV*)GvHV(gv));
539 SV *postentry = newSVpv(r,i);
541 sv_setsv(nname, postentry);
542 sv_catpvn(nname, entries[j], sizes[j]);
543 sv_catpvn(postentry, " = ", 3);
544 av_push(postav, postentry);
549 (void)sv_x(newapad, " ", 1, SvCUR(postentry));
551 DD_dump(e, SvPVX(nname), SvCUR(nname), postentry,
552 seenhv, postav, &nlevel, indent, pad, xpad,
553 newapad, sep, freezer, toaster, purity,
554 deepcopy, quotekeys, bless);
559 SvREFCNT_dec(newapad);
565 sv_grow(retval, SvCUR(retval)+3+2*i);
566 r = SvPVX(retval)+SvCUR(retval);
568 i += esc_q(r+1, c, i);
573 SvCUR_set(retval, SvCUR(retval)+i);
576 if (deepcopy && idlen)
577 (void)hv_delete(seenhv, id, idlen, G_DISCARD);
583 MODULE = Data::Dumper PACKAGE = Data::Dumper PREFIX = Data_Dumper_
586 # This is the exact equivalent of Dump. Well, almost. The things that are
587 # different as of now (due to Laziness):
588 # * doesnt do double-quotes yet.
592 Data_Dumper_Dumpxs(href, ...)
600 AV *postav, *todumpav, *namesav;
602 I32 indent, terse, useqq, i, imax, postlen;
604 SV *val, *name, *pad, *xpad, *apad, *sep, *tmp, *varname;
605 SV *freezer, *toaster, *bless;
606 I32 purity, deepcopy, quotekeys;
610 if (!SvROK(href)) { /* call new to get an object first */
619 croak("Usage: Data::Dumper::Dumpxs(PACKAGE, VAL_ARY_REF, NAME_ARY_REF)");
626 XPUSHs(sv_2mortal(newSVsv(valarray)));
627 XPUSHs(sv_2mortal(newSVsv(namearray)));
629 i = perl_call_method("new", G_SCALAR);
632 href = newSVsv(POPs);
638 (void)sv_2mortal(href);
641 todumpav = namesav = Nullav;
643 val = pad = xpad = apad = sep = tmp = varname
644 = freezer = toaster = bless = &PL_sv_undef;
645 name = sv_newmortal();
647 terse = useqq = purity = deepcopy = 0;
650 retval = newSVpv("", 0);
652 && (hv = (HV*)SvRV((SV*)href))
653 && SvTYPE(hv) == SVt_PVHV) {
655 if ((svp = hv_fetch(hv, "seen", 4, FALSE)) && SvROK(*svp))
656 seenhv = (HV*)SvRV(*svp);
657 if ((svp = hv_fetch(hv, "todump", 6, FALSE)) && SvROK(*svp))
658 todumpav = (AV*)SvRV(*svp);
659 if ((svp = hv_fetch(hv, "names", 5, FALSE)) && SvROK(*svp))
660 namesav = (AV*)SvRV(*svp);
661 if ((svp = hv_fetch(hv, "indent", 6, FALSE)))
663 if ((svp = hv_fetch(hv, "purity", 6, FALSE)))
665 if ((svp = hv_fetch(hv, "terse", 5, FALSE)))
666 terse = SvTRUE(*svp);
667 if ((svp = hv_fetch(hv, "useqq", 5, FALSE)))
668 useqq = SvTRUE(*svp);
669 if ((svp = hv_fetch(hv, "pad", 3, FALSE)))
671 if ((svp = hv_fetch(hv, "xpad", 4, FALSE)))
673 if ((svp = hv_fetch(hv, "apad", 4, FALSE)))
675 if ((svp = hv_fetch(hv, "sep", 3, FALSE)))
677 if ((svp = hv_fetch(hv, "varname", 7, FALSE)))
679 if ((svp = hv_fetch(hv, "freezer", 7, FALSE)))
681 if ((svp = hv_fetch(hv, "toaster", 7, FALSE)))
683 if ((svp = hv_fetch(hv, "deepcopy", 8, FALSE)))
684 deepcopy = SvTRUE(*svp);
685 if ((svp = hv_fetch(hv, "quotekeys", 9, FALSE)))
686 quotekeys = SvTRUE(*svp);
687 if ((svp = hv_fetch(hv, "bless", 5, FALSE)))
692 imax = av_len(todumpav);
695 valstr = newSVpv("",0);
696 for (i = 0; i <= imax; ++i) {
700 if ((svp = av_fetch(todumpav, i, FALSE)))
704 if ((svp = av_fetch(namesav, i, TRUE)))
705 sv_setsv(name, *svp);
710 if ((SvPVX(name))[0] == '*') {
712 switch (SvTYPE(SvRV(val))) {
714 (SvPVX(name))[0] = '@';
717 (SvPVX(name))[0] = '%';
720 (SvPVX(name))[0] = '*';
723 (SvPVX(name))[0] = '$';
728 (SvPVX(name))[0] = '$';
730 else if ((SvPVX(name))[0] != '$')
731 sv_insert(name, 0, 0, "$", 1);
735 sv_setpvn(name, "$", 1);
736 sv_catsv(name, varname);
737 (void) sprintf(tmpbuf, "%ld", i+1);
738 nchars = strlen(tmpbuf);
739 sv_catpvn(name, tmpbuf, nchars);
743 SV *tmpsv = sv_x(Nullsv, " ", 1, SvCUR(name)+3);
744 newapad = newSVsv(apad);
745 sv_catsv(newapad, tmpsv);
751 DD_dump(val, SvPVX(name), SvCUR(name), valstr, seenhv,
752 postav, &level, indent, pad, xpad, newapad, sep,
753 freezer, toaster, purity, deepcopy, quotekeys,
757 SvREFCNT_dec(newapad);
759 postlen = av_len(postav);
760 if (postlen >= 0 || !terse) {
761 sv_insert(valstr, 0, 0, " = ", 3);
762 sv_insert(valstr, 0, 0, SvPVX(name), SvCUR(name));
763 sv_catpvn(valstr, ";", 1);
765 sv_catsv(retval, pad);
766 sv_catsv(retval, valstr);
767 sv_catsv(retval, sep);
770 sv_catsv(retval, pad);
771 for (i = 0; i <= postlen; ++i) {
773 svp = av_fetch(postav, i, FALSE);
774 if (svp && (elem = *svp)) {
775 sv_catsv(retval, elem);
777 sv_catpvn(retval, ";", 1);
778 sv_catsv(retval, sep);
779 sv_catsv(retval, pad);
783 sv_catpvn(retval, ";", 1);
784 sv_catsv(retval, sep);
786 sv_setpvn(valstr, "", 0);
787 if (gimme == G_ARRAY) {
788 XPUSHs(sv_2mortal(retval));
789 if (i < imax) /* not the last time thro ? */
790 retval = newSVpv("",0);
793 SvREFCNT_dec(postav);
794 SvREFCNT_dec(valstr);
797 croak("Call to new() method failed to return HASH ref");
798 if (gimme == G_SCALAR)
799 XPUSHs(sv_2mortal(retval));