A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
4.0 KiB

  1. #! /usr/bin/env perl
  2. # Copyright 2016-2019 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. # Implements the functionality to read one or more template files and run
  9. # them through Text::Template
  10. package OpenSSL::Template;
  11. =head1 NAME
  12. OpenSSL::Template - a private extension of Text::Template
  13. =head1 DESCRIPTION
  14. This provides exactly the functionality from Text::Template, with the
  15. following additions:
  16. =over 4
  17. =item -
  18. The template perl code delimiters (given with the C<DELIMITER> option)
  19. are set to C<{-> and C<-}> by default.
  20. =item -
  21. A few extra functions are offered to be used by the template perl code, see
  22. L</Functions>.
  23. =back
  24. =cut
  25. use File::Basename;
  26. use File::Spec::Functions;
  27. use Text::Template 1.46;
  28. our @ISA = qw(Text::Template); # parent
  29. sub new {
  30. my $class = shift;
  31. # Call the constructor of the parent class.
  32. my $self = $class->SUPER::new(DELIMITERS => [ '{-', '-}'],
  33. @_ );
  34. # Add few more attributes
  35. $self->{_output_off} = 0; # Default to output hunks
  36. return bless $self, $class;
  37. }
  38. sub fill_in {
  39. my $self = shift;
  40. my %opts = @_;
  41. my %hash = ( %{$opts{HASH}} );
  42. delete $opts{HASH};
  43. $self->SUPER::fill_in(HASH => { quotify1 => \&quotify1,
  44. quotify_l => \&quotify_l,
  45. output_on => sub { $self->output_on() },
  46. output_off => sub { $self->output_off() },
  47. %hash },
  48. %opts);
  49. }
  50. =head2 Functions
  51. =cut
  52. # Override Text::Template's append_text_to_result, as recommended here:
  53. #
  54. # http://search.cpan.org/~mjd/Text-Template-1.46/lib/Text/Template.pm#Automatic_postprocessing_of_template_hunks
  55. sub append_text_to_output {
  56. my $self = shift;
  57. if ($self->{_output_off} == 0) {
  58. $self->SUPER::append_text_to_output(@_);
  59. }
  60. return;
  61. }
  62. =begin comment
  63. We lie about the OO nature of output_on() and output_off(), 'cause that's
  64. not how we pass them, see the HASH option used in fill_in() above
  65. =end comment
  66. =over 4
  67. =item output_on()
  68. =item output_off()
  69. Switch on or off template output. Here's an example usage:
  70. =over 4
  71. {- output_off() if CONDITION -}
  72. whatever
  73. {- output_on() if CONDITION -}
  74. =back
  75. In this example, C<whatever> will only become part of the template output
  76. if C<CONDITION> is true.
  77. =back
  78. =cut
  79. sub output_on {
  80. my $self = shift;
  81. if (--$self->{_output_off} < 0) {
  82. $self->{_output_off} = 0;
  83. }
  84. }
  85. sub output_off {
  86. my $self = shift;
  87. $self->{_output_off}++;
  88. }
  89. # Helper functions for the templates #################################
  90. # It might be practical to quotify some strings and have them protected
  91. # from possible harm. These functions primarily quote things that might
  92. # be interpreted wrongly by a perl eval.
  93. # NOTE THAT THESE AREN'T CLASS METHODS!
  94. =over 4
  95. =item quotify1 STRING
  96. This adds quotes (") around the given string, and escapes any $, @, \,
  97. " and ' by prepending a \ to them.
  98. =back
  99. =cut
  100. sub quotify1 {
  101. my $s = shift @_;
  102. $s =~ s/([\$\@\\"'])/\\$1/g;
  103. '"'.$s.'"';
  104. }
  105. =over 4
  106. =item quotify_l LIST
  107. For each defined element in LIST (i.e. elements that aren't undef), have
  108. it quotified with 'quotify1'.
  109. Undefined elements are ignored.
  110. =back
  111. =cut
  112. sub quotify_l {
  113. map {
  114. if (!defined($_)) {
  115. ();
  116. } else {
  117. quotify1($_);
  118. }
  119. } @_;
  120. }
  121. =head1 SEE ALSO
  122. L<Text::Template>
  123. =head1 AUTHORS
  124. Richard Levitte E<lt>levitte@openssl.orgE<gt>
  125. =head1 COPYRIGHT
  126. Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
  127. Licensed under the Apache License 2.0 (the "License"). You may not use
  128. this file except in compliance with the License. You can obtain a copy
  129. in the file LICENSE in the source distribution or at
  130. L<https://www.openssl.org/source/license.html>.
  131. =cut