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