1 divert(-1)# -*- Autoconf -*-
2 # This file is part of Autoconf.
6 # Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free
7 # 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'.
119 m4_rename_m4([builtin])
120 m4_rename_m4([changecom])
121 m4_rename_m4([changequote])
122 m4_undefine([changeword])
123 m4_rename_m4([debugfile])
124 m4_rename_m4([debugmode])
126 m4_undefine([divert])
127 m4_rename_m4([divnum])
128 m4_rename_m4([dumpdef])
129 m4_rename_m4([errprint])
130 m4_rename_m4([esyscmd])
132 m4_rename_m4([format])
133 m4_rename_m4([ifdef])
134 m4_rename([ifelse], [m4_if])
135 m4_undefine([include])
137 m4_rename_m4([index])
138 m4_rename_m4([indir])
140 m4_rename([m4exit], [m4_exit])
141 m4_rename([m4wrap], [m4_wrap])
142 m4_ifdef([mkstemp],dnl added in M4 1.4.8
143 [m4_rename_m4([mkstemp])
144 m4_copy([m4_mkstemp], [m4_maketemp])
145 m4_undefine([maketemp])],
146 [m4_rename_m4([maketemp])
147 m4_copy([m4_maketemp], [m4_mkstemp])])
148 m4_rename([patsubst], [m4_bpatsubst])
149 m4_undefine([popdef])
150 m4_rename_m4([pushdef])
151 m4_rename([regexp], [m4_bregexp])
152 m4_rename_m4([shift])
153 m4_undefine([sinclude])
154 m4_rename_m4([substr])
155 m4_rename_m4([symbols])
156 m4_rename_m4([syscmd])
157 m4_rename_m4([sysval])
158 m4_rename_m4([traceoff])
159 m4_rename_m4([traceon])
160 m4_rename_m4([translit])
161 m4_undefine([undivert])
164 ## ------------------- ##
165 ## 2. Error messages. ##
166 ## ------------------- ##
171 m4_define([m4_location],
177 # Same as `errprint', but with the missing end of line.
178 m4_define([m4_errprintn],
186 m4_define([m4_warning],
187 [m4_errprintn(m4_location[: warning: $1])])
190 # m4_fatal(MSG, [EXIT-STATUS])
191 # ----------------------------
193 m4_define([m4_fatal],
194 [m4_errprintn(m4_location[: error: $1])dnl
195 m4_expansion_stack_dump()dnl
196 m4_exit(m4_if([$2],, 1, [$2]))])
199 # m4_assert(EXPRESSION, [EXIT-STATUS = 1])
200 # ----------------------------------------
201 # This macro ensures that EXPRESSION evaluates to true, and exits if
202 # EXPRESSION evaluates to false.
203 m4_define([m4_assert],
204 [m4_if(m4_eval([$1]), 0,
205 [m4_fatal([assert failed: $1], [$2])])])
214 # _m4_warn(CATEGORY, MESSAGE, STACK-TRACE)
215 # ----------------------------------------
216 # Report a MESSAGE to the user if the CATEGORY of warnings is enabled.
217 # This is for traces only.
218 # The STACK-TRACE is a \n-separated list of "LOCATION: MESSAGE".
220 # Within m4, the macro is a no-op. This macro really matters
221 # when autom4te post-processes the trace output.
222 m4_define([_m4_warn], [])
225 # m4_warn(CATEGORY, MESSAGE)
226 # --------------------------
227 # Report a MESSAGE to the user if the CATEGORY of warnings is enabled.
229 [_m4_warn([$1], [$2],
230 m4_ifdef([m4_expansion_stack],
231 [m4_defn([m4_expansion_stack])
232 m4_location[: the top level]]))dnl
237 ## ------------------- ##
238 ## 4. File inclusion. ##
239 ## ------------------- ##
242 # We also want to neutralize include (and sinclude for symmetry),
243 # but we want to extend them slightly: warn when a file is included
244 # several times. This is, in general, a dangerous operation, because
245 # too many people forget to quote the first argument of m4_define.
247 # For instance in the following case:
248 # m4_define(foo, [bar])
249 # then a second reading will turn into
250 # m4_define(bar, [bar])
251 # which is certainly not what was meant.
253 # m4_include_unique(FILE)
254 # -----------------------
255 # Declare that the FILE was loading; and warn if it has already
257 m4_define([m4_include_unique],
258 [m4_ifdef([m4_include($1)],
259 [m4_warn([syntax], [file `$1' included several times])])dnl
260 m4_define([m4_include($1)])])
265 # Like the builtin include, but warns against multiple inclusions.
266 m4_define([m4_include],
267 [m4_include_unique([$1])dnl
268 m4_builtin([include], [$1])])
273 # Like the builtin sinclude, but warns against multiple inclusions.
274 m4_define([m4_sinclude],
275 [m4_include_unique([$1])dnl
276 m4_builtin([sinclude], [$1])])
280 ## ------------------------------------ ##
281 ## 5. Additional branching constructs. ##
282 ## ------------------------------------ ##
284 # Both `m4_ifval' and `m4_ifset' tests against the empty string. The
285 # difference is that `m4_ifset' is specialized on macros.
287 # In case of arguments of macros, eg. $1, it makes little difference.
288 # In the case of a macro `FOO', you don't want to check `m4_ifval(FOO,
289 # TRUE)', because if `FOO' expands with commas, there is a shifting of
290 # the arguments. So you want to run `m4_ifval([FOO])', but then you just
291 # compare the *string* `FOO' against `', which, of course fails.
293 # So you want the variation `m4_ifset' that expects a macro name as $1.
294 # If this macro is both defined and defined to a non empty value, then
298 # m4_ifval(COND, [IF-TRUE], [IF-FALSE])
299 # -------------------------------------
300 # If COND is not the empty string, expand IF-TRUE, otherwise IF-FALSE.
301 # Comparable to m4_ifdef.
302 m4_define([m4_ifval],
303 [m4_if([$1], [], [$3], [$2])])
308 # If TEXT is not empty, return TEXT and a new line, otherwise nothing.
316 # m4_ifvaln(COND, [IF-TRUE], [IF-FALSE])
317 # --------------------------------------
318 # Same as `m4_ifval', but add an extra newline to IF-TRUE or IF-FALSE
319 # unless that argument is empty.
320 m4_define([m4_ifvaln],
326 # m4_ifset(MACRO, [IF-TRUE], [IF-FALSE])
327 # --------------------------------------
328 # If MACRO has no definition, or of its definition is the empty string,
329 # expand IF-FALSE, otherwise IF-TRUE.
330 m4_define([m4_ifset],
332 [m4_ifval(m4_defn([$1]), [$2], [$3])],
336 # m4_ifndef(NAME, [IF-NOT-DEFINED], [IF-DEFINED])
337 # -----------------------------------------------
338 m4_define([m4_ifndef],
339 [m4_ifdef([$1], [$3], [$2])])
342 # m4_case(SWITCH, VAL1, IF-VAL1, VAL2, IF-VAL2, ..., DEFAULT)
343 # -----------------------------------------------------------
358 # All the values are optional, and the macro is robust to active
359 # symbols properly quoted.
365 [$0([$1], m4_shift3($@))])])
368 # m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT)
369 # -----------------------------------------------------
374 # elif (SWITCH =~ RE2)
381 # All the values are optional, and the macro is robust to active symbols
383 m4_define([m4_bmatch],
384 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
385 [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
387 [m4_if(m4_bregexp([$1], [$2]), -1, [$0([$1], m4_shift3($@))],
394 # Manipulate m4 lists.
395 m4_define([m4_car], [[$1]])
397 [m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])],
399 [m4_dquote(m4_shift($@))])])
402 # m4_cond(TEST1, VAL1, IF-VAL1, TEST2, VAL2, IF-VAL2, ..., [DEFAULT])
403 # -------------------------------------------------------------------
404 # Similar to m4_if, except that each TEST is expanded when encountered.
405 # If the expansion of TESTn matches the string VALn, the result is IF-VALn.
406 # The result is DEFAULT if no tests passed. This macro allows
407 # short-circuiting of expensive tests, where it pays to arrange quick
408 # filter tests to run first.
410 # For an example, consider a previous implementation of _AS_QUOTE_IFELSE:
412 # m4_if(m4_index([$1], [\]), [-1], [$2],
413 # m4_eval(m4_index([$1], [\\]) >= 0), [1], [$2],
414 # m4_eval(m4_index([$1], [\$]) >= 0), [1], [$2],
415 # m4_eval(m4_index([$1], [\`]) >= 0), [1], [$3],
416 # m4_eval(m4_index([$1], [\"]) >= 0), [1], [$3],
419 # Here, m4_index is computed 5 times, and m4_eval 4, even if $1 contains
420 # no backslash. It is more efficient to do:
422 # m4_cond([m4_index([$1], [\])], [-1], [$2],
423 # [m4_eval(m4_index([$1], [\\]) >= 0)], [1], [$2],
424 # [m4_eval(m4_index([$1], [\$]) >= 0)], [1], [$2],
425 # [m4_eval(m4_index([$1], [\`]) >= 0)], [1], [$3],
426 # [m4_eval(m4_index([$1], [\"]) >= 0)], [1], [$3],
429 # In the common case of $1 with no backslash, only one m4_index expansion
430 # occurs, and m4_eval is avoided altogether.
432 [m4_if([$#], [0], [m4_fatal([$0: cannot be called without arguments])],
434 [$#], [2], [m4_fatal([$0: missing an argument])],
435 [m4_if($1, [$2], [$3], [$0(m4_shift3($@))])])])
438 # m4_map(MACRO, LIST)
439 # -------------------
440 # Invoke MACRO($1), MACRO($2) etc. where $1, $2... are the elements
441 # of LIST (which can be lists themselves, for multiple arguments MACROs).
442 m4_define([m4_fst], [$1])
444 [m4_if([$2], [[]], [],
445 [_m4_map([$1], [$2])])])
448 [$1(m4_fst($2))[]_m4_map([$1], m4_cdr($2))])])
451 # m4_map_sep(MACRO, SEPARATOR, LIST)
452 # ----------------------------------
453 # Invoke MACRO($1), SEPARATOR, MACRO($2), ..., MACRO($N) where $1, $2... $N
454 # are the elements of LIST (which can be lists themselves, for multiple
456 m4_define([m4_map_sep],
457 [m4_if([$3], [[]], [],
458 [$1(m4_fst($3))[]_m4_map([$2[]$1], m4_cdr($3))])])
461 ## ---------------------------------------- ##
462 ## 6. Enhanced version of some primitives. ##
463 ## ---------------------------------------- ##
465 # m4_bpatsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...)
466 # ----------------------------------------------------
474 # All the values are optional, and the macro is robust to active symbols
477 # I would have liked to name this macro `m4_bpatsubst', unfortunately,
478 # due to quotation problems, I need to double quote $1 below, therefore
479 # the anchors are broken :( I can't let users be trapped by that.
481 # Recall that m4_shift3 always results in an argument. Hence, we need
482 # to distinguish between a final deletion vs. ending recursion.
483 m4_define([m4_bpatsubsts],
484 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
485 [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
486 [$#], 2, [m4_builtin([patsubst], $@)],
487 [_$0($@m4_if(m4_eval($# & 1), 0, [,]))])])
488 m4_define([_m4_bpatsubsts],
489 [m4_if([$#], 2, [$1],
490 [$0(m4_builtin([patsubst], [[$1]], [$2], [$3]),
497 # This macro invokes all its arguments (in sequence, of course). It is
498 # useful for making your macros more structured and readable by dropping
499 # unnecessary dnl's and have the macros indented properly.
503 [$1[]m4_do(m4_shift($@))])])
506 # m4_define_default(MACRO, VALUE)
507 # -------------------------------
508 # If MACRO is undefined, set it to VALUE.
509 m4_define([m4_define_default],
510 [m4_ifndef([$1], [m4_define($@)])])
513 # m4_default(EXP1, EXP2)
514 # ----------------------
515 # Returns EXP1 if non empty, otherwise EXP2.
517 # This macro is called on hot paths, so inline the contents of m4_ifval,
518 # for one less round of expansion.
519 m4_define([m4_default],
520 [m4_if([$1], [], [$2], [$1])])
525 # Like the original, except don't tolerate popping something which is
526 # undefined, and only support one argument.
528 # This macro is called frequently, so minimize the amount of additional
529 # expansions by skipping m4_ifndef.
532 [m4_fatal([$0: undefined macro: $1])])]dnl
533 [m4_builtin([defn], $@)])
536 # _m4_dumpdefs_up(NAME)
537 # ---------------------
538 m4_define([_m4_dumpdefs_up],
540 [m4_pushdef([_m4_dumpdefs], m4_defn([$1]))dnl
543 _m4_dumpdefs_up([$1])])])
546 # _m4_dumpdefs_down(NAME)
547 # -----------------------
548 m4_define([_m4_dumpdefs_down],
549 [m4_ifdef([_m4_dumpdefs],
550 [m4_pushdef([$1], m4_defn([_m4_dumpdefs]))dnl
551 m4_popdef([_m4_dumpdefs])dnl
552 _m4_dumpdefs_down([$1])])])
557 # Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its
558 # value stack (most recent displayed first).
559 m4_define([m4_dumpdefs],
560 [_m4_dumpdefs_up([$1])dnl
561 _m4_dumpdefs_down([$1])])
566 # Like the original, except don't tolerate popping something which is
567 # undefined, and only support one argument.
569 # This macro is called frequently, so minimize the amount of additional
570 # expansions by skipping m4_ifndef.
571 m4_define([m4_popdef],
573 [m4_fatal([$0: undefined macro: $1])])]dnl
574 [m4_builtin([popdef], $@)])
579 # Return ARGS as a single argument. Any whitespace after unquoted commas
582 # It is important to realize the difference between `m4_quote(exp)' and
583 # `[exp]': in the first case you obtain the quoted *result* of the
584 # expansion of EXP, while in the latter you just obtain the string
586 m4_define([m4_quote], [[$*]])
591 # Return ARGS as a quoted list of quoted arguments.
592 m4_define([m4_dquote], [[$@]])
597 # Return the result of ignoring all quotes in STRING and invoking the
598 # macros it contains. Amongst other things, this is useful for enabling
599 # macro invocations inside strings with [] blocks (for instance regexps
601 m4_define([m4_noquote],
602 [m4_changequote(-=<{,}>=-)$1-=<{}>=-m4_changequote([,])])
607 # Returns ... shifted N times. Useful for recursive "varargs" constructs.
608 m4_define([m4_shiftn],
609 [m4_assert(0 <= $1 && $1 < $#)dnl
612 m4_define([_m4_shiftn],
615 [_m4_shiftn(m4_eval([$1]-1), m4_shift(m4_shift($@)))])])
620 # Returns ... shifted twice, and three times. Faster than m4_shiftn.
621 m4_define([m4_shift2], [m4_shift(m4_shift($@))])
622 m4_define([m4_shift3], [m4_shift(m4_shift(m4_shift($@)))])
626 # Like the original, except don't tolerate undefining something which is
627 # undefined, and only support one argument.
629 # This macro is called frequently, so minimize the amount of additional
630 # expansions by skipping m4_ifndef.
631 m4_define([m4_undefine],
633 [m4_fatal([$0: undefined macro: $1])])]dnl
634 [m4_builtin([undefine], $@)])
637 ## -------------------------- ##
638 ## 7. Implementing m4 loops. ##
639 ## -------------------------- ##
642 # m4_for(VARIABLE, FIRST, LAST, [STEP = +/-1], EXPRESSION)
643 # --------------------------------------------------------
644 # Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO with
645 # increments of STEP.
646 # Both limits are included, and bounds are checked for consistency.
647 # The algorithm is robust to indirect VARIABLE names.
649 [m4_pushdef([$1], m4_eval([$2]))dnl
650 m4_if(m4_eval(([$3]) > m4_defn([$1])), 1,
651 [m4_pushdef([_m4_step], m4_eval(m4_default([$4], 1)))dnl
652 m4_assert(_m4_step > 0)dnl
653 _m4_for([$1], m4_eval((([$3]) - m4_defn([$1]))
654 / _m4_step * _m4_step + m4_defn([$1])),
656 m4_eval(([$3]) < m4_defn([$1])), 1,
657 [m4_pushdef([_m4_step], m4_eval(m4_default([$4], -1)))dnl
658 m4_assert(_m4_step < 0)dnl
659 _m4_for([$1], m4_eval((m4_defn([$1]) - ([$3]))
660 / -(_m4_step) * _m4_step + m4_defn([$1])),
662 [m4_pushdef(_m4_step,[])dnl
664 m4_popdef([_m4_step])dnl
668 # _m4_for(VARIABLE, LAST, STEP, EXPRESSION)
669 # -----------------------------------------
670 # Core of the loop, no consistency checks, all arguments are plain numbers.
673 m4_if(m4_defn([$1]), [$2], [],
674 [m4_define([$1], m4_eval(m4_defn([$1])+[$3]))$0($@)])])
677 # Implementing `foreach' loops in m4 is much more tricky than it may
678 # seem. For example, the old M4 1.4.4 manual had an incorrect example,
679 # which looked like this (when translated to m4sugar):
681 # | # foreach(VAR, (LIST), STMT)
682 # | m4_define([foreach],
683 # | [m4_pushdef([$1])_foreach([$1], [$2], [$3])m4_popdef([$1])])
684 # | m4_define([_arg1], [$1])
685 # | m4_define([_foreach],
686 # | [m4_if([$2], [()], ,
687 # | [m4_define([$1], _arg1$2)$3[]_foreach([$1], (m4_shift$2), [$3])])])
689 # But then if you run
694 # | foreach([f], [([a], [(b], [c)])], [echo f
702 # which is not what is expected.
704 # Of course the problem is that many quotes are missing. So you add
705 # plenty of quotes at random places, until you reach the expected
706 # result. Alternatively, if you are a quoting wizard, you directly
707 # reach the following implementation (but if you really did, then
708 # apply to the maintenance of m4sugar!).
710 # | # foreach(VAR, (LIST), STMT)
711 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
712 # | m4_define([_arg1], [[$1]])
713 # | m4_define([_foreach],
714 # | [m4_if($2, [()], ,
715 # | [m4_define([$1], [_arg1$2])$3[]_foreach([$1], [(m4_shift$2)], [$3])])])
717 # which this time answers
727 # With a better look, you realize that the parens are more a pain than
728 # a help: since anyway you need to quote properly the list, you end up
729 # with always using an outermost pair of parens and an outermost pair
730 # of quotes. Rejecting the parens both eases the implementation, and
731 # simplifies the use:
733 # | # foreach(VAR, (LIST), STMT)
734 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
735 # | m4_define([_arg1], [$1])
736 # | m4_define([_foreach],
738 # | [m4_define([$1], [_arg1($2)])$3[]_foreach([$1], [m4_shift($2)], [$3])])])
741 # Now, just replace the `$2' with `m4_quote($2)' in the outer `m4_if'
742 # to improve robustness, and you come up with a nice implementation
743 # that doesn't require extra parenthesis in the user's LIST.
745 # But wait - now the algorithm is quadratic, because every recursion of
746 # the algorithm keeps the entire LIST and merely adds another m4_shift to
747 # the quoted text. If the user has a lot of elements in LIST, you can
748 # bring the system to its knees with the memory m4 then requires, or trip
749 # the m4 --nesting-limit recursion factor. The only way to avoid
750 # quadratic growth is ensure m4_shift is expanded prior to the recursion.
751 # Hence the design below.
753 # The M4 manual now includes a chapter devoted to this issue, with
754 # the lessons learned from m4sugar.
757 # m4_foreach(VARIABLE, LIST, EXPRESSION)
758 # --------------------------------------
760 # Expand EXPRESSION assigning each value of the LIST to VARIABLE.
761 # LIST should have the form `item_1, item_2, ..., item_n', i.e. the
762 # whole list must *quoted*. Quote members too if you don't want them
765 # This macro is robust to active symbols:
766 # | m4_define(active, [ACT, IVE])
767 # | m4_foreach(Var, [active, active], [-Var-])
768 # => -ACT--IVE--ACT--IVE-
770 # | m4_foreach(Var, [[active], [active]], [-Var-])
771 # => -ACT, IVE--ACT, IVE-
773 # | m4_foreach(Var, [[[active]], [[active]]], [-Var-])
774 # => -active--active-
776 # This macro is called frequently, so avoid extra expansions such as
778 m4_define([m4_foreach],
779 [m4_pushdef([$1])_$0($@)m4_popdef([$1])])
781 m4_define([_m4_foreach],
783 [m4_define([$1], m4_car($2))$3[]$0([$1], m4_cdr($2), [$3])])])
786 # m4_foreach_w(VARIABLE, LIST, EXPRESSION)
787 # ----------------------------------------
789 # Like m4_foreach, but the list is whitespace separated.
791 # This macro is robust to active symbols:
792 # m4_foreach_w([Var], [ active
795 # => -active--b--active-end
797 m4_define([m4_foreach_w],
798 [m4_foreach([$1], m4_split(m4_normalize([$2]), [ ]), [$3])])
802 ## --------------------------- ##
803 ## 8. More diversion support. ##
804 ## --------------------------- ##
807 # _m4_divert(DIVERSION-NAME or NUMBER)
808 # ------------------------------------
809 # If DIVERSION-NAME is the name of a diversion, return its number,
810 # otherwise if it is a NUMBER return it.
811 m4_define([_m4_divert],
812 [m4_ifdef([_m4_divert($1)],
813 [m4_indir([_m4_divert($1)])],
816 # KILL is only used to suppress output.
817 m4_define([_m4_divert(KILL)], -1)
819 # The empty diversion name is a synonym for 0.
820 m4_define([_m4_divert()], 0)
825 # Print m4_divert_stack with newline prepended, if it's nonempty.
826 m4_define([_m4_divert_n_stack],
827 [m4_ifdef([m4_divert_stack], [
828 m4_defn([m4_divert_stack])])])
831 # m4_divert(DIVERSION-NAME)
832 # -------------------------
833 # Change the diversion stream to DIVERSION-NAME.
834 m4_define([m4_divert],
835 [m4_define([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
836 m4_builtin([divert], _m4_divert([$1]))dnl
840 # m4_divert_push(DIVERSION-NAME)
841 # ------------------------------
842 # Change the diversion stream to DIVERSION-NAME, while stacking old values.
843 m4_define([m4_divert_push],
844 [m4_pushdef([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
845 m4_pushdef([_m4_divert_diversion], [$1])dnl
846 m4_builtin([divert], _m4_divert([$1]))dnl
850 # m4_divert_pop([DIVERSION-NAME])
851 # -------------------------------
852 # Change the diversion stream to its previous value, unstacking it.
853 # If specified, verify we left DIVERSION-NAME.
854 # When we pop the last value from the stack, we divert to -1.
855 m4_define([m4_divert_pop],
856 [m4_ifndef([_m4_divert_diversion],
857 [m4_fatal([too many m4_divert_pop])])dnl
859 [$1], m4_defn([_m4_divert_diversion]), [],
860 [m4_fatal([$0($1): diversion mismatch: ]_m4_divert_n_stack)])dnl
861 m4_popdef([m4_divert_stack])dnl
862 m4_popdef([_m4_divert_diversion])dnl
864 m4_ifdef([_m4_divert_diversion],
865 [_m4_divert(m4_defn([_m4_divert_diversion]))],
870 # m4_divert_text(DIVERSION-NAME, CONTENT)
871 # ---------------------------------------
872 # Output CONTENT into DIVERSION-NAME (which may be a number actually).
873 # An end of line is appended for free to CONTENT.
874 m4_define([m4_divert_text],
875 [m4_divert_push([$1])dnl
877 m4_divert_pop([$1])dnl
881 # m4_divert_once(DIVERSION-NAME, CONTENT)
882 # ---------------------------------------
883 # Output CONTENT into DIVERSION-NAME once, if not already there.
884 # An end of line is appended for free to CONTENT.
885 m4_define([m4_divert_once],
886 [m4_expand_once([m4_divert_text([$1], [$2])])])
889 # m4_undivert(DIVERSION-NAME)
890 # ---------------------------
891 # Undivert DIVERSION-NAME.
892 m4_define([m4_undivert],
893 [m4_builtin([undivert], _m4_divert([$1]))])
896 ## -------------------------------------------- ##
897 ## 8. Defining macros with bells and whistles. ##
898 ## -------------------------------------------- ##
900 # `m4_defun' is basically `m4_define' but it equips the macro with the
901 # needed machinery for `m4_require'. A macro must be m4_defun'd if
902 # either it is m4_require'd, or it m4_require's.
904 # Two things deserve attention and are detailed below:
905 # 1. Implementation of m4_require
906 # 2. Keeping track of the expansion stack
908 # 1. Implementation of m4_require
909 # ===============================
911 # Of course m4_defun AC_PROVIDE's the macro, so that a macro which has
912 # been expanded is not expanded again when m4_require'd, but the
913 # difficult part is the proper expansion of macros when they are
916 # The implementation is based on two ideas, (i) using diversions to
917 # prepare the expansion of the macro and its dependencies (by Franc,ois
918 # Pinard), and (ii) expand the most recently m4_require'd macros _after_
919 # the previous macros (by Axel Thimm).
922 # The first idea: why use diversions?
923 # -----------------------------------
925 # When a macro requires another, the other macro is expanded in new
926 # diversion, GROW. When the outer macro is fully expanded, we first
927 # undivert the most nested diversions (GROW - 1...), and finally
928 # undivert GROW. To understand why we need several diversions,
929 # consider the following example:
931 # | m4_defun([TEST1], [Test...REQUIRE([TEST2])1])
932 # | m4_defun([TEST2], [Test...REQUIRE([TEST3])2])
933 # | m4_defun([TEST3], [Test...3])
935 # Because m4_require is not required to be first in the outer macros, we
936 # must keep the expansions of the various levels of m4_require separated.
937 # Right before executing the epilogue of TEST1, we have:
944 # Finally the epilogue of TEST1 undiverts GROW - 2, GROW - 1, and
945 # GROW into the regular flow, BODY.
950 # BODY: Test...3; Test...2; Test...1
952 # (The semicolons are here for clarification, but of course are not
953 # emitted.) This is what Autoconf 2.0 (I think) to 2.13 (I'm sure)
957 # The second idea: first required first out
958 # -----------------------------------------
960 # The natural implementation of the idea above is buggy and produces
961 # very surprising results in some situations. Let's consider the
962 # following example to explain the bug:
964 # | m4_defun([TEST1], [REQUIRE([TEST2a])REQUIRE([TEST2b])])
965 # | m4_defun([TEST2a], [])
966 # | m4_defun([TEST2b], [REQUIRE([TEST3])])
967 # | m4_defun([TEST3], [REQUIRE([TEST2a])])
972 # The dependencies between the macros are:
975 # / \ is m4_require'd by
976 # / \ left -------------------- right
979 # If you strictly apply the rules given in the previous section you get:
982 # GROW - 1: TEST2a; TEST2b
986 # (TEST2a, although required by TEST3 is not expanded in GROW - 3
987 # because is has already been expanded before in GROW - 1, so it has
988 # been AC_PROVIDE'd, so it is not expanded again) so when you undivert
989 # the stack of diversions, you get:
994 # BODY: TEST3; TEST2a; TEST2b; TEST1
996 # i.e., TEST2a is expanded after TEST3 although the latter required the
999 # Starting from 2.50, we use an implementation provided by Axel Thimm.
1000 # The idea is simple: the order in which macros are emitted must be the
1001 # same as the one in which macros are expanded. (The bug above can
1002 # indeed be described as: a macro has been AC_PROVIDE'd before its
1003 # dependent, but it is emitted after: the lack of correlation between
1004 # emission and expansion order is guilty).
1006 # How to do that? You keep the stack of diversions to elaborate the
1007 # macros, but each time a macro is fully expanded, emit it immediately.
1009 # In the example above, when TEST2a is expanded, but it's epilogue is
1010 # not run yet, you have:
1014 # GROW: Elaboration of TEST1
1017 # The epilogue of TEST2a emits it immediately:
1021 # GROW: Elaboration of TEST1
1024 # TEST2b then requires TEST3, so right before the epilogue of TEST3, you
1028 # GROW - 1: Elaboration of TEST2b
1029 # GROW: Elaboration of TEST1
1032 # The epilogue of TEST3 emits it:
1035 # GROW - 1: Elaboration of TEST2b
1036 # GROW: Elaboration of TEST1
1037 # BODY: TEST2a; TEST3
1039 # TEST2b is now completely expanded, and emitted:
1043 # GROW: Elaboration of TEST1
1044 # BODY: TEST2a; TEST3; TEST2b
1046 # and finally, TEST1 is finished and emitted:
1051 # BODY: TEST2a; TEST3; TEST2b: TEST1
1053 # The idea is simple, but the implementation is a bit evolved. If you
1054 # are like me, you will want to see the actual functioning of this
1055 # implementation to be convinced. The next section gives the full
1059 # The Axel Thimm implementation at work
1060 # -------------------------------------
1062 # We consider the macros above, and this configure.ac:
1067 # You should keep the definitions of _m4_defun_pro, _m4_defun_epi, and
1068 # m4_require at hand to follow the steps.
1070 # This implements tries not to assume that the current diversion is
1071 # BODY, so as soon as a macro (m4_defun'd) is expanded, we first
1072 # record the current diversion under the name _m4_divert_dump (denoted
1073 # DUMP below for short). This introduces an important difference with
1074 # the previous versions of Autoconf: you cannot use m4_require if you
1075 # are not inside an m4_defun'd macro, and especially, you cannot
1076 # m4_require directly from the top level.
1078 # We have not tried to simulate the old behavior (better yet, we
1079 # diagnose it), because it is too dangerous: a macro m4_require'd from
1080 # the top level is expanded before the body of `configure', i.e., before
1081 # any other test was run. I let you imagine the result of requiring
1082 # AC_STDC_HEADERS for instance, before AC_PROG_CC was actually run....
1084 # After AC_INIT was run, the current diversion is BODY.
1087 # diversion stack: BODY |-
1089 # * TEST1 is expanded
1090 # The prologue of TEST1 sets _m4_divert_dump, which is the diversion
1091 # where the current elaboration will be dumped, to the current
1092 # diversion. It also m4_divert_push to GROW, where the full
1093 # expansion of TEST1 and its dependencies will be elaborated.
1096 # diversions: GROW, BODY |-
1098 # * TEST1 requires TEST2a
1099 # _m4_require_call m4_divert_pushes another temporary diversion,
1100 # GROW - 1, and expands TEST2a in there.
1104 # diversions: GROW - 1, GROW, BODY |-
1105 # Than the content of the temporary diversion is moved to DUMP and the
1106 # temporary diversion is popped.
1109 # diversions: GROW, BODY |-
1111 # * TEST1 requires TEST2b
1112 # Again, _m4_require_call pushes GROW - 1 and heads to expand TEST2b.
1115 # diversions: GROW - 1, GROW, BODY |-
1117 # * TEST2b requires TEST3
1118 # _m4_require_call pushes GROW - 2 and expands TEST3 here.
1119 # (TEST3 requires TEST2a, but TEST2a has already been m4_provide'd, so
1124 # diversions: GROW - 2, GROW - 1, GROW, BODY |-
1125 # Than the diversion is appended to DUMP, and popped.
1127 # BODY: TEST2a; TEST3
1128 # diversions: GROW - 1, GROW, BODY |-
1130 # * TEST1 requires TEST2b (contd.)
1131 # The content of TEST2b is expanded...
1133 # BODY: TEST2a; TEST3
1135 # diversions: GROW - 1, GROW, BODY |-
1136 # ... and moved to DUMP.
1138 # BODY: TEST2a; TEST3; TEST2b
1139 # diversions: GROW, BODY |-
1141 # * TEST1 is expanded: epilogue
1142 # TEST1's own content is in GROW...
1144 # BODY: TEST2a; TEST3; TEST2b
1146 # diversions: BODY |-
1147 # ... and it's epilogue moves it to DUMP and then undefines DUMP.
1149 # BODY: TEST2a; TEST3; TEST2b; TEST1
1150 # diversions: BODY |-
1153 # 2. Keeping track of the expansion stack
1154 # =======================================
1156 # When M4 expansion goes wrong it is often extremely hard to find the
1157 # path amongst macros that drove to the failure. What is needed is
1158 # the stack of macro `calls'. One could imagine that GNU M4 would
1159 # maintain a stack of macro expansions, unfortunately it doesn't, so
1160 # we do it by hand. This is of course extremely costly, but the help
1161 # this stack provides is worth it. Nevertheless to limit the
1162 # performance penalty this is implemented only for m4_defun'd macros,
1163 # not for define'd macros.
1165 # The scheme is simplistic: each time we enter an m4_defun'd macros,
1166 # we prepend its name in m4_expansion_stack, and when we exit the
1167 # macro, we remove it (thanks to pushdef/popdef).
1169 # In addition, we want to detect circular m4_require dependencies.
1170 # Each time we expand a macro FOO we define _m4_expanding(FOO); and
1171 # m4_require(BAR) simply checks whether _m4_expanding(BAR) is defined.
1174 # m4_expansion_stack_push(TEXT)
1175 # -----------------------------
1176 m4_define([m4_expansion_stack_push],
1177 [m4_pushdef([m4_expansion_stack],
1178 [$1]m4_ifdef([m4_expansion_stack], [
1179 m4_defn([m4_expansion_stack])]))])
1182 # m4_expansion_stack_pop
1183 # ----------------------
1184 m4_define([m4_expansion_stack_pop],
1185 [m4_popdef([m4_expansion_stack])])
1188 # m4_expansion_stack_dump
1189 # -----------------------
1190 # Dump the expansion stack.
1191 m4_define([m4_expansion_stack_dump],
1192 [m4_ifdef([m4_expansion_stack],
1193 [m4_errprintn(m4_defn([m4_expansion_stack]))])dnl
1194 m4_errprintn(m4_location[: the top level])])
1199 # This diversion is used by the m4_defun/m4_require machinery. It is
1200 # important to keep room before GROW because for each nested
1201 # AC_REQUIRE we use an additional diversion (i.e., two m4_require's
1202 # will use GROW - 2. More than 3 levels has never seemed to be
1207 # m4_require'd code, 2 level deep
1209 # m4_require'd code, 1 level deep
1211 # m4_defun'd macros are elaborated here.
1213 m4_define([_m4_divert(GROW)], 10000)
1216 # _m4_defun_pro(MACRO-NAME)
1217 # -------------------------
1218 # The prologue for Autoconf macros.
1219 m4_define([_m4_defun_pro],
1220 [m4_ifndef([m4_expansion_stack], [_m4_defun_pro_outer[]])dnl
1221 m4_expansion_stack_push(m4_defn([m4_location($1)])[: $1 is expanded from...])dnl
1222 m4_pushdef([_m4_expanding($1)])dnl
1225 m4_define([_m4_defun_pro_outer],
1226 [m4_copy([_m4_divert_diversion], [_m4_divert_dump])dnl
1227 m4_divert_push([GROW])dnl
1230 # _m4_defun_epi(MACRO-NAME)
1231 # -------------------------
1232 # The Epilogue for Autoconf macros. MACRO-NAME only helps tracing
1233 # the PRO/EPI pairs.
1234 m4_define([_m4_defun_epi],
1235 [m4_popdef([_m4_expanding($1)])dnl
1236 m4_expansion_stack_pop()dnl
1237 m4_ifndef([m4_expansion_stack], [_m4_defun_epi_outer[]])dnl
1241 m4_define([_m4_defun_epi_outer],
1242 [m4_undefine([_m4_divert_dump])dnl
1243 m4_divert_pop([GROW])dnl
1244 m4_undivert([GROW])dnl
1248 # m4_defun(NAME, EXPANSION)
1249 # -------------------------
1250 # Define a macro which automatically provides itself. Add machinery
1251 # so the macro automatically switches expansion to the diversion
1252 # stack if it is not already using it. In this case, once finished,
1253 # it will bring back all the code accumulated in the diversion stack.
1254 # This, combined with m4_require, achieves the topological ordering of
1255 # macros. We don't use this macro to define some frequently called
1256 # macros that are not involved in ordering constraints, to save m4
1258 m4_define([m4_defun],
1259 [m4_define([m4_location($1)], m4_location)dnl
1261 [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])
1264 # m4_defun_once(NAME, EXPANSION)
1265 # ------------------------------
1266 # As m4_defun, but issues the EXPANSION only once, and warns if used
1268 m4_define([m4_defun_once],
1269 [m4_define([m4_location($1)], m4_location)dnl
1271 [m4_provide_if([$1],
1272 [m4_warn([syntax], [$1 invoked multiple times])],
1273 [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])])
1276 # m4_pattern_forbid(ERE, [WHY])
1277 # -----------------------------
1278 # Declare that no token matching the forbidden extended regular
1279 # expression ERE should be seen in the output unless...
1280 m4_define([m4_pattern_forbid], [])
1283 # m4_pattern_allow(ERE)
1284 # ---------------------
1285 # ... that token also matches the allowed extended regular expression ERE.
1286 # Both used via traces.
1287 m4_define([m4_pattern_allow], [])
1290 ## ----------------------------- ##
1291 ## Dependencies between macros. ##
1292 ## ----------------------------- ##
1295 # m4_before(THIS-MACRO-NAME, CALLED-MACRO-NAME)
1296 # ---------------------------------------------
1297 # Issue a warning if CALLED-MACRO-NAME was called before THIS-MACRO-NAME.
1298 m4_define([m4_before],
1299 [m4_provide_if([$2],
1300 [m4_warn([syntax], [$2 was called before $1])])])
1303 # m4_require(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
1304 # -----------------------------------------------------------
1305 # If NAME-TO-CHECK has never been expanded (actually, if it is not
1306 # m4_provide'd), expand BODY-TO-EXPAND *before* the current macro
1307 # expansion. Once expanded, emit it in _m4_divert_dump. Keep track
1308 # of the m4_require chain in m4_expansion_stack.
1310 # The normal cases are:
1312 # - NAME-TO-CHECK == BODY-TO-EXPAND
1313 # Which you can use for regular macros with or without arguments, e.g.,
1314 # m4_require([AC_PROG_CC], [AC_PROG_CC])
1315 # m4_require([AC_CHECK_HEADERS(limits.h)], [AC_CHECK_HEADERS(limits.h)])
1316 # which is just the same as
1317 # m4_require([AC_PROG_CC])
1318 # m4_require([AC_CHECK_HEADERS(limits.h)])
1320 # - BODY-TO-EXPAND == m4_indir([NAME-TO-CHECK])
1321 # In the case of macros with irregular names. For instance:
1322 # m4_require([AC_LANG_COMPILER(C)], [indir([AC_LANG_COMPILER(C)])])
1323 # which means `if the macro named `AC_LANG_COMPILER(C)' (the parens are
1324 # part of the name, it is not an argument) has not been run, then
1327 # m4_require([AC_LANG_COMPILER(C)], [AC_LANG_COMPILER(C)])
1328 # then m4_require would have tried to expand `AC_LANG_COMPILER(C)', i.e.,
1329 # call the macro `AC_LANG_COMPILER' with `C' as argument.
1331 # You could argue that `AC_LANG_COMPILER', when it receives an argument
1332 # such as `C' should dispatch the call to `AC_LANG_COMPILER(C)'. But this
1333 # `extension' prevents `AC_LANG_COMPILER' from having actual arguments that
1334 # it passes to `AC_LANG_COMPILER(C)'.
1335 m4_define([m4_require],
1336 [m4_ifdef([_m4_expanding($1)],
1337 [m4_fatal([$0: circular dependency of $1])])dnl
1338 m4_ifndef([_m4_divert_dump],
1339 [m4_fatal([$0($1): cannot be used outside of an ]dnl
1340 m4_bmatch([$0], [^AC_], [[AC_DEFUN]], [[m4_defun]])['d macro])])dnl
1343 [_m4_require_call([$1], [$2])])dnl
1347 # _m4_require_call(BODY-TO-EXPAND)
1348 # --------------------------------
1349 # If m4_require decides to expand the body, it calls this macro.
1350 m4_define([_m4_require_call],
1351 [m4_define([_m4_divert_grow], m4_decr(_m4_divert_grow))dnl
1352 m4_divert_push(_m4_divert_grow)dnl
1353 m4_default([$2], [$1])
1357 [$1 is m4_require'd but not m4_defun'd])])dnl
1358 m4_divert(m4_defn([_m4_divert_dump]))dnl
1359 m4_undivert(_m4_divert_grow)dnl
1360 m4_divert_pop(_m4_divert_grow)dnl
1361 m4_define([_m4_divert_grow], m4_incr(_m4_divert_grow))dnl
1367 # The counter for _m4_require_call.
1368 m4_define([_m4_divert_grow], _m4_divert([GROW]))
1371 # m4_expand_once(TEXT, [WITNESS = TEXT])
1372 # --------------------------------------
1373 # If TEXT has never been expanded, expand it *here*. Use WITNESS as
1374 # as a memory that TEXT has already been expanded.
1375 m4_define([m4_expand_once],
1376 [m4_provide_if(m4_ifval([$2], [[$2]], [[$1]]),
1378 [m4_provide(m4_ifval([$2], [[$2]], [[$1]]))[]$1])])
1381 # m4_provide(MACRO-NAME)
1382 # ----------------------
1383 m4_define([m4_provide],
1384 [m4_define([m4_provide($1)])])
1387 # m4_provide_if(MACRO-NAME, IF-PROVIDED, IF-NOT-PROVIDED)
1388 # -------------------------------------------------------
1389 # If MACRO-NAME is provided do IF-PROVIDED, else IF-NOT-PROVIDED.
1390 # The purpose of this macro is to provide the user with a means to
1391 # check macros which are provided without letting her know how the
1392 # information is coded.
1393 m4_define([m4_provide_if],
1394 [m4_ifdef([m4_provide($1)],
1398 ## -------------------- ##
1399 ## 9. Text processing. ##
1400 ## -------------------- ##
1407 m4_define([m4_cr_letters], [abcdefghijklmnopqrstuvwxyz])
1408 m4_define([m4_cr_LETTERS], [ABCDEFGHIJKLMNOPQRSTUVWXYZ])
1409 m4_define([m4_cr_Letters],
1410 m4_defn([m4_cr_letters])dnl
1411 m4_defn([m4_cr_LETTERS])dnl
1417 m4_define([m4_cr_digits], [0123456789])
1422 m4_define([m4_cr_alnum],
1423 m4_defn([m4_cr_Letters])dnl
1424 m4_defn([m4_cr_digits])dnl
1430 # -------------------------------
1431 m4_define([m4_cr_symbols1],
1432 m4_defn([m4_cr_Letters])dnl
1435 m4_define([m4_cr_symbols2],
1436 m4_defn([m4_cr_symbols1])dnl
1437 m4_defn([m4_cr_digits])dnl
1442 # The character range representing everything, with `-' as the last
1443 # character, since it is special to m4_translit. Use with care, because
1444 # it contains characters special to M4 (fortunately, both ASCII and EBCDIC
1445 # have [] in order, so m4_defn([m4_cr_all]) remains a valid string). It
1446 # also contains characters special to terminals, so it should never be
1447 # displayed in an error message. Also, attempts to map [ and ] to other
1448 # characters via m4_translit must deal with the fact that m4_translit does
1449 # not add quotes to the output.
1451 # It is mainly useful in generating inverted character range maps, for use
1452 # in places where m4_translit is faster than an equivalent m4_bpatsubst;
1453 # the regex `[^a-z]' is equivalent to:
1454 # m4_translit(m4_dquote(m4_defn([m4_cr_all])), [a-z])
1455 m4_define([m4_cr_all],
1456 m4_translit(m4_dquote(m4_format(m4_dquote(m4_for(
1457 ,1,255,,[[%c]]))m4_for([i],1,255,,[,i]))), [-])-)
1460 # _m4_define_cr_not(CATEGORY)
1461 # ---------------------------
1462 # Define m4_cr_not_CATEGORY as the inverse of m4_cr_CATEGORY.
1463 m4_define([_m4_define_cr_not],
1464 [m4_define([m4_cr_not_$1],
1465 m4_translit(m4_dquote(m4_defn([m4_cr_all])),
1466 m4_defn([m4_cr_$1])))])
1474 # m4_cr_not_symbols1
1475 # m4_cr_not_symbols2
1476 # ------------------
1477 # Inverse character sets
1478 _m4_define_cr_not([letters])
1479 _m4_define_cr_not([LETTERS])
1480 _m4_define_cr_not([Letters])
1481 _m4_define_cr_not([digits])
1482 _m4_define_cr_not([alnum])
1483 _m4_define_cr_not([symbols1])
1484 _m4_define_cr_not([symbols2])
1489 # Expands to a newline. Exists for formatting reasons.
1490 m4_define([m4_newline], [
1494 # m4_re_escape(STRING)
1495 # --------------------
1496 # Escape RE active characters in STRING.
1497 m4_define([m4_re_escape],
1499 [[][*+.?\^$]], [\\\&])])
1504 # Regexp for `[a-zA-Z_0-9]*'
1505 # m4_dquote provides literal [] for the character class.
1506 m4_define([m4_re_string],
1507 m4_dquote(m4_defn([m4_cr_symbols2]))dnl
1514 # Regexp for `[a-zA-Z_][a-zA-Z_0-9]*'
1515 m4_define([m4_re_word],
1516 m4_dquote(m4_defn([m4_cr_symbols1]))dnl
1517 m4_defn([m4_re_string])dnl
1521 # m4_tolower(STRING)
1522 # m4_toupper(STRING)
1523 # ------------------
1524 # These macros convert STRING to lowercase or uppercase.
1526 # Rather than expand the m4_defn each time, we inline them up front.
1527 m4_define([m4_tolower],
1528 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_LETTERS]))[,
1529 ]m4_dquote(m4_defn([m4_cr_letters]))[)])
1530 m4_define([m4_toupper],
1531 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_letters]))[,
1532 ]m4_dquote(m4_defn([m4_cr_LETTERS]))[)])
1535 # m4_split(STRING, [REGEXP])
1536 # --------------------------
1538 # Split STRING into an m4 list of quoted elements. The elements are
1539 # quoted with [ and ]. Beginning spaces and end spaces *are kept*.
1540 # Use m4_strip to remove them.
1542 # REGEXP specifies where to split. Default is [\t ]+.
1544 # If STRING is empty, the result is an empty list.
1546 # Pay attention to the m4_changequotes. When m4 reads the definition of
1547 # m4_split, it still has quotes set to [ and ]. Luckily, these are matched
1548 # in the macro body, so the definition is stored correctly. Use the same
1549 # alternate quotes as m4_noquote; it must be unlikely to appear in $1.
1551 # Also, notice that $1 is quoted twice, since we want the result to
1552 # be quoted. Then you should understand that the argument of
1553 # patsubst is -=<{STRING}>=- (i.e., with additional -=<{ and }>=-).
1555 # This macro is safe on active symbols, i.e.:
1556 # m4_define(active, ACTIVE)
1557 # m4_split([active active ])end
1558 # => [active], [active], []end
1560 # Optimize on regex of ` ' (space), since m4_foreach_w already guarantees
1561 # that the list contains single space separators, and a common case is
1562 # splitting a single-element list. This macro is called frequently,
1563 # so avoid unnecessary dnl inside the definition.
1564 m4_define([m4_split],
1565 [m4_if([$1], [], [],
1566 [$2], [ ], [m4_if(m4_index([$1], [ ]), [-1], [[[$1]]], [_$0($@)])],
1567 [$2], [], [_$0([$1], [[ ]+])],
1570 m4_define([_m4_split],
1571 [m4_changequote(-=<{,}>=-)]dnl
1572 [[m4_bpatsubst(-=<{-=<{$1}>=-}>=-, -=<{$2}>=-,
1573 -=<{], [}>=-)]m4_changequote([, ])])
1577 # m4_flatten(STRING)
1578 # ------------------
1579 # If STRING contains end of lines, replace them with spaces. If there
1580 # are backslashed end of lines, remove them. This macro is safe with
1582 # m4_define(active, ACTIVE)
1583 # m4_flatten([active
1586 # => active activeend
1588 # In m4, m4_bpatsubst is expensive, so first check for a newline.
1589 m4_define([m4_flatten],
1590 [m4_if(m4_index([$1], [
1592 [m4_translit(m4_bpatsubst([[[$1]]], [\\
1599 # Expands into STRING with tabs and spaces singled out into a single
1600 # space, and removing leading and trailing spaces.
1602 # This macro is robust to active symbols.
1603 # m4_define(active, ACTIVE)
1604 # m4_strip([ active <tab> <tab>active ])end
1605 # => active activeend
1607 # First, notice that we guarantee trailing space. Why? Because regular
1608 # expressions are greedy, and `.* ?' would alway groups the space into the
1609 # .* portion. The algorithm is simpler by avoiding `?' at the end. The
1610 # algorithm correctly strips everything if STRING is just ` '.
1612 # Then notice the second pattern: it is in charge of removing the
1613 # leading/trailing spaces. Why not just `[^ ]'? Because they are
1614 # applied to over-quoted strings, i.e. more or less [STRING], due
1615 # to the limitations of m4_bpatsubsts. So the leading space in STRING
1616 # is the *second* character; equally for the trailing space.
1617 m4_define([m4_strip],
1618 [m4_bpatsubsts([$1 ],
1620 [^. ?\(.*\) .$], [[[\1]]])])
1623 # m4_normalize(STRING)
1624 # --------------------
1625 # Apply m4_flatten and m4_strip to STRING.
1627 # The argument is quoted, so that the macro is robust to active symbols:
1629 # m4_define(active, ACTIVE)
1630 # m4_normalize([ act\
1633 # => active activeend
1635 m4_define([m4_normalize],
1636 [m4_strip(m4_flatten([$1]))])
1640 # m4_join(SEP, ARG1, ARG2...)
1641 # ---------------------------
1642 # Produce ARG1SEPARG2...SEPARGn.
1647 [[$2][$1]$0([$1], m4_shift2($@))])])
1651 # m4_append(MACRO-NAME, STRING, [SEPARATOR])
1652 # ------------------------------------------
1653 # Redefine MACRO-NAME to hold its former content plus `SEPARATOR`'STRING'
1654 # at the end. It is valid to use this macro with MACRO-NAME undefined,
1655 # in which case no SEPARATOR is added. Be aware that the criterion is
1656 # `not being defined', and not `not being empty'.
1658 # This macro is robust to active symbols. It can be used to grow
1661 # | m4_define(active, ACTIVE)
1662 # | m4_append([sentence], [This is an])
1663 # | m4_append([sentence], [ active ])
1664 # | m4_append([sentence], [symbol.])
1666 # | m4_undefine([active])dnl
1668 # => This is an ACTIVE symbol.
1669 # => This is an active symbol.
1671 # It can be used to define hooks.
1673 # | m4_define(active, ACTIVE)
1674 # | m4_append([hooks], [m4_define([act1], [act2])])
1675 # | m4_append([hooks], [m4_define([act2], [active])])
1676 # | m4_undefine([active])
1683 m4_define([m4_append],
1685 m4_ifdef([$1], [m4_defn([$1])$3])[$2])])
1688 # m4_append_uniq(MACRO-NAME, STRING, [SEPARATOR])
1689 # -----------------------------------------------
1690 # Like `m4_append', but append only if not yet present.
1691 m4_define([m4_append_uniq],
1693 [m4_if(m4_index([$3]m4_defn([$1])[$3], [$3$2$3]), [-1],
1698 # m4_text_wrap(STRING, [PREFIX], [FIRST-PREFIX], [WIDTH])
1699 # -------------------------------------------------------
1700 # Expands into STRING wrapped to hold in WIDTH columns (default = 79).
1701 # If PREFIX is given, each line is prefixed with it. If FIRST-PREFIX is
1702 # specified, then the first line is prefixed with it. As a special case,
1703 # if the length of FIRST-PREFIX is greater than that of PREFIX, then
1704 # FIRST-PREFIX will be left alone on the first line.
1706 # Typical outputs are:
1708 # m4_text_wrap([Short string */], [ ], [/* ], 20)
1709 # => /* Short string */
1711 # m4_text_wrap([Much longer string */], [ ], [/* ], 20)
1715 # m4_text_wrap([Short doc.], [ ], [ --short ], 30)
1716 # => --short Short doc.
1718 # m4_text_wrap([Short doc.], [ ], [ --too-wide ], 30)
1722 # m4_text_wrap([Super long documentation.], [ ], [ --too-wide ], 30)
1727 # FIXME: there is no checking of a longer PREFIX than WIDTH, but do
1728 # we really want to bother with people trying each single corner
1731 # This macro does not leave a trailing space behind the last word of a line,
1732 # which complicates it a bit. The algorithm is otherwise stupid and simple:
1733 # all the words are preceded by m4_Separator which is defined to empty for
1734 # the first word, and then ` ' (single space) for all the others.
1735 m4_define([m4_text_wrap],
1736 [m4_pushdef([m4_Prefix], [$2])dnl
1737 m4_pushdef([m4_Prefix1], m4_default([$3], [m4_Prefix]))dnl
1738 m4_pushdef([m4_Width], m4_default([$4], 79))dnl
1739 m4_pushdef([m4_Cursor], m4_qlen(m4_defn([m4_Prefix1])))dnl
1740 m4_pushdef([m4_Separator], [])dnl
1741 m4_defn([m4_Prefix1])[]dnl
1742 m4_cond([m4_eval(m4_qlen(m4_defn([m4_Prefix1])) > m4_len(m4_Prefix))],
1743 [1], [m4_define([m4_Cursor], m4_len(m4_Prefix))
1745 [m4_eval(m4_qlen(m4_defn([m4_Prefix1])) < m4_len(m4_Prefix))],
1747 [m4_define([m4_Cursor], m4_len(m4_Prefix))[]dnl
1749 m4_max(0,m4_eval(m4_len(m4_Prefix) - m4_qlen(m4_defn([m4_Prefix1])))),
1751 m4_foreach_w([m4_Word], [$1],
1752 [m4_define([m4_Cursor], m4_eval(m4_Cursor + m4_qlen(m4_defn([m4_Word])) + 1))dnl
1753 dnl New line if too long, else insert a space unless it is the first
1755 m4_if(m4_eval(m4_Cursor > m4_Width),
1756 1, [m4_define([m4_Cursor],
1757 m4_eval(m4_len(m4_Prefix) + m4_qlen(m4_defn([m4_Word])) + 1))]
1759 [m4_Separator])[]dnl
1760 m4_defn([m4_Word])[]dnl
1761 m4_define([m4_Separator], [ ])])dnl
1762 m4_popdef([m4_Separator])dnl
1763 m4_popdef([m4_Cursor])dnl
1764 m4_popdef([m4_Width])dnl
1765 m4_popdef([m4_Prefix1])dnl
1766 m4_popdef([m4_Prefix])dnl
1770 # m4_text_box(MESSAGE, [FRAME-CHARACTER = `-'])
1771 # ---------------------------------------------
1772 # Turn MESSAGE into:
1776 # using FRAME-CHARACTER in the border.
1777 m4_define([m4_text_box],
1778 [@%:@@%:@ m4_bpatsubst([$1], [.], m4_if([$2], [], [[-]], [[$2]])) @%:@@%:@
1779 @%:@@%:@ $1 @%:@@%:@
1780 @%:@@%:@ m4_bpatsubst([$1], [.], m4_if([$2], [], [[-]], [[$2]])) @%:@@%:@[]dnl
1786 # Expands to the length of STRING after autom4te converts all quadrigraphs.
1788 # Avoid bpatsubsts for the common case of no quadrigraphs.
1789 m4_define([m4_qlen],
1790 [m4_if(m4_index([$1], [@]), [-1], [m4_len([$1])],
1791 [m4_len(m4_bpatsubsts([[$1]], [@\(<:\|:>\|S|\|%:\)@], [P], [@&t@]))])])
1796 # Expands to the net change in the length of STRING from autom4te converting the
1797 # quadrigraphs in STRING. This number is always negative or zero.
1798 m4_define([m4_qdelta],
1799 [m4_eval(m4_qlen([$1]) - m4_len([$1]))])
1803 ## ----------------------- ##
1804 ## 10. Number processing. ##
1805 ## ----------------------- ##
1809 # Compare two integer expressions.
1814 [m4_eval((([$1]) > ([$2])) - (([$1]) < ([$2])))])
1820 # Compare the two lists of integers A and B. For instance:
1821 # m4_list_cmp((1, 0), (1)) -> 0
1822 # m4_list_cmp((1, 0), (1, 0)) -> 0
1823 # m4_list_cmp((1, 2), (1, 0)) -> 1
1824 # m4_list_cmp((1, 2, 3), (1, 2)) -> 1
1825 # m4_list_cmp((1, 2, -3), (1, 2)) -> -1
1826 # m4_list_cmp((1, 0), (1, 2)) -> -1
1827 # m4_list_cmp((1), (1, 2)) -> -1
1828 m4_define([m4_list_cmp],
1829 [m4_if([$1$2], [()()], 0,
1830 [$1], [()], [$0((0), [$2])],
1831 [$2], [()], [$0([$1], (0))],
1832 [m4_case(m4_cmp(m4_car$1, m4_car$2),
1835 0, [$0((m4_shift$1), (m4_shift$2))])])])
1840 # Return the maximum (or minimum) of a series of integer expressions.
1842 # M4 1.4.x doesn't provide ?:. Hence this huge m4_eval. Avoid m4_eval
1843 # if both arguments are identical, but be aware of m4_max(0xa, 10) (hence
1844 # the use of <=, not just <, in the second multiply).
1846 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
1848 [$#$1], [2$2], [$1],
1850 [m4_eval((([$1]) > ([$2])) * ([$1]) + (([$1]) <= ([$2])) * ([$2]))],
1851 [$0($0([$1], [$2]), m4_shift2($@))])])
1853 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
1855 [$#$1], [2$2], [$1],
1857 [m4_eval((([$1]) < ([$2])) * ([$1]) + (([$1]) >= ([$2])) * ([$2]))],
1858 [$0($0([$1], [$2]), m4_shift2($@))])])
1863 # The sign of the integer expression A.
1864 m4_define([m4_sign],
1865 [m4_eval((([$1]) > 0) - (([$1]) < 0))])
1869 ## ------------------------ ##
1870 ## 11. Version processing. ##
1871 ## ------------------------ ##
1874 # m4_version_unletter(VERSION)
1875 # ----------------------------
1876 # Normalize beta version numbers with letters to numbers only for comparison.
1878 # Nl -> (N+1).-1.(l#)
1880 #i.e., 2.14a -> 2.15.-1.1, 2.14b -> 2.15.-1.2, etc.
1881 # This macro is absolutely not robust to active macro, it expects
1882 # reasonable version numbers and is valid up to `z', no double letters.
1883 m4_define([m4_version_unletter],
1884 [m4_translit(m4_bpatsubsts(m4_tolower([[$1]]),
1885 [\([0-9]+\)\([abcdefghi]\)],
1886 [m4_eval(\1 + 1).-1.\2],
1887 [\([0-9]+\)\([jklmnopqrs]\)],
1888 [m4_eval(\1 + 1).-1.1\2],
1889 [\([0-9]+\)\([tuvwxyz]\)],
1890 [m4_eval(\1 + 1).-1.2\2]),
1891 [abcdefghijklmnopqrstuvwxyz],
1892 [12345678901234567890123456])])
1895 # m4_version_compare(VERSION-1, VERSION-2)
1896 # ----------------------------------------
1897 # Compare the two version numbers and expand into
1898 # -1 if VERSION-1 < VERSION-2
1901 m4_define([m4_version_compare],
1902 [m4_list_cmp((m4_translit(m4_version_unletter([$1]), [.], [,])),
1903 (m4_translit(m4_version_unletter([$2]), [.], [,])))])
1907 # m4_PACKAGE_TARNAME
1908 # m4_PACKAGE_VERSION
1910 # m4_PACKAGE_BUGREPORT
1911 # --------------------
1912 m4_include([m4sugar/version.m4])
1915 # m4_version_prereq(VERSION, [IF-OK], [IF-NOT = FAIL])
1916 # ----------------------------------------------------
1917 # Check this Autoconf version against VERSION.
1918 m4_define([m4_version_prereq],
1919 [m4_if(m4_version_compare(m4_defn([m4_PACKAGE_VERSION]), [$1]), -1,
1921 [m4_fatal([Autoconf version $1 or higher is required],
1928 ## ------------------- ##
1929 ## 12. File handling. ##
1930 ## ------------------- ##
1933 # It is a real pity that M4 comes with no macros to bind a diversion
1934 # to a file. So we have to deal without, which makes us a lot more
1935 # fragile than we should.
1938 # m4_file_append(FILE-NAME, CONTENT)
1939 # ----------------------------------
1940 m4_define([m4_file_append],
1941 [m4_syscmd([cat >>$1 <<_m4eof
1945 m4_if(m4_sysval, [0], [],
1946 [m4_fatal([$0: cannot write: $1])])])
1950 ## ------------------------ ##
1951 ## 13. Setting M4sugar up. ##
1952 ## ------------------------ ##
1957 m4_define([m4_init],
1958 [# All the M4sugar macros start with `m4_', except `dnl' kept as is
1959 # for sake of simplicity.
1960 m4_pattern_forbid([^_?m4_])
1961 m4_pattern_forbid([^dnl$])
1963 # _m4_divert_diversion should be defined:
1964 m4_divert_push([KILL])
1966 # Check the divert push/pop perfect balance.
1967 m4_wrap([m4_divert_pop([])
1968 m4_ifdef([_m4_divert_diversion],
1969 [m4_fatal([$0: unbalanced m4_divert_push:]_m4_divert_n_stack)])[]])