/usr/share/perl5/LinuxDocTools/Utils.pm is in linuxdoc-tools 0.9.71-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #
# Utils.pm
#
# $Id: Utils.pm,v 1.2 2001/08/31 22:39:44 sano Exp $
#
# Utilities, split off from other modules in order to cut down file size.
#
# © Copyright 1996, 1997, Cees de Groot
#
package LinuxDocTools::Utils;
use strict;
=head1 NAME
LinuxDocTools::Utils - various supporting routines
=head1 SYNOPSIS
@files = process_options (@args);
usage ($msg);
trap_signals;
cleanup;
create_temp($tempfile)
=head1 DESCRIPTION
The B<LinuxDocTools::Utils> module contains a number of generic routines, mainly
split off from the main module in order to keep file size down.
=head1 FUNCTIONS
=over 4
=cut
use DirHandle;
use FileHandle;
use Cwd;
use File::Basename;
use Exporter;
use LinuxDocTools::Vars;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $in_signal);
@ISA = qw(Exporter);
@EXPORT = qw(usage process_options);
@EXPORT_OK = qw(cleanup trap_signals remove_tmpfiles create_temp);
$VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
use subs qw(usage);
# check whether options are unique
sub check_option_consistency
{
my $owner = {};
my ($fmt, $opt);
foreach $fmt (keys %FmtList)
{
my $add = sub { # add to options of $fmt
my $str = shift;
if ($owner->{$str}) {
push(@{$owner->{$str}}, $fmt);
}
else {
$owner->{$str} = [$fmt];
}
};
foreach $opt (@{$Formats{$fmt}{OPTIONS}})
{
&$add("--$opt->{option}");
&$add("-$opt->{short}");
}
}
my $error = 0;
foreach $opt (keys %$owner)
{
if (scalar @{$owner->{$opt}} > 1)
{
warn "duplicate option: $opt in " .
join(', ', @{$owner->{$opt}}) . "\n";
$error = 1;
}
}
die "Internal error detected" if $error;
}
=item process_options
This function processes the command line, and sets the variables associated
with the options along the way. When successful, it returns the arguments
on the command line it didn't interpret. Normally, this will be a list of
filenames.
=cut
sub process_options
{
my @args = @_;
my @retval;
OPTPROC: while ($args[0])
{
my $long;
my $curarg = $args[0];
if ($curarg =~ /^--.*/)
{
#
# Long option, --opt[==value]
#
$long = 1;
}
elsif ($curarg =~ /^-.*/)
{
#
# Short option, -o value
#
$long = 0;
}
else
{
#
# Filename
#
push @retval, $curarg;
next OPTPROC;
}
#
# Start looking for the option
#
foreach my $fmt (keys %FmtList)
{
foreach my $opt (@{$Formats{$fmt}{OPTIONS}})
{
if (($long && $curarg =~ /^--$opt->{option}.*/) ||
$curarg =~ /^-$opt->{short}/)
{
#
# Found it! Get the argument and see whether all is OK
# with the option.
#
my $optval = "";
if ($long)
{
if ($curarg =~ /^--$opt->{option}=.*/)
{
$optval = $curarg;
$optval =~ s/[^=]*=(.*)/$1/;
}
}
else
{
if ($args[1] =~ /^[^-].*/)
{
$optval = $args[1];
}
}
$opt->{type} eq "f" && do
{
#
# "f" -> flag. Increment, so '-v -v' can work.
#
$Formats{$fmt}{$opt->{option}} += 1;
next OPTPROC;
};
#
# All other types require a value (for now).
#
shift @args unless $long;
if ($optval eq "")
{
usage "Option $curarg: value required";
}
($opt->{type} eq "i" || $opt->{type} eq "s") && do
{
#
# "i" -> numeric value.
# "s" -> string value.
#
# No type checking yet...
#
if ($opt->{option} eq "define")
{
$Formats{$fmt}{$opt->{option}} .= " " . $optval;
}
else
{
$Formats{$fmt}{$opt->{option}} = $optval;
}
next OPTPROC;
};
$opt->{type} eq "l" && do
{
#
# "l" -> list of values.
#
foreach my $val (@{$opt->{'values'}})
{
if ($val eq $optval)
{
$Formats{$fmt}{$opt->{option}} = $optval;
next OPTPROC;
}
}
usage "Invalid value '$optval' for '--$opt->{option}'";
};
usage "Unknown option type $opt->{type} in $fmt/$opt";
}
}
}
usage "Unknown option $curarg";
}
continue
{
shift @args;
}
return @retval;
}
=item usage
Prints out a generated help message about calling convention and allowed
options, then the argument string, and finally exits.
=cut
sub usage
{
my ($msg) = @_;
print "LinuxDoc-Tools version " . `cat $main::DataDir/VERSION` . "\n";
check_option_consistency;
print "Usage:\n";
print " " . $global->{myname} . " [options] <infile>\n\n";
my @helplist = sort(keys %Formats);
@helplist = sort (keys %FmtList) if ($global->{format});
foreach my $fmt (@helplist)
{
if ($fmt eq "global")
{
print "General options:\n";
}
else
{
print "Format: " . $fmt . "\n";
}
print $Formats{$fmt}{HELP};
for my $opt (@{$Formats{$fmt}{OPTIONS}})
{
my $value = '';
if ($opt->{type} eq "i")
{
$value = "number";
}
elsif ($opt->{type} eq "l")
{
$value = "{";
my $first = 1;
for my $val (@{$opt->{'values'}})
{
$first || ($value .= ",");
$first = 0;
$value .= $val;
}
$value .= "}";
}
elsif ($opt->{type} eq "s")
{
$value = "string";
}
print " --$opt->{option}"; print "=$value" if $value;
print " -$opt->{short}"; print " $value" if $value;
print "\n";
}
print "\n";
}
$msg && print "Error: $msg\n\n";
exit 1;
}
=item cleanup
This function cleans out all temporary files and exits. The unlink step
is skipped if debugging is turned on.
=cut
sub cleanup
{
my ($signame) = @_;
if( $signame ) {
if ( $in_signal ) {
if( $global->{debug} ) {
print STDERR "Caught SIG$signame during cleanup -- aborting\n";
}
exit -1;
}
else {
if( $global->{debug} ) {
print STDERR "Caught SIG$signame -- cleaning up\n";
}
$in_signal = 1;
}
}
if( !$global->{debug} && $global->{tmpbase} ) {
remove_tmpfiles($global->{tmpbase});
}
exit 0;
}
=item remove_tmpfiles( $tmpbase )
This function cleans out all temporary files, using the argument $tmpbase to
determine the directory and pattern to use to find the temporary files.
=cut
sub remove_tmpfiles($) {
my $tmpbase = shift;
my ($name,$tmpdir) = fileparse($tmpbase,"");
my $namelength = length $name;
my $savdir = cwd;
chdir($tmpdir);
my $dir = new DirHandle(".");
if (!defined($dir) ) {
warn "Couldn't open temp directory $tmpdir: $!\n";
} else {
foreach my $tmpfile ($dir->read()) {
if (substr ($tmpfile, 0, $namelength) eq $name) {
unlink ($tmpfile) || warn "Couldn't unlink $tmpfile: $! \n";
}
}
$dir->close();
}
chdir($savdir);
rmdir($tmpdir) || return -1;
}
=item trap_signals
This function traps all known signals, making sure that the B<cleanup>
function is executed on them. It should be called once at initialization
time.
=cut
sub trap_signals
{
foreach my $sig ( 'HUP', 'INT', 'QUIT', 'ILL',
'TRAP', 'IOT', 'BUS', 'FPE',
'USR1', 'SEGV', 'USR2',
'PIPE', 'ALRM', 'TERM', )
{
$SIG{$sig} = \&cleanup;
}
}
=item create_temp ( $tmpfile )
This function creates an empty temporary file with the required
permission for security reasons.
=cut
sub create_temp($) {
my $tmpnam = shift;
my $fh = new FileHandle($tmpnam,O_CREAT|O_EXCL|O_WRONLY,0600);
$fh or die "$0: failed to create temporary file: $!";
$fh->close;
}
=back
=head1 AUTHOR
Cees de Groot, C<E<lt>cg@pobox.comE<gt>>.
=cut
1;
|