| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | binhex.pl - use Convert::BinHex to encode files as BinHex | ||
| 7 | |||
| 8 | |||
| 9 | =head1 USAGE | ||
| 10 | |||
| 11 | Usage: | ||
| 12 | |||
| 13 | binhex.pl [options] file ... file | ||
| 14 | |||
| 15 | Where the options are: | ||
| 16 | |||
| 17 | -o dir Output in given directory (default outputs in file's directory) | ||
| 18 | -v Verbose output (normally just one line per file is shown) | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | Each file is converted to file.hqx. | ||
| 23 | |||
| 24 | |||
| 25 | =head1 WARNINGS | ||
| 26 | |||
| 27 | Largely untested. | ||
| 28 | |||
| 29 | |||
| 30 | =head1 AUTHOR | ||
| 31 | |||
| 32 | Paul J. Schinder (NASA/GSFC) mostly, though Eryq can't seem to keep | ||
| 33 | his grubby paws off anything... | ||
| 34 | |||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | use lib "./lib"; | ||
| 39 | |||
| 40 | use Getopt::Std; | ||
| 41 | use Convert::BinHex; | ||
| 42 | use POSIX; | ||
| 43 | use Fcntl; | ||
| 44 | use File::Basename; | ||
| 45 | use Carp; | ||
| 46 | require Mac::Files if (($^O||'') eq "MacOS"); | ||
| 47 | |||
| 48 | our $VERSION = '1.125'; # VERSION | ||
| 49 | |||
| 50 | use strict; | ||
| 51 | use vars qw( | ||
| 52 | $opt_o | ||
| 53 | $opt_v | ||
| 54 | ); | ||
| 55 | |||
| 56 | my $DEBUG = 0; | ||
| 57 | |||
| 58 | #------------------------------------------------------------ | ||
| 59 | # main | ||
| 60 | #------------------------------------------------------------ | ||
| 61 | sub main { | ||
| 62 | |||
| 63 | # What usage? | ||
| 64 | @ARGV or usage(); | ||
| 65 | getopts('o:v'); | ||
| 66 | $DEBUG = $opt_v; | ||
| 67 | |||
| 68 | # Process files: | ||
| 69 | my $file; | ||
| 70 | foreach $file (@ARGV) { | ||
| 71 | binhex($file); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | exit(&main ? 0 : -1); | ||
| 75 | |||
| 76 | #------------------------------------------------------------ | ||
| 77 | # usage | ||
| 78 | #------------------------------------------------------------ | ||
| 79 | # Get usage from me. | ||
| 80 | |||
| 81 | sub usage { | ||
| 82 | my $msg = shift || ''; | ||
| 83 | my $usage = ''; | ||
| 84 | if (open(USAGE, "<$0")) { | ||
| 85 | while ($_ = <USAGE> and !/^=head1 USAGE/) {}; | ||
| 86 | while ($_ = <USAGE> and !/^=head1/) {$usage .= $_}; | ||
| 87 | close USAGE; | ||
| 88 | } | ||
| 89 | else { | ||
| 90 | $usage = "Usage unavailable; please see the script itself."; | ||
| 91 | } | ||
| 92 | print STDERR "\n$msg$usage"; | ||
| 93 | exit -1; | ||
| 94 | } | ||
| 95 | |||
| 96 | #------------------------------------------------------------ | ||
| 97 | # binhex FILE | ||
| 98 | #------------------------------------------------------------ | ||
| 99 | # Encode the given FILE. | ||
| 100 | # | ||
| 101 | sub binhex { | ||
| 102 | my $inpath = shift || die "No filename given $!"; | ||
| 103 | local *BHEX; | ||
| 104 | my ($has, $dlength, $rlength, $finfo, $flags); | ||
| 105 | |||
| 106 | # Create new BinHex interface: | ||
| 107 | my $hqx = Convert::BinHex->new; | ||
| 108 | |||
| 109 | # Get input directory/filename: | ||
| 110 | my ($inname, $indir) = fileparse($inpath); | ||
| 111 | die "filename $inname too long!" if ((length($inname)+4) > 31); | ||
| 112 | $hqx->filename($inname); | ||
| 113 | |||
| 114 | # Set up output directory/filename: | ||
| 115 | my $outname = "$inname.hqx"; | ||
| 116 | my $outdir = $opt_o || $indir; | ||
| 117 | my $outpath = "$outdir/$outname"; $outpath =~ s{/+}{/}g; | ||
| 118 | |||
| 119 | # If we're on a Mac, we can get the real resource info: | ||
| 120 | if ($^O||'' eq "MacOS") { | ||
| 121 | |||
| 122 | # Get and set up type, creator, flags: | ||
| 123 | $has = Mac::Files::FSpGetCatInfo($inpath); | ||
| 124 | $finfo = $has->{ioFlFndrInfo}; | ||
| 125 | $dlength = $has->{ioFlLgLen}; | ||
| 126 | $rlength = $has->{ioFlRLgLen}; | ||
| 127 | $hqx->type($finfo->{fdType}); | ||
| 128 | $hqx->creator($finfo->{fdCreator}); | ||
| 129 | $hqx->flags($finfo->{fdFlags} & 0xfeff); # turn off inited bit | ||
| 130 | |||
| 131 | # Set up data fork: | ||
| 132 | $hqx->data(Path=>$inpath); | ||
| 133 | $hqx->data->length($dlength); | ||
| 134 | |||
| 135 | # Set up resource fork: | ||
| 136 | $hqx->resource(Path=>$inpath, Fork => "RSRC"); | ||
| 137 | $hqx->resource->length($rlength); | ||
| 138 | } | ||
| 139 | else { # not a Mac: fake it... | ||
| 140 | # Set up data fork: | ||
| 141 | $hqx->data(Path => $inpath); | ||
| 142 | $dlength = (-s $inpath); | ||
| 143 | |||
| 144 | # Set up resource fork: | ||
| 145 | if (-e "$inpath.rsrc") { | ||
| 146 | $hqx->resource(Path => "$inpath.rsrc"); | ||
| 147 | $rlength = (-s "$inpath.rsrc"); | ||
| 148 | } | ||
| 149 | else { | ||
| 150 | $hqx->resource(Data => ''); | ||
| 151 | $rlength = 0; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | # Ready! | ||
| 156 | print "BinHexing: $inpath\n"; | ||
| 157 | if ($DEBUG) { | ||
| 158 | print " Resource size: $rlength\n" if defined($rlength); | ||
| 159 | print " Data size: $dlength\n" if defined($dlength); | ||
| 160 | } | ||
| 161 | open BHEX, ">$outpath" or croak("Unable to open $outpath"); | ||
| 162 | $hqx->encode(\*BHEX); | ||
| 163 | close BHEX; | ||
| 164 | print "Wrote: $outpath\n"; | ||
| 165 | } | ||
| 166 | #------------------------------------------------------------ | ||
| 167 | 1; | ||
| 168 | |||
| 169 | |||
| 170 | |||
| 171 |