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