Fix yesterday's regression in m4_wrap([$1]).
[autoconf.git] / lib / m4sugar / m4sugar.m4
blobe755f98ab86413111d81acda596748a0c8d73d5b
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 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.
53 changequote()
54 changequote([, ])
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.
59 ifdef([__gnu__], ,
60 [errprint(M4sugar requires GNU M4. Install it before installing M4sugar or
61 set the M4 environment variable to its absolute file name.)
62 m4exit(2)])
65 ## ------------------------------- ##
66 ## 1. Simulate --prefix-builtins.  ##
67 ## ------------------------------- ##
69 # m4_define
70 # m4_defn
71 # m4_undefine
72 define([m4_define],   defn([define]))
73 define([m4_defn],     defn([defn]))
74 define([m4_undefine], defn([undefine]))
76 m4_undefine([define])
77 m4_undefine([defn])
78 m4_undefine([undefine])
81 # m4_copy(SRC, DST)
82 # -----------------
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'.
92 m4_define([m4_copy],
93 [m4_define([$2], m4_defn([$1]))])
96 # m4_rename(SRC, DST)
97 # -------------------
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])
125 m4_rename_m4([decr])
126 m4_undefine([divert])
127 m4_rename_m4([divnum])
128 m4_rename_m4([dumpdef])
129 m4_rename_m4([errprint])
130 m4_rename_m4([esyscmd])
131 m4_rename_m4([eval])
132 m4_rename_m4([format])
133 m4_rename_m4([ifdef])
134 m4_rename([ifelse], [m4_if])
135 m4_undefine([include])
136 m4_rename_m4([incr])
137 m4_rename_m4([index])
138 m4_rename_m4([indir])
139 m4_rename_m4([len])
140 m4_rename([m4exit], [m4_exit])
141 m4_undefine([m4wrap])
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 ## ------------------- ##
169 # m4_location
170 # -----------
171 m4_define([m4_location],
172 [__file__:__line__])
175 # m4_errprintn(MSG)
176 # -----------------
177 # Same as `errprint', but with the missing end of line.
178 m4_define([m4_errprintn],
179 [m4_errprint([$1
180 ])])
183 # m4_warning(MSG)
184 # ---------------
185 # Warn the user.
186 m4_define([m4_warning],
187 [m4_errprintn(m4_location[: warning: $1])])
190 # m4_fatal(MSG, [EXIT-STATUS])
191 # ----------------------------
192 # Fatal the user.                                                      :)
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])])])
209 ## ------------- ##
210 ## 3. Warnings.  ##
211 ## ------------- ##
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.
228 m4_define([m4_warn],
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
256 # been included.
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)])])
263 # m4_include(FILE)
264 # ----------------
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])])
271 # m4_sinclude(FILE)
272 # -----------------
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
295 # it runs TRUE, etc.
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])])
306 # m4_n(TEXT)
307 # ----------
308 # If TEXT is not empty, return TEXT and a new line, otherwise nothing.
309 m4_define([m4_n],
310 [m4_if([$1],
311        [], [],
312            [$1
313 ])])
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],
321 [m4_if([$1],
322        [],   [m4_n([$3])],
323              [m4_n([$2])])])
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],
331 [m4_ifdef([$1],
332           [m4_ifval(m4_defn([$1]), [$2], [$3])],
333           [$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 # -----------------------------------------------------------
344 # m4 equivalent of
345 # switch (SWITCH)
346 # {
347 #   case VAL1:
348 #     IF-VAL1;
349 #     break;
350 #   case VAL2:
351 #     IF-VAL2;
352 #     break;
353 #   ...
354 #   default:
355 #     DEFAULT;
356 #     break;
357 # }.
358 # All the values are optional, and the macro is robust to active
359 # symbols properly quoted.
360 m4_define([m4_case],
361 [m4_if([$#], 0, [],
362        [$#], 1, [],
363        [$#], 2, [$2],
364        [$1], [$2], [$3],
365        [$0([$1], m4_shift3($@))])])
368 # m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT)
369 # -----------------------------------------------------
370 # m4 equivalent of
372 # if (SWITCH =~ RE1)
373 #   VAL1;
374 # elif (SWITCH =~ RE2)
375 #   VAL2;
376 # elif ...
377 #   ...
378 # else
379 #   DEFAULT
381 # All the values are optional, and the macro is robust to active symbols
382 # properly quoted.
383 m4_define([m4_bmatch],
384 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
385        [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
386        [$#], 2, [$2],
387        [m4_if(m4_bregexp([$1], [$2]), -1, [$0([$1], m4_shift3($@))],
388               [$3])])])
391 # m4_car(LIST)
392 # m4_cdr(LIST)
393 # ------------
394 # Manipulate m4 lists.
395 m4_define([m4_car], [[$1]])
396 m4_define([m4_cdr],
397 [m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])],
398        [$#], 1, [],
399        [m4_dquote(m4_shift($@))])])
401 # _m4_cdr(LIST)
402 # -------------
403 # Like m4_cdr, except include a leading comma unless only one element
404 # remains.  Why?  Because comparing a large list against [] is more
405 # expensive in expansion time than comparing the number of arguments; so
406 # _m4_cdr can be used to reduce the number of arguments when it is time
407 # to end recursion.
408 m4_define([_m4_cdr],
409 [m4_if([$#], 1, [],
410        [, m4_dquote(m4_shift($@))])])
414 # m4_cond(TEST1, VAL1, IF-VAL1, TEST2, VAL2, IF-VAL2, ..., [DEFAULT])
415 # -------------------------------------------------------------------
416 # Similar to m4_if, except that each TEST is expanded when encountered.
417 # If the expansion of TESTn matches the string VALn, the result is IF-VALn.
418 # The result is DEFAULT if no tests passed.  This macro allows
419 # short-circuiting of expensive tests, where it pays to arrange quick
420 # filter tests to run first.
422 # For an example, consider a previous implementation of _AS_QUOTE_IFELSE:
424 #    m4_if(m4_index([$1], [\]), [-1], [$2],
425 #          m4_eval(m4_index([$1], [\\]) >= 0), [1], [$2],
426 #          m4_eval(m4_index([$1], [\$]) >= 0), [1], [$2],
427 #          m4_eval(m4_index([$1], [\`]) >= 0), [1], [$3],
428 #          m4_eval(m4_index([$1], [\"]) >= 0), [1], [$3],
429 #          [$2])
431 # Here, m4_index is computed 5 times, and m4_eval 4, even if $1 contains
432 # no backslash.  It is more efficient to do:
434 #    m4_cond([m4_index([$1], [\])], [-1], [$2],
435 #            [m4_eval(m4_index([$1], [\\]) >= 0)], [1], [$2],
436 #            [m4_eval(m4_index([$1], [\$]) >= 0)], [1], [$2],
437 #            [m4_eval(m4_index([$1], [\`]) >= 0)], [1], [$3],
438 #            [m4_eval(m4_index([$1], [\"]) >= 0)], [1], [$3],
439 #            [$2])
441 # In the common case of $1 with no backslash, only one m4_index expansion
442 # occurs, and m4_eval is avoided altogether.
443 m4_define([m4_cond],
444 [m4_if([$#], [0], [m4_fatal([$0: cannot be called without arguments])],
445        [$#], [1], [$1],
446        [$#], [2], [m4_fatal([$0: missing an argument])],
447        [m4_if($1, [$2], [$3], [$0(m4_shift3($@))])])])
450 ## ---------------------------------------- ##
451 ## 6. Enhanced version of some primitives.  ##
452 ## ---------------------------------------- ##
454 # m4_bpatsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...)
455 # ----------------------------------------------------
456 # m4 equivalent of
458 #   $_ = STRING;
459 #   s/RE1/SUBST1/g;
460 #   s/RE2/SUBST2/g;
461 #   ...
463 # All the values are optional, and the macro is robust to active symbols
464 # properly quoted.
466 # I would have liked to name this macro `m4_bpatsubst', unfortunately,
467 # due to quotation problems, I need to double quote $1 below, therefore
468 # the anchors are broken :(  I can't let users be trapped by that.
470 # Recall that m4_shift3 always results in an argument.  Hence, we need
471 # to distinguish between a final deletion vs. ending recursion.
472 m4_define([m4_bpatsubsts],
473 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
474        [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
475        [$#], 2, [m4_builtin([patsubst], [$1], [$2])],
476        [_$0($@m4_if(m4_eval($# & 1), 0, [,]))])])
477 m4_define([_m4_bpatsubsts],
478 [m4_if([$#], 2, [$1],
479        [$0(m4_builtin([patsubst], [[$1]], [$2], [$3]),
480            m4_shift3($@))])])
483 # m4_define_default(MACRO, VALUE)
484 # -------------------------------
485 # If MACRO is undefined, set it to VALUE.
486 m4_define([m4_define_default],
487 [m4_ifndef([$1], [m4_define($@)])])
490 # m4_default(EXP1, EXP2)
491 # ----------------------
492 # Returns EXP1 if non empty, otherwise EXP2.
494 # This macro is called on hot paths, so inline the contents of m4_ifval,
495 # for one less round of expansion.
496 m4_define([m4_default],
497 [m4_if([$1], [], [$2], [$1])])
500 # m4_defn(NAME)
501 # -------------
502 # Like the original, except don't tolerate popping something which is
503 # undefined, and only support one argument.
505 # This macro is called frequently, so minimize the amount of additional
506 # expansions by skipping m4_ifndef.
507 m4_define([m4_defn],
508 [m4_ifdef([$1], [],
509           [m4_fatal([$0: undefined macro: $1])])]dnl
510 [m4_builtin([defn], [$1])])
513 # _m4_dumpdefs_up(NAME)
514 # ---------------------
515 m4_define([_m4_dumpdefs_up],
516 [m4_ifdef([$1],
517           [m4_pushdef([_m4_dumpdefs], m4_defn([$1]))dnl
518 m4_dumpdef([$1])dnl
519 m4_popdef([$1])dnl
520 _m4_dumpdefs_up([$1])])])
523 # _m4_dumpdefs_down(NAME)
524 # -----------------------
525 m4_define([_m4_dumpdefs_down],
526 [m4_ifdef([_m4_dumpdefs],
527           [m4_pushdef([$1], m4_defn([_m4_dumpdefs]))dnl
528 m4_popdef([_m4_dumpdefs])dnl
529 _m4_dumpdefs_down([$1])])])
532 # m4_dumpdefs(NAME)
533 # -----------------
534 # Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its
535 # value stack (most recent displayed first).
536 m4_define([m4_dumpdefs],
537 [_m4_dumpdefs_up([$1])dnl
538 _m4_dumpdefs_down([$1])])
541 # m4_popdef(NAME)
542 # ---------------
543 # Like the original, except don't tolerate popping something which is
544 # undefined, and only support one argument.
546 # This macro is called frequently, so minimize the amount of additional
547 # expansions by skipping m4_ifndef.
548 m4_define([m4_popdef],
549 [m4_ifdef([$1], [],
550           [m4_fatal([$0: undefined macro: $1])])]dnl
551 [m4_builtin([popdef], [$1])])
554 # m4_shiftn(N, ...)
555 # -----------------
556 # Returns ... shifted N times.  Useful for recursive "varargs" constructs.
558 # Autoconf does not use this macro, because it is inherently slower than
559 # calling the common cases of m4_shift2 or m4_shift3 directly.  But it
560 # might as well be fast for other clients, such as Libtool.  One way to
561 # do this is to expand $@ only once in _m4_shiftn (otherwise, for long
562 # lists, the expansion of m4_if takes twice as much memory as what the
563 # list itself occupies, only to throw away the unused branch).  The end
564 # result is strictly equivalent to
565 #   m4_if([$1], 1, [m4_shift(,m4_shift(m4_shift($@)))],
566 #         [_m4_shiftn(m4_decr([$1]), m4_shift(m4_shift($@)))])
567 # but with the final `m4_shift(m4_shift($@)))' shared between the two
568 # paths.  The first leg uses a no-op m4_shift(,$@) to balance out the ().
569 m4_define([m4_shiftn],
570 [m4_assert(0 < $1 && $1 < $#)_$0($@)])
572 m4_define([_m4_shiftn],
573 [m4_if([$1], 1, [m4_shift(],
574        [$0(m4_decr([$1])]), m4_shift(m4_shift($@)))])
576 # m4_shift2(...)
577 # m4_shift3(...)
578 # -----------------
579 # Returns ... shifted twice, and three times.  Faster than m4_shiftn.
580 m4_define([m4_shift2], [m4_shift(m4_shift($@))])
581 m4_define([m4_shift3], [m4_shift(m4_shift(m4_shift($@)))])
583 # _m4_shift2(...)
584 # _m4_shift3(...)
585 # ---------------
586 # Like m4_shift2 or m4_shift3, except include a leading comma unless shifting
587 # consumes all arguments.  Why?  Because in recursion, it is nice to
588 # distinguish between 1 element left and 0 elements left, based on how many
589 # arguments this shift expands to.
590 m4_define([_m4_shift2],
591 [m4_if([$#], [2], [],
592        [, m4_shift(m4_shift($@))])])
593 m4_define([_m4_shift3],
594 [m4_if([$#], [3], [],
595        [, m4_shift(m4_shift(m4_shift($@)))])])
598 # m4_undefine(NAME)
599 # -----------------
600 # Like the original, except don't tolerate undefining something which is
601 # undefined, and only support one argument.
603 # This macro is called frequently, so minimize the amount of additional
604 # expansions by skipping m4_ifndef.
605 m4_define([m4_undefine],
606 [m4_ifdef([$1], [],
607           [m4_fatal([$0: undefined macro: $1])])]dnl
608 [m4_builtin([undefine], [$1])])
610 # _m4_wrap(PRE, POST)
611 # -------------------
612 # Helper macro for m4_wrap and m4_wrap_lifo.  Allows nested calls to
613 # m4_wrap within wrapped text.
614 # Skip m4_defn and m4_popdef for speed.
615 m4_define([_m4_wrap],
616 [m4_ifdef([$0_text],
617           [m4_define([$0_text], [$1]m4_builtin([defn], [$0_text])[$2])],
618           [m4_builtin([m4wrap], [m4_unquote(m4_builtin([defn],
619   [$0_text])m4_builtin([popdef], [$0_text]))])m4_define([$0_text], [$1$2])])])
621 # m4_wrap(TEXT)
622 # -------------
623 # Append TEXT to the list of hooks to be executed at the end of input.
624 # Whereas the order of the original may be LIFO in the underlying m4,
625 # this version is always FIFO.
626 m4_define([m4_wrap],
627 [_m4_wrap([], [$1[]])])
629 # m4_wrap_lifo(TEXT)
630 # ------------------
631 # Prepend TEXT to the list of hooks to be executed at the end of input.
632 # Whereas the order of m4_wrap may be FIFO in the underlying m4, this
633 # version is always LIFO.
634 m4_define([m4_wrap_lifo],
635 [_m4_wrap([$1[]])])
637 ## ------------------------- ##
638 ## 7. Quoting manipulation.  ##
639 ## ------------------------- ##
642 # m4_apply(MACRO, LIST)
643 # ---------------------
644 # Invoke MACRO, with arguments provided from the quoted list of
645 # comma-separated quoted arguments.  If LIST is empty, invoke MACRO
646 # without arguments.
647 m4_define([m4_apply],
648 [m4_if([$2], [], [$1], [$1($2)])[]])
651 # m4_count(ARGS)
652 # --------------
653 # Return a count of how many ARGS are present.
654 m4_define([m4_count], [$#])
657 # m4_do(STRING, ...)
658 # ------------------
659 # This macro invokes all its arguments (in sequence, of course).  It is
660 # useful for making your macros more structured and readable by dropping
661 # unnecessary dnl's and have the macros indented properly.
662 m4_define([m4_do],
663 [m4_if([$#], 0, [],
664        [$#], 1, [$1],
665        [$1[]m4_do(m4_shift($@))])])
668 # m4_dquote(ARGS)
669 # ---------------
670 # Return ARGS as a quoted list of quoted arguments.
671 m4_define([m4_dquote],  [[$@]])
674 # m4_dquote_elt(ARGS)
675 # -------------------
676 # Return ARGS as an unquoted list of double-quoted arguments.
677 m4_define([m4_dquote_elt],
678 [m4_if([$#], [0], [],
679        [$#], [1], [[[$1]]],
680        [[[$1]],$0(m4_shift($@))])])
683 # m4_echo(ARGS)
684 # -------------
685 # Return the ARGS, with the same level of quoting.  Whitespace after
686 # unquoted commas are consumed.
687 m4_define([m4_echo], [$@])
690 # m4_expand(ARG)
691 # --------------
692 # Return the expansion of ARG as a single string.  Unlike m4_quote($1), this
693 # correctly preserves whitespace following single-quoted commas that appeared
694 # within ARG.
696 #   m4_define([active], [ACT, IVE])
697 #   m4_define([active2], [[ACT, IVE]])
698 #   m4_quote(active, active2)
699 #   => ACT,IVE,ACT, IVE
700 #   m4_expand([active, active2])
701 #   => ACT, IVE, ACT, IVE
703 # Unfortunately, due to limitations in m4, ARG must contain balanced quotes
704 # (use quadrigraphs) and balanced parentheses (use creative shell comments
705 # when writing shell case statements).
707 # Exploit that extra () will group unquoted commas and the following
708 # whitespace, then convert () to [].  m4_bpatsubst can't handle newlines
709 # inside $1, and m4_substr strips quoting.  So we (ab)use m4_changequote.
710 m4_define([m4_expand], [_$0(($1))])
711 m4_define([_m4_expand],
712 [m4_changequote([(], [)])$1m4_changequote`'m4_changequote(`[', `]')])
715 # m4_ignore(ARGS)
716 # ---------------
717 # Expands to nothing.  Useful for conditionally ignoring an arbitrary
718 # number of arguments (see _m4_list_cmp for an example).
719 m4_define([m4_ignore])
722 # m4_make_list(ARGS)
723 # ------------------
724 # Similar to m4_dquote, this creates a quoted list of quoted ARGS.  This
725 # version is less efficient than m4_dquote, but separates each argument
726 # with a comma and newline, rather than just comma, for readability.
727 # When developing an m4sugar algorithm, you could temporarily use
728 #   m4_pushdef([m4_dquote],m4_defn([m4_make_list]))
729 # around your code to make debugging easier.
730 m4_define([m4_make_list], [m4_join([,
731 ], m4_dquote_elt($@))])
734 # m4_noquote(STRING)
735 # ------------------
736 # Return the result of ignoring all quotes in STRING and invoking the
737 # macros it contains.  Amongst other things, this is useful for enabling
738 # macro invocations inside strings with [] blocks (for instance regexps
739 # and help-strings).  On the other hand, since all quotes are disabled,
740 # any macro expanded during this time that relies on nested [] quoting
741 # will likely crash and burn.  This macro is seldom useful; consider
742 # m4_unquote instead.
743 m4_define([m4_noquote],
744 [m4_changequote(-=<{,}>=-)$1-=<{}>=-m4_changequote([,])])
747 # m4_quote(ARGS)
748 # --------------
749 # Return ARGS as a single argument.  Any whitespace after unquoted commas
750 # is stripped.  There is always output, even when there were no arguments.
752 # It is important to realize the difference between `m4_quote(exp)' and
753 # `[exp]': in the first case you obtain the quoted *result* of the
754 # expansion of EXP, while in the latter you just obtain the string
755 # `exp'.
756 m4_define([m4_quote],  [[$*]])
759 # _m4_quote(ARGS)
760 # ---------------
761 # Like m4_quote, except that when there are no arguments, there is no
762 # output.  For conditional scenarios (such as passing _m4_quote as the
763 # macro name in m4_map), this feature can be used to distinguish between
764 # one argument of the empty string vs. no arguments.  However, in the
765 # normal case with arguments present, this is less efficient than m4_quote.
766 m4_define([_m4_quote],
767 [m4_if([$#], [0], [], [[$*]])])
770 # m4_unquote(ARGS)
771 # ----------------
772 # Remove one layer of quotes from each ARG, performing one level of
773 # expansion.  For one argument, m4_unquote([arg]) is more efficient than
774 # m4_do([arg]), but for multiple arguments, the difference is that
775 # m4_unquote separates arguments with commas while m4_do concatenates.
776 m4_define([m4_unquote], [$*])
779 ## -------------------------- ##
780 ## 8. Implementing m4 loops.  ##
781 ## -------------------------- ##
784 # m4_for(VARIABLE, FIRST, LAST, [STEP = +/-1], EXPRESSION)
785 # --------------------------------------------------------
786 # Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO with
787 # increments of STEP.
788 # Both limits are included, and bounds are checked for consistency.
789 # The algorithm is robust to indirect VARIABLE names, and uses m4_builtin
790 # to avoid some of the m4_defn overhead.
791 m4_define([m4_for],
792 [m4_pushdef([$1], m4_eval([$2]))dnl
793 m4_cond([m4_eval(([$3]) > m4_builtin([defn], [$1]))], 1,
794 [m4_pushdef([_m4_step], m4_eval(m4_default([$4], 1)))dnl
795 m4_assert(_m4_step > 0)dnl
796 _m4_for([$1], m4_eval((([$3]) - m4_builtin([defn], [$1]))
797                       / _m4_step * _m4_step + m4_builtin([defn], [$1])),
798         _m4_step, [$5])],
799         [m4_eval(([$3]) < m4_builtin([defn], [$1]))], 1,
800 [m4_pushdef([_m4_step], m4_eval(m4_default([$4], -1)))dnl
801 m4_assert(_m4_step < 0)dnl
802 _m4_for([$1], m4_eval((m4_builtin([defn], [$1]) - ([$3]))
803                       / -(_m4_step) * _m4_step + m4_builtin([defn], [$1])),
804         _m4_step, [$5])],
805         [m4_pushdef([_m4_step])dnl
806 $5])[]dnl
807 m4_popdef([_m4_step])dnl
808 m4_popdef([$1])])
811 # _m4_for(VARIABLE, LAST, STEP, EXPRESSION)
812 # -----------------------------------------
813 # Core of the loop, no consistency checks, all arguments are plain numbers.
814 m4_define([_m4_for],
815 [$4[]dnl
816 m4_if(m4_defn([$1]), [$2], [],
817       [m4_define([$1], m4_eval(m4_defn([$1])+[$3]))$0($@)])])
820 # Implementing `foreach' loops in m4 is much more tricky than it may
821 # seem.  For example, the old M4 1.4.4 manual had an incorrect example,
822 # which looked like this (when translated to m4sugar):
824 # | # foreach(VAR, (LIST), STMT)
825 # | m4_define([foreach],
826 # |   [m4_pushdef([$1])_foreach([$1], [$2], [$3])m4_popdef([$1])])
827 # | m4_define([_arg1], [$1])
828 # | m4_define([_foreach],
829 # |   [m4_if([$2], [()], ,
830 # |     [m4_define([$1], _arg1$2)$3[]_foreach([$1], (m4_shift$2), [$3])])])
832 # But then if you run
834 # | m4_define(a, 1)
835 # | m4_define(b, 2)
836 # | m4_define(c, 3)
837 # | foreach([f], [([a], [(b], [c)])], [echo f
838 # | ])
840 # it gives
842 #  => echo 1
843 #  => echo (2,3)
845 # which is not what is expected.
847 # Of course the problem is that many quotes are missing.  So you add
848 # plenty of quotes at random places, until you reach the expected
849 # result.  Alternatively, if you are a quoting wizard, you directly
850 # reach the following implementation (but if you really did, then
851 # apply to the maintenance of m4sugar!).
853 # | # foreach(VAR, (LIST), STMT)
854 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
855 # | m4_define([_arg1], [[$1]])
856 # | m4_define([_foreach],
857 # |  [m4_if($2, [()], ,
858 # |    [m4_define([$1], [_arg1$2])$3[]_foreach([$1], [(m4_shift$2)], [$3])])])
860 # which this time answers
862 #  => echo a
863 #  => echo (b
864 #  => echo c)
866 # Bingo!
868 # Well, not quite.
870 # With a better look, you realize that the parens are more a pain than
871 # a help: since anyway you need to quote properly the list, you end up
872 # with always using an outermost pair of parens and an outermost pair
873 # of quotes.  Rejecting the parens both eases the implementation, and
874 # simplifies the use:
876 # | # foreach(VAR, (LIST), STMT)
877 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
878 # | m4_define([_arg1], [$1])
879 # | m4_define([_foreach],
880 # |  [m4_if($2, [], ,
881 # |    [m4_define([$1], [_arg1($2)])$3[]_foreach([$1], [m4_shift($2)], [$3])])])
884 # Now, just replace the `$2' with `m4_quote($2)' in the outer `m4_if'
885 # to improve robustness, and you come up with a nice implementation
886 # that doesn't require extra parentheses in the user's LIST.
888 # But wait -  now the algorithm is quadratic, because every recursion of
889 # the algorithm keeps the entire LIST and merely adds another m4_shift to
890 # the quoted text.  If the user has a lot of elements in LIST, you can
891 # bring the system to its knees with the memory m4 then requires, or trip
892 # the m4 --nesting-limit recursion factor.  The only way to avoid
893 # quadratic growth is ensure m4_shift is expanded prior to the recursion.
894 # Hence the design below.
896 # The M4 manual now includes a chapter devoted to this issue, with
897 # the lessons learned from m4sugar.
900 # m4_foreach(VARIABLE, LIST, EXPRESSION)
901 # --------------------------------------
903 # Expand EXPRESSION assigning each value of the LIST to VARIABLE.
904 # LIST should have the form `item_1, item_2, ..., item_n', i.e. the
905 # whole list must *quoted*.  Quote members too if you don't want them
906 # to be expanded.
908 # This macro is robust to active symbols:
909 #      | m4_define(active, [ACT, IVE])
910 #      | m4_foreach(Var, [active, active], [-Var-])
911 #     => -ACT--IVE--ACT--IVE-
913 #      | m4_foreach(Var, [[active], [active]], [-Var-])
914 #     => -ACT, IVE--ACT, IVE-
916 #      | m4_foreach(Var, [[[active]], [[active]]], [-Var-])
917 #     => -active--active-
919 # This macro is called frequently, so avoid extra expansions such as
920 # m4_ifval and dnl.  Also, since $2 might be quite large, try to use it
921 # as little as possible in _m4_foreach; each extra use requires that much
922 # more memory for expansion.  So, rather than directly compare $2 against
923 # [] and use m4_car/m4_cdr for recursion, we instead unbox the list (which
924 # requires swapping the argument order in the helper) and use _m4_shift3
925 # to detect when recursion is complete.
926 m4_define([m4_foreach],
927 [m4_pushdef([$1])_$0([$1], [$3]m4_if([$2], [], [], [, $2]))m4_popdef([$1])])
929 m4_define([_m4_foreach],
930 [m4_if([$#], [2], [],
931        [m4_define([$1], [$3])$2[]$0([$1], [$2]_m4_shift3($@))])])
934 # m4_foreach_w(VARIABLE, LIST, EXPRESSION)
935 # ----------------------------------------
937 # Like m4_foreach, but the list is whitespace separated.
939 # This macro is robust to active symbols:
940 #    m4_foreach_w([Var], [ active
941 #    b  act\
942 #    ive  ], [-Var-])end
943 #    => -active--b--active-end
945 m4_define([m4_foreach_w],
946 [m4_foreach([$1], m4_split(m4_normalize([$2]), [ ]), [$3])])
949 # m4_map(MACRO, LIST)
950 # -------------------
951 # Invoke MACRO($1), MACRO($2) etc. where $1, $2... are the elements
952 # of LIST.  $1, $2... must in turn be lists, appropriate for m4_apply.
954 # Since LIST may be quite large, we want to minimize how often it appears
955 # in the expansion.  Rather than use m4_car/m4_cdr iteration, we unbox the
956 # list, and use _m4_shift2 to detect the end of recursion.
957 m4_define([m4_map],
958 [m4_if([$2], [], [],
959        [_$0([$1], $2)])])
960 m4_define([_m4_map],
961 [m4_if([$#], [1], [],
962        [m4_apply([$1], [$2])$0([$1]_m4_shift2($@))])])
965 # m4_map_sep(MACRO, SEPARATOR, LIST)
966 # ----------------------------------
967 # Invoke MACRO($1), SEPARATOR, MACRO($2), ..., MACRO($N) where $1, $2... $N
968 # are the elements of LIST, and are in turn lists appropriate for m4_apply.
969 # SEPARATOR is not further expanded.
970 m4_define([m4_map_sep],
971 [m4_if([$3], [], [],
972        [m4_apply([$1], m4_car($3))m4_map([[$2]$1]_m4_cdr($3))])])
975 ## --------------------------- ##
976 ## 9. More diversion support.  ##
977 ## --------------------------- ##
980 # _m4_divert(DIVERSION-NAME or NUMBER)
981 # ------------------------------------
982 # If DIVERSION-NAME is the name of a diversion, return its number,
983 # otherwise if it is a NUMBER return it.
984 m4_define([_m4_divert],
985 [m4_ifdef([_m4_divert($1)],
986           [m4_indir([_m4_divert($1)])],
987           [$1])])
989 # KILL is only used to suppress output.
990 m4_define([_m4_divert(KILL)],           -1)
992 # The empty diversion name is a synonym for 0.
993 m4_define([_m4_divert()],                0)
996 # _m4_divert_n_stack
997 # ------------------
998 # Print m4_divert_stack with newline prepended, if it's nonempty.
999 m4_define([_m4_divert_n_stack],
1000 [m4_ifdef([m4_divert_stack], [
1001 m4_defn([m4_divert_stack])])])
1004 # m4_divert(DIVERSION-NAME)
1005 # -------------------------
1006 # Change the diversion stream to DIVERSION-NAME.
1007 m4_define([m4_divert],
1008 [m4_define([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
1009 m4_builtin([divert], _m4_divert([$1]))dnl
1013 # m4_divert_push(DIVERSION-NAME)
1014 # ------------------------------
1015 # Change the diversion stream to DIVERSION-NAME, while stacking old values.
1016 m4_define([m4_divert_push],
1017 [m4_pushdef([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
1018 m4_pushdef([_m4_divert_diversion], [$1])dnl
1019 m4_builtin([divert], _m4_divert([$1]))dnl
1023 # m4_divert_pop([DIVERSION-NAME])
1024 # -------------------------------
1025 # Change the diversion stream to its previous value, unstacking it.
1026 # If specified, verify we left DIVERSION-NAME.
1027 # When we pop the last value from the stack, we divert to -1.
1028 m4_define([m4_divert_pop],
1029 [m4_ifndef([_m4_divert_diversion],
1030            [m4_fatal([too many m4_divert_pop])])dnl
1031 m4_if([$1], [], [],
1032       [$1], m4_defn([_m4_divert_diversion]), [],
1033       [m4_fatal([$0($1): diversion mismatch: ]_m4_divert_n_stack)])dnl
1034 m4_popdef([m4_divert_stack])dnl
1035 m4_popdef([_m4_divert_diversion])dnl
1036 m4_builtin([divert],
1037            m4_ifdef([_m4_divert_diversion],
1038                     [_m4_divert(m4_defn([_m4_divert_diversion]))],
1039                     -1))dnl
1043 # m4_divert_text(DIVERSION-NAME, CONTENT)
1044 # ---------------------------------------
1045 # Output CONTENT into DIVERSION-NAME (which may be a number actually).
1046 # An end of line is appended for free to CONTENT.
1047 m4_define([m4_divert_text],
1048 [m4_divert_push([$1])dnl
1050 m4_divert_pop([$1])dnl
1054 # m4_divert_once(DIVERSION-NAME, CONTENT)
1055 # ---------------------------------------
1056 # Output CONTENT into DIVERSION-NAME once, if not already there.
1057 # An end of line is appended for free to CONTENT.
1058 m4_define([m4_divert_once],
1059 [m4_expand_once([m4_divert_text([$1], [$2])])])
1062 # m4_undivert(DIVERSION-NAME)
1063 # ---------------------------
1064 # Undivert DIVERSION-NAME.  Unlike the M4 version, this only takes a single
1065 # diversion identifier, and should not be used to undivert files.
1066 m4_define([m4_undivert],
1067 [m4_builtin([undivert], _m4_divert([$1]))])
1070 ## --------------------------------------------- ##
1071 ## 10. Defining macros with bells and whistles.  ##
1072 ## --------------------------------------------- ##
1074 # `m4_defun' is basically `m4_define' but it equips the macro with the
1075 # needed machinery for `m4_require'.  A macro must be m4_defun'd if
1076 # either it is m4_require'd, or it m4_require's.
1078 # Two things deserve attention and are detailed below:
1079 #  1. Implementation of m4_require
1080 #  2. Keeping track of the expansion stack
1082 # 1. Implementation of m4_require
1083 # ===============================
1085 # Of course m4_defun AC_PROVIDE's the macro, so that a macro which has
1086 # been expanded is not expanded again when m4_require'd, but the
1087 # difficult part is the proper expansion of macros when they are
1088 # m4_require'd.
1090 # The implementation is based on two ideas, (i) using diversions to
1091 # prepare the expansion of the macro and its dependencies (by Franc,ois
1092 # Pinard), and (ii) expand the most recently m4_require'd macros _after_
1093 # the previous macros (by Axel Thimm).
1096 # The first idea: why use diversions?
1097 # -----------------------------------
1099 # When a macro requires another, the other macro is expanded in new
1100 # diversion, GROW.  When the outer macro is fully expanded, we first
1101 # undivert the most nested diversions (GROW - 1...), and finally
1102 # undivert GROW.  To understand why we need several diversions,
1103 # consider the following example:
1105 # | m4_defun([TEST1], [Test...REQUIRE([TEST2])1])
1106 # | m4_defun([TEST2], [Test...REQUIRE([TEST3])2])
1107 # | m4_defun([TEST3], [Test...3])
1109 # Because m4_require is not required to be first in the outer macros, we
1110 # must keep the expansions of the various levels of m4_require separated.
1111 # Right before executing the epilogue of TEST1, we have:
1113 #          GROW - 2: Test...3
1114 #          GROW - 1: Test...2
1115 #          GROW:     Test...1
1116 #          BODY:
1118 # Finally the epilogue of TEST1 undiverts GROW - 2, GROW - 1, and
1119 # GROW into the regular flow, BODY.
1121 #          GROW - 2:
1122 #          GROW - 1:
1123 #          GROW:
1124 #          BODY:        Test...3; Test...2; Test...1
1126 # (The semicolons are here for clarification, but of course are not
1127 # emitted.)  This is what Autoconf 2.0 (I think) to 2.13 (I'm sure)
1128 # implement.
1131 # The second idea: first required first out
1132 # -----------------------------------------
1134 # The natural implementation of the idea above is buggy and produces
1135 # very surprising results in some situations.  Let's consider the
1136 # following example to explain the bug:
1138 # | m4_defun([TEST1],  [REQUIRE([TEST2a])REQUIRE([TEST2b])])
1139 # | m4_defun([TEST2a], [])
1140 # | m4_defun([TEST2b], [REQUIRE([TEST3])])
1141 # | m4_defun([TEST3],  [REQUIRE([TEST2a])])
1142 # |
1143 # | AC_INIT
1144 # | TEST1
1146 # The dependencies between the macros are:
1148 #                3 --- 2b
1149 #               /        \              is m4_require'd by
1150 #              /          \       left -------------------- right
1151 #           2a ------------ 1
1153 # If you strictly apply the rules given in the previous section you get:
1155 #          GROW - 2: TEST3
1156 #          GROW - 1: TEST2a; TEST2b
1157 #          GROW:     TEST1
1158 #          BODY:
1160 # (TEST2a, although required by TEST3 is not expanded in GROW - 3
1161 # because is has already been expanded before in GROW - 1, so it has
1162 # been AC_PROVIDE'd, so it is not expanded again) so when you undivert
1163 # the stack of diversions, you get:
1165 #          GROW - 2:
1166 #          GROW - 1:
1167 #          GROW:
1168 #          BODY:        TEST3; TEST2a; TEST2b; TEST1
1170 # i.e., TEST2a is expanded after TEST3 although the latter required the
1171 # former.
1173 # Starting from 2.50, we use an implementation provided by Axel Thimm.
1174 # The idea is simple: the order in which macros are emitted must be the
1175 # same as the one in which macros are expanded.  (The bug above can
1176 # indeed be described as: a macro has been AC_PROVIDE'd before its
1177 # dependent, but it is emitted after: the lack of correlation between
1178 # emission and expansion order is guilty).
1180 # How to do that?  You keep the stack of diversions to elaborate the
1181 # macros, but each time a macro is fully expanded, emit it immediately.
1183 # In the example above, when TEST2a is expanded, but it's epilogue is
1184 # not run yet, you have:
1186 #          GROW - 2:
1187 #          GROW - 1: TEST2a
1188 #          GROW:     Elaboration of TEST1
1189 #          BODY:
1191 # The epilogue of TEST2a emits it immediately:
1193 #          GROW - 2:
1194 #          GROW - 1:
1195 #          GROW:     Elaboration of TEST1
1196 #          BODY:     TEST2a
1198 # TEST2b then requires TEST3, so right before the epilogue of TEST3, you
1199 # have:
1201 #          GROW - 2: TEST3
1202 #          GROW - 1: Elaboration of TEST2b
1203 #          GROW:     Elaboration of TEST1
1204 #          BODY:      TEST2a
1206 # The epilogue of TEST3 emits it:
1208 #          GROW - 2:
1209 #          GROW - 1: Elaboration of TEST2b
1210 #          GROW:     Elaboration of TEST1
1211 #          BODY:     TEST2a; TEST3
1213 # TEST2b is now completely expanded, and emitted:
1215 #          GROW - 2:
1216 #          GROW - 1:
1217 #          GROW:     Elaboration of TEST1
1218 #          BODY:     TEST2a; TEST3; TEST2b
1220 # and finally, TEST1 is finished and emitted:
1222 #          GROW - 2:
1223 #          GROW - 1:
1224 #          GROW:
1225 #          BODY:     TEST2a; TEST3; TEST2b: TEST1
1227 # The idea is simple, but the implementation is a bit evolved.  If you
1228 # are like me, you will want to see the actual functioning of this
1229 # implementation to be convinced.  The next section gives the full
1230 # details.
1233 # The Axel Thimm implementation at work
1234 # -------------------------------------
1236 # We consider the macros above, and this configure.ac:
1238 #           AC_INIT
1239 #           TEST1
1241 # You should keep the definitions of _m4_defun_pro, _m4_defun_epi, and
1242 # m4_require at hand to follow the steps.
1244 # This implements tries not to assume that the current diversion is
1245 # BODY, so as soon as a macro (m4_defun'd) is expanded, we first
1246 # record the current diversion under the name _m4_divert_dump (denoted
1247 # DUMP below for short).  This introduces an important difference with
1248 # the previous versions of Autoconf: you cannot use m4_require if you
1249 # are not inside an m4_defun'd macro, and especially, you cannot
1250 # m4_require directly from the top level.
1252 # We have not tried to simulate the old behavior (better yet, we
1253 # diagnose it), because it is too dangerous: a macro m4_require'd from
1254 # the top level is expanded before the body of `configure', i.e., before
1255 # any other test was run.  I let you imagine the result of requiring
1256 # AC_STDC_HEADERS for instance, before AC_PROG_CC was actually run....
1258 # After AC_INIT was run, the current diversion is BODY.
1259 # * AC_INIT was run
1260 #   DUMP:                undefined
1261 #   diversion stack:     BODY |-
1263 # * TEST1 is expanded
1264 # The prologue of TEST1 sets _m4_divert_dump, which is the diversion
1265 # where the current elaboration will be dumped, to the current
1266 # diversion.  It also m4_divert_push to GROW, where the full
1267 # expansion of TEST1 and its dependencies will be elaborated.
1268 #   DUMP:        BODY
1269 #   BODY:        empty
1270 #   diversions:  GROW, BODY |-
1272 # * TEST1 requires TEST2a
1273 # _m4_require_call m4_divert_pushes another temporary diversion,
1274 # GROW - 1, and expands TEST2a in there.
1275 #   DUMP:        BODY
1276 #   BODY:        empty
1277 #   GROW - 1:    TEST2a
1278 #   diversions:  GROW - 1, GROW, BODY |-
1279 # Than the content of the temporary diversion is moved to DUMP and the
1280 # temporary diversion is popped.
1281 #   DUMP:        BODY
1282 #   BODY:        TEST2a
1283 #   diversions:  GROW, BODY |-
1285 # * TEST1 requires TEST2b
1286 # Again, _m4_require_call pushes GROW - 1 and heads to expand TEST2b.
1287 #   DUMP:        BODY
1288 #   BODY:        TEST2a
1289 #   diversions:  GROW - 1, GROW, BODY |-
1291 # * TEST2b requires TEST3
1292 # _m4_require_call pushes GROW - 2 and expands TEST3 here.
1293 # (TEST3 requires TEST2a, but TEST2a has already been m4_provide'd, so
1294 # nothing happens.)
1295 #   DUMP:        BODY
1296 #   BODY:        TEST2a
1297 #   GROW - 2:    TEST3
1298 #   diversions:  GROW - 2, GROW - 1, GROW, BODY |-
1299 # Than the diversion is appended to DUMP, and popped.
1300 #   DUMP:        BODY
1301 #   BODY:        TEST2a; TEST3
1302 #   diversions:  GROW - 1, GROW, BODY |-
1304 # * TEST1 requires TEST2b (contd.)
1305 # The content of TEST2b is expanded...
1306 #   DUMP:        BODY
1307 #   BODY:        TEST2a; TEST3
1308 #   GROW - 1:    TEST2b,
1309 #   diversions:  GROW - 1, GROW, BODY |-
1310 # ... and moved to DUMP.
1311 #   DUMP:        BODY
1312 #   BODY:        TEST2a; TEST3; TEST2b
1313 #   diversions:  GROW, BODY |-
1315 # * TEST1 is expanded: epilogue
1316 # TEST1's own content is in GROW...
1317 #   DUMP:        BODY
1318 #   BODY:        TEST2a; TEST3; TEST2b
1319 #   GROW:        TEST1
1320 #   diversions:  BODY |-
1321 # ... and it's epilogue moves it to DUMP and then undefines DUMP.
1322 #   DUMP:       undefined
1323 #   BODY:       TEST2a; TEST3; TEST2b; TEST1
1324 #   diversions: BODY |-
1327 # 2. Keeping track of the expansion stack
1328 # =======================================
1330 # When M4 expansion goes wrong it is often extremely hard to find the
1331 # path amongst macros that drove to the failure.  What is needed is
1332 # the stack of macro `calls'. One could imagine that GNU M4 would
1333 # maintain a stack of macro expansions, unfortunately it doesn't, so
1334 # we do it by hand.  This is of course extremely costly, but the help
1335 # this stack provides is worth it.  Nevertheless to limit the
1336 # performance penalty this is implemented only for m4_defun'd macros,
1337 # not for define'd macros.
1339 # The scheme is simplistic: each time we enter an m4_defun'd macros,
1340 # we prepend its name in m4_expansion_stack, and when we exit the
1341 # macro, we remove it (thanks to pushdef/popdef).
1343 # In addition, we want to detect circular m4_require dependencies.
1344 # Each time we expand a macro FOO we define _m4_expanding(FOO); and
1345 # m4_require(BAR) simply checks whether _m4_expanding(BAR) is defined.
1348 # m4_expansion_stack_push(TEXT)
1349 # -----------------------------
1350 # Use m4_builtin to avoid m4_defn overhead.
1351 m4_define([m4_expansion_stack_push],
1352 [m4_pushdef([m4_expansion_stack],
1353             [$1]m4_ifdef([m4_expansion_stack], [
1354 m4_builtin([defn], [m4_expansion_stack])]))])
1357 # m4_expansion_stack_pop
1358 # ----------------------
1359 m4_define([m4_expansion_stack_pop],
1360 [m4_popdef([m4_expansion_stack])])
1363 # m4_expansion_stack_dump
1364 # -----------------------
1365 # Dump the expansion stack.
1366 m4_define([m4_expansion_stack_dump],
1367 [m4_ifdef([m4_expansion_stack],
1368           [m4_errprintn(m4_defn([m4_expansion_stack]))])dnl
1369 m4_errprintn(m4_location[: the top level])])
1372 # _m4_divert(GROW)
1373 # ----------------
1374 # This diversion is used by the m4_defun/m4_require machinery.  It is
1375 # important to keep room before GROW because for each nested
1376 # AC_REQUIRE we use an additional diversion (i.e., two m4_require's
1377 # will use GROW - 2.  More than 3 levels has never seemed to be
1378 # needed.)
1380 # ...
1381 # - GROW - 2
1382 #   m4_require'd code, 2 level deep
1383 # - GROW - 1
1384 #   m4_require'd code, 1 level deep
1385 # - GROW
1386 #   m4_defun'd macros are elaborated here.
1388 m4_define([_m4_divert(GROW)],       10000)
1391 # _m4_defun_pro(MACRO-NAME)
1392 # -------------------------
1393 # The prologue for Autoconf macros.
1395 # This is called frequently, so minimize the number of macro invocations
1396 # by avoiding dnl and m4_defn overhead.
1397 m4_define([_m4_defun_pro],
1398 m4_do([[m4_ifdef([m4_expansion_stack], [], [_m4_defun_pro_outer[]])]],
1399       [[m4_expansion_stack_push(m4_builtin([defn],
1400           [m4_location($1)])[: $1 is expanded from...])]],
1401       [[m4_pushdef([_m4_expanding($1)])]]))
1403 m4_define([_m4_defun_pro_outer],
1404 [m4_copy([_m4_divert_diversion], [_m4_divert_dump])m4_divert_push([GROW])])
1406 # _m4_defun_epi(MACRO-NAME)
1407 # -------------------------
1408 # The Epilogue for Autoconf macros.  MACRO-NAME only helps tracing
1409 # the PRO/EPI pairs.
1411 # This is called frequently, so minimize the number of macro invocations
1412 # by avoiding dnl and m4_popdef overhead.
1413 m4_define([_m4_defun_epi],
1414 m4_do([[m4_builtin([popdef], [_m4_expanding($1)])]],
1415       [[m4_expansion_stack_pop()]],
1416       [[m4_ifdef([m4_expansion_stack], [], [_m4_defun_epi_outer[]])]],
1417       [[m4_provide([$1])]]))
1419 m4_define([_m4_defun_epi_outer],
1420 m4_do([[m4_builtin([undefine], [_m4_divert_dump])]],
1421       [[m4_divert_pop([GROW])]],
1422       [[m4_undivert([GROW])]]))
1425 # m4_defun(NAME, EXPANSION)
1426 # -------------------------
1427 # Define a macro which automatically provides itself.  Add machinery
1428 # so the macro automatically switches expansion to the diversion
1429 # stack if it is not already using it.  In this case, once finished,
1430 # it will bring back all the code accumulated in the diversion stack.
1431 # This, combined with m4_require, achieves the topological ordering of
1432 # macros.  We don't use this macro to define some frequently called
1433 # macros that are not involved in ordering constraints, to save m4
1434 # processing.
1435 m4_define([m4_defun],
1436 [m4_define([m4_location($1)], m4_location)dnl
1437 m4_define([$1],
1438           [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])
1441 # m4_defun_once(NAME, EXPANSION)
1442 # ------------------------------
1443 # As m4_defun, but issues the EXPANSION only once, and warns if used
1444 # several times.
1445 m4_define([m4_defun_once],
1446 [m4_define([m4_location($1)], m4_location)dnl
1447 m4_define([$1],
1448           [m4_provide_if([$1],
1449                          [m4_warn([syntax], [$1 invoked multiple times])],
1450                          [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])])
1453 # m4_pattern_forbid(ERE, [WHY])
1454 # -----------------------------
1455 # Declare that no token matching the forbidden extended regular
1456 # expression ERE should be seen in the output unless...
1457 m4_define([m4_pattern_forbid], [])
1460 # m4_pattern_allow(ERE)
1461 # ---------------------
1462 # ... that token also matches the allowed extended regular expression ERE.
1463 # Both used via traces.
1464 m4_define([m4_pattern_allow], [])
1467 ## --------------------------------- ##
1468 ## 11. Dependencies between macros.  ##
1469 ## --------------------------------- ##
1472 # m4_before(THIS-MACRO-NAME, CALLED-MACRO-NAME)
1473 # ---------------------------------------------
1474 # Issue a warning if CALLED-MACRO-NAME was called before THIS-MACRO-NAME.
1475 m4_define([m4_before],
1476 [m4_provide_if([$2],
1477                [m4_warn([syntax], [$2 was called before $1])])])
1480 # m4_require(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
1481 # -----------------------------------------------------------
1482 # If NAME-TO-CHECK has never been expanded (actually, if it is not
1483 # m4_provide'd), expand BODY-TO-EXPAND *before* the current macro
1484 # expansion.  Once expanded, emit it in _m4_divert_dump.  Keep track
1485 # of the m4_require chain in m4_expansion_stack.
1487 # The normal cases are:
1489 # - NAME-TO-CHECK == BODY-TO-EXPAND
1490 #   Which you can use for regular macros with or without arguments, e.g.,
1491 #     m4_require([AC_PROG_CC], [AC_PROG_CC])
1492 #     m4_require([AC_CHECK_HEADERS(limits.h)], [AC_CHECK_HEADERS(limits.h)])
1493 #   which is just the same as
1494 #     m4_require([AC_PROG_CC])
1495 #     m4_require([AC_CHECK_HEADERS(limits.h)])
1497 # - BODY-TO-EXPAND == m4_indir([NAME-TO-CHECK])
1498 #   In the case of macros with irregular names.  For instance:
1499 #     m4_require([AC_LANG_COMPILER(C)], [indir([AC_LANG_COMPILER(C)])])
1500 #   which means `if the macro named `AC_LANG_COMPILER(C)' (the parens are
1501 #   part of the name, it is not an argument) has not been run, then
1502 #   call it.'
1503 #   Had you used
1504 #     m4_require([AC_LANG_COMPILER(C)], [AC_LANG_COMPILER(C)])
1505 #   then m4_require would have tried to expand `AC_LANG_COMPILER(C)', i.e.,
1506 #   call the macro `AC_LANG_COMPILER' with `C' as argument.
1508 #   You could argue that `AC_LANG_COMPILER', when it receives an argument
1509 #   such as `C' should dispatch the call to `AC_LANG_COMPILER(C)'.  But this
1510 #   `extension' prevents `AC_LANG_COMPILER' from having actual arguments that
1511 #   it passes to `AC_LANG_COMPILER(C)'.
1513 # This is called frequently, so minimize the number of macro invocations
1514 # by avoiding dnl and other overhead on the common path.
1515 m4_define([m4_require],
1516 m4_do([[m4_ifdef([_m4_expanding($1)],
1517                  [m4_fatal([$0: circular dependency of $1])])]],
1518       [[m4_ifdef([_m4_divert_dump], [],
1519                  [m4_fatal([$0($1): cannot be used outside of an ]dnl
1520 m4_bmatch([$0], [^AC_], [[AC_DEFUN]], [[m4_defun]])['d macro])])]],
1521       [[m4_provide_if([$1],
1522                       [],
1523                       [_m4_require_call([$1], [$2])])]]))
1526 # _m4_require_call(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
1527 # -----------------------------------------------------------------
1528 # If m4_require decides to expand the body, it calls this macro.
1530 # This is called frequently, so minimize the number of macro invocations
1531 # by avoiding dnl and other overhead on the common path.
1532 m4_define([_m4_require_call],
1533 m4_do([[m4_define([_m4_divert_grow], m4_decr(_m4_divert_grow))]],
1534       [[m4_divert_push(_m4_divert_grow)]],
1535       [[m4_default([$2], [$1])
1536 m4_provide_if([$1],
1537               [],
1538               [m4_warn([syntax],
1539                        [$1 is m4_require'd but not m4_defun'd])])]],
1540       [[m4_divert(m4_builtin([defn], [_m4_divert_dump]))]],
1541       [[m4_undivert(_m4_divert_grow)]],
1542       [[m4_divert_pop(_m4_divert_grow)]],
1543       [[m4_define([_m4_divert_grow], m4_incr(_m4_divert_grow))]]))
1546 # _m4_divert_grow
1547 # ---------------
1548 # The counter for _m4_require_call.
1549 m4_define([_m4_divert_grow], _m4_divert([GROW]))
1552 # m4_expand_once(TEXT, [WITNESS = TEXT])
1553 # --------------------------------------
1554 # If TEXT has never been expanded, expand it *here*.  Use WITNESS as
1555 # as a memory that TEXT has already been expanded.
1556 m4_define([m4_expand_once],
1557 [m4_provide_if(m4_ifval([$2], [[$2]], [[$1]]),
1558                [],
1559                [m4_provide(m4_ifval([$2], [[$2]], [[$1]]))[]$1])])
1562 # m4_provide(MACRO-NAME)
1563 # ----------------------
1564 m4_define([m4_provide],
1565 [m4_define([m4_provide($1)])])
1568 # m4_provide_if(MACRO-NAME, IF-PROVIDED, IF-NOT-PROVIDED)
1569 # -------------------------------------------------------
1570 # If MACRO-NAME is provided do IF-PROVIDED, else IF-NOT-PROVIDED.
1571 # The purpose of this macro is to provide the user with a means to
1572 # check macros which are provided without letting her know how the
1573 # information is coded.
1574 m4_define([m4_provide_if],
1575 [m4_ifdef([m4_provide($1)],
1576           [$2], [$3])])
1579 ## --------------------- ##
1580 ## 12. Text processing.  ##
1581 ## --------------------- ##
1584 # m4_cr_letters
1585 # m4_cr_LETTERS
1586 # m4_cr_Letters
1587 # -------------
1588 m4_define([m4_cr_letters], [abcdefghijklmnopqrstuvwxyz])
1589 m4_define([m4_cr_LETTERS], [ABCDEFGHIJKLMNOPQRSTUVWXYZ])
1590 m4_define([m4_cr_Letters],
1591 m4_defn([m4_cr_letters])dnl
1592 m4_defn([m4_cr_LETTERS])dnl
1596 # m4_cr_digits
1597 # ------------
1598 m4_define([m4_cr_digits], [0123456789])
1601 # m4_cr_alnum
1602 # -----------
1603 m4_define([m4_cr_alnum],
1604 m4_defn([m4_cr_Letters])dnl
1605 m4_defn([m4_cr_digits])dnl
1609 # m4_cr_symbols1
1610 # m4_cr_symbols2
1611 # -------------------------------
1612 m4_define([m4_cr_symbols1],
1613 m4_defn([m4_cr_Letters])dnl
1616 m4_define([m4_cr_symbols2],
1617 m4_defn([m4_cr_symbols1])dnl
1618 m4_defn([m4_cr_digits])dnl
1621 # m4_cr_all
1622 # ---------
1623 # The character range representing everything, with `-' as the last
1624 # character, since it is special to m4_translit.  Use with care, because
1625 # it contains characters special to M4 (fortunately, both ASCII and EBCDIC
1626 # have [] in order, so m4_defn([m4_cr_all]) remains a valid string).  It
1627 # also contains characters special to terminals, so it should never be
1628 # displayed in an error message.  Also, attempts to map [ and ] to other
1629 # characters via m4_translit must deal with the fact that m4_translit does
1630 # not add quotes to the output.
1632 # It is mainly useful in generating inverted character range maps, for use
1633 # in places where m4_translit is faster than an equivalent m4_bpatsubst;
1634 # the regex `[^a-z]' is equivalent to:
1635 #  m4_translit(m4_dquote(m4_defn([m4_cr_all])), [a-z])
1636 m4_define([m4_cr_all],
1637 m4_translit(m4_dquote(m4_format(m4_dquote(m4_for(
1638   ,1,255,,[[%c]]))m4_for([i],1,255,,[,i]))), [-])-)
1641 # _m4_define_cr_not(CATEGORY)
1642 # ---------------------------
1643 # Define m4_cr_not_CATEGORY as the inverse of m4_cr_CATEGORY.
1644 m4_define([_m4_define_cr_not],
1645 [m4_define([m4_cr_not_$1],
1646            m4_translit(m4_dquote(m4_defn([m4_cr_all])),
1647                        m4_defn([m4_cr_$1])))])
1650 # m4_cr_not_letters
1651 # m4_cr_not_LETTERS
1652 # m4_cr_not_Letters
1653 # m4_cr_not_digits
1654 # m4_cr_not_alnum
1655 # m4_cr_not_symbols1
1656 # m4_cr_not_symbols2
1657 # ------------------
1658 # Inverse character sets
1659 _m4_define_cr_not([letters])
1660 _m4_define_cr_not([LETTERS])
1661 _m4_define_cr_not([Letters])
1662 _m4_define_cr_not([digits])
1663 _m4_define_cr_not([alnum])
1664 _m4_define_cr_not([symbols1])
1665 _m4_define_cr_not([symbols2])
1668 # m4_newline
1669 # ----------
1670 # Expands to a newline.  Exists for formatting reasons.
1671 m4_define([m4_newline], [
1675 # m4_re_escape(STRING)
1676 # --------------------
1677 # Escape RE active characters in STRING.
1678 m4_define([m4_re_escape],
1679 [m4_bpatsubst([$1],
1680               [[][*+.?\^$]], [\\\&])])
1683 # m4_re_string
1684 # ------------
1685 # Regexp for `[a-zA-Z_0-9]*'
1686 # m4_dquote provides literal [] for the character class.
1687 m4_define([m4_re_string],
1688 m4_dquote(m4_defn([m4_cr_symbols2]))dnl
1689 [*]dnl
1693 # m4_re_word
1694 # ----------
1695 # Regexp for `[a-zA-Z_][a-zA-Z_0-9]*'
1696 m4_define([m4_re_word],
1697 m4_dquote(m4_defn([m4_cr_symbols1]))dnl
1698 m4_defn([m4_re_string])dnl
1702 # m4_tolower(STRING)
1703 # m4_toupper(STRING)
1704 # ------------------
1705 # These macros convert STRING to lowercase or uppercase.
1707 # Rather than expand the m4_defn each time, we inline them up front.
1708 m4_define([m4_tolower],
1709 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_LETTERS]))[,
1710                    ]m4_dquote(m4_defn([m4_cr_letters]))[)])
1711 m4_define([m4_toupper],
1712 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_letters]))[,
1713                    ]m4_dquote(m4_defn([m4_cr_LETTERS]))[)])
1716 # m4_split(STRING, [REGEXP])
1717 # --------------------------
1719 # Split STRING into an m4 list of quoted elements.  The elements are
1720 # quoted with [ and ].  Beginning spaces and end spaces *are kept*.
1721 # Use m4_strip to remove them.
1723 # REGEXP specifies where to split.  Default is [\t ]+.
1725 # If STRING is empty, the result is an empty list.
1727 # Pay attention to the m4_changequotes.  When m4 reads the definition of
1728 # m4_split, it still has quotes set to [ and ].  Luckily, these are matched
1729 # in the macro body, so the definition is stored correctly.  Use the same
1730 # alternate quotes as m4_noquote; it must be unlikely to appear in $1.
1732 # Also, notice that $1 is quoted twice, since we want the result to
1733 # be quoted.  Then you should understand that the argument of
1734 # patsubst is -=<{STRING}>=- (i.e., with additional -=<{ and }>=-).
1736 # This macro is safe on active symbols, i.e.:
1737 #   m4_define(active, ACTIVE)
1738 #   m4_split([active active ])end
1739 #   => [active], [active], []end
1741 # Optimize on regex of ` ' (space), since m4_foreach_w already guarantees
1742 # that the list contains single space separators, and a common case is
1743 # splitting a single-element list.  This macro is called frequently,
1744 # so avoid unnecessary dnl inside the definition.
1745 m4_define([m4_split],
1746 [m4_if([$1], [], [],
1747        [$2], [ ], [m4_if(m4_index([$1], [ ]), [-1], [[[$1]]], [_$0($@)])],
1748        [$2], [], [_$0([$1], [[   ]+])],
1749        [_$0($@)])])
1751 m4_define([_m4_split],
1752 [m4_changequote(-=<{,}>=-)]dnl
1753 [[m4_bpatsubst(-=<{-=<{$1}>=-}>=-, -=<{$2}>=-,
1754                -=<{], [}>=-)]m4_changequote([, ])])
1758 # m4_flatten(STRING)
1759 # ------------------
1760 # If STRING contains end of lines, replace them with spaces.  If there
1761 # are backslashed end of lines, remove them.  This macro is safe with
1762 # active symbols.
1763 #    m4_define(active, ACTIVE)
1764 #    m4_flatten([active
1765 #    act\
1766 #    ive])end
1767 #    => active activeend
1769 # In m4, m4_bpatsubst is expensive, so first check for a newline.
1770 m4_define([m4_flatten],
1771 [m4_if(m4_index([$1], [
1772 ]), [-1], [[$1]],
1773        [m4_translit(m4_bpatsubst([[[$1]]], [\\
1774 ]), [
1775 ], [ ])])])
1778 # m4_strip(STRING)
1779 # ----------------
1780 # Expands into STRING with tabs and spaces singled out into a single
1781 # space, and removing leading and trailing spaces.
1783 # This macro is robust to active symbols.
1784 #    m4_define(active, ACTIVE)
1785 #    m4_strip([  active <tab> <tab>active ])end
1786 #    => active activeend
1788 # First, notice that we guarantee trailing space.  Why?  Because regular
1789 # expressions are greedy, and `.* ?' would always group the space into the
1790 # .* portion.  The algorithm is simpler by avoiding `?' at the end.  The
1791 # algorithm correctly strips everything if STRING is just ` '.
1793 # Then notice the second pattern: it is in charge of removing the
1794 # leading/trailing spaces.  Why not just `[^ ]'?  Because they are
1795 # applied to over-quoted strings, i.e. more or less [STRING], due
1796 # to the limitations of m4_bpatsubsts.  So the leading space in STRING
1797 # is the *second* character; equally for the trailing space.
1798 m4_define([m4_strip],
1799 [m4_bpatsubsts([$1 ],
1800                [[        ]+], [ ],
1801                [^. ?\(.*\) .$], [[[\1]]])])
1804 # m4_normalize(STRING)
1805 # --------------------
1806 # Apply m4_flatten and m4_strip to STRING.
1808 # The argument is quoted, so that the macro is robust to active symbols:
1810 #    m4_define(active, ACTIVE)
1811 #    m4_normalize([  act\
1812 #    ive
1813 #    active ])end
1814 #    => active activeend
1816 m4_define([m4_normalize],
1817 [m4_strip(m4_flatten([$1]))])
1821 # m4_join(SEP, ARG1, ARG2...)
1822 # ---------------------------
1823 # Produce ARG1SEPARG2...SEPARGn.  Avoid back-to-back SEP when a given ARG
1824 # is the empty string.  No expansion is performed on SEP or ARGs.
1826 # Since the number of arguments to join can be arbitrarily long, we
1827 # want to avoid having more than one $@ in the macro definition;
1828 # otherwise, the expansion would require twice the memory of the already
1829 # long list.  Hence, m4_join merely looks for the first non-empty element,
1830 # and outputs just that element; while _m4_join looks for all non-empty
1831 # elements, and outputs them following a separator.  The final trick to
1832 # note is that we decide between recursing with $0 or _$0 based on the
1833 # nested m4_if ending with `_'.
1834 m4_define([m4_join],
1835 [m4_if([$#], [1], [],
1836        [$#], [2], [[$2]],
1837        [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift2($@))])])
1838 m4_define([_m4_join],
1839 [m4_if([$#$2], [2], [],
1840        [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift2($@))])])
1843 # m4_combine([SEPARATOR], PREFIX-LIST, [INFIX], SUFFIX...)
1844 # --------------------------------------------------------
1845 # Produce the pairwise combination of every element in the quoted,
1846 # comma-separated PREFIX-LIST with every element from the SUFFIX arguments.
1847 # Each pair is joined with INFIX, and pairs are separated by SEPARATOR.
1848 # No expansion occurs on SEPARATOR, INFIX, or elements of either list.
1850 # For example:
1851 #   m4_combine([, ], [[a], [b], [c]], [-], [1], [2], [3])
1852 #   => a-1, a-2, a-3, b-1, b-2, b-3, c-1, c-2, c-3
1854 # In order to have the correct number of SEPARATORs, we use a temporary
1855 # variable that redefines itself after the first use.  Note that since there
1856 # is no user expansion, we can avoid m4_defn overhead by overquoting the
1857 # second definition of m4_Separator, and by using m4_builtin.  Likewise,
1858 # we compute the m4_shift3 only once, rather than in each iteration of the
1859 # outer m4_foreach.
1860 m4_define([m4_combine],
1861 [m4_if(m4_eval([$# > 3]), [1],
1862        [m4_pushdef([m4_Separator], [m4_define([m4_Separator], [[$1]])])]]dnl
1863 [[m4_foreach([m4_Prefix], [$2],
1864              [m4_foreach([m4_Suffix], ]m4_dquote(m4_dquote(m4_shift3($@)))[,
1865                          [m4_Separator[]m4_builtin([defn],
1866                                         [m4_Prefix])[$3]m4_builtin([defn],
1867                                                         [m4_Suffix])])])]]dnl
1868 [[m4_builtin([popdef], [m4_Separator])])])
1871 # m4_append(MACRO-NAME, STRING, [SEPARATOR])
1872 # ------------------------------------------
1873 # Redefine MACRO-NAME to hold its former content plus `SEPARATOR`'STRING'
1874 # at the end.  It is valid to use this macro with MACRO-NAME undefined,
1875 # in which case no SEPARATOR is added.  Be aware that the criterion is
1876 # `not being defined', and not `not being empty'.
1878 # Note that neither STRING nor SEPARATOR are expanded here; rather, when
1879 # you expand MACRO-NAME, they will be expanded at that point in time.
1881 # This macro is robust to active symbols.  It can be used to grow
1882 # strings.
1884 #    | m4_define(active, ACTIVE)dnl
1885 #    | m4_append([sentence], [This is an])dnl
1886 #    | m4_append([sentence], [ active ])dnl
1887 #    | m4_append([sentence], [symbol.])dnl
1888 #    | sentence
1889 #    | m4_undefine([active])dnl
1890 #    | sentence
1891 #    => This is an ACTIVE symbol.
1892 #    => This is an active symbol.
1894 # It can be used to define hooks.
1896 #    | m4_define(active, ACTIVE)dnl
1897 #    | m4_append([hooks], [m4_define([act1], [act2])])dnl
1898 #    | m4_append([hooks], [m4_define([act2], [active])])dnl
1899 #    | m4_undefine([active])dnl
1900 #    | act1
1901 #    | hooks
1902 #    | act1
1903 #    => act1
1904 #    =>
1905 #    => active
1907 # It can also be used to create lists, although this particular usage was
1908 # broken prior to autoconf 2.62.
1909 #    | m4_append([list], [one], [, ])dnl
1910 #    | m4_append([list], [two], [, ])dnl
1911 #    | m4_append([list], [three], [, ])dnl
1912 #    | list
1913 #    | m4_dquote(list)
1914 #    => one, two, three
1915 #    => [one],[two],[three]
1917 # Use m4_builtin to avoid overhead of m4_defn.
1918 m4_define([m4_append],
1919 [m4_define([$1],
1920            m4_ifdef([$1], [m4_builtin([defn], [$1])[$3]])[$2])])
1923 # m4_append_uniq(MACRO-NAME, STRING, [SEPARATOR], [IF-UNIQ], [IF-DUP])
1924 # --------------------------------------------------------------------
1925 # Like `m4_append', but append only if not yet present.  Additionally,
1926 # expand IF-UNIQ if STRING was appended, or IF-DUP if STRING was already
1927 # present.  Also, warn if SEPARATOR is not empty and occurs within STRING,
1928 # as the algorithm no longer guarantees uniqueness.
1929 m4_define([m4_append_uniq],
1930 [m4_ifval([$3], [m4_if(m4_index([$2], [$3]), [-1], [],
1931                        [m4_warn([syntax],
1932                                 [$0: `$2' contains `$3'])])])_$0($@)])
1933 m4_define([_m4_append_uniq],
1934 [m4_ifdef([$1],
1935           [m4_if(m4_index([$3]m4_builtin([defn], [$1])[$3], [$3$2$3]), [-1],
1936                  [m4_append([$1], [$2], [$3])$4], [$5])],
1937           [m4_append([$1], [$2], [$3])$4])])
1939 # m4_append_uniq_w(MACRO-NAME, STRINGS)
1940 # -------------------------------------
1941 # For each of the words in the whitespace separated list STRINGS, append
1942 # only the unique strings to the definition of MACRO-NAME.
1944 # Avoid overhead of m4_defn by using m4_builtin.
1945 m4_define([m4_append_uniq_w],
1946 [m4_foreach_w([m4_Word], [$2],
1947               [_m4_append_uniq([$1], m4_builtin([defn], [m4_Word]), [ ])])])
1950 # m4_text_wrap(STRING, [PREFIX], [FIRST-PREFIX], [WIDTH])
1951 # -------------------------------------------------------
1952 # Expands into STRING wrapped to hold in WIDTH columns (default = 79).
1953 # If PREFIX is given, each line is prefixed with it.  If FIRST-PREFIX is
1954 # specified, then the first line is prefixed with it.  As a special case,
1955 # if the length of FIRST-PREFIX is greater than that of PREFIX, then
1956 # FIRST-PREFIX will be left alone on the first line.
1958 # No expansion occurs on the contents STRING, PREFIX, or FIRST-PREFIX,
1959 # although quadrigraphs are correctly recognized.
1961 # Typical outputs are:
1963 # m4_text_wrap([Short string */], [   ], [/* ], 20)
1964 #  => /* Short string */
1966 # m4_text_wrap([Much longer string */], [   ], [/* ], 20)
1967 #  => /* Much longer
1968 #  =>    string */
1970 # m4_text_wrap([Short doc.], [          ], [  --short ], 30)
1971 #  =>   --short Short doc.
1973 # m4_text_wrap([Short doc.], [          ], [  --too-wide ], 30)
1974 #  =>   --too-wide
1975 #  =>           Short doc.
1977 # m4_text_wrap([Super long documentation.], [          ], [  --too-wide ], 30)
1978 #  =>   --too-wide
1979 #  =>      Super long
1980 #  =>      documentation.
1982 # FIXME: there is no checking of a longer PREFIX than WIDTH, but do
1983 # we really want to bother with people trying each single corner
1984 # of a software?
1986 # This macro does not leave a trailing space behind the last word of a line,
1987 # which complicates it a bit.  The algorithm is otherwise stupid and simple:
1988 # all the words are preceded by m4_Separator which is defined to empty for
1989 # the first word, and then ` ' (single space) for all the others.
1991 # The algorithm overquotes m4_Prefix and m4_Prefix1 to avoid m4_defn
1992 # overhead, and bypasses m4_popdef overhead with m4_builtin since no user
1993 # macro expansion occurs in the meantime.  Also, the definition is written
1994 # with m4_do, to avoid time wasted on dnl during expansion (since this is
1995 # already a time-consuming macro).
1996 m4_define([m4_text_wrap],
1997 m4_do(dnl set up local variables, to avoid repeated calculations
1998 [[m4_pushdef([m4_Prefix], [[$2]])]],
1999 [[m4_pushdef([m4_Prefix1], m4_if([$3], [], [m4_Prefix], [[[$3]]]))]],
2000 [[m4_pushdef([m4_Width], m4_default([$4], 79))]],
2001 [[m4_pushdef([m4_Indent], m4_qlen(m4_Prefix))]],
2002 [[m4_pushdef([m4_Cursor], m4_qlen(m4_Prefix1))]],
2003 [[m4_pushdef([m4_Separator], [m4_define([m4_Separator], [ ])])]],
2004 dnl expand the first prefix, then check its length vs. regular prefix
2005 dnl same length: nothing special
2006 dnl prefix1 longer: output on line by itself, and reset cursor
2007 dnl prefix1 shorter: pad to length of prefix, and reset cursor
2008 [[m4_Prefix1[]m4_cond([m4_Cursor], m4_Indent, [],
2009                       [m4_eval(m4_Cursor > m4_Indent)], [1], [
2010 m4_Prefix[]m4_define([m4_Cursor], m4_Indent)],
2011                       [m4_format([%*s], m4_max([0],
2012   m4_eval(m4_Indent - m4_Cursor)), [])m4_define([m4_Cursor], m4_Indent)])]],
2013 dnl now, for each word, compute the curser after the word is output, then
2014 dnl check if the cursor would exceed the wrap column
2015 dnl if so, reset cursor, and insert newline and prefix
2016 dnl if not, insert the separator (usually a space)
2017 dnl either way, insert the word
2018 [[m4_foreach_w([m4_Word], [$1],
2019   [m4_define([m4_Cursor],
2020              m4_eval(m4_Cursor + m4_qlen(m4_builtin([defn], [m4_Word]))
2021                      + 1))m4_if(m4_eval(m4_Cursor > m4_Width),
2022       [1], [m4_define([m4_Cursor],
2023                       m4_eval(m4_Indent
2024                               + m4_qlen(m4_builtin([defn], [m4_Word])) + 1))
2025 m4_Prefix[]],
2026       [m4_Separator[]])m4_builtin([defn], [m4_Word])])]],
2027 dnl finally, clean up the local variabls
2028 [[m4_builtin([popdef], [m4_Separator])]],
2029 [[m4_builtin([popdef], [m4_Cursor])]],
2030 [[m4_builtin([popdef], [m4_Indent])]],
2031 [[m4_builtin([popdef], [m4_Width])]],
2032 [[m4_builtin([popdef], [m4_Prefix1])]],
2033 [[m4_builtin([popdef], [m4_Prefix])]]))
2036 # m4_text_box(MESSAGE, [FRAME-CHARACTER = `-'])
2037 # ---------------------------------------------
2038 # Turn MESSAGE into:
2039 #  ## ------- ##
2040 #  ## MESSAGE ##
2041 #  ## ------- ##
2042 # using FRAME-CHARACTER in the border.
2043 m4_define([m4_text_box],
2044 [m4_pushdef([m4_Border],
2045             m4_translit(m4_format([%*s], m4_qlen(m4_expand([$1])), []),
2046                         [ ], m4_if([$2], [], [[-]], [[$2]])))dnl
2047 @%:@@%:@ m4_Border @%:@@%:@
2048 @%:@@%:@ $1 @%:@@%:@
2049 @%:@@%:@ m4_Border @%:@@%:@dnl
2050 m4_builtin([popdef], [m4_Border])dnl
2054 # m4_qlen(STRING)
2055 # ---------------
2056 # Expands to the length of STRING after autom4te converts all quadrigraphs.
2058 # Avoid bpatsubsts for the common case of no quadrigraphs.
2059 m4_define([m4_qlen],
2060 [m4_if(m4_index([$1], [@]), [-1], [m4_len([$1])],
2061        [m4_len(m4_bpatsubst([[$1]], [@\(\(<:\|:>\|S|\|%:\)\(@\)\|&t@\)],
2062                             [\3]))])])
2065 # m4_qdelta(STRING)
2066 # -----------------
2067 # Expands to the net change in the length of STRING from autom4te converting the
2068 # quadrigraphs in STRING.  This number is always negative or zero.
2069 m4_define([m4_qdelta],
2070 [m4_eval(m4_qlen([$1]) - m4_len([$1]))])
2074 ## ----------------------- ##
2075 ## 13. Number processing.  ##
2076 ## ----------------------- ##
2078 # m4_cmp(A, B)
2079 # ------------
2080 # Compare two integer expressions.
2081 # A < B -> -1
2082 # A = B ->  0
2083 # A > B ->  1
2084 m4_define([m4_cmp],
2085 [m4_eval((([$1]) > ([$2])) - (([$1]) < ([$2])))])
2088 # m4_list_cmp(A, B)
2089 # -----------------
2091 # Compare the two lists of integer expressions A and B.  For instance:
2092 #   m4_list_cmp([1, 0],     [1])    ->  0
2093 #   m4_list_cmp([1, 0],     [1, 0]) ->  0
2094 #   m4_list_cmp([1, 2],     [1, 0]) ->  1
2095 #   m4_list_cmp([1, 2, 3],  [1, 2]) ->  1
2096 #   m4_list_cmp([1, 2, -3], [1, 2]) -> -1
2097 #   m4_list_cmp([1, 0],     [1, 2]) -> -1
2098 #   m4_list_cmp([1],        [1, 2]) -> -1
2099 #   m4_define([xa], [oops])dnl
2100 #   m4_list_cmp([[0xa]],    [5+5])  -> 0
2102 # Rather than face the overhead of m4_case, we use a helper function whose
2103 # expansion includes the name of the macro to invoke on the tail, either
2104 # m4_ignore or m4_unquote.  This is particularly useful when comparing
2105 # long lists, since less text is being expanded for deciding when to end
2106 # recursion.
2107 m4_define([m4_list_cmp],
2108 [m4_if([$1$2], [], 0,
2109        [$1], [], [$0(0, [$2])],
2110        [$2], [], [$0([$1], 0)],
2111        [$1], [$2], 0,
2112        [_$0(m4_cmp(m4_car($1), m4_car($2)))([$0(m4_cdr($1), m4_cdr($2))])])])
2113 m4_define([_m4_list_cmp],
2114 [m4_if([$1], 0, [m4_unquote], [$1m4_ignore])])
2116 # m4_max(EXPR, ...)
2117 # m4_min(EXPR, ...)
2118 # -----------------
2119 # Return the decimal value of the maximum (or minimum) in a series of
2120 # integer expressions.
2122 # M4 1.4.x doesn't provide ?:.  Hence this huge m4_eval.  Avoid m4_eval
2123 # if both arguments are identical, but be aware of m4_max(0xa, 10) (hence
2124 # the use of <=, not just <, in the second multiply).
2125 m4_define([m4_max],
2126 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
2127        [$#], [1], [m4_eval([$1])],
2128        [$#$1], [2$2], [m4_eval([$1])],
2129        [$#], [2],
2130        [m4_eval((([$1]) > ([$2])) * ([$1]) + (([$1]) <= ([$2])) * ([$2]))],
2131        [$0($0([$1], [$2]), m4_shift2($@))])])
2132 m4_define([m4_min],
2133 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
2134        [$#], [1], [m4_eval([$1])],
2135        [$#$1], [2$2], [m4_eval([$1])],
2136        [$#], [2],
2137        [m4_eval((([$1]) < ([$2])) * ([$1]) + (([$1]) >= ([$2])) * ([$2]))],
2138        [$0($0([$1], [$2]), m4_shift2($@))])])
2141 # m4_sign(A)
2142 # ----------
2143 # The sign of the integer expression A.
2144 m4_define([m4_sign],
2145 [m4_eval((([$1]) > 0) - (([$1]) < 0))])
2149 ## ------------------------ ##
2150 ## 14. Version processing.  ##
2151 ## ------------------------ ##
2154 # m4_version_unletter(VERSION)
2155 # ----------------------------
2156 # Normalize beta version numbers with letters to numeric expressions, which
2157 # can then be handed to m4_eval for the purpose of comparison.
2159 #   Nl -> (N+1).-1.(l#)
2161 # for example:
2162 #   [2.14a] -> [2.14+1.-1.[0r36:a]] -> 2.15.-1.10
2163 #   [2.14b] -> [2.15+1.-1.[0r36:b]] -> 2.15.-1.11
2164 #   [2.61aa.b] -> [2.61+1.-1.[0r36:aa],+1.-1.[0r36:b]] -> 2.62.-1.370.1.-1.11
2166 # This macro expects reasonable version numbers, but can handle double
2167 # letters and does not expand any macros.  Original version strings can
2168 # use both `.' and `-' separators.
2170 # Inline constant expansions, to avoid m4_defn overhead.
2171 # _m4_version_unletter is the real workhorse used by m4_version_compare,
2172 # but since [0r36:a] is less readable than 10, we provide a wrapper for
2173 # human use.
2174 m4_define([m4_version_unletter],
2175 [m4_map_sep([m4_eval], [.],
2176             m4_dquote(m4_dquote_elt(m4_unquote(_$0([$1])))))])
2177 m4_define([_m4_version_unletter],
2178 [m4_bpatsubst(m4_translit([[[$1]]], [.-], [,,]),]dnl
2179 m4_dquote(m4_dquote(m4_defn([m4_cr_Letters])))[[+],
2180               [+1,-1,[0r36:\&]])])
2183 # m4_version_compare(VERSION-1, VERSION-2)
2184 # ----------------------------------------
2185 # Compare the two version numbers and expand into
2186 #  -1 if VERSION-1 < VERSION-2
2187 #   0 if           =
2188 #   1 if           >
2189 m4_define([m4_version_compare],
2190 [m4_list_cmp(_m4_version_unletter([$1]), _m4_version_unletter([$2]))])
2193 # m4_PACKAGE_NAME
2194 # m4_PACKAGE_TARNAME
2195 # m4_PACKAGE_VERSION
2196 # m4_PACKAGE_STRING
2197 # m4_PACKAGE_BUGREPORT
2198 # --------------------
2199 m4_include([m4sugar/version.m4])
2202 # m4_version_prereq(VERSION, [IF-OK], [IF-NOT = FAIL])
2203 # ----------------------------------------------------
2204 # Check this Autoconf version against VERSION.
2205 m4_define([m4_version_prereq],
2206 [m4_if(m4_version_compare(]m4_dquote(m4_defn([m4_PACKAGE_VERSION]))[, [$1]),
2207        [-1],
2208        [m4_default([$3],
2209                    [m4_fatal([Autoconf version $1 or higher is required],
2210                              [63])])],
2211        [$2])])
2215 ## ------------------- ##
2216 ## 15. File handling.  ##
2217 ## ------------------- ##
2220 # It is a real pity that M4 comes with no macros to bind a diversion
2221 # to a file.  So we have to deal without, which makes us a lot more
2222 # fragile than we should.
2225 # m4_file_append(FILE-NAME, CONTENT)
2226 # ----------------------------------
2227 m4_define([m4_file_append],
2228 [m4_syscmd([cat >>$1 <<_m4eof
2230 _m4eof
2232 m4_if(m4_sysval, [0], [],
2233       [m4_fatal([$0: cannot write: $1])])])
2237 ## ------------------------ ##
2238 ## 16. Setting M4sugar up.  ##
2239 ## ------------------------ ##
2242 # m4_init
2243 # -------
2244 # Initialize the m4sugar language.
2245 m4_define([m4_init],
2246 [# All the M4sugar macros start with `m4_', except `dnl' kept as is
2247 # for sake of simplicity.
2248 m4_pattern_forbid([^_?m4_])
2249 m4_pattern_forbid([^dnl$])
2251 # _m4_divert_diversion should be defined:
2252 m4_divert_push([KILL])
2254 # Check the divert push/pop perfect balance.
2255 m4_wrap([m4_divert_pop([])
2256          m4_ifdef([_m4_divert_diversion],
2257            [m4_fatal([$0: unbalanced m4_divert_push:]_m4_divert_n_stack)])[]])