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 = '2011-01-02.20:59'; # UTC
8 # Copyright (C) 2009-2011 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
26 # copyright statements to be updated. The copyright holder's name
27 # defaults to "Free Softward Foundation, Inc." but may be changed to
28 # any other name by using the "UPDATE_COPYRIGHT_HOLDER" environment
31 # For example, you might wish to use the update-copyright target rule
32 # in maint.mk from gnulib's maintainer-makefile module.
34 # Iff a copyright statement is recognized in a file and the final
35 # year is not the current year, then the statement is updated for the
36 # new year and it is reformatted to:
38 # 1. Fit within 72 columns.
39 # 2. Convert 2-digit years to 4-digit years by prepending "19".
40 # 3. Expand copyright year intervals. (See "Environment variables"
43 # A warning is printed for every file for which no copyright
44 # statement is recognized.
46 # Each file's copyright statement must be formated correctly in
47 # order to be recognized. For example, each of these is fine:
49 # Copyright @copyright{} 1990-2005, 2007-2009 Free Software
52 # # Copyright (C) 1990-2005, 2007-2009 Free Software
56 # * Copyright © 90,2005,2007-2009
57 # * Free Software Foundation, Inc.
60 # However, the following format is not recognized because the line
61 # prefix changes after the first line:
63 # ## Copyright (C) 1990-2005, 2007-2009 Free Software
66 # However, any correctly formatted copyright statement following
67 # a non-matching copyright statements would be recognized.
69 # The exact conditions that a file's copyright statement must meet
70 # to be recognized are:
72 # 1. It is the first copyright statement that meets all of the
73 # following conditions. Subsequent copyright statements are
75 # 2. Its format is "Copyright (C)", then a list of copyright years,
76 # and then the name of the copyright holder.
77 # 3. The "(C)" takes one of the following forms or is omitted
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 -- including white space.
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.
101 # Environment variables:
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 # 4. The default maximum line length for a copyright line is 72.
116 # Set UPDATE_COPYRIGHT_MAX_LINE_LENGTH to use a different length.
117 # 5. Set UPDATE_COPYRIGHT_HOLDER if the copyright holder is other
118 # than "Free Software Foundation, Inc.".
123 my $copyright_re = 'Copyright';
124 my $circle_c_re = '(?:\([cC]\)|@copyright{}|©)';
125 my $holder = $ENV{UPDATE_COPYRIGHT_HOLDER};
126 $holder ||= 'Free Software Foundation, Inc.';
128 my $margin = $ENV{UPDATE_COPYRIGHT_MAX_LINE_LENGTH};
129 !$margin || $margin !~ m/^\d+$/
134 my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR};
135 if (!$this_year || $this_year !~ m/^\d{4}$/)
137 my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ());
138 $this_year = $year + 1900;
141 # Unless the file consistently uses "\r\n" as the EOL, use "\n" instead.
142 my $eol = /(?:^|[^\r])\n/ ? "\n" : "\r\n";
148 while (/(^|\n)(.{0,$prefix_max})$copyright_re/g)
152 if ($prefix =~ /^(\s*\/)\*(\s*)$/)
155 my $prefix_ws = $prefix;
156 $prefix_ws =~ s/\*/ /; # Only whitespace.
157 if (/\G(?:[^*\n]|\*[^\/\n])*\*?\n$prefix_ws/)
159 $prefix = $prefix_ws;
162 $ws_re = '[ \t\r\f]'; # \s without \n
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/)
174 quotemeta($leading) . "($copyright_re$stmt_remainder_re)";
178 if (defined $stmt_re)
180 /$stmt_re/ or die; # Should never die.
182 my $final_year_orig = $2;
184 # Handle two-digit year numbers like "98" and "99".
185 my $final_year = $final_year_orig;
187 and $final_year += 1900;
189 if ($final_year != $this_year)
192 $stmt =~ s/$final_year_orig/$final_year, $this_year/;
194 if ($final_year != $this_year || $ENV{'UPDATE_COPYRIGHT_FORCE'})
196 # Normalize all whitespace including newline-prefix sequences.
197 $stmt =~ s/$ws_re/ /g;
199 # Put spaces after commas.
202 # Convert 2-digit to 4-digit years.
203 $stmt =~ s/(\b\d\d\b)/19$1/g;
205 # Make the use of intervals consistent.
206 if (!$ENV{UPDATE_COPYRIGHT_USE_INTERVALS})
208 $stmt =~ s/(\d{4})-(\d{4})/join(', ', $1..$2)/eg;
218 if ($2 eq '-') { '\d{4}'; }
219 elsif (!$3) { $1 + 1; }
226 # Format within margin.
228 my $text_margin = $margin - length($prefix);
229 if ($prefix =~ /^(\t+)/)
231 $text_margin -= length($1) * ($tab_width - 1);
235 if (($stmt =~ s/^(.{1,$text_margin})(?: |$)//)
236 || ($stmt =~ s/^([\S]+)(?: |$)//))
239 $stmt_wrapped .= $stmt_wrapped ? "$eol$prefix" : $leading;
240 $stmt_wrapped .= $line;
244 # Should be unreachable, but we don't want an infinite
245 # loop if it can be reached.
250 # Replace the old copyright statement.
251 s/$stmt_re/$stmt_wrapped/;
256 print STDERR "$ARGV: warning: copyright statement not found\n";
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"