7 # define PL_sv_undef sv_undef
10 # define ERRSV GvSV(errgv)
13 # define newSVpvn newSVpv
17 static I32 num_q _((char *s, STRLEN slen));
18 static I32 esc_q _((char *dest, char *src, STRLEN slen));
19 static SV *sv_x _((SV *sv, char *str, STRLEN len, I32 n));
20 static I32 DD_dump _((SV *val, char *name, STRLEN namelen, SV *retval,
21 HV *seenhv, AV *postav, I32 *levelp, I32 indent,
22 SV *pad, SV *xpad, SV *apad, SV *sep,
23 SV *freezer, SV *toaster,
24 I32 purity, I32 deepcopy, I32 quotekeys, SV *bless));
26 /* does a string need to be protected? */
28 needs_quote(register char *s)
52 /* count the number of "'"s and "\"s in string */
54 num_q(register char *s, register STRLEN slen)
59 if (*s == '\'' || *s == '\\')
68 /* returns number of chars added to escape "'"s and "\"s in s */
69 /* slen number of characters in s will be escaped */
70 /* destination must be long enough for additional chars */
72 esc_q(register char *d, register char *s, register STRLEN slen)
91 /* append a repeated string to an SV */
93 sv_x(SV *sv, register char *str, STRLEN len, I32 n)
98 assert(SvTYPE(sv) >= SVt_PV);
101 SvGROW(sv, len*n + SvCUR(sv) + 1);
103 char *start = SvPVX(sv) + SvCUR(sv);
111 sv_catpvn(sv, str, len);
119 * This ought to be split into smaller functions. (it is one long function since
120 * it exactly parallels the perl version, which was one long thing for
121 * efficiency raisins.) Ugggh!
124 DD_dump(SV *val, char *name, STRLEN namelen, SV *retval, HV *seenhv,
125 AV *postav, I32 *levelp, I32 indent, SV *pad, SV *xpad,
126 SV *apad, SV *sep, SV *freezer, SV *toaster, I32 purity,
127 I32 deepcopy, I32 quotekeys, SV *bless)
131 char *c, *r, *realpack, id[128];
133 SV *sv, *ipad, *ival;
134 SV *blesspad = Nullsv;
135 AV *seenentry = Nullav;
137 STRLEN inamelen, idlen = 0;
144 flags = SvFLAGS(val);
145 realtype = SvTYPE(val);
151 if (SvOBJECT(SvRV(val)) && freezer &&
152 SvPOK(freezer) && SvCUR(freezer))
154 dSP; ENTER; SAVETMPS; PUSHMARK(sp);
155 XPUSHs(val); PUTBACK;
156 i = perl_call_method(SvPVX(freezer), G_EVAL|G_SCALAR);
159 warn("WARNING(Freezer method call failed): %s",
163 PUTBACK; FREETMPS; LEAVE;
165 (void)sv_2mortal(val);
169 flags = SvFLAGS(ival);
170 realtype = SvTYPE(ival);
171 (void) sprintf(id, "0x%lx", (unsigned long)ival);
174 realpack = HvNAME(SvSTASH(ival));
178 /* if it has a name, we need to either look it up, or keep a tab
179 * on it so we know when we hit it later
182 if ((svp = hv_fetch(seenhv, id, idlen, FALSE))
183 && (sv = *svp) && SvROK(sv) && (seenentry = (AV*)SvRV(sv)))
186 if ((svp = av_fetch(seenentry, 0, FALSE))
187 && (othername = *svp))
189 if (purity && *levelp > 0) {
192 if (realtype == SVt_PVHV)
193 sv_catpvn(retval, "{}", 2);
194 else if (realtype == SVt_PVAV)
195 sv_catpvn(retval, "[]", 2);
197 sv_catpvn(retval, "''", 2);
198 postentry = newSVpvn(name, namelen);
199 sv_catpvn(postentry, " = ", 3);
200 sv_catsv(postentry, othername);
201 av_push(postav, postentry);
204 if (name[0] == '@' || name[0] == '%') {
205 if ((SvPVX(othername))[0] == '\\' &&
206 (SvPVX(othername))[1] == name[0]) {
207 sv_catpvn(retval, SvPVX(othername)+1,
211 sv_catpvn(retval, name, 1);
212 sv_catpvn(retval, "{", 1);
213 sv_catsv(retval, othername);
214 sv_catpvn(retval, "}", 1);
218 sv_catsv(retval, othername);
223 warn("ref name not found for %s", id);
227 else { /* store our name and continue */
229 if (name[0] == '@' || name[0] == '%') {
230 namesv = newSVpvn("\\", 1);
231 sv_catpvn(namesv, name, namelen);
233 else if (realtype == SVt_PVCV && name[0] == '*') {
234 namesv = newSVpvn("\\", 2);
235 sv_catpvn(namesv, name, namelen);
236 (SvPVX(namesv))[1] = '&';
239 namesv = newSVpvn(name, namelen);
241 av_push(seenentry, namesv);
242 (void)SvREFCNT_inc(val);
243 av_push(seenentry, val);
244 (void)hv_store(seenhv, id, strlen(id),
245 newRV((SV*)seenentry), 0);
246 SvREFCNT_dec(seenentry);
251 ipad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp);
253 if (realpack) { /* we have a blessed ref */
255 char *blessstr = SvPV(bless, blesslen);
256 sv_catpvn(retval, blessstr, blesslen);
257 sv_catpvn(retval, "( ", 2);
260 apad = newSVsv(apad);
261 sv_x(apad, " ", 1, blesslen+2);
265 if (realtype <= SVt_PVBM) { /* scalar ref */
266 SV *namesv = newSVpvn("${", 2);
267 sv_catpvn(namesv, name, namelen);
268 sv_catpvn(namesv, "}", 1);
269 if (realpack) { /* blessed */
270 sv_catpvn(retval, "do{\\(my $o = ", 13);
271 DD_dump(ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
272 postav, levelp, indent, pad, xpad, apad, sep,
273 freezer, toaster, purity, deepcopy, quotekeys, bless);
274 sv_catpvn(retval, ")}", 2);
277 sv_catpvn(retval, "\\", 1);
278 DD_dump(ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
279 postav, levelp, indent, pad, xpad, apad, sep,
280 freezer, toaster, purity, deepcopy, quotekeys, bless);
282 SvREFCNT_dec(namesv);
284 else if (realtype == SVt_PVGV) { /* glob ref */
285 SV *namesv = newSVpvn("*{", 2);
286 sv_catpvn(namesv, name, namelen);
287 sv_catpvn(namesv, "}", 1);
288 sv_catpvn(retval, "\\", 1);
289 DD_dump(ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
290 postav, levelp, indent, pad, xpad, apad, sep,
291 freezer, toaster, purity, deepcopy, quotekeys, bless);
292 SvREFCNT_dec(namesv);
294 else if (realtype == SVt_PVAV) {
297 I32 ixmax = av_len((AV *)ival);
299 SV *ixsv = newSViv(0);
300 /* allowing for a 24 char wide array index */
301 New(0, iname, namelen+28, char);
302 (void)strcpy(iname, name);
304 if (name[0] == '@') {
305 sv_catpvn(retval, "(", 1);
309 sv_catpvn(retval, "[", 1);
310 /* omit "->" in $foo{bar}->[0], but not in ${$foo}->[0] */
312 && name[namelen-1] != ']' && name[namelen-1] != '}'
313 && (namelen < 4 || (name[1] != '{' && name[2] != '{')))*/
315 && name[namelen-1] != ']' && name[namelen-1] != '}')
318 || (name[0] == '\\' && name[2] == '{'))))
320 iname[inamelen++] = '-'; iname[inamelen++] = '>';
321 iname[inamelen] = '\0';
324 if (iname[0] == '*' && iname[inamelen-1] == '}' && inamelen >= 8 &&
325 (instr(iname+inamelen-8, "{SCALAR}") ||
326 instr(iname+inamelen-7, "{ARRAY}") ||
327 instr(iname+inamelen-6, "{HASH}"))) {
328 iname[inamelen++] = '-'; iname[inamelen++] = '>';
330 iname[inamelen++] = '['; iname[inamelen] = '\0';
331 totpad = newSVsv(sep);
332 sv_catsv(totpad, pad);
333 sv_catsv(totpad, apad);
335 for (ix = 0; ix <= ixmax; ++ix) {
338 svp = av_fetch((AV*)ival, ix, FALSE);
346 (void) sprintf(iname+ilen, "%ld", ix);
347 ilen = strlen(iname);
348 iname[ilen++] = ']'; iname[ilen] = '\0';
350 sv_catsv(retval, totpad);
351 sv_catsv(retval, ipad);
352 sv_catpvn(retval, "#", 1);
353 sv_catsv(retval, ixsv);
355 sv_catsv(retval, totpad);
356 sv_catsv(retval, ipad);
357 DD_dump(elem, iname, ilen, retval, seenhv, postav,
358 levelp, indent, pad, xpad, apad, sep,
359 freezer, toaster, purity, deepcopy, quotekeys, bless);
361 sv_catpvn(retval, ",", 1);
364 SV *opad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), (*levelp)-1);
365 sv_catsv(retval, totpad);
366 sv_catsv(retval, opad);
370 sv_catpvn(retval, ")", 1);
372 sv_catpvn(retval, "]", 1);
374 SvREFCNT_dec(totpad);
377 else if (realtype == SVt_PVHV) {
378 SV *totpad, *newapad;
385 iname = newSVpvn(name, namelen);
386 if (name[0] == '%') {
387 sv_catpvn(retval, "(", 1);
388 (SvPVX(iname))[0] = '$';
391 sv_catpvn(retval, "{", 1);
392 /* omit "->" in $foo[0]->{bar}, but not in ${$foo}->{bar} */
394 && name[namelen-1] != ']' && name[namelen-1] != '}')
397 || (name[0] == '\\' && name[2] == '{'))))
399 sv_catpvn(iname, "->", 2);
402 if (name[0] == '*' && name[namelen-1] == '}' && namelen >= 8 &&
403 (instr(name+namelen-8, "{SCALAR}") ||
404 instr(name+namelen-7, "{ARRAY}") ||
405 instr(name+namelen-6, "{HASH}"))) {
406 sv_catpvn(iname, "->", 2);
408 sv_catpvn(iname, "{", 1);
409 totpad = newSVsv(sep);
410 sv_catsv(totpad, pad);
411 sv_catsv(totpad, apad);
413 (void)hv_iterinit((HV*)ival);
415 while ((entry = hv_iternext((HV*)ival))) {
420 sv_catpvn(retval, ",", 1);
422 key = hv_iterkey(entry, &klen);
423 hval = hv_iterval((HV*)ival, entry);
425 if (quotekeys || needs_quote(key)) {
426 nticks = num_q(key, klen);
427 New(0, nkey, klen+nticks+3, char);
430 klen += esc_q(nkey+1, key, klen);
432 (void)Copy(key, nkey+1, klen, char);
437 New(0, nkey, klen, char);
438 (void)Copy(key, nkey, klen, char);
441 sname = newSVsv(iname);
442 sv_catpvn(sname, nkey, klen);
443 sv_catpvn(sname, "}", 1);
445 sv_catsv(retval, totpad);
446 sv_catsv(retval, ipad);
447 sv_catpvn(retval, nkey, klen);
448 sv_catpvn(retval, " => ", 4);
452 newapad = newSVsv(apad);
453 New(0, extra, klen+4+1, char);
454 while (elen < (klen+4))
457 sv_catpvn(newapad, extra, elen);
463 DD_dump(hval, SvPVX(sname), SvCUR(sname), retval, seenhv,
464 postav, levelp, indent, pad, xpad, newapad, sep,
465 freezer, toaster, purity, deepcopy, quotekeys, bless);
469 SvREFCNT_dec(newapad);
472 SV *opad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp-1);
473 sv_catsv(retval, totpad);
474 sv_catsv(retval, opad);
478 sv_catpvn(retval, ")", 1);
480 sv_catpvn(retval, "}", 1);
482 SvREFCNT_dec(totpad);
484 else if (realtype == SVt_PVCV) {
485 sv_catpvn(retval, "sub { \"DUMMY\" }", 15);
487 warn("Encountered CODE ref, using dummy placeholder");
490 warn("cannot handle ref type %ld", realtype);
493 if (realpack) { /* free blessed allocs */
498 sv_catpvn(retval, ", '", 3);
499 sv_catpvn(retval, realpack, strlen(realpack));
500 sv_catpvn(retval, "' )", 3);
501 if (toaster && SvPOK(toaster) && SvCUR(toaster)) {
502 sv_catpvn(retval, "->", 2);
503 sv_catsv(retval, toaster);
504 sv_catpvn(retval, "()", 2);
514 (void) sprintf(id, "0x%lx", (unsigned long)val);
515 if ((svp = hv_fetch(seenhv, id, (idlen = strlen(id)), FALSE)) &&
516 (sv = *svp) && SvROK(sv) &&
517 (seenentry = (AV*)SvRV(sv)))
520 if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)
521 && (svp = av_fetch(seenentry, 2, FALSE)) && *svp && SvIV(*svp) > 0)
523 sv_catpvn(retval, "${", 2);
524 sv_catsv(retval, othername);
525 sv_catpvn(retval, "}", 1);
531 namesv = newSVpvn("\\", 1);
532 sv_catpvn(namesv, name, namelen);
534 av_push(seenentry, namesv);
535 av_push(seenentry, newRV(val));
536 (void)hv_store(seenhv, id, strlen(id), newRV((SV*)seenentry), 0);
537 SvREFCNT_dec(seenentry);
544 (void) sprintf(tmpbuf, "%d", i);
545 len = strlen(tmpbuf);
546 sv_catpvn(retval, tmpbuf, len);
548 else if (realtype == SVt_PVGV) {/* GLOBs can end up with scribbly names */
550 ++c; --i; /* just get the name */
551 if (i >= 6 && strncmp(c, "main::", 6) == 0) {
555 if (needs_quote(c)) {
556 sv_grow(retval, SvCUR(retval)+6+2*i);
557 r = SvPVX(retval)+SvCUR(retval);
558 r[0] = '*'; r[1] = '{'; r[2] = '\'';
559 i += esc_q(r+3, c, i);
561 r[i++] = '\''; r[i++] = '}';
565 sv_grow(retval, SvCUR(retval)+i+2);
566 r = SvPVX(retval)+SvCUR(retval);
567 r[0] = '*'; strcpy(r+1, c);
570 SvCUR_set(retval, SvCUR(retval)+i);
573 static char *entries[] = { "{SCALAR}", "{ARRAY}", "{HASH}" };
574 static STRLEN sizes[] = { 8, 7, 6 };
576 SV *nname = newSVpvn("", 0);
577 SV *newapad = newSVpvn("", 0);
581 for (j=0; j<3; j++) {
582 e = ((j == 0) ? GvSV(gv) : (j == 1) ? (SV*)GvAV(gv) : (SV*)GvHV(gv));
585 if (j == 0 && !SvOK(e))
590 SV *postentry = newSVpvn(r,i);
592 sv_setsv(nname, postentry);
593 sv_catpvn(nname, entries[j], sizes[j]);
594 sv_catpvn(postentry, " = ", 3);
595 av_push(postav, postentry);
600 (void)sv_x(newapad, " ", 1, SvCUR(postentry));
602 DD_dump(e, SvPVX(nname), SvCUR(nname), postentry,
603 seenhv, postav, &nlevel, indent, pad, xpad,
604 newapad, sep, freezer, toaster, purity,
605 deepcopy, quotekeys, bless);
610 SvREFCNT_dec(newapad);
614 else if (val == &PL_sv_undef || !SvOK(val)) {
615 sv_catpvn(retval, "undef", 5);
619 sv_grow(retval, SvCUR(retval)+3+2*i);
620 r = SvPVX(retval)+SvCUR(retval);
622 i += esc_q(r+1, c, i);
626 SvCUR_set(retval, SvCUR(retval)+i);
632 (void)hv_delete(seenhv, id, idlen, G_DISCARD);
633 else if (namelen && seenentry) {
634 SV *mark = *av_fetch(seenentry, 2, TRUE);
642 MODULE = Data::Dumper PACKAGE = Data::Dumper PREFIX = Data_Dumper_
645 # This is the exact equivalent of Dump. Well, almost. The things that are
646 # different as of now (due to Laziness):
647 # * doesnt do double-quotes yet.
651 Data_Dumper_Dumpxs(href, ...)
659 AV *postav, *todumpav, *namesav;
661 I32 indent, terse, useqq, i, imax, postlen;
663 SV *val, *name, *pad, *xpad, *apad, *sep, *tmp, *varname;
664 SV *freezer, *toaster, *bless;
665 I32 purity, deepcopy, quotekeys;
669 if (!SvROK(href)) { /* call new to get an object first */
678 croak("Usage: Data::Dumper::Dumpxs(PACKAGE, VAL_ARY_REF, NAME_ARY_REF)");
685 XPUSHs(sv_2mortal(newSVsv(valarray)));
686 XPUSHs(sv_2mortal(newSVsv(namearray)));
688 i = perl_call_method("new", G_SCALAR);
691 href = newSVsv(POPs);
697 (void)sv_2mortal(href);
700 todumpav = namesav = Nullav;
702 val = pad = xpad = apad = sep = tmp = varname
703 = freezer = toaster = bless = &PL_sv_undef;
704 name = sv_newmortal();
706 terse = useqq = purity = deepcopy = 0;
709 retval = newSVpvn("", 0);
711 && (hv = (HV*)SvRV((SV*)href))
712 && SvTYPE(hv) == SVt_PVHV) {
714 if ((svp = hv_fetch(hv, "seen", 4, FALSE)) && SvROK(*svp))
715 seenhv = (HV*)SvRV(*svp);
716 if ((svp = hv_fetch(hv, "todump", 6, FALSE)) && SvROK(*svp))
717 todumpav = (AV*)SvRV(*svp);
718 if ((svp = hv_fetch(hv, "names", 5, FALSE)) && SvROK(*svp))
719 namesav = (AV*)SvRV(*svp);
720 if ((svp = hv_fetch(hv, "indent", 6, FALSE)))
722 if ((svp = hv_fetch(hv, "purity", 6, FALSE)))
724 if ((svp = hv_fetch(hv, "terse", 5, FALSE)))
725 terse = SvTRUE(*svp);
726 if ((svp = hv_fetch(hv, "useqq", 5, FALSE)))
727 useqq = SvTRUE(*svp);
728 if ((svp = hv_fetch(hv, "pad", 3, FALSE)))
730 if ((svp = hv_fetch(hv, "xpad", 4, FALSE)))
732 if ((svp = hv_fetch(hv, "apad", 4, FALSE)))
734 if ((svp = hv_fetch(hv, "sep", 3, FALSE)))
736 if ((svp = hv_fetch(hv, "varname", 7, FALSE)))
738 if ((svp = hv_fetch(hv, "freezer", 7, FALSE)))
740 if ((svp = hv_fetch(hv, "toaster", 7, FALSE)))
742 if ((svp = hv_fetch(hv, "deepcopy", 8, FALSE)))
743 deepcopy = SvTRUE(*svp);
744 if ((svp = hv_fetch(hv, "quotekeys", 9, FALSE)))
745 quotekeys = SvTRUE(*svp);
746 if ((svp = hv_fetch(hv, "bless", 5, FALSE)))
751 imax = av_len(todumpav);
754 valstr = newSVpvn("",0);
755 for (i = 0; i <= imax; ++i) {
759 if ((svp = av_fetch(todumpav, i, FALSE)))
763 if ((svp = av_fetch(namesav, i, TRUE)))
764 sv_setsv(name, *svp);
769 if ((SvPVX(name))[0] == '*') {
771 switch (SvTYPE(SvRV(val))) {
773 (SvPVX(name))[0] = '@';
776 (SvPVX(name))[0] = '%';
779 (SvPVX(name))[0] = '*';
782 (SvPVX(name))[0] = '$';
787 (SvPVX(name))[0] = '$';
789 else if ((SvPVX(name))[0] != '$')
790 sv_insert(name, 0, 0, "$", 1);
794 sv_setpvn(name, "$", 1);
795 sv_catsv(name, varname);
796 (void) sprintf(tmpbuf, "%ld", i+1);
797 nchars = strlen(tmpbuf);
798 sv_catpvn(name, tmpbuf, nchars);
802 SV *tmpsv = sv_x(Nullsv, " ", 1, SvCUR(name)+3);
803 newapad = newSVsv(apad);
804 sv_catsv(newapad, tmpsv);
810 DD_dump(val, SvPVX(name), SvCUR(name), valstr, seenhv,
811 postav, &level, indent, pad, xpad, newapad, sep,
812 freezer, toaster, purity, deepcopy, quotekeys,
816 SvREFCNT_dec(newapad);
818 postlen = av_len(postav);
819 if (postlen >= 0 || !terse) {
820 sv_insert(valstr, 0, 0, " = ", 3);
821 sv_insert(valstr, 0, 0, SvPVX(name), SvCUR(name));
822 sv_catpvn(valstr, ";", 1);
824 sv_catsv(retval, pad);
825 sv_catsv(retval, valstr);
826 sv_catsv(retval, sep);
829 sv_catsv(retval, pad);
830 for (i = 0; i <= postlen; ++i) {
832 svp = av_fetch(postav, i, FALSE);
833 if (svp && (elem = *svp)) {
834 sv_catsv(retval, elem);
836 sv_catpvn(retval, ";", 1);
837 sv_catsv(retval, sep);
838 sv_catsv(retval, pad);
842 sv_catpvn(retval, ";", 1);
843 sv_catsv(retval, sep);
845 sv_setpvn(valstr, "", 0);
846 if (gimme == G_ARRAY) {
847 XPUSHs(sv_2mortal(retval));
848 if (i < imax) /* not the last time thro ? */
849 retval = newSVpvn("",0);
852 SvREFCNT_dec(postav);
853 SvREFCNT_dec(valstr);
856 croak("Call to new() method failed to return HASH ref");
857 if (gimme == G_SCALAR)
858 XPUSHs(sv_2mortal(retval));