diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2010-07-09 08:12:50 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2010-07-09 08:12:50 +0000 |
commit | 0ccdd07ea4d7f703fde8c7e6f1d35e3ff5b3f20f (patch) | |
tree | 0ef81e037cac6fe334c4c100aef0897baa0ad438 | |
parent | 54971de09b287ab807700078f548b926a7a1e30d (diff) |
don't print directly, use an interface that's similar enough to pkg_add
print framework so that we'll be able to integrate with it better.
ok millert@
-rw-r--r-- | libexec/makewhatis/OpenBSD/Makewhatis.pm | 164 | ||||
-rw-r--r-- | libexec/makewhatis/OpenBSD/Makewhatis/Check.pm | 15 | ||||
-rw-r--r-- | libexec/makewhatis/OpenBSD/Makewhatis/Formated.pm | 28 | ||||
-rw-r--r-- | libexec/makewhatis/OpenBSD/Makewhatis/Unformated.pm | 38 | ||||
-rw-r--r-- | libexec/makewhatis/OpenBSD/Makewhatis/Whatis.pm | 6 |
5 files changed, 153 insertions, 98 deletions
diff --git a/libexec/makewhatis/OpenBSD/Makewhatis.pm b/libexec/makewhatis/OpenBSD/Makewhatis.pm index 2f98021f61a..aaa922e7458 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.6 2007/08/22 15:50:05 espie Exp $ +# $OpenBSD: Makewhatis.pm,v 1.7 2010/07/09 08:12:49 espie Exp $ # Copyright (c) 2000-2004 Marc Espie <espie@openbsd.org> # # Permission to use, copy, modify, and distribute this software for any @@ -17,62 +17,119 @@ use strict; use warnings; -package OpenBSD::Makewhatis; +# used to print everything. People using makewhatis internally can +# override this + +package OpenBSD::Makewhatis::Printer; +sub new +{ + my $class = shift; + return bless {}, $class; +} + +sub print +{ + my $self = shift; + print $self->f(@_); +} + +sub errsay +{ + my $self = shift; + print STDERR $self->f(@_), "\n"; +} + +sub fatal +{ + my $self = shift; + die $self->f(@_); +} + +sub f +{ + my $self = shift; + if (@_ == 0) { + return ''; + } + my $_ = shift; + # make it so that #0 is # + unshift(@_, '#'); + s/\#(\d+)/$_[$1]/ge; + return $_; +} -my ($picky, $testmode); +sub picky +{ + return shift->{picky}; +} +sub testmode +{ + return shift->{testmode}; +} -# $subjects = scan_manpages(\@list) +sub check_dir +{ + my ($self, $dir) = @_; + unless (-d $dir) { + $self->fatal("#1: #2 is not a directory", $0, $dir); + } +} + +package OpenBSD::Makewhatis; + + +# $subjects = scan_manpages(\@list, $p) # # scan a set of manpages, return list of subjects # -sub scan_manpages($) +sub scan_manpages { - my $list = shift; - local $_; + my ($list, $p) = @_; + my $_; my $done=[]; for (@$list) { my ($file, $subjects); if (m/\.(?:Z|gz)$/) { unless (open $file, '-|', "gzip -fdc $_") { - warn "$0: Can't decompress $_\n"; + $p->errsay("#1: can't decompress #2: #3", $0, $_, $!); next; } $_ = $`; } else { unless (open $file, '<', $_) { - warn "$0: Can't read $_\n"; + $p->errsay("#1: can't read #2: #3", $0, $_, $!); next; } } if (m/\.(?:[1-9ln][^.]*|tbl)$/) { require OpenBSD::Makewhatis::Unformated; - $subjects = OpenBSD::Makewhatis::Unformated::handle($file, $_); + $subjects = OpenBSD::Makewhatis::Unformated::handle($file, $_, $p); } elsif (m/\.0$/) { require OpenBSD::Makewhatis::Formated; - $subjects = OpenBSD::Makewhatis::Formated::handle($file, $_); + $subjects = OpenBSD::Makewhatis::Formated::handle($file, $_, $p); # in test mode, we try harder - } elsif ($testmode) { + } elsif ($p->testmode) { require OpenBSD::Makewhatis::Unformated; - $subjects = OpenBSD::Makewhatis::Unformated::handle($file, $_); + $subjects = OpenBSD::Makewhatis::Unformated::handle($file, $_, $p); if (@$subjects == 0) { require OpenBSD::Makewhatis::Formated; - $subjects = OpenBSD::Makewhatis::Formated::handle($file, $_); + $subjects = OpenBSD::Makewhatis::Formated::handle($file, $_, $p); } } else { - print STDERR "Can't find type of $_\n"; + $p->errsay("Can't find type of #1", $_); next; } - if ($picky) { + if ($p->picky) { require OpenBSD::Makewhatis::Check; for my $s (@$subjects) { - OpenBSD::Makewhatis::Check::verify_subject($s, $_); + OpenBSD::Makewhatis::Check::verify_subject($s, $_, $p); } } push @$done, @$subjects; @@ -80,36 +137,34 @@ sub scan_manpages($) return $done; } -# build_index($dir) +# build_index($dir, $p) # # build index for $dir # -sub build_index($) +sub build_index { require OpenBSD::Makewhatis::Find; require OpenBSD::Makewhatis::Whatis; - my $dir = shift; + my ($dir, $p) = @_; my $list = OpenBSD::Makewhatis::Find::find_manpages($dir); - my $subjects = scan_manpages($list); - OpenBSD::Makewhatis::Whatis::write($subjects, $dir); + my $subjects = scan_manpages($list, $p); + OpenBSD::Makewhatis::Whatis::write($subjects, $dir, $p); } -# merge($dir, \@pages) +# merge($dir, \@pages, $p) # # merge set of pages into directory index # -sub merge($$) +sub merge { require OpenBSD::Makewhatis::Whatis; - my ($mandir, $args) = @_; - - unless (-d $mandir) { - die "$0: $mandir: not a directory" - } + my ($mandir, $args, $p) = @_; + $p //= OpenBSD::Makewhatis::Printer->new; + $p->check_dir($mandir); my $whatis = "$mandir/whatis.db"; - my $subjects = scan_manpages($args); + my $subjects = scan_manpages($args, $p); if (open(my $old, '<', $whatis)) { while (my $l = <$old>) { chomp $l; @@ -117,25 +172,24 @@ sub merge($$) } close($old); } - OpenBSD::Makewhatis::Whatis::write($subjects, $mandir); + OpenBSD::Makewhatis::Whatis::write($subjects, $mandir, $p); } -# remove(dir, \@pages) +# remove(dir, \@pages, $p) # # remove set of pages from directory index # -sub remove($$) +sub remove { require OpenBSD::Makewhatis::Whatis; - my ($mandir, $args) = @_; - unless (-d $mandir) { - die "$0: $mandir: not a directory" - } + my ($mandir, $args, $p) = @_; + $p //= OpenBSD::Makewhatis::Printer->new; + $p->check_dir($mandir); my $whatis = "$mandir/whatis.db"; open(my $old, '<', $whatis) or - die "$0: can't open $whatis to merge with: $!"; - my $subjects = scan_manpages($args); + $p->fatal("#1: can't open #2 to merge with: #3", $0, $whatis, $!); + my $subjects = scan_manpages($args, $p); my %remove = map {$_ => 1 } @$subjects; $subjects = []; while (my $l = <$old>) { @@ -143,18 +197,19 @@ sub remove($$) push(@$subjects, $l) unless defined $remove{$l}; } close($old); - OpenBSD::Makewhatis::Whatis::write($subjects, $mandir); + OpenBSD::Makewhatis::Whatis::write($subjects, $mandir, $p); } -# $dirs = default_dirs() +# $dirs = default_dirs($p) # # read list of default directories from man.conf # -sub default_dirs() +sub default_dirs { + my $p = shift; my $args=[]; open(my $conf, '<', '/etc/man.conf') or - die "$0: Can't open /etc/man.conf"; + $p->fatal("#1: can't open #2: #3", $0, "/etc/man.conf", $!); while (my $l = <$conf>) { chomp $l; push(@$args, $1) if $l =~ m/^_whatdb\s+(.*)\/whatis\.db\s*$/; @@ -167,36 +222,37 @@ sub default_dirs() # # glue for front-end, see makewhatis(8) # -sub makewhatis($$) +sub makewhatis { my ($args, $opts) = @_; + my $p = OpenBSD::Makewhatis::Printer->new; if (defined $opts->{'p'}) { - $picky = 1; + $p->{picky} = 1; } if (defined $opts->{'t'}) { - $testmode = 1; - my $subjects = scan_manpages($args); - print join("\n", @$subjects), "\n"; + $p->{testmode} = 1; + my $subjects = scan_manpages($args, $p); + $p->print("#1", join("\n", @$subjects)."\n"); return; } if (defined $opts->{'d'}) { - merge($opts->{'d'}, $args); + merge($opts->{'d'}, $args, $p); return; } if (defined $opts->{'u'}) { - remove($opts->{'u'}, $args); + remove($opts->{'u'}, $args, $p); return; } if (@$args == 0) { - $args = default_dirs(); + $args = default_dirs($p); } for my $mandir (@$args) { if (-d $mandir) { - build_index($mandir); - } elsif (-e $mandir || $picky) { - print STDERR "$0: $mandir is not a directory\n"; + 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/Check.pm b/libexec/makewhatis/OpenBSD/Makewhatis/Check.pm index 96e7a574e76..7bdaefca6c9 100644 --- a/libexec/makewhatis/OpenBSD/Makewhatis/Check.pm +++ b/libexec/makewhatis/OpenBSD/Makewhatis/Check.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Check.pm,v 1.2 2005/03/05 11:02:35 espie Exp $ +# $OpenBSD: Check.pm,v 1.3 2010/07/09 08:12:49 espie Exp $ # Copyright (c) 2000-2004 Marc Espie <espie@openbsd.org> # # Permission to use, copy, modify, and distribute this software for any @@ -18,7 +18,7 @@ use strict; use warnings; package OpenBSD::Makewhatis::Check; -sub found($$) +sub found { my ($pattern, $filename) = @_; my @candidates = glob $pattern; @@ -42,14 +42,13 @@ sub found($$) } return 0; } -# verify_subject($subject, $filename): +# verify_subject($subject, $filename, $p): # # reparse the subject we're about to add, and check whether it makes # sense, e.g., is there a man page around. -sub verify_subject($$) +sub verify_subject { - local $_ = shift; - my $filename = shift; + my ($_, $filename, $p) = @_; if (m/\s*(.*?)\s*\((.*?)\)\s-\s/) { my $man = $1; my $section = $2; @@ -75,8 +74,8 @@ sub verify_subject($$) push(@notfound, $func); } if (@notfound > 0) { - print STDERR "Couldn't find ", join(', ', @notfound), - " in $filename:\n$_\n" + $p->errsay("Couldn't find #1 in #2:\n#3", + join(', ', @notfound), $filename, $_); } } } diff --git a/libexec/makewhatis/OpenBSD/Makewhatis/Formated.pm b/libexec/makewhatis/OpenBSD/Makewhatis/Formated.pm index 1b6c850a0bf..8a35adb59c2 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.4 2009/10/11 08:14:37 sthen Exp $ +# $OpenBSD: Formated.pm,v 1.5 2010/07/09 08:12:49 espie Exp $ # Copyright (c) 2000-2004 Marc Espie <espie@openbsd.org> # # Permission to use, copy, modify, and distribute this software for any @@ -18,13 +18,13 @@ use strict; use warnings; package OpenBSD::Makewhatis::Formated; -# add_formated_subject($subjects, $_, $section, $filename, $picky): +# add_formated_subject($subjects, $_, $section, $filename, $p): # add subject $_ to the list of current $subjects, in section $section. # sub add_formated_subject { - my ($subjects, $line, $section, $filename, $picky) = @_; - local $_ = $line; + my ($subjects, $line, $section, $filename, $p) = @_; + my $_ = $line; if (m/-/) { s/([-+.\w\d,])\s+/$1 /g; @@ -43,7 +43,7 @@ sub add_formated_subject } } - print STDERR "Weird subject line in $filename:\n$_\n" if $picky; + $p->errsay("Weird subject line in #1:\n#2", $filename, $_) if $p->picky; # try to find subject in line anyway if (m/^\s*(.*\S)(?:\s{3,}|\(\)\s+)(.*?)\s*$/) { @@ -55,10 +55,10 @@ sub add_formated_subject return; } - print STDERR "Weird subject line in $filename:\n$_\n" unless $picky; + $p->errsay("Weird subject line in #1:\n#2", $filename, $_) unless $p->picky; } -# $lines = handle($file, $filename, $picky) +# $lines = handle($file, $filename, $p) # # handle a formatted manpage in $file # @@ -66,8 +66,8 @@ sub add_formated_subject # sub handle { - my ($file, $filename, $picky) = @_; - local $_; + my ($file, $filename, $p) = @_; + my $_; my ($section, $subject); my @lines=(); my $foundname = 0; @@ -76,7 +76,7 @@ sub handle 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) + add_formated_subject(\@lines, $subject, $section, $filename, $p) if defined $subject; $subject = undef; next; @@ -101,10 +101,10 @@ sub handle # 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; + $p->errsay("Can't find section in #1, deducting #2 from context", $filename, $section) if $p->picky; } else { $section='??'; - print STDERR "Can't find section in $filename\n"; + $p->errsay("Can't find section in #1", $filename); } } $foundname = 1; @@ -112,7 +112,7 @@ sub handle } if ($foundname) { if (m/^\S/ || m/^\s+\*{3,}\s*$/) { - add_formated_subject(\@lines, $subject, $section, $filename, $picky) + add_formated_subject(\@lines, $subject, $section, $filename, $p) if defined $subject; last; } else { @@ -132,7 +132,7 @@ sub handle } } - print STDERR "Can't parse $filename (not a manpage ?)\n" if @lines == 0; + $p->errsay("Can't parse #1 (not a manpage ?)", $filename) if @lines == 0; return \@lines; } diff --git a/libexec/makewhatis/OpenBSD/Makewhatis/Unformated.pm b/libexec/makewhatis/OpenBSD/Makewhatis/Unformated.pm index bfd2fc434e6..e01751e2f38 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.4 2009/12/24 14:08:20 espie Exp $ +# $OpenBSD: Unformated.pm,v 1.5 2010/07/09 08:12:49 espie Exp $ # Copyright (c) 2000-2004 Marc Espie <espie@openbsd.org> # # Permission to use, copy, modify, and distribute this software for any @@ -18,25 +18,25 @@ use strict; use warnings; package OpenBSD::Makewhatis::Unformated; -# add_unformated_subject($lines, $toadd, $section, $filename, $toexpand): +# add_unformated_subject($lines, $toadd, $section, $filename, $toexpand, $p) : # # 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, $filename, $toexpand, $picky) = @_; + my ($subjects, $toadd, $section, $filename, $toexpand, $p) = @_; my $exp = sub { if (defined $toexpand->{$_[0]}) { return $toexpand->{$_[0]}; } else { - print STDERR "$filename: can't expand $_[0]\n"; + $p->errsay("#1: can't expand #2", $filename, $_[0]); return ""; } }; - local $_ = join(' ', @$toadd); + my $_ = join(' ', @$toadd); # do interpolations s/\\\*\((..)/&$exp($1)/ge; s/\\\*\[(.*?)\]/&$exp($1)/ge; @@ -64,7 +64,7 @@ sub add_unformated_subject {} unless (s/\s+\\-\s+/ ($section) - / || s/\s*\\\-/ ($section) -/ || s/\s-\s/ ($section) - /) { - print STDERR "Weird subject line in $filename:\n$_\n" if $picky; + $p->errsay("Weird subject line in #1:\n#2", $filename, $_) if $p->picky; # Try guessing where the separation falls... s/\s+\:\s+/ ($section) - / || s/\S+\s+/$& ($section) - / || s/\s*$/ ($section) - (empty subject)/; } @@ -81,14 +81,14 @@ sub add_unformated_subject s/\s+/ /g; # some damage control if (m/^\Q($section) - \E/) { - print STDERR "Rejecting non-subject line from $filename:\n$_\n" - if $picky; + $p->errsay("Rejecting non-subject line from #1:\n#2", $filename, $_) + if $p->picky; return; } push(@$subjects, $_); } -# $lines = handle($file, $filename, $picky) +# $lines = handle($file, $filename, $p) # # handle an unformated manpage in $file # @@ -96,7 +96,7 @@ sub add_unformated_subject # sub handle { - my ($f, $filename, $picky) = @_; + my ($f, $filename, $p) = @_; my @lines = (); my %toexpand = (); my $so_found = 0; @@ -108,7 +108,7 @@ sub handle my @subject = (); my @keep = (); my $nd_seen = 0; - local $_; + my $_; # retrieve basename of file my ($name, $section) = $filename =~ m|(?:.*/)?(.*)\.([\w\d]+)|; # scan until macro @@ -155,7 +155,7 @@ sub handle # several subjects in one manpage if (m/^\.\s*(?:PP|Pp|br|PD|LP|sp)/) { add_unformated_subject(\@lines, \@subject, - $section, $filename, \%toexpand, $picky) + $section, $filename, \%toexpand, $p) if @subject != 0; @subject = (); next; @@ -169,8 +169,8 @@ sub handle chomp; s/\.\s*(?:B|I|IR|SM|BR)\s+//; if (m/^\.\s*(\S\S)/) { - print STDERR "$filename: not grokking $_\n" - if $picky; + $p->errsay("#1: not grokking #2", $filename, $_) + if $p->picky; next; } push(@subject, $_) unless m/^\s*$/; @@ -190,7 +190,7 @@ sub handle if ($macro eq 'Nd') { if (@keep != 0) { add_unformated_subject(\@lines, \@keep, - $section, $filename, \%toexpand, $picky); + $section, $filename, \%toexpand, $p); @keep = (); } push(@subject, "\\-"); @@ -206,16 +206,16 @@ sub handle } } if ($found_th && !$found_old) { - print STDERR "Couldn't find subject in old manpage $filename\n"; + $p->errsay("Couldn't find subject in old manpage #1", $filename); } if ($found_dt && !$found_new) { - print STDERR "Couldn't find subject in new manpage $filename\n"; + $p->errsay("Couldn't find subject in new manpage #1", $filename); } unshift(@subject, @keep) if @keep != 0; add_unformated_subject(\@lines, \@subject, $section, - $filename, \%toexpand, $picky) if @subject != 0; + $filename, \%toexpand, $p) if @subject != 0; if (!$so_found && !$found_old && !$found_new) { - print STDERR "Unknown manpage type $filename\n"; + $p->errsay("Unknown manpage type #1", $filename); } return \@lines; } diff --git a/libexec/makewhatis/OpenBSD/Makewhatis/Whatis.pm b/libexec/makewhatis/OpenBSD/Makewhatis/Whatis.pm index 308e7c7a9ab..8c31fa3d5e1 100644 --- a/libexec/makewhatis/OpenBSD/Makewhatis/Whatis.pm +++ b/libexec/makewhatis/OpenBSD/Makewhatis/Whatis.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Whatis.pm,v 1.3 2005/03/05 11:02:35 espie Exp $ +# $OpenBSD: Whatis.pm,v 1.4 2010/07/09 08:12:49 espie Exp $ # Copyright (c) 2000-2004 Marc Espie <espie@openbsd.org> # # Permission to use, copy, modify, and distribute this software for any @@ -31,7 +31,7 @@ use File::Compare; # sub write { - my ($list, $dir) = @_; + my ($list, $dir, $p) = @_; my $f = "$dir/whatis.db"; my ($out, $tempname); @@ -56,7 +56,7 @@ sub write chmod 0444, $f; chown 0, (getgrnam 'bin')[2], $f; } else { - print STDERR "$0: Can't create $f ($!)\n"; + $p->errsay("#1: Can't create #2: #3", $0, $f, $!); unlink($tempname); exit 1; } |