1 #! /usr/perl5/bin/perl
   2 #
   3 # This file and its contents are supplied under the terms of the
   4 # Common Development and Distribution License ("CDDL"), version 1.0.
   5 # You may only use this file in accordance with the terms version
   6 # 1.0 of the CDDL.
   7 #
   8 # A full copy of the text of the CDDL should have accompanied this
   9 # source.  A copy is of the CDDL is also available via the Internet
  10 # at http://www.illumos.org/license/CDDL.
  11 #
  12 
  13 #
  14 # Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
  15 # Copyright 2016 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  16 #
  17 
  18 #
  19 # This extracts all the BSD copyrights (excluding the CDDL licenses)
  20 # for use in a THIRDPARTYLICENSE file.  It tries hard to avoid duplicates.
  21 #
  22 
  23 use strict;
  24 use warnings;
  25 use File::Find;
  26 
  27 my %LICENSE = ();
  28 
  29 sub dofile
  30 {
  31         my $file = shift;
  32         my $comment = 0;
  33         my @license = ();
  34         my @block = ();;
  35         my $copyr = 0;
  36         open(FILE, $file);
  37         while (<FILE>) {
  38                 if (/^\/\*$/) {
  39                         $comment = 1;
  40                         $copyr = 0;
  41                         @block = ();
  42                         next;
  43                 }
  44                 if (!$comment) {
  45                         next;
  46                 }
  47                 #
  48                 # We don't want to know about CDDL files.  They don't
  49                 # require an explicit THIRDPARTYLICENSE file.
  50                 #
  51                 if (/CDDL/) {
  52                         #print "$file is CDDL.\n";
  53                         close(FILE);
  54                         return;
  55                 }
  56                 if (/Copyright/) {
  57                         $copyr = 1;
  58                 }
  59                 if (!/^ \*\//) {
  60                         push(@block, $_);
  61                         next;
  62                 }
  63                 #
  64                 # We have reached the end of the comment now.
  65                 #
  66                 $comment = 0;
  67 
  68                 # Check to see if we saw a copyright.
  69                 if (!$copyr) {
  70                         next;
  71                 }
  72                 my $line;
  73                 foreach $line (@block) {
  74                         chomp $line;
  75                         $line =~ s/^ \* //;
  76                         $line =~ s/^ \*//;
  77                         $line =~ s/^ \*$//;
  78                         push(@license, $line);
  79                 }
  80         }
  81 
  82         if ($#license > 0)  {
  83                 my $lic = join "\n", @license;
  84                 push (@{$LICENSE{$lic}}, $file);
  85         }
  86 
  87         close(FILE);
  88 }
  89 
  90 my @FILES;
  91 
  92 sub wanted {
  93         my $path = $File::Find::name;
  94 
  95         if (!-f $path) {
  96                 if ($path =~ /\.[chs]$/) {
  97                         push(@FILES, $path);
  98                 }
  99         }
 100         
 101 }
 102 foreach $a (@ARGV) {
 103         if (-d $a) {
 104                 find(\&wanted, $a);
 105         } elsif (-f $a) {
 106                 push(@FILES, $a);
 107         }
 108 }
 109 
 110 # sort files to get a stable ordering to aid wsdiff(1onbld)
 111 @FILES = sort @FILES;
 112 
 113 foreach $a (@FILES) {
 114         dofile($a);
 115 }
 116 
 117 foreach my $lic (keys %LICENSE) {
 118         my @files = @{$LICENSE{$lic}};
 119         print "\nThe following files from the C library:\n";
 120         foreach my $f (@files) {
 121                 print("    $f\n");
 122         }
 123         print "are provided under the following terms:\n\n";
 124         print "$lic\n";
 125 }