Home | History | Annotate | Line # | Download | only in common
extract-contrib-string.pl revision 1.7
      1 #!/usr/bin/env perl
      2 #
      3 # Copyright (c) 2004, 2008 The NetBSD Foundation, Inc.
      4 # All rights reserved.
      5 #
      6 # This code is derived from software contributed to The NetBSD Foundation
      7 # by Hubert Feyrer <hubert (at] feyrer.de>.
      8 # 
      9 # Redistribution and use in source and binary forms, with or without
     10 # modification, are permitted provided that the following conditions
     11 # are met:
     12 # 1. Redistributions of source code must retain the above copyright
     13 #    notice, this list of conditions and the following disclaimer.
     14 # 2. Redistributions in binary form must reproduce the above copyright
     15 #    notice, this list of conditions and the following disclaimer in the
     16 #    documentation and/or other materials provided with the distribution.
     17 # 
     18 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28 # POSSIBILITY OF SUCH DAMAGE.
     29 
     30 #
     31 # Extract BSD-mandated copyright messages for NetBSD documentation
     32 #
     33 # Usage:
     34 # 1) find /usr/src -type f -print \
     35 #    | perl extract-contrib-string.pl
     36 #    >x
     37 #
     38 # 2) merge text after "--------" in "x" into
     39 #    src/distrib/notes/common/legal.common
     40 #
     41 # Options:
     42 #
     43 #     perl extract-contrib-string.pl [-d] [-h]
     44 #
     45 # where
     46 #     -d  debug output
     47 #     -h  html output
     48 
     49 
     50 $ack_line1="[aA]ll( commercial)?( marketing or)? advertising materials mentioning( features)?";
     51 $ack_line2="display the following( acknowledge?ment)?";
     52 $ack_endline=
     53       '(\d\.\s*(Neither the name'
     54     .         '|The name of the company nor the name'	# Wasn't my idea
     55     .         '|The name of the author may not'
     56     .         '|The name of .* must not be used to endorse'
     57     .         '|The names? (of )?.* nor the names? of'
     58     .         '|The names? (of )?.* or any of it\'?s members'
     59     .         '|Redistributions of any form whatsoever'
     60     .         '|The names .*"OpenSSL Toolkit.*" and .*"OpenSSL Project.*" must not be used))'
     61     .'|(THIS SOFTWARE IS PROVIDED)'
     62     .'|(The word \'cryptographic\' can be left out if)'
     63     .'|(may be used to endorse)'
     64     .'|(@end cartouche)'
     65     .'|(Redistribution and use in source and binary forms)'
     66     .'|(may not be used to endorse)'
     67     .'|(\.IP 4)'
     68     .'|(ALLOW FREE USE OF)'
     69     .'|(materials provided with the distribution)'
     70     .'|(@InsertRedistribution@)';
     71 
     72 $known_bad_clause_3_wording=
     73       'usr.bin/lex/.*'				# UCB
     74     .'|usr.sbin/hilinfo/hilinfo.c'	   	# CSS @ Utah
     75     ;	
     76 
     77 sub warning {
     78     local($fn,$msg) = @_;
     79     print "XXX $fn line $.: $msg\n"
     80 }
     81 
     82 while ($#ARGV >= 0) {
     83     $debug=1 if ($ARGV[0] =~ /-d/i);
     84     $html=1  if ($ARGV[0] =~ /-h/i);
     85     $xml=1  if ($ARGV[0] =~ /-x/i);
     86     shift(@ARGV);
     87 }
     88 
     89 $comments = !$html && !$xml;
     90 
     91 file:
     92 while(<>) {
     93     chomp();
     94     $fn=$_;
     95     
     96     open(F, "$fn") || die "cannot read $fn: $!\n";
     97 
     98   line:
     99     while(<F>) {
    100 	if (0 and /$ack_line2/i){
    101 	    print "?> $_" if $debug;
    102 	    
    103 	    if ($fn !~ m,$known_bad_clause_3_wording,) {
    104 		warning($fn, "clause 3 start not caught");
    105 	    }
    106 	    last line;
    107 	}
    108 	
    109 	print "0> $_" if $debug;
    110 
    111 	if (/$ack_line1/i
    112 	    or (/$ack_line2/ and $fn =~ m,$known_bad_clause_3_wording,)) {
    113 	    
    114 	    print "1> $_" if $debug;
    115 
    116 	    $_=<F>
    117 		unless $fn =~ m,$known_bad_clause_3_wording,;
    118 	    if (/$ack_line2/i or $fn =~ m,$known_bad_clause_3_wording,){
    119 		
    120 		print "2> $_" if $debug;
    121 		
    122 		$msg="";
    123 		$cnt=0;
    124 		$_=<F>;
    125 		while(!/$ack_endline/i) {
    126 		    
    127 		    print "C> $_" if $debug;
    128 
    129 		    $msg .= $_;
    130 		    $cnt++;
    131 		    $_ = <F>;
    132 		    if ($cnt > 10) {
    133 			warning($fn,"loooong copyright?");
    134 			last line;
    135 		    }
    136 		}
    137 
    138 		print "E> $_" if $debug;
    139 		
    140 		# post-process
    141 		$msg =~ s/^\@c\s*//g;			# texinfo
    142 		$msg =~ s/\n\@c\s*/\n/g;		# texinfo
    143 		$msg =~ s/^REM\s*//g;			# BASIC?!?
    144 		$msg =~ s/\nREM\s*/\n/g;		# BASIC?!?
    145 		$msg =~ s/^dnl\s*//g;			# m4
    146 		$msg =~ s/\dnl\s*/\n/g;			# m4
    147 		$msg =~ s/^\.\\"\s*//g;			# *roff
    148 		$msg =~ s/\n\.\\"\s*/\n/g;		# *roff
    149 		$msg =~ s/^[#\\\|";]*\s*//g;		# sh etc.
    150 		$msg =~ s/\n[#\\\|";]\s*/\n/g;		# sh etc.
    151 		$msg =~ s/^[ 	*]*//g;      		# C
    152 		$msg =~ s/\n[ 	*]*/\n/g;    		# C
    153 
    154 		# C++/C99
    155 		while ($msg =~ /^\s*\/\/\s*/) {
    156 			$msg =~ s/^\s*\/\/\s*//o;
    157 		}
    158 		while ($msg =~ /\ns*\/\/\s*$/) {
    159 			$msg =~ s/\ns*\/\/\s*$//o;
    160 		}
    161 		$msg =~ s/\ns*\/\/\s*/ /g;
    162 
    163 		$msg =~ s/\@cartouche\n//;              # texinfo
    164 
    165 		$msg =~ s/
//g;
    167 		$msg =~ s/\s*\n/\n/g;
    168 		$msg =~ s/^\s*//;
    169 		$msg =~ s/\\\@/\@/g;
    170 		$msg =~ s/\n\n/\n/g;
    171 	        $msg =~ s/^\s*"//;
    172 	        $msg =~ s/"\s*$//;
    173 	        $msg =~ s/^\s*``//;
    174 	        $msg =~ s/''\s*$//;
    175 		while ($msg =~ /[\n\s]+$/) {
    176 			$msg =~ s/[\n\s]+$//o;
    177 		}
    178 
    179 		# Split up into separate paragraphs
    180 		#
    181 		$msgs=$msg;
    182 		$msgs=~s/(This (software|product))/|$1/g;
    183 		$msgs=~s,^\|,,;
    184 	      msg:
    185 		foreach $msg (split(/\|/, $msgs)) {
    186 		    if ($comments) {
    187 			print ".\\\" File $fn:\n";
    188 			print "$msg";
    189 			print "\n\n";
    190 		    }
    191 		    
    192 		    # Figure out if there's a version w/ or w/o trailing dot
    193 		    # 
    194 		    if ($msg =~ /\.\n$/) {
    195 			# check if there's a version of the same msg
    196 			# w/ a trailing dot
    197 			$msg2=$msg;
    198 			$msg2=~s,\.\n$,\n,;
    199 			if ($copyrights{"$msg2"}) {
    200 			    # already there - skip
    201 			    print "already there, w/o dot - skipping!\n"
    202 				if $debug;
    203 			    next msg;
    204 			}
    205 			
    206 			# ... maybe with other case?
    207 			$lc_msg2=lc($msg2);
    208 			if ($lc_copyrights{$lc_msg2}) {
    209 			    print "already there, in different case - skipping\n"
    210 				if $debug;
    211 			    next msg;
    212 			}
    213 		    } else {
    214 			# check if there's a version of the same msg
    215 			# w/o the trailing dot
    216 			$msg2=$msg;
    217 			chomp($msg2);
    218 			$msg2.=".\n";
    219 			if ($copyrights{"$msg2"}) {
    220 			    # already there - skip
    221 			    print "already there, w/ dot - skipping!\n"
    222 				if $debug;
    223 			    next msg;
    224 			}
    225 			
    226 			# ... maybe with other case?
    227 			$lc_msg2=lc($msg2);
    228 			if ($lc_copyrights{$lc_msg2}) {
    229 			    print "already there, in different case - skipping\n"
    230 				if $debug;
    231 			    next msg;
    232 			}
    233 		    }
    234 
    235 		    $copyrights{$msg} = 1;
    236 		    $lc_copyrights{$lc_msg} = 1;
    237 		}		 
    238 
    239 	    } else {
    240 		print "?> $_" if $debug;
    241 
    242                 if ($fn !~ m,$known_bad_clause_3_wording,) {
    243 		    warning($fn, "bad clause 3?");
    244                 }
    245 		last line;
    246 	    }
    247 	}
    248     }
    249     close(F);
    250 }
    251 
    252 
    253 if ($html) {
    254     print "<ul>\n";
    255     foreach $msg (sort keys %copyrights) {
    256 	print "<li>$msg</li>\n";
    257     }
    258     print "</ul>\n";
    259 } elsif ($xml) {
    260     foreach $msg (sort keys %copyrights) {
    261 	print "<listitem>$msg</listitem>\n";
    262     }
    263 } else {
    264     print "------------------------------------------------------------\n";
    265 
    266     $firsttime=1;
    267     foreach $msg (sort keys %copyrights) {
    268 	if ($firsttime) {
    269 	    $firsttime=0;
    270 	} else {
    271 	    print ".It\n";
    272 	}
    273 	print "$msg\n";
    274     }
    275 }
    276