1#! @PERL@ -w
2# -*- perl -*-
3# @configure_input@
4
5# autoupdate - modernize an Autoconf file.
6# Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
7# Free Software Foundation, Inc.
8
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2, or (at your option)
12# any later version.
13
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22# 02110-1301, USA.
23
24# Originally written by David MacKenzie <djm@gnu.ai.mit.edu>.
25# Rewritten by Akim Demaille <akim@freefriends.org>.
26
27eval 'case $# in 0) exec @PERL@ -S "$0";; *) exec @PERL@ -S "$0" "$@";; esac'
28    if 0;
29
30BEGIN
31{
32  my $datadir = $ENV{'autom4te_perllibdir'} || '@datadir@';
33  unshift @INC, $datadir;
34
35  # Override SHELL.  On DJGPP SHELL may not be set to a shell
36  # that can handle redirection and quote arguments correctly,
37  # e.g.: COMMAND.COM.  For DJGPP always use the shell that configure
38  # has detected.
39  $ENV{'SHELL'} = '@SHELL@' if ($^O eq 'dos');
40}
41
42use Autom4te::ChannelDefs;
43use Autom4te::Channels;
44use Autom4te::Configure_ac;
45use Autom4te::FileUtils;
46use Autom4te::General;
47use Autom4te::XFile;
48use File::Basename;
49use strict;
50
51# Lib files.
52my $autom4te = $ENV{'AUTOM4TE'} || '@bindir@/@autom4te-name@';
53my $autoconf = "$autom4te --language=autoconf";
54# We need to find m4sugar.
55my @prepend_include;
56my @include = ('@datadir@');
57my $force = 0;
58# m4.
59my $m4 = $ENV{"M4"} || '@M4@';
60
61
62# $HELP
63# -----
64$help = "Usage: $0 [OPTION] ...  [TEMPLATE-FILE...]
65
66Update the TEMPLATE-FILE... if given, or `configure.ac' if present,
67or else `configure.in', to the syntax of the current version of
68Autoconf.  The original files are backed up.
69
70Operation modes:
71  -h, --help                 print this help, then exit
72  -V, --version              print version number, then exit
73  -v, --verbose              verbosely report processing
74  -d, --debug                don't remove temporary files
75  -f, --force                consider all files obsolete
76
77Library directories:
78  -B, --prepend-include=DIR  prepend directory DIR to search path
79  -I, --include=DIR          append directory DIR to search path
80
81Report bugs to <bug-autoconf\@gnu.org>.
82";
83
84# $VERSION
85# --------
86$version = "autoupdate (@PACKAGE_NAME@) @VERSION@
87Copyright (C) 2006 Free Software Foundation, Inc.
88This is free software.  You may redistribute copies of it under the terms of
89the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
90There is NO WARRANTY, to the extent permitted by law.
91
92Written by David J. MacKenzie and Akim Demaille.
93";
94
95## ---------- ##
96## Routines.  ##
97## ---------- ##
98
99
100# parse_args ()
101# -------------
102# Process any command line arguments.
103sub parse_args ()
104{
105  my $srcdir;
106
107  getopt ('I|include=s'         => \@include,
108	  'B|prepend-include=s' => \@prepend_include,
109	  'f|force'             => \$force);
110
111  if (! @ARGV)
112    {
113      my $configure_ac = require_configure_ac;
114      push @ARGV, $configure_ac;
115    }
116}
117
118
119
120# ----------------- #
121# Autoconf macros.  #
122# ----------------- #
123
124my (%ac_macros, %au_macros, %m4_builtins);
125
126# HANDLE_AUTOCONF_MACROS ()
127# -------------------------
128# @M4_BUILTINS -- M4 builtins and a useful comment.
129sub handle_autoconf_macros ()
130{
131  # Get the builtins.
132  xsystem ("echo dumpdef | $m4 2>$tmp/m4.defs >/dev/null");
133  my $m4_defs = new Autom4te::XFile "$tmp/m4.defs";
134  while ($_ = $m4_defs->getline)
135    {
136      $m4_builtins{$1} = 1
137	if /^(\w+):/;
138    }
139  $m4_defs->close;
140
141  my $macros = new Autom4te::XFile ("$autoconf"
142				    . " --trace AU_DEFINE:'AU:\$f:\$1'"
143				    . " --trace define:'AC:\$f:\$1'"
144				    . " --melt /dev/null |");
145  while ($_ = $macros->getline)
146    {
147      chomp;
148      my ($domain, $file, $macro) = /^(AC|AU):(.*):([^:]*)$/ or next;
149      if ($domain eq "AU")
150	{
151	  $au_macros{$macro} = 1;
152	}
153      elsif ($file =~ /(^|\/)m4sugar\/(m4sugar|version)\.m4$/)
154	{
155	  # Add the m4sugar macros to m4_builtins.
156	  $m4_builtins{$macro} = 1;
157	}
158      else
159	{
160	  # Autoconf, aclocal, and m4sh macros.
161	  $ac_macros{$macro} = 1;
162	}
163    }
164  $macros->close;
165
166
167  # Don't keep AU macros in @AC_MACROS.
168  delete $ac_macros{$_}
169    foreach (keys %au_macros);
170  # Don't keep M4sugar macros which are redefined by Autoconf,
171  # such as `builtin', `changequote' etc.  See autoconf/autoconf.m4.
172  delete $ac_macros{$_}
173    foreach (keys %m4_builtins);
174  error "no current Autoconf macros found"
175    unless keys %ac_macros;
176  error "no obsolete Autoconf macros found"
177    unless keys %au_macros;
178
179  if ($debug)
180    {
181      print STDERR "Current Autoconf macros:\n";
182      print STDERR join (' ', sort keys %ac_macros) . "\n\n";
183      print STDERR "Obsolete Autoconf macros:\n";
184      print STDERR join (' ', sort keys %au_macros) . "\n\n";
185    }
186
187  # ac.m4 -- autoquoting definitions of the AC macros (M4sugar excluded).
188  # unac.m4 -- undefine the AC macros.
189  my $ac_m4 = new Autom4te::XFile ">$tmp/ac.m4";
190  print $ac_m4 "# ac.m4 -- autoquoting definitions of the AC macros.\n";
191  my $unac_m4 = new Autom4te::XFile ">$tmp/unac.m4";
192  print $unac_m4 "# unac.m4 -- undefine the AC macros.\n";
193  foreach (sort keys %ac_macros)
194    {
195      print $ac_m4   "_au_m4_define([$_], [m4_if(\$#, 0, [[\$0]], [[\$0(\$\@)]])])\n";
196      print $unac_m4 "_au_m4_undefine([$_])\n";
197    }
198
199  # m4save.m4 -- save the m4 builtins.
200  # unm4.m4 -- disable the m4 builtins.
201  # m4.m4 -- enable the m4 builtins.
202  my $m4save_m4 = new Autom4te::XFile ">$tmp/m4save.m4";
203  print $m4save_m4 "# m4save.m4 -- save the m4 builtins.\n";
204  my $unm4_m4 = new Autom4te::XFile ">$tmp/unm4.m4";
205  print $unm4_m4 "# unm4.m4 -- disable the m4 builtins.\n";
206  my $m4_m4 = new Autom4te::XFile ">$tmp/m4.m4";
207  print $m4_m4 "# m4.m4 -- enable the m4 builtins.\n";
208  foreach (sort keys %m4_builtins)
209    {
210      print $m4save_m4 "_au__save([$_])\n";
211      print $unm4_m4   "_au__undefine([$_])\n";
212      print $m4_m4     "_au__restore([$_])\n";
213    }
214}
215
216
217## -------------- ##
218## Main program.  ##
219## -------------- ##
220
221parse_args;
222$autoconf .= " --debug" if $debug;
223$autoconf .= " --force" if $force;
224$autoconf .= " --verbose" if $verbose;
225$autoconf .= join (' --include=', '', @include);
226$autoconf .= join (' --prepend-include=', '', @prepend_include);
227
228mktmpdir ('au');
229handle_autoconf_macros;
230
231# $au_changequote -- enable the quote `[', `]' right before any AU macro.
232my $au_changequote =
233  's/\b(' . join ('|', keys %au_macros) . ')\b/_au_m4_changequote([,])$1/g';
234
235# au.m4 -- definitions the AU macros.
236xsystem ("$autoconf --trace AU_DEFINE:'_au_defun(\@<:\@\$1\@:>\@,
237\@<:\@\$2\@:>\@)' --melt /dev/null "
238	. ">$tmp/au.m4");
239
240
241
242## ------------------- ##
243## Process the files.  ##
244## ------------------- ##
245
246foreach my $file (@ARGV)
247  {
248    # We need an actual file.
249    if ($file eq '-')
250      {
251	$file = "$tmp/stdin";
252	system "cat >$file";
253      }
254    elsif (! -r "$file")
255      {
256	die "$me: $file: No such file or directory";
257      }
258
259    # input.m4 -- m4 program to produce the updated file.
260    # Load the values, the dispatcher, neutralize m4, and the prepared
261    # input file.
262    my $input_m4 = <<\EOF;
263      divert(-1)                                            -*- Autoconf -*-
264      changequote([,])
265
266      # Define our special macros:
267      define([_au__defn], defn([defn]))
268      define([_au__divert], defn([divert]))
269      define([_au__include], defn([include]))
270      define([_au__undefine], defn([undefine]))
271      define([_au__save], [m4_ifdef([$1], [m4_copy([$1], [_au_$1])])])
272      define([_au__restore],
273	[_au_m4_ifdef([_au_$1],
274	  [_au_m4_define([$1], _au__defn([_au_$1]))])])
275
276      # Set up m4sugar.
277      include(m4sugar/m4sugar.m4)
278
279      # Redefine __file__ to make warnings nicer; $file is replaced below.
280      m4_define([__file__], [$file])
281
282      # Redefine m4_location to fix the line number.
283      m4_define([m4_location], [__file__:m4_eval(__line__ - _au__first_line)])
284
285      # Move all the builtins into the `_au_' pseudo namespace
286      m4_include([m4save.m4])
287
288      # _au_defun(NAME, BODY)
289      # ---------------------
290      # Define NAME to BODY, plus AU activation/deactivation.
291      _au_m4_define([_au_defun],
292      [_au_m4_define([$1],
293      [_au_enable()dnl
294      $2[]dnl
295      _au_disable()])])
296
297      # Import the definition of the obsolete macros.
298      _au__include([au.m4])
299
300
301      ## ------------------------ ##
302      ## _au_enable/_au_disable.  ##
303      ## ------------------------ ##
304
305      # They work by pair: each time an AU macro is activated, it runs
306      # _au_enable, and at its end its runs _au_disable (see _au_defun
307      # above).  AU macros might use AU macros, which should
308      # enable/disable only for the outer AU macros.
309      #
310      # `_au_enabled' is used to this end, determining whether we really
311      # enable/disable.
312
313
314      # __au_enable
315      # -----------
316      # Reenable the builtins, m4sugar, and the autoquoting AC macros.
317      _au_m4_define([__au_enable],
318      [_au__divert(-1)
319      # Enable special characters.
320      _au_m4_changecom([#])
321
322      _au__include([m4.m4])
323      _au__include([ac.m4])
324
325      _au__divert(0)])
326
327      # _au_enable
328      # ----------
329      # Called at the beginning of all the obsolete macros.  If this is the
330      # outermost level, call __au_enable.
331      _au_m4_define([_au_enable],
332      [_au_m4_ifdef([_au_enabled],
333		 [],
334		 [__au_enable()])_au_dnl
335      _au_m4_pushdef([_au_enabled])])
336
337
338      # __au_disable
339      # ------------
340      # Disable the AC autoquoting macros, m4sugar, and m4.
341      _au_m4_define([__au_disable],
342      [_au__divert(-1)
343      _au__include([unac.m4])
344      _au__include([unm4.m4])
345
346      # Disable special characters.
347      _au_m4_changequote()
348      _au_m4_changecom()
349
350      _au__divert(0)])
351
352      # _au_disable
353      # -----------
354      # Called at the end of all the obsolete macros.  If we are at the
355      # outermost level, call __au_disable.
356      _au_m4_define([_au_disable],
357      [_au_m4_popdef([_au_enabled])_au_dnl
358      _au_m4_ifdef([_au_enabled],
359		[],
360		[__au_disable()])])
361
362
363      ## ------------------------------- ##
364      ## Disable, and process the file.  ##
365      ## ------------------------------- ##
366      # The AC autoquoting macros are not loaded yet, hence invoking
367      # `_au_disable' would be wrong.
368      _au__include([unm4.m4])
369
370      # Disable special characters, and set the first line number.
371      _au_m4_changequote()
372      _au_m4_changecom()
373
374      _au_m4_define(_au__first_line, _au___line__)_au__divert(0)_au_dnl
375EOF
376
377    $input_m4 =~ s/^      //mg;
378    $input_m4 =~ s/\$file/$file/g;
379
380    # prepared input -- input, but reenables the quote before each AU macro.
381    open INPUT_M4, ">$tmp/input.m4"
382       or error "cannot open: $!";
383    open FILE, "<$file"
384       or error "cannot open: $!";
385    print INPUT_M4 "$input_m4";
386    while (<FILE>)
387       {
388	 eval $au_changequote;
389	 print INPUT_M4;
390       }
391    close FILE
392       or error "cannot close $file: $!";
393    close INPUT_M4
394       or error "cannot close $tmp/input.m4: $!";
395
396    # Now ask m4 to perform the update.
397    xsystem ("$m4 --include=$tmp"
398	     . join (' --include=', '', reverse (@prepend_include))
399	     . join (' --include=', '', @include)
400	     . " $tmp/input.m4 >$tmp/updated");
401    update_file ("$tmp/updated",
402		 "$file" eq "$tmp/stdin" ? '-' : "$file");
403  }
404exit 0;
405
406
407#		  ## ---------------------------- ##
408#		  ## How `autoupdate' functions.  ##
409#		  ## ---------------------------- ##
410#
411# The task of `autoupdate' is not trivial: the biggest difficulty being
412# that you must limit the changes to the parts that really need to be
413# updated.  Finding a satisfying implementation proved to be quite hard,
414# as this is the fifth implementation of `autoupdate'.
415#
416# Below, we will use a simple example of obsolete macro:
417#
418#     AU_DEFUN([OLD], [NEW([$1, $2], m4_eval([$1 + $2]))])
419#     AC_DEFUN([NEW], [echo "sum($1) = $2"])
420#
421# the input file contains
422#
423#     dnl The Unbelievable Truth
424#     OLD(1, 2)
425#     NEW([0, 0], [0])
426#
427# Of course the expected output is
428#
429#     dnl The Unbelievable Truth
430#     NEW([1, 2], [3])
431#     NEW([0, 0], [0])
432#
433#
434# # First implementation: sed
435# # =========================
436#
437# The first implementation was only able to change the name of obsolete
438# macros.
439#
440# The file `acoldnames.m4' defined the old names based on the new names.
441# It was simple then to produce a sed script such as:
442#
443#     s/OLD/NEW/g
444#
445# Updating merely consisted in running this script on the file to
446# update.
447#
448# This scheme suffers an obvious limitation: that `autoupdate' was
449# unable to cope with new macros that just swap some of its arguments
450# compared to the old macro.  Fortunately, that was enough to upgrade
451# from Autoconf 1 to Autoconf 2.  (But I have no idea whether the
452# changes in Autoconf 2 were precisely limited by this constraint.)
453#
454#
455# # Second implementation: hooks
456# # ============================
457#
458# The version 2.15 of Autoconf brought a vast number of changes compared
459# to 2.13, so a solution was needed.  One could think to extend the
460# `sed' scripts with specialized code for complex macros.  But this
461# approach is of course full of flaws:
462#
463# a. the Autoconf maintainers have to write these snippets, which we
464#    just don't want to,
465#
466# b. I really don't think you'll ever manage to handle the quoting of
467#    m4 from sed.
468#
469# To satisfy a., let's remark that the code which implements the old
470# features in term of the new feature is exactly the code which should
471# replace the old code.
472#
473# To answer point b, as usual in the history of Autoconf, the answer, at
474# least on the paper, is simple: m4 is the best tool to parse m4, so
475# let's use m4.
476#
477# Therefore the specification is:
478#
479#     I want to be able to tell Autoconf, well, m4, that the macro I
480#     am currently defining is an obsolete macro (so that the user is
481#     warned), which code is the code to use when running autoconf,
482#     but that the very same code has to be used when running
483#     autoupdate.  To summarize, the interface I want is
484#     `AU_DEFUN(OLD-NAME, NEW-CODE)'.
485#
486#
487# Now for the technical details.
488#
489# When running autoconf, except for the warning, AU_DEFUN is basically
490# AC_DEFUN.
491#
492# When running autoupdate, we want *only* OLD-NAMEs to be expanded.
493# This obviously means that acgeneral.m4 and acspecific.m4 must not be
494# loaded.  Nonetheless, because we want to use a rich set of m4
495# features, m4sugar.m4 is needed.  Please note that the fact that
496# Autoconf's macros are not loaded is positive on two points:
497#
498# - we do get an updated `configure.ac', not a `configure'!
499#
500# - the old macros are replaced by *calls* to the new-macros, not the
501#   body of the new macros, since their body is not defined!!!
502#   (Whoa, that's really beautiful!).
503#
504# Additionally we need to disable the quotes when reading the input for
505# two reasons: first because otherwise `m4' will swallow the quotes of
506# other macros:
507#
508#     NEW([1, 2], 3)
509#     => NEW(1, 2, 3)
510#
511# and second, because we want to update the macro calls which are
512# quoted, i.e., we want
513#
514#     FOO([OLD(1, 2)])
515#     => FOO([NEW([1, 2], [3])])
516#
517# If we don't disable the quotes, only the macros called at the top
518# level would be updated.
519#
520# So, let's disable the quotes.
521#
522# Well, not quite: m4sugar.m4 still needs to use quotes for some macros.
523# Well, in this case, when running in autoupdate code, each macro first
524# reestablishes the quotes, expands itself, and disables the quotes.
525#
526# Thinking a bit more, you realize that in fact, people may use `define'
527# `ifelse' etc. in their files, and you certainly don't want to process
528# them.  Another example is `dnl': you don't want to remove the
529# comments.  You then realize you don't want exactly to import m4sugar:
530# you want to specify when it is enabled (macros active), and disabled.
531# m4sugar provides m4_disable/m4_enable to this end.
532#
533# You're getting close to it.  Now remains one task: how to handle
534# twofold definitions?
535#
536# Remember that the same AU_DEFUN must be understood in two different
537# ways, the AC way, and the AU way.
538#
539# One first solution is to check whether acgeneral.m4 was loaded.  But
540# that's definitely not cute.  Another is simply to install `hooks',
541# that is to say, to keep in some place m4 knows, late `define' to be
542# triggered *only* in AU mode.
543#
544# You first think to design AU_DEFUN like this:
545#
546# 1. AC_DEFUN(OLD-NAME,
547#	      [Warn the user OLD-NAME is obsolete.
548#	       NEW-CODE])
549#
550# 2. Store for late AU binding([define(OLD_NAME,
551#				[Reestablish the quotes.
552#				 NEW-CODE
553#				 Disable the quotes.])])
554#
555# but this will not work: NEW-CODE has probably $1, $2 etc. and these
556# guys will be replaced with the argument of `Store for late AU binding'
557# when you call it.
558#
559# I don't think there is a means to avoid this using this technology
560# (remember that $1 etc. are *always* expanded in m4).  You may also try
561# to replace them with $[1] to preserve them for a later evaluation, but
562# if `Store for late AU binding' is properly written, it will remain
563# quoted till the end...
564#
565# You have to change technology.  Since the problem is that `$1'
566# etc. should be `consumed' right away, one solution is to define now a
567# second macro, `AU_OLD-NAME', and to install a hook than binds OLD-NAME
568# to AU_OLD-NAME.  Then, autoupdate.m4 just need to run the hooks.  By
569# the way, the same method was used in autoheader.
570#
571#
572# # Third implementation: m4 namespaces by m4sugar
573# # ==============================================
574#
575# Actually, this implementation was just a clean up of the previous
576# implementation: instead of defining hooks by hand, m4sugar was equipped
577# with `namespaces'.  What are they?
578#
579# Sometimes we want to disable some *set* of macros, and restore them
580# later.  We provide support for this via namespaces.
581#
582# There are basically three characters playing this scene: defining a
583# macro in a namespace, disabling a namespace, and restoring a namespace
584# (i.e., all the definitions it holds).
585#
586# Technically, to define a MACRO in NAMESPACE means to define the macro
587# named `NAMESPACE::MACRO' to the VALUE.  At the same time, we append
588# `undefine(NAME)' in the macro named `m4_disable(NAMESPACE)', and
589# similarly a binding of NAME to the value of `NAMESPACE::MACRO' in
590# `m4_enable(NAMESPACE)'.  These mechanisms allow to bind the macro of
591# NAMESPACE and to unbind them at will.
592#
593# Of course this implementation is really inefficient: m4 has to grow
594# strings which can become quickly huge, which slows it significantly.
595#
596# In particular one should avoid as much as possible to use `define' for
597# temporaries.  Now that `define' has quite a complex meaning, it is an
598# expensive operations that should be limited to macros.  Use
599# `m4_define' for temporaries.
600#
601# Private copies of the macros we used in entering / exiting the m4sugar
602# namespace.  It is much more convenient than fighting with the renamed
603# version of define etc.
604#
605#
606#
607# Those two implementations suffered from serious problems:
608#
609# - namespaces were really expensive, and incurred a major performance
610#   loss on `autoconf' itself, not only `autoupdate'.  One solution
611#   would have been the limit the use of namespaces to `autoupdate', but
612#   that's again some complications on m4sugar, which really doesn't need
613#   this.  So we wanted to get rid of the namespaces.
614#
615# - since the quotes were disabled, autoupdate was sometimes making
616#   wrong guesses, for instance on:
617#
618#     foo([1, 2])
619#
620#   m4 saw 2 arguments: `[1'and `2]'.  A simple solution, somewhat
621#   fragile, is to reestablish the quotes right before all the obsolete
622#   macros, i.e., to use sed so that the previous text becomes
623#
624#     changequote([, ])foo([1, 2])
625#
626#   To this end, one wants to trace the definition of obsolete macros.
627#
628# It was there that the limitations of the namespace approach became
629# painful: because it was a complex machinery playing a lot with the
630# builtins of m4 (hence, quite fragile), tracing was almost impossible.
631#
632#
633# So this approach was dropped.
634#
635#
636# # The fourth implementation: two steps
637# # ====================================
638#
639# If you drop the uses of namespaces, you no longer can compute the
640# updated value, and replace the old call with it simultaneously.
641#
642# Obviously you will use m4 to compute the updated values, but you may
643# use some other tool to achieve the replacement.  Personally, I trust
644# nobody but m4 to parse m4, so below, m4 will perform the two tasks.
645#
646# How can m4 be used to replace *some* macros calls with newer values.
647# Well, that's dead simple: m4 should learn the definitions of obsolete
648# macros, forget its builtins, disable the quotes, and then run on the
649# input file, which amounts to doing this:
650#
651#     divert(-1)dnl
652#     changequote([, ])
653#     define([OLD], [NEW([$1, $2], m4_eval([$1 + $2]))changequote()])
654#     undefine([dnl])
655#     undefine([m4_eval])
656#     # Some more undefines...
657#     changequote()
658#     divert(0)dnl
659#     dnl The Unbelievable Truth
660#     changequote([, ])OLD(1, 2)
661#     NEW([0, 0],
662#	  0)
663#
664# which will result in
665#
666#     dnl The Unbelievable Truth
667#     NEW(1, 2, m4_eval(1 + 2))
668#     NEW([0, 0],
669#	  0)
670#
671# Grpmh.  Two problems.  A minor problem: it would have been much better
672# to have the `m4_eval' computed, and a major problem: you lost the
673# quotation in the result.
674#
675# Let's address the big problem first.  One solution is to define any
676# modern macro to rewrite its calls with the proper quotation, thanks to
677# `$@'.  Again, tracing the `define's makes it possible to know which
678# are these macros, so you input is:
679#
680#     divert(-1)dnl
681#     changequote([, ])
682#     define([OLD], [NEW([$1, $2], m4_eval([$1 + $2]))changequote()])
683#     define([NEW], [[NEW($@)]changequote()])
684#     undefine([dnl])
685#     undefine([m4_eval])
686#     # Some more undefines...
687#     changequote()
688#     divert(0)dnl
689#     dnl The Unbelievable Truth
690#     changequote([, ])OLD(1, 2)
691#     changequote([, ])NEW([0, 0],
692#	  0)
693#
694# which results in
695#
696#     dnl The Unbelievable Truth
697#     NEW([1, 2],[m4_eval(1 + 2)])
698#     NEW([0, 0],[0])
699#
700# Our problem is solved, i.e., the first call to `NEW' is properly
701# quoted, but introduced another problem: we changed the layout of the
702# second calls, which can be a drama in the case of huge macro calls
703# (think of `AC_TRY_RUN' for instance).  This example didn't show it,
704# but we also introduced parens to macros which did not have some:
705#
706#     AC_INIT
707#     => AC_INIT()
708#
709# No big deal for the semantics (unless the macro depends upon $#, which
710# is bad), but the users would not be happy.
711#
712# Additionally, we introduced quotes that were not there before, which is
713# OK in most cases, but could change the semantics of the file.
714#
715# Cruel dilemma: we do want the auto-quoting definition of `NEW' when
716# evaluating `OLD', but we don't when we evaluate the second `NEW'.
717# Back to namespaces?
718#
719# No.
720#
721#
722# # Second step: replacement
723# # ------------------------
724#
725# No, as announced above, we will work in two steps: in a first step we
726# compute the updated values, and in a second step we replace them.  Our
727# goal is something like this:
728#
729#     divert(-1)dnl
730#     changequote([, ])
731#     define([OLD], [NEW([1, 2], [3])changequote()])
732#     undefine([dnl])
733#     undefine([m4_eval])
734#     # Some more undefines...
735#     changequote()
736#     divert(0)dnl
737#     dnl The Unbelievable Truth
738#     changequote([, ])OLD(1, 2)
739#     NEW([0, 0],
740#	  0)
741#
742# i.e., the new value of `OLD' is precomputed using the auto-quoting
743# definition of `NEW' and the m4 builtins.  We'll see how afterwards,
744# let's finish with the replacement.
745#
746# Of course the solution above is wrong: if there were other calls to
747# `OLD' with different values, we would smash them to the same value.
748# But it is quite easy to generalize the scheme above:
749#
750#     divert(-1)dnl
751#     changequote([, ])
752#     define([OLD([1],[2])], [NEW([1, 2], [3])])
753#     define([OLD], [defn([OLD($@)])changequote()])
754#     undefine([dnl])
755#     undefine([m4_eval])
756#     # Some more undefines...
757#     changequote()
758#     divert(0)dnl
759#     dnl The Unbelievable Truth
760#     changequote([, ])OLD(1, 2)
761#     NEW([0, 0],
762#	  0)
763#
764# i.e., for each call to obsolete macros, we build an array `call =>
765# value', and use a macro to dispatch these values.  This results in:
766#
767#     dnl The Unbelievable Truth
768#     NEW([1, 2], [3])
769#     NEW([0, 0],
770#	  0)
771#
772# In French, we say `Youpi !', which you might roughly translate as
773# `Yippee!'.
774#
775#
776# # First step: computation
777# # -----------------------
778#
779# Let's study the anatomy of the file, and name its sections:
780#
781# prologue
782#     divert(-1)dnl
783#     changequote([, ])
784# values
785#     define([OLD([1],[2])], [NEW([1, 2], [3])])
786# dispatcher
787#     define([OLD], [defn([OLD($@)])changequote()])
788# disabler
789#     undefine([dnl])
790#     undefine([m4_eval])
791#     # Some more undefines...
792#     changequote()
793#     divert(0)dnl
794# input
795#     dnl The Unbelievable Truth
796#     changequote([, ])OLD(1, 2)
797#     NEW([0, 0],
798#	  0)
799#
800#
801# # Computing the `values' section
802# # ..............................
803#
804# First we need to get the list of all the AU macro uses.  To this end,
805# first get the list of all the AU macros names by tracing `AU_DEFUN' in
806# the initialization of autoconf.  This list is computed in the file
807# `au.txt' below.
808#
809# Then use this list to trace all the AU macro uses in the input.  The
810# goal is obtain in the case of our example:
811#
812#     [define([OLD([1],[2])],]@<<@OLD([1],[2])@>>@[)]
813#
814# This is the file `values.in' below.
815#
816# We want to evaluate this with only the builtins (in fact m4sugar), the
817# auto-quoting definitions of the new macros (`new.m4'), and the
818# definition of the old macros (`old.m4').  Computing these last two
819# files is easy: it's just a matter of using the right `--trace' option.
820#
821# So the content of `values.in' is:
822#
823#     include($autoconf_dir/m4sugar.m4)
824#     m4_include(new.m4)
825#     m4_include(old.m4)
826#     divert(0)dnl
827#     [define([OLD([1],[2])],]@<<@OLD([1],[2])@>>@[)]
828#
829# We run m4 on it, which yields:
830#
831#     define([OLD([1],[2])],@<<@NEW([1, 2], [3])@>>@)
832#
833# Transform `@<<@' and `@>>@' into quotes and we get
834#
835#     define([OLD([1],[2])],[NEW([1, 2], [3])])
836#
837# This is `values.m4'.
838#
839#
840# # Computing the `dispatcher' section
841# # ..................................
842#
843# The `prologue', and the `disabler' are simple and need no commenting.
844#
845# To compute the `dispatcher' (`dispatch.m4'), again, it is a simple
846# matter of using the right `--trace'.
847#
848# Finally, the input is not exactly the input file, rather it is the
849# input file with the added `changequote'.  To this end, we build
850# `quote.sed'.
851#
852#
853# # Putting it all together
854# # .......................
855#
856# We build the file `input.m4' which contains:
857#
858#     divert(-1)dnl
859#     changequote([, ])
860#     include(values.m4)
861#     include(dispatch.m4)
862#     undefine([dnl])
863#     undefine([eval])
864#     # Some more undefines...
865#     changequote()
866#     divert(0)dnl
867#     dnl The Unbelievable Truth
868#     changequote([, ])OLD(1, 2)
869#     NEW([0, 0],
870#	  0)
871#
872# And we just run m4 on it.  Et voila`, Monsieur !  Mais oui, mais oui.
873#
874# Well, there are a few additional technicalities.  For instance, we
875# rely on `changequote', `ifelse' and `defn', but we don't want to
876# interpret the changequotes of the user, so we simply use another name:
877# `_au_changequote' etc.
878#
879#
880# # Failure of the fourth approach
881# # ------------------------------
882#
883# This approach is heavily based on traces, but then there is an obvious
884# problem: non expanded code will never be seen.  In particular, the body
885# of a `define' definition is not seen, so on the input
886#
887#	  define([idem], [OLD(0, [$1])])
888#
889# autoupdate would never see the `OLD', and wouldn't have updated it.
890# Worse yet, if `idem(0)' was used later, then autoupdate sees that
891# `OLD' is used, computes the result for `OLD(0, 0)' and sets up a
892# dispatcher for `OLD'.  Since there was no computed value for `OLD(0,
893# [$1])', the dispatcher would have replaced with... nothing, leading
894# to
895#
896#	  define([idem], [])
897#
898# With some more thinking, you see that the two step approach is wrong,
899# the namespace approach was much saner.
900#
901# But you learned a lot, in particular you realized that using traces
902# can make it possible to simulate namespaces!
903#
904#
905#
906# # The fifth implementation: m4 namespaces by files
907# # ================================================
908#
909# The fourth implementation demonstrated something unsurprising: you
910# cannot precompute, i.e., the namespace approach was the right one.
911# Still, we no longer want them, they're too expensive.  Let's have a
912# look at the way it worked.
913#
914# When updating
915#
916#     dnl The Unbelievable Truth
917#     OLD(1, 2)
918#     NEW([0, 0], [0])
919#
920# you evaluate `input.m4':
921#
922#     divert(-1)
923#     changequote([, ])
924#     define([OLD],
925#     [m4_enable()NEW([$1, $2], m4_eval([$1 + $2]))m4_disable()])
926#     ...
927#     m4_disable()
928#     dnl The Unbelievable Truth
929#     OLD(1, 2)
930#     NEW([0, 0], [0])
931#
932# where `m4_disable' undefines the m4 and m4sugar, and disables the quotes
933# and comments:
934#
935#     define([m4_disable],
936#     [undefine([__file__])
937#     ...
938#     changecom(#)
939#     changequote()])
940#
941# `m4_enable' does the converse: reestablish quotes and comments
942# --easy--, reestablish m4sugar --easy: just load `m4sugar.m4' again-- and
943# reenable the builtins.  This later task requires that you first save
944# the builtins.  And BTW, the definition above of `m4_disable' cannot
945# work: you undefined `changequote' before using it!  So you need to use
946# your privates copies of the builtins.  Let's introduce three files for
947# this:
948#
949#  `m4save.m4'
950#    moves the m4 builtins into the `_au_' pseudo namespace,
951#  `unm4.m4'
952#    undefines the builtins,
953#  `m4.m4'
954#    restores them.
955#
956# So `input.m4' is:
957#
958#     divert(-1)
959#     changequote([, ])
960#
961#     include([m4save.m4])
962#
963#     # Import AU.
964#     define([OLD],
965#     [m4_enable()NEW([$1, $2], m4_eval([$1 + $2]))m4_disable()])
966#
967#     define([_au_enable],
968#     [_au_changecom([#])
969#     _au_include([m4.m4])
970#     _au_include(m4sugar.m4)])
971#
972#     define([_au_disable],
973#     [# Disable m4sugar.
974#     # Disable the m4 builtins.
975#     _au_include([unm4.m4])
976#     # 1. Disable special characters.
977#     _au_changequote()
978#     _au_changecom()])
979#
980#     m4_disable()
981#     dnl The Unbelievable Truth
982#     OLD(1, 2)
983#     NEW([0, 0], [0])
984#
985# Based on what we learned in the fourth implementation we know that we
986# have to enable the quotes *before* any AU macro, and we know we need
987# to build autoquoting versions of the AC macros.  But the autoquoting
988# AC definitions must be disabled in the rest of the file, and enabled
989# inside AU macros.
990#
991# Using `autoconf --trace' it is easy to build the files
992#
993#   `ac.m4'
994#     define the autoquoting AC fake macros
995#   `disable.m4'
996#     undefine the m4sugar and AC autoquoting macros.
997#   `au.m4'
998#     definitions of the AU macros (such as `OLD' above).
999#
1000# Now, `input.m4' is:
1001#
1002#     divert(-1)
1003#     changequote([, ])
1004#
1005#     include([m4save.m4])
1006#     # Import AU.
1007#     include([au.m4])
1008#
1009#     define([_au_enable],
1010#     [_au_changecom([#])
1011#     _au_include([m4.m4])
1012#     _au_include(m4sugar.m4)
1013#     _au_include(ac.m4)])
1014#
1015#     define([_au_disable],
1016#     [_au_include([disable.m4])
1017#     _au_include([unm4.m4])
1018#     # 1. Disable special characters.
1019#     _au_changequote()
1020#     _au_changecom()])
1021#
1022#     m4_disable()
1023#     dnl The Unbelievable Truth
1024#     _au_changequote([, ])OLD(1, 2)
1025#     NEW([0, 0], [0])
1026#
1027# Finally, version V is ready.
1028#
1029# Well... almost.
1030#
1031# There is a slight problem that remains: if an AU macro OUTER includes
1032# an AU macro INNER, then _au_enable will be run when entering OUTER
1033# and when entering INNER (not good, but not too bad yet).  But when
1034# getting out of INNER, _au_disable will disable everything while we
1035# were still in OUTER.  Badaboom.
1036#
1037# Therefore _au_enable and _au_disable have to be written to work by
1038# pairs: each _au_enable pushdef's _au_enabled, and each _au_disable
1039# popdef's _au_enabled.  And of course _au_enable and _au_disable are
1040# effective when _au_enabled is *not* defined.
1041#
1042# Finally, version V' is ready.  And there is much rejoicing.  (And I
1043# have free time again.  I think.  Yeah, right.)
1044
1045### Setup "GNU" style for perl-mode and cperl-mode.
1046## Local Variables:
1047## perl-indent-level: 2
1048## perl-continued-statement-offset: 2
1049## perl-continued-brace-offset: 0
1050## perl-brace-offset: 0
1051## perl-brace-imaginary-offset: 0
1052## perl-label-offset: -2
1053## cperl-indent-level: 2
1054## cperl-brace-offset: 0
1055## cperl-continued-brace-offset: 0
1056## cperl-label-offset: -2
1057## cperl-extra-newline-before-brace: t
1058## cperl-merge-trailing-else: nil
1059## cperl-continued-statement-offset: 2
1060## End:
1061