2 # Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
9 # Reads one or more template files and runs it through Text::Template
11 # It is assumed that this scripts is called with -Mconfigdata, a module
12 # that holds configuration data in %config
19 # We actually expect to get the following hash tables from configdata:
26 # We just do a minimal test to see that we got what we expected.
27 # $config{target} must exist as an absolute minimum.
28 die "You must run this script with -Mconfigdata\n" if !exists($config{target});
30 # Make a subclass of Text::Template to override append_text_to_result,
31 # as recommended here:
33 # http://search.cpan.org/~mjd/Text-Template-1.46/lib/Text/Template.pm#Automatic_postprocessing_of_template_hunks
35 package OpenSSL::Template;
37 # Because we know that Text::Template isn't a core Perl module, we use
38 # a fallback in case it's not installed on the system
40 use File::Spec::Functions;
41 use lib catdir(dirname(__FILE__));
42 use with_fallback qw(Text::Template);
44 #use parent qw/Text::Template/;
46 push @ISA, qw/Text::Template/;
48 # Override constructor
52 # Call the constructor of the parent class, Person.
53 my $self = $class->SUPER::new( @_ );
54 # Add few more attributes
55 $self->{_output_off} = 0; # Default to output hunks
60 sub append_text_to_output {
63 if ($self->{_output_off} == 0) {
64 $self->SUPER::append_text_to_output(@_);
72 $self->{_output_off} = 0;
77 if (--$self->{_output_off} < 0) {
78 $self->{_output_off} = 0;
84 $self->{_output_off}++;
91 # Helper functions for the templates #################################
93 # It might be practical to quotify some strings and have them protected
94 # from possible harm. These functions primarly quote things that might
95 # be interpreted wrongly by a perl eval.
98 # This adds quotes (") around the given string, and escapes any $, @, \,
99 # " and ' by prepending a \ to them.
102 $s =~ s/([\$\@\\"'])/\\$1/g;
107 # For each defined element in LIST (i.e. elements that aren't undef), have
108 # it quotified with 'quotofy1'
119 # Error reporter #####################################################
121 # The error reporter uses %lines to figure out exactly which file the
122 # error happened and at what line. Not that the line number may be
123 # the start of a perl snippet rather than the exact line where it
124 # happened. Nothing we can do about that here.
129 my $filename = "<STDIN>";
131 foreach (sort keys %lines) {
132 $filename = $lines{$_};
133 last if ($_ > $args{lineno});
136 print STDERR $args{error}," in $filename, fragment starting at line ",$args{lineno}-$deducelines;
140 # Check options ######################################################
145 # declares ORIGINATOR as the originating script.
148 my @autowarntext = ("WARNING: do not edit!",
150 . (defined($opts{o}) ? " by ".$opts{o} : "")
151 . (scalar(@ARGV) > 0 ? " from ".join(", ",@ARGV) : ""));
153 # Template reading ###################################################
155 # Read in all the templates into $text, while keeping track of each
156 # file and its size in lines, to try to help report errors with the
157 # correct file name and line number.
159 my $prev_linecount = 0;
162 ? join("", map { my $x = Text::Template::_load_text($_);
164 die $Text::Template::ERROR, "\n";
166 $x = "{- output_reset_on() -}" . $x;
167 my $linecount = $x =~ tr/\n//;
168 $prev_linecount = ($linecount += $prev_linecount);
169 $lines{$linecount} = $_;
173 # Engage! ############################################################
175 # Load the full template (combination of files) into Text::Template
176 # and fill it up with our data. Output goes directly to STDOUT
178 my $template = OpenSSL::Template->new(TYPE => 'STRING', SOURCE => $text );
180 sub output_reset_on {
181 $template->output_reset_on();
185 $template->output_on();
189 $template->output_off();
193 $template->fill_in(OUTPUT => \*STDOUT,
194 HASH => { config => \%config,
196 disabled => \%disabled,
197 withargs => \%withargs,
198 unified_info => \%unified_info,
199 autowarntext => \@autowarntext,
200 quotify1 => \"ify1,
201 quotify_l => \"ify_l,
202 output_reset_on => \&output_reset_on,
203 output_on => \&output_on,
204 output_off => \&output_off },
205 DELIMITERS => [ "{-", "-}" ],