#!/usr/local/bin/perl
#
# $Id: as400-file-layout.pl,v 1.1.1.1 2002/03/01 21:50:52 valerio Exp $
#
# This script is copyrighted
# Copyright (c) 2002 Valerio Di Giampietro
# Valerio@DiGiampietro.com
#
# All rights reserved. This program is free software; you can
# redistribute it and/or modify it under the same terms as Perl
# itself.
#
# Perl/Expect script to download layout file information from the AS/400
#
# $Log: as400-file-layout.pl,v $
# Revision 1.1.1.1  2002/03/01 21:50:52  valerio
# Imported sources
#
#

use Getopt::Std;
require "as400-include.pl";
$|=1;


#
#======================================================================
# Process options
#======================================================================
#
getopts('u:p:l:f:d:x');
if (not ($opt_u and $opt_p and $opt_l and $opt_f)) {
    usage();
    exit;
}

$debug=$opt_d;
$user=$opt_u;
$upuser= uc $user;
$password=$opt_p;
$as400="as400dmit";
$library=$opt_l;
$uplibrary=uc $library;
$file=$opt_f;
$upfile=uc $file;
if ($opt_x) {
    $x3270cmd="x3270 -script -model 2 -color";
} else {
    $x3270cmd="s3270 -model 2";
}
#
#======================================================================
# usage 
#======================================================================
#
sub usage {
    print STDERR "usage: $0 [-d debuglevel] [-x] -u userid -p password -l library -f file\n";
    print STDERR "    debuglevel   a value between 0-9\n";
    print STDERR "    -x           display the x3270 window emulator\n";
}
#
#======================================================================
# ffd2layout   File field description to layout
#              input
#                $sin string output of the x3270_dspffd function
#              returned value
#                string in my own layout format
#                
#======================================================================
#
sub ffd2layout {
    my $sin=$_[0];
    my ($s,$sout,$fname,$firsttime);
    my ($fname,$ft,$flng,$frlen,$ffrm,$fto);
    my %tb = (
	      CHAR     => 'A',
	      PACKED   => 'P',
	      ZONED    => 'S'
	      );
    $sout="";
    my @sin=split("\n",$sin);
    #
    ##### skip to the beginning of field descriptions
    #
    do {
	$s=shift @sin;
    } until ($s=~'Field      Type       Length  Length  Position        Usag');
    #
    ##### extract field definitions
    #
    $firsttime=1;
    while (defined ($s=shift @sin)) {
	if (
	 $s=~/^    (\S.........) (\S........)  (...)(...)  (......)    (......)/
         #   "^    (\PLANN     ) (\PACKED   )  (  5)(  0)  (     3)    (    51)
	) {
	    if (not $firsttime) {
		$sout .= sprintf(" %-10s %-36s %-1s   %+3s %+2s  %+6s %+6s %+4s\n",
				 $fname,$fdescr,$tb{$ft},$flng,$fdc,$frlen,
				 $ffrm,$fto);
		undef $fdescr;
	    } else {
		$firsttime=0;
	    }
	    $fname=$1;  # field name
	    $ft=$2;     # field type
	    $flng=$3;   # field lenght
	    $fdc=$4;    # field num. of decimals
	    $frlen=$5;  # field real (num of bytes) length
	    $ffrm=$6;   # field starting position (from)
	    $fto=$ffrm+$frlen-1; #field ending position (to)
	    $ft=~s/ +$//;
	    if ($flng eq '   ') {
		$flng=$fdc;
		$fdc="";
	    } else {
		$fdc=substr($fdc,-2);
	    }
	} elsif ($s=~/^      Field text  [\. ]+:\s+(.*)$/) {
	    $fdescr=$1;
	    $fdescr=substr($fdescr,0,36);
	}
    }
    $sout .= sprintf(" %-10s %-36s %-1s   %+3s %+3s %+6s %+6s %+4s\n",
		     $fname,$fdescr,$tb{$ft},$flng,$fdc,$frlen,
		     $ffrm,$fto);
    return $sout;
} 
#
#======================================================================
#  Main program 
#======================================================================
#
$x3270 = Expect->spawn($x3270cmd) || 
    die "error spawning x3270\n";
sleep(2);

x3270_connect() || x3270_die("Didn't connect\n");
x3270_login() || x3270_die("Cannot login\n");
$s=x3270_dspffd($library,$file) || x3270_die("Error executing dsptrk\n");
print ffd2layout($s);
exit;





