update_copyrights revision b2f875a5354d86715284ed1e34dbad5b5c23f3ae
4737N/A#!/usr/local/bin/perl -w
4737N/A#
4737N/A# Copyright (C) 2004-2010 Internet Systems Consortium, Inc. ("ISC")
4737N/A# Copyright (C) 1998-2001 Internet Software Consortium.
4737N/A#
4737N/A# Permission to use, copy, modify, and/or distribute this software for any
4737N/A# purpose with or without fee is hereby granted, provided that the above
4737N/A# copyright notice and this permission notice appear in all copies.
4737N/A#
4737N/A# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
4737N/A# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
4737N/A# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
4737N/A# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
4737N/A# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
4737N/A# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
4737N/A# PERFORMANCE OF THIS SOFTWARE.
4737N/A
4737N/A# $Id: update_copyrights,v 1.56 2010/06/28 23:46:44 tbox Exp $
4737N/A
4737N/Arequire 5.002;
4737N/A
4737N/A# Map copyright owners to the files containing copyright messages.
5336N/A# The first line of the copyright message is not in the file;
4737N/A# it is constructed by this script.
5716N/A#
5716N/A# Usage:
5680N/A#
4737N/A# perl util/update_copyrights <util/copyrights
4737N/A
4737N/Amy %owner2filename = (
5363N/A "" => "util/COPYRIGHT",
4737N/A "NAI" => "util/COPYRIGHT.NAI",
4737N/A "NOM" => "util/COPYRIGHT.NOM",
5363N/A "BSDI" => "util/COPYRIGHT.BSDI",
4737N/A "BRIEF" => "util/COPYRIGHT.BRIEF",
4737N/A "PORTION" => "util/COPYRIGHT.PORTION",
4737N/A);
5363N/A
4737N/A# Map each copyright owner name to a reference to an array containing
6011N/A# the lines of the copyright message.
6011N/A
6011N/Amy %owner2text = ();
6011N/A
6011N/Amy $keyword_pat = '\$(Id:.*|Revision:.*|Id|Revision)\$';
6011N/A
6011N/Aforeach $owner (keys %owner2filename) {
6011N/A my $f = $owner2filename{$owner};
5680N/A open(COPYRIGHT, "<$f") || die "can't open $f: $!";
5680N/A @copyright_text = <COPYRIGHT>;
5680N/A close(COPYRIGHT);
4737N/A $owner2text{$owner} = [ @copyright_text ];
4737N/A}
4737N/A
4737N/Amy %file_types = ();
5680N/Amy %file_years = ();
5690N/Amy $years_list;
5690N/Amy $parent;
5690N/A
5690N/A($dummy,$dummy,$dummy,$dummy,$dummy,$this_year,$dummy,$dummy,$dummy) = localtime(time());
5690N/A$this_year += 1900;
4737N/A
4737N/A
5851N/Awhile (<>) {
4737N/A chomp;
4737N/A ($file, $type, $years) = split(/\s+/);
5851N/A $file_types{$file} = $type;
4737N/A $file_years{$file} = $years;
5851N/A}
5487N/A
5487N/Asub getyears {
5851N/A $parent = $_[0];
5851N/A $parent =~ s/PARENT://;
5851N/A $years_list = $file_years{$parent};
5851N/A if (defined($years_list) && $years_list =~ /^PARENT:/) {
5851N/A print "BAD PARENT:$parent\n";
5672N/A undefine($years_list);
5680N/A }
5672N/A}
5672N/A
5851N/Asub docbook {
5851N/A $parent = $_[0];
5851N/A $parent =~ s/\.[^.]*$/.docbook/;
5851N/A $years_list = $file_years{$parent};
5851N/A}
5851N/A
4737N/Asub copyright {
5851N/A my $holder = shift;
5851N/A my $result = "";
5851N/A return $result unless (@_);
5851N/A $result = "$result <copyright>\n";
5851N/A $result = "$result <year>$_</year>\n" foreach (@_);
5851N/A $result = "$result <holder>$holder</holder>\n";
5851N/A $result = "$result </copyright>\n";
5851N/A return $result;
5594N/A}
5851N/A
5851N/Asub copyrights {
5851N/A my $a = copyright("Internet Systems Consortium, Inc. (\"ISC\")",
5680N/A grep({ $_ >= 2004} @_));
5851N/A my $b = copyright("Internet Software Consortium.",
5851N/A grep({ $_ < 2004} @_));
5851N/A return "$a$b";
5851N/A}
5851N/A
5851N/Aforeach $file (keys %file_types) {
5851N/A $typeandowner = $file_types{$file};
5851N/A $years_list = $file_years{$file};
5851N/A
5851N/A if ( ! -f $file ) {
4737N/A print "$file: missing\n";
5851N/A next;
5851N/A }
5851N/A # print "Doing: $file";
4737N/A
4737N/A if ($years_list =~ /PARENT:/) {
4737N/A getyears($years_list);
4737N/A if (!defined $years_list) {
4737N/A print "$file: has bad parent $parent\n";
4737N/A next;
4737N/A }
4737N/A }
4737N/A
5680N/A # copyright notice is now generated from the source.
4737N/A next if ($years_list eq "DOCBOOK");
4737N/A
4737N/A if ($years_list eq "DOCBOOK") {
4737N/A docbook($file);
4737N/A if (!defined $years_list) {
4737N/A print "$file: has bad parent $parent\n";
4737N/A next;
4737N/A }
4737N/A }
4737N/A
4737N/A @years = split(/,/, $years_list);
4737N/A
4897N/A my ($type, $owner) = split(/\./, $typeandowner);
4897N/A $owner = "" if !defined $owner;
4897N/A
4737N/A $textp = $owner2text{$owner};
4737N/A if (!defined $textp) {
4897N/A print "$file: unknown copyright owner $owner\n";
4737N/A next;
5336N/A }
4737N/A
4737N/A next if $type eq "X" or $type eq "BAT";
4737N/A
4737N/A $before_copyright = "";
4737N/A $c_comment = 0;
5336N/A $shell_comment = 0;
5336N/A $m4_comment = 0;
5336N/A $sgml_comment = 0;
5336N/A $zone_comment = 0;
4737N/A $man_comment = 0;
4897N/A $start_comment = "";
4897N/A $end_comment = "";
5680N/A $first = "";
5680N/A if ($type =~ /^(C|YACC|CONF-C)$/) {
5680N/A $c_comment = 1;
5680N/A $start_comment = "/*\n";
5680N/A $prefix = " * ";
4897N/A $end_comment = " */\n";
4897N/A } elsif ($type =~ /^(SH|PERL|TCL|MAKE|CONF-SH|RNC)$/) {
4897N/A $shell_comment = 1;
4737N/A $prefix = "# ";
4737N/A } elsif ($type eq "ZONE" || $type eq "MC") {
4737N/A $zone_comment = 1;
4737N/A $prefix = "; ";
4737N/A } elsif ($type eq "MAN") {
4737N/A $man_comment = 1;
4737N/A $prefix = ".\\\" ";
4896N/A } elsif ($type eq "M4") {
4737N/A $m4_comment = 1;
4739N/A $prefix = "dnl ";
4737N/A } elsif ($type eq "HTML" || $type eq "SGML") {
4737N/A $sgml_comment = 1;
5851N/A $start_comment = "<!--\n";
4898N/A $prefix = " - ";
4737N/A $end_comment = "-->\n";
4737N/A } elsif ($type eq "TXT") {
4737N/A $prefix = "";
4737N/A } else {
4737N/A print "$file: type '$type' not supported yet; skipping\n";
4737N/A next;
}
($nonspaceprefix = $prefix) =~ s/\s+$//;
open(SOURCE, "<$file") || die "can't open $file: $!";
$_ = <SOURCE>;
if ($type eq "YACC") {
unless ($_ eq "%{\n") {
print "$file: unexpected yacc file start ",
"(expected \"%{\\n\")\n";
close(SOURCE);
next;
}
$before_copyright = "$_";
$_ = <SOURCE>;
}
if ($c_comment && /^\/\*/) {
$_ = <SOURCE>;
if ($_ !~ /[Cc]opyright/) {
print "$file: non-copyright comment\n";
close(SOURCE);
next;
}
if ($_ !~ /\*\//) {
while (<SOURCE>) {
last if $_ =~ /\*\//;
}
}
} elsif ($shell_comment) {
if (/^\#\!/) {
$before_copyright = "$_#\n";
$_ = <SOURCE>;
$_ = <SOURCE> if $_ eq "#\n";
}
if (/^\#/) {
if ($_ !~ /[Cc]opyright/) {
print "$file: non-copyright comment\n";
close(SOURCE);
next;
}
while (<SOURCE>) {
if ($_ !~ /^\#/) {
$first = $_;
last;
}
}
} else {
$first = $_;
}
} elsif (($m4_comment || $zone_comment || $man_comment) &&
/^\Q$nonspaceprefix\E/) {
while (/^\Q$nonspaceprefix\E\s*$/) {
$_ = <SOURCE>;
}
if ($_ !~ /[Cc]opyright/) {
print "$file: non-copyright comment\n";
close(SOURCE);
next;
}
while (<SOURCE>) {
if ($_ !~ /^\Q$nonspaceprefix\E/ ||
$_ =~ /$keyword_pat/) {
$first = $_;
last;
}
}
} elsif ($sgml_comment) {
$before_copyright = "";
while (/^<!DOCTYPE/ || /^<\?xml-stylesheet/ || /^<\?xml /) {
# print "SGML: $_";
$before_copyright = "$before_copyright$_";
if (/>$/ ) {
$_ = <SOURCE>;
close(SOURCE) if (eof(SOURCE));
next;
}
$_ = <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);
my $andor = 0;
foreach $year (@years) {
if ($year < 2004) { next; }
$andor = 1 if ($year >= 2007);
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, @otherlines) = @$textp;
$firstline =~ s/\@SYSYEARS\@/$sysyears/;
$secondline =~ s/\@SFTYEARS\@/$sftyears/;
print TARGET "$prefix$firstline";
if ($sftyears ne "" ) {
print TARGET $secondline =~ /^$/ ? $nonspaceprefix : $prefix;
print TARGET "$secondline";
}
foreach $_ (@otherlines) {
s:modify, and distribute:modify, and/or distribute: if ($andor);
print TARGET (/^$/ ? $nonspaceprefix : $prefix);
s/\@NOMYEARS\@/$nomyears/;
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";
}
if ($type eq "C" && $sysyears =~ /$this_year/) {
my $body = "";
while (<SOURCE>) {
# Process leading white space.
# Remove 1-7 spaces followed by a tab into a single
# tab if at start of line or proceeded by tabs.
s/^(\t*) {1,7}\t/$1\t/ while (/^\t* {1,7}\t/);
# Convert 8 spaces into tabs if at start of line
# or preceeded by tabs.
s/^(\t*) /$1\t/ while (/^\t* /);
# Remove trailing white space.
s/[ \t]*$//;
$body = "$body$_";
}
$_ = $body;
} else {
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/ &&
(!defined($_) || $_ !~ /$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 $_ if (defined($_));
}
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): $!";
}
}