xref: /netbsd-src/external/gpl3/gdb/dist/gnulib/import/extra/update-copyright (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
18dffb485Schristos#!/bin/sh
28dffb485Schristos#! -*-perl-*-
38dffb485Schristos
48dffb485Schristos# Update an FSF copyright year list to include the current year.
58dffb485Schristos
6*4b169a6bSchristos# Copyright (C) 2009-2022 Free Software Foundation, Inc.
78dffb485Schristos#
88dffb485Schristos# This program is free software: you can redistribute it and/or modify
98dffb485Schristos# it under the terms of the GNU General Public License as published by
10*4b169a6bSchristos# the Free Software Foundation, either version 3, or (at your option)
118dffb485Schristos# any later version.
128dffb485Schristos#
138dffb485Schristos# This program is distributed in the hope that it will be useful,
148dffb485Schristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
158dffb485Schristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
168dffb485Schristos# GNU General Public License for more details.
178dffb485Schristos#
188dffb485Schristos# You should have received a copy of the GNU General Public License
198dffb485Schristos# along with this program.  If not, see <https://www.gnu.org/licenses/>.
208dffb485Schristos#
218dffb485Schristos# Written by Jim Meyering and Joel E. Denny
228dffb485Schristos
238dffb485Schristos# This script updates an FSF copyright year list to include the current year.
248dffb485Schristos# Usage: update-copyright [FILE...]
258dffb485Schristos#
268dffb485Schristos# The arguments to this script should be names of files that contain
278dffb485Schristos# copyright statements to be updated.  The copyright holder's name
288dffb485Schristos# defaults to "Free Software Foundation, Inc." but may be changed to
298dffb485Schristos# any other name by using the "UPDATE_COPYRIGHT_HOLDER" environment
308dffb485Schristos# variable.
318dffb485Schristos#
328dffb485Schristos# For example, you might wish to use the update-copyright target rule
338dffb485Schristos# in maint.mk from gnulib's maintainer-makefile module.
348dffb485Schristos#
358dffb485Schristos# Iff a copyright statement is recognized in a file and the final
368dffb485Schristos# year is not the current year, then the statement is updated for the
378dffb485Schristos# new year and it is reformatted to:
388dffb485Schristos#
398dffb485Schristos#   1. Fit within 72 columns.
408dffb485Schristos#   2. Convert 2-digit years to 4-digit years by prepending "19".
418dffb485Schristos#   3. Expand copyright year intervals.  (See "Environment variables"
428dffb485Schristos#      below.)
438dffb485Schristos#
448dffb485Schristos# A warning is printed for every file for which no copyright
458dffb485Schristos# statement is recognized.
468dffb485Schristos#
478dffb485Schristos# Each file's copyright statement must be formatted correctly in
488dffb485Schristos# order to be recognized.  For example, each of these is fine:
498dffb485Schristos#
508dffb485Schristos#   Copyright @copyright{} 1990-2005, 2007-2009 Free Software
518dffb485Schristos#   Foundation, Inc.
528dffb485Schristos#
538dffb485Schristos#   # Copyright (C) 1990-2005, 2007-2009 Free Software
548dffb485Schristos#   # Foundation, Inc.
558dffb485Schristos#
568dffb485Schristos#   /*
578dffb485Schristos#    * Copyright &copy; 90,2005,2007-2009
588dffb485Schristos#    * Free Software Foundation, Inc.
598dffb485Schristos#    */
608dffb485Schristos#
618dffb485Schristos# However, the following format is not recognized because the line
628dffb485Schristos# prefix changes after the first line:
638dffb485Schristos#
648dffb485Schristos#   ## Copyright (C) 1990-2005, 2007-2009 Free Software
658dffb485Schristos#   #  Foundation, Inc.
668dffb485Schristos#
678dffb485Schristos# However, any correctly formatted copyright statement following
688dffb485Schristos# a non-matching copyright statements would be recognized.
698dffb485Schristos#
708dffb485Schristos# The exact conditions that a file's copyright statement must meet
718dffb485Schristos# to be recognized are:
728dffb485Schristos#
738dffb485Schristos#   1. It is the first copyright statement that meets all of the
748dffb485Schristos#      following conditions.  Subsequent copyright statements are
758dffb485Schristos#      ignored.
768dffb485Schristos#   2. Its format is "Copyright (C)", then a list of copyright years,
778dffb485Schristos#      and then the name of the copyright holder.
788dffb485Schristos#   3. The "(C)" takes one of the following forms or is omitted
798dffb485Schristos#      entirely:
808dffb485Schristos#
818dffb485Schristos#        A. (C)
828dffb485Schristos#        B. (c)
838dffb485Schristos#        C. @copyright{}
848dffb485Schristos#        D. &copy;
858dffb485Schristos#        E. ©
868dffb485Schristos#
878dffb485Schristos#   4. The "Copyright" appears at the beginning of a line, except that it
888dffb485Schristos#      may be prefixed by any sequence (e.g., a comment) of no more than
898dffb485Schristos#      5 characters -- including white space.
908dffb485Schristos#   5. Iff such a prefix is present, the same prefix appears at the
918dffb485Schristos#      beginning of each remaining line within the FSF copyright
928dffb485Schristos#      statement.  There is one exception in order to support C-style
938dffb485Schristos#      comments: if the first line's prefix contains nothing but
948dffb485Schristos#      whitespace surrounding a "/*", then the prefix for all subsequent
958dffb485Schristos#      lines is the same as the first line's prefix except with each of
968dffb485Schristos#      "/" and possibly "*" replaced by a " ".  The replacement of "*"
978dffb485Schristos#      by " " is consistent throughout all subsequent lines.
988dffb485Schristos#   6. Blank lines, even if preceded by the prefix, do not appear
998dffb485Schristos#      within the FSF copyright statement.
1008dffb485Schristos#   7. Each copyright year is 2 or 4 digits, and years are separated by
1018dffb485Schristos#      commas, "-", or "--".  Whitespace may appear after commas.
1028dffb485Schristos#
1038dffb485Schristos# Environment variables:
1048dffb485Schristos#
1058dffb485Schristos#   1. If UPDATE_COPYRIGHT_FORCE=1, a recognized FSF copyright statement
1068dffb485Schristos#      is reformatted even if it does not need updating for the new
1078dffb485Schristos#      year.  If unset or set to 0, only updated FSF copyright
1088dffb485Schristos#      statements are reformatted.
1098dffb485Schristos#   2. If UPDATE_COPYRIGHT_USE_INTERVALS=1, every series of consecutive
1108dffb485Schristos#      copyright years (such as 90, 1991, 1992-2007, 2008) in a
1118dffb485Schristos#      reformatted FSF copyright statement is collapsed to a single
1128dffb485Schristos#      interval (such as 1990-2008).  If unset or set to 0, all existing
1138dffb485Schristos#      copyright year intervals in a reformatted FSF copyright statement
1148dffb485Schristos#      are expanded instead.
1158dffb485Schristos#      If UPDATE_COPYRIGHT_USE_INTERVALS=2, convert a sequence with gaps
1168dffb485Schristos#      to the minimal containing range.  For example, convert
1178dffb485Schristos#      2000, 2004-2007, 2009 to 2000-2009.
1188dffb485Schristos#   3. For testing purposes, you can set the assumed current year in
1198dffb485Schristos#      UPDATE_COPYRIGHT_YEAR.
1208dffb485Schristos#   4. The default maximum line length for a copyright line is 72.
1218dffb485Schristos#      Set UPDATE_COPYRIGHT_MAX_LINE_LENGTH to use a different length.
1228dffb485Schristos#   5. Set UPDATE_COPYRIGHT_HOLDER if the copyright holder is other
1238dffb485Schristos#      than "Free Software Foundation, Inc.".
1248dffb485Schristos
1258dffb485Schristos# This is a prologue that allows to run a perl script as an executable
1268dffb485Schristos# on systems that are compliant to a POSIX version before POSIX:2017.
1278dffb485Schristos# On such systems, the usual invocation of an executable through execlp()
1288dffb485Schristos# or execvp() fails with ENOEXEC if it is a script that does not start
1298dffb485Schristos# with a #! line.  The script interpreter mentioned in the #! line has
1308dffb485Schristos# to be /bin/sh, because on GuixSD systems that is the only program that
1318dffb485Schristos# has a fixed file name.  The second line is essential for perl and is
1328dffb485Schristos# also useful for editing this file in Emacs.  The next two lines below
1338dffb485Schristos# are valid code in both sh and perl.  When executed by sh, they re-execute
1348dffb485Schristos# the script through the perl program found in $PATH.  The '-x' option
1358dffb485Schristos# is essential as well; without it, perl would re-execute the script
1368dffb485Schristos# through /bin/sh.  When executed by perl, the next two lines are a no-op.
1378dffb485Schristoseval 'exec perl -wSx -0777 -pi "$0" "$@"'
1388dffb485Schristos     if 0;
1398dffb485Schristos
1408dffb485Schristosmy $VERSION = '2020-04-04.15:07'; # UTC
1418dffb485Schristos# The definition above must lie within the first 8 lines in order
1428dffb485Schristos# for the Emacs time-stamp write hook (at end) to update it.
1438dffb485Schristos# If you change this file with Emacs, please let the write hook
1448dffb485Schristos# do its job.  Otherwise, update this string manually.
1458dffb485Schristos
1468dffb485Schristosuse strict;
1478dffb485Schristosuse warnings;
1488dffb485Schristos
1498dffb485Schristosmy $copyright_re = 'Copyright';
1508dffb485Schristosmy $circle_c_re = '(?:\([cC]\)|@copyright\{}|\\\\\(co|&copy;|©)';
1518dffb485Schristosmy $holder = $ENV{UPDATE_COPYRIGHT_HOLDER};
1528dffb485Schristos$holder ||= 'Free Software Foundation, Inc.';
1538dffb485Schristosmy $prefix_max = 5;
1548dffb485Schristosmy $margin = $ENV{UPDATE_COPYRIGHT_MAX_LINE_LENGTH};
1558dffb485Schristos!$margin || $margin !~ m/^\d+$/
1568dffb485Schristos  and $margin = 72;
1578dffb485Schristos
1588dffb485Schristosmy $tab_width = 8;
1598dffb485Schristos
1608dffb485Schristosmy $this_year = $ENV{UPDATE_COPYRIGHT_YEAR};
1618dffb485Schristosif (!$this_year || $this_year !~ m/^\d{4}$/)
1628dffb485Schristos  {
1638dffb485Schristos    my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ());
1648dffb485Schristos    $this_year = $year + 1900;
1658dffb485Schristos  }
1668dffb485Schristos
1678dffb485Schristos# Unless the file consistently uses "\r\n" as the EOL, use "\n" instead.
1688dffb485Schristosmy $eol = /(?:^|[^\r])\n/ ? "\n" : "\r\n";
1698dffb485Schristos
1708dffb485Schristosmy $leading;
1718dffb485Schristosmy $prefix;
1728dffb485Schristosmy $ws_re;
1738dffb485Schristosmy $stmt_re;
1748dffb485Schristoswhile (/(^|\n)(.{0,$prefix_max})$copyright_re/g)
1758dffb485Schristos  {
1768dffb485Schristos    $leading = "$1$2";
1778dffb485Schristos    $prefix = $2;
1788dffb485Schristos    if ($prefix =~ /^(\s*\/)\*(\s*)$/)
1798dffb485Schristos      {
1808dffb485Schristos        $prefix =~ s,/, ,;
1818dffb485Schristos        my $prefix_ws = $prefix;
1828dffb485Schristos        $prefix_ws =~ s/\*/ /; # Only whitespace.
1838dffb485Schristos        if (/\G(?:[^*\n]|\*[^\/\n])*\*?\n$prefix_ws/)
1848dffb485Schristos          {
1858dffb485Schristos            $prefix = $prefix_ws;
1868dffb485Schristos          }
1878dffb485Schristos      }
1888dffb485Schristos    $ws_re = '[ \t\r\f]'; # \s without \n
1898dffb485Schristos    $ws_re =
1908dffb485Schristos      "(?:$ws_re*(?:$ws_re|\\n" . quotemeta($prefix) . ")$ws_re*)";
1918dffb485Schristos    my $holder_re = $holder;
1928dffb485Schristos    $holder_re =~ s/\s/$ws_re/g;
1938dffb485Schristos    my $stmt_remainder_re =
1948dffb485Schristos      "(?:$ws_re$circle_c_re)?"
1958dffb485Schristos      . "$ws_re(?:(?:\\d\\d)?\\d\\d(?:,$ws_re?|--?))*"
1968dffb485Schristos      . "((?:\\d\\d)?\\d\\d)$ws_re$holder_re";
1978dffb485Schristos    if (/\G$stmt_remainder_re/)
1988dffb485Schristos      {
1998dffb485Schristos        $stmt_re =
2008dffb485Schristos          quotemeta($leading) . "($copyright_re$stmt_remainder_re)";
2018dffb485Schristos        last;
2028dffb485Schristos      }
2038dffb485Schristos  }
2048dffb485Schristosif (defined $stmt_re)
2058dffb485Schristos  {
2068dffb485Schristos    /$stmt_re/ or die; # Should never die.
2078dffb485Schristos    my $stmt = $1;
2088dffb485Schristos    my $final_year_orig = $2;
2098dffb485Schristos
2108dffb485Schristos    # Handle two-digit year numbers like "98" and "99".
2118dffb485Schristos    my $final_year = $final_year_orig;
2128dffb485Schristos    $final_year <= 99
2138dffb485Schristos      and $final_year += 1900;
2148dffb485Schristos
2158dffb485Schristos    if ($final_year != $this_year)
2168dffb485Schristos      {
2178dffb485Schristos        # Update the year.
2188dffb485Schristos        $stmt =~ s/\b$final_year_orig\b/$final_year, $this_year/;
2198dffb485Schristos      }
2208dffb485Schristos    if ($final_year != $this_year || $ENV{'UPDATE_COPYRIGHT_FORCE'})
2218dffb485Schristos      {
2228dffb485Schristos        # Normalize all whitespace including newline-prefix sequences.
2238dffb485Schristos        $stmt =~ s/$ws_re/ /g;
2248dffb485Schristos
2258dffb485Schristos        # Put spaces after commas.
2268dffb485Schristos        $stmt =~ s/, ?/, /g;
2278dffb485Schristos
2288dffb485Schristos        # Convert 2-digit to 4-digit years.
2298dffb485Schristos        $stmt =~ s/(\b\d\d\b)/19$1/g;
2308dffb485Schristos
2318dffb485Schristos        # Make the use of intervals consistent.
2328dffb485Schristos        if (!$ENV{UPDATE_COPYRIGHT_USE_INTERVALS})
2338dffb485Schristos          {
2348dffb485Schristos            $stmt =~ s/(\d{4})--?(\d{4})/join(', ', $1..$2)/eg;
2358dffb485Schristos          }
2368dffb485Schristos        else
2378dffb485Schristos          {
2388dffb485Schristos            my $ndash = $ARGV =~ /\.tex(i(nfo)?)?$/ ? "--" : "-";
2398dffb485Schristos
2408dffb485Schristos            $stmt =~
2418dffb485Schristos              s/
2428dffb485Schristos                (\d{4})
2438dffb485Schristos                (?:
2448dffb485Schristos                  (,\ |--?)
2458dffb485Schristos                  ((??{
2468dffb485Schristos                    if   ($2 ne ', ') { '\d{4}'; }
2478dffb485Schristos                    elsif (!$3)       { $1 + 1;  }
2488dffb485Schristos                    else              { $3 + 1;  }
2498dffb485Schristos                  }))
2508dffb485Schristos                )+
2518dffb485Schristos              /$1$ndash$3/gx;
2528dffb485Schristos
2538dffb485Schristos            # When it's 2, emit a single range encompassing all year numbers.
2548dffb485Schristos            $ENV{UPDATE_COPYRIGHT_USE_INTERVALS} == 2
2558dffb485Schristos              and $stmt =~ s/\b(\d{4})\b.*\b(\d{4})\b/$1$ndash$2/;
2568dffb485Schristos          }
2578dffb485Schristos
2588dffb485Schristos        # Format within margin.
2598dffb485Schristos        my $stmt_wrapped;
2608dffb485Schristos        my $text_margin = $margin - length($prefix);
2618dffb485Schristos        if ($prefix =~ /^(\t+)/)
2628dffb485Schristos          {
2638dffb485Schristos            $text_margin -= length($1) * ($tab_width - 1);
2648dffb485Schristos          }
2658dffb485Schristos        while (length $stmt)
2668dffb485Schristos          {
2678dffb485Schristos            if (($stmt =~ s/^(.{1,$text_margin})(?: |$)//)
2688dffb485Schristos                || ($stmt =~ s/^([\S]+)(?: |$)//))
2698dffb485Schristos              {
2708dffb485Schristos                my $line = $1;
2718dffb485Schristos                $stmt_wrapped .= $stmt_wrapped ? "$eol$prefix" : $leading;
2728dffb485Schristos                $stmt_wrapped .= $line;
2738dffb485Schristos              }
2748dffb485Schristos            else
2758dffb485Schristos              {
2768dffb485Schristos                # Should be unreachable, but we don't want an infinite
2778dffb485Schristos                # loop if it can be reached.
2788dffb485Schristos                die;
2798dffb485Schristos              }
2808dffb485Schristos          }
2818dffb485Schristos
2828dffb485Schristos        # Replace the old copyright statement.
2838dffb485Schristos        s/$stmt_re/$stmt_wrapped/;
2848dffb485Schristos      }
2858dffb485Schristos  }
2868dffb485Schristoselse
2878dffb485Schristos  {
2888dffb485Schristos    print STDERR "$ARGV: warning: copyright statement not found\n";
2898dffb485Schristos  }
2908dffb485Schristos
2918dffb485Schristos# Hey Emacs!
2928dffb485Schristos# Local variables:
2938dffb485Schristos# coding: utf-8
2948dffb485Schristos# mode: perl
2958dffb485Schristos# indent-tabs-mode: nil
2968dffb485Schristos# eval: (add-hook 'before-save-hook 'time-stamp)
2978dffb485Schristos# time-stamp-line-limit: 200
2988dffb485Schristos# time-stamp-start: "my $VERSION = '"
2998dffb485Schristos# time-stamp-format: "%:y-%02m-%02d.%02H:%02M"
3008dffb485Schristos# time-stamp-time-zone: "UTC0"
3018dffb485Schristos# time-stamp-end: "'; # UTC"
3028dffb485Schristos# End:
303