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