xref: /netbsd-src/crypto/external/bsd/openssl.old/dist/util/perl/OpenSSL/Util/Pod.pm (revision 4724848cf0da353df257f730694b7882798e5daf)
1*4724848cSchristos# Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
2*4724848cSchristos#
3*4724848cSchristos# Licensed under the OpenSSL license (the "License").  You may not use
4*4724848cSchristos# this file except in compliance with the License.  You can obtain a copy
5*4724848cSchristos# in the file LICENSE in the source distribution or at
6*4724848cSchristos# https://www.openssl.org/source/license.html
7*4724848cSchristos
8*4724848cSchristospackage OpenSSL::Util::Pod;
9*4724848cSchristos
10*4724848cSchristosuse strict;
11*4724848cSchristosuse warnings;
12*4724848cSchristos
13*4724848cSchristosuse Exporter;
14*4724848cSchristosuse vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
15*4724848cSchristos$VERSION = "0.1";
16*4724848cSchristos@ISA = qw(Exporter);
17*4724848cSchristos@EXPORT = qw(extract_pod_info);
18*4724848cSchristos@EXPORT_OK = qw();
19*4724848cSchristos
20*4724848cSchristos=head1 NAME
21*4724848cSchristos
22*4724848cSchristosOpenSSL::Util::Pod - utilities to manipulate .pod files
23*4724848cSchristos
24*4724848cSchristos=head1 SYNOPSIS
25*4724848cSchristos
26*4724848cSchristos  use OpenSSL::Util::Pod;
27*4724848cSchristos
28*4724848cSchristos  my %podinfo = extract_pod_info("foo.pod");
29*4724848cSchristos
30*4724848cSchristos  # or if the file is already opened...  Note that this consumes the
31*4724848cSchristos  # remainder of the file.
32*4724848cSchristos
33*4724848cSchristos  my %podinfo = extract_pod_info(\*STDIN);
34*4724848cSchristos
35*4724848cSchristos=head1 DESCRIPTION
36*4724848cSchristos
37*4724848cSchristos=over
38*4724848cSchristos
39*4724848cSchristos=item B<extract_pod_info "FILENAME", HASHREF>
40*4724848cSchristos
41*4724848cSchristos=item B<extract_pod_info "FILENAME">
42*4724848cSchristos
43*4724848cSchristos=item B<extract_pod_info GLOB, HASHREF>
44*4724848cSchristos
45*4724848cSchristos=item B<extract_pod_info GLOB>
46*4724848cSchristos
47*4724848cSchristosExtracts information from a .pod file, given a STRING (file name) or a
48*4724848cSchristosGLOB (a file handle).  The result is given back as a hash table.
49*4724848cSchristos
50*4724848cSchristosThe additional hash is for extra parameters:
51*4724848cSchristos
52*4724848cSchristos=over
53*4724848cSchristos
54*4724848cSchristos=item B<section =E<gt> N>
55*4724848cSchristos
56*4724848cSchristosThe value MUST be a number, and will be the man section number
57*4724848cSchristosto be used with the given .pod file.
58*4724848cSchristos
59*4724848cSchristos=item B<debug =E<gt> 0|1>
60*4724848cSchristos
61*4724848cSchristosIf set to 1, extra debug text will be printed on STDERR
62*4724848cSchristos
63*4724848cSchristos=back
64*4724848cSchristos
65*4724848cSchristos=back
66*4724848cSchristos
67*4724848cSchristos=head1 RETURN VALUES
68*4724848cSchristos
69*4724848cSchristos=over
70*4724848cSchristos
71*4724848cSchristos=item B<extract_pod_info> returns a hash table with the following
72*4724848cSchristositems:
73*4724848cSchristos
74*4724848cSchristos=over
75*4724848cSchristos
76*4724848cSchristos=item B<section =E<gt> N>
77*4724848cSchristos
78*4724848cSchristosThe man section number this .pod file belongs to.  Often the same as
79*4724848cSchristoswas given as input.
80*4724848cSchristos
81*4724848cSchristos=item B<names =E<gt> [ "name", ... ]>
82*4724848cSchristos
83*4724848cSchristosAll the names extracted from the NAME section.
84*4724848cSchristos
85*4724848cSchristos=back
86*4724848cSchristos
87*4724848cSchristos=back
88*4724848cSchristos
89*4724848cSchristos=cut
90*4724848cSchristos
91*4724848cSchristossub extract_pod_info {
92*4724848cSchristos    my $input = shift;
93*4724848cSchristos    my $defaults_ref = shift || {};
94*4724848cSchristos    my %defaults = ( debug => 0, section => 0, %$defaults_ref );
95*4724848cSchristos    my $fh = undef;
96*4724848cSchristos    my $filename = undef;
97*4724848cSchristos
98*4724848cSchristos    # If not a file handle, then it's assume to be a file path (a string)
99*4724848cSchristos    unless (ref $input eq "GLOB") {
100*4724848cSchristos        $filename = $input;
101*4724848cSchristos        open $fh, $input or die "Trying to read $filename: $!\n";
102*4724848cSchristos        print STDERR "DEBUG: Reading $input\n" if $defaults{debug};
103*4724848cSchristos        $input = $fh;
104*4724848cSchristos    }
105*4724848cSchristos
106*4724848cSchristos    my %podinfo = ( section => $defaults{section});
107*4724848cSchristos    while(<$input>) {
108*4724848cSchristos        s|\R$||;
109*4724848cSchristos        # Stop reading when we have reached past the NAME section.
110*4724848cSchristos        last if (m|^=head1|
111*4724848cSchristos                 && defined $podinfo{lastsect}
112*4724848cSchristos                 && $podinfo{lastsect} eq "NAME");
113*4724848cSchristos
114*4724848cSchristos        # Collect the section name
115*4724848cSchristos        if (m|^=head1\s*(.*)|) {
116*4724848cSchristos            $podinfo{lastsect} = $1;
117*4724848cSchristos            $podinfo{lastsect} =~ s/\s+$//;
118*4724848cSchristos            print STDERR "DEBUG: Found new pod section $1\n"
119*4724848cSchristos                if $defaults{debug};
120*4724848cSchristos            print STDERR "DEBUG: Clearing pod section text\n"
121*4724848cSchristos                if $defaults{debug};
122*4724848cSchristos            $podinfo{lastsecttext} = "";
123*4724848cSchristos        }
124*4724848cSchristos
125*4724848cSchristos        next if (m|^=| || m|^\s*$|);
126*4724848cSchristos
127*4724848cSchristos        # Collect the section text
128*4724848cSchristos        print STDERR "DEBUG: accumulating pod section text \"$_\"\n"
129*4724848cSchristos            if $defaults{debug};
130*4724848cSchristos        $podinfo{lastsecttext} .= " " if $podinfo{lastsecttext};
131*4724848cSchristos        $podinfo{lastsecttext} .= $_;
132*4724848cSchristos    }
133*4724848cSchristos
134*4724848cSchristos
135*4724848cSchristos    if (defined $fh) {
136*4724848cSchristos        close $fh;
137*4724848cSchristos        print STDERR "DEBUG: Done reading $filename\n" if $defaults{debug};
138*4724848cSchristos    }
139*4724848cSchristos
140*4724848cSchristos    $podinfo{lastsecttext} =~ s| - .*$||;
141*4724848cSchristos
142*4724848cSchristos    my @names =
143*4724848cSchristos        map { s|\s+||g; $_ }
144*4724848cSchristos        split(m|,|, $podinfo{lastsecttext});
145*4724848cSchristos
146*4724848cSchristos    return ( section => $podinfo{section}, names => [ @names ] );
147*4724848cSchristos}
148*4724848cSchristos
149*4724848cSchristos1;
150