update-copyright: convert 2-digit to 4-digit years
[gnulib.git] / build-aux / update-copyright
1 #!/usr/bin/perl -0777 -pi
2 # Update an FSF copyright year list to include the current year.
3
4 my $VERSION = '2009-08-14.02:23'; # UTC
5
6 # Copyright (C) 2009 Free Software Foundation, Inc.
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3, or (at your option)
11 # any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 # Written by Jim Meyering and Joel E. Denny
22
23 # The arguments to this script should be names of files that contain FSF
24 # copyright statements to be updated.  For example, you might wish to
25 # use the update-copyright target rule in maint.mk from gnulib's
26 # maintainer-makefile module.
27 #
28 # Iff an FSF copyright statement is discovered in a file and the final
29 # year is not the current year, then the statement is updated for the
30 # new year, 2-digit years are converted to 4-digit years by prepending
31 # "19", and the statement is reformatted to fit within 72 columns.  A
32 # warning is printed for every file for which no FSF copyright statement
33 # is discovered.
34 #
35 # Each file's FSF copyright statement must be formated correctly in
36 # order to be recognized.  For example, each of these is fine:
37 #
38 #   Copyright @copyright{} 1990-2005, 2007-2009 Free Software
39 #   Foundation, Inc.
40 #
41 #   # Copyright (C) 1990-2005, 2007-2009 Free Software
42 #   # Foundation, Inc.
43 #
44 #   /*
45 #    * Copyright &copy; 90,2005,2007-2009
46 #    * Free Software Foundation, Inc.
47 #    */
48 #
49 # However, the following format is not recognized because the line
50 # prefix changes after the first line:
51 #
52 #   ## Copyright (C) 1990-2005, 2007-2009 Free Software
53 #   #  Foundation, Inc.
54 #
55 # The following copyright statement is not recognized because the
56 # copyright holder is not the FSF:
57 #
58 #   Copyright (C) 1990-2005, 2007-2009 Acme, Inc.
59 #
60 # However, any correctly formatted FSF copyright statement following
61 # either of the previous two copyright statements would be recognized.
62 #
63 # The exact conditions that a file's FSF copyright statement must meet
64 # to be recognized are:
65 #
66 #   1. It is the first FSF copyright statement that meets all of the
67 #      following conditions.  Subsequent FSF copyright statements are
68 #      ignored.
69 #   2. Its format is "Copyright (C)", then a list of copyright years,
70 #      and then the name of the copyright holder, which is "Free
71 #      Software Foundation, Inc.".
72 #   3. The "(C)" takes one of the following forms or is omitted
73 #      entirely:
74 #
75 #        A. (C)
76 #        B. (c)
77 #        C. @copyright{}
78 #        D. &copy;
79 #
80 #   4. The "Copyright" appears at the beginning of a line except that it
81 #      may be prefixed by any sequence (e.g., a comment) of no more than
82 #      5 characters.
83 #   5. Iff such a prefix is present, the same prefix appears at the
84 #      beginning of each remaining line within the FSF copyright
85 #      statement.  There is one exception in order to support C-style
86 #      comments: if the first line's prefix contains nothing but
87 #      whitespace surrounding a "/*", then the prefix for all subsequent
88 #      lines is the same as the first line's prefix except with each of
89 #      "/" and possibly "*" replaced by a " ".  The replacement of "*"
90 #      by " " is consistent throughout all subsequent lines.
91 #   6. Blank lines, even if preceded by the prefix, do not appear
92 #      within the FSF copyright statement.
93 #   7. Each copyright year is 2 or 4 digits, and years are separated by
94 #      commas or dashes.  Whitespace may occur after commas.
95
96 use strict;
97 use warnings;
98
99 my $copyright_re = 'Copyright';
100 my $circle_c_re = '(?:\([cC]\)|@copyright{}|&copy;)';
101 my $holder = 'Free Software Foundation, Inc.';
102 my $prefix_max = 5;
103 my $margin = 72;
104 my $tab_width = 8;
105
106 my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR};
107 if (!$this_year || $this_year !~ m/^\d{4}$/)
108   {
109     my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ());
110     $this_year = $year + 1900;
111   }
112
113 # Unless the file consistently uses "\r\n" as the EOL, use "\n" instead.
114 my $eol = /(?:^|[^\r])\n/ ? "\n" : "\r\n";
115
116 my $leading;
117 my $prefix;
118 my $ws_re;
119 my $stmt_re;
120 while (/(^|\n)(.{0,$prefix_max})$copyright_re/g)
121   {
122     $leading = "$1$2";
123     $prefix = $2;
124     if ($prefix =~ /^(\s*\/)\*(\s*)$/)
125       {
126         $prefix =~ s,/, ,;
127         my $prefix_ws = $prefix;
128         $prefix_ws =~ s/\*/ /; # Only whitespace.
129         if (/\G(?:[^*\n]|\*[^\/\n])*\*?\n$prefix_ws/)
130           {
131             $prefix = $prefix_ws;
132           }
133       }
134     $ws_re = '[ \t\r\f]'; # \s without \n
135     $ws_re =
136       "(?:$ws_re*(?:$ws_re|\\n" . quotemeta($prefix) . ")$ws_re*)";
137     my $holder_re = $holder;
138     $holder_re =~ s/\s/$ws_re/g;
139     my $stmt_remainder_re =
140       "(?:$ws_re$circle_c_re)?"
141       . "$ws_re(?:(?:\\d\\d)?\\d\\d(,$ws_re?|-))*"
142       . "((?:\\d\\d)?\\d\\d)$ws_re$holder_re";
143     if (/\G$stmt_remainder_re/)
144       {
145         $stmt_re =
146           quotemeta($leading) . "($copyright_re$stmt_remainder_re)";
147         last;
148       }
149   }
150 if (defined $stmt_re)
151   {
152     /$stmt_re/ or die; # Should never die.
153     my $stmt = $1;
154     my $sep = $2 ? $2 : "";
155     my $final_year_orig = $3;
156
157     # Handle two-digit year numbers like "98" and "99".
158     my $final_year = $final_year_orig;
159     $final_year <= 99
160       and $final_year += 1900;
161
162     if ($final_year != $this_year)
163       {
164         # Update the year.
165         if ($sep eq '-' && $final_year + 1 == $this_year)
166           {
167             $stmt =~ s/$final_year_orig/$this_year/;
168           }
169         elsif ($sep ne '-' && $final_year + 1 == $this_year)
170           {
171             $stmt =~ s/$final_year_orig/$final_year-$this_year/;
172           }
173         else
174           {
175             $stmt =~ s/$final_year_orig/$final_year, $this_year/;
176           }
177
178         # Normalize all whitespace including newline-prefix sequences.
179         $stmt =~ s/$ws_re/ /g;
180
181         # Put spaces after commas.
182         $stmt =~ s/, ?/, /g;
183
184         # Convert 2-digit to 4-digit years.
185         $stmt =~ s/(\b\d\d\b)/19$1/g;
186
187         # Format within margin.
188         my $stmt_wrapped;
189         my $text_margin = $margin - length($prefix);
190         if ($prefix =~ /^(\t+)/)
191           {
192             $text_margin -= length($1) * ($tab_width - 1);
193           }
194         while (length $stmt)
195           {
196             if (($stmt =~ s/^(.{1,$text_margin})(?: |$)//)
197                 || ($stmt =~ s/^([\S]+)(?: |$)//))
198               {
199                 my $line = $1;
200                 $stmt_wrapped .= $stmt_wrapped ? "$eol$prefix" : $leading;
201                 $stmt_wrapped .= $line;
202               }
203             else
204               {
205                 # Should be unreachable, but we don't want an infinite
206                 # loop if it can be reached.
207                 die;
208               }
209           }
210
211         # Replace the old copyright statement.
212         s/$stmt_re/$stmt_wrapped/;
213       }
214   }
215 else
216   {
217     print STDERR "$ARGV: warning: FSF copyright statement not found\n";
218   }
219
220 # Local variables:
221 # indent-tabs-mode: nil
222 # eval: (add-hook 'write-file-hooks 'time-stamp)
223 # time-stamp-start: "my $VERSION = '"
224 # time-stamp-format: "%:y-%02m-%02d.%02H:%02M"
225 # time-stamp-time-zone: "UTC"
226 # time-stamp-end: "'; # UTC"
227 # End: