Reword the copyright notices to match what's suggested in GPLv3.
[autoconf/tsuna.git] / bin / autom4te.in
blob1612fe269695930e520099e1cb360eaed79131eb
1 #! @PERL@ -w
2 # -*- perl -*-
3 # @configure_input@
5 eval 'case $# in 0) exec @PERL@ -S "$0";; *) exec @PERL@ -S "$0" "$@";; esac'
6     if 0;
8 # autom4te - Wrapper around M4 libraries.
9 # Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007 Free Software
10 # Foundation, Inc.
12 # This program is free software: you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
26 BEGIN
28   my $datadir = $ENV{'autom4te_perllibdir'} || '@datadir@';
29   unshift @INC, $datadir;
31   # Override SHELL.  On DJGPP SHELL may not be set to a shell
32   # that can handle redirection and quote arguments correctly,
33   # e.g.: COMMAND.COM.  For DJGPP always use the shell that configure
34   # has detected.
35   $ENV{'SHELL'} = '@SHELL@' if ($^O eq 'dos');
38 use Autom4te::C4che;
39 use Autom4te::ChannelDefs;
40 use Autom4te::Channels;
41 use Autom4te::FileUtils;
42 use Autom4te::General;
43 use Autom4te::XFile;
44 use File::Basename;
45 use strict;
47 # Data directory.
48 my $datadir = $ENV{'AC_MACRODIR'} || '@datadir@';
50 # $LANGUAGE{LANGUAGE} -- Automatic options for LANGUAGE.
51 my %language;
53 my $output = '-';
55 # Mode of the output file except for traces.
56 my $mode = "0666";
58 # If melt, don't use frozen files.
59 my $melt = 0;
61 # Names of the cache directory, cache directory index, trace cache
62 # prefix, and output cache prefix.  And the IO object for the index.
63 my $cache;
64 my $icache;
65 my $tcache;
66 my $ocache;
67 my $icache_file;
69 # The macros to trace mapped to their format, as specified by the
70 # user.
71 my %trace;
73 # The macros the user will want to trace in the future.
74 # We need `include' to get the included file, `m4_pattern_forbid' and
75 # `m4_pattern_allow' to check the output.
77 # FIXME: What about `sinclude'?
78 my @preselect = ('include',
79                  'm4_pattern_allow', 'm4_pattern_forbid',
80                  '_m4_warn');
82 # M4 include path.
83 my @include;
85 # Do we freeze?
86 my $freeze = 0;
88 # $M4.
89 my $m4 = $ENV{"M4"} || '@M4@';
90 # Some non-GNU m4's don't reject the --help option, so give them /dev/null.
91 fatal "need GNU m4 1.4 or later: $m4"
92   if system "$m4 --help </dev/null 2>&1 | grep reload-state >/dev/null";
94 # Set some high recursion limit as the default limit, 250, has already
95 # been hit with AC_OUTPUT.  Don't override the user's choice.
96 $m4 .= ' --nesting-limit=1024'
97   if " $m4 " !~ / (--nesting-limit(=[0-9]+)?|-L[0-9]*) /;
100 # @M4_BUILTIN -- M4 builtins and a useful comment.
101 my @m4_builtin = `echo dumpdef | $m4 2>&1 >/dev/null`;
102 map { s/:.*//;s/\W// } @m4_builtin;
105 # %M4_BUILTIN_ALTERNATE_NAME
106 # --------------------------
107 # The builtins are renamed, e.g., `define' is renamed `m4_define'.
108 # So map `define' to `m4_define' and conversely.
109 # Some macros don't follow this scheme: be sure to properly map to their
110 # alternate name too.
112 # This is because GNU M4 1.4's tracing of builtins is buggy.  When run on
113 # this input:
115 # | divert(-1)
116 # | changequote([, ])
117 # | define([m4_eval], defn([eval]))
118 # | eval(1)
119 # | m4_eval(2)
120 # | undefine([eval])
121 # | m4_eval(3)
123 # it behaves this way:
125 # | % m4 input.m4 -da -t eval
126 # | m4trace: -1- eval(1)
127 # | m4trace: -1- m4_eval(2)
128 # | m4trace: -1- m4_eval(3)
129 # | %
131 # Conversely:
133 # | % m4 input.m4 -da -t m4_eval
134 # | %
136 # So we will merge them, i.e., tracing `BUILTIN' or tracing
137 # `m4_BUILTIN' will be the same: tracing both, but honoring the
138 # *last* trace specification.
140 # FIXME: This is not enough: in the output `$0' will be `BUILTIN'
141 # sometimes and `m4_BUILTIN' at others.  We should return a unique name,
142 # the one specified by the user.
144 # FIXME: To be absolutely rigorous, I would say that given that we
145 # _redefine_ divert (instead of _copying_ it), divert and the like
146 # should not be part of this list.
147 my %m4_builtin_alternate_name;
148 @m4_builtin_alternate_name{"$_", "m4_$_"} = ("m4_$_", "$_")
149   foreach (grep { !/m4wrap|m4exit|dnl|ifelse|__.*__/ } @m4_builtin);
150 @m4_builtin_alternate_name{"ifelse", "m4_if"}   = ("m4_if", "ifelse");
151 @m4_builtin_alternate_name{"m4exit", "m4_exit"} = ("m4_exit", "m4exit");
152 @m4_builtin_alternate_name{"m4wrap", "m4_wrap"} = ("m4_wrap", "m4wrap");
155 # $HELP
156 # -----
157 $help = "Usage: $0 [OPTION] ... [FILES]
159 Run GNU M4 on the FILES, avoiding useless runs.  Output the traces if tracing,
160 the frozen file if freezing, otherwise the expansion of the FILES.
162 If some of the FILES are named \`FILE.m4f\' they are considered to be M4
163 frozen files of all the previous files (which are therefore not loaded).
164 If \`FILE.m4f\' is not found, then \`FILE.m4\' will be used, together with
165 all the previous files.
167 Some files may be optional, i.e., will only be processed if found in the
168 include path, but then must end in \`.m4?\';  the question mark is not part of
169 the actual file name.
171 Operation modes:
172   -h, --help               print this help, then exit
173   -V, --version            print version number, then exit
174   -v, --verbose            verbosely report processing
175   -d, --debug              don\'t remove temporary files
176   -o, --output=FILE        save output in FILE (defaults to \`-\', stdout)
177   -f, --force              don\'t rely on cached values
178   -W, --warnings=CATEGORY  report the warnings falling in CATEGORY
179   -l, --language=LANG      specify the set of M4 macros to use
180   -C, --cache=DIRECTORY    preserve results for future runs in DIRECTORY
181       --no-cache           disable the cache
182   -m, --mode=OCTAL         change the non trace output file mode (0666)
183   -M, --melt               don\'t use M4 frozen files
185 Languages include:
186   \`Autoconf\'   create Autoconf configure scripts
187   \`Autotest\'   create Autotest test suites
188   \`M4sh\'       create M4sh shell scripts
189   \`M4sugar\'    create M4sugar output
191 " . Autom4te::ChannelDefs::usage . "
193 The environment variables \`M4\' and \`WARNINGS\' are honored.
195 Library directories:
196   -B, --prepend-include=DIR  prepend directory DIR to search path
197   -I, --include=DIR          append directory DIR to search path
199 Tracing:
200   -t, --trace=MACRO      report the MACRO invocations
201   -p, --preselect=MACRO  prepare to trace MACRO in a future run
203 Freezing:
204   -F, --freeze   produce an M4 frozen state file for FILES
206 Report bugs to <bug-autoconf\@gnu.org>.
209 # $VERSION
210 # --------
211 $version =  <<"EOF";
212 autom4te (@PACKAGE_NAME@) @VERSION@
213 Copyright (C) @RELEASE_YEAR@ Free Software Foundation, Inc.
214 This is free software.  You may redistribute copies of it under the terms of
215 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
216 There is NO WARRANTY, to the extent permitted by law.
218 Written by Akim Demaille.
222 ## ---------- ##
223 ## Routines.  ##
224 ## ---------- ##
227 # $OPTION
228 # files_to_options (@FILE)
229 # ------------------------
230 # Transform Autom4te conventions (e.g., using foo.m4f to designate a frozen
231 # file) into a suitable command line for M4 (e.g., using --reload-state).
232 sub files_to_options (@)
234   my (@file) = @_;
235   my @res;
236   foreach my $file (@file)
237     {
238       if ($file =~ /\.m4f$/)
239         {
240           push @res, "--reload-state=$file";
241         }
242       else
243         {
244           push @res, $file;
245         }
246     }
247   return join ' ', @res;
251 # load_configuration ($FILE)
252 # --------------------------
253 # Load the configuration $FILE.
254 sub load_configuration ($)
256   my ($file) = @_;
257   use Text::ParseWords;
259   my $cfg = new Autom4te::XFile ($file);
260   my $lang;
261   while ($_ = $cfg->getline)
262     {
263       chomp;
264       # Comments.
265       next
266         if /^\s*(\#.*)?$/;
268       my @words = shellwords ($_);
269       my $type = shift @words;
270       if ($type eq 'begin-language:')
271         {
272           fatal "$file:$.: end-language missing for: $lang"
273             if defined $lang;
274           $lang = lc $words[0];
275         }
276       elsif ($type eq 'end-language:')
277         {
278           error "$file:$.: end-language mismatch: $lang"
279             if $lang ne lc $words[0];
280           $lang = undef;
281         }
282       elsif ($type eq 'args:')
283         {
284           fatal "$file:$.: no current language"
285             unless defined $lang;
286           push @{$language{$lang}}, @words;
287         }
288       else
289         {
290           error "$file:$.: unknown directive: $type";
291         }
292     }
296 # parse_args ()
297 # -------------
298 # Process any command line arguments.
299 sub parse_args ()
301   # We want to look for the early options, which should not be found
302   # in the configuration file.  Prepend to the user arguments.
303   # Perform this repeatedly so that we can use --language in language
304   # definitions.  Beware that there can be several --language
305   # invocations.
306   my @language;
307   do {
308     @language = ();
309     use Getopt::Long;
310     Getopt::Long::Configure ("pass_through", "permute");
311     GetOptions ("l|language=s" => \@language);
313     foreach (@language)
314       {
315         error "unknown language: $_"
316           unless exists $language{lc $_};
317         unshift @ARGV, @{$language{lc $_}};
318       }
319   } while @language;
321   # --debug is useless: it is parsed below.
322   if (exists $ENV{'AUTOM4TE_DEBUG'})
323     {
324       print STDERR "$me: concrete arguments:\n";
325       foreach my $arg (@ARGV)
326         {
327           print STDERR "| $arg\n";
328         }
329     }
331   # Process the arguments for real this time.
332   my @trace;
333   my @prepend_include;
334   parse_WARNINGS;
335   getopt
336     (
337      # Operation modes:
338      "o|output=s"   => \$output,
339      "W|warnings=s" => \&parse_warnings,
340      "m|mode=s"     => \$mode,
341      "M|melt"       => \$melt,
343      # Library directories:
344      "B|prepend-include=s" => \@prepend_include,
345      "I|include=s"         => \@include,
347      # Tracing:
348      # Using a hash for traces is seducing.  Unfortunately, upon `-t FOO',
349      # instead of mapping `FOO' to undef, Getopt maps it to `1', preventing
350      # us from distinguishing `-t FOO' from `-t FOO=1'.  So let's do it
351      # by hand.
352      "t|trace=s"     => \@trace,
353      "p|preselect=s" => \@preselect,
355      # Freezing.
356      "F|freeze" => \$freeze,
358      # Caching.
359      "C|cache=s" => \$cache,
360      "no-cache"  => sub { $cache = undef; },
361     );
363   fatal "too few arguments
364 Try `$me --help' for more information."
365     unless @ARGV;
367   # Freezing:
368   # We cannot trace at the same time (well, we can, but it sounds insane).
369   # And it implies melting: there is risk not to update properly using
370   # old frozen files, and worse yet: we could load a frozen file and
371   # refreeze it!  A sort of caching :)
372   fatal "cannot freeze and trace"
373     if $freeze && @trace;
374   $melt = 1
375     if $freeze;
377   # Names of the cache directory, cache directory index, trace cache
378   # prefix, and output cache prefix.  If the cache is not to be
379   # preserved, default to a temporary directory (automatically removed
380   # on exit).
381   $cache = $tmp
382     unless $cache;
383   $icache = "$cache/requests";
384   $tcache = "$cache/traces.";
385   $ocache = "$cache/output.";
387   # Normalize the includes: the first occurrence is enough, several is
388   # a pain since it introduces a useless difference in the path which
389   # invalidates the cache.  And strip `.' which is implicit and always
390   # first.
391   @include = grep { !/^\.$/ } uniq (reverse(@prepend_include), @include);
393   # Convert @trace to %trace, and work around the M4 builtins tracing
394   # problem.
395   # The default format is `$f:$l:$n:$%'.
396   foreach (@trace)
397     {
398       /^([^:]+)(?::(.*))?$/ms;
399       $trace{$1} = defined $2 ? $2 : '$f:$l:$n:$%';
400       $trace{$m4_builtin_alternate_name{$1}} = $trace{$1}
401         if exists $m4_builtin_alternate_name{$1};
402     }
404   # Work around the M4 builtins tracing problem for @PRESELECT.
405   push (@preselect,
406         map { $m4_builtin_alternate_name{$_} }
407         grep { exists $m4_builtin_alternate_name{$_} } @preselect);
409   # If we find frozen files, then all the files before it are
410   # discarded: the frozen file is supposed to include them all.
411   #
412   # We don't want to depend upon m4's --include to find the top level
413   # files, so we use `find_file' here.  Try to get a canonical name,
414   # as it's part of the key for caching.  And some files are optional
415   # (also handled by `find_file').
416   my @argv;
417   foreach (@ARGV)
418     {
419       if ($_ eq '-')
420         {
421           push @argv, $_;
422         }
423       elsif (/\.m4f$/)
424         {
425           # Frozen files are optional => pass a `?' to `find_file'.
426           my $file = find_file ("$_?", @include);
427           if (!$melt && $file)
428             {
429               @argv = ($file);
430             }
431           else
432             {
433               s/\.m4f$/.m4/;
434               push @argv, find_file ($_, @include);
435             }
436         }
437       else
438         {
439           my $file = find_file ($_, @include);
440           push @argv, $file
441             if $file;
442         }
443     }
444   @ARGV = @argv;
448 # handle_m4 ($REQ, @MACRO)
449 # ------------------------
450 # Run m4 on the input files, and save the traces on the @MACRO.
451 sub handle_m4 ($@)
453   my ($req, @macro) = @_;
455   # GNU m4 appends when using --debugfile/--error-output.
456   unlink ($tcache . $req->id . "t");
458   # Run m4.
459   #
460   # We don't output directly to the cache files, to avoid problems
461   # when we are interrupted (that leaves corrupted files).
462   xsystem ("$m4"
463            . join (' --include=', '', @include)
464            . ' --debug=aflq'
465            . (!exists $ENV{'AUTOM4TE_NO_FATAL'} ? ' --fatal-warning' : '')
466            . " @M4_DEBUGFILE@=$tcache" . $req->id . "t"
467            . join (' --trace=',   '', sort @macro)
468            . " " . files_to_options (@ARGV)
469            . " >$ocache" . $req->id . "t");
471   # Everything went ok: preserve the outputs.
472   foreach my $file (map { $_ . $req->id } ($tcache, $ocache))
473     {
474       use File::Copy;
475       move ("${file}t", "$file")
476         or fatal "cannot rename ${file}t as $file: $!";
477     }
481 # warn_forbidden ($WHERE, $WORD, %FORBIDDEN)
482 # ------------------------------------------
483 # $WORD is forbidden.  Warn with a dedicated error message if in
484 # %FORBIDDEN, otherwise a simple `error: possibly undefined macro'
485 # will do.
486 my $first_warn_forbidden = 1;
487 sub warn_forbidden ($$%)
489   my ($where, $word, %forbidden) = @_;
490   my $message;
492   for my $re (sort keys %forbidden)
493     {
494       if ($word =~ $re)
495         {
496           $message = $forbidden{$re};
497           last;
498         }
499     }
500   $message ||= "possibly undefined macro: $word";
501   warn "$where: error: $message\n";
502   if ($first_warn_forbidden)
503     {
504       warn <<EOF;
505       If this token and others are legitimate, please use m4_pattern_allow.
506       See the Autoconf documentation.
508       $first_warn_forbidden = 0;
509     }
513 # handle_output ($REQ, $OUTPUT)
514 # -----------------------------
515 # Run m4 on the input files, perform quadrigraphs substitution, check for
516 # forbidden tokens, and save into $OUTPUT.
517 sub handle_output ($$)
519   my ($req, $output) = @_;
521   verb "creating $output";
523   # Load the forbidden/allowed patterns.
524   handle_traces ($req, "$tmp/patterns",
525                  ('m4_pattern_forbid' => 'forbid:$1:$2',
526                   'm4_pattern_allow'  => 'allow:$1'));
527   my @patterns = new Autom4te::XFile ("$tmp/patterns")->getlines;
528   chomp @patterns;
529   my %forbidden =
530     map { /^forbid:([^:]+):.+$/ => /^forbid:[^:]+:(.+)$/ } @patterns;
531   my $forbidden = join ('|', map { /^forbid:([^:]+)/ } @patterns) || "^\$";
532   my $allowed   = join ('|', map { /^allow:([^:]+)/  } @patterns) || "^\$";
534   verb "forbidden tokens: $forbidden";
535   verb "forbidden token : $_ => $forbidden{$_}"
536     foreach (sort keys %forbidden);
537   verb "allowed   tokens: $allowed";
539   # Read the (cached) raw M4 output, produce the actual result.  We
540   # have to use the 2nd arg to have Autom4te::XFile honor the third, but then
541   # stdout is to be handled by hand :(.  Don't use fdopen as it means
542   # we will close STDOUT, which we already do in END.
543   my $out = new Autom4te::XFile;
544   if ($output eq '-')
545     {
546       $out->open (">$output");
547     }
548   else
549     {
550       $out->open($output, O_CREAT | O_WRONLY | O_TRUNC, oct ($mode));
551     }
552   fatal "cannot create $output: $!"
553     unless $out;
554   my $in = new Autom4te::XFile ($ocache . $req->id);
556   my %prohibited;
557   my $res;
558   while ($_ = $in->getline)
559     {
560       s/\s+$//;
561       s/__oline__/$./g;
562       s/\@<:\@/[/g;
563       s/\@:>\@/]/g;
564       s/\@S\|\@/\$/g;
565       s/\@%:\@/#/g;
567       $res = $_;
569       # Don't complain in comments.  Well, until we have something
570       # better, don't consider `#include' etc. are comments.
571       s/\#.*//
572         unless /^\#\s*(if|include|endif|ifdef|ifndef|define)\b/;
573       foreach (split (/\W+/))
574         {
575           $prohibited{$_} = $.
576             if !/^$/ && /$forbidden/o && !/$allowed/o && ! exists $prohibited{$_};
577         }
579       # Performed *last*: the empty quadrigraph.
580       $res =~ s/\@&t\@//g;
582       print $out "$res\n";
583     }
585   # If no forbidden words, we're done.
586   return
587     if ! %prohibited;
589   # Locate the forbidden words in the last input file.
590   # This is unsatisfying but...
591   $exit_code = 1;
592   if ($ARGV[$#ARGV] ne '-')
593     {
594       my $prohibited = '\b(' . join ('|', keys %prohibited) . ')\b';
595       my $file = new Autom4te::XFile ($ARGV[$#ARGV]);
597       while ($_ = $file->getline)
598         {
599           # Don't complain in comments.  Well, until we have something
600           # better, don't consider `#include' etc. to be comments.
601           s/\#.*//
602             unless /^\#(if|include|endif|ifdef|ifndef|define)\b/;
604           # Complain once per word, but possibly several times per line.
605           while (/$prohibited/)
606             {
607               my $word = $1;
608               warn_forbidden ("$ARGV[$#ARGV]:$.", $word, %forbidden);
609               delete $prohibited{$word};
610               # If we're done, exit.
611               return
612                 if ! %prohibited;
613               $prohibited = '\b(' . join ('|', keys %prohibited) . ')\b';
614             }
615         }
616     }
617   warn_forbidden ("$output:$prohibited{$_}", $_, %forbidden)
618     foreach (sort { $prohibited{$a} <=> $prohibited{$b} } keys %prohibited);
622 ## --------------------- ##
623 ## Handling the traces.  ##
624 ## --------------------- ##
627 # $M4_MACRO
628 # trace_format_to_m4 ($FORMAT)
629 # ----------------------------
630 # Convert a trace $FORMAT into a M4 trace processing macro's body.
631 sub trace_format_to_m4 ($)
633   my ($format) = @_;
634   my $underscore = $_;
635   my %escape = (# File name.
636                 'f' => '$1',
637                 # Line number.
638                 'l' => '$2',
639                 # Depth.
640                 'd' => '$3',
641                 # Name (also available as $0).
642                 'n' => '$4',
643                 # Escaped dollar.
644                 '$' => '$');
646   my $res = '';
647   $_ = $format;
648   while ($_)
649     {
650       # $n -> $(n + 4)
651       if (s/^\$(\d+)//)
652         {
653           $res .= "\$" . ($1 + 4);
654         }
655       # $x, no separator given.
656       elsif (s/^\$([fldn\$])//)
657         {
658           $res .= $escape{$1};
659         }
660       # $.x or ${sep}x.
661       elsif (s/^\$\{([^}]*)\}([@*%])//
662             || s/^\$(.?)([@*%])//)
663         {
664           # $@, list of quoted effective arguments.
665           if ($2 eq '@')
666             {
667               $res .= ']at_at([' . ($1 ? $1 : ',') . '], $@)[';
668             }
669           # $*, list of unquoted effective arguments.
670           elsif ($2 eq '*')
671             {
672               $res .= ']at_star([' . ($1 ? $1 : ',') . '], $@)[';
673             }
674           # $%, list of flattened unquoted effective arguments.
675           elsif ($2 eq '%')
676             {
677               $res .= ']at_percent([' . ($1 ? $1 : ':') . '], $@)[';
678             }
679         }
680       elsif (/^(\$.)/)
681         {
682           error "invalid escape: $1";
683         }
684       else
685         {
686           s/^([^\$]+)//;
687           $res .= $1;
688         }
689     }
691   $_ = $underscore;
692   return '[[' . $res . ']]';
696 # handle_traces($REQ, $OUTPUT, %TRACE)
697 # ------------------------------------
698 # We use M4 itself to process the traces.  But to avoid name clashes when
699 # processing the traces, the builtins are disabled, and moved into `at_'.
700 # Actually, all the low level processing macros are in `at_' (and `_at_').
701 # To avoid clashes between user macros and `at_' macros, the macros which
702 # implement tracing are in `AT_'.
704 # Having $REQ is needed to neutralize the macros which have been traced,
705 # but are not wanted now.
706 sub handle_traces ($$%)
708   my ($req, $output, %trace) = @_;
710   verb "formatting traces for `$output': " . join (', ', sort keys %trace);
712   # Processing the traces.
713   my $trace_m4 = new Autom4te::XFile (">$tmp/traces.m4");
715   $_ = <<'EOF';
716   divert(-1)
717   changequote([, ])
718   # _at_MODE(SEPARATOR, ELT1, ELT2...)
719   # ----------------------------------
720   # List the elements, separating then with SEPARATOR.
721   # MODE can be:
722   #  `at'       -- the elements are enclosed in brackets.
723   #  `star'     -- the elements are listed as are.
724   #  `percent'  -- the elements are `flattened': spaces are singled out,
725   #                and no new line remains.
726   define([_at_at],
727   [at_ifelse([$#], [1], [],
728              [$#], [2], [[[$2]]],
729              [[[$2]][$1]$0([$1], at_shift(at_shift($@)))])])
731   define([_at_percent],
732   [at_ifelse([$#], [1], [],
733              [$#], [2], [at_flatten([$2])],
734              [at_flatten([$2])[$1]$0([$1], at_shift(at_shift($@)))])])
736   define([_at_star],
737   [at_ifelse([$#], [1], [],
738              [$#], [2], [[$2]],
739              [[$2][$1]$0([$1], at_shift(at_shift($@)))])])
741   # FLATTEN quotes its result.
742   # Note that the second pattern is `newline, tab or space'.  Don't lose
743   # the tab!
744   define([at_flatten],
745   [at_patsubst(at_patsubst(at_patsubst(at_patsubst([[[[$1]]]], [\\\n]),
746                                        [[\n\t ]+], [ ]),
747                            [ *\(.\)$], [\1]),
748                [^ *\(.*\)], [[\1]])])
750   define([at_args],    [at_shift(at_shift(at_shift(at_shift(at_shift($@)))))])
751   define([at_at],      [_$0([$1], at_args($@))])
752   define([at_percent], [_$0([$1], at_args($@))])
753   define([at_star],    [_$0([$1], at_args($@))])
756   s/^  //mg;s/\\t/\t/mg;s/\\n/\n/mg;
757   print $trace_m4 $_;
759   # If you trace `define', then on `define([m4_exit], defn([m4exit])' you
760   # will produce
761   #
762   #    AT_define([m4sugar.m4], [115], [1], [define], [m4_exit], <m4exit>)
763   #
764   # Since `<m4exit>' is not quoted, the outer m4, when processing
765   # `trace.m4' will exit prematurely.  Hence, move all the builtins to
766   # the `at_' name space.
768   print $trace_m4 "# Copy the builtins.\n";
769   map { print $trace_m4 "define([at_$_], defn([$_]))\n" } @m4_builtin;
770   print $trace_m4 "\n";
772   print $trace_m4 "# Disable them.\n";
773   map { print $trace_m4 "at_undefine([$_])\n" } @m4_builtin;
774   print $trace_m4 "\n";
777   # Neutralize traces: we don't want traces of cached requests (%REQUEST).
778   print $trace_m4
779    "## -------------------------------------- ##\n",
780    "## By default neutralize all the traces.  ##\n",
781    "## -------------------------------------- ##\n",
782    "\n";
783   print $trace_m4 "at_define([AT_$_], [at_dnl])\n"
784     foreach (sort keys %{$req->macro});
785   print $trace_m4 "\n";
787   # Implement traces for current requests (%TRACE).
788   print $trace_m4
789     "## ------------------------- ##\n",
790     "## Trace processing macros.  ##\n",
791     "## ------------------------- ##\n",
792     "\n";
793   foreach (sort keys %trace)
794     {
795       # Trace request can be embed \n.
796       (my $comment = "Trace $_:$trace{$_}") =~ s/^/\# /;
797       print $trace_m4 "$comment\n";
798       print $trace_m4 "at_define([AT_$_],\n";
799       print $trace_m4 trace_format_to_m4 ($trace{$_}) . ")\n\n";
800     }
801   print $trace_m4 "\n";
803   # Reenable output.
804   print $trace_m4 "at_divert(0)at_dnl\n";
806   # Transform the traces from m4 into an m4 input file.
807   # Typically, transform:
808   #
809   # | m4trace:configure.ac:3: -1- AC_SUBST([exec_prefix], [NONE])
810   #
811   # into
812   #
813   # | AT_AC_SUBST([configure.ac], [3], [1], [AC_SUBST], [exec_prefix], [NONE])
814   #
815   # Pay attention that the file name might include colons, if under DOS
816   # for instance, so we don't use `[^:]+'.
817   my $traces = new Autom4te::XFile ($tcache . $req->id);
818   while ($_ = $traces->getline)
819     {
820       # Trace with arguments, as the example above.  We don't try
821       # to match the trailing parenthesis as it might be on a
822       # separate line.
823       s{^m4trace:(.+):(\d+): -(\d+)- ([^(]+)\((.*)$}
824        {AT_$4([$1], [$2], [$3], [$4], $5};
825       # Traces without arguments, always on a single line.
826       s{^m4trace:(.+):(\d+): -(\d+)- ([^)]*)\n$}
827        {AT_$4([$1], [$2], [$3], [$4])\n};
828       print $trace_m4 "$_";
829     }
830   $trace_m4->close;
832   my $in = new Autom4te::XFile ("$m4 $tmp/traces.m4 |");
833   my $out = new Autom4te::XFile (">$output");
835   # This is dubious: should we really transform the quadrigraphs in
836   # traces?  It might break balanced [ ] etc. in the output.  The
837   # consensus seeems to be that traces are more useful this way.
838   while ($_ = $in->getline)
839     {
840       # It makes no sense to try to transform __oline__.
841       s/\@<:\@/[/g;
842       s/\@:>\@/]/g;
843       s/\@S\|\@/\$/g;
844       s/\@%:\@/#/g;
845       s/\@&t\@//g;
846       print $out $_;
847     }
851 # $BOOL
852 # up_to_date ($REQ)
853 # -----------------
854 # Are the cache files of $REQ up to date?
855 # $REQ is `valid' if it corresponds to the request and exists, which
856 # does not mean it is up to date.  It is up to date if, in addition,
857 # its files are younger than its dependencies.
858 sub up_to_date ($)
860   my ($req) = @_;
862   return 0
863     if ! $req->valid;
865   my $tfile = $tcache . $req->id;
866   my $ofile = $ocache . $req->id;
868   # We can't answer properly if the traces are not computed since we
869   # need to know what other files were included.  Actually, if any of
870   # the cache files is missing, we are not up to date.
871   return 0
872     if ! -f $tfile || ! -f $ofile;
874   # The youngest of the cache files must be older than the oldest of
875   # the dependencies.
876   my $tmtime = mtime ($tfile);
877   my $omtime = mtime ($ofile);
878   my ($file, $mtime) = ($tmtime < $omtime
879                         ? ($ofile, $omtime) : ($tfile, $tmtime));
881   # We depend at least upon the arguments.
882   my @dep = @ARGV;
884   # stdin is always out of date.
885   if (grep { $_ eq '-' } @dep)
886     { return 0 }
888   # Files may include others.  We can use traces since we just checked
889   # if they are available.
890   handle_traces ($req, "$tmp/dependencies",
891                  ('include'    => '$1',
892                   'm4_include' => '$1'));
893   my $deps = new Autom4te::XFile ("$tmp/dependencies");
894   while ($_ = $deps->getline)
895     {
896       chomp;
897       my $file = find_file ("$_?", @include);
898       # If a file which used to be included is no longer there, then
899       # don't say it's missing (it might no longer be included).  But
900       # of course, that causes the output to be outdated (as if the
901       # time stamp of that missing file was newer).
902       return 0
903         if ! $file;
904       push @dep, $file;
905     }
907   # If $FILE is younger than one of its dependencies, it is outdated.
908   return up_to_date_p ($file, @dep);
912 ## ---------- ##
913 ## Freezing.  ##
914 ## ---------- ##
916 # freeze ($OUTPUT)
917 # ----------------
918 sub freeze ($)
920   my ($output) = @_;
922   # When processing the file with diversion disabled, there must be no
923   # output but comments and empty lines.
924   my $result = xqx ("$m4"
925                     . ' --fatal-warning'
926                     . join (' --include=', '', @include)
927                     . ' --define=divert'
928                     . " " . files_to_options (@ARGV)
929                     . ' </dev/null');
930   $result =~ s/#.*\n//g;
931   $result =~ s/^\n//mg;
933   fatal "freezing produced output:\n$result"
934     if $result;
936   # If freezing produces output, something went wrong: a bad `divert',
937   # or an improper paren etc.
938   xsystem ("$m4"
939            . ' --fatal-warning'
940            . join (' --include=', '', @include)
941            . " --freeze-state=$output"
942            . " " . files_to_options (@ARGV)
943            . ' </dev/null');
946 ## -------------- ##
947 ## Main program.  ##
948 ## -------------- ##
950 mktmpdir ('am4t');
951 load_configuration ($ENV{'AUTOM4TE_CFG'} || "$datadir/autom4te.cfg");
952 load_configuration ("$ENV{'HOME'}/.autom4te.cfg")
953   if exists $ENV{'HOME'} && -f "$ENV{'HOME'}/.autom4te.cfg";
954 load_configuration (".autom4te.cfg")
955   if -f ".autom4te.cfg";
956 parse_args;
958 # Freezing does not involve the cache.
959 if ($freeze)
960   {
961     freeze ($output);
962     exit $exit_code;
963   }
965 # We need our cache directory.
966 if (! -d "$cache")
967   {
968     mkdir "$cache", 0755
969       or fatal "cannot create $cache: $!";
970   }
972 # Open the index for update, and lock it.  autom4te handles several
973 # files, but the index is the first and last file to be updated, so
974 # locking it is sufficient.
975 $icache_file = new Autom4te::XFile $icache, O_RDWR|O_CREAT;
976 $icache_file->lock (LOCK_EX);
978 # Read the cache index if available and older than autom4te itself.
979 # If autom4te is younger, then some structures such as C4che might
980 # have changed, which would corrupt its processing.
981 Autom4te::C4che->load ($icache_file)
982   if -f $icache && mtime ($icache) > mtime ($0);
984 # Add the new trace requests.
985 my $req = Autom4te::C4che->request ('input' => \@ARGV,
986                                     'path'  => \@include,
987                                     'macro' => [keys %trace, @preselect]);
989 # If $REQ's cache files are not up to date, or simply if the user
990 # discarded them (-f), declare it invalid.
991 $req->valid (0)
992   if $force || ! up_to_date ($req);
994 # We now know whether we can trust the Request object.  Say it.
995 verb "the trace request object is:\n" . $req->marshall;
997 # We need to run M4 if (i) the user wants it (--force), (ii) $REQ is
998 # invalid.
999 handle_m4 ($req, keys %{$req->macro})
1000   if $force || ! $req->valid;
1002 # Issue the warnings each time autom4te was run.
1003 my $separator = "\n" . ('-' x 25) . " END OF WARNING " . ('-' x 25) . "\n\n";
1004 handle_traces ($req, "$tmp/warnings",
1005                ('_m4_warn' => "\$1::\$f:\$l::\$2::\$3$separator"));
1006 # Swallow excessive newlines.
1007 for (split (/\n*$separator\n*/o, contents ("$tmp/warnings")))
1009   # The message looks like:
1010   # | syntax::input.as:5::ouch
1011   # | ::input.as:4: baz is expanded from...
1012   # | input.as:2: bar is expanded from...
1013   # | input.as:3: foo is expanded from...
1014   # | input.as:5: the top level
1015   my ($cat, $loc, $msg, $stacktrace) = split ('::', $_, 4);
1016   msg $cat, $loc, "warning: $msg";
1017   for (split /\n/, $stacktrace)
1018     {
1019       my ($loc, $trace) = split (': ', $_, 2);
1020       msg $cat, $loc, $trace;
1021     }
1024 # Now output...
1025 if (%trace)
1026   {
1027     # Always produce traces, since even if the output is young enough,
1028     # there is no guarantee that the traces use the same *format*
1029     # (e.g., `-t FOO:foo' and `-t FOO:bar' are both using the same M4
1030     # traces, hence the M4 traces cache is usable, but its formatting
1031     # will yield different results).
1032     handle_traces ($req, $output, %trace);
1033   }
1034 else
1035   {
1036     # Actual M4 expansion, if the user wants it, or if $output is old
1037     # (STDOUT is pretty old).
1038     handle_output ($req, $output)
1039       if $force || mtime ($output) < mtime ($ocache . $req->id);
1040   }
1042 # If we ran up to here, the cache is valid.
1043 $req->valid (1);
1044 Autom4te::C4che->save ($icache_file);
1046 exit $exit_code;
1048 ### Setup "GNU" style for perl-mode and cperl-mode.
1049 ## Local Variables:
1050 ## perl-indent-level: 2
1051 ## perl-continued-statement-offset: 2
1052 ## perl-continued-brace-offset: 0
1053 ## perl-brace-offset: 0
1054 ## perl-brace-imaginary-offset: 0
1055 ## perl-label-offset: -2
1056 ## cperl-indent-level: 2
1057 ## cperl-brace-offset: 0
1058 ## cperl-continued-brace-offset: 0
1059 ## cperl-label-offset: -2
1060 ## cperl-extra-newline-before-brace: t
1061 ## cperl-merge-trailing-else: nil
1062 ## cperl-continued-statement-offset: 2
1063 ## End: