3 # $Id: Info.pm,v 1.2 2004/04/08 01:52:19 mdw Exp $
5 # Manipulation and reading of Info files
10 #----- Licensing notice -----------------------------------------------------
12 # This file is part of sw-tools.
14 # sw-tools is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
19 # sw-tools is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with sw-tools; if not, write to the Free Software Foundation,
26 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #----- Package preamble -----------------------------------------------------
35 @EXPORT_OK = qw(setpath
);
37 #----- Low-level twiddling --------------------------------------------------
39 @infopath = ("/usr/info");
41 # --- @setpath(PATH...)@ ---
43 # Sets the Info search path.
49 # --- @getname(INFO)@ ---
51 # Given the name of an Info manual, find the actual file.
57 foreach my $p (@infopath) {
60 foreach my $suff ("", "-info", ".info") {
61 return $f . $suff if -r
$f . $suff;
62 return $f . $suff . ".gz" if -r
$f . $suff . ".gz";
68 # --- @snarf(FILE)@ ---
70 # Snarf a file into a string, given its name. Handles compressed files.
78 my $p = IO
::Pipe
->new();
80 defined($kid) or return undef;
83 dup2
($p->fileno(), 1);
84 exec("gzip", "-dc", $f);
88 $snarf = $p->getline();
92 my $fh = IO
::File
->new($f, O_RDONLY
) or return undef;
93 $snarf = $fh->getline();
99 #----- An Info-file object --------------------------------------------------
101 # --- @node(NAME)@ ---
103 # Picks an individual node out of an Info file.
106 my ($me, $node) = @_;
111 # --- If there's an index, it will help me find the node ---
114 $offset = $me->{index}{lc($node)};
116 # --- Maybe the offset is into a different file ---
120 PAIR
: foreach my $pair (@
{$me->{indir
}}) {
121 if ($pair->[0] <= $offset) {
122 ($loff, $file) = @
$pair;
127 return undef unless $file;
132 # --- Fetch the file ---
137 $fn = "$me->{dir}/$file", -r
$fn or
138 $fn = "$me->{dir}/$file.gz", -r
$fn or
141 if ($me->{cache
}{$fn}) {
142 $file = $me->{cache
}{$fn};
144 $file = $me->{cache
}{$fn} = snarf
($fn) or return undef;
150 # --- Dig through the file to find the right node ---
154 if ($file =~ / \G
.*\1f\n
162 $offset = 0, next GASP
if $offset;
169 # --- @load(NAME)@ ---
171 # Loads a file into an Info object.
174 my ($me, $file) = @_;
175 my $f = getname
($file) or return undef;
176 my $c = snarf
($f) or return undef;
178 # --- Read the index, and maybe snarf in the indirection file ---
180 if (my ($index) = ($c =~ /\1f\nTag Table:\n([^\1f]*)\1f\nEnd Tag Table\n/s)) {
182 while ($index =~ /Node: *([^\n\7f]*)\7f(\d+)\n/sg) { $index{lc($1)} = $2; }
183 $me->{index} = \
%index;
184 if ($index =~ /^\(Indirect\)/ and
185 my ($indir) = ($c =~ /\1f\nIndirect:\n([^\1f]*)\1f\n/s)) {
187 while ($indir =~ /([^\n:]*): *(\d+)\n/sg) { push(@indir, [$2, $1]); }
188 $me->{indir
} = \
@indir;
192 ($me->{dir
} = $f) =~ s
:/[^/]*$::;
197 # --- @new([NAME])@ ---
199 # Makes a new Info file and returns it to the caller.
202 my ($class, $file) = @_;
203 my $me = bless {}, $class;
204 return $me->load($file) if $file;
208 #----- That's all, folks ----------------------------------------------------