2 # Emacs should use -*- cperl -*- mode
4 # Copyright (c) 2003-2005 Simon L. Nielsen <simon@FreeBSD.org>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # Parse the list of supported hardware out of section 4 manual pages
32 # and output it on stdout as SGML/DocBook entities.
34 # The script will look for the following line in the manual page:
36 # and make an entity of the content until the line containing:
39 # For Lists only the first line will be printed. If there are
40 # arguments to the .It command, only the argument will be printed.
43 # man2hwnotes.pl [-l] [-d 0-6] [-a <archlist file>] [-o <outputfile>]
44 # <manualpage> [<manualpage> ...]
48 use Digest::MD5 qw(md5_hex);
50 # Section from manual page to extract
51 my $hwlist_sect = "HARDWARE";
53 # Override default archtecture list for some devices:
54 my $archlist_file = "dev.archlist.txt";
59 my $only_list_out = 0; # Should only lists be generated in the output?
60 my @out_lines; # Single lines
61 my @out_dev; # Device entities
65 if (!getopts("a:d:lo:",\%options)) {
66 die("$!: Invalid command line arguments in ", __LINE__, "\n");
69 if (defined($options{d})) {
70 $debuglevel = $options{d};
72 if (defined($options{a})) {
73 $archlist_file = $options{a};
75 if (defined($options{l})) {
79 my $outputfile = $options{o};
81 if ($debuglevel > 0) {
82 # Don't do output buffering in debug mode.
86 load_archlist($archlist_file);
88 if (defined($outputfile)) {
89 open(OLDOUT, ">&STDOUT") || die("$!: Could not open STDOUT in ", __LINE__, ".\n");
90 open(STDOUT, ">$outputfile") || die("$!: Could not open $outputfile in ", __LINE__, ".\n");
95 These are automatically generated device lists for FreeBSD hardware notes.
100 # Print the default device preamble entities
101 print "<!ENTITY hwlist.preamble.pre 'The'>\n";
102 print "<!ENTITY hwlist.preamble.post 'driver supports:'>\n";
105 foreach my $page (@ARGV) {
106 if ($page !~ m/\.4$/) {
107 dlog(2, "Skipped $page (not *.4)");
110 dlog(2, "Parsing $page");
114 print join("\n", @out_lines), "\n";
117 print join("\n", @out_dev), "\n";
124 if (defined($outputfile)) {
125 open(STDOUT, ">&OLDOUT") || die("$!: Could not open STDOUT in ", __LINE__, ".\n");
126 close(OLDOUT) || die("$!: Could not close OLDOUT in ", __LINE__, ".\n");
132 foreach my $l (@lines) {
134 $l =~ s:([\x21-\x2f\x5b-\x60\x7b-\x7f]):sprintf("&\#\%d;", ord($1)):eg;
135 # Make sure ampersand is encoded as & since jade seems to
136 # be confused when it is encoded as & inside an entity.
137 $l =~ s/&/&/g;
139 return (wantarray) ? @lines : join "", @lines;
146 my $found_hwlist = 0;
148 $mdocvars{isin_hwlist} = 0;
149 $mdocvars{isin_list} = 0;
150 $mdocvars{parabuf} = "";
151 $mdocvars{listtype} = "";
152 $mdocvars{it_nr} = 0;
154 open(MANPAGE, "$manpage") || die("$!: Could not open $manpage in ", __LINE__, ".\n");
159 dlog(5, "Read '$line'");
162 if (s/^\.(.*)$/$1/) {
165 # Detect, and ignore, comment lines
166 if (s/^\\"(.*)$/$1/) {
170 $cmd =~ s/^([^ ]+).*$/$1/;
172 if (/^Nm "?(\w+)"?/ && !defined($mdocvars{Nm})) {
173 dlog(3, "Setting Nm to $1");
175 # "_" cannot be used for an entity name.
176 $mdocvars{EntNm} = $1;
177 $mdocvars{EntNm} =~ s,_,.,g;
180 if (defined($mdocvars{Nm}) && $mdocvars{Nm} ne "") {
181 parabuf_addline(\%mdocvars, "&man.".$mdocvars{EntNm}.".$cur_mansection;");
183 dlog(2, "Warning: Bad Nm call in $manpage");
186 } elsif (/^Sh (.+)$/) {
187 dlog(4, "Setting section to $1");
188 my $cur_section = $1;
190 flush_out(\%mdocvars);
192 if ($cur_section =~ /^${hwlist_sect}$/) {
193 dlog(2, "Found the device section ${hwlist_sect}");
194 $mdocvars{isin_hwlist} = 1;
196 add_sgmltag(\%mdocvars, "<!ENTITY hwlist.".$mdocvars{cur_manname}." '");
197 if ($only_list_out) {
198 add_sgmltag("<para>&hwlist.preamble.pre; " .
199 "&man.".$mdocvars{EntNm}.".$cur_mansection; " .
200 "&hwlist.preamble.post;</para>");
202 } elsif ($mdocvars{isin_hwlist}) {
203 dlog(2, "Found a HWLIST STOP key!");
204 add_sgmltag(\%mdocvars, "'>");
205 $mdocvars{isin_hwlist} = 0;
208 } elsif (/^Dt ([^ ]+) ([^ ]+)/) {
209 dlog(4, "Setting mansection to $2");
210 $mdocvars{cur_manname} = lc($1);
211 $cur_mansection = $2;
213 # "_" cannot be used for an entity name.
214 $mdocvars{cur_manname} =~ s,_,.,g;
216 } elsif (/^It ?(.*)$/) {
222 if ($mdocvars{parabuf} ne "") {
223 add_listitem(\%mdocvars);
226 # Remove quotes, if any.
227 $txt =~ s/"(.*)"/$1/;
229 if ($mdocvars{listtype} eq "column") {
230 # Ignore first item when it is likely to be a
232 if ($mdocvars{it_nr} == 1 && $txt =~ m/^(Em|Sy) /) {
233 dlog(2, "Skipping header line in column list");
236 # Only extract the first column.
238 $txt =~ s/([^\t]+)\t.*/$1/;
240 parabuf_addline(\%mdocvars, normalize($txt));
242 $mdocvars{isin_list} = 1;
243 flush_out(\%mdocvars);
244 add_sgmltag(\%mdocvars, "<itemizedlist>");
247 $mdocvars{listtype} = "tag";
248 # YACK! Hack for ata(4)
249 if ($mdocvars{Nm} eq "ata") {
250 $mdocvars{listtype} = "tagHACK";
252 } elsif (/-bullet/) {
253 $mdocvars{listtype} = "bullet";
254 } elsif (/-column/) {
255 $mdocvars{listtype} = "column";
257 $mdocvars{listtype} = "unknown";
259 dlog(2, "Listtype set to $mdocvars{listtype}");
261 if ($mdocvars{parabuf} ne "") {
262 add_listitem(\%mdocvars);
265 add_sgmltag(\%mdocvars, "</itemizedlist>");
266 $mdocvars{isin_list} = 0;
267 } elsif (/^Tn (.+)$/) {
268 # For now we print TradeName text as regular text.
269 my ($txt, $punct_str) = split_punct_chars($1);
271 parabuf_addline(\%mdocvars, normalize($txt . $punct_str));
272 } elsif (/^Xr ([^ ]+) (.+)$/) {
273 my ($xr_sect, $punct_str) = split_punct_chars($2);
276 # We need to check if the manual page exist to avoid
277 # breaking the doc build just because of a broken
279 #$txt = "&man.$1.$xr_sect;$punct_str";
280 $txt = "$1($xr_sect)$punct_str";
281 parabuf_addline(\%mdocvars, normalize($txt));
282 } elsif (/^Dq (.+)$/) {
283 my ($txt, $punct_str) = split_punct_chars($1);
285 parabuf_addline(\%mdocvars,
286 normalize("<quote>$txt</quote>$punct_str"));
287 } elsif (/^Sx (.+)$/) {
288 if ($mdocvars{isin_hwlist}) {
289 dlog(1, "Warning: Reference to another section in the " .
290 "$hwlist_sect section in " . $mdocvars{Nm} .
291 "(${cur_mansection})");
293 parabuf_addline(\%mdocvars, normalize($1));
294 } elsif (/^Pa (.+)$/) {
295 my ($txt, $punct_str) = split_punct_chars($1);
297 $txt = make_ulink($txt) . $punct_str;
298 parabuf_addline(\%mdocvars, normalize($txt));
300 # Ignore all other commands.
301 dlog(3, "Ignoring unknown command $cmd");
304 # This is then regular text
305 parabuf_addline(\%mdocvars, normalize($_));
308 close(MANPAGE) || die("$!: Could not close $manpage in ", __LINE__, ".\n");
309 if (! $found_hwlist) {
310 dlog(2, "Hardware list not found in $manpage");
315 my ($level, $txt) = @_;
317 if ($level <= $debuglevel) {
318 print STDERR "$level: $txt\n";
324 my ($mdocvars, $txt) = (@_);
326 # We only care about the HW list for now.
327 if (${$mdocvars}{isin_hwlist}) {
328 push(@out_dev, $txt);
332 # Add a text entity, and return the used entity name.
334 my ($itemtxt) = (@_);
337 # Convert mdoc(7) minus
338 $itemtxt =~ s/\\-/-/g;
340 $itemtxt =~ s/'/‘/g;
342 $entity_name = "hwlist." . md5_hex($itemtxt);
343 dlog(4, "Adding '$itemtxt' as entity $entity_name");
344 push(@out_lines, "<!ENTITY $entity_name '$itemtxt'>");
346 return ($entity_name);
349 my ($mdocvars) = (@_);
350 my ($entity_name, $out);
353 if (!${$mdocvars}{isin_hwlist} || ${$mdocvars}{parabuf} eq "") {
357 $entity_name = add_txt_ent(${$mdocvars}{parabuf});
358 ${$mdocvars}{parabuf} = "";
359 if(defined($archlist{${$mdocvars}{Nm}})) {
360 $para_arch = ' arch="' . $archlist{${$mdocvars}{Nm}} . '"';
362 $out = "<para".$para_arch.">&".$entity_name.";</para>";
364 dlog(4, "Flushing parabuf");
365 add_sgmltag($mdocvars, $out);
368 # Add a new list item from the "parabuf".
370 my ($mdocvars) = (@_);
371 my ($listitem, $entity_name);
374 $entity_name = add_txt_ent(${$mdocvars}{parabuf});
375 ${$mdocvars}{parabuf} = "";
377 if(defined($archlist{${$mdocvars}{Nm}})) {
378 $para_arch = ' arch="' . $archlist{${$mdocvars}{Nm}} . '"';
380 $listitem = "<listitem><para".$para_arch.">&".$entity_name.";</para></listitem>";
381 dlog(4, "Adding '$listitem' to out_dev");
382 push(@out_dev, $listitem);
386 # Add a line to the "paragraph buffer"
387 sub parabuf_addline {
388 my $mdocvars = shift;
391 dlog(5, "Now in parabuf_addline");
393 # We only care about the HW list for now.
394 if (!${$mdocvars}{isin_hwlist}) {
401 if ($only_list_out && !${$mdocvars}{isin_list}) {
405 # We only add the first line for "tag" lists
406 if (${$mdocvars}{parabuf} ne "" && ${$mdocvars}{isin_list} &&
407 ${$mdocvars}{listtype} eq "tag") {
411 if (${$mdocvars}{parabuf} ne "") {
412 ${$mdocvars}{parabuf} .= " ";
415 dlog(4, "Adding '$txt' to parabuf");
417 ${$mdocvars}{parabuf} .= $txt;
425 dlog(2, "Parsing archlist $file");
427 open(FILE, "$file") || die("$!: Could not open archlist $file in ", __LINE__, ".\n");
432 if (/^#/ || $_ eq "") {
436 if (/(\w+)\t([\w,]+)/) {
437 dlog(4, "For driver $1 setting arch to $2");
440 dlog(1, "Warning: Could not parse archlist line $lineno");
447 # Check if a character is a mdoc(7) punctuation character.
451 return (length($str) == 1 && $str =~ /[\.,:;()\[\]\?!]/);
454 # Split out the punctuation characters of a mdoc(7) line.
455 sub split_punct_chars {
457 my (@stritems, $stritem, $punct_str);
460 @stritems = split(/ /, $str);
462 while (defined($stritem = $stritems[$#stritems]) &&
463 is_punct_char($stritem)) {
464 $punct_str = $stritem . $punct_str;
468 return (join(' ', @stritems), $punct_str);
471 # Create a ulink, if the string contains an URL.
475 $str =~ s,(http://[^ ]+),<ulink url="$1"></ulink>,;