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 # Override constructor
44 # Call the constructor of the parent class, Person.
45 my $self = $class->SUPER::new( @_ );
46 # Add few more attributes
47 $self->{_output_off} = 0; # Default to output hunks
52 sub append_text_to_output {
55 if ($self->{_output_off} == 0) {
56 $self->SUPER::append_text_to_output(@_);
64 $self->{_output_off} = 0;
69 if (--$self->{_output_off} < 0) {
70 $self->{_output_off} = 0;
76 $self->{_output_off}++;
83 # Helper functions for the templates #################################
85 # It might be practical to quotify some strings and have them protected
86 # from possible harm. These functions primarly quote things that might
87 # be interpreted wrongly by a perl eval.
90 # This adds quotes (") around the given string, and escapes any $, @, \,
91 # " and ' by prepending a \ to them.
94 $s =~ s/([\$\@\\"'])/\\$1/g;
99 # For each defined element in LIST (i.e. elements that aren't undef), have
100 # it quotified with 'quotofy1'
111 # Error reporter #####################################################
113 # The error reporter uses %lines to figure out exactly which file the
114 # error happened and at what line. Not that the line number may be
115 # the start of a perl snippet rather than the exact line where it
116 # happened. Nothing we can do about that here.
121 my $filename = "<STDIN>";
123 foreach (sort keys %lines) {
124 $filename = $lines{$_};
125 last if ($_ > $args{lineno});
128 print STDERR $args{error}," in $filename, fragment starting at line ",$args{lineno}-$deducelines;
132 # Check options ######################################################
137 # declares ORIGINATOR as the originating script.
140 my @autowarntext = ("WARNING: do not edit!",
142 . (defined($opts{o}) ? " by ".$opts{o} : "")
143 . (scalar(@ARGV) > 0 ? " from ".join(", ",@ARGV) : ""));
145 # Template reading ###################################################
147 # Read in all the templates into $text, while keeping track of each
148 # file and its size in lines, to try to help report errors with the
149 # correct file name and line number.
151 my $prev_linecount = 0;
154 ? join("", map { my $x = "{- output_reset_on() -}".Text::Template::_load_text($_);
155 my $linecount = $x =~ tr/\n//;
156 $prev_linecount = ($linecount += $prev_linecount);
157 $lines{$linecount} = $_;
161 # Engage! ############################################################
163 # Load the full template (combination of files) into Text::Template
164 # and fill it up with our data. Output goes directly to STDOUT
166 my $template = OpenSSL::Template->new(TYPE => 'STRING', SOURCE => $text );
168 sub output_reset_on {
169 $template->output_reset_on();
173 $template->output_on();
177 $template->output_off();
181 $template->fill_in(OUTPUT => \*STDOUT,
182 HASH => { config => \%config,
184 withargs => \%withargs,
185 unified_info => \%unified_info,
186 autowarntext => \@autowarntext,
187 quotify1 => \"ify1,
188 quotify_l => \"ify_l,
189 output_reset_on => \&output_reset_on,
190 output_on => \&output_on,
191 output_off => \&output_off },
192 DELIMITERS => [ "{-", "-}" ],