update-copyright: clean up code a little
[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-04.07:25'; # UTC
5
6 # Copyright (C) 2009 Free Software Foundation
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 may wish to
25 # place a target like the following in the top-level makefile in your
26 # project:
27 #
28 #   .PHONY: update-copyright
29 #   update-copyright:
30 #       if test -d .git; then                                   \
31 #         git grep -l -w Copyright                              \
32 #           | grep -v -E '(^|/)(COPYING|ChangeLog)'             \
33 #           | xargs $(srcdir)/build-aux/$@;                     \
34 #       fi
35 #
36 # In the second grep, you can build a list of files to skip within your
37 # project.
38 #
39 # Iff an FSF copyright statement is discovered in a file and the final
40 # year is not the current year, the statement is updated for the new
41 # year and reformatted to fit within 72 columns.  A warning is printed
42 # for every file for which no FSF copyright statement is discovered.
43 #
44 # Each file's FSF copyright statement must be formated correctly in
45 # order to be recognized, and it must appear before other text that
46 # looks like the start of a copyright statement.  For example, each of
47 # these by itself 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 &copy; 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 #
66 # The following copyright statement is not recognized because the
67 # copyright holder is not the FSF:
68 #
69 #   Copyright (C) 1990-2005, 2007-2009 Acme, Inc.
70 #
71 # Moreover, any FSF copyright statement following either of the previous
72 # copyright statements might not be recognized.
73 #
74 # The exact conditions that a file's FSF copyright statement must meet
75 # to be recognized are listed below.  They may seem slightly complex,
76 # but you need not worry if some file in your project accidentally
77 # breaks one.  The worst that can happen is that a file is not updated
78 # and a warning is issued.
79 #
80 #   1. The format is "Copyright (C)" (where "(C)" can also be "(c)",
81 #      "@copyright{}", or "&copy;"), then a list of copyright years, and
82 #      then the name of the copyright holder, which is "Free Software
83 #      Foundation, Inc.".
84 #   2. "Copyright (C)" appears at the beginning of a line except that it
85 #      may be prefixed by any sequence (e.g., a comment) of no more than
86 #      5 characters.
87 #   3. The prefix of "Copyright (C)" is the same as the prefix on the
88 #      file's first occurrence of "Copyright (C)" that matches condition
89 #      #2.  Stated more simply, if something that looks like the start
90 #      of a copyright statement appears earlier than the FSF copyright
91 #      statement, the FSF copyright statement might not be recognized.
92 #      This condition might be removed in the future.
93 #   4. Iff a prefix is present before "Copyright (C)", the same prefix
94 #      appears at the beginning of each remaining line within the FSF
95 #      copyright statement.
96 #   5. Blank lines, even if preceded by the prefix, do not appear
97 #      within the FSF copyright statement.
98 #   6. Each copyright year is 2 or 4 digits, and years are separated by
99 #      commas or dashes.  Whitespace may occur after commas.
100
101 use strict;
102 use warnings;
103
104 my $copyright_re = 'Copyright (?:\([cC]\)|@copyright{}|&copy;)';
105 my $holder = 'Free Software Foundation, Inc.';
106 my $prefix_max = 5;
107 my $margin = 72;
108 my $tab_width = 8;
109
110 my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR};
111 if (!$this_year || $this_year !~ m/^\d{4}$/)
112   {
113     my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ());
114     $this_year = $year + 1900;
115   }
116
117 # Unless the file consistently uses "\r\n" as the EOL, use "\n" instead.
118 my $eol = /(?:^|[^\r])\n/ ? "\n" : "\r\n";
119
120 my $leading;
121 my $prefix;
122 my $ws_re;
123 my $stmt_re;
124 if (/(^|\n)(.{0,$prefix_max})$copyright_re/)
125   {
126     $leading = $1;
127     $prefix = $2;
128     $ws_re = '[ \t\r\f]'; # \s without \n
129     $ws_re =
130       "(?:$ws_re*(?:$ws_re|\\n" . quotemeta($prefix) . ")$ws_re*)";
131     my $holder_re = $holder;
132     $holder_re =~ s/\s/$ws_re/g;
133     $stmt_re =
134       quotemeta("$leading$prefix") . "($copyright_re$ws_re"
135       . "(?:(?:\\d\\d)?\\d\\d(,$ws_re?|-))*"
136       . "((?:\\d\\d)?\\d\\d)$ws_re$holder_re)";
137   }
138 if (defined $stmt_re && /$stmt_re/)
139   {
140     my $stmt = $1;
141     my $sep = $2 ? $2 : "";
142     my $final_year_orig = $3;
143
144     # Handle two-digit year numbers like "98" and "99".
145     my $final_year = $final_year_orig;
146     $final_year <= 99
147       and $final_year += 1900;
148
149     if ($final_year != $this_year)
150       {
151         # Update the year.
152         if ($sep eq '-' && $final_year + 1 == $this_year)
153           {
154             $stmt =~ s/$final_year_orig/$this_year/;
155           }
156         elsif ($sep ne '-' && $final_year + 1 == $this_year)
157           {
158             $stmt =~ s/$final_year_orig/$final_year-$this_year/;
159           }
160         else
161           {
162             $stmt =~ s/$final_year_orig/$final_year, $this_year/;
163           }
164
165         # Normalize all whitespace including newline-prefix sequences.
166         $stmt =~ s/$ws_re/ /g;
167
168         # Put spaces after commas.
169         $stmt =~ s/, ?/, /g;
170
171         # Format within margin.
172         my $stmt_wrapped;
173         my $text_margin = $margin - length($prefix);
174         if ($prefix =~ /^(\t+)/)
175           {
176             $text_margin -= length($1) * ($tab_width - 1);
177           }
178         while (length $stmt)
179           {
180             if (($stmt =~ s/^(.{1,$text_margin})(?: |$)//)
181                 || ($stmt =~ s/^([\S]+)(?: |$)//))
182               {
183                 my $line = $1;
184                 $stmt_wrapped .= $stmt_wrapped ? $eol : $leading;
185                 $stmt_wrapped .= "$prefix$line";
186               }
187             else
188               {
189                 # Should be unreachable, but we don't want an infinite
190                 # loop if it can be reached.
191                 die;
192               }
193           }
194
195         # Replace the old copyright statement.
196         s/$stmt_re/$stmt_wrapped/;
197       }
198   }
199 else
200   {
201     print STDERR "$ARGV: warning: FSF copyright statement not found\n";
202   }
203
204 # Local variables:
205 # indent-tabs-mode: nil
206 # eval: (add-hook 'write-file-hooks 'time-stamp)
207 # time-stamp-start: "my $VERSION = '"
208 # time-stamp-format: "%:y-%02m-%02d.%02H:%02M"
209 # time-stamp-time-zone: "UTC"
210 # time-stamp-end: "'; # UTC"
211 # End: