#! /usr/bin/perl -w # # Copyright (c) 2002 Harry Holt . All rights reserved. # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # # THIS PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, # EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF # AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, # YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. # # TK-based GUI front-end to COBOLIO.pm - Harry Holt # use strict; use Tk; use Tk::DirTree; use File::Basename; use COBOLIO; require Tk::FileSelect; # ##################################### # Program Control Variables my $fileCheck = 0; my(@pl) = qw/-side top -anchor w -expand yes -fill y/; my(@secPl) = qw/-side left -expand yes -fill both/; my(@botPl) = qw/-side left -fill both -expand yes/; my(@ff) = qw/-side left -fill y -expand yes -fill y/; my $startDir = "C:\\"; my $dirTreeWindow; # ################################################ # Resource Variables my $shortInstructions = "To browse a different drive, enter the drive letter in the box."; my $dnWindow; my $btnCancel; my $btnDownload; # # ############################################### # User Data Variables my $copyLibDir = ""; my $dataDir = ""; my $copyLibFileName = ""; my $dataFileName = ""; my $dataOutFileName = ""; my $recordName = ""; my $recordNameToUse = ""; my $sameAsRecordName = 0; my $fileDesc = 0; my $includeHeader = 0; my $userID = ""; my $password = ""; my $rec01OnCopylib = 0; my $copyLibPDSName = "SYS.COPYLIB"; my $main = MainWindow->new(); my $f = $main->Frame->pack(-fill => 'both'); my $row = 0; ################################################################################################## ### Instructions my $lblInstr = $f->Label(-text => $shortInstructions, -anchor => 'e', -justify => 'center', -font => 'bold'); Tk::grid($lblInstr, -row => $row++, -column => 1, -sticky => 'ew'); # ################################################################################################## ### Copylib Directory Name my $txtCopyLibDir = $f->Entry(qw/-width 40/); my $lblCopyLibDir = $f->Label(-text => 'Copylibs Directory: ', -anchor => 'e', -justify => 'right'); my $btn1 = $f->Button(-text => 'Browse', -command => sub{pickCopyLibDir()}); Tk::grid($lblCopyLibDir, -row => $row, -column => 0, -sticky => 'e'); Tk::grid($txtCopyLibDir, -row => $row, -column => 1, -sticky => 'ew'); Tk::grid($btn1, -row => $row++, -column => 2, -sticky => 'w'); $f->gridRowconfigure(1, -weight => 1); $txtCopyLibDir->focus; # ### Data Directory Name my $txtDataDir = $f->Entry(qw/-width 40/); my $lblDataDir = $f->Label(-text => 'Data Directory: ', -anchor => 'e', -justify => 'right'); my $btn2 = $f->Button(-text => 'Browse', -command => sub{pickDataDir()}); Tk::grid($lblDataDir, -row => $row, -column => 0, -sticky => 'e'); Tk::grid($txtDataDir, -row => $row, -column => 1, -sticky => 'ew'); Tk::grid($btn2, -row => $row++, -column => 2, -sticky => 'w'); $f->gridRowconfigure(1, -weight => 1); # ################################################################################################## #### End of Directories # ### Copylib File Name my $txtCopyLibFileName = $f->Entry(qw/-width 40/); my $lblCopyLib = $f->Label(-text => 'Copylib Member Name: ', -anchor => 'e', -justify => 'right'); my $btn3 = $f->Button(-text => 'Browse', -command => sub{pickFileNoDir($txtCopyLibFileName, $txtCopyLibDir)}); my $btnDn3 = $f->Button(-text => 'Download', -command => sub{downloadFile($txtCopyLibFileName, $txtCopyLibDir, 1)}); Tk::grid($lblCopyLib, -row => $row, -column => 0, -sticky => 'e'); Tk::grid($txtCopyLibFileName, -row => $row, -column => 1, -sticky => 'ew'); Tk::grid($btn3, -row => $row, -column => 2, -sticky => 'w'); Tk::grid($btnDn3, -row => $row++, -column => 3, -sticky => 'w'); $f->gridRowconfigure(1, -weight => 1); # ### Data File Name - BASE name is used in box, prefix with text from $txtDataDir my $txtDataFileName = $f->Entry(qw/-width 40/); my $lblDataFile = $f->Label(-text => 'Input Data File Name: ', -anchor => 'e', -justify => 'right'); my $btn4 = $f->Button(-text => 'Browse', -command => sub{pickFileNoDir($txtDataFileName, $txtDataDir)}); my $btnDn4 = $f->Button(-text => 'Download', -command => sub{downloadFile($txtDataFileName, $txtDataDir, 0)}); Tk::grid($lblDataFile, -row => $row, -column => 0, -sticky => 'e'); Tk::grid($txtDataFileName, -row => $row, -column => 1, -sticky => 'ew'); Tk::grid($btn4, -row => $row, -column => 2, -sticky => 'w'); Tk::grid($btnDn4, -row => $row++, -column => 3, -sticky => 'w'); $f->gridRowconfigure(1, -weight => 1); # ### output Data File Name my $txtOutDataFileName = $f->Entry(qw/-width 40/); my $lblOutDataFile = $f->Label(-text => 'Output Data File Name: ', -anchor => 'e', -justify => 'right'); my $btn5 = $f->Button(-text => 'Browse', -command => sub{pickFileNoDir($txtOutDataFileName, $txtDataDir)}); Tk::grid($lblOutDataFile, -row => $row, -column => 0, -sticky => 'e'); Tk::grid($txtOutDataFileName, -row => $row, -column => 1, -sticky => 'ew'); Tk::grid($btn5, -row => $row++, -column => 2, -sticky => 'w'); $f->gridRowconfigure(1, -weight => 1); # ### Main Record Name (optional) my $txtRecord01Name = $f->Entry(qw/-width 40/); my $lblRecord01Name = $f->Label(-text => 'Main (01) Record Name: ', -anchor => 'e', -justify => 'right'); my $chkOnCopylib = $f->Checkbutton(); $chkOnCopylib->configure(-text => 'Specified in Copylib', -justify => 'left', -command => sub{rec01OnCopylib($chkOnCopylib, $txtRecord01Name)}); Tk::grid($lblRecord01Name, -row => $row, -column => 0, -sticky => 'e'); Tk::grid($txtRecord01Name, -row => $row, -column => 1, -sticky => 'ew'); Tk::grid($chkOnCopylib, -row => $row++, -column => 3, -sticky => 'w'); $f->gridRowconfigure(1, -weight => 1); # ### Record Name my $txtRecordName = $f->Entry(qw/-width 40/); my $lblRecordName = $f->Label(-text => 'Record Name to Use: ', -anchor => 'e', -justify => 'right'); my $chkUse01 = $f->Checkbutton(); $chkUse01->configure(-text => 'Same as 01 Record', -justify => 'left', -command => sub{sameRecClick($chkUse01, $txtRecord01Name, $txtRecordName)}); Tk::grid($lblRecordName, -row => $row, -column => 0, -sticky => 'e'); Tk::grid($txtRecordName, -row => $row, -column => 1, -sticky => 'ew'); Tk::grid($chkUse01, -row => $row++, -column => 3, -sticky => 'w'); $f->gridRowconfigure(1, -weight => 1); # ### File Descriptor my $chkFD = $f->Checkbutton(-text => 'Data Description is part of File Descriptor', -justify => 'left'); Tk::grid($chkFD, -row => $row++, -column => 0, -sticky => 'w'); $f->gridRowconfigure(1, -weight => 1); # ### Header for CSV my $chkHeader = $f->Checkbutton(-text => 'Include Header row in CSV', -justify => 'left'); Tk::grid($chkHeader, -row => $row++, -column => 0, -sticky => 'w'); $f->gridRowconfigure(1, -weight => 1); # ############################## ### Command Buttons $main->Button(-text => 'Quit', -command => sub{mainExit()})->pack(-side => "left", -anchor => "w",); $main->Button(-text => 'Show input', -command => sub{showInput()})->pack(-side => "left", -anchor => "w",); $main->Button(-text => 'File Check', -command => sub{checkFiles()})->pack(-side => "left", -anchor => "w"); $main->Button(-text => 'Make CSV', -command => sub{makeCSV( $txtDataDir->get(), $txtCopyLibDir->get(), $txtCopyLibFileName->get(), $txtDataFileName->get(), $txtRecord01Name->get(), $txtRecordName->get(), $txtOutDataFileName->get(), $chkFD->{Value}, $chkHeader->{Value} )})->pack(-side => "left", -anchor => "w"); # mainStart(); MainLoop; # # ###################################################################################################### ###################################################################################################### ### Subroutines for handling events in the Tk widgets # ### Quit button ### Exit the program, saving varibles as defaults for next time sub mainExit { open(INIFILE, ">C:\\CoblTool.ini"); print INIFILE "$copyLibDir\n"; print INIFILE "$dataDir\n"; print INIFILE $txtCopyLibFileName->get()."\n"; print INIFILE $txtDataFileName->get()."\n"; print INIFILE $txtOutDataFileName->get()."\n"; print INIFILE $txtRecord01Name->get()."\n"; print INIFILE $txtRecordName->get()."\n"; close(INIFILE); $main->destroy(); } # ### Load the Main form ### Read any saved information sub mainStart { my $inline; open(INIFILE, "C:\\CoblTool.ini"); if(!(eof(INIFILE))) { $inline = ; chomp($inline); $txtCopyLibDir->insert(0,$inline); $copyLibDir = $inline; } if(!(eof(INIFILE))) { $inline = ; chomp($inline); $txtDataDir->insert(0,$inline); $dataDir = $inline; } if(!(eof(INIFILE))) { $inline = ; chomp($inline); $txtCopyLibFileName->insert(0,$inline); $copyLibFileName = $inline; } if(!(eof(INIFILE))) { $inline = ; chomp($inline); $txtDataFileName->insert(0,$inline); } if(!(eof(INIFILE))) { $inline = ; chomp($inline); $txtOutDataFileName->insert(0,$inline); } if(!(eof(INIFILE))) { $inline = ; chomp($inline); $txtRecord01Name->insert(0,$inline); } if(!(eof(INIFILE))) { $inline = ; chomp($inline); $txtRecordName->insert(0,$inline); } close(INIFILE); } # ### Select Data Directory sub pickDataDir { $dirTreeWindow = MainWindow->new(); my $dl = $dirTreeWindow->Scrolled('DirTree')->pack(@pl); if($txtDataDir->get() =~ /:/) { $startDir = $txtDataDir->get(); } $dl->configure(-command => \&setDataDir, -directory => $startDir); } # sub setDataDir { (my $arg1) = @_; $dataDir = $arg1; $txtDataDir->delete(0,1000); $txtDataDir->insert(0,$dataDir); $dirTreeWindow->destroy(); } # ### Select Copy Lib directory ########################################################## sub pickCopyLibDir { $dirTreeWindow = MainWindow->new(); my $dl2 = $dirTreeWindow->Scrolled('DirTree')->pack(@pl); if($txtCopyLibDir->get() =~ /:/) { $startDir = $txtCopyLibDir->get(); } $dl2->configure(-command => \&setCopyLibDir, -directory => $startDir); } sub setCopyLibDir { (my $arg1) = @_; $copyLibDir = $arg1; $txtCopyLibDir->delete(0,1000); $txtCopyLibDir->insert(0,$copyLibDir); $dirTreeWindow->destroy(); } # ### Select files for various fields #################################################### sub pickFile { (my $textBox) = @_; my $mp; my $ep = 0; my $tmp = $textBox->get(); if(length($tmp) > 0) { while ( !($tmp =~ m/\/\z/) && (length($tmp) > 1) ) { $tmp = substr($tmp,0,length($tmp) - 1); } $startDir = $tmp; } else { $startDir = "C:\\"; } my $FSRef = $main->FileSelect(-directory => $startDir); if(my $selectedFileName = $FSRef->Show()) { if($selectedFileName ne "") { $textBox->delete(0,1000); $textBox->insert(0,$selectedFileName); } } } # # ### Select files for fields that should NOT include the directory. ###################### sub pickFileNoDir { (my $nameBox, my $dirBox) = @_; my $mp; my $ep = 0; my $tmp = $dirBox->get(); if(length($tmp) > 0) { while ( !($tmp =~ m/\/\z/) && (length($tmp) > 1) ) { $tmp = substr($tmp,0,length($tmp) - 1); } $startDir = $tmp; } else { $startDir = "C:\\"; } my $FSRef = $main->FileSelect(-directory => $startDir); if(my $selectedFileName = $FSRef->Show()) { if($selectedFileName ne "") { (my $bn, my $path, my $type) = fileparse($selectedFileName); $nameBox->delete(0,1000); #$nameBox->insert(0,$bn.$type); $nameBox->insert(0,$bn); } } } # ### Download files from the mainframe #################################################### sub downloadFile { (my $textBox, my $localDirBox, my $isPDS) = @_; my $msg = ""; my $mfFileName = $textBox->get(); my $locFileName = $localDirBox->get(); if($locFileName eq "") { $msg .= "Directory name required for downloads. Select a local directory and try again."; my $mbResp = $main->messageBox(-message => $msg, -type => 'OK'); return; } $locFileName .= "/".$mfFileName; if($mfFileName eq "") { $msg .= "No file name specified. Enter a file name and try again."; my $mbResp = $main->messageBox(-message => $msg, -type => 'OK'); return; } $dnWindow = MainWindow->new(); my $dwF = $dnWindow->Frame->pack(-fill => 'both'); my $dwRow = 0; my $lblUserID = $dwF->Label(-text => 'User ID:', -anchor => 'e', -justify => 'right'); my $txtUserID = $dwF->Entry(qw/-width 30/); my $lblPassword = $dwF->Label(-text => 'Password:', -anchor => 'e', -justify => 'right'); my $txtPassword = $dwF->Entry(qw/-width 30 -show */); $btnDownload = $dwF->Button(-text => 'OK', -command => sub{runDownload($txtUserID, $txtPassword, $mfFileName, $locFileName, $isPDS)}); $btnCancel = $dwF->Button(-text => 'Cancel', -command => sub{$dnWindow->destroy()}); Tk::grid($lblUserID, -row => $row, -column => 0, -sticky => 'e'); Tk::grid($txtUserID, -row => $row++, -column => 1, -sticky => 'ew'); Tk::grid($lblPassword, -row => $row, -column => 0, -sticky => 'e'); Tk::grid($txtPassword, -row => $row++, -column => 1, -sticky => 'ew'); Tk::grid($btnDownload, -row => $row, -column => 0, -sticky => 'e'); Tk::grid($btnCancel, -row => $row++, -column => 1, -sticky => 'w'); $dwF->gridRowconfigure(1, -weight => 1); $dnWindow->focus; $txtUserID->focus; } # ### Show Input button ### Dump the contents of the copylib sub showInput { $copyLibFileName = $txtCopyLibFileName->get(); my $copyLibFP = $txtCopyLibDir->get()."/".$copyLibFileName; my $dumpWindow = MainWindow->new(-width => 200, -height => 150); my $dumpText = $dumpWindow->Scrolled('Text')->pack(@pl); my $cmdClose = $dumpWindow->Button(-text => 'Close', -command => sub{$dumpWindow->destroy()})->pack(@pl); $dumpText->configure(-wrap => 'word'); $dumpText->insert('end',"Error opening file $copyLibFileName: $!\n") unless open(INPUT, $copyLibFP); while() { $dumpText->insert('end', "$_\n"); } close(INPUT); } # ### Check Files button ### Make sure all specified files exist on the system. sub checkFiles { my $fname; my $dname; my $msg = ""; $fileCheck = 0; foreach $dname ($txtCopyLibDir, $txtDataDir) { if(length($dname->get()) < 1) { $fileCheck = 1; $msg .= "No file specified\n"; } else { if(stat($dname->get())) { $dname->configure(-foreground => 'black'); } else { $dname->configure(-foreground => 'red'); $msg .= "Some files or directories were not found, and are highlighted in red.\n"; $fileCheck = 1; } } } if(length($txtCopyLibFileName->get()) < 1) { $fileCheck = 1; $msg .= "No Copylib file specified\n"; } else { if(stat($txtCopyLibDir->get()."/".$txtCopyLibFileName->get())) { $txtCopyLibFileName->configure(-foreground => 'black'); } else { $txtCopyLibFileName->configure(-foreground => 'red'); $msg .= "Copylib file or directory not found (highlighted in red).\n"; $fileCheck = 1; } } if(length($txtDataFileName->get()) < 1) { $fileCheck = 1; $msg .= "No Data file specified\n"; } else { if(stat($txtDataDir->get()."/".$txtDataFileName->get())) { $txtDataFileName->configure(-foreground => 'black'); } else { $txtDataFileName->configure(-foreground => 'red'); $msg .= "Data file or directory not found (highlighted in red).\n"; $fileCheck = 1; } } if($fileCheck == 1) { my $mbResp = $main->messageBox(-message => $msg, -type => 'OK'); } return $fileCheck; } # ### Copy the value from "record01" when the "same as" box is checked. sub sameRecClick { (my $checkBox, my $fromBox, my $toBox) = @_; $toBox->delete(0,1000); if($checkBox->{Value}) { $toBox->insert(0,$fromBox->get()); $toBox->configure(-state => 'disabled', -background => 'gray'); } else { $toBox->configure(-state => 'normal', -background => 'white'); } } # # ### Process clicks for the chkrec01OnCopylib checkbox sub rec01OnCopylib { (my $checkBox, my $textBox) = @_; if($checkBox->{Value}) { $textBox->delete(0,1000); $rec01OnCopylib = 1; $textBox->configure(-state => 'disabled', -foreground => 'gray', -background => 'gray'); } else { $rec01OnCopylib = 0; $textBox->configure(-state => 'normal', -foreground => 'black', -background => 'white'); } } # # ### Download files from the Mainframe ######################################################## sub runDownload { (my $txtUserID, my $txtPassword, my $mfFileName, my $locFileName, my $isPDS) = @_; my $msg = ""; $userID = $txtUserID->get(); $password = $txtPassword->get(); if($userID eq "") { $msg = "Please enter your User ID"; my $mbResp = $dnWindow->messageBox(-message => $msg, -type => 'OK'); return; } if($password eq "") { $msg = "Password is required"; my $mbResp = $dnWindow->messageBox(-message => $msg, -type => 'OK'); return; } if($isPDS == 1) { $mfFileName = "'".$copyLibPDSName."(".$mfFileName.")'"; } else { $mfFileName = "'".$mfFileName."'"; } my $textProgress = $dnWindow->Text()->pack(@pl); $btnDownload->destroy(); $btnCancel->destroy(); $dnWindow->focusNext(); open(PDNCMD, "getafile.pl $userID $password $mfFileName \"$locFileName\" |"); # open(PDNCMD, "c:\\temp\\getafile.pl $userID $password $mfFileName \"$locFileName\" |"); # open(PDNCMD, "dir *.txt|"); while() { $textProgress->insert('end', $_); $dnWindow->focusNext(); } $btnCancel = $dnWindow->Button(-text => 'Close', -command => sub{$dnWindow->destroy()})->pack(@pl); } # # #################################### ### Create a CSV file from the data file, based on information in the copylib sub makeCSV { (my $dataDir, my $copyLibDir, my $copyLib, my $dataFile, my $rec01, my $recName, my $outFile, my $FDFlag, my $CSVHeader) = @_; my $dataPSpec = $dataDir."/".$dataFile; my $copyLibPSpec = $copyLibDir."/".$copyLib; my $outPSpec = $dataDir."/".$outFile; my $FDName = $recName; my $recordLine = ""; my $cobio; open(DATAFILEHANDLE, $dataPSpec); if($rec01OnCopylib) { # $cobio = COBOLIO->new($copyLibPath, $FDName, $FDFlag); $cobio = COBOLIO->new($copyLibPSpec, $FDName, 1); } else { # $cobio = COBOLIO->new($copyLibPath, $FDName, $FDFlag, $rec01); $cobio = COBOLIO->new($copyLibPSpec, $FDName, 1, $rec01); } $cobio->PrintLayouts($FDName); open(OUTPUTFILEHANDLE, ">$outPSpec"); # $recordLine = ; # $cobio->ReadRecInto($recordLine, $FDName); my $headerLine; if($CSVHeader) { if($rec01) { $headerLine = $cobio->GetCSVHeader($FDName); } else { $headerLine = $cobio->GetCSVHeader($FDName, $recName); } print OUTPUTFILEHANDLE "$headerLine\n"; } my $limit = 0; while() { $cobio->ReadRecInto($_, $FDName); if($rec01) { $recordLine = $cobio->GetCSVRecord($FDName); } else { $recordLine = $cobio->GetCSVRecord($FDName, $recName); } print OUTPUTFILEHANDLE "$recordLine\n"; # if($limit > 2) { last; } $limit++; } close(OUTPUTFILEHANDLE); close(DATAFILEHANDLE); system($outPSpec); } # ############################################################################################### ######## End of Event Handlers ######## #