#!/usr/bin/perl -w
#---------------------------------------------------------------------------------------------------------
# print_source.pl - displays the source code of the parmed file in the browser
# written by Eric Pence
#---------------------------------------------------------------------------------------------------------
use strict;
use CGI qw(-oldstyle_urls :standard);
use CGI::Carp qw(fatalsToBrowser);
use FileHandle;

my $q = new CGI;

# Get the parmed file
my $filename = $q->param( 'src' ) || '';
my $source_file = "$filename";

# Extract the file name from the path for the browser title
my $title = '';
my $slash_loc = rindex($filename,"/");
if ($slash_loc != -1) {
my $len = length $filename;
$title = substr($filename,$slash_loc+1,$len-$slash_loc);
}
else {
$title = $filename;
}

print $q->header();
print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<title>$title</title>
</head>
<body>
<pre>";

#my $file_to_print = (new FileHandle $source_file) || die "Can't find $source_file file";
my $file_to_print = (new FileHandle $source_file) || exit;
while (<$file_to_print>) {
# Clean up so browser will display source as it looks in editor
$_ =~ s/&/&amp;/g;
$_ =~ s/</&lt;/g;
$_ =~ s/>/&gt;/g;
$_ =~ s/\n/<br>/g;
$_ =~ s/\t/&nbsp;&nbsp;/g;
print $_;
}

print "</pre>
</body>
</html>";

BEGIN {
# Set up error log file
my $log_file = "error_log.txt";
use CGI::Carp qw(carpout);
open(LOG, ">>$log_file") || die "Unable to append to error log: $!";
carpout(*LOG);
}