3 # Reads one or more template files and runs it through Text::Template
5 # It is assumed that this scripts is called with -Mconfigdata, a module
6 # that holds configuration data in %config
13 # We actually expect to get the following hash tables from configdata:
20 # We just do a minimal test to see that we got what we expected.
21 # $config{target} must exist as an absolute minimum.
22 die "You must run this script with -Mconfigdata\n" if !exists($config{target});
24 # Make a subclass of Text::Template to override append_text_to_result,
25 # as recommended here:
27 # http://search.cpan.org/~mjd/Text-Template-1.46/lib/Text/Template.pm#Automatic_postprocessing_of_template_hunks
29 package OpenSSL::Template;
31 # Because we know that Text::Template isn't a core Perl module, we use
32 # a fallback in case it's not installed on the system
34 use File::Spec::Functions;
35 use lib catdir(dirname(__FILE__));
36 use with_fallback qw(Text::Template);
38 #use parent qw/Text::Template/;
40 push @ISA, qw/Text::Template/;
42 # Override constructor
46 # Call the constructor of the parent class, Person.
47 my $self = $class->SUPER::new( @_ );
48 # Add few more attributes
49 $self->{_output_off} = 0; # Default to output hunks
54 sub append_text_to_output {
57 if ($self->{_output_off} == 0) {
58 $self->SUPER::append_text_to_output(@_);
66 $self->{_output_off} = 0;
71 if (--$self->{_output_off} < 0) {
72 $self->{_output_off} = 0;
78 $self->{_output_off}++;
85 # Helper functions for the templates #################################
87 # It might be practical to quotify some strings and have them protected
88 # from possible harm. These functions primarly quote things that might
89 # be interpreted wrongly by a perl eval.
92 # This adds quotes (") around the given string, and escapes any $, @, \,
93 # " and ' by prepending a \ to them.
96 $s =~ s/([\$\@\\"'])/\\$1/g;
101 # For each defined element in LIST (i.e. elements that aren't undef), have
102 # it quotified with 'quotofy1'
113 # Error reporter #####################################################
115 # The error reporter uses %lines to figure out exactly which file the
116 # error happened and at what line. Not that the line number may be
117 # the start of a perl snippet rather than the exact line where it
118 # happened. Nothing we can do about that here.
123 my $filename = "<STDIN>";
125 foreach (sort keys %lines) {
126 $filename = $lines{$_};
127 last if ($_ > $args{lineno});
130 print STDERR $args{error}," in $filename, fragment starting at line ",$args{lineno}-$deducelines;
134 # Check options ######################################################
139 # declares ORIGINATOR as the originating script.
142 my @autowarntext = ("WARNING: do not edit!",
144 . (defined($opts{o}) ? " by ".$opts{o} : "")
145 . (scalar(@ARGV) > 0 ? " from ".join(", ",@ARGV) : ""));
147 # Template reading ###################################################
149 # Read in all the templates into $text, while keeping track of each
150 # file and its size in lines, to try to help report errors with the
151 # correct file name and line number.
153 my $prev_linecount = 0;
156 ? join("", map { my $x = "{- output_reset_on() -}".Text::Template::_load_text($_);
157 my $linecount = $x =~ tr/\n//;
158 $prev_linecount = ($linecount += $prev_linecount);
159 $lines{$linecount} = $_;
163 # Engage! ############################################################
165 # Load the full template (combination of files) into Text::Template
166 # and fill it up with our data. Output goes directly to STDOUT
168 my $template = OpenSSL::Template->new(TYPE => 'STRING', SOURCE => $text );
170 sub output_reset_on {
171 $template->output_reset_on();
175 $template->output_on();
179 $template->output_off();
183 $template->fill_in(OUTPUT => \*STDOUT,
184 HASH => { config => \%config,
186 disabled => \%disabled,
187 withargs => \%withargs,
188 unified_info => \%unified_info,
189 autowarntext => \@autowarntext,
190 quotify1 => \"ify1,
191 quotify_l => \"ify_l,
192 output_reset_on => \&output_reset_on,
193 output_on => \&output_on,
194 output_off => \&output_off },
195 DELIMITERS => [ "{-", "-}" ],