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