2 # -*- Mode: perl; tab-width: 4; indent-tabs-mode: nil; -*-
7 # Copyright (c) 2002, 2003, 2004 by Ian Hickson
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 # Thanks to bryner and bsmedberg for suggestions.
24 # Thanks to jon rekai for a patch to not require File::Spec 0.8.
28 # takes as arguments the files to process
32 my $stack = new stack
;
35 # command line arguments
37 while ($_ = $ARGV[0], defined($_) && /^-./) {
42 if (/^([\w\.]+)=(.*)$/os) {
43 $stack->define($1, $2);
44 } elsif (/^([\w\.]+)$/os) {
45 $stack->define($1, 1);
47 die "$0: invalid argument to -D: $_\n";
50 } elsif (/^-F(.*)$/os) {
53 $stack->filter($1, 1);
55 die "$0: invalid argument to -F: $_\n";
58 } elsif (/^-I(.*)$/os) {
62 # define all variables that have valid names
63 $stack->define($_, $ENV{$_}) unless m/\W/;
66 $stack->{'dependencies'} = 1;
67 } elsif (/^--line-endings=crlf$/os) {
68 $stack->{'lineEndings'} = "\x0D\x0A";
69 } elsif (/^--line-endings=cr$/os) {
70 $stack->{'lineEndings'} = "\x0D";
71 } elsif (/^--line-endings=lf$/os) {
72 $stack->{'lineEndings'} = "\x0A";
73 } elsif (/^--line-endings=(.+)$/os) {
74 die "$0: unrecognised line ending: $1\n";
75 } elsif (/^--marker=(.)$/os) {
78 die "$0: invalid argument: $_\n";
81 unshift(@ARGV, '-') unless @ARGV;
82 unshift(@ARGV, @includes);
85 foreach (@ARGV) { include
($stack, $_); }
88 ########################################################################
92 use File
::Spec
::Unix
; # on all platforms, because the #include syntax is unix-based
94 # Note: Ideally we would use File::Spec 0.8. When this becomes
95 # possible, add "0.8" to the first "use" line above, then replace
96 # occurrences of "::_0_8::" with "->" below. And remove the code for
97 # File::Spec 0.8 much lower down the file.
100 my($stack, $filename) = @_;
101 my $directory = $stack->{'variables'}->{'DIRECTORY'};
102 if ($filename ne '-') {
103 $filename = File
::Spec
::_0_8
::rel2abs
($filename, $directory);
104 # splitpath expects forward-slash paths on windows, so we have to
105 # change the slashes if using Activestate Perl.
106 $filename =~ s?
\\?
/?g
if "$^O" eq "MSWin32";
107 my($volume, $path) = File
::Spec
::_0_8
::splitpath
($filename);
108 $directory = File
::Spec
::_0_8
::catpath
($volume, $path, '');
110 local $stack->{'variables'}->{'DIRECTORY'} = $directory;
111 local $stack->{'variables'}->{'FILE'} = $filename;
112 local $stack->{'variables'}->{'LINE'} = 0;
114 open(FILE
, $filename) or die "Couldn't open $filename: $!\n";
117 # on cygwin, line endings are screwed up, so normalise them.
118 s/[\x0D\x0A]+$/\n/os if ($^O
eq 'msys' || $^O
eq 'cygwin' || "$^O" eq "MSWin32");
120 if (/^\Q$marker\E([a-z]+)\n?$/os) { # argumentless processing instruction
122 } elsif (/^\Q$marker\E([a-z]+)\s(.*?)\n?$/os) { # processing instruction with arguments
123 process
($stack, $1, $2);
124 } elsif (/^\Q$marker\E/os) { # comment
126 } elsif ($stack->enabled) {
127 next if $stack->{'dependencies'};
129 # set the current line number in JavaScript if necessary
130 my $linein = $stack->{'variables'}->{'LINE'};
131 if (++$lineout != $linein) {
132 if ($filename =~ /\.js(|\.in)$/o) {
133 $stack->print("//\@line $linein \"$filename\"\n")
138 # print it, including any newlines
139 $stack->print(filtered
($stack, $_));
146 my($stack, $instruction, @arguments) = @_;
147 my $method = 'preprocessor'->can($instruction);
148 if (not defined($method)) {
149 fatal
($stack, 'unknown instruction', $instruction);
151 eval { &$method($stack, @arguments) };
153 fatal
($stack, "error evaluating $instruction:", $@
);
158 my($stack, $text) = @_;
159 foreach my $filter (sort keys %{$stack->{'filters'}}) {
160 next unless $stack->{'filters'}->{$filter};
161 my $method = 'filter'->can($filter);
162 if (not defined($method)) {
163 fatal
($stack, 'unknown filter', $filter);
165 $text = eval { &$method($stack, $text) };
167 fatal
($stack, "error using $filter:", $@
);
175 my $filename = $stack->{'variables'}->{'FILE'};
177 print STDERR "$0:$filename:$.: @_\n";
182 ########################################################################
186 # condition evaluated just prior to this context was false
187 use constant COND_FALSE => 0;
189 # condition evaluated just prior to this context was true
190 use constant COND_TRUE => 1;
192 # some prior condition at this level already evaluated to true (or a
193 # parent condition evaluated to false or must be ignored), so we're
194 # ignoring all remaining conditions at current level (and nested
196 use constant COND_COMPLETED => 2;
202 'LINE' => 0, # the line number in the source file
203 'DIRECTORY' => '', # current directory
204 'FILE' => '', # source filename
205 '1' => 1, # for convenience (the constant '1' is thus true)
210 'values' => [], # the value of the last condition evaluated at the nth level
211 'lastConditionState' => [], # whether the condition in the nth-level context was true, false, or not applicable
212 'conditionState' => COND_TRUE,
213 'dependencies' => 0, # whether we are showing dependencies
214 'lineEndings' => "\n", # default to platform conventions
220 $self->{'variables'}->{'LINE'}++;
225 my($variable, $value) = @_;
226 die "not a valid variable name
: '$variable'\n" if $variable =~ m/[^\w\.]/;
227 $self->{'variables'}->{$variable} = $value;
233 die "not a valid variable name
: '$variable'\n" if $variable =~ m/[^\w\.]/;
234 return defined($self->{'variables'}->{$variable});
240 die "not a valid variable name
: '$variable'\n" if $variable =~ m/[^\w\.]/;
241 delete($self->{'variables'}->{$variable});
246 my($variable, $required) = @_;
247 die "not a valid variable name
: '$variable'\n" if $variable =~ m/[^\w\.]/;
248 my $value = $self->{'variables'}->{$variable};
249 if (defined($value)) {
252 die "variable
'$variable' is
not defined\n" if $required;
261 ${$self->{'values'}}[-1] = $value;
262 $self->{'conditionState'} = $self->{'conditionState'} != COND_FALSE
264 : $value ? COND_TRUE : COND_FALSE;
271 push(@{$self->{'values'}}, $value);
272 my $lastCondition = $self->{'conditionState'};
273 push(@{$self->{'lastConditionState'}}, $lastCondition);
274 $self->{'conditionState'} = $lastCondition != COND_TRUE
276 : $value ? COND_TRUE : COND_FALSE;
281 $self->{'conditionState'} = pop(@{$self->{'lastConditionState'}});
282 return pop(@{$self->{'values'}});
287 return $self->{'conditionState'} == COND_TRUE;
292 return $self->{'conditionState'} != COND_TRUE;
297 my($filter, $value) = @_;
298 die "not a valid filter name
: '$filter'\n" if $filter =~ m/\W/;
299 $self->{'filters'}->{$filter} = $value;
305 $line =~ s/__(\w+)__/$self->get($1)/gose;
311 return if $self->{'dependencies'};
312 foreach my $line (@_) {
314 CORE::print("$line$self->{'lineEndings'}");
324 my $directory = $stack->{'variables'}->{'DIRECTORY'};
325 $filename = File::Spec::_0_8::abs2rel(File::Spec::_0_8::rel2abs($filename, $directory));
326 CORE::print("$filename\n");
329 ########################################################################
331 package preprocessor;
335 return if $stack->disabled;
336 die "argument expected
\n" unless @_;
337 my $argument = shift;
339 /^(\w+)\s(.*)$/os && do {
340 return $stack->define($1, $2);
343 return $stack->define($1, 1);
345 die "invalid argument
: '$_'\n";
351 return if $stack->disabled;
352 die "argument expected
\n" unless @_;
353 $stack->undefine(@_);
358 my $variable = shift;
359 my $replace = defined(shift);
360 die "argument expected
\n" unless defined($variable);
362 $stack->replace($stack->defined($variable));
364 $stack->push($stack->defined($variable));
370 my $variable = shift;
371 my $replace = defined(shift);
372 die "argument expected
\n" unless defined($variable);
374 $stack->replace(not $stack->defined($variable));
376 $stack->push(not $stack->defined($variable));
382 die "argument expected
\n" unless @_;
383 my $argument = shift;
384 my $replace = defined(shift);
386 /^(\w+)==(.*)$/os && do {
389 return $stack->replace($stack->get($1) eq $2);
391 return $stack->push($stack->get($1) eq $2);
394 /^(\w+)!=(.*)$/os && do {
397 return $stack->replace($stack->get($1) ne $2);
399 return $stack->push($stack->get($1) ne $2);
405 return $stack->replace($stack->get($1));
407 return $stack->push($stack->get($1));
413 return $stack->replace(not $stack->get($1));
415 return $stack->push(not $stack->get($1));
418 die "invalid argument
: '$_'\n";
424 die "argument unexpected
\n" if @_;
430 die "argument expected
\n" unless @_;
436 die "argument expected
\n" unless @_;
437 &ifdef($stack, @_, 1);
442 die "argument expected
\n" unless @_;
443 &ifndef($stack, @_, 1);
448 die "argument unexpected
\n" if @_;
454 return if $stack->disabled;
455 die "argument expected
\n" unless @_;
456 my $line = $stack->expand(@_);
462 return if $stack->disabled;
463 die "argument expected
\n" unless @_;
464 my $line = $stack->expand(@_);
465 $stack->print("$line\n");
470 return if $stack->disabled;
471 die "argument expected
\n" unless @_;
473 $stack->print("$line\n");
478 return if $stack->disabled;
479 die "argument expected
\n" unless @_;
480 my $filename = File::Spec::_0_8::catpath(File::Spec::_0_8::splitpath(@_));
481 if ($stack->{'dependencies'}) {
482 $stack->visit($filename);
484 main::include($stack, $filename);
489 my ($stack, $filename) = @_;
490 return if $stack->disabled;
491 die "argument expected
\n" unless $filename;
492 $filename =~ s/@(\w+)@/$stack->get($1, 1)/gose;
493 $filename = File::Spec::_0_8::catpath(File::Spec::_0_8::splitpath($filename));
494 if ($stack->{'dependencies'}) {
495 $stack->visit($filename);
497 main::include($stack, $filename);
503 return if $stack->disabled;
504 die "argument expected
\n" unless @_;
505 foreach (split(/\s/os, shift)) {
506 $stack->filter($_, 1);
512 return if $stack->disabled;
513 die "argument expected
\n" unless @_;
514 foreach (split(/\s/os, shift)) {
515 $stack->filter($_, 0);
520 ########################################################################
525 my($stack, $text) = @_;
526 $text = "" if $text eq "\n";
531 my($stack, $text) = @_;
532 $text =~ s/ +/ /gos; # middle spaces
533 $text =~ s/^ //gos; # start spaces
534 $text =~ s/ (\n?)$/$1/gos; # end spaces
539 my($stack, $text) = @_;
540 $text =~ s|//.*?(\n?)$|$1|gos;
545 my($stack, $text) = @_;
546 $text =~ s/@(\w+)@/$stack->get($1, 1)/gose;
550 sub attemptSubstitution {
551 my($stack, $text) = @_;
552 $text =~ s/@(\w+)@/$stack->get($1, 0)/gose;
556 ########################################################################
558 ########################################################################
559 # This code is from File::Spec::Unix 0.8.
560 # It is not considered a part of the preprocessor.pl source file
561 # This code is licensed under the same license as File::Spec itself.
563 package File::Spec::_0_8;
568 my ($path, $base) = @_;
569 if ( ! File::Spec->file_name_is_absolute( $path ) ) {
570 if ( !defined( $base ) || $base eq '' ) {
572 } elsif ( ! File::Spec->file_name_is_absolute( $base ) ) {
573 $base = rel2abs( $base );
575 $base = File::Spec->canonpath( $base );
577 $path = File::Spec->catdir( $base, $path );
579 return File::Spec->canonpath( $path );
583 return split m|/|, $_[1], -1; # Preserve trailing fields
587 my ($path, $nofile) = @_;
589 my ($volume,$directory,$file) = ('','','');
595 $path =~ m|^ ( (?: .* / (?: \.\.?\Z(?!\n) )? )? ) ([^/]*) |xs;
600 return ($volume,$directory,$file);
604 my ($volume,$directory,$file) = @_;
606 if ( $directory ne '' &&
608 substr( $directory, -1 ) ne '/' &&
609 substr( $file, 0, 1 ) ne '/'
611 $directory .= "/$file" ;
614 $directory .= $file ;
621 my($path,$base) = @_;
624 if ( ! File::Spec->file_name_is_absolute( $path ) ) {
625 $path = rel2abs( $path ) ;
628 $path = File::Spec->canonpath( $path ) ;
631 # Figure out the effective $base and clean it up.
632 if ( !defined( $base ) || $base eq '' ) {
635 elsif ( ! File::Spec->file_name_is_absolute( $base ) ) {
636 $base = rel2abs( $base ) ;
639 $base = File::Spec->canonpath( $base ) ;
642 # Now, remove all leading components that are the same
643 my @pathchunks = File::Spec::_0_8::splitdir( $path);
644 my @basechunks = File::Spec::_0_8::splitdir( $base);
646 while (@pathchunks && @basechunks && $pathchunks[0] eq $basechunks[0]) {
651 $path = CORE::join( '/', @pathchunks );
652 $base = CORE::join( '/', @basechunks );
654 # $base now contains the directories the resulting relative path
655 # must ascend out of before it can descend to $path_directory. So,
656 # replace all names with $parentDir
657 $base =~ s|[^/]+|..|g ;
659 # Glue the two together, using a separator if necessary, and preventing an
661 if ( $path ne '' && $base ne '' ) {
662 $path = "$base/$path" ;
664 $path = "$base$path" ;
667 return File::Spec->canonpath( $path ) ;
670 # End code from File::Spec::Unix 0.8.
671 ########################################################################