mkdef.pl revision 120631
155714Skris#!/usr/local/bin/perl -w
255714Skris#
355714Skris# generate a .def file
455714Skris#
555714Skris# It does this by parsing the header files and looking for the
655714Skris# prototyped functions: it then prunes the output.
755714Skris#
868651Skris# Intermediary files are created, call libeay.num and ssleay.num,...
968651Skris# Previously, they had the following format:
1068651Skris#
1168651Skris#	routine-name	nnnn
1268651Skris#
1368651Skris# But that isn't enough for a number of reasons, the first on being that
1468651Skris# this format is (needlessly) very Win32-centric, and even then...
1568651Skris# One of the biggest problems is that there's no information about what
1668651Skris# routines should actually be used, which varies with what crypto algorithms
1768651Skris# are disabled.  Also, some operating systems (for example VMS with VAX C)
1868651Skris# need to keep track of the global variables as well as the functions.
1968651Skris#
2068651Skris# So, a remake of this script is done so as to include information on the
2168651Skris# kind of symbol it is (function or variable) and what algorithms they're
2268651Skris# part of.  This will allow easy translating to .def files or the corresponding
2368651Skris# file in other operating systems (a .opt file for VMS, possibly with a .mar
2468651Skris# file).
2568651Skris#
2668651Skris# The format now becomes:
2768651Skris#
2868651Skris#	routine-name	nnnn	info
2968651Skris#
3068651Skris# and the "info" part is actually a colon-separated string of fields with
3168651Skris# the following meaning:
3268651Skris#
3368651Skris#	existence:platform:kind:algorithms
3468651Skris#
3568651Skris# - "existence" can be "EXIST" or "NOEXIST" depending on if the symbol is
3668651Skris#   found somewhere in the source,
3768651Skris# - "platforms" is empty if it exists on all platforms, otherwise it contains
3868651Skris#   comma-separated list of the platform, just as they are if the symbol exists
3968651Skris#   for those platforms, or prepended with a "!" if not.  This helps resolve
40109998Smarkm#   symbol name variants for platforms where the names are too long for the
4168651Skris#   compiler or linker, or if the systems is case insensitive and there is a
42109998Smarkm#   clash, or the symbol is implemented differently (see
43109998Smarkm#   EXPORT_VAR_AS_FUNCTION).  This script assumes renaming of symbols is found
44109998Smarkm#   in the file crypto/symhacks.h.
45109998Smarkm#   The semantics for the platforms is that every item is checked against the
46109998Smarkm#   environment.  For the negative items ("!FOO"), if any of them is false
47109998Smarkm#   (i.e. "FOO" is true) in the environment, the corresponding symbol can't be
48109998Smarkm#   used.  For the positive itms, if all of them are false in the environment,
49109998Smarkm#   the corresponding symbol can't be used.  Any combination of positive and
50109998Smarkm#   negative items are possible, and of course leave room for some redundancy.
5168651Skris# - "kind" is "FUNCTION" or "VARIABLE".  The meaning of that is obvious.
5268651Skris# - "algorithms" is a comma-separated list of algorithm names.  This helps
5368651Skris#   exclude symbols that are part of an algorithm that some user wants to
5468651Skris#   exclude.
5568651Skris#
5655714Skris
57109998Smarkmmy $debug=0;
58109998Smarkm
5968651Skrismy $crypto_num= "util/libeay.num";
6068651Skrismy $ssl_num=    "util/ssleay.num";
61109998Smarkmmy $libname;
6255714Skris
6355714Skrismy $do_update = 0;
64109998Smarkmmy $do_rewrite = 1;
6555714Skrismy $do_crypto = 0;
6655714Skrismy $do_ssl = 0;
6759191Skrismy $do_ctest = 0;
6868651Skrismy $do_ctestall = 0;
69109998Smarkmmy $do_checkexist = 0;
7055714Skris
71109998Smarkmmy $VMSVAX=0;
72109998Smarkmmy $VMSAlpha=0;
7368651Skrismy $VMS=0;
7468651Skrismy $W32=0;
7568651Skrismy $W16=0;
7659191Skrismy $NT=0;
77109998Smarkmmy $OS2=0;
7855714Skris# Set this to make typesafe STACK definitions appear in DEF
7968651Skrismy $safe_stack_def = 0;
8055714Skris
81109998Smarkmmy @known_platforms = ( "__FreeBSD__", "PERL5", "NeXT",
82109998Smarkm			"EXPORT_VAR_AS_FUNCTION" );
83109998Smarkmmy @known_ossl_platforms = ( "VMS", "WIN16", "WIN32", "WINNT", "OS2" );
8468651Skrismy @known_algorithms = ( "RC2", "RC4", "RC5", "IDEA", "DES", "BF",
85109998Smarkm			 "CAST", "MD2", "MD4", "MD5", "SHA", "SHA0", "SHA1",
86109998Smarkm			 "RIPEMD",
87109998Smarkm			 "MDC2", "RSA", "DSA", "DH", "EC", "HMAC", "AES",
88109998Smarkm			 # Envelope "algorithms"
89109998Smarkm			 "EVP", "X509", "ASN1_TYPEDEFS",
90109998Smarkm			 # Helper "algorithms"
91109998Smarkm			 "BIO", "COMP", "BUFFER", "LHASH", "STACK", "ERR",
92109998Smarkm			 "LOCKING",
93109998Smarkm			 # External "algorithms"
94111147Snectar			 "FP_API", "STDIO", "SOCK", "KRB5", "ENGINE", "HW" );
9568651Skris
9659191Skrismy $options="";
9755714Skrisopen(IN,"<Makefile.ssl") || die "unable to open Makefile.ssl!\n";
9855714Skriswhile(<IN>) {
9955714Skris    $options=$1 if (/^OPTIONS=(.*)$/);
10055714Skris}
10155714Skrisclose(IN);
10255714Skris
10359191Skris# The following ciphers may be excluded (by Configure). This means functions
10459191Skris# defined with ifndef(NO_XXX) are not included in the .def file, and everything
10559191Skris# in directory xxx is ignored.
10659191Skrismy $no_rc2; my $no_rc4; my $no_rc5; my $no_idea; my $no_des; my $no_bf;
10768651Skrismy $no_cast;
10868651Skrismy $no_md2; my $no_md4; my $no_md5; my $no_sha; my $no_ripemd; my $no_mdc2;
109109998Smarkmmy $no_rsa; my $no_dsa; my $no_dh; my $no_hmac=0; my $no_aes; my $no_krb5;
110111147Snectarmy $no_ec; my $no_engine; my $no_hw;
11168651Skrismy $no_fp_api;
11259191Skris
11355714Skrisforeach (@ARGV, split(/ /, $options))
11455714Skris	{
115109998Smarkm	$debug=1 if $_ eq "debug";
11655714Skris	$W32=1 if $_ eq "32";
11768651Skris	$W16=1 if $_ eq "16";
11855714Skris	if($_ eq "NT") {
11955714Skris		$W32 = 1;
12055714Skris		$NT = 1;
12155714Skris	}
122109998Smarkm	if ($_ eq "VMS-VAX") {
123109998Smarkm		$VMS=1;
124109998Smarkm		$VMSVAX=1;
125109998Smarkm	}
126109998Smarkm	if ($_ eq "VMS-Alpha") {
127109998Smarkm		$VMS=1;
128109998Smarkm		$VMSAlpha=1;
129109998Smarkm	}
13068651Skris	$VMS=1 if $_ eq "VMS";
131109998Smarkm	$OS2=1 if $_ eq "OS2";
13268651Skris
13355714Skris	$do_ssl=1 if $_ eq "ssleay";
134109998Smarkm	if ($_ eq "ssl") {
135109998Smarkm		$do_ssl=1;
136109998Smarkm		$libname=$_
137109998Smarkm	}
13855714Skris	$do_crypto=1 if $_ eq "libeay";
139109998Smarkm	if ($_ eq "crypto") {
140109998Smarkm		$do_crypto=1;
141109998Smarkm		$libname=$_;
142109998Smarkm	}
14355714Skris	$do_update=1 if $_ eq "update";
14468651Skris	$do_rewrite=1 if $_ eq "rewrite";
14559191Skris	$do_ctest=1 if $_ eq "ctest";
14668651Skris	$do_ctestall=1 if $_ eq "ctestall";
147109998Smarkm	$do_checkexist=1 if $_ eq "exist";
14868651Skris	#$safe_stack_def=1 if $_ eq "-DDEBUG_SAFESTACK";
14955714Skris
15055714Skris	if    (/^no-rc2$/)      { $no_rc2=1; }
15155714Skris	elsif (/^no-rc4$/)      { $no_rc4=1; }
15255714Skris	elsif (/^no-rc5$/)      { $no_rc5=1; }
15355714Skris	elsif (/^no-idea$/)     { $no_idea=1; }
15472613Skris	elsif (/^no-des$/)      { $no_des=1; $no_mdc2=1; }
15555714Skris	elsif (/^no-bf$/)       { $no_bf=1; }
15655714Skris	elsif (/^no-cast$/)     { $no_cast=1; }
15755714Skris	elsif (/^no-md2$/)      { $no_md2=1; }
15868651Skris	elsif (/^no-md4$/)      { $no_md4=1; }
15955714Skris	elsif (/^no-md5$/)      { $no_md5=1; }
16055714Skris	elsif (/^no-sha$/)      { $no_sha=1; }
16155714Skris	elsif (/^no-ripemd$/)   { $no_ripemd=1; }
16255714Skris	elsif (/^no-mdc2$/)     { $no_mdc2=1; }
16355714Skris	elsif (/^no-rsa$/)      { $no_rsa=1; }
16455714Skris	elsif (/^no-dsa$/)      { $no_dsa=1; }
16555714Skris	elsif (/^no-dh$/)       { $no_dh=1; }
166109998Smarkm	elsif (/^no-ec$/)       { $no_ec=1; }
16755714Skris	elsif (/^no-hmac$/)	{ $no_hmac=1; }
168109998Smarkm	elsif (/^no-aes$/)	{ $no_aes=1; }
169109998Smarkm	elsif (/^no-evp$/)	{ $no_evp=1; }
170109998Smarkm	elsif (/^no-lhash$/)	{ $no_lhash=1; }
171109998Smarkm	elsif (/^no-stack$/)	{ $no_stack=1; }
172109998Smarkm	elsif (/^no-err$/)	{ $no_err=1; }
173109998Smarkm	elsif (/^no-buffer$/)	{ $no_buffer=1; }
174109998Smarkm	elsif (/^no-bio$/)	{ $no_bio=1; }
175109998Smarkm	#elsif (/^no-locking$/)	{ $no_locking=1; }
176109998Smarkm	elsif (/^no-comp$/)	{ $no_comp=1; }
177109998Smarkm	elsif (/^no-dso$/)	{ $no_dso=1; }
178109998Smarkm	elsif (/^no-krb5$/)	{ $no_krb5=1; }
179111147Snectar	elsif (/^no-engine$/)	{ $no_engine=1; }
180111147Snectar	elsif (/^no-hw$/)	{ $no_hw=1; }
18155714Skris	}
18255714Skris
18359191Skris
184109998Smarkmif (!$libname) {
185109998Smarkm	if ($do_ssl) {
186109998Smarkm		$libname="SSLEAY";
187109998Smarkm	}
188109998Smarkm	if ($do_crypto) {
189109998Smarkm		$libname="LIBEAY";
190109998Smarkm	}
191109998Smarkm}
192109998Smarkm
19368651Skris# If no platform is given, assume WIN32
194109998Smarkmif ($W32 + $W16 + $VMS + $OS2 == 0) {
19568651Skris	$W32 = 1;
19668651Skris}
19768651Skris
19868651Skris# Add extra knowledge
19968651Skrisif ($W16) {
20068651Skris	$no_fp_api=1;
20168651Skris}
20268651Skris
20355714Skrisif (!$do_ssl && !$do_crypto)
20455714Skris	{
205109998Smarkm	print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT | OS2 ]\n";
20655714Skris	exit(1);
20755714Skris	}
20855714Skris
20955714Skris%ssl_list=&load_numbers($ssl_num);
21055714Skris$max_ssl = $max_num;
21155714Skris%crypto_list=&load_numbers($crypto_num);
21255714Skris$max_crypto = $max_num;
21355714Skris
21459191Skrismy $ssl="ssl/ssl.h";
215109998Smarkm$ssl.=" ssl/kssl.h";
21655714Skris
21759191Skrismy $crypto ="crypto/crypto.h";
218109998Smarkm$crypto.=" crypto/des/des.h crypto/des/des_old.h" ; # unless $no_des;
219109998Smarkm$crypto.=" crypto/idea/idea.h" ; # unless $no_idea;
220109998Smarkm$crypto.=" crypto/rc4/rc4.h" ; # unless $no_rc4;
221109998Smarkm$crypto.=" crypto/rc5/rc5.h" ; # unless $no_rc5;
222109998Smarkm$crypto.=" crypto/rc2/rc2.h" ; # unless $no_rc2;
223109998Smarkm$crypto.=" crypto/bf/blowfish.h" ; # unless $no_bf;
224109998Smarkm$crypto.=" crypto/cast/cast.h" ; # unless $no_cast;
225109998Smarkm$crypto.=" crypto/md2/md2.h" ; # unless $no_md2;
226109998Smarkm$crypto.=" crypto/md4/md4.h" ; # unless $no_md4;
227109998Smarkm$crypto.=" crypto/md5/md5.h" ; # unless $no_md5;
228109998Smarkm$crypto.=" crypto/mdc2/mdc2.h" ; # unless $no_mdc2;
229109998Smarkm$crypto.=" crypto/sha/sha.h" ; # unless $no_sha;
230109998Smarkm$crypto.=" crypto/ripemd/ripemd.h" ; # unless $no_ripemd;
231109998Smarkm$crypto.=" crypto/aes/aes.h" ; # unless $no_aes;
23255714Skris
23355714Skris$crypto.=" crypto/bn/bn.h";
234109998Smarkm$crypto.=" crypto/rsa/rsa.h" ; # unless $no_rsa;
235109998Smarkm$crypto.=" crypto/dsa/dsa.h" ; # unless $no_dsa;
236109998Smarkm$crypto.=" crypto/dh/dh.h" ; # unless $no_dh;
237109998Smarkm$crypto.=" crypto/ec/ec.h" ; # unless $no_ec;
238109998Smarkm$crypto.=" crypto/hmac/hmac.h" ; # unless $no_hmac;
23955714Skris
240111147Snectar$crypto.=" crypto/engine/engine.h"; # unless $no_engine;
241109998Smarkm$crypto.=" crypto/stack/stack.h" ; # unless $no_stack;
242109998Smarkm$crypto.=" crypto/buffer/buffer.h" ; # unless $no_buffer;
243109998Smarkm$crypto.=" crypto/bio/bio.h" ; # unless $no_bio;
244109998Smarkm$crypto.=" crypto/dso/dso.h" ; # unless $no_dso;
245109998Smarkm$crypto.=" crypto/lhash/lhash.h" ; # unless $no_lhash;
24655714Skris$crypto.=" crypto/conf/conf.h";
24755714Skris$crypto.=" crypto/txt_db/txt_db.h";
24855714Skris
249109998Smarkm$crypto.=" crypto/evp/evp.h" ; # unless $no_evp;
25055714Skris$crypto.=" crypto/objects/objects.h";
25155714Skris$crypto.=" crypto/pem/pem.h";
25255714Skris#$crypto.=" crypto/meth/meth.h";
25355714Skris$crypto.=" crypto/asn1/asn1.h";
254109998Smarkm$crypto.=" crypto/asn1/asn1t.h";
25555714Skris$crypto.=" crypto/asn1/asn1_mac.h";
256109998Smarkm$crypto.=" crypto/err/err.h" ; # unless $no_err;
25755714Skris$crypto.=" crypto/pkcs7/pkcs7.h";
25855714Skris$crypto.=" crypto/pkcs12/pkcs12.h";
25955714Skris$crypto.=" crypto/x509/x509.h";
26055714Skris$crypto.=" crypto/x509/x509_vfy.h";
26155714Skris$crypto.=" crypto/x509v3/x509v3.h";
26255714Skris$crypto.=" crypto/rand/rand.h";
263109998Smarkm$crypto.=" crypto/comp/comp.h" ; # unless $no_comp;
264109998Smarkm$crypto.=" crypto/ocsp/ocsp.h";
265109998Smarkm$crypto.=" crypto/ui/ui.h crypto/ui/ui_compat.h";
266109998Smarkm$crypto.=" crypto/krb5/krb5_asn.h";
26755714Skris$crypto.=" crypto/tmdiff.h";
26855714Skris
26968651Skrismy $symhacks="crypto/symhacks.h";
27055714Skris
27168651Skrismy @ssl_symbols = &do_defs("SSLEAY", $ssl, $symhacks);
27268651Skrismy @crypto_symbols = &do_defs("LIBEAY", $crypto, $symhacks);
27355714Skris
27455714Skrisif ($do_update) {
27555714Skris
27655714Skrisif ($do_ssl == 1) {
27768651Skris
27868651Skris	&maybe_add_info("SSLEAY",*ssl_list,@ssl_symbols);
27968651Skris	if ($do_rewrite == 1) {
28068651Skris		open(OUT, ">$ssl_num");
28168651Skris		&rewrite_numbers(*OUT,"SSLEAY",*ssl_list,@ssl_symbols);
28268651Skris	} else {
28368651Skris		open(OUT, ">>$ssl_num");
28468651Skris	}
28568651Skris	&update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl,@ssl_symbols);
28655714Skris	close OUT;
28755714Skris}
28855714Skris
28955714Skrisif($do_crypto == 1) {
29068651Skris
29168651Skris	&maybe_add_info("LIBEAY",*crypto_list,@crypto_symbols);
29268651Skris	if ($do_rewrite == 1) {
29368651Skris		open(OUT, ">$crypto_num");
29468651Skris		&rewrite_numbers(*OUT,"LIBEAY",*crypto_list,@crypto_symbols);
29568651Skris	} else {
29668651Skris		open(OUT, ">>$crypto_num");
29768651Skris	}
29868651Skris	&update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto,@crypto_symbols);
29955714Skris	close OUT;
30059191Skris}
30155714Skris
302109998Smarkm} elsif ($do_checkexist) {
303109998Smarkm	&check_existing(*ssl_list, @ssl_symbols)
304109998Smarkm		if $do_ssl == 1;
305109998Smarkm	&check_existing(*crypto_list, @crypto_symbols)
306109998Smarkm		if $do_crypto == 1;
30768651Skris} elsif ($do_ctest || $do_ctestall) {
30859191Skris
30959191Skris	print <<"EOF";
31059191Skris
31159191Skris/* Test file to check all DEF file symbols are present by trying
31259191Skris * to link to all of them. This is *not* intended to be run!
31359191Skris */
31459191Skris
31559191Skrisint main()
31659191Skris{
31759191SkrisEOF
31868651Skris	&print_test_file(*STDOUT,"SSLEAY",*ssl_list,$do_ctestall,@ssl_symbols)
31959191Skris		if $do_ssl == 1;
32059191Skris
32168651Skris	&print_test_file(*STDOUT,"LIBEAY",*crypto_list,$do_ctestall,@crypto_symbols)
32259191Skris		if $do_crypto == 1;
32359191Skris
32459191Skris	print "}\n";
32559191Skris
32655714Skris} else {
32755714Skris
328109998Smarkm	&print_def_file(*STDOUT,$libname,*ssl_list,@ssl_symbols)
32955714Skris		if $do_ssl == 1;
33055714Skris
331109998Smarkm	&print_def_file(*STDOUT,$libname,*crypto_list,@crypto_symbols)
33255714Skris		if $do_crypto == 1;
33355714Skris
33455714Skris}
33555714Skris
33655714Skris
33755714Skrissub do_defs
33855714Skris{
33968651Skris	my($name,$files,$symhacksfile)=@_;
34059191Skris	my $file;
34155714Skris	my @ret;
34268651Skris	my %syms;
34368651Skris	my %platform;		# For anything undefined, we assume ""
34468651Skris	my %kind;		# For anything undefined, we assume "FUNCTION"
34568651Skris	my %algorithm;		# For anything undefined, we assume ""
346109998Smarkm	my %variant;
347109998Smarkm	my %variant_cnt;	# To be able to allocate "name{n}" if "name"
348109998Smarkm				# is the same name as the original.
34959191Skris	my $cpp;
350109998Smarkm	my %unknown_algorithms = ();
35155714Skris
35268651Skris	foreach $file (split(/\s+/,$symhacksfile." ".$files))
35355714Skris		{
354109998Smarkm		print STDERR "DEBUG: starting on $file:\n" if $debug;
35555714Skris		open(IN,"<$file") || die "unable to open $file:$!\n";
35659191Skris		my $line = "", my $def= "";
35755714Skris		my %tag = (
35868651Skris			(map { $_ => 0 } @known_platforms),
359109998Smarkm			(map { "OPENSSL_SYS_".$_ => 0 } @known_ossl_platforms),
360109998Smarkm			(map { "OPENSSL_NO_".$_ => 0 } @known_algorithms),
36155714Skris			NOPROTO		=> 0,
36255714Skris			PERL5		=> 0,
36355714Skris			_WINDLL		=> 0,
36455714Skris			CONST_STRICT	=> 0,
36555714Skris			TRUE		=> 1,
36655714Skris		);
36768651Skris		my $symhacking = $file eq $symhacksfile;
368109998Smarkm		my @current_platforms = ();
369109998Smarkm		my @current_algorithms = ();
370109998Smarkm
371109998Smarkm		# params: symbol, alias, platforms, kind
372109998Smarkm		# The reason to put this subroutine in a variable is that
373109998Smarkm		# it will otherwise create it's own, unshared, version of
374109998Smarkm		# %tag and %variant...
375109998Smarkm		my $make_variant = sub
376109998Smarkm		{
377109998Smarkm			my ($s, $a, $p, $k) = @_;
378109998Smarkm			my ($a1, $a2);
379109998Smarkm
380109998Smarkm			print STDERR "DEBUG: make_variant: Entered with ",$s,", ",$a,", ",(defined($p)?$p:""),", ",(defined($k)?$k:""),"\n" if $debug;
381109998Smarkm			if (defined($p))
382109998Smarkm			{
383109998Smarkm				$a1 = join(",",$p,
384109998Smarkm					   grep(!/^$/,
385109998Smarkm						map { $tag{$_} == 1 ? $_ : "" }
386109998Smarkm						@known_platforms));
387109998Smarkm			}
388109998Smarkm			else
389109998Smarkm			{
390109998Smarkm				$a1 = join(",",
391109998Smarkm					   grep(!/^$/,
392109998Smarkm						map { $tag{$_} == 1 ? $_ : "" }
393109998Smarkm						@known_platforms));
394109998Smarkm			}
395109998Smarkm			$a2 = join(",",
396109998Smarkm				   grep(!/^$/,
397109998Smarkm					map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ : "" }
398109998Smarkm					@known_ossl_platforms));
399109998Smarkm			print STDERR "DEBUG: make_variant: a1 = $a1; a2 = $a2\n" if $debug;
400109998Smarkm			if ($a1 eq "") { $a1 = $a2; }
401109998Smarkm			elsif ($a1 ne "" && $a2 ne "") { $a1 .= ",".$a2; }
402109998Smarkm			if ($a eq $s)
403109998Smarkm			{
404109998Smarkm				if (!defined($variant_cnt{$s}))
405109998Smarkm				{
406109998Smarkm					$variant_cnt{$s} = 0;
407109998Smarkm				}
408109998Smarkm				$variant_cnt{$s}++;
409109998Smarkm				$a .= "{$variant_cnt{$s}}";
410109998Smarkm			}
411109998Smarkm			my $toadd = $a.":".$a1.(defined($k)?":".$k:"");
412109998Smarkm			my $togrep = $s.'(\{[0-9]+\})?:'.$a1.(defined($k)?":".$k:"");
413109998Smarkm			if (!grep(/^$togrep$/,
414109998Smarkm				  split(/;/, defined($variant{$s})?$variant{$s}:""))) {
415109998Smarkm				if (defined($variant{$s})) { $variant{$s} .= ";"; }
416109998Smarkm				$variant{$s} .= $toadd;
417109998Smarkm			}
418109998Smarkm			print STDERR "DEBUG: make_variant: Exit with variant of ",$s," = ",$variant{$s},"\n" if $debug;
419109998Smarkm		};
420109998Smarkm
421109998Smarkm		print STDERR "DEBUG: parsing ----------\n" if $debug;
42255714Skris		while(<IN>) {
423109998Smarkm			last if (/\/\* Error codes for the \w+ functions\. \*\//);
42455714Skris			if ($line ne '') {
42555714Skris				$_ = $line . $_;
42655714Skris				$line = '';
42755714Skris			}
42855714Skris
42955714Skris			if (/\\$/) {
430109998Smarkm				chomp; # remove eol
431109998Smarkm				chop; # remove ending backslash
43255714Skris				$line = $_;
43355714Skris				next;
43455714Skris			}
43555714Skris
43668651Skris	    		$cpp = 1 if /^\#.*ifdef.*cplusplus/;
43755714Skris			if ($cpp) {
43868651Skris				$cpp = 0 if /^\#.*endif/;
43955714Skris				next;
44055714Skris	    		}
44155714Skris
44255714Skris			s/\/\*.*?\*\///gs;                   # ignore comments
443120631Snectar			if (/\/\*/) {			     # if we have part
444120631Snectar				$line = $_;		     # of a comment,
445120631Snectar				next;			     # continue reading
446120631Snectar			}
44755714Skris			s/{[^{}]*}//gs;                      # ignore {} blocks
448120631Snectar			print STDERR "DEBUG: \$def=\"$def\"\n" if $debug && $def ne "";
449109998Smarkm			print STDERR "DEBUG: \$_=\"$_\"\n" if $debug;
450109998Smarkm			if (/^\#\s*ifndef\s+(.*)/) {
451109998Smarkm				push(@tag,"-");
45255714Skris				push(@tag,$1);
45355714Skris				$tag{$1}=-1;
454109998Smarkm				print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
455109998Smarkm			} elsif (/^\#\s*if\s+!defined\(([^\)]+)\)/) {
456109998Smarkm				push(@tag,"-");
457109998Smarkm				if (/^\#\s*if\s+(!defined\(([^\)]+)\)(\s+\&\&\s+!defined\(([^\)]+)\))*)$/) {
458109998Smarkm					my $tmp_1 = $1;
459109998Smarkm					my $tmp_;
460109998Smarkm					foreach $tmp_ (split '\&\&',$tmp_1) {
461109998Smarkm						$tmp_ =~ /!defined\(([^\)]+)\)/;
462109998Smarkm						print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
463109998Smarkm						push(@tag,$1);
464109998Smarkm						$tag{$1}=-1;
465109998Smarkm					}
466109998Smarkm				} else {
467109998Smarkm					print STDERR "Warning: $file: complicated expression: $_" if $debug; # because it is O...
468109998Smarkm					print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
469109998Smarkm					push(@tag,$1);
470109998Smarkm					$tag{$1}=-1;
471109998Smarkm				}
472109998Smarkm			} elsif (/^\#\s*ifdef\s+(.*)/) {
473109998Smarkm				push(@tag,"-");
47455714Skris				push(@tag,$1);
47555714Skris				$tag{$1}=1;
476109998Smarkm				print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
477109998Smarkm			} elsif (/^\#\s*if\s+defined\(([^\)]+)\)/) {
478109998Smarkm				push(@tag,"-");
479109998Smarkm				if (/^\#\s*if\s+(defined\(([^\)]+)\)(\s+\|\|\s+defined\(([^\)]+)\))*)$/) {
480109998Smarkm					my $tmp_1 = $1;
481109998Smarkm					my $tmp_;
482109998Smarkm					foreach $tmp_ (split '\|\|',$tmp_1) {
483109998Smarkm						$tmp_ =~ /defined\(([^\)]+)\)/;
484109998Smarkm						print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
485109998Smarkm						push(@tag,$1);
486109998Smarkm						$tag{$1}=1;
487109998Smarkm					}
488109998Smarkm				} else {
489109998Smarkm					print STDERR "Warning: $file: complicated expression: $_\n" if $debug; # because it is O...
490109998Smarkm					print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
491109998Smarkm					push(@tag,$1);
492109998Smarkm					$tag{$1}=1;
493109998Smarkm				}
49468651Skris			} elsif (/^\#\s*error\s+(\w+) is disabled\./) {
495109998Smarkm				my $tag_i = $#tag;
496109998Smarkm				while($tag[$tag_i] ne "-") {
497109998Smarkm					if ($tag[$tag_i] eq "OPENSSL_NO_".$1) {
498109998Smarkm						$tag{$tag[$tag_i]}=2;
499109998Smarkm						print STDERR "DEBUG: $file: chaged tag $1 = 2\n" if $debug;
500109998Smarkm					}
501109998Smarkm					$tag_i--;
50268651Skris				}
50355714Skris			} elsif (/^\#\s*endif/) {
504109998Smarkm				my $tag_i = $#tag;
505109998Smarkm				while($tag[$tag_i] ne "-") {
506109998Smarkm					my $t=$tag[$tag_i];
507109998Smarkm					print STDERR "DEBUG: \$t=\"$t\"\n" if $debug;
508109998Smarkm					if ($tag{$t}==2) {
509109998Smarkm						$tag{$t}=-1;
510109998Smarkm					} else {
511109998Smarkm						$tag{$t}=0;
512109998Smarkm					}
513109998Smarkm					print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
514109998Smarkm					pop(@tag);
515109998Smarkm					if ($t =~ /^OPENSSL_NO_([A-Z0-9_]+)$/) {
516109998Smarkm						$t=$1;
517109998Smarkm					} else {
518109998Smarkm						$t="";
519109998Smarkm					}
520109998Smarkm					if ($t ne ""
521109998Smarkm					    && !grep(/^$t$/, @known_algorithms)) {
522109998Smarkm						$unknown_algorithms{$t} = 1;
523109998Smarkm						#print STDERR "DEBUG: Added as unknown algorithm: $t\n" if $debug;
524109998Smarkm					}
525109998Smarkm					$tag_i--;
52668651Skris				}
52755714Skris				pop(@tag);
52855714Skris			} elsif (/^\#\s*else/) {
529109998Smarkm				my $tag_i = $#tag;
530109998Smarkm				while($tag[$tag_i] ne "-") {
531109998Smarkm					my $t=$tag[$tag_i];
532109998Smarkm					$tag{$t}= -$tag{$t};
533109998Smarkm					print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
534109998Smarkm					$tag_i--;
535109998Smarkm				}
53655714Skris			} elsif (/^\#\s*if\s+1/) {
537109998Smarkm				push(@tag,"-");
53855714Skris				# Dummy tag
53955714Skris				push(@tag,"TRUE");
54055714Skris				$tag{"TRUE"}=1;
541109998Smarkm				print STDERR "DEBUG: $file: found 1\n" if $debug;
54259191Skris			} elsif (/^\#\s*if\s+0/) {
543109998Smarkm				push(@tag,"-");
54459191Skris				# Dummy tag
54559191Skris				push(@tag,"TRUE");
54659191Skris				$tag{"TRUE"}=-1;
547109998Smarkm				print STDERR "DEBUG: $file: found 0\n" if $debug;
54868651Skris			} elsif (/^\#\s*define\s+(\w+)\s+(\w+)/
549109998Smarkm				 && $symhacking && $tag{'TRUE'} != -1) {
550109998Smarkm				# This is for aliasing.  When we find an alias,
551109998Smarkm				# we have to invert
552109998Smarkm				&$make_variant($1,$2);
553109998Smarkm				print STDERR "DEBUG: $file: defined $1 = $2\n" if $debug;
55468651Skris			}
55568651Skris			if (/^\#/) {
556109998Smarkm				@current_platforms =
557109998Smarkm				    grep(!/^$/,
558109998Smarkm					 map { $tag{$_} == 1 ? $_ :
559109998Smarkm						   $tag{$_} == -1 ? "!".$_  : "" }
560109998Smarkm					 @known_platforms);
561109998Smarkm				push @current_platforms
562109998Smarkm				    , grep(!/^$/,
563109998Smarkm					   map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ :
564109998Smarkm						     $tag{"OPENSSL_SYS_".$_} == -1 ? "!".$_  : "" }
565109998Smarkm					   @known_ossl_platforms);
566109998Smarkm				@current_algorithms =
567109998Smarkm				    grep(!/^$/,
568109998Smarkm					 map { $tag{"OPENSSL_NO_".$_} == -1 ? $_ : "" }
569109998Smarkm					 @known_algorithms);
570109998Smarkm				$def .=
571109998Smarkm				    "#INFO:"
572109998Smarkm					.join(',',@current_platforms).":"
573109998Smarkm					    .join(',',@current_algorithms).";";
57459191Skris				next;
57568651Skris			}
576109998Smarkm			if ($tag{'TRUE'} != -1) {
577109998Smarkm				if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
578109998Smarkm					next;
579109998Smarkm				} elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
580109998Smarkm					$def .= "int d2i_$3(void);";
581109998Smarkm					$def .= "int i2d_$3(void);";
582109998Smarkm					# Variant for platforms that do not
583109998Smarkm					# have to access globale variables
584109998Smarkm					# in shared libraries through functions
585109998Smarkm					$def .=
586109998Smarkm					    "#INFO:"
587109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
588109998Smarkm						    .join(',',@current_algorithms).";";
589109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
590109998Smarkm					$def .=
591109998Smarkm					    "#INFO:"
592109998Smarkm						.join(',',@current_platforms).":"
593109998Smarkm						    .join(',',@current_algorithms).";";
594109998Smarkm					# Variant for platforms that have to
595109998Smarkm					# access globale variables in shared
596109998Smarkm					# libraries through functions
597109998Smarkm					&$make_variant("$2_it","$2_it",
598109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
599109998Smarkm						      "FUNCTION");
600109998Smarkm					next;
601109998Smarkm				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS_fname\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
602109998Smarkm					$def .= "int d2i_$3(void);";
603109998Smarkm					$def .= "int i2d_$3(void);";
604109998Smarkm					$def .= "int $3_free(void);";
605109998Smarkm					$def .= "int $3_new(void);";
606109998Smarkm					# Variant for platforms that do not
607109998Smarkm					# have to access globale variables
608109998Smarkm					# in shared libraries through functions
609109998Smarkm					$def .=
610109998Smarkm					    "#INFO:"
611109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
612109998Smarkm						    .join(',',@current_algorithms).";";
613109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
614109998Smarkm					$def .=
615109998Smarkm					    "#INFO:"
616109998Smarkm						.join(',',@current_platforms).":"
617109998Smarkm						    .join(',',@current_algorithms).";";
618109998Smarkm					# Variant for platforms that have to
619109998Smarkm					# access globale variables in shared
620109998Smarkm					# libraries through functions
621109998Smarkm					&$make_variant("$2_it","$2_it",
622109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
623109998Smarkm						      "FUNCTION");
624109998Smarkm					next;
625109998Smarkm				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS\s*\(\s*(\w*)\s*\)/ ||
626109998Smarkm					 /^\s*DECLARE_ASN1_FUNCTIONS_const\s*\(\s*(\w*)\s*\)/) {
627109998Smarkm					$def .= "int d2i_$1(void);";
628109998Smarkm					$def .= "int i2d_$1(void);";
629109998Smarkm					$def .= "int $1_free(void);";
630109998Smarkm					$def .= "int $1_new(void);";
631109998Smarkm					# Variant for platforms that do not
632109998Smarkm					# have to access globale variables
633109998Smarkm					# in shared libraries through functions
634109998Smarkm					$def .=
635109998Smarkm					    "#INFO:"
636109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
637109998Smarkm						    .join(',',@current_algorithms).";";
638109998Smarkm					$def .= "OPENSSL_EXTERN int $1_it;";
639109998Smarkm					$def .=
640109998Smarkm					    "#INFO:"
641109998Smarkm						.join(',',@current_platforms).":"
642109998Smarkm						    .join(',',@current_algorithms).";";
643109998Smarkm					# Variant for platforms that have to
644109998Smarkm					# access globale variables in shared
645109998Smarkm					# libraries through functions
646109998Smarkm					&$make_variant("$1_it","$1_it",
647109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
648109998Smarkm						      "FUNCTION");
649109998Smarkm					next;
650109998Smarkm				} elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS_const\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
651109998Smarkm					$def .= "int d2i_$2(void);";
652109998Smarkm					$def .= "int i2d_$2(void);";
653109998Smarkm					# Variant for platforms that do not
654109998Smarkm					# have to access globale variables
655109998Smarkm					# in shared libraries through functions
656109998Smarkm					$def .=
657109998Smarkm					    "#INFO:"
658109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
659109998Smarkm						    .join(',',@current_algorithms).";";
660109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
661109998Smarkm					$def .=
662109998Smarkm					    "#INFO:"
663109998Smarkm						.join(',',@current_platforms).":"
664109998Smarkm						    .join(',',@current_algorithms).";";
665109998Smarkm					# Variant for platforms that have to
666109998Smarkm					# access globale variables in shared
667109998Smarkm					# libraries through functions
668109998Smarkm					&$make_variant("$2_it","$2_it",
669109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
670109998Smarkm						      "FUNCTION");
671109998Smarkm					next;
672109998Smarkm				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
673109998Smarkm					$def .= "int d2i_$2(void);";
674109998Smarkm					$def .= "int i2d_$2(void);";
675109998Smarkm					$def .= "int $2_free(void);";
676109998Smarkm					$def .= "int $2_new(void);";
677109998Smarkm					# Variant for platforms that do not
678109998Smarkm					# have to access globale variables
679109998Smarkm					# in shared libraries through functions
680109998Smarkm					$def .=
681109998Smarkm					    "#INFO:"
682109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
683109998Smarkm						    .join(',',@current_algorithms).";";
684109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
685109998Smarkm					$def .=
686109998Smarkm					    "#INFO:"
687109998Smarkm						.join(',',@current_platforms).":"
688109998Smarkm						    .join(',',@current_algorithms).";";
689109998Smarkm					# Variant for platforms that have to
690109998Smarkm					# access globale variables in shared
691109998Smarkm					# libraries through functions
692109998Smarkm					&$make_variant("$2_it","$2_it",
693109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
694109998Smarkm						      "FUNCTION");
695109998Smarkm					next;
696109998Smarkm				} elsif (/^\s*DECLARE_ASN1_ITEM\s*\(\s*(\w*)\s*\)/) {
697109998Smarkm					# Variant for platforms that do not
698109998Smarkm					# have to access globale variables
699109998Smarkm					# in shared libraries through functions
700109998Smarkm					$def .=
701109998Smarkm					    "#INFO:"
702109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
703109998Smarkm						    .join(',',@current_algorithms).";";
704109998Smarkm					$def .= "OPENSSL_EXTERN int $1_it;";
705109998Smarkm					$def .=
706109998Smarkm					    "#INFO:"
707109998Smarkm						.join(',',@current_platforms).":"
708109998Smarkm						    .join(',',@current_algorithms).";";
709109998Smarkm					# Variant for platforms that have to
710109998Smarkm					# access globale variables in shared
711109998Smarkm					# libraries through functions
712109998Smarkm					&$make_variant("$1_it","$1_it",
713109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
714109998Smarkm						      "FUNCTION");
715109998Smarkm					next;
716109998Smarkm				} elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
717109998Smarkm					next;
718109998Smarkm				} elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) {
719109998Smarkm					next;
720109998Smarkm				} elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
721109998Smarkm					 /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ) {
722109998Smarkm					# Things not in Win16
723109998Smarkm					$def .=
724109998Smarkm					    "#INFO:"
725109998Smarkm						.join(',',"!WIN16",@current_platforms).":"
726109998Smarkm						    .join(',',@current_algorithms).";";
727109998Smarkm					$def .= "int PEM_read_$1(void);";
728109998Smarkm					$def .= "int PEM_write_$1(void);";
729109998Smarkm					$def .=
730109998Smarkm					    "#INFO:"
731109998Smarkm						.join(',',@current_platforms).":"
732109998Smarkm						    .join(',',@current_algorithms).";";
733109998Smarkm					# Things that are everywhere
734109998Smarkm					$def .= "int PEM_read_bio_$1(void);";
735109998Smarkm					$def .= "int PEM_write_bio_$1(void);";
736109998Smarkm					next;
737109998Smarkm				} elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
738109998Smarkm					 /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
739109998Smarkm					# Things not in Win16
740109998Smarkm					$def .=
741109998Smarkm					    "#INFO:"
742109998Smarkm						.join(',',"!WIN16",@current_platforms).":"
743109998Smarkm						    .join(',',@current_algorithms).";";
744109998Smarkm					$def .= "int PEM_write_$1(void);";
745109998Smarkm					$def .=
746109998Smarkm					    "#INFO:"
747109998Smarkm						.join(',',@current_platforms).":"
748109998Smarkm						    .join(',',@current_algorithms).";";
749109998Smarkm					# Things that are everywhere
750109998Smarkm					$def .= "int PEM_write_bio_$1(void);";
751109998Smarkm					next;
752109998Smarkm				} elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
753109998Smarkm					 /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
754109998Smarkm					# Things not in Win16
755109998Smarkm					$def .=
756109998Smarkm					    "#INFO:"
757109998Smarkm						.join(',',"!WIN16",@current_platforms).":"
758109998Smarkm						    .join(',',@current_algorithms).";";
759109998Smarkm					$def .= "int PEM_read_$1(void);";
760109998Smarkm					$def .=
761109998Smarkm					    "#INFO:"
762109998Smarkm						.join(',',@current_platforms).":"
763109998Smarkm						    .join(',',@current_algorithms).";";
764109998Smarkm					# Things that are everywhere
765109998Smarkm					$def .= "int PEM_read_bio_$1(void);";
766109998Smarkm					next;
767109998Smarkm				} elsif (/^OPENSSL_DECLARE_GLOBAL\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
768109998Smarkm					# Variant for platforms that do not
769109998Smarkm					# have to access globale variables
770109998Smarkm					# in shared libraries through functions
771109998Smarkm					$def .=
772109998Smarkm					    "#INFO:"
773109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
774109998Smarkm						    .join(',',@current_algorithms).";";
775109998Smarkm					$def .= "OPENSSL_EXTERN int _shadow_$2;";
776109998Smarkm					$def .=
777109998Smarkm					    "#INFO:"
778109998Smarkm						.join(',',@current_platforms).":"
779109998Smarkm						    .join(',',@current_algorithms).";";
780109998Smarkm					# Variant for platforms that have to
781109998Smarkm					# access globale variables in shared
782109998Smarkm					# libraries through functions
783109998Smarkm					&$make_variant("_shadow_$2","_shadow_$2",
784109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
785109998Smarkm						      "FUNCTION");
786109998Smarkm				} elsif ($tag{'CONST_STRICT'} != 1) {
78768651Skris					if (/\{|\/\*|\([^\)]*$/) {
78855714Skris						$line = $_;
78955714Skris					} else {
79055714Skris						$def .= $_;
79155714Skris					}
79255714Skris				}
79355714Skris			}
794109998Smarkm		}
79555714Skris		close(IN);
79655714Skris
79768651Skris		my $algs;
79868651Skris		my $plays;
79968651Skris
800109998Smarkm		print STDERR "DEBUG: postprocessing ----------\n" if $debug;
80155714Skris		foreach (split /;/, $def) {
80268651Skris			my $s; my $k = "FUNCTION"; my $p; my $a;
80355714Skris			s/^[\n\s]*//g;
80455714Skris			s/[\n\s]*$//g;
80568651Skris			next if(/\#undef/);
80655714Skris			next if(/typedef\W/);
80768651Skris			next if(/\#define/);
80868651Skris
809109998Smarkm			print STDERR "DEBUG: \$_ = \"$_\"\n" if $debug;
81068651Skris			if (/^\#INFO:([^:]*):(.*)$/) {
81168651Skris				$plats = $1;
81268651Skris				$algs = $2;
813109998Smarkm				print STDERR "DEBUG: found info on platforms ($plats) and algorithms ($algs)\n" if $debug;
81468651Skris				next;
815109998Smarkm			} elsif (/^\s*OPENSSL_EXTERN\s.*?(\w+(\{[0-9]+\})?)(\[[0-9]*\])*\s*$/) {
81668651Skris				$s = $1;
81768651Skris				$k = "VARIABLE";
818109998Smarkm				print STDERR "DEBUG: found external variable $s\n" if $debug;
819109998Smarkm			} elsif (/\(\*(\w*(\{[0-9]+\})?)\([^\)]+/) {
82068651Skris				$s = $1;
821109998Smarkm				print STDERR "DEBUG: found ANSI C function $s\n" if $debug;
822120631Snectar			} elsif (/\w+\W+(\w+)\W*\(\s*\)(\s*__attribute__\(.*\)\s*)?$/s) {
82355714Skris				# K&R C
824109998Smarkm				print STDERR "DEBUG: found K&R C function $s\n" if $debug;
82555714Skris				next;
826120631Snectar			} elsif (/\w+\W+\w+(\{[0-9]+\})?\W*\(.*\)(\s*__attribute__\(.*\)\s*)?$/s) {
827120631Snectar				while (not /\(\)(\s*__attribute__\(.*\)\s*)?$/s) {
828120631Snectar					s/[^\(\)]*\)(\s*__attribute__\(.*\)\s*)?$/\)/s;
829120631Snectar					s/\([^\(\)]*\)\)(\s*__attribute__\(.*\)\s*)?$/\)/s;
83055714Skris				}
83155714Skris				s/\(void\)//;
832109998Smarkm				/(\w+(\{[0-9]+\})?)\W*\(\)/s;
83368651Skris				$s = $1;
834109998Smarkm				print STDERR "DEBUG: found function $s\n" if $debug;
83555714Skris			} elsif (/\(/ and not (/=/)) {
83655714Skris				print STDERR "File $file: cannot parse: $_;\n";
83768651Skris				next;
83868651Skris			} else {
83968651Skris				next;
84055714Skris			}
84168651Skris
84268651Skris			$syms{$s} = 1;
84368651Skris			$kind{$s} = $k;
84468651Skris
84568651Skris			$p = $plats;
84668651Skris			$a = $algs;
84768651Skris			$a .= ",BF" if($s =~ /EVP_bf/);
84868651Skris			$a .= ",CAST" if($s =~ /EVP_cast/);
84968651Skris			$a .= ",DES" if($s =~ /EVP_des/);
85068651Skris			$a .= ",DSA" if($s =~ /EVP_dss/);
85168651Skris			$a .= ",IDEA" if($s =~ /EVP_idea/);
85268651Skris			$a .= ",MD2" if($s =~ /EVP_md2/);
85368651Skris			$a .= ",MD4" if($s =~ /EVP_md4/);
85468651Skris			$a .= ",MD5" if($s =~ /EVP_md5/);
85568651Skris			$a .= ",RC2" if($s =~ /EVP_rc2/);
85668651Skris			$a .= ",RC4" if($s =~ /EVP_rc4/);
85768651Skris			$a .= ",RC5" if($s =~ /EVP_rc5/);
85868651Skris			$a .= ",RIPEMD" if($s =~ /EVP_ripemd/);
85968651Skris			$a .= ",SHA" if($s =~ /EVP_sha/);
86068651Skris			$a .= ",RSA" if($s =~ /EVP_(Open|Seal)(Final|Init)/);
86168651Skris			$a .= ",RSA" if($s =~ /PEM_Seal(Final|Init|Update)/);
86268651Skris			$a .= ",RSA" if($s =~ /RSAPrivateKey/);
86368651Skris			$a .= ",RSA" if($s =~ /SSLv23?_((client|server)_)?method/);
86468651Skris
865109998Smarkm			$platform{$s} =
866109998Smarkm			    &reduce_platforms((defined($platform{$s})?$platform{$s}.',':"").$p);
86768651Skris			$algorithm{$s} .= ','.$a;
86868651Skris
869109998Smarkm			if (defined($variant{$s})) {
870109998Smarkm				foreach $v (split /;/,$variant{$s}) {
871109998Smarkm					(my $r, my $p, my $k) = split(/:/,$v);
872109998Smarkm					my $ip = join ',',map({ /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p);
873109998Smarkm					$syms{$r} = 1;
874109998Smarkm					if (!defined($k)) { $k = $kind{$s}; }
875109998Smarkm					$kind{$r} = $k."(".$s.")";
876109998Smarkm					$algorithm{$r} = $algorithm{$s};
877109998Smarkm					$platform{$r} = &reduce_platforms($platform{$s}.",".$p.",".$p);
878109998Smarkm					$platform{$s} = &reduce_platforms($platform{$s}.','.$ip.','.$ip);
879109998Smarkm					print STDERR "DEBUG: \$variant{\"$s\"} = ",$v,"; \$r = $r; \$p = ",$platform{$r},"; \$a = ",$algorithm{$r},"; \$kind = ",$kind{$r},"\n" if $debug;
880109998Smarkm				}
88168651Skris			}
882109998Smarkm			print STDERR "DEBUG: \$s = $s; \$p = ",$platform{$s},"; \$a = ",$algorithm{$s},"; \$kind = ",$kind{$s},"\n" if $debug;
88355714Skris		}
88455714Skris	}
88555714Skris
88668651Skris	# Prune the returned symbols
88755714Skris
88868651Skris        delete $syms{"bn_dump1"};
88968651Skris	$platform{"BIO_s_log"} .= ",!WIN32,!WIN16,!macintosh";
89068651Skris
891109998Smarkm	$platform{"PEM_read_NS_CERT_SEQ"} = "VMS";
892109998Smarkm	$platform{"PEM_write_NS_CERT_SEQ"} = "VMS";
893109998Smarkm	$platform{"PEM_read_P8_PRIV_KEY_INFO"} = "VMS";
894109998Smarkm	$platform{"PEM_write_P8_PRIV_KEY_INFO"} = "VMS";
89568651Skris
89668651Skris	# Info we know about
89768651Skris
89868651Skris	push @ret, map { $_."\\".&info_string($_,"EXIST",
89968651Skris					      $platform{$_},
90068651Skris					      $kind{$_},
90168651Skris					      $algorithm{$_}) } keys %syms;
90268651Skris
903109998Smarkm	if (keys %unknown_algorithms) {
904109998Smarkm		print STDERR "WARNING: mkdef.pl doesn't know the following algorithms:\n";
905109998Smarkm		print STDERR "\t",join("\n\t",keys %unknown_algorithms),"\n";
906109998Smarkm	}
90768651Skris	return(@ret);
90868651Skris}
90968651Skris
910109998Smarkm# Param: string of comma-separated platform-specs.
911109998Smarkmsub reduce_platforms
912109998Smarkm{
913109998Smarkm	my ($platforms) = @_;
91468651Skris	my $pl = defined($platforms) ? $platforms : "";
91568651Skris	my %p = map { $_ => 0 } split /,/, $pl;
91668651Skris	my $ret;
91768651Skris
918109998Smarkm	print STDERR "DEBUG: Entered reduce_platforms with \"$platforms\"\n"
919109998Smarkm	    if $debug;
92068651Skris	# We do this, because if there's code like the following, it really
92168651Skris	# means the function exists in all cases and should therefore be
92268651Skris	# everywhere.  By increasing and decreasing, we may attain 0:
92368651Skris	#
92468651Skris	# ifndef WIN16
92568651Skris	#    int foo();
92668651Skris	# else
92768651Skris	#    int _fat foo();
92868651Skris	# endif
92968651Skris	foreach $platform (split /,/, $pl) {
93068651Skris		if ($platform =~ /^!(.*)$/) {
93168651Skris			$p{$1}--;
93268651Skris		} else {
93368651Skris			$p{$platform}++;
93455714Skris		}
93555714Skris	}
93668651Skris	foreach $platform (keys %p) {
93768651Skris		if ($p{$platform} == 0) { delete $p{$platform}; }
93855714Skris	}
93955714Skris
94068651Skris	delete $p{""};
941109998Smarkm
942109998Smarkm	$ret = join(',',sort(map { $p{$_} < 0 ? "!".$_ : $_ } keys %p));
943109998Smarkm	print STDERR "DEBUG: Exiting reduce_platforms with \"$ret\"\n"
944109998Smarkm	    if $debug;
945109998Smarkm	return $ret;
946109998Smarkm}
947109998Smarkm
948109998Smarkmsub info_string {
949109998Smarkm	(my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_;
950109998Smarkm
951109998Smarkm	my %a = defined($algorithms) ?
952109998Smarkm	    map { $_ => 1 } split /,/, $algorithms : ();
953109998Smarkm	my $k = defined($kind) ? $kind : "FUNCTION";
954109998Smarkm	my $ret;
955109998Smarkm	my $p = &reduce_platforms($platforms);
956109998Smarkm
95768651Skris	delete $a{""};
95855714Skris
95968651Skris	$ret = $exist;
960109998Smarkm	$ret .= ":".$p;
96168651Skris	$ret .= ":".$k;
962109998Smarkm	$ret .= ":".join(',',sort keys %a);
96368651Skris	return $ret;
96455714Skris}
96555714Skris
96668651Skrissub maybe_add_info {
96768651Skris	(my $name, *nums, my @symbols) = @_;
96868651Skris	my $sym;
96968651Skris	my $new_info = 0;
970109998Smarkm	my %syms=();
97168651Skris
97268651Skris	print STDERR "Updating $name info\n";
97368651Skris	foreach $sym (@symbols) {
97468651Skris		(my $s, my $i) = split /\\/, $sym;
97568651Skris		if (defined($nums{$s})) {
976109998Smarkm			$i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/;
97768651Skris			(my $n, my $dummy) = split /\\/, $nums{$s};
97868651Skris			if (!defined($dummy) || $i ne $dummy) {
97968651Skris				$nums{$s} = $n."\\".$i;
98068651Skris				$new_info++;
981109998Smarkm				print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n" if $debug;
98268651Skris			}
98368651Skris		}
984109998Smarkm		$syms{$s} = 1;
98568651Skris	}
986109998Smarkm
987109998Smarkm	my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
988109998Smarkm	foreach $sym (@s) {
989109998Smarkm		(my $n, my $i) = split /\\/, $nums{$sym};
990109998Smarkm		if (!defined($syms{$sym}) && $i !~ /^NOEXIST:/) {
991109998Smarkm			$new_info++;
992109998Smarkm			print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n" if $debug;
993109998Smarkm		}
994109998Smarkm	}
99568651Skris	if ($new_info) {
99668651Skris		print STDERR "$new_info old symbols got an info update\n";
99768651Skris		if (!$do_rewrite) {
99868651Skris			print STDERR "You should do a rewrite to fix this.\n";
99968651Skris		}
100068651Skris	} else {
100168651Skris		print STDERR "No old symbols needed info update\n";
100268651Skris	}
100368651Skris}
100468651Skris
1005109998Smarkm# Param: string of comma-separated keywords, each possibly prefixed with a "!"
1006109998Smarkmsub is_valid
1007109998Smarkm{
1008109998Smarkm	my ($keywords_txt,$platforms) = @_;
1009109998Smarkm	my (@keywords) = split /,/,$keywords_txt;
1010109998Smarkm	my ($falsesum, $truesum) = (0, !grep(/^[^!]/,@keywords));
1011109998Smarkm
1012109998Smarkm	# Param: one keyword
1013109998Smarkm	sub recognise
1014109998Smarkm	{
1015109998Smarkm		my ($keyword,$platforms) = @_;
1016109998Smarkm
1017109998Smarkm		if ($platforms) {
1018109998Smarkm			# platforms
1019109998Smarkm			if ($keyword eq "VMS" && $VMS) { return 1; }
1020109998Smarkm			if ($keyword eq "WIN32" && $W32) { return 1; }
1021109998Smarkm			if ($keyword eq "WIN16" && $W16) { return 1; }
1022109998Smarkm			if ($keyword eq "WINNT" && $NT) { return 1; }
1023109998Smarkm			if ($keyword eq "OS2" && $OS2) { return 1; }
1024109998Smarkm			# Special platforms:
1025109998Smarkm			# EXPORT_VAR_AS_FUNCTION means that global variables
1026109998Smarkm			# will be represented as functions.  This currently
1027109998Smarkm			# only happens on VMS-VAX.
1028109998Smarkm			if ($keyword eq "EXPORT_VAR_AS_FUNCTION" && ($VMSVAX || $W32 || $W16)) {
1029109998Smarkm				return 1;
1030109998Smarkm			}
1031109998Smarkm			return 0;
1032109998Smarkm		} else {
1033109998Smarkm			# algorithms
1034109998Smarkm			if ($keyword eq "RC2" && $no_rc2) { return 0; }
1035109998Smarkm			if ($keyword eq "RC4" && $no_rc4) { return 0; }
1036109998Smarkm			if ($keyword eq "RC5" && $no_rc5) { return 0; }
1037109998Smarkm			if ($keyword eq "IDEA" && $no_idea) { return 0; }
1038109998Smarkm			if ($keyword eq "DES" && $no_des) { return 0; }
1039109998Smarkm			if ($keyword eq "BF" && $no_bf) { return 0; }
1040109998Smarkm			if ($keyword eq "CAST" && $no_cast) { return 0; }
1041109998Smarkm			if ($keyword eq "MD2" && $no_md2) { return 0; }
1042109998Smarkm			if ($keyword eq "MD4" && $no_md4) { return 0; }
1043109998Smarkm			if ($keyword eq "MD5" && $no_md5) { return 0; }
1044109998Smarkm			if ($keyword eq "SHA" && $no_sha) { return 0; }
1045109998Smarkm			if ($keyword eq "RIPEMD" && $no_ripemd) { return 0; }
1046109998Smarkm			if ($keyword eq "MDC2" && $no_mdc2) { return 0; }
1047109998Smarkm			if ($keyword eq "RSA" && $no_rsa) { return 0; }
1048109998Smarkm			if ($keyword eq "DSA" && $no_dsa) { return 0; }
1049109998Smarkm			if ($keyword eq "DH" && $no_dh) { return 0; }
1050109998Smarkm			if ($keyword eq "EC" && $no_ec) { return 0; }
1051109998Smarkm			if ($keyword eq "HMAC" && $no_hmac) { return 0; }
1052109998Smarkm			if ($keyword eq "AES" && $no_aes) { return 0; }
1053109998Smarkm			if ($keyword eq "EVP" && $no_evp) { return 0; }
1054109998Smarkm			if ($keyword eq "LHASH" && $no_lhash) { return 0; }
1055109998Smarkm			if ($keyword eq "STACK" && $no_stack) { return 0; }
1056109998Smarkm			if ($keyword eq "ERR" && $no_err) { return 0; }
1057109998Smarkm			if ($keyword eq "BUFFER" && $no_buffer) { return 0; }
1058109998Smarkm			if ($keyword eq "BIO" && $no_bio) { return 0; }
1059109998Smarkm			if ($keyword eq "COMP" && $no_comp) { return 0; }
1060109998Smarkm			if ($keyword eq "DSO" && $no_dso) { return 0; }
1061109998Smarkm			if ($keyword eq "KRB5" && $no_krb5) { return 0; }
1062111147Snectar			if ($keyword eq "ENGINE" && $no_engine) { return 0; }
1063111147Snectar			if ($keyword eq "HW" && $no_hw) { return 0; }
1064109998Smarkm			if ($keyword eq "FP_API" && $no_fp_api) { return 0; }
1065109998Smarkm
1066109998Smarkm			# Nothing recognise as true
1067109998Smarkm			return 1;
1068109998Smarkm		}
1069109998Smarkm	}
1070109998Smarkm
1071109998Smarkm	foreach $k (@keywords) {
1072109998Smarkm		if ($k =~ /^!(.*)$/) {
1073109998Smarkm			$falsesum += &recognise($1,$platforms);
1074109998Smarkm		} else {
1075109998Smarkm			$truesum += &recognise($k,$platforms);
1076109998Smarkm		}
1077109998Smarkm	}
1078109998Smarkm	print STDERR "DEBUG: [",$#keywords,",",$#keywords < 0,"] is_valid($keywords_txt) => (\!$falsesum) && $truesum = ",(!$falsesum) && $truesum,"\n" if $debug;
1079109998Smarkm	return (!$falsesum) && $truesum;
1080109998Smarkm}
1081109998Smarkm
108259191Skrissub print_test_file
108359191Skris{
1084109998Smarkm	(*OUT,my $name,*nums,my $testall,my @symbols)=@_;
108559191Skris	my $n = 1; my @e; my @r;
108668651Skris	my $sym; my $prev = ""; my $prefSSLeay;
108759191Skris
1088109998Smarkm	(@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
1089109998Smarkm	(@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:.*/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
109068651Skris	@symbols=((sort @e),(sort @r));
109159191Skris
109268651Skris	foreach $sym (@symbols) {
109368651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1094109998Smarkm		my $v = 0;
1095109998Smarkm		$v = 1 if $i=~ /^.*?:.*?:VARIABLE/;
1096109998Smarkm		my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1097109998Smarkm		my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1098109998Smarkm		if (!defined($nums{$s})) {
1099109998Smarkm			print STDERR "Warning: $s does not have a number assigned\n"
1100109998Smarkm			    if(!$do_update);
1101109998Smarkm		} elsif (is_valid($p,1) && is_valid($a,0)) {
1102109998Smarkm			my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1103109998Smarkm			if ($prev eq $s2) {
1104109998Smarkm				print OUT "\t/* The following has already appeared previously */\n";
1105109998Smarkm				print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1106109998Smarkm			}
1107109998Smarkm			$prev = $s2;	# To warn about duplicates...
1108109998Smarkm
1109109998Smarkm			($nn,$ni)=($nums{$s2} =~ /^(.*?)\\(.*)$/);
1110109998Smarkm			if ($v) {
1111109998Smarkm				print OUT "\textern int $s2; /* type unknown */ /* $nn $ni */\n";
111268651Skris			} else {
1113109998Smarkm				print OUT "\textern int $s2(); /* type unknown */ /* $nn $ni */\n";
111468651Skris			}
111559191Skris		}
111659191Skris	}
111759191Skris}
111859191Skris
111955714Skrissub print_def_file
112055714Skris{
112168651Skris	(*OUT,my $name,*nums,my @symbols)=@_;
1122109998Smarkm	my $n = 1; my @e; my @r; my @v; my $prev="";
1123109998Smarkm	my $liboptions="";
112455714Skris
112555714Skris	if ($W32)
112655714Skris		{ $name.="32"; }
1127109998Smarkm	elsif ($W16)
112855714Skris		{ $name.="16"; }
1129109998Smarkm	elsif ($OS2)
1130109998Smarkm		{ $liboptions = "INITINSTANCE\nDATA NONSHARED"; }
113155714Skris
113255714Skris	print OUT <<"EOF";
113355714Skris;
113455714Skris; Definition file for the DLL version of the $name library from OpenSSL
113555714Skris;
113655714Skris
1137109998SmarkmLIBRARY         $name	$liboptions
113855714Skris
113955714SkrisDESCRIPTION     'OpenSSL $name - http://www.openssl.org/'
114055714Skris
114155714SkrisEOF
114255714Skris
1143109998Smarkm	if ($W16) {
114455714Skris		print <<"EOF";
114555714SkrisCODE            PRELOAD MOVEABLE
114655714SkrisDATA            PRELOAD MOVEABLE SINGLE
114755714Skris
114855714SkrisEXETYPE		WINDOWS
114955714Skris
115055714SkrisHEAPSIZE	4096
115155714SkrisSTACKSIZE	8192
115255714Skris
115355714SkrisEOF
115455714Skris	}
115555714Skris
115655714Skris	print "EXPORTS\n";
115755714Skris
1158109998Smarkm	(@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1159109998Smarkm	(@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:FUNCTION/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1160109998Smarkm	(@v)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:VARIABLE/,@symbols);
1161109998Smarkm	@symbols=((sort @e),(sort @r), (sort @v));
116255714Skris
116355714Skris
116468651Skris	foreach $sym (@symbols) {
116568651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1166109998Smarkm		my $v = 0;
1167109998Smarkm		$v = 1 if $i =~ /^.*?:.*?:VARIABLE/;
116868651Skris		if (!defined($nums{$s})) {
116968651Skris			printf STDERR "Warning: $s does not have a number assigned\n"
1170109998Smarkm			    if(!$do_update);
117155714Skris		} else {
1172109998Smarkm			(my $n, my $dummy) = split /\\/, $nums{$s};
117368651Skris			my %pf = ();
1174109998Smarkm			my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1175109998Smarkm			my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1176109998Smarkm			if (is_valid($p,1) && is_valid($a,0)) {
1177109998Smarkm				my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1178109998Smarkm				if ($prev eq $s2) {
1179109998Smarkm					print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1180109998Smarkm				}
1181109998Smarkm				$prev = $s2;	# To warn about duplicates...
1182109998Smarkm				if($v && !$OS2) {
1183109998Smarkm					printf OUT "    %s%-39s @%-8d DATA\n",($W32)?"":"_",$s2,$n;
1184109998Smarkm				} else {
1185109998Smarkm					printf OUT "    %s%-39s @%d\n",($W32||$OS2)?"":"_",$s2,$n;
1186109998Smarkm				}
118768651Skris			}
118855714Skris		}
118955714Skris	}
119055714Skris	printf OUT "\n";
119155714Skris}
119255714Skris
119355714Skrissub load_numbers
119455714Skris{
119555714Skris	my($name)=@_;
119655714Skris	my(@a,%ret);
119755714Skris
119855714Skris	$max_num = 0;
119968651Skris	$num_noinfo = 0;
120068651Skris	$prev = "";
1201109998Smarkm	$prev_cnt = 0;
120255714Skris
120355714Skris	open(IN,"<$name") || die "unable to open $name:$!\n";
120455714Skris	while (<IN>) {
120555714Skris		chop;
120655714Skris		s/#.*$//;
120755714Skris		next if /^\s*$/;
120855714Skris		@a=split;
120968651Skris		if (defined $ret{$a[0]}) {
1210109998Smarkm			# This is actually perfectly OK
1211109998Smarkm			#print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n";
121268651Skris		}
121368651Skris		if ($max_num > $a[1]) {
121468651Skris			print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n";
121568651Skris		}
1216109998Smarkm		elsif ($max_num == $a[1]) {
121768651Skris			# This is actually perfectly OK
121868651Skris			#print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n";
1219109998Smarkm			if ($a[0] eq $prev) {
1220109998Smarkm				$prev_cnt++;
1221109998Smarkm				$a[0] .= "{$prev_cnt}";
1222109998Smarkm			}
122368651Skris		}
1224109998Smarkm		else {
1225109998Smarkm			$prev_cnt = 0;
1226109998Smarkm		}
122768651Skris		if ($#a < 2) {
122868651Skris			# Existence will be proven later, in do_defs
122968651Skris			$ret{$a[0]}=$a[1];
123068651Skris			$num_noinfo++;
123168651Skris		} else {
123268651Skris			$ret{$a[0]}=$a[1]."\\".$a[2]; # \\ is a special marker
123368651Skris		}
123455714Skris		$max_num = $a[1] if $a[1] > $max_num;
123568651Skris		$prev=$a[0];
123655714Skris	}
123768651Skris	if ($num_noinfo) {
123868651Skris		print STDERR "Warning: $num_noinfo symbols were without info.";
123968651Skris		if ($do_rewrite) {
124068651Skris			printf STDERR "  The rewrite will fix this.\n";
124168651Skris		} else {
124268651Skris			printf STDERR "  You should do a rewrite to fix this.\n";
124368651Skris		}
124468651Skris	}
124555714Skris	close(IN);
124655714Skris	return(%ret);
124755714Skris}
124855714Skris
124968651Skrissub parse_number
125068651Skris{
125168651Skris	(my $str, my $what) = @_;
125268651Skris	(my $n, my $i) = split(/\\/,$str);
125368651Skris	if ($what eq "n") {
125468651Skris		return $n;
125568651Skris	} else {
125668651Skris		return $i;
125768651Skris	}
125868651Skris}
125968651Skris
126068651Skrissub rewrite_numbers
126168651Skris{
126268651Skris	(*OUT,$name,*nums,@symbols)=@_;
126368651Skris	my $thing;
126468651Skris
126568651Skris	print STDERR "Rewriting $name\n";
126668651Skris
1267109998Smarkm	my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
126868651Skris	my $r; my %r; my %rsyms;
126968651Skris	foreach $r (@r) {
127068651Skris		(my $s, my $i) = split /\\/, $r;
127168651Skris		my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
127268651Skris		$i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
127368651Skris		$r{$a} = $s."\\".$i;
127468651Skris		$rsyms{$s} = 1;
127568651Skris	}
127668651Skris
1277109998Smarkm	my %syms = ();
1278109998Smarkm	foreach $_ (@symbols) {
1279109998Smarkm		(my $n, my $i) = split /\\/;
1280109998Smarkm		$syms{$n} = 1;
1281109998Smarkm	}
1282109998Smarkm
1283109998Smarkm	my @s=sort {
1284109998Smarkm	    &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n")
1285109998Smarkm	    || $a cmp $b
1286109998Smarkm	} keys %nums;
128768651Skris	foreach $sym (@s) {
128868651Skris		(my $n, my $i) = split /\\/, $nums{$sym};
128968651Skris		next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/;
129068651Skris		next if defined($rsyms{$sym});
1291109998Smarkm		print STDERR "DEBUG: rewrite_numbers for sym = ",$sym,": i = ",$i,", n = ",$n,", rsym{sym} = ",$rsyms{$sym},"syms{sym} = ",$syms{$sym},"\n" if $debug;
1292109998Smarkm		$i="NOEXIST::FUNCTION:"
1293109998Smarkm			if !defined($i) || $i eq "" || !defined($syms{$sym});
1294109998Smarkm		my $s2 = $sym;
1295109998Smarkm		$s2 =~ s/\{[0-9]+\}$//;
1296109998Smarkm		printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i;
129768651Skris		if (exists $r{$sym}) {
129868651Skris			(my $s, $i) = split /\\/,$r{$sym};
1299109998Smarkm			my $s2 = $s;
1300109998Smarkm			$s2 =~ s/\{[0-9]+\}$//;
1301109998Smarkm			printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i;
130268651Skris		}
130368651Skris	}
130468651Skris}
130568651Skris
130655714Skrissub update_numbers
130755714Skris{
130868651Skris	(*OUT,$name,*nums,my $start_num, my @symbols)=@_;
130968651Skris	my $new_syms = 0;
131068651Skris
131168651Skris	print STDERR "Updating $name numbers\n";
131268651Skris
1313109998Smarkm	my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
131468651Skris	my $r; my %r; my %rsyms;
131568651Skris	foreach $r (@r) {
131668651Skris		(my $s, my $i) = split /\\/, $r;
131768651Skris		my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
131868651Skris		$i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
131968651Skris		$r{$a} = $s."\\".$i;
132068651Skris		$rsyms{$s} = 1;
132168651Skris	}
132268651Skris
132368651Skris	foreach $sym (@symbols) {
132468651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
132568651Skris		next if $i =~ /^.*?:.*?:\w+\(\w+\)/;
132668651Skris		next if defined($rsyms{$sym});
132768651Skris		die "ERROR: Symbol $sym had no info attached to it."
132868651Skris		    if $i eq "";
132968651Skris		if (!exists $nums{$s}) {
133068651Skris			$new_syms++;
1331109998Smarkm			my $s2 = $s;
1332109998Smarkm			$s2 =~ s/\{[0-9]+\}$//;
1333109998Smarkm			printf OUT "%s%-39s %d\t%s\n","",$s2, ++$start_num,$i;
133468651Skris			if (exists $r{$s}) {
133568651Skris				($s, $i) = split /\\/,$r{$s};
1336109998Smarkm				$s =~ s/\{[0-9]+\}$//;
1337109998Smarkm				printf OUT "%s%-39s %d\t%s\n","",$s, $start_num,$i;
133868651Skris			}
133955714Skris		}
134055714Skris	}
134168651Skris	if($new_syms) {
134268651Skris		print STDERR "$new_syms New symbols added\n";
134355714Skris	} else {
134468651Skris		print STDERR "No New symbols Added\n";
134555714Skris	}
134655714Skris}
134768651Skris
134868651Skrissub check_existing
134968651Skris{
135068651Skris	(*nums, my @symbols)=@_;
135168651Skris	my %existing; my @remaining;
135268651Skris	@remaining=();
135368651Skris	foreach $sym (@symbols) {
135468651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
135568651Skris		$existing{$s}=1;
135668651Skris	}
135768651Skris	foreach $sym (keys %nums) {
135868651Skris		if (!exists $existing{$sym}) {
135968651Skris			push @remaining, $sym;
136068651Skris		}
136168651Skris	}
136268651Skris	if(@remaining) {
136368651Skris		print STDERR "The following symbols do not seem to exist:\n";
136468651Skris		foreach $sym (@remaining) {
136568651Skris			print STDERR "\t",$sym,"\n";
136668651Skris		}
136768651Skris	}
136868651Skris}
136968651Skris
1370