/usr/share/perl5/Rex/Template/NG.pm is in rex 1.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | #
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:
package Rex::Template::NG;
use strict;
use warnings;
our $VERSION = '1.4.1'; # VERSION
sub new {
my $that = shift;
my $proto = ref($that) || $that;
my $self = {@_};
bless( $self, $proto );
$self->_init();
return $self;
}
sub _init {
my ($self) = @_;
$self->{__output__} = "";
$self->{__code__} = "";
$self->{__raw_data__} = "";
}
sub parse {
my $self = shift;
my $c = shift;
my %in_vars;
$self->_init();
if ( ref $_[0] eq "HASH" ) {
%in_vars = %{ +shift };
}
else {
%in_vars = @_;
}
my %vars;
for my $key ( keys %in_vars ) {
my $new_key = $key;
$new_key =~ s/[^a-zA-Z0-9_]/_/gms;
$vars{$new_key} = $in_vars{$key};
}
# some backward compat. to old template module.
$c =~ s/\$::([a-zA-Z0-9_]+)/_replace_var($1, \%vars)/egms;
my $code = "";
my $var_data = '
return sub {
my (
$self,
';
my @code_values;
for my $var ( keys %vars ) {
$var_data .= '$' . $var . ", \n";
push( @code_values, $vars{$var} );
}
$var_data .= '$this_is_really_nothing) = @_;';
$var_data .= "\n";
$code = $var_data;
$code .= $self->_parse($c);
$code .= "\n}";
my $idx_c = 1;
for my $l ( split( /\n/, $code ) ) {
$idx_c++;
$l ||= "";
Rex::Logger::debug("$idx_c. $l");
}
$self->{__code__} = $code;
$self->{__raw_data__} = $c;
no warnings;
my $tpl_code = eval($code);
use warnings;
if ($@) {
my $error = $@;
my ($error_line) = ( $error =~ m/line (\d+)[\.,]/ );
my @code_lines = split( /\n/, $code );
my @raw_lines = split( /\n/, $c );
my $idx = $error_line - 5;
for my $l ( @code_lines[ $error_line - 5 .. $error_line + 5 ] ) {
$idx++;
$l ||= "";
Rex::Logger::debug("$idx. $l");
}
my $template_line = 0;
my $add_to_error_line = -1;
# search the error line
Rex::Logger::debug("Template-Error-Line: $error_line");
for ( my $bi = $error_line - 1 ; $bi >= 0 ; $bi-- ) {
if ( $code_lines[$bi] =~ m/^# LINE: (\d+)$/ ) {
$template_line = $1 + $add_to_error_line;
last;
}
$add_to_error_line++;
}
if ( !$template_line ) {
die "Uncatchable error in template: $error ($error_line)";
}
my $start_part = $template_line - 5;
$start_part = 0 if $start_part <= 0;
my $end_part = $template_line + 5;
$end_part = scalar @raw_lines if $end_part > scalar @raw_lines;
my $idx_t = $start_part;
for my $l ( @raw_lines[ $start_part .. $end_part ] ) {
$idx_t++;
$l ||= "";
Rex::Logger::info("$idx_t. $l");
}
my $tpl_error = $error;
$tpl_error =~ s/at \(eval \d+\) line \d+/at template line $template_line/;
if ( $error =~ m/Global symbol "([^"]+)" requires explicit package name/ ) {
$tpl_error =
"Unknown variable name $1 in code line: ,,$raw_lines[$template_line-1]'' line: $template_line.\nOriginal Error:\n$error\n";
}
# internal parsing error, maybe runaway line without ";"
elsif ( $raw_lines[ $template_line - 2 ] =~ m/^%/
&& $raw_lines[ $template_line - 2 ] !~ m/[;{("']/ )
{
Rex::Logger::debug(
"Template Error in compiled line: $code_lines[$error_line-1]");
Rex::Logger::info(
"Template Error somewhere around: $raw_lines[$template_line-2]",
"error" );
my $template_line_ = $template_line - 1;
$tpl_error =
"Maybe missing <<;, {, (, \" or '>> in code line: ,,$raw_lines[$template_line-2]'' line $template_line_.\nOriginal Error:\n$error\n";
}
else {
$tpl_error =
"Failed parsing template. Unkown error near $template_line.\nOriginal Error:\n$error\n";
}
die $tpl_error;
}
$tpl_code->( $self, @code_values );
return $self->{__output__};
}
sub __out {
my ( $self, $str ) = @_;
$self->{__output__} .= defined $str ? $str : "";
}
sub _parse {
my ( $self, $c ) = @_;
my $parsed = "";
my @chars = split( //, $c );
my $begin_line = 0;
my $code_line = 0;
my $code_block = 0;
my $code_block_output = 0;
my $current_char_idx = -1;
my $line_count = 1;
my $string_open = 0;
my $skip_next = 0;
my $skip_next_newline = 0;
for my $curr_char (@chars) {
$current_char_idx++;
if ($skip_next) {
$skip_next = 0;
next;
}
my $prev_char = $chars[ $current_char_idx - 1 ] || "";
my $next_char = $chars[ $current_char_idx + 1 ] || "";
if ( $skip_next_newline && $curr_char eq "\n" ) {
$skip_next_newline = 0;
$curr_char = "";
}
if ( $curr_char eq "\n" && $prev_char ne "\n" ) { # count lines, for error messages
$line_count++;
$parsed .= $curr_char;
if ($string_open) {
$parsed .= "});\n";
}
# reset vars
$code_line = 0;
$string_open = 0;
next;
}
if ( $curr_char eq "\n" && $prev_char eq "\n" ) {
$parsed .= "\$self->__out(q{\n});\n";
$line_count++;
next;
}
if ( $curr_char eq "-"
&& $next_char eq "%"
&& ( $prev_char eq " " || $prev_char eq "\n" )
&& $chars[ $current_char_idx + 2 ] eq ">" )
{
# skip "-" of -%> sequence
$skip_next_newline = 1;
next;
}
# catch code line
# % some code
if (
!$code_block
&& ( $prev_char eq "\n"
|| $current_char_idx == 0 ) # first line or new line
&& $curr_char eq "%"
&& $next_char eq " " # code block, and no % char escape sequence
)
{
$code_line = 1;
$parsed .= "\n# LINE: $line_count\n";
next;
}
# catch '<% ' ...
if ( $prev_char eq "<"
&& $curr_char eq "%"
&& ( $next_char eq " " || $next_char eq "\n" ) )
{
$code_block = 1;
if ($string_open) {
$parsed .= "});\n";
$string_open = 0;
}
$parsed .= "\n# LINE: $line_count\n";
next;
}
# catch ' %>'
if (
$code_block
&& ( ( $code_block_output || $prev_char eq " " )
|| $prev_char eq "\n"
|| $prev_char eq "-" )
&& $curr_char eq "%"
&& $next_char eq ">"
)
{
$code_block = 0;
if ($code_block_output) {
$parsed .= ");\n";
$code_block_output = 0;
}
$string_open = 1;
$parsed .= "\n\$self->__out(q{";
next;
}
# catch '<%='
if ( $prev_char eq "<" && $curr_char eq "%" && $next_char eq "=" ) {
$code_block = 1;
$code_block_output = 1;
if ($string_open) {
$parsed .= "});\n";
}
$parsed .= "\n# LINE: $line_count\n";
$parsed .= "\$self->__out(";
$skip_next = 1;
next;
}
if ( $code_line || $code_block ) {
$parsed .= $curr_char;
next;
}
if ( !$string_open ) {
$string_open = 1;
$parsed .= '$self->__out(q{';
}
# don't catch opening <
if ( $curr_char eq "<" && $next_char eq "%" ) {
next;
}
# don't catch closing >
if ( $curr_char eq ">" && $prev_char eq "%" ) {
next;
}
# escaping of % sign
if ( $curr_char eq "%" && $prev_char eq "%" ) {
next;
}
$parsed .= $curr_char =~ m/[{}]/ ? "\\$curr_char" : $curr_char;
}
if ($string_open) {
$parsed .= "});\n";
}
return $parsed;
}
sub _replace_var {
my ( $var, $t_vars ) = @_;
if ( exists $t_vars->{$var} ) {
return '$' . $var;
}
else {
return '$::' . $var;
}
}
1;
|