package Autom4te::General;
=head1 NAME
Autom4te::General - general support functions for Autoconf and Automake
=head1 SYNOPSIS
use Autom4te::General
=head1 DESCRIPTION
This perl module provides various general purpose support functions
used in several executables of the Autoconf and Automake packages.
=cut
use 5.005_03;
use Exporter;
use Autom4te::ChannelDefs;
use Autom4te::Channels;
use File::Basename;
use File::Path ();
use File::stat;
use IO::File;
use Carp;
use strict;
use vars qw (@ISA @EXPORT);
@ISA = qw (Exporter);
my @export_vars =
qw ($debug $force $help $me $tmp $verbose $version);
my @export_subs =
qw (&debug
&getopt &mktmpdir
&uniq);
my @export_forward_subs =
qw (&basename &dirname &fileparse);
@EXPORT = (@export_vars, @export_subs, @export_forward_subs);
=head2 Global Variables
=over 4
=item C<$debug>
Set this variable to 1 if debug messages should be enabled. Debug
messages are meant for developpers only, or when tracking down an
incorrect execution.
=cut
use vars qw ($debug);
$debug = 0;
=item C<$force>
Set this variable to 1 to recreate all the files, or to consider all
the output files are obsolete.
=cut
use vars qw ($force);
$force = undef;
=item C<$help>
Set to the help message associated to the option C<--help>.
=cut
use vars qw ($help);
$help = undef;
=item C<$me>
The name of this application, as should be used in diagostic messages.
=cut
use vars qw ($me);
$me = basename ($0);
=item C<$tmp>
The name of the temporary directory created by C<mktmpdir>. Left
C<undef> otherwise.
=cut
use vars qw ($tmp);
$tmp = undef;
=item C<$verbose>
Enable verbosity messages. These messages are meant for ordinary
users, and typically make explicit the steps being performed.
=cut
use vars qw ($verbose);
$verbose = 0;
=item C<$version>
Set to the version message associated to the option C<--version>.
=cut
use vars qw ($version);
$version = undef;
=back
=cut
=head2 Functions
=over 4
=item C<END>
Filter Perl's exit codes, delete any temporary directory (unless
C<$debug>), and exit nonzero whenever closing C<STDOUT> fails.
=cut
sub END
{
my $status = $?;
$status = 1 if ($! && $! == $?) || $? == 255;
if (!$debug && defined $tmp && -d $tmp)
{
local $SIG{__WARN__} = sub { $status = 1; warn $_[0] };
File::Path::rmtree $tmp;
}
if (! close STDOUT)
{
print STDERR "$me: closing standard output: $!\n";
$? = 1;
return;
}
$? = $status;
}
=item C<debug (@message)>
If the debug mode is enabled (C<$debug> and C<$verbose>), report the
C<@message> on C<STDERR>, signed with the name of the program.
=cut
sub debug (@)
{
print STDERR "$me: ", @_, "\n"
if $verbose && $debug;
}
=item C<getopt (%option)>
Wrapper around C<Getopt::Long>. In addition to the user C<option>s,
support C<-h>/C<--help>, C<-V>/C<--version>, C<-v>/C<--verbose>,
C<-d>/C<--debug>, C<-f>/C<--force>. Conform to the GNU Coding
Standards for error messages. Try to work around a weird behavior
from C<Getopt::Long> to preserve C<-> as an C<@ARGV> instead of
rejecting it as a broken option.
=cut
sub getopt (%)
{
my (%option) = @_;
use Getopt::Long;
my $stdin = grep /^-$/, @ARGV;
@ARGV = grep !/^-$/, @ARGV;
%option = ("h|help" => sub { print $help; exit 0 },
"V|version" => sub { print $version; exit 0 },
"v|verbose" => sub { ++$verbose },
"d|debug" => sub { ++$debug },
'f|force' => \$force,
%option);
Getopt::Long::Configure ("bundling", "pass_through");
GetOptions (%option)
or exit 1;
foreach (grep { /^-./ } @ARGV)
{
print STDERR "$0: unrecognized option `$_'\n";
print STDERR "Try `$0 --help' for more information.\n";
exit (1);
}
push @ARGV, '-'
if $stdin;
setup_channel 'note', silent => !$verbose;
setup_channel 'verb', silent => !$verbose;
}
=item C<mktmpdir ($signature)>
Create a temporary directory which name is based on C<$signature>.
Store its name in C<$tmp>. C<END> is in charge of removing it, unless
C<$debug>.
=cut
sub mktmpdir ($)
{
my ($signature) = @_;
my $TMPDIR = $ENV{'TMPDIR'} || '/tmp';
$tmp = `(umask 077 &&
mktemp -d "$TMPDIR/${signature}XXXXXX") 2>/dev/null`;
chomp $tmp;
if (!$tmp || ! -d $tmp)
{
$tmp = "$TMPDIR/$signature" . int (rand 10000) . ".$$";
mkdir $tmp, 0700
or croak "$me: cannot create $tmp: $!\n";
}
print STDERR "$me:$$: working in $tmp\n"
if $debug;
}
=item C<uniq (@list)>
Return C<@list> with no duplicates, keeping only the first
occurrences.
=cut
sub uniq (@)
{
my @res = ();
my %seen = ();
foreach my $item (@_)
{
if (! exists $seen{$item})
{
$seen{$item} = 1;
push (@res, $item);
}
}
return wantarray ? @res : "@res";
}
=item C<handle_exec_errors ($command)>
Display an error message for C<$command>, based on the content of
C<$?> and C<$!>.
=cut
sub handle_exec_errors ($)
{
my ($command) = @_;
$command = (split (' ', $command))[0];
if ($!)
{
error "failed to run $command: $!";
}
else
{
use POSIX qw (WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG);
if (WIFEXITED ($?))
{
my $status = WEXITSTATUS ($?);
$! = 0;
error "$command failed with exit status: $status";
}
elsif (WIFSIGNALED ($?))
{
my $signal = WTERMSIG ($?);
$! = 1;
error "$command terminated by signal: $signal";
}
else
{
error "$command exited abnormally";
}
}
}
=back
=head1 SEE ALSO
L<Autom4te::XFile>
=head1 HISTORY
Written by Alexandre Duret-Lutz E<lt>F<adl@gnu.org>E<gt> and Akim
Demaille E<lt>F<akim@freefriends.org>E<gt>.
=cut
1;