update_copyrights revision 6292befae7d18aa1918c958d284dec4957ea31c3
493N/A#!/usr/local/bin/perl -w
0N/A#
416N/A# Copyright (C) 2004-2006 Internet Systems Consortium, Inc. ("ISC")
0N/A# Copyright (C) 1998-2001 Internet Software Consortium.
0N/A#
0N/A# Permission to use, copy, modify, and distribute this software for any
0N/A# purpose with or without fee is hereby granted, provided that the above
0N/A# copyright notice and this permission notice appear in all copies.
0N/A#
0N/A# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
0N/A# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
0N/A# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
0N/A# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
0N/A# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
0N/A# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
0N/A# PERFORMANCE OF THIS SOFTWARE.
0N/A
0N/A# $Id: update_copyrights,v 1.44 2006/11/30 23:58:49 marka Exp $
0N/A
0N/Arequire 5.002;
0N/A
0N/A# Map copyright owners to the files containing copyright messages.
0N/A# The first line of the copyright message is not in the file;
0N/A# it is constructed by this script.
0N/A
0N/Amy %owner2filename = (
0N/A "" => "util/COPYRIGHT",
0N/A "NAI" => "util/COPYRIGHT.NAI",
0N/A "NOM" => "util/COPYRIGHT.NOM",
493N/A "BSDI" => "util/COPYRIGHT.BSDI",
0N/A "BRIEF" => "util/COPYRIGHT.BRIEF",
0N/A "PORTION" => "util/COPYRIGHT.PORTION",
0N/A);
0N/A
0N/A# Map each copyright owner name to a reference to an array containing
0N/A# the lines of the copyright message.
0N/A
0N/Amy %owner2text = ();
0N/A
0N/Amy $keyword_pat = '\$(Id:.*|Revision:.*|Id|Revision)\$';
0N/A
0N/Aforeach $owner (keys %owner2filename) {
0N/A my $f = $owner2filename{$owner};
0N/A open(COPYRIGHT, "<$f") || die "can't open $f: $!";
0N/A @copyright_text = <COPYRIGHT>;
0N/A close(COPYRIGHT);
493N/A $owner2text{$owner} = [ @copyright_text ];
493N/A}
493N/A
0N/Amy %file_types = ();
493N/Amy %file_years = ();
493N/Amy $years_list;
0N/Amy $parent;
493N/A
493N/Awhile (<>) {
493N/A chomp;
493N/A ($file, $type, $years) = split(/\s+/);
493N/A $file_types{$file} = $type;
493N/A $file_years{$file} = $years;
493N/A}
493N/A
493N/Asub getyears {
493N/A $parent = $_[0];
493N/A $parent =~ s/PARENT://;
493N/A $years_list = $file_years{$parent};
493N/A if (defined($years_list) && $years_list =~ /^PARENT:/) {
0N/A print "BAD PARENT:$parent\n";
493N/A undefine($years_list);
493N/A }
493N/A}
493N/A
493N/Asub docbook {
493N/A $parent = $_[0];
493N/A $parent =~ s/\.[^.]*$/.docbook/;
493N/A $years_list = $file_years{$parent};
493N/A}
493N/A
493N/Asub copyright {
493N/A my $holder = shift;
493N/A my $result = "";
493N/A return $result unless (@_);
0N/A $result = "$result <copyright>\n";
0N/A $result = "$result <year>$_</year>\n" foreach (@_);
0N/A $result = "$result <holder>$holder</holder>\n";
0N/A $result = "$result </copyright>\n";
0N/A return $result;
0N/A}
0N/A
493N/Asub copyrights {
493N/A my $a = copyright("Internet Systems Consortium, Inc. (\"ISC\")",
493N/A grep({ $_ >= 2004} @_));
493N/A my $b = copyright("Internet Software Consortium.",
493N/A grep({ $_ < 2004} @_));
493N/A return "$a$b";
493N/A}
493N/A
493N/Aforeach $file (keys %file_types) {
493N/A $typeandowner = $file_types{$file};
0N/A $years_list = $file_years{$file};
493N/A
493N/A if ( ! -f $file ) {
493N/A print "$file: missing\n";
493N/A next;
493N/A }
493N/A
0N/A if ($years_list =~ /PARENT:/) {
493N/A getyears($years_list);
493N/A if (!defined $years_list) {
493N/A print "$file: has bad parent $parent\n";
493N/A next;
493N/A }
493N/A }
493N/A
493N/A # copyright notice is now generated from the source.
493N/A next if ($years_list eq "DOCBOOK");
493N/A
493N/A if ($years_list eq "DOCBOOK") {
0N/A docbook($file);
0N/A if (!defined $years_list) {
493N/A print "$file: has bad parent $parent\n";
493N/A next;
0N/A }
493N/A }
355N/A
355N/A @years = split(/,/, $years_list);
493N/A
493N/A my ($type, $owner) = split(/\./, $typeandowner);
493N/A $owner = "" if !defined $owner;
493N/A
493N/A $textp = $owner2text{$owner};
493N/A if (!defined $textp) {
493N/A print "$file: unknown copyright owner $owner\n";
493N/A next;
493N/A }
493N/A
493N/A next if $type eq "X" or $type eq "BAT";
493N/A
493N/A $before_copyright = "";
493N/A $c_comment = 0;
493N/A $shell_comment = 0;
493N/A $m4_comment = 0;
493N/A $sgml_comment = 0;
493N/A $zone_comment = 0;
0N/A $man_comment = 0;
493N/A $start_comment = "";
493N/A $end_comment = "";
493N/A $first = "";
0N/A if ($type =~ /^(C|YACC|CONF-C)$/) {
0N/A $c_comment = 1;
493N/A $start_comment = "/*\n";
0N/A $prefix = " * ";
493N/A $end_comment = " */\n";
0N/A } elsif ($type =~ /^(SH|PERL|TCL|MAKE|CONF-SH)$/) {
493N/A $shell_comment = 1;
493N/A $prefix = "# ";
0N/A } elsif ($type eq "ZONE" || $type eq "MC") {
493N/A $zone_comment = 1;
493N/A $prefix = "; ";
0N/A } elsif ($type eq "MAN") {
0N/A $man_comment = 1;
0N/A $prefix = ".\\\" ";
0N/A } elsif ($type eq "M4") {
493N/A $m4_comment = 1;
355N/A $prefix = "dnl ";
493N/A } elsif ($type eq "HTML" || $type eq "SGML") {
493N/A $sgml_comment = 1;
493N/A $start_comment = "<!--\n";
493N/A $prefix = " - ";
493N/A $end_comment = "-->\n";
493N/A } elsif ($type eq "TXT") {
493N/A $prefix = "";
493N/A } else {
493N/A print "$file: type '$type' not supported yet; skipping\n";
493N/A next;
493N/A }
493N/A
368N/A ($nonspaceprefix = $prefix) =~ s/\s+$//;
0N/A
0N/A open(SOURCE, "<$file") || die "can't open $file: $!";
0N/A $_ = <SOURCE>;
0N/A if ($type eq "YACC") {
0N/A unless ($_ eq "%{\n") {
493N/A print "$file: unexpected yacc file start ",
0N/A "(expected \"%{\\n\")\n";
355N/A close(SOURCE);
0N/A next;
493N/A }
0N/A $before_copyright = "$_";
0N/A $_ = <SOURCE>;
0N/A }
0N/A if ($c_comment && /^\/\*/) {
493N/A $_ = <SOURCE>;
0N/A if ($_ !~ /[Cc]opyright/) {
0N/A print "$file: non-copyright comment\n";
0N/A close(SOURCE);
0N/A next;
493N/A }
0N/A if ($_ !~ /\*\//) {
0N/A while (<SOURCE>) {
0N/A last if $_ =~ /\*\//;
0N/A }
0N/A }
493N/A } elsif ($shell_comment) {
493N/A if (/^\#\!/) {
493N/A $before_copyright = "$_#\n";
493N/A $_ = <SOURCE>;
0N/A $_ = <SOURCE> if $_ eq "#\n";
493N/A }
493N/A if (/^\#/) {
493N/A if ($_ !~ /[Cc]opyright/) {
493N/A print "$file: non-copyright comment\n";
0N/A close(SOURCE);
493N/A next;
493N/A }
493N/A while (<SOURCE>) {
493N/A if ($_ !~ /^\#/) {
493N/A $first = $_;
493N/A last;
493N/A }
493N/A }
493N/A } else {
493N/A $first = $_;
493N/A }
493N/A } elsif (($m4_comment || $zone_comment || $man_comment) &&
493N/A /^\Q$nonspaceprefix\E/) {
493N/A
493N/A while (/^\Q$nonspaceprefix\E\s*$/) {
0N/A $_ = <SOURCE>;
0N/A }
493N/A
0N/A if ($_ !~ /[Cc]opyright/) {
493N/A print "$file: non-copyright comment\n";
493N/A close(SOURCE);
493N/A next;
493N/A }
29N/A while (<SOURCE>) {
0N/A if ($_ !~ /^\Q$nonspaceprefix\E/ ||
493N/A $_ =~ /$keyword_pat/) {
0N/A $first = $_;
0N/A last;
0N/A }
493N/A }
493N/A } elsif ($sgml_comment) {
29N/A $before_copyright = "";
29N/A while (/^<!DOCTYPE/ || /^<\?xml-stylesheet/) {
0N/A $before_copyright = "$before_copyright$_";
493N/A $_ = <SOURCE>;
;
while (!eof(SOURCE) && ! /^<!/ ) {
$before_copyright = "$before_copyright$_";
$_ = <SOURCE>;
}
if (eof(SOURCE)) {
close(SOURCE);
next;
}
}
if (/^<!/) {
$_ = <SOURCE> if $_ eq "<!--\n";
if ($_ !~ /[Cc]opyright/) {
print "$file: non-copyright comment\n";
close(SOURCE);
next;
}
while (defined($_)) {
last if s/.*-->//;
$_ = <SOURCE>;
}
print "$file: unterminated comment\n"
unless defined($_);
if ($_ ne "\n") {
$first = $_;
} else {
$first = <SOURCE>;
}
} else {
$first = $_;
}
} elsif ($type eq "TXT") {
if ($_ =~ /[Cc]opyright/) {
$/ = ""; # paragraph at a time
while (<SOURCE>) {
# Not very maintainable, but ok enough for now.
last unless
/[Cc]opyright/ ||
/See COPYRIGHT in the source root/ ||
/Permission to use, copy, modify, and / ||
/THE SOFTWARE IS PROVIDED "AS IS" AND /;
}
$/ = "\n";
}
$first = $_;
} else {
$first = $_;
}
$first = "" if ! defined($first);
open(TARGET, ">$file.new") || die "can't open $file.new: $!";
print TARGET $before_copyright if $before_copyright;
print TARGET $start_comment if $start_comment;
$sysyears = "";
$sftyears = "";
$nomyears = "";
#
# Internet Software Consortium: up to 2003
#
$last_year = 0;
$anchor_year = 0;
$years = "";
foreach $year (@years) {
if ($year >= 2004) { next; }
if ($last_year != 0 && $year == $last_year + 1) {
if ($year > $anchor_year + 1) {
substr($years, $anchor_end) = "-$year";
} else {
$years .= ", $year";
}
} else {
$years .= $last_year == 0 ? "$year" : ", $year";
#if ($anchor_year != 0) {
# print "$file: noncontiguous year: ",
# "$year != $last_year + 1\n";
#}
$anchor_year = $year;
$anchor_end = length($years);
}
$last_year = $year;
}
$sftyears = $years;
#
# Nominum: up to 2001.
#
$last_year = 0;
$anchor_year = 0;
$years = "";
foreach $year (@years) {
if ($year >= 2002) { next; }
if ($last_year != 0 && $year == $last_year + 1) {
if ($year > $anchor_year + 1) {
substr($years, $anchor_end) = "-$year";
} else {
$years .= ", $year";
}
} else {
$years .= $last_year == 0 ? "$year" : ", $year";
#if ($anchor_year != 0) {
# print "$file: noncontiguous year: ",
# "$year != $last_year + 1\n";
#}
$anchor_year = $year;
$anchor_end = length($years);
}
$last_year = $year;
}
$nomyears = $years;
#
# Internet Systems Consortium: 2004 onwards.
#
$last_year = 0;
$anchor_year = 0;
$years = "";
$anchor_end = length($years);
foreach $year (@years) {
if ($year < 2004) { next; }
if ($last_year != 0 && $year == $last_year + 1) {
if ($year > $anchor_year + 1) {
substr($years, $anchor_end) = "-$year";
} else {
$years .= ", $year";
}
} else {
$years .= $last_year == 0 ? "$year" : ", $year";
#if ($anchor_year != 0) {
# print "$file: noncontiguous year: ",
# "$year != $last_year + 1\n";
#}
$anchor_year = $year;
$anchor_end = length($years);
}
$last_year = $year;
}
$sysyears = $years;
($firstline, $secondline, $thirdline, @otherlines) = @$textp;
$firstline =~ s/\@SYSYEARS\@/$sysyears/;
$secondline =~ s/\@SFTYEARS\@/$sftyears/;
$thirdline =~ s/\@NOMYEARS\@/$nomyears/;
print TARGET "$prefix$firstline";
if ($sftyears ne "" ) {
print TARGET $secondline =~ /^$/ ? $nonspaceprefix : $prefix;
print TARGET "$secondline";
}
print TARGET $thirdline =~ /^$/ ? $nonspaceprefix : $prefix;
print TARGET "$thirdline";
foreach $_ (@otherlines) {
print TARGET (/^$/ ? $nonspaceprefix : $prefix);
print TARGET "$_";
}
print TARGET $end_comment if $end_comment;
if ($first eq "") {
$first = <SOURCE>;
}
if (defined($first)) {
if ($type eq 'MAN') {
print TARGET "$nonspaceprefix\n";
} else {
print TARGET "\n";
}
undef $/;
$_ = <SOURCE>;
$/ = "\n";
if ($type eq 'SGML' && m:<articleinfo>.*?</articleinfo>:s) {
# print "docinfo: $file\n";
my $r = copyrights(@years);
s:<articleinfo>.*?</articleinfo>:<articleinfo>\n$r </articleinfo>:s;
}
if ($type eq 'SGML' && m:<docinfo>.*?</docinfo>:s) {
# print "docinfo: $file\n";
my $r = copyrights(@years);
s:<docinfo>.*?</docinfo>:<docinfo>\n$r </docinfo>:s;
}
if ($type eq 'SGML' && m:<bookinfo>.*?</bookinfo>:s) {
# print "bookinfo: $file\n";
my $r = copyrights(@years);
s:<bookinfo>.*?</bookinfo>:<bookinfo>\n$r </bookinfo>:s;
}
my ($start, $end);
if ($start_comment ne "") {
($start = $start_comment) =~ s/\s*\n/ /;
($end = $end_comment) =~ s/^\s*(.*)\n/ $1\n/;
} elsif ($prefix ne "") {
($start = $prefix) =~ s/\s*\n//;
$end = "\n";
} else {
$start = "";
$end = "\n";
}
if ($first !~ /$keyword_pat/ && $_ !~ /$keyword_pat/) {
$end = "\n$nonspaceprefix" if ($type eq "MAN");
print TARGET "$start\$";
print TARGET "Id";
print TARGET "\$$end\n";
}
print TARGET $first if $first !~ /^\s*$/;
print TARGET $_;
}
close(TARGET);
close(SOURCE);
$mode = (stat $file)[2]&511;
chmod $mode, "$file.new";
if (system("cmp -s $file.new $file") == 0) {
unlink("$file.new");
} else {
rename("$file.new", "$file")
or die "rename($file.new, $file): $!";
}
}