Invoke tear_down when exiting test_encode_tls_sct() prematurely
[openssl.git] / util / dofile.pl
1 #! /usr/bin/env perl
2 # Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3 #
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
8
9 # Reads one or more template files and runs it through Text::Template
10 #
11 # It is assumed that this scripts is called with -Mconfigdata, a module
12 # that holds configuration data in %config
13
14 use strict;
15 use warnings;
16
17 use FindBin;
18 use lib "$FindBin::Bin/perl";
19 use OpenSSL::fallback "$FindBin::Bin/../external/perl/MODULES.txt";
20 use Getopt::Std;
21 use OpenSSL::Template;
22
23 # We expect to get a lot of information from configdata, so check that
24 # it was part of our commandline.
25 die "You must run this script with -Mconfigdata\n"
26     if !exists($config{target});
27
28 # Check options ######################################################
29
30 # -o ORIGINATOR
31 #               declares ORIGINATOR as the originating script.
32 # -i .ext       Like Perl's edit-in-place -i flag
33 my %opts = ();
34 getopt('oi', \%opts);
35
36 my @autowarntext = (
37     "WARNING: do not edit!",
38     "Generated"
39         . (defined($opts{o}) ? " by $opts{o}" : "")
40         . (scalar(@ARGV) > 0 ? " from " .join(", ", @ARGV) : "")
41 );
42
43 if (defined($opts{s})) {
44     local $/ = undef;
45     open VARS, $opts{s} or die "Couldn't open $opts{s}, $!";
46     my $contents = <VARS>;
47     close VARS;
48     eval $contents;
49     die $@ if $@;
50 }
51 die "Must have input files"
52    if defined($opts{i}) and scalar(@ARGV) == 0;
53
54 # Template setup #####################################################
55
56 my @template_settings =
57     @ARGV
58     ? map { { TYPE => 'FILE', SOURCE => $_, FILENAME => $_ } } @ARGV
59     : ( { TYPE => 'FILEHANDLE', SOURCE => \*STDIN, FILENAME => '<stdin>' } );
60
61 # Error callback; print message, set status, return "stop processing"
62 my $failed = 0;
63 sub errorcallback {
64     my %args = @_;
65     print STDERR $args{error};
66     $failed++;
67     return undef;
68 }
69
70 # Engage! ############################################################
71
72 my $prepend = <<"_____";
73 use File::Spec::Functions;
74 use lib '$FindBin::Bin/../Configurations';
75 use lib '$config{builddir}';
76 use platform;
77 _____
78
79 foreach (@template_settings) {
80     my $template = OpenSSL::Template->new(%$_);
81     die "Couldn't create template: $Text::Template::ERROR"
82         if !defined($template);
83
84     my $result = $template->fill_in(%$_,
85                        HASH => { config => \%config,
86                                  target => \%target,
87                                  disabled => \%disabled,
88                                  withargs => \%withargs,
89                                  unified_info => \%unified_info,
90                                  autowarntext => \@autowarntext },
91                        BROKEN => \&errorcallback,
92                        PREPEND => $prepend,
93                        # To ensure that global variables and functions
94                        # defined in one template stick around for the
95                        # next, making them combinable
96                        PACKAGE => 'OpenSSL::safe');
97     exit 1 if $failed;
98
99     if (defined($opts{i})) {
100         my $in = $_->{FILENAME};
101         my $out = $in;
102         $out =~ s/$opts{i}$//;
103         die "Cannot replace file in-place $in"
104             if $in eq $out;
105         open OFH, ">$out"
106             or die "Can't open $out, $!";
107         print OFH $result;
108         close OFH;
109     } else {
110         print $result;
111     }
112 }