#!/usr/bin/perl
# Created by Mohammed Aslam P.S
# aslam at csa dot iisc dot ernet dot in

# To convert a C file to html
# Right now multi line #define and mulitline string won't work. Don't escape them with \
$tab=8;
%convert_start=("comment" => "<b><font color=#00ff00>", 
		"keyword" => "<b><font color=#ff0000>",
		"decl"	  => "<b><font color=#0000ff>",
		"string"  => "<font color=#ff00ff>",
		"include" => "<font color=#808000>",
		"define1" => "<font color=#800080>",
		"define2" => "<font color=#008080>",
		"brace"	  => "<b>");
%convert_end=(  "comment" => "</font></b>",
		"keyword" => "</font></b>",
		"decl"	  => "</font></b>",
		"string"  => "</font>",
		"include" => "</font>",
		"define1" => "</font>",
		"define2" => "</font>",
		"brace"   => "</b>");
@keyword = ("if","else","while","for","do","case","default");
@decl = ("int","float","short","long","char","void");
$file = shift(ARGV);
if( -z $file){
	print(STDERR "File not found\n");
	exit(1);
}
if(!open(FILE,$file)){
	print(STDERR "Cannot open file\n");
	exit(1);
}

print("<html>\n<head><title>$file</title></head><body>\n");
while($line=<FILE>){
	chomp($line);
	$line2=$line;
	if( $line =~ /</){
		$line2 =~ s/</&#60;/g;
		$line =~ s/</&#60;/g;
	}
	elsif( $line =~ /&/){
		$line2 =~ s/&/&#38;/g;
		$line =~ s/&/&#38;/g;
	}
	# Convert Comments...
	if(($comment) = $line =~ /(\/\/.*)/){ # For Comment
		$line2 =~ s/$comment/$convert_start{"comment"} $comment $convert_end{"comment"}/;
		$line =~ s/$comment//;
	}
	# Convert Strings...
	if(($string) = $line =~ /(\"[\S\s]*\")/){
		$line2 =~ s/$string/$convert_start{"string"} $string $convert_end{"string"}/;
		$line =~ s/$string//;
	}
	# Convert #include
	if(($text) = $line =~ /#include\s*&#60;(.*)>/){
		$line2 =~ s/#include\s*&#60;.*>/#include&#60; $convert_start{"include"} $text $convert_end{"include"}>/;
		$line =~ s/#include\s*&#60;.*>//;
	}
	# Convert #define
	if(($text1,$text2) = $line =~ /#define\s*(\S*)\s*(.*)/){
		$line2 =~ s/#define\s*\S*\s*.*/#define $convert_start{"define1"} $text1 $convert_end{"define1"} $convert_start{"define2"} $text2 $convert_end{"define2"}/;
		$line =~ s/#define\s*\S*\s*.*//;
	}
	# Convert Keywords...
	foreach $keyword (@keyword){
		if(($catch) = $line =~ /(\W)$keyword\W/){
			$line2 =~ s/\W$keyword/$catch $convert_start{"keyword"} $keyword $convert_end{"keyword"}/g;
			$line =~ s/\W$keyword/$catch/g;
			next;
		}
	}
	# Convert declarations...
	foreach $decl (@decl){
		if(($catch) = $line =~ /(\W)$decl\W/){
			$line2 =~ s/\W$decl/$catch $convert_start{"decl"} $decl $convert_end{"decl"}/g;
			$line =~ s/\W$decl/$catch/g;
			next;
		}
	}
	if($line =~ /\(/){
		$line2 =~ s/\(/$convert_start{"brace"} ( $convert_end{"brace"}/;
		$line =~ s/\(//;
	}
	if($line =~ /\)/){
		$line2 =~ s/\)/$convert_start{"brace"} ) $convert_end{"brace"}/;
		$line =~ s/\)//;
	}
	if($line =~ /}/){
		$line2 =~ s/}/$convert_start{"brace"} } $convert_end{"brace"}/;
		$line =~ s/}//;
		$indent--;
	}
	for($i=0;$i<$indent*$tab;$i++){
		$line2 = "&nbsp;".$line2;
	}
	if($line =~ /{/){
		$line2 =~ s/{/$convert_start{"brace"} { $convert_end{"brace"}/;
		$line =~ s/{//;
		$indent++;
	}
	$line2 = $line2 . "<br\\>";
	print "$line2\n";
}
print("</body></html>\n");
