update-copyright: update documentation to point to maint.mk
[pspp] / build-aux / update-copyright
1 #!/usr/bin/perl -0777 -pi
2 # Update an FSF copyright year list to include the current year.
3
4 my $VERSION = '2009-08-06.01:08'; # UTC
5
6 # Copyright (C) 2009 Free Software Foundation, Inc.
7 #
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.
12 #
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.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 # Written by Jim Meyering and Joel E. Denny
22
23 # The arguments to this script should be names of files that contain FSF
24 # copyright statements to be updated.  For example, you might wish to
25 # use the update-copyright target rule in maint.mk from gnulib's
26 # maintainer-makefile module.
27 #
28 # Iff an FSF copyright statement is discovered in a file and the final
29 # year is not the current year, the statement is updated for the new
30 # year and reformatted to fit within 72 columns.  A warning is printed
31 # for every file for which no FSF copyright statement is discovered.
32 #
33 # Each file's FSF copyright statement must be formated correctly in
34 # order to be recognized.  For example, each of these is fine:
35 #
36 #   Copyright @copyright{} 1990-2005, 2007-2009 Free Software
37 #   Foundation, Inc.
38 #
39 #   # Copyright (C) 1990-2005, 2007-2009 Free Software
40 #   # Foundation, Inc.
41 #
42 #   /*
43 #    * Copyright &copy; 90,2005,2007-2009
44 #    * Free Software Foundation, Inc.
45 #    */
46 #
47 # However, the following format is not recognized because the line
48 # prefix changes after the first line:
49 #
50 #   ## Copyright (C) 1990-2005, 2007-2009 Free Software
51 #   #  Foundation, Inc.
52 #
53 # The following copyright statement is not recognized because the
54 # copyright holder is not the FSF:
55 #
56 #   Copyright (C) 1990-2005, 2007-2009 Acme, Inc.
57 #
58 # However, any correctly formatted FSF copyright statement following
59 # either of the previous two copyright statements would be recognized.
60 #
61 # The exact conditions that a file's FSF copyright statement must meet
62 # to be recognized are:
63 #
64 #   1. It is the first FSF copyright statement that meets all of the
65 #      following conditions.  Subsequent FSF copyright statements are
66 #      ignored.
67 #   2. Its format is "Copyright (C)", then a list of copyright years,
68 #      and then the name of the copyright holder, which is "Free
69 #      Software Foundation, Inc.".
70 #   3. The "(C)" takes one of the following forms or is omitted
71 #      entirely:
72 #
73 #        A. (C)
74 #        B. (c)
75 #        C. @copyright{}
76 #        D. &copy;
77 #
78 #   4. The "Copyright" appears at the beginning of a line except that it
79 #      may be prefixed by any sequence (e.g., a comment) of no more than
80 #      5 characters.
81 #   5. Iff such a prefix is present, the same prefix appears at the
82 #      beginning of each remaining line within the FSF copyright
83 #      statement.  There is one exception in order to support C-style
84 #      comments: if the first line's prefix contains nothing but
85 #      whitespace surrounding a "/*", then the prefix for all subsequent
86 #      lines is the same as the first line's prefix except with each of
87 #      "/" and possibly "*" replaced by a " ".  The replacement of "*"
88 #      by " " is consistent throughout all subsequent lines.
89 #   6. Blank lines, even if preceded by the prefix, do not appear
90 #      within the FSF copyright statement.
91 #   7. Each copyright year is 2 or 4 digits, and years are separated by
92 #      commas or dashes.  Whitespace may occur after commas.
93
94 use strict;
95 use warnings;
96
97 my $copyright_re = 'Copyright';
98 my $circle_c_re = '(?:\([cC]\)|@copyright{}|&copy;)';
99 my $holder = 'Free Software Foundation, Inc.';
100 my $prefix_max = 5;
101 my $margin = 72;
102 my $tab_width = 8;
103
104 my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR};
105 if (!$this_year || $this_year !~ m/^\d{4}$/)
106   {
107     my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ());
108     $this_year = $year + 1900;
109   }
110
111 # Unless the file consistently uses "\r\n" as the EOL, use "\n" instead.
112 my $eol = /(?:^|[^\r])\n/ ? "\n" : "\r\n";
113
114 my $leading;
115 my $prefix;
116 my $ws_re;
117 my $stmt_re;
118 while (/(^|\n)(.{0,$prefix_max})$copyright_re/g)
119   {
120     $leading = "$1$2";
121     $prefix = $2;
122     if ($prefix =~ /^(\s*\/)\*(\s*)$/)
123       {
124         $prefix =~ s,/, ,;
125         my $prefix_ws = $prefix;
126         $prefix_ws =~ s/\*/ /; # Only whitespace.
127         if (/\G(?:[^*\n]|\*[^\/\n])*\*?\n$prefix_ws/)
128           {
129             $prefix = $prefix_ws;
130           }
131       }
132     $ws_re = '[ \t\r\f]'; # \s without \n
133     $ws_re =
134       "(?:$ws_re*(?:$ws_re|\\n" . quotemeta($prefix) . ")$ws_re*)";
135     my $holder_re = $holder;
136     $holder_re =~ s/\s/$ws_re/g;
137     my $stmt_remainder_re =
138       "(?:$ws_re$circle_c_re)?"
139       . "$ws_re(?:(?:\\d\\d)?\\d\\d(,$ws_re?|-))*"
140       . "((?:\\d\\d)?\\d\\d)$ws_re$holder_re";
141     if (/\G$stmt_remainder_re/)
142       {
143         $stmt_re =
144           quotemeta($leading) . "($copyright_re$stmt_remainder_re)";
145         last;
146       }
147   }
148 if (defined $stmt_re)
149   {
150     /$stmt_re/ or die; # Should never die.
151     my $stmt = $1;
152     my $sep = $2 ? $2 : "";
153     my $final_year_orig = $3;
154
155     # Handle two-digit year numbers like "98" and "99".
156     my $final_year = $final_year_orig;
157     $final_year <= 99
158       and $final_year += 1900;
159
160     if ($final_year != $this_year)
161       {
162         # Update the year.
163         if ($sep eq '-' && $final_year + 1 == $this_year)
164           {
165             $stmt =~ s/$final_year_orig/$this_year/;
166           }
167         elsif ($sep ne '-' && $final_year + 1 == $this_year)
168           {
169             $stmt =~ s/$final_year_orig/$final_year-$this_year/;
170           }
171         else
172           {
173             $stmt =~ s/$final_year_orig/$final_year, $this_year/;
174           }
175
176         # Normalize all whitespace including newline-prefix sequences.
177         $stmt =~ s/$ws_re/ /g;
178
179         # Put spaces after commas.
180         $stmt =~ s/, ?/, /g;
181
182         # Format within margin.
183         my $stmt_wrapped;
184         my $text_margin = $margin - length($prefix);
185         if ($prefix =~ /^(\t+)/)
186           {
187             $text_margin -= length($1) * ($tab_width - 1);
188           }
189         while (length $stmt)
190           {
191             if (($stmt =~ s/^(.{1,$text_margin})(?: |$)//)
192                 || ($stmt =~ s/^([\S]+)(?: |$)//))
193               {
194                 my $line = $1;
195                 $stmt_wrapped .= $stmt_wrapped ? "$eol$prefix" : $leading;
196                 $stmt_wrapped .= $line;
197               }
198             else
199               {
200                 # Should be unreachable, but we don't want an infinite
201                 # loop if it can be reached.
202                 die;
203               }
204           }
205
206         # Replace the old copyright statement.
207         s/$stmt_re/$stmt_wrapped/;
208       }
209   }
210 else
211   {
212     print STDERR "$ARGV: warning: FSF copyright statement not found\n";
213   }
214
215 # Local variables:
216 # indent-tabs-mode: nil
217 # eval: (add-hook 'write-file-hooks 'time-stamp)
218 # time-stamp-start: "my $VERSION = '"
219 # time-stamp-format: "%:y-%02m-%02d.%02H:%02M"
220 # time-stamp-time-zone: "UTC"
221 # time-stamp-end: "'; # UTC"
222 # End: