<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/net/pvcsserv01/sft/gnu/bin/perl
#!/usr/local/bin/perl
################################################################################
##                                                                            ##
## Author     :  Monty Scroggins                                              ##
## Description:  Perform database commands with a GUI interface               ##
##                                                                            ##
##  Parameters:  none                                                         ##
##                                                                            ##
## +++++++++++++++++++++++++++ Maintenance Log ++++++++++++++++++++++++++++++ ##
## Tue Sep 1 02:30:59 GMT 1998  Monty Scroggins - Script created.  
##    verified to work on RedHat Linux 5.1 and Solaris 2.6        
##
## Mon Oct 9 16:00:54 GMT 1998   Added code to perform db commands other than
##    select statements.  Changed listbox to text widget to accomodate 
##
## Sun Nov 1 18:39:51 GMT 1998  Many Enhancements - Snapshots, Print, Datatypes,
##    Enable/Disable Command Strings, remove/replace table header on queries,
##    Save file requester.   Many bug fixes
##
## Fri Nov 13 00:15:38 GMT 1998 More bug fixes
##
## Mon Jan 25 22:49:59 GMT 1999 Changed filerequester to new getFileName()
##    Also added Optionset calls to set default colors - trimmed 10% :-)
##
## Mon Feb 1 23:21:16 GMT 1999  Added commandtype assoc array to force row/column style
##    for certain "sp_" commands
##
## Sun Feb 21 11:48:14 CST 1999 Added Menubutton "Method" to force any command to be
##    executed with the system isql command even though the output is really ugly  ;-)
##
## Wed Mar 31 19:00:52 CST 1999 Re-wrote handling of multiple result sets.  Now multiple
##    result sets are detected and the header/sort functions are disabled.  Also
##    added Sqsh executable option for comparison
##
## Tue Apr 20 12:02:51 CDT 1999 Various cleaning up....  fixed a bug where the scroll
##    positions were not being restored after a DB command.
##
## Thu Apr 28 19:30:29 CDT 1999 Added clone window....  added functionality to allow
##    saving of the cloned data to a file. Added a datestring to the titlebars to indicate
##    the date/time of the sampled data
##
## Fri May 14 15:15:47 CDT 1999 Fixed problem with search dialog losing the histories
##
## Tue May 18 13:55:06 CDT 1999 Fixed problem where I wasnt displaying the errors returned
##     if an invalid command (for example) was executed on the server..  (no result sets returned)
##
## Tue May 25 17:47:55 CDT 1999  Moved the row/column counters to the top.  Makes for a smaller
##     and more efficient display.
## 
## Wed Jun 9 12:11:05 CDT 1999  Added the small change to display pound symbols instead of the
##     actual text for the password entry.  Changed the busy indicator to a simple colored frame
##     for simplicity.
##
## Wed Jun 16 17:54:50 CDT 1999  Made some minor - mostly cosmetic changes.  Fixed a bug where
##     the sql command was being printed twice in a save file.  
##
## Tue Jun 22 17:14:03 CDT 1999  Added the checkpoint file as an argument to allow pre-defined
##     menu histories to be defined and kept in separate database files.
##
## Fri Jul 9 14:05:10 CDT 1999  Fixed a bug in the handling of sql strings which include
##     special characters
################################################################################

#the current version
local $VERSION="1.6.7";

=head1 NAME

DBGUI - a Sybase database server graphical interface. 

=head1 DESCRIPTION

DBGUI features:

=over 4

Perform any SQL command.

Save the SQL results to a file.

Perform incremental or standard searches or the SQL results.

Keep a history of _all_ SQL commands and parameters.

Sort (normal, numerical and reverse) on any column of the SQL results.

Print the SQL results to a printer.

Quick command line clear and restore for easy command line generation/pasting.

"Clone" the results to a new display window for comparisons etc.

Utilize the SYBASE libraries or isql/sqsh binaries for the queries.

Maintain four complete configuration "snapshots" for easy retrieval.

Reload the last set of parameters on startup.

Interactively enable and disable any or all of three different command lines for execution.  All of
the 'active' command lines are concatenated, therefore the three command entry lines can be used to
quickly eliminate/add command parameters to an existing command.

Display the column data type in each column header.

Display the column data width in each column header.

Solicit and quickly popup a list of the system datatypes. 

Colored busy indicator (red/green) to indicate if the DBGUI is waiting on results from the DB server.

The date/time of command execution is captured in the title bar.

The checkpoint file can be specified as an argument. Allowing pre-defined menu histories to be defined

More probably....  :-)

=back

=head1 PREREQUISITES

This script requires the following Perl Modules:

=over 4

C&lt;Tk Toolkit&gt;

C&lt;Sybperl&gt;

C&lt;Sort::Fields&gt;

C&lt;Tk::HistEntry&gt;

C&lt;Tk::PrintDialog&gt;

=back


=pod SCRIPT CATEGORIES

CPAN/Administrative
Fun/Educational

=cut

use Tk;
#entry widget with history menu
use Tk::HistEntry;
#db query library
use Sybase::DBlib;
#sort library 
use Sort::Fields;
use Tk::PrintDialog;
use Text::Wrap;

