#!/usr/bin/perl

#####################################################################
#
# Offline HTMLHelp.com Validator
# by Liam Quinn <liam@htmlhelp.com>
#
# This is a simplified version of the online WDG HTML Validator
# found at <http://www.htmlhelp.com/tools/validator/>.
#
# Copyright (c) 1998-2001 by Liam Quinn
# This program is free software; you can redistribute it
# and/or modify it under the same terms as Perl itself.
#
#####################################################################

#####################################################################
# Required libraries #
######################

# These are all standard Perl modules

use strict;
use Getopt::Long;
use Text::Wrap;
use POSIX;

#####################################################################

#####################################################################
# Variables to define #
#######################

# Version and identifier of this program
my $VERSION = '1.0.3';
my $progname = "Offline HTMLHelp.com Validator, Version $VERSION
by Liam Quinn <liam\@htmlhelp.com>";
my $usage = "Usage: validate [OPTION] [FILE...]";
my $tryHelp = qq{Try "validate --help" for more information.};

# SGML directory (catalog, DTDs, SGML declarations)
my $sgmlDir = '/usr/local/validate/lib';

# Location of lq-nsgmls executable
my $nsgmlsLocation = '/usr/local/bin/lq-nsgmls';

# lq-nsgmls command line
# The SGML declaration and HTML document's filename will be appended
# to this string
my $nsgmls = "$nsgmlsLocation -E0 -s";

# Catalog files for HTML/SGML and XHTML/XML
my $htmlCatalog = "$sgmlDir/catalog";
my $xhtmlCatalog = "$sgmlDir/xhtml.soc";

# Where to direct errors (typically *STDOUT or *STDERR)
my $errout = *STDOUT;

# Versions of HTML associated with a given FPI
my %HTMLversion = (
  'PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN"' => 'XHTML Basic',
  'PUBLIC "ISO/IEC 15445:2000//DTD HyperText Markup Language//EN"' => 'ISO/IEC 15445:2000',
  'PUBLIC "ISO/IEC 15445:2000//DTD HTML//EN"' => 'ISO/IEC 15445:2000',
  'PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"' => 'XHTML 1.0 Strict',
  'PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' => 'XHTML 1.0 Transitional',
  'PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"' => 'XHTML 1.0 Frameset',
  'PUBLIC "-//W3C//DTD HTML 4.01//EN"' => 'HTML 4.01 Strict',
  'PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"' => 'HTML 4.01 Transitional',
  'PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"' => 'HTML 4.01 Frameset',
  'PUBLIC "-//W3C//DTD HTML 4.0//EN"' => 'HTML 4.0 Strict',
  'PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"' => 'HTML 4.0 Transitional',
  'PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN"' => 'HTML 4.0 Frameset',
  'PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"' => 'HTML 3.2',
  'PUBLIC "-//W3C//DTD HTML 3.2 Draft//EN"' => 'HTML 3.2',
  'PUBLIC "-//W3C//DTD HTML 3.2//EN"' => 'HTML 3.2',
  'PUBLIC "-//W3C//DTD HTML Experimental 970421//EN"' => 'HTML 3.2 + Style',
  'PUBLIC "-//W3O//DTD W3 HTML 3.0//EN"' => 'HTML 3.0 Draft',
  'PUBLIC "-//IETF//DTD HTML 3.0//EN//"' => 'HTML 3.0 Draft',
  'PUBLIC "-//IETF//DTD HTML 3.0//EN"' => 'HTML 3.0 Draft',
  'PUBLIC "-//IETF//DTD HTML i18n//EN"' => 'HTML 2.0 + i18n',
  'PUBLIC "-//IETF//DTD HTML//EN"' => 'HTML 2.0',
  'PUBLIC "-//IETF//DTD HTML 2.0//EN"' => 'HTML 2.0',
  'PUBLIC "-//IETF//DTD HTML Level 2//EN"' => 'HTML 2.0',
  'PUBLIC "-//IETF//DTD HTML 2.0 Level 2//EN"' => 'HTML 2.0',
  'PUBLIC "-//IETF//DTD HTML Level 1//EN"' => 'HTML 2.0 Level 1',
  'PUBLIC "-//IETF//DTD HTML 2.0 Level 1//EN"' => 'HTML 2.0 Level 1',
  'PUBLIC "-//IETF//DTD HTML Strict//EN"' => 'HTML 2.0 Strict',
  'PUBLIC "-//IETF//DTD HTML 2.0 Strict//EN"' => 'HTML 2.0 Strict',
  'PUBLIC "-//IETF//DTD HTML Strict Level 2//EN"' => 'HTML 2.0 Strict',
  'PUBLIC "-//IETF//DTD HTML 2.0 Strict Level 2//EN"' => 'HTML 2.0 Strict',
  'PUBLIC "-//IETF//DTD HTML Strict Level 1//EN"' => 'HTML 2.0 Strict Level 1',
  'PUBLIC "-//IETF//DTD HTML 2.0 Strict Level 1//EN"' => 'HTML 2.0 Strict Level 1'
);

# SGML declarations for a given level of HTML
my %sgmlDecl = (
  'XHTML Basic' => "$sgmlDir/xhtml-basic10/xml1.dcl",
  'ISO/IEC 15445:2000' => "$sgmlDir/15445.dcl",
  'XHTML 1.0 Strict' => "$sgmlDir/xhtml1/xhtml1.dcl",
  'XHTML 1.0 Transitional' => "$sgmlDir/xhtml1/xhtml1.dcl",
  'XHTML 1.0 Frameset' => "$sgmlDir/xhtml1/xhtml1.dcl",
  'HTML 4.01 Strict' => "$sgmlDir/HTML4.dcl",
  'HTML 4.01 Transitional' => "$sgmlDir/HTML4.dcl",
  'HTML 4.01 Frameset' => "$sgmlDir/HTML4.dcl",
  'HTML 4.0 Strict' => "$sgmlDir/HTML4.dcl",
  'HTML 4.0 Transitional' => "$sgmlDir/HTML4.dcl",
  'HTML 4.0 Frameset' => "$sgmlDir/HTML4.dcl",
  'HTML 3.2' => "$sgmlDir/HTML32.dcl",
  'HTML 3.2 + Style' => "$sgmlDir/html-970421.decl",
  'HTML 3.0 Draft' => "$sgmlDir/HTML3.dcl",
  'HTML 2.0 + i18n' => "$sgmlDir/i18n.dcl",
  'HTML 2.0' => "$sgmlDir/html.dcl",
  'HTML 2.0 Strict' => "$sgmlDir/html.dcl",
  'HTML 2.0 Level 1' => "$sgmlDir/html.dcl",
  'HTML 2.0 Strict Level 1' => "$sgmlDir/html.dcl",
  'Unknown' => "$sgmlDir/custom.dcl",

  # For generic XML validation (using the --xml option)
  'XML' => "$sgmlDir/xhtml1/xhtml1.dcl"
);

# XHTML DTDs
my %xhtml = (
  'XHTML Basic' => 1,
  'XHTML 1.0 Strict' => 1,
  'XHTML 1.0 Transitional' => 1,
  'XHTML 1.0 Frameset' => 1,
  'XML' => 1
);

# Default DOCTYPE if the document is missing a DOCTYPE
my $defaultDoctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">';

# Default DOCTYPE if the document contains frames
my $defaultFramesetDoctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
   "http://www.w3.org/TR/html4/frameset.dtd">';

# Error for missing DOCTYPE
my $noDoctype = "missing document type declaration; assuming HTML 4.01 Transitional";

# Error for missing DOCTYPE in a Frameset document
my $noFramesetDoctype = "missing document type declaration; assuming HTML 4.01 Frameset";

#####################################################################

#####################################################################
#
# The rest of the script...
#
#####################################################################

# Flush output buffer
$| = 1;

### Get user input ###

# Character encoding to use (optional)
my $charset;

# Verbose output (optional)
my $verbose;

# Emacs-friendly output
my $emacs;

# XML mode
my $xml;

# Help and version info
my $help;
my $versionInfo;

GetOptions("xml" => \$xml, "charset=s" => \$charset, "verbose" => \$verbose,
	"help|h" => \$help, "version|v" => \$versionInfo, "emacs" => \$emacs);

# Files to validate
my @files = @ARGV;

######################

my $errors = 0;

if ($versionInfo || $help) {
	if ($versionInfo) {
		print "$progname\n";
	}
	if ($help) {
		&helpText;
	}
	exit $errors;
}

if ($#files == -1) {
	push(@files, '-');
}

# Check that nsgmls is available before we get too far
unless (-e $nsgmlsLocation) {
	&error("$nsgmlsLocation is not installed");
	exit $errors;
}
unless (-x $nsgmlsLocation) {
	&error("$nsgmlsLocation is not executable");
	exit $errors;
}

