b124bb9aa7bbe02e929178db0ede669d4a32d499
[gnulib.git] / 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 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.  For example, each of these is fine:
46 #
47 #   Copyright @copyright{} 1990-2005, 2007-2009 Free Software
48 #   Foundation, Inc.
49 #
50 #   # Copyright (C) 1990-2005, 2007-2009 Free Software
51 #   # Foundation, Inc.
52 #
53 #   /*
54 #    * Copyright &copy; 90,2005,2007-2009
55 #    * Free Software Foundation, Inc.
56 #    */
57 #
58 # However, the following format is not recognized because the line
59 # prefix changes after the first line:
60 #
61 #   /* Copyright (C) 1990-2005, 2007-2009 Free Software
62 #    * Foundation, Inc.  */
63 #
64 # The following copyright statement is not recognized because the
65 # copyright holder is not the FSF:
66 #
67 #   Copyright (C) 1990-2005, 2007-2009 Acme, Inc.
68 #
69 # However, any correctly formatted FSF copyright statement following
70 # either of the previous two copyright statements would be recognized.
71 #
72 # The exact conditions that a file's FSF copyright statement must meet
73 # to be recognized are:
74 #
75 #   1. It is the first FSF copyright statement that meets all of the
76 #      following conditions.  Subsequent FSF copyright statements are
77 #      ignored.
78 #   2. Its format is "Copyright (C)", then a list of copyright years,
79 #      and then the name of the copyright holder, which is "Free
80 #      Software Foundation, Inc.".
81 #   3. The "(C)" takes one of the following forms or is omitted
82 #      entirely:
83 #
84 #        A. (C)
85 #        B. (c)
86 #        C. @copyright{}
87 #        D. &copy;
88 #
89 #   4. The "Copyright" appears at the beginning of a line except that it
90 #      may be prefixed by any sequence (e.g., a comment) of no more than
91 #      5 characters.
92 #   5. Iff such a prefix is present, the same prefix appears at the
93 #      beginning of each remaining line within the FSF copyright
94 #      statement.
95 #   6. Blank lines, even if preceded by the prefix, do not appear
96 #      within the FSF copyright statement.
97 #   7. Each copyright year is 2 or 4 digits, and years are separated by
98 #      commas or dashes.  Whitespace may occur after commas.
99
100 use strict;
101 use warnings;
102
103 my $copyright_re = 'Copyright';
104 my $circle_c_re = '(?:\([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 while (/(^|\n)(.{0,$prefix_max})$copyright_re/g)
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     my $stmt_remainder_re =
134       "(?:$ws_re$circle_c_re)?"
135       . "$ws_re(?:(?:\\d\\d)?\\d\\d(,$ws_re?|-))*"
136       . "((?:\\d\\d)?\\d\\d)$ws_re$holder_re";
137     if (/\G$stmt_remainder_re/)
138       {
139         $stmt_re =
140           quotemeta("$leading$prefix")
141           . "($copyright_re$stmt_remainder_re)";
142         last;
143       }
144   }
145 if (defined $stmt_re)
146   {
147     /$stmt_re/ or die; # Should never die.
148     my $stmt = $1;
149     my $sep = $2 ? $2 : "";
150     my $final_year_orig = $3;
151
152     # Handle two-digit year numbers like "98" and "99".
153     my $final_year = $final_year_orig;
154     $final_year <= 99
155       and $final_year += 1900;
156
157     if ($final_year != $this_year)
158       {
159         # Update the year.
160         if ($sep eq '-' && $final_year + 1 == $this_year)
161           {
162             $stmt =~ s/$final_year_orig/$this_year/;
163           }
164         elsif ($sep ne '-' && $final_year + 1 == $this_year)
165           {
166             $stmt =~ s/$final_year_orig/$final_year-$this_year/;
167           }
168         else
169           {
170             $stmt =~ s/$final_year_orig/$final_year, $this_year/;
171           }
172
173         # Normalize all whitespace including newline-prefix sequences.
174         $stmt =~ s/$ws_re/ /g;
175
176         # Put spaces after commas.
177         $stmt =~ s/, ?/, /g;
178
179         # Format within margin.
180         my $stmt_wrapped;
181         my $text_margin = $margin - length($prefix);
182         if ($prefix =~ /^(\t+)/)
183           {
184             $text_margin -= length($1) * ($tab_width - 1);
185           }
186         while (length $stmt)
187           {
188             if (($stmt =~ s/^(.{1,$text_margin})(?: |$)//)
189                 || ($stmt =~ s/^([\S]+)(?: |$)//))
190               {
191                 my $line = $1;
192                 $stmt_wrapped .= $stmt_wrapped ? $eol : $leading;
193                 $stmt_wrapped .= "$prefix$line";
194               }
195             else
196               {
197                 # Should be unreachable, but we don't want an infinite
198                 # loop if it can be reached.
199                 die;
200               }
201           }
202
203         # Replace the old copyright statement.
204         s/$stmt_re/$stmt_wrapped/;
205       }
206   }
207 else
208   {
209     print STDERR "$ARGV: warning: FSF copyright statement not found\n";
210   }
211
212 # Local variables:
213 # indent-tabs-mode: nil
214 # eval: (add-hook 'write-file-hooks 'time-stamp)
215 # time-stamp-start: "my $VERSION = '"
216 # time-stamp-format: "%:y-%02m-%02d.%02H:%02M"
217 # time-stamp-time-zone: "UTC"
218 # time-stamp-end: "'; # UTC"
219 # End: