<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># perl -w
# ShowTable.pm
#
#    Copyright (C) 1996,1997  Alan K. Stebbens &lt;aks@sgi.com&gt;
#
#    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.
#
# $Id: ShowTable.pm,v 1.20 1997/03/02 22:04:50 aks Exp $
#

package Data::ShowTable;

=head1 NAME

B&lt;ShowTable&gt; - routines to display tabular data in several formats.

=head1 USAGE

C&lt;use Data::ShowTable;&gt;

B&lt;ShowTable&gt; { I&lt;parameter&gt; =&gt; I&lt;value&gt;, ... };

B&lt;ShowTable&gt; I&lt;\@titles&gt;, I&lt;\@types&gt;, I&lt;\@widths&gt;, I&lt;\&amp;row_sub&gt; [, I&lt;\&amp;fmt_sub&gt; ];


B&lt;ShowDatabases&gt; I&lt;\@dbnames&gt;;

B&lt;ShowDatabases&gt; { I&lt;parameter&gt; =&gt; I&lt;value&gt;, ... };


B&lt;ShowTables&gt; I&lt;\@tblnames&gt;;

B&lt;ShowTables&gt; { I&lt;parameter&gt; =&gt; I&lt;value&gt;, ... };


B&lt;ShowColumns&gt; I&lt;\@columns&gt;, I&lt;\@col_types&gt;, I&lt;\@col_lengths&gt;, I&lt;\@col_attrs&gt;;

B&lt;ShowColumns&gt; { I&lt;parameter&gt; =&gt; I&lt;value&gt;, ... };


B&lt;ShowBoxTable&gt; I&lt;\@titles&gt;, I&lt;\@types&gt;, I&lt;\@widths&gt;, I&lt;\&amp;row_sub&gt; [, I&lt;\&amp;fmt_sub&gt; ];

B&lt;ShowBoxTable&gt; { I&lt;parameter&gt; =&gt; I&lt;value&gt;, ... };


B&lt;ShowSimpleTable&gt; I&lt;\@titles&gt;, I&lt;\@types&gt;, I&lt;\@widths&gt;, I&lt;\&amp;row_sub&gt; [, I&lt;\&amp;fmt_sub&gt;];

B&lt;ShowSimpleTable&gt; { I&lt;parameter&gt; =&gt; I&lt;value&gt;, ... };


B&lt;ShowHTMLTable&gt; I&lt;\@titles&gt;, I&lt;\@types&gt;, I&lt;\@widths&gt;, I&lt;\&amp;row_sub&gt; [, I&lt;\&amp;fmt_sub&gt;];

B&lt;ShowHTMLTable&gt; { I&lt;parameter&gt; =&gt; I&lt;value&gt;, ... };


B&lt;ShowListTable&gt; I&lt;\@titles&gt;, I&lt;\@types&gt;, I&lt;\@widths&gt;, I&lt;\&amp;row_sub&gt; [, I&lt;\&amp;fmt_sub&gt;];

B&lt;ShowListTable&gt; { I&lt;parameter&gt; =&gt; I&lt;value&gt;, ... };

C&lt;package Data::ShowTable&gt;;

B&lt;$Show_Mode&gt; = 'I&lt;mode&gt;';

B&lt;$Max_Table_Width&gt; = I&lt;number&gt;;

B&lt;$Max_List_Width&gt; = I&lt;number&gt;;

B&lt;$No_Escape&gt; = I&lt;flag&gt;;

B&lt;%URL_Keys&gt; = { "I&lt;$colname&gt;" =&gt; "I&lt;$col_URL&gt;", ... };

B&lt;@Title_Formats&gt; = ( I&lt;fmt1_html&gt;, &lt;fmt2_html&gt;, ... );

B&lt;@Data_Formats&gt; = ( I&lt;fmt1_html&gt;, &lt;fmt2_html&gt;, ... );

B&lt;ShowRow&gt; I&lt;$rewindflag&gt;, I&lt;\$index&gt;, I&lt;$col_array_1&gt; [, I&lt;$col_array_2&gt;, ...;]

I&lt;$fmt&gt; = B&lt;ShowTableValue&gt; I&lt;$value&gt;, I&lt;$type&gt;, I&lt;$max_width&gt;, I&lt;$width&gt;, I&lt;$precision&gt;, I&lt;$showmode&gt;;

[I&lt;$plaintext&gt; = ] B&lt;PlainText&gt; [I&lt;$htmltext&gt;];

=head1 DESCRIPTION

The B&lt;ShowTable&gt; module provides subroutines to display tabular data,
typially from a database, in nicely formatted columns, in several formats.
Its arguments can either be given in a fixed order, or, as
a single, anonymous hash-array.

The output format for any one invocation can be one of four possible styles:

=over 10

=item Box

A tabular format, with the column titles and the entire table surrounded by a
"box" of "C&lt;+&gt;", "C&lt;-&gt;", and "C&lt;|&gt;" characters.  See L&lt;"ShowBoxTable"&gt; for details.

=item Table

A simple tabular format, with columns automatically aligned, with column titles.
See L&lt;"ShowSimpleTable"&gt;.

=item List

A I&lt;list&gt; style, where columns of data are listed as a I&lt;name&gt;:I&lt;value&gt; pair, one
pair per line, with rows being one or more column values, separated by an empty line.
See L&lt;"ShowListTable"&gt;.

=item HTML

The data is output as an HTML I&lt;TABLE&gt;, suitable for display through a I&lt;Web&gt;-client.
See L&lt;"ShowHTMLTable"&gt;.  Input can either be plain ASCII text, or text
with embedded HTML elements, depending upon an argument or global parameter.

=back

The subroutines which perform these displays are listed below.

=head1 EXPORTED NAMES

This module exports the following subroutines: 

 ShowDatabases    - show list of databases
 ShowTables       - show list of tables
 ShowColumns      - show table of column info
 ShowTable        - show a table of data
 ShowRow          - show a row from one or more columns
 ShowTableValue   - show a single column's value
 ShowBoxTable     - show a table of data in a box
 ShowListTable    - show a table of data in a list
 ShowSimpleTable  - show a table of data in a simple table
 ShowHTMLTable    - show a table of data using HTML
 PlainText	  - convert HTML text into plain text

All of these subroutines, and others, are described in detail in the
following sections.

=cut

use Exporter;

@ISA = qw( Exporter );
@EXPORT = qw(   ShowDatabases 
                ShowTables 
                ShowColumns 
                ShowTable 
                ShowRow 
                ShowBoxTable 
                ShowHTMLTable 
                ShowListTable
                ShowSimpleTable 
		ShowTableValue
                Show_Mode
		PlainText
                URL_Keys 
            );

@EXPORT_OK = qw( Show_Mode
		 URL_Keys
		 Title_Formats
		 Data_Formats
		);

# Some control variables -- the user may set these

$Show_Mode        = 'Box';      # one of: List, Table, Box, or HTML
$List_Wrap_Margin = 10;         # break words up to this long
$Max_Table_Width  = '';         # if defined, scale tables
$Max_List_Width   = $ENV{'COLUMNS'} || 80;
$No_Escape        = '';		# escape by default

%URL_Keys = ();
@Title_Formats	  = ();		# formats for HTML formatting
@Data_Formats	  = ();

use Carp;

unshift(@INC, '.');

sub ShowDatabases;
sub ShowTables;
sub ShowColumns;
sub ShowTable;
sub ShowRow;
sub PlainText;
sub htmltext;

sub get_params;
sub html_formats;
sub center;
sub max_length;
sub max;
sub out;
sub put;

=head1 MODULES

=head1 ShowTable 

Format and display the contents of one or more rows of data.

S&lt;  &gt;B&lt;ShowTable&gt; { I&lt;parameter&gt; =&gt; I&lt;value&gt;, ... };

S&lt;  &gt;B&lt;ShowTable&gt; I&lt;\@titles&gt;, I&lt;\@types&gt;, I&lt;\@widths&gt;, I&lt;\&amp;row_sub&gt; 
[, I&lt;\&amp;fmt_sub&gt; [, I&lt;$max_width&gt; ] [, I&lt;$show_mode&gt; ] ];

The B&lt;ShowTable&gt; subroutine displays tabular data aligned in columns,
with headers.  B&lt;ShowTable&gt; supports four I&lt;modes&gt; of display: B&lt;Box&gt;, B&lt;Table&gt;,
B&lt;List&gt;, and B&lt;HTML&gt;.  Each mode is described separately below.

The arguments to B&lt;ShowTable&gt; may be given in one of two ways: as a
hashed-array, or by a combination of fixed order arguments, and some
package-global variable settings.  The hash-array parameters correspond
to the fixed arguments and the global-parameter settings.

In the list below, both the hash-array parameter name and the
fixed-order argument name is given as the value.  In the case where
there is no fixed-order argument for a given parameter-value pair, then
the corresponding global variable name is given.

=over 10

=item C&lt;titles&gt; =&gt; I&lt;\@titles&gt;

A reference to an array of column names, or titles.  If a particular column name
is null, then the string C&lt;Field_I&lt;num&gt;&gt; is used by default.  To have a column
have no title, use the empty string.

=item C&lt;types&gt; =&gt; I&lt;\@types&gt;

A reference to an array of types, one for each column.  These types are
passed to the I&lt;fmt_sub&gt; for appropriate formatting.  Also, if a column
type matches the regexp "C&lt;/text|char|string/i&gt;", then the column
alignment will be left-justified, otherwise it will be right-justified.

=item C&lt;widths&gt; =&gt; I&lt;\@widths&gt;

A reference to an array of column widths, which may be given as an integer, or
as a string of the form: "I&lt;width&gt;.I&lt;precision&gt;".

=item C&lt;row_sub&gt; =&gt; I&lt;\&amp;row_sub&gt;

A reference to a subroutine which successively returns rows of values in an array.
It is called for two purposes, each described separately:

* To fetch successive rows of data:

    @row = &amp;$row_sub(0);

When given a null, zero, or empty argument, the next row is returned.

* To initialize or rewind the data traversal.

    $rewindable = &amp;$row_sub(1);

When invoked with a non-null argument, the subroutine should rewind its
row pointer to start at the first row of data.  If the data which
I&lt;row_sub&gt; is traversing is not rewindable, it must return zero or null.
If the data is rewindable, a non-null, non-zero value should be returned.

