4 # Update an FSF copyright year list to include the current year.
6 # Copyright (C) 2009-2024 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 <https://www.gnu.org/licenses/>.
21 # Written by Jim Meyering and Joel E. Denny
23 # This script updates an FSF copyright year list to include the current year.
24 # Usage: update-copyright [FILE...]
26 # The arguments to this script should be names of files that contain
27 # copyright statements to be updated. The copyright holder's name
28 # defaults to "Free Software Foundation, Inc." but may be changed to
29 # any other name by using the "UPDATE_COPYRIGHT_HOLDER" environment
32 # For example, you might wish to use the update-copyright target rule
33 # in maint.mk from gnulib's maintainer-makefile module.
35 # Iff a copyright statement is recognized in a file and the final
36 # year is not the current year, then the statement is updated for the
37 # new year and it is reformatted to:
39 # 1. Fit within 72 columns.
40 # 2. Convert 2-digit years to 4-digit years by prepending "19".
41 # 3. Expand copyright year intervals. (See "Environment variables"
44 # A warning is printed for every file for which no copyright
45 # statement is recognized.
47 # Each file's copyright statement must be formatted correctly in
48 # order to be recognized. For example, each of these is fine:
50 # Copyright @copyright{} 1990-2005, 2007-2009 Free Software
53 # # Copyright (C) 1990-2005, 2007-2009 Free Software
57 # * Copyright © 90,2005,2007-2009
58 # * Free Software Foundation, Inc.
61 # However, the following format is not recognized because the line
62 # prefix changes after the first line:
64 # ## Copyright (C) 1990-2005, 2007-2009 Free Software
67 # However, any correctly formatted copyright statement following
68 # a non-matching copyright statements would be recognized.
70 # The exact conditions that a file's copyright statement must meet
71 # to be recognized are:
73 # 1. It is the first copyright statement that meets all of the
74 # following conditions. Subsequent 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.
78 # 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
89 # 5 characters -- including white space.
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 "\(en" (for troff). Whitespace may appear
104 # Environment variables:
106 # 1. If UPDATE_COPYRIGHT_FORCE=1, a recognized FSF copyright statement
107 # is reformatted even if it does not need updating for the new
108 # year. If unset or set to 0, only updated FSF copyright
109 # statements are reformatted.
110 # 2. If UPDATE_COPYRIGHT_USE_INTERVALS=1, every series of consecutive
111 # copyright years (such as 90, 1991, 1992-2007, 2008) in a
112 # reformatted FSF copyright statement is collapsed to a single
113 # interval (such as 1990-2008). If unset or set to 0, all existing
114 # copyright year intervals in a reformatted FSF copyright statement
115 # are expanded instead.
116 # If UPDATE_COPYRIGHT_USE_INTERVALS=2, convert a sequence with gaps
117 # to the minimal containing range. For example, convert
118 # 2000, 2004-2007, 2009 to 2000-2009.
119 # 3. For testing purposes, you can set the assumed current year in
120 # UPDATE_COPYRIGHT_YEAR.
121 # 4. The default maximum line length for a copyright line is 72.
122 # Set UPDATE_COPYRIGHT_MAX_LINE_LENGTH to use a different length.
123 # 5. Set UPDATE_COPYRIGHT_HOLDER if the copyright holder is other
124 # than "Free Software Foundation, Inc.".
126 # This is a prologue that allows running a perl script as an executable
127 # on systems that are compliant to a POSIX version before POSIX:2017.
128 # On such systems, the usual invocation of an executable through execlp()
129 # or execvp() fails with ENOEXEC if it is a script that does not start
130 # with a #! line. The script interpreter mentioned in the #! line has
131 # to be /bin/sh, because on GuixSD systems that is the only program that
132 # has a fixed file name. The second line is essential for perl and is
133 # also useful for editing this file in Emacs. The next two lines below
134 # are valid code in both sh and perl. When executed by sh, they re-execute
135 # the script through the perl program found in $PATH. The '-x' option
136 # is essential as well; without it, perl would re-execute the script
137 # through /bin/sh. When executed by perl, the next two lines are a no-op.
138 eval 'exec perl -wSx -0777 -pi "$0" "$@"'
141 my
$VERSION = '2024-07-04.10:56'; # UTC
142 # The definition above must lie within the first 8 lines in order
143 # for the Emacs time-stamp write hook (at end) to update it.
144 # If you change this file with Emacs, please let the write hook
145 # do its job. Otherwise, update this string manually.
151 my
$copyright_re = 'Copyright';
152 my
$circle_c_re = '(?:\([cC]\)|@copyright\{}|\\\\\(co|©|©)';
153 my
$ndash_re = '(?:--?|\\\\\(en)';
154 my
$holder = $ENV{UPDATE_COPYRIGHT_HOLDER
};
155 $holder ||
= 'Free Software Foundation, Inc.';
157 my
$margin = $ENV{UPDATE_COPYRIGHT_MAX_LINE_LENGTH
};
158 !$margin ||
$margin !~ m
/^\d
+$
/
163 my
$this_year = $ENV{UPDATE_COPYRIGHT_YEAR
};
164 if (!$this_year ||
$this_year !~ m
/^\d
{4}$
/)
166 my
($sec, $min, $hour, $mday, $month, $year) = localtime
(time ());
167 $this_year = $year + 1900;
170 # Unless the file consistently uses "\r\n" as the EOL, use "\n" instead.
171 my
$eol = /(?
:^|
[^
\r])\n/ ?
"\n" : "\r\n";
175 while (/(^|
\n)(.
{0,$prefix_max})$copyright_re/cg
)
178 my
$leading = "$1$2";
180 if ($prefix =~
/^
(\s
*\
/)\
*(\s
*)$
/)
183 my
$prefix_ws = $prefix;
184 $prefix_ws =~ s
/\
*/ /; # Only whitespace.
185 if (/\G
(?
:[^
*\n]|\
*[^\
/\n])*\
*?
\n$prefix_ws/)
187 $prefix = $prefix_ws;
190 my
$ws_re = '[ \t\r\f]'; # \s without \n
192 "(?:$ws_re*(?:$ws_re|\\n" . quotemeta
($prefix) .
")$ws_re*)";
193 my
$holder_re = $holder;
194 $holder_re =~ s
/\s
/$ws_re/g
;
195 my
$stmt_remainder_re =
196 "(?:$ws_re$circle_c_re)?"
197 .
"$ws_re(?:(?:\\d\\d)?\\d\\d(?:,$ws_re?|$ndash_re))*"
198 .
"((?:\\d\\d)?\\d\\d)$ws_re$holder_re";
199 if (/\G
$stmt_remainder_re/)
203 quotemeta
($leading) .
"($copyright_re$stmt_remainder_re)";
205 /$stmt_re/ or die
; # Should never die.
207 my
$final_year_orig = $2;
209 # Handle two-digit year numbers like "98" and "99".
210 my
$final_year = $final_year_orig;
212 and
$final_year += 1900;
214 if ($final_year != $this_year)
217 $stmt =~ s
/(^|
[^\d
])$final_year_orig\b/$1$final_year, $this_year/;
219 if ($final_year != $this_year ||
$ENV{'UPDATE_COPYRIGHT_FORCE'})
221 # Normalize all whitespace including newline-prefix sequences.
222 $stmt =~ s
/$ws_re/ /g
;
224 # Put spaces after commas.
227 # Convert 2-digit to 4-digit years.
228 $stmt =~ s
/(\b\d\d
\b)/19$1/g
;
230 # Make the use of intervals consistent.
231 if (!$ENV{UPDATE_COPYRIGHT_USE_INTERVALS
})
233 $stmt =~ s
/(\d
{4})$ndash_re(\d
{4})/join(', ', $1..
$2)/eg
;
237 my
$ndash = ($ARGV =~
/\.tex
(i
(nfo
)?
)?$
/ ?
"--"
238 : $ARGV =~
/\.
(\d
[a-z
]*|man
)$
/ ?
"\\(en"
247 if ($2 ne
', ') { '\d{4}'; }
248 elsif
(!$3) { $1 + 1; }
254 # When it's 2, emit a single range encompassing all year numbers.
255 $ENV{UPDATE_COPYRIGHT_USE_INTERVALS
} == 2
256 and
$stmt =~ s
/(^|
[^\d
])(\d
{4})\b.
*(?
:[^\d
])(\d
{4})\b/$1$2$ndash$3/;
259 # Format within margin.
261 my
$text_margin = $margin - length
($prefix);
262 if ($prefix =~
/^
(\t+)/)
264 $text_margin -= length
($1) * ($tab_width - 1);
268 if (($stmt =~ s
/^
(.
{1,$text_margin})(?
: |$
)//)
269 ||
($stmt =~ s
/^
([\S
]+)(?
: |$
)//))
272 $stmt_wrapped .
= $stmt_wrapped ?
"$eol$prefix" : $leading;
273 $stmt_wrapped .
= $line;
277 # Should be unreachable, but we don't want an infinite
278 # loop if it can be reached.
283 # Replace the old copyright statement.
285 s
/$stmt_re/$stmt_wrapped/g
;
293 print STDERR
"$ARGV: warning: copyright statement not found\n";
300 # indent-tabs-mode: nil
301 # eval: (add-hook 'before-save-hook 'time-stamp nil t)
302 # time-stamp-line-limit: 200
303 # time-stamp-start: "my $VERSION = '"
304 # time-stamp-format: "%:y-%02m-%02d.%02H:%02M"
305 # time-stamp-time-zone: "UTC0"
306 # time-stamp-end: "'; # UTC"