File Coverage

File:/usr/local/bin/autoupdate
Coverage:83.1%

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