Home | History | Annotate | Line # | Download | only in common
extract-contrib-string.pl revision 1.10
      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     .'|usr.sbin/hilinfo/hilinfo.c'	   	# CSS @ Utah
     77     ;	
     78 
     79 sub warning {
     80     local($fn,$msg) = @_;
     81     print "XXX $fn line $.: $msg\n"
     82 }
     83 
     84 while ($#ARGV >= 0) {
     85     $debug=1 if ($ARGV[0] =~ /-d/i);
     86     $html=1  if ($ARGV[0] =~ /-h/i);
     87     $xml=1  if ($ARGV[0] =~ /-x/i);
     88     $usage=1  if ($ARGV[0] =~ /-\?/);
     89     shift(@ARGV);
     90 }
     91 
     92 if ($usage) {
     93     print "usage: find /usr/src -type f -print |\n" .
     94 	" perl extract-contrib-string.pl [-h] [-x] [-?] [-d]\n" .
     95 	"   where\n" .
     96 	"    -h   output html\n" .
     97 	"    -x   output xml/docbook\n" .
     98 	"    -d   debug\n" .
     99 	"    -?   display this help message\n";
    100     exit(0);
    101 }
    102 
    103 $comments = !$html && !$xml;
    104 
    105 file:
    106 while(<>) {
    107     chomp();
    108     $fn=$_;
    109     
    110     open(F, "$fn") || die "cannot read $fn: $!\n";
    111 
    112   line:
    113     while(<F>) {
    114 	if (0 and /$ack_line2/i){
    115 	    print "?> $_" if $debug;
    116 	    
    117 	    if ($fn !~ m,$known_bad_clause_3_wording,) {
    118 		warning($fn, "clause 3 start not caught");
    119 	    }
    120 	    last line;
    121 	}
    122 	
    123 	print "0> $_" if $debug;
    124 
    125 	# special case perl script generating a license (openssl's
    126 	# mkerr.pl) - ignore the quoted license, there is another one
    127 	# inside:
    128 	if (/^\"\s\*.*$ack_line1.*\\n\"\,/) {
    129 		while(!/$ack_endline/i) {
    130 		    print "S> $_" if $debug;
    131 		    $_ = <F>;
    132 		}
    133 	}
    134 
    135 	if (/$ack_line1/i
    136 	    or (/$ack_line2/ and $fn =~ m,$known_bad_clause_3_wording,)) {
    137 	    
    138 	    print "1> $_" if $debug;
    139 
    140 	    $_=<F>
    141 		unless $fn =~ m,$known_bad_clause_3_wording,;
    142 	    if (/$ack_line2/i or $fn =~ m,$known_bad_clause_3_wording,){
    143 		
    144 		print "2> $_" if $debug;
    145 		
    146 		$msg="";
    147 		$cnt=0;
    148 		$_=<F>;
    149 		while(!/$ack_endline/i) {
    150 		    
    151 		    print "C> $_" if $debug;
    152 
    153 		    $msg .= $_;
    154 		    $cnt++;
    155 		    $_ = <F>;
    156 		    if ($cnt > 10) {
    157 			warning($fn,"loooong copyright?");
    158 			last line;
    159 		    }
    160 		}
    161 
    162 		print "E> $_" if $debug;
    163 		
    164 		# post-process
    165 
    166 		# *roff
    167 		while ($msg =~ /^\.\\"\s*/) {
    168 			$msg =~ s/^\.\\"\s*//o;
    169 		}
    170 		while ($msg =~ /\n\.\\"\s*/) {
    171 			$msg =~ s/\n\.\\"\s*/\n/o;
    172 		}
    173 		$msg =~ s/\n\.\\"\s*$/\n/g;
    174 
    175 		# C++/C99
    176 		while ($msg =~ /^\s*\/\/\s*/) {
    177 			$msg =~ s/^\s*\/\/\s*//o;
    178 		}
    179 		while ($msg =~ /\n\s*\/\/\s*$/) {
    180 			$msg =~ s/\n\s*\/\/\s*$//o;
    181 		}
    182 		$msg =~ s/\n\s*\/\/\s*/\n/g;
    183 
    184 		# C
    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 		# texinfo @c
    194 		while ($msg =~ /^\s*\@c\s+/) {
    195 			$msg =~ s/^\s*\@c\s+//o;
    196 		}
    197 		while ($msg =~ /\n\s*\@c\s+$/) {
    198 			$msg =~ s/\n\s*\@c\s+$//o;
    199 		}
    200 		$msg =~ s/\n\s*\@c\s+/\n/g;
    201 
    202 		$msg =~ s/^REM\s*//g;			# BASIC?!?
    203 		$msg =~ s/\nREM\s*/\n/g;		# BASIC?!?
    204 		$msg =~ s/^dnl\s*//g;			# m4
    205 		$msg =~ s/\dnl\s*/\n/g;			# m4
    206 		$msg =~ s/^\s+-\s+//g;			# seen in docbook files
    207 		$msg =~ s/\n\s+-\s+/ /g;		#
    208 		$msg =~ s/^[#\\\|";]*\s*//g;		# sh etc.
    209 		$msg =~ s/\n[#\\\|";]\s*/\n/g;		# sh etc.
    210 		$msg =~ s/^[ 	*]*//g;      		# C
    211 		$msg =~ s/\n[ 	*]*/\n/g;    		# C
    212 
    213 		$msg =~ s/\@cartouche\n//;              # texinfo
    214 
    215 		$msg =~ s/
//g;
    217 		$msg =~ s/\s*\n/\n/g;
    218 		$msg =~ s/^\s*//;
    219 		$msg =~ s/\\\@/\@/g;
    220 		$msg =~ s/\n\n/\n/g;
    221 	        $msg =~ s/^\s*``//;
    222 	        $msg =~ s/''\s*$//;
    223 		$msg =~ s/^\"//o;
    224 		$msg =~ s/\"$//o;
    225 
    226 		# Split up into separate paragraphs
    227 		#
    228 		$msgs=$msg;
    229 		$msgs=~s/(This (software|product))/|$1/g;
    230 		$msgs=~s,^\|,,;
    231 	      msg:
    232 		foreach $msg (split(/\|/, $msgs)) {
    233 		    while ($msg =~ /[\n\s]+$/) {
    234 			$msg =~ s/[\n\s]+$//o;
    235 		    }
    236 		    next if ($msg eq "");
    237 		    if ($comments) {
    238 			print ".\\\" File $fn:\n";
    239 			print "$msg";
    240 			print "\n\n";
    241 		    }
    242 		    
    243 		    # Figure out if there's a version w/ or w/o trailing dot
    244 		    # 
    245 		    if ($msg =~ /\.$/) {
    246 			# check if there's a version of the same msg
    247 			# w/o a trailing dot
    248 			$msg2=$msg;
    249 			$msg2=~s,\.$,,;
    250 			if ($copyrights{"$msg2"}) {
    251 			    # already there - skip
    252 			    print "already there, w/o dot - skipping!\n"
    253 				if $debug;
    254 			    next msg;
    255 			}
    256 			
    257 			# ... maybe with other case?
    258 			$lc_msg2=lc($msg2);
    259 			if ($lc_copyrights{$lc_msg2}) {
    260 			    print "already there, in different case - skipping\n"
    261 				if $debug;
    262 			    next msg;
    263 			}
    264 		    } else {
    265 			# check if there's a version of the same msg
    266 			# with a trailing dot
    267 			$msg2=$msg;
    268 			$msg2.=".";
    269 			if ($copyrights{"$msg2"}) {
    270 			    # already there - skip
    271 			    print "already there, w/ dot - skipping!\n"
    272 				if $debug;
    273 			    next msg;
    274 			}
    275 			
    276 			# ... maybe with other case?
    277 			$lc_msg2=lc($msg2);
    278 			if ($lc_copyrights{$lc_msg2}) {
    279 			    print "already there, in different case - skipping\n"
    280 				if $debug;
    281 			    next msg;
    282 			}
    283 		    }
    284 
    285 		    $copyrights{$msg} = 1;
    286 		    $lc_copyrights{$lc_msg} = 1;
    287 		}		 
    288 
    289 	    } else {
    290 		print "?> $_" if $debug;
    291 
    292                 if ($fn !~ m,$known_bad_clause_3_wording,) {
    293 		    warning($fn, "bad clause 3?");
    294                 }
    295 		last line;
    296 	    }
    297 	}
    298     }
    299     close(F);
    300 }
    301 
    302 
    303 if ($html) {
    304     print "<ul>\n";
    305     foreach $msg (sort keys %copyrights) {
    306 	print "<li>$msg</li>\n";
    307     }
    308     print "</ul>\n";
    309 } elsif ($xml) {
    310     foreach $msg (sort keys %copyrights) {
    311 	print "<listitem>$msg</listitem>\n";
    312     }
    313 } else {
    314     print "------------------------------------------------------------\n";
    315 
    316     $firsttime=1;
    317     foreach $msg (sort keys %copyrights) {
    318 	if ($firsttime) {
    319 	    $firsttime=0;
    320 	} else {
    321 	    print ".It\n";
    322 	}
    323 	print "$msg\n";
    324     }
    325 }
    326