6 #include "patchlevel.h"
7 #define PERL_VERSION PATCHLEVEL
12 # define PL_sv_undef sv_undef
15 # define ERRSV GvSV(errgv)
18 # define newSVpvn newSVpv
22 static I32 num_q (char *s, STRLEN slen);
23 static I32 esc_q (char *dest, char *src, STRLEN slen);
24 static SV *sv_x (SV *sv, char *str, STRLEN len, I32 n);
25 static I32 DD_dump (SV *val, char *name, STRLEN namelen, SV *retval,
26 HV *seenhv, AV *postav, I32 *levelp, I32 indent,
27 SV *pad, SV *xpad, SV *apad, SV *sep,
28 SV *freezer, SV *toaster,
29 I32 purity, I32 deepcopy, I32 quotekeys, SV *bless);
31 /* does a string need to be protected? */
33 needs_quote(register char *s)
57 /* count the number of "'"s and "\"s in string */
59 num_q(register char *s, register STRLEN slen)
64 if (*s == '\'' || *s == '\\')
73 /* returns number of chars added to escape "'"s and "\"s in s */
74 /* slen number of characters in s will be escaped */
75 /* destination must be long enough for additional chars */
77 esc_q(register char *d, register char *s, register STRLEN slen)
96 /* append a repeated string to an SV */
98 sv_x(SV *sv, register char *str, STRLEN len, I32 n)
101 sv = newSVpvn("", 0);
103 assert(SvTYPE(sv) >= SVt_PV);
106 SvGROW(sv, len*n + SvCUR(sv) + 1);
108 char *start = SvPVX(sv) + SvCUR(sv);
116 sv_catpvn(sv, str, len);
124 * This ought to be split into smaller functions. (it is one long function since
125 * it exactly parallels the perl version, which was one long thing for
126 * efficiency raisins.) Ugggh!
129 DD_dump(SV *val, char *name, STRLEN namelen, SV *retval, HV *seenhv,
130 AV *postav, I32 *levelp, I32 indent, SV *pad, SV *xpad,
131 SV *apad, SV *sep, SV *freezer, SV *toaster, I32 purity,
132 I32 deepcopy, I32 quotekeys, SV *bless)
136 char *c, *r, *realpack, id[128];
138 SV *sv, *ipad, *ival;
139 SV *blesspad = Nullsv;
140 AV *seenentry = Nullav;
142 STRLEN inamelen, idlen = 0;
149 flags = SvFLAGS(val);
150 realtype = SvTYPE(val);
156 if (SvOBJECT(SvRV(val)) && freezer &&
157 SvPOK(freezer) && SvCUR(freezer))
159 dSP; ENTER; SAVETMPS; PUSHMARK(sp);
160 XPUSHs(val); PUTBACK;
161 i = perl_call_method(SvPVX(freezer), G_EVAL|G_SCALAR);
164 warn("WARNING(Freezer method call failed): %s",
168 PUTBACK; FREETMPS; LEAVE;
170 (void)sv_2mortal(val);
174 flags = SvFLAGS(ival);
175 realtype = SvTYPE(ival);
176 (void) sprintf(id, "0x%lx", (unsigned long)ival);
179 realpack = HvNAME(SvSTASH(ival));
183 /* if it has a name, we need to either look it up, or keep a tab
184 * on it so we know when we hit it later
187 if ((svp = hv_fetch(seenhv, id, idlen, FALSE))
188 && (sv = *svp) && SvROK(sv) && (seenentry = (AV*)SvRV(sv)))
191 if ((svp = av_fetch(seenentry, 0, FALSE))
192 && (othername = *svp))
194 if (purity && *levelp > 0) {
197 if (realtype == SVt_PVHV)
198 sv_catpvn(retval, "{}", 2);
199 else if (realtype == SVt_PVAV)
200 sv_catpvn(retval, "[]", 2);
202 sv_catpvn(retval, "''", 2);
203 postentry = newSVpvn(name, namelen);
204 sv_catpvn(postentry, " = ", 3);
205 sv_catsv(postentry, othername);
206 av_push(postav, postentry);
209 if (name[0] == '@' || name[0] == '%') {
210 if ((SvPVX(othername))[0] == '\\' &&
211 (SvPVX(othername))[1] == name[0]) {
212 sv_catpvn(retval, SvPVX(othername)+1,
216 sv_catpvn(retval, name, 1);
217 sv_catpvn(retval, "{", 1);
218 sv_catsv(retval, othername);
219 sv_catpvn(retval, "}", 1);
223 sv_catsv(retval, othername);
228 warn("ref name not found for %s", id);
232 else { /* store our name and continue */
234 if (name[0] == '@' || name[0] == '%') {
235 namesv = newSVpvn("\\", 1);
236 sv_catpvn(namesv, name, namelen);
238 else if (realtype == SVt_PVCV && name[0] == '*') {
239 namesv = newSVpvn("\\", 2);
240 sv_catpvn(namesv, name, namelen);
241 (SvPVX(namesv))[1] = '&';
244 namesv = newSVpvn(name, namelen);
246 av_push(seenentry, namesv);
247 (void)SvREFCNT_inc(val);
248 av_push(seenentry, val);
249 (void)hv_store(seenhv, id, strlen(id),
250 newRV((SV*)seenentry), 0);
251 SvREFCNT_dec(seenentry);
256 ipad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp);
258 if (realpack) { /* we have a blessed ref */
260 char *blessstr = SvPV(bless, blesslen);
261 sv_catpvn(retval, blessstr, blesslen);
262 sv_catpvn(retval, "( ", 2);
265 apad = newSVsv(apad);
266 sv_x(apad, " ", 1, blesslen+2);
270 if (realtype <= SVt_PVBM) { /* scalar ref */
271 SV *namesv = newSVpvn("${", 2);
272 sv_catpvn(namesv, name, namelen);
273 sv_catpvn(namesv, "}", 1);
274 if (realpack) { /* blessed */
275 sv_catpvn(retval, "do{\\(my $o = ", 13);
276 DD_dump(ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
277 postav, levelp, indent, pad, xpad, apad, sep,
278 freezer, toaster, purity, deepcopy, quotekeys, bless);
279 sv_catpvn(retval, ")}", 2);
282 sv_catpvn(retval, "\\", 1);
283 DD_dump(ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
284 postav, levelp, indent, pad, xpad, apad, sep,
285 freezer, toaster, purity, deepcopy, quotekeys, bless);
287 SvREFCNT_dec(namesv);
289 else if (realtype == SVt_PVGV) { /* glob ref */
290 SV *namesv = newSVpvn("*{", 2);
291 sv_catpvn(namesv, name, namelen);
292 sv_catpvn(namesv, "}", 1);
293 sv_catpvn(retval, "\\", 1);
294 DD_dump(ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
295 postav, levelp, indent, pad, xpad, apad, sep,
296 freezer, toaster, purity, deepcopy, quotekeys, bless);
297 SvREFCNT_dec(namesv);
299 else if (realtype == SVt_PVAV) {
302 I32 ixmax = av_len((AV *)ival);
304 SV *ixsv = newSViv(0);
305 /* allowing for a 24 char wide array index */
306 New(0, iname, namelen+28, char);
307 (void)strcpy(iname, name);
309 if (name[0] == '@') {
310 sv_catpvn(retval, "(", 1);
314 sv_catpvn(retval, "[", 1);
315 /* omit "->" in $foo{bar}->[0], but not in ${$foo}->[0] */
317 && name[namelen-1] != ']' && name[namelen-1] != '}'
318 && (namelen < 4 || (name[1] != '{' && name[2] != '{')))*/
320 && name[namelen-1] != ']' && name[namelen-1] != '}')
323 || (name[0] == '\\' && name[2] == '{'))))
325 iname[inamelen++] = '-'; iname[inamelen++] = '>';
326 iname[inamelen] = '\0';
329 if (iname[0] == '*' && iname[inamelen-1] == '}' && inamelen >= 8 &&
330 (instr(iname+inamelen-8, "{SCALAR}") ||
331 instr(iname+inamelen-7, "{ARRAY}") ||
332 instr(iname+inamelen-6, "{HASH}"))) {
333 iname[inamelen++] = '-'; iname[inamelen++] = '>';
335 iname[inamelen++] = '['; iname[inamelen] = '\0';
336 totpad = newSVsv(sep);
337 sv_catsv(totpad, pad);
338 sv_catsv(totpad, apad);
340 for (ix = 0; ix <= ixmax; ++ix) {
343 svp = av_fetch((AV*)ival, ix, FALSE);
351 (void) sprintf(iname+ilen, "%ld", ix);
352 ilen = strlen(iname);
353 iname[ilen++] = ']'; iname[ilen] = '\0';
355 sv_catsv(retval, totpad);
356 sv_catsv(retval, ipad);
357 sv_catpvn(retval, "#", 1);
358 sv_catsv(retval, ixsv);
360 sv_catsv(retval, totpad);
361 sv_catsv(retval, ipad);
362 DD_dump(elem, iname, ilen, retval, seenhv, postav,
363 levelp, indent, pad, xpad, apad, sep,
364 freezer, toaster, purity, deepcopy, quotekeys, bless);
366 sv_catpvn(retval, ",", 1);
369 SV *opad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), (*levelp)-1);
370 sv_catsv(retval, totpad);
371 sv_catsv(retval, opad);
375 sv_catpvn(retval, ")", 1);
377 sv_catpvn(retval, "]", 1);
379 SvREFCNT_dec(totpad);
382 else if (realtype == SVt_PVHV) {
383 SV *totpad, *newapad;
390 iname = newSVpvn(name, namelen);
391 if (name[0] == '%') {
392 sv_catpvn(retval, "(", 1);
393 (SvPVX(iname))[0] = '$';
396 sv_catpvn(retval, "{", 1);
397 /* omit "->" in $foo[0]->{bar}, but not in ${$foo}->{bar} */
399 && name[namelen-1] != ']' && name[namelen-1] != '}')
402 || (name[0] == '\\' && name[2] == '{'))))
404 sv_catpvn(iname, "->", 2);
407 if (name[0] == '*' && name[namelen-1] == '}' && namelen >= 8 &&
408 (instr(name+namelen-8, "{SCALAR}") ||
409 instr(name+namelen-7, "{ARRAY}") ||
410 instr(name+namelen-6, "{HASH}"))) {
411 sv_catpvn(iname, "->", 2);
413 sv_catpvn(iname, "{", 1);
414 totpad = newSVsv(sep);
415 sv_catsv(totpad, pad);
416 sv_catsv(totpad, apad);
418 (void)hv_iterinit((HV*)ival);
420 while ((entry = hv_iternext((HV*)ival))) {
425 sv_catpvn(retval, ",", 1);
427 key = hv_iterkey(entry, &klen);
428 hval = hv_iterval((HV*)ival, entry);
430 if (quotekeys || needs_quote(key)) {
431 nticks = num_q(key, klen);
432 New(0, nkey, klen+nticks+3, char);
435 klen += esc_q(nkey+1, key, klen);
437 (void)Copy(key, nkey+1, klen, char);
442 New(0, nkey, klen, char);
443 (void)Copy(key, nkey, klen, char);
446 sname = newSVsv(iname);
447 sv_catpvn(sname, nkey, klen);
448 sv_catpvn(sname, "}", 1);
450 sv_catsv(retval, totpad);
451 sv_catsv(retval, ipad);
452 sv_catpvn(retval, nkey, klen);
453 sv_catpvn(retval, " => ", 4);
457 newapad = newSVsv(apad);
458 New(0, extra, klen+4+1, char);
459 while (elen < (klen+4))
462 sv_catpvn(newapad, extra, elen);
468 DD_dump(hval, SvPVX(sname), SvCUR(sname), retval, seenhv,
469 postav, levelp, indent, pad, xpad, newapad, sep,
470 freezer, toaster, purity, deepcopy, quotekeys, bless);
474 SvREFCNT_dec(newapad);
477 SV *opad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp-1);
478 sv_catsv(retval, totpad);
479 sv_catsv(retval, opad);
483 sv_catpvn(retval, ")", 1);
485 sv_catpvn(retval, "}", 1);
487 SvREFCNT_dec(totpad);
489 else if (realtype == SVt_PVCV) {
490 sv_catpvn(retval, "sub { \"DUMMY\" }", 15);
492 warn("Encountered CODE ref, using dummy placeholder");
495 warn("cannot handle ref type %ld", realtype);
498 if (realpack) { /* free blessed allocs */
503 sv_catpvn(retval, ", '", 3);
504 sv_catpvn(retval, realpack, strlen(realpack));
505 sv_catpvn(retval, "' )", 3);
506 if (toaster && SvPOK(toaster) && SvCUR(toaster)) {
507 sv_catpvn(retval, "->", 2);
508 sv_catsv(retval, toaster);
509 sv_catpvn(retval, "()", 2);
519 (void) sprintf(id, "0x%lx", (unsigned long)val);
520 if ((svp = hv_fetch(seenhv, id, (idlen = strlen(id)), FALSE)) &&
521 (sv = *svp) && SvROK(sv) &&
522 (seenentry = (AV*)SvRV(sv)))
525 if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)
526 && (svp = av_fetch(seenentry, 2, FALSE)) && *svp && SvIV(*svp) > 0)
528 sv_catpvn(retval, "${", 2);
529 sv_catsv(retval, othername);
530 sv_catpvn(retval, "}", 1);
536 namesv = newSVpvn("\\", 1);
537 sv_catpvn(namesv, name, namelen);
539 av_push(seenentry, namesv);
540 av_push(seenentry, newRV(val));
541 (void)hv_store(seenhv, id, strlen(id), newRV((SV*)seenentry), 0);
542 SvREFCNT_dec(seenentry);
549 (void) sprintf(tmpbuf, "%d", i);
550 len = strlen(tmpbuf);
551 sv_catpvn(retval, tmpbuf, len);
553 else if (realtype == SVt_PVGV) {/* GLOBs can end up with scribbly names */
555 ++c; --i; /* just get the name */
556 if (i >= 6 && strncmp(c, "main::", 6) == 0) {
560 if (needs_quote(c)) {
561 sv_grow(retval, SvCUR(retval)+6+2*i);
562 r = SvPVX(retval)+SvCUR(retval);
563 r[0] = '*'; r[1] = '{'; r[2] = '\'';
564 i += esc_q(r+3, c, i);
566 r[i++] = '\''; r[i++] = '}';
570 sv_grow(retval, SvCUR(retval)+i+2);
571 r = SvPVX(retval)+SvCUR(retval);
572 r[0] = '*'; strcpy(r+1, c);
575 SvCUR_set(retval, SvCUR(retval)+i);
578 static char *entries[] = { "{SCALAR}", "{ARRAY}", "{HASH}" };
579 static STRLEN sizes[] = { 8, 7, 6 };
581 SV *nname = newSVpvn("", 0);
582 SV *newapad = newSVpvn("", 0);
586 for (j=0; j<3; j++) {
587 e = ((j == 0) ? GvSV(gv) : (j == 1) ? (SV*)GvAV(gv) : (SV*)GvHV(gv));
590 if (j == 0 && !SvOK(e))
595 SV *postentry = newSVpvn(r,i);
597 sv_setsv(nname, postentry);
598 sv_catpvn(nname, entries[j], sizes[j]);
599 sv_catpvn(postentry, " = ", 3);
600 av_push(postav, postentry);
605 (void)sv_x(newapad, " ", 1, SvCUR(postentry));
607 DD_dump(e, SvPVX(nname), SvCUR(nname), postentry,
608 seenhv, postav, &nlevel, indent, pad, xpad,
609 newapad, sep, freezer, toaster, purity,
610 deepcopy, quotekeys, bless);
615 SvREFCNT_dec(newapad);
619 else if (val == &PL_sv_undef || !SvOK(val)) {
620 sv_catpvn(retval, "undef", 5);
624 sv_grow(retval, SvCUR(retval)+3+2*i);
625 r = SvPVX(retval)+SvCUR(retval);
627 i += esc_q(r+1, c, i);
631 SvCUR_set(retval, SvCUR(retval)+i);
637 (void)hv_delete(seenhv, id, idlen, G_DISCARD);
638 else if (namelen && seenentry) {
639 SV *mark = *av_fetch(seenentry, 2, TRUE);
647 MODULE = Data::Dumper PACKAGE = Data::Dumper PREFIX = Data_Dumper_
650 # This is the exact equivalent of Dump. Well, almost. The things that are
651 # different as of now (due to Laziness):
652 # * doesnt do double-quotes yet.
656 Data_Dumper_Dumpxs(href, ...)
664 AV *postav, *todumpav, *namesav;
666 I32 indent, terse, useqq, i, imax, postlen;
668 SV *val, *name, *pad, *xpad, *apad, *sep, *tmp, *varname;
669 SV *freezer, *toaster, *bless;
670 I32 purity, deepcopy, quotekeys;
674 if (!SvROK(href)) { /* call new to get an object first */
683 croak("Usage: Data::Dumper::Dumpxs(PACKAGE, VAL_ARY_REF, NAME_ARY_REF)");
690 XPUSHs(sv_2mortal(newSVsv(valarray)));
691 XPUSHs(sv_2mortal(newSVsv(namearray)));
693 i = perl_call_method("new", G_SCALAR);
696 href = newSVsv(POPs);
702 (void)sv_2mortal(href);
705 todumpav = namesav = Nullav;
707 val = pad = xpad = apad = sep = tmp = varname
708 = freezer = toaster = bless = &PL_sv_undef;
709 name = sv_newmortal();
711 terse = useqq = purity = deepcopy = 0;
714 retval = newSVpvn("", 0);
716 && (hv = (HV*)SvRV((SV*)href))
717 && SvTYPE(hv) == SVt_PVHV) {
719 if ((svp = hv_fetch(hv, "seen", 4, FALSE)) && SvROK(*svp))
720 seenhv = (HV*)SvRV(*svp);
721 if ((svp = hv_fetch(hv, "todump", 6, FALSE)) && SvROK(*svp))
722 todumpav = (AV*)SvRV(*svp);
723 if ((svp = hv_fetch(hv, "names", 5, FALSE)) && SvROK(*svp))
724 namesav = (AV*)SvRV(*svp);
725 if ((svp = hv_fetch(hv, "indent", 6, FALSE)))
727 if ((svp = hv_fetch(hv, "purity", 6, FALSE)))
729 if ((svp = hv_fetch(hv, "terse", 5, FALSE)))
730 terse = SvTRUE(*svp);
731 if ((svp = hv_fetch(hv, "useqq", 5, FALSE)))
732 useqq = SvTRUE(*svp);
733 if ((svp = hv_fetch(hv, "pad", 3, FALSE)))
735 if ((svp = hv_fetch(hv, "xpad", 4, FALSE)))
737 if ((svp = hv_fetch(hv, "apad", 4, FALSE)))
739 if ((svp = hv_fetch(hv, "sep", 3, FALSE)))
741 if ((svp = hv_fetch(hv, "varname", 7, FALSE)))
743 if ((svp = hv_fetch(hv, "freezer", 7, FALSE)))
745 if ((svp = hv_fetch(hv, "toaster", 7, FALSE)))
747 if ((svp = hv_fetch(hv, "deepcopy", 8, FALSE)))
748 deepcopy = SvTRUE(*svp);
749 if ((svp = hv_fetch(hv, "quotekeys", 9, FALSE)))
750 quotekeys = SvTRUE(*svp);
751 if ((svp = hv_fetch(hv, "bless", 5, FALSE)))
756 imax = av_len(todumpav);
759 valstr = newSVpvn("",0);
760 for (i = 0; i <= imax; ++i) {
764 if ((svp = av_fetch(todumpav, i, FALSE)))
768 if ((svp = av_fetch(namesav, i, TRUE)))
769 sv_setsv(name, *svp);
774 if ((SvPVX(name))[0] == '*') {
776 switch (SvTYPE(SvRV(val))) {
778 (SvPVX(name))[0] = '@';
781 (SvPVX(name))[0] = '%';
784 (SvPVX(name))[0] = '*';
787 (SvPVX(name))[0] = '$';
792 (SvPVX(name))[0] = '$';
794 else if ((SvPVX(name))[0] != '$')
795 sv_insert(name, 0, 0, "$", 1);
799 sv_setpvn(name, "$", 1);
800 sv_catsv(name, varname);
801 (void) sprintf(tmpbuf, "%ld", i+1);
802 nchars = strlen(tmpbuf);
803 sv_catpvn(name, tmpbuf, nchars);
807 SV *tmpsv = sv_x(Nullsv, " ", 1, SvCUR(name)+3);
808 newapad = newSVsv(apad);
809 sv_catsv(newapad, tmpsv);
815 DD_dump(val, SvPVX(name), SvCUR(name), valstr, seenhv,
816 postav, &level, indent, pad, xpad, newapad, sep,
817 freezer, toaster, purity, deepcopy, quotekeys,
821 SvREFCNT_dec(newapad);
823 postlen = av_len(postav);
824 if (postlen >= 0 || !terse) {
825 sv_insert(valstr, 0, 0, " = ", 3);
826 sv_insert(valstr, 0, 0, SvPVX(name), SvCUR(name));
827 sv_catpvn(valstr, ";", 1);
829 sv_catsv(retval, pad);
830 sv_catsv(retval, valstr);
831 sv_catsv(retval, sep);
834 sv_catsv(retval, pad);
835 for (i = 0; i <= postlen; ++i) {
837 svp = av_fetch(postav, i, FALSE);
838 if (svp && (elem = *svp)) {
839 sv_catsv(retval, elem);
841 sv_catpvn(retval, ";", 1);
842 sv_catsv(retval, sep);
843 sv_catsv(retval, pad);
847 sv_catpvn(retval, ";", 1);
848 sv_catsv(retval, sep);
850 sv_setpvn(valstr, "", 0);
851 if (gimme == G_ARRAY) {
852 XPUSHs(sv_2mortal(retval));
853 if (i < imax) /* not the last time thro ? */
854 retval = newSVpvn("",0);
857 SvREFCNT_dec(postav);
858 SvREFCNT_dec(valstr);
861 croak("Call to new() method failed to return HASH ref");
862 if (gimme == G_SCALAR)
863 XPUSHs(sv_2mortal(retval));