#!/usr/bin/perl # Copyright (C) 2000-2001 Knut Haugen # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # version 0.7 #Description: # update or read a jar's manifest file in a simple manner use strict; use Getopt::Std; # the known keys my ($TITLE) = 'Implementation-Title'; my ($VENDOR) = 'Implementation-Vendor'; my ($VERSION) = 'Implementation-Version'; my ($NAME) = 'Name'; # the files we use my ($MF_NAME) = 'META-INF/MANIFEST.MF'; my ($MF) = 'META-INF'; my ($MF_TMP) = 'manifest.txt'; #---------------------------------------- #main script # #---------------------------------------- # the jar executable my ($jar) = `which jar 2> /dev/null`; if (checkExit()){ chop ($jar); } else{ print ("Couldn't find the jar executable.\n"); printUsage (-1); } my ($only) = 0; if ($#ARGV == 0 ){ $only = 1; } my (%opts, $jarfile, %mf, @file); getopts ("t:v:s:n:ph", \%opts) || printUsage (1); if ( $opts{'h'}){ printUsage(0); } if ( $#ARGV == 0){ $jarfile = $ARGV[0]; getManifest (); updateManifest(); } else{ print ("No jar file supplied.\n"); printUsage (-1); } #print usage information sub printUsage ( ){ my ($exit) = shift; my $usage = < Set information for the Implementation-Title entry -v Set information for the Implementation-Vendor entry -s Set information for the Implementation-Version entry -n Set information for the Name entry -p flag to specifically tell the script to just print existing values. Also default if no option is given. -h Print this help and exit. EOF print ($usage); exit ($exit); } #update the manifest in the jar with the new information sub updateManifest { # update all entries if given on the cmdline if (exists($opts{'t'})){ $mf{$TITLE} = $opts{'t'}; } if (exists($opts{'v'})){ $mf{$VENDOR} = $opts{'v'}; } if (exists($opts{'s'})){ $mf{$VERSION} = $opts{'s'}; } if (exists($opts{'n'})){ $mf{$NAME} = $opts{'n'}; } open (JAR, "> $MF_TMP") or die ("Could not open tmp manifest-file for writing\n");; print (JAR "$NAME: $mf{$NAME}\n"); print (JAR "$TITLE: $mf{$TITLE}\n"); if ($mf{$VENDOR} =~ /\"/){ print (JAR "$VENDOR: $mf{$VENDOR}\n"); } else{ print (JAR "$VENDOR: \"$mf{$VENDOR}\"\n"); } print (JAR "$VERSION: $mf{$VERSION}\n\n"); print (JAR @file); close (JAR); makeNewjar (); } #update the jar with the new Manifest information sub makeNewjar (){ `$jar -ufm $jarfile $MF_TMP`; die ("Jar $jar not working properly. Is it in the path?") unless checkExit (); unlink ($MF_TMP); unlink ($MF_NAME); rmdir ($MF); } # get the manifest-file, well, really get the manifest sub getManifest{ if (-f $MF_NAME ){ unlink ($MF_NAME); } if (-f $jarfile) { `$jar -xf $jarfile $MF_NAME`; die ("Jar $jar not working properly. Is it in the path?") unless checkExit (); open (JAR, $MF_NAME) or die ("Could not open manifest file"); my (@lines) = ; close (JAR); parseManifest ( @lines ); if ($opts{'p'} || $only){ unlink ($MF_NAME); rmdir ($MF); exit (0); } } } #check the exit status of the last process. Returns true for #exit 0 and false for all others. sub checkExit (){ return ($?>>8) == 0; } #Parse the manifest file and assign the values to the hash. sub parseManifest { my ($test, $test2) = (0, 0); foreach (@_){ $test=0; if (!/^\s/){ my ($key, $value) = split (/:/, $_); $value = substr($value, 1); if ($opts{'p'} || $only){ print ("$key: $value"); } if ($key =~ /Vendor/ || $key =~ /Version/ || $key =~ /Title/){ chop ($value); $test=1; } if ($key =~ /Name/ && !$test2){ chop ($value); $test=1; $test2=1; } if ($test){ $mf{$key} = $value; } elsif (!/^Created-By.*/){ push (@file, $_); } } else{ push (@file, $_); } } } #pod people __END__ =head1 Name mf.pl - read and edit a jar's manifest file =head1 Description This script gives you easy access to the meta information in a jar file. No need to unpack any files and cat them. The script does this under the hood and prints the information. It also lets you set some of the values in the jar file. =head1 Usage mf.pl [OPTIONS] JARFILE =head1 Options -h Print usage information and exit. -t Set the title in the manifest -v Set the vendor information in the manifest -s Set the version information in the manifest -n Set the name information in the manifest -p Only print the information in the manifest. This is the default behaviour if no option is given as well. =head1 Author Written by Knut Haugen, Linpro AS, . Copyright 2000-2001, Knut Haugen. Email: . =cut