|
Packit Service |
084de1 |
#! /usr/bin/env perl
|
|
Packit Service |
084de1 |
# Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
|
|
Packit Service |
084de1 |
#
|
|
Packit Service |
084de1 |
# Licensed under the OpenSSL license (the "License"). You may not use
|
|
Packit Service |
084de1 |
# this file except in compliance with the License. You can obtain a copy
|
|
Packit Service |
084de1 |
# in the file LICENSE in the source distribution or at
|
|
Packit Service |
084de1 |
# https://www.openssl.org/source/license.html
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# Reads one or more template files and runs it through Text::Template
|
|
Packit Service |
084de1 |
#
|
|
Packit Service |
084de1 |
# It is assumed that this scripts is called with -Mconfigdata, a module
|
|
Packit Service |
084de1 |
# that holds configuration data in %config
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
use strict;
|
|
Packit Service |
084de1 |
use warnings;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
use FindBin;
|
|
Packit Service |
084de1 |
use Getopt::Std;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# We actually expect to get the following hash tables from configdata:
|
|
Packit Service |
084de1 |
#
|
|
Packit Service |
084de1 |
# %config
|
|
Packit Service |
084de1 |
# %target
|
|
Packit Service |
084de1 |
# %withargs
|
|
Packit Service |
084de1 |
# %unified_info
|
|
Packit Service |
084de1 |
#
|
|
Packit Service |
084de1 |
# We just do a minimal test to see that we got what we expected.
|
|
Packit Service |
084de1 |
# $config{target} must exist as an absolute minimum.
|
|
Packit Service |
084de1 |
die "You must run this script with -Mconfigdata\n" if !exists($config{target});
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# Make a subclass of Text::Template to override append_text_to_result,
|
|
Packit Service |
084de1 |
# as recommended here:
|
|
Packit Service |
084de1 |
#
|
|
Packit Service |
084de1 |
# http://search.cpan.org/~mjd/Text-Template-1.46/lib/Text/Template.pm#Automatic_postprocessing_of_template_hunks
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
package OpenSSL::Template;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# Because we know that Text::Template isn't a core Perl module, we use
|
|
Packit Service |
084de1 |
# a fallback in case it's not installed on the system
|
|
Packit Service |
084de1 |
use File::Basename;
|
|
Packit Service |
084de1 |
use File::Spec::Functions;
|
|
Packit Service |
084de1 |
use lib "$FindBin::Bin/perl";
|
|
Packit Service |
084de1 |
use with_fallback "Text::Template 1.46";
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#use parent qw/Text::Template/;
|
|
Packit Service |
084de1 |
use vars qw/@ISA/;
|
|
Packit Service |
084de1 |
push @ISA, qw/Text::Template/;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# Override constructor
|
|
Packit Service |
084de1 |
sub new {
|
|
Packit Service |
084de1 |
my ($class) = shift;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# Call the constructor of the parent class, Person.
|
|
Packit Service |
084de1 |
my $self = $class->SUPER::new( @_ );
|
|
Packit Service |
084de1 |
# Add few more attributes
|
|
Packit Service |
084de1 |
$self->{_output_off} = 0; # Default to output hunks
|
|
Packit Service |
084de1 |
bless $self, $class;
|
|
Packit Service |
084de1 |
return $self;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
sub append_text_to_output {
|
|
Packit Service |
084de1 |
my $self = shift;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ($self->{_output_off} == 0) {
|
|
Packit Service |
084de1 |
$self->SUPER::append_text_to_output(@_);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
sub output_reset_on {
|
|
Packit Service |
084de1 |
my $self = shift;
|
|
Packit Service |
084de1 |
$self->{_output_off} = 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
sub output_on {
|
|
Packit Service |
084de1 |
my $self = shift;
|
|
Packit Service |
084de1 |
if (--$self->{_output_off} < 0) {
|
|
Packit Service |
084de1 |
$self->{_output_off} = 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
sub output_off {
|
|
Packit Service |
084de1 |
my $self = shift;
|
|
Packit Service |
084de1 |
$self->{_output_off}++;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# Come back to main
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
package main;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# Helper functions for the templates #################################
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# It might be practical to quotify some strings and have them protected
|
|
Packit Service |
084de1 |
# from possible harm. These functions primarily quote things that might
|
|
Packit Service |
084de1 |
# be interpreted wrongly by a perl eval.
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# quotify1 STRING
|
|
Packit Service |
084de1 |
# This adds quotes (") around the given string, and escapes any $, @, \,
|
|
Packit Service |
084de1 |
# " and ' by prepending a \ to them.
|
|
Packit Service |
084de1 |
sub quotify1 {
|
|
Packit Service |
084de1 |
my $s = shift @_;
|
|
Packit Service |
084de1 |
$s =~ s/([\$\@\\"'])/\\$1/g;
|
|
Packit Service |
084de1 |
'"'.$s.'"';
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# quotify_l LIST
|
|
Packit Service |
084de1 |
# For each defined element in LIST (i.e. elements that aren't undef), have
|
|
Packit Service |
084de1 |
# it quotified with 'quotify1'
|
|
Packit Service |
084de1 |
sub quotify_l {
|
|
Packit Service |
084de1 |
map {
|
|
Packit Service |
084de1 |
if (!defined($_)) {
|
|
Packit Service |
084de1 |
();
|
|
Packit Service |
084de1 |
} else {
|
|
Packit Service |
084de1 |
quotify1($_);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
} @_;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# Error reporter #####################################################
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# The error reporter uses %lines to figure out exactly which file the
|
|
Packit Service |
084de1 |
# error happened and at what line. Not that the line number may be
|
|
Packit Service |
084de1 |
# the start of a perl snippet rather than the exact line where it
|
|
Packit Service |
084de1 |
# happened. Nothing we can do about that here.
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
my %lines = ();
|
|
Packit Service |
084de1 |
sub broken {
|
|
Packit Service |
084de1 |
my %args = @_;
|
|
Packit Service |
084de1 |
my $filename = "<STDIN>";
|
|
Packit Service |
084de1 |
my $deducelines = 0;
|
|
Packit Service |
084de1 |
foreach (sort keys %lines) {
|
|
Packit Service |
084de1 |
$filename = $lines{$_};
|
|
Packit Service |
084de1 |
last if ($_ > $args{lineno});
|
|
Packit Service |
084de1 |
$deducelines += $_;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
print STDERR $args{error}," in $filename, fragment starting at line ",$args{lineno}-$deducelines;
|
|
Packit Service |
084de1 |
undef;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# Check options ######################################################
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
my %opts = ();
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# -o ORIGINATOR
|
|
Packit Service |
084de1 |
# declares ORIGINATOR as the originating script.
|
|
Packit Service |
084de1 |
getopt('o', \%opts);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
my @autowarntext = ("WARNING: do not edit!",
|
|
Packit Service |
084de1 |
"Generated"
|
|
Packit Service |
084de1 |
. (defined($opts{o}) ? " by ".$opts{o} : "")
|
|
Packit Service |
084de1 |
. (scalar(@ARGV) > 0 ? " from ".join(", ",@ARGV) : ""));
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# Template reading ###################################################
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# Read in all the templates into $text, while keeping track of each
|
|
Packit Service |
084de1 |
# file and its size in lines, to try to help report errors with the
|
|
Packit Service |
084de1 |
# correct file name and line number.
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
my $prev_linecount = 0;
|
|
Packit Service |
084de1 |
my $text =
|
|
Packit Service |
084de1 |
@ARGV
|
|
Packit Service |
084de1 |
? join("", map { my $x = Text::Template::_load_text($_);
|
|
Packit Service |
084de1 |
if (!defined($x)) {
|
|
Packit Service |
084de1 |
die $Text::Template::ERROR, "\n";
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
$x = "{- output_reset_on() -}" . $x;
|
|
Packit Service |
084de1 |
my $linecount = $x =~ tr/\n//;
|
|
Packit Service |
084de1 |
$prev_linecount = ($linecount += $prev_linecount);
|
|
Packit Service |
084de1 |
$lines{$linecount} = $_;
|
|
Packit Service |
084de1 |
$x } @ARGV)
|
|
Packit Service |
084de1 |
: join("", <STDIN>);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# Engage! ############################################################
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# Load the full template (combination of files) into Text::Template
|
|
Packit Service |
084de1 |
# and fill it up with our data. Output goes directly to STDOUT
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
my $template =
|
|
Packit Service |
084de1 |
OpenSSL::Template->new(TYPE => 'STRING',
|
|
Packit Service |
084de1 |
SOURCE => $text,
|
|
Packit Service |
084de1 |
PREPEND => qq{use lib "$FindBin::Bin/perl";});
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
sub output_reset_on {
|
|
Packit Service |
084de1 |
$template->output_reset_on();
|
|
Packit Service |
084de1 |
"";
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
sub output_on {
|
|
Packit Service |
084de1 |
$template->output_on();
|
|
Packit Service |
084de1 |
"";
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
sub output_off {
|
|
Packit Service |
084de1 |
$template->output_off();
|
|
Packit Service |
084de1 |
"";
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
$template->fill_in(OUTPUT => \*STDOUT,
|
|
Packit Service |
084de1 |
HASH => { config => \%config,
|
|
Packit Service |
084de1 |
target => \%target,
|
|
Packit Service |
084de1 |
disabled => \%disabled,
|
|
Packit Service |
084de1 |
withargs => \%withargs,
|
|
Packit Service |
084de1 |
unified_info => \%unified_info,
|
|
Packit Service |
084de1 |
autowarntext => \@autowarntext,
|
|
Packit Service |
084de1 |
quotify1 => \"ify1,
|
|
Packit Service |
084de1 |
quotify_l => \"ify_l,
|
|
Packit Service |
084de1 |
output_reset_on => \&output_reset_on,
|
|
Packit Service |
084de1 |
output_on => \&output_on,
|
|
Packit Service |
084de1 |
output_off => \&output_off },
|
|
Packit Service |
084de1 |
DELIMITERS => [ "{-", "-}" ],
|
|
Packit Service |
084de1 |
BROKEN => \&broken);
|