1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
|
/*
|| ----------------------------------------------------------------------------
||
|| HETGET.C (c) Copyright Leland Lucius, 2000-2001
|| Released under terms of the Q Public License.
||
|| Extract files from an HET file
||
|| ----------------------------------------------------------------------------
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include "hetlib.h"
#include "sllib.h"
#include "hercules.h"
/*
|| Local volatile data
*/
#define O_NL 0x80
#define O_ASCII 0x40
#define O_STRIP 0x20
#define O_UNBLOCK 0x10
struct
{
char *ifile;
char *ofile;
int fileno;
int lrecl;
int blksize;
unsigned char flags;
unsigned char recfm;
}
opts =
{
NULL,
0,
0,
0,
0,
0,
};
/*
|| Local constant data
*/
static const char help[] =
"%s - Extract files from an HET file\n\n"
"Usage: %s [options] hetfile outfile fileno [recfm lrecl blksize]\n\n"
"Options:\n"
" -a convert to ASCII (implies -u)\n"
" -h display usage summary\n"
" -n file is an NL (or BLP like) tape\n"
" -u unblock (removes BDWs and RDWs if RECFM=V)\n"
" -s strip trailing blanks (requires -a)\n";
/*
|| Valid record formats
*/
#define O_UNDEFINED 0x80
#define O_FIXED 0x40
#define O_VARIABLE 0x20
#define O_BLOCKED 0x08
#define O_SPANNED 0x04
static const struct
{
char *recfm;
int fmt;
}
valfm[] =
{
{ "U", O_UNDEFINED | 0 | 0 },
{ "UA", O_UNDEFINED | 0 | 0 },
{ "UM", O_UNDEFINED | 0 | 0 },
{ "F", O_FIXED | 0 | 0 },
{ "FA", O_FIXED | 0 | 0 },
{ "FM", O_FIXED | 0 | 0 },
{ "FB", O_FIXED | O_BLOCKED | 0 },
{ "FBA", O_FIXED | O_BLOCKED | 0 },
{ "FBM", O_FIXED | O_BLOCKED | 0 },
{ "FS", O_FIXED | O_BLOCKED | 0 },
{ "FSA", O_FIXED | O_BLOCKED | 0 },
{ "FSM", O_FIXED | O_BLOCKED | 0 },
{ "FBS", O_FIXED | O_BLOCKED | 0 },
{ "FBSA", O_FIXED | O_BLOCKED | 0 },
{ "FBSM", O_FIXED | O_BLOCKED | 0 },
{ "V", O_VARIABLE | 0 | 0 },
{ "VA", O_VARIABLE | 0 | 0 },
{ "VM", O_VARIABLE | 0 | 0 },
{ "VB", O_VARIABLE | O_BLOCKED | 0 },
{ "VBA", O_VARIABLE | O_BLOCKED | 0 },
{ "VBM", O_VARIABLE | O_BLOCKED | 0 },
{ "VS", O_VARIABLE | 0 | O_SPANNED },
{ "VSA", O_VARIABLE | 0 | O_SPANNED },
{ "VSM", O_VARIABLE | 0 | O_SPANNED },
{ "VBS", O_VARIABLE | O_BLOCKED | O_SPANNED },
{ "VBSA", O_VARIABLE | O_BLOCKED | O_SPANNED },
{ "VBSM", O_VARIABLE | O_BLOCKED | O_SPANNED },
};
#define VALFMCNT ( sizeof( valfm ) / sizeof( valfm[ 0 ] ) )
/*
|| Block and record management
*/
unsigned char *blkptr = NULL;
int blkidx = 0;
int blklen = 0;
unsigned char *recptr = NULL;
int recidx = 0;
int reclen = 0;
#ifdef EXTERNALGUI
/*
|| Special flag to indicate whether or not we're being
|| run under the control of the external GUI facility.
*/
static int extgui = 0;
/*
|| Previously reported file position
*/
static long prevpos = 0;
/*
|| Report progress every this many bytes
*/
#define PROGRESS_MASK (~0x3FFFF /* 256K */)
#endif /*EXTERNALGUI*/
/*
|| Merge DCB information from HDR2 label
*/
void
merge( SLLABEL *lab )
{
SLFMT fmt;
int i;
/*
|| Make the label more managable
*/
sl_fmtlab( &fmt, lab );
/*
|| Merge the record format;
*/
if( opts.recfm == 0 )
{
opts.recfm = O_UNDEFINED;
for( i = 0 ; i < VALFMCNT ; i++ )
{
if( strcasecmp( fmt.slds2.recfm, valfm[ i ].recfm ) == 0 )
{
opts.recfm = valfm[ i ].fmt;
break;
}
}
}
/*
|| Merge in the record length
*/
if( opts.lrecl == 0 )
{
opts.lrecl = atoi( fmt.slds2.lrecl );
}
/*
|| Merge in the block size
*/
if( opts.blksize == 0 )
{
/*
|| Try the blksize field first
*/
opts.blksize = atoi( fmt.slds2.blksize );
if( opts.blksize == 0 )
{
/*
|| Still zero, so try the lblkln field
*/
opts.blksize = atoi( fmt.slds2.lblkln );
}
}
/*
|| Locate final RECFM string
*/
for( i = 0 ; i < VALFMCNT ; i++ )
{
if( strcasecmp( fmt.slds2.recfm, valfm[ i ].recfm ) == 0 )
{
break;
}
}
/*
|| Print DCB attributes
*/
printf( "DCB Attributes used:\n" );
printf( " RECFM=%-4.4s LRECL=%-5.5d BLKSIZE=%d\n",
valfm[ i ].recfm,
opts.lrecl,
opts.blksize );
return;
}
/*
|| Return block length from BDW
*/
int
bdw_length( const unsigned char *ptr )
{
unsigned int len;
/*
|| Extended format BDW?
*/
if( ptr[ 0 ] & 0x80 )
{
/*
|| Length is 31 bits
*/
len = ptr[ 0 ] << 24;
len += ptr[ 1 ] << 16;
len += ptr[ 2 ] << 8;
len += ptr[ 3 ];
len &= 0x7fffffff;
}
else
{
/*
|| Length is 15 bits
*/
len = ptr[ 0 ] << 8;
len += ptr[ 1 ];
}
return( len );
}
/*
|| Return record length from RDW
*/
int
rdw_length( const unsigned char *ptr )
{
unsigned int len;
/*
|| Get the record length
*/
len = ptr[ 0 ] << 8;
len += ptr[ 1 ];
return( len );
}
/*
|| Retrieves a block from the tape file and resets variables
*/
int
getblock( HETB *hetb )
{
int rc;
/*
|| Read a block from the tape
*/
rc = het_read( hetb, blkptr );
if( rc < 0 )
{
return( rc );
}
/*
|| Save the block length (should we use BDW for RECFM=V files???)
*/
blklen = rc;
return( rc );
}
/*
|| Retrieve logical records from the tape - doesn't handle SPANNED records
*/
int
getrecord( HETB *hetb )
{
int rc;
/*
|| Won't be null if we've been here before
*/
if( recptr != NULL )
{
recidx += reclen;
}
/*
|| Need a new block first time through or we've exhausted current block
*/
if( ( recptr == NULL ) || ( recidx >= blklen ) )
{
/*
|| Go get another block
*/
rc = getblock( hetb );
if( rc < 0 )
{
return( rc );
}
/*
|| For RECFM=V, bump index past BDW
*/
recidx = 0;
if( opts.recfm & O_VARIABLE )
{
recidx = 4;
}
}
/*
|| Set the new record pointer
*/
recptr = &blkptr[ recidx ];
/*
|| Set the record length depending on record type
*/
if( opts.recfm & O_FIXED )
{
reclen = opts.lrecl;
}
else if( opts.recfm & O_VARIABLE )
{
reclen = rdw_length( recptr );
}
else
{
reclen = blklen;
}
return( reclen );
}
/*
|| Retrieve and validate a standard label
*/
int
get_sl( HETB *hetb, SLLABEL *lab )
{
int rc;
/*
|| Read a block
*/
rc = het_read( hetb, blkptr );
if( rc >= 0 )
{
/*
|| Does is look like a standard label?
*/
if( sl_islabel( lab, blkptr, rc ) == TRUE )
{
return( 0 );
}
}
else
{
printf( "%s while reading block\n", het_error( rc ) );
}
return( -1 );
}
/*
|| Extract the file from the tape
*/
int
getfile( HETB *hetb, FILE *outf )
{
SLFMT fmt;
SLLABEL lab;
unsigned char *ptr;
int fileno;
int rc;
/*
|| Skip to the desired file
*/
if( opts.flags & O_NL )
{
/*
|| For NL tapes, just use the specified file number
*/
fileno = opts.fileno;
/*
|| Start skipping
*/
while( --fileno )
{
/*
|| Forward space to beginning of next file
*/
rc = het_fsf( hetb );
if( rc < 0 )
{
printf( "%s while positioning to file #%d\n",
het_error( rc ),
opts.fileno );
return( rc );
}
}
}
else
{
/*
|| First block should be a VOL1 record
*/
rc = get_sl( hetb, &lab );
if( rc < 0 || !sl_isvol( &lab, 1 ) )
{
printf( "Expected VOL1 label\n" );
return( -1 );
}
/*
|| For SL, adjust the file # so we end up on the label before the data
*/
fileno = ( opts.fileno * 3 ) - 2;
/*
|| Start skipping
*/
while( --fileno )
{
/*
|| Forward space to beginning of next file
*/
rc = het_fsf( hetb );
if( rc < 0 )
{
printf( "%s while positioning to file #%d\n",
het_error( rc ),
opts.fileno );
return( rc );
}
}
/*
|| Get the HDR1 label.
*/
rc = get_sl( hetb, &lab );
if( rc < 0 || !sl_ishdr( &lab, 1 ) )
{
printf( "Expected HDR1 label\n" );
return( -1 );
}
/*
|| Make the label more managable
*/
sl_fmtlab( &fmt, &lab );
printf("File Info:\n DSN=%-17.17s\n", fmt.slds1.dsid );
/*
|| Get the HDR2 label.
*/
rc = get_sl( hetb, &lab );
if( rc < 0 || !sl_ishdr( &lab, 2 ) )
{
printf( "Expected HDR2 label\n" );
return( -1 );
}
/*
|| Merge the DCB information
*/
merge( &lab );
/*
|| Hop over the tapemark
*/
rc = het_fsf( hetb );
if( rc < 0 )
{
printf( "%s while spacing to start of data\n",
het_error( rc ) );
return( rc );
}
}
/*
|| Different processing when converting to ASCII
*/
if( opts.flags & ( O_ASCII | O_UNBLOCK ) )
{
/*
|| Get a record
*/
while( ( rc = getrecord( hetb ) ) >= 0 )
{
#ifdef EXTERNALGUI
if( extgui )
{
/* Report progress every nnnK */
long curpos = ftell( hetb->fd );
if( ( curpos & PROGRESS_MASK ) != ( prevpos & PROGRESS_MASK ) )
{
prevpos = curpos;
fprintf( stderr, "IPOS=%ld\n", curpos );
}
}
#endif /*EXTERNALGUI*/
/*
|| Get working copy of record ptr
*/
ptr = recptr;
/*
|| Only want data portion for RECFM=V records
*/
if( opts.recfm & O_VARIABLE )
{
ptr += 4;
rc -= 4;
}
/*
|| Convert record to ASCII
*/
if( opts.flags & O_ASCII )
{
sl_etoa( NULL, ptr, rc );
}
/*
|| Strip trailing blanks
*/
if( opts.flags & O_STRIP )
{
while( ptr[ rc - 1 ] == ' ' )
{
rc--;
}
}
/*
|| Write the record out
*/
fwrite( ptr, rc, 1, outf );
/*
|| Put out a linefeed when converting
*/
if( opts.flags & O_ASCII )
{
fwrite( "\n", 1, 1, outf );
}
}
}
else
{
/*
|| Get a record
*/
while( ( rc = getblock( hetb ) ) >= 0 )
{
#ifdef EXTERNALGUI
if( extgui )
{
/* Report progress every nnnK */
long curpos = ftell( hetb->fd );
if( ( curpos & PROGRESS_MASK ) != ( prevpos & PROGRESS_MASK ) )
{
prevpos = curpos;
fprintf( stderr, "IPOS=%ld\n", curpos );
}
}
#endif /*EXTERNALGUI*/
/*
|| Write the record out
*/
fwrite( blkptr, blklen, 1, outf );
}
}
return( rc );
}
/*
|| Prints usage information
*/
void
usage( char *name )
{
printf( help, name, name );
}
/*
|| Standard main
*/
int
main( int argc, char *argv[] )
{
HETB *hetb;
FILE *outf;
int rc;
int i;
/* Display the program identification message */
display_version (stderr, "Hercules HET extract files program ");
#ifdef EXTERNALGUI
if (argc >= 1 && strncmp(argv[argc-1],"EXTERNALGUI",11) == 0)
{
extgui = 1;
argc--;
}
#endif /*EXTERNALGUI*/
/*
|| Process option switches
*/
while( TRUE )
{
rc = getopt( argc, argv, "abhnsu" );
if( rc == -1 )
{
break;
}
switch( rc )
{
case 'a':
opts.flags |= O_ASCII;
break;
case 'h':
usage( argv[ 0 ] );
exit( 1 );
break;
case 'n':
opts.flags |= O_NL;
break;
case 's':
opts.flags |= O_STRIP;
break;
case 'u':
opts.flags |= O_UNBLOCK;
break;
default:
usage( argv[ 0 ] );
exit( 1 );
break;
}
}
/*
|| Calc number of non-switch arguments
*/
argc -= optind;
/*
|| We must have at least the first 3 parms
*/
if( argc < 3 )
{
printf( "Must specify input tape, output file, and file #\n" );
exit( 1 );
}
opts.ifile = argv[ optind ];
opts.ofile = argv[ optind + 1 ];
opts.fileno = atoi( argv[ optind + 2 ] );
if( opts.fileno == 0 || opts.fileno > 9999 )
{
printf( "File number must be within 1-9999\n" );
exit( 1 );
}
/*
|| If NL tape, then we require the DCB attributes
*/
if( opts.flags & O_NL )
{
if( argc != 6 )
{
printf( "DCB attributes required for NL tapes\n" );
exit( 1 );
}
}
/*
|| If specified, get the DCB attributes
*/
if( argc > 3 )
{
/*
|| Must have only three
*/
if( argc != 6 )
{
usage( argv[ 0 ] );
exit( 1 );
}
/*
|| Lookup the specified RECFM in our table
*/
opts.recfm = 0;
for( i = 0 ; i < VALFMCNT ; i++ )
{
if( strcasecmp( argv[ optind + 3 ], valfm[ i ].recfm ) == 0 )
{
opts.recfm = valfm[ i ].fmt;
break;
}
}
/*
|| If we didn't find a match, show the user what the valid ones are
*/
if( opts.recfm == 0)
{
/*
|| Dump out the valid RECFMs
*/
printf( "Valid record formats are:\n" );
for( i = 0 ; i < VALFMCNT ; i++ )
{
printf( " %-4.4s", valfm[ i ].recfm );
if( ( ( i + 1 ) % 3 ) == 0 )
{
printf( "\n" );
}
}
exit( 1 );
}
/*
|| Get the record length
*/
opts.lrecl = atoi( argv[ optind + 4 ] );
/*
|| Get and validate the blksize
*/
opts.blksize = atoi( argv[ optind + 5 ] );
if( opts.blksize == 0 )
{
printf( "Block size can't be zero\n" );
exit( 1 );
}
}
/*
|| Open the tape file
*/
rc = het_open( &hetb, opts.ifile, 0 );
if( rc >= 0 )
{
/*
|| Get memory for the tape buffer
*/
blkptr = malloc( HETMAX_BLOCKSIZE );
if( blkptr != NULL )
{
/*
|| Open the output file
*/
outf = fopen( opts.ofile, "wb" );
if( outf != NULL )
{
/*
|| Go extract the file from the tape
*/
rc = getfile( hetb, outf );
/*
|| Close the output file
*/
fclose( outf );
}
/*
|| Free the buffer memory
*/
free( blkptr );
}
}
else
{
printf( "het_open() returned %s\n", het_error( rc ) );
}
/*
|| Close the tape file
*/
het_close( &hetb );
return 0;
}
|