Home | History | Annotate | Line # | Download | only in common
extract-contrib-string.pl revision 1.11
      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] [-x] [-?]
     44 #
     45 # where
     46 #     -d  debug output
     47 #     -h  html output
     48 #     -x  xml/docbook output
     49 #     -?  display help/usage message
     50 
     51 
     52 $ack_line1="[aA]ll( commercial)?( marketing or)? advertising materials mentioning( features)?";
     53 $ack_line2="display the following( acknowledge?ment)?";
     54 $ack_endline=
     55       '(\d\.\s*(Neither the name'
     56     .         '|The name of the company nor the name'	# Wasn't my idea
     57     .         '|The name of the author may not'
     58     .         '|The name of .* must not be used to endorse'
     59     .         '|The names? (of )?.* nor the names? of'
     60     .         '|The names? (of )?.* or any of it\'?s members'
     61     .         '|Redistributions of any form whatsoever'
     62     .         '|The names .*"OpenSSL Toolkit.*" and .*"OpenSSL Project.*" must not be used))'
     63     .'|(THIS SOFTWARE IS PROVIDED)'
     64     .'|(The word \'cryptographic\' can be left out if)'
     65     .'|(may be used to endorse)'
     66     .'|(@end cartouche)'
     67     .'|(Redistribution and use in source and binary forms)'
     68     .'|(may not be used to endorse)'
     69     .'|(\.IP 4)'
     70     .'|(ALLOW FREE USE OF)'
     71     .'|(materials provided with the distribution)'
     72     .'|(@InsertRedistribution@)';
     73 
     74 $known_bad_clause_3_wording=
     75       'usr.bin/lex/.*'				# UCB
     76     .'|dist/bind/contrib/nslint-2.1a3/lbl/.*'	#
     77     .'|usr.sbin/hilinfo/hilinfo.c'	   	# CSS @ Utah
     78     ;	
     79 
     80 sub warning {
     81     local($fn,$msg) = @_;
     82     print "XXX $fn line $.: $msg\n"
     83 }
     84 
     85 while ($#ARGV >= 0) {
     86     $debug=1 if ($ARGV[0] =~ /-d/i);
     87     $html=1  if ($ARGV[0] =~ /-h/i);
     88     $xml=1  if ($ARGV[0] =~ /-x/i);
     89     $usage=1  if ($ARGV[0] =~ /-\?/);
     90     shift(@ARGV);
     91 }
     92 
     93 if ($usage) {
     94     print "usage: find /usr/src -type f -print |\n" .
     95 	" perl extract-contrib-string.pl [-h] [-x] [-?] [-d]\n" .
     96 	"   where\n" .
     97 	"    -h   output html\n" .
     98 	"    -x   output xml/docbook\n" .
     99 	"    -d   debug\n" .
    100 	"    -?   display this help message\n";
    101     exit(0);
    102 }
    103 
    104 $comments = !$html && !$xml;
    105 
    106 file:
    107 while(<>) {
    108     chomp();
    109     $fn=$_;
    110     
    111     open(F, "$fn") || die "cannot read $fn: $!\n";
    112 
    113   line:
    114     while(<F>) {
    115 	if (0 and /$ack_line2/i){
    116 	    print "?> $_" if $debug;
    117 	    
    118 	    if ($fn !~ m,$known_bad_clause_3_wording,) {
    119 		warning($fn, "clause 3 start not caught");
    120 	    }
    121 	    last line;
    122 	}
    123 	
    124 	print "0> $_" if $debug;
    125 
    126 	# special case perl script generating a license (openssl's
    127 	# mkerr.pl) - ignore the quoted license, there is another one
    128 	# inside:
    129 	if (/^\"\s\*.*$ack_line1.*\\n\"\,/) {
    130 		while(!/$ack_endline/i) {
    131 		    print "S> $_" if $debug;
    132 		    $_ = <F>;
    133 		}
    134 	}
    135 
    136 	if (/$ack_line1/i
    137 	    or (/$ack_line2/ and $fn =~ m,$known_bad_clause_3_wording,)) {
    138 	    
    139 	    print "1> $_" if $debug;
    140 
    141 	    $_=<F>
    142 		unless $fn =~ m,$known_bad_clause_3_wording,;
    143 	    if (/$ack_line2/i or $fn =~ m,$known_bad_clause_3_wording,){
    144 		
    145 		print "2> $_" if $debug;
    146 		
    147 		$msg="";
    148 		$cnt=0;
    149 		$_=<F>;
    150 		while(!/$ack_endline/i) {
    151 		    
    152 		    print "C> $_" if $debug;
    153 
    154 		    $msg .= $_;
    155 		    $cnt++;
    156 		    $_ = <F>;
    157 		    if ($cnt > 10) {
    158 			warning($fn,"loooong copyright?");
    159 			last line;
    160 		    }
    161 		}
    162 
    163 		print "E> $_" if $debug;
    164 		
    165 		# post-process
    166 
    167 		if ($fn =~ m,$known_bad_clause_3_wording,) {
    168 			while ($msg !~ /^ .*``.*\n/) {
    169 				$msg =~ s/^.*\n//o;
    170 			}
    171 			$msg =~ s/\n.*``//o;
    172 			$msg =~ s/''.*$//o;
    173 		}
    174 
    175 		# *roff
    176 		while ($msg =~ /^\.\\"\s*/) {
    177 			$msg =~ s/^\.\\"\s*//o;
    178 		}
    179 		while ($msg =~ /\n\.\\"\s*/) {
    180 			$msg =~ s/\n\.\\"\s*/\n/o;
    181 		}
    182 		$msg =~ s/\n\.\\"\s*$/\n/g;
    183 
    184 		# C++/C99
    185 		while ($msg =~ /^\s*\/\/\s*/) {
    186 			$msg =~ s/^\s*\/\/\s*//o;
    187 		}
    188 		while ($msg =~ /\n\s*\/\/\s*$/) {
    189 			$msg =~ s/\n\s*\/\/\s*$//o;
    190 		}
    191 		$msg =~ s/\n\s*\/\/\s*/\n/g;
    192 
    193 		# C
    194 		while ($msg =~ /^\s*\*\s*/) {
    195 			$msg =~ s/^\s*\*\s*//o;
    196 		}
    197 		while ($msg =~ /\n\s*\*\s*$/) {
    198 			$msg =~ s/\n\s*\*\s*$//o;
    199 		}
    200 		$msg =~ s/\n\s*\*\s*/\n/g;
    201 
    202 		# texinfo @c
    203 		while ($msg =~ /^\s*\@c\s+/) {
    204 			$msg =~ s/^\s*\@c\s+//o;
    205 		}
    206 		while ($msg =~ /\n\s*\@c\s+$/) {
    207 			$msg =~ s/\n\s*\@c\s+$//o;
    208 		}
    209 		$msg =~ s/\n\s*\@c\s+/\n/g;
    210 
    211 		$msg =~ s/^REM\s*//g;			# BASIC?!?
    212 		$msg =~ s/\nREM\s*/\n/g;		# BASIC?!?
    213 		$msg =~ s/^dnl\s*//g;			# m4
    214 		$msg =~ s/\dnl\s*/\n/g;			# m4
    215 		$msg =~ s/^\s+-\s+//g;			# seen in docbook files
    216 		$msg =~ s/\n\s+-\s+/ /g;		#
    217 		$msg =~ s/^[#\\\|";]*\s*//g;		# sh etc.
    218 		$msg =~ s/\n[#\\\|";]\s*/\n/g;		# sh etc.
    219 		$msg =~ s/^[ 	*]*//g;      		# C
    220 		$msg =~ s/\n[ 	*]*/\n/g;    		# C
    221 
    222 		$msg =~ s/\@cartouche\n//;              # texinfo
    223 
    224 		$msg =~ s/
//g;
    226 		$msg =~ s/\s*\n/\n/g;
    227 		$msg =~ s/^\s*//;
    228 		$msg =~ s/\\\@/\@/g;
    229 		$msg =~ s/\n\n/\n/g;
    230 	        $msg =~ s/^\s*``//;
    231 	        $msg =~ s/''\s*$//;
    232 		$msg =~ s/^\"//o;
    233 		$msg =~ s/\"$//o;
    234 
    235 		# Split up into separate paragraphs
    236 		#
    237 		$msgs=$msg;
    238 		$msgs=~s/(This (software|product))/|$1/g;
    239 		$msgs=~s,^\|,,;
    240 	      msg:
    241 		foreach $msg (split(/\|/, $msgs)) {
    242 		    while ($msg =~ /[\n\s]+$/) {
    243 			$msg =~ s/[\n\s]+$//o;
    244 		    }
    245 		    next if ($msg eq "");
    246 		    if ($comments) {
    247 			print ".\\\" File $fn:\n";
    248 			print "$msg";
    249 			print "\n\n";
    250 		    }
    251 		    
    252 		    # Figure out if there's a version w/ or w/o trailing dot
    253 		    # 
    254 		    if ($msg =~ /\.$/) {
    255 			# check if there's a version of the same msg
    256 			# w/o a trailing dot
    257 			$msg2=$msg;
    258 			$msg2=~s,\.$,,;
    259 			if ($copyrights{"$msg2"}) {
    260 			    # already there - skip
    261 			    print "already there, w/o dot - skipping!\n"
    262 				if $debug;
    263 			    next msg;
    264 			}
    265 			
    266 			# ... maybe with other case?
    267 			$lc_msg2=lc($msg2);
    268 			if ($lc_copyrights{$lc_msg2}) {
    269 			    print "already there, in different case - skipping\n"
    270 				if $debug;
    271 			    next msg;
    272 			}
    273 		    } else {
    274 			# check if there's a version of the same msg
    275 			# with a trailing dot
    276 			$msg2=$msg;
    277 			$msg2.=".";
    278 			if ($copyrights{"$msg2"}) {
    279 			    # already there - skip
    280 			    print "already there, w/ dot - skipping!\n"
    281 				if $debug;
    282 			    next msg;
    283 			}
    284 			
    285 			# ... maybe with other case?
    286 			$lc_msg2=lc($msg2);
    287 			if ($lc_copyrights{$lc_msg2}) {
    288 			    print "already there, in different case - skipping\n"
    289 				if $debug;
    290 			    next msg;
    291 			}
    292 		    }
    293 
    294 		    $copyrights{$msg} = 1;
    295 		    $lc_copyrights{$lc_msg} = 1;
    296 		}		 
    297 
    298 	    } else {
    299 		print "?> $_" if $debug;
    300 
    301                 if ($fn !~ m,$known_bad_clause_3_wording,) {
    302 		    warning($fn, "bad clause 3?");
    303                 }
    304 		last line;
    305 	    }
    306 	}
    307     }
    308     close(F);
    309 }
    310 
    311 
    312 if ($html) {
    313     print "<ul>\n";
    314     foreach $msg (sort keys %copyrights) {
    315 	print "<li>$msg</li>\n";
    316     }
    317     print "</ul>\n";
    318 } elsif ($xml) {
    319     foreach $msg (sort keys %copyrights) {
    320 	print "<listitem>$msg</listitem>\n";
    321     }
    322 } else {
    323     print "------------------------------------------------------------\n";
    324 
    325     $firsttime=1;
    326     foreach $msg (sort keys %copyrights) {
    327 	if ($firsttime) {
    328 	    $firsttime=0;
    329 	} else {
    330 	    print ".It\n";
    331 	}
    332 	print "$msg\n";
    333     }
    334 }
    335