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]),
494 # m4_define_default(MACRO, VALUE)
495 # -------------------------------
496 # If MACRO is undefined, set it to VALUE.
497 m4_define([m4_define_default],
498 [m4_ifndef([$1], [m4_define($@)])])
501 # m4_default(EXP1, EXP2)
502 # ----------------------
503 # Returns EXP1 if non empty, otherwise EXP2.
505 # This macro is called on hot paths, so inline the contents of m4_ifval,
506 # for one less round of expansion.
507 m4_define([m4_default],
508 [m4_if([$1], [], [$2], [$1])])
513 # Like the original, except don't tolerate popping something which is
514 # undefined, and only support one argument.
516 # This macro is called frequently, so minimize the amount of additional
517 # expansions by skipping m4_ifndef.
520 [m4_fatal([$0: undefined macro: $1])])]dnl
521 [m4_builtin([defn], [$1])])
524 # _m4_dumpdefs_up(NAME)
525 # ---------------------
526 m4_define([_m4_dumpdefs_up],
528 [m4_pushdef([_m4_dumpdefs], m4_defn([$1]))dnl
531 _m4_dumpdefs_up([$1])])])
534 # _m4_dumpdefs_down(NAME)
535 # -----------------------
536 m4_define([_m4_dumpdefs_down],
537 [m4_ifdef([_m4_dumpdefs],
538 [m4_pushdef([$1], m4_defn([_m4_dumpdefs]))dnl
539 m4_popdef([_m4_dumpdefs])dnl
540 _m4_dumpdefs_down([$1])])])
545 # Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its
546 # value stack (most recent displayed first).
547 m4_define([m4_dumpdefs],
548 [_m4_dumpdefs_up([$1])dnl
549 _m4_dumpdefs_down([$1])])
554 # Like the original, except don't tolerate popping something which is
555 # undefined, and only support one argument.
557 # This macro is called frequently, so minimize the amount of additional
558 # expansions by skipping m4_ifndef.
559 m4_define([m4_popdef],
561 [m4_fatal([$0: undefined macro: $1])])]dnl
562 [m4_builtin([popdef], [$1])])
567 # Returns ... shifted N times. Useful for recursive "varargs" constructs.
568 m4_define([m4_shiftn],
569 [m4_assert(0 <= $1 && $1 < $#)dnl
572 m4_define([_m4_shiftn],
575 [_m4_shiftn(m4_eval([$1]-1), m4_shift(m4_shift($@)))])])
580 # Returns ... shifted twice, and three times. Faster than m4_shiftn.
581 m4_define([m4_shift2], [m4_shift(m4_shift($@))])
582 m4_define([m4_shift3], [m4_shift(m4_shift(m4_shift($@)))])
586 # Like the original, except don't tolerate undefining something which is
587 # undefined, and only support one argument.
589 # This macro is called frequently, so minimize the amount of additional
590 # expansions by skipping m4_ifndef.
591 m4_define([m4_undefine],
593 [m4_fatal([$0: undefined macro: $1])])]dnl
594 [m4_builtin([undefine], [$1])])
597 ## ------------------------- ##
598 ## 7. Quoting manipulation. ##
599 ## ------------------------- ##
603 # This macro invokes all its arguments (in sequence, of course). It is
604 # useful for making your macros more structured and readable by dropping
605 # unnecessary dnl's and have the macros indented properly.
609 [$1[]m4_do(m4_shift($@))])])
614 # Return ARGS as a quoted list of quoted arguments.
615 m4_define([m4_dquote], [[$@]])
620 # Expands to nothing. Useful for conditionally ignoring an arbitrary
621 # number of arguments (see _m4_list_cmp for an example).
622 m4_define([m4_ignore])
627 # Return the result of ignoring all quotes in STRING and invoking the
628 # macros it contains. Amongst other things, this is useful for enabling
629 # macro invocations inside strings with [] blocks (for instance regexps
630 # and help-strings). On the other hand, since all quotes are disabled,
631 # any macro expanded during this time that relies on nested [] quoting
632 # will likely crash and burn. This macro is seldom useful; consider
633 # m4_unquote instead.
634 m4_define([m4_noquote],
635 [m4_changequote(-=<{,}>=-)$1-=<{}>=-m4_changequote([,])])
640 # Return ARGS as a single argument. Any whitespace after unquoted commas
643 # It is important to realize the difference between `m4_quote(exp)' and
644 # `[exp]': in the first case you obtain the quoted *result* of the
645 # expansion of EXP, while in the latter you just obtain the string
647 m4_define([m4_quote], [[$*]])
652 # Remove one layer of quotes from each ARG, performing one level of
653 # expansion. For one argument, m4_unquote([arg]) is more efficient than
654 # m4_do([arg]), but for multiple arguments, the difference is that
655 # m4_unquote separates arguments with commas while m4_do concatenates.
656 m4_define([m4_unquote], [$*])
659 ## -------------------------- ##
660 ## 8. Implementing m4 loops. ##
661 ## -------------------------- ##
664 # m4_for(VARIABLE, FIRST, LAST, [STEP = +/-1], EXPRESSION)
665 # --------------------------------------------------------
666 # Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO with
667 # increments of STEP.
668 # Both limits are included, and bounds are checked for consistency.
669 # The algorithm is robust to indirect VARIABLE names, and uses m4_builtin
670 # to avoid some of the m4_defn overhead.
672 [m4_pushdef([$1], m4_eval([$2]))dnl
673 m4_cond([m4_eval(([$3]) > m4_builtin([defn], [$1]))], 1,
674 [m4_pushdef([_m4_step], m4_eval(m4_default([$4], 1)))dnl
675 m4_assert(_m4_step > 0)dnl
676 _m4_for([$1], m4_eval((([$3]) - m4_builtin([defn], [$1]))
677 / _m4_step * _m4_step + m4_builtin([defn], [$1])),
679 [m4_eval(([$3]) < m4_builtin([defn], [$1]))], 1,
680 [m4_pushdef([_m4_step], m4_eval(m4_default([$4], -1)))dnl
681 m4_assert(_m4_step < 0)dnl
682 _m4_for([$1], m4_eval((m4_builtin([defn], [$1]) - ([$3]))
683 / -(_m4_step) * _m4_step + m4_builtin([defn], [$1])),
685 [m4_pushdef([_m4_step])dnl
687 m4_popdef([_m4_step])dnl
691 # _m4_for(VARIABLE, LAST, STEP, EXPRESSION)
692 # -----------------------------------------
693 # Core of the loop, no consistency checks, all arguments are plain numbers.
696 m4_if(m4_defn([$1]), [$2], [],
697 [m4_define([$1], m4_eval(m4_defn([$1])+[$3]))$0($@)])])
700 # Implementing `foreach' loops in m4 is much more tricky than it may
701 # seem. For example, the old M4 1.4.4 manual had an incorrect example,
702 # which looked like this (when translated to m4sugar):
704 # | # foreach(VAR, (LIST), STMT)
705 # | m4_define([foreach],
706 # | [m4_pushdef([$1])_foreach([$1], [$2], [$3])m4_popdef([$1])])
707 # | m4_define([_arg1], [$1])
708 # | m4_define([_foreach],
709 # | [m4_if([$2], [()], ,
710 # | [m4_define([$1], _arg1$2)$3[]_foreach([$1], (m4_shift$2), [$3])])])
712 # But then if you run
717 # | foreach([f], [([a], [(b], [c)])], [echo f
725 # which is not what is expected.
727 # Of course the problem is that many quotes are missing. So you add
728 # plenty of quotes at random places, until you reach the expected
729 # result. Alternatively, if you are a quoting wizard, you directly
730 # reach the following implementation (but if you really did, then
731 # apply to the maintenance of m4sugar!).
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],
737 # | [m4_if($2, [()], ,
738 # | [m4_define([$1], [_arg1$2])$3[]_foreach([$1], [(m4_shift$2)], [$3])])])
740 # which this time answers
750 # With a better look, you realize that the parens are more a pain than
751 # a help: since anyway you need to quote properly the list, you end up
752 # with always using an outermost pair of parens and an outermost pair
753 # of quotes. Rejecting the parens both eases the implementation, and
754 # simplifies the use:
756 # | # foreach(VAR, (LIST), STMT)
757 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
758 # | m4_define([_arg1], [$1])
759 # | m4_define([_foreach],
761 # | [m4_define([$1], [_arg1($2)])$3[]_foreach([$1], [m4_shift($2)], [$3])])])
764 # Now, just replace the `$2' with `m4_quote($2)' in the outer `m4_if'
765 # to improve robustness, and you come up with a nice implementation
766 # that doesn't require extra parenthesis in the user's LIST.
768 # But wait - now the algorithm is quadratic, because every recursion of
769 # the algorithm keeps the entire LIST and merely adds another m4_shift to
770 # the quoted text. If the user has a lot of elements in LIST, you can
771 # bring the system to its knees with the memory m4 then requires, or trip
772 # the m4 --nesting-limit recursion factor. The only way to avoid
773 # quadratic growth is ensure m4_shift is expanded prior to the recursion.
774 # Hence the design below.
776 # The M4 manual now includes a chapter devoted to this issue, with
777 # the lessons learned from m4sugar.
780 # m4_foreach(VARIABLE, LIST, EXPRESSION)
781 # --------------------------------------
783 # Expand EXPRESSION assigning each value of the LIST to VARIABLE.
784 # LIST should have the form `item_1, item_2, ..., item_n', i.e. the
785 # whole list must *quoted*. Quote members too if you don't want them
788 # This macro is robust to active symbols:
789 # | m4_define(active, [ACT, IVE])
790 # | m4_foreach(Var, [active, active], [-Var-])
791 # => -ACT--IVE--ACT--IVE-
793 # | m4_foreach(Var, [[active], [active]], [-Var-])
794 # => -ACT, IVE--ACT, IVE-
796 # | m4_foreach(Var, [[[active]], [[active]]], [-Var-])
797 # => -active--active-
799 # This macro is called frequently, so avoid extra expansions such as
801 m4_define([m4_foreach],
802 [m4_pushdef([$1])_$0($@)m4_popdef([$1])])
804 m4_define([_m4_foreach],
806 [m4_define([$1], m4_car($2))$3[]$0([$1], m4_cdr($2), [$3])])])
809 # m4_foreach_w(VARIABLE, LIST, EXPRESSION)
810 # ----------------------------------------
812 # Like m4_foreach, but the list is whitespace separated.
814 # This macro is robust to active symbols:
815 # m4_foreach_w([Var], [ active
818 # => -active--b--active-end
820 m4_define([m4_foreach_w],
821 [m4_foreach([$1], m4_split(m4_normalize([$2]), [ ]), [$3])])
825 ## --------------------------- ##
826 ## 9. More diversion support. ##
827 ## --------------------------- ##
830 # _m4_divert(DIVERSION-NAME or NUMBER)
831 # ------------------------------------
832 # If DIVERSION-NAME is the name of a diversion, return its number,
833 # otherwise if it is a NUMBER return it.
834 m4_define([_m4_divert],
835 [m4_ifdef([_m4_divert($1)],
836 [m4_indir([_m4_divert($1)])],
839 # KILL is only used to suppress output.
840 m4_define([_m4_divert(KILL)], -1)
842 # The empty diversion name is a synonym for 0.
843 m4_define([_m4_divert()], 0)
848 # Print m4_divert_stack with newline prepended, if it's nonempty.
849 m4_define([_m4_divert_n_stack],
850 [m4_ifdef([m4_divert_stack], [
851 m4_defn([m4_divert_stack])])])
854 # m4_divert(DIVERSION-NAME)
855 # -------------------------
856 # Change the diversion stream to DIVERSION-NAME.
857 m4_define([m4_divert],
858 [m4_define([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
859 m4_builtin([divert], _m4_divert([$1]))dnl
863 # m4_divert_push(DIVERSION-NAME)
864 # ------------------------------
865 # Change the diversion stream to DIVERSION-NAME, while stacking old values.
866 m4_define([m4_divert_push],
867 [m4_pushdef([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
868 m4_pushdef([_m4_divert_diversion], [$1])dnl
869 m4_builtin([divert], _m4_divert([$1]))dnl
873 # m4_divert_pop([DIVERSION-NAME])
874 # -------------------------------
875 # Change the diversion stream to its previous value, unstacking it.
876 # If specified, verify we left DIVERSION-NAME.
877 # When we pop the last value from the stack, we divert to -1.
878 m4_define([m4_divert_pop],
879 [m4_ifndef([_m4_divert_diversion],
880 [m4_fatal([too many m4_divert_pop])])dnl
882 [$1], m4_defn([_m4_divert_diversion]), [],
883 [m4_fatal([$0($1): diversion mismatch: ]_m4_divert_n_stack)])dnl
884 m4_popdef([m4_divert_stack])dnl
885 m4_popdef([_m4_divert_diversion])dnl
887 m4_ifdef([_m4_divert_diversion],
888 [_m4_divert(m4_defn([_m4_divert_diversion]))],
893 # m4_divert_text(DIVERSION-NAME, CONTENT)
894 # ---------------------------------------
895 # Output CONTENT into DIVERSION-NAME (which may be a number actually).
896 # An end of line is appended for free to CONTENT.
897 m4_define([m4_divert_text],
898 [m4_divert_push([$1])dnl
900 m4_divert_pop([$1])dnl
904 # m4_divert_once(DIVERSION-NAME, CONTENT)
905 # ---------------------------------------
906 # Output CONTENT into DIVERSION-NAME once, if not already there.
907 # An end of line is appended for free to CONTENT.
908 m4_define([m4_divert_once],
909 [m4_expand_once([m4_divert_text([$1], [$2])])])
912 # m4_undivert(DIVERSION-NAME)
913 # ---------------------------
914 # Undivert DIVERSION-NAME. Unlike the M4 version, this only takes a single
915 # diversion identifier, and should not be used to undivert files.
916 m4_define([m4_undivert],
917 [m4_builtin([undivert], _m4_divert([$1]))])
920 ## --------------------------------------------- ##
921 ## 10. Defining macros with bells and whistles. ##
922 ## --------------------------------------------- ##
924 # `m4_defun' is basically `m4_define' but it equips the macro with the
925 # needed machinery for `m4_require'. A macro must be m4_defun'd if
926 # either it is m4_require'd, or it m4_require's.
928 # Two things deserve attention and are detailed below:
929 # 1. Implementation of m4_require
930 # 2. Keeping track of the expansion stack
932 # 1. Implementation of m4_require
933 # ===============================
935 # Of course m4_defun AC_PROVIDE's the macro, so that a macro which has
936 # been expanded is not expanded again when m4_require'd, but the
937 # difficult part is the proper expansion of macros when they are
940 # The implementation is based on two ideas, (i) using diversions to
941 # prepare the expansion of the macro and its dependencies (by Franc,ois
942 # Pinard), and (ii) expand the most recently m4_require'd macros _after_
943 # the previous macros (by Axel Thimm).
946 # The first idea: why use diversions?
947 # -----------------------------------
949 # When a macro requires another, the other macro is expanded in new
950 # diversion, GROW. When the outer macro is fully expanded, we first
951 # undivert the most nested diversions (GROW - 1...), and finally
952 # undivert GROW. To understand why we need several diversions,
953 # consider the following example:
955 # | m4_defun([TEST1], [Test...REQUIRE([TEST2])1])
956 # | m4_defun([TEST2], [Test...REQUIRE([TEST3])2])
957 # | m4_defun([TEST3], [Test...3])
959 # Because m4_require is not required to be first in the outer macros, we
960 # must keep the expansions of the various levels of m4_require separated.
961 # Right before executing the epilogue of TEST1, we have:
968 # Finally the epilogue of TEST1 undiverts GROW - 2, GROW - 1, and
969 # GROW into the regular flow, BODY.
974 # BODY: Test...3; Test...2; Test...1
976 # (The semicolons are here for clarification, but of course are not
977 # emitted.) This is what Autoconf 2.0 (I think) to 2.13 (I'm sure)
981 # The second idea: first required first out
982 # -----------------------------------------
984 # The natural implementation of the idea above is buggy and produces
985 # very surprising results in some situations. Let's consider the
986 # following example to explain the bug:
988 # | m4_defun([TEST1], [REQUIRE([TEST2a])REQUIRE([TEST2b])])
989 # | m4_defun([TEST2a], [])
990 # | m4_defun([TEST2b], [REQUIRE([TEST3])])
991 # | m4_defun([TEST3], [REQUIRE([TEST2a])])
996 # The dependencies between the macros are:
999 # / \ is m4_require'd by
1000 # / \ left -------------------- right
1003 # If you strictly apply the rules given in the previous section you get:
1006 # GROW - 1: TEST2a; TEST2b
1010 # (TEST2a, although required by TEST3 is not expanded in GROW - 3
1011 # because is has already been expanded before in GROW - 1, so it has
1012 # been AC_PROVIDE'd, so it is not expanded again) so when you undivert
1013 # the stack of diversions, you get:
1018 # BODY: TEST3; TEST2a; TEST2b; TEST1
1020 # i.e., TEST2a is expanded after TEST3 although the latter required the
1023 # Starting from 2.50, we use an implementation provided by Axel Thimm.
1024 # The idea is simple: the order in which macros are emitted must be the
1025 # same as the one in which macros are expanded. (The bug above can
1026 # indeed be described as: a macro has been AC_PROVIDE'd before its
1027 # dependent, but it is emitted after: the lack of correlation between
1028 # emission and expansion order is guilty).
1030 # How to do that? You keep the stack of diversions to elaborate the
1031 # macros, but each time a macro is fully expanded, emit it immediately.
1033 # In the example above, when TEST2a is expanded, but it's epilogue is
1034 # not run yet, you have:
1038 # GROW: Elaboration of TEST1
1041 # The epilogue of TEST2a emits it immediately:
1045 # GROW: Elaboration of TEST1
1048 # TEST2b then requires TEST3, so right before the epilogue of TEST3, you
1052 # GROW - 1: Elaboration of TEST2b
1053 # GROW: Elaboration of TEST1
1056 # The epilogue of TEST3 emits it:
1059 # GROW - 1: Elaboration of TEST2b
1060 # GROW: Elaboration of TEST1
1061 # BODY: TEST2a; TEST3
1063 # TEST2b is now completely expanded, and emitted:
1067 # GROW: Elaboration of TEST1
1068 # BODY: TEST2a; TEST3; TEST2b
1070 # and finally, TEST1 is finished and emitted:
1075 # BODY: TEST2a; TEST3; TEST2b: TEST1
1077 # The idea is simple, but the implementation is a bit evolved. If you
1078 # are like me, you will want to see the actual functioning of this
1079 # implementation to be convinced. The next section gives the full
1083 # The Axel Thimm implementation at work
1084 # -------------------------------------
1086 # We consider the macros above, and this configure.ac:
1091 # You should keep the definitions of _m4_defun_pro, _m4_defun_epi, and
1092 # m4_require at hand to follow the steps.
1094 # This implements tries not to assume that the current diversion is
1095 # BODY, so as soon as a macro (m4_defun'd) is expanded, we first
1096 # record the current diversion under the name _m4_divert_dump (denoted
1097 # DUMP below for short). This introduces an important difference with
1098 # the previous versions of Autoconf: you cannot use m4_require if you
1099 # are not inside an m4_defun'd macro, and especially, you cannot
1100 # m4_require directly from the top level.
1102 # We have not tried to simulate the old behavior (better yet, we
1103 # diagnose it), because it is too dangerous: a macro m4_require'd from
1104 # the top level is expanded before the body of `configure', i.e., before
1105 # any other test was run. I let you imagine the result of requiring
1106 # AC_STDC_HEADERS for instance, before AC_PROG_CC was actually run....
1108 # After AC_INIT was run, the current diversion is BODY.
1111 # diversion stack: BODY |-
1113 # * TEST1 is expanded
1114 # The prologue of TEST1 sets _m4_divert_dump, which is the diversion
1115 # where the current elaboration will be dumped, to the current
1116 # diversion. It also m4_divert_push to GROW, where the full
1117 # expansion of TEST1 and its dependencies will be elaborated.
1120 # diversions: GROW, BODY |-
1122 # * TEST1 requires TEST2a
1123 # _m4_require_call m4_divert_pushes another temporary diversion,
1124 # GROW - 1, and expands TEST2a in there.
1128 # diversions: GROW - 1, GROW, BODY |-
1129 # Than the content of the temporary diversion is moved to DUMP and the
1130 # temporary diversion is popped.
1133 # diversions: GROW, BODY |-
1135 # * TEST1 requires TEST2b
1136 # Again, _m4_require_call pushes GROW - 1 and heads to expand TEST2b.
1139 # diversions: GROW - 1, GROW, BODY |-
1141 # * TEST2b requires TEST3
1142 # _m4_require_call pushes GROW - 2 and expands TEST3 here.
1143 # (TEST3 requires TEST2a, but TEST2a has already been m4_provide'd, so
1148 # diversions: GROW - 2, GROW - 1, GROW, BODY |-
1149 # Than the diversion is appended to DUMP, and popped.
1151 # BODY: TEST2a; TEST3
1152 # diversions: GROW - 1, GROW, BODY |-
1154 # * TEST1 requires TEST2b (contd.)
1155 # The content of TEST2b is expanded...
1157 # BODY: TEST2a; TEST3
1159 # diversions: GROW - 1, GROW, BODY |-
1160 # ... and moved to DUMP.
1162 # BODY: TEST2a; TEST3; TEST2b
1163 # diversions: GROW, BODY |-
1165 # * TEST1 is expanded: epilogue
1166 # TEST1's own content is in GROW...
1168 # BODY: TEST2a; TEST3; TEST2b
1170 # diversions: BODY |-
1171 # ... and it's epilogue moves it to DUMP and then undefines DUMP.
1173 # BODY: TEST2a; TEST3; TEST2b; TEST1
1174 # diversions: BODY |-
1177 # 2. Keeping track of the expansion stack
1178 # =======================================
1180 # When M4 expansion goes wrong it is often extremely hard to find the
1181 # path amongst macros that drove to the failure. What is needed is
1182 # the stack of macro `calls'. One could imagine that GNU M4 would
1183 # maintain a stack of macro expansions, unfortunately it doesn't, so
1184 # we do it by hand. This is of course extremely costly, but the help
1185 # this stack provides is worth it. Nevertheless to limit the
1186 # performance penalty this is implemented only for m4_defun'd macros,
1187 # not for define'd macros.
1189 # The scheme is simplistic: each time we enter an m4_defun'd macros,
1190 # we prepend its name in m4_expansion_stack, and when we exit the
1191 # macro, we remove it (thanks to pushdef/popdef).
1193 # In addition, we want to detect circular m4_require dependencies.
1194 # Each time we expand a macro FOO we define _m4_expanding(FOO); and
1195 # m4_require(BAR) simply checks whether _m4_expanding(BAR) is defined.
1198 # m4_expansion_stack_push(TEXT)
1199 # -----------------------------
1200 m4_define([m4_expansion_stack_push],
1201 [m4_pushdef([m4_expansion_stack],
1202 [$1]m4_ifdef([m4_expansion_stack], [
1203 m4_defn([m4_expansion_stack])]))])
1206 # m4_expansion_stack_pop
1207 # ----------------------
1208 m4_define([m4_expansion_stack_pop],
1209 [m4_popdef([m4_expansion_stack])])
1212 # m4_expansion_stack_dump
1213 # -----------------------
1214 # Dump the expansion stack.
1215 m4_define([m4_expansion_stack_dump],
1216 [m4_ifdef([m4_expansion_stack],
1217 [m4_errprintn(m4_defn([m4_expansion_stack]))])dnl
1218 m4_errprintn(m4_location[: the top level])])
1223 # This diversion is used by the m4_defun/m4_require machinery. It is
1224 # important to keep room before GROW because for each nested
1225 # AC_REQUIRE we use an additional diversion (i.e., two m4_require's
1226 # will use GROW - 2. More than 3 levels has never seemed to be
1231 # m4_require'd code, 2 level deep
1233 # m4_require'd code, 1 level deep
1235 # m4_defun'd macros are elaborated here.
1237 m4_define([_m4_divert(GROW)], 10000)
1240 # _m4_defun_pro(MACRO-NAME)
1241 # -------------------------
1242 # The prologue for Autoconf macros.
1243 m4_define([_m4_defun_pro],
1244 [m4_ifndef([m4_expansion_stack], [_m4_defun_pro_outer[]])dnl
1245 m4_expansion_stack_push(m4_defn([m4_location($1)])[: $1 is expanded from...])dnl
1246 m4_pushdef([_m4_expanding($1)])dnl
1249 m4_define([_m4_defun_pro_outer],
1250 [m4_copy([_m4_divert_diversion], [_m4_divert_dump])dnl
1251 m4_divert_push([GROW])dnl
1254 # _m4_defun_epi(MACRO-NAME)
1255 # -------------------------
1256 # The Epilogue for Autoconf macros. MACRO-NAME only helps tracing
1257 # the PRO/EPI pairs.
1258 m4_define([_m4_defun_epi],
1259 [m4_popdef([_m4_expanding($1)])dnl
1260 m4_expansion_stack_pop()dnl
1261 m4_ifndef([m4_expansion_stack], [_m4_defun_epi_outer[]])dnl
1265 m4_define([_m4_defun_epi_outer],
1266 [m4_undefine([_m4_divert_dump])dnl
1267 m4_divert_pop([GROW])dnl
1268 m4_undivert([GROW])dnl
1272 # m4_defun(NAME, EXPANSION)
1273 # -------------------------
1274 # Define a macro which automatically provides itself. Add machinery
1275 # so the macro automatically switches expansion to the diversion
1276 # stack if it is not already using it. In this case, once finished,
1277 # it will bring back all the code accumulated in the diversion stack.
1278 # This, combined with m4_require, achieves the topological ordering of
1279 # macros. We don't use this macro to define some frequently called
1280 # macros that are not involved in ordering constraints, to save m4
1282 m4_define([m4_defun],
1283 [m4_define([m4_location($1)], m4_location)dnl
1285 [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])
1288 # m4_defun_once(NAME, EXPANSION)
1289 # ------------------------------
1290 # As m4_defun, but issues the EXPANSION only once, and warns if used
1292 m4_define([m4_defun_once],
1293 [m4_define([m4_location($1)], m4_location)dnl
1295 [m4_provide_if([$1],
1296 [m4_warn([syntax], [$1 invoked multiple times])],
1297 [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])])
1300 # m4_pattern_forbid(ERE, [WHY])
1301 # -----------------------------
1302 # Declare that no token matching the forbidden extended regular
1303 # expression ERE should be seen in the output unless...
1304 m4_define([m4_pattern_forbid], [])
1307 # m4_pattern_allow(ERE)
1308 # ---------------------
1309 # ... that token also matches the allowed extended regular expression ERE.
1310 # Both used via traces.
1311 m4_define([m4_pattern_allow], [])
1314 ## --------------------------------- ##
1315 ## 11. Dependencies between macros. ##
1316 ## --------------------------------- ##
1319 # m4_before(THIS-MACRO-NAME, CALLED-MACRO-NAME)
1320 # ---------------------------------------------
1321 # Issue a warning if CALLED-MACRO-NAME was called before THIS-MACRO-NAME.
1322 m4_define([m4_before],
1323 [m4_provide_if([$2],
1324 [m4_warn([syntax], [$2 was called before $1])])])
1327 # m4_require(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
1328 # -----------------------------------------------------------
1329 # If NAME-TO-CHECK has never been expanded (actually, if it is not
1330 # m4_provide'd), expand BODY-TO-EXPAND *before* the current macro
1331 # expansion. Once expanded, emit it in _m4_divert_dump. Keep track
1332 # of the m4_require chain in m4_expansion_stack.
1334 # The normal cases are:
1336 # - NAME-TO-CHECK == BODY-TO-EXPAND
1337 # Which you can use for regular macros with or without arguments, e.g.,
1338 # m4_require([AC_PROG_CC], [AC_PROG_CC])
1339 # m4_require([AC_CHECK_HEADERS(limits.h)], [AC_CHECK_HEADERS(limits.h)])
1340 # which is just the same as
1341 # m4_require([AC_PROG_CC])
1342 # m4_require([AC_CHECK_HEADERS(limits.h)])
1344 # - BODY-TO-EXPAND == m4_indir([NAME-TO-CHECK])
1345 # In the case of macros with irregular names. For instance:
1346 # m4_require([AC_LANG_COMPILER(C)], [indir([AC_LANG_COMPILER(C)])])
1347 # which means `if the macro named `AC_LANG_COMPILER(C)' (the parens are
1348 # part of the name, it is not an argument) has not been run, then
1351 # m4_require([AC_LANG_COMPILER(C)], [AC_LANG_COMPILER(C)])
1352 # then m4_require would have tried to expand `AC_LANG_COMPILER(C)', i.e.,
1353 # call the macro `AC_LANG_COMPILER' with `C' as argument.
1355 # You could argue that `AC_LANG_COMPILER', when it receives an argument
1356 # such as `C' should dispatch the call to `AC_LANG_COMPILER(C)'. But this
1357 # `extension' prevents `AC_LANG_COMPILER' from having actual arguments that
1358 # it passes to `AC_LANG_COMPILER(C)'.
1359 m4_define([m4_require],
1360 [m4_ifdef([_m4_expanding($1)],
1361 [m4_fatal([$0: circular dependency of $1])])dnl
1362 m4_ifndef([_m4_divert_dump],
1363 [m4_fatal([$0($1): cannot be used outside of an ]dnl
1364 m4_bmatch([$0], [^AC_], [[AC_DEFUN]], [[m4_defun]])['d macro])])dnl
1367 [_m4_require_call([$1], [$2])])dnl
1371 # _m4_require_call(BODY-TO-EXPAND)
1372 # --------------------------------
1373 # If m4_require decides to expand the body, it calls this macro.
1374 m4_define([_m4_require_call],
1375 [m4_define([_m4_divert_grow], m4_decr(_m4_divert_grow))dnl
1376 m4_divert_push(_m4_divert_grow)dnl
1377 m4_default([$2], [$1])
1381 [$1 is m4_require'd but not m4_defun'd])])dnl
1382 m4_divert(m4_defn([_m4_divert_dump]))dnl
1383 m4_undivert(_m4_divert_grow)dnl
1384 m4_divert_pop(_m4_divert_grow)dnl
1385 m4_define([_m4_divert_grow], m4_incr(_m4_divert_grow))dnl
1391 # The counter for _m4_require_call.
1392 m4_define([_m4_divert_grow], _m4_divert([GROW]))
1395 # m4_expand_once(TEXT, [WITNESS = TEXT])
1396 # --------------------------------------
1397 # If TEXT has never been expanded, expand it *here*. Use WITNESS as
1398 # as a memory that TEXT has already been expanded.
1399 m4_define([m4_expand_once],
1400 [m4_provide_if(m4_ifval([$2], [[$2]], [[$1]]),
1402 [m4_provide(m4_ifval([$2], [[$2]], [[$1]]))[]$1])])
1405 # m4_provide(MACRO-NAME)
1406 # ----------------------
1407 m4_define([m4_provide],
1408 [m4_define([m4_provide($1)])])
1411 # m4_provide_if(MACRO-NAME, IF-PROVIDED, IF-NOT-PROVIDED)
1412 # -------------------------------------------------------
1413 # If MACRO-NAME is provided do IF-PROVIDED, else IF-NOT-PROVIDED.
1414 # The purpose of this macro is to provide the user with a means to
1415 # check macros which are provided without letting her know how the
1416 # information is coded.
1417 m4_define([m4_provide_if],
1418 [m4_ifdef([m4_provide($1)],
1422 ## --------------------- ##
1423 ## 12. Text processing. ##
1424 ## --------------------- ##
1431 m4_define([m4_cr_letters], [abcdefghijklmnopqrstuvwxyz])
1432 m4_define([m4_cr_LETTERS], [ABCDEFGHIJKLMNOPQRSTUVWXYZ])
1433 m4_define([m4_cr_Letters],
1434 m4_defn([m4_cr_letters])dnl
1435 m4_defn([m4_cr_LETTERS])dnl
1441 m4_define([m4_cr_digits], [0123456789])
1446 m4_define([m4_cr_alnum],
1447 m4_defn([m4_cr_Letters])dnl
1448 m4_defn([m4_cr_digits])dnl
1454 # -------------------------------
1455 m4_define([m4_cr_symbols1],
1456 m4_defn([m4_cr_Letters])dnl
1459 m4_define([m4_cr_symbols2],
1460 m4_defn([m4_cr_symbols1])dnl
1461 m4_defn([m4_cr_digits])dnl
1466 # The character range representing everything, with `-' as the last
1467 # character, since it is special to m4_translit. Use with care, because
1468 # it contains characters special to M4 (fortunately, both ASCII and EBCDIC
1469 # have [] in order, so m4_defn([m4_cr_all]) remains a valid string). It
1470 # also contains characters special to terminals, so it should never be
1471 # displayed in an error message. Also, attempts to map [ and ] to other
1472 # characters via m4_translit must deal with the fact that m4_translit does
1473 # not add quotes to the output.
1475 # It is mainly useful in generating inverted character range maps, for use
1476 # in places where m4_translit is faster than an equivalent m4_bpatsubst;
1477 # the regex `[^a-z]' is equivalent to:
1478 # m4_translit(m4_dquote(m4_defn([m4_cr_all])), [a-z])
1479 m4_define([m4_cr_all],
1480 m4_translit(m4_dquote(m4_format(m4_dquote(m4_for(
1481 ,1,255,,[[%c]]))m4_for([i],1,255,,[,i]))), [-])-)
1484 # _m4_define_cr_not(CATEGORY)
1485 # ---------------------------
1486 # Define m4_cr_not_CATEGORY as the inverse of m4_cr_CATEGORY.
1487 m4_define([_m4_define_cr_not],
1488 [m4_define([m4_cr_not_$1],
1489 m4_translit(m4_dquote(m4_defn([m4_cr_all])),
1490 m4_defn([m4_cr_$1])))])
1498 # m4_cr_not_symbols1
1499 # m4_cr_not_symbols2
1500 # ------------------
1501 # Inverse character sets
1502 _m4_define_cr_not([letters])
1503 _m4_define_cr_not([LETTERS])
1504 _m4_define_cr_not([Letters])
1505 _m4_define_cr_not([digits])
1506 _m4_define_cr_not([alnum])
1507 _m4_define_cr_not([symbols1])
1508 _m4_define_cr_not([symbols2])
1513 # Expands to a newline. Exists for formatting reasons.
1514 m4_define([m4_newline], [
1518 # m4_re_escape(STRING)
1519 # --------------------
1520 # Escape RE active characters in STRING.
1521 m4_define([m4_re_escape],
1523 [[][*+.?\^$]], [\\\&])])
1528 # Regexp for `[a-zA-Z_0-9]*'
1529 # m4_dquote provides literal [] for the character class.
1530 m4_define([m4_re_string],
1531 m4_dquote(m4_defn([m4_cr_symbols2]))dnl
1538 # Regexp for `[a-zA-Z_][a-zA-Z_0-9]*'
1539 m4_define([m4_re_word],
1540 m4_dquote(m4_defn([m4_cr_symbols1]))dnl
1541 m4_defn([m4_re_string])dnl
1545 # m4_tolower(STRING)
1546 # m4_toupper(STRING)
1547 # ------------------
1548 # These macros convert STRING to lowercase or uppercase.
1550 # Rather than expand the m4_defn each time, we inline them up front.
1551 m4_define([m4_tolower],
1552 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_LETTERS]))[,
1553 ]m4_dquote(m4_defn([m4_cr_letters]))[)])
1554 m4_define([m4_toupper],
1555 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_letters]))[,
1556 ]m4_dquote(m4_defn([m4_cr_LETTERS]))[)])
1559 # m4_split(STRING, [REGEXP])
1560 # --------------------------
1562 # Split STRING into an m4 list of quoted elements. The elements are
1563 # quoted with [ and ]. Beginning spaces and end spaces *are kept*.
1564 # Use m4_strip to remove them.
1566 # REGEXP specifies where to split. Default is [\t ]+.
1568 # If STRING is empty, the result is an empty list.
1570 # Pay attention to the m4_changequotes. When m4 reads the definition of
1571 # m4_split, it still has quotes set to [ and ]. Luckily, these are matched
1572 # in the macro body, so the definition is stored correctly. Use the same
1573 # alternate quotes as m4_noquote; it must be unlikely to appear in $1.
1575 # Also, notice that $1 is quoted twice, since we want the result to
1576 # be quoted. Then you should understand that the argument of
1577 # patsubst is -=<{STRING}>=- (i.e., with additional -=<{ and }>=-).
1579 # This macro is safe on active symbols, i.e.:
1580 # m4_define(active, ACTIVE)
1581 # m4_split([active active ])end
1582 # => [active], [active], []end
1584 # Optimize on regex of ` ' (space), since m4_foreach_w already guarantees
1585 # that the list contains single space separators, and a common case is
1586 # splitting a single-element list. This macro is called frequently,
1587 # so avoid unnecessary dnl inside the definition.
1588 m4_define([m4_split],
1589 [m4_if([$1], [], [],
1590 [$2], [ ], [m4_if(m4_index([$1], [ ]), [-1], [[[$1]]], [_$0($@)])],
1591 [$2], [], [_$0([$1], [[ ]+])],
1594 m4_define([_m4_split],
1595 [m4_changequote(-=<{,}>=-)]dnl
1596 [[m4_bpatsubst(-=<{-=<{$1}>=-}>=-, -=<{$2}>=-,
1597 -=<{], [}>=-)]m4_changequote([, ])])
1601 # m4_flatten(STRING)
1602 # ------------------
1603 # If STRING contains end of lines, replace them with spaces. If there
1604 # are backslashed end of lines, remove them. This macro is safe with
1606 # m4_define(active, ACTIVE)
1607 # m4_flatten([active
1610 # => active activeend
1612 # In m4, m4_bpatsubst is expensive, so first check for a newline.
1613 m4_define([m4_flatten],
1614 [m4_if(m4_index([$1], [
1616 [m4_translit(m4_bpatsubst([[[$1]]], [\\
1623 # Expands into STRING with tabs and spaces singled out into a single
1624 # space, and removing leading and trailing spaces.
1626 # This macro is robust to active symbols.
1627 # m4_define(active, ACTIVE)
1628 # m4_strip([ active <tab> <tab>active ])end
1629 # => active activeend
1631 # First, notice that we guarantee trailing space. Why? Because regular
1632 # expressions are greedy, and `.* ?' would alway groups the space into the
1633 # .* portion. The algorithm is simpler by avoiding `?' at the end. The
1634 # algorithm correctly strips everything if STRING is just ` '.
1636 # Then notice the second pattern: it is in charge of removing the
1637 # leading/trailing spaces. Why not just `[^ ]'? Because they are
1638 # applied to over-quoted strings, i.e. more or less [STRING], due
1639 # to the limitations of m4_bpatsubsts. So the leading space in STRING
1640 # is the *second* character; equally for the trailing space.
1641 m4_define([m4_strip],
1642 [m4_bpatsubsts([$1 ],
1644 [^. ?\(.*\) .$], [[[\1]]])])
1647 # m4_normalize(STRING)
1648 # --------------------
1649 # Apply m4_flatten and m4_strip to STRING.
1651 # The argument is quoted, so that the macro is robust to active symbols:
1653 # m4_define(active, ACTIVE)
1654 # m4_normalize([ act\
1657 # => active activeend
1659 m4_define([m4_normalize],
1660 [m4_strip(m4_flatten([$1]))])
1664 # m4_join(SEP, ARG1, ARG2...)
1665 # ---------------------------
1666 # Produce ARG1SEPARG2...SEPARGn. Avoid back-to-back SEP when a given ARG
1667 # is the empty string.
1669 # Since the number of arguments to join can be arbitrarily long, we
1670 # want to avoid having more than one $@ in the macro definition;
1671 # otherwise, the expansion would require twice the memory of the already
1672 # long list. Hence, m4_join merely looks for the first non-empty element,
1673 # and outputs just that element; while _m4_join looks for all non-empty
1674 # elements, and outputs them following a separator. The final trick to
1675 # note is that we decide between recursing with $0 or _$0 based on the
1676 # nested m4_if ending with `_'.
1677 m4_define([m4_join],
1678 [m4_if([$#], [1], [],
1680 [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift2($@))])])
1681 m4_define([_m4_join],
1682 [m4_if([$#$2], [2], [],
1683 [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift2($@))])])
1686 # m4_append(MACRO-NAME, STRING, [SEPARATOR])
1687 # ------------------------------------------
1688 # Redefine MACRO-NAME to hold its former content plus `SEPARATOR`'STRING'
1689 # at the end. It is valid to use this macro with MACRO-NAME undefined,
1690 # in which case no SEPARATOR is added. Be aware that the criterion is
1691 # `not being defined', and not `not being empty'.
1693 # Note that neither STRING nor SEPARATOR are expanded here; rather, when
1694 # you expand MACRO-NAME, they will be expanded at that point in time.
1696 # This macro is robust to active symbols. It can be used to grow
1699 # | m4_define(active, ACTIVE)dnl
1700 # | m4_append([sentence], [This is an])dnl
1701 # | m4_append([sentence], [ active ])dnl
1702 # | m4_append([sentence], [symbol.])dnl
1704 # | m4_undefine([active])dnl
1706 # => This is an ACTIVE symbol.
1707 # => This is an active symbol.
1709 # It can be used to define hooks.
1711 # | m4_define(active, ACTIVE)dnl
1712 # | m4_append([hooks], [m4_define([act1], [act2])])dnl
1713 # | m4_append([hooks], [m4_define([act2], [active])])dnl
1714 # | m4_undefine([active])dnl
1722 # It can also be used to create lists, although this particular usage was
1723 # broken prior to autoconf 2.62.
1724 # | m4_append([list], [one], [, ])dnl
1725 # | m4_append([list], [two], [, ])dnl
1726 # | m4_append([list], [three], [, ])dnl
1729 # => one, two, three
1730 # => [one],[two],[three]
1732 # Use m4_builtin to avoid overhead of m4_defn.
1733 m4_define([m4_append],
1735 m4_ifdef([$1], [m4_builtin([defn], [$1])[$3]])[$2])])
1738 # m4_append_uniq(MACRO-NAME, STRING, [SEPARATOR], [IF-UNIQ], [IF-DUP])
1739 # --------------------------------------------------------------------
1740 # Like `m4_append', but append only if not yet present. Additionally,
1741 # expand IF-UNIQ if STRING was appended, or IF-DUP if STRING was already
1743 m4_define([m4_append_uniq],
1745 [m4_if(m4_index([$3]m4_builtin([defn], [$1])[$3], [$3$2$3]), [-1],
1746 [m4_append([$1], [$2], [$3])$4], [$5])],
1747 [m4_append([$1], [$2], [$3])$4])])
1750 # m4_text_wrap(STRING, [PREFIX], [FIRST-PREFIX], [WIDTH])
1751 # -------------------------------------------------------
1752 # Expands into STRING wrapped to hold in WIDTH columns (default = 79).
1753 # If PREFIX is given, each line is prefixed with it. If FIRST-PREFIX is
1754 # specified, then the first line is prefixed with it. As a special case,
1755 # if the length of FIRST-PREFIX is greater than that of PREFIX, then
1756 # FIRST-PREFIX will be left alone on the first line.
1758 # Typical outputs are:
1760 # m4_text_wrap([Short string */], [ ], [/* ], 20)
1761 # => /* Short string */
1763 # m4_text_wrap([Much longer string */], [ ], [/* ], 20)
1767 # m4_text_wrap([Short doc.], [ ], [ --short ], 30)
1768 # => --short Short doc.
1770 # m4_text_wrap([Short doc.], [ ], [ --too-wide ], 30)
1774 # m4_text_wrap([Super long documentation.], [ ], [ --too-wide ], 30)
1779 # FIXME: there is no checking of a longer PREFIX than WIDTH, but do
1780 # we really want to bother with people trying each single corner
1783 # This macro does not leave a trailing space behind the last word of a line,
1784 # which complicates it a bit. The algorithm is otherwise stupid and simple:
1785 # all the words are preceded by m4_Separator which is defined to empty for
1786 # the first word, and then ` ' (single space) for all the others.
1788 # The algorithm overquotes m4_Prefix1 to avoid m4_defn overhead, and bypasses
1789 # m4_popdef overhead with m4_builtin since no user macro expansion occurs in
1791 m4_define([m4_text_wrap],
1792 [m4_pushdef([m4_Prefix], [$2])dnl
1793 m4_pushdef([m4_Prefix1], m4_dquote(m4_default([$3], [m4_Prefix])))dnl
1794 m4_pushdef([m4_Width], m4_default([$4], 79))dnl
1795 m4_pushdef([m4_Cursor], m4_qlen(m4_Prefix1))dnl
1796 m4_pushdef([m4_Separator], [])dnl
1798 m4_cond([m4_eval(m4_qlen(m4_Prefix1) > m4_len(m4_Prefix))],
1799 [1], [m4_define([m4_Cursor], m4_len(m4_Prefix))
1801 [m4_eval(m4_qlen(m4_Prefix1) < m4_len(m4_Prefix))],
1803 [m4_define([m4_Cursor], m4_len(m4_Prefix))[]dnl
1805 m4_max([0], m4_eval(m4_len(m4_Prefix) - m4_qlen(m4_Prefix1))),
1807 m4_foreach_w([m4_Word], [$1],
1808 [m4_define([m4_Cursor],
1809 m4_eval(m4_Cursor + m4_qlen(m4_builtin([defn], [m4_Word])) + 1))dnl
1810 dnl New line if too long, else insert a space unless it is the first
1812 m4_if(m4_eval(m4_Cursor > m4_Width),
1813 1, [m4_define([m4_Cursor],
1814 m4_eval(m4_len(m4_Prefix)
1815 + m4_qlen(m4_builtin([defn], [m4_Word])) + 1))]
1817 [m4_Separator])[]dnl
1818 m4_builtin([defn], [m4_Word])[]dnl
1819 m4_define([m4_Separator], [ ])])dnl
1820 m4_builtin([popdef], [m4_Separator])dnl
1821 m4_builtin([popdef], [m4_Cursor])dnl
1822 m4_builtin([popdef], [m4_Width])dnl
1823 m4_builtin([popdef], [m4_Prefix1])dnl
1824 m4_builtin([popdef], [m4_Prefix])dnl
1828 # m4_text_box(MESSAGE, [FRAME-CHARACTER = `-'])
1829 # ---------------------------------------------
1830 # Turn MESSAGE into:
1834 # using FRAME-CHARACTER in the border.
1835 m4_define([m4_text_box],
1836 [m4_pushdef([m4_Border],
1837 m4_translit(m4_format([%*s], m4_qlen(m4_quote($1)), []),
1838 [ ], m4_if([$2], [], [[-]], [[$2]])))dnl
1839 @%:@@%:@ m4_Border @%:@@%:@
1840 @%:@@%:@ $1 @%:@@%:@
1841 @%:@@%:@ m4_Border @%:@@%:@dnl
1842 m4_builtin([popdef], [m4_Border])dnl
1848 # Expands to the length of STRING after autom4te converts all quadrigraphs.
1850 # Avoid bpatsubsts for the common case of no quadrigraphs.
1851 m4_define([m4_qlen],
1852 [m4_if(m4_index([$1], [@]), [-1], [m4_len([$1])],
1853 [m4_len(m4_bpatsubsts([[$1]], [@\(<:\|:>\|S|\|%:\)@], [P], [@&t@]))])])
1858 # Expands to the net change in the length of STRING from autom4te converting the
1859 # quadrigraphs in STRING. This number is always negative or zero.
1860 m4_define([m4_qdelta],
1861 [m4_eval(m4_qlen([$1]) - m4_len([$1]))])
1865 ## ----------------------- ##
1866 ## 13. Number processing. ##
1867 ## ----------------------- ##
1871 # Compare two integer expressions.
1876 [m4_eval((([$1]) > ([$2])) - (([$1]) < ([$2])))])
1882 # Compare the two lists of integer expressions A and B. For instance:
1883 # m4_list_cmp([1, 0], [1]) -> 0
1884 # m4_list_cmp([1, 0], [1, 0]) -> 0
1885 # m4_list_cmp([1, 2], [1, 0]) -> 1
1886 # m4_list_cmp([1, 2, 3], [1, 2]) -> 1
1887 # m4_list_cmp([1, 2, -3], [1, 2]) -> -1
1888 # m4_list_cmp([1, 0], [1, 2]) -> -1
1889 # m4_list_cmp([1], [1, 2]) -> -1
1890 # m4_define([xa], [oops])dnl
1891 # m4_list_cmp([[0xa]], [5+5]) -> 0
1893 # Rather than face the overhead of m4_case, we use a helper function whose
1894 # expansion includes the name of the macro to invoke on the tail, either
1895 # m4_ignore or m4_unquote. This is particularly useful when comparing
1896 # long lists, since less text is being expanded to determine when to recurse.
1897 m4_define([m4_list_cmp],
1898 [m4_if([$1$2], [], 0,
1899 [$1], [], [$0(0, [$2])],
1900 [$2], [], [$0([$1], 0)],
1902 [_$0(m4_cmp(m4_car($1), m4_car($2)))([$0(m4_cdr($1), m4_cdr($2))])])])
1903 m4_define([_m4_list_cmp],
1904 [m4_if([$1], 0, [m4_unquote], [$1m4_ignore])])
1909 # Return the decimal value of the maximum (or minimum) in a series of
1910 # integer expressions.
1912 # M4 1.4.x doesn't provide ?:. Hence this huge m4_eval. Avoid m4_eval
1913 # if both arguments are identical, but be aware of m4_max(0xa, 10) (hence
1914 # the use of <=, not just <, in the second multiply).
1916 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
1917 [$#], [1], [m4_eval([$1])],
1918 [$#$1], [2$2], [m4_eval([$1])],
1920 [m4_eval((([$1]) > ([$2])) * ([$1]) + (([$1]) <= ([$2])) * ([$2]))],
1921 [$0($0([$1], [$2]), m4_shift2($@))])])
1923 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
1924 [$#], [1], [m4_eval([$1])],
1925 [$#$1], [2$2], [m4_eval([$1])],
1927 [m4_eval((([$1]) < ([$2])) * ([$1]) + (([$1]) >= ([$2])) * ([$2]))],
1928 [$0($0([$1], [$2]), m4_shift2($@))])])
1933 # The sign of the integer expression A.
1934 m4_define([m4_sign],
1935 [m4_eval((([$1]) > 0) - (([$1]) < 0))])
1939 ## ------------------------ ##
1940 ## 14. Version processing. ##
1941 ## ------------------------ ##
1944 # m4_version_unletter(VERSION)
1945 # ----------------------------
1946 # Normalize beta version numbers with letters to numeric expressions, which
1947 # can then be handed to m4_eval for the purpose of comparison.
1949 # Nl -> (N+1).-1.(l#)
1952 # [2.14a] -> [2.14+1.-1.[0r36:a]] -> 2.15.-1.10
1953 # [2.14b] -> [2.15+1.-1.[0r36:b]] -> 2.15.-1.11
1954 # [2.61aa.b] -> [2.61+1.-1.[0r36:aa],+1.-1.[0r36:b]] -> 2.62.-1.370.1.-1.11
1956 # This macro expects reasonable version numbers, but can handle double
1957 # letters and does not expand one-letter macros. Inline constant expansions,
1958 # to avoid m4_defn overhead. _m4_version_unletter is the real workhorse
1959 # used by m4_version_compare, but since [0r36:a] is less readable than 10,
1960 # we provide a wrapper for human use.
1961 m4_define([m4_version_unletter],
1962 [m4_map_sep([m4_eval], [.], _$0([$1]))])
1963 m4_define([_m4_version_unletter],
1964 [m4_translit(m4_bpatsubst([[[$1]]], ]dnl
1965 m4_dquote(m4_dquote(m4_defn([m4_cr_Letters])))[[+],
1970 # m4_version_compare(VERSION-1, VERSION-2)
1971 # ----------------------------------------
1972 # Compare the two version numbers and expand into
1973 # -1 if VERSION-1 < VERSION-2
1976 m4_define([m4_version_compare],
1977 [m4_list_cmp(_m4_version_unletter([$1]), _m4_version_unletter([$2]))])
1981 # m4_PACKAGE_TARNAME
1982 # m4_PACKAGE_VERSION
1984 # m4_PACKAGE_BUGREPORT
1985 # --------------------
1986 m4_include([m4sugar/version.m4])
1989 # m4_version_prereq(VERSION, [IF-OK], [IF-NOT = FAIL])
1990 # ----------------------------------------------------
1991 # Check this Autoconf version against VERSION.
1992 m4_define([m4_version_prereq],
1993 [m4_if(m4_version_compare(]m4_dquote(m4_defn([m4_PACKAGE_VERSION]))[, [$1]),
1996 [m4_fatal([Autoconf version $1 or higher is required],
2002 ## ------------------- ##
2003 ## 15. File handling. ##
2004 ## ------------------- ##
2007 # It is a real pity that M4 comes with no macros to bind a diversion
2008 # to a file. So we have to deal without, which makes us a lot more
2009 # fragile than we should.
2012 # m4_file_append(FILE-NAME, CONTENT)
2013 # ----------------------------------
2014 m4_define([m4_file_append],
2015 [m4_syscmd([cat >>$1 <<_m4eof
2019 m4_if(m4_sysval, [0], [],
2020 [m4_fatal([$0: cannot write: $1])])])
2024 ## ------------------------ ##
2025 ## 16. Setting M4sugar up. ##
2026 ## ------------------------ ##
2031 m4_define([m4_init],
2032 [# All the M4sugar macros start with `m4_', except `dnl' kept as is
2033 # for sake of simplicity.
2034 m4_pattern_forbid([^_?m4_])
2035 m4_pattern_forbid([^dnl$])
2037 # _m4_divert_diversion should be defined:
2038 m4_divert_push([KILL])
2040 # Check the divert push/pop perfect balance.
2041 m4_wrap([m4_divert_pop([])
2042 m4_ifdef([_m4_divert_diversion],
2043 [m4_fatal([$0: unbalanced m4_divert_push:]_m4_divert_n_stack)])[]])