#!/usr/bin/perl # # $Id: condhtml,v 1.2 1997/09/10 01:25:09 earl Exp $ # # Copyright (c) 1997 by Earl A. Killian. # # This file is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published # by the Free Software Foundation version 1. # # This file is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this file; see the file COPYING. If not, write to the # Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. $myname = 'condhtml'; # parse options while ($#ARGV >= 0 && $ARGV[0] =~ /^-D/) { $_ = $'; $v = 1; if (/=/) { $_ = $`; $v = $'; } $value{$_} = $v; shift (@ARGV); } $line = 0; # current line number $on = 1; @name = (); @v = (); @on = (); @line = (); # for error messages while (<>) { $line += 1; if (/\<\!\-\-\s+if (\!?)(\S+) --\>/) { push (@name, $2); push (@v, $v); push (@on, $on); push (@line, $line); $v = $value{$2}; if (!defined($v)) { print if $on; } else { $v = !$v if $1 eq '!'; $on = $on[$#on] && $v; } } elsif (/^\<\!--\s+else --\>$/) { if ($#name < 0) { print STDERR "$myname: else without matching if on line $line.\n"; } elsif (!defined($v)) { print if $on; } else { $v = !$v; $on = $on[$#on] && $v; } } elsif (/^\<\!--\s+endif (\S+) --\>$/) { if ($#name < 0) { print STDERR "$myname: endif without matching if on line $line.\n"; } else { print if $on && !defined($v); $n = pop(@name); if ($n ne $1) { print STDERR "$myname: if/endif mismatch on line $line: $n/$1.\n"; } $on = pop(@on); $v = pop(@v); pop(@line); } } else { print $_ if $on; } } while ($#name >= 0) { print STDERR "$myname: missing endif ", pop(@name), " for if on line ", pop(@line), ".\n"; } # # Local Variables: # mode:perl # perl-indent-level:2 # End: