xref: /titanic_51/usr/src/lib/libc/extract-copyright.pl (revision bee7008e5bcfda6119afed76f9d71de946a8c708)
16b5e5868SGarrett D'Amore#! /usr/perl5/bin/perl
26b5e5868SGarrett D'Amore#
36b5e5868SGarrett D'Amore# This file and its contents are supplied under the terms of the
46b5e5868SGarrett D'Amore# Common Development and Distribution License ("CDDL"), version 1.0.
56b5e5868SGarrett D'Amore# You may only use this file in accordance with the terms version
66b5e5868SGarrett D'Amore# 1.0 of the CDDL.
76b5e5868SGarrett D'Amore#
86b5e5868SGarrett D'Amore# A full copy of the text of the CDDL should have accompanied this
96b5e5868SGarrett D'Amore# source.  A copy is of the CDDL is also available via the Internet
106b5e5868SGarrett D'Amore# at http://www.illumos.org/license/CDDL.
116b5e5868SGarrett D'Amore#
126b5e5868SGarrett D'Amore
136b5e5868SGarrett D'Amore#
146b5e5868SGarrett D'Amore# Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
156b5e5868SGarrett D'Amore#
166b5e5868SGarrett D'Amore
176b5e5868SGarrett D'Amore#
186b5e5868SGarrett D'Amore# This extracts all the BSD copyrights (excluding the CDDL licenses)
196b5e5868SGarrett D'Amore# for use in a THIRDPARTYLICENSE file.  It tries hard to avoid duplicates.
206b5e5868SGarrett D'Amore#
216b5e5868SGarrett D'Amore
226b5e5868SGarrett D'Amoreuse strict;
236b5e5868SGarrett D'Amoreuse warnings;
246b5e5868SGarrett D'Amoreuse File::Find;
256b5e5868SGarrett D'Amore
266b5e5868SGarrett D'Amoremy %LICENSE = ();
276b5e5868SGarrett D'Amore
286b5e5868SGarrett D'Amoresub dofile
296b5e5868SGarrett D'Amore{
306b5e5868SGarrett D'Amore	my $file = shift;
316b5e5868SGarrett D'Amore	my $comment = 0;
326b5e5868SGarrett D'Amore	my @license = ();
336b5e5868SGarrett D'Amore	my @block = ();;
346b5e5868SGarrett D'Amore	my $copyr = 0;
356b5e5868SGarrett D'Amore	open(FILE, $file);
366b5e5868SGarrett D'Amore	while (<FILE>) {
376b5e5868SGarrett D'Amore		if (/^\/\*$/) {
386b5e5868SGarrett D'Amore			$comment = 1;
396b5e5868SGarrett D'Amore			$copyr = 0;
406b5e5868SGarrett D'Amore			@block = ();
416b5e5868SGarrett D'Amore			next;
426b5e5868SGarrett D'Amore		}
436b5e5868SGarrett D'Amore		if (!$comment) {
446b5e5868SGarrett D'Amore			next;
456b5e5868SGarrett D'Amore		}
466b5e5868SGarrett D'Amore		#
476b5e5868SGarrett D'Amore		# We don't want to know about CDDL files.  They don't
486b5e5868SGarrett D'Amore		# require an explicit THIRDPARTYLICENSE file.
496b5e5868SGarrett D'Amore		#
506b5e5868SGarrett D'Amore		if (/CDDL/) {
516b5e5868SGarrett D'Amore			#print "$file is CDDL.\n";
526b5e5868SGarrett D'Amore			close(FILE);
536b5e5868SGarrett D'Amore			return;
546b5e5868SGarrett D'Amore		}
556b5e5868SGarrett D'Amore		if (/Copyright/) {
566b5e5868SGarrett D'Amore			$copyr = 1;
576b5e5868SGarrett D'Amore		}
586b5e5868SGarrett D'Amore		if (!/^ \*\//) {
596b5e5868SGarrett D'Amore			push(@block, $_);
606b5e5868SGarrett D'Amore			next;
616b5e5868SGarrett D'Amore		}
626b5e5868SGarrett D'Amore		#
636b5e5868SGarrett D'Amore		# We have reached the end of the comment now.
646b5e5868SGarrett D'Amore		#
656b5e5868SGarrett D'Amore		$comment = 0;
666b5e5868SGarrett D'Amore
676b5e5868SGarrett D'Amore		# Check to see if we saw a copyright.
686b5e5868SGarrett D'Amore		if (!$copyr) {
696b5e5868SGarrett D'Amore			next;
706b5e5868SGarrett D'Amore		}
716b5e5868SGarrett D'Amore		my $line;
726b5e5868SGarrett D'Amore		foreach $line (@block) {
736b5e5868SGarrett D'Amore			chomp $line;
746b5e5868SGarrett D'Amore			$line =~ s/^ \* //;
756b5e5868SGarrett D'Amore			$line =~ s/^ \*//;
766b5e5868SGarrett D'Amore			$line =~ s/^ \*$//;
776b5e5868SGarrett D'Amore			push(@license, $line);
786b5e5868SGarrett D'Amore		}
796b5e5868SGarrett D'Amore	}
806b5e5868SGarrett D'Amore
816b5e5868SGarrett D'Amore	if ($#license > 0)  {
826b5e5868SGarrett D'Amore		my $lic = join "\n", @license;
836b5e5868SGarrett D'Amore		push (@{$LICENSE{$lic}}, $file);
846b5e5868SGarrett D'Amore	}
856b5e5868SGarrett D'Amore
866b5e5868SGarrett D'Amore	close(FILE);
876b5e5868SGarrett D'Amore}
886b5e5868SGarrett D'Amore
896b5e5868SGarrett D'Amoremy @FILES;
906b5e5868SGarrett D'Amore
916b5e5868SGarrett D'Amoresub wanted {
926b5e5868SGarrett D'Amore	my $path = $File::Find::name;
936b5e5868SGarrett D'Amore
946b5e5868SGarrett D'Amore	if (!-f $path) {
956b5e5868SGarrett D'Amore		if ($path =~ /\.[chs]$/) {
966b5e5868SGarrett D'Amore			push(@FILES, $path);
976b5e5868SGarrett D'Amore		}
986b5e5868SGarrett D'Amore	}
996b5e5868SGarrett D'Amore
1006b5e5868SGarrett D'Amore}
1016b5e5868SGarrett D'Amoreforeach $a (@ARGV) {
1026b5e5868SGarrett D'Amore    	if (-d $a) {
1036b5e5868SGarrett D'Amore		find(\&wanted, $a);
1046b5e5868SGarrett D'Amore	} elsif (-f $a) {
1056b5e5868SGarrett D'Amore		push(@FILES, $a);
1066b5e5868SGarrett D'Amore	}
1076b5e5868SGarrett D'Amore}
1086b5e5868SGarrett D'Amore
1096b5e5868SGarrett D'Amoreforeach $a (@FILES) {
1106b5e5868SGarrett D'Amore	dofile($a);
1116b5e5868SGarrett D'Amore}
1126b5e5868SGarrett D'Amore
113*bee7008eSMarcel Telkaforeach my $lic (sort keys %LICENSE) {
1146b5e5868SGarrett D'Amore	my @files = @{$LICENSE{$lic}};
1156b5e5868SGarrett D'Amore	print "\nThe following files from the C library:\n";
1166b5e5868SGarrett D'Amore	foreach my $f (@files) {
1176b5e5868SGarrett D'Amore		print("    $f\n");
1186b5e5868SGarrett D'Amore	}
1196b5e5868SGarrett D'Amore	print "are provided under the following terms:\n\n";
1206b5e5868SGarrett D'Amore	print "$lic\n";
1216b5e5868SGarrett D'Amore}
122