Imported Upstream version 1.3.14
[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-10-30.15:57'; # UTC
7
8 # Copyright (C) 2009 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
118 use strict;
119 use warnings;
120
121 my $copyright_re = 'Copyright';
122 my $circle_c_re = '(?:\([cC]\)|@copyright{}|&copy;)';
123 my $holder = 'Free Software Foundation, Inc.';
124 my $prefix_max = 5;
125 my $margin = 72;
126 my $tab_width = 8;
127
128 my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR};
129 if (!$this_year || $this_year !~ m/^\d{4}$/)
130   {
131     my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ());
132     $this_year = $year + 1900;
133   }
134
135 # Unless the file consistently uses "\r\n" as the EOL, use "\n" instead.
136 my $eol = /(?:^|[^\r])\n/ ? "\n" : "\r\n";
137
138 my $leading;
139 my $prefix;
140 my $ws_re;
141 my $stmt_re;
142 while (/(^|\n)(.{0,$prefix_max})$copyright_re/g)
143   {
144     $leading = "$1$2";
145     $prefix = $2;
146     if ($prefix =~ /^(\s*\/)\*(\s*)$/)
147       {
148         $prefix =~ s,/, ,;
149         my $prefix_ws = $prefix;
150         $prefix_ws =~ s/\*/ /; # Only whitespace.
151         if (/\G(?:[^*\n]|\*[^\/\n])*\*?\n$prefix_ws/)
152           {
153             $prefix = $prefix_ws;
154           }
155       }
156     $ws_re = '[ \t\r\f]'; # \s without \n
157     $ws_re =
158       "(?:$ws_re*(?:$ws_re|\\n" . quotemeta($prefix) . ")$ws_re*)";
159     my $holder_re = $holder;
160     $holder_re =~ s/\s/$ws_re/g;
161     my $stmt_remainder_re =
162       "(?:$ws_re$circle_c_re)?"
163       . "$ws_re(?:(?:\\d\\d)?\\d\\d(?:,$ws_re?|-))*"
164       . "((?:\\d\\d)?\\d\\d)$ws_re$holder_re";
165     if (/\G$stmt_remainder_re/)
166       {
167         $stmt_re =
168           quotemeta($leading) . "($copyright_re$stmt_remainder_re)";
169         last;
170       }
171   }
172 if (defined $stmt_re)
173   {
174     /$stmt_re/ or die; # Should never die.
175     my $stmt = $1;
176     my $final_year_orig = $2;
177
178     # Handle two-digit year numbers like "98" and "99".
179     my $final_year = $final_year_orig;
180     $final_year <= 99
181       and $final_year += 1900;
182
183     if ($final_year != $this_year)
184       {
185         # Update the year.
186         $stmt =~ s/$final_year_orig/$final_year, $this_year/;
187       }
188     if ($final_year != $this_year || $ENV{'UPDATE_COPYRIGHT_FORCE'})
189       {
190         # Normalize all whitespace including newline-prefix sequences.
191         $stmt =~ s/$ws_re/ /g;
192
193         # Put spaces after commas.
194         $stmt =~ s/, ?/, /g;
195
196         # Convert 2-digit to 4-digit years.
197         $stmt =~ s/(\b\d\d\b)/19$1/g;
198
199         # Make the use of intervals consistent.
200         if (!$ENV{UPDATE_COPYRIGHT_USE_INTERVALS})
201           {
202             $stmt =~ s/(\d{4})-(\d{4})/join(', ', $1..$2)/eg;
203           }
204         else
205           {
206             $stmt =~
207               s/
208                 (\d{4})
209                 (?:
210                   (,\ |-)
211                   ((??{
212                     if    ($2 eq '-') { '\d{4}'; }
213                     elsif (!$3)       { $1 + 1;  }
214                     else              { $3 + 1;  }
215                   }))
216                 )+
217               /$1-$3/gx;
218           }
219
220         # Format within margin.
221         my $stmt_wrapped;
222         my $text_margin = $margin - length($prefix);
223         if ($prefix =~ /^(\t+)/)
224           {
225             $text_margin -= length($1) * ($tab_width - 1);
226           }
227         while (length $stmt)
228           {
229             if (($stmt =~ s/^(.{1,$text_margin})(?: |$)//)
230                 || ($stmt =~ s/^([\S]+)(?: |$)//))
231               {
232                 my $line = $1;
233                 $stmt_wrapped .= $stmt_wrapped ? "$eol$prefix" : $leading;
234                 $stmt_wrapped .= $line;
235               }
236             else
237               {
238                 # Should be unreachable, but we don't want an infinite
239                 # loop if it can be reached.
240                 die;
241               }
242           }
243
244         # Replace the old copyright statement.
245         s/$stmt_re/$stmt_wrapped/;
246       }
247   }
248 else
249   {
250     print STDERR "$ARGV: warning: FSF copyright statement not found\n";
251   }
252
253 # Local variables:
254 # mode: perl
255 # indent-tabs-mode: nil
256 # eval: (add-hook 'write-file-hooks 'time-stamp)
257 # time-stamp-start: "my $VERSION = '"
258 # time-stamp-format: "%:y-%02m-%02d.%02H:%02M"
259 # time-stamp-time-zone: "UTC"
260 # time-stamp-end: "'; # UTC"
261 # End: