File Coverage

blib/lib/auto/share/dist/Alien-autoconf/share/autoconf/Autom4te/General.pm
Criterion Covered Total %
statement 55 89 61.8
branch 3 20 15.0
condition 2 9 22.2
subroutine 18 25 72.0
pod 6 6 100.0
total 84 149 56.3


line stmt bran cond sub pod time code
1             # autoconf -- create 'configure' using m4 macros
2             # Copyright (C) 2001-2004, 2006-2007, 2009-2017, 2020-2023 Free Software
3             # Foundation, Inc.
4              
5             # This program is free software: you can redistribute it and/or modify
6             # it under the terms of the GNU General Public License as published by
7             # the Free Software Foundation, either version 3 of the License, or
8             # (at your option) any later version.
9              
10             # This program is distributed in the hope that it will be useful,
11             # but WITHOUT ANY WARRANTY; without even the implied warranty of
12             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13             # GNU General Public License for more details.
14              
15             # You should have received a copy of the GNU General Public License
16             # along with this program. If not, see .
17              
18             package Autom4te::General;
19              
20             =head1 NAME
21              
22             Autom4te::General - general support functions for Autoconf
23              
24             =head1 SYNOPSIS
25              
26             use Autom4te::General
27              
28             =head1 DESCRIPTION
29              
30             This perl module provides various general purpose support functions
31             used in several executables of the Autoconf package.
32              
33             =cut
34              
35 7     7   131 use 5.006;
  7         27  
36 7     7   41 use strict;
  7         62  
  7         252  
37 7     7   33 use warnings FATAL => 'all';
  7         13  
  7         419  
38              
39 7     7   38 use Carp;
  7         19  
  7         498  
40 7     7   37 use Exporter;
  7         12  
  7         259  
41 7     7   34 use File::Basename;
  7         11  
  7         526  
42 7     7   39 use File::Spec ();
  7         14  
  7         149  
43 7     7   3805 use File::stat;
  7         61706  
  7         456  
44 7     7   5592 use File::Temp ();
  7         110928  
  7         244  
45 7     7   1059 use IO::File;
  7         3164  
  7         1127  
46              
47 7     7   709 use Autom4te::ChannelDefs;
  7         20  
  7         1059  
48 7     7   49 use Autom4te::Channels;
  7         34  
  7         1295  
49 7     7   4414 use Autom4te::Getopt ();
  7         25  
  7         8043  