#perl variables
$|=1; # set output buffering to off
$[ = 0; # set array base to 0
$, = ' '; # set output field separator
$\ = "\n"; # set output record separator

#The colors and such
local $txtbackground="snow2";
local $txtforeground="black";
local $background="bisque3";
local $troughbackground="bisque4";
local $buttonbackground="tan";
local $headerbackground='#f0f0c7';
local $headerforeground="red4";
local $winfont=$listboxfont="8x13bold";
local $trbgd="bisque4";
local $labelbackground='bisque2';
local $rowcolcolor='#002030';
local $entrywidth=11;
local $toplabelwidth=12;
local $tophistentrywidth=9;
local $buttonwidth=4;
local $ypad=4;
local $busy="Ready";
local $busycolor="red2",
local $unbusycolor='#009f00',
local $savedialogcolor="#caC2BBBBA7A7";
local $histlimit=100;

#the path to the isql binary
local $isqlbinary="/net/pvcsserv01/sft/sybase/bin/isql";

#the path to the sqsh binary
local $sqshbinary="/net/pvcsserv01/usr/tools/sqsh";

#the sql command used to extract the defined datatypes from the database
local $dbtypescmd="select type,length,name from systypes";

#available printers
local @printers=("hp4si-678-1158","Print-to-File");

#set to the postscript printing program.  We use a2ps
local $psprint="/net/pvcsserv01/sft/gnu/opt/a2ps/bin/a2ps";

#a list of variables that are saved in the checkpoint file
local @variablelist=qw(
dbserver  
dbuser
dbpass
dbuse
maxrowcount
querystring1
querystring2
querystring3
qsactive1
qsactive2
qsactive3
snapshot0
snapshot1
snapshot2
snapshot3
snapshot4
srchstring
method
);

#a list of arrays that are saved in the checkpoint file (minus the temp)
local @arraylist=qw(
dbservhisttemp
dbuserhisttemp
dbpasshisttemp
dbusehisttemp 
queryhist1temp
queryhist2temp
queryhist3temp
searchhisttemp
);

#check the arguments.  If one is given it has to be checkpoint file to load.  If none
#is given, use the home directory
$checkf="$ARGV[0]";
$checkpointfile="$ENV{HOME}/.dbgui";

#if the checkpoint file is not found and a real value has been passed as ARGV0
if (!-f $checkf &amp;&amp; $checkf) {
   $checkpointfile=$checkf;
   }

#if the checkpoint file is found in the home directory, use it
if (-f "$ENV{HOME}\/$checkf") {
   $checkpointfile="$ENV{HOME}/$checkf";
   }

#if the checkpoint file contains a full path, use it
if (-f "$checkf") {
   $checkpointfile="$checkf";
   }

#if it just doesnt exist, prompt to create it
if (!-f $checkpointfile) {
   $confirmwin-&gt;destroy if Exists($confirmwin);
   $confirmwin = Tk::MainWindow-&gt;new;
   $confirmwin-&gt;withdraw;
   local $confirmbox=$confirmwin-&gt;messageBox(
      -type=&gt;'OKCancel',
      -bg=&gt;$background,
      -font=&gt;'8x13bold',
      -title=&gt;"Prompt",
      -text=&gt;"The Checkpoint file \"$checkpointfile\" does not exist...\nCreate it??",
      );
   if ($confirmbox ne "Ok") {
       exit;
      }#
   }#if ! -f checkf

#if the checkpoint file exists, execute it to startup in the same state as when it was shutdown
if (-e $checkpointfile) {
  require ("$checkpointfile");
  }

#startup with the last data in the widgets
local $snapshot=0;

#force specific column widths for these columns
#If the column title string is longer than this value, the length of the column title
#string is used instead. (like it is normally) 
local %specialcols=(
"keys1"=&gt;"260",
"keys2"=&gt;"260",
"versionts"=&gt;"24",
"xactid"=&gt;"12",
"password"=&gt;"60",
);

#set specific column widths for the columns that have expandable data. ex - 
#date and time values are expanded into long english type strings by the server even
#though the length of the column might only be 4 bytes.
#56=integer 61=datetime 52=smallint 37=timestamp
local %specialcollengths=(
"56"=&gt;"10",
"61"=&gt;"26",
"62"=&gt;"20",
"37"=&gt;"26",
);

#a list of potentially dangerous commands that should be operator confirmed before being executed
local @dangercmds=qw(
update
truncate
delete
drop
shutdown
);

#reset the data counters
local $dbrowcount=0;
local $dbcolcount=0;

#set the initial maxrowcount to 1000 if not defined  a nice number
if (!$maxrowcount) {
   local $maxrowcount=1000;
   };

#                                       Main Window     
#------------------------------------------------------------------------------------------
#
$LW = new  MainWindow (-title=&gt;"DBGUI $VERSION  [$checkpointfile]");
#set some inherited default colors
$LW-&gt;optionAdd("*background","$background"); 
$LW-&gt;optionAdd("*foreground","$txtforeground"); 
$LW-&gt;optionAdd("*highlightBackground", "$savedialogcolor");
$LW-&gt;optionAdd("*Button.Background", "$buttonbackground");
$LW-&gt;optionAdd("*activeForeground", "$txtforeground");
$LW-&gt;optionAdd("*Menubutton*Background", "$buttonbackground");
$LW-&gt;optionAdd("*Menubutton*activeForeground", "$txtforeground");
$LW-&gt;optionAdd("*Label*Background", "$labelbackground");
$LW-&gt;optionAdd("*troughColor", "$troughbackground");
$LW-&gt;optionAdd("*borderWidth", "1");
$LW-&gt;optionAdd("*highlightThickness", "0");
$LW-&gt;optionAdd("*font", "8x13bold");
$LW-&gt;optionAdd("*label*relief", "flat");
$LW-&gt;optionAdd("*frame*relief", "flat");
$LW-&gt;optionAdd("*button*relief", "raised");
$LW-&gt;optionAdd("*Checkbutton*relief", "raised");
$LW-&gt;optionAdd("*optionmenu*relief", "raised");

#set an initial size
$LW-&gt;minsize(85,2);
$LW-&gt;geometry("85x3");

#label frame 
$listframeall=$LW-&gt;Frame(
   -borderwidth=&gt;'1',
   -relief=&gt;'sunken',
   )-&gt;pack(
      -fill=&gt;'both',
      -expand=&gt;1,
      -pady=&gt;0,
      -padx=&gt;0,
      -side=&gt;'top',
      );

$buttonframe=$LW-&gt;Frame(
   )-&gt;pack(
      -fill=&gt;'x',
      -pady=&gt;0,
      -padx=&gt;0,
      -side=&gt;'bottom',
      );

#label frame for all of the labels and histentrys at the top
$labelframe=$listframeall-&gt;Frame(
   -borderwidth=&gt;'0',
   -relief=&gt;'raised',
   )-&gt;pack(
      -fill=&gt;'x',
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;0,
      -side=&gt;'top',
      );
    
$labelent1=$labelframe-&gt;Frame(
   -relief=&gt;'raised',
   -background=&gt;$labelbackground,
   )-&gt;pack(
      -fill=&gt;'y',
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;0,
      -side=&gt;'left',
      );

$labelent2=$labelframe-&gt;Frame(
   #-borderwidth=&gt;'1',
   -relief=&gt;'raised',
   -background=&gt;$labelbackground,
   )-&gt;pack(
      -fill=&gt;'y',
      -expand=&gt;0,    
      -pady=&gt;0,
      -padx=&gt;0,
      -side=&gt;'left',
      );
    
$labelent3=$labelframe-&gt;Frame(
   -relief=&gt;'raised',
   -background=&gt;$labelbackground,
   )-&gt;pack(
      -fill=&gt;'y',
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;0,
      -side=&gt;'left',
      );
    
$labelent4=$labelframe-&gt;Frame(
   -relief=&gt;'raised',
   -background=&gt;$labelbackground,
   )-&gt;pack(
      -fill=&gt;'y',
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;0,
      -side=&gt;'left',
      );
    
$labelent5=$labelframe-&gt;Frame(
   -relief=&gt;'raised',
   -background=&gt;$labelbackground,
   )-&gt;pack(
      -fill=&gt;'y',
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;0,
      -side=&gt;'left',
      );    

$labelent6=$labelframe-&gt;Frame(
   -relief=&gt;'raised',
   -background=&gt;$labelbackground,
   )-&gt;pack(
      -fill=&gt;'y',
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;0,
      -side=&gt;'left',
      );

$labelent7=$labelframe-&gt;Frame(
   -relief=&gt;'raised',
   -background=&gt;$labelbackground,
   )-&gt;pack(
      -fill=&gt;'y',
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;0,
      -side=&gt;'left',
      );
      
$busymarker=$labelframe-&gt;Frame(
   -relief=&gt;'raised',
   -width=&gt;16,
   -height=&gt;16,
   -background=&gt;$unbusycolor,
   )-&gt;pack(
      -expand=&gt;0,    
      -pady=&gt;2,
      -padx=&gt;2,
      -side=&gt;'right',
      );    
      
$labelent8=$labelframe-&gt;Frame(
   -relief=&gt;'raised',
   -background=&gt;$labelbackground,
   )-&gt;pack(
      -fill=&gt;'y',
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;0,
      -side=&gt;'left',
      );
        
$listframe1=$listframeall-&gt;Frame(
   -borderwidth=&gt;'0',
   -relief=&gt;'sunken',
   )-&gt;pack(
      -fill=&gt;'both',
      -expand=&gt;1,    
      -pady=&gt;0,
      -padx=&gt;0,
      );
    
#the labels frame across the top
##############################################    
#                              query strings
$labelent1-&gt;Label(
   -text=&gt;'DB Server',
   -width=&gt;$toplabelwidth,
   )-&gt;pack(
      -fill=&gt;'x', 
      -side=&gt;'top',
      -padx=&gt;0,
      -pady=&gt;0,
      -expand=&gt;0,
      );

$servhistframe=$labelent1-&gt;Frame(
   -relief=&gt;'sunken',
   )-&gt;pack(
      -side=&gt;'bottom',
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;1,
      );
     
$dbserventry=$servhistframe-&gt;HistEntry(
   -relief=&gt;'flat',
   -textvariable=&gt;\$dbserver,
   -selectforeground=&gt;$txtforeground,
   -selectbackground=&gt;'#c0d0c0',
   -background=&gt; 'white',
   -width=&gt;$tophistentrywidth,
   -limit=&gt;$histlimit,
   -dup=&gt;0,
   -match=&gt;0,  
   -command=&gt;sub{@dbservhisttemp=$dbserventry-&gt;history;},
   -justify=&gt;'center',
   )-&gt;pack(
      -side=&gt;'left',
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;0,
      -fill=&gt;'x',
      );

$dbserventry-&gt;bind('&lt;Return&gt;'    =&gt; \&amp;check_cmd); 
$dbserventry-&gt;bind('&lt;Up&gt;'        =&gt; sub { $dbserventry-&gt;historyUp });
$dbserventry-&gt;bind('&lt;Control-p&gt;' =&gt; sub { $dbserventry-&gt;historyUp });
$dbserventry-&gt;bind('&lt;Down&gt;'      =&gt; sub { $dbserventry-&gt;historyDown });
$dbserventry-&gt;bind('&lt;Control-n&gt;' =&gt; sub { $dbserventry-&gt;historyDown });
$dbserventry-&gt;history([@dbservhist]);

$labelent2-&gt;Label(
   -text=&gt;'Username',
   -width=&gt;$toplabelwidth,
   )-&gt;pack(
      -fill=&gt;'y', 
      -padx=&gt;0,
      -pady=&gt;0,
      -expand=&gt;0,
      -side=&gt;'top',
      );

$userhistframe=$labelent2-&gt;Frame(
   -relief=&gt;'sunken',
   )-&gt;pack(
      -side=&gt;'bottom',
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;1,
      );
     
$dbuserentry=$userhistframe-&gt;HistEntry(
   -relief=&gt;'flat',
   -textvariable=&gt;\$dbuser,
   -highlightcolor=&gt;'black',
   -selectforeground=&gt;$txtforeground,
   -selectbackground=&gt;'#c0d0c0',
   -borderwidth=&gt;0,
   -background=&gt; 'white',
   -width=&gt;$tophistentrywidth,
   -limit=&gt;$histlimit,  
   -dup=&gt;0,
   -match=&gt;0,  
   -justify=&gt;'center',
   -command=&gt;sub{@dbuserhisttemp=$dbuserentry-&gt;history;},
   )-&gt;pack(  
      -expand=&gt;0,
      -padx=&gt;0,
      -pady=&gt;0,
      -fill=&gt;'x',
      );

$dbuserentry-&gt;bind('&lt;Return&gt;'    =&gt; \&amp;check_cmd); 
$dbuserentry-&gt;bind('&lt;Up&gt;'        =&gt; sub { $dbuserentry-&gt;historyUp });
$dbuserentry-&gt;bind('&lt;Control-p&gt;' =&gt; sub { $dbuserentry-&gt;historyUp });
$dbuserentry-&gt;bind('&lt;Down&gt;'      =&gt; sub { $dbuserentry-&gt;historyDown });
$dbuserentry-&gt;bind('&lt;Control-n&gt;' =&gt; sub { $dbuserentry-&gt;historyDown });
$dbuserentry-&gt;history([@dbuserhist]);

$labelent3-&gt;Label(
   -text=&gt;'Password',
   -width=&gt;$toplabelwidth,
   )-&gt;pack(
      -fill=&gt;'y', 
      -side=&gt;'top',
      -padx=&gt;0,
      -pady=&gt;0,
      -expand=&gt;0,
      );

$passhistframe=$labelent3-&gt;Frame(
   -relief=&gt;'sunken',
   )-&gt;pack(
      -side=&gt;'bottom',
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;1,         
      );
     

$dbpassentry=$passhistframe-&gt;HistEntry(
   -relief=&gt;'flat',
   -textvariable=&gt;\$dbpass,
   -highlightcolor=&gt;'black',
   -selectforeground=&gt;$txtforeground,
   -selectbackground=&gt;'#c0d0c0',
   -borderwidth=&gt;0,
   -background=&gt; 'white',
   -width=&gt;$tophistentrywidth,
   -limit=&gt;$histlimit,  
   -dup=&gt;0,
   -match=&gt;0,  
   -justify=&gt;'center',
   -show =&gt; '#',
   -command=&gt;sub{@dbpasshisttemp=$dbpassentry-&gt;history;},
   )-&gt;pack(
      -expand=&gt;0,
      -padx=&gt;0,
      -pady=&gt;0,        
      -fill=&gt;'x',
      );
    
$dbpassentry-&gt;bind('&lt;Return&gt;'    =&gt; \&amp;check_cmd); 
$dbpassentry-&gt;bind('&lt;Up&gt;'        =&gt; sub { $dbpassentry-&gt;historyUp });
$dbpassentry-&gt;bind('&lt;Control-p&gt;' =&gt; sub { $dbpassentry-&gt;historyUp });
$dbpassentry-&gt;bind('&lt;Down&gt;'      =&gt; sub { $dbpassentry-&gt;historyDown });
$dbpassentry-&gt;bind('&lt;Control-n&gt;' =&gt; sub { $dbpassentry-&gt;historyDown });
$dbpassentry-&gt;history([@dbpasshist]);


$labelent4-&gt;Label(
   -text=&gt;'Use DB',
   -width=&gt;$toplabelwidth,
   )-&gt;pack(
      -fill=&gt;'y', 
      -side=&gt;'top',
      -padx=&gt;0,
      -pady=&gt;0,
      -expand=&gt;0,
      );

$dbusehistframe=$labelent4-&gt;Frame(
   -relief=&gt;'sunken',
   )-&gt;pack(
      -side=&gt;'bottom',
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;1,         
      );
     
$dbuseentry=$dbusehistframe-&gt;HistEntry(
   -relief=&gt;'flat',
   -textvariable=&gt;\$dbuse,
   -highlightcolor=&gt;'black',
   -selectforeground=&gt;$txtforeground,
   -selectbackground=&gt;'#c0d0c0',
   -borderwidth=&gt;0,
   -background=&gt; 'white',
   -width=&gt;$tophistentrywidth,
   -limit=&gt;$histlimit,  
   -justify=&gt;'center',
   -dup=&gt;0,
   -match=&gt;0,  
   -command=&gt;sub{@dbusehisttemp=$dbuseentry-&gt;history;},
   )-&gt;pack(
      -expand=&gt;0,
      -padx=&gt;0,
      -pady=&gt;0, 
      -fill=&gt;'x',
      );
      
$dbuseentry-&gt;bind('&lt;Return&gt;'    =&gt; \&amp;check_cmd); 
$dbuseentry-&gt;bind('&lt;Up&gt;'        =&gt; sub { $dbuseentry-&gt;historyUp });
$dbuseentry-&gt;bind('&lt;Control-p&gt;' =&gt; sub { $dbuseentry-&gt;historyUp });
$dbuseentry-&gt;bind('&lt;Down&gt;'      =&gt; sub { $dbuseentry-&gt;historyDown });
$dbuseentry-&gt;bind('&lt;Control-n&gt;' =&gt; sub { $dbuseentry-&gt;historyDown });
$dbuseentry-&gt;history([@dbusehist]);

$labelent5-&gt;Label(
   -text=&gt;'Max Rows',
   -width=&gt;$toplabelwidth, 
   )-&gt;pack(
      -fill=&gt;'y',  
      -side=&gt;'top',
      -padx=&gt;0,
      -pady=&gt;0,
      );
   
$maxrowentry=$labelent5-&gt;Entry(
   -textvariable=&gt;\$maxrowcount,
   -width=&gt;$toplabelwidth,
   -background=&gt;'white',
   -selectforeground=&gt;$txtforeground,
   -selectbackground=&gt;'#c0d0c0',  
   -relief=&gt;'sunken',
   -justify=&gt;'center',
   )-&gt;pack(
      -side=&gt;'bottom',
      -expand=&gt;1,
      -padx=&gt;1,
      -pady=&gt;0,
      -fill=&gt;'y',
      );
    
$maxrowentry-&gt;bind('&lt;Return&gt;'=&gt;\&amp;check_cmd); 

$labelent6-&gt;Label(
   -text=&gt;'Snapshot',
   -width=&gt;$toplabelwidth, 
   )-&gt;pack(
      -fill=&gt;'y',  
      -side=&gt;'top',
      -padx=&gt;0,
      -pady=&gt;0,
      );

$labelent6-&gt;Label(
   -textvariable=&gt;\$snapshot,
   -width=&gt;1,
   -background=&gt;$txtbackground,
   -relief=&gt;'sunken',
   )-&gt;pack(
      -side=&gt;'right',
      -expand=&gt;1,
      -padx=&gt;1,
      -pady=&gt;1,
      -fill=&gt;'y',
      );

$snapscale=$labelent6-&gt;Scale(
   -variable=&gt;\$snapshot,
   -orient=&gt;'horizontal',
   -label=&gt;'',
   -from=&gt;0,
   -to=&gt;4,
   -length=&gt;86,    
   -troughcolor=&gt;$txtbackground,
   -sliderlength=&gt;12,
   -width=&gt;19,
   -showvalue=&gt;0,
   -command=&gt;sub{&amp;run_snapshot("$snapshot");},
   )-&gt;pack(
      -side=&gt;'bottom',
      -padx=&gt;0,
      -pady=&gt;0,
      -fill=&gt;'y', 
      );
$snapscale-&gt;bind('&lt;Return&gt;'=&gt; \&amp;check_cmd); 

$labelent7-&gt;Label(
   -text=&gt;'Method',
   -width=&gt;$toplabelwidth,
   )-&gt;pack(
      -fill=&gt;'y', 
      -side=&gt;'top',
      -padx=&gt;0,
      -expand=&gt;0,
      );

$methodframe=$labelent7-&gt;Frame(
   -relief=&gt;'sunken',
   -height=&gt;20,
   -borderwidth=&gt;1,
   )-&gt;pack(
      -side=&gt;'bottom',
      -expand=&gt;1,
      -pady=&gt;0,
      -padx=&gt;0,
      -fill=&gt;'x',
      );

$methodmenu=$methodframe-&gt;Menubutton(
   -textvariable=&gt;\$method,
   -relief=&gt;'sunken',
   -indicatoron=&gt;1,
   -borderwidth=&gt;0,
   -background=&gt;$txtbackground,
   )-&gt;pack(
      -side=&gt;'bottom',
      -padx=&gt;0,
      -pady=&gt;0,
      -fill=&gt;'x',
      );

$methodmenu-&gt;command(
   -label=&gt;'Sybase Libraries',
   -command=&gt;sub{$method="Sybase";},
   -background=&gt;$background,
   );

$methodmenu-&gt;command(
   -label=&gt;'Isql Command',
   -command=&gt;sub{$method="Isql";},
   -background=&gt;$background,
   );
   
$methodmenu-&gt;command(
   -label=&gt;'Sqsh Command',
   -command=&gt;sub{$method="Sqsh";},
   -background=&gt;$background,
   );

$rowdata=$labelent8-&gt;Frame(
   -relief=&gt;'flat',
   -background=&gt;$labelbackground,
   )-&gt;pack(
      -side=&gt;'top',
      -padx=&gt;0,
      -pady=&gt;0,
      -expand=&gt;1,
      -fill=&gt;'both',
      );

$coldata=$labelent8-&gt;Frame(
   -relief=&gt;'flat',
   -background=&gt;$labelbackground,
   )-&gt;pack(
      -side=&gt;'bottom',
      -padx=&gt;0,
      -pady=&gt;0,
      -expand=&gt;1,
      -fill=&gt;'both',
      );

$rownumlabel=$rowdata-&gt;Label(
   -text=&gt;' Rows:',
   -foreground=&gt;$rowcolcolor,
   -justify=&gt;'right'
   )-&gt;pack(
      -side=&gt;'left',
      -padx=&gt;0,
      -pady=&gt;0,
      );

$rowdata-&gt;Label(
   -textvariable=&gt;\$dbrowcount,
   -width=&gt;6,
   -background=&gt;$txtbackground,
   -foreground=&gt;$rowcolcolor,
   -relief=&gt;'sunken',
   )-&gt;pack(
      -side=&gt;'right',
      -padx=&gt;0,
      -pady=&gt;0,
      -fill=&gt;'y',
      );

$colnumlabel=$coldata-&gt;Label(
   -text=&gt;' Cols:',
   -foreground=&gt;$rowcolcolor,
   -justify=&gt;'right'
   )-&gt;pack(
      -side=&gt;'left',
      -padx=&gt;0,
      -pady=&gt;0,
      );

$coldata-&gt;Label(
   -textvariable=&gt;\$dbcolcount,
   -width=&gt;6,
   -background=&gt;$txtbackground,
   -foreground=&gt;$rowcolcolor,
   -relief=&gt;'sunken',
   )-&gt;pack(
      -side=&gt;'right',
      -padx=&gt;0,
      -pady=&gt;0,
      -fill=&gt;'y',
      );
    



# #create the circle button to show the active status
# #$canvas-&gt;createOval(x1, y1, x2, y2, ?option, value, option, value, ...?)
# #$busymarker-&gt;createOval(3,15,15,3, 
# $busymarker-&gt;createOval(3,35,15,3, 
#    -tags=&gt;'circle',
#    -outline=&gt;'grey40',
#    -fill=&gt;$unbusycolor,
#    -width=&gt;1,
#    );
#     
##############################################
#query strings

$qs1frame=$listframe1-&gt;Frame(
   -relief=&gt;'sunken',
   -highlightthickness=&gt;0,
   )-&gt;pack(
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;0,
      -fill=&gt;'x',
      );
      
$clr1=$qs1frame-&gt;Button(
   -text=&gt;"C",
   -width=&gt;1,
   -command=&gt;sub{
      if ($querystring1 ne "") {
         $qs1sav=$querystring1;
         $querystring1="";
         };},
   )-&gt;pack(
      -side=&gt;'left',
      -expand=&gt;0,
      -padx=&gt;0,
      -fill=&gt;'y',
      );     

#use button three to restore the clear operation
$clr1-&gt;bind('&lt;Button-3&gt;'=&gt;sub{if ($qs1sav) {$querystring1=$qs1sav}});

$qscheck1=$qs1frame-&gt;Checkbutton(
   -variable=&gt;\$qsactive1,
   -text=&gt;"Sel",
   -background=&gt;$buttonbackground,
   -borderwidth=&gt;1,
   -selectcolor=&gt;'red4',
   -width=&gt;4,
   -offvalue=&gt;0,
   -onvalue=&gt;1,
   -command=&gt;sub{&amp;act_deactivate("qsentry1","qsactive1")},
   )-&gt;pack(
      -side=&gt;'left',
      -expand=&gt;0,
      -padx=&gt;0,
      -fill=&gt;'y',
      );     

$qsentry1=$qs1frame-&gt;HistEntry(
   -relief=&gt;'flat',
   -textvariable=&gt;\$querystring1,
   -highlightcolor=&gt;'black',
   -selectforeground=&gt;$txtforeground,
   -selectbackground=&gt;'#c0d0c0',
   -borderwidth=&gt;0,
   -background=&gt; 'white',
   -limit=&gt;$histlimit,
   -dup=&gt;0,
   -match=&gt;0,
   -command=&gt;sub{@queryhist1temp=$qsentry1-&gt;history;},
   )-&gt;pack(
      -fill=&gt;'both',
      -expand=&gt;1,
      -pady=&gt;1,
      );

$qsentry1-&gt;bind('&lt;Return&gt;'    =&gt; \&amp;check_cmd); 
$qsentry1-&gt;bind('&lt;Up&gt;'        =&gt; sub { $qsentry1-&gt;historyUp });
$qsentry1-&gt;bind('&lt;Control-p&gt;' =&gt; sub { $qsentry1-&gt;historyUp });
$qsentry1-&gt;bind('&lt;Down&gt;'      =&gt; sub { $qsentry1-&gt;historyDown });
$qsentry1-&gt;bind('&lt;Control-n&gt;' =&gt; sub { $qsentry1-&gt;historyDown });
$qsentry1-&gt;history([@queryhist1]);

$qs2frame=$listframe1-&gt;Frame(
   -relief=&gt;'sunken',
   )-&gt;pack(
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;0,
      -fill=&gt;'x',         
      );
         
$clr2=$qs2frame-&gt;Button(
   -text=&gt;"C",
   -width=&gt;1,
   -command=&gt;sub{
      if ($querystring2 ne "") {
         $qs2sav=$querystring2;
         $querystring2="";
         };},
   )-&gt;pack(
      -side=&gt;'left',
      -expand=&gt;0,
      -padx=&gt;0,
      -fill=&gt;'y',
      );   
      
#use button three to restore the clear operation
$clr2-&gt;bind('&lt;Button-3&gt;'=&gt;sub{if ($qs2sav) {$querystring2=$qs2sav}});

$qscheck2=$qs2frame-&gt;Checkbutton(
   -variable=&gt;\$qsactive2,
   -relief=&gt;'raised',
   -text=&gt;"Sel",
   -background=&gt;$buttonbackground,
   -selectcolor=&gt;'red4',
   -width=&gt;4,
   -offvalue=&gt;0,
   -onvalue=&gt;1,
   -command=&gt;sub{&amp;act_deactivate("qsentry2","qsactive2")},
   )-&gt;pack(
      -side=&gt;'left',
      -expand=&gt;0,
      -padx=&gt;0,
      -fill=&gt;'y',
      );       

$qsentry2=$qs2frame-&gt;HistEntry(
   -relief=&gt;'flat',
   -textvariable=&gt;\$querystring2,
   -highlightcolor=&gt;'black',
   -selectforeground=&gt;$txtforeground,
   -selectbackground=&gt;'#c0d0c0',
   -borderwidth=&gt;0,
   -background=&gt; 'white',
   -limit=&gt;$histlimit,
   -dup=&gt;0,
   -match=&gt;0,
   -command=&gt;sub{@queryhist2temp=$qsentry2-&gt;history;},
   )-&gt;pack(
      -fill=&gt;'both',
      -expand=&gt;1,
      -pady=&gt;1,
      );
    
$qsentry2-&gt;bind('&lt;Return&gt;'    =&gt; \&amp;check_cmd); 
$qsentry2-&gt;bind('&lt;Up&gt;'        =&gt; sub { $qsentry2-&gt;historyUp });
$qsentry2-&gt;bind('&lt;Control-p&gt;' =&gt; sub { $qsentry2-&gt;historyUp });
$qsentry2-&gt;bind('&lt;Down&gt;'      =&gt; sub { $qsentry2-&gt;historyDown });
$qsentry2-&gt;bind('&lt;Control-n&gt;' =&gt; sub { $qsentry2-&gt;historyDown });
$qsentry2-&gt;history([@queryhist2]);

$qs3frame=$listframe1-&gt;Frame(
   -relief=&gt;'sunken',
   )-&gt;pack(
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;0,
      -fill=&gt;'x',         
      );
         
$clr3=$qs3frame-&gt;Button(
   -text=&gt;"C",
   -width=&gt;1,
   -command=&gt;sub{
      if ($querystring3 ne "") {
         $qs3sav=$querystring3;
         $querystring3="";
         };},
   )-&gt;pack(
      -side=&gt;'left',
      -expand=&gt;0,
      -padx=&gt;0,
      -fill=&gt;'y',
      );         
#use button three to restore the clear operation
$clr3-&gt;bind('&lt;Button-3&gt;'=&gt;sub{if ($qs3sav) {$querystring3=$qs3sav}});

$qscheck3=$qs3frame-&gt;Checkbutton(
   -variable=&gt;\$qsactive3,
   -relief=&gt;'raised',
   -text=&gt;"Sel",
   -background=&gt;$buttonbackground,
   -selectcolor=&gt;'red4',
   -width=&gt;4,
   -offvalue=&gt;0,
   -onvalue=&gt;1,
   -command=&gt;sub{&amp;act_deactivate("qsentry3","qsactive3")},
   )-&gt;pack(
      -side=&gt;'left',
      -expand=&gt;0,
      -padx=&gt;0,
      -fill=&gt;'y',
      );       

$qsentry3=$qs3frame-&gt;HistEntry(
   -relief=&gt;'flat',
   -textvariable=&gt;\$querystring3,
   -highlightcolor=&gt;'black',
   -selectforeground=&gt;$txtforeground,
   -selectbackground=&gt;'#c0d0c0',
   -borderwidth=&gt;0,
   -background=&gt; 'white',
   -limit=&gt;$histlimit,
   -dup=&gt;0,
   -match=&gt;0,
   -command=&gt;sub{@queryhist3temp=$qsentry3-&gt;history;},
   )-&gt;pack(
      -fill=&gt;'both',
      -expand=&gt;1,
      -pady=&gt;1,
      );
    
$qsentry3-&gt;bind('&lt;Return&gt;'    =&gt; \&amp;check_cmd); 
$qsentry3-&gt;bind('&lt;Up&gt;'        =&gt; sub { $qsentry3-&gt;historyUp });
$qsentry3-&gt;bind('&lt;Control-p&gt;' =&gt; sub { $qsentry3-&gt;historyUp });
$qsentry3-&gt;bind('&lt;Down&gt;'      =&gt; sub { $qsentry3-&gt;historyDown });
$qsentry3-&gt;bind('&lt;Control-n&gt;' =&gt; sub { $qsentry3-&gt;historyDown });
$qsentry3-&gt;history([@queryhist3]);

#padding for spacing between the query strings and the output Text widget
   $listframe1-&gt;Frame(
   -relief=&gt;'sunken',
   -height=&gt;3,    
   )-&gt;pack(
      -side=&gt;'top',  
      -padx=&gt;0,
      -pady=&gt;0,
      -expand=&gt;0,
      -fill=&gt;'x',
      );
    
#-----------------------------------------------------------------------------
#                                   data listboxes

$scrolly=$listframe1-&gt;Scrollbar(
   -orient=&gt;'vert',
   -elementborderwidth=&gt;1,
   )-&gt;pack(
      -side=&gt;'right',
      -fill=&gt;'y',
      );

$scrollx=$listframe1-&gt;Scrollbar(
   -orient=&gt;'horiz',
   -elementborderwidth=&gt;1,
   )-&gt;pack(
      -side=&gt;'bottom',
      -fill=&gt;'x',
      );
    
$queryheader=$listframe1-&gt;Text(
   -xscrollcommand=&gt;['set', $scrollx],
   -relief=&gt;'flat',
   -background=&gt;$headerbackground,
   -foreground=&gt;$headerforeground,
   -selectforeground=&gt;$txtforeground,
   -selectbackground=&gt;'#c0d0c0',
   -setgrid=&gt;'yes',
   -wrap=&gt;'none',  
   -height=&gt;2,
   -exportselection=&gt;1,
   )-&gt;pack(
      -fill=&gt;'x',
      -expand=&gt;0,
      -pady=&gt;0,
      -anchor=&gt;'n',
      );

$queryout=$listframe1-&gt;Text(
   -yscrollcommand=&gt;['set', $scrolly],
   -xscrollcommand=&gt;['set', $scrollx],
   -relief=&gt;'sunken',
   -background=&gt;$txtbackground,
   -selectforeground=&gt;$txtforeground,
   -selectbackground=&gt;'#c0d0c0',
   -wrap=&gt;'none',
   -height=&gt;14,
   -exportselection=&gt;1, 
   )-&gt;pack(
      -fill=&gt;'both',
      -expand=&gt;1,
      -pady=&gt;0,
      );

$scrolly-&gt;configure(-command=&gt;['yview', $queryout]);
$scrollx-&gt;configure(-command=&gt;\&amp;my_xscroll);

#------------------------------------------------------------------------------------------
#                              bottom row of buttons and labels

$sortframe=$buttonframe-&gt;Frame(
   -relief=&gt;'sunken',
   )-&gt;pack(
      -side=&gt;'left',
      -padx=&gt;0,
      -pady=&gt;0,
      -expand=&gt;1,
      -fill=&gt;'x',      
      );
         
$sortlabel=$sortframe-&gt;Label(
   -text=&gt;'Sort:',
   -relief=&gt;'flat',
   -width=&gt;6,
   -background=&gt;$background,
   )-&gt;pack(
      -side=&gt;'left',
      -padx=&gt;0,
      -pady=&gt;$ypad,
      );

#the sortby textvariable will be configured after the window is created.
$sortbyentry=$sortframe-&gt;Optionmenu(
   -background=&gt;$buttonbackground,
   -width=&gt;24,
   -command=&gt;\&amp;sortby,
   )-&gt;pack(
      -side=&gt;'left',
      -expand=&gt;1,
      -pady=&gt;$ypad,
      -padx=&gt;0,
      -fill=&gt;'both',
      );

$revsortbutton=$sortframe-&gt;Checkbutton(
   -variable=&gt;\$reversesort,
   -text=&gt;"Rev",
   -background=&gt;$buttonbackground,
   -borderwidth=&gt;1,
   -selectcolor=&gt;'red4',
   -width=&gt;4,
   -offvalue=&gt;0,
   -onvalue=&gt;1,
   -command=&gt;\&amp;sortby,
   )-&gt;pack(
      -side=&gt;'left',
      -expand=&gt;0,
      -padx=&gt;0,
      -pady=&gt;$ypad,
      -fill=&gt;'y',
      );     

$numsortbutton=$sortframe-&gt;Checkbutton(
   -variable=&gt;\$numsort,
   -text=&gt;"Num",
   -background=&gt;$buttonbackground,
   -borderwidth=&gt;1,
   -selectcolor=&gt;'red4',
   -width=&gt;4,
   -offvalue=&gt;0,
   -onvalue=&gt;1,
   -command=&gt;\&amp;sortby,
   )-&gt;pack(
      -side=&gt;'left',
      -expand=&gt;0,
      -padx=&gt;0,
      -pady=&gt;$ypad,
      -fill=&gt;'y',
      );     

#frame for spacing 
$sortframe-&gt;Frame(
   -borderwidth=&gt;'0',
   -width=&gt;2,
   )-&gt;pack(
      -side=&gt;'right',
      -padx=&gt;0,
      -pady=&gt;0,
      -expand=&gt;0,
      );
      
#frame for spacing
$buttonframe-&gt;Frame(
   -borderwidth=&gt;'0',
   -width=&gt;2,
   )-&gt;pack(
      -side=&gt;'left',
      -padx=&gt;0,
      -pady=&gt;0,
      -expand=&gt;0,
      );
  
$buttonframe-&gt;Button(
   -text=&gt;'Exit',
   -width=&gt;$buttonwidth,
   -command=&gt;\&amp;save_exit,
   )-&gt;pack(
      -side=&gt;'right',
      -padx=&gt;0,
      -pady=&gt;2,
      );

$buttonframe-&gt;Button(
   -text=&gt;'Search',
   -width=&gt;$buttonwidth,
   -command=&gt;\&amp;searchit,
   )-&gt;pack(
      -side=&gt;'right',
      -padx=&gt;1,
      -pady=&gt;2,
      );
        
$printbutton=$buttonframe-&gt;Button(
   -text=&gt;'Print',
   -width=&gt;$buttonwidth,
   -command=&gt;\&amp;printit,
   )-&gt;pack(
      -side=&gt;'right',
      -padx=&gt;0,
      -pady=&gt;2,
      );

$savebutton=$buttonframe-&gt;Button(
   -text=&gt;'Save',
   -width=&gt;$buttonwidth,
   -command=&gt;sub{&amp;savit("");},
   )-&gt;pack(
      -side=&gt;'right',
      -padx=&gt;1,
      -pady=&gt;2,
      );

$typesbutton=$buttonframe-&gt;Button(
   -text=&gt;'DTypes',
   -width=&gt;$buttonwidth,
   -command=&gt;\&amp;getdatatypes,
   )-&gt;pack(
      -side=&gt;'right',
      -padx=&gt;0,
      -pady=&gt;2,
      );

$typesbutton=$buttonframe-&gt;Button(
   -text=&gt;'Clone',
   -width=&gt;$buttonwidth,
   -command=&gt;\&amp;clone_data,
   )-&gt;pack(
      -side=&gt;'right',
      -padx=&gt;0,
      -pady=&gt;2,
      );

#frame for which to stick the menubutton.  Menubuttons dont line up right with a 
#row of buttons so I have to put them into a frame and pad with another empty frame  :-\
$mbframe=$buttonframe-&gt;Frame( 
   )-&gt;pack(
      -side=&gt;'right',  
      -padx=&gt;0,
      -pady=&gt;2,
      -expand=&gt;0,
      -fill=&gt;'y',
      );
    
$mbutton=$mbframe-&gt;Menubutton(
   -text=&gt;' Snap',
   -width=&gt;$buttonwidth-1,
   -relief=&gt;'raised',
   -indicatoron=&gt;1,
   )-&gt;pack(
      -side=&gt;'right',
      -padx=&gt;0,
      -pady=&gt;1,
      -fill=&gt;'y',
      );

$mbutton-&gt;command(
   -label=&gt;'Take Snapshot 1',
   -command=&gt;sub{&amp;take_snapshot("1");},
   );
$mbutton-&gt;command(
   -label=&gt;'Take Snapshot 2',
   -command=&gt;sub{&amp;take_snapshot("2");},
   );
$mbutton-&gt;command(
   -label=&gt;'Take Snapshot 3',
   -command=&gt;sub{&amp;take_snapshot("3");},
   );
$mbutton-&gt;command(
   -label=&gt;'Take Snapshot 4',
   -command=&gt;sub{&amp;take_snapshot("4");},
   );
       
$querybutton=$buttonframe-&gt;Button(
   -text=&gt;'Exec',
   -width=&gt;$buttonwidth,
   -foreground=&gt;'red4',
   -command=&gt;\&amp;check_cmd,
   )-&gt;pack(
      -side=&gt;'right',
      -padx=&gt;0,
      -pady=&gt;2,
      );

#this is important to do initially otherwise the histories wont be checkpointed
$dbserventry-&gt;invoke;
$dbuserentry-&gt;invoke;
$dbpassentry-&gt;invoke;
$qsentry1-&gt;invoke;
$qsentry2-&gt;invoke;
$qsentry3-&gt;invoke;


$qscheck1-&gt;update;
$qscheck2-&gt;update;
$qscheck3-&gt;update;

&amp;act_deactivate("qsentry1","qsactive1");
&amp;act_deactivate("qsentry2","qsactive2");
&amp;act_deactivate("qsentry3","qsactive3");

#record the pack info for header and the data listboxes..
#needed when switching between command mode and query mode
@headerinfo=$queryheader-&gt;packInfo;
@datainfo=$queryout-&gt;packInfo;

#ensure default is on the list of dbusehist
$founddefault=grep(/^default/,@dbusehist);
if ($founddefault lt "1") {
   push (@dbusehist,"default");
   $dbuseentry-&gt;history([@dbusehist]);
   }
$dbuseentry-&gt;invoke;  

&amp;dbmsghandle (\&amp;message_handler); 
&amp;dberrhandle (\&amp;error_handler);   

#force the optionmenu to be invoked, otherwise the first query will be sorted twice
$sortby="     ";
$sortbyentry-&gt;configure(-textvariable=&gt;\$sortby);

MainLoop;

#                                         subroutines     
#------------------------------------------------------------------------------------------

sub clone_data {
   $CW-&gt;destroy if Exists($CW);
   $clonedate=$LW-&gt;cget(-title);
   #collect the x scroll setting so it can be resumed after the sort
   ($clscrollx,$rest)=$queryout-&gt;xview;
   ($clscrolly,$rest)=$queryout-&gt;yview;
   #The main clone window
   $CW=new MainWindow(-title=&gt;"Cloned Data - $clonedate");
   #set a minimum size so the window cant be resized down to mess up the cancel button
   #$CW-&gt;minsize(850,220);
   #The top frame for the text
   $cloneframe1=$CW-&gt;Frame(
      -borderwidth=&gt;'0',
      -relief=&gt;'flat',
      -background=&gt;$background,
      )-&gt;pack(
         -expand=&gt;1,
         -fill=&gt;'both',
         );

   #for the buttons
   $cloneframe2=$CW-&gt;Frame(
      -borderwidth=&gt;'0',
      -relief=&gt;'flat',
      -background=&gt;$background,
      -height=&gt;60,
      )-&gt;pack(
         -fill=&gt;'x',
         -expand=&gt;0,
         );

   # Create a scrollbar on the right side and bottom of the text
   $hscrolly=$cloneframe1-&gt;Scrollbar(
      -orient=&gt;'vert',
      -elementborderwidth=&gt;1,
      -highlightthickness=&gt;0,
      -background=&gt;$background,
      -troughcolor=&gt;$troughbackground,
      -relief=&gt;'flat',
      )-&gt;pack(
         -side=&gt;'right',
         -fill =&gt;'y',
         );

   $hscrollx=$cloneframe1-&gt;Scrollbar(
      -orient=&gt;'horiz',
      -elementborderwidth=&gt;1,
      -highlightthickness=&gt;0,
      -background=&gt;$background,
      -troughcolor=&gt;$troughbackground,
      -relief=&gt;'flat',
      )-&gt;pack(
         -side=&gt;'bottom',
         -fill=&gt;'x',
         );
         
   $cloneheader=$cloneframe1-&gt;Text(
      -font=&gt;$winfont,
      -xscrollcommand=&gt;['set', $hscrollx],
      -relief=&gt;'flat',
      -highlightthickness=&gt;0,
      -background=&gt;$headerbackground,
      -foreground=&gt;$headerforeground,
      -selectforeground=&gt;$txtforeground,
      -selectbackground=&gt;'#c0d0c0',
      -wrap=&gt;'none',  
      -height=&gt;2,
      -width=&gt;102,
      )-&gt;pack(
         -fill=&gt;'x',
         -expand=&gt;0,
         -pady=&gt;0,
         -anchor=&gt;'n',
         );
         
   $clonewin=$cloneframe1-&gt;Text(
      -yscrollcommand =&gt; ['set', $hscrolly],
      -xscrollcommand =&gt; ['set', $hscrollx],
      -font=&gt;$winfont,
      -relief =&gt; 'sunken',
      -highlightthickness=&gt;0,
      -foreground=&gt;$txtforeground,
      -background=&gt;$txtbackground,
      -borderwidth=&gt;1, 
      -wrap=&gt;'none',
      -height=&gt;10,
      -width=&gt;102,
      -exportselection=&gt;1,
      )-&gt;pack(
         -expand=&gt;1,
         -fill=&gt;'both',
         );

   $hscrolly-&gt;configure(-command =&gt; ['yview', $clonewin]);
   $hscrollx-&gt;configure(-command=&gt;\&amp;my_clonexscroll);

   $cloneframe2-&gt;Label(
      -text=&gt;'Rows:',
      -borderwidth=&gt;1,
      -background=&gt;$background,
      -foreground=&gt;$rowcolcolor,
      -font=&gt;$winfont,
      )-&gt;pack(
         -side=&gt;'left',
         -padx=&gt;1,
         -pady=&gt;4,
         );

   $clrowcount=$cloneframe2-&gt;Label(
      -borderwidth=&gt;1,
      -width=&gt;5,
      -background=&gt;$txtbackground,
      -foreground=&gt;$rowcolcolor,
      -relief=&gt;'sunken',
      -font=&gt;$winfont,
      )-&gt;pack(
         -side=&gt;'left',
         -padx=&gt;1,
         -pady=&gt;4,
         -fill=&gt;'y',
         );

   $cloneframe2-&gt;Label(
      -text=&gt;' Cols:',
      -borderwidth=&gt;1,
      -background=&gt;$background,
      -foreground=&gt;$rowcolcolor,
      -font=&gt;$winfont,
      )-&gt;pack(
         -side=&gt;'left',
         -padx=&gt;0,
         -pady=&gt;4,
         );

   $clcolcount=$cloneframe2-&gt;Label(
      -borderwidth=&gt;1,
      -width=&gt;5,
      -background=&gt;$txtbackground,
      -foreground=&gt;$rowcolcolor,
      -relief=&gt;'sunken',
       -font=&gt;$winfont,
      )-&gt;pack(
         -side=&gt;'left',
         -padx=&gt;4,
         -pady=&gt;4,
         -fill=&gt;'y',
         );

   $cloneframe2-&gt;Label(
      -text=&gt;'Server:',
      -borderwidth=&gt;1,
      -background=&gt;$background,
      -foreground=&gt;$rowcolcolor,
      -font=&gt;$winfont,
      )-&gt;pack(
         -side=&gt;'left',
         -padx=&gt;0,
         -pady=&gt;4,
         );

   $clserver=$cloneframe2-&gt;Label(
      -borderwidth=&gt;1,
      -width=&gt;$tophistentrywidth+1,
      -background=&gt;$txtbackground,
      -foreground=&gt;$rowcolcolor,
      -relief=&gt;'sunken',
       -font=&gt;$winfont,
      )-&gt;pack(
         -side=&gt;'left',
         -padx=&gt;4,
         -pady=&gt;4,
         -fill=&gt;'y',
         );

   $cloneframe2-&gt;Label(
      -text=&gt;'Database:',
      -borderwidth=&gt;1,
      -background=&gt;$background,
      -foreground=&gt;$rowcolcolor,
      -font=&gt;$winfont,
      )-&gt;pack(
         -side=&gt;'left',
         -padx=&gt;0,
         -pady=&gt;4,
         );

   $cldbuse=$cloneframe2-&gt;Label(
      -borderwidth=&gt;1,
      -width=&gt;$tophistentrywidth+1,
      -background=&gt;$txtbackground,
      -foreground=&gt;$rowcolcolor,
      -relief=&gt;'sunken',
       -font=&gt;$winfont,
      )-&gt;pack(
         -side=&gt;'left',
         -padx=&gt;4,
         -pady=&gt;4,
         -fill=&gt;'y',
         );

   $cloneframe2-&gt;Label(
      -text=&gt;'Max Rows:',
      -borderwidth=&gt;1,
      -background=&gt;$background,
      -foreground=&gt;$rowcolcolor,
      -font=&gt;$winfont,
      )-&gt;pack(
         -side=&gt;'left',
         -padx=&gt;0,
         -pady=&gt;4,
         );

   $clmaxrows=$cloneframe2-&gt;Label(
      -borderwidth=&gt;1,
      -width=&gt;$tophistentrywidth+1,
      -background=&gt;$txtbackground,
      -foreground=&gt;$rowcolcolor,
      -relief=&gt;'sunken',
       -font=&gt;$winfont,
      )-&gt;pack(
         -side=&gt;'left',
         -padx=&gt;4,
         -pady=&gt;4,
         -fill=&gt;'y',
         );

   $cloneframe2-&gt;Button(
      -text=&gt;'Cancel',
      -borderwidth=&gt;1,
      -width=&gt;6,
      -background=&gt;$buttonbackground,
      -foreground=&gt;$txtforeground,
      -highlightthickness=&gt;0,
      -font=&gt;$winfont,
      -command=&gt;sub{$CW-&gt;destroy;}
      )-&gt;pack(
         -expand=&gt;0,
         -side=&gt;'right',
         -padx=&gt;0,
         -pady=&gt;2,
         );
         
   $clonesavebutton=$cloneframe2-&gt;Button(
      -text=&gt;'Save',
      -borderwidth=&gt;1,
      -width=&gt;6,
      -background=&gt;$buttonbackground,
      -foreground=&gt;$txtforeground,
      -highlightthickness=&gt;0,
      -font=&gt;$winfont,
      -command=&gt;sub{&amp;clone_savit("");},
      )-&gt;pack(
         -expand=&gt;0,
         -side=&gt;'right',
         -padx=&gt;1,
         -pady=&gt;2,
         );

  $cldbcmd=$cloneframe2-&gt;Menubutton(
      -text=&gt;'Command',
      -relief=&gt;'raised',
      -indicatoron=&gt;0,
      -borderwidth=&gt;1,
      -background=&gt;$buttonbackground,
      -highlightthickness=&gt;0,
      -width=&gt;8,
      -tearoff=&gt;0,
      -font=&gt;$winfont,
      )-&gt;pack(
         -side=&gt;'right',
         -padx=&gt;0,
         -pady=&gt;2,
         -fill=&gt;'y',
         );

   #if there has been more than one resultcount returned, dont display the header 
   if ($resultcount&gt;1) {
      $cloneheader-&gt;packForget;
      }else{
         @theaderdata=$queryheader-&gt;get('0.0','end');
         #ensure there are no trailing spaces on the header to mess up the alignment with the data
         foreach (@theaderdata) {
            $_=~s/ +\n +/\n/g;
            }
         $cloneheader-&gt;insert('end',@theaderdata);
         }
   @tquerydata=$queryout-&gt;get('0.0','end');
   $clonewin-&gt;insert('end',@tquerydata);
   
   #populate the row and column counts etc in the clone window
   $clrowcount-&gt;configure(-text=&gt;$dbrowcount);
   $clcolcount-&gt;configure(-text=&gt;$dbcolcount);
   $clserver-&gt;configure(-text=&gt;$dbserver);
   $cldbuse-&gt;configure(-text=&gt;$dbuse);
   $clmaxrows-&gt;configure(-text=&gt;$maxrowcount);
   $savsqlstring=~s/^ +//;
   $savsqlstring=~s/ +$//;
   $cldbcmd-&gt;command(
      -label=&gt;"$savsqlstring",
      -background=&gt;$txtbackground,
      -activeforeground=&gt;$txtforeground,
      -activebackground=&gt;$txtbackground,
      );
   #scroll the window to display the same lines as the original window
   $clonewin-&gt;xview(moveto=&gt;$clscrollx);
   $clonewin-&gt;yview(moveto=&gt;$clscrolly);
}#sub clone
   
#tie two text widgets (header and data) to scroll horizontally together
sub my_xscroll {
   $queryheader-&gt;xview(@_); 
   $queryout-&gt;xview(@_);     
   }#sub

#tie two text widgets (header and data) to scroll horizontally together
sub my_clonexscroll {
   $cloneheader-&gt;xview(@_); 
   $clonewin-&gt;xview(@_);     
   }#sub

sub operconfirm {
   if ($dangerflag==1) {
      local $ask=$LW-&gt;messageBox(
         -icon=&gt;'warning', 
         -type=&gt;'OKCancel',
         -bg=&gt;$background,
         -title=&gt;'Action Confirm',
         -text=&gt;"You are about to perform a potentially dangerous command!\n\nPlease confirm the action before\nit is executed..",
         -font=&gt;'8x13bold',
         );
      return $ask;
      }else{
         return "Ok";
         }
}#sub operconfirm

#connect with sybase and execute the proper command
sub check_cmd {
   &amp;setbusy;
   $dbserventry-&gt;invoke;
   $dbuserentry-&gt;invoke;
   $dbpassentry-&gt;invoke;
   $dbuseentry-&gt;invoke;
   $qsentry1-&gt;invoke;
   $qsentry2-&gt;invoke;
   $qsentry3-&gt;invoke;
   $dangerflag=0;	
   #start the command clean
   $sqlstring="";
   @dbretrows="";
   $confirm="";
   #force the query button to a normal state before the display is locked
   $querybutton-&gt;configure(-state =&gt;'normal');
   $LW-&gt;update;
   #build the sql command string... to be used by sybase and the isql binary too
   foreach ("querystring1","querystring2","querystring3") {
      #check to see which of the sql strings is enabled with the checkbutton - 
      #only collect and concatenate the enabled sql string lines
      $qsmark=substr($_,-1);
      $qsmark="qsactive$qsmark";
      if (${$qsmark}==1) {
         ${$_}=~s/^ *//;
         $sqlstring.="${$_} ";
         #get the first word of the sql string to check against the dangercmds list
         $verb=(split(/ +\w/,${$_}))[0];
         #check to see if the command is one of the dangerous ones to enable the confirmation when executed
         if (grep(/^$verb$/i,@dangercmds)) {
            $dangerflag=1;
            }
         }#if qsmark eq 1 
      }#foreach query string
   
   $confirm=&amp;operconfirm;
   if ($confirm eq "Ok") {
      $date=`date`;
      $LW-&gt;configure(-title=&gt;"DBGUI $VERSION  [$checkpointfile]  $date"); 
      #save off a wrapped version of the sqlstring for the ascii save files
      $savsqlstring=wrap("    ","    ","$sqlstring");
      #the header has to be nulled out for querys and db commands
      $queryheader-&gt;delete('0.0','end');
      $queryout-&gt;delete('0.0','end');
      $LW-&gt;update;
      #if the menuitem on the display is set to Isql or sqsh, call the isql binary for the
      #dbcommand and skip all of the other stuff
      if ($method eq "Isql"||$method eq "Sqsh") {
         &amp;run_isql_cmd;
         &amp;setunbusy;
         return;
         }
      &amp;run_query;
      }#if confirm eq OK
   &amp;setunbusy;
}#sub check_cmd

#sub to actually execute a DB query. the run_command routine execs all other DB commands
#with the messagehandler, there is no need to check each command for successful status
sub run_query {
   #connect to the database and run the command
   $dbh = new Sybase::DBlib $dbuser,$dbpass,$dbserver;
   #if the login info is bad, complain and quit.  The error handler posts the errors returned
   #from the libraries.  No need to add to them
   if (!$dbh) {
      $queryout-&gt;insert('end',"\n Open Connection to Database Server Failed..\n\n");
      INT_CANCEL;
      return;
      }
   if ($maxrowcount&gt;0) {
      $status=$dbh-&gt;dbcmd("set rowcount $maxrowcount");
      $status=$dbh-&gt;dbsqlexec;
      if ($status==0) {
         $queryout-&gt;insert('end',"\n Set Rowcount Statement Failed..\n\n");
         INT_CANCEL;
         return;
         }#if status eq 0
      }
   #if the dbuse variable is not set to default, specifically set it.
   if ($dbuse !~ /default/i) {
      $status=$dbh-&gt;dbuse($dbuse); 
      if ($status==0) {
         $queryout-&gt;insert('end',"\n Use &lt;DB&gt; Statement Failed\n\n Please Check Use DB Parameters..\n\n");
         INT_CANCEL;
         return;
         }#if status eq 0
      }#if dbuse is not default
   $dbh-&gt;dbcmd("$sqlstring \n");
   #send the sql string to the server.  If there are any errors, the error handler and message
   #handler will post the error text.  No need to test for dbcmd status
   $dbh-&gt;dbsqlexec;
   #set the counters to zero
   $dbrowcount=0;
   $dbcolcount=0;
   $resultcount=0;
   @sortbyhist=();
   $skipsort=0;
   #until no results are pending, crunch the data
   while($dbh-&gt;dbresults != NO_MORE_RESULTS) { 
      $colheaderstring="";
      $collengthheaderstring="";
      ################################## Get the header ###################################  
      #ask sybase the number of columns
      $dbcolcount=$dbh-&gt;dbnumcols;
      $resultcount++;
      $headerstring="";
      $divider="";
      for($i=1;$i&lt;=$dbcolcount;++$i) {
         #ask sybase for the name of each column
         $colheader=$dbh-&gt;dbcolname($i);
         if ($colheader eq "") {
            $colheader="&lt;Func&gt;";
            }
         $coltype=$dbh-&gt;dbcoltype($i);
         #push the column name onto the sortby popup
         push(@sortbyhist,$colheader);
         #collect the length of the column header string
         $hlength{$i}=length($colheader);
         #ask sybase the length of the column data
         $realcollength{$i}=$collength{$i}=$dbh-&gt;dbcollen($i);

         $testcollength=$specialcollengths{$coltype};
         if ($testcollength) {
            $collength{$i}=$testcollength;
            };   

         #this allows me to force specific column widths for some column names
         $testcollength=$specialcols{$colheader};
         if ($testcollength) {
            $collength{$i}=$testcollength;
            };
                     
         #take the longest of the two column lengths for a good display
         if ($hlength{$i} &gt; $collength{$i}) { 
            $collength{$i}=$hlength{$i}
            }
            
         #set a minimum of seven characters wide
         if ($collength{$i}&lt;7) {
            $collength{$i}=7;
            }
            
         #pad the string with spaces out to the column length  
         for($lc=1; $lc&lt;=$collength{$i}-$hlength{$i}; ++$lc) { 
            $colheader.=" ";
            }
            
         for($lc=1; $lc&lt;=$collength{$i}; ++$lc) { 
            $divider.="\-";
            }
          
         #tack on a delimiter for sorting
         $colheaderstring.="$colheader | ";
         $divider.=" | ";
         #build a string displaying the sybase returned lengths of the columns 
         $collengthheader="\[$coltype\] $realcollength{$i}";
         #calculate the length of the real returned value of the length of the column
         $realhlength{$i}=length($collengthheader);       
         #pad the string with spaces out to the column length 
         for($lc=1; $lc&lt;=$collength{$i}-$realhlength{$i}; ++$lc) { 
            $collengthheader.=" ";
            }
         #tack on the same delimiter as the data - for a nice display of columns
         $collengthheaderstring.="$collengthheader | ";
       }#for i=1&lt;scalar(@dbdata...
      
      #push the query lines (2) into the column header listbox widget.
      #if the resultcount is greater than one, prepend a newline so the display looks better 
      if ($resultcount&gt;1) {
         $colheaderstring="\n$colheaderstring";
         }
      push(@dbretrows,"$colheaderstring\n");
      push(@dbretrows,"$collengthheaderstring\n");
      push(@dbretrows,"$divider\n");
      
      ################################## pull in the data ###################################
      #for each row returned from the query..
      while (@dbdata=$dbh-&gt;dbnextrow) {
         #Pull together the data returned for sorting and display  
         #for some reason the sybase call for row count wouldn't work - I'll count em myself
         $dbrowcount++;
         $dbrowstring="";
         $i=0;
         #dont use foreach here to ensure the data is in proper order
         for($xx=0; $xx&lt;scalar(@dbdata); ++$xx) { 
            $i++;
            #calculate the length of the real returned data 
            $element="$dbdata[$xx]";
            $realdlength{$i}=length($element);
            #pad the string with spaces out to the column length
            for($lc=1; $lc&lt;=$collength{$i}-$realdlength{$i}; ++$lc) { 
               $element.=" ";
               }#for $lc=1..
            #tack on a delimiter for sorting
            $dbrowstring.="$element | "
            }#foreach @dbdata
         #remove any accidental tabs found in the data and replace with a single space
         #since I calculate the widths of the columns by the number of characters, tabs
         #only get counted as a single character.  It is safe to remove them and replace with
         #a single space without worrying about mis-aligned columns
         $dbrowstring=~s/\t/ /g;
         #push the final strings onto the dbretrows array 
         push(@dbretrows,"$dbrowstring\n");
         }#while (@dbdata=$dbh-&gt;dbnextrow)
   }#while data is being returned from the server $dbh-&gt;dbresults != NO_MORE_RESULTS)
   #now all of the DB returned data has been collected, close the connection
   $dbh-&gt;dbclose;
   push(@dbretrows,"\n");
   #if only one resultset has been returned, populate and display the pretty header
   #otherwise display the plain db text     argh...
   if ($resultcount==1 &amp;&amp; $skipsort==0) {
      #the header and data have to be repacked in order, otherwise they get flipped
      $queryheader-&gt;insert('end',"$colheaderstring\n");
      $queryheader-&gt;insert('end',"$collengthheaderstring");
      #if the first element is empty remove it
      if ($dbretrows[0] eq "") {
         splice(@dbretrows,0,1);
         }
      #take off the first few elements of the retrows array until real data is encountered
      #or until there is no data on the array at all (like when a truncate command etc is executed)
      until ($dbretrows[0]!~/^\Q$colheaderstring\E$|^\Q$collengthheaderstring\E$|^\Q$divider\E|^$/||!$dbretrows[0]) {
         splice(@dbretrows,0,1);
         }
      #set query state so sorts etc are allowed   
      &amp;set_query_state;
      }else{
         #change the display for the command state (multiple result sets or raw text)
         &amp;set_command_state;
         }
      #call the sortby function to have the data sorted and pushed into the display listbox
      &amp;sortby;
   }#sub

#connect with sybase and execute a DB command using the isql or sqsh binary
sub run_isql_cmd {
   $queryheader-&gt;packForget;
   &amp;set_command_state;
   $usecmd="";
   #if the maxrowcount is 0, dont set one
   if ($maxrowcount&gt;0) {
      $usecmd="set rowcount $maxrowcount\ngo";
      }
   #if the database field is not default, set it   
   if ($dbuse !~ /default/i) {
      $usecmd.="\nuse $dbuse\ngo";
      }
   if ($method eq "Isql") {   
      $sqlbinary=$isqlbinary;
      }
   if ($method eq "Sqsh") {   
      $sqlbinary=$sqshbinary;
      }
   @dbretrows=`$sqlbinary -U$dbuser -P$dbpass -S$dbserver -w999 &lt;&lt;EOD\n$usecmd\n$sqlstring\ngo\nEOD`;
   $queryout-&gt;insert('end',"\ @dbretrows"); 
   }#sub

#set the busy LED to red  
sub setbusy {
   $busymarker-&gt;configure(-background=&gt;$busycolor);
   #$busymarker-&gt;itemconfigure('circle',-fill=&gt;$busycolor,-outline=&gt;'black');
   $LW-&gt;update;
   }

#return the busy LED to green
sub setunbusy {
   $busymarker-&gt;configure(-background=&gt;$unbusycolor);
   #$busymarker-&gt;itemconfigure('circle',-fill=&gt;$unbusycolor,-outline=&gt;'grey40');
   $LW-&gt;update;
   }  

#configure colors to make these labels and the sort checkboxes available
sub set_query_state {
   $queryheader-&gt;pack(@headerinfo);
   $queryout-&gt;pack(@datainfo);
   $sortbyentry-&gt;configure(-options=&gt;\@sortbyhist,-state=&gt;'normal');
   $sortlabel-&gt;configure(-foreground=&gt;$txtforeground);
   $colnumlabel-&gt;configure(-foreground=&gt;$txtforeground);
   $rownumlabel-&gt;configure(-foreground=&gt;$txtforeground);  
   $revsortbutton-&gt;configure(-state=&gt;'normal');
   $numsortbutton-&gt;configure(-state=&gt;'normal',-selectcolor=&gt;'red4');
   }

#configure colors to make these labels and the sort checkboxes unavailable
sub set_command_state {
   $queryheader-&gt;packForget;
   $queryout-&gt;pack(@datainfo);
   $sortby=" ";
   $sortlabel-&gt;configure(-foreground=&gt;'grey65');
   $revsortbutton-&gt;configure(-state=&gt;'disabled');
   $numsortbutton-&gt;configure(-state=&gt;'disabled',-selectcolor=&gt;$buttonbackground);
   $sortbyentry-&gt;configure(-state=&gt;'disabled');
   $colnumlabel-&gt;configure(-foreground=&gt;'grey65');
   $rownumlabel-&gt;configure(-foreground=&gt;'grey65');   
   @sortbyhist=();
   $dbrowcount=$dbcolcount="";
   }
  
#the sort routine can be called standalone, so it is splitout of the run_query routine
sub sortby {
    return if ($#dbretrows&lt;=0);
   #collect the x scroll setting so it can be resumed after the sort
   ($tscrollx,$rest)=$queryout-&gt;xview;
   ($tscrolly,$rest)=$queryout-&gt;yview;
   $queryout-&gt;delete('0.0','end');
   #execute the sort ONLY if 1 result set has been returned and the skipsort flag is 0
   if ($resultcount==1 &amp;&amp; $skipsort==0) {
      if (grep(/^$sortby$/,@sortbyhist)) {
         # we have to figure what element of the sortbyhist array the sort parameter is..
         $sortindex=0;
         foreach (@sortbyhist){
            $sortindex++;
            #if we find the sort parameter in the sortbyhist array, exit the loop
            last if (/^$sortby$/);
            }#foreach @sortbyhist
         }else{
            $sortindex=0;
            $sortby=();
            }#else if grep sortby..
      #if the numeric flag is set, sort differently
      if ($numsort) {
         $sortindex.="n";
         }
      #if the reverse flag is set, sort differently again
      if ($reversesort) {
         $sortindex="\-$sortindex";
         }      
      #execute the sort 
      @dbretrows=fieldsort '\|',[$sortindex],@dbretrows;
      #remove the first element if it is an empty line from the sort
      if ($dbretrows[0] =~/^ *$/) {
         splice(@dbretrows,0,1);
         }
      }#if resultcount ==1

   #post the results regardless of whether or not a sort is being executed
   #if I post the entire array at once, some of the headers are missing when a command has returned
   #multiple resultsets, so I will insert a row at a time :-(
   for ($t=0; $t&lt;scalar(@dbretrows); $t++) {
      $queryout-&gt;insert('end',"$dbretrows[$t]");
      }
   #if no sortby is defined, default to sorting by the first element on the sortbyhist array
   if (!$sortby) {
      $sortby=$sortbyhist[0];
      }
   #manually set the history for sortby and move the display back to where it was previously, also
   #make sure the width is set, otherwise, the menubutton will resize and mess up the execute button
   $sortbyentry-&gt;configure(-options=&gt;\@sortbyhist,-width=&gt;24,-justify=&gt;'left');
   $queryout-&gt;xview(moveto=&gt;$tscrollx);
   $queryout-&gt;yview(moveto=&gt;$tscrolly);
   $queryout-&gt;update;
   }#sub sortby

#write out the returned query information to an ascii file 
sub savit {
   ($outfile)=@_;
   if ($outfile eq "") {
      $savebutton-&gt;configure(-state=&gt;'normal');
      @types =
      (["Log files ",          ['.out']],
       ["Text files",          ['.txt']],
       ["All files ",          ['*']],
      ); 
   $LW-&gt;optionAdd("*background","$savedialogcolor"); 
   $LW-&gt;optionAdd("*highlightBackground", "$savedialogcolor");
   $outfile=$LW-&gt;getSaveFile(
      -filetypes        =&gt; \@types,
      -initialfile      =&gt; 'dbgui.out',
      -defaultextension =&gt; '.out',
      );
   $LW-&gt;optionAdd("*background","$background"); 
   $LW-&gt;optionAdd("*highlightBackground", "$savedialogcolor");
   #if the save dialog was canceled off, dont continue
   return if ($outfile eq "");
   }
   $date=`date`;
   open(outfile, "&gt;$outfile") || die "Can't open save file :$outfile";
   print outfile "\nReport created $date";
   print outfile "Query Data:";
   print outfile "  Server Name - $dbserver";  
   print outfile "     Username - $dbuser";
   print outfile "Database Name - $dbuse";
   if ($maxrowcount) {
      print outfile "Max Row Count - $maxrowcount";
      }
   foreach ("querystring1","querystring2","querystring3") {
      #check to see which of the sql strings is enabled with the checkbutton
      #only print the enabled ones
      $qsmark=substr($_,-1);
      $qsmark="qsactive$qsmark";
      if (${$qsmark}==1) {
         print outfile " Query String - ${$_}";
         }
      }#foreach
   print outfile "Rows Returned - $dbrowcount";
   print outfile "Cols Returned - $dbcolcount\n";
   print outfile "Returned Data:\n";
   $queryhsave=$queryheader-&gt;get('0.0','end');
   $querydsave=$queryout-&gt;get('0.0','end');
   $queryhsave=~s/\n+$//;
   chomp $querydsave;
   print outfile "$queryhsave";
   #create a divider line to go in the output report between the header and data lines
   $thsave=$queryheader-&gt;get('0.0','2.1');
   $divider="";
   for($i = 1; $i &lt; (length($thsave)-2); ++$i) {
      $divider.="-";
      }
   print outfile "$divider";   
   print outfile "$querydsave";
   close outfile;
   }#sub savit


#write out the clone window data to an ascii file 
sub clone_savit {
   ($cloneoutfile)=@_;
   if ($cloneoutfile eq "") {
      $clonesavebutton-&gt;configure(-state=&gt;'normal');
      @types =
      (["Log files ",          ['.out']],
       ["Text files",          ['.txt']],
       ["All files ",          ['*']],
      ); 
   $LW-&gt;optionAdd("*background","$savedialogcolor"); 
   $LW-&gt;optionAdd("*highlightBackground", "$savedialogcolor");
   $cloneoutfile=$LW-&gt;getSaveFile(
      -filetypes        =&gt; \@types,
      -initialfile      =&gt; 'clone_dbgui.out',
      -defaultextension =&gt; '.out',
      );
   $LW-&gt;optionAdd("*background","$background"); 
   $LW-&gt;optionAdd("*highlightBackground", "$savedialogcolor");
   #if the save dialog was canceled off, dont continue
   return if ($cloneoutfile eq "");
   }
   $date=`date`;
   open(outfile, "&gt;$cloneoutfile") || die "Can't open save file :$cloneoutfile";
   print  outfile "\nCloned Data Report created $date\n\nQuery Data:";
   printf outfile "  Server Name - %20s\n",$clserver-&gt;cget(-text);
   printf outfile "Database Name - %20s\n",$cldbuse-&gt;cget(-text); 
   printf outfile "Max Row Count - %20s\n",$clmaxrows-&gt;cget(-text);
   printf outfile "Rows Returned - %20s\n",$clrowcount-&gt;cget(-text);
   printf outfile "Cols Returned - %20s\n\n",$clcolcount-&gt;cget(-text);
   local $mytempcmd=$cldbcmd-&gt;entrycget('end',-label);
   print outfile "DBCmd - $mytempcmd"; 
   print outfile "\nReturned Data:\n";  
   $clqueryhsave=$cloneheader-&gt;get('0.0','end');
   $clqueryhsave=~s/\n+$//;
   chomp $clquerydsave;
   print outfile "$clqueryhsave";
   #create a divider line to go in the output report between the header and data lines
   $clthsave=$cloneheader-&gt;get('0.0','2.1');
   $cldivider="";
   for($i = 1; $i&lt;(length($clthsave)-2); ++$i) {
      $cldivider.="-";
      }
   print outfile "$cldivider";
   $clquerydsave=$clonewin-&gt;get('0.0','end');
   print outfile "$clquerydsave";
   close outfile;
   }#sub clone_savit

sub printit {
   &amp;savit("/tmp/dbgui.prt"); 
   $p=$LW-&gt;PrintDialog(
      -Printers=&gt;\@printers,
      -InFile=&gt;$outfile,
      -Program=&gt;$psprint,
      );
   $p-&gt;Show;
   $outfile="";
   }#sub printit

#eval the snapshot string for the current snapshot variable value
sub run_snapshot {
   ($snapnum)=@_;
   $tsnap="snapshot$snapnum";
   (eval $$tsnap);
   #once the snapshot string has been executed, pull the single quotes off of the query strings
   #otherwise, everytime a snapshot is executed, the slashes start stacking up, we dont want
   #the slashes displayed in the GUI, only in the checkpoint file
   foreach ($querystring1,$querystring2,$querystring3) {
      #replace any single quote strings with an escaped quote 
      $_ =~ s/\\'/\'/g;
       $_ =~ s/\\"/\"/g;
      $_ =~ s/\\@/\@/g;
      }
   &amp;act_deactivate("qsentry1","qsactive1");
   &amp;act_deactivate("qsentry2","qsactive2");
   &amp;act_deactivate("qsentry3","qsactive3");
   }#sub run snapshot

#capture the snapshot string to be saved
sub take_snapshot {
   ($snapnum)=@_;
   $tsnap="snapshot$snapnum";
   foreach ($querystring1,$querystring2,$querystring3) {
      #replace any slashes with an escaped quote with a three slashes and a single quote
      #needed to properly save the values off in the .dbgui file 
      $_ =~ s/\\*\'/\\\'/g;
      #substitute double quotes for escaped ones
      $_=~s/\"/\\\"/g;    
      }
        
   #collect all other variables for the snapshot string
   $snapshotstring="";   
   foreach (@variablelist) {
      next if (/^snapshot/);
      $snapshotstring.="\$$_=\'$$_\'\;"
      }  
   ${$tsnap}=$snapshotstring;   
   &amp;run_snapshot("$snapnum");
   }#sub snapshot

#save the checkpoint file and exit the program
sub save_exit { 
   #write out the checkpoint file for the next time the utility is started
   open(ckptfile, "&gt;$checkpointfile") or die "Can't open checkpoint file - $checkpointfile"; 
   print ckptfile "\#Checkpoint file for dbgui.pl utility";
   print ckptfile "\#Query parameters and histories are saved on exit and restored on startup\n";
   print ckptfile "\#NOTE - Snapshot0 is used to set the variables on application startup, but";
   print ckptfile "\#the variables have to be initialized for the ComboEntries..  therefore they";
   print ckptfile "\#are set to null initially and redifined from Snapshot0..\n";

   #Snapshot 0 is always the state the utility was last left if exited properly.
   #Since snapshot 0 will be restored on startup, there is no need to record any variables
   #that are not the snapshot strings - however if the variables are not initialized,
   #the history arrays will get lost the next time the utility is started, so I write all
   #variables to the checkpoint file as empty even though they are redefined with snapshot0 
   &amp;take_snapshot("0");
   
   #since the variables can contain dollar signs (like in sp_helptext commands) 
   #etc... escape the special characters
   foreach (@variablelist) {
      if (!/^snapshot/) {
         print ckptfile "\$$_=\"\"\;";
         }else{
            if ("${$_}" ne "") {      
               print ckptfile "\$$_=q\($$_\)\;";
               }else{
                   #the snapshot variable is empty - write out null definitions for each variable
                   $snapshotstring="$_\=q\(";   
                   foreach (@variablelist) {
                      next if (/^snapshot/);
                      $snapshotstring.="\$$_=\'\'\;"
                      }
                    ${$_}=$snapshotstring;
                    print ckptfile "\$${$_}\)\;";
                   }#else
             }#first else  
      }#foreach variablelist 
   foreach $temparrayname (@arraylist) {
      $realarrayname=$temparrayname;
      $realarrayname=~s/temp$//;
      $arraystring="\@$realarrayname=(\n";
      foreach (@{$temparrayname}) {
         #substitute double quotes for escaped ones
         $_=~s/\"/\\\"/g;          
         #substitute @ symbols for escaped ones
         $_=~s/\@/\\\@/g;         
         $arraystring.="\"$_\",\n";
         }
      $arraystring.="\);";  
      print ckptfile "\n$arraystring";
      }
   print ckptfile "\n1\;";
   close ckptfile;  
   exit;
   }#sub save_exit

#configure the query strings to be greyed or black depending on their execute state set
#by the checkbuttons
sub act_deactivate {
   ($querywid,$queryline)=@_;
   if (${$queryline} eq "1") {
      ${$querywid}-&gt;configure(-fg=&gt;$txtforeground);
      ${$querywid}-&gt;focus;
      }else{
         ${$querywid}-&gt;configure(-fg=&gt;'grey65');   
         }
    ${$querywid}-&gt;update;
   }#sub

#error handler taken from subutil.pl in the sybperl distribution.  Needed to trap
#the error strings returned for non DB type
sub error_handler {
   my ($db, $severity, $error, $os_error, $error_msg, $os_error_msg)= @_;
   #print "($db, $severity, $error, $os_error, $error_msg, $os_error_msg)";
   
   # Check the error code to see if we should report this.   
   if ($error != SYBESMSG) {
      $error_msg=wrap(" "," ","$error_msg");
      &amp;set_command_state;
      $queryout-&gt;insert('end',"Error:$error\nSeverity:$severity\nOS Error:$os_error\nOS Error Message:$os_error_msg\n\n$error_msg");
      $skipsort=1;
      }
   INT_CANCEL;
}#sub error_handler

#message handler taken from subutil.pl in the sybperl distribution.  Needed to trap
#the message strings returned form the DB server  "lib/linutil.pl"
sub message_handler {
   my ($db, $message, $state, $severity, $dbtext, $server, $procedure, $line)= @_;
   #print "(db, message, state, severity, dbtext, server, procedure, line)";
   #print "($db, $message, $state, $severity, $dbtext, $server, $procedure, $line)";
   # Don't display 'informational' messages
   if ($severity &gt; 10) {
      #wrap the error text
      $dbtext=wrap(" "," ","$dbtext");
      #if there are no result rows returned, sortby wont do anything, therefore we push the
      #error text into the display pane.
      push(@dbretrows,"Message:$message\nSeverity:$severity\nProcedure:$procedure\nLine:$line\nState:$state\n\n$dbtext");
      $queryout-&gt;insert('end',"Message:$message\nProcedure:$procedure\nLine:$line\nState:$state\nSeverity:$severity\n\n$dbtext");
      $skipsort=1;
      INT_CANCEL;
      }
   #the message variable will be a zero if a print is called from the database
   #(like when sp_helpcode is executed)
   if ($message == 0) {
      return if ($dbtext =~/^Message empty/);
      $queryout-&gt;insert('end',"$dbtext\n");
      }
   0;
}#sub message_handler

sub getdatatypes {
   $typesbutton-&gt;configure(-state =&gt;'normal');
   $confirmtext=" ID  Length  Description    \n---- ------  ------------\n";
   @datatypes=();
   #connect to the database and run the command
   $dbh = new Sybase::DBlib $dbuser,$dbpass,$dbserver;
   return if (!$dbh);
   if ($dbuse !~ /default/i) {
      $status=$dbh-&gt;dbuse($dbuse); 
      if ($status==0) {
         $queryout-&gt;insert('end',"\n Use &lt;DB&gt; Statement Failed\n\n Please Check Use DB Parameters..\n\n");
         INT_CANCEL;
         return;
         }#if status eq 0
      }
   #extract from the datatypes using "select type,name from systypes"
   $dbh-&gt;dbcmd("$dbtypescmd \n");
   $dbh-&gt;dbsqlexec;
   $dbh-&gt;dbresults;
   #dont display the results if the comman failed
   while (@dbdata=$dbh-&gt;dbnextrow) {  
      push (@datatypes,"@dbdata");
      }
   $dbh-&gt;dbclose;
   #use the fieldsort to perform a numerical sort on the datatypes array
   @datatypes=fieldsort ['1n'], @datatypes;  
   foreach (@datatypes) {
      ($dtnum,$dttype,$dtname)=split(" ",$_);
      $dtnum = sprintf("%4d",$dtnum);
      $dttype = sprintf("%6d",$dttype);
      $confirmtext.="$dtnum$dttype   $dtname\n";
      }  
   #dialog box for datatypes...
   $confirmwin-&gt;destroy if Exists($confirmwin);
      local $confirmbox=$LW-&gt;messageBox(
         -type=&gt;'OK',
         -bg=&gt;$background,
         -font=&gt;'8x13bold',
         -title=&gt;"Data Types         ",
         -text=&gt;$confirmtext,
         );
   }#sub getdatatypes

#query results search dialog
sub searchit {
   $srchstring="";
   $SW-&gt;destroy if Exists($SW);
   $SW=new MainWindow(-title=&gt;'DBGUI search');
   #width,height in pixels    
   $SW-&gt;minsize(424,51);
   $SW-&gt;maxsize(724,51);
   #default to non case sensitive
   $caseflag="nocase";
   $newsearch=1;
   #The top frame for the text
   $searchframe1=$SW-&gt;Frame(
      -borderwidth=&gt;'0',
      -relief=&gt;'flat',
      -background=&gt;$background,
      )-&gt;pack(
         -expand=&gt;1,
         -fill=&gt;'both',
         );

   $searchframe2=$SW-&gt;Frame(
      -borderwidth=&gt;'0',
      -relief=&gt;'flat',
      -background=&gt;$background,
      )-&gt;pack(
         -fill=&gt;'x',
         -pady=&gt;2,
         );

   $searchframe1-&gt;Checkbutton(
      -variable=&gt;\$caseflag,
      -font=&gt;$winfont,
      -relief=&gt;'flat',
      -text=&gt;"Case",
      -highlightthickness=&gt;0,
      -highlightcolor=&gt;'black',
      -activebackground=&gt;$background,
      -bg=&gt;$background,
      -foreground=&gt;$txtforeground,
      -borderwidth=&gt;'1',
      -width=&gt;6,
      -offvalue=&gt;"nocase",
      -onvalue=&gt;"case",
      -command=&gt;sub{$current='0.0',$searchcount=0;$newsearch=1},
      -background=&gt;$background,
      )-&gt;pack(
         -side=&gt;'left',
         -expand=&gt;0,
         );
         
   $searchhistframe=$searchframe1-&gt;Frame(
   -borderwidth=&gt;1,
   -relief=&gt;'sunken',
   -background=&gt;$background,
   -foreground=&gt;$txtforeground,
   -highlightthickness=&gt;0,
   )-&gt;pack(
      -side=&gt;'bottom',
      -expand=&gt;0,
      -pady=&gt;0,
      -padx=&gt;1,
      -fill=&gt;'x', 
      );
      
   $ssentry=$searchhistframe-&gt;HistEntry(
      -font=&gt;$winfont,
      -relief=&gt;'sunken',
      -textvariable=&gt;\$srchstring,
      -highlightthickness=&gt;0,
      -highlightcolor=&gt;'black',
      -selectforeground=&gt;$txtforeground,
      -selectbackground=&gt;'#c0d0c0',
      -bg=&gt;$background,
      -foreground=&gt;$txtforeground,
      -borderwidth=&gt;0,
      -bg=&gt; 'white',
      -limit=&gt;$histlimit,
      -dup=&gt;0,
      -match =&gt; 1,  
      -justify=&gt;'left',
      -command=&gt;sub{@searchhisttemp=$ssentry-&gt;history;},
      )-&gt;pack(
         -fill=&gt;'both',
         -expand=&gt;0,
         );

   #press enter and perform a single fine
   $ssentry-&gt;bind('&lt;Return&gt;'    =&gt; \&amp;find_one);
   $ssentry-&gt;bind('&lt;Up&gt;'        =&gt; sub { $ssentry-&gt;historyUp });
   $ssentry-&gt;bind('&lt;Control-p&gt;' =&gt; sub { $ssentry-&gt;historyUp });
   $ssentry-&gt;bind('&lt;Down&gt;'      =&gt; sub { $ssentry-&gt;historyDown });
   $ssentry-&gt;bind('&lt;Control-n&gt;' =&gt; sub { $ssentry-&gt;historyDown });
   $ssentry-&gt;history([@searchhist]);

   $searchframe2-&gt;Button(
      -text=&gt;'Find',
      -borderwidth=&gt;'1',
      -width=&gt;'10',
      -background=&gt;$buttonbackground,
      -foreground=&gt;$txtforeground,
      -highlightthickness=&gt;0,
      -font=&gt;$winfont,
      -command=&gt;\&amp;find_one,
      )-&gt;pack(
         -side=&gt;'left',
         -padx=&gt;2,
         );

   $searchframe2-&gt;Button(
      -text=&gt;'Find All',
      -borderwidth=&gt;'1',
      -width=&gt;'10',
      -background=&gt;$buttonbackground,
      -foreground=&gt;$txtforeground,
      -highlightthickness=&gt;0,
      -font=&gt;$winfont,
      -command=&gt;\&amp;find_all,
      )-&gt;pack(
         -side=&gt;'left',
         -padx=&gt;2,
         );

   $searchframe2-&gt;Button(
      -text=&gt;'Cancel',
      -borderwidth=&gt;'1',
      -width=&gt;'10',
      -background=&gt;$buttonbackground,
      -foreground=&gt;$txtforeground,
      -highlightthickness=&gt;0,
      -font=&gt;$winfont,
      -command=&gt;sub{$SW-&gt;destroy;$queryout-&gt;tag('remove','search', qw/0.0 end/);}
      )-&gt;pack(
         -side=&gt;'right',
         -padx=&gt;2,
         );
   $ssentry-&gt;focus;      
} # sub search


# search the Logfile for a term and return a highlighted line containing the term.
sub find_all {
   return if ($srchstring eq "");
   $ssentry-&gt;invoke;
   #delete any old tags so new ones will show
   $queryout-&gt;tag('remove','search', qw/0.0 end/);
   $current='0.0';
   $stringlength=length($srchstring);
   $searchcount=0;
   while (1) {
      if ($caseflag eq "case") {  
         $current=$queryout-&gt;search(-exact,$srchstring,$current,'end');
         }else{
            $current=$queryout-&gt;search(-nocase,$srchstring,$current,'end');
            }#else  
      last if (!$current);
      $queryout-&gt;tag('add','search',$current,"$current + $stringlength char");
      $queryout-&gt;tag('configure','search',
         -background=&gt;'chartreuse',
         -foreground=&gt;'black',
         );
      $searchcount++;       
      $current=$queryout-&gt;index("$current + 1 char");
      }#while true
   $SW-&gt;configure(-title=&gt;"$searchcount Matches");
   $searchcount=0;
   $current='0.0';
}#sub find all

#find and highlight one instance of a string at a time
sub find_one {
   return if ($srchstring eq "");
   #since the search dialog is spawned and respawned again and again, we have to push the
   #search string onto the history array manually.  otherwise we will reset to the original
   #history as loaded by the .nedit file
   push (@searchhist,$srchstring);
   $ssentry-&gt;invoke;
   $queryout-&gt;tag('remove','search', qw/0.0 end/);
   #mull through the text tagging the matched strings along the way
   if ($srchstring ne $srchstringold || $newsearch==1) {
      $allcount=0;
      $tempcurrent='0.0';
      $srchstringold=$srchstring;
      while (1) {
         if ($caseflag eq "case") {  
            $tempcurrent=$queryout-&gt;search(-exact,$srchstring,$tempcurrent,'end');
            }else{
               $tempcurrent=$queryout-&gt;search(-nocase,$srchstring,$tempcurrent,'end');
               }#else  
         last if (!$tempcurrent);
         $allcount++;       
         $tempcurrent=$queryout-&gt;index("$tempcurrent + 1 char");
         $searchcount=0;
         $current='0.0';
         }#while true
     $newsearch=0; 
    }#if srchstring ne srstringold
   #set the titlebar of the search dialog to indicate the matches
   $SW-&gt;configure(-title=&gt;"$allcount Matches");  
   $stringlength=length($srchstring);
   if (!$current) {
      $current='0.0';
      $searchcount=0;
      } # if current
   local $currentold=$current;   
   if ($caseflag eq "case") {  
      $current=$queryout-&gt;search(-exact,$srchstring,"$current +1 char");
      }else{
         $current=$queryout-&gt;search(-nocase,$srchstring,"$current +1 char");
         }#else
   #no matches were found - set the titlebar
   if ($current eq "") {
      $SW-&gt;configure(-title=&gt;"No Matches");
      return;
      }      
   $current=$queryout-&gt;index($current);
   $queryout-&gt;tag('add','search',$current,"$current + $stringlength char");
   $queryout-&gt;tag('configure','search',
      -background=&gt;'chartreuse',
      -foreground=&gt;'black',
      );         
   $queryout-&gt;see($current);
   #see where the display has horizontally scrolled and move the header text to match 
   ($tscrollx,$rest)=$queryout-&gt;xview;
   $queryheader-&gt;xview(moveto=&gt;$tscrollx);
} #sub find one

#return a positive status 
1;
</pre></body></html>