1divert(-1)#                                                  -*- Autoconf -*-
2# This file is part of Autoconf.
3# Base M4 layer.
4# Requires GNU M4.
5#
6# Copyright (C) 1999-2017, 2020 Free Software Foundation, Inc.
7
8# This file is part of Autoconf.  This program is free
9# software; you can redistribute it and/or modify it under the
10# terms of the GNU General Public License as published by the
11# 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# Under Section 7 of GPL version 3, you are granted additional
20# permissions described in the Autoconf Configure Script Exception,
21# version 3.0, as published by the Free Software Foundation.
22#
23# You should have received a copy of the GNU General Public License
24# and a copy of the Autoconf Configure Script Exception along with
25# this program; see the files COPYINGv3 and COPYING.EXCEPTION
26# respectively.  If not, see <https://www.gnu.org/licenses/>.
27
28# Written by Akim Demaille.
29
30# Set the quotes, whatever the current quoting system.
31changequote()
32changequote([, ])
33
34# Some old m4's don't support m4exit.  But they provide
35# equivalent functionality by core dumping because of the
36# long macros we define.
37ifdef([__gnu__], ,
38[errprint(M4sugar requires GNU M4. Install it before installing M4sugar or
39set the M4 environment variable to its absolute file name.)
40m4exit(2)])
41
42
43## ------------------------------- ##
44## 1. Simulate --prefix-builtins.  ##
45## ------------------------------- ##
46
47# m4_define
48# m4_defn
49# m4_undefine
50define([m4_define],   defn([define]))
51define([m4_defn],     defn([defn]))
52define([m4_undefine], defn([undefine]))
53
54m4_undefine([define])
55m4_undefine([defn])
56m4_undefine([undefine])
57
58
59# m4_copy(SRC, DST)
60# -----------------
61# Define DST as the definition of SRC.
62# What's the difference between:
63# 1. m4_copy([from], [to])
64# 2. m4_define([to], [from($@)])
65# Well, obviously 1 is more expensive in space.  Maybe 2 is more expensive
66# in time, but because of the space cost of 1, it's not that obvious.
67# Nevertheless, one huge difference is the handling of `$0'.  If `from'
68# uses `$0', then with 1, `to''s `$0' is `to', while it is `from' in 2.
69# The user would certainly prefer to see `to'.
70#
71# This definition is in effect during m4sugar initialization, when
72# there are no pushdef stacks; later on, we redefine it to something
73# more powerful for all other clients to use.
74m4_define([m4_copy],
75[m4_define([$2], m4_defn([$1]))])
76
77
78# m4_rename(SRC, DST)
79# -------------------
80# Rename the macro SRC to DST.
81m4_define([m4_rename],
82[m4_copy([$1], [$2])m4_undefine([$1])])
83
84
85# m4_rename_m4(MACRO-NAME)
86# ------------------------
87# Rename MACRO-NAME to m4_MACRO-NAME.
88m4_define([m4_rename_m4],
89[m4_rename([$1], [m4_$1])])
90
91
92# m4_copy_unm4(m4_MACRO-NAME)
93# ---------------------------
94# Copy m4_MACRO-NAME to MACRO-NAME.
95m4_define([m4_copy_unm4],
96[m4_copy([$1], m4_bpatsubst([$1], [^m4_\(.*\)], [[\1]]))])
97
98
99# Some m4 internals have names colliding with tokens we might use.
100# Rename them a` la `m4 --prefix-builtins'.  Conditionals first, since
101# some subsequent renames are conditional.
102m4_rename_m4([ifdef])
103m4_rename([ifelse], [m4_if])
104
105m4_rename_m4([builtin])
106m4_rename_m4([changecom])
107m4_rename_m4([changequote])
108m4_ifdef([changeword],dnl conditionally available in 1.4.x
109[m4_undefine([changeword])])
110m4_rename_m4([debugfile])
111m4_rename_m4([debugmode])
112m4_rename_m4([decr])
113m4_rename_m4([divnum])
114m4_rename_m4([dumpdef])
115m4_rename_m4([errprint])
116m4_rename_m4([esyscmd])
117m4_rename_m4([eval])
118m4_rename_m4([format])
119m4_undefine([include])
120m4_rename_m4([incr])
121m4_rename_m4([index])
122m4_rename_m4([indir])
123m4_rename_m4([len])
124m4_rename([m4exit], [m4_exit])
125m4_undefine([m4wrap])
126m4_ifdef([mkstemp],dnl added in M4 1.4.8
127[m4_rename_m4([mkstemp])
128m4_copy([m4_mkstemp], [m4_maketemp])
129m4_undefine([maketemp])],
130[m4_rename_m4([maketemp])
131m4_copy([m4_maketemp], [m4_mkstemp])])
132m4_rename([patsubst], [m4_bpatsubst])
133m4_rename_m4([popdef])
134m4_rename_m4([pushdef])
135m4_rename([regexp], [m4_bregexp])
136m4_rename_m4([shift])
137m4_undefine([sinclude])
138m4_rename_m4([substr])
139m4_ifdef([symbols],dnl present only in alpha-quality 1.4o
140[m4_rename_m4([symbols])])
141m4_rename_m4([syscmd])
142m4_rename_m4([sysval])
143m4_rename_m4([traceoff])
144m4_rename_m4([traceon])
145m4_rename_m4([translit])
146
147# _m4_defn(ARG)
148# -------------
149# _m4_defn is for internal use only - it bypasses the wrapper, so it
150# must only be used on one argument at a time, and only on macros
151# known to be defined.  Make sure this still works if the user renames
152# m4_defn but not _m4_defn.
153m4_copy([m4_defn], [_m4_defn])
154
155# _m4_divert_raw(NUM)
156# -------------------
157# _m4_divert_raw is for internal use only.  Use this instead of
158# m4_builtin([divert], NUM), so that tracing diversion flow is easier.
159m4_rename([divert], [_m4_divert_raw])
160
161# _m4_popdef(ARG...)
162# ------------------
163# _m4_popdef is for internal use only - it bypasses the wrapper, so it
164# must only be used on macros known to be defined.  Make sure this
165# still works if the user renames m4_popdef but not _m4_popdef.
166m4_copy([m4_popdef], [_m4_popdef])
167
168# _m4_undefine(ARG...)
169# --------------------
170# _m4_undefine is for internal use only - it bypasses the wrapper, so
171# it must only be used on macros known to be defined.  Make sure this
172# still works if the user renames m4_undefine but not _m4_undefine.
173m4_copy([m4_undefine], [_m4_undefine])
174
175# _m4_undivert(NUM...)
176# --------------------
177# _m4_undivert is for internal use only, and should always be given
178# arguments.  Use this instead of m4_builtin([undivert], NUM...), so
179# that tracing diversion flow is easier.
180m4_rename([undivert], [_m4_undivert])
181
182
183## ------------------- ##
184## 2. Error messages.  ##
185## ------------------- ##
186
187
188# m4_location
189# -----------
190# Output the current file, colon, and the current line number.
191m4_define([m4_location],
192[__file__:__line__])
193
194
195# m4_errprintn(MSG)
196# -----------------
197# Same as `errprint', but with the missing end of line.
198m4_define([m4_errprintn],
199[m4_errprint([$1
200])])
201
202
203# m4_warning(MSG)
204# ---------------
205# Warn the user.
206m4_define([m4_warning],
207[m4_errprintn(m4_location[: warning: $1])])
208
209
210# m4_fatal(MSG, [EXIT-STATUS])
211# ----------------------------
212# Fatal the user.                                                      :)
213m4_define([m4_fatal],
214[m4_errprintn(m4_location[: error: $1]
215m4_expansion_stack)m4_exit(m4_if([$2],, 1, [$2]))])
216
217
218# m4_assert(EXPRESSION, [EXIT-STATUS = 1])
219# ----------------------------------------
220# This macro ensures that EXPRESSION evaluates to true, and exits if
221# EXPRESSION evaluates to false.
222m4_define([m4_assert],
223[m4_if(m4_eval([$1]), 0,
224       [m4_fatal([assert failed: $1], [$2])])])
225
226
227
228## ------------- ##
229## 3. Warnings.  ##
230## ------------- ##
231
232
233# _m4_warn(CATEGORY, MESSAGE, [STACK-TRACE])
234# ------------------------------------------
235# Report a MESSAGE to the user if the CATEGORY of warnings is enabled.
236# This is for traces only.
237# If present, STACK-TRACE is a \n-separated list of "LOCATION: MESSAGE",
238# where the last line (and no other) ends with "the top level".
239#
240# Within m4, the macro is a no-op.  This macro really matters
241# when autom4te post-processes the trace output.
242m4_define([_m4_warn], [])
243
244
245# m4_warn(CATEGORY, MESSAGE)
246# --------------------------
247# Report a MESSAGE to the user if the CATEGORY of warnings is enabled.
248m4_define([m4_warn],
249[_m4_warn([$1], [$2],
250m4_ifdef([_m4_expansion_stack], [m4_expansion_stack]))])
251
252
253
254## ------------------- ##
255## 4. File inclusion.  ##
256## ------------------- ##
257
258
259# We also want to neutralize include (and sinclude for symmetry),
260# but we want to extend them slightly: warn when a file is included
261# several times.  This is, in general, a dangerous operation, because
262# too many people forget to quote the first argument of m4_define.
263#
264# For instance in the following case:
265#   m4_define(foo, [bar])
266# then a second reading will turn into
267#   m4_define(bar, [bar])
268# which is certainly not what was meant.
269
270# m4_include_unique(FILE)
271# -----------------------
272# Declare that the FILE was loading; and warn if it has already
273# been included.
274m4_define([m4_include_unique],
275[m4_ifdef([m4_include($1)],
276	  [m4_warn([syntax], [file `$1' included several times])])dnl
277m4_define([m4_include($1)])])
278
279
280# m4_include(FILE)
281# ----------------
282# Like the builtin include, but warns against multiple inclusions.
283m4_define([m4_include],
284[m4_include_unique([$1])dnl
285m4_builtin([include], [$1])])
286
287
288# m4_sinclude(FILE)
289# -----------------
290# Like the builtin sinclude, but warns against multiple inclusions.
291m4_define([m4_sinclude],
292[m4_include_unique([$1])dnl
293m4_builtin([sinclude], [$1])])
294
295
296
297## ------------------------------------ ##
298## 5. Additional branching constructs.  ##
299## ------------------------------------ ##
300
301# Both `m4_ifval' and `m4_ifset' tests against the empty string.  The
302# difference is that `m4_ifset' is specialized on macros.
303#
304# In case of arguments of macros, eg. $1, it makes little difference.
305# In the case of a macro `FOO', you don't want to check `m4_ifval(FOO,
306# TRUE)', because if `FOO' expands with commas, there is a shifting of
307# the arguments.  So you want to run `m4_ifval([FOO])', but then you just
308# compare the *string* `FOO' against `', which, of course fails.
309#
310# So you want the variation `m4_ifset' that expects a macro name as $1.
311# If this macro is both defined and defined to a non empty value, then
312# it runs TRUE, etc.
313
314
315# m4_ifblank(COND, [IF-BLANK], [IF-TEXT])
316# m4_ifnblank(COND, [IF-TEXT], [IF-BLANK])
317# ----------------------------------------
318# If COND is empty, or consists only of blanks (space, tab, newline),
319# then expand IF-BLANK, otherwise expand IF-TEXT.  This differs from
320# m4_ifval only if COND has just whitespace, but it helps optimize in
321# spite of users who mistakenly leave trailing space after what they
322# thought was an empty argument:
323#   macro(
324#         []
325#        )
326#
327# Writing one macro in terms of the other causes extra overhead, so
328# we inline both definitions.
329m4_define([m4_ifblank],
330[m4_if(m4_translit([[$1]],  [ ][	][
331]), [], [$2], [$3])])
332
333m4_define([m4_ifnblank],
334[m4_if(m4_translit([[$1]],  [ ][	][
335]), [], [$3], [$2])])
336
337
338# m4_ifval(COND, [IF-TRUE], [IF-FALSE])
339# -------------------------------------
340# If COND is not the empty string, expand IF-TRUE, otherwise IF-FALSE.
341# Comparable to m4_ifdef.
342m4_define([m4_ifval],
343[m4_if([$1], [], [$3], [$2])])
344
345
346# m4_n(TEXT)
347# ----------
348# If TEXT is not empty, return TEXT and a new line, otherwise nothing.
349m4_define([m4_n],
350[m4_if([$1],
351       [], [],
352	   [$1
353])])
354
355
356# m4_ifvaln(COND, [IF-TRUE], [IF-FALSE])
357# --------------------------------------
358# Same as `m4_ifval', but add an extra newline to IF-TRUE or IF-FALSE
359# unless that argument is empty.
360m4_define([m4_ifvaln],
361[m4_if([$1],
362       [],   [m4_n([$3])],
363	     [m4_n([$2])])])
364
365
366# m4_ifset(MACRO, [IF-TRUE], [IF-FALSE])
367# --------------------------------------
368# If MACRO has no definition, or of its definition is the empty string,
369# expand IF-FALSE, otherwise IF-TRUE.
370m4_define([m4_ifset],
371[m4_ifdef([$1],
372	  [m4_ifval(_m4_defn([$1]), [$2], [$3])],
373	  [$3])])
374
375
376# m4_ifndef(NAME, [IF-NOT-DEFINED], [IF-DEFINED])
377# -----------------------------------------------
378m4_define([m4_ifndef],
379[m4_ifdef([$1], [$3], [$2])])
380
381
382# m4_case(SWITCH, VAL1, IF-VAL1, VAL2, IF-VAL2, ..., DEFAULT)
383# -----------------------------------------------------------
384# m4 equivalent of
385# switch (SWITCH)
386# {
387#   case VAL1:
388#     IF-VAL1;
389#     break;
390#   case VAL2:
391#     IF-VAL2;
392#     break;
393#   ...
394#   default:
395#     DEFAULT;
396#     break;
397# }.
398# All the values are optional, and the macro is robust to active
399# symbols properly quoted.
400#
401# Please keep foreach.m4 in sync with any adjustments made here.
402m4_define([m4_case],
403[m4_if([$#], 0, [],
404       [$#], 1, [],
405       [$#], 2, [$2],
406       [$1], [$2], [$3],
407       [$0([$1], m4_shift3($@))])])
408
409
410# m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT)
411# -----------------------------------------------------
412# m4 equivalent of
413#
414# if (SWITCH =~ RE1)
415#   VAL1;
416# elif (SWITCH =~ RE2)
417#   VAL2;
418# elif ...
419#   ...
420# else
421#   DEFAULT
422#
423# All the values are optional, and the macro is robust to active symbols
424# properly quoted.
425#
426# Please keep foreach.m4 in sync with any adjustments made here.
427m4_define([m4_bmatch],
428[m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
429       [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
430       [$#], 2, [$2],
431       [m4_if(m4_bregexp([$1], [$2]), -1, [$0([$1], m4_shift3($@))],
432	      [$3])])])
433
434# m4_argn(N, ARGS...)
435# -------------------
436# Extract argument N (greater than 0) from ARGS.  Example:
437#   m4_define([b], [B])
438#   m4_argn([2], [a], [b], [c]) => b
439#
440# Rather than using m4_car(m4_shiftn([$1], $@)), we exploit the fact that
441# GNU m4 can directly reference any argument, through an indirect macro.
442m4_define([m4_argn],
443[m4_assert([0 < $1])]dnl
444[m4_pushdef([_$0], [_m4_popdef([_$0])]m4_dquote([$]m4_incr([$1])))_$0($@)])
445
446
447# m4_car(ARGS...)
448# m4_cdr(ARGS...)
449# ---------------
450# Manipulate m4 lists.  m4_car returns the first argument.  m4_cdr
451# bundles all but the first argument into a quoted list.  These two
452# macros are generally used with list arguments, with quoting removed
453# to break the list into multiple m4 ARGS.
454m4_define([m4_car], [[$1]])
455m4_define([m4_cdr],
456[m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])],
457       [$#], 1, [],
458       [m4_dquote(m4_shift($@))])])
459
460# _m4_cdr(ARGS...)
461# ----------------
462# Like m4_cdr, except include a leading comma unless only one argument
463# remains.  Why?  Because comparing a large list against [] is more
464# expensive in expansion time than comparing the number of arguments; so
465# _m4_cdr can be used to reduce the number of arguments when it is time
466# to end recursion.
467m4_define([_m4_cdr],
468[m4_if([$#], 1, [],
469       [, m4_dquote(m4_shift($@))])])
470
471
472
473# m4_cond(TEST1, VAL1, IF-VAL1, TEST2, VAL2, IF-VAL2, ..., [DEFAULT])
474# -------------------------------------------------------------------
475# Similar to m4_if, except that each TEST is expanded when encountered.
476# If the expansion of TESTn matches the string VALn, the result is IF-VALn.
477# The result is DEFAULT if no tests passed.  This macro allows
478# short-circuiting of expensive tests, where it pays to arrange quick
479# filter tests to run first.
480#
481# For an example, consider a previous implementation of _AS_QUOTE_IFELSE:
482#
483#    m4_if(m4_index([$1], [\]), [-1], [$2],
484#          m4_eval(m4_index([$1], [\\]) >= 0), [1], [$2],
485#          m4_eval(m4_index([$1], [\$]) >= 0), [1], [$2],
486#          m4_eval(m4_index([$1], [\`]) >= 0), [1], [$3],
487#          m4_eval(m4_index([$1], [\"]) >= 0), [1], [$3],
488#          [$2])
489#
490# Here, m4_index is computed 5 times, and m4_eval 4, even if $1 contains
491# no backslash.  It is more efficient to do:
492#
493#    m4_cond([m4_index([$1], [\])], [-1], [$2],
494#            [m4_eval(m4_index([$1], [\\]) >= 0)], [1], [$2],
495#            [m4_eval(m4_index([$1], [\$]) >= 0)], [1], [$2],
496#            [m4_eval(m4_index([$1], [\`]) >= 0)], [1], [$3],
497#            [m4_eval(m4_index([$1], [\"]) >= 0)], [1], [$3],
498#            [$2])
499#
500# In the common case of $1 with no backslash, only one m4_index expansion
501# occurs, and m4_eval is avoided altogether.
502#
503# Please keep foreach.m4 in sync with any adjustments made here.
504m4_define([m4_cond],
505[m4_if([$#], [0], [m4_fatal([$0: cannot be called without arguments])],
506       [$#], [1], [$1],
507       m4_eval([$# % 3]), [2], [m4_fatal([$0: missing an argument])],
508       [_$0($@)])])
509
510m4_define([_m4_cond],
511[m4_if(($1), [($2)], [$3],
512       [$#], [3], [],
513       [$#], [4], [$4],
514       [$0(m4_shift3($@))])])
515
516
517## ---------------------------------------- ##
518## 6. Enhanced version of some primitives.  ##
519## ---------------------------------------- ##
520
521# m4_bpatsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...)
522# ----------------------------------------------------
523# m4 equivalent of
524#
525#   $_ = STRING;
526#   s/RE1/SUBST1/g;
527#   s/RE2/SUBST2/g;
528#   ...
529#
530# All the values are optional, and the macro is robust to active symbols
531# properly quoted.
532#
533# I would have liked to name this macro `m4_bpatsubst', unfortunately,
534# due to quotation problems, I need to double quote $1 below, therefore
535# the anchors are broken :(  I can't let users be trapped by that.
536#
537# Recall that m4_shift3 always results in an argument.  Hence, we need
538# to distinguish between a final deletion vs. ending recursion.
539#
540# Please keep foreach.m4 in sync with any adjustments made here.
541m4_define([m4_bpatsubsts],
542[m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
543       [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
544       [$#], 2, [m4_unquote(m4_builtin([patsubst], [[$1]], [$2]))],
545       [$#], 3, [m4_unquote(m4_builtin([patsubst], [[$1]], [$2], [$3]))],
546       [_$0($@m4_if(m4_eval($# & 1), 0, [,]))])])
547m4_define([_m4_bpatsubsts],
548[m4_if([$#], 2, [$1],
549       [$0(m4_builtin([patsubst], [[$1]], [$2], [$3]),
550	   m4_shift3($@))])])
551
552
553# m4_copy(SRC, DST)
554# -----------------
555# Define the pushdef stack DST as a copy of the pushdef stack SRC;
556# give an error if DST is already defined.  This is particularly nice
557# for copying self-modifying pushdef stacks, where the top definition
558# includes one-shot initialization that is later popped to the normal
559# definition.  This version intentionally does nothing if SRC is
560# undefined.
561#
562# Some macros simply can't be renamed with this method: namely, anything
563# involved in the implementation of m4_stack_foreach_sep.
564m4_define([m4_copy],
565[m4_ifdef([$2], [m4_fatal([$0: won't overwrite defined macro: $2])],
566	  [m4_stack_foreach_sep([$1], [m4_pushdef([$2],], [)])])]dnl
567[m4_ifdef([m4_location($1)], [m4_define([m4_location($2)], m4_location)])])
568
569
570# m4_copy_force(SRC, DST)
571# m4_rename_force(SRC, DST)
572# -------------------------
573# Like m4_copy/m4_rename, except blindly overwrite any existing DST.
574# Note that m4_copy_force tolerates undefined SRC, while m4_rename_force
575# does not.
576m4_define([m4_copy_force],
577[m4_ifdef([$2], [_m4_undefine([$2])])m4_copy($@)])
578
579m4_define([m4_rename_force],
580[m4_ifdef([$2], [_m4_undefine([$2])])m4_rename($@)])
581
582
583# m4_define_default(MACRO, VALUE)
584# -------------------------------
585# If MACRO is undefined, set it to VALUE.
586m4_define([m4_define_default],
587[m4_ifndef([$1], [m4_define($@)])])
588
589
590# m4_default(EXP1, EXP2)
591# m4_default_nblank(EXP1, EXP2)
592# -----------------------------
593# Returns EXP1 if not empty/blank, otherwise EXP2.  Expand the result.
594#
595# m4_default is called on hot paths, so inline the contents of m4_ifval,
596# for one less round of expansion.
597m4_define([m4_default],
598[m4_if([$1], [], [$2], [$1])])
599
600m4_define([m4_default_nblank],
601[m4_ifblank([$1], [$2], [$1])])
602
603
604# m4_default_quoted(EXP1, EXP2)
605# m4_default_nblank_quoted(EXP1, EXP2)
606# ------------------------------------
607# Returns EXP1 if non empty/blank, otherwise EXP2.  Leave the result quoted.
608#
609# For comparison:
610#   m4_define([active], [ACTIVE])
611#   m4_default([active], [default]) => ACTIVE
612#   m4_default([], [active]) => ACTIVE
613#   -m4_default([ ], [active])- => - -
614#   -m4_default_nblank([ ], [active])- => -ACTIVE-
615#   m4_default_quoted([active], [default]) => active
616#   m4_default_quoted([], [active]) => active
617#   -m4_default_quoted([ ], [active])- => - -
618#   -m4_default_nblank_quoted([ ], [active])- => -active-
619#
620# m4_default macro is called on hot paths, so inline the contents of m4_ifval,
621# for one less round of expansion.
622m4_define([m4_default_quoted],
623[m4_if([$1], [], [[$2]], [[$1]])])
624
625m4_define([m4_default_nblank_quoted],
626[m4_ifblank([$1], [[$2]], [[$1]])])
627
628
629# m4_defn(NAME)
630# -------------
631# Like the original, except guarantee a warning when using something which is
632# undefined (unlike M4 1.4.x).  This replacement is not a full-featured
633# replacement: if any of the defined macros contain unbalanced quoting, but
634# when pasted together result in a well-quoted string, then only native m4
635# support is able to get it correct.  But that's where quadrigraphs come in
636# handy, if you really need unbalanced quotes inside your macros.
637#
638# This macro is called frequently, so minimize the amount of additional
639# expansions by skipping m4_ifndef.  Better yet, if __m4_version__ exists,
640# (added in M4 1.6), then let m4 do the job for us (see m4_init).
641m4_define([m4_defn],
642[m4_if([$#], [0], [[$0]],
643       [$#], [1], [m4_ifdef([$1], [_m4_defn([$1])],
644			    [m4_fatal([$0: undefined macro: $1])])],
645       [m4_map_args([$0], $@)])])
646
647
648# m4_dumpdef(NAME...)
649# -------------------
650# In m4 1.4.x, dumpdef writes to the current debugfile, rather than
651# stderr.  This in turn royally confuses autom4te; so we follow the
652# lead of newer m4 and always dump to stderr.  Unlike the original,
653# this version requires an argument, since there is no convenient way
654# in m4 1.4.x to grab the names of all defined macros.  Newer m4
655# always dumps to stderr, regardless of the current debugfile; it also
656# provides m4symbols as a way to grab all current macro names.  But
657# dumpdefs is not frequently called, so we don't need to worry about
658# conditionally using these newer features.  Also, this version
659# doesn't sort multiple arguments.
660#
661# If we detect m4 1.6 or newer, then provide an alternate definition,
662# installed during m4_init, that allows builtins through.
663# Unfortunately, there is no nice way in m4 1.4.x to dump builtins.
664m4_define([m4_dumpdef],
665[m4_if([$#], [0], [m4_fatal([$0: missing argument])],
666       [$#], [1], [m4_ifdef([$1], [m4_errprintn(
667  [$1:	]m4_dquote(_m4_defn([$1])))], [m4_fatal([$0: undefined macro: $1])])],
668       [m4_map_args([$0], $@)])])
669
670m4_define([_m4_dumpdef],
671[m4_if([$#], [0], [m4_fatal([$0: missing argument])],
672       [$#], [1], [m4_builtin([dumpdef], [$1])],
673       [m4_map_args_sep([m4_builtin([dumpdef],], [)], [], $@)])])
674
675
676# m4_dumpdefs(NAME...)
677# --------------------
678# Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its
679# value stack (most recent displayed first).  Also, this version silently
680# ignores undefined macros, rather than erroring out.
681#
682# This macro cheats, because it relies on the current definition of NAME
683# while the second argument of m4_stack_foreach_lifo is evaluated (which
684# would be undefined according to the API).
685m4_define([m4_dumpdefs],
686[m4_if([$#], [0], [m4_fatal([$0: missing argument])],
687       [$#], [1], [m4_stack_foreach_lifo([$1], [m4_dumpdef([$1])m4_ignore])],
688       [m4_map_args([$0], $@)])])
689
690# m4_esyscmd_s(COMMAND)
691# ---------------------
692# Like m4_esyscmd, except strip any trailing newlines, thus behaving
693# more like shell command substitution.
694m4_define([m4_esyscmd_s],
695[m4_chomp_all(m4_esyscmd([$1]))])
696
697
698# m4_popdef(NAME)
699# ---------------
700# Like the original, except guarantee a warning when using something which is
701# undefined (unlike M4 1.4.x).
702#
703# This macro is called frequently, so minimize the amount of additional
704# expansions by skipping m4_ifndef.  Better yet, if __m4_version__ exists,
705# (added in M4 1.6), then let m4 do the job for us (see m4_init).
706m4_define([m4_popdef],
707[m4_if([$#], [0], [[$0]],
708       [$#], [1], [m4_ifdef([$1], [_m4_popdef([$1])],
709			    [m4_fatal([$0: undefined macro: $1])])],
710       [m4_map_args([$0], $@)])])
711
712
713# m4_shiftn(N, ...)
714# -----------------
715# Returns ... shifted N times.  Useful for recursive "varargs" constructs.
716#
717# Autoconf does not use this macro, because it is inherently slower than
718# calling the common cases of m4_shift2 or m4_shift3 directly.  But it
719# might as well be fast for other clients, such as Libtool.  One way to
720# do this is to expand $@ only once in _m4_shiftn (otherwise, for long
721# lists, the expansion of m4_if takes twice as much memory as what the
722# list itself occupies, only to throw away the unused branch).  The end
723# result is strictly equivalent to
724#   m4_if([$1], 1, [m4_shift(,m4_shift(m4_shift($@)))],
725#         [_m4_shiftn(m4_decr([$1]), m4_shift(m4_shift($@)))])
726# but with the final `m4_shift(m4_shift($@)))' shared between the two
727# paths.  The first leg uses a no-op m4_shift(,$@) to balance out the ().
728#
729# Please keep foreach.m4 in sync with any adjustments made here.
730m4_define([m4_shiftn],
731[m4_assert(0 < $1 && $1 < $#)_$0($@)])
732
733m4_define([_m4_shiftn],
734[m4_if([$1], 1, [m4_shift(],
735       [$0(m4_decr([$1])]), m4_shift(m4_shift($@)))])
736
737# m4_shift2(...)
738# m4_shift3(...)
739# --------------
740# Returns ... shifted twice, and three times.  Faster than m4_shiftn.
741m4_define([m4_shift2], [m4_shift(m4_shift($@))])
742m4_define([m4_shift3], [m4_shift(m4_shift(m4_shift($@)))])
743
744# _m4_shift2(...)
745# _m4_shift3(...)
746# ---------------
747# Like m4_shift2 or m4_shift3, except include a leading comma unless shifting
748# consumes all arguments.  Why?  Because in recursion, it is nice to
749# distinguish between 1 element left and 0 elements left, based on how many
750# arguments this shift expands to.
751m4_define([_m4_shift2],
752[m4_if([$#], [2], [],
753       [, m4_shift(m4_shift($@))])])
754m4_define([_m4_shift3],
755[m4_if([$#], [3], [],
756       [, m4_shift(m4_shift(m4_shift($@)))])])
757
758
759# m4_undefine(NAME)
760# -----------------
761# Like the original, except guarantee a warning when using something which is
762# undefined (unlike M4 1.4.x).
763#
764# This macro is called frequently, so minimize the amount of additional
765# expansions by skipping m4_ifndef.  Better yet, if __m4_version__ exists,
766# (added in M4 1.6), then let m4 do the job for us (see m4_init).
767m4_define([m4_undefine],
768[m4_if([$#], [0], [[$0]],
769       [$#], [1], [m4_ifdef([$1], [_m4_undefine([$1])],
770			    [m4_fatal([$0: undefined macro: $1])])],
771       [m4_map_args([$0], $@)])])
772
773# _m4_wrap(PRE, POST)
774# -------------------
775# Helper macro for m4_wrap and m4_wrap_lifo.  Allows nested calls to
776# m4_wrap within wrapped text.  Use _m4_defn and _m4_popdef for speed.
777m4_define([_m4_wrap],
778[m4_ifdef([$0_text],
779	  [m4_define([$0_text], [$1]_m4_defn([$0_text])[$2])],
780	  [m4_builtin([m4wrap], [m4_unquote(
781  _m4_defn([$0_text])_m4_popdef([$0_text]))])m4_define([$0_text], [$1$2])])])
782
783# m4_wrap(TEXT)
784# -------------
785# Append TEXT to the list of hooks to be executed at the end of input.
786# Whereas the order of the original may be LIFO in the underlying m4,
787# this version is always FIFO.
788m4_define([m4_wrap],
789[_m4_wrap([], [$1[]])])
790
791# m4_wrap_lifo(TEXT)
792# ------------------
793# Prepend TEXT to the list of hooks to be executed at the end of input.
794# Whereas the order of m4_wrap may be FIFO in the underlying m4, this
795# version is always LIFO.
796m4_define([m4_wrap_lifo],
797[_m4_wrap([$1[]])])
798
799## ------------------------- ##
800## 7. Quoting manipulation.  ##
801## ------------------------- ##
802
803
804# m4_apply(MACRO, LIST)
805# ---------------------
806# Invoke MACRO, with arguments provided from the quoted list of
807# comma-separated quoted arguments.  If LIST is empty, invoke MACRO
808# without arguments.  The expansion will not be concatenated with
809# subsequent text.
810m4_define([m4_apply],
811[m4_if([$2], [], [$1], [$1($2)])[]])
812
813# _m4_apply(MACRO, LIST)
814# ----------------------
815# Like m4_apply, except do nothing if LIST is empty.
816m4_define([_m4_apply],
817[m4_if([$2], [], [], [$1($2)[]])])
818
819
820# m4_count(ARGS)
821# --------------
822# Return a count of how many ARGS are present.
823m4_define([m4_count], [$#])
824
825
826# m4_curry(MACRO, ARG...)
827# -----------------------
828# Perform argument currying.  The expansion of this macro is another
829# macro that takes exactly one argument, appends it to the end of the
830# original ARG list, then invokes MACRO.  For example:
831#   m4_curry([m4_curry], [m4_reverse], [1])([2])([3]) => 3, 2, 1
832# Not quite as practical as m4_incr, but you could also do:
833#   m4_define([add], [m4_eval(([$1]) + ([$2]))])
834#   m4_define([add_one], [m4_curry([add], [1])])
835#   add_one()([2]) => 3
836m4_define([m4_curry], [$1(m4_shift($@,)_$0])
837m4_define([_m4_curry],               [[$1])])
838
839
840# m4_do(STRING, ...)
841# ------------------
842# This macro invokes all its arguments (in sequence, of course).  It is
843# useful for making your macros more structured and readable by dropping
844# unnecessary dnl's and have the macros indented properly.  No concatenation
845# occurs after a STRING; use m4_unquote(m4_join(,STRING)) for that.
846#
847# Please keep foreach.m4 in sync with any adjustments made here.
848m4_define([m4_do],
849[m4_if([$#], 0, [],
850       [$#], 1, [$1[]],
851       [$1[]$0(m4_shift($@))])])
852
853
854# m4_dquote(ARGS)
855# ---------------
856# Return ARGS as a quoted list of quoted arguments.
857m4_define([m4_dquote],  [[$@]])
858
859
860# m4_dquote_elt(ARGS)
861# -------------------
862# Return ARGS as an unquoted list of double-quoted arguments.
863#
864# Please keep foreach.m4 in sync with any adjustments made here.
865m4_define([m4_dquote_elt],
866[m4_if([$#], [0], [],
867       [$#], [1], [[[$1]]],
868       [[[$1]],$0(m4_shift($@))])])
869
870
871# m4_echo(ARGS)
872# -------------
873# Return the ARGS, with the same level of quoting.  Whitespace after
874# unquoted commas are consumed.
875m4_define([m4_echo], [$@])
876
877
878# m4_expand(ARG)
879# _m4_expand(ARG)
880# ---------------
881# Return the expansion of ARG as a single string.  Unlike
882# m4_quote($1), this preserves whitespace following single-quoted
883# commas that appear within ARG.  It also deals with shell case
884# statements.
885#
886#   m4_define([active], [ACT, IVE])
887#   m4_define([active2], [[ACT, IVE]])
888#   m4_quote(active, active2)
889#   => ACT,IVE,ACT, IVE
890#   m4_expand([active, active2])
891#   => ACT, IVE, ACT, IVE
892#
893# Unfortunately, due to limitations in m4, ARG must expand to
894# something with balanced quotes (use quadrigraphs to get around
895# this), and should not contain the unlikely delimiters -=<{( or
896# )}>=-.  It is possible to have unbalanced quoted `(' or `)', as well
897# as unbalanced unquoted `)'.  m4_expand can handle unterminated
898# comments or dnl on the final line, at the expense of speed; it also
899# aids in detecting attempts to incorrectly change the current
900# diversion inside ARG.  Meanwhile, _m4_expand is faster but must be
901# given a terminated expansion, and has no safety checks for
902# mis-diverted text.
903#
904# Exploit that extra unquoted () will group unquoted commas and the
905# following whitespace.  m4_bpatsubst can't handle newlines inside $1,
906# and m4_substr strips quoting.  So we (ab)use m4_changequote, using
907# temporary quotes to remove the delimiters that conveniently included
908# the unquoted () that were added prior to the changequote.
909#
910# Thanks to shell case statements, too many people are prone to pass
911# underquoted `)', so we try to detect that by passing a marker as a
912# fourth argument; if the marker is not present, then we assume that
913# we encountered an early `)', and re-expand the first argument, but
914# this time with one more `(' in the second argument and in the
915# open-quote delimiter.  We must also ignore the slop from the
916# previous try.  The final macro is thus half line-noise, half art.
917m4_define([m4_expand],
918[m4_pushdef([m4_divert], _m4_defn([_m4_divert_unsafe]))]dnl
919[m4_pushdef([m4_divert_push], _m4_defn([_m4_divert_unsafe]))]dnl
920[m4_chomp(_$0([$1
921]))_m4_popdef([m4_divert], [m4_divert_push])])
922
923m4_define([_m4_expand], [$0_([$1], [(], -=<{($1)}>=-, [}>=-])])
924
925m4_define([_m4_expand_],
926[m4_if([$4], [}>=-],
927       [m4_changequote([-=<{$2], [)}>=-])$3m4_changequote([, ])],
928       [$0([$1], [($2], -=<{($2$1)}>=-, [}>=-])m4_ignore$2])])
929
930
931# m4_ignore(ARGS)
932# ---------------
933# Expands to nothing.  Useful for conditionally ignoring an arbitrary
934# number of arguments (see _m4_list_cmp for an example).
935m4_define([m4_ignore])
936
937
938# m4_make_list(ARGS)
939# ------------------
940# Similar to m4_dquote, this creates a quoted list of quoted ARGS.  This
941# version is less efficient than m4_dquote, but separates each argument
942# with a comma and newline, rather than just comma, for readability.
943# When developing an m4sugar algorithm, you could temporarily use
944#   m4_pushdef([m4_dquote],m4_defn([m4_make_list]))
945# around your code to make debugging easier.
946m4_define([m4_make_list], [m4_join([,
947], m4_dquote_elt($@))])
948
949
950# m4_noquote(STRING)
951# ------------------
952# Return the result of ignoring all quotes in STRING and invoking the
953# macros it contains.  Among other things, this is useful for enabling
954# macro invocations inside strings with [] blocks (for instance regexps
955# and help-strings).  On the other hand, since all quotes are disabled,
956# any macro expanded during this time that relies on nested [] quoting
957# will likely crash and burn.  This macro is seldom useful; consider
958# m4_unquote or m4_expand instead.
959m4_define([m4_noquote],
960[m4_changequote([-=<{(],[)}>=-])$1-=<{()}>=-m4_changequote([,])])
961
962
963# m4_quote(ARGS)
964# --------------
965# Return ARGS as a single argument.  Any whitespace after unquoted commas
966# is stripped.  There is always output, even when there were no arguments.
967#
968# It is important to realize the difference between `m4_quote(exp)' and
969# `[exp]': in the first case you obtain the quoted *result* of the
970# expansion of EXP, while in the latter you just obtain the string
971# `exp'.
972m4_define([m4_quote],  [[$*]])
973
974
975# _m4_quote(ARGS)
976# ---------------
977# Like m4_quote, except that when there are no arguments, there is no
978# output.  For conditional scenarios (such as passing _m4_quote as the
979# macro name in m4_mapall), this feature can be used to distinguish between
980# one argument of the empty string vs. no arguments.  However, in the
981# normal case with arguments present, this is less efficient than m4_quote.
982m4_define([_m4_quote],
983[m4_if([$#], [0], [], [[$*]])])
984
985
986# m4_reverse(ARGS)
987# ----------------
988# Output ARGS in reverse order.
989#
990# Please keep foreach.m4 in sync with any adjustments made here.
991m4_define([m4_reverse],
992[m4_if([$#], [0], [], [$#], [1], [[$1]],
993       [$0(m4_shift($@)), [$1]])])
994
995
996# m4_unquote(ARGS)
997# ----------------
998# Remove one layer of quotes from each ARG, performing one level of
999# expansion.  For one argument, m4_unquote([arg]) is more efficient than
1000# m4_do([arg]), but for multiple arguments, the difference is that
1001# m4_unquote separates arguments with commas while m4_do concatenates.
1002# Follow this macro with [] if concatenation with subsequent text is
1003# undesired.
1004m4_define([m4_unquote], [$*])
1005
1006
1007## -------------------------- ##
1008## 8. Implementing m4 loops.  ##
1009## -------------------------- ##
1010
1011
1012# m4_for(VARIABLE, FIRST, LAST, [STEP = +/-1], EXPRESSION)
1013# --------------------------------------------------------
1014# Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO with
1015# increments of STEP.  Both limits are included, and bounds are
1016# checked for consistency.  The algorithm is robust to indirect
1017# VARIABLE names.  Changing VARIABLE inside EXPRESSION will not impact
1018# the number of iterations.
1019#
1020# Uses _m4_defn for speed, and avoid dnl in the macro body.  Factor
1021# the _m4_for call so that EXPRESSION is only parsed once.
1022m4_define([m4_for],
1023[m4_pushdef([$1], m4_eval([$2]))]dnl
1024[m4_cond([m4_eval(([$3]) > ([$2]))], 1,
1025	   [m4_pushdef([_m4_step], m4_eval(m4_default_quoted([$4],
1026	      1)))m4_assert(_m4_step > 0)_$0(_m4_defn([$1]),
1027  m4_eval((([$3]) - ([$2])) / _m4_step * _m4_step + ([$2])), _m4_step,],
1028	 [m4_eval(([$3]) < ([$2]))], 1,
1029	   [m4_pushdef([_m4_step], m4_eval(m4_default_quoted([$4],
1030	      -1)))m4_assert(_m4_step < 0)_$0(_m4_defn([$1]),
1031  m4_eval((([$2]) - ([$3])) / -(_m4_step) * _m4_step + ([$2])), _m4_step,],
1032	 [m4_pushdef([_m4_step])_$0(_m4_defn([$1]), _m4_defn([$1]), 0,])]dnl
1033[[m4_define([$1],], [)$5])m4_popdef([_m4_step], [$1])])
1034
1035# _m4_for(COUNT, LAST, STEP, PRE, POST)
1036# -------------------------------------
1037# Core of the loop, no consistency checks, all arguments are plain
1038# numbers.  Expand PRE[COUNT]POST, then alter COUNT by STEP and
1039# iterate if COUNT is not LAST.
1040m4_define([_m4_for],
1041[$4[$1]$5[]m4_if([$1], [$2], [],
1042		 [$0(m4_eval([$1 + $3]), [$2], [$3], [$4], [$5])])])
1043
1044
1045# Implementing `foreach' loops in m4 is much more tricky than it may
1046# seem.  For example, the old M4 1.4.4 manual had an incorrect example,
1047# which looked like this (when translated to m4sugar):
1048#
1049# | # foreach(VAR, (LIST), STMT)
1050# | m4_define([foreach],
1051# |   [m4_pushdef([$1])_foreach([$1], [$2], [$3])m4_popdef([$1])])
1052# | m4_define([_arg1], [$1])
1053# | m4_define([_foreach],
1054# |   [m4_if([$2], [()], ,
1055# |     [m4_define([$1], _arg1$2)$3[]_foreach([$1], (m4_shift$2), [$3])])])
1056#
1057# But then if you run
1058#
1059# | m4_define(a, 1)
1060# | m4_define(b, 2)
1061# | m4_define(c, 3)
1062# | foreach([f], [([a], [(b], [c)])], [echo f
1063# | ])
1064#
1065# it gives
1066#
1067#  => echo 1
1068#  => echo (2,3)
1069#
1070# which is not what is expected.
1071#
1072# Of course the problem is that many quotes are missing.  So you add
1073# plenty of quotes at random places, until you reach the expected
1074# result.  Alternatively, if you are a quoting wizard, you directly
1075# reach the following implementation (but if you really did, then
1076# apply to the maintenance of m4sugar!).
1077#
1078# | # foreach(VAR, (LIST), STMT)
1079# | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
1080# | m4_define([_arg1], [[$1]])
1081# | m4_define([_foreach],
1082# |  [m4_if($2, [()], ,
1083# |    [m4_define([$1], [_arg1$2])$3[]_foreach([$1], [(m4_shift$2)], [$3])])])
1084#
1085# which this time answers
1086#
1087#  => echo a
1088#  => echo (b
1089#  => echo c)
1090#
1091# Bingo!
1092#
1093# Well, not quite.
1094#
1095# With a better look, you realize that the parens are more a pain than
1096# a help: since anyway you need to quote properly the list, you end up
1097# with always using an outermost pair of parens and an outermost pair
1098# of quotes.  Rejecting the parens both eases the implementation, and
1099# simplifies the use:
1100#
1101# | # foreach(VAR, (LIST), STMT)
1102# | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
1103# | m4_define([_arg1], [$1])
1104# | m4_define([_foreach],
1105# |  [m4_if($2, [], ,
1106# |    [m4_define([$1], [_arg1($2)])$3[]_foreach([$1], [m4_shift($2)], [$3])])])
1107#
1108#
1109# Now, just replace the `$2' with `m4_quote($2)' in the outer `m4_if'
1110# to improve robustness, and you come up with a nice implementation
1111# that doesn't require extra parentheses in the user's LIST.
1112#
1113# But wait -  now the algorithm is quadratic, because every recursion of
1114# the algorithm keeps the entire LIST and merely adds another m4_shift to
1115# the quoted text.  If the user has a lot of elements in LIST, you can
1116# bring the system to its knees with the memory m4 then requires, or trip
1117# the m4 --nesting-limit recursion factor.  The only way to avoid
1118# quadratic growth is ensure m4_shift is expanded prior to the recursion.
1119# Hence the design below.
1120#
1121# The M4 manual now includes a chapter devoted to this issue, with
1122# the lessons learned from m4sugar.  And still, this design is only
1123# optimal for M4 1.6; see foreach.m4 for yet more comments on why
1124# M4 1.4.x uses yet another implementation.
1125
1126
1127# m4_foreach(VARIABLE, LIST, EXPRESSION)
1128# --------------------------------------
1129#
1130# Expand EXPRESSION assigning each value of the LIST to VARIABLE.
1131# LIST should have the form `item_1, item_2, ..., item_n', i.e. the
1132# whole list must *quoted*.  Quote members too if you don't want them
1133# to be expanded.
1134#
1135# This macro is robust to active symbols:
1136#      | m4_define(active, [ACT, IVE])
1137#      | m4_foreach(Var, [active, active], [-Var-])
1138#     => -ACT--IVE--ACT--IVE-
1139#
1140#      | m4_foreach(Var, [[active], [active]], [-Var-])
1141#     => -ACT, IVE--ACT, IVE-
1142#
1143#      | m4_foreach(Var, [[[active]], [[active]]], [-Var-])
1144#     => -active--active-
1145#
1146# This macro is called frequently, so avoid extra expansions such as
1147# m4_ifval and dnl.  Also, since $2 might be quite large, try to use it
1148# as little as possible in _m4_foreach; each extra use requires that much
1149# more memory for expansion.  So, rather than directly compare $2 against
1150# [] and use m4_car/m4_cdr for recursion, we instead unbox the list (which
1151# requires swapping the argument order in the helper), insert an ignored
1152# third argument, and use m4_shift3 to detect when recursion is complete,
1153# at which point this looks very much like m4_map_args.
1154m4_define([m4_foreach],
1155[m4_if([$2], [], [],
1156       [m4_pushdef([$1])_$0([m4_define([$1],], [)$3], [],
1157  $2)m4_popdef([$1])])])
1158
1159# _m4_foreach(PRE, POST, IGNORED, ARG...)
1160# ---------------------------------------
1161# Form the common basis of the m4_foreach and m4_map macros.  For each
1162# ARG, expand PRE[ARG]POST[].  The IGNORED argument makes recursion
1163# easier, and must be supplied rather than implicit.
1164#
1165# Please keep foreach.m4 in sync with any adjustments made here.
1166m4_define([_m4_foreach],
1167[m4_if([$#], [3], [],
1168       [$1[$4]$2[]$0([$1], [$2], m4_shift3($@))])])
1169
1170
1171# m4_foreach_w(VARIABLE, LIST, EXPRESSION)
1172# ----------------------------------------
1173# Like m4_foreach, but the list is whitespace separated.  Depending on
1174# EXPRESSION, it may be more efficient to use m4_map_args_w.
1175#
1176# This macro is robust to active symbols:
1177#    m4_foreach_w([Var], [ active
1178#    b	act\
1179#    ive  ], [-Var-])end
1180#    => -active--b--active-end
1181#
1182# This used to use a slower implementation based on m4_foreach:
1183#   m4_foreach([$1], m4_split(m4_normalize([$2]), [ ]), [$3])
1184m4_define([m4_foreach_w],
1185[m4_pushdef([$1])m4_map_args_w([$2],
1186  [m4_define([$1],], [)$3])m4_popdef([$1])])
1187
1188
1189# m4_map(MACRO, LIST)
1190# m4_mapall(MACRO, LIST)
1191# ----------------------
1192# Invoke MACRO($1), MACRO($2) etc. where $1, $2... are the elements of
1193# LIST.  $1, $2... must in turn be lists, appropriate for m4_apply.
1194# If LIST contains an empty sublist, m4_map skips the expansion of
1195# MACRO, while m4_mapall expands MACRO with no arguments.
1196#
1197# Since LIST may be quite large, we want to minimize how often it
1198# appears in the expansion.  Rather than use m4_car/m4_cdr iteration,
1199# we unbox the list, and use _m4_foreach for iteration.  For m4_map,
1200# an empty list behaves like an empty sublist and gets ignored; for
1201# m4_mapall, we must special-case the empty list.
1202m4_define([m4_map],
1203[_m4_foreach([_m4_apply([$1],], [)], [], $2)])
1204
1205m4_define([m4_mapall],
1206[m4_if([$2], [], [],
1207       [_m4_foreach([m4_apply([$1],], [)], [], $2)])])
1208
1209
1210# m4_map_sep(MACRO, [SEPARATOR], LIST)
1211# m4_mapall_sep(MACRO, [SEPARATOR], LIST)
1212# ---------------------------------------
1213# Invoke MACRO($1), SEPARATOR, MACRO($2), ..., MACRO($N) where $1,
1214# $2... $N are the elements of LIST, and are in turn lists appropriate
1215# for m4_apply.  SEPARATOR is expanded, in order to allow the creation
1216# of a list of arguments by using a single-quoted comma as the
1217# separator.  For each empty sublist, m4_map_sep skips the expansion
1218# of MACRO and SEPARATOR, while m4_mapall_sep expands MACRO with no
1219# arguments.
1220#
1221# For m4_mapall_sep, merely expand the first iteration without the
1222# separator, then include separator as part of subsequent recursion;
1223# but avoid extra expansion of LIST's side-effects via a helper macro.
1224# For m4_map_sep, things are trickier - we don't know if the first
1225# list element is an empty sublist, so we must define a self-modifying
1226# helper macro and use that as the separator instead.
1227m4_define([m4_map_sep],
1228[m4_pushdef([m4_Sep], [m4_define([m4_Sep], _m4_defn([m4_unquote]))])]dnl
1229[_m4_foreach([_m4_apply([m4_Sep([$2])[]$1],], [)], [], $3)m4_popdef([m4_Sep])])
1230
1231m4_define([m4_mapall_sep],
1232[m4_if([$3], [], [], [_$0([$1], [$2], $3)])])
1233
1234m4_define([_m4_mapall_sep],
1235[m4_apply([$1], [$3])_m4_foreach([m4_apply([$2[]$1],], [)], m4_shift2($@))])
1236
1237# m4_map_args(EXPRESSION, ARG...)
1238# -------------------------------
1239# Expand EXPRESSION([ARG]) for each argument.  More efficient than
1240#   m4_foreach([var], [ARG...], [EXPRESSION(m4_defn([var]))])
1241# Shorthand for m4_map_args_sep([EXPRESSION(], [)], [], ARG...).
1242m4_define([m4_map_args],
1243[m4_if([$#], [0], [m4_fatal([$0: too few arguments: $#])],
1244       [$#], [1], [],
1245       [$#], [2], [$1([$2])[]],
1246       [_m4_foreach([$1(], [)], $@)])])
1247
1248
1249# m4_map_args_pair(EXPRESSION, [END-EXPR = EXPRESSION], ARG...)
1250# -------------------------------------------------------------
1251# Perform a pairwise grouping of consecutive ARGs, by expanding
1252# EXPRESSION([ARG1], [ARG2]).  If there are an odd number of ARGs, the
1253# final argument is expanded with END-EXPR([ARGn]).
1254#
1255# For example:
1256#   m4_define([show], [($*)m4_newline])dnl
1257#   m4_map_args_pair([show], [], [a], [b], [c], [d], [e])dnl
1258#   => (a,b)
1259#   => (c,d)
1260#   => (e)
1261#
1262# Please keep foreach.m4 in sync with any adjustments made here.
1263m4_define([m4_map_args_pair],
1264[m4_if([$#], [0], [m4_fatal([$0: too few arguments: $#])],
1265       [$#], [1], [m4_fatal([$0: too few arguments: $#: $1])],
1266       [$#], [2], [],
1267       [$#], [3], [m4_default([$2], [$1])([$3])[]],
1268       [$#], [4], [$1([$3], [$4])[]],
1269       [$1([$3], [$4])[]$0([$1], [$2], m4_shift(m4_shift3($@)))])])
1270
1271
1272# m4_map_args_sep([PRE], [POST], [SEP], ARG...)
1273# ---------------------------------------------
1274# Expand PRE[ARG]POST for each argument, with SEP between arguments.
1275m4_define([m4_map_args_sep],
1276[m4_if([$#], [0], [m4_fatal([$0: too few arguments: $#])],
1277       [$#], [1], [],
1278       [$#], [2], [],
1279       [$#], [3], [],
1280       [$#], [4], [$1[$4]$2[]],
1281       [$1[$4]$2[]_m4_foreach([$3[]$1], [$2], m4_shift3($@))])])
1282
1283
1284# m4_map_args_w(STRING, [PRE], [POST], [SEP])
1285# -------------------------------------------
1286# Perform the expansion of PRE[word]POST[] for each word in STRING
1287# separated by whitespace.  More efficient than:
1288#   m4_foreach_w([var], [STRING], [PRE[]m4_defn([var])POST])
1289# Additionally, expand SEP between words.
1290#
1291# As long as we have to use m4_bpatsubst to split the string, we might
1292# as well make it also apply PRE and POST; this avoids iteration
1293# altogether.  But we must be careful of any \ in PRE or POST.
1294# _m4_strip returns a quoted string, but that's okay, since it also
1295# supplies an empty leading and trailing argument due to our
1296# intentional whitespace around STRING.  We use m4_substr to strip the
1297# empty elements and remove the extra layer of quoting.
1298m4_define([m4_map_args_w],
1299[_$0(_m4_split([ ]m4_flatten([$1])[ ], [[	 ]+],
1300	       m4_if(m4_index([$2$3$4], [\]), [-1], [[$3[]$4[]$2]],
1301		     [m4_bpatsubst([[$3[]$4[]$2]], [\\], [\\\\])])),
1302     m4_len([[]$3[]$4]), m4_len([$4[]$2[]]))])
1303
1304m4_define([_m4_map_args_w],
1305[m4_substr([$1], [$2], m4_eval(m4_len([$1]) - [$2] - [$3]))])
1306
1307
1308# m4_stack_foreach(MACRO, FUNC)
1309# m4_stack_foreach_lifo(MACRO, FUNC)
1310# ----------------------------------
1311# Pass each stacked definition of MACRO to the one-argument macro FUNC.
1312# m4_stack_foreach proceeds in FIFO order, while m4_stack_foreach_lifo
1313# processes the topmost definitions first.  In addition, FUNC should
1314# not push or pop definitions of MACRO, and should not expect anything about
1315# the active definition of MACRO (it will not be the topmost, and may not
1316# be the one passed to FUNC either).
1317#
1318# Some macros simply can't be examined with this method: namely,
1319# anything involved in the implementation of _m4_stack_reverse.
1320m4_define([m4_stack_foreach],
1321[_m4_stack_reverse([$1], [m4_tmp-$1])]dnl
1322[_m4_stack_reverse([m4_tmp-$1], [$1], [$2(_m4_defn([m4_tmp-$1]))])])
1323
1324m4_define([m4_stack_foreach_lifo],
1325[_m4_stack_reverse([$1], [m4_tmp-$1], [$2(_m4_defn([m4_tmp-$1]))])]dnl
1326[_m4_stack_reverse([m4_tmp-$1], [$1])])
1327
1328# m4_stack_foreach_sep(MACRO, [PRE], [POST], [SEP])
1329# m4_stack_foreach_sep_lifo(MACRO, [PRE], [POST], [SEP])
1330# ------------------------------------------------------
1331# Similar to m4_stack_foreach and m4_stack_foreach_lifo, in that every
1332# definition of a pushdef stack will be visited.  But rather than
1333# passing the definition as a single argument to a macro, this variant
1334# expands the concatenation of PRE[]definition[]POST, and expands SEP
1335# between consecutive expansions.  Note that m4_stack_foreach([a], [b])
1336# is equivalent to m4_stack_foreach_sep([a], [b(], [)]).
1337m4_define([m4_stack_foreach_sep],
1338[_m4_stack_reverse([$1], [m4_tmp-$1])]dnl
1339[_m4_stack_reverse([m4_tmp-$1], [$1], [$2[]_m4_defn([m4_tmp-$1])$3], [$4[]])])
1340
1341m4_define([m4_stack_foreach_sep_lifo],
1342[_m4_stack_reverse([$1], [m4_tmp-$1], [$2[]_m4_defn([m4_tmp-$1])$3], [$4[]])]dnl
1343[_m4_stack_reverse([m4_tmp-$1], [$1])])
1344
1345
1346# _m4_stack_reverse(OLD, NEW, [ACTION], [SEP])
1347# --------------------------------------------
1348# A recursive worker for pushdef stack manipulation.  Destructively
1349# copy the OLD stack into the NEW, and expanding ACTION for each
1350# iteration.  After the first iteration, SEP is promoted to the front
1351# of ACTION (note that SEP should include a trailing [] if it is to
1352# avoid interfering with ACTION).  The current definition is examined
1353# after the NEW has been pushed but before OLD has been popped; this
1354# order is important, as ACTION is permitted to operate on either
1355# _m4_defn([OLD]) or _m4_defn([NEW]).  Since the operation is
1356# destructive, this macro is generally used twice, with a temporary
1357# macro name holding the swapped copy.
1358m4_define([_m4_stack_reverse],
1359[m4_ifdef([$1], [m4_pushdef([$2],
1360  _m4_defn([$1]))$3[]_m4_popdef([$1])$0([$1], [$2], [$4$3])])])
1361
1362
1363
1364## --------------------------- ##
1365## 9. More diversion support.  ##
1366## --------------------------- ##
1367
1368
1369# m4_cleardivert(DIVERSION-NAME...)
1370# ---------------------------------
1371# Discard any text in DIVERSION-NAME.
1372#
1373# This works even inside m4_expand.
1374m4_define([m4_cleardivert],
1375[m4_if([$#], [0], [m4_fatal([$0: missing argument])],
1376       [_m4_divert_raw([-1])m4_undivert($@)_m4_divert_raw(
1377	 _m4_divert(_m4_defn([_m4_divert_diversion]), [-]))])])
1378
1379
1380# _m4_divert(DIVERSION-NAME or NUMBER, [NOWARN])
1381# ----------------------------------------------
1382# If DIVERSION-NAME is the name of a diversion, return its number,
1383# otherwise if it is a NUMBER return it.  Issue a warning about
1384# the use of a number instead of a name, unless NOWARN is provided.
1385m4_define([_m4_divert],
1386[m4_ifdef([_m4_divert($1)],
1387	  [m4_indir([_m4_divert($1)])],
1388	  [m4_if([$2], [], [m4_warn([syntax],
1389	     [prefer named diversions])])$1])])
1390
1391# KILL is only used to suppress output.
1392m4_define([_m4_divert(KILL)],           -1)
1393
1394# The empty diversion name is a synonym for 0.
1395m4_define([_m4_divert()],                0)
1396
1397
1398# m4_divert_stack
1399# ---------------
1400# Print the diversion stack, if it's nonempty.  The caller is
1401# responsible for any leading or trailing newline.
1402m4_define([m4_divert_stack],
1403[m4_stack_foreach_sep_lifo([_m4_divert_stack], [], [], [
1404])])
1405
1406
1407# m4_divert_stack_push(MACRO-NAME, DIVERSION-NAME)
1408# ------------------------------------------------
1409# Form an entry of the diversion stack from caller MACRO-NAME and
1410# entering DIVERSION-NAME and push it.
1411m4_define([m4_divert_stack_push],
1412[m4_pushdef([_m4_divert_stack], m4_location[: $1: $2])])
1413
1414
1415# m4_divert(DIVERSION-NAME)
1416# -------------------------
1417# Change the diversion stream to DIVERSION-NAME.
1418m4_define([m4_divert],
1419[m4_popdef([_m4_divert_stack])]dnl
1420[m4_define([_m4_divert_diversion], [$1])]dnl
1421[m4_divert_stack_push([$0], [$1])]dnl
1422[_m4_divert_raw(_m4_divert([$1]))])
1423
1424
1425# m4_divert_push(DIVERSION-NAME, [NOWARN])
1426# ----------------------------------------
1427# Change the diversion stream to DIVERSION-NAME, while stacking old values.
1428# For internal use only: if NOWARN is not empty, DIVERSION-NAME can be a
1429# number instead of a name.
1430m4_define([m4_divert_push],
1431[m4_divert_stack_push([$0], [$1])]dnl
1432[m4_pushdef([_m4_divert_diversion], [$1])]dnl
1433[_m4_divert_raw(_m4_divert([$1], [$2]))])
1434
1435
1436# m4_divert_pop([DIVERSION-NAME])
1437# -------------------------------
1438# Change the diversion stream to its previous value, unstacking it.
1439# If specified, verify we left DIVERSION-NAME.
1440# When we pop the last value from the stack, we divert to -1.
1441m4_define([m4_divert_pop],
1442[m4_if([$1], [], [],
1443       [$1], _m4_defn([_m4_divert_diversion]), [],
1444       [m4_fatal([$0($1): diversion mismatch:
1445]m4_divert_stack)])]dnl
1446[_m4_popdef([_m4_divert_stack], [_m4_divert_diversion])]dnl
1447[m4_ifdef([_m4_divert_diversion], [],
1448	   [m4_fatal([too many m4_divert_pop])])]dnl
1449[_m4_divert_raw(_m4_divert(_m4_defn([_m4_divert_diversion]), [-]))])
1450
1451
1452# m4_divert_text(DIVERSION-NAME, CONTENT)
1453# ---------------------------------------
1454# Output CONTENT into DIVERSION-NAME (which may be a number actually).
1455# An end of line is appended for free to CONTENT.
1456m4_define([m4_divert_text],
1457[m4_divert_push([$1])$2
1458m4_divert_pop([$1])])
1459
1460
1461# m4_divert_once(DIVERSION-NAME, CONTENT)
1462# ---------------------------------------
1463# Output CONTENT into DIVERSION-NAME once, if not already there.
1464# An end of line is appended for free to CONTENT.
1465m4_define([m4_divert_once],
1466[m4_expand_once([m4_divert_text([$1], [$2])])])
1467
1468
1469# _m4_divert_unsafe(DIVERSION-NAME)
1470# ---------------------------------
1471# Issue a warning that the attempt to change the current diversion to
1472# DIVERSION-NAME is unsafe, because this macro is being expanded
1473# during argument collection of m4_expand.
1474m4_define([_m4_divert_unsafe],
1475[m4_fatal([$0: cannot change diversion to `$1' inside m4_expand])])
1476
1477
1478# m4_undivert(DIVERSION-NAME...)
1479# ------------------------------
1480# Undivert DIVERSION-NAME.  Unlike the M4 version, this requires at
1481# least one DIVERSION-NAME; also, due to support for named diversions,
1482# this should not be used to undivert files.
1483m4_define([m4_undivert],
1484[m4_if([$#], [0], [m4_fatal([$0: missing argument])],
1485       [$#], [1], [_m4_undivert(_m4_divert([$1]))],
1486       [m4_map_args([$0], $@)])])
1487
1488
1489## --------------------------------------------- ##
1490## 10. Defining macros with bells and whistles.  ##
1491## --------------------------------------------- ##
1492
1493# `m4_defun' is basically `m4_define' but it equips the macro with the
1494# needed machinery for `m4_require'.  A macro must be m4_defun'd if
1495# either it is m4_require'd, or it m4_require's.
1496#
1497# Two things deserve attention and are detailed below:
1498#  1. Implementation of m4_require
1499#  2. Keeping track of the expansion stack
1500#
1501# 1. Implementation of m4_require
1502# ===============================
1503#
1504# Of course m4_defun calls m4_provide, so that a macro which has
1505# been expanded is not expanded again when m4_require'd, but the
1506# difficult part is the proper expansion of macros when they are
1507# m4_require'd.
1508#
1509# The implementation is based on three ideas, (i) using diversions to
1510# prepare the expansion of the macro and its dependencies (by Franc,ois
1511# Pinard), (ii) expand the most recently m4_require'd macros _after_
1512# the previous macros (by Axel Thimm), and (iii) track instances of
1513# provide before require (by Eric Blake).
1514#
1515#
1516# The first idea: why use diversions?
1517# -----------------------------------
1518#
1519# When a macro requires another, the other macro is expanded in new
1520# diversion, GROW.  When the outer macro is fully expanded, we first
1521# undivert the most nested diversions (GROW - 1...), and finally
1522# undivert GROW.  To understand why we need several diversions,
1523# consider the following example:
1524#
1525# | m4_defun([TEST1], [Test...m4_require([TEST2])1])
1526# | m4_defun([TEST2], [Test...m4_require([TEST3])2])
1527# | m4_defun([TEST3], [Test...3])
1528#
1529# Because m4_require is not required to be first in the outer macros, we
1530# must keep the expansions of the various levels of m4_require separated.
1531# Right before executing the epilogue of TEST1, we have:
1532#
1533#	   GROW - 2: Test...3
1534#	   GROW - 1: Test...2
1535#	   GROW:     Test...1
1536#	   BODY:
1537#
1538# Finally the epilogue of TEST1 undiverts GROW - 2, GROW - 1, and
1539# GROW into the regular flow, BODY.
1540#
1541#	   GROW - 2:
1542#	   GROW - 1:
1543#	   GROW:
1544#	   BODY:        Test...3; Test...2; Test...1
1545#
1546# (The semicolons are here for clarification, but of course are not
1547# emitted.)  This is what Autoconf 2.0 (I think) to 2.13 (I'm sure)
1548# implement.
1549#
1550#
1551# The second idea: first required first out
1552# -----------------------------------------
1553#
1554# The natural implementation of the idea above is buggy and produces
1555# very surprising results in some situations.  Let's consider the
1556# following example to explain the bug:
1557#
1558# | m4_defun([TEST1],  [m4_require([TEST2a])m4_require([TEST2b])])
1559# | m4_defun([TEST2a], [])
1560# | m4_defun([TEST2b], [m4_require([TEST3])])
1561# | m4_defun([TEST3],  [m4_require([TEST2a])])
1562# |
1563# | AC_INIT
1564# | TEST1
1565#
1566# The dependencies between the macros are:
1567#
1568#		 3 --- 2b
1569#		/        \              is m4_require'd by
1570#	       /          \       left -------------------- right
1571#	    2a ------------ 1
1572#
1573# If you strictly apply the rules given in the previous section you get:
1574#
1575#	   GROW - 2: TEST3
1576#	   GROW - 1: TEST2a; TEST2b
1577#	   GROW:     TEST1
1578#	   BODY:
1579#
1580# (TEST2a, although required by TEST3 is not expanded in GROW - 3
1581# because is has already been expanded before in GROW - 1, so it has
1582# been AC_PROVIDE'd, so it is not expanded again) so when you undivert
1583# the stack of diversions, you get:
1584#
1585#	   GROW - 2:
1586#	   GROW - 1:
1587#	   GROW:
1588#	   BODY:        TEST3; TEST2a; TEST2b; TEST1
1589#
1590# i.e., TEST2a is expanded after TEST3 although the latter required the
1591# former.
1592#
1593# Starting from 2.50, we use an implementation provided by Axel Thimm.
1594# The idea is simple: the order in which macros are emitted must be the
1595# same as the one in which macros are expanded.  (The bug above can
1596# indeed be described as: a macro has been m4_provide'd before its
1597# dependent, but it is emitted after: the lack of correlation between
1598# emission and expansion order is guilty).
1599#
1600# How to do that?  You keep the stack of diversions to elaborate the
1601# macros, but each time a macro is fully expanded, emit it immediately.
1602#
1603# In the example above, when TEST2a is expanded, but it's epilogue is
1604# not run yet, you have:
1605#
1606#	   GROW - 2:
1607#	   GROW - 1: TEST2a
1608#	   GROW:     Elaboration of TEST1
1609#	   BODY:
1610#
1611# The epilogue of TEST2a emits it immediately:
1612#
1613#	   GROW - 2:
1614#	   GROW - 1:
1615#	   GROW:     Elaboration of TEST1
1616#	   BODY:     TEST2a
1617#
1618# TEST2b then requires TEST3, so right before the epilogue of TEST3, you
1619# have:
1620#
1621#	   GROW - 2: TEST3
1622#	   GROW - 1: Elaboration of TEST2b
1623#	   GROW:     Elaboration of TEST1
1624#	   BODY:      TEST2a
1625#
1626# The epilogue of TEST3 emits it:
1627#
1628#	   GROW - 2:
1629#	   GROW - 1: Elaboration of TEST2b
1630#	   GROW:     Elaboration of TEST1
1631#	   BODY:     TEST2a; TEST3
1632#
1633# TEST2b is now completely expanded, and emitted:
1634#
1635#	   GROW - 2:
1636#	   GROW - 1:
1637#	   GROW:     Elaboration of TEST1
1638#	   BODY:     TEST2a; TEST3; TEST2b
1639#
1640# and finally, TEST1 is finished and emitted:
1641#
1642#	   GROW - 2:
1643#	   GROW - 1:
1644#	   GROW:
1645#	   BODY:     TEST2a; TEST3; TEST2b: TEST1
1646#
1647# The idea is simple, but the implementation is a bit involved.  If
1648# you are like me, you will want to see the actual functioning of this
1649# implementation to be convinced.  The next section gives the full
1650# details.
1651#
1652#
1653# The Axel Thimm implementation at work
1654# -------------------------------------
1655#
1656# We consider the macros above, and this configure.ac:
1657#
1658#	    AC_INIT
1659#	    TEST1
1660#
1661# You should keep the definitions of _m4_defun_pro, _m4_defun_epi, and
1662# m4_require at hand to follow the steps.
1663#
1664# This implementation tries not to assume that the current diversion is
1665# BODY, so as soon as a macro (m4_defun'd) is expanded, we first
1666# record the current diversion under the name _m4_divert_dump (denoted
1667# DUMP below for short).  This introduces an important difference with
1668# the previous versions of Autoconf: you cannot use m4_require if you
1669# are not inside an m4_defun'd macro, and especially, you cannot
1670# m4_require directly from the top level.
1671#
1672# We have not tried to simulate the old behavior (better yet, we
1673# diagnose it), because it is too dangerous: a macro m4_require'd from
1674# the top level is expanded before the body of `configure', i.e., before
1675# any other test was run.  I let you imagine the result of requiring
1676# AC_STDC_HEADERS for instance, before AC_PROG_CC was actually run....
1677#
1678# After AC_INIT was run, the current diversion is BODY.
1679# * AC_INIT was run
1680#   DUMP:                undefined
1681#   diversion stack:     BODY |-
1682#
1683# * TEST1 is expanded
1684# The prologue of TEST1 sets _m4_divert_dump, which is the diversion
1685# where the current elaboration will be dumped, to the current
1686# diversion.  It also m4_divert_push to GROW, where the full
1687# expansion of TEST1 and its dependencies will be elaborated.
1688#   DUMP:        BODY
1689#   BODY:        empty
1690#   diversions:  GROW, BODY |-
1691#
1692# * TEST1 requires TEST2a
1693# _m4_require_call m4_divert_pushes another temporary diversion,
1694# GROW - 1, and expands TEST2a in there.
1695#   DUMP:        BODY
1696#   BODY:        empty
1697#   GROW - 1:    TEST2a
1698#   diversions:  GROW - 1, GROW, BODY |-
1699# Then the content of the temporary diversion is moved to DUMP and the
1700# temporary diversion is popped.
1701#   DUMP:        BODY
1702#   BODY:        TEST2a
1703#   diversions:  GROW, BODY |-
1704#
1705# * TEST1 requires TEST2b
1706# Again, _m4_require_call pushes GROW - 1 and heads to expand TEST2b.
1707#   DUMP:        BODY
1708#   BODY:        TEST2a
1709#   diversions:  GROW - 1, GROW, BODY |-
1710#
1711# * TEST2b requires TEST3
1712# _m4_require_call pushes GROW - 2 and expands TEST3 here.
1713# (TEST3 requires TEST2a, but TEST2a has already been m4_provide'd, so
1714# nothing happens.)
1715#   DUMP:        BODY
1716#   BODY:        TEST2a
1717#   GROW - 2:    TEST3
1718#   diversions:  GROW - 2, GROW - 1, GROW, BODY |-
1719# Then the diversion is appended to DUMP, and popped.
1720#   DUMP:        BODY
1721#   BODY:        TEST2a; TEST3
1722#   diversions:  GROW - 1, GROW, BODY |-
1723#
1724# * TEST1 requires TEST2b (contd.)
1725# The content of TEST2b is expanded...
1726#   DUMP:        BODY
1727#   BODY:        TEST2a; TEST3
1728#   GROW - 1:    TEST2b,
1729#   diversions:  GROW - 1, GROW, BODY |-
1730# ... and moved to DUMP.
1731#   DUMP:        BODY
1732#   BODY:        TEST2a; TEST3; TEST2b
1733#   diversions:  GROW, BODY |-
1734#
1735# * TEST1 is expanded: epilogue
1736# TEST1's own content is in GROW...
1737#   DUMP:        BODY
1738#   BODY:        TEST2a; TEST3; TEST2b
1739#   GROW:        TEST1
1740#   diversions:  BODY |-
1741# ... and it's epilogue moves it to DUMP and then undefines DUMP.
1742#   DUMP:       undefined
1743#   BODY:       TEST2a; TEST3; TEST2b; TEST1
1744#   diversions: BODY |-
1745#
1746#
1747# The third idea: track macros provided before they were required
1748# ---------------------------------------------------------------
1749#
1750# Using just the first two ideas, Autoconf 2.50 through 2.63 still had
1751# a subtle bug for more than seven years.  Let's consider the
1752# following example to explain the bug:
1753#
1754# | m4_defun([TEST1], [1])
1755# | m4_defun([TEST2], [2[]m4_require([TEST1])])
1756# | m4_defun([TEST3], [3 TEST1 m4_require([TEST2])])
1757# | TEST3
1758#
1759# After the prologue of TEST3, we are collecting text in GROW with the
1760# intent of dumping it in BODY during the epilogue.  Next, we
1761# encounter the direct invocation of TEST1, which provides the macro
1762# in place in GROW.  From there, we encounter a requirement for TEST2,
1763# which must be collected in a new diversion.  While expanding TEST2,
1764# we encounter a requirement for TEST1, but since it has already been
1765# expanded, the Axel Thimm algorithm states that we can treat it as a
1766# no-op.  But that would lead to an end result of `2 3 1', meaning
1767# that we have once again output a macro (TEST2) prior to its
1768# requirements (TEST1).
1769#
1770# The problem can only occur if a single defun'd macro first provides,
1771# then later indirectly requires, the same macro.  Note that directly
1772# expanding then requiring a macro is okay: because the dependency was
1773# met, the require phase can be a no-op.  For that matter, the outer
1774# macro can even require two helpers, where the first helper expands
1775# the macro, and the second helper indirectly requires the macro.
1776# Out-of-order expansion is only present if the inner macro is
1777# required by something that will be hoisted in front of where the
1778# direct expansion occurred.  In other words, we must be careful not
1779# to warn on:
1780#
1781# | m4_defun([TEST4], [4])
1782# | m4_defun([TEST5], [5 TEST4 m4_require([TEST4])])
1783# | TEST5 => 5 4
1784#
1785# or even the more complex:
1786#
1787# | m4_defun([TEST6], [6])
1788# | m4_defun([TEST7], [7 TEST6])
1789# | m4_defun([TEST8], [8 m4_require([TEST6])])
1790# | m4_defun([TEST9], [9 m4_require([TEST8])])
1791# | m4_defun([TEST10], [10 m4_require([TEST7]) m4_require([TEST9])])
1792# | TEST10 => 7 6 8 9 10
1793#
1794# So, to detect whether a require was direct or indirect, m4_defun and
1795# m4_require track the name of the macro that caused a diversion to be
1796# created (using the stack _m4_diverting, coupled with an O(1) lookup
1797# _m4_diverting([NAME])), and m4_provide stores the name associated
1798# with the diversion at which a macro was provided.  A require call is
1799# direct if it occurs within the same diversion where the macro was
1800# provided, or if the diversion associated with the providing context
1801# has been collected.
1802#
1803# The implementation of the warning involves tracking the set of
1804# macros which have been provided since the start of the outermost
1805# defun'd macro (the set is named _m4_provide).  When starting an
1806# outermost macro, the set is emptied; when a macro is provided, it is
1807# added to the set; when require expands the body of a macro, it is
1808# removed from the set; and when a macro is indirectly required, the
1809# set is checked.  If a macro is in the set, then it has been provided
1810# before it was required, and we satisfy dependencies by expanding the
1811# macro as if it had never been provided; in the example given above,
1812# this means we now output `1 2 3 1'.  Meanwhile, a warning is issued
1813# to inform the user that her macros trigger the bug in older autoconf
1814# versions, and that her output file now contains redundant contents
1815# (and possibly new problems, if the repeated macro was not
1816# idempotent).  Meanwhile, macros defined by m4_defun_once instead of
1817# m4_defun are idempotent, avoiding any warning or duplicate output.
1818#
1819#
1820# 2. Keeping track of the expansion stack
1821# =======================================
1822#
1823# When M4 expansion goes wrong it is often extremely hard to find the
1824# path amongst macros that drove to the failure.  What is needed is
1825# the stack of macro `calls'. One could imagine that GNU M4 would
1826# maintain a stack of macro expansions, unfortunately it doesn't, so
1827# we do it by hand.  This is of course extremely costly, but the help
1828# this stack provides is worth it.  Nevertheless to limit the
1829# performance penalty this is implemented only for m4_defun'd macros,
1830# not for define'd macros.
1831#
1832# Each time we enter an m4_defun'd macros, we add a definition in
1833# _m4_expansion_stack, and when we exit the macro, we remove it (thanks
1834# to pushdef/popdef).  m4_stack_foreach is used to print the expansion
1835# stack in the rare cases when it's needed.
1836#
1837# In addition, we want to detect circular m4_require dependencies.
1838# Each time we expand a macro FOO we define _m4_expanding(FOO); and
1839# m4_require(BAR) simply checks whether _m4_expanding(BAR) is defined.
1840
1841
1842# m4_expansion_stack
1843# ------------------
1844# Expands to the entire contents of the expansion stack.  The caller
1845# must supply a trailing newline.  This macro always prints a
1846# location; check whether _m4_expansion_stack is defined to filter out
1847# the case when no defun'd macro is in force.
1848m4_define([m4_expansion_stack],
1849[m4_stack_foreach_sep_lifo([_$0], [_$0_entry(], [)
1850])m4_location[: the top level]])
1851
1852# _m4_expansion_stack_entry(MACRO)
1853# --------------------------------
1854# Format an entry for MACRO found on the expansion stack.
1855m4_define([_m4_expansion_stack_entry],
1856[_m4_defn([m4_location($1)])[: $1 is expanded from...]])
1857
1858# m4_expansion_stack_push(MACRO)
1859# ------------------------------
1860# Form an entry of the expansion stack on entry to MACRO and push it.
1861m4_define([m4_expansion_stack_push],
1862[m4_pushdef([_m4_expansion_stack], [$1])])
1863
1864
1865# _m4_divert(GROW)
1866# ----------------
1867# This diversion is used by the m4_defun/m4_require machinery.  It is
1868# important to keep room before GROW because for each nested
1869# AC_REQUIRE we use an additional diversion (i.e., two m4_require's
1870# will use GROW - 2.  More than 3 levels has never seemed to be
1871# needed.)
1872#
1873# ...
1874# - GROW - 2
1875#   m4_require'd code, 2 level deep
1876# - GROW - 1
1877#   m4_require'd code, 1 level deep
1878# - GROW
1879#   m4_defun'd macros are elaborated here.
1880
1881m4_define([_m4_divert(GROW)],       10000)
1882
1883
1884# _m4_defun_pro(MACRO-NAME)
1885# -------------------------
1886# The prologue for Autoconf macros.
1887#
1888# This is called frequently, so minimize the number of macro invocations
1889# by avoiding dnl and m4_defn overhead.
1890m4_define([_m4_defun_pro],
1891[m4_ifdef([_m4_expansion_stack], [], [_m4_defun_pro_outer([$1])])]dnl
1892[m4_expansion_stack_push([$1])m4_pushdef([_m4_expanding($1)])])
1893
1894m4_define([_m4_defun_pro_outer],
1895[m4_set_delete([_m4_provide])]dnl
1896[m4_pushdef([_m4_diverting([$1])])m4_pushdef([_m4_diverting], [$1])]dnl
1897[m4_pushdef([_m4_divert_dump], m4_divnum)m4_divert_push([GROW])])
1898
1899# _m4_defun_epi(MACRO-NAME)
1900# -------------------------
1901# The Epilogue for Autoconf macros.  MACRO-NAME only helps tracing
1902# the PRO/EPI pairs.
1903#
1904# This is called frequently, so minimize the number of macro invocations
1905# by avoiding dnl and m4_popdef overhead.
1906m4_define([_m4_defun_epi],
1907[_m4_popdef([_m4_expanding($1)], [_m4_expansion_stack])]dnl
1908[m4_ifdef([_m4_expansion_stack], [], [_m4_defun_epi_outer([$1])])]dnl
1909[m4_provide([$1])])
1910
1911m4_define([_m4_defun_epi_outer],
1912[_m4_popdef([_m4_divert_dump], [_m4_diverting([$1])], [_m4_diverting])]dnl
1913[m4_divert_pop([GROW])m4_undivert([GROW])])
1914
1915
1916# _m4_divert_dump
1917# ---------------
1918# If blank, we are outside of any defun'd macro.  Otherwise, expands
1919# to the diversion number (not name) where require'd macros should be
1920# moved once completed.
1921m4_define([_m4_divert_dump])
1922
1923
1924# m4_divert_require(DIVERSION, NAME-TO-CHECK, [BODY-TO-EXPAND])
1925# -------------------------------------------------------------
1926# Same as m4_require, but BODY-TO-EXPAND goes into the named DIVERSION;
1927# requirements still go in the current diversion though.
1928#
1929m4_define([m4_divert_require],
1930[m4_ifdef([_m4_expanding($2)],
1931  [m4_fatal([$0: circular dependency of $2])])]dnl
1932[m4_if(_m4_divert_dump, [],
1933  [m4_fatal([$0($2): cannot be used outside of an m4_defun'd macro])])]dnl
1934[m4_provide_if([$2], [],
1935  [_m4_require_call([$2], [$3], _m4_divert([$1], [-]))])])
1936
1937
1938# m4_defun(NAME, EXPANSION, [MACRO = m4_define])
1939# ----------------------------------------------
1940# Define a macro NAME which automatically provides itself.  Add
1941# machinery so the macro automatically switches expansion to the
1942# diversion stack if it is not already using it, prior to EXPANSION.
1943# In this case, once finished, it will bring back all the code
1944# accumulated in the diversion stack.  This, combined with m4_require,
1945# achieves the topological ordering of macros.  We don't use this
1946# macro to define some frequently called macros that are not involved
1947# in ordering constraints, to save m4 processing.
1948#
1949# MACRO is an undocumented argument; when set to m4_pushdef, and NAME
1950# is already defined, the new definition is added to the pushdef
1951# stack, rather than overwriting the current definition.  It can thus
1952# be used to write self-modifying macros, which pop themselves to a
1953# previously m4_define'd definition so that subsequent use of the
1954# macro is faster.
1955m4_define([m4_defun],
1956[m4_define([m4_location($1)], m4_location)]dnl
1957[m4_default([$3], [m4_define])([$1],
1958  [_m4_defun_pro(]m4_dquote($[0])[)$2[]_m4_defun_epi(]m4_dquote($[0])[)])])
1959
1960
1961# m4_defun_init(NAME, INIT, COMMON)
1962# ---------------------------------
1963# Like m4_defun, but split EXPANSION into two portions: INIT which is
1964# done only the first time NAME is invoked, and COMMON which is
1965# expanded every time.
1966#
1967# For now, the COMMON definition is always m4_define'd, giving an even
1968# lighter-weight definition.  m4_defun allows self-providing, but once
1969# a macro is provided, m4_require no longer cares if it is m4_define'd
1970# or m4_defun'd.  m4_defun also provides location tracking to identify
1971# dependency bugs, but once the INIT has been expanded, we know there
1972# are no dependency bugs.  However, if a future use needs COMMON to be
1973# m4_defun'd, we can add a parameter, similar to the third parameter
1974# to m4_defun.
1975m4_define([m4_defun_init],
1976[m4_define([$1], [$3[]])m4_defun([$1],
1977   [$2[]_m4_popdef(]m4_dquote($[0])[)m4_indir(]m4_dquote($[0])dnl
1978[m4_if(]m4_dquote($[#])[, [0], [], ]m4_dquote([,$]@)[))], [m4_pushdef])])
1979
1980
1981# m4_defun_once(NAME, EXPANSION)
1982# ------------------------------
1983# Like m4_defun, but guarantee that EXPANSION only happens once
1984# (thereafter, using NAME is a no-op).
1985#
1986# If _m4_divert_dump is empty, we are called at the top level;
1987# otherwise, we must ensure that we are required in front of the
1988# current defun'd macro.  Use a helper macro so that EXPANSION need
1989# only occur once in the definition of NAME, since it might be large.
1990m4_define([m4_defun_once],
1991[m4_define([m4_location($1)], m4_location)]dnl
1992[m4_define([$1], [_m4_defun_once([$1], [$2], m4_if(_m4_divert_dump, [],
1993  [[_m4_defun_pro([$1])m4_unquote(], [)_m4_defun_epi([$1])]],
1994m4_ifdef([_m4_diverting([$1])], [-]), [-], [[m4_unquote(], [)]],
1995  [[_m4_require_call([$1],], [, _m4_divert_dump)]]))])])
1996
1997m4_define([_m4_defun_once],
1998[m4_pushdef([$1])$3[$2[]m4_provide([$1])]$4])
1999
2000
2001# m4_pattern_forbid(ERE, [WHY])
2002# -----------------------------
2003# Declare that no token matching the forbidden perl extended regular
2004# expression ERE should be seen in the output unless...
2005m4_define([m4_pattern_forbid], [])
2006
2007
2008# m4_pattern_allow(ERE)
2009# ---------------------
2010# ... that token also matches the allowed extended regular expression ERE.
2011# Both used via traces, by autom4te post-processing.
2012m4_define([m4_pattern_allow], [])
2013
2014
2015## --------------------------------- ##
2016## 11. Dependencies between macros.  ##
2017## --------------------------------- ##
2018
2019
2020# m4_before(THIS-MACRO-NAME, CALLED-MACRO-NAME)
2021# ---------------------------------------------
2022# Issue a warning if CALLED-MACRO-NAME was called before THIS-MACRO-NAME.
2023m4_define([m4_before],
2024[m4_provide_if([$2],
2025	       [m4_warn([syntax], [$2 was called before $1])])])
2026
2027
2028# m4_require(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
2029# -----------------------------------------------------------
2030# If NAME-TO-CHECK has never been expanded (actually, if it is not
2031# m4_provide'd), expand BODY-TO-EXPAND *before* the current macro
2032# expansion; follow the expansion with a newline.  Once expanded, emit
2033# it in _m4_divert_dump.  Keep track of the m4_require chain in
2034# _m4_expansion_stack.
2035#
2036# The normal cases are:
2037#
2038# - NAME-TO-CHECK == BODY-TO-EXPAND
2039#   Which you can use for regular macros with or without arguments, e.g.,
2040#     m4_require([AC_PROG_CC], [AC_PROG_CC])
2041#     m4_require([AC_CHECK_HEADERS(threads.h)], [AC_CHECK_HEADERS(threads.h)])
2042#   which is just the same as
2043#     m4_require([AC_PROG_CC])
2044#     m4_require([AC_CHECK_HEADERS(threads.h)])
2045#
2046# - BODY-TO-EXPAND == m4_indir([NAME-TO-CHECK])
2047#   In the case of macros with irregular names.  For instance:
2048#     m4_require([AC_LANG_COMPILER(C)], [indir([AC_LANG_COMPILER(C)])])
2049#   which means `if the macro named `AC_LANG_COMPILER(C)' (the parens are
2050#   part of the name, it is not an argument) has not been run, then
2051#   call it.'
2052#   Had you used
2053#     m4_require([AC_LANG_COMPILER(C)], [AC_LANG_COMPILER(C)])
2054#   then m4_require would have tried to expand `AC_LANG_COMPILER(C)', i.e.,
2055#   call the macro `AC_LANG_COMPILER' with `C' as argument.
2056#
2057#   You could argue that `AC_LANG_COMPILER', when it receives an argument
2058#   such as `C' should dispatch the call to `AC_LANG_COMPILER(C)'.  But this
2059#   `extension' prevents `AC_LANG_COMPILER' from having actual arguments that
2060#   it passes to `AC_LANG_COMPILER(C)'.
2061#
2062# This is called frequently, so minimize the number of macro invocations
2063# by avoiding dnl and other overhead on the common path.
2064m4_define([m4_require],
2065[m4_ifdef([_m4_expanding($1)],
2066  [m4_fatal([$0: circular dependency of $1])])]dnl
2067[m4_if(_m4_divert_dump, [],
2068  [m4_fatal([$0($1): cannot be used outside of an ]dnl
2069m4_if([$0], [m4_require], [[m4_defun]], [[AC_DEFUN]])['d macro])])]dnl
2070[m4_provide_if([$1], [m4_set_contains([_m4_provide], [$1],
2071    [_m4_require_check([$1], _m4_defn([m4_provide($1)]), [$0])], [m4_ignore])],
2072  [_m4_require_call])([$1], [$2], _m4_divert_dump)])
2073
2074
2075# _m4_require_call(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK],
2076#                  DIVERSION-NUMBER)
2077# -----------------------------------------------------------------
2078# If m4_require decides to expand the body, it calls this macro.  The
2079# expansion is placed in DIVERSION-NUMBER.
2080#
2081# This is called frequently, so minimize the number of macro invocations
2082# by avoiding dnl and other overhead on the common path.
2083# The use of a witness macro protecting the warning allows aclocal
2084# to silence any warnings when probing for what macros are required
2085# and must therefore be located, when using the Autoconf-without-aclocal-m4
2086# autom4te language.  For more background, see:
2087# https://lists.gnu.org/archive/html/automake-patches/2012-11/msg00035.html
2088m4_define([_m4_require_call],
2089[m4_pushdef([_m4_divert_grow], m4_decr(_m4_divert_grow))]dnl
2090[m4_pushdef([_m4_diverting([$1])])m4_pushdef([_m4_diverting], [$1])]dnl
2091[m4_divert_push(_m4_divert_grow, [-])]dnl
2092[m4_if([$2], [], [$1], [$2])
2093m4_provide_if([$1], [m4_set_remove([_m4_provide], [$1])],
2094  [m4_ifndef([m4_require_silent_probe],
2095    [m4_warn([syntax], [$1 is m4_require'd but not m4_defun'd])])])]dnl
2096[_m4_divert_raw($3)_m4_undivert(_m4_divert_grow)]dnl
2097[m4_divert_pop(_m4_divert_grow)_m4_popdef([_m4_divert_grow],
2098[_m4_diverting([$1])], [_m4_diverting])])
2099
2100
2101# _m4_require_check(NAME-TO-CHECK, OWNER, CALLER)
2102# -----------------------------------------------
2103# NAME-TO-CHECK has been identified as previously expanded in the
2104# diversion owned by OWNER.  If this is a problem, warn on behalf of
2105# CALLER and return _m4_require_call; otherwise return m4_ignore.
2106m4_define([_m4_require_check],
2107[m4_if(_m4_defn([_m4_diverting]), [$2], [m4_ignore],
2108       m4_ifdef([_m4_diverting([$2])], [-]), [-], [m4_warn([syntax],
2109   [$3: `$1' was expanded before it was required
2110https://www.gnu.org/software/autoconf/manual/autoconf.html#Expanded-Before-Required])_m4_require_call],
2111       [m4_ignore])])
2112
2113
2114# _m4_divert_grow
2115# ---------------
2116# The counter for _m4_require_call.
2117m4_define([_m4_divert_grow], _m4_divert([GROW]))
2118
2119
2120# m4_expand_once(TEXT, [WITNESS = TEXT])
2121# --------------------------------------
2122# If TEXT has never been expanded, expand it *here*.  Use WITNESS as
2123# as a memory that TEXT has already been expanded.
2124m4_define([m4_expand_once],
2125[m4_provide_if(m4_default_quoted([$2], [$1]),
2126	       [],
2127	       [m4_provide(m4_default_quoted([$2], [$1]))[]$1])])
2128
2129
2130# m4_provide(MACRO-NAME)
2131# ----------------------
2132m4_define([m4_provide],
2133[m4_ifdef([m4_provide($1)], [],
2134[m4_set_add([_m4_provide], [$1], [m4_define([m4_provide($1)],
2135  m4_ifdef([_m4_diverting], [_m4_defn([_m4_diverting])]))])])])
2136
2137
2138# m4_provide_if(MACRO-NAME, IF-PROVIDED, IF-NOT-PROVIDED)
2139# -------------------------------------------------------
2140# If MACRO-NAME is provided do IF-PROVIDED, else IF-NOT-PROVIDED.
2141# The purpose of this macro is to provide the user with a means to
2142# check macros which are provided without letting her know how the
2143# information is coded.
2144m4_define([m4_provide_if],
2145[m4_ifdef([m4_provide($1)],
2146	  [$2], [$3])])
2147
2148
2149## --------------------- ##
2150## 12. Text processing.  ##
2151## --------------------- ##
2152
2153
2154# m4_cr_letters
2155# m4_cr_LETTERS
2156# m4_cr_Letters
2157# -------------
2158m4_define([m4_cr_letters], [abcdefghijklmnopqrstuvwxyz])
2159m4_define([m4_cr_LETTERS], [ABCDEFGHIJKLMNOPQRSTUVWXYZ])
2160m4_define([m4_cr_Letters],
2161m4_defn([m4_cr_letters])dnl
2162m4_defn([m4_cr_LETTERS])dnl
2163)
2164
2165
2166# m4_cr_digits
2167# ------------
2168m4_define([m4_cr_digits], [0123456789])
2169
2170
2171# m4_cr_alnum
2172# -----------
2173m4_define([m4_cr_alnum],
2174m4_defn([m4_cr_Letters])dnl
2175m4_defn([m4_cr_digits])dnl
2176)
2177
2178
2179# m4_cr_symbols1
2180# m4_cr_symbols2
2181# --------------
2182m4_define([m4_cr_symbols1],
2183m4_defn([m4_cr_Letters])dnl
2184_)
2185
2186m4_define([m4_cr_symbols2],
2187m4_defn([m4_cr_symbols1])dnl
2188m4_defn([m4_cr_digits])dnl
2189)
2190
2191# m4_cr_all
2192# ---------
2193# The character range representing everything, with `-' as the last
2194# character, since it is special to m4_translit.  Use with care, because
2195# it contains characters special to M4 (fortunately, both ASCII and EBCDIC
2196# have [] in order, so m4_defn([m4_cr_all]) remains a valid string).  It
2197# also contains characters special to terminals, so it should never be
2198# displayed in an error message.  Also, attempts to map [ and ] to other
2199# characters via m4_translit must deal with the fact that m4_translit does
2200# not add quotes to the output.
2201#
2202# In EBCDIC, $ is immediately followed by *, which leads to problems
2203# if m4_cr_all is inlined into a macro definition; so swap them.
2204#
2205# It is mainly useful in generating inverted character range maps, for use
2206# in places where m4_translit is faster than an equivalent m4_bpatsubst;
2207# the regex `[^a-z]' is equivalent to:
2208#  m4_translit(m4_dquote(m4_defn([m4_cr_all])), [a-z])
2209m4_define([m4_cr_all],
2210m4_translit(m4_dquote(m4_format(m4_dquote(m4_for(
2211  ,1,255,,[[%c]]))m4_for([i],1,255,,[,i]))), [$*-], [*$])-)
2212
2213
2214# _m4_define_cr_not(CATEGORY)
2215# ---------------------------
2216# Define m4_cr_not_CATEGORY as the inverse of m4_cr_CATEGORY.
2217m4_define([_m4_define_cr_not],
2218[m4_define([m4_cr_not_$1],
2219	   m4_translit(m4_dquote(m4_defn([m4_cr_all])),
2220		       m4_defn([m4_cr_$1])))])
2221
2222
2223# m4_cr_not_letters
2224# m4_cr_not_LETTERS
2225# m4_cr_not_Letters
2226# m4_cr_not_digits
2227# m4_cr_not_alnum
2228# m4_cr_not_symbols1
2229# m4_cr_not_symbols2
2230# ------------------
2231# Inverse character sets
2232_m4_define_cr_not([letters])
2233_m4_define_cr_not([LETTERS])
2234_m4_define_cr_not([Letters])
2235_m4_define_cr_not([digits])
2236_m4_define_cr_not([alnum])
2237_m4_define_cr_not([symbols1])
2238_m4_define_cr_not([symbols2])
2239
2240
2241# m4_newline([STRING])
2242# --------------------
2243# Expands to a newline, possibly followed by STRING.  Exists mostly for
2244# formatting reasons.
2245m4_define([m4_newline], [
2246$1])
2247
2248
2249# m4_re_escape(STRING)
2250# --------------------
2251# Escape RE active characters in STRING.
2252m4_define([m4_re_escape],
2253[m4_bpatsubst([$1],
2254	      [[][*+.?\^$]], [\\\&])])
2255
2256
2257# m4_re_string
2258# ------------
2259# Regexp for `[a-zA-Z_0-9]*'
2260# m4_dquote provides literal [] for the character class.
2261m4_define([m4_re_string],
2262m4_dquote(m4_defn([m4_cr_symbols2]))dnl
2263[*]dnl
2264)
2265
2266
2267# m4_re_word
2268# ----------
2269# Regexp for `[a-zA-Z_][a-zA-Z_0-9]*'
2270m4_define([m4_re_word],
2271m4_dquote(m4_defn([m4_cr_symbols1]))dnl
2272m4_defn([m4_re_string])dnl
2273)
2274
2275
2276# m4_tolower(STRING)
2277# m4_toupper(STRING)
2278# ------------------
2279# These macros convert STRING to lowercase or uppercase.
2280#
2281# Rather than expand the m4_defn each time, we inline them up front.
2282m4_define([m4_tolower],
2283[m4_translit([[$1]], ]m4_dquote(m4_defn([m4_cr_LETTERS]))[,
2284		     ]m4_dquote(m4_defn([m4_cr_letters]))[)])
2285m4_define([m4_toupper],
2286[m4_translit([[$1]], ]m4_dquote(m4_defn([m4_cr_letters]))[,
2287		     ]m4_dquote(m4_defn([m4_cr_LETTERS]))[)])
2288
2289
2290# m4_split(STRING, [REGEXP])
2291# --------------------------
2292# Split STRING into an m4 list of quoted elements.  The elements are
2293# quoted with [ and ].  Beginning spaces and end spaces *are kept*.
2294# Use m4_strip to remove them.
2295#
2296# REGEXP specifies where to split.  Default is [\t ]+.
2297#
2298# If STRING is empty, the result is an empty list.
2299#
2300# Pay attention to the m4_changequotes.  When m4 reads the definition of
2301# m4_split, it still has quotes set to [ and ].  Luckily, these are matched
2302# in the macro body, so the definition is stored correctly.  Use the same
2303# alternate quotes as m4_noquote; it must be unlikely to appear in $1.
2304#
2305# Also, notice that $1 is quoted twice, since we want the result to
2306# be quoted.  Then you should understand that the argument of
2307# patsubst is -=<{(STRING)}>=- (i.e., with additional -=<{( and )}>=-).
2308#
2309# This macro is safe on active symbols, i.e.:
2310#   m4_define(active, ACTIVE)
2311#   m4_split([active active ])end
2312#   => [active], [active], []end
2313#
2314# Optimize on regex of ` ' (space), since m4_foreach_w already guarantees
2315# that the list contains single space separators, and a common case is
2316# splitting a single-element list.  This macro is called frequently,
2317# so avoid unnecessary dnl inside the definition.
2318m4_define([m4_split],
2319[m4_if([$1], [], [],
2320       [$2], [ ], [m4_if(m4_index([$1], [ ]), [-1], [[[$1]]],
2321			 [_$0([$1], [$2], [, ])])],
2322       [$2], [], [_$0([$1], [[	 ]+], [, ])],
2323       [_$0([$1], [$2], [, ])])])
2324
2325m4_define([_m4_split],
2326[m4_changequote([-=<{(],[)}>=-])]dnl
2327[[m4_bpatsubst(-=<{(-=<{($1)}>=-)}>=-, -=<{($2)}>=-,
2328	       -=<{(]$3[)}>=-)]m4_changequote([, ])])
2329
2330
2331# m4_chomp(STRING)
2332# m4_chomp_all(STRING)
2333# --------------------
2334# Return STRING quoted, but without a trailing newline.  m4_chomp
2335# removes at most one newline, while m4_chomp_all removes all
2336# consecutive trailing newlines.  Embedded newlines are not touched,
2337# and a trailing backslash-newline leaves just a trailing backslash.
2338#
2339# m4_bregexp is slower than m4_index, and we don't always want to
2340# remove all newlines; hence the two variants.  We massage characters
2341# to give a nicer pattern to match, particularly since m4_bregexp is
2342# line-oriented.  Both versions must guarantee a match, to avoid bugs
2343# with precision -1 in m4_format in older m4.
2344m4_define([m4_chomp],
2345[m4_format([[%.*s]], m4_index(m4_translit([[$1]], [
2346/.], [/  ])[./.], [/.]), [$1])])
2347
2348m4_define([m4_chomp_all],
2349[m4_format([[%.*s]], m4_bregexp(m4_translit([[$1]], [
2350/], [/ ]), [/*$]), [$1])])
2351
2352
2353# m4_flatten(STRING)
2354# ------------------
2355# If STRING contains end of lines, replace them with spaces.  If there
2356# are backslashed end of lines, remove them.  This macro is safe with
2357# active symbols.
2358#    m4_define(active, ACTIVE)
2359#    m4_flatten([active
2360#    act\
2361#    ive])end
2362#    => active activeend
2363#
2364# In m4, m4_bpatsubst is expensive, so first check for a newline.
2365m4_define([m4_flatten],
2366[m4_if(m4_index([$1], [
2367]), [-1], [[$1]],
2368       [m4_translit(m4_bpatsubst([[[$1]]], [\\
2369]), [
2370], [ ])])])
2371
2372
2373# m4_strip(STRING)
2374# ----------------
2375# Expands into STRING with tabs and spaces singled out into a single
2376# space, and removing leading and trailing spaces.
2377#
2378# This macro is robust to active symbols.
2379#    m4_define(active, ACTIVE)
2380#    m4_strip([  active <tab> <tab>active ])end
2381#    => active activeend
2382#
2383# First, notice that we guarantee trailing space.  Why?  Because regular
2384# expressions are greedy, and `.* ?' would always group the space into the
2385# .* portion.  The algorithm is simpler by avoiding `?' at the end.  The
2386# algorithm correctly strips everything if STRING is just ` '.
2387#
2388# Then notice the second pattern: it is in charge of removing the
2389# leading/trailing spaces.  Why not just `[^ ]'?  Because they are
2390# applied to over-quoted strings, i.e. more or less [STRING], due
2391# to the limitations of m4_bpatsubsts.  So the leading space in STRING
2392# is the *second* character; equally for the trailing space.
2393m4_define([m4_strip],
2394[m4_bpatsubsts([$1 ],
2395	       [[	 ]+], [ ],
2396	       [^. ?\(.*\) .$], [[[\1]]])])
2397
2398
2399# m4_normalize(STRING)
2400# --------------------
2401# Apply m4_flatten and m4_strip to STRING.
2402#
2403# The argument is quoted, so that the macro is robust to active symbols:
2404#
2405#    m4_define(active, ACTIVE)
2406#    m4_normalize([  act\
2407#    ive
2408#    active ])end
2409#    => active activeend
2410
2411m4_define([m4_normalize],
2412[m4_strip(m4_flatten([$1]))])
2413
2414
2415# m4_validate_w(STRING)
2416# ---------------------
2417# Expands into m4_normalize(m4_expand([STRING])), but if that is not
2418# the same as just m4_normalize([STRING]), issue a warning.
2419#
2420# This is used in several Autoconf macros that take a
2421# whitespace-separated list of symbols as an argument.  Ideally that
2422# list would not be expanded before use, but several packages used
2423# `dnl' to put comments inside those lists, so they must be expanded
2424# for compatibility's sake.
2425m4_define([m4_validate_w],
2426[_m4_validate_w(m4_normalize([$1]), m4_normalize(m4_expand([$1])))])
2427
2428m4_define([_m4_validate_w],
2429[m4_if([$1], [$2], [],
2430  [m4_warn([obsolete], [whitespace-separated list contains macros;
2431in a future version of Autoconf they will not be expanded]dnl
2432m4_if(m4_bregexp([$1], [\bdn[l]\b]), -1, [], [
2433note: `dn@&t@l' is a macro]))])dnl
2434[$2]])
2435
2436
2437# m4_join(SEP, ARG1, ARG2...)
2438# ---------------------------
2439# Produce ARG1SEPARG2...SEPARGn.  Avoid back-to-back SEP when a given ARG
2440# is the empty string.  No expansion is performed on SEP or ARGs.
2441#
2442# Since the number of arguments to join can be arbitrarily long, we
2443# want to avoid having more than one $@ in the macro definition;
2444# otherwise, the expansion would require twice the memory of the already
2445# long list.  Hence, m4_join merely looks for the first non-empty element,
2446# and outputs just that element; while _m4_join looks for all non-empty
2447# elements, and outputs them following a separator.  The final trick to
2448# note is that we decide between recursing with $0 or _$0 based on the
2449# nested m4_if ending with `_'.
2450#
2451# Please keep foreach.m4 in sync with any adjustments made here.
2452m4_define([m4_join],
2453[m4_if([$#], [1], [],
2454       [$#], [2], [[$2]],
2455       [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift2($@))])])
2456m4_define([_m4_join],
2457[m4_if([$#$2], [2], [],
2458       [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift2($@))])])
2459
2460# m4_joinall(SEP, ARG1, ARG2...)
2461# ------------------------------
2462# Produce ARG1SEPARG2...SEPARGn.  An empty ARG results in back-to-back SEP.
2463# No expansion is performed on SEP or ARGs.
2464#
2465# Please keep foreach.m4 in sync with any adjustments made here.
2466m4_define([m4_joinall], [[$2]_$0([$1], m4_shift($@))])
2467m4_define([_m4_joinall],
2468[m4_if([$#], [2], [], [[$1$3]$0([$1], m4_shift2($@))])])
2469
2470# m4_combine([SEPARATOR], PREFIX-LIST, [INFIX], SUFFIX...)
2471# --------------------------------------------------------
2472# Produce the pairwise combination of every element in the quoted,
2473# comma-separated PREFIX-LIST with every element from the SUFFIX arguments.
2474# Each pair is joined with INFIX, and pairs are separated by SEPARATOR.
2475# No expansion occurs on SEPARATOR, INFIX, or elements of either list.
2476#
2477# For example:
2478#   m4_combine([, ], [[a], [b], [c]], [-], [1], [2], [3])
2479#   => a-1, a-2, a-3, b-1, b-2, b-3, c-1, c-2, c-3
2480#
2481# This definition is a bit hairy; the thing to realize is that we want
2482# to construct m4_map_args_sep([[prefix$3]], [], [[$1]], m4_shift3($@))
2483# as the inner loop, using each prefix generated by the outer loop,
2484# and without recalculating m4_shift3 every outer iteration.
2485m4_define([m4_combine],
2486[m4_if([$2], [], [], m4_eval([$# > 3]), [1],
2487[m4_map_args_sep([m4_map_args_sep(m4_dquote(], [)[[$3]], [], [[$1]],]]]dnl
2488[m4_dquote(m4_dquote(m4_shift3($@)))[[)], [[$1]], $2)])])
2489
2490
2491# m4_append(MACRO-NAME, STRING, [SEPARATOR])
2492# ------------------------------------------
2493# Redefine MACRO-NAME to hold its former content plus `SEPARATOR`'STRING'
2494# at the end.  It is valid to use this macro with MACRO-NAME undefined,
2495# in which case no SEPARATOR is added.  Be aware that the criterion is
2496# `not being defined', and not `not being empty'.
2497#
2498# Note that neither STRING nor SEPARATOR are expanded here; rather, when
2499# you expand MACRO-NAME, they will be expanded at that point in time.
2500#
2501# This macro is robust to active symbols.  It can be used to grow
2502# strings.
2503#
2504#    | m4_define(active, ACTIVE)dnl
2505#    | m4_append([sentence], [This is an])dnl
2506#    | m4_append([sentence], [ active ])dnl
2507#    | m4_append([sentence], [symbol.])dnl
2508#    | sentence
2509#    | m4_undefine([active])dnl
2510#    | sentence
2511#    => This is an ACTIVE symbol.
2512#    => This is an active symbol.
2513#
2514# It can be used to define hooks.
2515#
2516#    | m4_define(active, ACTIVE)dnl
2517#    | m4_append([hooks], [m4_define([act1], [act2])])dnl
2518#    | m4_append([hooks], [m4_define([act2], [active])])dnl
2519#    | m4_undefine([active])dnl
2520#    | act1
2521#    | hooks
2522#    | act1
2523#    => act1
2524#    =>
2525#    => active
2526#
2527# It can also be used to create lists, although this particular usage was
2528# broken prior to autoconf 2.62.
2529#    | m4_append([list], [one], [, ])dnl
2530#    | m4_append([list], [two], [, ])dnl
2531#    | m4_append([list], [three], [, ])dnl
2532#    | list
2533#    | m4_dquote(list)
2534#    => one, two, three
2535#    => [one],[two],[three]
2536#
2537# Note that m4_append can benefit from amortized O(n) m4 behavior, if
2538# the underlying m4 implementation is smart enough to avoid copying existing
2539# contents when enlarging a macro's definition into any pre-allocated storage
2540# (m4 1.4.x unfortunately does not implement this optimization).  We do
2541# not implement m4_prepend, since it is inherently O(n^2) (pre-allocated
2542# storage only occurs at the end of a macro, so the existing contents must
2543# always be moved).
2544#
2545# Use _m4_defn for speed.
2546m4_define([m4_append],
2547[m4_define([$1], m4_ifdef([$1], [_m4_defn([$1])[$3]])[$2])])
2548
2549
2550# m4_append_uniq(MACRO-NAME, STRING, [SEPARATOR], [IF-UNIQ], [IF-DUP])
2551# --------------------------------------------------------------------
2552# Like `m4_append', but append only if not yet present.  Additionally,
2553# expand IF-UNIQ if STRING was appended, or IF-DUP if STRING was already
2554# present.  Also, warn if SEPARATOR is not empty and occurs within STRING,
2555# as the algorithm no longer guarantees uniqueness.
2556#
2557# Note that while m4_append can be O(n) (depending on the quality of the
2558# underlying M4 implementation), m4_append_uniq is inherently O(n^2)
2559# because each append operation searches the entire string.
2560m4_define([m4_append_uniq],
2561[m4_ifval([$3], [m4_if(m4_index([$2], [$3]), [-1], [],
2562		       [m4_warn([syntax],
2563				[$0: `$2' contains `$3'])])])_$0($@)])
2564m4_define([_m4_append_uniq],
2565[m4_ifdef([$1],
2566	  [m4_if(m4_index([$3]_m4_defn([$1])[$3], [$3$2$3]), [-1],
2567		 [m4_append([$1], [$2], [$3])$4], [$5])],
2568	  [m4_define([$1], [$2])$4])])
2569
2570# m4_append_uniq_w(MACRO-NAME, STRINGS)
2571# -------------------------------------
2572# For each of the words in the whitespace separated list STRINGS, append
2573# only the unique strings to the definition of MACRO-NAME.
2574#
2575# Use _m4_defn for speed.
2576m4_define([m4_append_uniq_w],
2577[m4_map_args_w([$2], [_m4_append_uniq([$1],], [, [ ])])])
2578
2579
2580# m4_escape(STRING)
2581# -----------------
2582# Output quoted STRING, but with embedded #, $, [ and ] turned into
2583# quadrigraphs.
2584#
2585# It is faster to check if STRING is already good using m4_translit
2586# than to blindly perform four m4_bpatsubst.
2587#
2588# Because the translit is stripping quotes, it must also neutralize
2589# anything that might be in a macro name, as well as comments, commas,
2590# and parentheses.  All the problem characters are unified so that a
2591# single m4_index can scan the result.
2592#
2593# Rather than expand m4_defn every time m4_escape is expanded, we
2594# inline its expansion up front.
2595m4_define([m4_escape],
2596[m4_if(m4_index(m4_translit([$1],
2597   [[]#,()]]m4_dquote(m4_defn([m4_cr_symbols2]))[, [$$$]), [$]),
2598  [-1], [m4_echo], [_$0])([$1])])
2599
2600m4_define([_m4_escape],
2601[m4_changequote([-=<{(],[)}>=-])]dnl
2602[m4_bpatsubst(m4_bpatsubst(m4_bpatsubst(m4_bpatsubst(
2603	  -=<{(-=<{(-=<{(-=<{(-=<{($1)}>=-)}>=-)}>=-)}>=-)}>=-,
2604	-=<{(#)}>=-, -=<{(@%:@)}>=-),
2605      -=<{(\[)}>=-, -=<{(@<:@)}>=-),
2606    -=<{(\])}>=-, -=<{(@:>@)}>=-),
2607  -=<{(\$)}>=-, -=<{(@S|@)}>=-)m4_changequote([,])])
2608
2609
2610# m4_text_wrap(STRING, [PREFIX], [FIRST-PREFIX], [WIDTH])
2611# -------------------------------------------------------
2612# Expands into STRING wrapped to hold in WIDTH columns (default = 79).
2613# If PREFIX is given, each line is prefixed with it.  If FIRST-PREFIX is
2614# specified, then the first line is prefixed with it.  As a special case,
2615# if the length of FIRST-PREFIX is greater than that of PREFIX, then
2616# FIRST-PREFIX will be left alone on the first line.
2617#
2618# No expansion occurs on the contents STRING, PREFIX, or FIRST-PREFIX,
2619# although quadrigraphs are correctly recognized.  More precisely,
2620# you may redefine m4_qlen to recognize whatever escape sequences that
2621# you will post-process.
2622#
2623# Typical outputs are:
2624#
2625# m4_text_wrap([Short string */], [   ], [/* ], 20)
2626#  => /* Short string */
2627#
2628# m4_text_wrap([Much longer string */], [   ], [/* ], 20)
2629#  => /* Much longer
2630#  =>    string */
2631#
2632# m4_text_wrap([Short doc.], [          ], [  --short ], 30)
2633#  =>   --short Short doc.
2634#
2635# m4_text_wrap([Short doc.], [          ], [  --too-wide ], 30)
2636#  =>   --too-wide
2637#  =>           Short doc.
2638#
2639# m4_text_wrap([Super long documentation.], [          ], [  --too-wide ], 30)
2640#  =>   --too-wide
2641#  =>      Super long
2642#  =>      documentation.
2643#
2644# FIXME: there is no checking of a longer PREFIX than WIDTH, but do
2645# we really want to bother with people trying each single corner
2646# of a software?
2647#
2648# This macro does not leave a trailing space behind the last word of a line,
2649# which complicates it a bit.  The algorithm is otherwise stupid and simple:
2650# all the words are preceded by m4_Separator which is defined to empty for
2651# the first word, and then ` ' (single space) for all the others.
2652#
2653# The algorithm uses a helper that uses $2 through $4 directly, rather than
2654# using local variables, to avoid m4_defn overhead, or expansion swallowing
2655# any $.  It also bypasses m4_popdef overhead with _m4_popdef since no user
2656# macro expansion occurs in the meantime.  Also, the definition is written
2657# with m4_do, to avoid time wasted on dnl during expansion (since this is
2658# already a time-consuming macro).
2659m4_define([m4_text_wrap],
2660[_$0(m4_escape([$1]), [$2], m4_default_quoted([$3], [$2]),
2661     m4_default_quoted([$4], [79]))])
2662
2663m4_define([_m4_text_wrap],
2664m4_do(dnl set up local variables, to avoid repeated calculations
2665[[m4_pushdef([m4_Indent], m4_qlen([$2]))]],
2666[[m4_pushdef([m4_Cursor], m4_qlen([$3]))]],
2667[[m4_pushdef([m4_Separator], [m4_define([m4_Separator], [ ])])]],
2668dnl expand the first prefix, then check its length vs. regular prefix
2669dnl same length: nothing special
2670dnl prefix1 longer: output on line by itself, and reset cursor
2671dnl prefix1 shorter: pad to length of prefix, and reset cursor
2672[[[$3]m4_cond([m4_Cursor], m4_Indent, [],
2673	      [m4_eval(m4_Cursor > m4_Indent)], [1], [
2674[$2]m4_define([m4_Cursor], m4_Indent)],
2675	      [m4_format([%*s], m4_max([0],
2676  m4_eval(m4_Indent - m4_Cursor)), [])m4_define([m4_Cursor], m4_Indent)])]],
2677dnl now, for each word, compute the cursor after the word is output, then
2678dnl check if the cursor would exceed the wrap column
2679dnl if so, reset cursor, and insert newline and prefix
2680dnl if not, insert the separator (usually a space)
2681dnl either way, insert the word
2682[[m4_map_args_w([$1], [$0_word(], [, [$2], [$4])])]],
2683dnl finally, clean up the local variables
2684[[_m4_popdef([m4_Separator], [m4_Cursor], [m4_Indent])]]))
2685
2686m4_define([_m4_text_wrap_word],
2687[m4_define([m4_Cursor], m4_eval(m4_Cursor + m4_qlen([$1]) + 1))]dnl
2688[m4_if(m4_eval(m4_Cursor > ([$3])),
2689      [1], [m4_define([m4_Cursor], m4_eval(m4_Indent + m4_qlen([$1]) + 1))
2690[$2]],
2691      [m4_Separator[]])[$1]])
2692
2693# m4_text_box(MESSAGE, [FRAME-CHARACTER = `-'])
2694# ---------------------------------------------
2695# Turn MESSAGE into:
2696#  ## ------- ##
2697#  ## MESSAGE ##
2698#  ## ------- ##
2699# using FRAME-CHARACTER in the border.
2700#
2701# Quadrigraphs are correctly recognized.  More precisely, you may
2702# redefine m4_qlen to recognize whatever escape sequences that you
2703# will post-process.
2704m4_define([m4_text_box],
2705[m4_pushdef([m4_Border],
2706	    m4_translit(m4_format([[[%*s]]], m4_decr(m4_qlen(_m4_expand([$1
2707]))), []), [ ], m4_default_quoted([$2], [-])))]dnl
2708[[##] _m4_defn([m4_Border]) [##]
2709[##] $1 [##]
2710[##] _m4_defn([m4_Border]) [##]_m4_popdef([m4_Border])])
2711
2712
2713# m4_qlen(STRING)
2714# ---------------
2715# Expands to the length of STRING after autom4te converts all quadrigraphs.
2716#
2717# If you use some other means of post-processing m4 output rather than
2718# autom4te, then you may redefine this macro to recognize whatever
2719# escape sequences your post-processor will handle.  For that matter,
2720# m4_define([m4_qlen], m4_defn([m4_len])) is sufficient if you don't
2721# do any post-processing.
2722#
2723# Avoid bpatsubsts for the common case of no quadrigraphs.  Cache
2724# results, as configure scripts tend to ask about lengths of common
2725# strings like `/*' and `*/' rather frequently.  Minimize the number
2726# of times that $1 occurs in m4_qlen, so there is less text to parse
2727# on a cache hit.
2728m4_define([m4_qlen],
2729[m4_ifdef([$0-$1], [_m4_defn([$0-]], [_$0(])[$1])])
2730m4_define([_m4_qlen],
2731[m4_define([m4_qlen-$1],
2732m4_if(m4_index([$1], [@]), [-1], [m4_len([$1])],
2733      [m4_len(m4_bpatsubst([[$1]],
2734			   [@\(\(<:\|:>\|S|\|%:\|\{:\|:\}\)\(@\)\|&t@\)],
2735			   [\3]))]))_m4_defn([m4_qlen-$1])])
2736
2737# m4_copyright_condense(TEXT)
2738# ---------------------------
2739# Condense the copyright notice in TEXT to only display the final
2740# year, wrapping the results to fit in 80 columns.
2741m4_define([m4_copyright_condense],
2742[m4_text_wrap(m4_bpatsubst(m4_flatten([[$1]]),
2743[(C)[-	 ,0-9]*\([1-9][0-9][0-9][0-9]\)], [(C) \1]))])
2744
2745## ----------------------- ##
2746## 13. Number processing.  ##
2747## ----------------------- ##
2748
2749# m4_cmp(A, B)
2750# ------------
2751# Compare two integer expressions.
2752# A < B -> -1
2753# A = B ->  0
2754# A > B ->  1
2755m4_define([m4_cmp],
2756[m4_eval((([$1]) > ([$2])) - (([$1]) < ([$2])))])
2757
2758
2759# m4_list_cmp(A, B)
2760# -----------------
2761#
2762# Compare the two lists of integer expressions A and B.  For instance:
2763#   m4_list_cmp([1, 0],     [1])    ->  0
2764#   m4_list_cmp([1, 0],     [1, 0]) ->  0
2765#   m4_list_cmp([1, 2],     [1, 0]) ->  1
2766#   m4_list_cmp([1, 2, 3],  [1, 2]) ->  1
2767#   m4_list_cmp([1, 2, -3], [1, 2]) -> -1
2768#   m4_list_cmp([1, 0],     [1, 2]) -> -1
2769#   m4_list_cmp([1],        [1, 2]) -> -1
2770#   m4_define([xa], [oops])dnl
2771#   m4_list_cmp([[0xa]],    [5+5])  -> 0
2772#
2773# Rather than face the overhead of m4_case, we use a helper function whose
2774# expansion includes the name of the macro to invoke on the tail, either
2775# m4_ignore or m4_unquote.  This is particularly useful when comparing
2776# long lists, since less text is being expanded for deciding when to end
2777# recursion.  The recursion is between a pair of macros that alternate
2778# which list is trimmed by one element; this is more efficient than
2779# calling m4_cdr on both lists from a single macro.  Guarantee exactly
2780# one expansion of both lists' side effects.
2781#
2782# Please keep foreach.m4 in sync with any adjustments made here.
2783m4_define([m4_list_cmp],
2784[_$0_raw(m4_dquote($1), m4_dquote($2))])
2785
2786m4_define([_m4_list_cmp_raw],
2787[m4_if([$1], [$2], [0], [_m4_list_cmp_1([$1], $2)])])
2788
2789m4_define([_m4_list_cmp],
2790[m4_if([$1], [], [0m4_ignore], [$2], [0], [m4_unquote], [$2m4_ignore])])
2791
2792m4_define([_m4_list_cmp_1],
2793[_m4_list_cmp_2([$2], [m4_shift2($@)], $1)])
2794
2795m4_define([_m4_list_cmp_2],
2796[_m4_list_cmp([$1$3], m4_cmp([$3+0], [$1+0]))(
2797  [_m4_list_cmp_1(m4_dquote(m4_shift3($@)), $2)])])
2798
2799# m4_max(EXPR, ...)
2800# m4_min(EXPR, ...)
2801# -----------------
2802# Return the decimal value of the maximum (or minimum) in a series of
2803# integer expressions.
2804#
2805# M4 1.4.x doesn't provide ?:.  Hence this huge m4_eval.  Avoid m4_eval
2806# if both arguments are identical, but be aware of m4_max(0xa, 10) (hence
2807# the use of <=, not just <, in the second multiply).
2808#
2809# Please keep foreach.m4 in sync with any adjustments made here.
2810m4_define([m4_max],
2811[m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
2812       [$#], [1], [m4_eval([$1])],
2813       [$#$1], [2$2], [m4_eval([$1])],
2814       [$#], [2], [_$0($@)],
2815       [_m4_minmax([_$0], $@)])])
2816
2817m4_define([_m4_max],
2818[m4_eval((([$1]) > ([$2])) * ([$1]) + (([$1]) <= ([$2])) * ([$2]))])
2819
2820m4_define([m4_min],
2821[m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
2822       [$#], [1], [m4_eval([$1])],
2823       [$#$1], [2$2], [m4_eval([$1])],
2824       [$#], [2], [_$0($@)],
2825       [_m4_minmax([_$0], $@)])])
2826
2827m4_define([_m4_min],
2828[m4_eval((([$1]) < ([$2])) * ([$1]) + (([$1]) >= ([$2])) * ([$2]))])
2829
2830# _m4_minmax(METHOD, ARG1, ARG2...)
2831# ---------------------------------
2832# Common recursion code for m4_max and m4_min.  METHOD must be _m4_max
2833# or _m4_min, and there must be at least two arguments to combine.
2834#
2835# Please keep foreach.m4 in sync with any adjustments made here.
2836m4_define([_m4_minmax],
2837[m4_if([$#], [3], [$1([$2], [$3])],
2838       [$0([$1], $1([$2], [$3]), m4_shift3($@))])])
2839
2840
2841# m4_sign(A)
2842# ----------
2843# The sign of the integer expression A.
2844m4_define([m4_sign],
2845[m4_eval((([$1]) > 0) - (([$1]) < 0))])
2846
2847
2848
2849## ------------------------ ##
2850## 14. Version processing.  ##
2851## ------------------------ ##
2852
2853
2854# m4_version_unletter(VERSION)
2855# ----------------------------
2856# Normalize beta version numbers with letters to numeric expressions, which
2857# can then be handed to m4_eval for the purpose of comparison.
2858#
2859#   Nl -> (N+1).-1.(l#)
2860#
2861# for example:
2862#   [2.14a] -> [0,2,14+1,-1,[0r36:a]] -> 2.15.-1.10
2863#   [2.14b] -> [0,2,15+1,-1,[0r36:b]] -> 2.15.-1.11
2864#   [2.61aa.b] -> [0,2.61,1,-1,[0r36:aa],+1,-1,[0r36:b]] -> 2.62.-1.370.1.-1.11
2865#   [08] -> [0,[0r10:0]8] -> 8
2866#
2867# This macro expects reasonable version numbers, but can handle double
2868# letters and does not expand any macros.  Original version strings can
2869# use both `.' and `-' separators.
2870#
2871# Inline constant expansions, to avoid m4_defn overhead.
2872# _m4_version_unletter is the real workhorse used by m4_version_compare,
2873# but since [0r36:a] and commas are less readable than 10 and dots, we
2874# provide a wrapper for human use.
2875m4_define([m4_version_unletter],
2876[m4_substr(m4_map_args([.m4_eval], m4_unquote(_$0([$1]))), [3])])
2877m4_define([_m4_version_unletter],
2878[m4_bpatsubst(m4_bpatsubst(m4_translit([[[[0,$1]]]], [.-], [,,]),]dnl
2879m4_dquote(m4_dquote(m4_defn([m4_cr_Letters])))[[+],
2880	      [+1,-1,[0r36:\&]]), [,0], [,[0r10:0]])])
2881
2882
2883# m4_version_compare(VERSION-1, VERSION-2)
2884# ----------------------------------------
2885# Compare the two version numbers and expand into
2886#  -1 if VERSION-1 < VERSION-2
2887#   0 if           =
2888#   1 if           >
2889#
2890# Since _m4_version_unletter does not output side effects, we can
2891# safely bypass the overhead of m4_version_cmp.
2892m4_define([m4_version_compare],
2893[_m4_list_cmp_raw(_m4_version_unletter([$1]), _m4_version_unletter([$2]))])
2894
2895
2896# m4_PACKAGE_NAME
2897# m4_PACKAGE_TARNAME
2898# m4_PACKAGE_VERSION
2899# m4_PACKAGE_STRING
2900# m4_PACKAGE_BUGREPORT
2901# --------------------
2902# If m4sugar/version.m4 is present, then define version strings.  This
2903# file is optional, provided by Autoconf but absent in Bison.
2904m4_sinclude([m4sugar/version.m4])
2905
2906
2907# m4_version_prereq(VERSION, [IF-OK], [IF-NOT = FAIL])
2908# ----------------------------------------------------
2909# Check this Autoconf version against VERSION.
2910m4_define([m4_version_prereq],
2911m4_ifdef([m4_PACKAGE_VERSION],
2912[[m4_if(m4_version_compare(]m4_dquote(m4_defn([m4_PACKAGE_VERSION]))[, [$1]),
2913	[-1],
2914	[m4_default([$3],
2915		    [m4_fatal([Autoconf version $1 or higher is required],
2916			      [63])])],
2917	[$2])]],
2918[[m4_fatal([m4sugar/version.m4 not found])]]))
2919
2920
2921## ------------------ ##
2922## 15. Set handling.  ##
2923## ------------------ ##
2924
2925# Autoconf likes to create arbitrarily large sets; for example, as of
2926# this writing, the configure.ac for coreutils tracks a set of more
2927# than 400 AC_SUBST.  How do we track all of these set members,
2928# without introducing duplicates?  We could use m4_append_uniq, with
2929# the set NAME residing in the contents of the macro NAME.
2930# Unfortunately, m4_append_uniq is quadratic for set creation, because
2931# it costs O(n) to search the string for each of O(n) insertions; not
2932# to mention that with m4 1.4.x, even using m4_append is slow, costing
2933# O(n) rather than O(1) per insertion.  Other set operations, not used
2934# by Autoconf but still possible by manipulation of the definition
2935# tracked in macro NAME, include O(n) deletion of one element and O(n)
2936# computation of set size.  Because the set is exposed to the user via
2937# the definition of a single macro, we cannot cache any data about the
2938# set without risking the cache being invalidated by the user
2939# redefining NAME.
2940#
2941# Can we do better?  Yes, because m4 gives us an O(1) search function
2942# for free: ifdef.  Additionally, even m4 1.4.x gives us an O(1)
2943# insert operation for free: pushdef.  But to use these, we must
2944# represent the set via a group of macros; to keep the set consistent,
2945# we must hide the set so that the user can only manipulate it through
2946# accessor macros.  The contents of the set are maintained through two
2947# access points; _m4_set([name]) is a pushdef stack of values in the
2948# set, useful for O(n) traversal of the set contents; while the
2949# existence of _m4_set([name],value) with no particular value is
2950# useful for O(1) querying of set membership.  And since the user
2951# cannot externally manipulate the set, we are free to add additional
2952# caching macros for other performance improvements.  Deletion can be
2953# O(1) per element rather than O(n), by reworking the definition of
2954# _m4_set([name],value) to be 0 or 1 based on current membership, and
2955# adding _m4_set_cleanup(name) to defer the O(n) cleanup of
2956# _m4_set([name]) until we have another reason to do an O(n)
2957# traversal.  The existence of _m4_set_cleanup(name) can then be used
2958# elsewhere to determine if we must dereference _m4_set([name],value),
2959# or assume that definition implies set membership.  Finally, size can
2960# be tracked in an O(1) fashion with _m4_set_size(name).
2961#
2962# The quoting in _m4_set([name],value) is chosen so that there is no
2963# ambiguity with a set whose name contains a comma, and so that we can
2964# supply the value via _m4_defn([_m4_set([name])]) without needing any
2965# quote manipulation.
2966
2967# m4_set_add(SET, VALUE, [IF-UNIQ], [IF-DUP])
2968# -------------------------------------------
2969# Add VALUE as an element of SET.  Expand IF-UNIQ on the first
2970# addition, and IF-DUP if it is already in the set.  Addition of one
2971# element is O(1), such that overall set creation is O(n).
2972#
2973# We do not want to add a duplicate for a previously deleted but
2974# unpruned element, but it is just as easy to check existence directly
2975# as it is to query _m4_set_cleanup($1).
2976m4_define([m4_set_add],
2977[m4_ifdef([_m4_set([$1],$2)],
2978	  [m4_if(m4_indir([_m4_set([$1],$2)]), [0],
2979		 [m4_define([_m4_set([$1],$2)],
2980			    [1])_m4_set_size([$1], [m4_incr])$3], [$4])],
2981	  [m4_define([_m4_set([$1],$2)],
2982		     [1])m4_pushdef([_m4_set([$1])],
2983				    [$2])_m4_set_size([$1], [m4_incr])$3])])
2984
2985# m4_set_add_all(SET, VALUE...)
2986# -----------------------------
2987# Add each VALUE into SET.  This is O(n) in the number of VALUEs, and
2988# can be faster than calling m4_set_add for each VALUE.
2989#
2990# Implement two recursion helpers; the check variant is slower but
2991# handles the case where an element has previously been removed but
2992# not pruned.  The recursion helpers ignore their second argument, so
2993# that we can use the faster m4_shift2 and 2 arguments, rather than
2994# _m4_shift2 and one argument, as the signal to end recursion.
2995#
2996# Please keep foreach.m4 in sync with any adjustments made here.
2997m4_define([m4_set_add_all],
2998[m4_define([_m4_set_size($1)], m4_eval(m4_set_size([$1])
2999  + m4_len(m4_ifdef([_m4_set_cleanup($1)], [_$0_check], [_$0])([$1], $@))))])
3000
3001m4_define([_m4_set_add_all],
3002[m4_if([$#], [2], [],
3003       [m4_ifdef([_m4_set([$1],$3)], [],
3004		 [m4_define([_m4_set([$1],$3)], [1])m4_pushdef([_m4_set([$1])],
3005	   [$3])-])$0([$1], m4_shift2($@))])])
3006
3007m4_define([_m4_set_add_all_check],
3008[m4_if([$#], [2], [],
3009       [m4_set_add([$1], [$3])$0([$1], m4_shift2($@))])])
3010
3011# m4_set_contains(SET, VALUE, [IF-PRESENT], [IF-ABSENT])
3012# ------------------------------------------------------
3013# Expand IF-PRESENT if SET contains VALUE, otherwise expand IF-ABSENT.
3014# This is always O(1).
3015m4_define([m4_set_contains],
3016[m4_ifdef([_m4_set_cleanup($1)],
3017	  [m4_if(m4_ifdef([_m4_set([$1],$2)],
3018		    [m4_indir([_m4_set([$1],$2)])], [0]), [1], [$3], [$4])],
3019	  [m4_ifdef([_m4_set([$1],$2)], [$3], [$4])])])
3020
3021# m4_set_contents(SET, [SEP])
3022# ---------------------------
3023# Expand to a single string containing all the elements in SET,
3024# separated by SEP, without modifying SET.  No provision is made for
3025# disambiguating set elements that contain non-empty SEP as a
3026# sub-string, or for recognizing a set that contains only the empty
3027# string.  Order of the output is not guaranteed.  If any elements
3028# have been previously removed from the set, this action will prune
3029# the unused memory.  This is O(n) in the size of the set before
3030# pruning.
3031#
3032# Use _m4_popdef for speed.  The existence of _m4_set_cleanup($1)
3033# determines which version of _1 helper we use.
3034m4_define([m4_set_contents],
3035[m4_set_map_sep([$1], [], [], [[$2]])])
3036
3037# _m4_set_contents_1(SET)
3038# _m4_set_contents_1c(SET)
3039# _m4_set_contents_2(SET, [PRE], [POST], [SEP])
3040# ---------------------------------------------
3041# Expand to a list of quoted elements currently in the set, each
3042# surrounded by PRE and POST, and moving SEP in front of PRE on
3043# recursion.  To avoid nesting limit restrictions, the algorithm must
3044# be broken into two parts; _1 destructively copies the stack in
3045# reverse into _m4_set_($1), producing no output; then _2
3046# destructively copies _m4_set_($1) back into the stack in reverse.
3047# If no elements were deleted, then this visits the set in the order
3048# that elements were inserted.  Behavior is undefined if PRE/POST/SEP
3049# tries to recursively list or modify SET in any way other than
3050# calling m4_set_remove on the current element.  Use _1 if all entries
3051# in the stack are guaranteed to be in the set, and _1c to prune
3052# removed entries.  Uses _m4_defn and _m4_popdef for speed.
3053m4_define([_m4_set_contents_1],
3054[_m4_stack_reverse([_m4_set([$1])], [_m4_set_($1)])])
3055
3056m4_define([_m4_set_contents_1c],
3057[m4_ifdef([_m4_set([$1])],
3058	  [m4_set_contains([$1], _m4_defn([_m4_set([$1])]),
3059		   [m4_pushdef([_m4_set_($1)], _m4_defn([_m4_set([$1])]))],
3060		   [_m4_popdef([_m4_set([$1],]_m4_defn(
3061      [_m4_set([$1])])[)])])_m4_popdef([_m4_set([$1])])$0([$1])],
3062	  [_m4_popdef([_m4_set_cleanup($1)])])])
3063
3064m4_define([_m4_set_contents_2],
3065[_m4_stack_reverse([_m4_set_($1)], [_m4_set([$1])],
3066  [$2[]_m4_defn([_m4_set_($1)])$3], [$4[]])])
3067
3068# m4_set_delete(SET)
3069# ------------------
3070# Delete all elements in SET, and reclaim any memory occupied by the
3071# set.  This is O(n) in the set size.
3072#
3073# Use _m4_defn and _m4_popdef for speed.
3074m4_define([m4_set_delete],
3075[m4_ifdef([_m4_set([$1])],
3076	  [_m4_popdef([_m4_set([$1],]_m4_defn([_m4_set([$1])])[)],
3077		      [_m4_set([$1])])$0([$1])],
3078	  [m4_ifdef([_m4_set_cleanup($1)],
3079		    [_m4_popdef([_m4_set_cleanup($1)])])m4_ifdef(
3080		    [_m4_set_size($1)],
3081		    [_m4_popdef([_m4_set_size($1)])])])])
3082
3083# m4_set_difference(SET1, SET2)
3084# -----------------------------
3085# Produce a LIST of quoted elements that occur in SET1 but not SET2.
3086# Output a comma prior to any elements, to distinguish the empty
3087# string from no elements.  This can be directly used as a series of
3088# arguments, such as for m4_join, or wrapped inside quotes for use in
3089# m4_foreach.  Order of the output is not guaranteed.
3090#
3091# Short-circuit the idempotence relation.
3092m4_define([m4_set_difference],
3093[m4_if([$1], [$2], [], [m4_set_map_sep([$1], [_$0([$2],], [)])])])
3094
3095m4_define([_m4_set_difference],
3096[m4_set_contains([$1], [$2], [], [,[$2]])])
3097
3098# m4_set_dump(SET, [SEP])
3099# -----------------------
3100# Expand to a single string containing all the elements in SET,
3101# separated by SEP, then delete SET.  In general, if you only need to
3102# list the contents once, this is faster than m4_set_contents.  No
3103# provision is made for disambiguating set elements that contain
3104# non-empty SEP as a sub-string.  Order of the output is not
3105# guaranteed.  This is O(n) in the size of the set before pruning.
3106#
3107# Use _m4_popdef for speed.  Use existence of _m4_set_cleanup($1) to
3108# decide if more expensive recursion is needed.
3109m4_define([m4_set_dump],
3110[m4_ifdef([_m4_set_size($1)],
3111	  [_m4_popdef([_m4_set_size($1)])])m4_ifdef([_m4_set_cleanup($1)],
3112    [_$0_check], [_$0])([$1], [], [$2])])
3113
3114# _m4_set_dump(SET, [SEP], [PREP])
3115# _m4_set_dump_check(SET, [SEP], [PREP])
3116# --------------------------------------
3117# Print SEP and the current element, then delete the element and
3118# recurse with empty SEP changed to PREP.  The check variant checks
3119# whether the element has been previously removed.  Use _m4_defn and
3120# _m4_popdef for speed.
3121m4_define([_m4_set_dump],
3122[m4_ifdef([_m4_set([$1])],
3123	  [[$2]_m4_defn([_m4_set([$1])])_m4_popdef([_m4_set([$1],]_m4_defn(
3124		[_m4_set([$1])])[)], [_m4_set([$1])])$0([$1], [$2$3])])])
3125
3126m4_define([_m4_set_dump_check],
3127[m4_ifdef([_m4_set([$1])],
3128	  [m4_set_contains([$1], _m4_defn([_m4_set([$1])]),
3129			   [[$2]_m4_defn([_m4_set([$1])])])_m4_popdef(
3130    [_m4_set([$1],]_m4_defn([_m4_set([$1])])[)],
3131    [_m4_set([$1])])$0([$1], [$2$3])],
3132	  [_m4_popdef([_m4_set_cleanup($1)])])])
3133
3134# m4_set_empty(SET, [IF-EMPTY], [IF-ELEMENTS])
3135# --------------------------------------------
3136# Expand IF-EMPTY if SET has no elements, otherwise IF-ELEMENTS.
3137m4_define([m4_set_empty],
3138[m4_ifdef([_m4_set_size($1)],
3139	  [m4_if(m4_indir([_m4_set_size($1)]), [0], [$2], [$3])], [$2])])
3140
3141# m4_set_foreach(SET, VAR, ACTION)
3142# --------------------------------
3143# For each element of SET, define VAR to the element and expand
3144# ACTION.  ACTION should not recursively list SET's contents, add
3145# elements to SET, nor delete any element from SET except the one
3146# currently in VAR.  The order that the elements are visited in is not
3147# guaranteed.  This is faster than the corresponding m4_foreach([VAR],
3148#   m4_indir([m4_dquote]m4_set_listc([SET])), [ACTION])
3149m4_define([m4_set_foreach],
3150[m4_pushdef([$2])m4_set_map_sep([$1],
3151[m4_define([$2],], [)$3])m4_popdef([$2])])
3152
3153# m4_set_intersection(SET1, SET2)
3154# -------------------------------
3155# Produce a LIST of quoted elements that occur in both SET1 or SET2.
3156# Output a comma prior to any elements, to distinguish the empty
3157# string from no elements.  This can be directly used as a series of
3158# arguments, such as for m4_join, or wrapped inside quotes for use in
3159# m4_foreach.  Order of the output is not guaranteed.
3160#
3161# Iterate over the smaller set, and short-circuit the idempotence
3162# relation.
3163m4_define([m4_set_intersection],
3164[m4_if([$1], [$2], [m4_set_listc([$1])],
3165       m4_eval(m4_set_size([$2]) < m4_set_size([$1])), [1], [$0([$2], [$1])],
3166       [m4_set_map_sep([$1], [_$0([$2],], [)])])])
3167
3168m4_define([_m4_set_intersection],
3169[m4_set_contains([$1], [$2], [,[$2]])])
3170
3171# m4_set_list(SET)
3172# m4_set_listc(SET)
3173# -----------------
3174# Produce a LIST of quoted elements of SET.  This can be directly used
3175# as a series of arguments, such as for m4_join or m4_set_add_all, or
3176# wrapped inside quotes for use in m4_foreach or m4_map.  With
3177# m4_set_list, there is no way to distinguish an empty set from a set
3178# containing only the empty string; with m4_set_listc, a leading comma
3179# is output if there are any elements.
3180m4_define([m4_set_list],
3181[m4_set_map_sep([$1], [], [], [,])])
3182
3183m4_define([m4_set_listc],
3184[m4_set_map_sep([$1], [,])])
3185
3186# m4_set_map(SET, ACTION)
3187# -----------------------
3188# For each element of SET, expand ACTION with a single argument of the
3189# current element.  ACTION should not recursively list SET's contents,
3190# add elements to SET, nor delete any element from SET except the one
3191# passed as an argument.  The order that the elements are visited in
3192# is not guaranteed.  This is faster than either of the corresponding
3193#   m4_map_args([ACTION]m4_set_listc([SET]))
3194#   m4_set_foreach([SET], [VAR], [ACTION(m4_defn([VAR]))])
3195m4_define([m4_set_map],
3196[m4_set_map_sep([$1], [$2(], [)])])
3197
3198# m4_set_map_sep(SET, [PRE], [POST], [SEP])
3199# -----------------------------------------
3200# For each element of SET, expand PRE[value]POST[], and expand SEP
3201# between elements.
3202m4_define([m4_set_map_sep],
3203[m4_ifdef([_m4_set_cleanup($1)], [_m4_set_contents_1c],
3204	  [_m4_set_contents_1])([$1])_m4_set_contents_2($@)])
3205
3206# m4_set_remove(SET, VALUE, [IF-PRESENT], [IF-ABSENT])
3207# ----------------------------------------------------
3208# If VALUE is an element of SET, delete it and expand IF-PRESENT.
3209# Otherwise expand IF-ABSENT.  Deleting a single value is O(1),
3210# although it leaves memory occupied until the next O(n) traversal of
3211# the set which will compact the set.
3212#
3213# Optimize if the element being removed is the most recently added,
3214# since defining _m4_set_cleanup($1) slows down so many other macros.
3215# In particular, this plays well with m4_set_foreach and m4_set_map.
3216m4_define([m4_set_remove],
3217[m4_set_contains([$1], [$2], [_m4_set_size([$1],
3218    [m4_decr])m4_if(_m4_defn([_m4_set([$1])]), [$2],
3219		    [_m4_popdef([_m4_set([$1],$2)], [_m4_set([$1])])],
3220		    [m4_define([_m4_set_cleanup($1)])m4_define(
3221		      [_m4_set([$1],$2)], [0])])$3], [$4])])
3222
3223# m4_set_size(SET)
3224# ----------------
3225# Expand to the number of elements currently in SET.  This operation
3226# is O(1), and thus more efficient than m4_count(m4_set_list([SET])).
3227m4_define([m4_set_size],
3228[m4_ifdef([_m4_set_size($1)], [m4_indir([_m4_set_size($1)])], [0])])
3229
3230# _m4_set_size(SET, ACTION)
3231# -------------------------
3232# ACTION must be either m4_incr or m4_decr, and the size of SET is
3233# changed accordingly.  If the set is empty, ACTION must not be
3234# m4_decr.
3235m4_define([_m4_set_size],
3236[m4_define([_m4_set_size($1)],
3237	   m4_ifdef([_m4_set_size($1)], [$2(m4_indir([_m4_set_size($1)]))],
3238		    [1]))])
3239
3240# m4_set_union(SET1, SET2)
3241# ------------------------
3242# Produce a LIST of double quoted elements that occur in either SET1
3243# or SET2, without duplicates.  Output a comma prior to any elements,
3244# to distinguish the empty string from no elements.  This can be
3245# directly used as a series of arguments, such as for m4_join, or
3246# wrapped inside quotes for use in m4_foreach.  Order of the output is
3247# not guaranteed.
3248#
3249# We can rely on the fact that m4_set_listc prunes SET1, so we don't
3250# need to check _m4_set([$1],element) for 0.  Short-circuit the
3251# idempotence relation.
3252m4_define([m4_set_union],
3253[m4_set_listc([$1])m4_if([$1], [$2], [],
3254  [m4_set_map_sep([$2], [_$0([$1],], [)])])])
3255
3256m4_define([_m4_set_union],
3257[m4_ifdef([_m4_set([$1],$2)], [], [,[$2]])])
3258
3259
3260## ------------------- ##
3261## 16. File handling.  ##
3262## ------------------- ##
3263
3264
3265# It is a real pity that M4 comes with no macros to bind a diversion
3266# to a file.  So we have to deal without, which makes us a lot more
3267# fragile than we should.
3268
3269
3270# m4_file_append(FILE-NAME, CONTENT)
3271# ----------------------------------
3272m4_define([m4_file_append],
3273[m4_syscmd([cat >>$1 <<_m4eof
3274$2
3275_m4eof
3276])
3277m4_if(m4_sysval, [0], [],
3278      [m4_fatal([$0: cannot write: $1])])])
3279
3280
3281
3282## ------------------------ ##
3283## 17. Setting M4sugar up.  ##
3284## ------------------------ ##
3285
3286# _m4_divert_diversion should be defined.
3287m4_divert_push([KILL])
3288
3289# m4_init
3290# -------
3291# Initialize the m4sugar language.
3292m4_define([m4_init],
3293[# All the M4sugar macros start with `m4_', except `dnl' kept as is
3294# for sake of simplicity.
3295m4_pattern_forbid([^_?m4_])
3296m4_pattern_forbid([^dnl$])
3297
3298# If __m4_version__ is defined, we assume that we are being run by M4
3299# 1.6 or newer, thus $@ recursion is linear, and debugmode(+do)
3300# is available for faster checks of dereferencing undefined macros
3301# and forcing dumpdef to print to stderr regardless of debugfile.
3302# But if it is missing, we assume we are being run by M4 1.4.x, that
3303# $@ recursion is quadratic, and that we need foreach-based
3304# replacement macros.  Also, m4 prior to 1.4.8 loses track of location
3305# during m4wrap text; __line__ should never be 0.
3306#
3307# Use the raw builtin to avoid tripping up include tracing.
3308# Meanwhile, avoid m4_copy, since it temporarily undefines m4_defn.
3309m4_ifdef([__m4_version__],
3310[m4_debugmode([+do])
3311m4_define([m4_defn], _m4_defn([_m4_defn]))
3312m4_define([m4_dumpdef], _m4_defn([_m4_dumpdef]))
3313m4_define([m4_popdef], _m4_defn([_m4_popdef]))
3314m4_define([m4_undefine], _m4_defn([_m4_undefine]))],
3315[m4_builtin([include], [m4sugar/foreach.m4])
3316m4_wrap_lifo([m4_if(__line__, [0], [m4_pushdef([m4_location],
3317]]m4_dquote(m4_dquote(m4_dquote(__file__:__line__)))[[)])])])
3318
3319# Rewrite the first entry of the diversion stack.
3320m4_divert([KILL])
3321
3322# Check the divert push/pop perfect balance.
3323# Some users are prone to also use m4_wrap to register last-minute
3324# m4_divert_text; so after our diversion cleanups, we restore
3325# KILL as the bottom of the diversion stack.
3326m4_wrap([m4_popdef([_m4_divert_diversion])m4_ifdef(
3327  [_m4_divert_diversion], [m4_fatal([$0: unbalanced m4_divert_push:
3328]m4_divert_stack)])_m4_popdef([_m4_divert_stack])m4_divert_push([KILL])])
3329])
3330