aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/err/err_code.pl
blob: 8b510c96fcf88135aad20a222d1e754ab0e881c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/local/bin/perl -w

# Modified by Steve Henson. It should now read in the .err
# file and only add new error codes, retaining the old
# numbers. 

# Before it re-sorted new codes and re-ordered the whole thing. 
# This is the motivation for the change: the re numbering caused large
# patch files even if only one error or reason code was added.
# To force regeneration of all error codes (the old behaviour) use the
# -regen flag.

$regen = 0;

while (@ARGV)
	{
	$in=shift(@ARGV);
	if ($in =~ /^-conf$/)
		{
		$in=shift(@ARGV);
		open(IN,"<$in") || die "unable to open '$in'\n";
		while (<IN>)
			{
			s/#.*$//;
			s/\s+$//;
			next if (/^$/);
			if (/^L\s+(\S+)\s+(\S+)$/)
				{ $errfile{$1}=$2; }
			elsif (/^F\s+(\S+)$/)
				{ $function{$1}=1; }
			elsif (/^R\s+(\S+)\s+(\S+)$/)
				{ $r_value{$1}=$2; }
			else { die "bad input line: $in:$.\n"; }
			}
		close(IN);
		next;
		}
	elsif ($in =~ /^-regen/)
		{
		$regen = 1;
		next;
	}

	open(IN,"<$in") || die "unable to open '$in'\n";
	$last="";
	while (<IN>)
		{
		if (/err\(([A-Z0-9]+_F_[0-9A-Z_]+)\s*,\s*([0-9A-Z]+_R_[0-9A-Z_]+)\s*\)/)
			{
# Not sure what this was supposed to be for: it's broken anyway [steve]
#			if ($1 != $last)
#				{
#				if ($function{$1} == 0)
#					{
#					printf STDERR "$. $1 is bad\n";
#					}
#				}
			$function{$1}++;
			$last=$1;
			$reason{$2}++;
			}
		}
	close(IN);
	}

foreach (keys %function,keys %reason)
	{
	/^([A-Z0-9]+)_/;
	$prefix{$1}++;
	}

@F=sort keys %function;
@R=sort keys %reason;
foreach $j (sort keys %prefix)
	{
	next if !defined $errfile{$j};
	next if $errfile{$j} eq "NONE";
	printf STDERR "doing %-6s - ",$j;
	@f=grep(/^${j}_/,@F);
	@r=grep(/^${j}_/,@R);
	if (defined($errfile{$j}))
		{
		read_errcodes($errfile{$j});
		# Check to see if any new codes: if not ignore.
		$new_codes = 0;
		foreach (@f) {
			if(!exists $func_codes{$_}) {
				$new_codes = 1;
				last;
			}
		}
		if(!$new_codes) {
			foreach (@r) {
				if(!exists $reason_codes{$_}) {
					$new_codes = 1;
					last;
				}
			}
		}
		if(!$new_codes) {
			print STDERR "No New Codes\n";
			next;
		}
		open(OUT,">$errfile{$j}") ||
			die "unable to open '$errfile{$j}':$!\n";
		$close_file=1;
		}
	else
		{
		$min_func = 100;
		$min_reason = 100;
		*OUT=*STDOUT;
		$close_file=0;
		}
	$num=$min_func;
	print OUT "/* Error codes for the $j functions. */\n\n";
	print OUT "/* Function codes. */\n";
	$f_count=0;
	foreach $i (@f)
		{
		$z=6-int(length($i)/8);
		if(exists $func_codes{$i}) {
			printf OUT "#define $i%s $func_codes{$i}\n","\t" x $z;
		} else {
			printf OUT "#define $i%s $num\n","\t" x $z;
			$num++;
		}
		$f_count++;
		}
	$num=$min_reason;
	print OUT "\n/* Reason codes. */\n";
	$r_count=0;
	foreach $i (@r)
		{
		$z=6-int(length($i)/8);
		if (exists $reason_codes{$i}) {
			printf OUT "#define $i%s $reason_codes{$i}\n","\t" x $z;
		} elsif (exists $r_value{$i}) {
			printf OUT "#define $i%s $r_value{$i}\n","\t" x $z;
		} else {
			printf OUT "#define $i%s $num\n","\t" x $z;
			$num++;
		}
		$r_count++;
		}
	close(OUT) if $close_file;

	printf STDERR "%3d functions, %3d reasons\n",$f_count,$r_count;
	}

# Read in the error codes and populate %function and %reason with the
# old codes. Also define $min_func and $min_reason with the smallest
# unused function and reason codes. Care is needed because the
# config file can define larger reason codes and these should be
# ignored.

sub read_errcodes {
$file = $_[0];
$min_func = 100;
$min_reason = 100;
undef %func_codes;
undef %reason_codes;
return if ($regen);
if (open IN, $file) {
	while(<IN>) {
		if(/^#define\s*(\S*)\s*(\S*)/) {
			if (exists $function{$1} ) {
				if($2 >= $min_func) {$min_func = $2 + 1;}
				$func_codes{$1} = $2;
			} elsif ((defined %reason) && exists $reason{$1}) {
				$reason_codes{$1} = $2;
				if( !(exists $r_value{$1})  &&
						 ($2 >= $min_reason))
							 {$min_reason = $2 + 1;}
			}
		}
	}
	close IN;
}
}