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