This file is indexed.

/usr/bin/validate is in wdg-html-validator 1.6.2-7.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#!/usr/bin/perl -T

#####################################################################
#
# 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-2005 by Liam Quinn
# This program is free software; you can redistribute it
# and/or modify it under the same terms as Perl itself.
#
# Contributors:
# * Ville Skytta
#
#####################################################################

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

# These are all standard Perl modules; we'll check for URI and LWP
# later on demand.

use strict;
use Getopt::Long qw(GetOptions);
use Text::Wrap qw(wrap);
use POSIX qw(:fcntl_h);

# If File::Spec::Functions isn't available, let's fall back quietly
# to a replacement function.
eval {
    require File::Spec::Functions;
    File::Spec::Functions->import('catfile');
};
*catfile = sub { join('/', @_) } if $@;

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

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

# Version and identifier of this program
my $VERSION = '1.2.2';
my $progname = "Offline HTMLHelp.com Validator, Version $VERSION
by Liam Quinn <liam\@htmlhelp.com>";
my $usage = "Usage: validate [OPTION] [FILE...]";

# SGML directory (catalog, DTDs, SGML declarations)
my $sgmlDir = '/usr/share/sgml/html/dtd';

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

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

# Warnings to pass on command-line to lq-nsgmls, if desired
my $nsgmlsWarnings = '-wnon-sgml-char-ref -wmin-tag';
my $nsgmlsXMLWarnings = '-wxml';

# lq-nsgmls "errors" that are not reported unless warnings are requested.
# These are true errors in XML validation, but they should only be
# reported as warnings otherwise.
my %errorAsWarning = (
  ' net-enabling start-tag not supported in {{XML}}' => 1,
  ' unclosed start-tag' => 1,
  ' unclosed end-tag' => 1
);

# Catalog files for HTML/SGML and XHTML/XML
my $htmlCatalog  = "/etc/sgml/sgml-data.cat";
my $xhtmlCatalog = "/etc/sgml/w3c-dtd-xhtml.cat";

# 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 1.1 plus MathML 2.0 plus SVG 1.1//EN"' => 'XHTML 1.1 plus MathML 2.0 plus SVG 1.1',
  'PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN"' => 'XHTML 1.1 plus MathML 2.0',
  'PUBLIC "-//W3C//DTD MathML 2.0//EN"' => 'MathML 2.0',
  'PUBLIC "-//W3C//DTD XHTML 1.1//EN"' => 'XHTML 1.1',
  'PUBLIC "-//WAPFORUM//DTD WML 1.3//EN"' => 'WML 1.3',
  'PUBLIC "-//WAPFORUM//DTD WML 1.2//EN"' => 'WML 1.2',
  'PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"' => 'WML 1.1',
  'PUBLIC "-//WAPFORUM//DTD WML 1.0//EN"' => 'WML 1.0',
  '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 1.1 plus MathML 2.0 plus SVG 1.1' => catfile($sgmlDir, 'xhtml', '1.1', 'xml1n.dcl'),
  'XHTML 1.1 plus MathML 2.0' => catfile($sgmlDir, 'xhtml', '1.1', 'xml1n.dcl'),
  'MathML 2.0'                => catfile($sgmlDir, 'xhtml', '1.1', 'xml1n.dcl'),
  'XHTML 1.1'                 => catfile($sgmlDir, 'xhtml', '1.1', 'xml1n.dcl'),
  'WML 1.3'                   => catfile($sgmlDir, 'xhtml', '1.0', 'xhtml1.dcl'),
  'WML 1.2'                   => catfile($sgmlDir, 'xhtml', '1.0', 'xhtml1.dcl'),
  'WML 1.1'                   => catfile($sgmlDir, 'xhtml', '1.0', 'xhtml1.dcl'),
  'WML 1.0'                   => catfile($sgmlDir, 'xhtml', '1.0', 'xhtml1.dcl'),
  'XHTML Basic'               => catfile($sgmlDir, 'xhtml', 'basic', 'xml1.dcl'),
  'ISO/IEC 15445:2000'        => catfile($sgmlDir, 'iso-15445', '15445.dcl'),
  'XHTML 1.0 Strict'          => catfile($sgmlDir ,'xhtml', '1.0', 'xhtml1.dcl'),
  'XHTML 1.0 Transitional'    => catfile($sgmlDir, 'xhtml', '1.0', 'xhtml1.dcl'),
  'XHTML 1.0 Frameset'        => catfile($sgmlDir, 'xhtml', '1.0', 'xhtml1.dcl'),
  'HTML 4.01 Strict'          => catfile($sgmlDir, '4.01', 'HTML4.decl'),
  'HTML 4.01 Transitional'    => catfile($sgmlDir, '4.01', 'HTML4.decl'),
  'HTML 4.01 Frameset'        => catfile($sgmlDir, '4.01', 'HTML4.decl'),
  'HTML 4.0 Strict'           => catfile($sgmlDir, '4.0', 'HTML4.decl'),
  'HTML 4.0 Transitional'     => catfile($sgmlDir, '4.0', 'HTML4.decl'),
  'HTML 4.0 Frameset'         => catfile($sgmlDir, '4.0', 'HTML4.decl'),
  'HTML 3.2'                  => catfile($sgmlDir, 'html-3.2.decl'),
  'HTML 3.2 + Style'          => catfile($sgmlDir, 'html-970421.decl'),
  'HTML 3.0 Draft'            => catfile($sgmlDir, 'html-3.decl'),
  'HTML 2.0 + i18n'           => catfile($sgmlDir, 'html-2-i18n.decl'),
  'HTML 2.0'                  => catfile($sgmlDir, 'html-2.decl'),
  'HTML 2.0 Strict'           => catfile($sgmlDir, 'html-2.decl'),
  'HTML 2.0 Level 1'          => catfile($sgmlDir, 'html-2.decl'),
  'HTML 2.0 Strict Level 1'   => catfile($sgmlDir, 'html-2.decl'),
  'Unknown'                   => catfile($sgmlDir, '/usr/share/wdg-html-validator/custom.dcl'),
 
  # For generic XML validation (using the --xml option)
  'XML'                       => catfile($sgmlDir, 'xhtml', '1.0', 'xhtml1.dcl'),
);

# XHTML DTDs
my %xhtml = (
  'XHTML 1.1 plus MathML 2.0 plus SVG 1.1' => 1,
  'XHTML 1.1 plus MathML 2.0' => 1,
  'MathML 2.0'                => 1,
  'XHTML 1.1'                 => 1,
  'WML 1.3'                   => 1,
  'WML 1.2'                   => 1,
  'WML 1.1'                   => 1,
  'WML 1.0'                   => 1,
  '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...
#
#####################################################################

# Get rid of unsafe environment variables, see perlsec
delete(@ENV{qw(PATH IFS CDPATH ENV BASH_ENV)});

# Flush output buffer
$| = 1;

### Get user input ###

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

# Verbose output (optional)
my $verbose;

# Emacs-friendly output
my $emacs = ($ENV{EMACS} && $ENV{EMACS} eq 't');

# XML mode
my $xml;

# Whether warnings are desired
my $warnings;

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

GetOptions("xml" => \$xml, "charset=s" => \$charsetOverride,
    "verbose" => \$verbose, "help|h" => \$help,
    "version|v" => \$versionInfo, "emacs!" => \$emacs,
    "warn|w|W" => \$warnings);

# 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 _) {
    &error("$nsgmlsLocation is not executable");
    exit $errors;
}

# Check if we can use URIs.
eval {
    require URI;
    require LWP::UserAgent;
};
my $uri_ok = !$@;

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

    my $tempname = undef;
    my $tempfh = undef;
    my $charset = $charsetOverride;

    # Read in document
    my $document = "";
    my $fileIsURL = 0;
    if ($file ne '-') {
        if ($uri_ok && $file =~ m|^\w+://.+|i) {
            $fileIsURL = 1;
            $ua ||= LWP::UserAgent->new(env_proxy => 1, keep_alive => 1);
            my $uri = URI->new($file);
            unless ($ua->is_protocol_supported($uri)) {
                &error('Unsupported protocol: ' . $uri->scheme());
                next;
            }
            my $res = $ua->get($uri->canonical());
            if ($res->is_success()) {
                $document = $res->content();
                
                unless ($charset) {
                    my $contentType = $res->header('Content-Type');
                    if ($contentType && $contentType =~ /[\s;]charset\s*=\s*"?([^,"\s]+)/io) {
                        $charset = $1;
                    }
                }
            } else {
                &error($res->status_line());
                next;
            }

        } else {
            unless (-e $file) {
                &error("File $file does not exist.");
                next;
            }
            unless (-r _) {
                &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 ($fileIsURL || $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';
        $xml = 1;
    } else {
        $ENV{'SGML_CATALOG_FILES'} = $htmlCatalog;
        if (defined $charset) {
            $ENV{'SP_ENCODING'} = $charset;
        } else {
            $ENV{'SP_ENCODING'} = "ISO-8859-1";
        }
    }
    $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");
        }
    }

    my $warningsCmd = '';
    if ($warnings) {
        if ($xml) {
            $warningsCmd = "$nsgmlsXMLWarnings";
        } else {
            $warningsCmd = "$nsgmlsWarnings";
        }
    }

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

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

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

        if ($#error < 4) {

            next;

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

            # With warnings enabled in non-XML validation, some "errors"
            # reported by lq-nsgmls are probably better reported as "warnings"
            # since they are only reported with warnings enabled.
            if ($warnings && !$xml) {
                if ($errorAsWarning{$error[5]}) {
                    $error[4] = 'W';

                    # lq-nsgmls uses an XML-specific message for one of
                    # these warnings.  Let's try something more helpful
                    # for HTML.
                    if ($error[5] eq ' net-enabling start-tag not supported in {{XML}}') {
                        $error[5] = ' net-enabling start-tag; possibly missing required quotes around an attribute value or using XHTML syntax in HTML';
                    }

                    $_ = join(':', @error);
                }
            }

            push(@errors, $_);

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

        } elsif ($error[4] eq 'W') {

            unless ($error[5] eq ' characters in the document character set with numbers exceeding 65535 not supported')
            {
                push(@errors, $_);
            }

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

            push(@externalErrors, $_);

        } elsif (length($error[4]) > 1 # Allow secondary messages about preceding error
            && $error[3] ne 'W') # Prevent error about SGML declaration not implied with -wxml
        {
            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(stripLqNsgmlsGunk($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;

            my $msgType;
            if ($error[4] eq 'E' || $error[4] eq 'X') { # Error message
                $msgType = $emacs ? 'E' : 'Error at';
            } elsif ($error[4] eq 'W') {
                $msgType = $emacs ? 'W' : 'Warning at';
            }


            # Prepare error message
            my $errorMsg;
            if ($emacs) {
                $errorMsg = "$file:$lineNumber:$character:";
                if (defined $msgType) {
                    $errorMsg .= "$msgType:";
                }
            } else {
                my $line;
                if (defined $msgType) {
                    $line = ' line';
                } else {
                    $line = 'Line';
                    $msgType = "\t";
                }
                $errorMsg = "$msgType$line $lineNumber, character $character: ";
            }

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

            &htmlError(stripLqNsgmlsGunk($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) {
        my $andWarnings = $warnings ? ' and warnings' : '';

        if ($file eq '-') {
            print $errout "*** Errors$andWarnings: ***\n";
        } else {
            if ($emacs) {
                print $errout "*** Errors" . $andWarnings . " validating $file: ***\n";
            } else {
                print $errout wrap("", "\t",
                    "*** Errors" . $andWarnings . " validating $file: ***\n");
            }
        }
    }
}

sub quoteFilename {
    my $filename = shift;

    $filename =~ s/\\/\\\\/go;
    $filename =~ s/"/\\"/go;
 
    # Untaint
    if ($filename =~ /^(.*)$/) {
        $filename = $1;
    }

    return "\"$filename\"";
}

# Clean the "{{foo}}" used in nsgmls error messages
# The error message must be given as the first argument
sub stripLqNsgmlsGunk {
    my $errorMsg = shift;
    while ($errorMsg =~ m#\{\{"?(.+?)"?\}\}#gos) {
        my $linkText = $1;
        $errorMsg =~ s#\{\{(")?$linkText(")?\}\}#$1$linkText$2#;
    }
    return $errorMsg;
}

# 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:
  -w, --warn            include warnings
  --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
  --[no]emacs           (don't) use an output format intended for parsing
                        by (X)Emacs, autodetected
  -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.

Files can also be URIs if you have the URI and libwww-perl packages
installed.  Support for different URI schemes is also determined by these
packages.  Proxy settings are loaded from environment variables for
each scheme--e.g., http_proxy=http://localhost:3128.

"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

}