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