1 eval '(exit $?0)' && eval 'exec perl -wS -0777 -pi "$0" ${1+"$@"}'
2 & eval 'exec perl -wS -0777 -pi "$0" $argv:q'
4 # Update an FSF copyright year list to include the current year.
6 my $VERSION = '2009-12-28.11:09'; # UTC
8 # Copyright (C) 2009-2010 Free Software Foundation, Inc.
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)
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.
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/>.
23 # Written by Jim Meyering and Joel E. Denny
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.
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:
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"
39 # A warning is printed for every file for which no FSF copyright
40 # statement is recognized.
42 # Each file's FSF copyright statement must be formated correctly in
43 # order to be recognized. For example, each of these is fine:
45 # Copyright @copyright{} 1990-2005, 2007-2009 Free Software
48 # # Copyright (C) 1990-2005, 2007-2009 Free Software
52 # * Copyright © 90,2005,2007-2009
53 # * Free Software Foundation, Inc.
56 # However, the following format is not recognized because the line
57 # prefix changes after the first line:
59 # ## Copyright (C) 1990-2005, 2007-2009 Free Software
62 # The following copyright statement is not recognized because the
63 # copyright holder is not the FSF:
65 # Copyright (C) 1990-2005, 2007-2009 Acme, Inc.
67 # However, any correctly formatted FSF copyright statement following
68 # either of the previous two copyright statements would be recognized.
70 # The exact conditions that a file's FSF copyright statement must meet
71 # to be recognized are:
73 # 1. It is the first FSF copyright statement that meets all of the
74 # following conditions. Subsequent FSF copyright statements are
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
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
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.
103 # Environment variables:
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.
123 my $copyright_re = 'Copyright';
124 my $circle_c_re = '(?:\([cC]\)|@copyright{}|©)';
125 my $holder = 'Free Software Foundation, Inc.';
127 my $margin = $ENV{UPDATE_COPYRIGHT_MAX_LINE_LENGTH};
128 !$margin || $margin !~ m/^\d+$/
133 my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR};
134 if (!$this_year || $this_year !~ m/^\d{4}$/)
136 my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ());
137 $this_year = $year + 1900;
140 # Unless the file consistently uses "\r\n" as the EOL, use "\n" instead.
141 my $eol = /(?:^|[^\r])\n/ ? "\n" : "\r\n";
147 while (/(^|\n)(.{0,$prefix_max})$copyright_re/g)
151 if ($prefix =~ /^(\s*\/)\*(\s*)$/)
154 my $prefix_ws = $prefix;
155 $prefix_ws =~ s/\*/ /; # Only whitespace.
156 if (/\G(?:[^*\n]|\*[^\/\n])*\*?\n$prefix_ws/)
158 $prefix = $prefix_ws;
161 $ws_re = '[ \t\r\f]'; # \s without \n
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/)
173 quotemeta($leading) . "($copyright_re$stmt_remainder_re)";
177 if (defined $stmt_re)
179 /$stmt_re/ or die; # Should never die.
181 my $final_year_orig = $2;
183 # Handle two-digit year numbers like "98" and "99".
184 my $final_year = $final_year_orig;
186 and $final_year += 1900;
188 if ($final_year != $this_year)
191 $stmt =~ s/$final_year_orig/$final_year, $this_year/;
193 if ($final_year != $this_year || $ENV{'UPDATE_COPYRIGHT_FORCE'})
195 # Normalize all whitespace including newline-prefix sequences.
196 $stmt =~ s/$ws_re/ /g;
198 # Put spaces after commas.
201 # Convert 2-digit to 4-digit years.
202 $stmt =~ s/(\b\d\d\b)/19$1/g;
204 # Make the use of intervals consistent.
205 if (!$ENV{UPDATE_COPYRIGHT_USE_INTERVALS})
207 $stmt =~ s/(\d{4})-(\d{4})/join(', ', $1..$2)/eg;
217 if ($2 eq '-') { '\d{4}'; }
218 elsif (!$3) { $1 + 1; }
225 # Format within margin.
227 my $text_margin = $margin - length($prefix);
228 if ($prefix =~ /^(\t+)/)
230 $text_margin -= length($1) * ($tab_width - 1);
234 if (($stmt =~ s/^(.{1,$text_margin})(?: |$)//)
235 || ($stmt =~ s/^([\S]+)(?: |$)//))
238 $stmt_wrapped .= $stmt_wrapped ? "$eol$prefix" : $leading;
239 $stmt_wrapped .= $line;
243 # Should be unreachable, but we don't want an infinite
244 # loop if it can be reached.
249 # Replace the old copyright statement.
250 s/$stmt_re/$stmt_wrapped/;
255 print STDERR "$ARGV: warning: FSF copyright statement not found\n";
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"