| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package CGI::Multiscript; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 24269 | use 5.008004; | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 40 |  | 
| 4 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 35 |  | 
| 5 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 7 |  | 
|  | 1 |  |  |  |  | 40 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 1098 | use IO::Handle; | 
|  | 1 |  |  |  |  | 8832 |  | 
|  | 1 |  |  |  |  | 57 |  | 
| 8 | 1 |  |  | 1 |  | 970 | use IO::File; | 
|  | 1 |  |  |  |  | 2578 |  | 
|  | 1 |  |  |  |  | 151 |  | 
| 9 | 1 |  |  | 1 |  | 6 | use Fcntl; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 458 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | require Exporter; | 
| 12 | 1 |  |  | 1 |  | 1006 | use AutoLoader qw(AUTOLOAD); | 
|  | 1 |  |  |  |  | 1683 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | our @ISA = qw(Exporter); | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # Items to export into callers namespace by default. Note: do not export | 
| 17 |  |  |  |  |  |  | # names by default without a very good reason. Use EXPORT_OK instead. | 
| 18 |  |  |  |  |  |  | # Do not simply export all your public functions/methods/constants. | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | # This allows declaration	use CGI::Multiscript ':all'; | 
| 21 |  |  |  |  |  |  | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | 
| 22 |  |  |  |  |  |  | # will save memory. | 
| 23 |  |  |  |  |  |  | our %EXPORT_TAGS = ( 'all' => [ qw( | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | ) ] ); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | our @EXPORT = qw( | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | ); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | our $VERSION = '0.73'; | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | # Preloaded methods go here. | 
| 37 |  |  |  |  |  |  | our $writeflag = 0; | 
| 38 |  |  |  |  |  |  | our $tmpfilename; | 
| 39 |  |  |  |  |  |  | our $TMPFILE; | 
| 40 |  |  |  |  |  |  | our $default; | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | sub new { | 
| 43 | 1 |  |  | 1 | 0 | 7 | my ($filename) = @_; | 
| 44 | 1 |  |  |  |  | 2 | my ($self) = {}; | 
| 45 | 1 |  |  |  |  | 2 | bless ($self); | 
| 46 | 1 |  |  |  |  | 10 | $self->{'FILE'} = $filename; | 
| 47 | 1 |  |  |  |  | 4 | $self->{'LANGS'} = 0; | 
| 48 | 1 |  |  |  |  | 3 | return $self; | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | # set default language executor | 
| 52 |  |  |  |  |  |  | sub setDefault { | 
| 53 | 2 |  |  | 2 | 0 | 13 | my ($value) = @_; | 
| 54 | 2 |  |  |  |  | 7 | $default = $value; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | # get the default language executor | 
| 58 |  |  |  |  |  |  | sub getDefault { | 
| 59 | 1 |  |  | 1 | 0 | 36 | return $default; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | # set the Multiscript filename to execute | 
| 63 |  |  |  |  |  |  | sub setFilename { | 
| 64 | 1 |  |  | 1 | 0 | 6 | my ($self, $value) = @_; | 
| 65 | 1 |  |  |  |  | 4 | $self->{'FILE'} = $value; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | # get the current Multiscript filename | 
| 69 |  |  |  |  |  |  | sub getFilename { | 
| 70 | 1 |  |  | 1 | 0 | 6 | my ($self) = @_; | 
| 71 | 1 |  |  |  |  | 8 | return $self->{'FILE'}; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | # display the current Multiscript filename | 
| 75 |  |  |  |  |  |  | sub displayFilename { | 
| 76 | 0 |  |  | 0 | 0 | 0 | my ($self) = @_; | 
| 77 | 0 |  |  |  |  | 0 | print $self->{'FILE'}, "\n"; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | # add a language to the Multiscript execution list | 
| 81 |  |  |  |  |  |  | sub addLanguage { | 
| 82 | 2 |  |  | 2 | 0 | 12 | my ($self, $lang, $args) = @_; | 
| 83 | 2 |  |  |  |  | 5 | $self->{$lang} = $args; | 
| 84 | 2 |  |  |  |  | 5 | $self->{'LANGS'}++; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | # add a language version to the Multiscript execution list | 
| 88 |  |  |  |  |  |  | sub addVersion { | 
| 89 | 0 |  |  | 0 | 0 | 0 | my ($self, $version, $args) = @_; | 
| 90 | 0 |  |  |  |  | 0 | $self->{$version} = $args; | 
| 91 | 0 |  |  |  |  | 0 | $self->{'LANGS'}++; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | # add a language name to the Multiscript execution list | 
| 95 |  |  |  |  |  |  | sub addName { | 
| 96 | 0 |  |  | 0 | 0 | 0 | my ($self, $version, $args) = @_; | 
| 97 | 0 |  |  |  |  | 0 | $self->{$version} = $args; | 
| 98 | 0 |  |  |  |  | 0 | $self->{'LANGS'}++; | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | # get the number of current languages in the execution list | 
| 103 |  |  |  |  |  |  | sub getNumberoflangs { | 
| 104 | 0 |  |  | 0 | 0 | 0 | my ($self) = @_; | 
| 105 | 0 |  |  |  |  | 0 | my $number; | 
| 106 | 0 |  |  |  |  | 0 | $number = $self->{'LANGS'}; | 
| 107 | 0 |  |  |  |  | 0 | return $number; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | # display the number of languages in the execution list | 
| 111 |  |  |  |  |  |  | sub displayLangs { | 
| 112 | 1 |  |  | 1 | 0 | 15 | my ($self) = @_; | 
| 113 | 1 |  |  |  |  | 2 | my $keys = 0; | 
| 114 | 1 |  |  |  |  | 13 | print "There are ", $self->{'LANGS'}, " languages selected\n"; | 
| 115 |  |  |  |  |  |  | # print "The languages/versions/names scheduled for execution are:\n"; | 
| 116 |  |  |  |  |  |  | # while ($keys < $self->{'LANGS'}) | 
| 117 |  |  |  |  |  |  | # { | 
| 118 |  |  |  |  |  |  | # print "$self->{'LANGS'}\n"; | 
| 119 |  |  |  |  |  |  | # $keys++; | 
| 120 |  |  |  |  |  |  | # } | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | sub get { | 
| 124 | 0 |  |  | 0 | 0 | 0 | my ($self, $key) = @_; | 
| 125 | 0 |  |  |  |  | 0 | return $self->{$key}; | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | # parse command line arguments into the language execution list | 
| 129 |  |  |  |  |  |  | sub parseArgs { | 
| 130 | 1 |  |  | 1 | 0 | 8 | my ($self, @parseArgs) = @_; | 
| 131 | 1 |  |  |  |  | 3 | my $argnum; | 
| 132 | 1 |  |  |  |  | 6 | foreach $argnum (0 .. $#parseArgs) { | 
| 133 |  |  |  |  |  |  | # print "$ARGV[$argnum]\n"; | 
| 134 | 0 |  |  |  |  | 0 | $self->{$ARGV[$argnum]} = ""; | 
| 135 | 0 |  |  |  |  | 0 | $self->{'LANGS'}++; | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | } | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | # exeute the current file in the Multiscript object | 
| 141 |  |  |  |  |  |  | sub execute { | 
| 142 | 1 |  |  | 1 | 0 | 6 | my ($self) = @_; | 
| 143 |  |  |  |  |  |  |  | 
| 144 | 1 |  |  |  |  | 2 | my $filename; | 
| 145 |  |  |  |  |  |  | my $line; | 
| 146 | 0 |  |  |  |  | 0 | my $currentLanguage; | 
| 147 | 0 |  |  |  |  | 0 | my $currentVersion; | 
| 148 | 0 |  |  |  |  | 0 | my $currentName; | 
| 149 | 0 |  |  |  |  | 0 | my $currentArgs; | 
| 150 |  |  |  |  |  |  |  | 
| 151 | 1 |  |  |  |  | 3 | $filename = $self->{'FILE'}; | 
| 152 |  |  |  |  |  |  |  | 
| 153 | 1 | 50 |  |  |  | 55 | open (CODEFILE, $filename) or die "Can't Open Multiscript $filename"; | 
| 154 | 1 |  |  |  |  | 5 | $tmpfilename = get_tmpfilename(); | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | # print "Creating a new script temp file $tmpfilename\n"; | 
| 157 | 1 |  |  |  |  | 9 | umask 077; | 
| 158 | 1 | 50 |  |  |  | 158 | open ($TMPFILE, ">$tmpfilename") or die $!; | 
| 159 |  |  |  |  |  |  |  | 
| 160 | 1 |  |  |  |  | 4 | $currentLanguage = ""; | 
| 161 | 1 |  |  |  |  | 3 | $currentVersion = ""; | 
| 162 | 1 |  |  |  |  | 1 | $currentName = ""; | 
| 163 | 1 |  |  |  |  | 2 | $currentArgs = ""; | 
| 164 |  |  |  |  |  |  |  | 
| 165 | 1 |  |  |  |  | 27 | while ($line = ) { | 
| 166 |  |  |  |  |  |  | # print $line; | 
| 167 | 19 | 100 |  |  |  | 67 | if ($line =~ /^ \n/) {  | 
| 168 | 1 |  |  |  |  | 21 | $currentLanguage = $1; | 
| 169 | 1 |  |  |  |  | 9 | $currentVersion  = $2; | 
| 170 | 1 |  |  |  |  | 11 | $currentName 	 = $3; | 
| 171 | 1 |  |  |  |  | 45 | $currentArgs 	 = $4; | 
| 172 | 1 |  |  |  |  | 11 | $line = ""; # tmp fix | 
| 173 |  |  |  |  |  |  | # print "Current ", $currentLanguage, " ", $currentVersion, "\n"; | 
| 174 | 1 |  |  |  |  | 18 | set_writeflag(1); | 
| 175 |  |  |  |  |  |  | } | 
| 176 | 19 | 100 |  |  |  | 120 | if ($line =~ /^ \n/) {  | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | # print "Current Code lang $line\n"; | 
| 178 | 2 |  |  |  |  | 17 | $currentLanguage = $1; | 
| 179 | 2 |  |  |  |  | 4 | $currentArgs = ""; | 
| 180 | 2 |  |  |  |  | 5 | $line = ""; | 
| 181 | 2 |  |  |  |  | 9 | set_writeflag(2); | 
| 182 |  |  |  |  |  |  | } | 
| 183 |  |  |  |  |  |  | elsif ($line =~ /^ \n/) {  | 
| 184 | 2 |  |  |  |  | 5 | $currentLanguage = ""; | 
| 185 | 2 |  |  |  |  | 7 | $currentArgs = ""; | 
| 186 | 2 |  |  |  |  | 7 | set_writeflag(3); | 
| 187 |  |  |  |  |  |  | } | 
| 188 |  |  |  |  |  |  | elsif ($line =~ /^<\/code>\n/) { | 
| 189 | 5 |  |  |  |  | 19 | clear_writeflag(1); | 
| 190 |  |  |  |  |  |  | # if should run and is in argument list | 
| 191 | 5 | 50 |  |  |  | 50 | if ($self->{'LANGS'} == 0) { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 192 | 0 |  |  |  |  | 0 | execTmpfile($currentLanguage, $currentArgs); | 
| 193 |  |  |  |  |  |  | } | 
| 194 |  |  |  |  |  |  | elsif (exists $self->{$currentLanguage} ) { | 
| 195 | 2 |  |  |  |  | 10 | execTmpfile($currentLanguage, $currentArgs); | 
| 196 |  |  |  |  |  |  | } | 
| 197 |  |  |  |  |  |  | elsif (exists $self->{$currentName} ) { | 
| 198 | 0 |  |  |  |  | 0 | execTmpfile($currentLanguage, $currentArgs); | 
| 199 |  |  |  |  |  |  | } | 
| 200 |  |  |  |  |  |  | elsif (exists $self->{$currentVersion} ) { | 
| 201 | 0 |  |  |  |  | 0 | execTmpfile($currentLanguage, $currentArgs); | 
| 202 |  |  |  |  |  |  | } | 
| 203 | 5 |  |  |  |  | 59 | truncateTmpfile(); | 
| 204 | 5 |  |  |  |  | 24 | $currentLanguage = ""; | 
| 205 | 5 |  |  |  |  | 16 | $currentVersion = ""; | 
| 206 | 5 |  |  |  |  | 14 | $currentName = ""; | 
| 207 | 5 |  |  |  |  | 95 | $currentArgs = ""; | 
| 208 |  |  |  |  |  |  | } | 
| 209 |  |  |  |  |  |  | else | 
| 210 |  |  |  |  |  |  | { | 
| 211 | 10 | 50 |  |  |  | 21 | if ($writeflag != 0) { | 
| 212 |  |  |  |  |  |  | # print "Writing", $line; | 
| 213 | 10 |  |  |  |  | 43 | print $TMPFILE $line; | 
| 214 |  |  |  |  |  |  | } | 
| 215 |  |  |  |  |  |  | } | 
| 216 |  |  |  |  |  |  | } | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  |  | 
| 219 | 1 |  |  |  |  | 23 | close($TMPFILE); | 
| 220 | 1 |  |  |  |  | 11 | close(CODEFILE); | 
| 221 | 1 |  |  |  |  | 250 | unlink($tmpfilename); | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | } | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | # Create a temporary file | 
| 226 |  |  |  |  |  |  | # With a random name | 
| 227 |  |  |  |  |  |  | sub get_tmpfilename() { | 
| 228 | 1 |  |  | 1 | 0 | 2 | my $tmpname; | 
| 229 |  |  |  |  |  |  | my $random; | 
| 230 |  |  |  |  |  |  |  | 
| 231 | 1 |  |  |  |  | 3 | $tmpname = ".ms."; | 
| 232 | 1 |  |  |  |  | 16 | srand(time()); | 
| 233 | 1 |  |  |  |  | 3 | $random = rand(); | 
| 234 | 1 |  |  |  |  | 6 | $tmpname .= "$$"; | 
| 235 | 1 |  |  |  |  | 14 | $tmpname .= $random; | 
| 236 | 1 |  |  |  |  | 2 | $tmpname .= ".tmp"; | 
| 237 |  |  |  |  |  |  |  | 
| 238 |  |  |  |  |  |  | # print "tmpname = $tmpname\n"; | 
| 239 |  |  |  |  |  |  |  | 
| 240 | 1 |  |  |  |  | 3 | return ($tmpname); | 
| 241 |  |  |  |  |  |  |  | 
| 242 |  |  |  |  |  |  | } | 
| 243 |  |  |  |  |  |  |  | 
| 244 |  |  |  |  |  |  | sub set_writeflag() | 
| 245 |  |  |  |  |  |  | { | 
| 246 | 5 |  |  | 5 | 0 | 8 | my $flag = $_[0]; | 
| 247 | 5 | 50 |  |  |  | 14 | if ($writeflag != 0) { | 
| 248 | 0 |  |  |  |  | 0 | print "Code Error -- Not allowed nested code within code!!\n"; | 
| 249 | 0 |  |  |  |  | 0 | unlink($tmpfilename); | 
| 250 | 0 |  |  |  |  | 0 | exit(1); | 
| 251 |  |  |  |  |  |  | } | 
| 252 | 5 |  |  |  |  | 21 | $writeflag = $flag; | 
| 253 |  |  |  |  |  |  |  | 
| 254 |  |  |  |  |  |  | } | 
| 255 |  |  |  |  |  |  |  | 
| 256 |  |  |  |  |  |  | sub clear_writeflag() | 
| 257 |  |  |  |  |  |  | { | 
| 258 | 5 |  |  | 5 | 0 | 7 | my $flag = $_[0]; | 
| 259 | 5 |  |  |  |  | 7 | $writeflag = 0; | 
| 260 |  |  |  |  |  |  | } | 
| 261 |  |  |  |  |  |  |  | 
| 262 |  |  |  |  |  |  | sub execTmpfile() | 
| 263 |  |  |  |  |  |  | { | 
| 264 | 2 |  |  | 2 | 0 | 5 | my ($lang, $args) = @_; | 
| 265 | 2 |  |  |  |  | 3 | my $returncode; | 
| 266 |  |  |  |  |  |  |  | 
| 267 |  |  |  |  |  |  | # print "executing 1 $lang $tmpfilename\n"; | 
| 268 |  |  |  |  |  |  |  | 
| 269 | 2 | 50 | 33 |  |  | 31 | if (($lang eq "") && ($args eq "")) { | 
|  |  | 50 | 33 |  |  |  |  | 
|  |  | 0 | 0 |  |  |  |  | 
|  |  | 0 | 0 |  |  |  |  | 
| 270 | 0 |  |  |  |  | 0 | $returncode = system("$default$tmpfilename"); | 
| 271 |  |  |  |  |  |  | } | 
| 272 |  |  |  |  |  |  | elsif (($lang ne "") && ($args eq "")) { | 
| 273 | 2 |  |  |  |  | 16928 | $returncode = system("$lang $tmpfilename"); | 
| 274 |  |  |  |  |  |  | } | 
| 275 |  |  |  |  |  |  | elsif (($lang eq "") && ($args ne "")) { | 
| 276 | 0 |  |  |  |  | 0 | $returncode = system("$default$tmpfilename $args"); | 
| 277 |  |  |  |  |  |  | } | 
| 278 |  |  |  |  |  |  | elsif (($lang ne "") && ($args ne "")) { | 
| 279 | 0 |  |  |  |  | 0 | $returncode = system("$lang $tmpfilename $args"); | 
| 280 |  |  |  |  |  |  | } | 
| 281 |  |  |  |  |  |  |  | 
| 282 |  |  |  |  |  |  | } | 
| 283 |  |  |  |  |  |  |  | 
| 284 |  |  |  |  |  |  |  | 
| 285 |  |  |  |  |  |  | sub truncateTmpfile() | 
| 286 |  |  |  |  |  |  | { | 
| 287 | 5 |  |  | 5 | 0 | 121 | seek($TMPFILE, 0, 0); | 
| 288 | 5 |  |  |  |  | 275 | truncate($TMPFILE, 0); | 
| 289 |  |  |  |  |  |  | } | 
| 290 |  |  |  |  |  |  |  | 
| 291 |  |  |  |  |  |  | # Autoload methods go after =cut, and are processed by the autosplit program. | 
| 292 |  |  |  |  |  |  |  | 
| 293 |  |  |  |  |  |  | 1; | 
| 294 |  |  |  |  |  |  | __END__ |