diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2013-01-29 11:08:57 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2013-01-29 11:08:57 +0000 |
commit | 9fb8e3b70b926231553fb6f1014156bfa1e26747 (patch) | |
tree | 8923848e63792292190f71155e34910e12fbbe17 /libexec | |
parent | 9514bcb7468b013172136c4ad9b3de12d3bc1cf7 (diff) |
refactor code a bit to actually store subjects in the subject object
reformat for 8 spaces tab.
okay millert@
Diffstat (limited to 'libexec')
-rw-r--r-- | libexec/makewhatis/Makefile | 6 | ||||
-rw-r--r-- | libexec/makewhatis/OpenBSD/Makewhatis.pm | 165 | ||||
-rw-r--r-- | libexec/makewhatis/OpenBSD/Makewhatis/Formated.pm | 28 | ||||
-rw-r--r-- | libexec/makewhatis/OpenBSD/Makewhatis/Subject.pm | 19 | ||||
-rw-r--r-- | libexec/makewhatis/OpenBSD/Makewhatis/Unformated.pm | 29 | ||||
-rw-r--r-- | libexec/makewhatis/makewhatis | 2 |
6 files changed, 126 insertions, 123 deletions
diff --git a/libexec/makewhatis/Makefile b/libexec/makewhatis/Makefile index 9d29fb9e888..be9b992bccf 100644 --- a/libexec/makewhatis/Makefile +++ b/libexec/makewhatis/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.9 2011/01/26 15:10:59 espie Exp $ +# $OpenBSD: Makefile,v 1.10 2013/01/29 11:08:55 espie Exp $ MAN=makewhatis.8 NOPROG= @@ -23,9 +23,7 @@ realinstall: ${INSTALL} ${INSTALL_COPY} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \ ${.CURDIR}/$i ${DESTDIR}${LIBBASE}/$i .endfor -.for i in ${SCRIPTS} ${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \ - ${.CURDIR}/$i ${DESTDIR}${BINDIR}/$i -.endfor + ${.CURDIR}/makewhatis ${DESTDIR}${BINDIR}/makewhatis .include <bsd.prog.mk> diff --git a/libexec/makewhatis/OpenBSD/Makewhatis.pm b/libexec/makewhatis/OpenBSD/Makewhatis.pm index 32d01d2bfd9..5099d5fa0b2 100644 --- a/libexec/makewhatis/OpenBSD/Makewhatis.pm +++ b/libexec/makewhatis/OpenBSD/Makewhatis.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Makewhatis.pm,v 1.11 2012/01/27 11:27:18 espie Exp $ +# $OpenBSD: Makewhatis.pm,v 1.12 2013/01/29 11:08:55 espie Exp $ # Copyright (c) 2000-2004 Marc Espie <espie@openbsd.org> # # Permission to use, copy, modify, and distribute this software for any @@ -67,7 +67,6 @@ sub verbose { return shift->{verbose}; } - sub testmode { return shift->{testmode}; @@ -90,64 +89,65 @@ package OpenBSD::Makewhatis; # sub scan_manpages { - my ($list, $p) = @_; - my $_; - my $done=[]; - - require OpenBSD::Makewhatis::Subject; - my $h = OpenBSD::Makewhatis::SubjectHandler->new($p); - - for my $_ (@$list) { - my ($file, $subjects); - if (m/\.(?:Z|gz)$/) { - unless (open $file, '-|', "gzip", "-fdc", $_) { - $p->errsay("#1: can't decompress #2: #3", $0, $_, $!); - next; - } - $_ = $`; - } else { - if (-z $_) { - $p->errsay("Empty file #1", $_); - next; - } - unless (open $file, '<', $_) { - $p->errsay("#1: can't read #2: #3", $0, $_, $!); - next; - } - } - $h->set_filename($_); - if (m/\.(?:[1-9ln][^.]*|tbl)$/) { - require OpenBSD::Makewhatis::Unformated; - - $subjects = OpenBSD::Makewhatis::Unformated::handle($file, $h); - } elsif (m/\.0$/) { - require OpenBSD::Makewhatis::Formated; - - $subjects = OpenBSD::Makewhatis::Formated::handle($file, $h); - # in test mode, we try harder - } elsif ($p->testmode) { - require OpenBSD::Makewhatis::Unformated; - - $subjects = OpenBSD::Makewhatis::Unformated::handle($file, $h); - if (@$subjects == 0) { - require OpenBSD::Makewhatis::Formated; - - $subjects = OpenBSD::Makewhatis::Formated::handle($file, $h); - } - } else { - $p->errsay("Can't find type of #1", $_); - next; - } - if ($p->picky) { - require OpenBSD::Makewhatis::Check; + my ($list, $p) = @_; + + require OpenBSD::Makewhatis::Subject; + my $h = OpenBSD::Makewhatis::SubjectHandler->new($p); + + for my $_ (@$list) { + my $file; + if (m/\.(?:Z|gz)$/) { + unless (open $file, '-|', "gzip", "-fdc", $_) { + $p->errsay("#1: can't decompress #2: #3", + $0, $_, $!); + next; + } + $_ = $`; + } else { + if (-z $_) { + $p->errsay("Empty file #1", $_); + next; + } + unless (open $file, '<', $_) { + $p->errsay("#1: can't read #2: #3", $0, $_, $!); + next; + } + } + $h->set_filename($_); + if (m/\.(?:[1-9ln][^.]*|tbl)$/) { + require OpenBSD::Makewhatis::Unformated; + + OpenBSD::Makewhatis::Unformated::handle($file, $h); + } elsif (m/\.0$/) { + require OpenBSD::Makewhatis::Formated; + + OpenBSD::Makewhatis::Formated::handle($file, $h); + # in test mode, we try harder + } elsif ($p->testmode) { + require OpenBSD::Makewhatis::Unformated; + + OpenBSD::Makewhatis::Unformated::handle($file, $h); + if ($h->no_subjects) { + require OpenBSD::Makewhatis::Formated; + + OpenBSD::Makewhatis::Formated::handle($file, + $h); + } + } else { + $p->errsay("Can't find type of #1", $_); + next; + } + if ($p->picky) { + require OpenBSD::Makewhatis::Check; - for my $s (@$subjects) { - OpenBSD::Makewhatis::Check::verify_subject($s, $_, $p); + for my $s ($h->subjects) { + OpenBSD::Makewhatis::Check::verify_subject($s, + $_, $p); + } } } - push @$done, @$subjects; - } - return $done; + my @done = $h->subjects; + return \@done; } # build_index($dir, $p) @@ -156,13 +156,13 @@ sub scan_manpages # sub build_index { - require OpenBSD::Makewhatis::Find; - require OpenBSD::Makewhatis::Whatis; + require OpenBSD::Makewhatis::Find; + require OpenBSD::Makewhatis::Whatis; - my ($dir, $p) = @_; - my $list = OpenBSD::Makewhatis::Find::find_manpages($dir); - my $subjects = scan_manpages($list, $p); - OpenBSD::Makewhatis::Whatis::write($subjects, $dir, $p); + my ($dir, $p) = @_; + my $list = OpenBSD::Makewhatis::Find::find_manpages($dir); + my $subjects = scan_manpages($list, $p); + OpenBSD::Makewhatis::Whatis::write($subjects, $dir, $p); } # merge($dir, \@pages, $p) @@ -180,8 +180,8 @@ sub merge my $subjects = scan_manpages($args, $p); if (open(my $old, '<', $whatis)) { while (my $l = <$old>) { - chomp $l; - push(@$subjects, $l); + chomp $l; + push(@$subjects, $l); } close($old); } @@ -222,8 +222,8 @@ sub remove my %remove = map {$_ => 1 } @$subjects; $subjects = []; while (my $l = <$old>) { - chomp $l; - push(@$subjects, $l) unless defined $remove{$l}; + chomp $l; + push(@$subjects, $l) unless defined $remove{$l}; } close($old); OpenBSD::Makewhatis::Whatis::write($subjects, $mandir, $p); @@ -256,37 +256,36 @@ sub makewhatis my ($args, $opts) = @_; my $p = OpenBSD::Makewhatis::Printer->new; if (defined $opts->{'p'}) { - $p->{picky} = 1; + $p->{picky} = 1; } if (defined $opts->{'t'}) { - $p->{testmode} = 1; - my $subjects = scan_manpages($args, $p); - $p->print("#1", join("\n", @$subjects)."\n"); - return; + $p->{testmode} = 1; + my $subjects = scan_manpages($args, $p); + $p->print("#1", join("\n", @$subjects)."\n"); + return; } - if (defined $opts->{'v'}) { $p->{verbose} = 1; } if (defined $opts->{'d'}) { - merge($opts->{'d'}, $args, $p); - return; + merge($opts->{'d'}, $args, $p); + return; } if (defined $opts->{'u'}) { - remove($opts->{'u'}, $args, $p); - return; + remove($opts->{'u'}, $args, $p); + return; } if (@$args == 0) { - $args = default_dirs($p); + $args = default_dirs($p); } for my $mandir (@$args) { - if (-d $mandir) { - build_index($mandir, $p); - } elsif (-e $mandir || $p->picky) { - $p->errsay("#1: #2 is not a directory", $0, $mandir); - } + if (-d $mandir) { + build_index($mandir, $p); + } elsif (-e $mandir || $p->picky) { + $p->errsay("#1: #2 is not a directory", $0, $mandir); + } } } diff --git a/libexec/makewhatis/OpenBSD/Makewhatis/Formated.pm b/libexec/makewhatis/OpenBSD/Makewhatis/Formated.pm index 820391d6910..cd3f1e88455 100644 --- a/libexec/makewhatis/OpenBSD/Makewhatis/Formated.pm +++ b/libexec/makewhatis/OpenBSD/Makewhatis/Formated.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Formated.pm,v 1.6 2011/02/22 00:23:14 espie Exp $ +# $OpenBSD: Formated.pm,v 1.7 2013/01/29 11:08:56 espie Exp $ # Copyright (c) 2000-2004 Marc Espie <espie@openbsd.org> # # Permission to use, copy, modify, and distribute this software for any @@ -18,15 +18,14 @@ use strict; use warnings; package OpenBSD::Makewhatis::Formated; -# add_formated_subject($subjects, $_, $section, $h): -# add subject $_ to the list of current $subjects, in section $section. +# add_formated_subject($_, $section, $h): +# add subject $_ to the list of current subjects in $h, in section $section. # sub add_formated_subject { - my ($subjects, $line, $section, $h) = @_; - my $_ = $line; + my ($_, $section, $h) = @_; - if (m/-/) { + if (m/\-/) { s/([-+.\w\d,])\s+/$1 /g; s/([a-z][A-z])-\s+/$1/g; # some twits use: func -- description @@ -37,8 +36,7 @@ sub add_formated_subject if (length($func) > 40 && $func =~ m/,/ && $section =~ /^3/) { $func =~ s/\b \b//g; } - $_ = "$func ($section) - $descr"; - push(@$subjects, $_); + $h->add("$func ($section) - $descr"); return; } } @@ -50,15 +48,14 @@ sub add_formated_subject my ($func, $descr) = ($1, $2); $func =~ s/\s+/ /g; $descr =~ s/\s+/ /g; - $_ = "$func ($section) - $descr"; - push(@$subjects, $_); + $h->add("$func ($section) - $descr"); return; } $h->weird_subject($_) unless $h->p->picky; } -# $lines = handle($file, $h) +# handle($file, $h) # # handle a formatted manpage in $file # @@ -69,15 +66,13 @@ sub handle my ($file, $h) = @_; my $_; 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, $h) - if defined $subject; + add_formated_subject($subject, $section, $h) if defined $subject; $subject = undef; next; } @@ -112,7 +107,7 @@ sub handle } if ($foundname) { if (m/^\S/ || m/^\s+\*{3,}\s*$/) { - add_formated_subject(\@lines, $subject, $section, $h) + add_formated_subject($subject, $section, $h) if defined $subject; last; } else { @@ -132,8 +127,7 @@ sub handle } } - $h->cant_find_subject if @lines == 0; - return \@lines; + $h->cant_find_subject if $h->no_subjects; } 1; diff --git a/libexec/makewhatis/OpenBSD/Makewhatis/Subject.pm b/libexec/makewhatis/OpenBSD/Makewhatis/Subject.pm index d349317d47c..37e1394360e 100644 --- a/libexec/makewhatis/OpenBSD/Makewhatis/Subject.pm +++ b/libexec/makewhatis/OpenBSD/Makewhatis/Subject.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Subject.pm,v 1.2 2011/02/22 00:23:14 espie Exp $ +# $OpenBSD: Subject.pm,v 1.3 2013/01/29 11:08:56 espie Exp $ # Copyright (c) 2010 Marc Espie <espie@openbsd.org> # # Permission to use, copy, modify, and distribute this software for any @@ -22,11 +22,26 @@ package OpenBSD::Makewhatis::SubjectHandler; sub new { my ($class, $p) = @_; - return bless { p => $p}, $class; + return bless { p => $p, subjects => []}, $class; } sub add { + my ($h, $s) = @_; + push(@{$h->{subjects}}, $s); + $h->{has_subjects} = 1; +} + +sub no_subjects +{ + my $h = shift; + return !$h->{has_subjects}; +} + +sub subjects +{ + my $h = shift; + return @{$h->{subjects}}; } sub p diff --git a/libexec/makewhatis/OpenBSD/Makewhatis/Unformated.pm b/libexec/makewhatis/OpenBSD/Makewhatis/Unformated.pm index 29cc9dbe69a..21ecacbdbeb 100644 --- a/libexec/makewhatis/OpenBSD/Makewhatis/Unformated.pm +++ b/libexec/makewhatis/OpenBSD/Makewhatis/Unformated.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Unformated.pm,v 1.8 2011/06/24 10:40:18 schwarze Exp $ +# $OpenBSD: Unformated.pm,v 1.9 2013/01/29 11:08:56 espie Exp $ # Copyright (c) 2000-2004 Marc Espie <espie@openbsd.org> # # Permission to use, copy, modify, and distribute this software for any @@ -18,14 +18,14 @@ use strict; use warnings; package OpenBSD::Makewhatis::Unformated; -# add_unformated_subject($lines, $toadd, $section, $p) : +# add_unformated_subject($toadd, $section, $toexpand, $h) : # # build subject from list of $toadd lines, and add it to the list # of current subjects as section $section # sub add_unformated_subject { - my ($subjects, $toadd, $section, $toexpand, $h) = @_; + my ($toadd, $section, $toexpand, $h) = @_; my $exp = sub { if (defined $toexpand->{$_[0]}) { @@ -62,11 +62,11 @@ sub add_unformated_subject # fine space adjustments while (s/\\[vh]\'.*?\'//g) {} - unless (s/\s+\\-\s+/ ($section) - / || s/\s*\\\-/ ($section) -/ || + unless (s/\s+\\-\s+/ ($section) - / || s/\s?\\\-/ ($section) -/ || s/\s-\s/ ($section) - /) { $h->weird_subject($_) if $h->p->picky; # Try guessing where the separation falls... - s/\s+\:\s+/ ($section) - / || s/\S+\s+/$& ($section) - / || s/\s*$/ ($section) - (empty subject)/; + s/\s+\:\s+/ ($section) - / || s/\S+\s/$& ($section) - / || s/$/ ($section) - (empty subject)/; } # other dashes s/\\-/-/g; @@ -84,10 +84,10 @@ sub add_unformated_subject $h->weird_subject($_) if $h->p->picky; return; } - push(@$subjects, $_); + $h->add($_); } -# $lines = handle($file, $h) +# handle($file, $h) # # handle an unformated manpage in $file # @@ -96,7 +96,6 @@ sub add_unformated_subject sub handle { my ($f, $h) = @_; - my @lines = (); my %toexpand = (Na => 'NaN', Tm => '(tm)'); my $so_found = 0; my $found_th = 0; @@ -153,7 +152,7 @@ sub handle last if m/^\.\s*(?:SH|sh|SS|ss|nf|LI)/; # several subjects in one manpage if (m/^\.\s*(?:PP|Pp|br|PD|LP|sp)/) { - add_unformated_subject(\@lines, \@subject, + add_unformated_subject(\@subject, $section, \%toexpand, $h) if @subject != 0; @subject = (); next; @@ -167,8 +166,7 @@ sub handle chomp; s/\.\s*(?:B|I|IR|SM|BR)\s+//; if (m/^\.\s*(\S\S)/) { - $h->errsay("#2: not grokking #1", $_) - if $h->p->picky; + $h->errsay("#2: not grokking #1", $_) if $h->p->picky; next; } push(@subject, $_) unless m/^\s*$/; @@ -187,7 +185,7 @@ sub handle $macro eq 'Nx' and s/^/NetBSD /; if ($macro eq 'Nd') { if (@keep != 0) { - add_unformated_subject(\@lines, \@keep, + add_unformated_subject(\@keep, $section, \%toexpand, $h); @keep = (); } @@ -204,18 +202,17 @@ sub handle } } if ($found_th && !$found_old) { - $h->cant_find_subject; + $h->cant_find_subject; } if ($found_dt && !$found_new) { - $h->cant_find_subject; + $h->cant_find_subject; } unshift(@subject, @keep) if @keep != 0; - add_unformated_subject(\@lines, \@subject, $section, + add_unformated_subject(\@subject, $section, \%toexpand, $h) if @subject != 0; if (!$so_found && !$found_old && !$found_new) { $h->errsay("Unknown manpage type #1"); } - return \@lines; } 1; diff --git a/libexec/makewhatis/makewhatis b/libexec/makewhatis/makewhatis index 8ba8a48f384..bc9f6fe5cc2 100644 --- a/libexec/makewhatis/makewhatis +++ b/libexec/makewhatis/makewhatis @@ -1,7 +1,7 @@ #!/usr/bin/perl -w # ex:ts=8 sw=4: -# $OpenBSD: makewhatis,v 1.2 2011/02/22 00:23:14 espie Exp $ +# $OpenBSD: makewhatis,v 1.3 2013/01/29 11:08:55 espie Exp $ # Copyright (c) 2000-2004 Marc Espie <espie@openbsd.org> # # Permission to use, copy, modify, and distribute this software for any |