#! /usr/local/bin/perl
##---------------------------------------------------------------------------##
##  File:
##      dtdtree
##  Author:
##      Earl Hood       ehood@convex.com
##  Description:
## 	Perl program to output content hierarchy trees of SGML elements.
##---------------------------------------------------------------------------##
##  Copyright (C) 1994  Earl Hood, ehood@convex.com
##
##  This program is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.
##  
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##  
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to the Free Software
##  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##---------------------------------------------------------------------------##


package main;

## Store name of program ##
($PROG = $0) =~ s/.*\///;

unshift(@INC, '.', '/usr/local/hp/lib/perl', '/usr/local/sun/lib/perl');
require "dtd/dtd.pl" || die "Unable to require dtd.pl\n";
require "newgetopt.pl" || die "Unable to require newgetopt.pl\n";

##-------------##
## Get options ##
##-------------##
&NGetOpt(
    "mapfile=s",
    "dtd=s",
    "treefile=s",
    "level=i",
    "help"
);
&Usage() if defined($opt_help);

$DTDFILE  = ($opt_dtd ? $opt_dtd : "");
$TREEFILE = ($opt_treefile ? $opt_treefile : "");
$MAPFILE  = ($opt_mapfile ? $opt_mapfile : "map.txt");
$LEVEL    = ($opt_level ? $opt_level : 15);

if ($DTDFILE) {
    open(DTD_FILE, "< $DTDFILE") || die "Unable to open $DTDFILE\n";
    $DTD = "main'DTD_FILE";
} else {
    $DTD = 'STDIN';
}
if ($TREEFILE) {
    open(TREE_FILE, "> $TREEFILE") || die "Unable to create $TREEFILE\n";
    $TREE = 'TREE_FILE';
} else {
    $TREE = 'STDOUT';
}

##----------##
## Read DTD ##
##----------##
&DTDread_mapfile($MAPFILE);
&DTDread_dtd($DTD);

##-------------##
## Print Trees ##
##-------------##
if ($#ARGV >= 0) { @array = @ARGV; }
else { @array = &DTDget_top_elements(); }

select($TREE); $^ = Empty; $~ = ElementHead; $= = 10000000;
foreach $elem (@array) {
    $elem =~ tr/a-z/A-Z/;
    write;
    &DTDprint_tree($elem, $LEVEL, $TREE);
}

##---------##
## Formats ##
##---------##
format Empty=
.

format ElementHead=
------------------------------------------------------------------------------
@|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$elem
------------------------------------------------------------------------------
.

exit(0);

##---------------------------##
## Usage description routine ##
##---------------------------##
sub Usage {
    print STDOUT <<EndOfUsage;

USAGE: $PROG [-dtd <filename>] [-help] [-level <#>] [-mapfile <filename>]
		[-treefile <filename>] <element> ...
    -dtd <filename>
	Use <filename> as the SGML dtd to parse.  Otherwise read from stdin.
    -help
	Print this usage message.
    -level <#>
	Only go down to tree level <#>.  The root element is at level 1.
	Default tree level depth is 15.
    -mapfile <filename>
	Use <filename> as entity map file.  Defaults to "map.txt".
    -treefile <filename>
	Output element content tree(s) to <filename>.  Otherwise print to
	stdout.

DESCRIPTION
    $PROG outputs the content hierarchy tree of SGML elements defined in a
    dtd.  Any strings that are not part of the command-line options are
    treated as the elements to output trees for.  If no elements are
    specified, than the tree(s) for the top-most element(s) defined in the
    dtd are printed.

EndOfUsage
    exit(0);
}