50              
51             our @ISA = qw (Exporter);
52              
53             # Variables we define and export.
54             my @export_vars =
55             qw ($debug $force $help $me $tmp $verbose $version);
56              
57             # Functions we define and export.
58             my @export_subs =
59             qw (&debug
60             &getopt &shell_quote &mktmpdir
61             &uniq);
62              
63             # Functions we forward (coming from modules we use).
64             my @export_forward_subs =
65             qw (&basename &dirname &fileparse);
66              
67             our @EXPORT = (@export_vars, @export_subs, @export_forward_subs);
68              
69              
70             # Variable we share with the main package. Be sure to have a single
71             # copy of them: using 'my' together with multiple inclusion of this
72             # package would introduce several copies.
73              
74             =head2 Global Variables
75              
76             =over 4
77              
78             =item C<$debug>
79              
80             Set this variable to 1 if debug messages should be enabled. Debug
81             messages are meant for developers only, or when tracking down an
82             incorrect execution.
83              
84             =cut
85              
86             our $debug = 0;
87              
88             =item C<$force>
89              
90             Set this variable to 1 to recreate all the files, or to consider all
91             the output files are obsolete.
92              
93             =cut
94              
95             our $force = undef;
96              
97             =item C<$help>
98              
99             Set to the help message associated with the option C<--help>.
100              
101             =cut
102              
103             our $help = undef;
104              
105             =item C<$me>
106              
107             The name of this application, for diagnostic messages.
108              
109             =cut
110              
111             our $me = basename ($0);
112              
113             =item C<$tmp>
114              
115             The name of the temporary directory created by C. Left
116             C otherwise.
117              
118             =cut
119              
120             # Our tmp dir.
121             our $tmp = undef;
122              
123             =item C<$verbose>
124              
125             Enable verbosity messages. These messages are meant for ordinary
126             users, and typically make explicit the steps being performed.
127              
128             =cut
129              
130             our $verbose = 0;
131              
132             =item C<$version>
133              
134             Set to the version message associated to the option C<--version>.
135              
136             =cut
137              
138             our $version = undef;
139              
140             =back
141              
142             =cut
143              
144              
145              
146             ## ----- ##
147             ## END. ##
148             ## ----- ##
149              
150             =head2 Functions
151              
152             =over 4
153              
154             =item C
155              
156             Filter Perl's exit codes and exit nonzero whenever closing C fails.
157              
158             =cut
159              
160             # END
161             # ---
162             sub END
163             {
164             # $? contains the exit status we will return.
165             # It was set using one of the following ways:
166             #
167             # 1) normal termination
168             # this sets $? = 0
169             # 2) calling 'exit (n)'
170             # this sets $? = n
171             # 3) calling die or friends (croak, confess...):
172             # a) when $! is non-0
173             # this set $? = $!
174             # b) when $! is 0 but $? is not
175             # this sets $? = ($? >> 8) (i.e., the exit code of the
176             # last program executed)
177             # c) when both $! and $? are 0
178             # this sets $? = 255
179             #
180             # Cases 1), 2), and 3b) are fine, but we prefer $? = 1 for 3a) and 3c).
181 7     7   40 my $status = $?;
182 7 50 33     279 $status = 1 if ($! && $! == $?) || $? == 255;
      33        
183             # (Note that we cannot safely distinguish calls to 'exit (n)'
184             # from calls to die when '$! = n'. It's not big deal because
185             # we only call 'exit (0)' or 'exit (1)'.)
186              
187             # This is required if the code might send any output to stdout
188             # E.g., even --version or --help. So it's best to do it unconditionally.
189 7 50       503 if (! close STDOUT)
190             {
191 0         0 print STDERR "$me: closing standard output: $!\n";
192 0         0 $? = 1;
193 0         0 return;
194             }
195              
196 7         95 $? = $status;
197             }
198              
199              
200             ## ----------- ##
201             ## Functions. ##
202             ## ----------- ##
203              
204              
205             =item C
206              
207             If the debug mode is enabled (C<$debug> and C<$verbose>), report the
208             C<@message> on C, signed with the name of the program.
209              
210             =cut
211              
212             # &debug(@MESSAGE)
213             # ----------------
214             # Messages displayed only if $DEBUG and $VERBOSE.
215             sub debug (@)
216             {
217 0 0 0 0 1 0 print STDERR "$me: ", @_, "\n"
218             if $verbose && $debug;
219             }
220              
221              
222             =item C
223              
224             Wrapper around C. In addition to
225             the user C
226             C<-v>/C<--verbose>, C<-d>/C<--debug>, C<-f>/C<--force>. Conform to
227             the GNU Coding Standards for error messages.
228              
229             =cut
230              
231             # getopt (%OPTION)
232             # ----------------
233             # Handle the %OPTION, plus all the common options.
234             sub getopt (%)
235             {
236 7     7 1 56 my (%option) = @_;
237 0     0   0 %option = ("h|help" => sub { print $help; exit 0 },
  0         0  
238 7     7   10893 "V|version" => sub { print $version; exit 0 },
  7         950  
239              
240 0     0   0 "v|verbose" => sub { ++$verbose },
241 0     0   0 "d|debug" => sub { ++$debug },
242 7         190 'f|force' => \$force,
243              
244             # User options last, so that they have precedence.
245             %option);
246 7         126 Autom4te::Getopt::parse_options (%option);
247              
248 0         0 setup_channel 'note', silent => !$verbose;
249 0         0 setup_channel 'verb', silent => !$verbose;
250             }
251              
252              
253             =item C
254              
255             Quote C<$file_name> for the shell.
256              
257             =cut
258              
259             # $FILE_NAME
260             # shell_quote ($FILE_NAME)
261             # ------------------------
262             # If the string $S is a well-behaved file name, simply return it.
263             # If it contains white space, quotes, etc., quote it, and return
264             # the new string.
265             sub shell_quote($)
266             {
267 0     0 1 0 my ($s) = @_;
268 0 0       0 if ($s =~ m![^\w+/.,-]!)
269             {
270             # Convert each single quote to '\''
271 0         0 $s =~ s/\'/\'\\\'\'/g;
272             # Then single quote the string.
273 0         0 $s = "'$s'";
274             }
275 0         0 return $s;
276             }
277              
278             =item C
279              
280             Create a temporary directory which name is based on C<$signature>.
281             Store its name in C<$tmp>. It will be removed at program exit,
282             unless C<$debug> is true.
283              
284             =cut
285              
286             # mktmpdir ($SIGNATURE)
287             # ---------------------
288             sub mktmpdir ($)
289             {
290 3     3 1 19 my ($signature) = @_;
291              
292             # Ensure that we refer to the temporary directory by absolute
293             # pathname; most importantly, this ensures that C will
294             # work whenever FILE is in $tmp, even when '.' is not in @INC
295             # (perl 5.26 and later).
296 3         569 my $TMPDIR = File::Spec->rel2abs (File::Spec->tmpdir ());
297 3         54 $tmp = File::Temp::tempdir (
298             $signature . "XXXXXX",
299             DIR => $TMPDIR,
300             CLEANUP => !$debug
301             );
302              
303 3 50       2741 print STDERR "$me:$$: working in $tmp\n"
304             if $debug;
305              
306 3         12 return $tmp;
307             }
308              
309              
310             =item C
311              
312             Return C<@list> with no duplicates, keeping only the first
313             occurrences.
314              
315             =cut
316              
317             # @RES
318             # uniq (@LIST)
319             # ------------
320             sub uniq (@)
321             {
322 0     0 1   my @res = ();
323 0           my %seen = ();
324 0           foreach my $item (@_)
325             {
326 0 0         if (! exists $seen{$item})
327             {
328 0           $seen{$item} = 1;
329 0           push (@res, $item);
330             }
331             }
332 0 0         return wantarray ? @res : "@res";
333             }
334              
335              
336             =item C
337              
338             Display an error message for C<$command>, based on the content of
339             C<$?> and C<$!>.
340              
341             =cut
342              
343              
344             # handle_exec_errors ($COMMAND)
345             # -----------------------------
346             sub handle_exec_errors ($)
347             {
348 0     0 1   my ($command) = @_;
349              
350 0           $command = (split (' ', $command))[0];
351 0 0         if ($!)
352             {
353 0           error "failed to run $command: $!";
354             }
355             else
356             {
357 7     7   1108 use POSIX qw (WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG);
  7         18056  
  7         64  
358              
359 0 0         if (WIFEXITED ($?))
    0          
360             {
361 0           my $status = WEXITSTATUS ($?);
362             # WIFEXITED and WEXITSTATUS can alter $!, reset it so that
363             # error() actually propagates the command's exit status, not $!.
364 0           $! = 0;
365 0           error "$command failed with exit status: $status";
366             }
367             elsif (WIFSIGNALED ($?))
368             {
369 0           my $signal = WTERMSIG ($?);
370             # In this case we prefer to exit with status 1.
371 0           $! = 1;
372 0           error "$command terminated by signal: $signal";
373             }
374             else
375             {
376 0           error "$command exited abnormally";
377             }
378             }
379             }
380              
381             =back
382              
383             =head1 SEE ALSO
384              
385             L
386              
387             =head1 HISTORY
388              
389             Written by Alexandre Duret-Lutz EFE and Akim
390             Demaille EFE.
391              
392             =cut
393              
394              
395              
396             1; # for require
397              
398             ### Setup "GNU" style for perl-mode and cperl-mode.
399             ## Local Variables:
400             ## perl-indent-level: 2
401             ## perl-continued-statement-offset: 2
402             ## perl-continued-brace-offset: 0
403             ## perl-brace-offset: 0
404             ## perl-brace-imaginary-offset: 0
405             ## perl-label-offset: -2
406             ## cperl-indent-level: 2
407             ## cperl-brace-offset: 0
408             ## cperl-continued-brace-offset: 0
409             ## cperl-label-offset: -2
410             ## cperl-extra-newline-before-brace: t
411             ## cperl-merge-trailing-else: nil
412             ## cperl-continued-statement-offset: 2
413             ## End: