1 #!/usr/bin/perl -0777 -pi
2 # Update an FSF copyright year list to include the current year.
4 my $VERSION = '2009-08-06.01:08'; # UTC
6 # Copyright (C) 2009 Free Software Foundation, Inc.
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)
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.
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/>.
21 # Written by Jim Meyering and Joel E. Denny
23 # The arguments to this script should be names of files that contain FSF
24 # copyright statements to be updated. For example, you may wish to
25 # place a target like the following in the top-level makefile in your
28 # .PHONY: update-copyright
30 # if test -d .git; then \
31 # git grep -l -w Copyright \
32 # | grep -v -E '(^|/)(COPYING|ChangeLog)' \
33 # | xargs $(srcdir)/build-aux/$@; \
36 # In the second grep, you can build a list of files to skip within your
39 # Iff an FSF copyright statement is discovered in a file and the final
40 # year is not the current year, the statement is updated for the new
41 # year and reformatted to fit within 72 columns. A warning is printed
42 # for every file for which no FSF copyright statement is discovered.
44 # Each file's FSF copyright statement must be formated correctly in
45 # order to be recognized. For example, each of these is fine:
47 # Copyright @copyright{} 1990-2005, 2007-2009 Free Software
50 # # Copyright (C) 1990-2005, 2007-2009 Free Software
54 # * Copyright © 90,2005,2007-2009
55 # * Free Software Foundation, Inc.
58 # However, the following format is not recognized because the line
59 # prefix changes after the first line:
61 # ## Copyright (C) 1990-2005, 2007-2009 Free Software
64 # The following copyright statement is not recognized because the
65 # copyright holder is not the FSF:
67 # Copyright (C) 1990-2005, 2007-2009 Acme, Inc.
69 # However, any correctly formatted FSF copyright statement following
70 # either of the previous two copyright statements would be recognized.
72 # The exact conditions that a file's FSF copyright statement must meet
73 # to be recognized are:
75 # 1. It is the first FSF copyright statement that meets all of the
76 # following conditions. Subsequent FSF copyright statements are
78 # 2. Its format is "Copyright (C)", then a list of copyright years,
79 # and then the name of the copyright holder, which is "Free
80 # Software Foundation, Inc.".
81 # 3. The "(C)" takes one of the following forms or is omitted
89 # 4. The "Copyright" appears at the beginning of a line except that it
90 # may be prefixed by any sequence (e.g., a comment) of no more than
92 # 5. Iff such a prefix is present, the same prefix appears at the
93 # beginning of each remaining line within the FSF copyright
94 # statement. There is one exception in order to support C-style
95 # comments: if the first line's prefix contains nothing but
96 # whitespace surrounding a "/*", then the prefix for all subsequent
97 # lines is the same as the first line's prefix except with each of
98 # "/" and possibly "*" replaced by a " ". The replacement of "*"
99 # by " " is consistent throughout all subsequent lines.
100 # 6. Blank lines, even if preceded by the prefix, do not appear
101 # within the FSF copyright statement.
102 # 7. Each copyright year is 2 or 4 digits, and years are separated by
103 # commas or dashes. Whitespace may occur after commas.
108 my $copyright_re = 'Copyright';
109 my $circle_c_re = '(?:\([cC]\)|@copyright{}|©)';
110 my $holder = 'Free Software Foundation, Inc.';
115 my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR};
116 if (!$this_year || $this_year !~ m/^\d{4}$/)
118 my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ());
119 $this_year = $year + 1900;
122 # Unless the file consistently uses "\r\n" as the EOL, use "\n" instead.
123 my $eol = /(?:^|[^\r])\n/ ? "\n" : "\r\n";
129 while (/(^|\n)(.{0,$prefix_max})$copyright_re/g)
133 if ($prefix =~ /^(\s*\/)\*(\s*)$/)
136 my $prefix_ws = $prefix;
137 $prefix_ws =~ s/\*/ /; # Only whitespace.
138 if (/\G(?:[^*\n]|\*[^\/\n])*\*?\n$prefix_ws/)
140 $prefix = $prefix_ws;
143 $ws_re = '[ \t\r\f]'; # \s without \n
145 "(?:$ws_re*(?:$ws_re|\\n" . quotemeta($prefix) . ")$ws_re*)";
146 my $holder_re = $holder;
147 $holder_re =~ s/\s/$ws_re/g;
148 my $stmt_remainder_re =
149 "(?:$ws_re$circle_c_re)?"
150 . "$ws_re(?:(?:\\d\\d)?\\d\\d(,$ws_re?|-))*"
151 . "((?:\\d\\d)?\\d\\d)$ws_re$holder_re";
152 if (/\G$stmt_remainder_re/)
155 quotemeta($leading) . "($copyright_re$stmt_remainder_re)";
159 if (defined $stmt_re)
161 /$stmt_re/ or die; # Should never die.
163 my $sep = $2 ? $2 : "";
164 my $final_year_orig = $3;
166 # Handle two-digit year numbers like "98" and "99".
167 my $final_year = $final_year_orig;
169 and $final_year += 1900;
171 if ($final_year != $this_year)
174 if ($sep eq '-' && $final_year + 1 == $this_year)
176 $stmt =~ s/$final_year_orig/$this_year/;
178 elsif ($sep ne '-' && $final_year + 1 == $this_year)
180 $stmt =~ s/$final_year_orig/$final_year-$this_year/;
184 $stmt =~ s/$final_year_orig/$final_year, $this_year/;
187 # Normalize all whitespace including newline-prefix sequences.
188 $stmt =~ s/$ws_re/ /g;
190 # Put spaces after commas.
193 # Format within margin.
195 my $text_margin = $margin - length($prefix);
196 if ($prefix =~ /^(\t+)/)
198 $text_margin -= length($1) * ($tab_width - 1);
202 if (($stmt =~ s/^(.{1,$text_margin})(?: |$)//)
203 || ($stmt =~ s/^([\S]+)(?: |$)//))
206 $stmt_wrapped .= $stmt_wrapped ? "$eol$prefix" : $leading;
207 $stmt_wrapped .= $line;
211 # Should be unreachable, but we don't want an infinite
212 # loop if it can be reached.
217 # Replace the old copyright statement.
218 s/$stmt_re/$stmt_wrapped/;
223 print STDERR "$ARGV: warning: FSF copyright statement not found\n";
227 # indent-tabs-mode: nil
228 # eval: (add-hook 'write-file-hooks 'time-stamp)
229 # time-stamp-start: "my $VERSION = '"
230 # time-stamp-format: "%:y-%02m-%02d.%02H:%02M"
231 # time-stamp-time-zone: "UTC"
232 # time-stamp-end: "'; # UTC"