X-Git-Url: https://git.openssl.org/?a=blobdiff_plain;f=util%2Fdofile.pl;h=6d4ffa4abdff43c1ba58b0bdda020a3dbec89396;hb=55ca81259a63286a88dd1f53ef14f7830f444c7e;hp=a6b0905a58ab9d4cab7b889f9764f1d61f627653;hpb=107b5792b24ae701df442cd36bda785cbaf1bfdc;p=openssl.git diff --git a/util/dofile.pl b/util/dofile.pl index a6b0905a58..6d4ffa4abd 100644 --- a/util/dofile.pl +++ b/util/dofile.pl @@ -1,5 +1,11 @@ -#! /usr/bin/perl +#! /usr/bin/env perl +# Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. # +# Licensed under the Apache License 2.0 (the "License"). You may not use +# this file except in compliance with the License. You can obtain a copy +# in the file LICENSE in the source distribution or at +# https://www.openssl.org/source/license.html + # Reads one or more template files and runs it through Text::Template # # It is assumed that this scripts is called with -Mconfigdata, a module @@ -8,99 +14,99 @@ use strict; use warnings; -# Because we know that Text::Template isn't a core Perl module, we use -# a fallback in case it's not installed on the system -use File::Basename; -use File::Spec::Functions; -use lib catdir(dirname(__FILE__)); -use with_fallback qw(Text::Template); - -# We actually expect to get the following hash tables from configdata: -# -# %config -# %target -# %withargs -# -# We just do a minimal test to see that we got what we expected. -# $config{target} must exist as an absolute minimum. -die "You must run this script with -Mconfigdata\n" if !exists($config{target}); - -# Helper functions for the templates ################################# - -# It might be practical to quotify some strings and have them protected -# from possible harm. These functions primarly quote things that might -# be interpreted wrongly by a perl eval. - -# quotify1 STRING -# This adds quotes (") around the given string, and escapes any $, @, \, -# " and ' by prepending a \ to them. -sub quotify1 { - my $s = shift @_; - $s =~ s/([\$\@\\"'])/\\$1/g; - '"'.$s.'"'; -} - -# quotify_l LIST -# For each defined element in LIST (i.e. elements that aren't undef), have -# it quotified with 'quotofy1' -sub quotify_l { - map { - if (!defined($_)) { - (); - } else { - quotify1($_); - } - } @_; +use FindBin; +use lib "$FindBin::Bin/perl"; +use OpenSSL::fallback "$FindBin::Bin/../external/perl/MODULES.txt"; +use Getopt::Std; +use OpenSSL::Template; + +# We expect to get a lot of information from configdata, so check that +# it was part of our commandline. +die "You must run this script with -Mconfigdata\n" + if !exists($config{target}); + +# Check options ###################################################### + +# -o ORIGINATOR +# declares ORIGINATOR as the originating script. +# -i .ext Like Perl's edit-in-place -i flag +my %opts = (); +getopt('oi', \%opts); + +my @autowarntext = ( + "WARNING: do not edit!", + "Generated" + . (defined($opts{o}) ? " by $opts{o}" : "") + . (scalar(@ARGV) > 0 ? " from " .join(", ", @ARGV) : "") +); + +if (defined($opts{s})) { + local $/ = undef; + open VARS, $opts{s} or die "Couldn't open $opts{s}, $!"; + my $contents = ; + close VARS; + eval $contents; + die $@ if $@; } +die "Must have input files" + if defined($opts{i}) and scalar(@ARGV) == 0; -# Error reporter ##################################################### +# Template setup ##################################################### -# The error reporter uses %lines to figure out exactly which file the -# error happened and at what line. Not that the line number may be -# the start of a perl snippet rather than the exact line where it -# happened. Nothing we can do about that here. +my @template_settings = + @ARGV + ? map { { TYPE => 'FILE', SOURCE => $_, FILENAME => $_ } } @ARGV + : ( { TYPE => 'FILEHANDLE', SOURCE => \*STDIN, FILENAME => '' } ); -my %lines = (); -sub broken { +# Error callback; print message, set status, return "stop processing" +my $failed = 0; +sub errorcallback { my %args = @_; - my $filename = ""; - my $deducelines = 0; - foreach (sort keys %lines) { - $filename = $lines{$_}; - last if ($_ > $args{lineno}); - $deducelines += $_; - } - print STDERR $args{error}," in $filename, fragment starting at line ",$args{lineno}-$deducelines; - undef; + print STDERR $args{error}; + $failed++; + return undef; } -# Template reading ################################################### - -# Read in all the templates into $text, while keeping track of each -# file and its size in lines, to try to help report errors with the -# correct file name and line number. - -my $prev_linecount = 0; -my $text = - @ARGV - ? join("", map { my $x = Text::Template::_load_text($_); - my $linecount = $x =~ tr/\n//; - $prev_linecount = ($linecount += $prev_linecount); - $lines{$linecount} = $_; - $x } @ARGV) - : join("", ); - # Engage! ############################################################ -# Load the full template (combination of files) into Text::Template -# and fill it up with our data. Output goes directly to STDOUT - -my $template = Text::Template->new(TYPE => 'STRING', SOURCE => $text ); -$template->fill_in(OUTPUT => \*STDOUT, - HASH => { config => \%config, - target => \%target, - withargs => \%withargs, - quotify1 => \"ify1, - quotify_l => \"ify_l }, - DELIMITERS => [ "{-", "-}" ], - BROKEN => \&broken); +my $prepend = <<"_____"; +use File::Spec::Functions; +use lib "$FindBin::Bin/../Configurations"; +use lib '$config{builddir}'; +use platform; +_____ + +foreach (@template_settings) { + my $template = OpenSSL::Template->new(%$_); + die "Couldn't create template: $Text::Template::ERROR" + if !defined($template); + + my $result = $template->fill_in(%$_, + HASH => { config => \%config, + target => \%target, + disabled => \%disabled, + withargs => \%withargs, + unified_info => \%unified_info, + autowarntext => \@autowarntext }, + BROKEN => \&errorcallback, + PREPEND => $prepend, + # To ensure that global variables and functions + # defined in one template stick around for the + # next, making them combinable + PACKAGE => 'OpenSSL::safe'); + exit 1 if $failed; + + if (defined($opts{i})) { + my $in = $_->{FILENAME}; + my $out = $in; + $out =~ s/$opts{i}$//; + die "Cannot replace file in-place $in" + if $in eq $out; + open OFH, ">$out" + or die "Can't open $out, $!"; + print OFH $result; + close OFH; + } else { + print $result; + } +}