-#!/usr/bin/perl
+#!/usr/bin/perl -w
-open(EM, ">embed.h") || die "Can't create embed.h: $!\n";
+require 5.003;
+
+# XXX others that may need adding
+# warnhook
+# hints
+# copline
+my @extvars = qw(sv_undef sv_yes sv_no na dowarn
+ curcop compiling
+ tainting tainted stack_base stack_sp sv_arenaroot
+ curstash DBsub DBsingle debstash
+ rsfp
+ stdingv
+ defgv
+ errgv
+ rsfp_filters
+ perldb
+ diehook
+ dirty
+ perl_destruct_level
+ );
+
+sub readsyms (\%$) {
+ my ($syms, $file) = @_;
+ %$syms = ();
+ local (*FILE, $_);
+ open(FILE, "< $file")
+ or die "embed.pl: Can't open $file: $!\n";
+ while (<FILE>) {
+ s/[ \t]*#.*//; # Delete comments.
+ if (/^\s*(\S+)\s*$/) {
+ $$syms{$1} = 1;
+ }
+ }
+ close(FILE);
+}
+
+readsyms %global, 'global.sym';
+
+sub readvars(\%$$) {
+ my ($syms, $file,$pre) = @_;
+ %$syms = ();
+ local (*FILE, $_);
+ open(FILE, "< $file")
+ or die "embed.pl: Can't open $file: $!\n";
+ while (<FILE>) {
+ s/[ \t]*#.*//; # Delete comments.
+ if (/PERLVARI?C?\($pre(\w+)/) {
+ $$syms{$1} = 1;
+ }
+ }
+ close(FILE);
+}
+
+my %intrp;
+my %thread;
+
+readvars %intrp, 'intrpvar.h','I';
+readvars %thread, 'thrdvar.h','T';
+readvars %globvar, 'perlvars.h','G';
+
+foreach my $sym (sort keys %intrp)
+ {
+ if (exists $global{$sym})
+ {
+ delete $global{$sym};
+ warn "$sym in global.sym as well as intrpvar.h\n";
+ }
+ }
+
+foreach my $sym (sort keys %globvar)
+ {
+ if (exists $global{$sym})
+ {
+ delete $global{$sym};
+ warn "$sym in global.sym as well as perlvars.h\n";
+ }
+ }
+
+foreach my $sym (sort keys %thread)
+ {
+ warn "$sym in intrpvar.h as well as thrdvar.h\n" if exists $intrp{$sym};
+ if (exists $global{$sym})
+ {
+ delete $global{$sym};
+ warn "$sym in global.sym as well as thrdvar.h\n";
+ }
+ }
+
+sub hide ($$) {
+ my ($from, $to) = @_;
+ my $t = int(length($from) / 8);
+ "#define $from" . "\t" x ($t < 3 ? 3 - $t : 1) . "$to\n";
+}
+sub embed ($) {
+ my ($sym) = @_;
+ hide($sym, "Perl_$sym");
+}
+sub embedvar ($) {
+ my ($sym) = @_;
+# hide($sym, "Perl_$sym");
+ return '';
+}
+
+sub multon ($$$) {
+ my ($sym,$pre,$ptr) = @_;
+ hide("PL_$sym", "($ptr$pre$sym)");
+}
+sub multoff ($$) {
+ my ($sym,$pre) = @_;
+ return hide("PL_$pre$sym", "PL_$sym");
+}
+
+unlink 'embed.h';
+open(EM, '> embed.h')
+ or die "Can't create embed.h: $!\n";
print EM <<'END';
/* !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
- This file is built by embed.pl from global.sym and interp.sym.
- Any changes made here will be lost
+ This file is built by embed.pl from global.sym, intrpvar.h,
+ and thrdvar.h. Any changes made here will be lost!
*/
/* (Doing namespace management portably in C is really gross.) */
# define EMBED 1
#endif
+/* Hide global symbols? */
+
#ifdef EMBED
-/* globals we need to hide from the world */
END
-open(GL, "<global.sym") || die "Can't open global.sym: $!\n";
+for $sym (sort keys %global) {
+ print EM embed($sym);
+}
-while(<GL>) {
- s/[ \t]*#.*//; # Delete comments.
- next unless /\S/;
- s/^\s*(\S+).*$/#define $1\t\tPerl_$1/;
- $global{$1} = 1;
- s/(................\t)\t/$1/;
- print EM $_;
+print EM <<'END';
+
+#endif /* EMBED */
+
+END
+
+close(EM);
+
+unlink 'embedvar.h';
+open(EM, '> embedvar.h')
+ or die "Can't create embedvar.h: $!\n";
+
+print EM <<'END';
+/* !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
+ This file is built by embed.pl from global.sym, intrpvar.h,
+ and thrdvar.h. Any changes made here will be lost!
+*/
+
+/* (Doing namespace management portably in C is really gross.) */
+
+/* EMBED has no run-time penalty, but helps keep the Perl namespace
+ from colliding with that used by other libraries pulled in
+ by extensions or by embedding perl. Allow a cc -DNO_EMBED
+ override, however, to keep binary compatability with previous
+ versions of perl.
+*/
+
+
+/* Put interpreter-specific symbols into a struct? */
+
+#ifdef MULTIPLICITY
+
+#ifndef USE_THREADS
+/* If we do not have threads then per-thread vars are per-interpreter */
+
+END
+
+for $sym (sort keys %thread) {
+ print EM multon($sym,'T','PL_curinterp->');
+}
+
+print EM <<'END';
+
+#endif /* !USE_THREADS */
+
+/* These are always per-interpreter if there is more than one */
+
+END
+
+for $sym (sort keys %intrp) {
+ print EM multon($sym,'I','PL_curinterp->');
}
-close(GL) || warn "Can't close global.sym: $!\n";
+print EM <<'END';
+
+#else /* !MULTIPLICITY */
+
+END
+
+for $sym (sort keys %intrp) {
+ print EM multoff($sym,'I');
+}
+
+print EM <<'END';
+
+#ifndef USE_THREADS
+
+END
+
+for $sym (sort keys %thread) {
+ print EM multoff($sym,'T');
+}
+
+print EM <<'END';
+
+#endif /* USE_THREADS */
+
+/* Hide what would have been interpreter-specific symbols? */
+
+#ifdef EMBED
+
+END
+
+for $sym (sort keys %intrp) {
+ print EM embedvar($sym);
+}
+
+print EM <<'END';
+
+#ifndef USE_THREADS
+
+END
+
+for $sym (sort keys %thread) {
+ print EM embedvar($sym);
+}
print EM <<'END';
+#endif /* USE_THREADS */
#endif /* EMBED */
+#endif /* MULTIPLICITY */
-/* Put interpreter specific symbols into a struct? */
+/* Now same trickey for per-thread variables */
-#ifdef MULTIPLICITY
+#ifdef USE_THREADS
END
-open(INT, "<interp.sym") || die "Can't open interp.sym: $!\n";
-while (<INT>) {
- s/[ \t]*#.*//; # Delete comments.
- next unless /\S/;
- s/^\s*(\S+).*$/#define $1\t\t(curinterp->I$1)/;
- s/(................\t)\t/$1/;
- print EM $_;
+for $sym (sort keys %thread) {
+ print EM multon($sym,'T','thr->');
}
-close(INT) || warn "Can't close interp.sym: $!\n";
print EM <<'END';
-#else /* not multiple, so translate interpreter symbols the other way... */
+#endif /* USE_THREADS */
+
+#ifdef PERL_GLOBAL_STRUCT
END
-open(INT, "<interp.sym") || die "Can't open interp.sym: $!\n";
-while (<INT>) {
- s/[ \t]*#.*//; # Delete comments.
- next unless /\S/;
- s/^\s*(\S+).*$/#define I$1\t\t$1/;
- s/(................\t)\t/$1/;
- print EM $_;
+for $sym (sort keys %globvar) {
+ print EM multon($sym,'G','PL_Vars.');
+}
+
+print EM <<'END';
+
+#else /* !PERL_GLOBAL_STRUCT */
+
+END
+
+for $sym (sort keys %globvar) {
+ print EM multoff($sym,'G');
}
-close(INT) || warn "Can't close interp.sym: $!\n";
print EM <<'END';
END
-open(INT, "<interp.sym") || die "Can't open interp.sym: $!\n";
-while (<INT>) {
- s/[ \t]*#.*//; # Delete comments.
- next unless /\S/;
- s/^\s*(\S+).*$/#define $1\t\tPerl_$1/;
- s/(................\t)\t/$1/;
- print EM $_;
+for $sym (sort keys %globvar) {
+ print EM embedvar($sym);
}
-close(INT) || warn "Can't close interp.sym: $!\n";
print EM <<'END';
#endif /* EMBED */
+#endif /* PERL_GLOBAL_STRUCT */
+
+END
+
+print EM <<'END';
+
+#if 0 /* ndef MIN_PERL_DEFINE */ /* unsupported in 5.006 */
-#endif /* MULTIPLICITY */
END
+for $sym (sort @extvars) {
+ print EM hide($sym,"PL_$sym");
+}
+
+print EM <<'END';
+
+#endif /* MIN_PERL_DEFINE */
+END
+
+
+close(EM);