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.
29 # $FreeBSD: src/gnu/usr.bin/man/makewhatis/makewhatis.perl,v 1.21.2.4 2002/03/29 15:38:09 ru Exp $
35 usage: makewhatis [-a|-append] [-h|-help] [-i|-indent column] [-L|-locale]
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
56 # Array of all entries
61 local($file) = $whatisdb;
62 $file =~ s/\.tmp$// if !$outfile;
64 if (open(A, "$file")) {
65 warn "Open $file for append mode\n" if $verbose;
73 warn "$whatisdb: $!\n" if lstat($file) && $verbose; #
79 warn "Open $whatisdb\n" if $verbose;
80 if (!open(A, "> $whatisdb")) {
81 die "$whatisdb: $!\n" if $outfile;
83 warn "$whatisdb: $!\n"; $err++; return 0;
92 local($w) = $whatisdb;
97 if ($success) { # success
99 warn "\n" if $verbose && $pointflag;
100 warn "sort -u > $whatisdb\n" if $verbose;
101 foreach $i (sort @a) {
109 print @b; close A; select STDOUT;
112 warn "Rename $whatisdb to $w\n" if $verbose;
113 rename($whatisdb, $w) || warn "rename $whatisdb $w\n";
114 $counter_all += $counter;
115 warn "$counter entries in $w\n" if $verbose;
117 $counter_all = $counter;
119 } else { # building whatisdb failed
121 warn "building whatisdb: $whatisdb failed\n" if $verbose;
128 local($file, $dev,$ino);
130 warn "\n" if $pointflag;
131 warn "traverse $dir\n" if $verbose;
134 if (!opendir(M, $dir)) {
135 warn "$dir: $!\n"; $err++; return 0;
139 foreach $file (readdir(M)) {
140 next if $file =~ /^(\.|\.\.)$/;
142 ($dev, $ino) = ((stat("$dir/$file"))[01]);
144 if ($man_red{"$dev.$ino"}) {
146 print STDERR "+" if $verbose;
147 $pointflag++ if $verbose;
149 &manual("$dir/$file");
151 $man_red{"$dev.$ino"} = 1;
153 warn "Cannot find file: $dir/$file\n"; $err++;
163 local($subdir, $file);
165 # clean up, in case mandir and subdirs are called simultaneously
166 # e. g.: ~/man/man1 ~/man/man2 ~/man
167 #~/man/ man1 and ~/man/man2 are a subset of ~/man
168 foreach $file (keys %man_red) {
169 delete $man_red{$file};
173 warn "\n" if $verbose && $pointflag;
174 warn "open manpath directory ``$dir''\n" if $verbose;
176 if (!opendir(DIR, $dir)) {
177 warn "opendir ``$dir'':$!\n"; $err = 1; return 0;
179 foreach $subdir (sort(readdir(DIR))) {
180 if ($subdir =~ /^man\w+$/) {
181 $subdir = "$dir/$subdir";
182 &parse_subdir($subdir);
183 &parse_subdir($subdir) if -d ($subdir .= "/${machine}");
188 } elsif ($dir =~ /man\w+$/) {
191 warn "Assume ``$dir'' is not a man directory.\n";
200 local($dev,$ino) = (stat($dir))[0..1];
202 if ($dir_redundant{"$dev.$ino"}) {
203 warn "$dir is equal to: $dir_redundant{\"$dev.$ino\"}\n" if $verbose;
206 $dir_redundant{"$dev.$ino"} = $dir;
211 # ``/usr/man/man1/foo.l'' -> ``l''
213 local($filename) = @_;
214 local($extension) = $filename;
216 $extension =~ s/$ext$//g; # strip .gz
217 $extension =~ s/.*\///g; # basename
219 if ($extension !~ m%[^/]+\.[^.]+$%) { # no dot
220 $extension = $filename;
221 #$extension =~ s|/[^/]+$||;
222 $extension =~ s%.*man([^/]+)/[^/]+%$1%; # last character
223 warn "\n" if $verbose && $pointflag;
224 warn "$filename has no extension, try section ``$extension''\n"
228 $extension =~ s/.*\.//g; # foo.bla.1 -> 1
233 # ``/usr/man/man1/foo.1'' -> ``foo''
239 $name =~ s=\.[^\.]+$==;
247 local($delim) = " - ";
255 s/\(OBSOLETED\)[ ]?//;
261 s/ / - / unless / - /;
262 ($man,$desc) = split(/ - /);
264 $man = $name unless $man;
266 $man =~ s/,/($extension),/g;
267 $man .= "($extension)";
271 $desc =~ s/^[ \t]+//;
273 for($i = length($man); $i < $indent && $desc; $i++) {
277 push(@a, "$man$delim$desc\n");
283 # The filename of manual page is not a keyword.
284 # This is bad, because you don't find the manpage
285 # whith: $ man <section> <keyword>
287 # Add filename if a) filename is not a keyword and b) no keyword(s)
288 # exist as file in same mansection
291 foreach (split(/,\s+/, $man)) {
293 # filename is keyword
294 return if $name eq $_;
297 local($f) = $file; $f =~ s%/*[^/]+$%%; # dirname
298 local($e) = $file; $e =~ s/$ext$//; $e =~ s%.*(\.[^.]+)$%$1%; # .1
300 foreach (split(/,\s+/, $man)) {
303 # a keyword exist as file
304 return if -e "$f/$_$e" || -e "$f/$_$e$ext";
307 $man = "$name($extension), $man";
313 local($list, $desc, $extension);
314 local($ofile) = $file;
316 # Compressed man pages
317 if ($ofile =~ /$ext$/) {
318 $ofile = "gzcat $file |";
319 print STDERR "*" if $verbose;
321 print STDERR "." if $verbose;
323 $pointflag++ if $verbose;
325 if (!open(F, "$ofile")) {
326 warn "Cannot open file: $ofile\n"; $err++;
330 $extension = &ext($file);
331 $name = &name($file);
333 $section_name = "NAME|Name|NAMN|BEZEICHNUNG|̾¾Î|îáú÷áîéå";
338 # ``man'' style pages
339 # &&: it takes you only half the user time, regexp is slow!!!
340 if (/^\.SH/ && /^\.SH[ \t]+["]?($section_name)["]?/) {
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 while(<F>) { } # skip remaining input to avoid pipe errors
357 &out($list); close F; return 1;
358 } elsif (/^\.Sh/ && /^\.Sh[ \t]+["]?($section_name)["]?/) {
359 # ``doc'' style pages
364 s/^\.\\".*$//; #" delete comments
365 next if /^\.[ \t]*$/; # skip empty calls
373 $list .= '- ' if (!$flag && !/^- /);
378 $list .= "(@_[2])" if @_[2];
380 s/^\.([A-Z][a-z])?[ \t]*//;
387 while(<F>) { } # skip remaining input to avoid pipe errors
388 &out($list); close F; return 1;
390 } elsif(/^\.so/ && /^\.so[ \t]+man/) {
391 while(<F>) { } # skip remaining input to avoid pipe errors
395 if (!$source && $verbose) {
396 warn "\n" if $pointflag;
397 warn "Maybe $file is not a manpage\n" ;
403 # make relative path to absolute path
405 local(@dirlist) = @_;
406 local($pwd, $dir, @a);
409 foreach $dir (@dirlist) {
411 chop($pwd = `pwd`) if (!$pwd || $pwd !~ /^\//);
412 push(@a, "$pwd/$dir");
421 # e.g.: //usr///home// -> /usr/home
425 $dir =~ s|/+|/|g; # delete double '/'
426 $dir =~ s|/$||; # delete '/' at end
427 $dir =~ s|/(\.\/)+|/|g; # delete ././././
429 $dir =~ s|/+|/|g; # delete double '/'
430 $dir =~ s|/$||; # delete '/' at end
431 $dir =~ s|/\.$||; # delete /. at end
432 return $dir if $dir ne "";
437 $verbose = 0; # Verbose
438 $indent = 24; # Indent for description
439 $outfile = 0; # Don't write to ./whatis
440 $whatis_name = "whatis"; # Default name for DB
441 $append = 0; # Don't delete old entries
442 $locale = 0; # Build DB only for localized man directories
443 chomp($machine = $ENV{'MACHINE'} || `uname -m`);
445 # choose localized man directories suffix.
446 $local_suffix = $ENV{'LC_ALL'} || $ENV{'LC_CTYPE'} || $ENV{'LANG'};
448 # if no argument for directories given
449 @defaultmanpath = ( '/usr/share/man' );
451 $ext = '.gz'; # extension
454 $err = 0; # exit code
457 $dir_redundant = ''; # redundant directories
458 $man_red = ''; # redundant man pages
459 @a = (); # Array for output
462 $SIG{'INT'} = 'Exit';
463 $SIG{'HUP'} = 'Exit';
464 $SIG{'TRAP'} = 'Exit';
465 $SIG{'QUIT'} = 'Exit';
466 $SIG{'TERM'} = 'Exit';
467 $tmp = ''; # tmp file
469 $ENV{'PATH'} = "/bin:/usr/bin:$ENV{'PATH'}";
473 unlink($tmp) if $tmp ne ""; # unlink if a filename
474 die "$0: die on signal SIG@_\n";
481 while ($_ = $argv[0], /^-/) {
484 if (/^--?(v|verbose)$/) { $verbose = 1 }
485 elsif (/^--?(h|help|\?)$/) { &usage }
486 elsif (/^--?(o|outfile)$/) { $outfile = $argv[0]; shift @argv }
487 elsif (/^--?(f|format|i|indent)$/) { $i = $argv[0]; shift @argv }
488 elsif (/^--?(n|name)$/) { $whatis_name = $argv[0];shift @argv }
489 elsif (/^--?(a|append)$/) { $append = 1 }
490 elsif (/^--?(L|locale)$/) { $locale = 1 }
493 warn "Localized man directory suffix is ``$local_suffix''\n"
494 if $verbose && $locale;
497 if ($i =~ /^[0-9]+$/) {
500 warn "Ignoring wrong indent value: ``$i''\n";
504 return &absolute_path(@argv) if $#argv >= 0;
505 return @defaultmanpath if $#defaultmanpath >= 0;
507 warn "Missing directories\n"; &usage;
510 # Process man directory
514 $dir = &stripdir($dir);
515 &dir_redundant($dir) && &parse_dir($dir);
518 # Process man directory and store output to file
519 sub process_dir_to_file {
522 $dir = &stripdir($dir);
523 &dir_redundant($dir) &&
524 &close_output(&open_output($dir) && &parse_dir($dir));
527 # convert locale name to short notation (ru_RU.KOI8-R -> ru.KOI8-R)
528 sub short_locale_name {
531 $lname =~ s|_[A-Z][A-Z]||;
532 warn "short locale name is $lname\n" if $verbose && $locale;
541 # allow colons in dir: ``makewhatis dir1:dir2:dir3''
542 @argv = &parse(split(/[: ]/, join($", @ARGV))); # "
545 if(&open_output($outfile)){
546 foreach $dir (@argv) {
547 # "Local only" flag set ? Yes ...
549 if ($local_suffix ne "") {
550 &process_dir($dir.'/'.$local_suffix);
551 &process_dir($dir.'/'.&short_locale_name($local_suffix));
560 foreach $dir (@argv) {
561 # "Local only" flag set ? Yes ...
563 if ($local_suffix ne "") {
564 &process_dir_to_file($dir.'/'.$local_suffix);
565 &process_dir_to_file($dir.'/'.&short_locale_name($local_suffix));
568 &process_dir_to_file($dir);
573 warn "Total entries: $counter_all\n" if $verbose && ($#argv > 0 || $outfile);