#!/usr/bin/perl -w
# -*- perl -*-
#
# This script is eval'ed by the fileViewer.cgi script.
#
# Input is a scalar, $FILE, that contains the file text
#
#
#################################################################
#
# Add color to C and C++ code (html) (really simple)
#
# This is a quick late-night hack and far from complete. It also
# tries to work for both C and C++ which might be a mistake
#
#################################################################
my $COMMENT_STYLE="color: red ;" ;
my $MACRO_STYLE="color: #006000;" ;
my $STRING_CONSTANT_STYLE=" background-color: #e0e0e0 ; " ;
my $RESERVED_WORD_STYLE="color: blue; " ;
my $wasInclude = 0 ;
sub c_comment {
my $token = shift @_ ;
my $inr = shift @_ ;
my $outr = shift @_ ;
$$inr =~ s/(.*?\*\/)//s ;
my $tmp = $1 ;
$tmp =~ s/\n/<\/span>\n<span style=\"$COMMENT_STYLE\">/sg ;
$$outr .= "<span style=\"$COMMENT_STYLE\">${token}$tmp</span>" ;
}
sub cpp_comment {
my $token = shift @_ ;
my $inr = shift @_ ;
my $outr = shift @_ ;
$$inr =~ s/(.*?)\n/\n/i ;
$$outr .= "<span style=\"$COMMENT_STYLE\">${token}$1</span>" ;
}
sub hash {
my $token = shift @_ ;
my $inr = shift @_ ;
my $outr = shift @_ ;
$$inr =~ s/(\s*\w+)//i ;
$$outr .= "<span style=\"$MACRO_STYLE\">${token}$1</span>" ;
if($1 eq "include") {
if($$inr =~ s/^(\s+<|\s+\")(\S+)(>|")//) {
my ($s,$f,$e) = ($1,$2,$3) ;
$f = P4CGI::ahref(-url => "fileSearch.cgi",
"FSPC=//...$f",
$f) ;
$$outr .= "$s$f$e" ;
}
}
}
sub string {
my $token = shift @_ ;
my $inr = shift @_ ;
my $outr = shift @_ ;
$$inr =~ s/(.*?)(?<!\\)"// ;
$$outr .= "$token<span style=\"$STRING_CONSTANT_STYLE\">$1</span>"" ;
}
sub blueBold {
my $token = shift @_ ;
my $inr = shift @_ ;
my $outr = shift @_ ;
$$outr .= "<span style=\"$RESERVED_WORD_STYLE\"><b>$token</b></span>" ;
}
sub blue {
my $token = shift @_ ;
my $inr = shift @_ ;
my $outr = shift @_ ;
$$outr .= "<span style=\"$RESERVED_WORD_STYLE\">$token</span>" ;
}
sub bold {
my $token = shift @_ ;
my $inr = shift @_ ;
my $outr = shift @_ ;
$$outr .= "<b>$token</b>" ;
}
my @blueBoldWords=("if","else","while","do","goto","for","until") ;
my @blueWords=(
"asm",
"auto",
"bool",
"break",
"case",
"catch",
"char",
"class",
"const",
"continue",
"default",
"delete",
"do",
"double",
"else",
"enum",
"extern",
"false",
"float",
"for",
"friend",
"goto",
"if",
"inline",
"int",
"long",
"new",
"operator",
"private",
"protected",
"public",
"register",
"return",
"short",
"signed",
"sizeof",
"static",
"struct",
"switch",
"template",
"this",
"throw",
"true",
"try",
"typedef",
"union",
"unsigned",
"virtual",
"void",
"volatile",
"while"
) ;
my $boldchars="{}" ;
my %routine = (
"/*" => \&c_comment,
"//" => \&cpp_comment,
"#" => \&hash,
""" => \&string
) ;
my @re ;
my $b ;
foreach $b (keys %routine) {
push @re,"\Q$b\E" ;
}
foreach $b (@blueWords) {
$routine{"$b"} = \&blue ;
push @re,'\b'.$b.'\b' ;
}
foreach $b (@blueBoldWords) {
if(! exists $routine{"$b"}) {
push @re,'\b'.$b.'\b' ;
}
$routine{"$b"} = \&blueBold ;
}
foreach $b (split('',$boldchars)) {
$routine{"$b"} = \&blue ;
push @re,"\Q$b\E" ;
}
my $in = $FILE ;
$FILE = "" ;
my $re = join("|",@re) ;
while($in =~ s/^(.*?)($re)//s) {
$FILE .= $1 ;
my $tok = $2 ;
if(exists $routine{$tok}) {
&{$routine{$tok}}($tok,\$in,\$FILE) ;
}
else {
$FILE .= $tok ;
}
if($wasInclude) {
$wasInclude = 0 ;
$FILE .= "III" ;
}
}
$FILE .= $in ;
#
# End
#