xref: /freebsd-src/crypto/openssl/util/perl/OpenSSL/Template.pm (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert#! /usr/bin/env perl
2*e0c4386eSCy Schubert# Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert#
4*e0c4386eSCy Schubert# Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert# this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert# in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert# https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert
9*e0c4386eSCy Schubert# Implements the functionality to read one or more template files and run
10*e0c4386eSCy Schubert# them through Text::Template
11*e0c4386eSCy Schubert
12*e0c4386eSCy Schubertpackage OpenSSL::Template;
13*e0c4386eSCy Schubert
14*e0c4386eSCy Schubert=head1 NAME
15*e0c4386eSCy Schubert
16*e0c4386eSCy SchubertOpenSSL::Template - a private extension of Text::Template
17*e0c4386eSCy Schubert
18*e0c4386eSCy Schubert=head1 DESCRIPTION
19*e0c4386eSCy Schubert
20*e0c4386eSCy SchubertThis provides exactly the functionality from Text::Template, with the
21*e0c4386eSCy Schubertfollowing additions:
22*e0c4386eSCy Schubert
23*e0c4386eSCy Schubert=over 4
24*e0c4386eSCy Schubert
25*e0c4386eSCy Schubert=item *
26*e0c4386eSCy Schubert
27*e0c4386eSCy SchubertThe template perl code delimiters (given with the C<DELIMITER> option)
28*e0c4386eSCy Schubertare set to C<{-> and C<-}> by default.
29*e0c4386eSCy Schubert
30*e0c4386eSCy Schubert=item *
31*e0c4386eSCy Schubert
32*e0c4386eSCy SchubertA few extra functions are offered to be used by the template perl code, see
33*e0c4386eSCy SchubertL</Functions>.
34*e0c4386eSCy Schubert
35*e0c4386eSCy Schubert=back
36*e0c4386eSCy Schubert
37*e0c4386eSCy Schubert=cut
38*e0c4386eSCy Schubert
39*e0c4386eSCy Schubertuse File::Basename;
40*e0c4386eSCy Schubertuse File::Spec::Functions;
41*e0c4386eSCy Schubertuse Text::Template 1.46;
42*e0c4386eSCy Schubert
43*e0c4386eSCy Schubertour @ISA = qw(Text::Template);  # parent
44*e0c4386eSCy Schubert
45*e0c4386eSCy Schubertsub new {
46*e0c4386eSCy Schubert    my $class = shift;
47*e0c4386eSCy Schubert
48*e0c4386eSCy Schubert    # Call the constructor of the parent class.
49*e0c4386eSCy Schubert    my $self = $class->SUPER::new(DELIMITERS => [ '{-', '-}'],
50*e0c4386eSCy Schubert                                  @_ );
51*e0c4386eSCy Schubert
52*e0c4386eSCy Schubert    # Add few more attributes
53*e0c4386eSCy Schubert    $self->{_output_off}   = 0; # Default to output hunks
54*e0c4386eSCy Schubert
55*e0c4386eSCy Schubert    return bless $self, $class;
56*e0c4386eSCy Schubert}
57*e0c4386eSCy Schubert
58*e0c4386eSCy Schubertsub fill_in {
59*e0c4386eSCy Schubert    my $self = shift;
60*e0c4386eSCy Schubert    my %opts = @_;
61*e0c4386eSCy Schubert    my %hash = ( %{$opts{HASH}} );
62*e0c4386eSCy Schubert    delete $opts{HASH};
63*e0c4386eSCy Schubert
64*e0c4386eSCy Schubert    $self->SUPER::fill_in(HASH => { quotify1 => \&quotify1,
65*e0c4386eSCy Schubert                                    quotify_l => \&quotify_l,
66*e0c4386eSCy Schubert                                    output_on => sub { $self->output_on() },
67*e0c4386eSCy Schubert                                    output_off => sub { $self->output_off() },
68*e0c4386eSCy Schubert                                    %hash },
69*e0c4386eSCy Schubert                          %opts);
70*e0c4386eSCy Schubert}
71*e0c4386eSCy Schubert
72*e0c4386eSCy Schubert=head2 Functions
73*e0c4386eSCy Schubert
74*e0c4386eSCy Schubert=cut
75*e0c4386eSCy Schubert
76*e0c4386eSCy Schubert# Override Text::Template's append_text_to_result, as recommended here:
77*e0c4386eSCy Schubert#
78*e0c4386eSCy Schubert# http://search.cpan.org/~mjd/Text-Template-1.46/lib/Text/Template.pm#Automatic_postprocessing_of_template_hunks
79*e0c4386eSCy Schubertsub append_text_to_output {
80*e0c4386eSCy Schubert    my $self = shift;
81*e0c4386eSCy Schubert
82*e0c4386eSCy Schubert    if ($self->{_output_off} == 0) {
83*e0c4386eSCy Schubert        $self->SUPER::append_text_to_output(@_);
84*e0c4386eSCy Schubert    }
85*e0c4386eSCy Schubert
86*e0c4386eSCy Schubert    return;
87*e0c4386eSCy Schubert}
88*e0c4386eSCy Schubert
89*e0c4386eSCy Schubert=begin comment
90*e0c4386eSCy Schubert
91*e0c4386eSCy SchubertWe lie about the OO nature of output_on() and output_off(), 'cause that's
92*e0c4386eSCy Schubertnot how we pass them, see the HASH option used in fill_in() above
93*e0c4386eSCy Schubert
94*e0c4386eSCy Schubert=end comment
95*e0c4386eSCy Schubert
96*e0c4386eSCy Schubert=over 4
97*e0c4386eSCy Schubert
98*e0c4386eSCy Schubert=item output_on()
99*e0c4386eSCy Schubert
100*e0c4386eSCy Schubert=item output_off()
101*e0c4386eSCy Schubert
102*e0c4386eSCy SchubertSwitch on or off template output.  Here's an example usage:
103*e0c4386eSCy Schubert
104*e0c4386eSCy Schubert=over 4
105*e0c4386eSCy Schubert
106*e0c4386eSCy Schubert {- output_off() if CONDITION -}
107*e0c4386eSCy Schubert whatever
108*e0c4386eSCy Schubert {- output_on() if CONDITION -}
109*e0c4386eSCy Schubert
110*e0c4386eSCy Schubert=back
111*e0c4386eSCy Schubert
112*e0c4386eSCy SchubertIn this example, C<whatever> will only become part of the template output
113*e0c4386eSCy Schubertif C<CONDITION> is true.
114*e0c4386eSCy Schubert
115*e0c4386eSCy Schubert=back
116*e0c4386eSCy Schubert
117*e0c4386eSCy Schubert=cut
118*e0c4386eSCy Schubert
119*e0c4386eSCy Schubertsub output_on {
120*e0c4386eSCy Schubert    my $self = shift;
121*e0c4386eSCy Schubert    if (--$self->{_output_off} < 0) {
122*e0c4386eSCy Schubert        $self->{_output_off} = 0;
123*e0c4386eSCy Schubert    }
124*e0c4386eSCy Schubert}
125*e0c4386eSCy Schubert
126*e0c4386eSCy Schubertsub output_off {
127*e0c4386eSCy Schubert    my $self = shift;
128*e0c4386eSCy Schubert    $self->{_output_off}++;
129*e0c4386eSCy Schubert}
130*e0c4386eSCy Schubert
131*e0c4386eSCy Schubert# Helper functions for the templates #################################
132*e0c4386eSCy Schubert
133*e0c4386eSCy Schubert=head1 SEE ALSO
134*e0c4386eSCy Schubert
135*e0c4386eSCy SchubertL<Text::Template>
136*e0c4386eSCy Schubert
137*e0c4386eSCy Schubert=head1 AUTHORS
138*e0c4386eSCy Schubert
139*e0c4386eSCy SchubertRichard Levitte E<lt>levitte@openssl.orgE<gt>
140*e0c4386eSCy Schubert
141*e0c4386eSCy Schubert=head1 COPYRIGHT
142*e0c4386eSCy Schubert
143*e0c4386eSCy SchubertCopyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
144*e0c4386eSCy Schubert
145*e0c4386eSCy SchubertLicensed under the Apache License 2.0 (the "License").  You may not use
146*e0c4386eSCy Schubertthis file except in compliance with the License.  You can obtain a copy
147*e0c4386eSCy Schubertin the file LICENSE in the source distribution or at
148*e0c4386eSCy SchubertL<https://www.openssl.org/source/license.html>.
149*e0c4386eSCy Schubert
150*e0c4386eSCy Schubert=cut
151