err_code.pl 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/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;
14

15
while (@ARGV)
16
	{
17 18
	$in=shift(@ARGV);
	if ($in =~ /^-conf$/)
19
		{
20 21 22
		$in=shift(@ARGV);
		open(IN,"<$in") || die "unable to open '$in'\n";
		while (<IN>)
23
			{
24 25 26 27 28 29 30 31 32 33 34 35 36 37
			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;
		}
38 39 40 41 42
	elsif ($in =~ /^-regen/)
		{
		$regen = 1;
		next;
	}
43 44 45 46 47 48 49

	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*\)/)
			{
50 51 52 53 54 55 56 57
# 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";
#					}
#				}
58 59 60
			$function{$1}++;
			$last=$1;
			$reason{$2}++;
61 62
			}
		}
63
	close(IN);
64 65 66 67 68 69 70 71 72 73 74 75
	}

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

@F=sort keys %function;
@R=sort keys %reason;
foreach $j (sort keys %prefix)
	{
76
	next if !defined $errfile{$j};
77 78
	next if $errfile{$j} eq "NONE";
	printf STDERR "doing %-6s - ",$j;
79 80
	@f=grep(/^${j}_/,@F);
	@r=grep(/^${j}_/,@R);
81 82
	if (defined($errfile{$j}))
		{
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
		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;
		}
104 105 106 107 108 109
		open(OUT,">$errfile{$j}") ||
			die "unable to open '$errfile{$j}':$!\n";
		$close_file=1;
		}
	else
		{
110 111
		$min_func = 100;
		$min_reason = 100;
112
		*OUT=*STDOUT;
113
		$close_file=0;
114
		}
115
	$num=$min_func;
116 117 118 119 120 121
	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);
122 123 124 125 126 127
		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++;
		}
128 129
		$f_count++;
		}
130
	$num=$min_reason;
131 132 133 134 135
	print OUT "\n/* Reason codes. */\n";
	$r_count=0;
	foreach $i (@r)
		{
		$z=6-int(length($i)/8);
136 137 138
		if (exists $reason_codes{$i}) {
			printf OUT "#define $i%s $reason_codes{$i}\n","\t" x $z;
		} elsif (exists $r_value{$i}) {
139
			printf OUT "#define $i%s $r_value{$i}\n","\t" x $z;
140
		} else {
141 142
			printf OUT "#define $i%s $num\n","\t" x $z;
			$num++;
143
		}
144 145
		$r_count++;
		}
146
	close(OUT) if $close_file;
147 148 149 150

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

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
# 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;
}
}