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