Provide O(n) replacement macros for M4 1.4.x.
[autoconf.git] / lib / m4sugar / m4sugar.m4
blob14809532f8136352b76a68ee6e395b7a681d1aa9
1 divert(-1)#                                                  -*- Autoconf -*-
2 # This file is part of Autoconf.
3 # Base M4 layer.
4 # Requires GNU M4.
6 # Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7 # 2008 Free Software Foundation, Inc.
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2, or (at your option)
12 # 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, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 # 02110-1301, USA.
24 # As a special exception, the Free Software Foundation gives unlimited
25 # permission to copy, distribute and modify the configure scripts that
26 # are the output of Autoconf.  You need not follow the terms of the GNU
27 # General Public License when using or distributing such scripts, even
28 # though portions of the text of Autoconf appear in them.  The GNU
29 # General Public License (GPL) does govern all other use of the material
30 # that constitutes the Autoconf program.
32 # Certain portions of the Autoconf source text are designed to be copied
33 # (in certain cases, depending on the input) into the output of
34 # Autoconf.  We call these the "data" portions.  The rest of the Autoconf
35 # source text consists of comments plus executable code that decides which
36 # of the data portions to output in any given case.  We call these
37 # comments and executable code the "non-data" portions.  Autoconf never
38 # copies any of the non-data portions into its output.
40 # This special exception to the GPL applies to versions of Autoconf
41 # released by the Free Software Foundation.  When you make and
42 # distribute a modified version of Autoconf, you may extend this special
43 # exception to the GPL to apply to your modified version as well, *unless*
44 # your modified version has the potential to copy into its output some
45 # of the text that was the non-data portion of the version that you started
46 # with.  (In other words, unless your change moves or copies text from
47 # the non-data portions to the data portions.)  If your modification has
48 # such potential, you must delete any notice of this special exception
49 # to the GPL from your modified version.
51 # Written by Akim Demaille.
54 # Set the quotes, whatever the current quoting system.
55 changequote()
56 changequote([, ])
58 # Some old m4's don't support m4exit.  But they provide
59 # equivalent functionality by core dumping because of the
60 # long macros we define.
61 ifdef([__gnu__], ,
62 [errprint(M4sugar requires GNU M4. Install it before installing M4sugar or
63 set the M4 environment variable to its absolute file name.)
64 m4exit(2)])
67 ## ------------------------------- ##
68 ## 1. Simulate --prefix-builtins.  ##
69 ## ------------------------------- ##
71 # m4_define
72 # m4_defn
73 # m4_undefine
74 define([m4_define],   defn([define]))
75 define([m4_defn],     defn([defn]))
76 define([m4_undefine], defn([undefine]))
78 m4_undefine([define])
79 m4_undefine([defn])
80 m4_undefine([undefine])
83 # m4_copy(SRC, DST)
84 # -----------------
85 # Define DST as the definition of SRC.
86 # What's the difference between:
87 # 1. m4_copy([from], [to])
88 # 2. m4_define([to], [from($@)])
89 # Well, obviously 1 is more expensive in space.  Maybe 2 is more expensive
90 # in time, but because of the space cost of 1, it's not that obvious.
91 # Nevertheless, one huge difference is the handling of `$0'.  If `from'
92 # uses `$0', then with 1, `to''s `$0' is `to', while it is `from' in 2.
93 # The user would certainly prefer to see `to'.
94 m4_define([m4_copy],
95 [m4_define([$2], m4_defn([$1]))])
98 # m4_rename(SRC, DST)
99 # -------------------
100 # Rename the macro SRC to DST.
101 m4_define([m4_rename],
102 [m4_copy([$1], [$2])m4_undefine([$1])])
105 # m4_rename_m4(MACRO-NAME)
106 # ------------------------
107 # Rename MACRO-NAME to m4_MACRO-NAME.
108 m4_define([m4_rename_m4],
109 [m4_rename([$1], [m4_$1])])
112 # m4_copy_unm4(m4_MACRO-NAME)
113 # ---------------------------
114 # Copy m4_MACRO-NAME to MACRO-NAME.
115 m4_define([m4_copy_unm4],
116 [m4_copy([$1], m4_bpatsubst([$1], [^m4_\(.*\)], [[\1]]))])
119 # Some m4 internals have names colliding with tokens we might use.
120 # Rename them a` la `m4 --prefix-builtins'.  Conditionals first, since
121 # some subsequent renames are conditional.
122 m4_rename_m4([ifdef])
123 m4_rename([ifelse], [m4_if])
125 m4_rename_m4([builtin])
126 m4_rename_m4([changecom])
127 m4_rename_m4([changequote])
128 m4_ifdef([changeword],dnl conditionally available in 1.4.x
129 [m4_undefine([changeword])])
130 m4_rename_m4([debugfile])
131 m4_rename_m4([debugmode])
132 m4_rename_m4([decr])
133 m4_undefine([divert])
134 m4_rename_m4([divnum])
135 m4_rename_m4([dumpdef])
136 m4_rename_m4([errprint])
137 m4_rename_m4([esyscmd])
138 m4_rename_m4([eval])
139 m4_rename_m4([format])
140 m4_undefine([include])
141 m4_rename_m4([incr])
142 m4_rename_m4([index])
143 m4_rename_m4([indir])
144 m4_rename_m4([len])
145 m4_rename([m4exit], [m4_exit])
146 m4_undefine([m4wrap])
147 m4_ifdef([mkstemp],dnl added in M4 1.4.8
148 [m4_rename_m4([mkstemp])
149 m4_copy([m4_mkstemp], [m4_maketemp])
150 m4_undefine([maketemp])],
151 [m4_rename_m4([maketemp])
152 m4_copy([m4_maketemp], [m4_mkstemp])])
153 m4_rename([patsubst], [m4_bpatsubst])
154 m4_rename_m4([popdef])
155 m4_rename_m4([pushdef])
156 m4_rename([regexp], [m4_bregexp])
157 m4_rename_m4([shift])
158 m4_undefine([sinclude])
159 m4_rename_m4([substr])
160 m4_ifdef([symbols],dnl present only in alpha-quality 1.4o
161 [m4_rename_m4([symbols])])
162 m4_rename_m4([syscmd])
163 m4_rename_m4([sysval])
164 m4_rename_m4([traceoff])
165 m4_rename_m4([traceon])
166 m4_rename_m4([translit])
167 m4_undefine([undivert])
170 ## ------------------- ##
171 ## 2. Error messages.  ##
172 ## ------------------- ##
175 # m4_location
176 # -----------
177 m4_define([m4_location],
178 [__file__:__line__])
181 # m4_errprintn(MSG)
182 # -----------------
183 # Same as `errprint', but with the missing end of line.
184 m4_define([m4_errprintn],
185 [m4_errprint([$1
186 ])])
189 # m4_warning(MSG)
190 # ---------------
191 # Warn the user.
192 m4_define([m4_warning],
193 [m4_errprintn(m4_location[: warning: $1])])
196 # m4_fatal(MSG, [EXIT-STATUS])
197 # ----------------------------
198 # Fatal the user.                                                      :)
199 m4_define([m4_fatal],
200 [m4_errprintn(m4_location[: error: $1])dnl
201 m4_expansion_stack_dump()dnl
202 m4_exit(m4_if([$2],, 1, [$2]))])
205 # m4_assert(EXPRESSION, [EXIT-STATUS = 1])
206 # ----------------------------------------
207 # This macro ensures that EXPRESSION evaluates to true, and exits if
208 # EXPRESSION evaluates to false.
209 m4_define([m4_assert],
210 [m4_if(m4_eval([$1]), 0,
211        [m4_fatal([assert failed: $1], [$2])])])
215 ## ------------- ##
216 ## 3. Warnings.  ##
217 ## ------------- ##
220 # _m4_warn(CATEGORY, MESSAGE, STACK-TRACE)
221 # ----------------------------------------
222 # Report a MESSAGE to the user if the CATEGORY of warnings is enabled.
223 # This is for traces only.
224 # The STACK-TRACE is a \n-separated list of "LOCATION: MESSAGE".
226 # Within m4, the macro is a no-op.  This macro really matters
227 # when autom4te post-processes the trace output.
228 m4_define([_m4_warn], [])
231 # m4_warn(CATEGORY, MESSAGE)
232 # --------------------------
233 # Report a MESSAGE to the user if the CATEGORY of warnings is enabled.
234 m4_define([m4_warn],
235 [_m4_warn([$1], [$2],
236 m4_ifdef([m4_expansion_stack],
237          [_m4_defn([m4_expansion_stack])
238 m4_location[: the top level]]))dnl
243 ## ------------------- ##
244 ## 4. File inclusion.  ##
245 ## ------------------- ##
248 # We also want to neutralize include (and sinclude for symmetry),
249 # but we want to extend them slightly: warn when a file is included
250 # several times.  This is, in general, a dangerous operation, because
251 # too many people forget to quote the first argument of m4_define.
253 # For instance in the following case:
254 #   m4_define(foo, [bar])
255 # then a second reading will turn into
256 #   m4_define(bar, [bar])
257 # which is certainly not what was meant.
259 # m4_include_unique(FILE)
260 # -----------------------
261 # Declare that the FILE was loading; and warn if it has already
262 # been included.
263 m4_define([m4_include_unique],
264 [m4_ifdef([m4_include($1)],
265           [m4_warn([syntax], [file `$1' included several times])])dnl
266 m4_define([m4_include($1)])])
269 # m4_include(FILE)
270 # ----------------
271 # Like the builtin include, but warns against multiple inclusions.
272 m4_define([m4_include],
273 [m4_include_unique([$1])dnl
274 m4_builtin([include], [$1])])
277 # m4_sinclude(FILE)
278 # -----------------
279 # Like the builtin sinclude, but warns against multiple inclusions.
280 m4_define([m4_sinclude],
281 [m4_include_unique([$1])dnl
282 m4_builtin([sinclude], [$1])])
286 ## ------------------------------------ ##
287 ## 5. Additional branching constructs.  ##
288 ## ------------------------------------ ##
290 # Both `m4_ifval' and `m4_ifset' tests against the empty string.  The
291 # difference is that `m4_ifset' is specialized on macros.
293 # In case of arguments of macros, eg. $1, it makes little difference.
294 # In the case of a macro `FOO', you don't want to check `m4_ifval(FOO,
295 # TRUE)', because if `FOO' expands with commas, there is a shifting of
296 # the arguments.  So you want to run `m4_ifval([FOO])', but then you just
297 # compare the *string* `FOO' against `', which, of course fails.
299 # So you want the variation `m4_ifset' that expects a macro name as $1.
300 # If this macro is both defined and defined to a non empty value, then
301 # it runs TRUE, etc.
304 # m4_ifval(COND, [IF-TRUE], [IF-FALSE])
305 # -------------------------------------
306 # If COND is not the empty string, expand IF-TRUE, otherwise IF-FALSE.
307 # Comparable to m4_ifdef.
308 m4_define([m4_ifval],
309 [m4_if([$1], [], [$3], [$2])])
312 # m4_n(TEXT)
313 # ----------
314 # If TEXT is not empty, return TEXT and a new line, otherwise nothing.
315 m4_define([m4_n],
316 [m4_if([$1],
317        [], [],
318            [$1
319 ])])
322 # m4_ifvaln(COND, [IF-TRUE], [IF-FALSE])
323 # --------------------------------------
324 # Same as `m4_ifval', but add an extra newline to IF-TRUE or IF-FALSE
325 # unless that argument is empty.
326 m4_define([m4_ifvaln],
327 [m4_if([$1],
328        [],   [m4_n([$3])],
329              [m4_n([$2])])])
332 # m4_ifset(MACRO, [IF-TRUE], [IF-FALSE])
333 # --------------------------------------
334 # If MACRO has no definition, or of its definition is the empty string,
335 # expand IF-FALSE, otherwise IF-TRUE.
336 m4_define([m4_ifset],
337 [m4_ifdef([$1],
338           [m4_ifval(_m4_defn([$1]), [$2], [$3])],
339           [$3])])
342 # m4_ifndef(NAME, [IF-NOT-DEFINED], [IF-DEFINED])
343 # -----------------------------------------------
344 m4_define([m4_ifndef],
345 [m4_ifdef([$1], [$3], [$2])])
348 # m4_case(SWITCH, VAL1, IF-VAL1, VAL2, IF-VAL2, ..., DEFAULT)
349 # -----------------------------------------------------------
350 # m4 equivalent of
351 # switch (SWITCH)
352 # {
353 #   case VAL1:
354 #     IF-VAL1;
355 #     break;
356 #   case VAL2:
357 #     IF-VAL2;
358 #     break;
359 #   ...
360 #   default:
361 #     DEFAULT;
362 #     break;
363 # }.
364 # All the values are optional, and the macro is robust to active
365 # symbols properly quoted.
366 m4_define([m4_case],
367 [m4_if([$#], 0, [],
368        [$#], 1, [],
369        [$#], 2, [$2],
370        [$1], [$2], [$3],
371        [$0([$1], m4_shift3($@))])])
374 # m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT)
375 # -----------------------------------------------------
376 # m4 equivalent of
378 # if (SWITCH =~ RE1)
379 #   VAL1;
380 # elif (SWITCH =~ RE2)
381 #   VAL2;
382 # elif ...
383 #   ...
384 # else
385 #   DEFAULT
387 # All the values are optional, and the macro is robust to active symbols
388 # properly quoted.
389 m4_define([m4_bmatch],
390 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
391        [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
392        [$#], 2, [$2],
393        [m4_if(m4_bregexp([$1], [$2]), -1, [$0([$1], m4_shift3($@))],
394               [$3])])])
397 # m4_car(LIST)
398 # m4_cdr(LIST)
399 # ------------
400 # Manipulate m4 lists.
401 m4_define([m4_car], [[$1]])
402 m4_define([m4_cdr],
403 [m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])],
404        [$#], 1, [],
405        [m4_dquote(m4_shift($@))])])
407 # _m4_cdr(LIST)
408 # -------------
409 # Like m4_cdr, except include a leading comma unless only one element
410 # remains.  Why?  Because comparing a large list against [] is more
411 # expensive in expansion time than comparing the number of arguments; so
412 # _m4_cdr can be used to reduce the number of arguments when it is time
413 # to end recursion.
414 m4_define([_m4_cdr],
415 [m4_if([$#], 1, [],
416        [, m4_dquote(m4_shift($@))])])
420 # m4_cond(TEST1, VAL1, IF-VAL1, TEST2, VAL2, IF-VAL2, ..., [DEFAULT])
421 # -------------------------------------------------------------------
422 # Similar to m4_if, except that each TEST is expanded when encountered.
423 # If the expansion of TESTn matches the string VALn, the result is IF-VALn.
424 # The result is DEFAULT if no tests passed.  This macro allows
425 # short-circuiting of expensive tests, where it pays to arrange quick
426 # filter tests to run first.
428 # For an example, consider a previous implementation of _AS_QUOTE_IFELSE:
430 #    m4_if(m4_index([$1], [\]), [-1], [$2],
431 #          m4_eval(m4_index([$1], [\\]) >= 0), [1], [$2],
432 #          m4_eval(m4_index([$1], [\$]) >= 0), [1], [$2],
433 #          m4_eval(m4_index([$1], [\`]) >= 0), [1], [$3],
434 #          m4_eval(m4_index([$1], [\"]) >= 0), [1], [$3],
435 #          [$2])
437 # Here, m4_index is computed 5 times, and m4_eval 4, even if $1 contains
438 # no backslash.  It is more efficient to do:
440 #    m4_cond([m4_index([$1], [\])], [-1], [$2],
441 #            [m4_eval(m4_index([$1], [\\]) >= 0)], [1], [$2],
442 #            [m4_eval(m4_index([$1], [\$]) >= 0)], [1], [$2],
443 #            [m4_eval(m4_index([$1], [\`]) >= 0)], [1], [$3],
444 #            [m4_eval(m4_index([$1], [\"]) >= 0)], [1], [$3],
445 #            [$2])
447 # In the common case of $1 with no backslash, only one m4_index expansion
448 # occurs, and m4_eval is avoided altogether.
449 m4_define([m4_cond],
450 [m4_if([$#], [0], [m4_fatal([$0: cannot be called without arguments])],
451        [$#], [1], [$1],
452        [$#], [2], [m4_fatal([$0: missing an argument])],
453        [m4_if($1, [$2], [$3], [$0(m4_shift3($@))])])])
456 ## ---------------------------------------- ##
457 ## 6. Enhanced version of some primitives.  ##
458 ## ---------------------------------------- ##
460 # m4_bpatsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...)
461 # ----------------------------------------------------
462 # m4 equivalent of
464 #   $_ = STRING;
465 #   s/RE1/SUBST1/g;
466 #   s/RE2/SUBST2/g;
467 #   ...
469 # All the values are optional, and the macro is robust to active symbols
470 # properly quoted.
472 # I would have liked to name this macro `m4_bpatsubst', unfortunately,
473 # due to quotation problems, I need to double quote $1 below, therefore
474 # the anchors are broken :(  I can't let users be trapped by that.
476 # Recall that m4_shift3 always results in an argument.  Hence, we need
477 # to distinguish between a final deletion vs. ending recursion.
478 m4_define([m4_bpatsubsts],
479 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
480        [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
481        [$#], 2, [m4_builtin([patsubst], [$1], [$2])],
482        [_$0($@m4_if(m4_eval($# & 1), 0, [,]))])])
483 m4_define([_m4_bpatsubsts],
484 [m4_if([$#], 2, [$1],
485        [$0(m4_builtin([patsubst], [[$1]], [$2], [$3]),
486            m4_shift3($@))])])
489 # m4_define_default(MACRO, VALUE)
490 # -------------------------------
491 # If MACRO is undefined, set it to VALUE.
492 m4_define([m4_define_default],
493 [m4_ifndef([$1], [m4_define($@)])])
496 # m4_default(EXP1, EXP2)
497 # ----------------------
498 # Returns EXP1 if non empty, otherwise EXP2.
500 # This macro is called on hot paths, so inline the contents of m4_ifval,
501 # for one less round of expansion.
502 m4_define([m4_default],
503 [m4_if([$1], [], [$2], [$1])])
506 # m4_defn(NAME)
507 # -------------
508 # Like the original, except guarantee a warning when using something which is
509 # undefined (unlike M4 1.4.x).  This replacement is not a full-featured
510 # replacement: if any of the defined macros contain unbalanced quoting, but
511 # when pasted together result in a well-quoted string, then only native m4
512 # support is able to get it correct.  But that's where quadrigraphs come in
513 # handy, if you really need unbalanced quotes inside your macros.
515 # This macro is called frequently, so minimize the amount of additional
516 # expansions by skipping m4_ifndef.  Better yet, if __m4_version__ exists,
517 # (added in M4 1.6), then let m4 do the job for us.
519 # _m4_defn is for internal use only - it bypasses the wrapper, so it
520 # must only be used on one argument at a time, and only on macros
521 # known to be defined.  Make sure this still works if the user renames
522 # m4_defn but not _m4_defn.
523 m4_copy([m4_defn], [_m4_defn])
524 m4_ifdef([__m4_version__], [],
525 [m4_define([m4_defn],
526 [m4_if([$#], [0], [[$0]],
527        [$#], [1], [m4_ifdef([$1], [_m4_defn([$1])],
528                             [m4_fatal([$0: undefined macro: $1])])],
529        [m4_foreach([_m4_macro], [$@], [$0(_m4_defn([_m4_macro]))])])])])
532 # _m4_dumpdefs_up(NAME)
533 # ---------------------
534 m4_define([_m4_dumpdefs_up],
535 [m4_ifdef([$1],
536           [m4_pushdef([_m4_dumpdefs], _m4_defn([$1]))dnl
537 m4_dumpdef([$1])dnl
538 _m4_popdef([$1])dnl
539 _m4_dumpdefs_up([$1])])])
542 # _m4_dumpdefs_down(NAME)
543 # -----------------------
544 m4_define([_m4_dumpdefs_down],
545 [m4_ifdef([_m4_dumpdefs],
546           [m4_pushdef([$1], _m4_defn([_m4_dumpdefs]))dnl
547 _m4_popdef([_m4_dumpdefs])dnl
548 _m4_dumpdefs_down([$1])])])
551 # m4_dumpdefs(NAME)
552 # -----------------
553 # Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its
554 # value stack (most recent displayed first).
555 m4_define([m4_dumpdefs],
556 [_m4_dumpdefs_up([$1])dnl
557 _m4_dumpdefs_down([$1])])
560 # m4_popdef(NAME)
561 # ---------------
562 # Like the original, except guarantee a warning when using something which is
563 # undefined (unlike M4 1.4.x).
565 # This macro is called frequently, so minimize the amount of additional
566 # expansions by skipping m4_ifndef.  Better yet, if __m4_version__ exists,
567 # (added in M4 1.6), then let m4 do the job for us.
569 # _m4_popdef is for internal use only - it bypasses the wrapper, so it
570 # must only be used on macros known to be defined.  Make sure this
571 # still works if the user renames m4_popdef but not _m4_popdef.
572 m4_copy([m4_popdef], [_m4_popdef])
573 m4_ifdef([__m4_version__], [],
574 [m4_define([m4_popdef],
575 [m4_if([$#], [0], [[$0]],
576        [$#], [1], [m4_ifdef([$1], [_m4_popdef([$1])],
577                             [m4_fatal([$0: undefined macro: $1])])],
578        [m4_foreach([_m4_macro], [$@], [$0(_m4_defn([_m4_macro]))])])])])
581 # m4_shiftn(N, ...)
582 # -----------------
583 # Returns ... shifted N times.  Useful for recursive "varargs" constructs.
585 # Autoconf does not use this macro, because it is inherently slower than
586 # calling the common cases of m4_shift2 or m4_shift3 directly.  But it
587 # might as well be fast for other clients, such as Libtool.  One way to
588 # do this is to expand $@ only once in _m4_shiftn (otherwise, for long
589 # lists, the expansion of m4_if takes twice as much memory as what the
590 # list itself occupies, only to throw away the unused branch).  The end
591 # result is strictly equivalent to
592 #   m4_if([$1], 1, [m4_shift(,m4_shift(m4_shift($@)))],
593 #         [_m4_shiftn(m4_decr([$1]), m4_shift(m4_shift($@)))])
594 # but with the final `m4_shift(m4_shift($@)))' shared between the two
595 # paths.  The first leg uses a no-op m4_shift(,$@) to balance out the ().
596 m4_define([m4_shiftn],
597 [m4_assert(0 < $1 && $1 < $#)_$0($@)])
599 m4_define([_m4_shiftn],
600 [m4_if([$1], 1, [m4_shift(],
601        [$0(m4_decr([$1])]), m4_shift(m4_shift($@)))])
603 # m4_shift2(...)
604 # m4_shift3(...)
605 # -----------------
606 # Returns ... shifted twice, and three times.  Faster than m4_shiftn.
607 m4_define([m4_shift2], [m4_shift(m4_shift($@))])
608 m4_define([m4_shift3], [m4_shift(m4_shift(m4_shift($@)))])
610 # _m4_shift2(...)
611 # _m4_shift3(...)
612 # ---------------
613 # Like m4_shift2 or m4_shift3, except include a leading comma unless shifting
614 # consumes all arguments.  Why?  Because in recursion, it is nice to
615 # distinguish between 1 element left and 0 elements left, based on how many
616 # arguments this shift expands to.
617 m4_define([_m4_shift2],
618 [m4_if([$#], [2], [],
619        [, m4_shift(m4_shift($@))])])
620 m4_define([_m4_shift3],
621 [m4_if([$#], [3], [],
622        [, m4_shift(m4_shift(m4_shift($@)))])])
625 # m4_undefine(NAME)
626 # -----------------
627 # Like the original, except guarantee a warning when using something which is
628 # undefined (unlike M4 1.4.x).
630 # This macro is called frequently, so minimize the amount of additional
631 # expansions by skipping m4_ifndef.  Better yet, if __m4_version__ exists,
632 # (added in M4 1.6), then let m4 do the job for us.
634 # _m4_undefine is for internal use only - it bypasses the wrapper, so
635 # it must only be used on macros known to be defined.  Make sure this
636 # still works if the user renames m4_undefine but not _m4_undefine.
637 m4_copy([m4_undefine], [_m4_undefine])
638 m4_ifdef([__m4_version__], [],
639 [m4_define([m4_undefine],
640 [m4_if([$#], [0], [[$0]],
641        [$#], [1], [m4_ifdef([$1], [_m4_undefine([$1])],
642                             [m4_fatal([$0: undefined macro: $1])])],
643        [m4_foreach([_m4_macro], [$@], [$0(_m4_defn([_m4_macro]))])])])])
645 # _m4_wrap(PRE, POST)
646 # -------------------
647 # Helper macro for m4_wrap and m4_wrap_lifo.  Allows nested calls to
648 # m4_wrap within wrapped text.  Use _m4_defn and _m4_popdef for speed.
649 m4_define([_m4_wrap],
650 [m4_ifdef([$0_text],
651           [m4_define([$0_text], [$1]_m4_defn([$0_text])[$2])],
652           [m4_builtin([m4wrap], [m4_unquote(
653   _m4_defn([$0_text])_m4_popdef([$0_text]))])m4_define([$0_text], [$1$2])])])
655 # m4_wrap(TEXT)
656 # -------------
657 # Append TEXT to the list of hooks to be executed at the end of input.
658 # Whereas the order of the original may be LIFO in the underlying m4,
659 # this version is always FIFO.
660 m4_define([m4_wrap],
661 [_m4_wrap([], [$1[]])])
663 # m4_wrap_lifo(TEXT)
664 # ------------------
665 # Prepend TEXT to the list of hooks to be executed at the end of input.
666 # Whereas the order of m4_wrap may be FIFO in the underlying m4, this
667 # version is always LIFO.
668 m4_define([m4_wrap_lifo],
669 [_m4_wrap([$1[]])])
671 ## ------------------------- ##
672 ## 7. Quoting manipulation.  ##
673 ## ------------------------- ##
676 # m4_apply(MACRO, LIST)
677 # ---------------------
678 # Invoke MACRO, with arguments provided from the quoted list of
679 # comma-separated quoted arguments.  If LIST is empty, invoke MACRO
680 # without arguments.
681 m4_define([m4_apply],
682 [m4_if([$2], [], [$1], [$1($2)])[]])
685 # m4_count(ARGS)
686 # --------------
687 # Return a count of how many ARGS are present.
688 m4_define([m4_count], [$#])
691 # m4_do(STRING, ...)
692 # ------------------
693 # This macro invokes all its arguments (in sequence, of course).  It is
694 # useful for making your macros more structured and readable by dropping
695 # unnecessary dnl's and have the macros indented properly.
696 m4_define([m4_do],
697 [m4_if([$#], 0, [],
698        [$#], 1, [$1],
699        [$1[]m4_do(m4_shift($@))])])
702 # m4_dquote(ARGS)
703 # ---------------
704 # Return ARGS as a quoted list of quoted arguments.
705 m4_define([m4_dquote],  [[$@]])
708 # m4_dquote_elt(ARGS)
709 # -------------------
710 # Return ARGS as an unquoted list of double-quoted arguments.
711 m4_define([m4_dquote_elt],
712 [m4_if([$#], [0], [],
713        [$#], [1], [[[$1]]],
714        [[[$1]],$0(m4_shift($@))])])
717 # m4_echo(ARGS)
718 # -------------
719 # Return the ARGS, with the same level of quoting.  Whitespace after
720 # unquoted commas are consumed.
721 m4_define([m4_echo], [$@])
724 # m4_expand(ARG)
725 # --------------
726 # Return the expansion of ARG as a single string.  Unlike m4_quote($1), this
727 # correctly preserves whitespace following single-quoted commas that appeared
728 # within ARG.
730 #   m4_define([active], [ACT, IVE])
731 #   m4_define([active2], [[ACT, IVE]])
732 #   m4_quote(active, active2)
733 #   => ACT,IVE,ACT, IVE
734 #   m4_expand([active, active2])
735 #   => ACT, IVE, ACT, IVE
737 # Unfortunately, due to limitations in m4, ARG must expand to something
738 # with balanced quotes (use quadrigraphs to get around this).  The input
739 # is not likely to have unbalanced -=<{(/)}>=- quotes, and it is possible
740 # to have unbalanced (), provided it was specified with proper [] quotes.
742 # Exploit that extra () will group unquoted commas and the following
743 # whitespace, then convert () to [].  m4_bpatsubst can't handle newlines
744 # inside $1, and m4_substr strips quoting.  So we (ab)use m4_changequote.
745 m4_define([m4_expand], [_$0(-=<{($1)}>=-)])
746 m4_define([_m4_expand],
747 [m4_changequote([-=<{(], [)}>=-])$1m4_changequote([, ])])
750 # m4_ignore(ARGS)
751 # ---------------
752 # Expands to nothing.  Useful for conditionally ignoring an arbitrary
753 # number of arguments (see _m4_list_cmp for an example).
754 m4_define([m4_ignore])
757 # m4_make_list(ARGS)
758 # ------------------
759 # Similar to m4_dquote, this creates a quoted list of quoted ARGS.  This
760 # version is less efficient than m4_dquote, but separates each argument
761 # with a comma and newline, rather than just comma, for readability.
762 # When developing an m4sugar algorithm, you could temporarily use
763 #   m4_pushdef([m4_dquote],m4_defn([m4_make_list]))
764 # around your code to make debugging easier.
765 m4_define([m4_make_list], [m4_join([,
766 ], m4_dquote_elt($@))])
769 # m4_noquote(STRING)
770 # ------------------
771 # Return the result of ignoring all quotes in STRING and invoking the
772 # macros it contains.  Amongst other things, this is useful for enabling
773 # macro invocations inside strings with [] blocks (for instance regexps
774 # and help-strings).  On the other hand, since all quotes are disabled,
775 # any macro expanded during this time that relies on nested [] quoting
776 # will likely crash and burn.  This macro is seldom useful; consider
777 # m4_unquote or m4_expand instead.
778 m4_define([m4_noquote],
779 [m4_changequote([-=<{(],[)}>=-])$1-=<{()}>=-m4_changequote([,])])
782 # m4_quote(ARGS)
783 # --------------
784 # Return ARGS as a single argument.  Any whitespace after unquoted commas
785 # is stripped.  There is always output, even when there were no arguments.
787 # It is important to realize the difference between `m4_quote(exp)' and
788 # `[exp]': in the first case you obtain the quoted *result* of the
789 # expansion of EXP, while in the latter you just obtain the string
790 # `exp'.
791 m4_define([m4_quote],  [[$*]])
794 # _m4_quote(ARGS)
795 # ---------------
796 # Like m4_quote, except that when there are no arguments, there is no
797 # output.  For conditional scenarios (such as passing _m4_quote as the
798 # macro name in m4_map), this feature can be used to distinguish between
799 # one argument of the empty string vs. no arguments.  However, in the
800 # normal case with arguments present, this is less efficient than m4_quote.
801 m4_define([_m4_quote],
802 [m4_if([$#], [0], [], [[$*]])])
805 # m4_unquote(ARGS)
806 # ----------------
807 # Remove one layer of quotes from each ARG, performing one level of
808 # expansion.  For one argument, m4_unquote([arg]) is more efficient than
809 # m4_do([arg]), but for multiple arguments, the difference is that
810 # m4_unquote separates arguments with commas while m4_do concatenates.
811 m4_define([m4_unquote], [$*])
814 ## -------------------------- ##
815 ## 8. Implementing m4 loops.  ##
816 ## -------------------------- ##
819 # m4_for(VARIABLE, FIRST, LAST, [STEP = +/-1], EXPRESSION)
820 # --------------------------------------------------------
821 # Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO with
822 # increments of STEP.
823 # Both limits are included, and bounds are checked for consistency.
824 # The algorithm is robust to indirect VARIABLE names, and uses _m4_defn
825 # where possible for speed.
826 m4_define([m4_for],
827 [m4_pushdef([$1], m4_eval([$2]))dnl
828 m4_cond([m4_eval(([$3]) > _m4_defn([$1]))], 1,
829 [m4_pushdef([_m4_step], m4_eval(m4_default([$4], 1)))dnl
830 m4_assert(_m4_step > 0)dnl
831 _m4_for([$1], m4_eval((([$3]) - _m4_defn([$1]))
832                       / _m4_step * _m4_step + _m4_defn([$1])),
833         _m4_step, [$5])],
834         [m4_eval(([$3]) < _m4_defn([$1]))], 1,
835 [m4_pushdef([_m4_step], m4_eval(m4_default([$4], -1)))dnl
836 m4_assert(_m4_step < 0)dnl
837 _m4_for([$1], m4_eval((_m4_defn([$1]) - ([$3]))
838                       / -(_m4_step) * _m4_step + _m4_defn([$1])),
839         _m4_step, [$5])],
840         [m4_pushdef([_m4_step])dnl
841 $5])[]dnl
842 m4_popdef([_m4_step])dnl
843 m4_popdef([$1])])
846 # _m4_for(VARIABLE, LAST, STEP, EXPRESSION)
847 # -----------------------------------------
848 # Core of the loop, no consistency checks, all arguments are plain numbers.
849 m4_define([_m4_for],
850 [$4[]dnl
851 m4_if(m4_defn([$1]), [$2], [],
852       [m4_define([$1], m4_eval(m4_defn([$1])+[$3]))$0($@)])])
855 # Implementing `foreach' loops in m4 is much more tricky than it may
856 # seem.  For example, the old M4 1.4.4 manual had an incorrect example,
857 # which looked like this (when translated to m4sugar):
859 # | # foreach(VAR, (LIST), STMT)
860 # | m4_define([foreach],
861 # |   [m4_pushdef([$1])_foreach([$1], [$2], [$3])m4_popdef([$1])])
862 # | m4_define([_arg1], [$1])
863 # | m4_define([_foreach],
864 # |   [m4_if([$2], [()], ,
865 # |     [m4_define([$1], _arg1$2)$3[]_foreach([$1], (m4_shift$2), [$3])])])
867 # But then if you run
869 # | m4_define(a, 1)
870 # | m4_define(b, 2)
871 # | m4_define(c, 3)
872 # | foreach([f], [([a], [(b], [c)])], [echo f
873 # | ])
875 # it gives
877 #  => echo 1
878 #  => echo (2,3)
880 # which is not what is expected.
882 # Of course the problem is that many quotes are missing.  So you add
883 # plenty of quotes at random places, until you reach the expected
884 # result.  Alternatively, if you are a quoting wizard, you directly
885 # reach the following implementation (but if you really did, then
886 # apply to the maintenance of m4sugar!).
888 # | # foreach(VAR, (LIST), STMT)
889 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
890 # | m4_define([_arg1], [[$1]])
891 # | m4_define([_foreach],
892 # |  [m4_if($2, [()], ,
893 # |    [m4_define([$1], [_arg1$2])$3[]_foreach([$1], [(m4_shift$2)], [$3])])])
895 # which this time answers
897 #  => echo a
898 #  => echo (b
899 #  => echo c)
901 # Bingo!
903 # Well, not quite.
905 # With a better look, you realize that the parens are more a pain than
906 # a help: since anyway you need to quote properly the list, you end up
907 # with always using an outermost pair of parens and an outermost pair
908 # of quotes.  Rejecting the parens both eases the implementation, and
909 # simplifies the use:
911 # | # foreach(VAR, (LIST), STMT)
912 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
913 # | m4_define([_arg1], [$1])
914 # | m4_define([_foreach],
915 # |  [m4_if($2, [], ,
916 # |    [m4_define([$1], [_arg1($2)])$3[]_foreach([$1], [m4_shift($2)], [$3])])])
919 # Now, just replace the `$2' with `m4_quote($2)' in the outer `m4_if'
920 # to improve robustness, and you come up with a nice implementation
921 # that doesn't require extra parentheses in the user's LIST.
923 # But wait -  now the algorithm is quadratic, because every recursion of
924 # the algorithm keeps the entire LIST and merely adds another m4_shift to
925 # the quoted text.  If the user has a lot of elements in LIST, you can
926 # bring the system to its knees with the memory m4 then requires, or trip
927 # the m4 --nesting-limit recursion factor.  The only way to avoid
928 # quadratic growth is ensure m4_shift is expanded prior to the recursion.
929 # Hence the design below.
931 # The M4 manual now includes a chapter devoted to this issue, with
932 # the lessons learned from m4sugar.  And still, this design is only
933 # optimal for M4 1.6; see foreach.m4 for yet more comments on why
934 # M4 1.4.x uses yet another implementation.
937 # m4_foreach(VARIABLE, LIST, EXPRESSION)
938 # --------------------------------------
940 # Expand EXPRESSION assigning each value of the LIST to VARIABLE.
941 # LIST should have the form `item_1, item_2, ..., item_n', i.e. the
942 # whole list must *quoted*.  Quote members too if you don't want them
943 # to be expanded.
945 # This macro is robust to active symbols:
946 #      | m4_define(active, [ACT, IVE])
947 #      | m4_foreach(Var, [active, active], [-Var-])
948 #     => -ACT--IVE--ACT--IVE-
950 #      | m4_foreach(Var, [[active], [active]], [-Var-])
951 #     => -ACT, IVE--ACT, IVE-
953 #      | m4_foreach(Var, [[[active]], [[active]]], [-Var-])
954 #     => -active--active-
956 # This macro is called frequently, so avoid extra expansions such as
957 # m4_ifval and dnl.  Also, since $2 might be quite large, try to use it
958 # as little as possible in _m4_foreach; each extra use requires that much
959 # more memory for expansion.  So, rather than directly compare $2 against
960 # [] and use m4_car/m4_cdr for recursion, we instead unbox the list (which
961 # requires swapping the argument order in the helper) and use _m4_shift3
962 # to detect when recursion is complete.
963 m4_define([m4_foreach],
964 [m4_pushdef([$1])_$0([$1], [$3]m4_if([$2], [], [], [, $2]))m4_popdef([$1])])
966 m4_define([_m4_foreach],
967 [m4_if([$#], [2], [],
968        [m4_define([$1], [$3])$2[]$0([$1], [$2]_m4_shift3($@))])])
971 # m4_foreach_w(VARIABLE, LIST, EXPRESSION)
972 # ----------------------------------------
974 # Like m4_foreach, but the list is whitespace separated.
976 # This macro is robust to active symbols:
977 #    m4_foreach_w([Var], [ active
978 #    b  act\
979 #    ive  ], [-Var-])end
980 #    => -active--b--active-end
982 m4_define([m4_foreach_w],
983 [m4_foreach([$1], m4_split(m4_normalize([$2]), [ ]), [$3])])
986 # m4_map(MACRO, LIST)
987 # -------------------
988 # Invoke MACRO($1), MACRO($2) etc. where $1, $2... are the elements
989 # of LIST.  $1, $2... must in turn be lists, appropriate for m4_apply.
991 # Since LIST may be quite large, we want to minimize how often it appears
992 # in the expansion.  Rather than use m4_car/m4_cdr iteration, we unbox the
993 # list, and use _m4_shift2 to detect the end of recursion.
994 m4_define([m4_map],
995 [m4_if([$2], [], [],
996        [_$0([$1], $2)])])
997 m4_define([_m4_map],
998 [m4_if([$#], [1], [],
999        [m4_apply([$1], [$2])$0([$1]_m4_shift2($@))])])
1002 # m4_map_sep(MACRO, SEPARATOR, LIST)
1003 # ----------------------------------
1004 # Invoke MACRO($1), SEPARATOR, MACRO($2), ..., MACRO($N) where $1, $2... $N
1005 # are the elements of LIST, and are in turn lists appropriate for m4_apply.
1006 # SEPARATOR is not further expanded.
1007 m4_define([m4_map_sep],
1008 [m4_if([$3], [], [],
1009        [m4_apply([$1], m4_car($3))_m4_map([[$2]$1]_m4_shift2(,$3))])])
1012 ## --------------------------- ##
1013 ## 9. More diversion support.  ##
1014 ## --------------------------- ##
1017 # _m4_divert(DIVERSION-NAME or NUMBER)
1018 # ------------------------------------
1019 # If DIVERSION-NAME is the name of a diversion, return its number,
1020 # otherwise if it is a NUMBER return it.
1021 m4_define([_m4_divert],
1022 [m4_ifdef([_m4_divert($1)],
1023           [m4_indir([_m4_divert($1)])],
1024           [$1])])
1026 # KILL is only used to suppress output.
1027 m4_define([_m4_divert(KILL)],           -1)
1029 # The empty diversion name is a synonym for 0.
1030 m4_define([_m4_divert()],                0)
1033 # _m4_divert_n_stack
1034 # ------------------
1035 # Print m4_divert_stack with newline prepended, if it's nonempty.
1036 m4_define([_m4_divert_n_stack],
1037 [m4_ifdef([m4_divert_stack], [
1038 _m4_defn([m4_divert_stack])])])
1041 # m4_divert(DIVERSION-NAME)
1042 # -------------------------
1043 # Change the diversion stream to DIVERSION-NAME.
1044 m4_define([m4_divert],
1045 [m4_define([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
1046 m4_builtin([divert], _m4_divert([$1]))dnl
1050 # m4_divert_push(DIVERSION-NAME)
1051 # ------------------------------
1052 # Change the diversion stream to DIVERSION-NAME, while stacking old values.
1053 m4_define([m4_divert_push],
1054 [m4_pushdef([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
1055 m4_pushdef([_m4_divert_diversion], [$1])dnl
1056 m4_builtin([divert], _m4_divert([$1]))dnl
1060 # m4_divert_pop([DIVERSION-NAME])
1061 # -------------------------------
1062 # Change the diversion stream to its previous value, unstacking it.
1063 # If specified, verify we left DIVERSION-NAME.
1064 # When we pop the last value from the stack, we divert to -1.
1065 m4_define([m4_divert_pop],
1066 [m4_ifndef([_m4_divert_diversion],
1067            [m4_fatal([too many m4_divert_pop])])dnl
1068 m4_if([$1], [], [],
1069       [$1], m4_defn([_m4_divert_diversion]), [],
1070       [m4_fatal([$0($1): diversion mismatch: ]_m4_divert_n_stack)])dnl
1071 m4_popdef([m4_divert_stack])dnl
1072 m4_popdef([_m4_divert_diversion])dnl
1073 m4_builtin([divert],
1074            m4_ifdef([_m4_divert_diversion],
1075                     [_m4_divert(_m4_defn([_m4_divert_diversion]))],
1076                     -1))dnl
1080 # m4_divert_text(DIVERSION-NAME, CONTENT)
1081 # ---------------------------------------
1082 # Output CONTENT into DIVERSION-NAME (which may be a number actually).
1083 # An end of line is appended for free to CONTENT.
1084 m4_define([m4_divert_text],
1085 [m4_divert_push([$1])dnl
1087 m4_divert_pop([$1])dnl
1091 # m4_divert_once(DIVERSION-NAME, CONTENT)
1092 # ---------------------------------------
1093 # Output CONTENT into DIVERSION-NAME once, if not already there.
1094 # An end of line is appended for free to CONTENT.
1095 m4_define([m4_divert_once],
1096 [m4_expand_once([m4_divert_text([$1], [$2])])])
1099 # m4_undivert(DIVERSION-NAME)
1100 # ---------------------------
1101 # Undivert DIVERSION-NAME.  Unlike the M4 version, this only takes a single
1102 # diversion identifier, and should not be used to undivert files.
1103 m4_define([m4_undivert],
1104 [m4_builtin([undivert], _m4_divert([$1]))])
1107 ## --------------------------------------------- ##
1108 ## 10. Defining macros with bells and whistles.  ##
1109 ## --------------------------------------------- ##
1111 # `m4_defun' is basically `m4_define' but it equips the macro with the
1112 # needed machinery for `m4_require'.  A macro must be m4_defun'd if
1113 # either it is m4_require'd, or it m4_require's.
1115 # Two things deserve attention and are detailed below:
1116 #  1. Implementation of m4_require
1117 #  2. Keeping track of the expansion stack
1119 # 1. Implementation of m4_require
1120 # ===============================
1122 # Of course m4_defun AC_PROVIDE's the macro, so that a macro which has
1123 # been expanded is not expanded again when m4_require'd, but the
1124 # difficult part is the proper expansion of macros when they are
1125 # m4_require'd.
1127 # The implementation is based on two ideas, (i) using diversions to
1128 # prepare the expansion of the macro and its dependencies (by Franc,ois
1129 # Pinard), and (ii) expand the most recently m4_require'd macros _after_
1130 # the previous macros (by Axel Thimm).
1133 # The first idea: why use diversions?
1134 # -----------------------------------
1136 # When a macro requires another, the other macro is expanded in new
1137 # diversion, GROW.  When the outer macro is fully expanded, we first
1138 # undivert the most nested diversions (GROW - 1...), and finally
1139 # undivert GROW.  To understand why we need several diversions,
1140 # consider the following example:
1142 # | m4_defun([TEST1], [Test...REQUIRE([TEST2])1])
1143 # | m4_defun([TEST2], [Test...REQUIRE([TEST3])2])
1144 # | m4_defun([TEST3], [Test...3])
1146 # Because m4_require is not required to be first in the outer macros, we
1147 # must keep the expansions of the various levels of m4_require separated.
1148 # Right before executing the epilogue of TEST1, we have:
1150 #          GROW - 2: Test...3
1151 #          GROW - 1: Test...2
1152 #          GROW:     Test...1
1153 #          BODY:
1155 # Finally the epilogue of TEST1 undiverts GROW - 2, GROW - 1, and
1156 # GROW into the regular flow, BODY.
1158 #          GROW - 2:
1159 #          GROW - 1:
1160 #          GROW:
1161 #          BODY:        Test...3; Test...2; Test...1
1163 # (The semicolons are here for clarification, but of course are not
1164 # emitted.)  This is what Autoconf 2.0 (I think) to 2.13 (I'm sure)
1165 # implement.
1168 # The second idea: first required first out
1169 # -----------------------------------------
1171 # The natural implementation of the idea above is buggy and produces
1172 # very surprising results in some situations.  Let's consider the
1173 # following example to explain the bug:
1175 # | m4_defun([TEST1],  [REQUIRE([TEST2a])REQUIRE([TEST2b])])
1176 # | m4_defun([TEST2a], [])
1177 # | m4_defun([TEST2b], [REQUIRE([TEST3])])
1178 # | m4_defun([TEST3],  [REQUIRE([TEST2a])])
1179 # |
1180 # | AC_INIT
1181 # | TEST1
1183 # The dependencies between the macros are:
1185 #                3 --- 2b
1186 #               /        \              is m4_require'd by
1187 #              /          \       left -------------------- right
1188 #           2a ------------ 1
1190 # If you strictly apply the rules given in the previous section you get:
1192 #          GROW - 2: TEST3
1193 #          GROW - 1: TEST2a; TEST2b
1194 #          GROW:     TEST1
1195 #          BODY:
1197 # (TEST2a, although required by TEST3 is not expanded in GROW - 3
1198 # because is has already been expanded before in GROW - 1, so it has
1199 # been AC_PROVIDE'd, so it is not expanded again) so when you undivert
1200 # the stack of diversions, you get:
1202 #          GROW - 2:
1203 #          GROW - 1:
1204 #          GROW:
1205 #          BODY:        TEST3; TEST2a; TEST2b; TEST1
1207 # i.e., TEST2a is expanded after TEST3 although the latter required the
1208 # former.
1210 # Starting from 2.50, we use an implementation provided by Axel Thimm.
1211 # The idea is simple: the order in which macros are emitted must be the
1212 # same as the one in which macros are expanded.  (The bug above can
1213 # indeed be described as: a macro has been AC_PROVIDE'd before its
1214 # dependent, but it is emitted after: the lack of correlation between
1215 # emission and expansion order is guilty).
1217 # How to do that?  You keep the stack of diversions to elaborate the
1218 # macros, but each time a macro is fully expanded, emit it immediately.
1220 # In the example above, when TEST2a is expanded, but it's epilogue is
1221 # not run yet, you have:
1223 #          GROW - 2:
1224 #          GROW - 1: TEST2a
1225 #          GROW:     Elaboration of TEST1
1226 #          BODY:
1228 # The epilogue of TEST2a emits it immediately:
1230 #          GROW - 2:
1231 #          GROW - 1:
1232 #          GROW:     Elaboration of TEST1
1233 #          BODY:     TEST2a
1235 # TEST2b then requires TEST3, so right before the epilogue of TEST3, you
1236 # have:
1238 #          GROW - 2: TEST3
1239 #          GROW - 1: Elaboration of TEST2b
1240 #          GROW:     Elaboration of TEST1
1241 #          BODY:      TEST2a
1243 # The epilogue of TEST3 emits it:
1245 #          GROW - 2:
1246 #          GROW - 1: Elaboration of TEST2b
1247 #          GROW:     Elaboration of TEST1
1248 #          BODY:     TEST2a; TEST3
1250 # TEST2b is now completely expanded, and emitted:
1252 #          GROW - 2:
1253 #          GROW - 1:
1254 #          GROW:     Elaboration of TEST1
1255 #          BODY:     TEST2a; TEST3; TEST2b
1257 # and finally, TEST1 is finished and emitted:
1259 #          GROW - 2:
1260 #          GROW - 1:
1261 #          GROW:
1262 #          BODY:     TEST2a; TEST3; TEST2b: TEST1
1264 # The idea is simple, but the implementation is a bit evolved.  If you
1265 # are like me, you will want to see the actual functioning of this
1266 # implementation to be convinced.  The next section gives the full
1267 # details.
1270 # The Axel Thimm implementation at work
1271 # -------------------------------------
1273 # We consider the macros above, and this configure.ac:
1275 #           AC_INIT
1276 #           TEST1
1278 # You should keep the definitions of _m4_defun_pro, _m4_defun_epi, and
1279 # m4_require at hand to follow the steps.
1281 # This implements tries not to assume that the current diversion is
1282 # BODY, so as soon as a macro (m4_defun'd) is expanded, we first
1283 # record the current diversion under the name _m4_divert_dump (denoted
1284 # DUMP below for short).  This introduces an important difference with
1285 # the previous versions of Autoconf: you cannot use m4_require if you
1286 # are not inside an m4_defun'd macro, and especially, you cannot
1287 # m4_require directly from the top level.
1289 # We have not tried to simulate the old behavior (better yet, we
1290 # diagnose it), because it is too dangerous: a macro m4_require'd from
1291 # the top level is expanded before the body of `configure', i.e., before
1292 # any other test was run.  I let you imagine the result of requiring
1293 # AC_STDC_HEADERS for instance, before AC_PROG_CC was actually run....
1295 # After AC_INIT was run, the current diversion is BODY.
1296 # * AC_INIT was run
1297 #   DUMP:                undefined
1298 #   diversion stack:     BODY |-
1300 # * TEST1 is expanded
1301 # The prologue of TEST1 sets _m4_divert_dump, which is the diversion
1302 # where the current elaboration will be dumped, to the current
1303 # diversion.  It also m4_divert_push to GROW, where the full
1304 # expansion of TEST1 and its dependencies will be elaborated.
1305 #   DUMP:        BODY
1306 #   BODY:        empty
1307 #   diversions:  GROW, BODY |-
1309 # * TEST1 requires TEST2a
1310 # _m4_require_call m4_divert_pushes another temporary diversion,
1311 # GROW - 1, and expands TEST2a in there.
1312 #   DUMP:        BODY
1313 #   BODY:        empty
1314 #   GROW - 1:    TEST2a
1315 #   diversions:  GROW - 1, GROW, BODY |-
1316 # Than the content of the temporary diversion is moved to DUMP and the
1317 # temporary diversion is popped.
1318 #   DUMP:        BODY
1319 #   BODY:        TEST2a
1320 #   diversions:  GROW, BODY |-
1322 # * TEST1 requires TEST2b
1323 # Again, _m4_require_call pushes GROW - 1 and heads to expand TEST2b.
1324 #   DUMP:        BODY
1325 #   BODY:        TEST2a
1326 #   diversions:  GROW - 1, GROW, BODY |-
1328 # * TEST2b requires TEST3
1329 # _m4_require_call pushes GROW - 2 and expands TEST3 here.
1330 # (TEST3 requires TEST2a, but TEST2a has already been m4_provide'd, so
1331 # nothing happens.)
1332 #   DUMP:        BODY
1333 #   BODY:        TEST2a
1334 #   GROW - 2:    TEST3
1335 #   diversions:  GROW - 2, GROW - 1, GROW, BODY |-
1336 # Than the diversion is appended to DUMP, and popped.
1337 #   DUMP:        BODY
1338 #   BODY:        TEST2a; TEST3
1339 #   diversions:  GROW - 1, GROW, BODY |-
1341 # * TEST1 requires TEST2b (contd.)
1342 # The content of TEST2b is expanded...
1343 #   DUMP:        BODY
1344 #   BODY:        TEST2a; TEST3
1345 #   GROW - 1:    TEST2b,
1346 #   diversions:  GROW - 1, GROW, BODY |-
1347 # ... and moved to DUMP.
1348 #   DUMP:        BODY
1349 #   BODY:        TEST2a; TEST3; TEST2b
1350 #   diversions:  GROW, BODY |-
1352 # * TEST1 is expanded: epilogue
1353 # TEST1's own content is in GROW...
1354 #   DUMP:        BODY
1355 #   BODY:        TEST2a; TEST3; TEST2b
1356 #   GROW:        TEST1
1357 #   diversions:  BODY |-
1358 # ... and it's epilogue moves it to DUMP and then undefines DUMP.
1359 #   DUMP:       undefined
1360 #   BODY:       TEST2a; TEST3; TEST2b; TEST1
1361 #   diversions: BODY |-
1364 # 2. Keeping track of the expansion stack
1365 # =======================================
1367 # When M4 expansion goes wrong it is often extremely hard to find the
1368 # path amongst macros that drove to the failure.  What is needed is
1369 # the stack of macro `calls'. One could imagine that GNU M4 would
1370 # maintain a stack of macro expansions, unfortunately it doesn't, so
1371 # we do it by hand.  This is of course extremely costly, but the help
1372 # this stack provides is worth it.  Nevertheless to limit the
1373 # performance penalty this is implemented only for m4_defun'd macros,
1374 # not for define'd macros.
1376 # The scheme is simplistic: each time we enter an m4_defun'd macros,
1377 # we prepend its name in m4_expansion_stack, and when we exit the
1378 # macro, we remove it (thanks to pushdef/popdef).
1380 # In addition, we want to detect circular m4_require dependencies.
1381 # Each time we expand a macro FOO we define _m4_expanding(FOO); and
1382 # m4_require(BAR) simply checks whether _m4_expanding(BAR) is defined.
1385 # m4_expansion_stack_push(TEXT)
1386 # -----------------------------
1387 m4_define([m4_expansion_stack_push],
1388 [m4_pushdef([m4_expansion_stack],
1389             [$1]m4_ifdef([m4_expansion_stack], [
1390 _m4_defn([m4_expansion_stack])]))])
1393 # m4_expansion_stack_pop
1394 # ----------------------
1395 m4_define([m4_expansion_stack_pop],
1396 [m4_popdef([m4_expansion_stack])])
1399 # m4_expansion_stack_dump
1400 # -----------------------
1401 # Dump the expansion stack.
1402 m4_define([m4_expansion_stack_dump],
1403 [m4_ifdef([m4_expansion_stack],
1404           [m4_errprintn(_m4_defn([m4_expansion_stack]))])dnl
1405 m4_errprintn(m4_location[: the top level])])
1408 # _m4_divert(GROW)
1409 # ----------------
1410 # This diversion is used by the m4_defun/m4_require machinery.  It is
1411 # important to keep room before GROW because for each nested
1412 # AC_REQUIRE we use an additional diversion (i.e., two m4_require's
1413 # will use GROW - 2.  More than 3 levels has never seemed to be
1414 # needed.)
1416 # ...
1417 # - GROW - 2
1418 #   m4_require'd code, 2 level deep
1419 # - GROW - 1
1420 #   m4_require'd code, 1 level deep
1421 # - GROW
1422 #   m4_defun'd macros are elaborated here.
1424 m4_define([_m4_divert(GROW)],       10000)
1427 # _m4_defun_pro(MACRO-NAME)
1428 # -------------------------
1429 # The prologue for Autoconf macros.
1431 # This is called frequently, so minimize the number of macro invocations
1432 # by avoiding dnl and m4_defn overhead.
1433 m4_define([_m4_defun_pro],
1434 m4_do([[m4_ifdef([m4_expansion_stack], [], [_m4_defun_pro_outer[]])]],
1435       [[m4_expansion_stack_push(_m4_defn(
1436           [m4_location($1)])[: $1 is expanded from...])]],
1437       [[m4_pushdef([_m4_expanding($1)])]]))
1439 m4_define([_m4_defun_pro_outer],
1440 [m4_copy([_m4_divert_diversion], [_m4_divert_dump])m4_divert_push([GROW])])
1442 # _m4_defun_epi(MACRO-NAME)
1443 # -------------------------
1444 # The Epilogue for Autoconf macros.  MACRO-NAME only helps tracing
1445 # the PRO/EPI pairs.
1447 # This is called frequently, so minimize the number of macro invocations
1448 # by avoiding dnl and m4_popdef overhead.
1449 m4_define([_m4_defun_epi],
1450 m4_do([[_m4_popdef([_m4_expanding($1)])]],
1451       [[m4_expansion_stack_pop()]],
1452       [[m4_ifdef([m4_expansion_stack], [], [_m4_defun_epi_outer[]])]],
1453       [[m4_provide([$1])]]))
1455 m4_define([_m4_defun_epi_outer],
1456 [_m4_undefine([_m4_divert_dump])m4_divert_pop([GROW])m4_undivert([GROW])])
1459 # m4_defun(NAME, EXPANSION)
1460 # -------------------------
1461 # Define a macro which automatically provides itself.  Add machinery
1462 # so the macro automatically switches expansion to the diversion
1463 # stack if it is not already using it.  In this case, once finished,
1464 # it will bring back all the code accumulated in the diversion stack.
1465 # This, combined with m4_require, achieves the topological ordering of
1466 # macros.  We don't use this macro to define some frequently called
1467 # macros that are not involved in ordering constraints, to save m4
1468 # processing.
1469 m4_define([m4_defun],
1470 [m4_define([m4_location($1)], m4_location)dnl
1471 m4_define([$1],
1472           [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])
1475 # m4_defun_once(NAME, EXPANSION)
1476 # ------------------------------
1477 # As m4_defun, but issues the EXPANSION only once, and warns if used
1478 # several times.
1479 m4_define([m4_defun_once],
1480 [m4_define([m4_location($1)], m4_location)dnl
1481 m4_define([$1],
1482           [m4_provide_if([$1],
1483                          [m4_warn([syntax], [$1 invoked multiple times])],
1484                          [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])])
1487 # m4_pattern_forbid(ERE, [WHY])
1488 # -----------------------------
1489 # Declare that no token matching the forbidden extended regular
1490 # expression ERE should be seen in the output unless...
1491 m4_define([m4_pattern_forbid], [])
1494 # m4_pattern_allow(ERE)
1495 # ---------------------
1496 # ... that token also matches the allowed extended regular expression ERE.
1497 # Both used via traces.
1498 m4_define([m4_pattern_allow], [])
1501 ## --------------------------------- ##
1502 ## 11. Dependencies between macros.  ##
1503 ## --------------------------------- ##
1506 # m4_before(THIS-MACRO-NAME, CALLED-MACRO-NAME)
1507 # ---------------------------------------------
1508 # Issue a warning if CALLED-MACRO-NAME was called before THIS-MACRO-NAME.
1509 m4_define([m4_before],
1510 [m4_provide_if([$2],
1511                [m4_warn([syntax], [$2 was called before $1])])])
1514 # m4_require(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
1515 # -----------------------------------------------------------
1516 # If NAME-TO-CHECK has never been expanded (actually, if it is not
1517 # m4_provide'd), expand BODY-TO-EXPAND *before* the current macro
1518 # expansion.  Once expanded, emit it in _m4_divert_dump.  Keep track
1519 # of the m4_require chain in m4_expansion_stack.
1521 # The normal cases are:
1523 # - NAME-TO-CHECK == BODY-TO-EXPAND
1524 #   Which you can use for regular macros with or without arguments, e.g.,
1525 #     m4_require([AC_PROG_CC], [AC_PROG_CC])
1526 #     m4_require([AC_CHECK_HEADERS(limits.h)], [AC_CHECK_HEADERS(limits.h)])
1527 #   which is just the same as
1528 #     m4_require([AC_PROG_CC])
1529 #     m4_require([AC_CHECK_HEADERS(limits.h)])
1531 # - BODY-TO-EXPAND == m4_indir([NAME-TO-CHECK])
1532 #   In the case of macros with irregular names.  For instance:
1533 #     m4_require([AC_LANG_COMPILER(C)], [indir([AC_LANG_COMPILER(C)])])
1534 #   which means `if the macro named `AC_LANG_COMPILER(C)' (the parens are
1535 #   part of the name, it is not an argument) has not been run, then
1536 #   call it.'
1537 #   Had you used
1538 #     m4_require([AC_LANG_COMPILER(C)], [AC_LANG_COMPILER(C)])
1539 #   then m4_require would have tried to expand `AC_LANG_COMPILER(C)', i.e.,
1540 #   call the macro `AC_LANG_COMPILER' with `C' as argument.
1542 #   You could argue that `AC_LANG_COMPILER', when it receives an argument
1543 #   such as `C' should dispatch the call to `AC_LANG_COMPILER(C)'.  But this
1544 #   `extension' prevents `AC_LANG_COMPILER' from having actual arguments that
1545 #   it passes to `AC_LANG_COMPILER(C)'.
1547 # This is called frequently, so minimize the number of macro invocations
1548 # by avoiding dnl and other overhead on the common path.
1549 m4_define([m4_require],
1550 m4_do([[m4_ifdef([_m4_expanding($1)],
1551                  [m4_fatal([$0: circular dependency of $1])])]],
1552       [[m4_ifdef([_m4_divert_dump], [],
1553                  [m4_fatal([$0($1): cannot be used outside of an ]dnl
1554 m4_bmatch([$0], [^AC_], [[AC_DEFUN]], [[m4_defun]])['d macro])])]],
1555       [[m4_provide_if([$1],
1556                       [],
1557                       [_m4_require_call([$1], [$2])])]]))
1560 # _m4_require_call(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
1561 # -----------------------------------------------------------------
1562 # If m4_require decides to expand the body, it calls this macro.
1564 # This is called frequently, so minimize the number of macro invocations
1565 # by avoiding dnl and other overhead on the common path.
1566 m4_define([_m4_require_call],
1567 m4_do([[m4_define([_m4_divert_grow], m4_decr(_m4_divert_grow))]],
1568       [[m4_divert_push(_m4_divert_grow)]],
1569       [[m4_default([$2], [$1])
1570 m4_provide_if([$1],
1571               [],
1572               [m4_warn([syntax],
1573                        [$1 is m4_require'd but not m4_defun'd])])]],
1574       [[m4_divert(_m4_defn([_m4_divert_dump]))]],
1575       [[m4_undivert(_m4_divert_grow)]],
1576       [[m4_divert_pop(_m4_divert_grow)]],
1577       [[m4_define([_m4_divert_grow], m4_incr(_m4_divert_grow))]]))
1580 # _m4_divert_grow
1581 # ---------------
1582 # The counter for _m4_require_call.
1583 m4_define([_m4_divert_grow], _m4_divert([GROW]))
1586 # m4_expand_once(TEXT, [WITNESS = TEXT])
1587 # --------------------------------------
1588 # If TEXT has never been expanded, expand it *here*.  Use WITNESS as
1589 # as a memory that TEXT has already been expanded.
1590 m4_define([m4_expand_once],
1591 [m4_provide_if(m4_ifval([$2], [[$2]], [[$1]]),
1592                [],
1593                [m4_provide(m4_ifval([$2], [[$2]], [[$1]]))[]$1])])
1596 # m4_provide(MACRO-NAME)
1597 # ----------------------
1598 m4_define([m4_provide],
1599 [m4_define([m4_provide($1)])])
1602 # m4_provide_if(MACRO-NAME, IF-PROVIDED, IF-NOT-PROVIDED)
1603 # -------------------------------------------------------
1604 # If MACRO-NAME is provided do IF-PROVIDED, else IF-NOT-PROVIDED.
1605 # The purpose of this macro is to provide the user with a means to
1606 # check macros which are provided without letting her know how the
1607 # information is coded.
1608 m4_define([m4_provide_if],
1609 [m4_ifdef([m4_provide($1)],
1610           [$2], [$3])])
1613 ## --------------------- ##
1614 ## 12. Text processing.  ##
1615 ## --------------------- ##
1618 # m4_cr_letters
1619 # m4_cr_LETTERS
1620 # m4_cr_Letters
1621 # -------------
1622 m4_define([m4_cr_letters], [abcdefghijklmnopqrstuvwxyz])
1623 m4_define([m4_cr_LETTERS], [ABCDEFGHIJKLMNOPQRSTUVWXYZ])
1624 m4_define([m4_cr_Letters],
1625 m4_defn([m4_cr_letters])dnl
1626 m4_defn([m4_cr_LETTERS])dnl
1630 # m4_cr_digits
1631 # ------------
1632 m4_define([m4_cr_digits], [0123456789])
1635 # m4_cr_alnum
1636 # -----------
1637 m4_define([m4_cr_alnum],
1638 m4_defn([m4_cr_Letters])dnl
1639 m4_defn([m4_cr_digits])dnl
1643 # m4_cr_symbols1
1644 # m4_cr_symbols2
1645 # -------------------------------
1646 m4_define([m4_cr_symbols1],
1647 m4_defn([m4_cr_Letters])dnl
1650 m4_define([m4_cr_symbols2],
1651 m4_defn([m4_cr_symbols1])dnl
1652 m4_defn([m4_cr_digits])dnl
1655 # m4_cr_all
1656 # ---------
1657 # The character range representing everything, with `-' as the last
1658 # character, since it is special to m4_translit.  Use with care, because
1659 # it contains characters special to M4 (fortunately, both ASCII and EBCDIC
1660 # have [] in order, so m4_defn([m4_cr_all]) remains a valid string).  It
1661 # also contains characters special to terminals, so it should never be
1662 # displayed in an error message.  Also, attempts to map [ and ] to other
1663 # characters via m4_translit must deal with the fact that m4_translit does
1664 # not add quotes to the output.
1666 # It is mainly useful in generating inverted character range maps, for use
1667 # in places where m4_translit is faster than an equivalent m4_bpatsubst;
1668 # the regex `[^a-z]' is equivalent to:
1669 #  m4_translit(m4_dquote(m4_defn([m4_cr_all])), [a-z])
1670 m4_define([m4_cr_all],
1671 m4_translit(m4_dquote(m4_format(m4_dquote(m4_for(
1672   ,1,255,,[[%c]]))m4_for([i],1,255,,[,i]))), [-])-)
1675 # _m4_define_cr_not(CATEGORY)
1676 # ---------------------------
1677 # Define m4_cr_not_CATEGORY as the inverse of m4_cr_CATEGORY.
1678 m4_define([_m4_define_cr_not],
1679 [m4_define([m4_cr_not_$1],
1680            m4_translit(m4_dquote(m4_defn([m4_cr_all])),
1681                        m4_defn([m4_cr_$1])))])
1684 # m4_cr_not_letters
1685 # m4_cr_not_LETTERS
1686 # m4_cr_not_Letters
1687 # m4_cr_not_digits
1688 # m4_cr_not_alnum
1689 # m4_cr_not_symbols1
1690 # m4_cr_not_symbols2
1691 # ------------------
1692 # Inverse character sets
1693 _m4_define_cr_not([letters])
1694 _m4_define_cr_not([LETTERS])
1695 _m4_define_cr_not([Letters])
1696 _m4_define_cr_not([digits])
1697 _m4_define_cr_not([alnum])
1698 _m4_define_cr_not([symbols1])
1699 _m4_define_cr_not([symbols2])
1702 # m4_newline
1703 # ----------
1704 # Expands to a newline.  Exists for formatting reasons.
1705 m4_define([m4_newline], [
1709 # m4_re_escape(STRING)
1710 # --------------------
1711 # Escape RE active characters in STRING.
1712 m4_define([m4_re_escape],
1713 [m4_bpatsubst([$1],
1714               [[][*+.?\^$]], [\\\&])])
1717 # m4_re_string
1718 # ------------
1719 # Regexp for `[a-zA-Z_0-9]*'
1720 # m4_dquote provides literal [] for the character class.
1721 m4_define([m4_re_string],
1722 m4_dquote(m4_defn([m4_cr_symbols2]))dnl
1723 [*]dnl
1727 # m4_re_word
1728 # ----------
1729 # Regexp for `[a-zA-Z_][a-zA-Z_0-9]*'
1730 m4_define([m4_re_word],
1731 m4_dquote(m4_defn([m4_cr_symbols1]))dnl
1732 m4_defn([m4_re_string])dnl
1736 # m4_tolower(STRING)
1737 # m4_toupper(STRING)
1738 # ------------------
1739 # These macros convert STRING to lowercase or uppercase.
1741 # Rather than expand the m4_defn each time, we inline them up front.
1742 m4_define([m4_tolower],
1743 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_LETTERS]))[,
1744                    ]m4_dquote(m4_defn([m4_cr_letters]))[)])
1745 m4_define([m4_toupper],
1746 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_letters]))[,
1747                    ]m4_dquote(m4_defn([m4_cr_LETTERS]))[)])
1750 # m4_split(STRING, [REGEXP])
1751 # --------------------------
1753 # Split STRING into an m4 list of quoted elements.  The elements are
1754 # quoted with [ and ].  Beginning spaces and end spaces *are kept*.
1755 # Use m4_strip to remove them.
1757 # REGEXP specifies where to split.  Default is [\t ]+.
1759 # If STRING is empty, the result is an empty list.
1761 # Pay attention to the m4_changequotes.  When m4 reads the definition of
1762 # m4_split, it still has quotes set to [ and ].  Luckily, these are matched
1763 # in the macro body, so the definition is stored correctly.  Use the same
1764 # alternate quotes as m4_noquote; it must be unlikely to appear in $1.
1766 # Also, notice that $1 is quoted twice, since we want the result to
1767 # be quoted.  Then you should understand that the argument of
1768 # patsubst is -=<{(STRING)}>=- (i.e., with additional -=<{( and )}>=-).
1770 # This macro is safe on active symbols, i.e.:
1771 #   m4_define(active, ACTIVE)
1772 #   m4_split([active active ])end
1773 #   => [active], [active], []end
1775 # Optimize on regex of ` ' (space), since m4_foreach_w already guarantees
1776 # that the list contains single space separators, and a common case is
1777 # splitting a single-element list.  This macro is called frequently,
1778 # so avoid unnecessary dnl inside the definition.
1779 m4_define([m4_split],
1780 [m4_if([$1], [], [],
1781        [$2], [ ], [m4_if(m4_index([$1], [ ]), [-1], [[[$1]]], [_$0($@)])],
1782        [$2], [], [_$0([$1], [[   ]+])],
1783        [_$0($@)])])
1785 m4_define([_m4_split],
1786 [m4_changequote([-=<{(],[)}>=-])]dnl
1787 [[m4_bpatsubst(-=<{(-=<{($1)}>=-)}>=-, -=<{($2)}>=-,
1788                -=<{(], [)}>=-)]m4_changequote([, ])])
1792 # m4_flatten(STRING)
1793 # ------------------
1794 # If STRING contains end of lines, replace them with spaces.  If there
1795 # are backslashed end of lines, remove them.  This macro is safe with
1796 # active symbols.
1797 #    m4_define(active, ACTIVE)
1798 #    m4_flatten([active
1799 #    act\
1800 #    ive])end
1801 #    => active activeend
1803 # In m4, m4_bpatsubst is expensive, so first check for a newline.
1804 m4_define([m4_flatten],
1805 [m4_if(m4_index([$1], [
1806 ]), [-1], [[$1]],
1807        [m4_translit(m4_bpatsubst([[[$1]]], [\\
1808 ]), [
1809 ], [ ])])])
1812 # m4_strip(STRING)
1813 # ----------------
1814 # Expands into STRING with tabs and spaces singled out into a single
1815 # space, and removing leading and trailing spaces.
1817 # This macro is robust to active symbols.
1818 #    m4_define(active, ACTIVE)
1819 #    m4_strip([  active <tab> <tab>active ])end
1820 #    => active activeend
1822 # First, notice that we guarantee trailing space.  Why?  Because regular
1823 # expressions are greedy, and `.* ?' would always group the space into the
1824 # .* portion.  The algorithm is simpler by avoiding `?' at the end.  The
1825 # algorithm correctly strips everything if STRING is just ` '.
1827 # Then notice the second pattern: it is in charge of removing the
1828 # leading/trailing spaces.  Why not just `[^ ]'?  Because they are
1829 # applied to over-quoted strings, i.e. more or less [STRING], due
1830 # to the limitations of m4_bpatsubsts.  So the leading space in STRING
1831 # is the *second* character; equally for the trailing space.
1832 m4_define([m4_strip],
1833 [m4_bpatsubsts([$1 ],
1834                [[        ]+], [ ],
1835                [^. ?\(.*\) .$], [[[\1]]])])
1838 # m4_normalize(STRING)
1839 # --------------------
1840 # Apply m4_flatten and m4_strip to STRING.
1842 # The argument is quoted, so that the macro is robust to active symbols:
1844 #    m4_define(active, ACTIVE)
1845 #    m4_normalize([  act\
1846 #    ive
1847 #    active ])end
1848 #    => active activeend
1850 m4_define([m4_normalize],
1851 [m4_strip(m4_flatten([$1]))])
1855 # m4_join(SEP, ARG1, ARG2...)
1856 # ---------------------------
1857 # Produce ARG1SEPARG2...SEPARGn.  Avoid back-to-back SEP when a given ARG
1858 # is the empty string.  No expansion is performed on SEP or ARGs.
1860 # Since the number of arguments to join can be arbitrarily long, we
1861 # want to avoid having more than one $@ in the macro definition;
1862 # otherwise, the expansion would require twice the memory of the already
1863 # long list.  Hence, m4_join merely looks for the first non-empty element,
1864 # and outputs just that element; while _m4_join looks for all non-empty
1865 # elements, and outputs them following a separator.  The final trick to
1866 # note is that we decide between recursing with $0 or _$0 based on the
1867 # nested m4_if ending with `_'.
1868 m4_define([m4_join],
1869 [m4_if([$#], [1], [],
1870        [$#], [2], [[$2]],
1871        [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift2($@))])])
1872 m4_define([_m4_join],
1873 [m4_if([$#$2], [2], [],
1874        [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift2($@))])])
1876 # m4_joinall(SEP, ARG1, ARG2...)
1877 # ------------------------------
1878 # Produce ARG1SEPARG2...SEPARGn.  An empty ARG results in back-to-back SEP.
1879 # No expansion is performed on SEP or ARGs.
1880 m4_define([m4_joinall], [[$2]_$0([$1], m4_shift($@))])
1881 m4_define([_m4_joinall],
1882 [m4_if([$#], [2], [], [[$1$3]$0([$1], m4_shift2($@))])])
1884 # m4_combine([SEPARATOR], PREFIX-LIST, [INFIX], SUFFIX...)
1885 # --------------------------------------------------------
1886 # Produce the pairwise combination of every element in the quoted,
1887 # comma-separated PREFIX-LIST with every element from the SUFFIX arguments.
1888 # Each pair is joined with INFIX, and pairs are separated by SEPARATOR.
1889 # No expansion occurs on SEPARATOR, INFIX, or elements of either list.
1891 # For example:
1892 #   m4_combine([, ], [[a], [b], [c]], [-], [1], [2], [3])
1893 #   => a-1, a-2, a-3, b-1, b-2, b-3, c-1, c-2, c-3
1895 # In order to have the correct number of SEPARATORs, we use a temporary
1896 # variable that redefines itself after the first use.  We must use defn
1897 # rather than overquoting in case PREFIX or SUFFIX contains $1, but use
1898 # _m4_defn for speed.  Likewise, we compute the m4_shift3 only once,
1899 # rather than in each iteration of the outer m4_foreach.
1900 m4_define([m4_combine],
1901 [m4_if(m4_eval([$# > 3]), [1],
1902        [m4_pushdef([m4_Separator], [m4_define([m4_Separator],
1903                                     _m4_defn([m4_echo]))])]]dnl
1904 [[m4_foreach([m4_Prefix], [$2],
1905              [m4_foreach([m4_Suffix], ]m4_dquote(m4_dquote(m4_shift3($@)))[,
1906         [m4_Separator([$1])[]_m4_defn([m4_Prefix])[$3]_m4_defn(
1907                                                       [m4_Suffix])])])]]dnl
1908 [[_m4_popdef([m4_Separator])])])
1911 # m4_append(MACRO-NAME, STRING, [SEPARATOR])
1912 # ------------------------------------------
1913 # Redefine MACRO-NAME to hold its former content plus `SEPARATOR`'STRING'
1914 # at the end.  It is valid to use this macro with MACRO-NAME undefined,
1915 # in which case no SEPARATOR is added.  Be aware that the criterion is
1916 # `not being defined', and not `not being empty'.
1918 # Note that neither STRING nor SEPARATOR are expanded here; rather, when
1919 # you expand MACRO-NAME, they will be expanded at that point in time.
1921 # This macro is robust to active symbols.  It can be used to grow
1922 # strings.
1924 #    | m4_define(active, ACTIVE)dnl
1925 #    | m4_append([sentence], [This is an])dnl
1926 #    | m4_append([sentence], [ active ])dnl
1927 #    | m4_append([sentence], [symbol.])dnl
1928 #    | sentence
1929 #    | m4_undefine([active])dnl
1930 #    | sentence
1931 #    => This is an ACTIVE symbol.
1932 #    => This is an active symbol.
1934 # It can be used to define hooks.
1936 #    | m4_define(active, ACTIVE)dnl
1937 #    | m4_append([hooks], [m4_define([act1], [act2])])dnl
1938 #    | m4_append([hooks], [m4_define([act2], [active])])dnl
1939 #    | m4_undefine([active])dnl
1940 #    | act1
1941 #    | hooks
1942 #    | act1
1943 #    => act1
1944 #    =>
1945 #    => active
1947 # It can also be used to create lists, although this particular usage was
1948 # broken prior to autoconf 2.62.
1949 #    | m4_append([list], [one], [, ])dnl
1950 #    | m4_append([list], [two], [, ])dnl
1951 #    | m4_append([list], [three], [, ])dnl
1952 #    | list
1953 #    | m4_dquote(list)
1954 #    => one, two, three
1955 #    => [one],[two],[three]
1957 # Note that m4_append can benefit from amortized O(n) m4 behavior, if
1958 # the underlying m4 implementation is smart enough to avoid copying existing
1959 # contents when enlarging a macro's definition into any pre-allocated storage
1960 # (m4 1.4.x unfortunately does not implement this optimization).  We do
1961 # not implement m4_prepend, since it is inherently O(n^2) (pre-allocated
1962 # storage only occurs at the end of a macro, so the existing contents must
1963 # always be moved).
1965 # Use _m4_defn for speed.
1966 m4_define([m4_append],
1967 [m4_define([$1], m4_ifdef([$1], [_m4_defn([$1])[$3]])[$2])])
1970 # m4_append_uniq(MACRO-NAME, STRING, [SEPARATOR], [IF-UNIQ], [IF-DUP])
1971 # --------------------------------------------------------------------
1972 # Like `m4_append', but append only if not yet present.  Additionally,
1973 # expand IF-UNIQ if STRING was appended, or IF-DUP if STRING was already
1974 # present.  Also, warn if SEPARATOR is not empty and occurs within STRING,
1975 # as the algorithm no longer guarantees uniqueness.
1977 # Note that while m4_append can be O(n) (depending on the quality of the
1978 # underlying M4 implementation), m4_append_uniq is inherently O(n^2)
1979 # because each append operation searches the entire string.
1980 m4_define([m4_append_uniq],
1981 [m4_ifval([$3], [m4_if(m4_index([$2], [$3]), [-1], [],
1982                        [m4_warn([syntax],
1983                                 [$0: `$2' contains `$3'])])])_$0($@)])
1984 m4_define([_m4_append_uniq],
1985 [m4_ifdef([$1],
1986           [m4_if(m4_index([$3]_m4_defn([$1])[$3], [$3$2$3]), [-1],
1987                  [m4_append([$1], [$2], [$3])$4], [$5])],
1988           [m4_define([$1], [$2])$4])])
1990 # m4_append_uniq_w(MACRO-NAME, STRINGS)
1991 # -------------------------------------
1992 # For each of the words in the whitespace separated list STRINGS, append
1993 # only the unique strings to the definition of MACRO-NAME.
1995 # Use _m4_defn for speed.
1996 m4_define([m4_append_uniq_w],
1997 [m4_foreach_w([m4_Word], [$2],
1998               [_m4_append_uniq([$1], _m4_defn([m4_Word]), [ ])])])
2001 # m4_text_wrap(STRING, [PREFIX], [FIRST-PREFIX], [WIDTH])
2002 # -------------------------------------------------------
2003 # Expands into STRING wrapped to hold in WIDTH columns (default = 79).
2004 # If PREFIX is given, each line is prefixed with it.  If FIRST-PREFIX is
2005 # specified, then the first line is prefixed with it.  As a special case,
2006 # if the length of FIRST-PREFIX is greater than that of PREFIX, then
2007 # FIRST-PREFIX will be left alone on the first line.
2009 # No expansion occurs on the contents STRING, PREFIX, or FIRST-PREFIX,
2010 # although quadrigraphs are correctly recognized.
2012 # Typical outputs are:
2014 # m4_text_wrap([Short string */], [   ], [/* ], 20)
2015 #  => /* Short string */
2017 # m4_text_wrap([Much longer string */], [   ], [/* ], 20)
2018 #  => /* Much longer
2019 #  =>    string */
2021 # m4_text_wrap([Short doc.], [          ], [  --short ], 30)
2022 #  =>   --short Short doc.
2024 # m4_text_wrap([Short doc.], [          ], [  --too-wide ], 30)
2025 #  =>   --too-wide
2026 #  =>           Short doc.
2028 # m4_text_wrap([Super long documentation.], [          ], [  --too-wide ], 30)
2029 #  =>   --too-wide
2030 #  =>      Super long
2031 #  =>      documentation.
2033 # FIXME: there is no checking of a longer PREFIX than WIDTH, but do
2034 # we really want to bother with people trying each single corner
2035 # of a software?
2037 # This macro does not leave a trailing space behind the last word of a line,
2038 # which complicates it a bit.  The algorithm is otherwise stupid and simple:
2039 # all the words are preceded by m4_Separator which is defined to empty for
2040 # the first word, and then ` ' (single space) for all the others.
2042 # The algorithm uses a helper that uses $2 through $4 directly, rather than
2043 # using local variables, to avoid m4_defn overhead, or expansion swallowing
2044 # any $.  It also bypasses m4_popdef overhead with _m4_popdef since no user
2045 # macro expansion occurs in the meantime.  Also, the definition is written
2046 # with m4_do, to avoid time wasted on dnl during expansion (since this is
2047 # already a time-consuming macro).
2048 m4_define([m4_text_wrap],
2049 [_$0([$1], [$2], m4_if([$3], [], [[$2]], [[$3]]),
2050      m4_if([$4], [], [79], [[$4]]))])
2051 m4_define([_m4_text_wrap],
2052 m4_do(dnl set up local variables, to avoid repeated calculations
2053 [[m4_pushdef([m4_Indent], m4_qlen([$2]))]],
2054 [[m4_pushdef([m4_Cursor], m4_qlen([$3]))]],
2055 [[m4_pushdef([m4_Separator], [m4_define([m4_Separator], [ ])])]],
2056 dnl expand the first prefix, then check its length vs. regular prefix
2057 dnl same length: nothing special
2058 dnl prefix1 longer: output on line by itself, and reset cursor
2059 dnl prefix1 shorter: pad to length of prefix, and reset cursor
2060 [[[$3]m4_cond([m4_Cursor], m4_Indent, [],
2061               [m4_eval(m4_Cursor > m4_Indent)], [1], [
2062 [$2]m4_define([m4_Cursor], m4_Indent)],
2063               [m4_format([%*s], m4_max([0],
2064   m4_eval(m4_Indent - m4_Cursor)), [])m4_define([m4_Cursor], m4_Indent)])]],
2065 dnl now, for each word, compute the curser after the word is output, then
2066 dnl check if the cursor would exceed the wrap column
2067 dnl if so, reset cursor, and insert newline and prefix
2068 dnl if not, insert the separator (usually a space)
2069 dnl either way, insert the word
2070 [[m4_foreach_w([m4_Word], [$1],
2071   [m4_define([m4_Cursor],
2072              m4_eval(m4_Cursor + m4_qlen(_m4_defn([m4_Word]))
2073                      + 1))m4_if(m4_eval(m4_Cursor > ([$4])),
2074       [1], [m4_define([m4_Cursor],
2075                       m4_eval(m4_Indent + m4_qlen(_m4_defn([m4_Word])) + 1))
2076 [$2]],
2077       [m4_Separator[]])_m4_defn([m4_Word])])]],
2078 dnl finally, clean up the local variabls
2079 [[_m4_popdef([m4_Separator], [m4_Cursor], [m4_Indent])]]))
2082 # m4_text_box(MESSAGE, [FRAME-CHARACTER = `-'])
2083 # ---------------------------------------------
2084 # Turn MESSAGE into:
2085 #  ## ------- ##
2086 #  ## MESSAGE ##
2087 #  ## ------- ##
2088 # using FRAME-CHARACTER in the border.
2089 m4_define([m4_text_box],
2090 [m4_pushdef([m4_Border],
2091             m4_translit(m4_format([%*s], m4_qlen(m4_expand([$1])), []),
2092                         [ ], m4_if([$2], [], [[-]], [[$2]])))dnl
2093 @%:@@%:@ m4_Border @%:@@%:@
2094 @%:@@%:@ $1 @%:@@%:@
2095 @%:@@%:@ m4_Border @%:@@%:@_m4_popdef([m4_Border])dnl
2099 # m4_qlen(STRING)
2100 # ---------------
2101 # Expands to the length of STRING after autom4te converts all quadrigraphs.
2103 # Avoid bpatsubsts for the common case of no quadrigraphs.
2104 m4_define([m4_qlen],
2105 [m4_if(m4_index([$1], [@]), [-1], [m4_len([$1])],
2106        [m4_len(m4_bpatsubst([[$1]],
2107                             [@\(\(<:\|:>\|S|\|%:\|\{:\|:\}\)\(@\)\|&t@\)],
2108                             [\3]))])])
2111 # m4_qdelta(STRING)
2112 # -----------------
2113 # Expands to the net change in the length of STRING from autom4te converting the
2114 # quadrigraphs in STRING.  This number is always negative or zero.
2115 m4_define([m4_qdelta],
2116 [m4_eval(m4_qlen([$1]) - m4_len([$1]))])
2120 ## ----------------------- ##
2121 ## 13. Number processing.  ##
2122 ## ----------------------- ##
2124 # m4_cmp(A, B)
2125 # ------------
2126 # Compare two integer expressions.
2127 # A < B -> -1
2128 # A = B ->  0
2129 # A > B ->  1
2130 m4_define([m4_cmp],
2131 [m4_eval((([$1]) > ([$2])) - (([$1]) < ([$2])))])
2134 # m4_list_cmp(A, B)
2135 # -----------------
2137 # Compare the two lists of integer expressions A and B.  For instance:
2138 #   m4_list_cmp([1, 0],     [1])    ->  0
2139 #   m4_list_cmp([1, 0],     [1, 0]) ->  0
2140 #   m4_list_cmp([1, 2],     [1, 0]) ->  1
2141 #   m4_list_cmp([1, 2, 3],  [1, 2]) ->  1
2142 #   m4_list_cmp([1, 2, -3], [1, 2]) -> -1
2143 #   m4_list_cmp([1, 0],     [1, 2]) -> -1
2144 #   m4_list_cmp([1],        [1, 2]) -> -1
2145 #   m4_define([xa], [oops])dnl
2146 #   m4_list_cmp([[0xa]],    [5+5])  -> 0
2148 # Rather than face the overhead of m4_case, we use a helper function whose
2149 # expansion includes the name of the macro to invoke on the tail, either
2150 # m4_ignore or m4_unquote.  This is particularly useful when comparing
2151 # long lists, since less text is being expanded for deciding when to end
2152 # recursion.
2153 m4_define([m4_list_cmp],
2154 [m4_if([$1$2], [], 0,
2155        [$1], [], [$0(0, [$2])],
2156        [$2], [], [$0([$1], 0)],
2157        [$1], [$2], 0,
2158        [_$0(m4_cmp(m4_car($1), m4_car($2)))([$0(m4_cdr($1), m4_cdr($2))])])])
2159 m4_define([_m4_list_cmp],
2160 [m4_if([$1], 0, [m4_unquote], [$1m4_ignore])])
2162 # m4_max(EXPR, ...)
2163 # m4_min(EXPR, ...)
2164 # -----------------
2165 # Return the decimal value of the maximum (or minimum) in a series of
2166 # integer expressions.
2168 # M4 1.4.x doesn't provide ?:.  Hence this huge m4_eval.  Avoid m4_eval
2169 # if both arguments are identical, but be aware of m4_max(0xa, 10) (hence
2170 # the use of <=, not just <, in the second multiply).
2171 m4_define([m4_max],
2172 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
2173        [$#], [1], [m4_eval([$1])],
2174        [$#$1], [2$2], [m4_eval([$1])],
2175        [$#], [2], [_$0($@)],
2176        [_m4_minmax([_$0], $@)])])
2178 m4_define([_m4_max],
2179 [m4_eval((([$1]) > ([$2])) * ([$1]) + (([$1]) <= ([$2])) * ([$2]))])
2181 m4_define([m4_min],
2182 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
2183        [$#], [1], [m4_eval([$1])],
2184        [$#$1], [2$2], [m4_eval([$1])],
2185        [$#], [2], [_$0($@)],
2186        [_m4_minmax([_$0], $@)])])
2188 m4_define([_m4_min],
2189 [m4_eval((([$1]) < ([$2])) * ([$1]) + (([$1]) >= ([$2])) * ([$2]))])
2191 # _m4_minmax(METHOD, ARG1, ARG2...)
2192 # ---------------------------------
2193 # Common recursion code for m4_max and m4_min.  METHOD must be _m4_max
2194 # or _m4_min, and there must be at least two arguments to combine.
2195 m4_define([_m4_minmax],
2196 [m4_if([$#], [3], [$1([$2], [$3])],
2197        [$0([$1], $1([$2], [$3]), m4_shift3($@))])])
2200 # m4_sign(A)
2201 # ----------
2202 # The sign of the integer expression A.
2203 m4_define([m4_sign],
2204 [m4_eval((([$1]) > 0) - (([$1]) < 0))])
2208 ## ------------------------ ##
2209 ## 14. Version processing.  ##
2210 ## ------------------------ ##
2213 # m4_version_unletter(VERSION)
2214 # ----------------------------
2215 # Normalize beta version numbers with letters to numeric expressions, which
2216 # can then be handed to m4_eval for the purpose of comparison.
2218 #   Nl -> (N+1).-1.(l#)
2220 # for example:
2221 #   [2.14a] -> [2.14+1.-1.[0r36:a]] -> 2.15.-1.10
2222 #   [2.14b] -> [2.15+1.-1.[0r36:b]] -> 2.15.-1.11
2223 #   [2.61aa.b] -> [2.61+1.-1.[0r36:aa],+1.-1.[0r36:b]] -> 2.62.-1.370.1.-1.11
2225 # This macro expects reasonable version numbers, but can handle double
2226 # letters and does not expand any macros.  Original version strings can
2227 # use both `.' and `-' separators.
2229 # Inline constant expansions, to avoid m4_defn overhead.
2230 # _m4_version_unletter is the real workhorse used by m4_version_compare,
2231 # but since [0r36:a] is less readable than 10, we provide a wrapper for
2232 # human use.
2233 m4_define([m4_version_unletter],
2234 [m4_map_sep([m4_eval], [.],
2235             m4_dquote(m4_dquote_elt(m4_unquote(_$0([$1])))))])
2236 m4_define([_m4_version_unletter],
2237 [m4_bpatsubst(m4_translit([[[$1]]], [.-], [,,]),]dnl
2238 m4_dquote(m4_dquote(m4_defn([m4_cr_Letters])))[[+],
2239               [+1,-1,[0r36:\&]])])
2242 # m4_version_compare(VERSION-1, VERSION-2)
2243 # ----------------------------------------
2244 # Compare the two version numbers and expand into
2245 #  -1 if VERSION-1 < VERSION-2
2246 #   0 if           =
2247 #   1 if           >
2248 m4_define([m4_version_compare],
2249 [m4_list_cmp(_m4_version_unletter([$1]), _m4_version_unletter([$2]))])
2252 # m4_PACKAGE_NAME
2253 # m4_PACKAGE_TARNAME
2254 # m4_PACKAGE_VERSION
2255 # m4_PACKAGE_STRING
2256 # m4_PACKAGE_BUGREPORT
2257 # --------------------
2258 # If m4sugar/version.m4 is present, then define version strings.  This
2259 # file is optional, provided by Autoconf but absent in Bison.
2260 m4_sinclude([m4sugar/version.m4])
2263 # m4_version_prereq(VERSION, [IF-OK], [IF-NOT = FAIL])
2264 # ----------------------------------------------------
2265 # Check this Autoconf version against VERSION.
2266 m4_define([m4_version_prereq],
2267 m4_ifdef([m4_PACKAGE_VERSION],
2268 [[m4_if(m4_version_compare(]m4_dquote(m4_defn([m4_PACKAGE_VERSION]))[, [$1]),
2269         [-1],
2270         [m4_default([$3],
2271                     [m4_fatal([Autoconf version $1 or higher is required],
2272                               [63])])],
2273         [$2])]],
2274 [[m4_fatal([m4sugar/version.m4 not found])]]))
2278 ## ------------------- ##
2279 ## 15. File handling.  ##
2280 ## ------------------- ##
2283 # It is a real pity that M4 comes with no macros to bind a diversion
2284 # to a file.  So we have to deal without, which makes us a lot more
2285 # fragile than we should.
2288 # m4_file_append(FILE-NAME, CONTENT)
2289 # ----------------------------------
2290 m4_define([m4_file_append],
2291 [m4_syscmd([cat >>$1 <<_m4eof
2293 _m4eof
2295 m4_if(m4_sysval, [0], [],
2296       [m4_fatal([$0: cannot write: $1])])])
2300 ## ------------------------ ##
2301 ## 16. Setting M4sugar up.  ##
2302 ## ------------------------ ##
2305 # m4_init
2306 # -------
2307 # Initialize the m4sugar language.
2308 m4_define([m4_init],
2309 [# All the M4sugar macros start with `m4_', except `dnl' kept as is
2310 # for sake of simplicity.
2311 m4_pattern_forbid([^_?m4_])
2312 m4_pattern_forbid([^dnl$])
2314 # If __m4_version__ is defined, we assume that we are being run by M4
2315 # 1.6 or newer, and thus that $@ recursion is linear; nothing further
2316 # needs to be done.  But if it is missing, we assume we are being run
2317 # by M4 1.4.x, that $@ recursion is quadratic, and that we need
2318 # foreach-based replacement macros.
2319 m4_ifndef([__m4_version__], [m4_include([m4sugar/foreach.m4])])
2321 # _m4_divert_diversion should be defined:
2322 m4_divert_push([KILL])
2324 # Check the divert push/pop perfect balance.
2325 m4_wrap([m4_divert_pop([])
2326          m4_ifdef([_m4_divert_diversion],
2327            [m4_fatal([$0: unbalanced m4_divert_push:]_m4_divert_n_stack)])[]])