Commit | Line | Data |
---|---|---|
49e4877c | 1 | eval '(exit $?0)' && eval 'exec perl -wS -0777 -pi "$0" "$@"' |
09c01c30 JB |
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 | ||
7a6dbc2f | 6 | my $VERSION = '2018-03-07.03:47'; # UTC |
09c01c30 | 7 | |
7a6dbc2f | 8 | # Copyright (C) 2009-2018 Free Software Foundation, Inc. |
09c01c30 JB |
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 | |
7a6dbc2f | 21 | # along with this program. If not, see <https://www.gnu.org/licenses/>. |
09c01c30 JB |
22 | |
23 | # Written by Jim Meyering and Joel E. Denny | |
24 | ||
770d76d7 PA |
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 Software Foundation, Inc." but may be changed to | |
28 | # any other name by using the "UPDATE_COPYRIGHT_HOLDER" environment | |
29 | # variable. | |
09c01c30 | 30 | # |
770d76d7 PA |
31 | # For example, you might wish to use the update-copyright target rule |
32 | # in maint.mk from gnulib's maintainer-makefile module. | |
33 | # | |
34 | # Iff a copyright statement is recognized in a file and the final | |
09c01c30 JB |
35 | # year is not the current year, then the statement is updated for the |
36 | # new year and it is reformatted to: | |
37 | # | |
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" | |
41 | # below.) | |
42 | # | |
770d76d7 | 43 | # A warning is printed for every file for which no copyright |
09c01c30 JB |
44 | # statement is recognized. |
45 | # | |
770d76d7 | 46 | # Each file's copyright statement must be formatted correctly in |
09c01c30 JB |
47 | # order to be recognized. For example, each of these is fine: |
48 | # | |
49 | # Copyright @copyright{} 1990-2005, 2007-2009 Free Software | |
50 | # Foundation, Inc. | |
51 | # | |
52 | # # Copyright (C) 1990-2005, 2007-2009 Free Software | |
53 | # # Foundation, Inc. | |
54 | # | |
55 | # /* | |
56 | # * Copyright © 90,2005,2007-2009 | |
57 | # * Free Software Foundation, Inc. | |
58 | # */ | |
59 | # | |
60 | # However, the following format is not recognized because the line | |
61 | # prefix changes after the first line: | |
62 | # | |
63 | # ## Copyright (C) 1990-2005, 2007-2009 Free Software | |
64 | # # Foundation, Inc. | |
65 | # | |
770d76d7 PA |
66 | # However, any correctly formatted copyright statement following |
67 | # a non-matching copyright statements would be recognized. | |
09c01c30 | 68 | # |
770d76d7 | 69 | # The exact conditions that a file's copyright statement must meet |
09c01c30 JB |
70 | # to be recognized are: |
71 | # | |
770d76d7 PA |
72 | # 1. It is the first copyright statement that meets all of the |
73 | # following conditions. Subsequent copyright statements are | |
09c01c30 JB |
74 | # ignored. |
75 | # 2. Its format is "Copyright (C)", then a list of copyright years, | |
770d76d7 | 76 | # and then the name of the copyright holder. |
09c01c30 JB |
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. © | |
7a6dbc2f | 84 | # E. © |
09c01c30 | 85 | # |
770d76d7 | 86 | # 4. The "Copyright" appears at the beginning of a line, except that it |
09c01c30 | 87 | # may be prefixed by any sequence (e.g., a comment) of no more than |
770d76d7 | 88 | # 5 characters -- including white space. |
09c01c30 JB |
89 | # 5. Iff such a prefix is present, the same prefix appears at the |
90 | # beginning of each remaining line within the FSF copyright | |
91 | # statement. There is one exception in order to support C-style | |
92 | # comments: if the first line's prefix contains nothing but | |
93 | # whitespace surrounding a "/*", then the prefix for all subsequent | |
94 | # lines is the same as the first line's prefix except with each of | |
95 | # "/" and possibly "*" replaced by a " ". The replacement of "*" | |
96 | # by " " is consistent throughout all subsequent lines. | |
97 | # 6. Blank lines, even if preceded by the prefix, do not appear | |
98 | # within the FSF copyright statement. | |
99 | # 7. Each copyright year is 2 or 4 digits, and years are separated by | |
100 | # commas or dashes. Whitespace may appear after commas. | |
101 | # | |
102 | # Environment variables: | |
103 | # | |
104 | # 1. If UPDATE_COPYRIGHT_FORCE=1, a recognized FSF copyright statement | |
105 | # is reformatted even if it does not need updating for the new | |
106 | # year. If unset or set to 0, only updated FSF copyright | |
107 | # statements are reformatted. | |
108 | # 2. If UPDATE_COPYRIGHT_USE_INTERVALS=1, every series of consecutive | |
109 | # copyright years (such as 90, 1991, 1992-2007, 2008) in a | |
110 | # reformatted FSF copyright statement is collapsed to a single | |
111 | # interval (such as 1990-2008). If unset or set to 0, all existing | |
112 | # copyright year intervals in a reformatted FSF copyright statement | |
113 | # are expanded instead. | |
770d76d7 PA |
114 | # If UPDATE_COPYRIGHT_USE_INTERVALS=2, convert a sequence with gaps |
115 | # to the minimal containing range. For example, convert | |
116 | # 2000, 2004-2007, 2009 to 2000-2009. | |
09c01c30 JB |
117 | # 3. For testing purposes, you can set the assumed current year in |
118 | # UPDATE_COPYRIGHT_YEAR. | |
119 | # 4. The default maximum line length for a copyright line is 72. | |
120 | # Set UPDATE_COPYRIGHT_MAX_LINE_LENGTH to use a different length. | |
770d76d7 PA |
121 | # 5. Set UPDATE_COPYRIGHT_HOLDER if the copyright holder is other |
122 | # than "Free Software Foundation, Inc.". | |
09c01c30 JB |
123 | |
124 | use strict; | |
125 | use warnings; | |
126 | ||
127 | my $copyright_re = 'Copyright'; | |
7a6dbc2f | 128 | my $circle_c_re = '(?:\([cC]\)|@copyright\{}|\\\\\(co|©|©)'; |
770d76d7 PA |
129 | my $holder = $ENV{UPDATE_COPYRIGHT_HOLDER}; |
130 | $holder ||= 'Free Software Foundation, Inc.'; | |
09c01c30 JB |
131 | my $prefix_max = 5; |
132 | my $margin = $ENV{UPDATE_COPYRIGHT_MAX_LINE_LENGTH}; | |
133 | !$margin || $margin !~ m/^\d+$/ | |
134 | and $margin = 72; | |
135 | ||
136 | my $tab_width = 8; | |
137 | ||
138 | my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR}; | |
139 | if (!$this_year || $this_year !~ m/^\d{4}$/) | |
140 | { | |
141 | my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ()); | |
142 | $this_year = $year + 1900; | |
143 | } | |
144 | ||
145 | # Unless the file consistently uses "\r\n" as the EOL, use "\n" instead. | |
146 | my $eol = /(?:^|[^\r])\n/ ? "\n" : "\r\n"; | |
147 | ||
148 | my $leading; | |
149 | my $prefix; | |
150 | my $ws_re; | |
151 | my $stmt_re; | |
152 | while (/(^|\n)(.{0,$prefix_max})$copyright_re/g) | |
153 | { | |
154 | $leading = "$1$2"; | |
155 | $prefix = $2; | |
156 | if ($prefix =~ /^(\s*\/)\*(\s*)$/) | |
157 | { | |
158 | $prefix =~ s,/, ,; | |
159 | my $prefix_ws = $prefix; | |
160 | $prefix_ws =~ s/\*/ /; # Only whitespace. | |
161 | if (/\G(?:[^*\n]|\*[^\/\n])*\*?\n$prefix_ws/) | |
162 | { | |
163 | $prefix = $prefix_ws; | |
164 | } | |
165 | } | |
166 | $ws_re = '[ \t\r\f]'; # \s without \n | |
167 | $ws_re = | |
168 | "(?:$ws_re*(?:$ws_re|\\n" . quotemeta($prefix) . ")$ws_re*)"; | |
169 | my $holder_re = $holder; | |
170 | $holder_re =~ s/\s/$ws_re/g; | |
171 | my $stmt_remainder_re = | |
172 | "(?:$ws_re$circle_c_re)?" | |
173 | . "$ws_re(?:(?:\\d\\d)?\\d\\d(?:,$ws_re?|-))*" | |
174 | . "((?:\\d\\d)?\\d\\d)$ws_re$holder_re"; | |
175 | if (/\G$stmt_remainder_re/) | |
176 | { | |
177 | $stmt_re = | |
178 | quotemeta($leading) . "($copyright_re$stmt_remainder_re)"; | |
179 | last; | |
180 | } | |
181 | } | |
182 | if (defined $stmt_re) | |
183 | { | |
184 | /$stmt_re/ or die; # Should never die. | |
185 | my $stmt = $1; | |
186 | my $final_year_orig = $2; | |
187 | ||
188 | # Handle two-digit year numbers like "98" and "99". | |
189 | my $final_year = $final_year_orig; | |
190 | $final_year <= 99 | |
191 | and $final_year += 1900; | |
192 | ||
193 | if ($final_year != $this_year) | |
194 | { | |
195 | # Update the year. | |
4a626d0a | 196 | $stmt =~ s/\b$final_year_orig\b/$final_year, $this_year/; |
09c01c30 JB |
197 | } |
198 | if ($final_year != $this_year || $ENV{'UPDATE_COPYRIGHT_FORCE'}) | |
199 | { | |
200 | # Normalize all whitespace including newline-prefix sequences. | |
201 | $stmt =~ s/$ws_re/ /g; | |
202 | ||
203 | # Put spaces after commas. | |
204 | $stmt =~ s/, ?/, /g; | |
205 | ||
206 | # Convert 2-digit to 4-digit years. | |
207 | $stmt =~ s/(\b\d\d\b)/19$1/g; | |
208 | ||
209 | # Make the use of intervals consistent. | |
210 | if (!$ENV{UPDATE_COPYRIGHT_USE_INTERVALS}) | |
211 | { | |
212 | $stmt =~ s/(\d{4})-(\d{4})/join(', ', $1..$2)/eg; | |
213 | } | |
214 | else | |
215 | { | |
216 | $stmt =~ | |
217 | s/ | |
218 | (\d{4}) | |
219 | (?: | |
220 | (,\ |-) | |
221 | ((??{ | |
222 | if ($2 eq '-') { '\d{4}'; } | |
223 | elsif (!$3) { $1 + 1; } | |
224 | else { $3 + 1; } | |
225 | })) | |
226 | )+ | |
227 | /$1-$3/gx; | |
770d76d7 PA |
228 | |
229 | # When it's 2, emit a single range encompassing all year numbers. | |
230 | $ENV{UPDATE_COPYRIGHT_USE_INTERVALS} == 2 | |
231 | and $stmt =~ s/\b(\d{4})\b.*\b(\d{4})\b/$1-$2/; | |
09c01c30 JB |
232 | } |
233 | ||
234 | # Format within margin. | |
235 | my $stmt_wrapped; | |
236 | my $text_margin = $margin - length($prefix); | |
237 | if ($prefix =~ /^(\t+)/) | |
238 | { | |
239 | $text_margin -= length($1) * ($tab_width - 1); | |
240 | } | |
241 | while (length $stmt) | |
242 | { | |
243 | if (($stmt =~ s/^(.{1,$text_margin})(?: |$)//) | |
244 | || ($stmt =~ s/^([\S]+)(?: |$)//)) | |
245 | { | |
246 | my $line = $1; | |
247 | $stmt_wrapped .= $stmt_wrapped ? "$eol$prefix" : $leading; | |
248 | $stmt_wrapped .= $line; | |
249 | } | |
250 | else | |
251 | { | |
252 | # Should be unreachable, but we don't want an infinite | |
253 | # loop if it can be reached. | |
254 | die; | |
255 | } | |
256 | } | |
257 | ||
258 | # Replace the old copyright statement. | |
259 | s/$stmt_re/$stmt_wrapped/; | |
260 | } | |
261 | } | |
262 | else | |
263 | { | |
770d76d7 | 264 | print STDERR "$ARGV: warning: copyright statement not found\n"; |
09c01c30 JB |
265 | } |
266 | ||
7a6dbc2f | 267 | # Hey Emacs! |
09c01c30 | 268 | # Local variables: |
7a6dbc2f | 269 | # coding: utf-8 |
09c01c30 JB |
270 | # mode: perl |
271 | # indent-tabs-mode: nil | |
7a6dbc2f | 272 | # eval: (add-hook 'before-save-hook 'time-stamp) |
09c01c30 JB |
273 | # time-stamp-start: "my $VERSION = '" |
274 | # time-stamp-format: "%:y-%02m-%02d.%02H:%02M" | |
49e4877c | 275 | # time-stamp-time-zone: "UTC0" |
09c01c30 JB |
276 | # time-stamp-end: "'; # UTC" |
277 | # End: |