1 divert(-1)# -*- Autoconf -*-
2 # This file is part of Autoconf.
6 # Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7 # 2008 Free Software Foundation, Inc.
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
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 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 # As a special exception, the Free Software Foundation gives unlimited
23 # permission to copy, distribute and modify the configure scripts that
24 # are the output of Autoconf. You need not follow the terms of the GNU
25 # General Public License when using or distributing such scripts, even
26 # though portions of the text of Autoconf appear in them. The GNU
27 # General Public License (GPL) does govern all other use of the material
28 # that constitutes the Autoconf program.
30 # Certain portions of the Autoconf source text are designed to be copied
31 # (in certain cases, depending on the input) into the output of
32 # Autoconf. We call these the "data" portions. The rest of the Autoconf
33 # source text consists of comments plus executable code that decides which
34 # of the data portions to output in any given case. We call these
35 # comments and executable code the "non-data" portions. Autoconf never
36 # copies any of the non-data portions into its output.
38 # This special exception to the GPL applies to versions of Autoconf
39 # released by the Free Software Foundation. When you make and
40 # distribute a modified version of Autoconf, you may extend this special
41 # exception to the GPL to apply to your modified version as well, *unless*
42 # your modified version has the potential to copy into its output some
43 # of the text that was the non-data portion of the version that you started
44 # with. (In other words, unless your change moves or copies text from
45 # the non-data portions to the data portions.) If your modification has
46 # such potential, you must delete any notice of this special exception
47 # to the GPL from your modified version.
49 # Written by Akim Demaille.
52 # Set the quotes, whatever the current quoting system.
56 # Some old m4's don't support m4exit. But they provide
57 # equivalent functionality by core dumping because of the
58 # long macros we define.
60 [errprint(M4sugar requires GNU M4. Install it before installing M4sugar or
61 set the M4 environment variable to its absolute file name.)
65 ## ------------------------------- ##
66 ## 1. Simulate --prefix-builtins. ##
67 ## ------------------------------- ##
72 define([m4_define], defn([define]))
73 define([m4_defn], defn([defn]))
74 define([m4_undefine], defn([undefine]))
78 m4_undefine([undefine])
83 # Define DST as the definition of SRC.
84 # What's the difference between:
85 # 1. m4_copy([from], [to])
86 # 2. m4_define([to], [from($@)])
87 # Well, obviously 1 is more expensive in space. Maybe 2 is more expensive
88 # in time, but because of the space cost of 1, it's not that obvious.
89 # Nevertheless, one huge difference is the handling of `$0'. If `from'
90 # uses `$0', then with 1, `to''s `$0' is `to', while it is `from' in 2.
91 # The user would certainly prefer to see `to'.
93 [m4_define([$2], m4_defn([$1]))])
98 # Rename the macro SRC to DST.
99 m4_define([m4_rename],
100 [m4_copy([$1], [$2])m4_undefine([$1])])
103 # m4_rename_m4(MACRO-NAME)
104 # ------------------------
105 # Rename MACRO-NAME to m4_MACRO-NAME.
106 m4_define([m4_rename_m4],
107 [m4_rename([$1], [m4_$1])])
110 # m4_copy_unm4(m4_MACRO-NAME)
111 # ---------------------------
112 # Copy m4_MACRO-NAME to MACRO-NAME.
113 m4_define([m4_copy_unm4],
114 [m4_copy([$1], m4_bpatsubst([$1], [^m4_\(.*\)], [[\1]]))])
117 # Some m4 internals have names colliding with tokens we might use.
118 # Rename them a` la `m4 --prefix-builtins'. Conditionals first, since
119 # some subsequent renames are conditional.
120 m4_rename_m4([ifdef])
121 m4_rename([ifelse], [m4_if])
123 m4_rename_m4([builtin])
124 m4_rename_m4([changecom])
125 m4_rename_m4([changequote])
126 m4_ifdef([changeword],dnl conditionally available in 1.4.x
127 [m4_undefine([changeword])])
128 m4_rename_m4([debugfile])
129 m4_rename_m4([debugmode])
131 m4_undefine([divert])
132 m4_rename_m4([divnum])
133 m4_rename_m4([dumpdef])
134 m4_rename_m4([errprint])
135 m4_rename_m4([esyscmd])
137 m4_rename_m4([format])
138 m4_undefine([include])
140 m4_rename_m4([index])
141 m4_rename_m4([indir])
143 m4_rename([m4exit], [m4_exit])
144 m4_undefine([m4wrap])
145 m4_ifdef([mkstemp],dnl added in M4 1.4.8
146 [m4_rename_m4([mkstemp])
147 m4_copy([m4_mkstemp], [m4_maketemp])
148 m4_undefine([maketemp])],
149 [m4_rename_m4([maketemp])
150 m4_copy([m4_maketemp], [m4_mkstemp])])
151 m4_rename([patsubst], [m4_bpatsubst])
152 m4_undefine([popdef])
153 m4_rename_m4([pushdef])
154 m4_rename([regexp], [m4_bregexp])
155 m4_rename_m4([shift])
156 m4_undefine([sinclude])
157 m4_rename_m4([substr])
158 m4_ifdef([symbols],dnl present only in alpha-quality 1.4o
159 [m4_rename_m4([symbols])])
160 m4_rename_m4([syscmd])
161 m4_rename_m4([sysval])
162 m4_rename_m4([traceoff])
163 m4_rename_m4([traceon])
164 m4_rename_m4([translit])
165 m4_undefine([undivert])
168 ## ------------------- ##
169 ## 2. Error messages. ##
170 ## ------------------- ##
175 m4_define([m4_location],
181 # Same as `errprint', but with the missing end of line.
182 m4_define([m4_errprintn],
190 m4_define([m4_warning],
191 [m4_errprintn(m4_location[: warning: $1])])
194 # m4_fatal(MSG, [EXIT-STATUS])
195 # ----------------------------
197 m4_define([m4_fatal],
198 [m4_errprintn(m4_location[: error: $1])dnl
199 m4_expansion_stack_dump()dnl
200 m4_exit(m4_if([$2],, 1, [$2]))])
203 # m4_assert(EXPRESSION, [EXIT-STATUS = 1])
204 # ----------------------------------------
205 # This macro ensures that EXPRESSION evaluates to true, and exits if
206 # EXPRESSION evaluates to false.
207 m4_define([m4_assert],
208 [m4_if(m4_eval([$1]), 0,
209 [m4_fatal([assert failed: $1], [$2])])])
218 # _m4_warn(CATEGORY, MESSAGE, STACK-TRACE)
219 # ----------------------------------------
220 # Report a MESSAGE to the user if the CATEGORY of warnings is enabled.
221 # This is for traces only.
222 # The STACK-TRACE is a \n-separated list of "LOCATION: MESSAGE".
224 # Within m4, the macro is a no-op. This macro really matters
225 # when autom4te post-processes the trace output.
226 m4_define([_m4_warn], [])
229 # m4_warn(CATEGORY, MESSAGE)
230 # --------------------------
231 # Report a MESSAGE to the user if the CATEGORY of warnings is enabled.
233 [_m4_warn([$1], [$2],
234 m4_ifdef([m4_expansion_stack],
235 [m4_defn([m4_expansion_stack])
236 m4_location[: the top level]]))dnl
241 ## ------------------- ##
242 ## 4. File inclusion. ##
243 ## ------------------- ##
246 # We also want to neutralize include (and sinclude for symmetry),
247 # but we want to extend them slightly: warn when a file is included
248 # several times. This is, in general, a dangerous operation, because
249 # too many people forget to quote the first argument of m4_define.
251 # For instance in the following case:
252 # m4_define(foo, [bar])
253 # then a second reading will turn into
254 # m4_define(bar, [bar])
255 # which is certainly not what was meant.
257 # m4_include_unique(FILE)
258 # -----------------------
259 # Declare that the FILE was loading; and warn if it has already
261 m4_define([m4_include_unique],
262 [m4_ifdef([m4_include($1)],
263 [m4_warn([syntax], [file `$1' included several times])])dnl
264 m4_define([m4_include($1)])])
269 # Like the builtin include, but warns against multiple inclusions.
270 m4_define([m4_include],
271 [m4_include_unique([$1])dnl
272 m4_builtin([include], [$1])])
277 # Like the builtin sinclude, but warns against multiple inclusions.
278 m4_define([m4_sinclude],
279 [m4_include_unique([$1])dnl
280 m4_builtin([sinclude], [$1])])
284 ## ------------------------------------ ##
285 ## 5. Additional branching constructs. ##
286 ## ------------------------------------ ##
288 # Both `m4_ifval' and `m4_ifset' tests against the empty string. The
289 # difference is that `m4_ifset' is specialized on macros.
291 # In case of arguments of macros, eg. $1, it makes little difference.
292 # In the case of a macro `FOO', you don't want to check `m4_ifval(FOO,
293 # TRUE)', because if `FOO' expands with commas, there is a shifting of
294 # the arguments. So you want to run `m4_ifval([FOO])', but then you just
295 # compare the *string* `FOO' against `', which, of course fails.
297 # So you want the variation `m4_ifset' that expects a macro name as $1.
298 # If this macro is both defined and defined to a non empty value, then
302 # m4_ifval(COND, [IF-TRUE], [IF-FALSE])
303 # -------------------------------------
304 # If COND is not the empty string, expand IF-TRUE, otherwise IF-FALSE.
305 # Comparable to m4_ifdef.
306 m4_define([m4_ifval],
307 [m4_if([$1], [], [$3], [$2])])
312 # If TEXT is not empty, return TEXT and a new line, otherwise nothing.
320 # m4_ifvaln(COND, [IF-TRUE], [IF-FALSE])
321 # --------------------------------------
322 # Same as `m4_ifval', but add an extra newline to IF-TRUE or IF-FALSE
323 # unless that argument is empty.
324 m4_define([m4_ifvaln],
330 # m4_ifset(MACRO, [IF-TRUE], [IF-FALSE])
331 # --------------------------------------
332 # If MACRO has no definition, or of its definition is the empty string,
333 # expand IF-FALSE, otherwise IF-TRUE.
334 m4_define([m4_ifset],
336 [m4_ifval(m4_defn([$1]), [$2], [$3])],
340 # m4_ifndef(NAME, [IF-NOT-DEFINED], [IF-DEFINED])
341 # -----------------------------------------------
342 m4_define([m4_ifndef],
343 [m4_ifdef([$1], [$3], [$2])])
346 # m4_case(SWITCH, VAL1, IF-VAL1, VAL2, IF-VAL2, ..., DEFAULT)
347 # -----------------------------------------------------------
362 # All the values are optional, and the macro is robust to active
363 # symbols properly quoted.
369 [$0([$1], m4_shift3($@))])])
372 # m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT)
373 # -----------------------------------------------------
378 # elif (SWITCH =~ RE2)
385 # All the values are optional, and the macro is robust to active symbols
387 m4_define([m4_bmatch],
388 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
389 [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
391 [m4_if(m4_bregexp([$1], [$2]), -1, [$0([$1], m4_shift3($@))],
398 # Manipulate m4 lists.
399 m4_define([m4_car], [[$1]])
401 [m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])],
403 [m4_dquote(m4_shift($@))])])
406 ## ---------------------------------------- ##
407 ## 6. Enhanced version of some primitives. ##
408 ## ---------------------------------------- ##
410 # m4_bpatsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...)
411 # ----------------------------------------------------
419 # All the values are optional, and the macro is robust to active symbols
422 # I would have liked to name this macro `m4_bpatsubst', unfortunately,
423 # due to quotation problems, I need to double quote $1 below, therefore
424 # the anchors are broken :( I can't let users be trapped by that.
426 # Recall that m4_shift3 always results in an argument. Hence, we need
427 # to distinguish between a final deletion vs. ending recursion.
428 m4_define([m4_bpatsubsts],
429 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
430 [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
431 [$#], 2, [m4_unquote(m4_builtin([patsubst], [[$1]], [$2]))],
432 [$#], 3, [m4_unquote(m4_builtin([patsubst], [[$1]], [$2], [$3]))],
433 [_$0($@m4_if(m4_eval($# & 1), 0, [,]))])])
434 m4_define([_m4_bpatsubsts],
435 [m4_if([$#], 2, [$1],
436 [$0(m4_builtin([patsubst], [[$1]], [$2], [$3]),
442 # This macro invokes all its arguments (in sequence, of course). It is
443 # useful for making your macros more structured and readable by dropping
444 # unnecessary dnl's and have the macros indented properly.
448 [$1[]m4_do(m4_shift($@))])])
451 # m4_define_default(MACRO, VALUE)
452 # -------------------------------
453 # If MACRO is undefined, set it to VALUE.
454 m4_define([m4_define_default],
455 [m4_ifndef([$1], [m4_define($@)])])
458 # m4_default(EXP1, EXP2)
459 # ----------------------
460 # Returns EXP1 if non empty, otherwise EXP2.
461 m4_define([m4_default],
462 [m4_ifval([$1], [$1], [$2])])
467 # Unlike to the original, don't tolerate popping something which is
471 [m4_fatal([$0: undefined macro: $1])])dnl
472 m4_builtin([defn], $@)])
475 # _m4_dumpdefs_up(NAME)
476 # ---------------------
477 m4_define([_m4_dumpdefs_up],
479 [m4_pushdef([_m4_dumpdefs], m4_defn([$1]))dnl
482 _m4_dumpdefs_up([$1])])])
485 # _m4_dumpdefs_down(NAME)
486 # -----------------------
487 m4_define([_m4_dumpdefs_down],
488 [m4_ifdef([_m4_dumpdefs],
489 [m4_pushdef([$1], m4_defn([_m4_dumpdefs]))dnl
490 m4_popdef([_m4_dumpdefs])dnl
491 _m4_dumpdefs_down([$1])])])
496 # Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its
497 # value stack (most recent displayed first).
498 m4_define([m4_dumpdefs],
499 [_m4_dumpdefs_up([$1])dnl
500 _m4_dumpdefs_down([$1])])
505 # Unlike to the original, don't tolerate popping something which is
507 m4_define([m4_popdef],
509 [m4_fatal([$0: undefined macro: $1])])dnl
510 m4_builtin([popdef], $@)])
515 # Return ARGS as a single arguments.
517 # It is important to realize the difference between `m4_quote(exp)' and
518 # `[exp]': in the first case you obtain the quoted *result* of the
519 # expansion of EXP, while in the latter you just obtain the string
521 m4_define([m4_quote], [[$*]])
522 m4_define([m4_dquote], [[$@]])
527 # Return the result of ignoring all quotes in STRING and invoking the
528 # macros it contains. Amongst other things, this is useful for enabling
529 # macro invocations inside strings with [] blocks (for instance regexps
530 # and help-strings). On the other hand, since all quotes are disabled,
531 # any macro expanded during this time that relies on nested [] quoting
532 # will likely crash and burn. This macro is seldom useful; consider
533 # m4_unquote or m4_expand instead.
534 m4_define([m4_noquote],
535 [m4_changequote(-=<{,}>=-)$1-=<{}>=-m4_changequote([,])])
540 # Returns ... shifted N times. Useful for recursive "varargs" constructs.
542 # Autoconf does not use this macro, because it is inherently slower than
543 # calling the common cases of m4_shift2 or m4_shift3 directly. But it
544 # might as well be fast for other clients, such as Libtool. One way to
545 # do this is to expand $@ only once in _m4_shiftn (otherwise, for long
546 # lists, the expansion of m4_if takes twice as much memory as what the
547 # list itself occupies, only to throw away the unused branch). The end
548 # result is strictly equivalent to
549 # m4_if([$1], 1, [m4_shift(,m4_shift(m4_shift($@)))],
550 # [_m4_shiftn(m4_decr([$1]), m4_shift(m4_shift($@)))])
551 # but with the final `m4_shift(m4_shift($@)))' shared between the two
552 # paths. The first leg uses a no-op m4_shift(,$@) to balance out the ().
553 m4_define([m4_shiftn],
554 [m4_assert(0 < $1 && $1 < $#)_$0($@)])
556 m4_define([_m4_shiftn],
557 [m4_if([$1], 1, [m4_shift(],
558 [$0(m4_decr([$1])]), m4_shift(m4_shift($@)))])
563 # Returns ... shifted twice, and three times. Faster than m4_shiftn.
564 m4_define([m4_shift2], [m4_shift(m4_shift($@))])
565 m4_define([m4_shift3], [m4_shift(m4_shift(m4_shift($@)))])
570 # Like m4_shift2 or m4_shift3, except include a leading comma unless shifting
571 # consumes all arguments. Why? Because in recursion, it is nice to
572 # distinguish between 1 element left and 0 elements left, based on how many
573 # arguments this shift expands to.
574 m4_define([_m4_shift2],
575 [m4_if([$#], [2], [],
576 [, m4_shift(m4_shift($@))])])
577 m4_define([_m4_shift3],
578 [m4_if([$#], [3], [],
579 [, m4_shift(m4_shift(m4_shift($@)))])])
584 # Unlike to the original, don't tolerate undefining something which is
586 m4_define([m4_undefine],
588 [m4_fatal([$0: undefined macro: $1])])dnl
589 m4_builtin([undefine], $@)])
594 # Remove one layer of quotes from each ARG, performing one level of
595 # expansion. For one argument, m4_unquote([arg]) is more efficient than
596 # m4_do([arg]), but for multiple arguments, the difference is that
597 # m4_unquote separates arguments with commas while m4_do concatenates.
598 m4_define([m4_unquote], [$*])
600 # _m4_wrap(PRE, POST)
601 # -------------------
602 # Helper macro for m4_wrap and m4_wrap_lifo. Allows nested calls to
603 # m4_wrap within wrapped text.
604 # Skip m4_defn and m4_popdef for speed.
605 m4_define([_m4_wrap],
607 [m4_define([$0_text], [$1]m4_builtin([defn], [$0_text])[$2])],
608 [m4_builtin([m4wrap], [m4_unquote(m4_builtin([defn],
609 [$0_text])m4_builtin([popdef], [$0_text]))])m4_define([$0_text], [$1$2])])])
613 # Append TEXT to the list of hooks to be executed at the end of input.
614 # Whereas the order of the original may be LIFO in the underlying m4,
615 # this version is always FIFO.
617 [_m4_wrap([], [$1[]])])
621 # Prepend TEXT to the list of hooks to be executed at the end of input.
622 # Whereas the order of m4_wrap may be FIFO in the underlying m4, this
623 # version is always LIFO.
624 m4_define([m4_wrap_lifo],
627 ## ------------------------- ##
628 ## 7. Quoting manipulation. ##
629 ## ------------------------- ##
632 # m4_apply(MACRO, LIST)
633 # ---------------------
634 # Invoke MACRO, with arguments provided from the quoted list of
635 # comma-separated quoted arguments. If LIST is empty, invoke MACRO
636 # without arguments. The expansion will not be concatenated with
638 m4_define([m4_apply],
639 [m4_if([$2], [], [$1], [$1($2)])[]])
641 # _m4_apply(MACRO, LIST)
642 # ----------------------
643 # Like m4_apply, except do nothing if LIST is empty.
644 m4_define([_m4_apply],
645 [m4_if([$2], [], [], [$1($2)[]])])
648 ## -------------------------- ##
649 ## 8. Implementing m4 loops. ##
650 ## -------------------------- ##
653 # m4_for(VARIABLE, FIRST, LAST, [STEP = +/-1], EXPRESSION)
654 # --------------------------------------------------------
655 # Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO with
656 # increments of STEP.
657 # Both limits are included, and bounds are checked for consistency.
659 [m4_case(m4_sign(m4_eval($3 - $2)),
660 1, [m4_assert(m4_sign(m4_default($4, 1)) == 1)],
661 -1, [m4_assert(m4_sign(m4_default($4, -1)) == -1)])dnl
662 m4_pushdef([$1], [$2])dnl
663 m4_if(m4_eval([$3 > $2]), 1,
664 [_m4_for([$1], [$3], m4_default([$4], 1), [$5])],
665 [_m4_for([$1], [$3], m4_default([$4], -1), [$5])])dnl
669 # _m4_for(VARIABLE, FIRST, LAST, STEP, EXPRESSION)
670 # ------------------------------------------------
671 # Core of the loop, no consistency checks.
675 [m4_define([$1], m4_eval($1+[$3]))_m4_for([$1], [$2], [$3], [$4])])])
678 # Implementing `foreach' loops in m4 is much more tricky than it may
679 # seem. For example, the old M4 1.4.4 manual had an incorrect example,
680 # which looked like this (when translated to m4sugar):
682 # | # foreach(VAR, (LIST), STMT)
683 # | m4_define([foreach],
684 # | [m4_pushdef([$1])_foreach([$1], [$2], [$3])m4_popdef([$1])])
685 # | m4_define([_arg1], [$1])
686 # | m4_define([_foreach],
687 # | [m4_if([$2], [()], ,
688 # | [m4_define([$1], _arg1$2)$3[]_foreach([$1], (m4_shift$2), [$3])])])
690 # But then if you run
695 # | foreach([f], [([a], [(b], [c)])], [echo f
703 # which is not what is expected.
705 # Of course the problem is that many quotes are missing. So you add
706 # plenty of quotes at random places, until you reach the expected
707 # result. Alternatively, if you are a quoting wizard, you directly
708 # reach the following implementation (but if you really did, then
709 # apply to the maintenance of m4sugar!).
711 # | # foreach(VAR, (LIST), STMT)
712 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
713 # | m4_define([_arg1], [[$1]])
714 # | m4_define([_foreach],
715 # | [m4_if($2, [()], ,
716 # | [m4_define([$1], [_arg1$2])$3[]_foreach([$1], [(m4_shift$2)], [$3])])])
718 # which this time answers
728 # With a better look, you realize that the parens are more a pain than
729 # a help: since anyway you need to quote properly the list, you end up
730 # with always using an outermost pair of parens and an outermost pair
731 # of quotes. Rejecting the parens both eases the implementation, and
732 # simplifies the use:
734 # | # foreach(VAR, (LIST), STMT)
735 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
736 # | m4_define([_arg1], [$1])
737 # | m4_define([_foreach],
739 # | [m4_define([$1], [_arg1($2)])$3[]_foreach([$1], [m4_shift($2)], [$3])])])
742 # Now, just replace the `$2' with `m4_quote($2)' in the outer `m4_if'
743 # to improve robustness, and you come up with a nice implementation
744 # that doesn't require extra parentheses in the user's LIST.
746 # But wait - now the algorithm is quadratic, because every recursion of
747 # the algorithm keeps the entire LIST and merely adds another m4_shift to
748 # the quoted text. If the user has a lot of elements in LIST, you can
749 # bring the system to its knees with the memory m4 then requires, or trip
750 # the m4 --nesting-limit recursion factor. The only way to avoid
751 # quadratic growth is ensure m4_shift is expanded prior to the recursion.
752 # Hence the design below.
754 # The M4 manual now includes a chapter devoted to this issue, with
755 # the lessons learned from m4sugar. And still, this design is only
756 # optimal for M4 1.6; see foreach.m4 for yet more comments on why
757 # M4 1.4.x uses yet another implementation.
760 # m4_foreach(VARIABLE, LIST, EXPRESSION)
761 # --------------------------------------
763 # Expand EXPRESSION assigning each value of the LIST to VARIABLE.
764 # LIST should have the form `item_1, item_2, ..., item_n', i.e. the
765 # whole list must *quoted*. Quote members too if you don't want them
768 # This macro is robust to active symbols:
769 # | m4_define(active, [ACT, IVE])
770 # | m4_foreach(Var, [active, active], [-Var-])
771 # => -ACT--IVE--ACT--IVE-
773 # | m4_foreach(Var, [[active], [active]], [-Var-])
774 # => -ACT, IVE--ACT, IVE-
776 # | m4_foreach(Var, [[[active]], [[active]]], [-Var-])
777 # => -active--active-
778 m4_define([m4_foreach],
779 [m4_pushdef([$1])_m4_foreach($@)m4_popdef([$1])])
781 m4_define([_m4_foreach],
783 [m4_define([$1], m4_car($2))$3[]dnl
784 _m4_foreach([$1], m4_cdr($2), [$3])])])
787 # m4_foreach_w(VARIABLE, LIST, EXPRESSION)
788 # ----------------------------------------
790 # Like m4_foreach, but the list is whitespace separated.
792 # This macro is robust to active symbols:
793 # m4_foreach_w([Var], [ active
796 # => -active--b--active-end
798 m4_define([m4_foreach_w],
799 [m4_foreach([$1], m4_split(m4_normalize([$2])), [$3])])
802 # m4_map(MACRO, LIST)
803 # m4_mapall(MACRO, LIST)
804 # ----------------------
805 # Invoke MACRO($1), MACRO($2) etc. where $1, $2... are the elements of
806 # LIST. $1, $2... must in turn be lists, appropriate for m4_apply.
807 # If LIST contains an empty sublist, m4_map skips the expansion of
808 # MACRO, while m4_mapall expands MACRO with no arguments.
810 # Since LIST may be quite large, we want to minimize how often it
811 # appears in the expansion. Rather than use m4_car/m4_cdr iteration,
812 # we unbox the list, ignore the second argument, and use m4_shift2 to
813 # detect the end of recursion. The mismatch in () is intentional; see
814 # _m4_map. For m4_map, an empty list behaves like an empty sublist
815 # and gets ignored; for m4_mapall, we must special-case the empty
818 [_m4_map([_m4_apply([$1]], [], $2)])
820 m4_define([m4_mapall],
822 [_m4_map([m4_apply([$1]], [], $2)])])
825 # m4_map_sep(MACRO, SEPARATOR, LIST)
826 # m4_mapall_sep(MACRO, SEPARATOR, LIST)
827 # -------------------------------------
828 # Invoke MACRO($1), SEPARATOR, MACRO($2), ..., MACRO($N) where $1,
829 # $2... $N are the elements of LIST, and are in turn lists appropriate
830 # for m4_apply. SEPARATOR is expanded, in order to allow the creation
831 # of a list of arguments by using a single-quoted comma as the
832 # separator. For each empty sublist, m4_map_sep skips the expansion
833 # of MACRO and SEPARATOR, while m4_mapall_sep expands MACRO with no
836 # For m4_mapall_sep, merely expand the first iteration without the
837 # separator, then include separator as part of subsequent recursion.
838 # For m4_map_sep, things are trickier - we don't know if the first
839 # list element is an empty sublist, so we must define a self-modifying
840 # helper macro and use that as the separator instead.
841 m4_define([m4_map_sep],
842 [m4_pushdef([m4_Sep], [m4_define([m4_Sep], m4_defn([m4_unquote]))])]dnl
843 [_m4_map([_m4_apply([m4_Sep([$2])[]$1]], [], $3)m4_popdef([m4_Sep])])
845 m4_define([m4_mapall_sep],
847 [m4_apply([$1], m4_car($3))_m4_map([m4_apply([$2[]$1]], $3)])])
849 # _m4_map(PREFIX, IGNORED, SUBLIST, ...)
850 # --------------------------------------
851 # Common implementation for all four m4_map variants. The mismatch in
852 # the number of () is intentional. PREFIX must supply a form of
853 # m4_apply, the open `(', and the MACRO to be applied. Each iteration
854 # then appends `,', the current SUBLIST and the closing `)', then
855 # recurses to the next SUBLIST. IGNORED is an aid to ending recursion
858 [m4_if([$#], [2], [],
859 [$1, [$3])$0([$1], m4_shift2($@))])])
862 ## --------------------------- ##
863 ## 9. More diversion support. ##
864 ## --------------------------- ##
867 # _m4_divert(DIVERSION-NAME or NUMBER)
868 # ------------------------------------
869 # If DIVERSION-NAME is the name of a diversion, return its number,
870 # otherwise if it is a NUMBER return it.
871 m4_define([_m4_divert],
872 [m4_ifdef([_m4_divert($1)],
873 [m4_indir([_m4_divert($1)])],
876 # KILL is only used to suppress output.
877 m4_define([_m4_divert(KILL)], -1)
882 # Print m4_divert_stack with newline prepended, if it's nonempty.
883 m4_define([_m4_divert_n_stack],
884 [m4_ifdef([m4_divert_stack], [
885 m4_defn([m4_divert_stack])])])
888 # m4_divert(DIVERSION-NAME)
889 # -------------------------
890 # Change the diversion stream to DIVERSION-NAME.
891 m4_define([m4_divert],
892 [m4_define([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
893 m4_builtin([divert], _m4_divert([$1]))dnl
897 # m4_divert_push(DIVERSION-NAME)
898 # ------------------------------
899 # Change the diversion stream to DIVERSION-NAME, while stacking old values.
900 m4_define([m4_divert_push],
901 [m4_pushdef([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
902 m4_pushdef([_m4_divert_diversion], [$1])dnl
903 m4_builtin([divert], _m4_divert([$1]))dnl
907 # m4_divert_pop([DIVERSION-NAME])
908 # -------------------------------
909 # Change the diversion stream to its previous value, unstacking it.
910 # If specified, verify we left DIVERSION-NAME.
911 # When we pop the last value from the stack, we divert to -1.
912 m4_define([m4_divert_pop],
913 [m4_ifndef([_m4_divert_diversion],
914 [m4_fatal([too many m4_divert_pop])])dnl
916 [$1], m4_defn([_m4_divert_diversion]), [],
917 [m4_fatal([$0($1): diversion mismatch: ]_m4_divert_n_stack)])dnl
918 m4_popdef([m4_divert_stack])dnl
919 m4_popdef([_m4_divert_diversion])dnl
921 m4_ifdef([_m4_divert_diversion],
922 [_m4_divert(m4_defn([_m4_divert_diversion]))],
927 # m4_divert_text(DIVERSION-NAME, CONTENT)
928 # ---------------------------------------
929 # Output CONTENT into DIVERSION-NAME (which may be a number actually).
930 # An end of line is appended for free to CONTENT.
931 m4_define([m4_divert_text],
932 [m4_divert_push([$1])dnl
934 m4_divert_pop([$1])dnl
938 # m4_divert_once(DIVERSION-NAME, CONTENT)
939 # ---------------------------------------
940 # Output CONTENT into DIVERSION-NAME once, if not already there.
941 # An end of line is appended for free to CONTENT.
942 m4_define([m4_divert_once],
943 [m4_expand_once([m4_divert_text([$1], [$2])])])
946 # m4_undivert(DIVERSION-NAME)
947 # ---------------------------
948 # Undivert DIVERSION-NAME. Unlike the M4 version, this only takes a single
949 # diversion identifier, and should not be used to undivert files.
950 m4_define([m4_undivert],
951 [m4_builtin([undivert], _m4_divert([$1]))])
954 ## --------------------------------------------- ##
955 ## 10. Defining macros with bells and whistles. ##
956 ## --------------------------------------------- ##
958 # `m4_defun' is basically `m4_define' but it equips the macro with the
959 # needed machinery for `m4_require'. A macro must be m4_defun'd if
960 # either it is m4_require'd, or it m4_require's.
962 # Two things deserve attention and are detailed below:
963 # 1. Implementation of m4_require
964 # 2. Keeping track of the expansion stack
966 # 1. Implementation of m4_require
967 # ===============================
969 # Of course m4_defun AC_PROVIDE's the macro, so that a macro which has
970 # been expanded is not expanded again when m4_require'd, but the
971 # difficult part is the proper expansion of macros when they are
974 # The implementation is based on two ideas, (i) using diversions to
975 # prepare the expansion of the macro and its dependencies (by Franc,ois
976 # Pinard), and (ii) expand the most recently m4_require'd macros _after_
977 # the previous macros (by Axel Thimm).
980 # The first idea: why use diversions?
981 # -----------------------------------
983 # When a macro requires another, the other macro is expanded in new
984 # diversion, GROW. When the outer macro is fully expanded, we first
985 # undivert the most nested diversions (GROW - 1...), and finally
986 # undivert GROW. To understand why we need several diversions,
987 # consider the following example:
989 # | m4_defun([TEST1], [Test...REQUIRE([TEST2])1])
990 # | m4_defun([TEST2], [Test...REQUIRE([TEST3])2])
991 # | m4_defun([TEST3], [Test...3])
993 # Because m4_require is not required to be first in the outer macros, we
994 # must keep the expansions of the various levels of m4_require separated.
995 # Right before executing the epilogue of TEST1, we have:
1002 # Finally the epilogue of TEST1 undiverts GROW - 2, GROW - 1, and
1003 # GROW into the regular flow, BODY.
1008 # BODY: Test...3; Test...2; Test...1
1010 # (The semicolons are here for clarification, but of course are not
1011 # emitted.) This is what Autoconf 2.0 (I think) to 2.13 (I'm sure)
1015 # The second idea: first required first out
1016 # -----------------------------------------
1018 # The natural implementation of the idea above is buggy and produces
1019 # very surprising results in some situations. Let's consider the
1020 # following example to explain the bug:
1022 # | m4_defun([TEST1], [REQUIRE([TEST2a])REQUIRE([TEST2b])])
1023 # | m4_defun([TEST2a], [])
1024 # | m4_defun([TEST2b], [REQUIRE([TEST3])])
1025 # | m4_defun([TEST3], [REQUIRE([TEST2a])])
1030 # The dependencies between the macros are:
1033 # / \ is m4_require'd by
1034 # / \ left -------------------- right
1037 # If you strictly apply the rules given in the previous section you get:
1040 # GROW - 1: TEST2a; TEST2b
1044 # (TEST2a, although required by TEST3 is not expanded in GROW - 3
1045 # because is has already been expanded before in GROW - 1, so it has
1046 # been AC_PROVIDE'd, so it is not expanded again) so when you undivert
1047 # the stack of diversions, you get:
1052 # BODY: TEST3; TEST2a; TEST2b; TEST1
1054 # i.e., TEST2a is expanded after TEST3 although the latter required the
1057 # Starting from 2.50, we use an implementation provided by Axel Thimm.
1058 # The idea is simple: the order in which macros are emitted must be the
1059 # same as the one in which macros are expanded. (The bug above can
1060 # indeed be described as: a macro has been AC_PROVIDE'd before its
1061 # dependent, but it is emitted after: the lack of correlation between
1062 # emission and expansion order is guilty).
1064 # How to do that? You keep the stack of diversions to elaborate the
1065 # macros, but each time a macro is fully expanded, emit it immediately.
1067 # In the example above, when TEST2a is expanded, but it's epilogue is
1068 # not run yet, you have:
1072 # GROW: Elaboration of TEST1
1075 # The epilogue of TEST2a emits it immediately:
1079 # GROW: Elaboration of TEST1
1082 # TEST2b then requires TEST3, so right before the epilogue of TEST3, you
1086 # GROW - 1: Elaboration of TEST2b
1087 # GROW: Elaboration of TEST1
1090 # The epilogue of TEST3 emits it:
1093 # GROW - 1: Elaboration of TEST2b
1094 # GROW: Elaboration of TEST1
1095 # BODY: TEST2a; TEST3
1097 # TEST2b is now completely expanded, and emitted:
1101 # GROW: Elaboration of TEST1
1102 # BODY: TEST2a; TEST3; TEST2b
1104 # and finally, TEST1 is finished and emitted:
1109 # BODY: TEST2a; TEST3; TEST2b: TEST1
1111 # The idea is simple, but the implementation is a bit evolved. If you
1112 # are like me, you will want to see the actual functioning of this
1113 # implementation to be convinced. The next section gives the full
1117 # The Axel Thimm implementation at work
1118 # -------------------------------------
1120 # We consider the macros above, and this configure.ac:
1125 # You should keep the definitions of _m4_defun_pro, _m4_defun_epi, and
1126 # m4_require at hand to follow the steps.
1128 # This implements tries not to assume that the current diversion is
1129 # BODY, so as soon as a macro (m4_defun'd) is expanded, we first
1130 # record the current diversion under the name _m4_divert_dump (denoted
1131 # DUMP below for short). This introduces an important difference with
1132 # the previous versions of Autoconf: you cannot use m4_require if you
1133 # are not inside an m4_defun'd macro, and especially, you cannot
1134 # m4_require directly from the top level.
1136 # We have not tried to simulate the old behavior (better yet, we
1137 # diagnose it), because it is too dangerous: a macro m4_require'd from
1138 # the top level is expanded before the body of `configure', i.e., before
1139 # any other test was run. I let you imagine the result of requiring
1140 # AC_STDC_HEADERS for instance, before AC_PROG_CC was actually run....
1142 # After AC_INIT was run, the current diversion is BODY.
1145 # diversion stack: BODY |-
1147 # * TEST1 is expanded
1148 # The prologue of TEST1 sets _m4_divert_dump, which is the diversion
1149 # where the current elaboration will be dumped, to the current
1150 # diversion. It also m4_divert_push to GROW, where the full
1151 # expansion of TEST1 and its dependencies will be elaborated.
1154 # diversions: GROW, BODY |-
1156 # * TEST1 requires TEST2a
1157 # _m4_require_call m4_divert_pushes another temporary diversion,
1158 # GROW - 1, and expands TEST2a in there.
1162 # diversions: GROW - 1, GROW, BODY |-
1163 # Than the content of the temporary diversion is moved to DUMP and the
1164 # temporary diversion is popped.
1167 # diversions: GROW, BODY |-
1169 # * TEST1 requires TEST2b
1170 # Again, _m4_require_call pushes GROW - 1 and heads to expand TEST2b.
1173 # diversions: GROW - 1, GROW, BODY |-
1175 # * TEST2b requires TEST3
1176 # _m4_require_call pushes GROW - 2 and expands TEST3 here.
1177 # (TEST3 requires TEST2a, but TEST2a has already been m4_provide'd, so
1182 # diversions: GROW - 2, GROW - 1, GROW, BODY |-
1183 # Than the diversion is appended to DUMP, and popped.
1185 # BODY: TEST2a; TEST3
1186 # diversions: GROW - 1, GROW, BODY |-
1188 # * TEST1 requires TEST2b (contd.)
1189 # The content of TEST2b is expanded...
1191 # BODY: TEST2a; TEST3
1193 # diversions: GROW - 1, GROW, BODY |-
1194 # ... and moved to DUMP.
1196 # BODY: TEST2a; TEST3; TEST2b
1197 # diversions: GROW, BODY |-
1199 # * TEST1 is expanded: epilogue
1200 # TEST1's own content is in GROW...
1202 # BODY: TEST2a; TEST3; TEST2b
1204 # diversions: BODY |-
1205 # ... and it's epilogue moves it to DUMP and then undefines DUMP.
1207 # BODY: TEST2a; TEST3; TEST2b; TEST1
1208 # diversions: BODY |-
1211 # 2. Keeping track of the expansion stack
1212 # =======================================
1214 # When M4 expansion goes wrong it is often extremely hard to find the
1215 # path amongst macros that drove to the failure. What is needed is
1216 # the stack of macro `calls'. One could imagine that GNU M4 would
1217 # maintain a stack of macro expansions, unfortunately it doesn't, so
1218 # we do it by hand. This is of course extremely costly, but the help
1219 # this stack provides is worth it. Nevertheless to limit the
1220 # performance penalty this is implemented only for m4_defun'd macros,
1221 # not for define'd macros.
1223 # The scheme is simplistic: each time we enter an m4_defun'd macros,
1224 # we prepend its name in m4_expansion_stack, and when we exit the
1225 # macro, we remove it (thanks to pushdef/popdef).
1227 # In addition, we want to detect circular m4_require dependencies.
1228 # Each time we expand a macro FOO we define _m4_expanding(FOO); and
1229 # m4_require(BAR) simply checks whether _m4_expanding(BAR) is defined.
1232 # m4_expansion_stack_push(TEXT)
1233 # -----------------------------
1234 m4_define([m4_expansion_stack_push],
1235 [m4_pushdef([m4_expansion_stack],
1236 [$1]m4_ifdef([m4_expansion_stack], [
1237 m4_defn([m4_expansion_stack])]))])
1240 # m4_expansion_stack_pop
1241 # ----------------------
1242 m4_define([m4_expansion_stack_pop],
1243 [m4_popdef([m4_expansion_stack])])
1246 # m4_expansion_stack_dump
1247 # -----------------------
1248 # Dump the expansion stack.
1249 m4_define([m4_expansion_stack_dump],
1250 [m4_ifdef([m4_expansion_stack],
1251 [m4_errprintn(m4_defn([m4_expansion_stack]))])dnl
1252 m4_errprintn(m4_location[: the top level])])
1257 # This diversion is used by the m4_defun/m4_require machinery. It is
1258 # important to keep room before GROW because for each nested
1259 # AC_REQUIRE we use an additional diversion (i.e., two m4_require's
1260 # will use GROW - 2. More than 3 levels has never seemed to be
1265 # m4_require'd code, 2 level deep
1267 # m4_require'd code, 1 level deep
1269 # m4_defun'd macros are elaborated here.
1271 m4_define([_m4_divert(GROW)], 10000)
1274 # _m4_defun_pro(MACRO-NAME)
1275 # -------------------------
1276 # The prologue for Autoconf macros.
1277 m4_define([_m4_defun_pro],
1278 [m4_ifndef([m4_expansion_stack], [_m4_defun_pro_outer[]])dnl
1279 m4_expansion_stack_push(m4_defn([m4_location($1)])[: $1 is expanded from...])dnl
1280 m4_pushdef([_m4_expanding($1)])dnl
1283 m4_define([_m4_defun_pro_outer],
1284 [m4_copy([_m4_divert_diversion], [_m4_divert_dump])dnl
1285 m4_divert_push([GROW])dnl
1288 # _m4_defun_epi(MACRO-NAME)
1289 # -------------------------
1290 # The Epilogue for Autoconf macros. MACRO-NAME only helps tracing
1291 # the PRO/EPI pairs.
1292 m4_define([_m4_defun_epi],
1293 [m4_popdef([_m4_expanding($1)])dnl
1294 m4_expansion_stack_pop()dnl
1295 m4_ifndef([m4_expansion_stack], [_m4_defun_epi_outer[]])dnl
1299 m4_define([_m4_defun_epi_outer],
1300 [m4_undefine([_m4_divert_dump])dnl
1301 m4_divert_pop([GROW])dnl
1302 m4_undivert([GROW])dnl
1306 # m4_defun(NAME, EXPANSION)
1307 # -------------------------
1308 # Define a macro which automatically provides itself. Add machinery
1309 # so the macro automatically switches expansion to the diversion
1310 # stack if it is not already using it. In this case, once finished,
1311 # it will bring back all the code accumulated in the diversion stack.
1312 # This, combined with m4_require, achieves the topological ordering of
1313 # macros. We don't use this macro to define some frequently called
1314 # macros that are not involved in ordering constraints, to save m4
1316 m4_define([m4_defun],
1317 [m4_define([m4_location($1)], m4_location)dnl
1319 [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])
1322 # m4_defun_once(NAME, EXPANSION)
1323 # ------------------------------
1324 # As m4_defun, but issues the EXPANSION only once, and warns if used
1326 m4_define([m4_defun_once],
1327 [m4_define([m4_location($1)], m4_location)dnl
1329 [m4_provide_if([$1],
1330 [m4_warn([syntax], [$1 invoked multiple times])],
1331 [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])])
1334 # m4_pattern_forbid(ERE, [WHY])
1335 # -----------------------------
1336 # Declare that no token matching the forbidden extended regular
1337 # expression ERE should be seen in the output unless...
1338 m4_define([m4_pattern_forbid], [])
1341 # m4_pattern_allow(ERE)
1342 # ---------------------
1343 # ... that token also matches the allowed extended regular expression ERE.
1344 # Both used via traces.
1345 m4_define([m4_pattern_allow], [])
1348 ## --------------------------------- ##
1349 ## 11. Dependencies between macros. ##
1350 ## --------------------------------- ##
1353 # m4_before(THIS-MACRO-NAME, CALLED-MACRO-NAME)
1354 # ---------------------------------------------
1355 # Issue a warning if CALLED-MACRO-NAME was called before THIS-MACRO-NAME.
1356 m4_define([m4_before],
1357 [m4_provide_if([$2],
1358 [m4_warn([syntax], [$2 was called before $1])])])
1361 # m4_require(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
1362 # -----------------------------------------------------------
1363 # If NAME-TO-CHECK has never been expanded (actually, if it is not
1364 # m4_provide'd), expand BODY-TO-EXPAND *before* the current macro
1365 # expansion. Once expanded, emit it in _m4_divert_dump. Keep track
1366 # of the m4_require chain in m4_expansion_stack.
1368 # The normal cases are:
1370 # - NAME-TO-CHECK == BODY-TO-EXPAND
1371 # Which you can use for regular macros with or without arguments, e.g.,
1372 # m4_require([AC_PROG_CC], [AC_PROG_CC])
1373 # m4_require([AC_CHECK_HEADERS(limits.h)], [AC_CHECK_HEADERS(limits.h)])
1374 # which is just the same as
1375 # m4_require([AC_PROG_CC])
1376 # m4_require([AC_CHECK_HEADERS(limits.h)])
1378 # - BODY-TO-EXPAND == m4_indir([NAME-TO-CHECK])
1379 # In the case of macros with irregular names. For instance:
1380 # m4_require([AC_LANG_COMPILER(C)], [indir([AC_LANG_COMPILER(C)])])
1381 # which means `if the macro named `AC_LANG_COMPILER(C)' (the parens are
1382 # part of the name, it is not an argument) has not been run, then
1385 # m4_require([AC_LANG_COMPILER(C)], [AC_LANG_COMPILER(C)])
1386 # then m4_require would have tried to expand `AC_LANG_COMPILER(C)', i.e.,
1387 # call the macro `AC_LANG_COMPILER' with `C' as argument.
1389 # You could argue that `AC_LANG_COMPILER', when it receives an argument
1390 # such as `C' should dispatch the call to `AC_LANG_COMPILER(C)'. But this
1391 # `extension' prevents `AC_LANG_COMPILER' from having actual arguments that
1392 # it passes to `AC_LANG_COMPILER(C)'.
1393 m4_define([m4_require],
1394 [m4_ifdef([_m4_expanding($1)],
1395 [m4_fatal([$0: circular dependency of $1])])dnl
1396 m4_ifndef([_m4_divert_dump],
1397 [m4_fatal([$0($1): cannot be used outside of an m4_defun'd macro])])dnl
1400 [_m4_require_call([$1], [$2])])dnl
1404 # _m4_require_call(BODY-TO-EXPAND)
1405 # --------------------------------
1406 # If m4_require decides to expand the body, it calls this macro.
1407 m4_define([_m4_require_call],
1408 [m4_define([_m4_divert_grow], m4_decr(_m4_divert_grow))dnl
1409 m4_divert_push(_m4_divert_grow)dnl
1410 m4_default([$2], [$1])
1414 [$1 is m4_require'd but not m4_defun'd])])dnl
1415 m4_divert(m4_defn([_m4_divert_dump]))dnl
1416 m4_undivert(_m4_divert_grow)dnl
1417 m4_divert_pop(_m4_divert_grow)dnl
1418 m4_define([_m4_divert_grow], m4_incr(_m4_divert_grow))dnl
1424 # The counter for _m4_require_call.
1425 m4_define([_m4_divert_grow], _m4_divert([GROW]))
1428 # m4_expand_once(TEXT, [WITNESS = TEXT])
1429 # --------------------------------------
1430 # If TEXT has never been expanded, expand it *here*. Use WITNESS as
1431 # as a memory that TEXT has already been expanded.
1432 m4_define([m4_expand_once],
1433 [m4_provide_if(m4_ifval([$2], [[$2]], [[$1]]),
1435 [m4_provide(m4_ifval([$2], [[$2]], [[$1]]))[]$1])])
1438 # m4_provide(MACRO-NAME)
1439 # ----------------------
1440 m4_define([m4_provide],
1441 [m4_define([m4_provide($1)])])
1444 # m4_provide_if(MACRO-NAME, IF-PROVIDED, IF-NOT-PROVIDED)
1445 # -------------------------------------------------------
1446 # If MACRO-NAME is provided do IF-PROVIDED, else IF-NOT-PROVIDED.
1447 # The purpose of this macro is to provide the user with a means to
1448 # check macros which are provided without letting her know how the
1449 # information is coded.
1450 m4_define([m4_provide_if],
1451 [m4_ifdef([m4_provide($1)],
1455 ## --------------------- ##
1456 ## 12. Text processing. ##
1457 ## --------------------- ##
1464 m4_define([m4_cr_letters], [abcdefghijklmnopqrstuvwxyz])
1465 m4_define([m4_cr_LETTERS], [ABCDEFGHIJKLMNOPQRSTUVWXYZ])
1466 m4_define([m4_cr_Letters],
1467 m4_defn([m4_cr_letters])dnl
1468 m4_defn([m4_cr_LETTERS])dnl
1474 m4_define([m4_cr_digits], [0123456789])
1477 # m4_cr_symbols1 & m4_cr_symbols2
1478 # -------------------------------
1479 m4_define([m4_cr_symbols1],
1480 m4_defn([m4_cr_Letters])dnl
1483 m4_define([m4_cr_symbols2],
1484 m4_defn([m4_cr_symbols1])dnl
1485 m4_defn([m4_cr_digits])dnl
1489 # m4_re_escape(STRING)
1490 # --------------------
1491 # Escape RE active characters in STRING.
1492 m4_define([m4_re_escape],
1494 [[][*+.?\^$]], [\\\&])])
1499 # Regexp for `[a-zA-Z_0-9]*'
1500 # m4_dquote provides literal [] for the character class.
1501 m4_define([m4_re_string],
1502 m4_dquote(m4_defn([m4_cr_symbols2]))dnl
1509 # Regexp for `[a-zA-Z_][a-zA-Z_0-9]*'
1510 m4_define([m4_re_word],
1511 m4_dquote(m4_defn([m4_cr_symbols1]))dnl
1512 m4_defn([m4_re_string])dnl
1516 # m4_tolower(STRING)
1517 # m4_toupper(STRING)
1518 # ------------------
1519 # These macros lowercase and uppercase strings.
1520 m4_define([m4_tolower],
1521 [m4_translit([$1], m4_defn([m4_cr_LETTERS]), m4_defn([m4_cr_letters]))])
1522 m4_define([m4_toupper],
1523 [m4_translit([$1], m4_defn([m4_cr_letters]), m4_defn([m4_cr_LETTERS]))])
1526 # m4_split(STRING, [REGEXP])
1527 # --------------------------
1529 # Split STRING into an m4 list of quoted elements. The elements are
1530 # quoted with [ and ]. Beginning spaces and end spaces *are kept*.
1531 # Use m4_strip to remove them.
1533 # REGEXP specifies where to split. Default is [\t ]+.
1535 # If STRING is empty, the result is an empty list.
1537 # Pay attention to the m4_changequotes. When m4 reads the definition of
1538 # m4_split, it still has quotes set to [ and ]. Luckily, these are matched
1539 # in the macro body, so the definition is stored correctly.
1541 # Also, notice that $1 is quoted twice, since we want the result to
1542 # be quoted. Then you should understand that the argument of
1543 # patsubst is ``STRING'' (i.e., with additional `` and '').
1545 # This macro is safe on active symbols, i.e.:
1546 # m4_define(active, ACTIVE)
1547 # m4_split([active active ])end
1548 # => [active], [active], []end
1550 m4_define([m4_split],
1551 [m4_ifval([$1], [_m4_split($@)])])
1553 m4_define([_m4_split],
1554 [m4_changequote(``, '')dnl
1555 [dnl Can't use m4_default here instead of m4_if, because m4_default uses
1556 dnl [ and ] as quotes.
1557 m4_bpatsubst(````$1'''',
1558 m4_if(``$2'',, ``[ ]+'', ``$2''),
1560 m4_changequote([, ])])
1564 # m4_flatten(STRING)
1565 # ------------------
1566 # If STRING contains end of lines, replace them with spaces. If there
1567 # are backslashed end of lines, remove them. This macro is safe with
1569 # m4_define(active, ACTIVE)
1570 # m4_flatten([active
1573 # => active activeend
1574 m4_define([m4_flatten],
1575 [m4_translit(m4_bpatsubst([[[$1]]], [\\
1582 # Expands into STRING with tabs and spaces singled out into a single
1583 # space, and removing leading and trailing spaces.
1585 # This macro is robust to active symbols.
1586 # m4_define(active, ACTIVE)
1587 # m4_strip([ active <tab> <tab>active ])end
1588 # => active activeend
1590 # Because we want to preserve active symbols, STRING must be double-quoted.
1592 # Then notice the 2 last patterns: they are in charge of removing the
1593 # leading/trailing spaces. Why not just `[^ ]'? Because they are
1594 # applied to doubly quoted strings, i.e. more or less [[STRING]]. So
1595 # if there is a leading space in STRING, then it is the *third*
1596 # character, since there are two leading `['; equally for the last pattern.
1597 m4_define([m4_strip],
1598 [m4_bpatsubsts([[$1]],
1604 # m4_normalize(STRING)
1605 # --------------------
1606 # Apply m4_flatten and m4_strip to STRING.
1608 # The argument is quoted, so that the macro is robust to active symbols:
1610 # m4_define(active, ACTIVE)
1611 # m4_normalize([ act\
1614 # => active activeend
1616 m4_define([m4_normalize],
1617 [m4_strip(m4_flatten([$1]))])
1621 # m4_join(SEP, ARG1, ARG2...)
1622 # ---------------------------
1623 # Produce ARG1SEPARG2...SEPARGn. Avoid back-to-back SEP when a given ARG
1624 # is the empty string. No expansion is performed on SEP or ARGs.
1626 # Since the number of arguments to join can be arbitrarily long, we
1627 # want to avoid having more than one $@ in the macro definition;
1628 # otherwise, the expansion would require twice the memory of the already
1629 # long list. Hence, m4_join merely looks for the first non-empty element,
1630 # and outputs just that element; while _m4_join looks for all non-empty
1631 # elements, and outputs them following a separator. The final trick to
1632 # note is that we decide between recursing with $0 or _$0 based on the
1633 # nested m4_if ending with `_'.
1634 m4_define([m4_join],
1635 [m4_if([$#], [1], [],
1637 [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift2($@))])])
1638 m4_define([_m4_join],
1639 [m4_if([$#$2], [2], [],
1640 [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift2($@))])])
1643 # m4_append(MACRO-NAME, STRING, [SEPARATOR])
1644 # ------------------------------------------
1645 # Redefine MACRO-NAME to hold its former content plus `SEPARATOR`'STRING'
1646 # at the end. It is valid to use this macro with MACRO-NAME undefined,
1647 # in which case no SEPARATOR is added. Be aware that the criterion is
1648 # `not being defined', and not `not being empty'.
1650 # This macro is robust to active symbols. It can be used to grow
1653 # | m4_define(active, ACTIVE)
1654 # | m4_append([sentence], [This is an])
1655 # | m4_append([sentence], [ active ])
1656 # | m4_append([sentence], [symbol.])
1658 # | m4_undefine([active])dnl
1660 # => This is an ACTIVE symbol.
1661 # => This is an active symbol.
1663 # It can be used to define hooks.
1665 # | m4_define(active, ACTIVE)
1666 # | m4_append([hooks], [m4_define([act1], [act2])])
1667 # | m4_append([hooks], [m4_define([act2], [active])])
1668 # | m4_undefine([active])
1675 m4_define([m4_append],
1677 m4_ifdef([$1], [m4_defn([$1])$3])[$2])])
1679 # m4_append_uniq(MACRO-NAME, STRING, [SEPARATOR])
1680 # -----------------------------------------------
1681 # As `m4_append', but append only if not yet present.
1682 m4_define([m4_append_uniq],
1684 [m4_bmatch([$3]m4_defn([$1])[$3], m4_re_escape([$3$2$3]), [],
1689 # m4_text_wrap(STRING, [PREFIX], [FIRST-PREFIX], [WIDTH])
1690 # -------------------------------------------------------
1691 # Expands into STRING wrapped to hold in WIDTH columns (default = 79).
1692 # If PREFIX is given, each line is prefixed with it. If FIRST-PREFIX is
1693 # specified, then the first line is prefixed with it. As a special case,
1694 # if the length of FIRST-PREFIX is greater than that of PREFIX, then
1695 # FIRST-PREFIX will be left alone on the first line.
1697 # Typical outputs are:
1699 # m4_text_wrap([Short string */], [ ], [/* ], 20)
1700 # => /* Short string */
1702 # m4_text_wrap([Much longer string */], [ ], [/* ], 20)
1706 # m4_text_wrap([Short doc.], [ ], [ --short ], 30)
1707 # => --short Short doc.
1709 # m4_text_wrap([Short doc.], [ ], [ --too-wide ], 30)
1713 # m4_text_wrap([Super long documentation.], [ ], [ --too-wide ], 30)
1718 # FIXME: there is no checking of a longer PREFIX than WIDTH, but do
1719 # we really want to bother with people trying each single corner
1723 # FIXME: handle quadrigraphs correctly, both in TEXT and in FIRST_PREFIX.
1725 # This macro does not leave a trailing space behind the last word,
1726 # what complicates it a bit. The algorithm is stupid simple: all the
1727 # words are preceded by m4_Separator which is defined to empty for the
1728 # first word, and then ` ' (single space) for all the others.
1729 m4_define([m4_text_wrap],
1730 [m4_pushdef([m4_Prefix], [$2])dnl
1731 m4_pushdef([m4_Prefix1], m4_default([$3], [m4_Prefix]))dnl
1732 m4_pushdef([m4_Width], m4_default([$4], 79))dnl
1733 m4_pushdef([m4_Cursor], m4_len(m4_Prefix1))dnl
1734 m4_pushdef([m4_Separator], [])dnl
1736 m4_if(m4_eval(m4_Cursor > m4_len(m4_Prefix)),
1737 1, [m4_define([m4_Cursor], m4_len(m4_Prefix))
1739 m4_foreach_w([m4_Word], [$1],
1740 [m4_define([m4_Cursor], m4_eval(m4_Cursor + m4_len(m4_defn([m4_Word])) + 1))dnl
1741 dnl New line if too long, else insert a space unless it is the first
1743 m4_if(m4_eval(m4_Cursor > m4_Width),
1744 1, [m4_define([m4_Cursor],
1745 m4_eval(m4_len(m4_Prefix) + m4_len(m4_defn([m4_Word])) + 1))]
1747 [m4_Separator])[]dnl
1748 m4_defn([m4_Word])[]dnl
1749 m4_define([m4_Separator], [ ])])dnl
1750 m4_popdef([m4_Separator])dnl
1751 m4_popdef([m4_Cursor])dnl
1752 m4_popdef([m4_Width])dnl
1753 m4_popdef([m4_Prefix1])dnl
1754 m4_popdef([m4_Prefix])dnl
1758 # m4_text_box(MESSAGE, [FRAME-CHARACTER = `-'])
1759 # ---------------------------------------------
1760 m4_define([m4_text_box],
1761 [@%:@@%:@ m4_bpatsubst([$1], [.], m4_if([$2], [], [[-]], [[$2]])) @%:@@%:@
1762 @%:@@%:@ $1 @%:@@%:@
1763 @%:@@%:@ m4_bpatsubst([$1], [.], m4_if([$2], [], [[-]], [[$2]])) @%:@@%:@[]dnl
1769 # Expands to the length of STRING after autom4te converts all quadrigraphs.
1770 m4_define([m4_qlen],
1771 [m4_len(m4_bpatsubsts([[$1]], [@\(<:\|:>\|S|\|%:\)@], [P], [@&t@]))])
1776 # Expands to the net change in the length of STRING from autom4te converting the
1777 # quadrigraphs in STRING. This number is always negative or zero.
1778 m4_define([m4_qdelta],
1779 [m4_eval(m4_qlen([$1]) - m4_len([$1]))])
1783 ## ----------------------- ##
1784 ## 13. Number processing. ##
1785 ## ----------------------- ##
1790 # The sign of the integer A.
1791 m4_define([m4_sign],
1799 # Compare two integer expressions.
1804 [m4_sign(m4_eval([$1 - $2]))])
1810 # Compare the two lists of integers A and B. For instance:
1811 # m4_list_cmp((1, 0), (1)) -> 0
1812 # m4_list_cmp((1, 0), (1, 0)) -> 0
1813 # m4_list_cmp((1, 2), (1, 0)) -> 1
1814 # m4_list_cmp((1, 2, 3), (1, 2)) -> 1
1815 # m4_list_cmp((1, 2, -3), (1, 2)) -> -1
1816 # m4_list_cmp((1, 0), (1, 2)) -> -1
1817 # m4_list_cmp((1), (1, 2)) -> -1
1818 m4_define([m4_list_cmp],
1819 [m4_if([$1$2], [()()], 0,
1820 [$1], [()], [$0((0), [$2])],
1821 [$2], [()], [$0([$1], (0))],
1822 [m4_case(m4_cmp(m4_car$1, m4_car$2),
1825 0, [$0((m4_shift$1), (m4_shift$2))])])])
1829 ## ------------------------ ##
1830 ## 14. Version processing. ##
1831 ## ------------------------ ##
1834 # m4_version_unletter(VERSION)
1835 # ----------------------------
1836 # Normalize beta version numbers with letters to numbers only for comparison.
1838 # Nl -> (N+1).-1.(l#)
1840 #i.e., 2.14a -> 2.15.-1.1, 2.14b -> 2.15.-1.2, etc.
1841 # This macro is absolutely not robust to active macro, it expects
1842 # reasonable version numbers and is valid up to `z', no double letters.
1843 m4_define([m4_version_unletter],
1844 [m4_translit(m4_bpatsubsts([$1],
1845 [\([0-9]+\)\([abcdefghi]\)],
1846 [m4_eval(\1 + 1).-1.\2],
1847 [\([0-9]+\)\([jklmnopqrs]\)],
1848 [m4_eval(\1 + 1).-1.1\2],
1849 [\([0-9]+\)\([tuvwxyz]\)],
1850 [m4_eval(\1 + 1).-1.2\2]),
1851 [abcdefghijklmnopqrstuvwxyz],
1852 [12345678901234567890123456])])
1855 # m4_version_compare(VERSION-1, VERSION-2)
1856 # ----------------------------------------
1857 # Compare the two version numbers and expand into
1858 # -1 if VERSION-1 < VERSION-2
1861 m4_define([m4_version_compare],
1862 [m4_list_cmp((m4_split(m4_version_unletter([$1]), [\.])),
1863 (m4_split(m4_version_unletter([$2]), [\.])))])
1867 # m4_PACKAGE_TARNAME
1868 # m4_PACKAGE_VERSION
1870 # m4_PACKAGE_BUGREPORT
1871 # --------------------
1872 # If m4sugar/version.m4 is present, then define version strings. This
1873 # file is optional, provided by Autoconf but absent in Bison.
1874 m4_sinclude([m4sugar/version.m4])
1877 # m4_version_prereq(VERSION, [IF-OK], [IF-NOT = FAIL])
1878 # ----------------------------------------------------
1879 # Check this Autoconf version against VERSION.
1880 m4_define([m4_version_prereq],
1881 m4_ifdef([m4_PACKAGE_VERSION],
1882 [[m4_if(m4_version_compare(]m4_dquote(m4_defn([m4_PACKAGE_VERSION]))[, [$1]),
1885 [m4_fatal([Autoconf version $1 or higher is required],
1888 [[m4_fatal([m4sugar/version.m4 not found])]]))
1892 ## ------------------- ##
1893 ## 15. File handling. ##
1894 ## ------------------- ##
1897 # It is a real pity that M4 comes with no macros to bind a diversion
1898 # to a file. So we have to deal without, which makes us a lot more
1899 # fragile than we should.
1902 # m4_file_append(FILE-NAME, CONTENT)
1903 # ----------------------------------
1904 m4_define([m4_file_append],
1905 [m4_syscmd([cat >>$1 <<_m4eof
1909 m4_if(m4_sysval, [0], [],
1910 [m4_fatal([$0: cannot write: $1])])])
1914 ## ------------------------ ##
1915 ## 16. Setting M4sugar up. ##
1916 ## ------------------------ ##
1921 # Initialize the m4sugar language.
1922 m4_define([m4_init],
1923 [# All the M4sugar macros start with `m4_', except `dnl' kept as is
1924 # for sake of simplicity.
1925 m4_pattern_forbid([^_?m4_])
1926 m4_pattern_forbid([^dnl$])
1928 # _m4_divert_diversion should be defined:
1929 m4_divert_push([KILL])
1931 # Check the divert push/pop perfect balance.
1932 m4_wrap([m4_divert_pop([])
1933 m4_ifdef([_m4_divert_diversion],
1934 [m4_fatal([$0: unbalanced m4_divert_push:]_m4_divert_n_stack)])[]])