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 # Because we know that Text::Template isn't a core Perl module, we use
14 # a fallback in case it's not installed on the system
16 use File::Spec::Functions;
17 use lib catdir(dirname(__FILE__));
18 use with_fallback qw(Text::Template);
20 # 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 # Helper functions for the templates #################################
32 # It might be practical to quotify some strings and have them protected
33 # from possible harm. These functions primarly quote things that might
34 # be interpreted wrongly by a perl eval.
37 # This adds quotes (") around the given string, and escapes any $, @, \,
38 # " and ' by prepending a \ to them.
41 $s =~ s/([\$\@\\"'])/\\$1/g;
46 # For each defined element in LIST (i.e. elements that aren't undef), have
47 # it quotified with 'quotofy1'
58 # Error reporter #####################################################
60 # The error reporter uses %lines to figure out exactly which file the
61 # error happened and at what line. Not that the line number may be
62 # the start of a perl snippet rather than the exact line where it
63 # happened. Nothing we can do about that here.
68 my $filename = "<STDIN>";
70 foreach (sort keys %lines) {
71 $filename = $lines{$_};
72 last if ($_ > $args{lineno});
75 print STDERR $args{error}," in $filename, fragment starting at line ",$args{lineno}-$deducelines;
79 # Check options ######################################################
84 # declares ORIGINATOR as the originating script.
87 my @autowarntext = ("WARNING: do not edit!",
89 . (defined($opts{o}) ? " by ".$opts{o} : "")
90 . (scalar(@ARGV) > 0 ? " from ".join(", ",@ARGV) : ""));
92 # Template reading ###################################################
94 # Read in all the templates into $text, while keeping track of each
95 # file and its size in lines, to try to help report errors with the
96 # correct file name and line number.
98 my $prev_linecount = 0;
101 ? join("", map { my $x = Text::Template::_load_text($_);
102 my $linecount = $x =~ tr/\n//;
103 $prev_linecount = ($linecount += $prev_linecount);
104 $lines{$linecount} = $_;
108 # Engage! ############################################################
110 # Load the full template (combination of files) into Text::Template
111 # and fill it up with our data. Output goes directly to STDOUT
113 my $template = Text::Template->new(TYPE => 'STRING', SOURCE => $text );
114 $template->fill_in(OUTPUT => \*STDOUT,
115 HASH => { config => \%config,
117 withargs => \%withargs,
118 autowarntext => \@autowarntext,
119 quotify1 => \"ify1,
120 quotify_l => \"ify_l },
121 DELIMITERS => [ "{-", "-}" ],