File Coverage

blib/lib/auto/share/dist/Alien-autoconf/bin/autom4te
Criterion Covered Total %
statement 123 394 31.2
branch 18 146 12.3
condition 13 85 15.2
subroutine 20 30 66.6
pod n/a
total 174 655 26.5


line stmt bran cond sub pod time code
1             #! /usr/local/bin/perl
2             BEGIN {
3 1     1   5345 use strict;
  1         2  
  1         41  
4 1     1   5 use warnings;
  1         3  
  1         53  
5 1     1   7 use File::Spec;
  1         2  
  1         258  
6 1     1   31 my($v,$d) = File::Spec->splitpath(File::Spec->rel2abs(__FILE__));
7 1         17 my @d = File::Spec->splitdir($d);
8 1         7 pop @d for 1..2;
9 1         19 my $dist_dir = File::Spec->catpath($v,File::Spec->catdir(@d), '');
10 1   33     28 $ENV{AUTOM4TE} ||= File::Spec->catfile($dist_dir, 'bin/autom4te');
11 1   33     22 $ENV{autom4te_perllibdir} ||= File::Spec->catdir($dist_dir, 'share/autoconf');
12 1   33     15 $ENV{AC_MACRODIR} ||= File::Spec->catdir($dist_dir, 'share/autoconf');
13 1   33     47 $ENV{AUTOCONF} ||= File::Spec->catfile($dist_dir, 'bin/autoconf');
14 1   33     14 $ENV{AUTOHEADER} ||= File::Spec->catfile($dist_dir, 'bin/autoheader');
15 1 50 33     32 $ENV{AUTOM4TE_CFG} ||= File::Spec->catfile($dist_dir, 'share/autoconf/autom4te.blib.cfg')
      33        
16             if $d[-5] eq 'lib' && $d[-6] eq 'blib';
17 1   33     59 $ENV{trailer_m4} ||= File::Spec->catdir($dist_dir, 'share/autoconf/autoconf/trailer.m4');
18             }
19             # -*- perl -*-
20             # Generated from bin/autom4te.in; do not edit by hand.
21              
22 1         109240 eval 'case $# in 0) exec /usr/local/bin/perl -S "$0";; *) exec /usr/local/bin/perl -S "$0" "$@";; esac'
23             if 0;
24              
25             # autom4te - Wrapper around M4 libraries.
26             # Copyright (C) 2001-2003, 2005-2017, 2020-2023 Free Software
27             # Foundation, Inc.
28              
29             # This program is free software: you can redistribute it and/or modify
30             # it under the terms of the GNU General Public License as published by
31             # the Free Software Foundation, either version 3 of the License, or
32             # (at your option) any later version.
33              
34             # This program is distributed in the hope that it will be useful,
35             # but WITHOUT ANY WARRANTY; without even the implied warranty of
36             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37             # GNU General Public License for more details.
38              
39             # You should have received a copy of the GNU General Public License
40             # along with this program. If not, see .
41              
42 1     1   17 use 5.006;
  1         4  
43 1     1   7 use strict;
  1         2  
  1         31  
44 1     1   5 use warnings FATAL => 'all';
  1         2  
  1         126  
45              
46             BEGIN
47             {
48 1   50 1   6 my $pkgdatadir = $ENV{'autom4te_perllibdir'} || '/usr/local/lib/perl5/site_perl/5.42.0/x86_64-linux/auto/share/dist/Alien-autoconf/share/autoconf';
49 1         2 unshift @INC, $pkgdatadir;
50              
51             # Override SHELL. On DJGPP SHELL may not be set to a shell
52             # that can handle redirection and quote arguments correctly,
53             # e.g.: COMMAND.COM. For DJGPP always use the shell that configure
54             # has detected.
55 1 50       22 $ENV{'SHELL'} = '/bin/bash' if ($^O eq 'dos');
56             }
57              
58 1     1   7 use File::Basename;
  1         1  
  1         143  
59              
60 1     1   709 use Autom4te::C4che;
  1         3  
  1         38  
61 1     1   643 use Autom4te::ChannelDefs;
  1         4  
  1         173  
62 1     1   7 use Autom4te::Channels;
  1         2  
  1         157  
63 1     1   613 use Autom4te::FileUtils;
  1         14  
  1         187  
64 1     1   620 use Autom4te::General;
  1         6  
  1         268  
65 1     1   767 use Autom4te::XFile;
  1         4  
  1         1806  
