1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# ex:ts=8 sw=4:
# $OpenBSD: Formated.pm,v 1.4 2009/10/11 08:14:37 sthen Exp $
# Copyright (c) 2000-2004 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use strict;
use warnings;
package OpenBSD::Makewhatis::Formated;
# add_formated_subject($subjects, $_, $section, $filename, $picky):
# add subject $_ to the list of current $subjects, in section $section.
#
sub add_formated_subject
{
my ($subjects, $line, $section, $filename, $picky) = @_;
local $_ = $line;
if (m/-/) {
s/([-+.\w\d,])\s+/$1 /g;
s/([a-z][A-z])-\s+/$1/g;
# some twits use: func -- description
if (m/^[^-+.\w\d]*(.*?) -(?:-?)\s+(.*)/) {
my ($func, $descr) = ($1, $2);
$func =~ s/,\s*$//;
# nroff will tend to cut function names at the weirdest places
if (length($func) > 40 && $func =~ m/,/ && $section =~ /^3/) {
$func =~ s/\b \b//g;
}
$_ = "$func ($section) - $descr";
push(@$subjects, $_);
return;
}
}
print STDERR "Weird subject line in $filename:\n$_\n" if $picky;
# try to find subject in line anyway
if (m/^\s*(.*\S)(?:\s{3,}|\(\)\s+)(.*?)\s*$/) {
my ($func, $descr) = ($1, $2);
$func =~ s/\s+/ /g;
$descr =~ s/\s+/ /g;
$_ = "$func ($section) - $descr";
push(@$subjects, $_);
return;
}
print STDERR "Weird subject line in $filename:\n$_\n" unless $picky;
}
# $lines = handle($file, $filename, $picky)
#
# handle a formatted manpage in $file
#
# may return several subjects, perl(3p) do !
#
sub handle
{
my ($file, $filename, $picky) = @_;
local $_;
my ($section, $subject);
my @lines=();
my $foundname = 0;
while (<$file>) {
chomp;
if (m/^$/) {
# perl aggregates several subjects in one manpage
# so we don't stop after we've got one subject
add_formated_subject(\@lines, $subject, $section, $filename, $picky)
if defined $subject;
$subject = undef;
next;
}
# Remove boldface from wide characters
while (s/(..)\cH\cH\1/$1/g)
{}
# Remove boldface and underlining
while (s/_\cH//g || s/(.)\cH\1/$1/g)
{}
if (!$foundname && m/\w[-+.\w\d]*\(([-+.\w\d\/]+)\)/) {
$section = $1;
# Find architecture
if (m/Manual\s+\((.*?)\)/) {
$section = "$section/$1";
}
}
# Not all man pages are in english
# weird hex is `Namae' in japanese
if (m/^(?:NAME|NAMES|NAMN|NOMBRE|NOME|Name|\xbe|\xcc\xbe\xbe\xce|\xcc\xbe\xc1\xb0)\s*$/) {
unless (defined $section) {
# try to retrieve section from filename
if ($filename =~ m/(?:cat|man)([\dln])\//) {
$section = $1;
print STDERR "Can't find section in $filename, deducting $section from context\n" if $picky;
} else {
$section='??';
print STDERR "Can't find section in $filename\n";
}
}
$foundname = 1;
next;
}
if ($foundname) {
if (m/^\S/ || m/^\s+\*{3,}\s*$/) {
add_formated_subject(\@lines, $subject, $section, $filename, $picky)
if defined $subject;
last;
} else {
# deal with troff hyphenations
if (defined $subject and $subject =~ m/\xad\s*$/) {
$subject =~ s/(?:\xad\cH)*\xad\s*$//;
s/^\s*//;
}
# more troff hyphenation
if (defined $subject and $subject =~ m/\S(?:\-\cH)*\-$/) {
$subject =~ s/(?:\-\cH)*\-$//;
s/^\s*//;
}
s/^\s+/ /;
$subject.=$_;
}
}
}
print STDERR "Can't parse $filename (not a manpage ?)\n" if @lines == 0;
return \@lines;
}
1;
|