Refactor file writing - introduce template driven file writing
[openssl.git] / util / dofile.pl
1 #! /usr/bin/perl
2 #
3 # Reads one or more template files and runs it through Text::Template
4 #
5 # It is assumed that this scripts is called with -Mconfigdata, a module
6 # that holds configuration data in %config
7
8 use strict;
9 use warnings;
10 use Text::Template;
11
12 # We actually expect to get the following hash tables from configdata:
13 #
14 #    %config
15 #    %target
16 #    %withargs
17 #
18 # We just do a minimal test to see that we got what we expected.
19 # $config{target} must exist as an absolute minimum.
20 die "You must run this script with -Mconfigdata\n" if !exists($config{target});
21
22 # Helper functions for the templates #################################
23
24 # It might be practical to quotify some strings and have them protected
25 # from possible harm.  These functions primarly quote things that might
26 # be interpreted wrongly by a perl eval.
27
28 # quotify1 STRING
29 # This adds quotes (") around the given string, and escapes any $, @, \,
30 # " and ' by prepending a \ to them.
31 sub quotify1 {
32     my $s = shift @_;
33     $s =~ s/([\$\@\\"'])/\\$1/g;
34     '"'.$s.'"';
35 }
36
37 # quotify_l LIST
38 # For each defined element in LIST (i.e. elements that aren't undef), have
39 # it quotified with 'quotofy1'
40 sub quotify_l {
41     map {
42         if (!defined($_)) {
43             ();
44         } else {
45             quotify1($_);
46         }
47     } @_;
48 }
49
50 # Error reporter #####################################################
51
52 # The error reporter uses %lines to figure out exactly which file the
53 # error happened and at what line.  Not that the line number may be
54 # the start of a perl snippet rather than the exact line where it
55 # happened.  Nothing we can do about that here.
56
57 my %lines = ();
58 sub broken {
59     my %args = @_;
60     my $filename = "<STDIN>";
61     my $deducelines = 0;
62     foreach (sort keys %lines) {
63         $filename = $lines{$_};
64         last if ($_ > $args{lineno});
65         $deducelines += $_;
66     }
67     print STDERR $args{error}," in $filename, fragment starting at line ",$args{lineno}-$deducelines;
68     undef;
69 }
70
71 # Template reading ###################################################
72
73 # Read in all the templates into $text, while keeping track of each
74 # file and its size in lines, to try to help report errors with the
75 # correct file name and line number.
76
77 my $prev_linecount = 0;
78 my $text =
79     @ARGV
80     ? join("", map { my $x = my $y = Text::Template::_load_text($_);
81                      my $linecount = $x =~ tr/\n//;
82                      $prev_linecount = ($linecount += $prev_linecount);
83                      $lines{$linecount} = $_;
84                      $x } @ARGV)
85     : join("", <STDIN>);
86
87 # Engage! ############################################################
88
89 # Load the full template (combination of files) into Text::Template
90 # and fill it up with our data.  Output goes directly to STDOUT
91
92 my $template = Text::Template->new(TYPE => 'STRING', SOURCE => $text );
93 $template->fill_in(OUTPUT => \*STDOUT,
94                    HASH => { config => \%config,
95                              target => \%target,
96                              quotify1 => \&quotify1,
97                              quotify_l => \&quotify_l },
98                    DELIMITERS => [ "{-", "-}" ],
99                    BROKEN => \&broken);