3 # Copyright (c) 1994-1996 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 # makewhatis -- update the whatis database in the man directories.
35 usage: makewhatis [-a|-append ] [-h|-help] [-i|-indent column]
36 [-n|-name name] [-o|-outfile file] [-v|-verbose]
47 die "Name for whatis is empty\n" if $whatis_name eq "";
49 if ($outfile) { # Write all Output to $outfile
51 } else { # Use man/whatis
52 $whatisdb = $dir . "/$whatis_name.tmp";
54 $tmp = $whatisdb; # for signals
57 # Array of all entries
63 local($file) = $whatisdb;
64 $file =~ s/\.tmp$// if !$outfile;
66 if (open(A, "$file")) {
67 warn "Open $file for append mode\n" if $verbose;
75 warn "$whatisdb: $!\n" if lstat($file) && $verbose; #
81 warn "Open $whatisdb\n" if $verbose;
82 if (!open(A, "> $whatisdb")) {
83 die "$whatisdb: $!\n" if $outfile;
85 warn "$whatisdb: $!\n"; $err++; return 0;
94 local($w) = $whatisdb;
99 if ($success) { # success
102 warn "\n" if $verbose && $pointflag;
103 warn "sort -u > $whatisdb\n" if $verbose;
104 foreach $i (sort @a) {
112 print @b; close A; select STDOUT;
115 warn "Rename $whatisdb to $w\n" if $verbose;
116 rename($whatisdb, $w) || warn "rename $whatisdb $w\n";
117 $counter_all += $counter;
118 warn "$counter entries in $w\n" if $verbose;
120 $counter_all = $counter;
122 } else { # building whatisdb failed
124 warn "building whatisdb: $whatisdb failed\n" if $verbose;
131 local($file, $dev,$ino);
133 warn "\n" if $pointflag;
134 warn "traverse $dir\n" if $verbose;
137 if (!opendir(M, $dir)) {
138 warn "$dir: $!\n"; $err++; return 0;
142 foreach $file (readdir(M)) {
143 next if $file =~ /^(\.|\.\.)$/;
145 ($dev, $ino) = ((stat("$dir/$file"))[01]);
147 if ($man_red{"$dev.$ino"}) {
149 print STDERR "+" if $verbose;
150 $pointflag++ if $verbose;
152 &manual("$dir/$file");
154 $man_red{"$dev.$ino"} = 1;
156 warn "Cannot find file: $dir/$file\n"; $err++;
166 local($subdir, $file);
168 # clean up, in case mandir and subdirs are called simultaneously
169 # e. g.: ~/man/man1 ~/man/man2 ~/man
170 #~/man/ man1 and ~/man/man2 are a subset of ~/man
171 foreach $file (keys %man_red) {
172 delete $man_red{$file};
176 warn "\n" if $verbose && $pointflag;
177 warn "open manpath directory ``$dir''\n" if $verbose;
179 if (!opendir(DIR, $dir)) {
180 warn "opendir ``$dir'':$!\n"; $err = 1; return 0;
182 foreach $subdir (sort(readdir(DIR))) {
183 if ($subdir =~ /^man\w+$/) {
184 $subdir = "$dir/$subdir";
185 &parse_subdir($subdir);
190 } elsif ($dir =~ /man\w+$/) {
193 warn "Assume ``$dir'' is not a man directory.\n";
202 local ($dev,$ino) = (stat($dir))[0..1];
204 if ($dir_redundant{"$dev.$ino"}) {
205 warn "$dir is equal to: $dir_redundant{\"$dev.$ino\"}\n" if $verbose;
208 $dir_redundant{"$dev.$ino"} = $dir;
213 # ``/usr/man/man1/foo.l'' -> ``l''
215 local($filename) = @_;
216 local($extension) = $filename;
218 $extension =~ s/$ext$//g; # strip .gz
219 $extension =~ s/.*\///g; # basename
221 if ($extension !~ m%[^/]+\.[^.]+$%) { # no dot
222 $extension = $filename;
223 #$extension =~ s|/[^/]+$||;
224 $extension =~ s%.*man([^/]+)/[^/]+%$1%; # last character
225 warn "\n" if $verbose && $pointflag;
226 warn "$filename has no extension, try section ``$extension''\n"
230 $extension =~ s/.*\.//g; # foo.bla.1 -> 1
235 # ``/usr/man/man1/foo.1'' -> ``foo''
241 $name =~ s=\.[^\.]+$==;
249 local($delim) = " - ";
257 s/\(OBSOLETED\)[ ]?//;
263 s/ / - / unless / - /;
264 ($man,$desc) = split(/ - /);
266 $man = $name unless $man;
268 $man =~ s/,/($extension),/g;
269 $man .= "($extension)";
273 $desc =~ s/^[ \t]+//;
275 for($i = length($man); $i < $indent && $desc; $i++) {
279 push(@a, "$man$delim$desc\n");
285 # The filename of manual page is not a keyword.
286 # This is bad, because you don't find the manpage
287 # whith: $ man <section> <keyword>
289 # Add filename if a) filename is not a keyword and b) no keyword(s)
290 # exist as file in same mansection
293 foreach (split(/,\s+/, $man)) {
295 # filename is keyword
296 return if $name eq $_;
299 local($f) = $file; $f =~ s%/*[^/]+$%%; # dirname
300 local($e) = $file; $e =~ s/$ext$//; $e =~ s%.*(\.[^.]+)$%$1%; # .1
302 foreach (split(/,\s+/, $man)) {
305 # a keyword exist as file
306 return if -e "$f/$_$e" || -e "$f/$_$e$ext";
309 $man = "$name($extension), $man";
315 local($list, $desc, $extension);
316 local($ofile) = $file;
318 # Compressed man pages
319 if ($ofile =~ /$ext$/) {
320 $ofile = "gzcat $file |";
321 print STDERR "*" if $verbose;
323 print STDERR "." if $verbose;
325 $pointflag++ if $verbose;
327 if (!open(F, "$ofile")) {
328 warn "Cannot open file: $ofile\n"; $err++;
332 $extension = &ext($file);
333 $name = &name($file);
338 # ``man'' style pages
339 # &&: it takes you only half the user time, regexp is slow!!!
340 if (/^\.SH/ && /^\.SH[ \t]+["]?(NAME|Name|NAMN|BEZEICHNUNG|̾¾Î)["]?/) {
341 #while(<F>) { last unless /^\./ } # Skip
344 last if /^\.SH[ \t]/;
346 s/^\.IX\s.*//; # delete perlpod garbage
347 s/^\.[A-Z]+[ ]+[0-9]+$//; # delete commands
348 s/^\.[A-Za-z]+[ \t]*//; # delete commands
349 s/^\.\\".*$//; #" delete comments
356 &out($list); close F; return 1;
357 } elsif (/^\.Sh/ && /^\.Sh[ \t]+["]?(NAME|Name|BEZEICHNUNG|̾¾Î)["]?/) {
358 # ``doc'' style pages
363 s/^\.\\".*$//; #" delete comments
371 $list .= '- ' if (!$flag && !/-/);
373 s/^\.[A-Z][a-z][ \t]*//;
379 &out($list); close F; return 1;
381 } elsif(/^\.so/ && /^\.so[ \t]+man/) {
385 if (!$source && $verbose) {
386 warn "\n" if $pointflag;
387 warn "Maybe $file is not a manpage\n" ;
393 # make relative path to absolute path
395 local(@dirlist) = @_;
396 local($pwd, $dir, @a);
399 foreach $dir (@dirlist) {
401 chop($pwd = `pwd`) if (!$pwd || $pwd !~ /^\//);
402 push(@a, "$pwd/$dir");
411 # e.g.: //usr///home// -> /usr/home
415 $dir =~ s|/+|/|g; # delete double '/'
416 $dir =~ s|/$||; # delete '/' at end
417 $dir =~ s|/(\.\/)+|/|g; # delete ././././
419 $dir =~ s|/+|/|g; # delete double '/'
420 $dir =~ s|/$||; # delete '/' at end
421 $dir =~ s|/\.$||; # delete /. at end
422 return $dir if $dir ne "";
427 $verbose = 0; # Verbose
428 $indent = 24; # indent for description
429 $outfile = 0; # Don't write to ./whatis
430 $whatis_name = "whatis"; # Default name for DB
431 $append = 0; # Don't delete old entries
433 # if no argument for directories given
434 @defaultmanpath = ( '/usr/share/man' );
436 $ext = '.gz'; # extension
439 $err = 0; # exit code
442 $dir_redundant = ''; # redundant directories
443 $man_red = ''; # redundant man pages
444 @a = (); # Array for output
447 $SIG{'INT'} = 'Exit';
448 $SIG{'HUP'} = 'Exit';
449 $SIG{'TRAP'} = 'Exit';
450 $SIG{'QUIT'} = 'Exit';
451 $SIG{'TERM'} = 'Exit';
452 $tmp = ''; # tmp file
454 $ENV{'PATH'} = "/bin:/usr/bin:$ENV{'PATH'}";
458 unlink($tmp) if $tmp ne ""; # unlink if a filename
459 die "$0: die on signal SIG@_\n";
466 while ($_ = $argv[0], /^-/) {
469 if (/^--?(v|verbose)$/) { $verbose = 1 }
470 elsif (/^--?(h|help|\?)$/) { &usage }
471 elsif (/^--?(o|outfile)$/) { $outfile = $argv[0]; shift @argv }
472 elsif (/^--?(f|format|i|indent)$/) { $i = $argv[0]; shift @argv }
473 elsif (/^--?(n|name)$/) { $whatis_name = $argv[0];shift @argv }
474 elsif (/^--?(a|append)$/) { $append = 1 }
479 if ($i =~ /^[0-9]+$/) {
482 warn "Ignoring wrong indent value: ``$i''\n";
486 return &absolute_path(@argv) if $#argv >= 0;
487 return @defaultmanpath if $#defaultmanpath >= 0;
489 warn "Missing directories\n"; &usage;
498 # allow colons in dir: ``makewhatis dir1:dir2:dir3''
499 @argv = &parse(split(/[: ]/, join($", @ARGV))); # "
503 if(&open_output($outfile)){
504 foreach $dir (@argv) {
505 $dir = &stripdir($dir);
506 &dir_redundant($dir) && &parse_dir($dir);
511 foreach $dir (@argv) {
512 $dir = &stripdir($dir);
513 &dir_redundant($dir) &&
514 &close_output(&open_output($dir) && &parse_dir($dir));
518 warn "Total entries: $counter_all\n" if $verbose && ($#argv > 0 || $outfile);