mkdef.pl revision 142425
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",
82142425Snectar			"EXPORT_VAR_AS_FUNCTION", "OPENSSL_FIPS" );
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="";
97142425Snectaropen(IN,"<Makefile") || die "unable to open Makefile!\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;
112142425Snectarmy $fips;
11359191Skris
11455714Skrisforeach (@ARGV, split(/ /, $options))
11555714Skris	{
116109998Smarkm	$debug=1 if $_ eq "debug";
11755714Skris	$W32=1 if $_ eq "32";
11868651Skris	$W16=1 if $_ eq "16";
11955714Skris	if($_ eq "NT") {
12055714Skris		$W32 = 1;
12155714Skris		$NT = 1;
12255714Skris	}
123109998Smarkm	if ($_ eq "VMS-VAX") {
124109998Smarkm		$VMS=1;
125109998Smarkm		$VMSVAX=1;
126109998Smarkm	}
127109998Smarkm	if ($_ eq "VMS-Alpha") {
128109998Smarkm		$VMS=1;
129109998Smarkm		$VMSAlpha=1;
130109998Smarkm	}
13168651Skris	$VMS=1 if $_ eq "VMS";
132109998Smarkm	$OS2=1 if $_ eq "OS2";
133142425Snectar	$fips=1 if $_ eq "fips";
13468651Skris
13555714Skris	$do_ssl=1 if $_ eq "ssleay";
136109998Smarkm	if ($_ eq "ssl") {
137109998Smarkm		$do_ssl=1;
138109998Smarkm		$libname=$_
139109998Smarkm	}
14055714Skris	$do_crypto=1 if $_ eq "libeay";
141109998Smarkm	if ($_ eq "crypto") {
142109998Smarkm		$do_crypto=1;
143109998Smarkm		$libname=$_;
144109998Smarkm	}
14555714Skris	$do_update=1 if $_ eq "update";
14668651Skris	$do_rewrite=1 if $_ eq "rewrite";
14759191Skris	$do_ctest=1 if $_ eq "ctest";
14868651Skris	$do_ctestall=1 if $_ eq "ctestall";
149109998Smarkm	$do_checkexist=1 if $_ eq "exist";
15068651Skris	#$safe_stack_def=1 if $_ eq "-DDEBUG_SAFESTACK";
15155714Skris
15255714Skris	if    (/^no-rc2$/)      { $no_rc2=1; }
15355714Skris	elsif (/^no-rc4$/)      { $no_rc4=1; }
15455714Skris	elsif (/^no-rc5$/)      { $no_rc5=1; }
15555714Skris	elsif (/^no-idea$/)     { $no_idea=1; }
15672613Skris	elsif (/^no-des$/)      { $no_des=1; $no_mdc2=1; }
15755714Skris	elsif (/^no-bf$/)       { $no_bf=1; }
15855714Skris	elsif (/^no-cast$/)     { $no_cast=1; }
15955714Skris	elsif (/^no-md2$/)      { $no_md2=1; }
16068651Skris	elsif (/^no-md4$/)      { $no_md4=1; }
16155714Skris	elsif (/^no-md5$/)      { $no_md5=1; }
16255714Skris	elsif (/^no-sha$/)      { $no_sha=1; }
16355714Skris	elsif (/^no-ripemd$/)   { $no_ripemd=1; }
16455714Skris	elsif (/^no-mdc2$/)     { $no_mdc2=1; }
16555714Skris	elsif (/^no-rsa$/)      { $no_rsa=1; }
16655714Skris	elsif (/^no-dsa$/)      { $no_dsa=1; }
16755714Skris	elsif (/^no-dh$/)       { $no_dh=1; }
168109998Smarkm	elsif (/^no-ec$/)       { $no_ec=1; }
16955714Skris	elsif (/^no-hmac$/)	{ $no_hmac=1; }
170109998Smarkm	elsif (/^no-aes$/)	{ $no_aes=1; }
171109998Smarkm	elsif (/^no-evp$/)	{ $no_evp=1; }
172109998Smarkm	elsif (/^no-lhash$/)	{ $no_lhash=1; }
173109998Smarkm	elsif (/^no-stack$/)	{ $no_stack=1; }
174109998Smarkm	elsif (/^no-err$/)	{ $no_err=1; }
175109998Smarkm	elsif (/^no-buffer$/)	{ $no_buffer=1; }
176109998Smarkm	elsif (/^no-bio$/)	{ $no_bio=1; }
177109998Smarkm	#elsif (/^no-locking$/)	{ $no_locking=1; }
178109998Smarkm	elsif (/^no-comp$/)	{ $no_comp=1; }
179109998Smarkm	elsif (/^no-dso$/)	{ $no_dso=1; }
180109998Smarkm	elsif (/^no-krb5$/)	{ $no_krb5=1; }
181111147Snectar	elsif (/^no-engine$/)	{ $no_engine=1; }
182111147Snectar	elsif (/^no-hw$/)	{ $no_hw=1; }
18355714Skris	}
18455714Skris
18559191Skris
186109998Smarkmif (!$libname) {
187109998Smarkm	if ($do_ssl) {
188109998Smarkm		$libname="SSLEAY";
189109998Smarkm	}
190109998Smarkm	if ($do_crypto) {
191109998Smarkm		$libname="LIBEAY";
192109998Smarkm	}
193109998Smarkm}
194109998Smarkm
19568651Skris# If no platform is given, assume WIN32
196109998Smarkmif ($W32 + $W16 + $VMS + $OS2 == 0) {
19768651Skris	$W32 = 1;
19868651Skris}
19968651Skris
20068651Skris# Add extra knowledge
20168651Skrisif ($W16) {
20268651Skris	$no_fp_api=1;
20368651Skris}
20468651Skris
20555714Skrisif (!$do_ssl && !$do_crypto)
20655714Skris	{
207109998Smarkm	print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT | OS2 ]\n";
20855714Skris	exit(1);
20955714Skris	}
21055714Skris
21155714Skris%ssl_list=&load_numbers($ssl_num);
21255714Skris$max_ssl = $max_num;
21355714Skris%crypto_list=&load_numbers($crypto_num);
21455714Skris$max_crypto = $max_num;
21555714Skris
21659191Skrismy $ssl="ssl/ssl.h";
217109998Smarkm$ssl.=" ssl/kssl.h";
21855714Skris
21959191Skrismy $crypto ="crypto/crypto.h";
220109998Smarkm$crypto.=" crypto/des/des.h crypto/des/des_old.h" ; # unless $no_des;
221109998Smarkm$crypto.=" crypto/idea/idea.h" ; # unless $no_idea;
222109998Smarkm$crypto.=" crypto/rc4/rc4.h" ; # unless $no_rc4;
223109998Smarkm$crypto.=" crypto/rc5/rc5.h" ; # unless $no_rc5;
224109998Smarkm$crypto.=" crypto/rc2/rc2.h" ; # unless $no_rc2;
225109998Smarkm$crypto.=" crypto/bf/blowfish.h" ; # unless $no_bf;
226109998Smarkm$crypto.=" crypto/cast/cast.h" ; # unless $no_cast;
227109998Smarkm$crypto.=" crypto/md2/md2.h" ; # unless $no_md2;
228109998Smarkm$crypto.=" crypto/md4/md4.h" ; # unless $no_md4;
229109998Smarkm$crypto.=" crypto/md5/md5.h" ; # unless $no_md5;
230109998Smarkm$crypto.=" crypto/mdc2/mdc2.h" ; # unless $no_mdc2;
231109998Smarkm$crypto.=" crypto/sha/sha.h" ; # unless $no_sha;
232109998Smarkm$crypto.=" crypto/ripemd/ripemd.h" ; # unless $no_ripemd;
233109998Smarkm$crypto.=" crypto/aes/aes.h" ; # unless $no_aes;
23455714Skris
23555714Skris$crypto.=" crypto/bn/bn.h";
236109998Smarkm$crypto.=" crypto/rsa/rsa.h" ; # unless $no_rsa;
237109998Smarkm$crypto.=" crypto/dsa/dsa.h" ; # unless $no_dsa;
238109998Smarkm$crypto.=" crypto/dh/dh.h" ; # unless $no_dh;
239109998Smarkm$crypto.=" crypto/ec/ec.h" ; # unless $no_ec;
240109998Smarkm$crypto.=" crypto/hmac/hmac.h" ; # unless $no_hmac;
24155714Skris
242111147Snectar$crypto.=" crypto/engine/engine.h"; # unless $no_engine;
243109998Smarkm$crypto.=" crypto/stack/stack.h" ; # unless $no_stack;
244109998Smarkm$crypto.=" crypto/buffer/buffer.h" ; # unless $no_buffer;
245109998Smarkm$crypto.=" crypto/bio/bio.h" ; # unless $no_bio;
246109998Smarkm$crypto.=" crypto/dso/dso.h" ; # unless $no_dso;
247109998Smarkm$crypto.=" crypto/lhash/lhash.h" ; # unless $no_lhash;
24855714Skris$crypto.=" crypto/conf/conf.h";
24955714Skris$crypto.=" crypto/txt_db/txt_db.h";
25055714Skris
251109998Smarkm$crypto.=" crypto/evp/evp.h" ; # unless $no_evp;
25255714Skris$crypto.=" crypto/objects/objects.h";
25355714Skris$crypto.=" crypto/pem/pem.h";
25455714Skris#$crypto.=" crypto/meth/meth.h";
25555714Skris$crypto.=" crypto/asn1/asn1.h";
256109998Smarkm$crypto.=" crypto/asn1/asn1t.h";
25755714Skris$crypto.=" crypto/asn1/asn1_mac.h";
258109998Smarkm$crypto.=" crypto/err/err.h" ; # unless $no_err;
25955714Skris$crypto.=" crypto/pkcs7/pkcs7.h";
26055714Skris$crypto.=" crypto/pkcs12/pkcs12.h";
26155714Skris$crypto.=" crypto/x509/x509.h";
26255714Skris$crypto.=" crypto/x509/x509_vfy.h";
26355714Skris$crypto.=" crypto/x509v3/x509v3.h";
26455714Skris$crypto.=" crypto/rand/rand.h";
265109998Smarkm$crypto.=" crypto/comp/comp.h" ; # unless $no_comp;
266109998Smarkm$crypto.=" crypto/ocsp/ocsp.h";
267109998Smarkm$crypto.=" crypto/ui/ui.h crypto/ui/ui_compat.h";
268109998Smarkm$crypto.=" crypto/krb5/krb5_asn.h";
26955714Skris$crypto.=" crypto/tmdiff.h";
270142425Snectar$crypto.=" fips/fips.h fips/rand/fips_rand.h";
27155714Skris
27268651Skrismy $symhacks="crypto/symhacks.h";
27355714Skris
27468651Skrismy @ssl_symbols = &do_defs("SSLEAY", $ssl, $symhacks);
27568651Skrismy @crypto_symbols = &do_defs("LIBEAY", $crypto, $symhacks);
27655714Skris
27755714Skrisif ($do_update) {
27855714Skris
27955714Skrisif ($do_ssl == 1) {
28068651Skris
28168651Skris	&maybe_add_info("SSLEAY",*ssl_list,@ssl_symbols);
28268651Skris	if ($do_rewrite == 1) {
28368651Skris		open(OUT, ">$ssl_num");
28468651Skris		&rewrite_numbers(*OUT,"SSLEAY",*ssl_list,@ssl_symbols);
28568651Skris	} else {
28668651Skris		open(OUT, ">>$ssl_num");
28768651Skris	}
28868651Skris	&update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl,@ssl_symbols);
28955714Skris	close OUT;
29055714Skris}
29155714Skris
29255714Skrisif($do_crypto == 1) {
29368651Skris
29468651Skris	&maybe_add_info("LIBEAY",*crypto_list,@crypto_symbols);
29568651Skris	if ($do_rewrite == 1) {
29668651Skris		open(OUT, ">$crypto_num");
29768651Skris		&rewrite_numbers(*OUT,"LIBEAY",*crypto_list,@crypto_symbols);
29868651Skris	} else {
29968651Skris		open(OUT, ">>$crypto_num");
30068651Skris	}
30168651Skris	&update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto,@crypto_symbols);
30255714Skris	close OUT;
30359191Skris}
30455714Skris
305109998Smarkm} elsif ($do_checkexist) {
306109998Smarkm	&check_existing(*ssl_list, @ssl_symbols)
307109998Smarkm		if $do_ssl == 1;
308109998Smarkm	&check_existing(*crypto_list, @crypto_symbols)
309109998Smarkm		if $do_crypto == 1;
31068651Skris} elsif ($do_ctest || $do_ctestall) {
31159191Skris
31259191Skris	print <<"EOF";
31359191Skris
31459191Skris/* Test file to check all DEF file symbols are present by trying
31559191Skris * to link to all of them. This is *not* intended to be run!
31659191Skris */
31759191Skris
31859191Skrisint main()
31959191Skris{
32059191SkrisEOF
32168651Skris	&print_test_file(*STDOUT,"SSLEAY",*ssl_list,$do_ctestall,@ssl_symbols)
32259191Skris		if $do_ssl == 1;
32359191Skris
32468651Skris	&print_test_file(*STDOUT,"LIBEAY",*crypto_list,$do_ctestall,@crypto_symbols)
32559191Skris		if $do_crypto == 1;
32659191Skris
32759191Skris	print "}\n";
32859191Skris
32955714Skris} else {
33055714Skris
331109998Smarkm	&print_def_file(*STDOUT,$libname,*ssl_list,@ssl_symbols)
33255714Skris		if $do_ssl == 1;
33355714Skris
334109998Smarkm	&print_def_file(*STDOUT,$libname,*crypto_list,@crypto_symbols)
33555714Skris		if $do_crypto == 1;
33655714Skris
33755714Skris}
33855714Skris
33955714Skris
34055714Skrissub do_defs
34155714Skris{
34268651Skris	my($name,$files,$symhacksfile)=@_;
34359191Skris	my $file;
34455714Skris	my @ret;
34568651Skris	my %syms;
34668651Skris	my %platform;		# For anything undefined, we assume ""
34768651Skris	my %kind;		# For anything undefined, we assume "FUNCTION"
34868651Skris	my %algorithm;		# For anything undefined, we assume ""
349109998Smarkm	my %variant;
350109998Smarkm	my %variant_cnt;	# To be able to allocate "name{n}" if "name"
351109998Smarkm				# is the same name as the original.
35259191Skris	my $cpp;
353109998Smarkm	my %unknown_algorithms = ();
35455714Skris
35568651Skris	foreach $file (split(/\s+/,$symhacksfile." ".$files))
35655714Skris		{
357109998Smarkm		print STDERR "DEBUG: starting on $file:\n" if $debug;
35855714Skris		open(IN,"<$file") || die "unable to open $file:$!\n";
35959191Skris		my $line = "", my $def= "";
36055714Skris		my %tag = (
36168651Skris			(map { $_ => 0 } @known_platforms),
362109998Smarkm			(map { "OPENSSL_SYS_".$_ => 0 } @known_ossl_platforms),
363109998Smarkm			(map { "OPENSSL_NO_".$_ => 0 } @known_algorithms),
36455714Skris			NOPROTO		=> 0,
36555714Skris			PERL5		=> 0,
36655714Skris			_WINDLL		=> 0,
36755714Skris			CONST_STRICT	=> 0,
36855714Skris			TRUE		=> 1,
36955714Skris		);
37068651Skris		my $symhacking = $file eq $symhacksfile;
371109998Smarkm		my @current_platforms = ();
372109998Smarkm		my @current_algorithms = ();
373109998Smarkm
374109998Smarkm		# params: symbol, alias, platforms, kind
375109998Smarkm		# The reason to put this subroutine in a variable is that
376109998Smarkm		# it will otherwise create it's own, unshared, version of
377109998Smarkm		# %tag and %variant...
378109998Smarkm		my $make_variant = sub
379109998Smarkm		{
380109998Smarkm			my ($s, $a, $p, $k) = @_;
381109998Smarkm			my ($a1, $a2);
382109998Smarkm
383109998Smarkm			print STDERR "DEBUG: make_variant: Entered with ",$s,", ",$a,", ",(defined($p)?$p:""),", ",(defined($k)?$k:""),"\n" if $debug;
384109998Smarkm			if (defined($p))
385109998Smarkm			{
386109998Smarkm				$a1 = join(",",$p,
387109998Smarkm					   grep(!/^$/,
388109998Smarkm						map { $tag{$_} == 1 ? $_ : "" }
389109998Smarkm						@known_platforms));
390109998Smarkm			}
391109998Smarkm			else
392109998Smarkm			{
393109998Smarkm				$a1 = join(",",
394109998Smarkm					   grep(!/^$/,
395109998Smarkm						map { $tag{$_} == 1 ? $_ : "" }
396109998Smarkm						@known_platforms));
397109998Smarkm			}
398109998Smarkm			$a2 = join(",",
399109998Smarkm				   grep(!/^$/,
400109998Smarkm					map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ : "" }
401109998Smarkm					@known_ossl_platforms));
402109998Smarkm			print STDERR "DEBUG: make_variant: a1 = $a1; a2 = $a2\n" if $debug;
403109998Smarkm			if ($a1 eq "") { $a1 = $a2; }
404109998Smarkm			elsif ($a1 ne "" && $a2 ne "") { $a1 .= ",".$a2; }
405109998Smarkm			if ($a eq $s)
406109998Smarkm			{
407109998Smarkm				if (!defined($variant_cnt{$s}))
408109998Smarkm				{
409109998Smarkm					$variant_cnt{$s} = 0;
410109998Smarkm				}
411109998Smarkm				$variant_cnt{$s}++;
412109998Smarkm				$a .= "{$variant_cnt{$s}}";
413109998Smarkm			}
414109998Smarkm			my $toadd = $a.":".$a1.(defined($k)?":".$k:"");
415109998Smarkm			my $togrep = $s.'(\{[0-9]+\})?:'.$a1.(defined($k)?":".$k:"");
416109998Smarkm			if (!grep(/^$togrep$/,
417109998Smarkm				  split(/;/, defined($variant{$s})?$variant{$s}:""))) {
418109998Smarkm				if (defined($variant{$s})) { $variant{$s} .= ";"; }
419109998Smarkm				$variant{$s} .= $toadd;
420109998Smarkm			}
421109998Smarkm			print STDERR "DEBUG: make_variant: Exit with variant of ",$s," = ",$variant{$s},"\n" if $debug;
422109998Smarkm		};
423109998Smarkm
424109998Smarkm		print STDERR "DEBUG: parsing ----------\n" if $debug;
42555714Skris		while(<IN>) {
426109998Smarkm			last if (/\/\* Error codes for the \w+ functions\. \*\//);
42755714Skris			if ($line ne '') {
42855714Skris				$_ = $line . $_;
42955714Skris				$line = '';
43055714Skris			}
43155714Skris
43255714Skris			if (/\\$/) {
433109998Smarkm				chomp; # remove eol
434109998Smarkm				chop; # remove ending backslash
43555714Skris				$line = $_;
43655714Skris				next;
43755714Skris			}
43855714Skris
43968651Skris	    		$cpp = 1 if /^\#.*ifdef.*cplusplus/;
44055714Skris			if ($cpp) {
44168651Skris				$cpp = 0 if /^\#.*endif/;
44255714Skris				next;
44355714Skris	    		}
44455714Skris
44555714Skris			s/\/\*.*?\*\///gs;                   # ignore comments
446120631Snectar			if (/\/\*/) {			     # if we have part
447120631Snectar				$line = $_;		     # of a comment,
448120631Snectar				next;			     # continue reading
449120631Snectar			}
45055714Skris			s/{[^{}]*}//gs;                      # ignore {} blocks
451120631Snectar			print STDERR "DEBUG: \$def=\"$def\"\n" if $debug && $def ne "";
452109998Smarkm			print STDERR "DEBUG: \$_=\"$_\"\n" if $debug;
453109998Smarkm			if (/^\#\s*ifndef\s+(.*)/) {
454109998Smarkm				push(@tag,"-");
45555714Skris				push(@tag,$1);
45655714Skris				$tag{$1}=-1;
457109998Smarkm				print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
458109998Smarkm			} elsif (/^\#\s*if\s+!defined\(([^\)]+)\)/) {
459109998Smarkm				push(@tag,"-");
460109998Smarkm				if (/^\#\s*if\s+(!defined\(([^\)]+)\)(\s+\&\&\s+!defined\(([^\)]+)\))*)$/) {
461109998Smarkm					my $tmp_1 = $1;
462109998Smarkm					my $tmp_;
463109998Smarkm					foreach $tmp_ (split '\&\&',$tmp_1) {
464109998Smarkm						$tmp_ =~ /!defined\(([^\)]+)\)/;
465109998Smarkm						print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
466109998Smarkm						push(@tag,$1);
467109998Smarkm						$tag{$1}=-1;
468109998Smarkm					}
469109998Smarkm				} else {
470109998Smarkm					print STDERR "Warning: $file: complicated expression: $_" if $debug; # because it is O...
471109998Smarkm					print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
472109998Smarkm					push(@tag,$1);
473109998Smarkm					$tag{$1}=-1;
474109998Smarkm				}
475109998Smarkm			} elsif (/^\#\s*ifdef\s+(.*)/) {
476109998Smarkm				push(@tag,"-");
47755714Skris				push(@tag,$1);
47855714Skris				$tag{$1}=1;
479109998Smarkm				print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
480109998Smarkm			} elsif (/^\#\s*if\s+defined\(([^\)]+)\)/) {
481109998Smarkm				push(@tag,"-");
482109998Smarkm				if (/^\#\s*if\s+(defined\(([^\)]+)\)(\s+\|\|\s+defined\(([^\)]+)\))*)$/) {
483109998Smarkm					my $tmp_1 = $1;
484109998Smarkm					my $tmp_;
485109998Smarkm					foreach $tmp_ (split '\|\|',$tmp_1) {
486109998Smarkm						$tmp_ =~ /defined\(([^\)]+)\)/;
487109998Smarkm						print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
488109998Smarkm						push(@tag,$1);
489109998Smarkm						$tag{$1}=1;
490109998Smarkm					}
491109998Smarkm				} else {
492109998Smarkm					print STDERR "Warning: $file: complicated expression: $_\n" if $debug; # because it is O...
493109998Smarkm					print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
494109998Smarkm					push(@tag,$1);
495109998Smarkm					$tag{$1}=1;
496109998Smarkm				}
49768651Skris			} elsif (/^\#\s*error\s+(\w+) is disabled\./) {
498109998Smarkm				my $tag_i = $#tag;
499109998Smarkm				while($tag[$tag_i] ne "-") {
500109998Smarkm					if ($tag[$tag_i] eq "OPENSSL_NO_".$1) {
501109998Smarkm						$tag{$tag[$tag_i]}=2;
502109998Smarkm						print STDERR "DEBUG: $file: chaged tag $1 = 2\n" if $debug;
503109998Smarkm					}
504109998Smarkm					$tag_i--;
50568651Skris				}
50655714Skris			} elsif (/^\#\s*endif/) {
507109998Smarkm				my $tag_i = $#tag;
508109998Smarkm				while($tag[$tag_i] ne "-") {
509109998Smarkm					my $t=$tag[$tag_i];
510109998Smarkm					print STDERR "DEBUG: \$t=\"$t\"\n" if $debug;
511109998Smarkm					if ($tag{$t}==2) {
512109998Smarkm						$tag{$t}=-1;
513109998Smarkm					} else {
514109998Smarkm						$tag{$t}=0;
515109998Smarkm					}
516109998Smarkm					print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
517109998Smarkm					pop(@tag);
518109998Smarkm					if ($t =~ /^OPENSSL_NO_([A-Z0-9_]+)$/) {
519109998Smarkm						$t=$1;
520109998Smarkm					} else {
521109998Smarkm						$t="";
522109998Smarkm					}
523109998Smarkm					if ($t ne ""
524109998Smarkm					    && !grep(/^$t$/, @known_algorithms)) {
525109998Smarkm						$unknown_algorithms{$t} = 1;
526109998Smarkm						#print STDERR "DEBUG: Added as unknown algorithm: $t\n" if $debug;
527109998Smarkm					}
528109998Smarkm					$tag_i--;
52968651Skris				}
53055714Skris				pop(@tag);
53155714Skris			} elsif (/^\#\s*else/) {
532109998Smarkm				my $tag_i = $#tag;
533109998Smarkm				while($tag[$tag_i] ne "-") {
534109998Smarkm					my $t=$tag[$tag_i];
535109998Smarkm					$tag{$t}= -$tag{$t};
536109998Smarkm					print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
537109998Smarkm					$tag_i--;
538109998Smarkm				}
53955714Skris			} elsif (/^\#\s*if\s+1/) {
540109998Smarkm				push(@tag,"-");
54155714Skris				# Dummy tag
54255714Skris				push(@tag,"TRUE");
54355714Skris				$tag{"TRUE"}=1;
544109998Smarkm				print STDERR "DEBUG: $file: found 1\n" if $debug;
54559191Skris			} elsif (/^\#\s*if\s+0/) {
546109998Smarkm				push(@tag,"-");
54759191Skris				# Dummy tag
54859191Skris				push(@tag,"TRUE");
54959191Skris				$tag{"TRUE"}=-1;
550109998Smarkm				print STDERR "DEBUG: $file: found 0\n" if $debug;
55168651Skris			} elsif (/^\#\s*define\s+(\w+)\s+(\w+)/
552109998Smarkm				 && $symhacking && $tag{'TRUE'} != -1) {
553109998Smarkm				# This is for aliasing.  When we find an alias,
554109998Smarkm				# we have to invert
555109998Smarkm				&$make_variant($1,$2);
556109998Smarkm				print STDERR "DEBUG: $file: defined $1 = $2\n" if $debug;
55768651Skris			}
55868651Skris			if (/^\#/) {
559109998Smarkm				@current_platforms =
560109998Smarkm				    grep(!/^$/,
561109998Smarkm					 map { $tag{$_} == 1 ? $_ :
562109998Smarkm						   $tag{$_} == -1 ? "!".$_  : "" }
563109998Smarkm					 @known_platforms);
564109998Smarkm				push @current_platforms
565109998Smarkm				    , grep(!/^$/,
566109998Smarkm					   map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ :
567109998Smarkm						     $tag{"OPENSSL_SYS_".$_} == -1 ? "!".$_  : "" }
568109998Smarkm					   @known_ossl_platforms);
569109998Smarkm				@current_algorithms =
570109998Smarkm				    grep(!/^$/,
571109998Smarkm					 map { $tag{"OPENSSL_NO_".$_} == -1 ? $_ : "" }
572109998Smarkm					 @known_algorithms);
573109998Smarkm				$def .=
574109998Smarkm				    "#INFO:"
575109998Smarkm					.join(',',@current_platforms).":"
576109998Smarkm					    .join(',',@current_algorithms).";";
57759191Skris				next;
57868651Skris			}
579109998Smarkm			if ($tag{'TRUE'} != -1) {
580109998Smarkm				if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
581109998Smarkm					next;
582109998Smarkm				} elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
583109998Smarkm					$def .= "int d2i_$3(void);";
584109998Smarkm					$def .= "int i2d_$3(void);";
585109998Smarkm					# Variant for platforms that do not
586109998Smarkm					# have to access globale variables
587109998Smarkm					# in shared libraries through functions
588109998Smarkm					$def .=
589109998Smarkm					    "#INFO:"
590109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
591109998Smarkm						    .join(',',@current_algorithms).";";
592109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
593109998Smarkm					$def .=
594109998Smarkm					    "#INFO:"
595109998Smarkm						.join(',',@current_platforms).":"
596109998Smarkm						    .join(',',@current_algorithms).";";
597109998Smarkm					# Variant for platforms that have to
598109998Smarkm					# access globale variables in shared
599109998Smarkm					# libraries through functions
600109998Smarkm					&$make_variant("$2_it","$2_it",
601109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
602109998Smarkm						      "FUNCTION");
603109998Smarkm					next;
604109998Smarkm				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS_fname\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
605109998Smarkm					$def .= "int d2i_$3(void);";
606109998Smarkm					$def .= "int i2d_$3(void);";
607109998Smarkm					$def .= "int $3_free(void);";
608109998Smarkm					$def .= "int $3_new(void);";
609109998Smarkm					# Variant for platforms that do not
610109998Smarkm					# have to access globale variables
611109998Smarkm					# in shared libraries through functions
612109998Smarkm					$def .=
613109998Smarkm					    "#INFO:"
614109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
615109998Smarkm						    .join(',',@current_algorithms).";";
616109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
617109998Smarkm					$def .=
618109998Smarkm					    "#INFO:"
619109998Smarkm						.join(',',@current_platforms).":"
620109998Smarkm						    .join(',',@current_algorithms).";";
621109998Smarkm					# Variant for platforms that have to
622109998Smarkm					# access globale variables in shared
623109998Smarkm					# libraries through functions
624109998Smarkm					&$make_variant("$2_it","$2_it",
625109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
626109998Smarkm						      "FUNCTION");
627109998Smarkm					next;
628109998Smarkm				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS\s*\(\s*(\w*)\s*\)/ ||
629109998Smarkm					 /^\s*DECLARE_ASN1_FUNCTIONS_const\s*\(\s*(\w*)\s*\)/) {
630109998Smarkm					$def .= "int d2i_$1(void);";
631109998Smarkm					$def .= "int i2d_$1(void);";
632109998Smarkm					$def .= "int $1_free(void);";
633109998Smarkm					$def .= "int $1_new(void);";
634109998Smarkm					# Variant for platforms that do not
635109998Smarkm					# have to access globale variables
636109998Smarkm					# in shared libraries through functions
637109998Smarkm					$def .=
638109998Smarkm					    "#INFO:"
639109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
640109998Smarkm						    .join(',',@current_algorithms).";";
641109998Smarkm					$def .= "OPENSSL_EXTERN int $1_it;";
642109998Smarkm					$def .=
643109998Smarkm					    "#INFO:"
644109998Smarkm						.join(',',@current_platforms).":"
645109998Smarkm						    .join(',',@current_algorithms).";";
646109998Smarkm					# Variant for platforms that have to
647109998Smarkm					# access globale variables in shared
648109998Smarkm					# libraries through functions
649109998Smarkm					&$make_variant("$1_it","$1_it",
650109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
651109998Smarkm						      "FUNCTION");
652109998Smarkm					next;
653109998Smarkm				} elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS_const\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
654109998Smarkm					$def .= "int d2i_$2(void);";
655109998Smarkm					$def .= "int i2d_$2(void);";
656109998Smarkm					# Variant for platforms that do not
657109998Smarkm					# have to access globale variables
658109998Smarkm					# in shared libraries through functions
659109998Smarkm					$def .=
660109998Smarkm					    "#INFO:"
661109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
662109998Smarkm						    .join(',',@current_algorithms).";";
663109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
664109998Smarkm					$def .=
665109998Smarkm					    "#INFO:"
666109998Smarkm						.join(',',@current_platforms).":"
667109998Smarkm						    .join(',',@current_algorithms).";";
668109998Smarkm					# Variant for platforms that have to
669109998Smarkm					# access globale variables in shared
670109998Smarkm					# libraries through functions
671109998Smarkm					&$make_variant("$2_it","$2_it",
672109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
673109998Smarkm						      "FUNCTION");
674109998Smarkm					next;
675109998Smarkm				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
676109998Smarkm					$def .= "int d2i_$2(void);";
677109998Smarkm					$def .= "int i2d_$2(void);";
678109998Smarkm					$def .= "int $2_free(void);";
679109998Smarkm					$def .= "int $2_new(void);";
680109998Smarkm					# Variant for platforms that do not
681109998Smarkm					# have to access globale variables
682109998Smarkm					# in shared libraries through functions
683109998Smarkm					$def .=
684109998Smarkm					    "#INFO:"
685109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
686109998Smarkm						    .join(',',@current_algorithms).";";
687109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
688109998Smarkm					$def .=
689109998Smarkm					    "#INFO:"
690109998Smarkm						.join(',',@current_platforms).":"
691109998Smarkm						    .join(',',@current_algorithms).";";
692109998Smarkm					# Variant for platforms that have to
693109998Smarkm					# access globale variables in shared
694109998Smarkm					# libraries through functions
695109998Smarkm					&$make_variant("$2_it","$2_it",
696109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
697109998Smarkm						      "FUNCTION");
698109998Smarkm					next;
699109998Smarkm				} elsif (/^\s*DECLARE_ASN1_ITEM\s*\(\s*(\w*)\s*\)/) {
700109998Smarkm					# Variant for platforms that do not
701109998Smarkm					# have to access globale variables
702109998Smarkm					# in shared libraries through functions
703109998Smarkm					$def .=
704109998Smarkm					    "#INFO:"
705109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
706109998Smarkm						    .join(',',@current_algorithms).";";
707109998Smarkm					$def .= "OPENSSL_EXTERN int $1_it;";
708109998Smarkm					$def .=
709109998Smarkm					    "#INFO:"
710109998Smarkm						.join(',',@current_platforms).":"
711109998Smarkm						    .join(',',@current_algorithms).";";
712109998Smarkm					# Variant for platforms that have to
713109998Smarkm					# access globale variables in shared
714109998Smarkm					# libraries through functions
715109998Smarkm					&$make_variant("$1_it","$1_it",
716109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
717109998Smarkm						      "FUNCTION");
718109998Smarkm					next;
719109998Smarkm				} elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
720109998Smarkm					next;
721109998Smarkm				} elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) {
722109998Smarkm					next;
723109998Smarkm				} elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
724109998Smarkm					 /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ) {
725109998Smarkm					# Things not in Win16
726109998Smarkm					$def .=
727109998Smarkm					    "#INFO:"
728109998Smarkm						.join(',',"!WIN16",@current_platforms).":"
729109998Smarkm						    .join(',',@current_algorithms).";";
730109998Smarkm					$def .= "int PEM_read_$1(void);";
731109998Smarkm					$def .= "int PEM_write_$1(void);";
732109998Smarkm					$def .=
733109998Smarkm					    "#INFO:"
734109998Smarkm						.join(',',@current_platforms).":"
735109998Smarkm						    .join(',',@current_algorithms).";";
736109998Smarkm					# Things that are everywhere
737109998Smarkm					$def .= "int PEM_read_bio_$1(void);";
738109998Smarkm					$def .= "int PEM_write_bio_$1(void);";
739109998Smarkm					next;
740109998Smarkm				} elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
741109998Smarkm					 /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
742109998Smarkm					# Things not in Win16
743109998Smarkm					$def .=
744109998Smarkm					    "#INFO:"
745109998Smarkm						.join(',',"!WIN16",@current_platforms).":"
746109998Smarkm						    .join(',',@current_algorithms).";";
747109998Smarkm					$def .= "int PEM_write_$1(void);";
748109998Smarkm					$def .=
749109998Smarkm					    "#INFO:"
750109998Smarkm						.join(',',@current_platforms).":"
751109998Smarkm						    .join(',',@current_algorithms).";";
752109998Smarkm					# Things that are everywhere
753109998Smarkm					$def .= "int PEM_write_bio_$1(void);";
754109998Smarkm					next;
755109998Smarkm				} elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
756109998Smarkm					 /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
757109998Smarkm					# Things not in Win16
758109998Smarkm					$def .=
759109998Smarkm					    "#INFO:"
760109998Smarkm						.join(',',"!WIN16",@current_platforms).":"
761109998Smarkm						    .join(',',@current_algorithms).";";
762109998Smarkm					$def .= "int PEM_read_$1(void);";
763109998Smarkm					$def .=
764109998Smarkm					    "#INFO:"
765109998Smarkm						.join(',',@current_platforms).":"
766109998Smarkm						    .join(',',@current_algorithms).";";
767109998Smarkm					# Things that are everywhere
768109998Smarkm					$def .= "int PEM_read_bio_$1(void);";
769109998Smarkm					next;
770109998Smarkm				} elsif (/^OPENSSL_DECLARE_GLOBAL\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
771109998Smarkm					# Variant for platforms that do not
772109998Smarkm					# have to access globale variables
773109998Smarkm					# in shared libraries through functions
774109998Smarkm					$def .=
775109998Smarkm					    "#INFO:"
776109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
777109998Smarkm						    .join(',',@current_algorithms).";";
778109998Smarkm					$def .= "OPENSSL_EXTERN int _shadow_$2;";
779109998Smarkm					$def .=
780109998Smarkm					    "#INFO:"
781109998Smarkm						.join(',',@current_platforms).":"
782109998Smarkm						    .join(',',@current_algorithms).";";
783109998Smarkm					# Variant for platforms that have to
784109998Smarkm					# access globale variables in shared
785109998Smarkm					# libraries through functions
786109998Smarkm					&$make_variant("_shadow_$2","_shadow_$2",
787109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
788109998Smarkm						      "FUNCTION");
789109998Smarkm				} elsif ($tag{'CONST_STRICT'} != 1) {
79068651Skris					if (/\{|\/\*|\([^\)]*$/) {
79155714Skris						$line = $_;
79255714Skris					} else {
79355714Skris						$def .= $_;
79455714Skris					}
79555714Skris				}
79655714Skris			}
797109998Smarkm		}
79855714Skris		close(IN);
79955714Skris
800142425Snectar		my $algs = '';
80168651Skris		my $plays;
80268651Skris
803109998Smarkm		print STDERR "DEBUG: postprocessing ----------\n" if $debug;
80455714Skris		foreach (split /;/, $def) {
80568651Skris			my $s; my $k = "FUNCTION"; my $p; my $a;
80655714Skris			s/^[\n\s]*//g;
80755714Skris			s/[\n\s]*$//g;
80868651Skris			next if(/\#undef/);
80955714Skris			next if(/typedef\W/);
81068651Skris			next if(/\#define/);
81168651Skris
812109998Smarkm			print STDERR "DEBUG: \$_ = \"$_\"\n" if $debug;
81368651Skris			if (/^\#INFO:([^:]*):(.*)$/) {
81468651Skris				$plats = $1;
81568651Skris				$algs = $2;
816109998Smarkm				print STDERR "DEBUG: found info on platforms ($plats) and algorithms ($algs)\n" if $debug;
81768651Skris				next;
818109998Smarkm			} elsif (/^\s*OPENSSL_EXTERN\s.*?(\w+(\{[0-9]+\})?)(\[[0-9]*\])*\s*$/) {
81968651Skris				$s = $1;
82068651Skris				$k = "VARIABLE";
821109998Smarkm				print STDERR "DEBUG: found external variable $s\n" if $debug;
822109998Smarkm			} elsif (/\(\*(\w*(\{[0-9]+\})?)\([^\)]+/) {
82368651Skris				$s = $1;
824109998Smarkm				print STDERR "DEBUG: found ANSI C function $s\n" if $debug;
825120631Snectar			} elsif (/\w+\W+(\w+)\W*\(\s*\)(\s*__attribute__\(.*\)\s*)?$/s) {
82655714Skris				# K&R C
827109998Smarkm				print STDERR "DEBUG: found K&R C function $s\n" if $debug;
82855714Skris				next;
829120631Snectar			} elsif (/\w+\W+\w+(\{[0-9]+\})?\W*\(.*\)(\s*__attribute__\(.*\)\s*)?$/s) {
830120631Snectar				while (not /\(\)(\s*__attribute__\(.*\)\s*)?$/s) {
831120631Snectar					s/[^\(\)]*\)(\s*__attribute__\(.*\)\s*)?$/\)/s;
832120631Snectar					s/\([^\(\)]*\)\)(\s*__attribute__\(.*\)\s*)?$/\)/s;
83355714Skris				}
83455714Skris				s/\(void\)//;
835109998Smarkm				/(\w+(\{[0-9]+\})?)\W*\(\)/s;
83668651Skris				$s = $1;
837109998Smarkm				print STDERR "DEBUG: found function $s\n" if $debug;
83855714Skris			} elsif (/\(/ and not (/=/)) {
83955714Skris				print STDERR "File $file: cannot parse: $_;\n";
84068651Skris				next;
84168651Skris			} else {
84268651Skris				next;
84355714Skris			}
84468651Skris
84568651Skris			$syms{$s} = 1;
84668651Skris			$kind{$s} = $k;
84768651Skris
84868651Skris			$p = $plats;
84968651Skris			$a = $algs;
85068651Skris			$a .= ",BF" if($s =~ /EVP_bf/);
85168651Skris			$a .= ",CAST" if($s =~ /EVP_cast/);
85268651Skris			$a .= ",DES" if($s =~ /EVP_des/);
85368651Skris			$a .= ",DSA" if($s =~ /EVP_dss/);
85468651Skris			$a .= ",IDEA" if($s =~ /EVP_idea/);
85568651Skris			$a .= ",MD2" if($s =~ /EVP_md2/);
85668651Skris			$a .= ",MD4" if($s =~ /EVP_md4/);
85768651Skris			$a .= ",MD5" if($s =~ /EVP_md5/);
85868651Skris			$a .= ",RC2" if($s =~ /EVP_rc2/);
85968651Skris			$a .= ",RC4" if($s =~ /EVP_rc4/);
86068651Skris			$a .= ",RC5" if($s =~ /EVP_rc5/);
86168651Skris			$a .= ",RIPEMD" if($s =~ /EVP_ripemd/);
86268651Skris			$a .= ",SHA" if($s =~ /EVP_sha/);
86368651Skris			$a .= ",RSA" if($s =~ /EVP_(Open|Seal)(Final|Init)/);
86468651Skris			$a .= ",RSA" if($s =~ /PEM_Seal(Final|Init|Update)/);
86568651Skris			$a .= ",RSA" if($s =~ /RSAPrivateKey/);
86668651Skris			$a .= ",RSA" if($s =~ /SSLv23?_((client|server)_)?method/);
86768651Skris
868109998Smarkm			$platform{$s} =
869109998Smarkm			    &reduce_platforms((defined($platform{$s})?$platform{$s}.',':"").$p);
870142425Snectar			$algorithm{$s} = '' if !defined $algorithm{$s};
87168651Skris			$algorithm{$s} .= ','.$a;
87268651Skris
873109998Smarkm			if (defined($variant{$s})) {
874109998Smarkm				foreach $v (split /;/,$variant{$s}) {
875109998Smarkm					(my $r, my $p, my $k) = split(/:/,$v);
876109998Smarkm					my $ip = join ',',map({ /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p);
877109998Smarkm					$syms{$r} = 1;
878109998Smarkm					if (!defined($k)) { $k = $kind{$s}; }
879109998Smarkm					$kind{$r} = $k."(".$s.")";
880109998Smarkm					$algorithm{$r} = $algorithm{$s};
881109998Smarkm					$platform{$r} = &reduce_platforms($platform{$s}.",".$p.",".$p);
882109998Smarkm					$platform{$s} = &reduce_platforms($platform{$s}.','.$ip.','.$ip);
883109998Smarkm					print STDERR "DEBUG: \$variant{\"$s\"} = ",$v,"; \$r = $r; \$p = ",$platform{$r},"; \$a = ",$algorithm{$r},"; \$kind = ",$kind{$r},"\n" if $debug;
884109998Smarkm				}
88568651Skris			}
886109998Smarkm			print STDERR "DEBUG: \$s = $s; \$p = ",$platform{$s},"; \$a = ",$algorithm{$s},"; \$kind = ",$kind{$s},"\n" if $debug;
88755714Skris		}
88855714Skris	}
88955714Skris
89068651Skris	# Prune the returned symbols
89155714Skris
89268651Skris        delete $syms{"bn_dump1"};
89368651Skris	$platform{"BIO_s_log"} .= ",!WIN32,!WIN16,!macintosh";
89468651Skris
895109998Smarkm	$platform{"PEM_read_NS_CERT_SEQ"} = "VMS";
896109998Smarkm	$platform{"PEM_write_NS_CERT_SEQ"} = "VMS";
897109998Smarkm	$platform{"PEM_read_P8_PRIV_KEY_INFO"} = "VMS";
898109998Smarkm	$platform{"PEM_write_P8_PRIV_KEY_INFO"} = "VMS";
89968651Skris
90068651Skris	# Info we know about
90168651Skris
90268651Skris	push @ret, map { $_."\\".&info_string($_,"EXIST",
90368651Skris					      $platform{$_},
90468651Skris					      $kind{$_},
90568651Skris					      $algorithm{$_}) } keys %syms;
90668651Skris
907109998Smarkm	if (keys %unknown_algorithms) {
908109998Smarkm		print STDERR "WARNING: mkdef.pl doesn't know the following algorithms:\n";
909109998Smarkm		print STDERR "\t",join("\n\t",keys %unknown_algorithms),"\n";
910109998Smarkm	}
91168651Skris	return(@ret);
91268651Skris}
91368651Skris
914109998Smarkm# Param: string of comma-separated platform-specs.
915109998Smarkmsub reduce_platforms
916109998Smarkm{
917109998Smarkm	my ($platforms) = @_;
91868651Skris	my $pl = defined($platforms) ? $platforms : "";
91968651Skris	my %p = map { $_ => 0 } split /,/, $pl;
92068651Skris	my $ret;
92168651Skris
922109998Smarkm	print STDERR "DEBUG: Entered reduce_platforms with \"$platforms\"\n"
923109998Smarkm	    if $debug;
92468651Skris	# We do this, because if there's code like the following, it really
92568651Skris	# means the function exists in all cases and should therefore be
92668651Skris	# everywhere.  By increasing and decreasing, we may attain 0:
92768651Skris	#
92868651Skris	# ifndef WIN16
92968651Skris	#    int foo();
93068651Skris	# else
93168651Skris	#    int _fat foo();
93268651Skris	# endif
93368651Skris	foreach $platform (split /,/, $pl) {
93468651Skris		if ($platform =~ /^!(.*)$/) {
93568651Skris			$p{$1}--;
93668651Skris		} else {
93768651Skris			$p{$platform}++;
93855714Skris		}
93955714Skris	}
94068651Skris	foreach $platform (keys %p) {
94168651Skris		if ($p{$platform} == 0) { delete $p{$platform}; }
94255714Skris	}
94355714Skris
94468651Skris	delete $p{""};
945109998Smarkm
946109998Smarkm	$ret = join(',',sort(map { $p{$_} < 0 ? "!".$_ : $_ } keys %p));
947109998Smarkm	print STDERR "DEBUG: Exiting reduce_platforms with \"$ret\"\n"
948109998Smarkm	    if $debug;
949109998Smarkm	return $ret;
950109998Smarkm}
951109998Smarkm
952109998Smarkmsub info_string {
953109998Smarkm	(my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_;
954109998Smarkm
955109998Smarkm	my %a = defined($algorithms) ?
956109998Smarkm	    map { $_ => 1 } split /,/, $algorithms : ();
957109998Smarkm	my $k = defined($kind) ? $kind : "FUNCTION";
958109998Smarkm	my $ret;
959109998Smarkm	my $p = &reduce_platforms($platforms);
960109998Smarkm
96168651Skris	delete $a{""};
96255714Skris
96368651Skris	$ret = $exist;
964109998Smarkm	$ret .= ":".$p;
96568651Skris	$ret .= ":".$k;
966109998Smarkm	$ret .= ":".join(',',sort keys %a);
96768651Skris	return $ret;
96855714Skris}
96955714Skris
97068651Skrissub maybe_add_info {
97168651Skris	(my $name, *nums, my @symbols) = @_;
97268651Skris	my $sym;
97368651Skris	my $new_info = 0;
974109998Smarkm	my %syms=();
97568651Skris
97668651Skris	print STDERR "Updating $name info\n";
97768651Skris	foreach $sym (@symbols) {
97868651Skris		(my $s, my $i) = split /\\/, $sym;
97968651Skris		if (defined($nums{$s})) {
980109998Smarkm			$i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/;
98168651Skris			(my $n, my $dummy) = split /\\/, $nums{$s};
98268651Skris			if (!defined($dummy) || $i ne $dummy) {
98368651Skris				$nums{$s} = $n."\\".$i;
98468651Skris				$new_info++;
985109998Smarkm				print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n" if $debug;
98668651Skris			}
98768651Skris		}
988109998Smarkm		$syms{$s} = 1;
98968651Skris	}
990109998Smarkm
991109998Smarkm	my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
992109998Smarkm	foreach $sym (@s) {
993109998Smarkm		(my $n, my $i) = split /\\/, $nums{$sym};
994109998Smarkm		if (!defined($syms{$sym}) && $i !~ /^NOEXIST:/) {
995109998Smarkm			$new_info++;
996109998Smarkm			print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n" if $debug;
997109998Smarkm		}
998109998Smarkm	}
99968651Skris	if ($new_info) {
100068651Skris		print STDERR "$new_info old symbols got an info update\n";
100168651Skris		if (!$do_rewrite) {
100268651Skris			print STDERR "You should do a rewrite to fix this.\n";
100368651Skris		}
100468651Skris	} else {
100568651Skris		print STDERR "No old symbols needed info update\n";
100668651Skris	}
100768651Skris}
100868651Skris
1009109998Smarkm# Param: string of comma-separated keywords, each possibly prefixed with a "!"
1010109998Smarkmsub is_valid
1011109998Smarkm{
1012109998Smarkm	my ($keywords_txt,$platforms) = @_;
1013109998Smarkm	my (@keywords) = split /,/,$keywords_txt;
1014109998Smarkm	my ($falsesum, $truesum) = (0, !grep(/^[^!]/,@keywords));
1015109998Smarkm
1016109998Smarkm	# Param: one keyword
1017109998Smarkm	sub recognise
1018109998Smarkm	{
1019109998Smarkm		my ($keyword,$platforms) = @_;
1020109998Smarkm
1021109998Smarkm		if ($platforms) {
1022109998Smarkm			# platforms
1023109998Smarkm			if ($keyword eq "VMS" && $VMS) { return 1; }
1024109998Smarkm			if ($keyword eq "WIN32" && $W32) { return 1; }
1025109998Smarkm			if ($keyword eq "WIN16" && $W16) { return 1; }
1026109998Smarkm			if ($keyword eq "WINNT" && $NT) { return 1; }
1027109998Smarkm			if ($keyword eq "OS2" && $OS2) { return 1; }
1028109998Smarkm			# Special platforms:
1029109998Smarkm			# EXPORT_VAR_AS_FUNCTION means that global variables
1030109998Smarkm			# will be represented as functions.  This currently
1031109998Smarkm			# only happens on VMS-VAX.
1032109998Smarkm			if ($keyword eq "EXPORT_VAR_AS_FUNCTION" && ($VMSVAX || $W32 || $W16)) {
1033109998Smarkm				return 1;
1034109998Smarkm			}
1035142425Snectar			if ($keyword eq "OPENSSL_FIPS" && $fips) {
1036142425Snectar				return 1;
1037142425Snectar			}
1038109998Smarkm			return 0;
1039109998Smarkm		} else {
1040109998Smarkm			# algorithms
1041109998Smarkm			if ($keyword eq "RC2" && $no_rc2) { return 0; }
1042109998Smarkm			if ($keyword eq "RC4" && $no_rc4) { return 0; }
1043109998Smarkm			if ($keyword eq "RC5" && $no_rc5) { return 0; }
1044109998Smarkm			if ($keyword eq "IDEA" && $no_idea) { return 0; }
1045109998Smarkm			if ($keyword eq "DES" && $no_des) { return 0; }
1046109998Smarkm			if ($keyword eq "BF" && $no_bf) { return 0; }
1047109998Smarkm			if ($keyword eq "CAST" && $no_cast) { return 0; }
1048109998Smarkm			if ($keyword eq "MD2" && $no_md2) { return 0; }
1049109998Smarkm			if ($keyword eq "MD4" && $no_md4) { return 0; }
1050109998Smarkm			if ($keyword eq "MD5" && $no_md5) { return 0; }
1051109998Smarkm			if ($keyword eq "SHA" && $no_sha) { return 0; }
1052109998Smarkm			if ($keyword eq "RIPEMD" && $no_ripemd) { return 0; }
1053109998Smarkm			if ($keyword eq "MDC2" && $no_mdc2) { return 0; }
1054109998Smarkm			if ($keyword eq "RSA" && $no_rsa) { return 0; }
1055109998Smarkm			if ($keyword eq "DSA" && $no_dsa) { return 0; }
1056109998Smarkm			if ($keyword eq "DH" && $no_dh) { return 0; }
1057109998Smarkm			if ($keyword eq "EC" && $no_ec) { return 0; }
1058109998Smarkm			if ($keyword eq "HMAC" && $no_hmac) { return 0; }
1059109998Smarkm			if ($keyword eq "AES" && $no_aes) { return 0; }
1060109998Smarkm			if ($keyword eq "EVP" && $no_evp) { return 0; }
1061109998Smarkm			if ($keyword eq "LHASH" && $no_lhash) { return 0; }
1062109998Smarkm			if ($keyword eq "STACK" && $no_stack) { return 0; }
1063109998Smarkm			if ($keyword eq "ERR" && $no_err) { return 0; }
1064109998Smarkm			if ($keyword eq "BUFFER" && $no_buffer) { return 0; }
1065109998Smarkm			if ($keyword eq "BIO" && $no_bio) { return 0; }
1066109998Smarkm			if ($keyword eq "COMP" && $no_comp) { return 0; }
1067109998Smarkm			if ($keyword eq "DSO" && $no_dso) { return 0; }
1068109998Smarkm			if ($keyword eq "KRB5" && $no_krb5) { return 0; }
1069111147Snectar			if ($keyword eq "ENGINE" && $no_engine) { return 0; }
1070111147Snectar			if ($keyword eq "HW" && $no_hw) { return 0; }
1071109998Smarkm			if ($keyword eq "FP_API" && $no_fp_api) { return 0; }
1072109998Smarkm
1073109998Smarkm			# Nothing recognise as true
1074109998Smarkm			return 1;
1075109998Smarkm		}
1076109998Smarkm	}
1077109998Smarkm
1078109998Smarkm	foreach $k (@keywords) {
1079109998Smarkm		if ($k =~ /^!(.*)$/) {
1080109998Smarkm			$falsesum += &recognise($1,$platforms);
1081109998Smarkm		} else {
1082109998Smarkm			$truesum += &recognise($k,$platforms);
1083109998Smarkm		}
1084109998Smarkm	}
1085109998Smarkm	print STDERR "DEBUG: [",$#keywords,",",$#keywords < 0,"] is_valid($keywords_txt) => (\!$falsesum) && $truesum = ",(!$falsesum) && $truesum,"\n" if $debug;
1086109998Smarkm	return (!$falsesum) && $truesum;
1087109998Smarkm}
1088109998Smarkm
108959191Skrissub print_test_file
109059191Skris{
1091109998Smarkm	(*OUT,my $name,*nums,my $testall,my @symbols)=@_;
109259191Skris	my $n = 1; my @e; my @r;
109368651Skris	my $sym; my $prev = ""; my $prefSSLeay;
109459191Skris
1095109998Smarkm	(@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
1096109998Smarkm	(@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:.*/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
109768651Skris	@symbols=((sort @e),(sort @r));
109859191Skris
109968651Skris	foreach $sym (@symbols) {
110068651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1101109998Smarkm		my $v = 0;
1102109998Smarkm		$v = 1 if $i=~ /^.*?:.*?:VARIABLE/;
1103109998Smarkm		my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1104109998Smarkm		my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1105109998Smarkm		if (!defined($nums{$s})) {
1106109998Smarkm			print STDERR "Warning: $s does not have a number assigned\n"
1107109998Smarkm			    if(!$do_update);
1108109998Smarkm		} elsif (is_valid($p,1) && is_valid($a,0)) {
1109109998Smarkm			my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1110109998Smarkm			if ($prev eq $s2) {
1111109998Smarkm				print OUT "\t/* The following has already appeared previously */\n";
1112109998Smarkm				print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1113109998Smarkm			}
1114109998Smarkm			$prev = $s2;	# To warn about duplicates...
1115109998Smarkm
1116109998Smarkm			($nn,$ni)=($nums{$s2} =~ /^(.*?)\\(.*)$/);
1117109998Smarkm			if ($v) {
1118109998Smarkm				print OUT "\textern int $s2; /* type unknown */ /* $nn $ni */\n";
111968651Skris			} else {
1120109998Smarkm				print OUT "\textern int $s2(); /* type unknown */ /* $nn $ni */\n";
112168651Skris			}
112259191Skris		}
112359191Skris	}
112459191Skris}
112559191Skris
1126127128Snectarsub get_version {
1127127128Snectar   local *MF;
1128127128Snectar   my $v = '?';
1129142425Snectar   open MF, 'Makefile' or return $v;
1130127128Snectar   while (<MF>) {
1131127128Snectar     $v = $1, last if /^VERSION=(.*?)\s*$/;
1132127128Snectar   }
1133127128Snectar   close MF;
1134127128Snectar   return $v;
1135127128Snectar}
1136127128Snectar
113755714Skrissub print_def_file
113855714Skris{
113968651Skris	(*OUT,my $name,*nums,my @symbols)=@_;
1140109998Smarkm	my $n = 1; my @e; my @r; my @v; my $prev="";
1141109998Smarkm	my $liboptions="";
1142127128Snectar	my $libname = $name;
1143127128Snectar	my $http_vendor = 'www.openssl.org/';
1144127128Snectar	my $version = get_version();
1145127128Snectar	my $what = "OpenSSL: implementation of Secure Socket Layer";
1146127128Snectar	my $description = "$what $version, $name - http://$http_vendor";
114755714Skris
114855714Skris	if ($W32)
1149127128Snectar		{ $libname.="32"; }
1150109998Smarkm	elsif ($W16)
1151127128Snectar		{ $libname.="16"; }
1152109998Smarkm	elsif ($OS2)
1153127128Snectar		{ # DLL names should not clash on the whole system.
1154127128Snectar		  # However, they should not have any particular relationship
1155127128Snectar		  # to the name of the static library.  Chose descriptive names
1156127128Snectar		  # (must be at most 8 chars).
1157127128Snectar		  my %translate = (ssl => 'open_ssl', crypto => 'cryptssl');
1158127128Snectar		  $libname = $translate{$name} || $name;
1159127128Snectar		  $liboptions = <<EOO;
1160127128SnectarINITINSTANCE
1161127128SnectarDATA MULTIPLE NONSHARED
1162127128SnectarEOO
1163127128Snectar		  # Vendor field can't contain colon, drat; so we omit http://
1164127128Snectar		  $description = "\@#$http_vendor:$version#\@$what; DLL for library $name.  Build for EMX -Zmtd";
1165127128Snectar		}
116655714Skris
116755714Skris	print OUT <<"EOF";
116855714Skris;
116955714Skris; Definition file for the DLL version of the $name library from OpenSSL
117055714Skris;
117155714Skris
1172127128SnectarLIBRARY         $libname	$liboptions
117355714Skris
1174127128SnectarDESCRIPTION     '$description'
117555714Skris
117655714SkrisEOF
117755714Skris
1178109998Smarkm	if ($W16) {
117955714Skris		print <<"EOF";
118055714SkrisCODE            PRELOAD MOVEABLE
118155714SkrisDATA            PRELOAD MOVEABLE SINGLE
118255714Skris
118355714SkrisEXETYPE		WINDOWS
118455714Skris
118555714SkrisHEAPSIZE	4096
118655714SkrisSTACKSIZE	8192
118755714Skris
118855714SkrisEOF
118955714Skris	}
119055714Skris
119155714Skris	print "EXPORTS\n";
119255714Skris
1193109998Smarkm	(@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1194109998Smarkm	(@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:FUNCTION/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1195109998Smarkm	(@v)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:VARIABLE/,@symbols);
1196109998Smarkm	@symbols=((sort @e),(sort @r), (sort @v));
119755714Skris
119855714Skris
119968651Skris	foreach $sym (@symbols) {
120068651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1201109998Smarkm		my $v = 0;
1202109998Smarkm		$v = 1 if $i =~ /^.*?:.*?:VARIABLE/;
120368651Skris		if (!defined($nums{$s})) {
120468651Skris			printf STDERR "Warning: $s does not have a number assigned\n"
1205109998Smarkm			    if(!$do_update);
120655714Skris		} else {
1207109998Smarkm			(my $n, my $dummy) = split /\\/, $nums{$s};
120868651Skris			my %pf = ();
1209109998Smarkm			my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1210109998Smarkm			my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1211109998Smarkm			if (is_valid($p,1) && is_valid($a,0)) {
1212109998Smarkm				my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1213109998Smarkm				if ($prev eq $s2) {
1214109998Smarkm					print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1215109998Smarkm				}
1216109998Smarkm				$prev = $s2;	# To warn about duplicates...
1217109998Smarkm				if($v && !$OS2) {
1218109998Smarkm					printf OUT "    %s%-39s @%-8d DATA\n",($W32)?"":"_",$s2,$n;
1219109998Smarkm				} else {
1220109998Smarkm					printf OUT "    %s%-39s @%d\n",($W32||$OS2)?"":"_",$s2,$n;
1221109998Smarkm				}
122268651Skris			}
122355714Skris		}
122455714Skris	}
122555714Skris	printf OUT "\n";
122655714Skris}
122755714Skris
122855714Skrissub load_numbers
122955714Skris{
123055714Skris	my($name)=@_;
123155714Skris	my(@a,%ret);
123255714Skris
123355714Skris	$max_num = 0;
123468651Skris	$num_noinfo = 0;
123568651Skris	$prev = "";
1236109998Smarkm	$prev_cnt = 0;
123755714Skris
123855714Skris	open(IN,"<$name") || die "unable to open $name:$!\n";
123955714Skris	while (<IN>) {
124055714Skris		chop;
124155714Skris		s/#.*$//;
124255714Skris		next if /^\s*$/;
124355714Skris		@a=split;
124468651Skris		if (defined $ret{$a[0]}) {
1245109998Smarkm			# This is actually perfectly OK
1246109998Smarkm			#print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n";
124768651Skris		}
124868651Skris		if ($max_num > $a[1]) {
124968651Skris			print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n";
125068651Skris		}
1251109998Smarkm		elsif ($max_num == $a[1]) {
125268651Skris			# This is actually perfectly OK
125368651Skris			#print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n";
1254109998Smarkm			if ($a[0] eq $prev) {
1255109998Smarkm				$prev_cnt++;
1256109998Smarkm				$a[0] .= "{$prev_cnt}";
1257109998Smarkm			}
125868651Skris		}
1259109998Smarkm		else {
1260109998Smarkm			$prev_cnt = 0;
1261109998Smarkm		}
126268651Skris		if ($#a < 2) {
126368651Skris			# Existence will be proven later, in do_defs
126468651Skris			$ret{$a[0]}=$a[1];
126568651Skris			$num_noinfo++;
126668651Skris		} else {
126768651Skris			$ret{$a[0]}=$a[1]."\\".$a[2]; # \\ is a special marker
126868651Skris		}
126955714Skris		$max_num = $a[1] if $a[1] > $max_num;
127068651Skris		$prev=$a[0];
127155714Skris	}
127268651Skris	if ($num_noinfo) {
127368651Skris		print STDERR "Warning: $num_noinfo symbols were without info.";
127468651Skris		if ($do_rewrite) {
127568651Skris			printf STDERR "  The rewrite will fix this.\n";
127668651Skris		} else {
127768651Skris			printf STDERR "  You should do a rewrite to fix this.\n";
127868651Skris		}
127968651Skris	}
128055714Skris	close(IN);
128155714Skris	return(%ret);
128255714Skris}
128355714Skris
128468651Skrissub parse_number
128568651Skris{
128668651Skris	(my $str, my $what) = @_;
128768651Skris	(my $n, my $i) = split(/\\/,$str);
128868651Skris	if ($what eq "n") {
128968651Skris		return $n;
129068651Skris	} else {
129168651Skris		return $i;
129268651Skris	}
129368651Skris}
129468651Skris
129568651Skrissub rewrite_numbers
129668651Skris{
129768651Skris	(*OUT,$name,*nums,@symbols)=@_;
129868651Skris	my $thing;
129968651Skris
130068651Skris	print STDERR "Rewriting $name\n";
130168651Skris
1302109998Smarkm	my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
130368651Skris	my $r; my %r; my %rsyms;
130468651Skris	foreach $r (@r) {
130568651Skris		(my $s, my $i) = split /\\/, $r;
130668651Skris		my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
130768651Skris		$i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
130868651Skris		$r{$a} = $s."\\".$i;
130968651Skris		$rsyms{$s} = 1;
131068651Skris	}
131168651Skris
1312109998Smarkm	my %syms = ();
1313109998Smarkm	foreach $_ (@symbols) {
1314109998Smarkm		(my $n, my $i) = split /\\/;
1315109998Smarkm		$syms{$n} = 1;
1316109998Smarkm	}
1317109998Smarkm
1318109998Smarkm	my @s=sort {
1319109998Smarkm	    &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n")
1320109998Smarkm	    || $a cmp $b
1321109998Smarkm	} keys %nums;
132268651Skris	foreach $sym (@s) {
132368651Skris		(my $n, my $i) = split /\\/, $nums{$sym};
132468651Skris		next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/;
132568651Skris		next if defined($rsyms{$sym});
1326109998Smarkm		print STDERR "DEBUG: rewrite_numbers for sym = ",$sym,": i = ",$i,", n = ",$n,", rsym{sym} = ",$rsyms{$sym},"syms{sym} = ",$syms{$sym},"\n" if $debug;
1327109998Smarkm		$i="NOEXIST::FUNCTION:"
1328109998Smarkm			if !defined($i) || $i eq "" || !defined($syms{$sym});
1329109998Smarkm		my $s2 = $sym;
1330109998Smarkm		$s2 =~ s/\{[0-9]+\}$//;
1331109998Smarkm		printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i;
133268651Skris		if (exists $r{$sym}) {
133368651Skris			(my $s, $i) = split /\\/,$r{$sym};
1334109998Smarkm			my $s2 = $s;
1335109998Smarkm			$s2 =~ s/\{[0-9]+\}$//;
1336109998Smarkm			printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i;
133768651Skris		}
133868651Skris	}
133968651Skris}
134068651Skris
134155714Skrissub update_numbers
134255714Skris{
134368651Skris	(*OUT,$name,*nums,my $start_num, my @symbols)=@_;
134468651Skris	my $new_syms = 0;
134568651Skris
134668651Skris	print STDERR "Updating $name numbers\n";
134768651Skris
1348109998Smarkm	my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
134968651Skris	my $r; my %r; my %rsyms;
135068651Skris	foreach $r (@r) {
135168651Skris		(my $s, my $i) = split /\\/, $r;
135268651Skris		my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
135368651Skris		$i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
135468651Skris		$r{$a} = $s."\\".$i;
135568651Skris		$rsyms{$s} = 1;
135668651Skris	}
135768651Skris
135868651Skris	foreach $sym (@symbols) {
135968651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
136068651Skris		next if $i =~ /^.*?:.*?:\w+\(\w+\)/;
136168651Skris		next if defined($rsyms{$sym});
136268651Skris		die "ERROR: Symbol $sym had no info attached to it."
136368651Skris		    if $i eq "";
136468651Skris		if (!exists $nums{$s}) {
136568651Skris			$new_syms++;
1366109998Smarkm			my $s2 = $s;
1367109998Smarkm			$s2 =~ s/\{[0-9]+\}$//;
1368109998Smarkm			printf OUT "%s%-39s %d\t%s\n","",$s2, ++$start_num,$i;
136968651Skris			if (exists $r{$s}) {
137068651Skris				($s, $i) = split /\\/,$r{$s};
1371109998Smarkm				$s =~ s/\{[0-9]+\}$//;
1372109998Smarkm				printf OUT "%s%-39s %d\t%s\n","",$s, $start_num,$i;
137368651Skris			}
137455714Skris		}
137555714Skris	}
137668651Skris	if($new_syms) {
137768651Skris		print STDERR "$new_syms New symbols added\n";
137855714Skris	} else {
137968651Skris		print STDERR "No New symbols Added\n";
138055714Skris	}
138155714Skris}
138268651Skris
138368651Skrissub check_existing
138468651Skris{
138568651Skris	(*nums, my @symbols)=@_;
138668651Skris	my %existing; my @remaining;
138768651Skris	@remaining=();
138868651Skris	foreach $sym (@symbols) {
138968651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
139068651Skris		$existing{$s}=1;
139168651Skris	}
139268651Skris	foreach $sym (keys %nums) {
139368651Skris		if (!exists $existing{$sym}) {
139468651Skris			push @remaining, $sym;
139568651Skris		}
139668651Skris	}
139768651Skris	if(@remaining) {
139868651Skris		print STDERR "The following symbols do not seem to exist:\n";
139968651Skris		foreach $sym (@remaining) {
140068651Skris			print STDERR "\t",$sym,"\n";
140168651Skris		}
140268651Skris	}
140368651Skris}
140468651Skris
1405