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