#!/usr/bin/perl #WebWarper: processing simple include directive unless(@ARGV) { die "Usage: processinclude.pl filemask1[,filemask2,filemask3...] {constantsfile}\n" ."constantsfile is a set of lines such as\n" ." name=\"value\"\n" ."These constants can be used in included files in <%=name%> format.\n"; } sub readInclude { my($params,$basePath,$defResult,$constants,$pErrorFlag)= @_; my(%ATTRS)= (); my(%CONST)= %$constants; $params=~s/(\w+)\s*=\s*\"([^\"]*)\"/$ATTRS{$1}=$2/ges; my $PAGE_PATH= $basePath; my $PAGE_FILE_NAME= $basePath; if ($basePath=~s/(^|\/|\\)([^\\\/]+)$/$1/s) { $PAGE_FILE_NAME= $2; } my $PAGE_FILE_NAME_WITHOUT_EXTENSION= $PAGE_FILE_NAME; $PAGE_FILE_NAME_WITHOUT_EXTENSION=~s/\.[^\.]+$//s; my $incFileName= $ATTRS{"file"}; unless ($incFileName) { $$pErrorFlag=1; return "\n".$defResult; } if ($incFileName eq "\$PAGE_PATH") { $incFileName= $PAGE_PATH; } elsif ($incFileName!~/^(\/|\\|\w+:)/s) { $incFileName= $basePath.$incFileName; } my $incSectionName= $ATTRS{"section"}; $incSectionName=~s/\$PAGE_FILE_NAME_WITHOUT_EXTENSION/$PAGE_FILE_NAME_WITHOUT_EXTENSION/gs; $defResult=~s/^\s*//is; local(*G); my($buf); unless (open(G,$incFileName)) { $$pErrorFlag=1; return "\n".$defResult; } binmode(G); unless (read(G,$buf,-s G)) { $$pErrorFlag=1; return "\n".$defResult; } close(G); if ($incSectionName) { if ($buf=~/(.*?)/is) { $buf= $1; } else { $$pErrorFlag=1; return "\n".$defResult; } } $buf=~s/.*?\s*(\n\r?|\r|$)?//gis; $buf=~s///gis; for (my $depth=9; $depth>=0; $depth--) { my $caseKeyword= $depth>0? "case$depth": "case"; # xxxxxxx # $1: # $6: xxxxxxx $buf=~s/()(.*?)(?=)(.*?)(?=)?//gis; # remove only "endcase" statements which has processed above, # not remove "endcase" that could appear inside case/endcase preserved by "$1$2$3$4$5$6" } my $evalRes; $buf=~s/(<%=\s*_eval:)(.*?)(%>)/ defined($evalRes=eval($2))? $evalRes: $1.$2.$3/ges; $buf=~s/(<%=\s*)(\w+)(\s*%>)/ defined $ATTRS{$2} && ($ATTRS{$2} ne "n\/a")? $ATTRS{$2}: defined $CONST{$2} && ($CONST{$2} ne "n\/a")? $CONST{$2}: $1.$2.$3/ges; for (my $i=1; $i<=20; $i++) { my $find = $ATTRS{"FIND_$i"}; my $replace = $ATTRS{"REPLACE_$i"}; if ($find && $replace) { #print "$find => $replace\n"; eval('$buf=~s/'.$find.'/'.$replace.'/gis'); } } return $buf; } my @processedFileMasks= split(/,/,$ARGV[0]); my $constantsFileName= $ARGV[1]; my(%constants)= (); if ($constantsFileName) { local(*C); open(C,$constantsFileName) || die "Error: cannot open \"$constantsFileName\". $!\n"; %constants= (); while (my $line=) { if ($line=~/^\s*(\w+)\s*=\s*\"([^\"]*)\"/s) { $constants{$1}=$2; } } close(C); } for (my $argIndex=2; defined $ARGV[$argIndex]; $argIndex+=2) { $constants{$ARGV[$argIndex]}=$ARGV[$argIndex+1]; } my $processedFileMask; my $processedFileName; foreach $processedFileMask(@processedFileMasks) { foreach $processedFileName(glob($processedFileMask)) { next if $processedFileName=~/pr\.html?$/s; local(*F); my($buf); open(F,$processedFileName) || die "Error: cannot open \"$processedFileName\". $!\n"; binmode(F); read(F,$buf,-s F) || die "Error: cannot read \"$processedFileName\". $!\n"; close(F); my $bufsave= $buf; my $errorFlag= 0; my $wwincludeExist= $buf=~s/()(.*?)()/ $1.$2.$3.readInclude($2,$processedFileName,$4,\%constants,\$errorFlag).$5/gies; if ($buf eq $bufsave) { if ($wwincludeExist && $errorFlag) { print "$processedFileName not changed; there were ERRORS\n"; } } else { open(F,">$processedFileName") || die "Error: cannot write to $processedFileName. System error message: $!"; binmode(F); print(F $buf) || die "Error: cannot write to $processedFileName. System error message: $!"; close(F); print "$processedFileName CORRECTED".($errorFlag?"; there were ERRORS":"")."\n"; } } }