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