66              
67             # Data directory.
68 1   50     7 my $pkgdatadir = $ENV{'AC_MACRODIR'} || '/usr/local/lib/perl5/site_perl/5.42.0/x86_64-linux/auto/share/dist/Alien-autoconf/share/autoconf';
69              
70             # $LANGUAGE{LANGUAGE} -- Automatic options for LANGUAGE.
71 1         1 my %language;
72              
73 1         2 my $output = '-';
74              
75             # Mode of the output file except for traces.
76 1         2 my $mode = "0666";
77              
78             # If melt, don't use frozen files.
79 1         2 my $melt = 0;
80              
81             # Names of the cache directory, cache directory index, trace cache
82             # prefix, and output cache prefix. And the IO object for the index.
83 1         5 my $cache;
84             my $icache;
85 1         0 my $tcache;
86 1         0 my $ocache;
87 1         0 my $icache_file;
88              
89 1         2 my $flock_implemented = 'yes';
90              
91             # The macros to trace mapped to their format, as specified by the
92             # user.
93 1         1 my %trace;
94              
95             # The macros the user will want to trace in the future.
96             # We need 'include' to get the included file, 'm4_pattern_forbid' and
97             # 'm4_pattern_allow' to check the output.
98             #
99             # FIXME: What about 'sinclude'?
100 1         5 my @preselect = ('include',
101             'm4_pattern_allow', 'm4_pattern_forbid',
102             '_m4_warn');
103              
104             # M4 include path.
105 1         1 my @include;
106              
107             # Do we freeze?
108 1         2 my $freeze = 0;
109              
110             # $M4.
111 1   50     5 my $m4 = $ENV{"M4"} || '/usr/local/lib/perl5/site_perl/5.42.0/x86_64-linux/auto/share/dist/Alien-m4/bin/m4';
112             # Some non-GNU m4's don't reject the --help option, so give them /dev/null.
113 1 50       8975 fatal "need GNU m4 1.4 or later: $m4"
114             if system "$m4 --help &1 | grep reload-state >/dev/null";
115              
116             # Set some high recursion limit as the default limit, 250, has already
117             # been hit with AC_OUTPUT. Don't override the user's choice.
118 1 50       71 $m4 .= ' --nesting-limit=1024'
119             if " $m4 " !~ / (--nesting-limit(=[0-9]+)?|-L[0-9]*) /;
120              
121              
122             # @M4_BUILTIN -- M4 builtins and a useful comment.
123 1         10307 my @m4_builtin = `echo dumpdef | $m4 2>&1 >/dev/null`;
124 1         42 map { s/:.*//;s/\W// } @m4_builtin;
  46         132  
  46         140  
125              
126              
127             # %M4_BUILTIN_ALTERNATE_NAME
128             # --------------------------
129             # The builtins are renamed, e.g., 'define' is renamed 'm4_define'.
130             # So map 'define' to 'm4_define' and conversely.
131             # Some macros don't follow this scheme: be sure to properly map to their
132             # alternate name too.
133             #
134             # FIXME: Trace status of renamed builtins was fixed in M4 1.4.5, which
135             # we now depend on; do we still need to do this mapping?
136             #
137             # So we will merge them, i.e., tracing 'BUILTIN' or tracing
138             # 'm4_BUILTIN' will be the same: tracing both, but honoring the
139             # *last* trace specification.
140             #
141             # FIXME: This is not enough: in the output '$0' will be 'BUILTIN'
142             # sometimes and 'm4_BUILTIN' at others. We should return a unique name,
143             # the one specified by the user.
144             #
145             # FIXME: To be absolutely rigorous, I would say that given that we
146             # _redefine_ divert (instead of _copying_ it), divert and the like
147             # should not be part of this list.
148 1         14 my %m4_builtin_alternate_name;
149             @m4_builtin_alternate_name{"$_", "m4_$_"} = ("m4_$_", "$_")
150 1         7 foreach (grep { !/m4wrap|m4exit|dnl|ifelse|__.*__/ } @m4_builtin);
  46         400  
151 1         19 @m4_builtin_alternate_name{"ifelse", "m4_if"} = ("m4_if", "ifelse");
152 1         15 @m4_builtin_alternate_name{"m4exit", "m4_exit"} = ("m4_exit", "m4exit");
153 1         9 @m4_builtin_alternate_name{"m4wrap", "m4_wrap"} = ("m4_wrap", "m4wrap");
154              
155              
156             # $HELP
157             # -----
158 1         80 $help = "Usage: $0 [OPTION]... [FILES]
159              
160             Run GNU M4 on the FILES, avoiding useless runs. Output the traces if tracing,
161             the frozen file if freezing, otherwise the expansion of the FILES.
162              
163             If some of the FILES are named 'FILE.m4f' they are considered to be M4
164             frozen files of all the previous files (which are therefore not loaded).
165             If 'FILE.m4f' is not found, then 'FILE.m4' will be used, together with
166             all the previous files.
167              
168             Some files may be optional, i.e., will only be processed if found in the
169             include path, but then must end in '.m4?'; the question mark is not part
170             of the actual file name.
171              
172             Operation modes:
173             -h, --help print this help, then exit
174             -V, --version print version number, then exit
175             -v, --verbose verbosely report processing
176             -d, --debug don't remove temporary files
177             -o, --output=FILE save output in FILE (defaults to '-', stdout)
178             -f, --force don't rely on cached values
179             -W, --warnings=CATEGORY report the warnings falling in CATEGORY
180             (comma-separated list accepted)
181             -l, --language=LANG specify the set of M4 macros to use
182             -C, --cache=DIRECTORY preserve results for future runs in DIRECTORY
183             --no-cache disable the cache
184             -m, --mode=OCTAL change the non trace output file mode (0666)
185             -M, --melt don't use M4 frozen files
186              
187             Languages include:
188             'Autoconf' create Autoconf configure scripts
189             'Autotest' create Autotest test suites
190             'M4sh' create M4sh shell scripts
191             'M4sugar' create M4sugar output
192              
193             " . Autom4te::ChannelDefs::usage . "
194              
195             The environment variables 'M4' and 'WARNINGS' are honored.
196              
197             Library directories:
198             -B, --prepend-include=DIR prepend directory DIR to search path
199             -I, --include=DIR append directory DIR to search path
200              
201             Tracing:
202             -t, --trace=MACRO[:FORMAT] report the MACRO invocations
203             -p, --preselect=MACRO prepare to trace MACRO in a future run
204              
205             Freezing:
206             -F, --freeze produce an M4 frozen state file for FILES
207              
208             FORMAT defaults to '\$f:\$l:\$n:\$%', and can use the following escapes:
209             \$\$ literal \$
210             \$f file where macro was called
211             \$l line where macro was called
212             \$d nesting depth of macro call
213             \$n name of the macro
214             \$NUM argument NUM, unquoted and with newlines
215             \$SEP\@ all arguments, with newlines, quoted, and separated by SEP
216             \$SEP* all arguments, with newlines, unquoted, and separated by SEP
217             \$SEP% all arguments, without newlines, unquoted, and separated by SEP
218             SEP can be empty for the default (comma for \@ and *, colon for %),
219             a single character for that character, or {STRING} to use a string.
220              
221             Report bugs to .
222              
223             The full documentation for Autoconf can be read via 'info autoconf',
224             or on the Web at .
225             ";
226              
227             # $VERSION
228             # --------
229 1 50       20 $version = "autom4te (GNU Autoconf) 2.72\n"
230             . ($Autom4te::FileUtils::subsecond_mtime
231             ? "Features: subsecond-mtime\n" : "")
232             . "\nCopyright (C) 2023 Free Software Foundation, Inc.
233             License GPLv3+/Autoconf: GNU GPL version 3 or later
234             ,
235             This is free software: you are free to change and redistribute it.
236             There is NO WARRANTY, to the extent permitted by law.
237              
238             Written by Akim Demaille.
239             ";
240              
241              
242             ## ---------- ##
243             ## Routines. ##
244             ## ---------- ##
245              
246             # tempfile_with_mode ($dir, $mode)
247             # --------------------------------
248             # Create a temporary file in $dir with access control bits $mode.
249             # Returns a list ($fh, $fname) where $fh is a filehandle open for
250             # writing to the file, and $fname is the name of the file.
251             sub tempfile_with_mode ($$)
252             {
253 0     0   0 my ($dir, $mode) = @_;
254              
255 0         0 require File::Temp;
256 0         0 my $template = "actmp." . File::Temp::TEMPXXX;
257              
258             # The PERMS argument was added to File::Temp::tempfile in version
259             # 0.2310 of the File::Temp module; it will be silently ignored if
260             # passed to an older version of the function. This is the simplest
261             # way to do a non-fatal version check without features of Perl 5.10.
262 0         0 local $@;
263 0 0       0 if (eval { File::Temp->VERSION("0.2310"); 1 })
  0         0  
  0         0  
264             {
265             # Can use PERMS argument to tempfile().
266 0         0 return File::Temp::tempfile ($template, DIR => $dir, PERMS => $mode,
267             UNLINK => 0);
268             }
269             else
270             {
271             # PERMS is not available.
272             # This is functionally equivalent to what it would do.
273 0         0 require Fcntl;
274 0         0 my $openflags = Fcntl::O_RDWR | Fcntl::O_CREAT | Fcntl::O_EXCL;
275              
276 0         0 require File::Spec;
277 0         0 $template = File::Spec->catfile($dir, $template);
278              
279             # 50 = $MAX_GUESS in File::Temp (not an exported constant).
280 0         0 for (my $i = 0; $i < 50; $i++)
281             {
282 0         0 my $filename = File::Temp::mktemp($template);
283 0         0 my $fh;
284 0         0 my $success = sysopen ($fh, $filename, $openflags, $mode);
285 0 0       0 return ($fh, $filename) if $success;
286             fatal "Could not create temp file $filename: $!"
287 0 0       0 unless $!{EEXIST};
288             }
289 0         0 fatal "Could not create any temp file from $template: $!";
290             }
291             }
292              
293             # $OPTION
294             # files_to_options (@FILE)
295             # ------------------------
296             # Transform Autom4te conventions (e.g., using foo.m4f to designate a frozen
297             # file) into a suitable command line for M4 (e.g., using --reload-state).
298             # parse_args guarantees that we will see at most one frozen file, and that
299             # if a frozen file is present, it is the first argument.
300             sub files_to_options (@)
301             {
302 0     0   0 my (@file) = @_;
303 0         0 my @res;
304 0         0 foreach my $file (@file)
305             {
306 0         0 my $arg = shell_quote ($file);
307 0 0       0 if ($file =~ /\.m4f$/)
308             {
309 0         0 $arg = "--reload-state=$arg";
310             # If the user downgraded M4 from 1.6 to 1.4.x after freezing
311             # the file, then we ensure the frozen __m4_version__ will
312             # not cause m4_init to make the wrong decision about the
313             # current M4 version.
314             $arg .= " --undefine=__m4_version__"
315 0 0       0 unless grep {/__m4_version__/} @m4_builtin;
  0         0  
316             }
317 0         0 push @res, $arg;
318             }
319 0         0 return join ' ', @res;
320             }
321              
322              
323             # load_configuration ($FILE)
324             # --------------------------
325             # Load the configuration $FILE.
326             sub load_configuration ($)
327             {
328 1     1   11 my ($file) = @_;
329 1     1   884 use Text::ParseWords;
  1         2338  
  1         525  
330              
331 1         30 my $cfg = new Autom4te::XFile ($file, "<");
332 1         8 my $lang;
333 1         9 while ($_ = $cfg->getline)
334             {
335 173         329 chomp;
336             # Comments.
337             next
338 173 100       849 if /^\s*(\#.*)?$/;
339              
340 104         297 my @words = shellwords ($_);
341 104         12226 my $type = shift @words;
342 104 100       396 if ($type eq 'begin-language:')
    100          
    50          
343             {
344 8 50       26 fatal "$file:$.: end-language missing for: $lang"
345             if defined $lang;
346 8         36 $lang = lc $words[0];
347             }
348             elsif ($type eq 'end-language:')
349             {
350 8 50       30 error "$file:$.: end-language mismatch: $lang"
351             if $lang ne lc $words[0];
352 8         110 $lang = undef;
353             }
354             elsif ($type eq 'args:')
355             {
356 88 50       243 fatal "$file:$.: no current language"
357             unless defined $lang;
358 88         148 push @{$language{$lang}}, @words;
  88         503  
359             }
360             else
361             {
362 0         0 error "$file:$.: unknown directive: $type";
363             }
364             }
365             }
366              
367              
368             # parse_args ()
369             # -------------
370             # Process any command line arguments.
371             sub parse_args ()
372             {
373             # We want to look for the early options, which should not be found
374             # in the configuration file. Prepend to the user arguments.
375             # Perform this repeatedly so that we can use --language in language
376             # definitions. Beware that there can be several --language
377             # invocations.
378 1     1   4 my @language;
379 1         9 do {
380 1         4 @language = ();
381 1     1   13 use Getopt::Long;
  1         2  
  1         10  
382 1         42 Getopt::Long::Configure ("pass_through", "permute");
383 1         83 GetOptions ("l|language=s" => \@language);
384              
385 1         714 foreach (@language)
386             {
387             error "unknown language: $_"
388 0 0       0 unless exists $language{lc $_};
389 0         0 unshift @ARGV, @{$language{lc $_}};
  0         0  
390             }
391             } while @language;
392              
393             # --debug is useless: it is parsed below.
394 1 50       5 if (exists $ENV{'AUTOM4TE_DEBUG'})
395             {
396 0         0 print STDERR "$me: concrete arguments:\n";
397 0         0 foreach my $arg (@ARGV)
398             {
399 0         0 print STDERR "| $arg\n";
400             }
401             }
402              
403             # Process the arguments for real this time.
404 1         3 my @trace;
405             my @prepend_include;
406 1         0 my @warnings;
407              
408             getopt
409             (
410             # Operation modes:
411             "o|output=s" => \$output,
412             "W|warnings=s" => \@warnings,
413             "m|mode=s" => \$mode,
414             "M|melt" => \$melt,
415              
416             # Library directories:
417             "B|prepend-include=s" => \@prepend_include,
418             "I|include=s" => \@include,
419              
420             # Tracing:
421             # Using a hash for traces is seducing. Unfortunately, upon '-t FOO',
422             # instead of mapping 'FOO' to undef, Getopt maps it to '1', preventing
423             # us from distinguishing '-t FOO' from '-t FOO=1'. So let's do it
424             # by hand.
425             "t|trace=s" => \@trace,
426             "p|preselect=s" => \@preselect,
427              
428             # Freezing.
429             "F|freeze" => \$freeze,
430              
431             # Caching.
432             "C|cache=s" => \$cache,
433 0     0     "no-cache" => sub { $cache = undef; },
434 1         26 );
435              
436 0           parse_WARNINGS;
437 0           parse_warnings @warnings;
438              
439 0 0         fatal "too few arguments
440             Try '$me --help' for more information."
441             unless @ARGV;
442              
443             # Freezing:
444             # We cannot trace at the same time (well, we can, but it sounds insane).
445             # And it implies melting: there is risk not to update properly using
446             # old frozen files, and worse yet: we could load a frozen file and
447             # refreeze it! A sort of caching :)
448 0 0 0       fatal "cannot freeze and trace"
449             if $freeze && @trace;
450 0 0         $melt = 1
451             if $freeze;
452              
453             # Names of the cache directory, cache directory index, trace cache
454             # prefix, and output cache prefix. If the cache is not to be
455             # preserved, default to a temporary directory (automatically removed
456             # on exit).
457 0 0         $cache = $tmp
458             unless $cache;
459 0           $icache = "$cache/requests";
460 0           $tcache = "$cache/traces.";
461 0           $ocache = "$cache/output.";
462              
463             # Normalize the includes: the first occurrence is enough, several is
464             # a pain since it introduces a useless difference in the path which
465             # invalidates the cache. And strip '.' which is implicit and always
466             # first.
467 0           @include = grep { !/^\.$/ } uniq (reverse(@prepend_include), @include);
  0            
468              
469             # Convert @trace to %trace, and work around the M4 builtins tracing
470             # problem.
471             # The default format is '$f:$l:$n:$%'.
472 0           foreach (@trace)
473             {
474 0           /^([^:]+)(?::(.*))?$/ms;
475 0 0         $trace{$1} = defined $2 ? $2 : '$f:$l:$n:$%';
476             $trace{$m4_builtin_alternate_name{$1}} = $trace{$1}
477 0 0         if exists $m4_builtin_alternate_name{$1};
478             }
479              
480             # Work around the M4 builtins tracing problem for @PRESELECT.
481             # FIXME: Is this still needed, now that we rely on M4 1.4.5?
482             push (@preselect,
483 0           map { $m4_builtin_alternate_name{$_} }
484 0           grep { exists $m4_builtin_alternate_name{$_} } @preselect);
  0            
485              
486             # If we find frozen files, then all the files before it are
487             # discarded: the frozen file is supposed to include them all.
488             #
489             # We don't want to depend upon m4's --include to find the top level
490             # files, so we use 'find_file' here. Try to get a canonical name,
491             # as it's part of the key for caching. And some files are optional
492             # (also handled by 'find_file').
493 0           my @argv;
494 0           foreach (@ARGV)
495             {
496 0 0         if ($_ eq '-')
    0          
497             {
498 0           push @argv, $_;
499             }
500             elsif (/\.m4f$/)
501             {
502             # Frozen files are optional => pass a '?' to 'find_file'.
503 0           my $file = find_file ("$_?", @include);
504 0 0 0       if (!$melt && $file)
505             {
506 0           @argv = ($file);
507             }
508             else
509             {
510 0           s/\.m4f$/.m4/;
511 0           push @argv, find_file ($_, @include);
512             }
513             }
514             else
515             {
516 0           my $file = find_file ($_, @include);
517 0 0         push @argv, $file
518             if $file;
519             }
520             }
521 0           @ARGV = @argv;
522             }
523              
524              
525             # handle_m4 ($REQ, @MACRO)
526             # ------------------------
527             # Run m4 on the input files, and save the traces on the @MACRO.
528             sub handle_m4 ($@)
529             {
530 0     0     my ($req, @macro) = @_;
531              
532             # GNU m4 appends when using --debugfile/--error-output.
533 0           unlink ($tcache . $req->id . "t");
534              
535             # Run m4.
536             #
537             # We don't output directly to the cache files, to avoid problems
538             # when we are interrupted (that leaves corrupted files).
539             xsystem ("$m4 --gnu"
540 0           . join (' --include=', '', map { shell_quote ($_) } @include)
541             . ' --debug=aflq'
542             . (!exists $ENV{'AUTOM4TE_NO_FATAL'} ? ' --fatal-warning' : '')
543             . " --debugfile=" . shell_quote ("$tcache" . $req->id . "t")
544 0 0         . join (' --trace=', '', map { shell_quote ($_) } sort @macro)
  0            
545             . " " . files_to_options (@ARGV)
546             . " > " . shell_quote ("$ocache" . $req->id . "t"));
547              
548             # Everything went ok: preserve the outputs.
549 0           foreach my $file (map { $_ . $req->id } ($tcache, $ocache))
  0            
550             {
551 1     1   1740 use File::Copy;
  1         3  
  1         72192  
552 0 0         move ("${file}t", "$file")
553             or fatal "cannot rename ${file}t as $file: $!";
554             }
555             }
556              
557              
558             # warn_forbidden ($WHERE, $WORD, %FORBIDDEN)
559             # ------------------------------------------
560             # $WORD is forbidden. Warn with a dedicated error message if in
561             # %FORBIDDEN, otherwise a simple 'error: possibly undefined macro'
562             # will do.
563 1         14 my $first_warn_forbidden = 1;
564             sub warn_forbidden ($$%)
565             {
566 0     0     my ($where, $word, %forbidden) = @_;
567 0           my $message;
568              
569 0           for my $re (sort keys %forbidden)
570             {
571 0 0         if ($word =~ $re)
572             {
573 0           $message = $forbidden{$re};
574 0           last;
575             }
576             }
577 0   0       $message ||= "possibly undefined macro: $word";
578 0           warn "$where: error: $message\n";
579 0 0         if ($first_warn_forbidden)
580             {
581 0           warn <
582             If this token and others are legitimate, please use m4_pattern_allow.
583             See the Autoconf documentation.
584             EOF
585 0           $first_warn_forbidden = 0;
586             }
587             }
588              
589              
590             # handle_output ($REQ, $OUTPUT)
591             # -----------------------------
592             # Run m4 on the input files, perform quadrigraphs substitution, check for
593             # forbidden tokens, and save into $OUTPUT.
594             sub handle_output ($$)
595             {
596 0     0     my ($req, $output) = @_;
597              
598 0           verb "creating $output";
599              
600             # Load the forbidden/allowed patterns.
601 0           handle_traces ($req, "$tmp/patterns",
602             ('m4_pattern_forbid' => 'forbid:$1:$2',
603             'm4_pattern_allow' => 'allow:$1'));
604 0           my @patterns = new Autom4te::XFile ("$tmp/patterns", "<")->getlines;
605 0           chomp @patterns;
606             my %forbidden =
607 0           map { /^forbid:([^:]+):.+$/ => /^forbid:[^:]+:(.+)$/ } @patterns;
  0            
608 0   0       my $forbidden = join ('|', map { /^forbid:([^:]+)/ } @patterns) || "^\$";
609 0   0       my $allowed = join ('|', map { /^allow:([^:]+)/ } @patterns) || "^\$";
610              
611 0           verb "forbidden tokens: $forbidden";
612             verb "forbidden token : $_ => $forbidden{$_}"
613 0           foreach (sort keys %forbidden);
614 0           verb "allowed tokens: $allowed";
615              
616             # Read the (cached) raw M4 output, produce the actual result.
617             # If we are writing to a regular file, replace it atomically.
618 0           my $scratchfile;
619             my $out;
620 0 0 0       if ($output eq '-')
    0          
621             {
622             # Don't just make $out be STDOUT, because then we would close STDOUT,
623             # which we already do in END.
624 0           $out = new Autom4te::XFile ('>&STDOUT');
625             }
626             elsif (-e $output && ! -f $output)
627             {
628 0           $out = new Autom4te::XFile ($output, '>');
629             }
630             else
631             {
632 0           my (undef, $outdir, undef) = fileparse ($output);
633 0           ($out, $scratchfile) = tempfile_with_mode ($outdir, oct($mode));
634             }
635              
636 0           my $in = new Autom4te::XFile ($ocache . $req->id, "<");
637              
638 0           my %prohibited;
639             my $res;
640 0           while ($_ = $in->getline)
641             {
642 0           s/\s+$//;
643 0           s/__oline__/$./g;
644 0           s/\@<:\@/[/g;
645 0           s/\@:>\@/]/g;
646 0           s/\@\{:\@/(/g;
647 0           s/\@:\}\@/)/g;
648 0           s/\@S\|\@/\$/g;
649 0           s/\@%:\@/#/g;
650              
651 0           $res = $_;
652              
653             # Don't complain in comments. Well, until we have something
654             # better, don't consider '#include' etc. are comments.
655 0 0         s/\#.*//
656             unless /^\#\s*(if|include|endif|ifdef|ifndef|define)\b/;
657 0           foreach (split (/\W+/))
658             {
659             $prohibited{$_} = $.
660             if !/^$/ && /$forbidden/o && !/$allowed/o
661 0 0 0       && ! exists $prohibited{$_};
      0        
      0        
662             }
663              
664             # Performed *last*: the empty quadrigraph.
665 0           $res =~ s/\@&t\@//g;
666              
667 0           print $out "$res\n";
668             }
669              
670 0           $out->close();
671             # Always update the file, even if it didn't change;
672             # Automake relies on this.
673 0 0         update_file ($scratchfile, $output, 1)
674             if defined $scratchfile;
675              
676             # If no forbidden words, we're done.
677             return
678 0 0         if ! %prohibited;
679              
680             # Locate the forbidden words in the last input file.
681             # This is unsatisfying but...
682 0           $exit_code = 1;
683 0 0         if ($ARGV[$#ARGV] ne '-')
684             {
685 0           my $prohibited = '\b(' . join ('|', keys %prohibited) . ')\b';
686 0           my $file = new Autom4te::XFile ($ARGV[$#ARGV], "<");
687              
688 0           while ($_ = $file->getline)
689             {
690             # Don't complain in comments. Well, until we have something
691             # better, don't consider '#include' etc. to be comments.
692 0 0         s/\#.*//
693             unless /^\#(if|include|endif|ifdef|ifndef|define)\b/;
694              
695             # Complain once per word, but possibly several times per line.
696 0           while (/$prohibited/)
697             {
698 0           my $word = $1;
699 0           warn_forbidden ("$ARGV[$#ARGV]:$.", $word, %forbidden);
700 0           delete $prohibited{$word};
701             # If we're done, exit.
702             return
703 0 0         if ! %prohibited;
704 0           $prohibited = '\b(' . join ('|', keys %prohibited) . ')\b';
705             }
706             }
707             }
708             warn_forbidden ("$output:$prohibited{$_}", $_, %forbidden)
709 0           foreach (sort { $prohibited{$a} <=> $prohibited{$b} } keys %prohibited);
  0            
710             }
711              
712              
713             ## --------------------- ##
714             ## Handling the traces. ##
715             ## --------------------- ##
716              
717              
718             # $M4_MACRO
719             # trace_format_to_m4 ($FORMAT)
720             # ----------------------------
721             # Convert a trace $FORMAT into a M4 trace processing macro's body.
722             sub trace_format_to_m4 ($)
723             {
724 0     0     my ($format) = @_;
725 0           my $underscore = $_;
726 0           my %escape = (# File name.
727             'f' => '$1',
728             # Line number.
729             'l' => '$2',
730             # Depth.
731             'd' => '$3',
732             # Name (also available as $0).
733             'n' => '$4',
734             # Escaped dollar.
735             '$' => '$');
736              
737 0           my $res = '';
738 0           $_ = $format;
739 0           while ($_)
740             {
741             # $n -> $(n + 4)
742 0 0 0       if (s/^\$(\d+)//)
    0          
    0          
    0          
743             {
744 0           $res .= "\$" . ($1 + 4);
745             }
746             # $x, no separator given.
747             elsif (s/^\$([fldn\$])//)
748             {
749 0           $res .= $escape{$1};
750             }
751             # $.x or ${sep}x.
752             elsif (s/^\$\{([^}]*)\}([@*%])//
753             || s/^\$(.?)([@*%])//)
754             {
755             # $@, list of quoted effective arguments.
756 0 0         if ($2 eq '@')
    0          
    0          
757             {
758 0 0         $res .= ']at_at([' . ($1 ? $1 : ',') . '], $@)[';
759             }
760             # $*, list of unquoted effective arguments.
761             elsif ($2 eq '*')
762             {
763 0 0         $res .= ']at_star([' . ($1 ? $1 : ',') . '], $@)[';
764             }
765             # $%, list of flattened unquoted effective arguments.
766             elsif ($2 eq '%')
767             {
768 0 0         $res .= ']at_percent([' . ($1 ? $1 : ':') . '], $@)[';
769             }
770             }
771             elsif (/^(\$.)/)
772             {
773 0           error "invalid escape: $1";
774             }
775             else
776             {
777 0           s/^([^\$]+)//;
778 0           $res .= $1;
779             }
780             }
781              
782 0           $_ = $underscore;
783 0           return '[[' . $res . ']]';
784             }
785              
786              
787             # handle_traces($REQ, $OUTPUT, %TRACE)
788             # ------------------------------------
789             # We use M4 itself to process the traces. But to avoid name clashes when
790             # processing the traces, the builtins are disabled, and moved into 'at_'.
791             # Actually, all the low level processing macros are in 'at_' (and '_at_').
792             # To avoid clashes between user macros and 'at_' macros, the macros which
793             # implement tracing are in 'AT_'.
794             #
795             # Having $REQ is needed to neutralize the macros which have been traced,
796             # but are not wanted now.
797             sub handle_traces ($$%)
798             {
799 0     0     my ($req, $output, %trace) = @_;
800              
801 0           verb "formatting traces for '$output': " . join (', ', sort keys %trace);
802              
803             # Processing the traces.
804 0           my $trace_m4 = new Autom4te::XFile ("$tmp/traces.m4", ">");
805              
806 0           $_ = <<'EOF';
807             divert(-1)
808             changequote([, ])
809             # _at_MODE(SEPARATOR, ELT1, ELT2...)
810             # ----------------------------------
811             # List the elements, separating then with SEPARATOR.
812             # MODE can be:
813             # 'at' -- the elements are enclosed in brackets.
814             # 'star' -- the elements are listed as are.
815             # 'percent' -- the elements are 'flattened': spaces are singled out,
816             # and no new line remains.
817             define([_at_at],
818             [at_ifelse([$#], [1], [],
819             [$#], [2], [[[$2]]],
820             [[[$2]][$1]$0([$1], at_shift(at_shift($@)))])])
821              
822             define([_at_percent],
823             [at_ifelse([$#], [1], [],
824             [$#], [2], [at_flatten([$2])],
825             [at_flatten([$2])[$1]$0([$1], at_shift(at_shift($@)))])])
826              
827             define([_at_star],
828             [at_ifelse([$#], [1], [],
829             [$#], [2], [[$2]],
830             [[$2][$1]$0([$1], at_shift(at_shift($@)))])])
831              
832             # FLATTEN quotes its result.
833             # Note that the second pattern is 'newline, tab or space'. Don't lose
834             # the tab!
835             define([at_flatten],
836             [at_patsubst(at_patsubst([[[$1]]], [\\\n]), [[\n\t ]+], [ ])])
837              
838             define([at_args], [at_shift(at_shift(at_shift(at_shift(at_shift($@)))))])
839             define([at_at], [_$0([$1], at_args($@))])
840             define([at_percent], [_$0([$1], at_args($@))])
841             define([at_star], [_$0([$1], at_args($@))])
842              
843             EOF
844 0           s/^ //mg;s/\\t/\t/mg;s/\\n/\n/mg;
  0            
  0            
845 0           print $trace_m4 $_;
846              
847             # If you trace 'define', then on 'define([m4_exit], defn([m4exit])' you
848             # will produce
849             #
850             # AT_define([m4sugar.m4], [115], [1], [define], [m4_exit], )
851             #
852             # Since '' is not quoted, the outer m4, when processing
853             # 'trace.m4' will exit prematurely. Hence, move all the builtins to
854             # the 'at_' name space.
855              
856 0           print $trace_m4 "# Copy the builtins.\n";
857 0           map { print $trace_m4 "define([at_$_], defn([$_]))\n" } @m4_builtin;
  0            
858 0           print $trace_m4 "\n";
859              
860 0           print $trace_m4 "# Disable them.\n";
861 0           map { print $trace_m4 "at_undefine([$_])\n" } @m4_builtin;
  0            
862 0           print $trace_m4 "\n";
863              
864              
865             # Neutralize traces: we don't want traces of cached requests (%REQUEST).
866 0           print $trace_m4
867             "## -------------------------------------- ##\n",
868             "## By default neutralize all the traces. ##\n",
869             "## -------------------------------------- ##\n",
870             "\n";
871             print $trace_m4 "at_define([AT_$_], [at_dnl])\n"
872 0           foreach (sort keys %{$req->macro});
  0            
873 0           print $trace_m4 "\n";
874              
875             # Implement traces for current requests (%TRACE).
876 0           print $trace_m4
877             "## ------------------------- ##\n",
878             "## Trace processing macros. ##\n",
879             "## ------------------------- ##\n",
880             "\n";
881 0           foreach (sort keys %trace)
882             {
883             # Trace request can be embed \n.
884 0           (my $comment = "Trace $_:$trace{$_}") =~ s/^/\# /;
885 0           print $trace_m4 "$comment\n";
886 0           print $trace_m4 "at_define([AT_$_],\n";
887 0           print $trace_m4 trace_format_to_m4 ($trace{$_}) . ")\n\n";
888             }
889 0           print $trace_m4 "\n";
890              
891             # Reenable output.
892 0           print $trace_m4 "at_divert(0)at_dnl\n";
893              
894             # Transform the traces from m4 into an m4 input file.
895             # Typically, transform:
896             #
897             # | m4trace:configure.ac:3: -1- AC_SUBST([exec_prefix], [NONE])
898             #
899             # into
900             #
901             # | AT_AC_SUBST([configure.ac], [3], [1], [AC_SUBST], [exec_prefix], [NONE])
902             #
903             # Pay attention that the file name might include colons, if under DOS
904             # for instance, so we don't use '[^:]+'.
905 0           my $traces = new Autom4te::XFile ($tcache . $req->id, "<");
906 0           while ($_ = $traces->getline)
907             {
908             # Trace with arguments, as the example above. We don't try
909             # to match the trailing parenthesis as it might be on a
910             # separate line.
911 0           s{^m4trace:(.+):(\d+): -(\d+)- ([^(]+)\((.*)$}
912             {AT_$4([$1], [$2], [$3], [$4], $5};
913             # Traces without arguments, always on a single line.
914 0           s{^m4trace:(.+):(\d+): -(\d+)- ([^)]*)\n$}
915             {AT_$4([$1], [$2], [$3], [$4])\n};
916 0           print $trace_m4 "$_";
917             }
918 0           $trace_m4->close;
919              
920 0           my $in = new Autom4te::XFile ("$m4 " . shell_quote ("$tmp/traces.m4") . " |");
921 0           my $out = new Autom4te::XFile;
922 0 0         if ($output eq '-')
923             {
924 0           $out->open (">$output");
925             }
926             else
927             {
928 0           $out->open ($output, ">");
929             }
930              
931             # This is dubious: should we really transform the quadrigraphs in
932             # traces? It might break balanced [ ] etc. in the output. The
933             # consensus seems to be that traces are more useful this way.
934 0           while ($_ = $in->getline)
935             {
936             # It makes no sense to try to transform __oline__.
937 0           s/\@<:\@/[/g;
938 0           s/\@:>\@/]/g;
939 0           s/\@\{:\@/(/g;
940 0           s/\@:\}\@/)/g;
941 0           s/\@S\|\@/\$/g;
942 0           s/\@%:\@/#/g;
943 0           s/\@&t\@//g;
944 0           print $out $_;
945             }
946             }
947              
948              
949             # $BOOL
950             # up_to_date ($REQ)
951             # -----------------
952             # Are the cache files of $REQ up to date?
953             # $REQ is 'valid' if it corresponds to the request and exists, which
954             # does not mean it is up to date. It is up to date if, in addition,
955             # its files are younger than its dependencies.
956             sub up_to_date ($)
957             {
958 0     0     my ($req) = @_;
959              
960 0 0         return 0
961             if ! $req->valid;
962              
963 0           my $tfile = $tcache . $req->id;
964 0           my $ofile = $ocache . $req->id;
965              
966             # We can't answer properly if the traces are not computed since we
967             # need to know what other files were included. Actually, if any of
968             # the cache files are missing, we are not up to date.
969 0 0 0       return 0
970             if ! -f $tfile || ! -f $ofile;
971              
972             # Both cache files must be younger than all dependencies,
973             # so use the minimum of the two cache files' timestamps.
974 0           my $tmtime = mtime ($tfile);
975 0           my $omtime = mtime ($ofile);
976 0 0         my ($file, $mtime) = ($omtime < $tmtime
977             ? ($ofile, $omtime) : ($tfile, $tmtime));
978              
979             # stdin is always out of date.
980 0 0         if (grep { $_ eq '-' } @ARGV)
  0            
981 0           { return 0 }
982              
983             # We depend at least upon the arguments.
984 0           foreach my $dep (@ARGV)
985             {
986 0 0         if ($mtime <= mtime ($dep))
987             {
988 0           verb "up_to_date ($file): outdated: $dep";
989 0           return 0;
990             }
991             }
992              
993             # Files may include others. We can use traces since we just checked
994             # if they are available.
995 0           handle_traces ($req, "$tmp/dependencies",
996             ('include' => '$1',
997             'm4_include' => '$1'));
998 0           my $deps = new Autom4te::XFile ("$tmp/dependencies", "<");
999 0           while ($_ = $deps->getline)
1000             {
1001 0           chomp;
1002 0           my $dep = find_file ("$_?", @include);
1003             # If a file which used to be included is no longer there, then
1004             # don't say it's missing (it might no longer be included). But
1005             # of course, that causes the output to be outdated (as if the
1006             # timestamp of that missing file was newer).
1007 0 0         return 0
1008             if ! $dep;
1009 0 0         if ($mtime <= mtime ($dep))
1010             {
1011 0           verb "up_to_date ($file): outdated: $dep";
1012 0           return 0;
1013             }
1014             }
1015              
1016 0           verb "up_to_date ($file): up to date";
1017 0           return 1;
1018             }
1019              
1020              
1021             ## ---------- ##
1022             ## Freezing. ##
1023             ## ---------- ##
1024              
1025             # freeze ($OUTPUT)
1026             # ----------------
1027             sub freeze ($)
1028             {
1029 0     0     my ($output) = @_;
1030              
1031             # When processing the file with diversion disabled, there must be no
1032             # output but comments and empty lines.
1033             my $result = xqx ("$m4"
1034             . ' --fatal-warning'
1035 0           . join (' --include=', '', map { shell_quote ($_) } @include)
  0            
1036             . ' --define=divert'
1037             . " " . files_to_options (@ARGV)
1038             . '
1039 0           $result =~ s/#.*\n//g;
1040 0           $result =~ s/^\n//mg;
1041              
1042 0 0         fatal "freezing produced output:\n$result"
1043             if $result;
1044              
1045             # If freezing produces output, something went wrong: a bad 'divert',
1046             # or an improper paren etc.
1047             xsystem ("$m4"
1048             . ' --fatal-warning'
1049 0           . join (' --include=', '', map { shell_quote ($_) } @include)
  0            
1050             . " --freeze-state=" . shell_quote ($output)
1051             . " " . files_to_options (@ARGV)
1052             . '
1053             }
1054              
1055             ## -------------- ##
1056             ## Main program. ##
1057             ## -------------- ##
1058              
1059 1         25 mktmpdir ('am4t');
1060 1   33     86 load_configuration ($ENV{'AUTOM4TE_CFG'} || "$pkgdatadir/autom4te.cfg");
1061             load_configuration ("$ENV{'HOME'}/.autom4te.cfg")
1062 1 50 33     40 if exists $ENV{'HOME'} && -f "$ENV{'HOME'}/.autom4te.cfg";
1063 1 50       47 load_configuration (".autom4te.cfg")
1064             if -f ".autom4te.cfg";
1065 1         10 parse_args;
1066              
1067             # Freezing does not involve the cache.
1068 0 0       0 if ($freeze)
1069             {
1070 0         0 freeze ($output);
1071 0         0 exit $exit_code;
1072             }
1073              
1074             # Ensure the cache directory exists.
1075 0 0       0 if (! mkdir ($cache, 0755))
1076             {
1077             # Snapshot $! immediately, the next few operations may clobber it.
1078 0         0 my $eexist = $!{EEXIST};
1079 0         0 my $errmsg = "$!";
1080              
1081             # If mkdir failed with EEXIST, that means the *name* $cache
1082             # already exists, but it might be the wrong kind of file.
1083 0 0 0     0 if (! $eexist || ! -d $cache)
1084             {
1085 0         0 require Cwd;
1086 0         0 my $cwd = Cwd::cwd();
1087 0         0 fatal "cannot create $cache in $cwd: $errmsg";
1088             }
1089             }
1090              
1091             # Open the index for update, and lock it. autom4te handles several
1092             # files, but the index is the first and last file to be updated, so
1093             # locking it is sufficient.
1094 0         0 $icache_file = new Autom4te::XFile $icache, O_RDWR|O_CREAT;
1095 0 0       0 $icache_file->lock (LOCK_EX)
1096             if ($flock_implemented eq "yes");
1097              
1098             # Read the cache index if available and younger than autom4te itself.
1099             # If the cache index is not younger, some structures such as C4che might
1100             # have changed, which would corrupt its processing.
1101 0 0 0     0 Autom4te::C4che->load ($icache_file)
      0        
1102             if (-f $icache && mtime ($icache) > mtime ($0)
1103             && Autom4te::C4che->good_version ($icache_file, '2.72'));
1104              
1105             # Add the new trace requests.
1106 0         0 my $req = Autom4te::C4che->request ('input' => \@ARGV,
1107             'path' => \@include,
1108             'macro' => [keys %trace, @preselect]);
1109              
1110             # If $REQ's cache files are not up to date, or simply if the user
1111             # discarded them (-f), declare it invalid.
1112 0 0 0     0 $req->valid (0)
1113             if $force || ! up_to_date ($req);
1114              
1115             # We now know whether we can trust the Request object. Say it.
1116 0         0 verb "the trace request object is:\n" . $req->marshall;
1117              
1118             # We need to run M4 if (i) the user wants it (--force), (ii) $REQ is
1119             # invalid.
1120 0 0 0     0 handle_m4 ($req, keys %{$req->macro})
  0         0  
1121             if $force || ! $req->valid;
1122              
1123             # Issue the warnings each time autom4te was run.
1124 0         0 my $separator = "\n" . ('-' x 25) . " END OF WARNING " . ('-' x 25) . "\n\n";
1125 0         0 handle_traces ($req, "$tmp/warnings",
1126             ('_m4_warn' => "\$1::\$f:\$l::\$2::\$3$separator"));
1127             # Swallow excessive newlines.
1128 0         0 for (split (/\n*$separator\n*/o, contents ("$tmp/warnings")))
1129             {
1130             # The message looks like:
1131             # | syntax::input.as:5::ouch
1132             # | ::input.as:4: baz is expanded from...
1133             # | input.as:2: bar is expanded from...
1134             # | input.as:3: foo is expanded from...
1135             # | input.as:5: the top level
1136             # In particular, m4_warn guarantees that either $stackdump is empty, or
1137             # it consists of lines where only the last line ends in "top level".
1138 0         0 my ($cat, $loc, $msg, $stacktrace) = split ('::', $_, 4);
1139             # There might not have been a stacktrace.
1140 0 0       0 $stacktrace = '' unless defined $stacktrace;
1141 0         0 msg $cat, $loc, $msg,
1142             partial => ($stacktrace =~ /top level$/) + 0;
1143 0         0 for (split /\n/, $stacktrace)
1144             {
1145 0         0 my ($loc, $trace) = split (': ', $_, 2);
1146 0         0 msg $cat, $loc, $trace, partial => ($trace !~ /top level$/) + 0;
1147             }
1148             }
1149              
1150             # Now output...
1151 0 0       0 if (%trace)
1152             {
1153             # Always produce traces, since even if the output is young enough,
1154             # there is no guarantee that the traces use the same *format*
1155             # (e.g., '-t FOO:foo' and '-t FOO:bar' are both using the same M4
1156             # traces, hence the M4 traces cache is usable, but its formatting
1157             # will yield different results).
1158 0         0 handle_traces ($req, $output, %trace);
1159             }
1160             else
1161             {
1162             # Actual M4 expansion, if the user wants it, or if $output is old
1163             # (STDOUT is pretty old).
1164 0 0 0     0 handle_output ($req, $output)
1165             if $force || mtime ($output) <= mtime ($ocache . $req->id);
1166             }
1167              
1168             # If we ran up to here, the cache is valid.
1169 0         0 $req->valid (1);
1170 0         0 Autom4te::C4che->save ($icache_file, '2.72');
1171              
1172 0         0 exit $exit_code;
1173              
1174             ### Setup "GNU" style for perl-mode and cperl-mode.
1175             ## Local Variables:
1176             ## perl-indent-level: 2
1177             ## perl-continued-statement-offset: 2
1178             ## perl-continued-brace-offset: 0
1179             ## perl-brace-offset: 0
1180             ## perl-brace-imaginary-offset: 0
1181             ## perl-label-offset: -2
1182             ## cperl-indent-level: 2
1183             ## cperl-brace-offset: 0
1184             ## cperl-continued-brace-offset: 0
1185             ## cperl-label-offset: -2
1186             ## cperl-extra-newline-before-brace: t
1187             ## cperl-merge-trailing-else: nil
1188             ## cperl-continued-statement-offset: 2
1189             ## End: