summaryrefslogtreecommitdiff
path: root/libexec/makewhatis/makewhatis.pl
blob: 5b39c3d48a05df8cefc999a2d72e48988f4b3e8e (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/perl -w
# ex:ts=4 sw=4:

# $OpenBSD: makewhatis.pl,v 1.2 2000/02/05 22:15:16 espie Exp $
#
# Copyright (c) 2000 Marc Espie.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
# PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use strict;
use File::Find;
use IO::File;


# write_uniques($list, $file):
#
#	write $list to file named $file, removing duplicate entries.
#	Change $file mode/owners to expected values
#
sub write_uniques
{
	my $list = shift;
	my $f = shift;
	my ($out, $last);
	local $_;

	$out = new IO::File $f, "w" or die "$0: Can't open $f";

	my @sorted = sort @$list;

	while ($_ = shift @sorted) {
		print $out $_, "\n" unless defined $last and $_ eq $last;
		$last = $_;
	}
	close $out;
	chmod 0444, $f;
	chown 0, (getgrnam 'bin')[2], $f;
}

# handle_unformated($result, $args)
#
#	handle a batch of unformated manpages $args,
#	push the subjects to $result
#
sub handle_unformated
{
	my $result = shift;
	my $args = shift;
	local $_;
	my $cmd;

	$cmd = new IO::File "/usr/libexec/getNAME ".join(" ", @$args)."|";
	while (<$cmd>) {
		chomp;
		s/ [a-zA-Z\d]* \\-/ -/;
		push(@$result, $_);
	}
	close $cmd;
}

sub add_subject
{
	my $lines = shift;
	local $_ = shift;
	my $section = shift;

	if (m/-/) {
		# some twits underline the command name
		while (s/_\cH//g || s/(.)\cH\1/$1/g)
			{}
		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*$//;
			push(@$lines, "$func ($section) - $descr");
			return;
		}
	}
	print STDERR "Weird subject line $_ in ", shift, "\n";
}

# $lines = handle_formated($file)
#
#	handle a formatted manpage in $file
#
# 	may return several subjects, perl(3p) do !
#
sub handle_formated
{
	my $file = shift;
	my $filename = shift;
	local $_;
	my ($section, $subject);
	my @lines=();
	while (<$file>) {
		next if /^$/;
		chomp;
		# Remove boldface and underlining
		while (s/_\cH//g || s/(.)\cH\1/$1/g)
			{}
		if (m/\w[-+.\w\d]*\(([-+.\w\d\/]+)\)/) {
			$section = $1;
		}
		# Not all man pages are in english
		if (m/^(?:NAME|NAMN)\s*$/) {
			unless (defined $section) {
				print STDERR "Can't find section in $filename\n";
				$section='??';
			}
			while (<$file>) {
			    chomp;
			    # perl agregates several subjects in one manpage
				if (m/^$/) {
					add_subject(\@lines, $subject, $section, $filename) 
						if defined $subject;
					$subject = undef;
				} elsif (m/^\S/ || m/^\s+\*{3,}\s*$/) {
					add_subject(\@lines, $subject, $section, $filename) 
						if defined $subject;
					last;
				} else {
					$subject.=$_;
				}
			}
		last;
		}
	}

	print STDERR "Can't parse $filename (not a manpage ?)\n" if @lines == 0;
	return \@lines;
}

# $list = find_manpages($dir)
#
#	find all manpages under $dir, trim some duplicates.
#
sub find_manpages
{
	my $dir = shift;
	my ($list, %nodes);
	$list=[];
	find(
	    sub {
		return unless /(?:\.[0-9]|0\.Z|0\.gz)$/;
		return unless -f $_;
		my $inode = (stat _)[1];
		return if defined $nodes{$inode};
		$nodes{$inode} = 1;
		push(@$list, $File::Find::name);
	    }, $dir);
	return $list;
}

# $subjects = scan_manpages($list)
#
#	scan a set of manpages, return list of subjects
#
sub scan_manpages
{
	my $list = shift;
	local $_;
	my (@todo, $done);
	$done=[];

	for (@$list) {
	    my ($file, $subjects);
	    if (m/\.[1-9]$/) {
		    push(@todo, $_);
		    if (@todo > 5000) {
			    handle_unformated($done, \@todo);
			    @todo = ();
		    }
		    next;
	    } elsif (m/\.0\.(?:Z|gz)$/) {
		    $file = new IO::File "gzip -fdc $_|";
	    } else {
		    $file = new IO::File $_ or die "$0: Can't read $_\n";
	    }

	    $subjects = handle_formated($file, $_);
	    push @$done, @$subjects;
	}
	if (@todo > 0) {
		handle_unformated($done, \@todo);
	}
	return $done;
}

# build_index($dir)
#
#	build index for $dir
sub build_index
{
	my $dir = shift;
	my $list = find_manpages($dir);
	my $subjects = scan_manpages($list);
	write_uniques($subjects, "$dir/whatis.db");
}


# main code
    
while ($#ARGV != -1 and $ARGV[0] =~ m/^-/) {
	my $opt = shift;
	last if $opt eq '--';
	if ($opt eq '-d') {
		my $mandir = shift;
		unless (-d $mandir) {
			die "$0: $mandir: not a directory"
		}
		chdir $mandir;

		my $whatis = "$mandir/whatis.db";
		my $old = new IO::File $whatis or 
		    die "$0 $whatis to merge with";
		my $subjects = scan_manpages(\@ARGV);
		while (<$old>) {
			chomp;
			push(@$subjects, $_);
		}
		close $old;
		write_uniques($subjects, $whatis);
		exit 0;
	} else {
		die "$0: unknown option $opt\n";
	}
}

if ($#ARGV == -1) {
	local $_;
	@ARGV=();
	my $conf;
	$conf = new IO::File '/etc/man.conf' or 
	    die "$0: Can't open /etc/man.conf";
	while (<$conf>) {
		chomp;
		push(@ARGV, $1) if /^_whatdb\s+(.*)\/whatis\.db\s*$/;
	}
	close $conf;
}
		
for my $mandir (@ARGV) {
	unless (-d $mandir) {
		die "$0: $mandir: not a directory"
	}
    build_index($mandir);
}