2 # Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (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
20 # We actually expect to get the following hash tables from configdata:
27 # We just do a minimal test to see that we got what we expected.
28 # $config{target} must exist as an absolute minimum.
29 die "You must run this script with -Mconfigdata\n" if !exists($config{target});
31 # Make a subclass of Text::Template to override append_text_to_result,
32 # as recommended here:
34 # http://search.cpan.org/~mjd/Text-Template-1.46/lib/Text/Template.pm#Automatic_postprocessing_of_template_hunks
36 package OpenSSL::Template;
38 # Because we know that Text::Template isn't a core Perl module, we use
39 # a fallback in case it's not installed on the system
41 use File::Spec::Functions;
42 use lib "$FindBin::Bin/perl";
43 use with_fallback "Text::Template 1.46";
45 #use parent qw/Text::Template/;
47 push @ISA, qw/Text::Template/;
49 # Override constructor
53 # Call the constructor of the parent class, Person.
54 my $self = $class->SUPER::new( @_ );
55 # Add few more attributes
56 $self->{_output_off} = 0; # Default to output hunks
61 sub append_text_to_output {
64 if ($self->{_output_off} == 0) {
65 $self->SUPER::append_text_to_output(@_);
73 $self->{_output_off} = 0;
78 if (--$self->{_output_off} < 0) {
79 $self->{_output_off} = 0;
85 $self->{_output_off}++;
92 # Helper functions for the templates #################################
94 # It might be practical to quotify some strings and have them protected
95 # from possible harm. These functions primarily quote things that might
96 # be interpreted wrongly by a perl eval.
99 # This adds quotes (") around the given string, and escapes any $, @, \,
100 # " and ' by prepending a \ to them.
103 $s =~ s/([\$\@\\"'])/\\$1/g;
108 # For each defined element in LIST (i.e. elements that aren't undef), have
109 # it quotified with 'quotify1'
120 # Error reporter #####################################################
122 # The error reporter uses %lines to figure out exactly which file the
123 # error happened and at what line. Not that the line number may be
124 # the start of a perl snippet rather than the exact line where it
125 # happened. Nothing we can do about that here.
130 my $filename = "<STDIN>";
132 foreach (sort keys %lines) {
133 $filename = $lines{$_};
134 last if ($_ > $args{lineno});
137 print STDERR $args{error}," in $filename, fragment starting at line ",$args{lineno}-$deducelines;
141 # Check options ######################################################
146 # declares ORIGINATOR as the originating script.
149 my @autowarntext = ("WARNING: do not edit!",
151 . (defined($opts{o}) ? " by ".$opts{o} : "")
152 . (scalar(@ARGV) > 0 ? " from ".join(", ",@ARGV) : ""));
154 # Template reading ###################################################
156 # Read in all the templates into $text, while keeping track of each
157 # file and its size in lines, to try to help report errors with the
158 # correct file name and line number.
160 my $prev_linecount = 0;
163 ? join("", map { my $x = Text::Template::_load_text($_);
165 die $Text::Template::ERROR, "\n";
167 $x = "{- output_reset_on() -}" . $x;
168 my $linecount = $x =~ tr/\n//;
169 $prev_linecount = ($linecount += $prev_linecount);
170 $lines{$linecount} = $_;
174 # Engage! ############################################################
176 # Load the full template (combination of files) into Text::Template
177 # and fill it up with our data. Output goes directly to STDOUT
180 use File::Spec::Functions;
181 use lib catdir('$config{sourcedir}', 'util', 'perl');
184 use lib catdir('$config{sourcedir}', 'Configurations');
185 use lib '$config{builddir}';
187 } if defined $target{perl_platform};
190 OpenSSL::Template->new(TYPE => 'STRING',
192 PREPEND => $prepend);
194 sub output_reset_on {
195 $template->output_reset_on();
199 $template->output_on();
203 $template->output_off();
207 $template->fill_in(OUTPUT => \*STDOUT,
208 HASH => { config => \%config,
210 disabled => \%disabled,
211 withargs => \%withargs,
212 unified_info => \%unified_info,
213 autowarntext => \@autowarntext,
214 quotify1 => \"ify1,
215 quotify_l => \"ify_l,
216 output_reset_on => \&output_reset_on,
217 output_on => \&output_on,
218 output_off => \&output_off },
219 DELIMITERS => [ "{-", "-}" ],