The I&lt;row_sub&gt; must expect to be invoked once with a non-null argument,
in order to discover whether or not the data is rewindable.  If the data
cannot be rewound, I&lt;row_sub&gt; will thereafter only be called with a zero
argument. 

Specifically, I&lt;row_sub&gt; subroutine is used in this manner:

    $rewindable = &amp;$row_sub(1);
    if ($rewindable) {
        while ((@row = &amp;$row_sub(0)), $#row &gt;= 0) {
            # examine lengths for optimal formatting
        }
        &amp;$row_sub(1);   # rewind
    }
    while ((@row = &amp;$row_sub(0)), $#row &gt;= 0) {
        # format the data
    }

The consequence of data that is not rewindable, a reasonably nice table
will still be formatted, but it may contain fairly large amounts of
whitespace for wide columns.

=item C&lt;fmtsub&gt; =&gt; I&lt;\&amp;fmt_sub&gt;

A reference to a subroutine which formats a value, according to its
type, width, precision, and the current column width.  It is invoked
either with a fixed list of arguments, or with a hash-array of parameter
and value pairs.

  $string = &amp;fmt_sub { I&lt;parameter&gt; =&gt; I&lt;value&gt;, ... };

  $string = &amp;fmt_sub($value, $type, $max_width, $width, $precision)

If I&lt;\&amp;fmt_sub&gt; is omitted, then a default subroutine, B&lt;ShowTableValue&gt;, 
will be used, which will use Perl's standard string formatting rules.

The arguments to I&lt;\&amp;fmt_sub&gt;, either as values passed in a fixed
order, or as part of the parameter value pair, are described in the
section on L&lt;"ShowTableValue&gt; below.

=item C&lt;max_width&gt; =&gt; I&lt;number&gt;,

The maximum table width, including the table formatting characters.  If
not given, defaults to the global variable B&lt;$Max_Table_Width&gt;;

=item C&lt;show_mode&gt; =&gt; 'I&lt;mode&gt;',

The display mode of the output.  One of five strings: C&lt;'Box'&gt;,
C&lt;'Table'&gt;, C&lt;'Simple'&gt;, C&lt;'List'&gt;, and C&lt;'HTML'&gt;.

=back

=cut

sub ShowTable {
    my @argv = @_;
    local ($_,$titles,$types,$widths,$row_sub,$fmt_sub,
    	   $max_width, $show_mode, $wrap_margin, $url_keys,
	   $no_escape, $title_formats, $data_formats);
    my $args = 
    	get_params \@argv, 
	{   titles        =&gt; \$titles,     
	    types	  =&gt; \$types,
	    widths        =&gt; \$widths,
	    row_sub       =&gt; \$row_sub,
	    fmt_sub       =&gt; \$fmt_sub,
	    max_width     =&gt; \$max_width,
	    show_mode     =&gt; \$show_mode,
	},
	[qw(titles types widths row_sub fmt_sub max_width show_mode)];

    # Default mode is from $Show_Mode global
    $show_mode = $args-&gt;{'show_mode'} = $Show_Mode unless $show_mode ne '';
    $_ = $show_mode;
    if    (/List/i)     { &amp;ShowListTable($args); }
    elsif (/HTML/i)     { &amp;ShowHTMLTable($args); }
    elsif (/Table/i)    { &amp;ShowSimpleTable($args);  }
    else                { &amp;ShowBoxTable($args); }
}

=head1 ShowDatabases 

Show a list of database names.

S&lt;  &gt;B&lt;ShowDatabases&gt; I&lt;\@dbnames&gt;;

S&lt;  &gt;B&lt;ShowDatabases&gt; { 'data' =&gt; I&lt;\@dbnames&gt;, I&lt;parameter&gt; =&gt;
I&lt;value&gt;, ...};

