nstrftime: use clearer code for padding
[gnulib.git] / build-aux / update-copyright
blobea3e46fe60fc3be9c2be2a2f08cb19b8762fd90e
1 #!/bin/sh
2 #! -*-perl-*-
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)
11 # any later version.
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
30 # variable.
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"
42 # below.)
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
51 # Foundation, Inc.
53 # # Copyright (C) 1990-2005, 2007-2009 Free Software
54 # # Foundation, Inc.
56 # /*
57 # * Copyright &copy; 90,2005,2007-2009
58 # * Free Software Foundation, Inc.
59 # */
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
65 # # Foundation, Inc.
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
75 # ignored.
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
79 # entirely:
81 # A. (C)
82 # B. (c)
83 # C. @copyright{}
84 # D. &copy;
85 # E. ©
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
102 # after commas.
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" "$@"'
139 if 0;
141 my $VERSION = '2024-01-15.18:30'; # 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.
147 use strict;
148 use warnings;
149 use re 'eval';
151 my $copyright_re = 'Copyright';
152 my $circle_c_re = '(?:\([cC]\)|@copyright\{}|\\\\\(co|&copy;|©)';
153 my $ndash_re = '(?:--?|\\\\\(en)';
154 my $holder = $ENV{UPDATE_COPYRIGHT_HOLDER};
155 $holder ||= 'Free Software Foundation, Inc.';
156 my $prefix_max = 5;
157 my $margin = $ENV{UPDATE_COPYRIGHT_MAX_LINE_LENGTH};
158 !$margin || $margin !~ m/^\d+$/
159 and $margin = 72;
161 my $tab_width = 8;
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";
173 my $stmt_re;
174 my $found;
175 while (/(^|\n)(.{0,$prefix_max})$copyright_re/cg)
177 my $pos=pos();
178 my $leading = "$1$2";
179 my $prefix = $2;
180 if ($prefix =~ /^(\s*\/)\*(\s*)$/)
182 $prefix =~ 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
191 $ws_re =
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/)
201 $found = 1;
202 $stmt_re =
203 quotemeta($leading) . "($copyright_re$stmt_remainder_re)";
205 /$stmt_re/ or die; # Should never die.
206 my $stmt = $1;
207 my $final_year_orig = $2;
209 # Handle two-digit year numbers like "98" and "99".
210 my $final_year = $final_year_orig;
211 $final_year <= 99
212 and $final_year += 1900;
214 if ($final_year != $this_year)
216 # Update the 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.
225 $stmt =~ s/, ?/, /g;
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;
235 else
237 my $ndash = ($ARGV =~ /\.tex(i(nfo)?)?$/ ? "--"
238 : $ARGV =~ /\.(\d[a-z]*|man)$/ ? "\\(en"
239 : "-");
241 $stmt =~
243 (\d{4})
245 (,\ |$ndash_re)
246 ((??{
247 if ($2 ne ', ') { '\d{4}'; }
248 elsif (!$3) { $1 + 1; }
249 else { $3 + 1; }
252 /$1$ndash$3/gx;
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.
260 my $stmt_wrapped;
261 my $text_margin = $margin - length($prefix);
262 if ($prefix =~ /^(\t+)/)
264 $text_margin -= length($1) * ($tab_width - 1);
266 while (length $stmt)
268 if (($stmt =~ s/^(.{1,$text_margin})(?: |$)//)
269 || ($stmt =~ s/^([\S]+)(?: |$)//))
271 my $line = $1;
272 $stmt_wrapped .= $stmt_wrapped ? "$eol$prefix" : $leading;
273 $stmt_wrapped .= $line;
275 else
277 # Should be unreachable, but we don't want an infinite
278 # loop if it can be reached.
279 die;
283 # Replace the old copyright statement.
284 my $p = pos();
285 s/$stmt_re/$stmt_wrapped/g;
286 pos() = $p;
291 if (!$found)
293 print STDERR "$ARGV: warning: copyright statement not found\n";
296 # Hey Emacs!
297 # Local variables:
298 # coding: utf-8
299 # mode: perl
300 # indent-tabs-mode: nil
301 # eval: (add-hook 'before-save-hook 'time-stamp)
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"
307 # End: