update_copyrights 15.9 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
#!/usr/local/bin/perl -w
Bob Halley's avatar
update  
Bob Halley committed
2
#
Automatic Updater's avatar
Automatic Updater committed
3
# Copyright (C) 2004-2009  Internet Systems Consortium, Inc. ("ISC")
Brian Wellington's avatar
Brian Wellington committed
4
# Copyright (C) 1998-2001  Internet Software Consortium.
5
#
Mark Andrews's avatar
Mark Andrews committed
6
# Permission to use, copy, modify, and/or distribute this software for any
Bob Halley's avatar
update  
Bob Halley committed
7 8
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
9
#
Mark Andrews's avatar
Mark Andrews committed
10 11 12 13 14 15 16
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
17

Automatic Updater's avatar
Automatic Updater committed
18
# $Id: update_copyrights,v 1.53 2009/01/06 23:47:57 tbox Exp $
19

20 21 22 23
require 5.002;

# Map copyright owners to the files containing copyright messages.
# The first line of the copyright message is not in the file;
24
# it is constructed by this script.
Rob Austein's avatar
Rob Austein committed
25 26 27 28
#
# Usage:
#
#   perl util/update_copyrights <util/copyrights
29 30

my %owner2filename = (
31 32
	"" => "util/COPYRIGHT",
	"NAI" => "util/COPYRIGHT.NAI",
33
	"NOM" => "util/COPYRIGHT.NOM",
34 35 36
	"BSDI" => "util/COPYRIGHT.BSDI",
	"BRIEF" => "util/COPYRIGHT.BRIEF",
	"PORTION" => "util/COPYRIGHT.PORTION",
37 38 39 40 41 42 43
);

# Map each copyright owner name to a reference to an array containing
# the lines of the copyright message.

my %owner2text = ();

44
my $keyword_pat = '\$(Id:.*|Revision:.*|Id|Revision)\$';
45

46
foreach $owner (keys %owner2filename) {
47 48 49 50 51
        my $f = $owner2filename{$owner};
        open(COPYRIGHT, "<$f") || die "can't open $f: $!";
        @copyright_text = <COPYRIGHT>;
        close(COPYRIGHT);
        $owner2text{$owner} = [ @copyright_text ];
Bob Halley's avatar
Bob Halley committed
52 53
}

54 55 56 57 58
my %file_types = ();
my %file_years = ();
my $years_list;
my $parent;

Bob Halley's avatar
Bob Halley committed
59
while (<>) {
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	chomp;
	($file, $type, $years) = split(/\s+/);
	$file_types{$file} = $type;
	$file_years{$file} = $years;
}

sub getyears {
	$parent = $_[0];
	$parent =~ s/PARENT://;
	$years_list = $file_years{$parent};
	if (defined($years_list) && $years_list =~ /^PARENT:/) {
		print "BAD PARENT:$parent\n";
		undefine($years_list);
	}
}

sub docbook {
	$parent = $_[0];
	$parent =~ s/\.[^.]*$/.docbook/;
	$years_list = $file_years{$parent};
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
sub copyright {
	my $holder = shift;
	my $result = "";
	return $result unless (@_);
	$result = "$result    <copyright>\n";
	$result = "$result      <year>$_</year>\n" foreach (@_);
	$result = "$result      <holder>$holder</holder>\n";
	$result = "$result    </copyright>\n";
	return $result;
}

sub copyrights {
	my $a = copyright("Internet Systems Consortium, Inc. (\"ISC\")",
		   grep({ $_ >= 2004} @_));
	my $b = copyright("Internet Software Consortium.",
		   grep({ $_ < 2004} @_));
	return "$a$b";
}

101 102 103
foreach $file (keys %file_types) {
	$typeandowner = $file_types{$file};
	$years_list = $file_years{$file};
104 105 106 107

        if ( ! -f $file ) {
                print "$file: missing\n";
                next;
David Lawrence's avatar
David Lawrence committed
108
        }
Mark Andrews's avatar
update  
Mark Andrews committed
109
	# print "Doing: $file";
David Lawrence's avatar
David Lawrence committed
110

111 112 113 114 115 116 117 118
        if ($years_list =~ /PARENT:/) {
		getyears($years_list);
		if (!defined $years_list) {
			print "$file: has bad parent $parent\n";
			next;
		}
	}

119
	# copyright notice is now generated from the source.
Mark Andrews's avatar
Mark Andrews committed
120
	next if ($years_list eq "DOCBOOK");
121

122 123 124 125 126 127 128 129 130 131
	if ($years_list eq "DOCBOOK") {
		docbook($file);
		if (!defined $years_list) {
			print "$file: has bad parent $parent\n";
			next;
		}
	}

        @years = split(/,/, $years_list);

132 133 134 135 136 137 138
        my ($type, $owner) = split(/\./, $typeandowner);
        $owner = "" if !defined $owner;

        $textp = $owner2text{$owner};
        if (!defined $textp) {
                print "$file: unknown copyright owner $owner\n";
                next;
David Lawrence's avatar
David Lawrence committed
139
        }
140

141
        next if $type eq "X" or $type eq "BAT";
142 143 144 145 146

        $before_copyright = "";
        $c_comment = 0;
        $shell_comment = 0;
        $m4_comment = 0;
Bob Halley's avatar
Bob Halley committed
147
        $sgml_comment = 0;
148 149 150 151 152
        $zone_comment = 0;
        $man_comment = 0;
        $start_comment = "";
        $end_comment = "";
        $first = "";
153
        if ($type =~ /^(C|YACC|CONF-C)$/) {
154 155 156 157
                $c_comment = 1;
                $start_comment = "/*\n";
                $prefix = " * ";
                $end_comment = " */\n";
Rob Austein's avatar
Rob Austein committed
158
        } elsif ($type =~ /^(SH|PERL|TCL|MAKE|CONF-SH|RNC)$/) {
159 160
                $shell_comment = 1;
                $prefix = "# ";
Mark Andrews's avatar
Mark Andrews committed
161
        } elsif ($type eq "ZONE" || $type eq "MC") {
162 163 164 165 166 167 168 169
                $zone_comment = 1;
                $prefix = "; ";
        } elsif ($type eq "MAN") {
                $man_comment = 1;
                $prefix = ".\\\" ";
        } elsif ($type eq "M4") {
                $m4_comment = 1;
                $prefix = "dnl ";
Bob Halley's avatar
Bob Halley committed
170 171
        } elsif ($type eq "HTML" || $type eq "SGML") {
                $sgml_comment = 1;
172 173 174 175 176
                $start_comment = "<!--\n";
                $prefix = " - ";
                $end_comment = "-->\n";
        } elsif ($type eq "TXT") {
                $prefix = "";
David Lawrence's avatar
David Lawrence committed
177
        } else {
178 179
                print "$file: type '$type' not supported yet; skipping\n";
                next;
David Lawrence's avatar
David Lawrence committed
180
        }
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

        ($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>) {
242 243
                        if ($_ !~ /^\Q$nonspaceprefix\E/ ||
                            $_ =~ /$keyword_pat/) {
244 245 246 247
                                $first = $_;
                                last;
                        }
                }
Bob Halley's avatar
Bob Halley committed
248
        } elsif ($sgml_comment) {
Mark Andrews's avatar
Mark Andrews committed
249
		$before_copyright = "";
Mark Andrews's avatar
Mark Andrews committed
250
                while (/^<!DOCTYPE/ || /^<\?xml-stylesheet/ || /^<\?xml /) {
Mark Andrews's avatar
update  
Mark Andrews committed
251
			# print "SGML: $_";
Mark Andrews's avatar
Mark Andrews committed
252
			$before_copyright = "$before_copyright$_";
Mark Andrews's avatar
update  
Mark Andrews committed
253
			if (/>$/ ) {
Mark Andrews's avatar
Mark Andrews committed
254 255 256 257
				$_ = <SOURCE>;
				close(SOURCE) if (eof(SOURCE));
				next;
			}
Mark Andrews's avatar
update  
Mark Andrews committed
258
			$_ = <SOURCE>;
Mark Andrews's avatar
Mark Andrews committed
259
			while (!eof(SOURCE) && ! /^<!/ ) {
260 261 262
				$before_copyright = "$before_copyright$_";
				$_ = <SOURCE>;
			}
Mark Andrews's avatar
Mark Andrews committed
263 264 265 266
			if (eof(SOURCE)) {
				close(SOURCE);
				next;
			}
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
                }
                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
Mark Andrews's avatar
Mark Andrews committed
295
                                  /[Cc]opyright/ ||
296 297 298 299 300 301 302
                                  /See COPYRIGHT in the source root/ ||
                                  /Permission to use, copy, modify, and / ||
                                  /THE SOFTWARE IS PROVIDED "AS IS" AND /;
                        }
                        $/ = "\n";
                }
                $first = $_;
David Lawrence's avatar
David Lawrence committed
303
        } else {
304
                $first = $_;
David Lawrence's avatar
David Lawrence committed
305 306
        }

307
        $first = "" if ! defined($first);
308

309 310 311
        open(TARGET, ">$file.new") || die "can't open $file.new: $!";
        print TARGET $before_copyright if $before_copyright;
        print TARGET $start_comment if $start_comment;
312

Mark Andrews's avatar
Mark Andrews committed
313 314 315 316 317 318 319
        $sysyears = "";
        $sftyears = "";
        $nomyears = "";

	#
	# Internet Software Consortium: up to 2003
	#
320 321
        $last_year = 0;
        $anchor_year = 0;
Mark Andrews's avatar
Mark Andrews committed
322 323 324 325 326 327 328 329 330 331 332
	$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";
333 334 335 336
                        #if ($anchor_year != 0) {
                        #        print "$file: noncontiguous year: ",
                        #              "$year != $last_year + 1\n";
                        #}
Mark Andrews's avatar
Mark Andrews committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
                        $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";
361 362 363 364
                        #if ($anchor_year != 0) {
                        #        print "$file: noncontiguous year: ",
                        #              "$year != $last_year + 1\n";
                        #}
Mark Andrews's avatar
Mark Andrews committed
365 366 367 368 369 370 371 372 373 374 375
                        $anchor_year = $year;
                        $anchor_end = length($years);
                }

                $last_year = $year;
        }
	$nomyears = $years;

	#
	# Internet Systems Consortium: 2004 onwards.
	#
Mark Andrews's avatar
Mark Andrews committed
376 377 378
        $last_year = 0;
        $anchor_year = 0;
	$years = "";
Mark Andrews's avatar
Mark Andrews committed
379
	$anchor_end = length($years);
380
	my $andor = 0;
381
        foreach $year (@years) {
Mark Andrews's avatar
Mark Andrews committed
382
		if ($year < 2004) { next; }
383
		$andor = 1 if ($year >= 2007);
384 385 386 387 388 389 390 391
                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";
392 393 394 395
                        #if ($anchor_year != 0) {
                        #        print "$file: noncontiguous year: ",
                        #              "$year != $last_year + 1\n";
                        #}
396 397 398
                        $anchor_year = $year;
                        $anchor_end = length($years);
                }
399

400 401
                $last_year = $year;
        }
Mark Andrews's avatar
Mark Andrews committed
402
	$sysyears = $years;
403

Mark Andrews's avatar
Mark Andrews committed
404
        ($firstline, $secondline, @otherlines) = @$textp;
405

Mark Andrews's avatar
Mark Andrews committed
406 407
        $firstline =~ s/\@SYSYEARS\@/$sysyears/;
        $secondline =~ s/\@SFTYEARS\@/$sftyears/;
408 409

        print TARGET "$prefix$firstline";
Mark Andrews's avatar
Mark Andrews committed
410 411 412 413
	if ($sftyears ne "" ) {
		print TARGET $secondline =~ /^$/ ? $nonspaceprefix : $prefix;
		print TARGET "$secondline";
	}
414 415

        foreach $_ (@otherlines) {
416
		s:modify, and distribute:modify, and/or distribute: if ($andor);
417
                print TARGET (/^$/ ? $nonspaceprefix : $prefix);
Mark Andrews's avatar
Mark Andrews committed
418
		s/\@NOMYEARS\@/$nomyears/;
419 420 421 422 423 424
                print TARGET "$_";
        }
        print TARGET $end_comment if $end_comment;

        if ($first eq "") {
                $first = <SOURCE>;
425
        }
426

427
        if (defined($first)) {
428 429 430 431 432
          	if ($type eq 'MAN') {
                	print TARGET "$nonspaceprefix\n";
		} else {
	                print TARGET "\n";
                }
433

Mark Andrews's avatar
Mark Andrews committed
434
		if ($type eq "C" && $sysyears =~ /2009/) {
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
			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";
		}
454

Mark Andrews's avatar
Mark Andrews committed
455 456 457 458 459
		if ($type eq 'SGML' && m:<articleinfo>.*?</articleinfo>:s) {
			# print "docinfo: $file\n";
			my $r = copyrights(@years);
			s:<articleinfo>.*?</articleinfo>:<articleinfo>\n$r  </articleinfo>:s;
		}
460
		if ($type eq 'SGML' && m:<docinfo>.*?</docinfo>:s) {
Mark Andrews's avatar
Mark Andrews committed
461
			# print "docinfo: $file\n";
462
			my $r = copyrights(@years);
Mark Andrews's avatar
Mark Andrews committed
463
			s:<docinfo>.*?</docinfo>:<docinfo>\n$r  </docinfo>:s;
464 465
		}
		if ($type eq 'SGML' && m:<bookinfo>.*?</bookinfo>:s) {
Mark Andrews's avatar
Mark Andrews committed
466
			# print "bookinfo: $file\n";
467 468 469 470
			my $r = copyrights(@years);
			s:<bookinfo>.*?</bookinfo>:<bookinfo>\n$r  </bookinfo>:s;
		}

471 472 473 474 475 476 477 478 479 480 481 482
                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";
                }

483
                if ($first !~ /$keyword_pat/ && $_ !~ /$keyword_pat/) {
484
			$end = "\n$nonspaceprefix" if ($type eq "MAN");
485
                        print TARGET "$start\$";
486
                        print TARGET "Id";
487 488
                        print TARGET "\$$end\n";
                }
489

490 491
                print TARGET $first if $first !~ /^\s*$/;
                print TARGET $_;
492
        }
493 494
        close(TARGET);
        close(SOURCE);
495

496 497 498 499 500 501 502 503 504
        $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): $!";
        }
Bob Halley's avatar
Bob Halley committed
505
}