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