23e4a2279764da58b16f73ac83cceeeb39cdf80a
[openssl.git] / util / mkerr.pl
1 #! /usr/bin/env perl
2 # Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the OpenSSL license (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9 use strict;
10 use warnings;
11
12 my $config       = "crypto/err/openssl.ec";
13 my $debug        = 0;
14 my $internal     = 0;
15 my $nowrite      = 0;
16 my $rebuild      = 0;
17 my $reindex      = 0;
18 my $static       = 0;
19 my $unref        = 0;
20
21 my $errors       = 0;
22 my @t            = localtime();
23 my $YEAR         = $t[5] + 1900;
24
25 sub phase
26 {
27     my $text = uc(shift);
28     print STDERR "\n---\n$text\n" if $debug;
29 }
30
31 sub help
32 {
33     print STDERR <<"EOF";
34 mkerr.pl [options] [files...]
35
36 Options:
37
38     -conf FILE  Use the named config file FILE instead of the default.
39
40     -debug      Verbose output debugging on stderr.
41
42     -internal   Generate code that is to be built as part of OpenSSL itself.
43                 Also scans internal list of files.
44
45     -nowrite    Do not write the header/source files, even if changed.
46
47     -rebuild    Rebuild all header and C source files, even if there
48                 were no changes.
49
50     -reindex    Ignore previously assigned values (except for R records in
51                 the config file) and renumber everything starting at 100.
52
53     -static     Make the load/unload functions static.
54
55     -unref      List all unreferenced function and reason codes on stderr;
56                 implies -nowrite.
57
58     -help       Show this help text.
59
60     ...         Additional arguments are added to the file list to scan,
61                 if '-internal' was NOT specified on the command line.
62
63 EOF
64 }
65
66 while ( @ARGV ) {
67     my $arg = $ARGV[0];
68     last unless $arg =~ /-.*/;
69     $arg = $1 if $arg =~ /-(-.*)/;
70     if ( $arg eq "-conf" ) {
71         $config = $ARGV[1];
72         shift @ARGV;
73     } elsif ( $arg eq "-debug" ) {
74         $debug = 1;
75         $unref = 1;
76     } elsif ( $arg eq "-internal" ) {
77         $internal = 1;
78     } elsif ( $arg eq "-nowrite" ) {
79         $nowrite = 1;
80     } elsif ( $arg eq "-rebuild" ) {
81         $rebuild = 1;
82     } elsif ( $arg eq "-reindex" ) {
83         $reindex = 1;
84     } elsif ( $arg eq "-static" ) {
85         $static = 1;
86     } elsif ( $arg eq "-unref" ) {
87         $unref = 1;
88         $nowrite = 1;
89     } elsif ( $arg =~ /-*h(elp)?/ ) {
90         &help();
91         exit;
92     } elsif ( $arg =~ /-.*/ ) {
93         die "Unknown option $arg; use -h for help.\n";
94     }
95     shift @ARGV;
96 }
97
98 my @source;
99 if ( $internal ) {
100     die "Cannot mix -internal and -static\n" if $static;
101     die "Extra parameters given.\n" if @ARGV;
102     @source = ( glob('crypto/*.c'), glob('crypto/*/*.c'),
103                 glob('ssl/*.c'), glob('ssl/*/*.c') );
104 } else {
105     @source = @ARGV;
106 }
107
108 # Data parsed out of the config and state files.
109 my %hinc;       # lib -> header
110 my %libinc;     # header -> lib
111 my %cskip;      # error_file -> lib
112 my %errorfile;  # lib -> error file name
113 my %fmax;       # lib -> max assigned function code
114 my %rmax;       # lib -> max assigned reason code
115 my %fassigned;  # lib -> colon-separated list of assigned function codes
116 my %rassigned;  # lib -> colon-separated list of assigned reason codes
117 my %fnew;       # lib -> count of new function codes
118 my %rnew;       # lib -> count of new reason codes
119 my %rextra;     # "extra" reason code -> lib
120 my %rcodes;     # reason-name -> value
121 my %ftrans;     # old name -> #define-friendly name (all caps)
122 my %fcodes;     # function-name -> value
123 my $statefile;  # state file with assigned reason and function codes
124
125 # Read and parse the config file
126 open(IN, "$config") || die "Can't open config file $config, $!,";
127 while ( <IN> ) {
128     next if /^#/ || /^$/;
129     if ( /^L\s+(\S+)\s+(\S+)\s+(\S+)/ ) {
130         my $lib = $1;
131         my $hdr = $2;
132         my $err = $3;
133         $hinc{$lib}   = $hdr;
134         $libinc{$hdr} = $lib;
135         $cskip{$err}  = $lib;
136         next if $err eq 'NONE';
137         $errorfile{$lib} = $err;
138         $fmax{$lib}      = 100;
139         $rmax{$lib}      = 100;
140         $fassigned{$lib} = ":";
141         $rassigned{$lib} = ":";
142         $fnew{$lib}      = 0;
143         $rnew{$lib}      = 0;
144     } elsif ( /^R\s+(\S+)\s+(\S+)/ ) {
145         $rextra{$1} = $2;
146         $rcodes{$1} = $2;
147     } elsif ( /^S\s+(\S+)/ ) {
148         $statefile = $1;
149     } else {
150         die "Illegal config line $_\n";
151     }
152 }
153 close IN;
154
155 if ( ! $statefile ) {
156     $statefile = $config;
157     $statefile =~ s/.ec/.txt/;
158 }
159
160 # The statefile has all the previous assignments.
161 &phase("Reading state");
162 my $skippedstate = 0;
163 if ( ! $reindex && $statefile ) {
164     open(STATE, "<$statefile") || die "Can't open $statefile, $!";
165
166     # Scan function and reason codes and store them: keep a note of the
167     # maximum code used.
168     while ( <STATE> ) {
169         next if /^#/ || /^$/;
170         die "Bad line in $statefile:\n$_\n" unless /(\S+)\s+(\d+)/;
171         my $name = $1;
172         my $code = $2;
173         my $lib = $name;
174         $lib =~ s/_.*//;
175         $lib = "SSL" if $lib =~ /TLS/;
176         if ( !defined $errorfile{$lib} ) {
177             print "Skipping $_";
178             $skippedstate++;
179             next;
180         }
181         if ( $name =~ /^[A-Z0-9]+_R_/ ) {
182             die "$lib reason code $code collision at $name\n"
183                 if $rassigned{$lib} =~ /:$code:/;
184             $rassigned{$lib} .= "$code:";
185             if ( !exists $rextra{$name} ) {
186                 $rmax{$lib} = $code if $code > $rmax{$lib};
187             }
188             $rcodes{$name} = $code;
189         } elsif ( $name =~ /^[A-Z0-9]+_F_/ ) {
190             die "$lib function code $code collision at $name\n"
191                 if $fassigned{$lib} =~ /:$code:/;
192             $fassigned{$lib} .= "$code:";
193             $fmax{$lib} = $code if $code > $fmax{$lib};
194             $fcodes{$name} = $code;
195         } else {
196             die "Bad line in $statefile:\n$_\n";
197         }
198     }
199     close(STATE);
200
201     if ( $debug ) {
202         foreach my $lib ( sort keys %rmax ) {
203             print STDERR "Reason codes for ${lib}:\n";
204             if ( $rassigned{$lib} =~ m/^:(.*):$/ ) {
205                 my @rassigned = sort { $a <=> $b } split( ":", $1 );
206                 print STDERR "  ", join(' ', @rassigned), "\n";
207             } else {
208                 print STDERR "  --none--\n";
209             }
210         }
211         print STDERR "\n";
212         foreach my $lib ( sort keys %fmax ) {
213             print STDERR "Function codes for ${lib}:\n";
214             if ( $fassigned{$lib} =~ m/^:(.*):$/ ) {
215                 my @fassigned = sort { $a <=> $b } split( ":", $1 );
216                 print STDERR "  ", join(' ', @fassigned), "\n";
217             } else {
218                 print STDERR "  --none--\n";
219             }
220         }
221     }
222 }
223
224 # Scan each header file and make a list of error codes
225 # and function names
226 &phase("Scanning headers");
227 while ( ( my $hdr, my $lib ) = each %libinc ) {
228     next if $hdr eq "NONE";
229     print STDERR " ." if $debug;
230     my $line = "";
231     my $def = "";
232     my $linenr = 0;
233     my $cpp = 0;
234
235     open(IN, "<$hdr") || die "Can't open $hdr, $!,";
236     while ( <IN> ) {
237         $linenr++;
238
239         if ( $line ne '' ) {
240             $_    = $line . $_;
241             $line = '';
242         }
243
244         if ( /\\$/ ) {
245             $line = $_;
246             next;
247         }
248
249         if ( /\/\*/ ) {
250             if ( not /\*\// ) {    # multiline comment...
251                 $line = $_;        # ... just accumulate
252                 next;
253             } else {
254                 s/\/\*.*?\*\///gs;    # wipe it
255             }
256         }
257
258         if ( $cpp ) {
259             $cpp++ if /^#\s*if/;
260             $cpp-- if /^#\s*endif/;
261             next;
262         }
263         $cpp = 1 if /^#.*ifdef.*cplusplus/;    # skip "C" declaration
264
265         next if /^\#/;    # skip preprocessor directives
266
267         s/{[^{}]*}//gs;     # ignore {} blocks
268
269         if ( /\{|\/\*/ ) {    # Add a so editor works...
270             $line = $_;
271         } else {
272             $def .= $_;
273         }
274     }
275
276     # Delete any DECLARE_ macros
277     my $defnr = 0;
278     $def =~ s/DECLARE_\w+\([\w,\s]+\)//gs;
279     foreach ( split /;/, $def ) {
280         $defnr++;
281         # The goal is to collect function names from function declarations.
282
283         s/^[\n\s]*//g;
284         s/[\n\s]*$//g;
285
286         # Skip over recognized non-function declarations
287         next if /typedef\W/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/;
288
289         # Remove STACK_OF(foo)
290         s/STACK_OF\(\w+\)/void/;
291
292         # Reduce argument lists to empty ()
293         # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
294         while ( /\(.*\)/s ) {
295             s/\([^\(\)]+\)/\{\}/gs;
296             s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs;    #(*f{}) -> f
297         }
298
299         # pretend as we didn't use curly braces: {} -> ()
300         s/\{\}/\(\)/gs;
301
302         if ( /(\w+)\s*\(\).*/s ) {    # first token prior [first] () is
303             my $name = $1;          # a function name!
304             $name =~ tr/[a-z]/[A-Z]/;
305             $ftrans{$name} = $1;
306         } elsif ( /[\(\)]/ and not(/=/) ) {
307             print STDERR "Header $hdr: cannot parse: $_;\n";
308         }
309     }
310
311     next if $reindex;
312
313     if ( $lib eq "SSL" && $rmax{$lib} >= 1000 ) {
314         print STDERR "SSL error codes 1000+ are reserved for alerts.\n";
315         print STDERR "Any new alerts must be added to $config.\n";
316         $errors++;
317     }
318     close IN;
319 }
320 print STDERR "\n" if $debug;
321
322 # Scan each C source file and look for function and reason codes
323 # This is done by looking for strings that "look like" function or
324 # reason codes: basically anything consisting of all upper case and
325 # numerics which has _F_ or _R_ in it and which has the name of an
326 # error library at the start.  This seems to work fine except for the
327 # oddly named structure BIO_F_CTX which needs to be ignored.
328 # If a code doesn't exist in list compiled from headers then mark it
329 # with the value "X" as a place holder to give it a value later.
330 # Store all function and reason codes found in %usedfuncs and %usedreasons
331 # so all those unreferenced can be printed out.
332 &phase("Scanning source");
333 my %usedfuncs;
334 my %usedreasons;
335 foreach my $file ( @source ) {
336     # Don't parse the error source file.
337     next if exists $cskip{$file};
338     open( IN, "<$file" ) || die "Can't open $file, $!,";
339     my $func;
340     my $linenr = 0;
341     print STDERR "$file:\n" if $debug;
342     while ( <IN> ) {
343
344         # skip obsoleted source files entirely!
345         last if /^#error\s+obsolete/;
346         $linenr++;
347         if ( !/;$/ && /^\**([a-zA-Z_].*[\s*])?([A-Za-z_0-9]+)\(.*([),]|$)/ ) {
348             /^([^()]*(\([^()]*\)[^()]*)*)\(/;
349             $1 =~ /([A-Za-z_0-9]*)$/;
350             $func = $1;
351         }
352
353         if ( /(([A-Z0-9]+)_F_([A-Z0-9_]+))/ ) {
354             next unless exists $errorfile{$2};
355             next if $1 eq "BIO_F_BUFFER_CTX";
356             $usedfuncs{$1} = 1;
357             if ( !exists $fcodes{$1} ) {
358                 print STDERR "  New function $1\n" if $debug;
359                 $fcodes{$1} = "X";
360                 $fnew{$2}++;
361             }
362             $ftrans{$3} = $func unless exists $ftrans{$3};
363             if ( uc($func) ne $3 ) {
364                 print STDERR "ERROR: mismatch $file:$linenr $func:$3\n";
365                 $errors++;
366             }
367             print STDERR "  Function $1 = $fcodes{$1}\n"
368               if $debug;
369         }
370         if ( /(([A-Z0-9]+)_R_[A-Z0-9_]+)/ ) {
371             next unless exists $errorfile{$2};
372             $usedreasons{$1} = 1;
373             if ( !exists $rcodes{$1} ) {
374                 print STDERR "  New reason $1\n" if $debug;
375                 $rcodes{$1} = "X";
376                 $rnew{$2}++;
377             }
378             print STDERR "  Reason $1 = $rcodes{$1}\n" if $debug;
379         }
380     }
381     close IN;
382 }
383 print STDERR "\n" if $debug;
384
385 # Now process each library in turn.
386 &phase("Writing files");
387 my $newstate = 0;
388 foreach my $lib ( keys %errorfile ) {
389     if ( ! $fnew{$lib} && ! $rnew{$lib} ) {
390         next unless $rebuild;
391     }
392     next if $nowrite;
393     print STDERR "$lib: $fnew{$lib} new functions\n" if $fnew{$lib};
394     print STDERR "$lib: $rnew{$lib} new reasons\n" if $rnew{$lib};
395     $newstate = 1;
396
397     # If we get here then we have some new error codes so we
398     # need to rebuild the header file and C file.
399
400     # Make a sorted list of error and reason codes for later use.
401     my @function = sort grep( /^${lib}_/, keys %fcodes );
402     my @reasons  = sort grep( /^${lib}_/, keys %rcodes );
403
404     # Rewrite the header file
405
406     my $hfile = $hinc{$lib};
407     $hfile =~ s/.h$/err.h/ if $internal;
408     open( OUT, ">$hfile" ) || die "Can't write to $hfile, $!,";
409     print OUT <<"EOF";
410 /*
411  * Generated by util/mkerr.pl DO NOT EDIT
412  * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
413  *
414  * Licensed under the OpenSSL license (the \"License\").  You may not use
415  * this file except in compliance with the License.  You can obtain a copy
416  * in the file LICENSE in the source distribution or at
417  * https://www.openssl.org/source/license.html
418  */
419
420 #ifndef HEADER_${lib}ERR_H
421 # define HEADER_${lib}ERR_H
422
423 EOF
424     if ( $internal ) {
425         # Declare the load function because the generate C file
426         # includes "fooerr.h" not "foo.h"
427         print OUT <<"EOF";
428 # ifdef  __cplusplus
429 extern \"C\" {
430 # endif
431 int ERR_load_${lib}_strings(void);
432 # ifdef  __cplusplus
433 }
434 # endif
435 EOF
436     } else {
437         print OUT <<"EOF";
438 # define ${lib}err(f, r) ERR_${lib}_error((f), (r), OPENSSL_FILE, OPENSSL_LINE)
439
440 EOF
441         if ( ! $static ) {
442             print OUT <<"EOF";
443
444 # ifdef  __cplusplus
445 extern \"C\" {
446 # endif
447 int ERR_load_${lib}_strings(void);
448 void ERR_unload_${lib}_strings(void);
449 void ERR_${lib}_error(int function, int reason, char *file, int line);
450 # ifdef  __cplusplus
451 }
452 # endif
453 EOF
454         }
455     }
456
457     print OUT "\n/*\n * $lib function codes.\n */\n";
458     foreach my $i ( @function ) {
459         my $z = 48 - length($i);
460         if ( $fcodes{$i} eq "X" ) {
461             $fassigned{$lib} =~ m/^:([^:]*):/;
462             my $findcode = $1;
463             $findcode = $fmax{$lib} if !defined $findcode;
464             while ( $fassigned{$lib} =~ m/:$findcode:/ ) {
465                 $findcode++;
466             }
467             $fcodes{$i} = $findcode;
468             $fassigned{$lib} .= "$findcode:";
469             print STDERR "New Function code $i\n" if $debug;
470         }
471         printf OUT "# define $i%s $fcodes{$i}\n", " " x $z;
472     }
473
474     print OUT "\n/*\n * $lib reason codes.\n */\n";
475     foreach my $i ( @reasons ) {
476         my $z = 48 - length($i);
477         if ( $rcodes{$i} eq "X" ) {
478             $rassigned{$lib} =~ m/^:([^:]*):/;
479             my $findcode = $1;
480             $findcode = $rmax{$lib} if !defined $findcode;
481             while ( $rassigned{$lib} =~ m/:$findcode:/ ) {
482                 $findcode++;
483             }
484             $rcodes{$i} = $findcode;
485             $rassigned{$lib} .= "$findcode:";
486             print STDERR "New Reason code $i\n" if $debug;
487         }
488         printf OUT "# define $i%s $rcodes{$i}\n", " " x $z;
489     }
490     print OUT "\n";
491
492     print OUT "#endif\n";
493
494     # Rewrite the C source file containing the error details.
495
496     # First, read any existing reason string definitions:
497     my %err_reason_strings;
498     my $cfile = $errorfile{$lib};
499     if ( open( IN, "<$cfile" ) ) {
500         my $line = "";
501         while ( <IN> ) {
502             s|\R$||;    # Better chomp
503             $_    = $line . $_;
504             $line = "";
505             if ( /{ERR_(PACK|FUNC|REASON)\(/ ) {
506                 if ( /\b(${lib}_R_\w*)\b.*\"(.*)\"/ ) {
507                     $err_reason_strings{$1} = $2;
508                 } elsif ( /\b${lib}_F_(\w*)\b.*\"(.*)\"/ ) {
509                     if ( !exists $ftrans{$1} && $1 ne $2 ) {
510 #                       Don't print warning, too noisy. :(
511 #                       print STDERR "WARNING: Mismatched/unused function $2\n";
512                         $ftrans{$1} = $2;
513                     }
514                 } else {
515                     $line = $_;
516                 }
517             }
518         }
519         close(IN);
520     }
521
522     my $pack_lib = $internal ? "ERR_LIB_${lib}" : "0";
523     my $hincf = $hfile;
524     $hincf =~ s|.*include/||;
525     if ( $hincf =~ m|^openssl/| ) {
526         $hincf = "<${hincf}>";
527     } else {
528         $hincf = "\"${hincf}\"";
529     }
530
531     open( OUT, ">$cfile" )
532         || die "Can't open $cfile for writing, $!, stopped";
533
534     my $const = $internal ? 'const ' : '';
535
536     print OUT <<"EOF";
537 /*
538  * Generated by util/mkerr.pl DO NOT EDIT
539  * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
540  *
541  * Licensed under the OpenSSL license (the "License").  You may not use
542  * this file except in compliance with the License.  You can obtain a copy
543  * in the file LICENSE in the source distribution or at
544  * https://www.openssl.org/source/license.html
545  */
546
547 #include <openssl/err.h>
548 #include $hincf
549
550 #ifndef OPENSSL_NO_ERR
551
552 static ${const}ERR_STRING_DATA ${lib}_str_functs[] = {
553 EOF
554
555     # Add each function code: if a function name is found then use it.
556     foreach my $i ( @function ) {
557         my $fn;
558         $i =~ /^${lib}_F_(\S+)$/;
559         $fn = $1;
560         $fn = $ftrans{$fn} if exists $ftrans{$fn};
561         my $short = "    {ERR_PACK($pack_lib, $i, 0), \"$fn\"},";
562         if ( length($short) <= 80 ) {
563             print OUT "$short\n";
564         } else {
565             print OUT "    {ERR_PACK($pack_lib, $i, 0),\n     \"$fn\"},\n";
566         }
567     }
568     print OUT <<"EOF";
569     {0, NULL}
570 };
571
572 static ${const}ERR_STRING_DATA ${lib}_str_reasons[] = {
573 EOF
574
575     # Add each reason code.
576     foreach my $i ( @reasons ) {
577         my $rn;
578         if ( exists $err_reason_strings{$i} ) {
579             $rn = $err_reason_strings{$i};
580         } else {
581             $i =~ /^${lib}_R_(\S+)$/;
582             $rn = $1;
583             $rn =~ tr/_[A-Z]/ [a-z]/;
584         }
585         my $short = "    {ERR_PACK($pack_lib, 0, $i), \"$rn\"},";
586         if ( length($short) <= 80 ) {
587             print OUT "$short\n";
588         } else {
589             print OUT "    {ERR_PACK($pack_lib, 0, $i),\n    \"$rn\"},\n";
590         }
591     }
592     print OUT <<"EOF";
593     {0, NULL}
594 };
595
596 #endif
597 EOF
598     if ( $internal ) {
599         print OUT <<"EOF";
600
601 int ERR_load_${lib}_strings(void)
602 {
603 #ifndef OPENSSL_NO_ERR
604     if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL) {
605         ERR_load_strings_const(${lib}_str_functs);
606         ERR_load_strings_const(${lib}_str_reasons);
607     }
608 #endif
609     return 1;
610 }
611 EOF
612     } else {
613         my $st = $static ? "static " : "";
614         print OUT <<"EOF";
615
616 static int lib_code = 0;
617 static int error_loaded = 0;
618
619 ${st}int ERR_load_${lib}_strings(void)
620 {
621     if (lib_code == 0)
622         lib_code = ERR_get_next_error_library();
623
624     if (!error_loaded) {
625 #ifndef OPENSSL_NO_ERR
626         ERR_load_strings(lib_code, ${lib}_str_functs);
627         ERR_load_strings(lib_code, ${lib}_str_reasons);
628 #endif
629         error_loaded = 1;
630     }
631     return 1;
632 }
633
634 ${st}void ERR_unload_${lib}_strings(void)
635 {
636     if (error_loaded) {
637 #ifndef OPENSSL_NO_ERR
638         ERR_unload_strings(lib_code, ${lib}_str_functs);
639         ERR_unload_strings(lib_code, ${lib}_str_reasons);
640 #endif
641         error_loaded = 0;
642     }
643 }
644
645 ${st}void ERR_${lib}_error(int function, int reason, char *file, int line)
646 {
647     if (lib_code == 0)
648         lib_code = ERR_get_next_error_library();
649     ERR_PUT_error(lib_code, function, reason, file, line);
650 }
651 EOF
652
653     }
654
655     close OUT;
656     undef %err_reason_strings;
657 }
658
659 &phase("Ending");
660 # Make a list of unreferenced function and reason codes
661 if ( $unref ) {
662     my @funref;
663     foreach ( keys %fcodes ) {
664         push( @funref, $_ ) unless exists $usedfuncs{$_};
665     }
666     my @runref;
667     foreach ( keys %rcodes ) {
668         push( @runref, $_ ) unless exists $usedreasons{$_};
669     }
670     if ( @funref ) {
671         print STDERR "The following function codes were not referenced:\n";
672         foreach ( sort @funref ) {
673             print STDERR "  $_\n";
674         }
675     }
676     if ( @runref ) {
677         print STDERR "The following reason codes were not referenced:\n";
678         foreach ( sort @runref ) {
679             print STDERR "  $_\n";
680         }
681     }
682 }
683
684 die "Found $errors errors, quitting" if $errors;
685
686 # Update the state file
687 if ( $newstate )  {
688     open(OUT, ">$statefile.new")
689         || die "Can't write $statefile.new, $!";
690     foreach my $i ( sort keys %fcodes ) {
691         print OUT "$i $fcodes{$i}\n";
692     }
693     foreach my $i ( sort keys %rcodes ) {
694         print OUT "$i $rcodes{$i}\n" if !exists $rextra{$i};
695     }
696     close(OUT);
697     if ( $skippedstate ) {
698         print "Skipped state, leaving update in $statefile.new";
699     } else {
700         rename "$statefile", "$statefile.old"
701             || die "Can't backup $statefile to $statefile.old, $!";
702         rename "$statefile.new", "$statefile"
703             || die "Can't rename $statefile to $statefile.new, $!";
704     }
705 }
706 exit;