B&lt;ShowDatabases&gt; is intended to be used to display a list of database
names, under the column heading of "Databases".  It is a special case
usage of B&lt;ShowTable&gt; (and can thus be passed any parameter suitable 
for B&lt;ShowTable&gt;.

The argument, I&lt;\@dbnames&gt;, is a reference to an array of strings, used
as the values of the single column display.

=cut

sub ShowDatabases {
    my @argv = @_;
    local $databases;
    my $args = get_params \@argv, {data =&gt; \$databases}, ['data'];
    $databases ne '' or croak "Missing array of databases.\n";

    $args-&gt;{'titles'}	= 'Databases' unless exists $args-&gt;{'titles'};
    $args-&gt;{'types'}	= [ 'char' ];
    $args-&gt;{'width'}	= max_length $databases;
    $args-&gt;{'lengths'}	= $args-&gt;{'width'};
    local( $current_row ) = 0;
    $args-&gt;{'row_sub'}	= sub { &amp;ShowRow( $_[0], \$current_row, $databases ); };
    ShowTable $args;
}

=head1 ShowTables 

Show an array of table names.

S&lt;  &gt;B&lt;ShowTables&gt; I&lt;\@tblnames&gt;;

S&lt;  &gt;B&lt;ShowTables&gt; { 'data' =&gt; I&lt;\@tblnames&gt;, I&lt;parameter&gt; =&gt; I&lt;value&gt;, ...};

B&lt;ShowTables&gt; is used to display a list of table names, under the column
heading of "Tables".  It is a special case usage of B&lt;ShowTable&gt;, and can
be passed any L&lt;"ShowTable"&gt; argument parameter.

=cut

sub ShowTables {
    my @argv = @_;
    local $tables;
    my $args = get_params \@argv, {data =&gt; \$tables}, ['data'];
    $tables ne '' or croak "Missing array of tables.\n";

    $args-&gt;{'titles'}	= 'Tables' unless exists $args-&gt;{'titles'};
    $args-&gt;{'types'}	= 'char';
    $args-&gt;{'width'}	= max_length $tables;
    $args-&gt;{'lengths'}	= $args-&gt;{'width'};
    local( $current_row ) = 0;
    $args-&gt;{'row_sub'}	= sub { &amp;ShowRow( $_[0], \$current_row, $tables ); };
    ShowTable $args;
}

=head1 ShowColumns 

Display a table of column names, types, and attributes.

S&lt;  &gt;B&lt;ShowColumns&gt; { I&lt;parameter&gt; =&gt; I&lt;values&gt;, ... };

S&lt;  &gt;B&lt;ShowColumns&gt; I&lt;\@columns&gt;, I&lt;\@col_types&gt;, I&lt;\@col_lengths&gt;, I&lt;\@col_attrs&gt;;

The B&lt;ShowColumns&gt; subroutine displays a table of column names, types, lengths,
and other attributes in a nicely formatted table.  It is a special case usage
of B&lt;ShowTable&gt;, and can be passed any argument suitable for L&lt;"ShowTable"&gt;;

The arguments are:

=over 10

=item C&lt;columns&gt; = I&lt;\@columns&gt;

An array of column names.  This provides the value for the first column
of the output.

=item C&lt;col_types&gt; = I&lt;\@col_types&gt;

An array of column types names.  This provides the value for the second
column. 

=item C&lt;col_lengths&gt; = I&lt;\@col_lengths&gt;

An array of maximum lengths for corresponding columns.  This provides
the value for the third column of the output.

=item C&lt;col_attrs&gt; = I&lt;\@col_attrs&gt;

An array of column attributes array references (ie: an array of arrays).
The attributes array for the first column are at "I&lt;$col_attrs&gt;-\&gt;[0]".
The first attribute of the second column is "I&lt;$col_attrs&gt;-\&gt;[1][0]".

=back

The columns, types, lengths, and attributes are displayed in a table
with the column headings: "Column", "Type", "Length", and "Attributes".
This is a special case usage of B&lt;ShowTable&gt;, and can be passed
additional arguments suitable for L&lt;"ShowTable"&gt;.

=cut

sub ShowColumns {
    my @argv = @_;
    local ($col_names, $col_types, $col_lengths, $col_attributes);
    my $args = 
	get_params 
	    \@argv, 
	    { col_names	     =&gt; \$col_names,
	      col_types	     =&gt; \$col_types,
	      col_lengths    =&gt; \$col_lengths,
	      col_attributes =&gt; \$col_attributes,
	    },[qw(col_names col_types col_lengths col_attributes)];

    $col_names ne ''      or croak "Missing array of column names.\n";
    $col_types ne ''      or croak "Missing array of column types.\n";
    $col_lengths ne ''    or croak "Missing array of column lengths.\n";
    $col_attributes ne '' or croak "Missing array of column attributes.\n";

    # setup the descriptor arrays
    $args-&gt;{'titles'} = [ qw(Column Type Length Attributes) ];
    $args-&gt;{'types'}  = [ qw(varchar varchar int varchar) ];

    # Do some data conversions before displaying
    # Convert attribute array to a string of attributes
    local @col_attrs = ();
    my $i;
    for ($i = 0; $i &lt;= $#{$col_attributes}; $i++) {
	$col_attrs[$i] = join(', ',@{$col_attributes-&gt;[$i]});
    }

    # count the widths, to setup the Column name column width
    $args-&gt;{'lengths'} = [ (max_length $col_names),   (max_length $col_types), 
		           (max_length $col_lengths), (max_length \@col_attrs) ];

    local($current_row) = 0;
    $args-&gt;{'row_sub'} = sub { &amp;ShowRow($_[0], \$current_row, $col_names, 
    				   $col_types, $col_lengths, \@col_attrs); };

    # Finally, show the darn thing
    ShowTable $args;
}


=head1 ShowBoxTable 

Show tabular data in a box.

S&lt;  &gt;B&lt;ShowBoxTable&gt; { I&lt;parameter&gt; = I&lt;value&gt;, ... };

S&lt;  &gt;B&lt;ShowBoxTable&gt; I&lt;\@titles&gt;, I&lt;\@types&gt;, I&lt;\@widths&gt;, I&lt;\&amp;row_sub&gt;
S&lt;      &gt;[, [ I&lt;\&amp;fmt_sub&gt; ] [, I&lt;$max_width&gt; ] ];

The B&lt;ShowBoxTable&gt; displays tabular data in titled columns using a "box" 
of ASCII graphics, looking something like this:
 

	+------------+----------+-----+----------+ 
	| Column1    | Column2  | ... | ColumnN  |
	+------------+----------+-----+----------+
	| Value11    | Value12  | ... | Value 1M |
	| Value21    | Value22  | ... | Value 2M |
	| Value31    | Value32  | ... | Value 3M |
	|  ...       |  ...     | ... |  ...     |
	| ValueN1    | ValueN2  | ... | Value NM |
	+------------+----------+-----+----------+


The arguments are the same as with L&lt;"ShowTable"&gt;.  If the I&lt;@titles&gt; array
is empty, the header row is omitted.

=cut

sub ShowBoxTable {
    my @argv = @_;
    local ($titles, $types, $col_widths, $row_sub, $fmt_sub, $max_width);
    my $args = 
	get_params 
	    \@argv, 
	    { titles	=&gt; \$titles,
	      types	=&gt; \$types, 
	      widths	=&gt; \$col_widths,
	      row_sub   =&gt; \$row_sub, 
	      fmtsub	=&gt; \$fmt_sub,
	      max_width =&gt; \$max_width,
	    },
	    [qw(titles types widths row_sub fmtsub max_width)];

    $titles      ne ''  or croak "Missing column names array.\n";
    $types       ne ''  or croak "Missing column types array.\n";
    $col_widths  ne ''  or croak "Missing column width array.\n";
    $row_sub     ne ''  or croak "Missing row subroutine.\n";
    $fmt_sub   = \&amp;ShowTableValue if !defined($fmt_sub)   || $fmt_sub eq '';
    $max_width = $Max_Table_Width if !defined($max_width) || $max_width eq '';

    my $rewindable  = &amp;$row_sub(1);	# see if data is rewindable

    my ($num_cols, $widths, $precision, $max_widths) = 
    	&amp;calc_widths($col_widths, $titles, $rewindable, 
		     $row_sub, $fmt_sub, $types, 'box', $max_width);

    my $width = 1;
    my $dashes = ' +';
    my $title_line = ' |';
    my $title;
    my $fmt = ' |';		# initial format string
    my $c;

    # Compose the box header
    for ($c = 0; $c &lt; $num_cols; $c++) {
	$width = $max_widths-&gt;[$c];	# get previously calculated max col width
	$width += 2; 			# account for a blank on either
					# side of each value
	$dashes .= ('-' x $width);
	$dashes .= '+';

	$title = $#$titles &gt;= 0 &amp;&amp; defined($titles-&gt;[$c]) ? $titles-&gt;[$c] :
		sprintf("Field_%d", $c+1);
	$title_line .= center $title, $width;
	$title_line .= '|';
    }
    out $dashes;
    if ($#$titles &gt;= 0) {
	out $title_line;
	out $dashes;
    }

    my @values;
    my @prefix = (" ", "&lt;");
    my @suffix = (" |", "&gt;|");
    my @cell;

    # loop over the data, formatting it into cells, one row at a time.
    while ((@values = &amp;$row_sub(0)), $#values &gt;= $[) {
	# first pass -- format each value into a string
	@cell = ();
	for ($c = 0; $c &lt;= $#values; $c++) {
	    $cell[$c] = &amp;$fmt_sub($values[$c], $types-&gt;[$c], $max_widths-&gt;[$c],
				  $widths-&gt;[$c], $precision-&gt;[$c], 'box');
	}
	# second pass -- output each cell, wrapping if necessary
	my $will_wrap;
	my $wrapped = 0;
	do { $will_wrap = 0;
	    put " |";		# start a line
	    for ($c = 0; $c &lt;= $#cell; $c++) {
		$will_wrap |= &amp;putcell(\@cell, $c, $max_widths-&gt;[$c],
				       \@prefix, \@suffix, $wrapped);
	    }
	    out "";
	    $wrapped++;
	} while ($will_wrap);
    }
    out $dashes;
    out "";
}


=head1 ShowSimpleTable 

Display a table of data using a simple table format.

S&lt;  &gt;B&lt;ShowSimpleTable&gt; I&lt;\@titles&gt;, I&lt;\@types&gt;, I&lt;\@widths&gt;, I&lt;\&amp;row_sub&gt; [, I&lt;\&amp;fmt_sub&gt;];

S&lt;  &gt;B&lt;ShowSimpleTable&gt; { I&lt;parameter&gt; =&gt; I&lt;values&gt;, ... };

The B&lt;ShowSimpleTable&gt; subroutine formats data into a simple table of
aligned columns, in the following example:

   Column1  Column2  Column3
   -------  -------  -------
   Value1   Value2   Value3
   Value12  Value22  Value32

Columns are auto-sized by the data's widths, plus two spaces between columns.
Values which are too long for the maximum colulmn width are wrapped within
the column.

=cut

sub ShowSimpleTable {
    my @argv = @_;
    local ($titles, $types, $col_widths, $row_sub, $fmt_sub, $max_width);
    my $args = 
    	get_params 
	    \@argv, 
	    { titles	=&gt; \$titles,
	      types	=&gt; \$types, 
	      widths	=&gt; \$col_widths,
	      row_sub   =&gt; \$row_sub, 
	      fmtsub	=&gt; \$fmt_sub,
	      max_width =&gt; \$max_width,
	    },
	    [qw(titles types widths row_sub fmtsub max_width)];

    $titles      ne ''  or croak "Missing column names array.\n";
    $types       ne ''  or croak "Missing column types array.\n";
    $col_widths  ne ''  or croak "Missing column width array.\n";
    $row_sub     ne ''  or croak "Missing row sub array.\n";
    $fmt_sub  = \&amp;ShowTableValue  if !defined($fmt_sub)   || $fmt_sub eq '';
    $max_width = $Max_Table_Width if !defined($max_width) || $max_width eq '';

    my $rewindable  = &amp;$row_sub(1);		# see if data is rewindable

    my ($num_cols, $widths, $precision, $max_widths) = 
    	&amp;calc_widths($col_widths, $titles, $rewindable, 
		     $row_sub, $fmt_sub, $types, 'table', $max_width);

    my $width  = 1;
    my $dashes      = ' ';
    my $title_line  = ' ';
    my $title ;
    my $postfix = shift;
    my $c ;

    # Calculate the maximum widths
    for ($c = 0; $c &lt; $num_cols; $c++) {
	$width = $max_widths-&gt;[$c];
	$dashes .= ('-' x $width);
	$dashes .= '  ';

	next if $#$titles &lt; 0;
	$title = center $titles-&gt;[$c], $width;
	$title_line .= $title;
	$title_line .= '  ';

    }
    out $title_line if $#$titles &gt;= 0;
    out $dashes;

    my @values;
    my @prefix = (" ", "&lt;");
    my @suffix = (" ", "&gt;");

    while ((@values = &amp;$row_sub(0)), $#values &gt;= $[) {
	# first pass -- format each value into a string
	my @cell;
	for ($c = 0; $c &lt;= $#values; $c++) {
	    $cell[$c] = &amp;$fmt_sub($values[$c], $types-&gt;[$c], $max_widths-&gt;[$c],
				  $widths-&gt;[$c], $precision-&gt;[$c], 'table');
	}
	# second pass -- output each cell, wrapping if necessary
	my $will_wrap;
	my $wrapped = 0;
	do { $will_wrap = 0;
	    for ($c = 0; $c &lt;= $#cell; $c++) {
		$will_wrap |= &amp;putcell(\@cell, $c, $max_widths-&gt;[$c],
		             	       \@prefix, \@suffix, $wrapped);
	    }
	    out "";
	    $wrapped++;
	} while ($will_wrap);
    }
    out "";
}

=head1 ShowHTMLTable 

Display a table of data nicely using HTML tables.

S&lt;  &gt;B&lt;ShowHTMLTable&gt; { I&lt;parameter&gt; =&gt; I&lt;value&gt;, ... };

S&lt;  &gt;B&lt;ShowHTMLTable&gt; I&lt;\@titles&gt;, I&lt;\@types&gt;, I&lt;\@widths&gt;, I&lt;\&amp;row_sub&gt;
[, I&lt;\&amp;fmt_sub&gt; [, I&lt;$max_width&gt; [, I&lt;\%URL_Keys&gt; [, I&lt;$no_escape&gt; 
[, I&lt;\@title_formats&gt; [, I&lt;\@data_formats&gt; [, I&lt;$table_attrs&gt; ] ] ] ] ] ] ];

The B&lt;ShowHTMLTable&gt; displays one or more rows of columns of data using
the HTML C&lt;\&lt;TABLE\&gt;&gt; feature.  In addition to the usual parameter arguments
of L&lt;"ShowTable"&gt;, the following parameter arguments are defined:

=over 10

=item C&lt;url_keys&gt; =&gt; I&lt;\%URL_Keys&gt;,

This is a hash array of column names (titles) and corresponding base
URLs.  The values of any column names or indexes occuring as keys in
the hash array will be generated as hypertext anchors using the
associated I&lt;printf&gt;-like string as the base URL. Either the column name
or the column index (beginning with 1) may be used as the hash key.

In the string value, these macros can be substituted:  

"C&lt;%K&gt;" is replaced with the column name.

"C&lt;%V&gt;" is replaced with the column value;

"C&lt;%I&gt;" is replaced with the column index.

For example, if we define the array:

    $base_url = "http://www.$domain/cgi/lookup?col=%K?val=%V";
    %url_cols = ('Author' =&gt; $base_url,
		 'Name'   =&gt; $base_url);

Then, the values in the C&lt;Author&gt; column will be generated with the following
HTML text:

    &lt;A HREF="http://www.$domain/cgi/lookup?col=Author?val=somevalue&gt;somevalue&lt;/A&gt;

and the values in the C&lt;Name&gt; column will be generated with the URL:

    &lt;A HREF="http://www.$domain/cgi/lookup?col=Name?val=othervalue&gt;othervalue&lt;/A&gt;

If this variable is not given, it will default to the global variable
C&lt;\%URL_Keys&gt;.

=item C&lt;no_escape&gt; =&gt; I&lt;boolean&gt;,

Unless B&lt;$no_escape&gt; is set, HTML-escaping is performed on the data
values in order to properly display the special HTML formatting
characters : '\&lt;', '\&gt;', and '&amp;'.  If you wish to display data with
embedded HTML text, you must set B&lt;$no_escape&gt;.

Enabling embedded HTML, turns on certain heuristics which enable the
user to more completely define appearance of the table.  For instance,
any C&lt;\&lt;TR\&gt;&gt; tokens found embedded *anywhere* within a row of data will
be placed at the front of the row, within the generated C&lt;\&lt;TR\&gt;&gt;.

Similarly, a row of data containing the C&lt;\&lt;THEAD\&gt;&gt; or C&lt;\&lt;TFOOT\&gt;&gt;
tokens, and their closing counterparts, will begin and end, respectively
a table header or footer data.

=item C&lt;title_formats&gt; =&gt; I&lt;\@title_formats&gt;,

=item C&lt;tformats&gt; =&gt; I&lt;\@title_formats&gt;,

An array of HTML formatting elements for the column titles, one for each
column.  Each array element is a list of one or more HTML elements,
given as C&lt;\&lt;ELEMENT\&gt;&gt; or plainly, C&lt;ELEMENT&gt;, and separated by a comma
C&lt;','&gt;, semi-colon C&lt;';'&gt;, or vertical bar C&lt;'|'&gt;.  Each given HTML
element is prepended to the corresponding column title, in the order
given.  The corresponding HTML closing elements are appended in the
opposite order.

For example, if I&lt;\@title_formats&gt; contains the two elements:

    [ 'FONT SIZE=+2,BOLD', 'FONT COLOR=red,EM' ]

then the text output for the title of the first column would be:

    &lt;FONT SIZE=+2&gt;&lt;BOLD&gt;I&lt;column_1_title&gt;&lt;/BOLD&gt;&lt;/FONT&gt;

If C&lt;title_formats&gt; is omitted, the global variable B&lt;@Title_Formats&gt;
is used by default.

=item C&lt;data_formats&gt; =&gt; I&lt;\@data_formats&gt;,

=item C&lt;dformats&gt; =&gt; I&lt;\@data_formats&gt;,

Similar to C&lt;title_formats&gt;, this array provides HTML formatting for
the columns of each row of data.  If C&lt;data_formats&gt; is omitted or
null, then the global variable B&lt;\@Data_Formats&gt; is used by default.

=item C&lt;table_attrs&gt; =&gt; I&lt;$table_attrs&gt;,

This variable defines a string of attributes to be inserted within the
C&lt;\&lt;TABLE\&gt;&gt; token.  For example, if the user wishes to have no table
border:

    ShowHTMLTable { 
	...
    	table_attrs =&gt; 'BORDER=0', 
        ...
    };

=back

=cut

sub ShowHTMLTable {
    my @argv = @_;
    local ($titles, $types, $col_widths, $row_sub, $fmt_sub, $max_width, 
    	   $url_keys, $no_escape, $title_formats, $data_formats, 
	   $show_mode, $table_attrs);
    my $args = 
    	get_params 
	    \@argv, 
	    { titles	    =&gt; \$titles,
	      types	    =&gt; \$types, 
	      widths	    =&gt; \$col_widths,
	      row_sub       =&gt; \$row_sub, 
	      fmtsub	    =&gt; \$fmt_sub,
	      max_width     =&gt; \$max_width,
	      url_keys	    =&gt; \$url_keys,
	      no_escape     =&gt; \$no_escape,
	      tformats 	    =&gt; \$title_formats,
	      dformats      =&gt; \$data_formats,
	      table_attrs   =&gt; \$table_attrs,
	      data_formats  =&gt; 'tformats',
	      title_formats =&gt; 'tformats',
	    },
	    [qw(titles types widths row_sub fmtsub max_width 
	        url_keys no_escape title_formats data_formats
		table_attrs)];

    $titles      ne ''  or croak "Missing column names array.\n";
    $types       ne ''  or croak "Missing column types array.\n";
    $col_widths  ne ''  or croak "Missing column width array.\n";
    $row_sub     ne ''  or croak "Missing row sub array.\n";

    # Defaults
    $fmt_sub  = \&amp;ShowTableValue     if !defined($fmt_sub)       || $fmt_sub eq '';
    $max_width = $Max_Table_Width    if !defined($max_width)     || $max_width eq '';
    $url_keys = \%URL_Keys 	     if !defined($url_keys)      || $url_keys eq '';
    $title_formats = \@Title_Formats if !defined($title_formats) || $title_formats eq '';
    $data_formats = \@Data_Formats   if !defined($data_formats)  || $data_formats eq '';
    $no_escape = $No_Escape 	     if !defined($no_escape);

    my $rewindable = &amp;$row_sub(1);		# see if rewindable

    my ($num_cols, $widths, $precision, $max_widths) = 
	&amp;calc_widths($col_widths, $titles, $rewindable, 
		     $row_sub, $fmt_sub, $types, 'html', $max_width);

    my $width  = 1;
    my $total_width = 0;
    my $title_line = '';
    my $title;
    my ($c,$x);
    my ($tprefixes,$tsuffixes,$dprefixes,$dsuffixes);

    # prepare the HTML prefixes and suffixes, if any
    ($tprefixes,$tsuffixes) = html_formats $title_formats 
    	if defined($title_formats) &amp;&amp; $title_formats ne '';
    ($dprefixes,$dsuffixes) = html_formats $data_formats  
    	if defined($data_formats) &amp;&amp; $data_formats ne '';

    if ($table_attrs) {			# any table attributes?
	local($_) = $table_attrs;
	$table_attrs .= ' BORDER=1'      unless /\bBORDER=/i;
	$table_attrs .= ' CELLPADDING=1' unless /\bCELLPADDING=/i;
	$table_attrs .= ' CELLSPACING=1' unless /\bCELLSPACING=/i;
    } else {
	$table_attrs = 'BORDER=2 CELLPADDING=1 CELLSPACING=1';
    }
	
    out "&lt;TABLE $table_attrs&gt;\n&lt;TR&gt;" ;
    map { $total_width += defined($_) ? $_ : 0; } @$max_widths;
    for ($c = 0; $c &lt; $num_cols; $c++) {
	# If the user specified a width, then use it.
	$width = defined($widths-&gt;[$c]) ? $widths-&gt;[$c] : $max_widths-&gt;[$c];
	my $pct_width = int(100 * $width/$total_width);
	$title_line .= " &lt;TH ALIGN=CENTER WIDTH=$pct_width%%&gt;";
	if ($#$titles &gt;= 0) {
	    if (($x = $#$tprefixes) &gt;= 0) {
		$title_line .= $tprefixes-&gt;[$c &gt; $x ? $x : $c];
	    }
	    $title_line .= $no_escape ? $titles-&gt;[$c] : &amp;htmltext($titles-&gt;[$c]);
	    if (($x = $#$tsuffixes) &gt;= 0) {
		$title_line .= $tsuffixes-&gt;[$c &gt; $x ? $x : $c];
	    }
	}
	$title_line .= "&lt;/TH&gt;\n";
    }
    out $title_line;
    out "&lt;/TR&gt;";

    my ($href, $key, $val, $out);
    while ((@values = &amp;$row_sub(0)), $#values &gt;= $[) {
	out "&lt;TR&gt; ";
	# Walk through the values
	for ($c = 0; $c &lt;= $#values; $c++) {
	    $out = "&lt;TD";
	    if (defined($val = $values[$c])) { # only worry about defined values
		# In HTML mode, all CHAR, TEXT, SYMBOL, or STRING data should
		# be escaped to protect HTML syntax "&lt;", "&gt;", "\", and "&amp;".
		if ($types-&gt;[$c] =~ /char|text|symbol|string/i) {
		    $val = &amp;htmltext($val) unless $no_escape;
		    $out .= " ALIGN=LEFT";
		} else {
		    $out .= " ALIGN=RIGHT";
		}
		$out .= "&gt;";
		# Discover if either the column name or column index
		# have been mapped to a URL.
		$href = '';
		foreach $key ( $#$titles &gt;= 0 &amp;&amp; &amp;PlainText($titles-&gt;[$c]),
				sprintf("%d", $c+1)) {
		    next unless $key ne '' &amp;&amp; defined($url_keys-&gt;{$key});
		    $href = $url_keys-&gt;{$key};
		    last;
		}
		if ($href ne '') {
		    if ($href =~ /%K/) {
			my $s = &amp;htmltext(&amp;PlainText($titles-&gt;[$c]), 1);
			$href =~ s/%K/$s/g;
		    }
		    if ($href =~ /%V/) {
			my $s = &amp;htmltext($val, 1);
			$href =~ s/%V/$s/g;
		    }
		    if ($href =~ /%I/) {
			my $s = sprintf("%d", $c+1);
			$href =~ s/%I/$s/g;
		    }
		    $out .= sprintf("&lt;A HREF=\"%s\"&gt;",$href);
		}
		$val = &amp;$fmt_sub($val, $types-&gt;[$c], 0, $widths-&gt;[$c], 
				 $precision-&gt;[$c], 'html');
		$val =~ s/^\s+//;		# don't try to align
		$val =~ s/\s+$//;

		if (($x = $#$dprefixes) &gt;= 0) {
		    $out .= $dprefixes-&gt;[$c &gt; $x ? $x : $c];
		}
		$out .= $val;
		if (($x = $#$dsuffixes) &gt;= 0) {
		    $out .= $dsuffixes-&gt;[$c &gt; $x ? $x : $c];
		}
		$out .= "&lt;/A&gt;" if $href;
	    } else {
		$out .= "&gt;";
	    }
	    $out .= "&lt;/TD&gt;";
	    out $out;
	}
	out "&lt;/TR&gt;";
    }
    out "&lt;/TABLE&gt;";
}

=head1 ShowListTable

Display a table of data using a list format.

S&lt;  &gt;B&lt;ShowListTable&gt; { I&lt;parameter&gt; =&gt; I&lt;value&gt;, ... };

S&lt;  &gt;B&lt;ShowListTable&gt; I&lt;\@titles&gt;, I&lt;\@types&gt;, I&lt;\@widths&gt;, I&lt;\&amp;row_sub&gt; 
[, I&lt;\&amp;fmt_sub&gt; [, I&lt;$max_width&gt; [, I&lt;$wrap_margin&gt; ] ] ];

The arguments for B&lt;ShowListTable&gt; are the same as for L&lt;"ShowTable"&gt;,
except for those described next.

=over 10

=item C&lt;max_width&gt; = I&lt;number&gt;,

=item C&lt;wrap_margin&gt; = I&lt;number&gt;,

Lines are truncated, and wrapped when their length exceeds
I&lt;$max_width&gt;.  Wrapping is done on a word-basis, unless the resulting
right margin exceeds I&lt;$wrap_margin&gt;, in which case the line is simply
truncated at the I&lt;$max_width&gt; limit.

The I&lt;$max_width&gt; variable defaults to B&lt;$Max_List_Width&gt;.  The
I&lt;$wrap_margin&gt; defaults to B&lt;$List_Wrap_Margin&gt;.

=back

In I&lt;List&gt; mode, columns (called "fields" in List mode) are displayed
wth a field name and value pair per line, with records being one or
more fields .  In other words, the output of a table would
look something like this:

    Field1_1: Value1_1
    Field1_2: Value1_2
    Field1_3: Value1_3
    ...
    Field1-N: Value1_M
    &lt;empty line&gt;
    Field2_1: Value2_1
    Field2_2: Value2_2
    Field2_3: Value2_3
    ...
    Field2_N: Value2_N
    ...
    FieldM_1: ValueM_1
    FieldM_2: ValueM_2
    ...
    FieldM_N: ValueM_N
    &lt;empty line&gt;
    &lt;empty line&gt;

Characteristics of I&lt;List&gt; mode:

=over 10

=item *

two empty lines indicate the end of data.

=item *

An empty field (column) may be omitted, or may have a label, but no
data.

=item *

A long line can be continue by a null field (column):

    Field2: blah blah blah
          : blah blah blah

=item *

On a continuation, the null field is an arbitrary number of leading
white space, a colon ':', a single blank or tab, followed by the
continued text.

=item *

Embedded newlines are indicated by the escape mechanism "\n".
Similarly, embedded tabs are indicated with "\t", returns with "\r". 

=item *

If the I&lt;@titles&gt; array is empty, the field names "C&lt;Field_&gt;I&lt;NN&gt;" are used
instead.

=back

=cut

sub ShowListTable {
    my @argv = @_;
    local ($titles, $types, $col_widths, $row_sub, $fmt_sub, $max_width, 
    	   $wrap_margin);
    my $args = 
    	get_params 
	    \@argv, 
	    { titles 	  =&gt; \$titles,
	      types	  =&gt; \$types,
	      widths 	  =&gt; \$col_widths,
	      row_sub	  =&gt; \$row_sub,
	      fmtsub 	  =&gt; \$fmt_sub,
	      max_width   =&gt; \$max_width,
	      wrap_margin =&gt; \$wrap_margin,
	    },
	    [qw(titles types widths row_sub fmt_sub max_width wrap_margin)];

    defined($titles)     &amp;&amp; $titles ne ''       or croak "Missing column names array.\n";
    defined($types)      &amp;&amp; $types  ne ''       or croak "Missing column types array.\n";
    defined($col_widths) &amp;&amp; $col_widths  ne ''  or croak "Missing column width array.\n";
    defined($row_sub)    &amp;&amp; $row_sub ne ''      or croak "Missing row sub array.\n";

    $fmt_sub  = \&amp;ShowTableValue     if !defined($fmt_sub)     || $fmt_sub eq '';
    $max_width = $Max_List_Width     if !defined($max_width)   || $max_width eq '';
    $wrap_margin = $List_Wrap_Margin if !defined($wrap_margin) || $wrap_margin eq '';

    my $rewindable = &amp;$row_sub(1);	# init the row pointer

    my ($num_cols, $widths, $precision, $max_widths) = 
	&amp;calc_widths($col_widths, $titles, $rewindable,
    		     $row_sub, $fmt_sub, $types, 'list', '');

    my $fmt = sprintf("%%-%ds : %%s\n", ($#$titles &gt;= 0 ? &amp;max_length($titles) : 8));
    my @values;
    my ($value, $c, $cut, $line);
    my $col_limit = $max_width - 2;

    while ((@values = &amp;$row_sub(0)), $#values &gt;= $[) {
	for ($c = 0; $c &lt;= $#values; $c++) {
	    # get this column's title
	    $title = $#$titles &gt;= 0 ? $titles-&gt;[$c] : sprintf("Field_%d", $c+1);
	    my $type  = $types-&gt;[$c];
	    my $width = 0;
	    my $prec  = $precision-&gt;[$c];
	    $value = &amp;$fmt_sub($values[$c], $type, 0, $width, $prec, 'list');
	    while (length($value)) {
		if (length($value) &gt; ($cut = $col_limit)) {
		    $line = substr($value, 0, $cut);
		    if ($line =~ m/([-,;? \t])([^-,;? \t]*)$/ &amp;&amp; 
			length($2) &lt;= $wrap_margin) {
			$cut = $col_limit - length($2);
			$line = substr($value, 0, $cut);
		    }
		    ($value = substr($value, $cut)) =~ s/^\s+//;
		} else {
		    $line = $value;
		    $value = '';
		}
		out $fmt, $title, $line;
		$title = '';
	    }
	}
	out "";
    }
}

=head1 ShowRow 

Fetch rows successively from one or more columns of data.

S&lt;  &gt;B&lt;ShowRow&gt; I&lt;$rewindflag&gt;, I&lt;\$index&gt;, I&lt;$col_array_1&gt; [, I&lt;$col_array_2&gt;, ...;]

The B&lt;ShowRow&gt; subroutine returns a row of data from one or more
columns of data.  It is designed to be used as a I&lt;callback&gt; routine,
within the B&lt;ShowTable&gt; routine.   It can be used to select elements
from one or more array reference arguments.

If passed two or more array references as arguments, elements of the
arrays selected by I&lt;$index&gt; are returned as the "row" of data.

If a single array argument is passed, and each element of the array is
itself an array, the subarray is returned as the "row" of data.

If the I&lt;$rewindflag&gt; flag is set, then the I&lt;$index&gt; pointer is reset
to zero, and "true" is returned (a scalar 1).  This indicates that the
data is rewindable to the B&lt;ShowTable&gt; routines.

When the I&lt;$rewindflag&gt; is not set, then the current row of data, as
determined by I&lt;$index&gt; is returned, and I&lt;$index&gt; will
have been incremented.

An actual invocation (from B&lt;ShowColumns&gt;) is:

  ShowTable \@titles, \@types, \@lengths, 
      sub { &amp;ShowRow( $_[0], \$current_row, $col_names, $col_types,
                      $col_lengths, \@col_attrs); };

In the example above, after each invocation, the I&lt;$current_row&gt; argument 
will have been incremented.

=cut

sub ShowRow {
    my $rewind_flag = shift;
    my $index_ref = shift;              # an indirect index
    my @columns = @_;                   # get rest of columns
    my @row;                            # we're selecting a row
    if ($rewind_flag) {
        $$index_ref = 0;                # reset the pointer
        return 1;
    }
    return () if $#{$columns[0]} &lt; $$index_ref;
    if ($#columns == 0) {               # exactly one array ref argument
        my $data = $columns[0]-&gt;[$$index_ref];  # get the current data
        if (ref($data) eq 'ARRAY') {    # if an array..
            @row = @$data;              # ..return the array of data
        } elsif (ref($data) eq 'HASH') {# if a hash..
            @row = values %$data;       # ..return the values 
        } else {                        # otherwise..
            @row = ($data);             # ..return the data element
        }
    } else {                            # with two or more array refs..
        my $col;                        # select elements from each
        for ($col = 0; $col &lt;= $#columns; $col++) {
            push(@row, ${$columns[$col]}[$$index_ref]);
        }
    }
    ${$index_ref}++;                    # increment the index for the next call
    @row;                               # return this row of data
}

=head1 ShowTableValue

Prepare and return a formatted representation of a value.  A value
argument, using its corresponding type, effective width, and precision
is formatted into a field of a given maximum width. 

S&lt;  &gt;I&lt;$fmt&gt; = B&lt;ShowTableValue&gt; I&lt;$value&gt;, I&lt;$type&gt;, I&lt;$max_width&gt;, I&lt;$width&gt;, I&lt;$precision&gt;, I&lt;$showmode&gt;;

=over 10

=item C&lt;width&gt; =&gt; I&lt;$width&gt;

=item I&lt;$width&gt;

The width of the current value.  If omittied, I&lt;$max_width&gt; is assumed.

=item C&lt;precision&gt; =&gt; I&lt;$precision&gt;

=item I&lt;$precision&gt;

The number of decimal digits; zero is assumed if omittied.

=item C&lt;value&gt; =&gt; I&lt;$value&gt;

=item I&lt;$value&gt;

The value to be formatted.

=item I&lt;$type&gt;

The type name of the value; eg: C&lt;char&gt;, C&lt;varchar&gt;, C&lt;int&gt;, etc.

=item C&lt;maxwidth&gt; =&gt; I&lt;$max_width&gt;

=item I&lt;$max_width&gt;

The maximum width of any value in the current value's column.  If I&lt;$width&gt;
is zero or null, I&lt;$max_width&gt; is used by default.  I&lt;$max_width&gt; is also
used as a I&lt;minimum&gt; width, in case I&lt;$width&gt; is a smaller value.

=item I&lt;$width&gt;

The default width of the value, obtained from the width specification of the
column in which this value occurs.

=item I&lt;$precision&gt;

The precision specification, if any, from the column width specification.

=item I&lt;$showmode&gt;

The mode of the output: one of "table", "list", "box", or "html".  Currently,
only the "html" mode is significant: it is used to avoid using HTML tokens
as part of the formatted text and length calculations.

=back

=cut
    
sub ShowTableValue { 
    my $value     = shift;
    my $type      = shift;
    my $max_width = shift;
    my $width     = shift;
    my $prec      = shift || 2;
    my $showmode  = shift;
    my $fmt       = ($Type2Format{lc($type)} || $Type2Format{'char'});
    my $str;

    $max_width = 0 if !defined($max_width) || $max_width eq '';
    $width = $max_width if !defined($width) || $width eq '';

    $width = min($width, $max_width) if $max_width &gt; 0;
    if ($type =~ /money/i) {	# money formatting is special
	if (($str = $value) !~ /[\$,]/) {	# not already formatted?
	    my ($d,$c) = split(/\./,$value,2);
	    # reverse the digits
	    $d = join('',reverse(split(//,abs($d))));
	    # do the grouping from the rightmost to the left
	    $d =~ s/(...)(?=.)/$1,/g;
	    # reverse the digits and grouping char
	    $d = '$'.join('',reverse(split(//,$d)));
	    # If there is any precision, add on pennies
	    $d .= sprintf(".%02d",$c) if $prec &gt; 0;
	    # Mark as negative with '(xxx)'
	    $d = '-'.$d if $value &lt; 0;
	    $str = $d;
	}
    } else {
	$fmt = sprintf ($fmt,$width,$prec);

	# If we are in HTML mode, and the value has any HTML tokens,
	# then format it always as a string (even if it might
	# be a decimal--this is a kluge but seems to work).

	if ($showmode =~ /html/i &amp;&amp; $value =~ /&lt;\/?($HTML_Elements)/) {
	    $fmt =~ s/[df]/s/;	# convert to string sub
	}
	$str = sprintf($fmt,$value);
    }
    if ($width &gt; length(&amp;PlainText($str))) {
	# right align the value if any kind of number
	$str = sprintf("%${width}s", $str) 
	    if $type =~ /int|float|real|numeric|money/i;
    }
    $str;
}

%Type2Format = (
  'char'	=&gt; '%%-%ds',
  'varchar'	=&gt; '%%-%ds',
  'symbol'	=&gt; '%%-%ds',
  'tinyint'	=&gt; '%%%dd',
  'shortint'	=&gt; '%%%dd',
  'int'		=&gt; '%%%dd',
  'real'	=&gt; '%%%d.%df',
  'float'	=&gt; '%%%d.%df',
  'numeric'	=&gt; '%%%d.%df',
  'text'	=&gt; '%%-%ds',

  # The money types do not actually need to be in this table, since 
  # ShowTableValue handle money formatting explicitly.  However, some
  # one else might use this table, so we treat them like right-aligned
  # strings.
  'money'	=&gt; '%%%ds',
  'smallmoney'	=&gt; '%%%ds',

  );

=head1 PlainText

S&lt;  &gt;I&lt;$plaintext&gt; = B&lt;&amp;PlainText&gt;(I&lt;$htmltext&gt;);

S&lt;  &gt;B&lt;&amp;PlainText&gt;

This function removes any HTML formatting sequences from the input argument,
or from C&lt;$_&gt; if no argument is given.  The resulting plain text is returned
as the result.

=cut

#   $plaintext = &amp;PlainText($htmltext);
# or:
#   &amp;PlainText;
#
# Convert the argument and return as a string, or convert $_.

sub PlainText {
    local($_) = shift if $#_ &gt;= 0;	# set local $_ if there's an argument
					# skip unless there's a sequence
    return $_ unless m=&lt;/?($HTML_Elements)=i;	# HTML text?
    s{&lt;/?(?:$HTML_Elements)#		# match and remove any HTML token..
	 (?:\ \w+#			# ..then PARAM or PARAM=VALUE
	     (?:\=(?:"(?:[^"]|\\")*"|#	# ...."STRING" or..
		    [^"&gt; ]+#		# ....VALUE
		 )#
	     )?#			# ..=VALUE is optional
	 )*#				# zero or more PARAM or PARAM=VALUE
      &gt;}{}igx;				# up to the closing '&gt;'
    $_;					# return the result
}

BEGIN {

@HTML_Elements = qw(
    A ABBREV ACRONYM ADDRESS APP APPLET AREA AU B BANNER BASE BASEFONT BDO
    BGSOUND BIG BLINK BLOCKQUOTE BODY BQ BR CAPTION CENTER CITE CODE COL
    COLGROUP CREDIT DD DEL DFN DIR DIV DL DT EM EMBED FN FIG FONT FORM FRAME
    FRAMESET H1 H2 H3 H4 H5 H6 HEAD HP HR HTML I IMG INPUT INS ISINDEX KBD
    LANG LH LI LINK LISTING MAP MARQUEE MENU META NEXTID NOBR NOEMBED
    NOFRAMES NOTE OL OPTION OVERLAY P PARAM PERSON PLAINTEXT PRE Q S SAMP
    SELECT SMALL SPAN STRIKE STRONG SUB SUP TAB TABLE TBODY TD TEXTAREA
    TFOOT TH THEAD TITLE TR TT U UL VAR WBR XMP 
);

$HTML_Elements = join("|",@HTML_Elements);

}

=head1 VARIABLES

The following variables may be set by the user to affect the display (with
the defaults enclosed in square brackets [..]):

=over 10

=item B&lt;$Show_Mode&gt; [Box]

This is the default display mode when using B&lt;ShowTable&gt;.  The
environment variable, C&lt;$ENV{'SHOW_MODE'}&gt;, is used when this variable is
null or the empty string.  The possible values for this variable are:
C&lt;"Box"&gt;, C&lt;"List"&gt;, C&lt;"Table"&gt;, and C&lt;"HTML"&gt;.  Case is insignificant.

=item B&lt;$List_Wrap_Margin&gt; [2]

This variable's value determines how large a margin to keep before wrarpping a
long value's display in a column.  This value is only used in "List" mode.

=item B&lt;$Max_List_Width&gt; [80]

This variable, used in "List" mode, is used to determine how long an output line
may be before wrapping it.  The environment variable, C&lt;$ENV{'COLUMNS'}&gt;, is
used to define this value when it is null.

=item B&lt;$Max_Table_Width&gt; ['']

This variable, when set, causes all tables to have their columns scaled
such that their total combined width does not exceed this value.  When
this variable is not set, which is the default case, there is no maximum
table width, and no scaling will be done.

=item B&lt;$No_Escape&gt; ['']

If set, allows embedded HTML text to be included in the data displayed
in an HTML-formatted table.  By default, the HTML formatting characters
("&lt;", "&gt;", and "&amp;") occuring in values are escaped.

=item B&lt;%URL_Keys&gt;

In HTML mode, this variable is used to recognize which columns are to be 
displayed with a corresponding hypertext anchor.  See L&lt;"ShowHTMLTable"&gt; 
for more details.

=item B&lt;@HTML_Elements&gt;

An array of HTML elements (as of HTML 3.0) used to recognize and strip for 
width calculations.

=item B&lt;$HTML_Elements&gt;

A regular expression string formed from the elements of B&lt;@HTML_Elements&gt;.

=back

=cut

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

=head1 INTERNAL SUBROUTINES

=head1 get_params

S&lt;  &gt;my I&lt;$args&gt; = B&lt;&amp;get_params&gt; I&lt;\@argv&gt;, I&lt;\%params&gt;, I&lt;\@arglist&gt;;

Given the I&lt;@argv&gt; originally passed to the calling sub, and the hash of
named parameters as I&lt;%params&gt;, and the array of parameter names in the
order expected for a pass-by-value invocation, set the values of each of
the variables named in I&lt;@vars&gt;.  

If the only element of the I&lt;@argv&gt; is a hash array, then set the
variables to the values of their corresponding parameters used as keys
to the hash array.  If the parameter is not a key of the I&lt;%params&gt;
hash, and is not a key in the global hash B&lt;%ShowTableParams&gt;, then an
error is noted.

When I&lt;@argv&gt; has multiple elements, or is not a hash array, set each
variable, in the order given within I&lt;@arglist&gt;, to the values from the
I&lt;@argv&gt;, setting the variables named by each value in I&lt;%params&gt;.

Variables may given either by name or by reference.

The result is a HASH array reference, either corresponding directly to
the HASH array passed as the single argument, or one created by
associating the resulting variable values to the parameter names
associated with the variable names.

=cut

sub get_params {
    my $argvref = shift or croak "Missing required argument.\n";
    my $params  = shift or croak "Missing required parameters hash.\n";
    my $arglist = shift or croak "Missing required arglist array.\n";
    my %args;
    my ($param, $var);
    if ($#$argvref == 0 &amp;&amp; ref($argvref-&gt;[0]) eq 'HASH') {
	my $href = $argvref-&gt;[0];
	%args = %$href;			# initialize result with input hash
	foreach $param (keys %$href) {	# for each named argument...
	    # Is this a known parameter?
	    if (exists($params-&gt;{$param})) {
		$var = $params-&gt;{$param};
		while ($var ne '' &amp;&amp; ref($var) eq '') {	# indirect refs?
		    $var = $params-&gt;{$param = $var};
		}
		if ($var ne '') {
		    $$var = $href-&gt;{$param}; # assign the param's variable
		    $args{$param} = $$var;	# make sure canonical param gets defined
		    next;		# go to the next parameter
		}
	    }
	    if (!exists($show_table_params{$param})) {
		croak "Unknown parameter: \"$param\"\n";
	    }
	}
    } else {			# use args in the order given for variables
	my $i;
	for ($i = 0; $i &lt;= $#$arglist; $i++) {
	    $param = $arglist-&gt;[$i];	# get the next argument
	    $var = $params-&gt;{$param};	# get it's variable
	    next unless defined($var);
	    while ($var ne '' &amp;&amp; ref($var) eq '') {
		$var = $params-&gt;{$param = $var};
	    }
	    if ($var ne '') {
		$$var = $i &lt;= $#$argvref ? $argvref-&gt;[$i] : '';
		$args{$param} = $$var;	# assign to the hash
	    } elsif (!exists($show_table_params{$param})) {
		croak "Unknown parameter: \"$param\" for argument $i.\n";
	    }
	}
    }
    # Now, make sure all variables get initialized
    foreach $param (keys %$params) {
	$var = $params-&gt;{$param};
	while ($var ne '' &amp;&amp; ref($var) eq '') {
	    $var = $params-&gt;{$param = $var};
	}
	if ($var ne '' &amp;&amp; !exists($args{$param})) {
	    $$var = $args{$param} = undef;
	}
    }
    \%args;			# return the HASH ref
}

BEGIN {

# A table of parameters used by all the external subroutines For
# example, in order for parameters applicable to ShowHTMLTable to be
# passed through ShowTable, they need to be defined in this table.

@show_table_params = qw(
	caption
	col_attributes
	col_lengths
	col_names
	col_types
	data
	data_formats
	dformats
	fmt_sub
	fmtsub
	max_width
	no_escape
	row_sub
	show_mode
	table_attrs
	tformats
	title_formats
	titles
	types
	url_keys
	widths
	wrap_margin
    );
@show_table_params{@show_table_params} = () x (1 + $#show_table_params);
undef @show_table_params;

}

=head1 html_formats

S&lt;  &gt;(I&lt;$prefixes&gt;,I&lt;$suffixes&gt;) = B&lt;html_formats&gt; I&lt;\@html_formats&gt;;

The B&lt;html_format&gt; function takes an array reference of HTML formatting
elements I&lt;\@html_formats&gt;, and builds two arrays of strings: the first:
I&lt;$prefixes&gt;, is an array of prefixes containing the corresponding HTML
formatting elements from I&lt;\@html_formats&gt;, and the second,
I&lt;$suffixes&gt;, containing the appropriate HTML closing elements, in the
opposite order.

The result is designed to be used as prefixes and suffixes for the
corresponding titles and column values.

The array I&lt;\@html_formats&gt; contains lists of HTML formatting elements,
one for each column (either title or data).  Each array element is a
list of one or more HTML elements, either given in HTML syntax, or as a
"plain" name (ie: given as C&lt;\&lt;ELEMENT\&gt;&gt; or plainly, C&lt;ELEMENT&gt;).
Multiple elements are separated by a comma C&lt;','&gt;.

The resulting array of I&lt;$prefixes&gt; contains the corresponding opening
elements, in the order given, with the proper HTML element syntax.  The
resulting array of I&lt;$suffixes&gt; contains the closing elements, in the
opposite order given, with the proper HTML element syntax.

For example, if I&lt;\@html_formats&gt; contains the two elements:

    [ 'FONT SIZE=+2,BOLD', 'FONT COLOR=red,EM' ]

then the resulting two arrays will be returned as:

    [ [ '&lt;FONT SIZE=+2&gt;&lt;BOLD&gt;', '&lt;FONT COLOR=red&gt;&lt;EM&gt;' ],
      [ '&lt;/FONT&gt;&lt;/BOLD&gt;',	'&lt;/FONT&gt;&lt;/EM&gt;' ] ]

=cut

sub html_formats {
    my $html_formats = shift;		# array ref
    my $i;
    my (@prefixes, @suffixes);
    my ($html, $elt, $html_list, @html_list);
    my ($prefixes, $suffixes);
    local($_);

    foreach $html_list (@$html_formats) {
	@html_list = split(/,/,$html_list);
	$prefixes = $suffixes = '';	# initialize the list
	my %formats;			# keep track of formats
	foreach (@html_list) {
	    ($html, $elt) = ();
	    if (($html, $elt) = /^(&lt;)?\s*(\w+)/) {# &lt;KEYWORD or KEYWORD
		next if $formats{$elt}++ &gt; 0;	# only do an element once
		$html = '&lt;' unless $html;
		$prefixes .= $html.$elt.$';
		$prefixes .= '&gt;' unless $prefixes =~ /&gt;$/;
		$suffixes = $html.'/'.$elt.'&gt;'.$suffixes;
	    }
	}
	push(@prefixes, $prefixes);	# even push empty items
	push(@suffixes, $suffixes);
    }
    ( \@prefixes, \@suffixes );
}


=head1 calc_widths

S&lt;  &gt;(I&lt;$num_cols&gt;, I&lt;$widths&gt;, I&lt;$precision&gt;, I&lt;$max_widths&gt;) =
S&lt;  &gt;B&lt;&amp;calc_widths&gt;( I&lt;$widthspec&gt;, I&lt;$titles&gt;, I&lt;$rewindable&gt;,
S&lt;      &gt;I&lt;$row_sub&gt;, I&lt;$fmt_sub&gt;, I&lt;$types&gt;, I&lt;$showmode&gt;, 
S&lt;      &gt;I&lt;$max_width&gt;);

=head2 B&lt;DESCRIPTION&gt;

B&lt;calc_widths&gt; is a generalized subroutine used by all the B&lt;ShowTable&gt;
variant subroutines to setup internal variables prior to formatting for
display.  B&lt;Calc_widths&gt; handles the column width and precision
analysis, including scanning the data (if rewindable) for appropriate
default values.

The number of columns in the data is returned, as well as three arrays:
the declared column widths, the column precision values, and the maximum
column widths.

=head2 B&lt;RETURN VALUES&gt;

=over 10

=item I&lt;$num_cols&gt;

is the number of columns in the data.  If the data is not rewindable,
this is computed as the maximum of the number of elements in the
I&lt;$widthspec&gt; array and the number of elements in the I&lt;$titles&gt;
array.  When the data is rewindable, this is the maximum of the number
of columns of each row of data.

=item I&lt;$widths&gt;

is the column widths array ref, without the precision specs (if any).
Each column's width value is determined by the original I&lt;$widthspec&gt;
value and/or the maximum length of the formatted data for the column.

=item I&lt;$precision&gt;

is the precision component (if any) of the original I&lt;$widthspec&gt;
array ref.  If there was no original precision component from the I&lt;$widthspec&gt;,
and the data is rewindable, then the data is examined to determine the
maximum default precision.

=item I&lt;$max_widths&gt;

is the ref to the array of maximum widths for the given columns.

=head2 B&lt;ARGUMENTS&gt;

=item I&lt;$widthspec&gt;

A reference to an array of column width (or length) values, each given
as an integer, real number, or a string value of
"I&lt;width&gt;.I&lt;precision&gt;".  If a value is zero or null, the length of the
corresponding formatted data (if rewindable) and column title length are
used to determine a reasonable default.

If a column's I&lt;width&gt; portion is a positive, non-zero number, then the
column will be this wide, regardless of the values lengths of the data
in the column.

If the column's I&lt;width&gt; portion is given as a negative number, then the
positive value is used as a minimum column width, with no limit on the
maximum column width.  In other words, the column will be at least
I&lt;width&gt; characters wide.

If the data is not rewindable, and a column's width value is null or
zero, then the length of the column title is used.  This may cause severe
wrapping of data in the column, if the column data lengths are much
greater than the column title widths.

=item I&lt;$titles&gt;

The array ref to the column titles; used to determine the minimum
acceptable width, as well as the default number of columns.  If the
C&lt;$titles&gt; array is empty, then the C&lt;$widthspec&gt; array is used to
determine the default number of columns.

=item I&lt;$rewindable&gt;

A flag indicating whether or not the data being formatted is rewindable.
If this is true, a pass over the data will be done in order to calculate
the maximum lengths of the actual formatted data, using I&lt;$fmt_sub&gt;
(below), rather than just rely on the declared column lengths.  This
allows for optimal column width adjustments (ie: the actual column
widths may be less than the declared column widths).

If it is not desired to have the column widths dynamically adjusted,
then set the I&lt;$rewindable&gt; argument to 0, even if the data is
rewindable.

=item I&lt;$row_sub&gt;

The code reference to the subroutine which returns the data; invoked
only if I&lt;$rewindable&gt; is non-null.

=item I&lt;$fmt_sub&gt;

The subroutine used to determine the length of the data when formatted;
if this is omitted or null, the length of the data is used by default.
The I&lt;$fmt_sub&gt; is used only when the data is rewindable.

=item I&lt;$types&gt;

An array reference to the types of each of the value columns; used only 
when I&lt;$fmt_sub&gt; is invoked.

=item I&lt;$showmode&gt;

A string indicating the mode of the eventual display; one of four strings:
"C&lt;box&gt;", "C&lt;table&gt;", "C&lt;list&gt;", and "C&lt;html&gt;".  Used to adjust widths
for formatting requirements.

=item I&lt;$max_width&gt;

The maximum width of the table being formatted.  If set, and the total
sum of the individual columns exceeds this value, the column widths are
scaled down uniformly.  If not set (null), no column width scaling is done.

=back

=cut

sub calc_widths {
    my $widthspec	= shift;
    my $titles		= shift;
    my $rewindable	= shift;
    my $row_sub		= shift;
    my $fmt_sub		= shift;
    my $types		= shift;
    my $showmode	= shift;
    my $max_width 	= shift;

    my @precision;			# array of precision values
    my @setprec;			# array of flags to set default precision
    my @widths;				# array of widths
    my @max_widths;			# array of max widths
    my @expandable;			# flag if widths expandable
    my $num_cols;
    my $c;

    if ($#$widthspec &gt;= 0) {
	@precision = @$widthspec;
	foreach (@precision) { s/^.*\.(\d+)/$1/ || ($_ = ''); }

	# The setprec array indicates which columns need a default precision
	@setprec = map { !length } @precision;

	# Get the integer portions
	@widths = map { length($_) ? int : 0 } @$widthspec;

	# Set @expandable if negative widths
	@expandable = map { $_ &lt; 0 } @widths;

	# Convert widths to all positive values
	@widths = map abs, @widths;
	@max_widths = (0) x (1 + $#widths);	# no maximums yet
	$num_cols = 1 + $#widths;
    } else {
	# No widths given
	@expandable = (1) x (1 + $#$titles);
	@precision = ('') x (1 + $#$titles);
	@setprec   = @expandable;
	@max_widths = map length, @$titles;	# initialize maximums to title widths 
	$num_cols = 1 + $#$titles;
    }

    # If the data is rewindable, scan and accumulate *actual* widths for
    # each column, using the title lengths as a minimum.
    if ($rewindable) {
	my @values;
	my @prectype;
	if (ref($types) eq 'ARRAY') {
	    @prectype = map {/float|num(eric|ber)|money|dec|real|precision|double/i } @$types;
	}

	# Scan the values
	while ((@values = &amp;$row_sub(0)), $#values &gt;= $[) {
	    # If the new row is larger than the number of titles, adjust
	    # the info arrays..
	    if ($num_cols &lt; 1 + $#values) {	# new column?
		$num_cols = 1 + $#values;	# new # of columns
		for ($c = $#expandable + 1; $c &lt;= $#values; $c++) {
		    $expandable[$c] = 1;
		    $precision[$c] = '';
		    $setprec[$c] = 1;
		    $max_widths[$c] = 0;
		}
	    }
	    my $len;
	    my $value;
	    for ($c = 0; $c &lt; $num_cols; $c++) {
		# Does this column's precision need setting?
		if ($setprec[$c]) {
		    # Yes, is it a type of value which can use the precision?
		    if ($prectype[$c]) {
			# yes, how much is the current value's default precision?
		    	if ($values[$c] =~ /\.(.*)$/) {
			    $precision[$c] = length($1) if length($1) &gt; $precision[$c];
			}
		    } else {
			# No, this column can't use the precision value -- don't
			# do this check on this column again
			$precision[$c] = $setprec[$c] = 0;
		    }
		}

		# Now, let's get the formatted value so we can guess the best
		# default widths
		$value = 
		    # If a fmt_sub is available, use it to format the value
		    $fmt_sub ? 
			&amp;$fmt_sub($values[$c], $types-&gt;[$c], 0, 0, $precision[$c], $showmode)
			# If no fmt sub, then use Perl stringify
		        : length($showmode eq 'html' ?  # in HTML mode?
				&amp;PlainText($values[$c]) # use plain text
			        : $values[$c]); 	# else, use raw text
		$len = length($value);

		$max_widths[$c] = $len if 
			$c &gt; $#max_widths || $len &gt; $max_widths[$c];
	    }
	}
	# okay -- maximums scanned.  
	# If the maximum table width set, scale the max_widths
	$max_width = 0 unless 
		defined($max_width) &amp;&amp; $max_width ne '';
	if ($max_width &gt; 0) {
	    # Start with the given maximum, but adjust it to account for
	    # the formatting and space characters.
	    my $max_width = $max_width;
	    $max_width -= $num_cols * 3 + 2 if $showmode eq 'box';
	    $max_width -= $num_cols * 2 - 1 if $showmode eq 'table';
	    my $total = 0;
	    # Calculate the total table width
	    for ($c = 0; $c &lt;= $#max_widths; $c++) {
		$total += $max_widths[$c];
	    }
	    if ($max_width &lt; $total) {
		# Now scale it to the adjusted maximum table width
		for ($c = 0; $c &lt;= $#max_widths; $c++) {
		    $max_widths[$c] = int($max_widths[$c] *
					  $max_width / $total); 
		}
	    }
	}
		
	# If the column is expandable, allow the width to grow to the max_width.
	# If the column is not expandable, allow the width to shrink to
	# the max_width if it is smaller.

	if ($#widths &lt; 0) {		# were there any widths?
	    @widths = @max_widths;	# nope, set them to the scanned values
	} else {
	    $num_cols = max($num_cols, 1 + $#widths) if $#widths &gt;= 0;
	    my $len;
	    for ($c = 0; $c &lt; $num_cols; $c++) {
	    	# provide defaults first
		$max_widths[$c] = 0 if !defined($max_widths[$c]);
		$widths[$c] = $max_widths[$c] 
		    if $c &gt; $#widths || !defined($widths[$c]);
		# if the column can shrink, let it
		if ($max_widths[$c] &lt; $widths[$c]) {
		    $widths[$c] = $max_widths[$c];
		} elsif ($expandable[$c] || !$widths[$c]) {
		    # allow the width to grow to the maximum width
		    $widths[$c] = $max_widths[$c] if $widths[$c] &lt; $max_widths[$c];
		} elsif ($max_widths[$c] &gt; $widths[$c] &amp;&amp; $widths[$c] &gt; 0) {
		    # not expandable -- set the max width to the width value
		    $max_widths[$c] = $widths[$c];
		}
		# In either case, however, ensure that the widths are at
		# least as long as the title length
		if ($c &lt;= $#$titles) {
		    if (defined($titles-&gt;[$c])) {
			# If we're in HTML mode, get the length of the plaintext
			$len = length($showmode eq 'html' ? &amp;PlainText($titles-&gt;[$c])
							  # else, use raw text.
							  : $titles-&gt;[$c]);
		    } else { 
			$len = length("Field_$c");
		    }
		    $widths[$c] = $len 
			if $widths[$c] &lt; $len;
		    $max_widths[$c] = $len
			if $max_widths[$c] &lt; $len;
		}
	    }
	}
	&amp;$row_sub(1);			# reset the pointer for the next scan
    } else {
	# Use title width as default if original width is null or zero
	my $len;
	for ($c = 0; $c &lt;= $#widths; $c++) {
	    next unless $c &lt;= $#$titles;
	    # Get the length of the title (sans HTML text if in that mode)
	    $len = length($showmode eq 'html' ? &amp;PlainText($titles-&gt;[$c])
					      : $titles-&gt;[$c]);
	    $widths[$c] = $len if $widths[$c] &lt; $len;
	}
	# Can't scan the data, so the maximums can only be set by using the
	# explicit widths.
	@max_widths = @widths;
    }
    ($num_cols, \@widths, \@precision, \@max_widths);
}

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

=head1 putcell

S&lt;  &gt;I&lt;$wrapped&gt; = B&lt;&amp;putcell&gt;( I&lt;\@cells&gt;, I&lt;$c&gt;, I&lt;$cell_width&gt;, I&lt;\@prefix&gt;, I&lt;\@suffix&gt;, I&lt;$wrap_flag&gt; );

Output the contents of an array cell at I&lt;$cell&gt;[I&lt;$c&gt;], causing text
longer than I&lt;$cell_width&gt; to be saved for output on subsequent calls.
Prefixing the output of each cell's value is a string from the
two-element array I&lt;@prefix&gt;.  Suffixing each cell's value is a string
from the two-element array I&lt;@suffix&gt;.  The first element of either 
array is selected when I&lt;$wrap_flag&gt; is zero or null, or when there is
no more text in the current to be output.  The second element
is selected when I&lt;$wrap_flag&gt; is non-zero, and when there is more text in
the current cell to be output.

In the case of text longer than I&lt;$cell_width&gt;, a non-zero value is
returned. 

Cells with undefined data are not output, nor are the prefix or suffix
strings. 

=cut

sub putcell {
    my $cells      = shift;	# ref to cell array
    my $c          = shift;	# index
    my $cell_width = shift;	# maximum width of the cell
    my $prefix     = shift;	# 2-elt array of prefix strings
    my $suffix     = shift;	# 2-elt array of suffix strings
    my $wrap_flag  = shift;	# non-zero for wrapped lines
    my $fmt        = sprintf("%%s%%-%ds%%s",$cell_width);
    my $more;

    my $v = $cells-&gt;[$c];	# get the data
    my $px = 0;			# prefix index
    my $sx = 0;			# suffix index
    if (defined $v) {		# not undef data?
	my $text = $v;		# save the text
	$cell_width = 1 unless $cell_width &gt; 0;	# be sane
	if ($cell_width &lt;= length($text)) {
	    $more = substr($text,$cell_width);
	    $v = substr($text,0,$cell_width);
	} else {
	    $v = $text; $more = '';
	}

	# wrapping?
	if ($more ne '' &amp;&amp;

	    # See if we can wrap on a word boundary, instead of 
	    # arbitrarily splitting one; note that we try to not 
	    # split grouped numbers (1,345) or reals (1.234).

	    $v =~ /([-,;? \t])([^-,;? \t0-9]*)$/ &amp;&amp; 

	    # but also make sure that it is not too long

	    length($2) &lt;= $List_Wrap_Margin ) 
	{

	    # Okay, cut on the word boundary, leaving the break char
	    # on the tail end of the current output value

	    my $cut = $cell_width - length($2);
	    $v = substr($text,0,$cut);		# get new value
	    $more = substr($text, $cut);	# new remainder
	}
	$cells-&gt;[$c] = $more;	# leave the rest for later
	$px = $wrap_flag != 0 &amp;&amp; length($v) &gt; 0;
	$sx = length($more) &gt; 0;
    }
    put $fmt,$prefix-&gt;[$px],$v,$suffix-&gt;[$sx];	# output something (could be blanks)
    $sx;			# leave wrapped flag
}

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

=head1 center 

Center a string within a given width.

S&lt;  &gt;I&lt;$field&gt; = B&lt;center&gt; I&lt;$string&gt;, I&lt;$width&gt;;

=cut

sub center {
    my($string,$width) = @_;
    $width = 0 if !defined($width);
    return $string if length($string) &gt;= $width;
    my($pad) = int(($width - length($string))/2);	# pad left half
    my($center) = (' ' x $pad) . $string;
    $pad = $width - length($center);
    $center .= ' ' x $pad;	# pad right half
    $center;			# return with the centered string
}

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

=head1 max

Compute the maximum value from a list of values.

S&lt;  &gt;I&lt;$max&gt; = B&lt;&amp;max&gt;( I&lt;@values&gt; );

=cut

sub max {
    my ($max) = shift;
    foreach (@_) { $max = $_ if $max &lt; $_; }
    $max;
}

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

=head1 min

Compute the minum value from a list of values.

S&lt;  &gt;I&lt;$min&gt; = B&lt;&amp;min&gt;( I&lt;@values&gt; );

=cut

sub min {
    my ($min) = shift;
    foreach (@_) { $min = $_ if $min &gt; $_; }
    $min;
}

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

=head1 max_length

Compute the maximum length of a set of strings in an array reference.

S&lt;  &gt;I&lt;$maxlength&gt; = B&lt;&amp;max_length&gt;( I&lt;\@array_ref&gt; );

=cut

sub max_length {
    my($aref) = shift;
    my(@lens) = map { length } @$aref;
    my($maxlen) = max( @lens );
    $maxlen;
}

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

=head1 htmltext

Translate regular text for output into an HTML document.  This means
certain characters, such as "&amp;", "&gt;", and "&lt;" must be escaped. 

S&lt;  &gt;I&lt;$output&gt; = B&lt;&amp;htmltext&gt;( I&lt;$input&gt; [, I&lt;$allflag&gt; ] );

If I&lt;$allflag&gt; is non-zero, then all characters are escaped.  Normally,
only the four HTML syntactic break characters are escaped.

=cut

# htmltext -- translate special text into HTML esacpes
sub htmltext {
    local($_) = shift;
    my $all = shift;
    return undef unless defined($_);
    s/&amp;(?!(?:amp|quot|gt|lt|#\d+);)/&amp;amp;/g; 
    s/\"/&amp;quot;/g;
    s/&gt;/&amp;gt;/g;
    s/&lt;/\&amp;lt;/g;
    if ($all) {
	s/ /\&amp;#32;/g;
	s/\t/\&amp;#09;/g;
    }
    $_;
}

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

=head1 out

Print text followed by a newline.

S&lt;  &gt;B&lt;out&gt; I&lt;$fmt&gt; [, I&lt;@text&gt; ];

=cut

sub out {
    my $fmt = shift;
    $fmt .= "\n" unless $fmt =~ /\n$/;
    printf STDOUT $fmt, @_;
}

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

=head1 put

Print text (without a trailing newline).

S&lt;  &gt;B&lt;out&gt; I&lt;$fmt&gt; [, I&lt;@text&gt; ];

=cut

sub put {
    printf STDOUT @_;
}

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

=head1 AUTHOR

Alan K. Stebbens &lt;aks@sgi.com&gt;

=cut

=head1 BUGS

=over 10

=item *

Embedded HTML is how the user can insert formatting overrides.  However,
the HTML formatting techniques have not been given much consideration --
feel free to provide constructive feedback.

=cut

#
1;
</pre></body></html>