| 1 | # | ||
| 2 | # $Id: Env.pm,v 0.1 2001/04/25 10:41:48 ram Exp $ | ||
| 3 | # | ||
| 4 | # Copyright (c) 2000-2001, Raphael Manfredi | ||
| 5 | # | ||
| 6 | # You may redistribute only under the terms of the Artistic License, | ||
| 7 | # as specified in the README file that comes with the distribution. | ||
| 8 | # | ||
| 9 | # HISTORY | ||
| 10 | # $Log: Env.pm,v $ | ||
| 11 | # Revision 0.1 2001/04/25 10:41:48 ram | ||
| 12 | # Baseline for first Alpha release. | ||
| 13 | # | ||
| 14 | # $EndLog$ | ||
| 15 | # | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | |||
| 19 | package Pod::PP::Env; | ||
| 20 | |||
| 21 | require Clone; | ||
| 22 | use vars qw(@ISA); | ||
| 23 | @ISA = qw(Clone); | ||
| 24 | |||
| 25 | use Cwd qw(abs_path); | ||
| 26 | use File::Basename; | ||
| 27 | use File::Spec; | ||
| 28 | use Carp::Datum; | ||
| 29 | use Log::Agent; | ||
| 30 | use Safe; | ||
| 31 | |||
| 32 | # | ||
| 33 | # ->make | ||
| 34 | # | ||
| 35 | # Creation routine | ||
| 36 | # | ||
| 37 | sub make { | ||
| 38 | DFEATURE my $f_; | ||
| 39 | my $self = bless {}, shift; | ||
| 40 | my ($incpath, $symbols) = @_; | ||
| 41 | $self->{incpath} = Clone::clone($incpath); | ||
| 42 | $self->{symbols} = Clone::clone($symbols); | ||
| 43 | $self->{parsed} = {}; | ||
| 44 | $self->{safe} = Safe->new; | ||
| 45 | return DVAL $self; | ||
| 46 | } | ||
| 47 | |||
| 48 | # | ||
| 49 | # Attribute access | ||
| 50 | # | ||
| 51 | |||
| 52 | sub incpath { $_[0]->{incpath} } | ||
| 53 | sub symbols { $_[0]->{symbols} } | ||
| 54 | sub parsed { $_[0]->{parsed} } | ||
| 55 | sub safe { $_[0]->{safe} } | ||
| 56 | |||
| 57 | ### | ||
| 58 | ### Symbol processing | ||
| 59 | ### | ||
| 60 | |||
| 61 | # | ||
| 62 | # ->is_defined | ||
| 63 | # | ||
| 64 | # Test whether symbol is defined | ||
| 65 | # | ||
| 66 | sub is_defined { | ||
| 67 | DFEATURE my $f_; | ||
| 68 | my $self = shift; | ||
| 69 | my ($sym) = @_; | ||
| 70 | |||
| 71 | return DVAL exists $self->symbols->{$sym}; | ||
| 72 | } | ||
| 73 | |||
| 74 | # | ||
| 75 | # ->define | ||
| 76 | # | ||
| 77 | # Record symbol definition | ||
| 78 | # | ||
| 79 | sub define { | ||
| 80 | DFEATURE my $f_; | ||
| 81 | my $self = shift; | ||
| 82 | my ($sym, $value) = @_; | ||
| 83 | |||
| 84 | $self->symbols->{$sym} = $value; | ||
| 85 | return DVOID; | ||
| 86 | } | ||
| 87 | |||
| 88 | # | ||
| 89 | # ->undefine | ||
| 90 | # | ||
| 91 | # Undefine symbol. | ||
| 92 | # | ||
| 93 | sub undefine { | ||
| 94 | DFEATURE my $f_; | ||
| 95 | my $self = shift; | ||
| 96 | my ($sym) = @_; | ||
| 97 | |||
| 98 | delete $self->symbols->{$sym}; | ||
| 99 | return DVOID; | ||
| 100 | } | ||
| 101 | |||
| 102 | # | ||
| 103 | # ->symbol_value | ||
| 104 | # | ||
| 105 | # Returns symbol value. | ||
| 106 | # | ||
| 107 | sub symbol_value { | ||
| 108 | DFEATURE my $f_; | ||
| 109 | my $self = shift; | ||
| 110 | my ($sym) = @_; | ||
| 111 | |||
| 112 | DREQUIRE $self->is_defined($sym), "symbol $sym defined"; | ||
| 113 | |||
| 114 | return DVAL $self->symbols->{$sym}; | ||
| 115 | } | ||
| 116 | |||
| 117 | ### | ||
| 118 | ### Expression processing | ||
| 119 | ### | ||
| 120 | |||
| 121 | # | ||
| 122 | # to_perl | ||
| 123 | # | ||
| 124 | # Transform an expression into an executable perl expression. | ||
| 125 | # Valid expressions are: | ||
| 126 | # | ||
| 127 | # defined(SYM) test for defined SYM | ||
| 128 | # SYM+1 arithmetic on SYM | ||
| 129 | # SYM == 1 numeric test | ||
| 130 | # SYM eq "abc" string test | ||
| 131 | # "string" string, same as 'string' (no interpolation) | ||
| 132 | # 'string' litteral string | ||
| 133 | # | ||
| 134 | # and combinations of things according to normal expression construction, | ||
| 135 | # with "&&", "||" and "!" as boolean connectors and "()" to change order of | ||
| 136 | # evaluation. | ||
| 137 | # | ||
| 138 | # This is a valid expression: | ||
| 139 | # | ||
| 140 | # defined(SYM) || ((OTHER - THIS) ne FOOBAR && SYM <= 43) | ||
| 141 | # | ||
| 142 | # Returns expression that can be evaled. All symbols are transformed into | ||
| 143 | # symbol lookups in $S, supposed to hold a HASH ref. | ||
| 144 | # | ||
| 145 | sub to_perl { | ||
| 146 | DFEATURE my $f_; | ||
| 147 | my $self = shift; | ||
| 148 | my ($expr) = @_; | ||
| 149 | |||
| 150 | logdbg 'debug', "Pod::PP expression: $expr"; | ||
| 151 | |||
| 152 | # | ||
| 153 | # Remove all strings 'x' and "x", and protect string operators | ||
| 154 | # | ||
| 155 | |||
| 156 | my $idx = 0; | ||
| 157 | my @strings; | ||
| 158 | |||
| 159 | $strings[$idx++] = $1 while $expr =~ s/'(.*?)'/\01$idx\01/; | ||
| 160 | $strings[$idx++] = $1 while $expr =~ s/"(.*?)"/\02$idx\02/; | ||
| 161 | $strings[$idx++] = $1 while $expr =~ s/\b(eq|ne|ge|le|gt|lt)\b/\03$idx\03/; | ||
| 162 | |||
| 163 | # | ||
| 164 | # Perform translation into Perl: need only to transform "defined(SYM)" | ||
| 165 | # and all occurrences of "SYM". | ||
| 166 | # | ||
| 167 | |||
| 168 | $expr =~ s/defined\s*\((\w+)\)/exists(\$S->{'$1'})/g; | ||
| 169 | $expr =~ s/ | ||
| 170 | ( | ||
| 171 | ^| # start of line OR | ||
| 172 | [(-+=*\/%^~\s] # arithmetic symbol OR space | ||
| 173 | ) | ||
| 174 | (?!exists\() # not followed by "exists(" | ||
| 175 | ([A-Za-z]\w*) # but followed by identifier | ||
| 176 | /$1\$S->{'$2'}/gx; | ||
| 177 | |||
| 178 | # | ||
| 179 | # Restore all strings 'x' and "x", as well as string operators. | ||
| 180 | # | ||
| 181 | |||
| 182 | $expr =~ s/\01(\d+)\01/"'" . $strings[$1] . "'"/ge; | ||
| 183 | $expr =~ s/\02(\d+)\02/'"' . $strings[$1] . '"'/ge; | ||
| 184 | $expr =~ s/\03(\d+)\03/ $strings[$1] /ge; | ||
| 185 | |||
| 186 | logdbg 'debug', "Perl expression: $expr"; | ||
| 187 | |||
| 188 | return DVAL $expr; | ||
| 189 | } | ||
| 190 | |||
| 191 | # | ||
| 192 | # ->evaluate | ||
| 193 | # | ||
| 194 | # Evaluate Pod::PP boolean expression. | ||
| 195 | # Returns undef when expression does not evaluate properly | ||
| 196 | # | ||
| 197 | sub evaluate { | ||
| 198 | DFEATURE my $f_; | ||
| 199 | my $self = shift; | ||
| 200 | my ($expr, $podinfo) = @_; | ||
| 201 | |||
| 202 | my $perl = $self->to_perl($expr); # Transform into Perl | ||
| 203 | |||
| 204 | # | ||
| 205 | # Evaluate the Perl expression within the Safe compartment. | ||
| 206 | # | ||
| 207 | # The expression expects variable $S to hold the HASH ref to the | ||
| 208 | # symbol table. The only way to share it with the Safe compartment | ||
| 209 | # is by using a global... | ||
| 210 | # | ||
| 211 | |||
| 212 | my $safe = $self->safe; | ||
| 213 | my $val; | ||
| 214 | |||
| 215 | { | ||
| 216 | no strict 'vars'; | ||
| 217 | |||
| 218 | local $S = $self->symbols; | ||
| 219 | $safe->share('$S'); | ||
| 220 | $val = $safe->reval($perl); | ||
| 221 | } | ||
| 222 | |||
| 223 | if (chop $@) { | ||
| 224 | my ($file, $line) = $podinfo->file_line(); | ||
| 225 | logerr "error in Pod:PP expression '%s' at \"%s\", line %d: %s", | ||
| 226 | $expr, $file, $line, $@; | ||
| 227 | return DVAL undef; | ||
| 228 | } | ||
| 229 | |||
| 230 | return DVAL $val ? 1 : 0; | ||
| 231 | } | ||
| 232 | |||
| 233 | ### | ||
| 234 | ### Include file processing | ||
| 235 | ### | ||
| 236 | |||
| 237 | # | ||
| 238 | # ->is_parsed | ||
| 239 | # | ||
| 240 | # Check whether file was parsed. | ||
| 241 | # | ||
| 242 | sub is_parsed { | ||
| 243 | DFEATURE my $f_; | ||
| 244 | my $self = shift; | ||
| 245 | my ($path) = @_; | ||
| 246 | |||
| 247 | my $absolute = $self->absolute_path($path); | ||
| 248 | return DVAL exists $self->parsed->{$absolute}; | ||
| 249 | } | ||
| 250 | |||
| 251 | # | ||
| 252 | # ->set_is_parsed | ||
| 253 | # | ||
| 254 | # Mark file as parsed. | ||
| 255 | # | ||
| 256 | sub set_is_parsed { | ||
| 257 | DFEATURE my $f_; | ||
| 258 | my $self = shift; | ||
| 259 | my ($path) = @_; | ||
| 260 | my $absolute = $self->absolute_path($path); | ||
| 261 | |||
| 262 | DREQUIRE !$self->is_parsed($absolute), "file '$path' not parsed already"; | ||
| 263 | |||
| 264 | $self->parsed->{$absolute} = undef; | ||
| 265 | return DVOID; | ||
| 266 | } | ||
| 267 | |||
| 268 | # | ||
| 269 | # ->lookup_file | ||
| 270 | # | ||
| 271 | # Lookup file to be included. | ||
| 272 | # | ||
| 273 | # File to include ($finc) is located from the current directory, which is the | ||
| 274 | # one where the including file ($fbase) is located. If $finc is not found | ||
| 275 | # there, we follow the include path. | ||
| 276 | # | ||
| 277 | # Returns path of file we found, undef when not found. | ||
| 278 | # | ||
| 279 | sub lookup_file { | ||
| 280 | DFEATURE my $f_; | ||
| 281 | my $self = shift; | ||
| 282 | my ($fbase, $finc) = @_; | ||
| 283 | |||
| 284 | my $dir = dirname($fbase); | ||
| 285 | my $path = $self->_locate($finc, [$dir]); | ||
| 286 | $path = $self->_locate($finc, $self->incpath) unless defined $path; | ||
| 287 | $path = $self->simplify_path($path) if defined $path; | ||
| 288 | |||
| 289 | return DVAL $path; | ||
| 290 | } | ||
| 291 | |||
| 292 | # | ||
| 293 | # ->_locate | ||
| 294 | # | ||
| 295 | # Locate file through the specified directory list (given via array ref). | ||
| 296 | # Returns normalized path if found, undef otherwise. | ||
| 297 | # | ||
| 298 | sub _locate { | ||
| 299 | DFEATURE my $f_; | ||
| 300 | my $self = shift; | ||
| 301 | my ($file, $locref) = @_; | ||
| 302 | |||
| 303 | foreach my $dir (@$locref) { | ||
| 304 | return DVAL $self->normalize_path("$dir/$file") if -f "$dir/$file"; | ||
| 305 | } | ||
| 306 | |||
| 307 | return DVAL undef; | ||
| 308 | } | ||
| 309 | |||
| 310 | # | ||
| 311 | # ->normalize_path | ||
| 312 | # | ||
| 313 | # Normalize path by removing extra /'s or .'s. | ||
| 314 | # | ||
| 315 | sub normalize_path { | ||
| 316 | DFEATURE my $f_; | ||
| 317 | my $self = shift; | ||
| 318 | my ($path) = @_; | ||
| 319 | |||
| 320 | $path =~ tr|/||s; | ||
| 321 | $path =~ s|^\./||g; | ||
| 322 | $path =~ s|/\./||g; | ||
| 323 | |||
| 324 | return DVAL $path; | ||
| 325 | } | ||
| 326 | |||
| 327 | # | ||
| 328 | # ->simplify_path | ||
| 329 | # | ||
| 330 | # Simplify paths by turning things like: | ||
| 331 | # | ||
| 332 | # ../h/../h/../h/foo.h | ||
| 333 | # | ||
| 334 | # into | ||
| 335 | # ../h/foo.h | ||
| 336 | # | ||
| 337 | # by removing .. and . in paths. | ||
| 338 | # | ||
| 339 | sub simplify_path { | ||
| 340 | DFEATURE my $f_; | ||
| 341 | my $self = shift; | ||
| 342 | my ($path) = @_; | ||
| 343 | my @path = split(m:/:, $path); | ||
| 344 | my @cur; | ||
| 345 | my $absolute = 0; | ||
| 346 | $absolute++ if $path[0] eq ''; | ||
| 347 | foreach my $dir (@path) { | ||
| 348 | next if $dir eq '' || $dir eq '.'; | ||
| 349 | if ($dir eq '..') { | ||
| 350 | if (@cur == 0) { | ||
| 351 | push(@cur, '..') unless $absolute; | ||
| 352 | } elsif ($cur[$#cur] eq '..') { | ||
| 353 | push(@cur, '..'); | ||
| 354 | } else { | ||
| 355 | pop(@cur); | ||
| 356 | } | ||
| 357 | } else { | ||
| 358 | push(@cur, $dir); | ||
| 359 | } | ||
| 360 | } | ||
| 361 | my $simplified = ($absolute ? '/' : '') . join('/', @cur); | ||
| 362 | return DVAL $simplified; | ||
| 363 | } | ||
| 364 | |||
| 365 | # | ||
| 366 | # ->absolute_path | ||
| 367 | # | ||
| 368 | # Return the absolute path of filename or directory. | ||
| 369 | # | ||
| 370 | sub absolute_path { | ||
| 371 | DFEATURE my $f_; | ||
| 372 | my $self = shift; | ||
| 373 | my ($path) = @_; | ||
| 374 | |||
| 375 | DREQUIRE -e $path, "entry '$path' exists on the filesystem"; | ||
| 376 | |||
| 377 | return DVAL abs_path($path) if -d $path; | ||
| 378 | my $ap = File::Spec->catfile(abs_path(dirname $path), basename $path); | ||
| 379 | return DVAL $ap; | ||
| 380 | } | ||
| 381 | |||
| 382 | 1; | ||
| 383 |