my $file;
foreach $file (@files) {

	my $tempname = undef;
	my $tempfh = undef;

	# Read in document
	my $document = "";
	if ($file ne '-') {
		unless (-e $file) {
			&error("File $file does not exist.");
			next;
		}
		unless (-r $file) {
			&error("File $file is not readable.");
			next;
		}

		open(IN, $file) || die "Unexpected error reading $file: $!\n";
		while (<IN>) {
			$document .= $_;
		}
		close(IN);
	} else {
		while (<>) {
			$document .= $_;
		}
	}

	unless ($charset) {
		# Check for a META element specifying the character encoding
		if ($document =~ m#<META(\s[^>]*http\-equiv\s*=\s*["']?Content\-Type["']?[^>]*)>#iso) {
			my $metaAttributes = $1;
			if ($metaAttributes =~ m#\scontent\s*=\s*["']?.*[\s;]charset\s*=\s*['"]?([^"']+)#iso) {
				$charset = $1;
			}
		}
	}

	my @errors; # queue of errors
	my @externalErrors; # queue of errors in an external DTD
	my $lineAdjust = 0; # account for line number changes if we add a DOCTYPE

	# Determine the level of HTML
	my $htmlLevel;
	my $fileToValidate = $file;
	if ($xml) {
		$htmlLevel = 'XML';
	} else {
		$htmlLevel = 'Unknown';
	}
	if ($document =~ /<!DOCTYPE([^>]*)>/iso) {

		my $doctypeMeat = $1;
		if ($doctypeMeat =~ /PUBLIC\s+("[^"]*")/iso) {
			$htmlLevel = $HTMLversion{"PUBLIC $1"} || $htmlLevel;
		}

		if ($file eq '-') {
			($tempname, $tempfh) = getTempFile();
			print $tempfh "$document";
			close($tempfh);

			$fileToValidate = $tempname;
		}

	} else { # Missing DOCTYPE

		# Add a default DOCTYPE
		my ($insertedDoctype, $doctypeError);
		if ($document =~ /<FRAMESET/io) {
			$insertedDoctype = $defaultFramesetDoctype;
			$doctypeError = $noFramesetDoctype;
		} else {
			$insertedDoctype = $defaultDoctype;
			$doctypeError = $noDoctype;
		}

		($tempname, $tempfh) = getTempFile();
		print $tempfh "$insertedDoctype\n$document";
		close($tempfh);

		$fileToValidate = $tempname;
		$lineAdjust = 2;
		push(@errors, "::" . (1 + $lineAdjust) . ":0:E: $doctypeError");

	}

	# Determine whether we're dealing with HTML or XHTML and set the SP
	# environment accordingly.
	if ($xhtml{$htmlLevel}) {
		$ENV{'SGML_CATALOG_FILES'} = $xhtmlCatalog;
		$ENV{'SP_ENCODING'} = 'xml';
	} else {
		$ENV{'SGML_CATALOG_FILES'} = $htmlCatalog;
		if (defined $charset) {
			$ENV{'SP_ENCODING'} = $charset;
		}
	}
	$ENV{'SP_CHARSET_FIXED'} = 1;

	if ($verbose) {
		if ($file eq '-') {
			print wrap("", "\t",
				"Checking with $htmlLevel document type...\n");
		} else {
			print wrap("", "\t",
				"Checking $file with $htmlLevel document type...\n");
		}
	}

	# Run the validator
	open(NSGMLS, "$nsgmls $sgmlDecl{$htmlLevel} $fileToValidate 2>&1 |")
		|| die("Unable to execute $nsgmls: $!\n");

	# Create a queue of errors
	while (<NSGMLS>) {

		my @error = split(/:/, $_, 6);

		if ($#error < 5) {

			next;

		} elsif ($error[4] eq 'E' || $error[4] eq 'X') {

			push(@errors, $_);

			# If the DOCTYPE is bad, bail out
			last if ($error[5] eq " unrecognized {{DOCTYPE}}; unable to check document\n");

		} elsif ($error[1] =~ /^<URL>/o) { # error from external DTD

			push(@externalErrors, $_);

		} elsif (length($error[4]) > 1) { # Allow secondary messages about preceding error

			push(@errors, $_);

		}

	}
	close(NSGMLS);

	# If we created a tempfile, unlink it
	if (defined $tempname) {
		unlink($tempname);
	}

	# Report errors
	if ($#errors > -1 || $#externalErrors > -1) {

		&startErrors($file);

		foreach (@externalErrors) {
			my @error = split(/:/, $_, 7);

			# Determine URL containing the error
			my $errorURL;
			if ($error[1] =~ /<URL>(.+)/o) {
				$errorURL = "$1:$error[2]";
			}

			my $lineNumber = $error[3];
			my $character = $error[4] + 1;

			my $errorMsg;
			if ($emacs) {
				$errorMsg = "$errorURL:$lineNumber:$character:";
			} else {
				$errorMsg = "$errorURL, line $lineNumber, character $character: ";
			}

			if ($error[6]) {
				$errorMsg .= superChomp($error[6]);
			} else {
				$errorMsg .= superChomp($error[5]);
			}

			&htmlError($errorMsg);
		}

		foreach (@errors) {
			my @error = split(/:/, $_, 6);

			# I don't think this should happen, but I'm not sure
			next if $#error < 4;

			# Determine line number and character of error
			my $lineNumber = $error[2] - $lineAdjust;
			next unless $lineNumber > 0;
			my $character = $error[3] + 1;

			# Prepare error message
			my $errorMsg;
			if ($emacs) {
				$errorMsg = "$file:$lineNumber:$character:";
			} else {
				$errorMsg = "Line $lineNumber, character $character: ";
			}

			if ($error[5]) {
				$errorMsg .= superChomp($error[5]);
			} else {
				$errorMsg .= superChomp($error[4]);
			}
			while ($errorMsg =~ m#\{\{"?(.+?)"?\}\}#gos) {
				my $linkText = $1;
				$errorMsg =~ s#\{\{(")?$linkText(")?\}\}#$1$linkText$2#;
			}

			&htmlError($errorMsg);

		}

	} else {

		if ($verbose) {
			print "No errors!\n";
		}
	}

}

