]> git.saurik.com Git - bison.git/blob - build-aux/update-b4-copyright
maint: automate b4_copyright updates.
[bison.git] / build-aux / update-b4-copyright
1 #!/usr/bin/perl -0777 -pi
2 # Update an b4_copyright invocation to include the current year.
3
4 # Copyright (C) 2009 Free Software Foundation, Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 use strict;
20 use warnings;
21
22 my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR};
23 if (!$this_year || $this_year !~ m/^\d\d(\d\d)?$/)
24 {
25 my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ());
26 $this_year = $year + 1900;
27 }
28 my $margin = 72;
29 my $old_re = <<'EOF'
30 (
31 (?:^|\n)
32 (?:
33 b4_copyright\(\[[^]]*]
34 | m4_(?:push|pop)def\(\[b4_copyright_years]
35 )
36 )
37 (?:
38 ,\s*
39 (
40 \[\s* (?:\d{4}(,\s*|-))* (\d{4}) \s*]
41 )
42 )?
43 \)
44 EOF
45 ;
46
47 while (/$old_re/x)
48 {
49 my $b4_copyright_line = $1;
50 my $year_lines = $2;
51 my $sep = $3 ? $3 : "";
52 my $final_year = $4;
53 $year_lines .= ')';
54
55 # Mark it completed.
56 $b4_copyright_line =~ s/b4_/b4*/g;
57
58 # If there was a second argument, it contains years, so update them.
59 if ($final_year)
60 {
61 $b4_copyright_line .= ',';
62 if ($final_year != $this_year)
63 {
64 # Update the year.
65 if ($sep eq '-' && $final_year + 1 == $this_year)
66 {
67 $year_lines =~ s/$final_year/$this_year/;
68 }
69 elsif ($sep ne '-' && $final_year + 1 == $this_year)
70 {
71 $year_lines =~ s/$final_year/$final_year-$this_year/;
72 }
73 else
74 {
75 $year_lines =~ s/$final_year/$final_year, $this_year/;
76 }
77 }
78
79 # Normalize all whitespace.
80 $year_lines =~ s/\s+/ /g;
81
82 # Put spaces after commas.
83 $year_lines =~ s/, ?/, /g;
84
85 # Format within margin.
86 my $year_lines_new;
87 my $indent = index ($b4_copyright_line, '[');
88 --$indent if ($b4_copyright_line =~ m/^\n/);
89 while (length $year_lines)
90 {
91 my $text_margin = $margin - $indent;
92 if (($year_lines =~ s/^(.{1,$text_margin})(?: |$)//)
93 || ($year_lines =~ s/^([\S]+)(?: |$)//))
94 {
95 my $line = "\n" . (' 'x$indent) . $1;
96 ++$indent if (!$year_lines_new);
97 $year_lines_new .= $line;
98 }
99 else
100 {
101 # Should be unreachable, but we don't want an infinite
102 # loop if it can be reached.
103 die;
104 }
105 }
106 $year_lines = $year_lines_new;
107 }
108
109 # Replace the old invocation.
110 s/$old_re/$b4_copyright_line$year_lines/x;
111 }
112
113 if (/\bb4_copyright\(/)
114 {
115 print STDERR
116 "$ARGV: warning: failed to update a b4_copyright invocation\n";
117 }
118 if (/\[b4_copyright_years]/)
119 {
120 print STDERR
121 "$ARGV: warning: failed to update a b4_copyright_years use\n";
122 }
123
124 s/b4\*copyright/b4_copyright/g;