]> git.saurik.com Git - bison.git/blame - build-aux/update-b4-copyright
maint: run "make update-copyright"
[bison.git] / build-aux / update-b4-copyright
CommitLineData
c9e0a880 1#!/usr/bin/perl -0777 -pi
ae509723
JD
2
3# Update b4_copyright invocations or b4_copyright_years definitions to
4# include the current year.
c9e0a880 5
219c26ea 6# Copyright (C) 2009-2010 Free Software Foundation, Inc.
c9e0a880
JD
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
21use strict;
22use warnings;
23
ae509723
JD
24my $margin = 72;
25
c9e0a880 26my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR};
ae509723 27if (!$this_year || $this_year !~ m/^\d{4}$/)
c9e0a880
JD
28 {
29 my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ());
30 $this_year = $year + 1900;
31 }
c9e0a880
JD
32my $old_re = <<'EOF'
33 (
34 (?:^|\n)
ae509723 35 #BEFORE
c9e0a880 36 (?:
ae509723 37 b4_copyright\(\[[^][]*]
c9e0a880
JD
38 | m4_(?:push|pop)def\(\[b4_copyright_years]
39 )
ae509723 40 #AFTER
c9e0a880
JD
41 )
42 (?:
43 ,\s*
44 (
75ac158b 45 \[\s* (?:\d{4}(?:,\s*|-))* (\d{4}) \s*]
c9e0a880
JD
46 )
47 )?
48 \)
49EOF
50 ;
51
ae509723 52while (/($old_re)/gx)
c9e0a880 53 {
ae509723
JD
54 my $start = pos() - length ($1);
55 my $b4_copyright_line = $2;
56 my $year_lines = $3;
75ac158b 57 my $final_year = $4;
c9e0a880
JD
58 $year_lines .= ')';
59
c9e0a880
JD
60 # If there was a second argument, it contains years, so update them.
61 if ($final_year)
62 {
63 $b4_copyright_line .= ',';
64 if ($final_year != $this_year)
65 {
66 # Update the year.
75ac158b 67 $year_lines =~ s/$final_year/$final_year, $this_year/;
c9e0a880
JD
68 }
69
ae509723
JD
70 # Normalize all whitespace.
71 $year_lines =~ s/\s+/ /g;
c9e0a880 72
ae509723
JD
73 # Put spaces after commas.
74 $year_lines =~ s/, ?/, /g;
c9e0a880 75
75ac158b
JD
76 # Compress to intervals.
77 $year_lines =~
78 s/
79 (\d{4})
80 (?:
81 (,\ |-)
82 ((??{
83 if ($2 eq '-') { '\d{4}'; }
84 elsif (!$3) { $1 + 1; }
85 else { $3 + 1; }
86 }))
87 )+
88 /$1-$3/gx;
89
ae509723
JD
90 # Format within margin.
91 my $year_lines_new;
92 my $indent = index ($b4_copyright_line, '[');
93 --$indent if ($b4_copyright_line =~ m/^\n/);
94 while (length $year_lines)
95 {
96 my $text_margin = $margin - $indent;
97 if (($year_lines =~ s/^(.{1,$text_margin})(?: |$)//)
98 || ($year_lines =~ s/^([\S]+)(?: |$)//))
99 {
100 my $line = "\n" . (' 'x$indent) . $1;
101 ++$indent if (!$year_lines_new);
102 $year_lines_new .= $line;
103 }
104 else
105 {
106 # Should be unreachable, but we don't want an infinite
107 # loop if it can be reached.
108 die;
109 }
110 }
111
112 # Replace the old invocation. Should never die.
113 die if (!s/$old_re\G/$b4_copyright_line$year_lines_new/x);
c9e0a880 114
ae509723
JD
115 # Prepare for the next search.
116 pos () = $start + length ("$b4_copyright_line$year_lines_new");
117 }
c9e0a880
JD
118 }
119
ae509723 120while (/(\bb4_copyright\()/g)
c9e0a880 121 {
915becaa
JD
122 my $start = pos () - length ($1);
123 my $end = pos ();
ae509723 124 my $re = $old_re;
915becaa 125 pos () = $start;
ae509723
JD
126 $re =~ s/\#BEFORE/\\G/;
127 if (!/$re/x)
128 {
915becaa 129 my $line = (substr ($_, 0, $start) =~ s/\n/\n/g) + 1;
ae509723 130 print STDERR
915becaa 131 "$ARGV:$line: warning: failed to update a b4_copyright\n";
ae509723 132 }
915becaa 133 pos () = $end;
c9e0a880 134 }
ae509723 135
915becaa 136while (/(\[b4_copyright_years])/g)
c9e0a880 137 {
915becaa
JD
138 my $start = pos () - length ($1);
139 my $end = pos ();
ae509723
JD
140 my $re = $old_re;
141 $re =~ s/\#AFTER/\\G/;
142 if (!/$re/x)
143 {
915becaa
JD
144 # The substr operation blows away pos (), so restoring pos ()
145 # at the end is necessary.
146 my $line = (substr ($_, 0, $start) =~ s/\n/\n/g) + 1;
ae509723 147 print STDERR
915becaa
JD
148 "$ARGV:$line: warning: failed to update a"
149 . " b4_copyright_years\n";
ae509723 150 }
915becaa 151 pos () = $end;
c9e0a880 152 }