exit $errors;

# Return an error message
# The error message must be given as the first argument
sub error {
	my $error_message = shift;
	print $errout wrap("", "\t", "ERROR: \t$error_message\n");
	++$errors;
}

# Heading to start HTML errors
# The file being validated should be given as the first argument
sub startErrors {
	my $file = shift;
	if (length $file) {
		if ($file eq '-') {
			print $errout "*** Errors: ***\n";
		} else {
			if ($emacs) {
				print $errout "*** Errors validating $file: ***\n";
			} else {
				print $errout wrap("", "\t",
					"*** Errors validating $file: ***\n");
			}
		}
	}
}

# Report an HTML error
# The error message must be given as the first argument
sub htmlError {
	my $error_message = shift;

	if ($emacs) {
		print $errout "$error_message\n";
	} else {
		print $errout wrap("", "\t", "$error_message\n");
	}

	++$errors;
}


# Remove any newline characters (\r or \n) at the end of a string
# First argument is the string
# Returns the new string
sub superChomp {

	my $str = shift || return;
	$str =~ s/[\r\n]+$//o;
	return $str;

}


# Create temporary file securely
# Returns the name and file handle of the created file
sub getTempFile {
	my $filename;
	do {
		$filename = POSIX::tmpnam();
	} until sysopen(FH, $filename, O_RDWR|O_CREAT|O_EXCL, 0666);

	return ($filename, \*FH);
}


sub helpText {

	print<<"EndOfHelp";
"validate", the Offline HTMLHelp.com Validator, checks the syntax of HTML
documents using an SGML parser and reports any errors.  XHTML documents
may also be validated using an XML parser.

$usage
		
The program's options are as follows:
  --xml                 indicate that the documents to be validated are XML
                        documents.  Known document types, such as HTML 4.01 and
                        XHTML 1.0, are automatically handled by "validate".
                        For unknown document types, "validate" will assume
                        XHTML/XML if this option is specified and HTML/SGML
                        otherwise.
  --charset=ENCODING    force ENCODING to be used as the character encoding
                        when validating HTML/SGML documents.  This option is
                        ignored when validating XHTML/XML documents, which
                        are assumed to use XML rules for specifying the
                        character encoding.  The following encodings
                        (case-insensitive) are supported: "utf-8",
                        "iso-10646-ucs-2", "euc-jp", "euc-kr", "gb2312",
                        "shift_jis", "big5", and "iso-8859-n" where n is
                        between 1 and 9 inclusive.
  --verbose             turn on verbose output messages
  --emacs               use an output format intended for parsing by Emacs
  -h, --help            display this help and exit
  -v, --version         output version information and exit

Any number of files may be specified after the options.  With no FILE,
standard input is read.

"validate" is written by Liam Quinn <liam\@htmlhelp.com> and is based on the
WDG HTML Validator, an online validation service available at
<http://www.htmlhelp.com/tools/validator/>.
EndOfHelp

}

