Document m4_expand limitation.
[autoconf.git] / lib / m4sugar / m4sugar.m4
blob70b9a84a889dffe395b3aec309e404c5539cdb50
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 Free
7 # Software Foundation, Inc.
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 # As a special exception, the Free Software Foundation gives unlimited
23 # permission to copy, distribute and modify the configure scripts that
24 # are the output of Autoconf.  You need not follow the terms of the GNU
25 # General Public License when using or distributing such scripts, even
26 # though portions of the text of Autoconf appear in them.  The GNU
27 # General Public License (GPL) does govern all other use of the material
28 # that constitutes the Autoconf program.
30 # Certain portions of the Autoconf source text are designed to be copied
31 # (in certain cases, depending on the input) into the output of
32 # Autoconf.  We call these the "data" portions.  The rest of the Autoconf
33 # source text consists of comments plus executable code that decides which
34 # of the data portions to output in any given case.  We call these
35 # comments and executable code the "non-data" portions.  Autoconf never
36 # copies any of the non-data portions into its output.
38 # This special exception to the GPL applies to versions of Autoconf
39 # released by the Free Software Foundation.  When you make and
40 # distribute a modified version of Autoconf, you may extend this special
41 # exception to the GPL to apply to your modified version as well, *unless*
42 # your modified version has the potential to copy into its output some
43 # of the text that was the non-data portion of the version that you started
44 # with.  (In other words, unless your change moves or copies text from
45 # the non-data portions to the data portions.)  If your modification has
46 # such potential, you must delete any notice of this special exception
47 # to the GPL from your modified version.
49 # Written by Akim Demaille.
52 # Set the quotes, whatever the current quoting system.
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_rename([m4wrap], [m4_wrap])
142 m4_ifdef([mkstemp],dnl added in M4 1.4.8
143 [m4_rename_m4([mkstemp])
144 m4_copy([m4_mkstemp], [m4_maketemp])
145 m4_undefine([maketemp])],
146 [m4_rename_m4([maketemp])
147 m4_copy([m4_maketemp], [m4_mkstemp])])
148 m4_rename([patsubst], [m4_bpatsubst])
149 m4_undefine([popdef])
150 m4_rename_m4([pushdef])
151 m4_rename([regexp], [m4_bregexp])
152 m4_rename_m4([shift])
153 m4_undefine([sinclude])
154 m4_rename_m4([substr])
155 m4_rename_m4([symbols])
156 m4_rename_m4([syscmd])
157 m4_rename_m4([sysval])
158 m4_rename_m4([traceoff])
159 m4_rename_m4([traceon])
160 m4_rename_m4([translit])
161 m4_undefine([undivert])
164 ## ------------------- ##
165 ## 2. Error messages.  ##
166 ## ------------------- ##
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])])
611 ## ------------------------- ##
612 ## 7. Quoting manipulation.  ##
613 ## ------------------------- ##
616 # m4_apply(MACRO, LIST)
617 # ---------------------
618 # Invoke MACRO, with arguments provided from the quoted list of
619 # comma-separated quoted arguments.  If LIST is empty, invoke MACRO
620 # without arguments.
621 m4_define([m4_apply],
622 [m4_if([$2], [], [$1], [$1($2)])[]])
625 # m4_count(ARGS)
626 # --------------
627 # Return a count of how many ARGS are present.
628 m4_define([m4_count], [$#])
631 # m4_do(STRING, ...)
632 # ------------------
633 # This macro invokes all its arguments (in sequence, of course).  It is
634 # useful for making your macros more structured and readable by dropping
635 # unnecessary dnl's and have the macros indented properly.
636 m4_define([m4_do],
637 [m4_if([$#], 0, [],
638        [$#], 1, [$1],
639        [$1[]m4_do(m4_shift($@))])])
642 # m4_dquote(ARGS)
643 # ---------------
644 # Return ARGS as a quoted list of quoted arguments.
645 m4_define([m4_dquote],  [[$@]])
648 # m4_dquote_elt(ARGS)
649 # -------------------
650 # Return ARGS as an unquoted list of double-quoted arguments.
651 m4_define([m4_dquote_elt],
652 [m4_if([$#], [0], [],
653        [$#], [1], [[[$1]]],
654        [[[$1]],$0(m4_shift($@))])])
657 # m4_echo(ARGS)
658 # -------------
659 # Return the ARGS, with the same level of quoting.  Whitespace after
660 # unquoted commas are consumed.
661 m4_define([m4_echo], [$@])
664 # m4_expand(ARG)
665 # --------------
666 # Return the expansion of ARG as a single string.  Unlike m4_quote($1), this
667 # correctly preserves whitespace following single-quoted commas that appeared
668 # within ARG (however, it does not preserve whitespace after any unquoted
669 # commas encountered in the expansion).
671 #   m4_define([active], [ACT, IVE])
672 #   m4_define([active2], [[ACT, IVE]])
673 #   m4_quote(active, active2)
674 #   => ACT,IVE,ACT, IVE
675 #   m4_expand([active, active2])
676 #   => ACT,IVE, ACT, IVE
678 # Unfortunately, due to limitations in m4, ARG must contain balanced quotes
679 # (use quadrigraphs) and balanced parenthesis (use creative shell comments
680 # when writing shell case statements).
682 # Splitting a quoted ARG on `,' preserves space, but produces a quoted list.
683 # Unquote the list, then expand each argument while preserving the leading
684 # spaces; finally, collect each argument back into the final string.
685 m4_define([m4_expand],
686 [m4_quote(_$0(m4_unquote(m4_split([$1], [,]))))])
688 # _m4_expand(ARGS)
689 # ----------------
690 # Return the expansion of each ARG, separated by `,'.  Less efficient than
691 # m4_unquote, but preserves quoted leading space in each ARG.
692 m4_define([_m4_expand],
693 [m4_if([$#], [0], [],
694        [$#], [1], [$1],
695        [$1,$0(m4_shift($@))])])
698 # m4_ignore(ARGS)
699 # ---------------
700 # Expands to nothing.  Useful for conditionally ignoring an arbitrary
701 # number of arguments (see _m4_list_cmp for an example).
702 m4_define([m4_ignore])
705 # m4_make_list(ARGS)
706 # ------------------
707 # Similar to m4_dquote, this creates a quoted list of quoted ARGS.  This
708 # version is less efficient than m4_dquote, but separates each argument
709 # with a comma and newline, rather than just comma, for readability.
710 # When developing an m4sugar algorithm, you could temporarily use
711 #   m4_pushdef([m4_dquote],m4_defn([m4_make_list]))
712 # around your code to make debugging easier.
713 m4_define([m4_make_list], [m4_join([,
714 ], m4_dquote_elt($@))])
717 # m4_noquote(STRING)
718 # ------------------
719 # Return the result of ignoring all quotes in STRING and invoking the
720 # macros it contains.  Amongst other things, this is useful for enabling
721 # macro invocations inside strings with [] blocks (for instance regexps
722 # and help-strings).  On the other hand, since all quotes are disabled,
723 # any macro expanded during this time that relies on nested [] quoting
724 # will likely crash and burn.  This macro is seldom useful; consider
725 # m4_unquote instead.
726 m4_define([m4_noquote],
727 [m4_changequote(-=<{,}>=-)$1-=<{}>=-m4_changequote([,])])
730 # m4_quote(ARGS)
731 # --------------
732 # Return ARGS as a single argument.  Any whitespace after unquoted commas
733 # is stripped.  There is always output, even when there were no arguments.
735 # It is important to realize the difference between `m4_quote(exp)' and
736 # `[exp]': in the first case you obtain the quoted *result* of the
737 # expansion of EXP, while in the latter you just obtain the string
738 # `exp'.
739 m4_define([m4_quote],  [[$*]])
742 # _m4_quote(ARGS)
743 # ---------------
744 # Like m4_quote, except that when there are no arguments, there is no
745 # output.  For conditional scenarios (such as passing _m4_quote as the
746 # macro name in m4_map), this feature can be used to distinguish between
747 # one argument of the empty string vs. no arguments.  However, in the
748 # normal case with arguments present, this is less efficient than m4_quote.
749 m4_define([_m4_quote],
750 [m4_if([$#], [0], [], [[$*]])])
753 # m4_unquote(ARGS)
754 # ----------------
755 # Remove one layer of quotes from each ARG, performing one level of
756 # expansion.  For one argument, m4_unquote([arg]) is more efficient than
757 # m4_do([arg]), but for multiple arguments, the difference is that
758 # m4_unquote separates arguments with commas while m4_do concatenates.
759 m4_define([m4_unquote], [$*])
762 ## -------------------------- ##
763 ## 8. Implementing m4 loops.  ##
764 ## -------------------------- ##
767 # m4_for(VARIABLE, FIRST, LAST, [STEP = +/-1], EXPRESSION)
768 # --------------------------------------------------------
769 # Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO with
770 # increments of STEP.
771 # Both limits are included, and bounds are checked for consistency.
772 # The algorithm is robust to indirect VARIABLE names, and uses m4_builtin
773 # to avoid some of the m4_defn overhead.
774 m4_define([m4_for],
775 [m4_pushdef([$1], m4_eval([$2]))dnl
776 m4_cond([m4_eval(([$3]) > m4_builtin([defn], [$1]))], 1,
777 [m4_pushdef([_m4_step], m4_eval(m4_default([$4], 1)))dnl
778 m4_assert(_m4_step > 0)dnl
779 _m4_for([$1], m4_eval((([$3]) - m4_builtin([defn], [$1]))
780                       / _m4_step * _m4_step + m4_builtin([defn], [$1])),
781         _m4_step, [$5])],
782         [m4_eval(([$3]) < m4_builtin([defn], [$1]))], 1,
783 [m4_pushdef([_m4_step], m4_eval(m4_default([$4], -1)))dnl
784 m4_assert(_m4_step < 0)dnl
785 _m4_for([$1], m4_eval((m4_builtin([defn], [$1]) - ([$3]))
786                       / -(_m4_step) * _m4_step + m4_builtin([defn], [$1])),
787         _m4_step, [$5])],
788         [m4_pushdef([_m4_step])dnl
789 $5])[]dnl
790 m4_popdef([_m4_step])dnl
791 m4_popdef([$1])])
794 # _m4_for(VARIABLE, LAST, STEP, EXPRESSION)
795 # -----------------------------------------
796 # Core of the loop, no consistency checks, all arguments are plain numbers.
797 m4_define([_m4_for],
798 [$4[]dnl
799 m4_if(m4_defn([$1]), [$2], [],
800       [m4_define([$1], m4_eval(m4_defn([$1])+[$3]))$0($@)])])
803 # Implementing `foreach' loops in m4 is much more tricky than it may
804 # seem.  For example, the old M4 1.4.4 manual had an incorrect example,
805 # which looked like this (when translated to m4sugar):
807 # | # foreach(VAR, (LIST), STMT)
808 # | m4_define([foreach],
809 # |   [m4_pushdef([$1])_foreach([$1], [$2], [$3])m4_popdef([$1])])
810 # | m4_define([_arg1], [$1])
811 # | m4_define([_foreach],
812 # |   [m4_if([$2], [()], ,
813 # |     [m4_define([$1], _arg1$2)$3[]_foreach([$1], (m4_shift$2), [$3])])])
815 # But then if you run
817 # | m4_define(a, 1)
818 # | m4_define(b, 2)
819 # | m4_define(c, 3)
820 # | foreach([f], [([a], [(b], [c)])], [echo f
821 # | ])
823 # it gives
825 #  => echo 1
826 #  => echo (2,3)
828 # which is not what is expected.
830 # Of course the problem is that many quotes are missing.  So you add
831 # plenty of quotes at random places, until you reach the expected
832 # result.  Alternatively, if you are a quoting wizard, you directly
833 # reach the following implementation (but if you really did, then
834 # apply to the maintenance of m4sugar!).
836 # | # foreach(VAR, (LIST), STMT)
837 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
838 # | m4_define([_arg1], [[$1]])
839 # | m4_define([_foreach],
840 # |  [m4_if($2, [()], ,
841 # |    [m4_define([$1], [_arg1$2])$3[]_foreach([$1], [(m4_shift$2)], [$3])])])
843 # which this time answers
845 #  => echo a
846 #  => echo (b
847 #  => echo c)
849 # Bingo!
851 # Well, not quite.
853 # With a better look, you realize that the parens are more a pain than
854 # a help: since anyway you need to quote properly the list, you end up
855 # with always using an outermost pair of parens and an outermost pair
856 # of quotes.  Rejecting the parens both eases the implementation, and
857 # simplifies the use:
859 # | # foreach(VAR, (LIST), STMT)
860 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
861 # | m4_define([_arg1], [$1])
862 # | m4_define([_foreach],
863 # |  [m4_if($2, [], ,
864 # |    [m4_define([$1], [_arg1($2)])$3[]_foreach([$1], [m4_shift($2)], [$3])])])
867 # Now, just replace the `$2' with `m4_quote($2)' in the outer `m4_if'
868 # to improve robustness, and you come up with a nice implementation
869 # that doesn't require extra parenthesis in the user's LIST.
871 # But wait -  now the algorithm is quadratic, because every recursion of
872 # the algorithm keeps the entire LIST and merely adds another m4_shift to
873 # the quoted text.  If the user has a lot of elements in LIST, you can
874 # bring the system to its knees with the memory m4 then requires, or trip
875 # the m4 --nesting-limit recursion factor.  The only way to avoid
876 # quadratic growth is ensure m4_shift is expanded prior to the recursion.
877 # Hence the design below.
879 # The M4 manual now includes a chapter devoted to this issue, with
880 # the lessons learned from m4sugar.
883 # m4_foreach(VARIABLE, LIST, EXPRESSION)
884 # --------------------------------------
886 # Expand EXPRESSION assigning each value of the LIST to VARIABLE.
887 # LIST should have the form `item_1, item_2, ..., item_n', i.e. the
888 # whole list must *quoted*.  Quote members too if you don't want them
889 # to be expanded.
891 # This macro is robust to active symbols:
892 #      | m4_define(active, [ACT, IVE])
893 #      | m4_foreach(Var, [active, active], [-Var-])
894 #     => -ACT--IVE--ACT--IVE-
896 #      | m4_foreach(Var, [[active], [active]], [-Var-])
897 #     => -ACT, IVE--ACT, IVE-
899 #      | m4_foreach(Var, [[[active]], [[active]]], [-Var-])
900 #     => -active--active-
902 # This macro is called frequently, so avoid extra expansions such as
903 # m4_ifval and dnl.  Also, since $2 might be quite large, try to use it
904 # as little as possible in _m4_foreach; each extra use requires that much
905 # more memory for expansion.  So, rather than directly compare $2 against
906 # [] and use m4_car/m4_cdr for recursion, we instead unbox the list (which
907 # requires swapping the argument order in the helper) and use _m4_shift3
908 # to detect when recursion is complete.
909 m4_define([m4_foreach],
910 [m4_pushdef([$1])_$0([$1], [$3]m4_if([$2], [], [], [, $2]))m4_popdef([$1])])
912 m4_define([_m4_foreach],
913 [m4_if([$#], [2], [],
914        [m4_define([$1], [$3])$2[]$0([$1], [$2]_m4_shift3($@))])])
917 # m4_foreach_w(VARIABLE, LIST, EXPRESSION)
918 # ----------------------------------------
920 # Like m4_foreach, but the list is whitespace separated.
922 # This macro is robust to active symbols:
923 #    m4_foreach_w([Var], [ active
924 #    b  act\
925 #    ive  ], [-Var-])end
926 #    => -active--b--active-end
928 m4_define([m4_foreach_w],
929 [m4_foreach([$1], m4_split(m4_normalize([$2]), [ ]), [$3])])
932 # m4_map(MACRO, LIST)
933 # -------------------
934 # Invoke MACRO($1), MACRO($2) etc. where $1, $2... are the elements
935 # of LIST.  $1, $2... must in turn be lists, appropriate for m4_apply.
937 # Since LIST may be quite large, we want to minimize how often it appears
938 # in the expansion.  Rather than use m4_car/m4_cdr iteration, we unbox the
939 # list, and use _m4_shift2 to detect the end of recursion.
940 m4_define([m4_map],
941 [m4_if([$2], [], [],
942        [_$0([$1], $2)])])
943 m4_define([_m4_map],
944 [m4_if([$#], [1], [],
945        [m4_apply([$1], [$2])$0([$1]_m4_shift2($@))])])
948 # m4_map_sep(MACRO, SEPARATOR, LIST)
949 # ----------------------------------
950 # Invoke MACRO($1), SEPARATOR, MACRO($2), ..., MACRO($N) where $1, $2... $N
951 # are the elements of LIST, and are in turn lists appropriate for m4_apply.
952 # SEPARATOR is not further expanded.
953 m4_define([m4_map_sep],
954 [m4_if([$3], [], [],
955        [m4_apply([$1], m4_car($3))m4_map([[$2]$1]_m4_cdr($3))])])
958 ## --------------------------- ##
959 ## 9. More diversion support.  ##
960 ## --------------------------- ##
963 # _m4_divert(DIVERSION-NAME or NUMBER)
964 # ------------------------------------
965 # If DIVERSION-NAME is the name of a diversion, return its number,
966 # otherwise if it is a NUMBER return it.
967 m4_define([_m4_divert],
968 [m4_ifdef([_m4_divert($1)],
969           [m4_indir([_m4_divert($1)])],
970           [$1])])
972 # KILL is only used to suppress output.
973 m4_define([_m4_divert(KILL)],           -1)
975 # The empty diversion name is a synonym for 0.
976 m4_define([_m4_divert()],                0)
979 # _m4_divert_n_stack
980 # ------------------
981 # Print m4_divert_stack with newline prepended, if it's nonempty.
982 m4_define([_m4_divert_n_stack],
983 [m4_ifdef([m4_divert_stack], [
984 m4_defn([m4_divert_stack])])])
987 # m4_divert(DIVERSION-NAME)
988 # -------------------------
989 # Change the diversion stream to DIVERSION-NAME.
990 m4_define([m4_divert],
991 [m4_define([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
992 m4_builtin([divert], _m4_divert([$1]))dnl
996 # m4_divert_push(DIVERSION-NAME)
997 # ------------------------------
998 # Change the diversion stream to DIVERSION-NAME, while stacking old values.
999 m4_define([m4_divert_push],
1000 [m4_pushdef([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
1001 m4_pushdef([_m4_divert_diversion], [$1])dnl
1002 m4_builtin([divert], _m4_divert([$1]))dnl
1006 # m4_divert_pop([DIVERSION-NAME])
1007 # -------------------------------
1008 # Change the diversion stream to its previous value, unstacking it.
1009 # If specified, verify we left DIVERSION-NAME.
1010 # When we pop the last value from the stack, we divert to -1.
1011 m4_define([m4_divert_pop],
1012 [m4_ifndef([_m4_divert_diversion],
1013            [m4_fatal([too many m4_divert_pop])])dnl
1014 m4_if([$1], [], [],
1015       [$1], m4_defn([_m4_divert_diversion]), [],
1016       [m4_fatal([$0($1): diversion mismatch: ]_m4_divert_n_stack)])dnl
1017 m4_popdef([m4_divert_stack])dnl
1018 m4_popdef([_m4_divert_diversion])dnl
1019 m4_builtin([divert],
1020            m4_ifdef([_m4_divert_diversion],
1021                     [_m4_divert(m4_defn([_m4_divert_diversion]))],
1022                     -1))dnl
1026 # m4_divert_text(DIVERSION-NAME, CONTENT)
1027 # ---------------------------------------
1028 # Output CONTENT into DIVERSION-NAME (which may be a number actually).
1029 # An end of line is appended for free to CONTENT.
1030 m4_define([m4_divert_text],
1031 [m4_divert_push([$1])dnl
1033 m4_divert_pop([$1])dnl
1037 # m4_divert_once(DIVERSION-NAME, CONTENT)
1038 # ---------------------------------------
1039 # Output CONTENT into DIVERSION-NAME once, if not already there.
1040 # An end of line is appended for free to CONTENT.
1041 m4_define([m4_divert_once],
1042 [m4_expand_once([m4_divert_text([$1], [$2])])])
1045 # m4_undivert(DIVERSION-NAME)
1046 # ---------------------------
1047 # Undivert DIVERSION-NAME.  Unlike the M4 version, this only takes a single
1048 # diversion identifier, and should not be used to undivert files.
1049 m4_define([m4_undivert],
1050 [m4_builtin([undivert], _m4_divert([$1]))])
1053 ## --------------------------------------------- ##
1054 ## 10. Defining macros with bells and whistles.  ##
1055 ## --------------------------------------------- ##
1057 # `m4_defun' is basically `m4_define' but it equips the macro with the
1058 # needed machinery for `m4_require'.  A macro must be m4_defun'd if
1059 # either it is m4_require'd, or it m4_require's.
1061 # Two things deserve attention and are detailed below:
1062 #  1. Implementation of m4_require
1063 #  2. Keeping track of the expansion stack
1065 # 1. Implementation of m4_require
1066 # ===============================
1068 # Of course m4_defun AC_PROVIDE's the macro, so that a macro which has
1069 # been expanded is not expanded again when m4_require'd, but the
1070 # difficult part is the proper expansion of macros when they are
1071 # m4_require'd.
1073 # The implementation is based on two ideas, (i) using diversions to
1074 # prepare the expansion of the macro and its dependencies (by Franc,ois
1075 # Pinard), and (ii) expand the most recently m4_require'd macros _after_
1076 # the previous macros (by Axel Thimm).
1079 # The first idea: why use diversions?
1080 # -----------------------------------
1082 # When a macro requires another, the other macro is expanded in new
1083 # diversion, GROW.  When the outer macro is fully expanded, we first
1084 # undivert the most nested diversions (GROW - 1...), and finally
1085 # undivert GROW.  To understand why we need several diversions,
1086 # consider the following example:
1088 # | m4_defun([TEST1], [Test...REQUIRE([TEST2])1])
1089 # | m4_defun([TEST2], [Test...REQUIRE([TEST3])2])
1090 # | m4_defun([TEST3], [Test...3])
1092 # Because m4_require is not required to be first in the outer macros, we
1093 # must keep the expansions of the various levels of m4_require separated.
1094 # Right before executing the epilogue of TEST1, we have:
1096 #          GROW - 2: Test...3
1097 #          GROW - 1: Test...2
1098 #          GROW:     Test...1
1099 #          BODY:
1101 # Finally the epilogue of TEST1 undiverts GROW - 2, GROW - 1, and
1102 # GROW into the regular flow, BODY.
1104 #          GROW - 2:
1105 #          GROW - 1:
1106 #          GROW:
1107 #          BODY:        Test...3; Test...2; Test...1
1109 # (The semicolons are here for clarification, but of course are not
1110 # emitted.)  This is what Autoconf 2.0 (I think) to 2.13 (I'm sure)
1111 # implement.
1114 # The second idea: first required first out
1115 # -----------------------------------------
1117 # The natural implementation of the idea above is buggy and produces
1118 # very surprising results in some situations.  Let's consider the
1119 # following example to explain the bug:
1121 # | m4_defun([TEST1],  [REQUIRE([TEST2a])REQUIRE([TEST2b])])
1122 # | m4_defun([TEST2a], [])
1123 # | m4_defun([TEST2b], [REQUIRE([TEST3])])
1124 # | m4_defun([TEST3],  [REQUIRE([TEST2a])])
1125 # |
1126 # | AC_INIT
1127 # | TEST1
1129 # The dependencies between the macros are:
1131 #                3 --- 2b
1132 #               /        \              is m4_require'd by
1133 #              /          \       left -------------------- right
1134 #           2a ------------ 1
1136 # If you strictly apply the rules given in the previous section you get:
1138 #          GROW - 2: TEST3
1139 #          GROW - 1: TEST2a; TEST2b
1140 #          GROW:     TEST1
1141 #          BODY:
1143 # (TEST2a, although required by TEST3 is not expanded in GROW - 3
1144 # because is has already been expanded before in GROW - 1, so it has
1145 # been AC_PROVIDE'd, so it is not expanded again) so when you undivert
1146 # the stack of diversions, you get:
1148 #          GROW - 2:
1149 #          GROW - 1:
1150 #          GROW:
1151 #          BODY:        TEST3; TEST2a; TEST2b; TEST1
1153 # i.e., TEST2a is expanded after TEST3 although the latter required the
1154 # former.
1156 # Starting from 2.50, we use an implementation provided by Axel Thimm.
1157 # The idea is simple: the order in which macros are emitted must be the
1158 # same as the one in which macros are expanded.  (The bug above can
1159 # indeed be described as: a macro has been AC_PROVIDE'd before its
1160 # dependent, but it is emitted after: the lack of correlation between
1161 # emission and expansion order is guilty).
1163 # How to do that?  You keep the stack of diversions to elaborate the
1164 # macros, but each time a macro is fully expanded, emit it immediately.
1166 # In the example above, when TEST2a is expanded, but it's epilogue is
1167 # not run yet, you have:
1169 #          GROW - 2:
1170 #          GROW - 1: TEST2a
1171 #          GROW:     Elaboration of TEST1
1172 #          BODY:
1174 # The epilogue of TEST2a emits it immediately:
1176 #          GROW - 2:
1177 #          GROW - 1:
1178 #          GROW:     Elaboration of TEST1
1179 #          BODY:     TEST2a
1181 # TEST2b then requires TEST3, so right before the epilogue of TEST3, you
1182 # have:
1184 #          GROW - 2: TEST3
1185 #          GROW - 1: Elaboration of TEST2b
1186 #          GROW:     Elaboration of TEST1
1187 #          BODY:      TEST2a
1189 # The epilogue of TEST3 emits it:
1191 #          GROW - 2:
1192 #          GROW - 1: Elaboration of TEST2b
1193 #          GROW:     Elaboration of TEST1
1194 #          BODY:     TEST2a; TEST3
1196 # TEST2b is now completely expanded, and emitted:
1198 #          GROW - 2:
1199 #          GROW - 1:
1200 #          GROW:     Elaboration of TEST1
1201 #          BODY:     TEST2a; TEST3; TEST2b
1203 # and finally, TEST1 is finished and emitted:
1205 #          GROW - 2:
1206 #          GROW - 1:
1207 #          GROW:
1208 #          BODY:     TEST2a; TEST3; TEST2b: TEST1
1210 # The idea is simple, but the implementation is a bit evolved.  If you
1211 # are like me, you will want to see the actual functioning of this
1212 # implementation to be convinced.  The next section gives the full
1213 # details.
1216 # The Axel Thimm implementation at work
1217 # -------------------------------------
1219 # We consider the macros above, and this configure.ac:
1221 #           AC_INIT
1222 #           TEST1
1224 # You should keep the definitions of _m4_defun_pro, _m4_defun_epi, and
1225 # m4_require at hand to follow the steps.
1227 # This implements tries not to assume that the current diversion is
1228 # BODY, so as soon as a macro (m4_defun'd) is expanded, we first
1229 # record the current diversion under the name _m4_divert_dump (denoted
1230 # DUMP below for short).  This introduces an important difference with
1231 # the previous versions of Autoconf: you cannot use m4_require if you
1232 # are not inside an m4_defun'd macro, and especially, you cannot
1233 # m4_require directly from the top level.
1235 # We have not tried to simulate the old behavior (better yet, we
1236 # diagnose it), because it is too dangerous: a macro m4_require'd from
1237 # the top level is expanded before the body of `configure', i.e., before
1238 # any other test was run.  I let you imagine the result of requiring
1239 # AC_STDC_HEADERS for instance, before AC_PROG_CC was actually run....
1241 # After AC_INIT was run, the current diversion is BODY.
1242 # * AC_INIT was run
1243 #   DUMP:                undefined
1244 #   diversion stack:     BODY |-
1246 # * TEST1 is expanded
1247 # The prologue of TEST1 sets _m4_divert_dump, which is the diversion
1248 # where the current elaboration will be dumped, to the current
1249 # diversion.  It also m4_divert_push to GROW, where the full
1250 # expansion of TEST1 and its dependencies will be elaborated.
1251 #   DUMP:        BODY
1252 #   BODY:        empty
1253 #   diversions:  GROW, BODY |-
1255 # * TEST1 requires TEST2a
1256 # _m4_require_call m4_divert_pushes another temporary diversion,
1257 # GROW - 1, and expands TEST2a in there.
1258 #   DUMP:        BODY
1259 #   BODY:        empty
1260 #   GROW - 1:    TEST2a
1261 #   diversions:  GROW - 1, GROW, BODY |-
1262 # Than the content of the temporary diversion is moved to DUMP and the
1263 # temporary diversion is popped.
1264 #   DUMP:        BODY
1265 #   BODY:        TEST2a
1266 #   diversions:  GROW, BODY |-
1268 # * TEST1 requires TEST2b
1269 # Again, _m4_require_call pushes GROW - 1 and heads to expand TEST2b.
1270 #   DUMP:        BODY
1271 #   BODY:        TEST2a
1272 #   diversions:  GROW - 1, GROW, BODY |-
1274 # * TEST2b requires TEST3
1275 # _m4_require_call pushes GROW - 2 and expands TEST3 here.
1276 # (TEST3 requires TEST2a, but TEST2a has already been m4_provide'd, so
1277 # nothing happens.)
1278 #   DUMP:        BODY
1279 #   BODY:        TEST2a
1280 #   GROW - 2:    TEST3
1281 #   diversions:  GROW - 2, GROW - 1, GROW, BODY |-
1282 # Than the diversion is appended to DUMP, and popped.
1283 #   DUMP:        BODY
1284 #   BODY:        TEST2a; TEST3
1285 #   diversions:  GROW - 1, GROW, BODY |-
1287 # * TEST1 requires TEST2b (contd.)
1288 # The content of TEST2b is expanded...
1289 #   DUMP:        BODY
1290 #   BODY:        TEST2a; TEST3
1291 #   GROW - 1:    TEST2b,
1292 #   diversions:  GROW - 1, GROW, BODY |-
1293 # ... and moved to DUMP.
1294 #   DUMP:        BODY
1295 #   BODY:        TEST2a; TEST3; TEST2b
1296 #   diversions:  GROW, BODY |-
1298 # * TEST1 is expanded: epilogue
1299 # TEST1's own content is in GROW...
1300 #   DUMP:        BODY
1301 #   BODY:        TEST2a; TEST3; TEST2b
1302 #   GROW:        TEST1
1303 #   diversions:  BODY |-
1304 # ... and it's epilogue moves it to DUMP and then undefines DUMP.
1305 #   DUMP:       undefined
1306 #   BODY:       TEST2a; TEST3; TEST2b; TEST1
1307 #   diversions: BODY |-
1310 # 2. Keeping track of the expansion stack
1311 # =======================================
1313 # When M4 expansion goes wrong it is often extremely hard to find the
1314 # path amongst macros that drove to the failure.  What is needed is
1315 # the stack of macro `calls'. One could imagine that GNU M4 would
1316 # maintain a stack of macro expansions, unfortunately it doesn't, so
1317 # we do it by hand.  This is of course extremely costly, but the help
1318 # this stack provides is worth it.  Nevertheless to limit the
1319 # performance penalty this is implemented only for m4_defun'd macros,
1320 # not for define'd macros.
1322 # The scheme is simplistic: each time we enter an m4_defun'd macros,
1323 # we prepend its name in m4_expansion_stack, and when we exit the
1324 # macro, we remove it (thanks to pushdef/popdef).
1326 # In addition, we want to detect circular m4_require dependencies.
1327 # Each time we expand a macro FOO we define _m4_expanding(FOO); and
1328 # m4_require(BAR) simply checks whether _m4_expanding(BAR) is defined.
1331 # m4_expansion_stack_push(TEXT)
1332 # -----------------------------
1333 m4_define([m4_expansion_stack_push],
1334 [m4_pushdef([m4_expansion_stack],
1335             [$1]m4_ifdef([m4_expansion_stack], [
1336 m4_defn([m4_expansion_stack])]))])
1339 # m4_expansion_stack_pop
1340 # ----------------------
1341 m4_define([m4_expansion_stack_pop],
1342 [m4_popdef([m4_expansion_stack])])
1345 # m4_expansion_stack_dump
1346 # -----------------------
1347 # Dump the expansion stack.
1348 m4_define([m4_expansion_stack_dump],
1349 [m4_ifdef([m4_expansion_stack],
1350           [m4_errprintn(m4_defn([m4_expansion_stack]))])dnl
1351 m4_errprintn(m4_location[: the top level])])
1354 # _m4_divert(GROW)
1355 # ----------------
1356 # This diversion is used by the m4_defun/m4_require machinery.  It is
1357 # important to keep room before GROW because for each nested
1358 # AC_REQUIRE we use an additional diversion (i.e., two m4_require's
1359 # will use GROW - 2.  More than 3 levels has never seemed to be
1360 # needed.)
1362 # ...
1363 # - GROW - 2
1364 #   m4_require'd code, 2 level deep
1365 # - GROW - 1
1366 #   m4_require'd code, 1 level deep
1367 # - GROW
1368 #   m4_defun'd macros are elaborated here.
1370 m4_define([_m4_divert(GROW)],       10000)
1373 # _m4_defun_pro(MACRO-NAME)
1374 # -------------------------
1375 # The prologue for Autoconf macros.
1376 m4_define([_m4_defun_pro],
1377 [m4_ifndef([m4_expansion_stack], [_m4_defun_pro_outer[]])dnl
1378 m4_expansion_stack_push(m4_defn([m4_location($1)])[: $1 is expanded from...])dnl
1379 m4_pushdef([_m4_expanding($1)])dnl
1382 m4_define([_m4_defun_pro_outer],
1383 [m4_copy([_m4_divert_diversion], [_m4_divert_dump])dnl
1384 m4_divert_push([GROW])dnl
1387 # _m4_defun_epi(MACRO-NAME)
1388 # -------------------------
1389 # The Epilogue for Autoconf macros.  MACRO-NAME only helps tracing
1390 # the PRO/EPI pairs.
1391 m4_define([_m4_defun_epi],
1392 [m4_popdef([_m4_expanding($1)])dnl
1393 m4_expansion_stack_pop()dnl
1394 m4_ifndef([m4_expansion_stack], [_m4_defun_epi_outer[]])dnl
1395 m4_provide([$1])dnl
1398 m4_define([_m4_defun_epi_outer],
1399 [m4_undefine([_m4_divert_dump])dnl
1400 m4_divert_pop([GROW])dnl
1401 m4_undivert([GROW])dnl
1405 # m4_defun(NAME, EXPANSION)
1406 # -------------------------
1407 # Define a macro which automatically provides itself.  Add machinery
1408 # so the macro automatically switches expansion to the diversion
1409 # stack if it is not already using it.  In this case, once finished,
1410 # it will bring back all the code accumulated in the diversion stack.
1411 # This, combined with m4_require, achieves the topological ordering of
1412 # macros.  We don't use this macro to define some frequently called
1413 # macros that are not involved in ordering constraints, to save m4
1414 # processing.
1415 m4_define([m4_defun],
1416 [m4_define([m4_location($1)], m4_location)dnl
1417 m4_define([$1],
1418           [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])
1421 # m4_defun_once(NAME, EXPANSION)
1422 # ------------------------------
1423 # As m4_defun, but issues the EXPANSION only once, and warns if used
1424 # several times.
1425 m4_define([m4_defun_once],
1426 [m4_define([m4_location($1)], m4_location)dnl
1427 m4_define([$1],
1428           [m4_provide_if([$1],
1429                          [m4_warn([syntax], [$1 invoked multiple times])],
1430                          [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])])
1433 # m4_pattern_forbid(ERE, [WHY])
1434 # -----------------------------
1435 # Declare that no token matching the forbidden extended regular
1436 # expression ERE should be seen in the output unless...
1437 m4_define([m4_pattern_forbid], [])
1440 # m4_pattern_allow(ERE)
1441 # ---------------------
1442 # ... that token also matches the allowed extended regular expression ERE.
1443 # Both used via traces.
1444 m4_define([m4_pattern_allow], [])
1447 ## --------------------------------- ##
1448 ## 11. Dependencies between macros.  ##
1449 ## --------------------------------- ##
1452 # m4_before(THIS-MACRO-NAME, CALLED-MACRO-NAME)
1453 # ---------------------------------------------
1454 # Issue a warning if CALLED-MACRO-NAME was called before THIS-MACRO-NAME.
1455 m4_define([m4_before],
1456 [m4_provide_if([$2],
1457                [m4_warn([syntax], [$2 was called before $1])])])
1460 # m4_require(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
1461 # -----------------------------------------------------------
1462 # If NAME-TO-CHECK has never been expanded (actually, if it is not
1463 # m4_provide'd), expand BODY-TO-EXPAND *before* the current macro
1464 # expansion.  Once expanded, emit it in _m4_divert_dump.  Keep track
1465 # of the m4_require chain in m4_expansion_stack.
1467 # The normal cases are:
1469 # - NAME-TO-CHECK == BODY-TO-EXPAND
1470 #   Which you can use for regular macros with or without arguments, e.g.,
1471 #     m4_require([AC_PROG_CC], [AC_PROG_CC])
1472 #     m4_require([AC_CHECK_HEADERS(limits.h)], [AC_CHECK_HEADERS(limits.h)])
1473 #   which is just the same as
1474 #     m4_require([AC_PROG_CC])
1475 #     m4_require([AC_CHECK_HEADERS(limits.h)])
1477 # - BODY-TO-EXPAND == m4_indir([NAME-TO-CHECK])
1478 #   In the case of macros with irregular names.  For instance:
1479 #     m4_require([AC_LANG_COMPILER(C)], [indir([AC_LANG_COMPILER(C)])])
1480 #   which means `if the macro named `AC_LANG_COMPILER(C)' (the parens are
1481 #   part of the name, it is not an argument) has not been run, then
1482 #   call it.'
1483 #   Had you used
1484 #     m4_require([AC_LANG_COMPILER(C)], [AC_LANG_COMPILER(C)])
1485 #   then m4_require would have tried to expand `AC_LANG_COMPILER(C)', i.e.,
1486 #   call the macro `AC_LANG_COMPILER' with `C' as argument.
1488 #   You could argue that `AC_LANG_COMPILER', when it receives an argument
1489 #   such as `C' should dispatch the call to `AC_LANG_COMPILER(C)'.  But this
1490 #   `extension' prevents `AC_LANG_COMPILER' from having actual arguments that
1491 #   it passes to `AC_LANG_COMPILER(C)'.
1492 m4_define([m4_require],
1493 [m4_ifdef([_m4_expanding($1)],
1494          [m4_fatal([$0: circular dependency of $1])])dnl
1495 m4_ifndef([_m4_divert_dump],
1496           [m4_fatal([$0($1): cannot be used outside of an ]dnl
1497 m4_bmatch([$0], [^AC_], [[AC_DEFUN]], [[m4_defun]])['d macro])])dnl
1498 m4_provide_if([$1],
1499               [],
1500               [_m4_require_call([$1], [$2])])dnl
1504 # _m4_require_call(BODY-TO-EXPAND)
1505 # --------------------------------
1506 # If m4_require decides to expand the body, it calls this macro.
1507 m4_define([_m4_require_call],
1508 [m4_define([_m4_divert_grow], m4_decr(_m4_divert_grow))dnl
1509 m4_divert_push(_m4_divert_grow)dnl
1510 m4_default([$2], [$1])
1511 m4_provide_if([$1],
1512               [],
1513               [m4_warn([syntax],
1514                        [$1 is m4_require'd but not m4_defun'd])])dnl
1515 m4_divert(m4_defn([_m4_divert_dump]))dnl
1516 m4_undivert(_m4_divert_grow)dnl
1517 m4_divert_pop(_m4_divert_grow)dnl
1518 m4_define([_m4_divert_grow], m4_incr(_m4_divert_grow))dnl
1522 # _m4_divert_grow
1523 # ---------------
1524 # The counter for _m4_require_call.
1525 m4_define([_m4_divert_grow], _m4_divert([GROW]))
1528 # m4_expand_once(TEXT, [WITNESS = TEXT])
1529 # --------------------------------------
1530 # If TEXT has never been expanded, expand it *here*.  Use WITNESS as
1531 # as a memory that TEXT has already been expanded.
1532 m4_define([m4_expand_once],
1533 [m4_provide_if(m4_ifval([$2], [[$2]], [[$1]]),
1534                [],
1535                [m4_provide(m4_ifval([$2], [[$2]], [[$1]]))[]$1])])
1538 # m4_provide(MACRO-NAME)
1539 # ----------------------
1540 m4_define([m4_provide],
1541 [m4_define([m4_provide($1)])])
1544 # m4_provide_if(MACRO-NAME, IF-PROVIDED, IF-NOT-PROVIDED)
1545 # -------------------------------------------------------
1546 # If MACRO-NAME is provided do IF-PROVIDED, else IF-NOT-PROVIDED.
1547 # The purpose of this macro is to provide the user with a means to
1548 # check macros which are provided without letting her know how the
1549 # information is coded.
1550 m4_define([m4_provide_if],
1551 [m4_ifdef([m4_provide($1)],
1552           [$2], [$3])])
1555 ## --------------------- ##
1556 ## 12. Text processing.  ##
1557 ## --------------------- ##
1560 # m4_cr_letters
1561 # m4_cr_LETTERS
1562 # m4_cr_Letters
1563 # -------------
1564 m4_define([m4_cr_letters], [abcdefghijklmnopqrstuvwxyz])
1565 m4_define([m4_cr_LETTERS], [ABCDEFGHIJKLMNOPQRSTUVWXYZ])
1566 m4_define([m4_cr_Letters],
1567 m4_defn([m4_cr_letters])dnl
1568 m4_defn([m4_cr_LETTERS])dnl
1572 # m4_cr_digits
1573 # ------------
1574 m4_define([m4_cr_digits], [0123456789])
1577 # m4_cr_alnum
1578 # -----------
1579 m4_define([m4_cr_alnum],
1580 m4_defn([m4_cr_Letters])dnl
1581 m4_defn([m4_cr_digits])dnl
1585 # m4_cr_symbols1
1586 # m4_cr_symbols2
1587 # -------------------------------
1588 m4_define([m4_cr_symbols1],
1589 m4_defn([m4_cr_Letters])dnl
1592 m4_define([m4_cr_symbols2],
1593 m4_defn([m4_cr_symbols1])dnl
1594 m4_defn([m4_cr_digits])dnl
1597 # m4_cr_all
1598 # ---------
1599 # The character range representing everything, with `-' as the last
1600 # character, since it is special to m4_translit.  Use with care, because
1601 # it contains characters special to M4 (fortunately, both ASCII and EBCDIC
1602 # have [] in order, so m4_defn([m4_cr_all]) remains a valid string).  It
1603 # also contains characters special to terminals, so it should never be
1604 # displayed in an error message.  Also, attempts to map [ and ] to other
1605 # characters via m4_translit must deal with the fact that m4_translit does
1606 # not add quotes to the output.
1608 # It is mainly useful in generating inverted character range maps, for use
1609 # in places where m4_translit is faster than an equivalent m4_bpatsubst;
1610 # the regex `[^a-z]' is equivalent to:
1611 #  m4_translit(m4_dquote(m4_defn([m4_cr_all])), [a-z])
1612 m4_define([m4_cr_all],
1613 m4_translit(m4_dquote(m4_format(m4_dquote(m4_for(
1614   ,1,255,,[[%c]]))m4_for([i],1,255,,[,i]))), [-])-)
1617 # _m4_define_cr_not(CATEGORY)
1618 # ---------------------------
1619 # Define m4_cr_not_CATEGORY as the inverse of m4_cr_CATEGORY.
1620 m4_define([_m4_define_cr_not],
1621 [m4_define([m4_cr_not_$1],
1622            m4_translit(m4_dquote(m4_defn([m4_cr_all])),
1623                        m4_defn([m4_cr_$1])))])
1626 # m4_cr_not_letters
1627 # m4_cr_not_LETTERS
1628 # m4_cr_not_Letters
1629 # m4_cr_not_digits
1630 # m4_cr_not_alnum
1631 # m4_cr_not_symbols1
1632 # m4_cr_not_symbols2
1633 # ------------------
1634 # Inverse character sets
1635 _m4_define_cr_not([letters])
1636 _m4_define_cr_not([LETTERS])
1637 _m4_define_cr_not([Letters])
1638 _m4_define_cr_not([digits])
1639 _m4_define_cr_not([alnum])
1640 _m4_define_cr_not([symbols1])
1641 _m4_define_cr_not([symbols2])
1644 # m4_newline
1645 # ----------
1646 # Expands to a newline.  Exists for formatting reasons.
1647 m4_define([m4_newline], [
1651 # m4_re_escape(STRING)
1652 # --------------------
1653 # Escape RE active characters in STRING.
1654 m4_define([m4_re_escape],
1655 [m4_bpatsubst([$1],
1656               [[][*+.?\^$]], [\\\&])])
1659 # m4_re_string
1660 # ------------
1661 # Regexp for `[a-zA-Z_0-9]*'
1662 # m4_dquote provides literal [] for the character class.
1663 m4_define([m4_re_string],
1664 m4_dquote(m4_defn([m4_cr_symbols2]))dnl
1665 [*]dnl
1669 # m4_re_word
1670 # ----------
1671 # Regexp for `[a-zA-Z_][a-zA-Z_0-9]*'
1672 m4_define([m4_re_word],
1673 m4_dquote(m4_defn([m4_cr_symbols1]))dnl
1674 m4_defn([m4_re_string])dnl
1678 # m4_tolower(STRING)
1679 # m4_toupper(STRING)
1680 # ------------------
1681 # These macros convert STRING to lowercase or uppercase.
1683 # Rather than expand the m4_defn each time, we inline them up front.
1684 m4_define([m4_tolower],
1685 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_LETTERS]))[,
1686                    ]m4_dquote(m4_defn([m4_cr_letters]))[)])
1687 m4_define([m4_toupper],
1688 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_letters]))[,
1689                    ]m4_dquote(m4_defn([m4_cr_LETTERS]))[)])
1692 # m4_split(STRING, [REGEXP])
1693 # --------------------------
1695 # Split STRING into an m4 list of quoted elements.  The elements are
1696 # quoted with [ and ].  Beginning spaces and end spaces *are kept*.
1697 # Use m4_strip to remove them.
1699 # REGEXP specifies where to split.  Default is [\t ]+.
1701 # If STRING is empty, the result is an empty list.
1703 # Pay attention to the m4_changequotes.  When m4 reads the definition of
1704 # m4_split, it still has quotes set to [ and ].  Luckily, these are matched
1705 # in the macro body, so the definition is stored correctly.  Use the same
1706 # alternate quotes as m4_noquote; it must be unlikely to appear in $1.
1708 # Also, notice that $1 is quoted twice, since we want the result to
1709 # be quoted.  Then you should understand that the argument of
1710 # patsubst is -=<{STRING}>=- (i.e., with additional -=<{ and }>=-).
1712 # This macro is safe on active symbols, i.e.:
1713 #   m4_define(active, ACTIVE)
1714 #   m4_split([active active ])end
1715 #   => [active], [active], []end
1717 # Optimize on regex of ` ' (space), since m4_foreach_w already guarantees
1718 # that the list contains single space separators, and a common case is
1719 # splitting a single-element list.  This macro is called frequently,
1720 # so avoid unnecessary dnl inside the definition.
1721 m4_define([m4_split],
1722 [m4_if([$1], [], [],
1723        [$2], [ ], [m4_if(m4_index([$1], [ ]), [-1], [[[$1]]], [_$0($@)])],
1724        [$2], [], [_$0([$1], [[   ]+])],
1725        [_$0($@)])])
1727 m4_define([_m4_split],
1728 [m4_changequote(-=<{,}>=-)]dnl
1729 [[m4_bpatsubst(-=<{-=<{$1}>=-}>=-, -=<{$2}>=-,
1730                -=<{], [}>=-)]m4_changequote([, ])])
1734 # m4_flatten(STRING)
1735 # ------------------
1736 # If STRING contains end of lines, replace them with spaces.  If there
1737 # are backslashed end of lines, remove them.  This macro is safe with
1738 # active symbols.
1739 #    m4_define(active, ACTIVE)
1740 #    m4_flatten([active
1741 #    act\
1742 #    ive])end
1743 #    => active activeend
1745 # In m4, m4_bpatsubst is expensive, so first check for a newline.
1746 m4_define([m4_flatten],
1747 [m4_if(m4_index([$1], [
1748 ]), [-1], [[$1]],
1749        [m4_translit(m4_bpatsubst([[[$1]]], [\\
1750 ]), [
1751 ], [ ])])])
1754 # m4_strip(STRING)
1755 # ----------------
1756 # Expands into STRING with tabs and spaces singled out into a single
1757 # space, and removing leading and trailing spaces.
1759 # This macro is robust to active symbols.
1760 #    m4_define(active, ACTIVE)
1761 #    m4_strip([  active <tab> <tab>active ])end
1762 #    => active activeend
1764 # First, notice that we guarantee trailing space.  Why?  Because regular
1765 # expressions are greedy, and `.* ?' would alway groups the space into the
1766 # .* portion.  The algorithm is simpler by avoiding `?' at the end.  The
1767 # algorithm correctly strips everything if STRING is just ` '.
1769 # Then notice the second pattern: it is in charge of removing the
1770 # leading/trailing spaces.  Why not just `[^ ]'?  Because they are
1771 # applied to over-quoted strings, i.e. more or less [STRING], due
1772 # to the limitations of m4_bpatsubsts.  So the leading space in STRING
1773 # is the *second* character; equally for the trailing space.
1774 m4_define([m4_strip],
1775 [m4_bpatsubsts([$1 ],
1776                [[        ]+], [ ],
1777                [^. ?\(.*\) .$], [[[\1]]])])
1780 # m4_normalize(STRING)
1781 # --------------------
1782 # Apply m4_flatten and m4_strip to STRING.
1784 # The argument is quoted, so that the macro is robust to active symbols:
1786 #    m4_define(active, ACTIVE)
1787 #    m4_normalize([  act\
1788 #    ive
1789 #    active ])end
1790 #    => active activeend
1792 m4_define([m4_normalize],
1793 [m4_strip(m4_flatten([$1]))])
1797 # m4_join(SEP, ARG1, ARG2...)
1798 # ---------------------------
1799 # Produce ARG1SEPARG2...SEPARGn.  Avoid back-to-back SEP when a given ARG
1800 # is the empty string.  No expansion is performed on SEP or ARGs.
1802 # Since the number of arguments to join can be arbitrarily long, we
1803 # want to avoid having more than one $@ in the macro definition;
1804 # otherwise, the expansion would require twice the memory of the already
1805 # long list.  Hence, m4_join merely looks for the first non-empty element,
1806 # and outputs just that element; while _m4_join looks for all non-empty
1807 # elements, and outputs them following a separator.  The final trick to
1808 # note is that we decide between recursing with $0 or _$0 based on the
1809 # nested m4_if ending with `_'.
1810 m4_define([m4_join],
1811 [m4_if([$#], [1], [],
1812        [$#], [2], [[$2]],
1813        [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift2($@))])])
1814 m4_define([_m4_join],
1815 [m4_if([$#$2], [2], [],
1816        [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift2($@))])])
1819 # m4_combine([SEPARATOR], PREFIX-LIST, [INFIX], SUFFIX...)
1820 # --------------------------------------------------------
1821 # Produce the pairwise combination of every element in the quoted,
1822 # comma-separated PREFIX-LIST with every element from the SUFFIX arguments.
1823 # Each pair is joined with INFIX, and pairs are separated by SEPARATOR.
1824 # No expansion occurs on SEPARATOR, INFIX, or elements of either list.
1826 # For example:
1827 #   m4_combine([, ], [[a], [b], [c]], [-], [1], [2], [3])
1828 #   => a-1, a-2, a-3, b-1, b-2, b-3, c-1, c-2, c-3
1830 # In order to have the correct number of SEPARATORs, we use a temporary
1831 # variable that redefines itself after the first use.  Note that since there
1832 # is no user expansion, we can avoid m4_defn overhead by overquoting the
1833 # second definition of m4_Separator, and by using m4_builtin.  Likewise,
1834 # we compute the m4_shift3 only once, rather than in each iteration of the
1835 # outer m4_foreach.
1836 m4_define([m4_combine],
1837 [m4_if(m4_eval([$# > 3]), [1],
1838        [m4_pushdef([m4_Separator], [m4_define([m4_Separator], [[$1]])])]]dnl
1839 [[m4_foreach([m4_Prefix], [$2],
1840              [m4_foreach([m4_Suffix], ]m4_dquote(m4_dquote(m4_shift3($@)))[,
1841                          [m4_Separator[]m4_builtin([defn],
1842                                         [m4_Prefix])[$3]m4_builtin([defn],
1843                                                         [m4_Suffix])])])]]dnl
1844 [[m4_builtin([popdef], [m4_Separator])])])
1847 # m4_append(MACRO-NAME, STRING, [SEPARATOR])
1848 # ------------------------------------------
1849 # Redefine MACRO-NAME to hold its former content plus `SEPARATOR`'STRING'
1850 # at the end.  It is valid to use this macro with MACRO-NAME undefined,
1851 # in which case no SEPARATOR is added.  Be aware that the criterion is
1852 # `not being defined', and not `not being empty'.
1854 # Note that neither STRING nor SEPARATOR are expanded here; rather, when
1855 # you expand MACRO-NAME, they will be expanded at that point in time.
1857 # This macro is robust to active symbols.  It can be used to grow
1858 # strings.
1860 #    | m4_define(active, ACTIVE)dnl
1861 #    | m4_append([sentence], [This is an])dnl
1862 #    | m4_append([sentence], [ active ])dnl
1863 #    | m4_append([sentence], [symbol.])dnl
1864 #    | sentence
1865 #    | m4_undefine([active])dnl
1866 #    | sentence
1867 #    => This is an ACTIVE symbol.
1868 #    => This is an active symbol.
1870 # It can be used to define hooks.
1872 #    | m4_define(active, ACTIVE)dnl
1873 #    | m4_append([hooks], [m4_define([act1], [act2])])dnl
1874 #    | m4_append([hooks], [m4_define([act2], [active])])dnl
1875 #    | m4_undefine([active])dnl
1876 #    | act1
1877 #    | hooks
1878 #    | act1
1879 #    => act1
1880 #    =>
1881 #    => active
1883 # It can also be used to create lists, although this particular usage was
1884 # broken prior to autoconf 2.62.
1885 #    | m4_append([list], [one], [, ])dnl
1886 #    | m4_append([list], [two], [, ])dnl
1887 #    | m4_append([list], [three], [, ])dnl
1888 #    | list
1889 #    | m4_dquote(list)
1890 #    => one, two, three
1891 #    => [one],[two],[three]
1893 # Use m4_builtin to avoid overhead of m4_defn.
1894 m4_define([m4_append],
1895 [m4_define([$1],
1896            m4_ifdef([$1], [m4_builtin([defn], [$1])[$3]])[$2])])
1899 # m4_append_uniq(MACRO-NAME, STRING, [SEPARATOR], [IF-UNIQ], [IF-DUP])
1900 # --------------------------------------------------------------------
1901 # Like `m4_append', but append only if not yet present.  Additionally,
1902 # expand IF-UNIQ if STRING was appended, or IF-DUP if STRING was already
1903 # present.  Also, warn if SEPARATOR is not empty and occurs within STRING,
1904 # as the algorithm no longer guarantees uniqueness.
1905 m4_define([m4_append_uniq],
1906 [m4_ifval([$3], [m4_if(m4_index([$2], [$3]), [-1], [],
1907                        [m4_warn([syntax],
1908                                 [$0: `$2' contains `$3'])])])_$0($@)])
1909 m4_define([_m4_append_uniq],
1910 [m4_ifdef([$1],
1911           [m4_if(m4_index([$3]m4_builtin([defn], [$1])[$3], [$3$2$3]), [-1],
1912                  [m4_append([$1], [$2], [$3])$4], [$5])],
1913           [m4_append([$1], [$2], [$3])$4])])
1915 # m4_append_uniq_w(MACRO-NAME, STRINGS)
1916 # -------------------------------------
1917 # For each of the words in the whitespace separated list STRINGS, append
1918 # only the unique strings to the definition of MACRO-NAME.
1920 # Avoid overhead of m4_defn by using m4_builtin.
1921 m4_define([m4_append_uniq_w],
1922 [m4_foreach_w([m4_Word], [$2],
1923               [_m4_append_uniq([$1], m4_builtin([defn], [m4_Word]), [ ])])])
1926 # m4_text_wrap(STRING, [PREFIX], [FIRST-PREFIX], [WIDTH])
1927 # -------------------------------------------------------
1928 # Expands into STRING wrapped to hold in WIDTH columns (default = 79).
1929 # If PREFIX is given, each line is prefixed with it.  If FIRST-PREFIX is
1930 # specified, then the first line is prefixed with it.  As a special case,
1931 # if the length of FIRST-PREFIX is greater than that of PREFIX, then
1932 # FIRST-PREFIX will be left alone on the first line.
1934 # No expansion occurs on the contents STRING, PREFIX, or FIRST-PREFIX,
1935 # although quadrigraphs are correctly recognized.
1937 # Typical outputs are:
1939 # m4_text_wrap([Short string */], [   ], [/* ], 20)
1940 #  => /* Short string */
1942 # m4_text_wrap([Much longer string */], [   ], [/* ], 20)
1943 #  => /* Much longer
1944 #  =>    string */
1946 # m4_text_wrap([Short doc.], [          ], [  --short ], 30)
1947 #  =>   --short Short doc.
1949 # m4_text_wrap([Short doc.], [          ], [  --too-wide ], 30)
1950 #  =>   --too-wide
1951 #  =>           Short doc.
1953 # m4_text_wrap([Super long documentation.], [          ], [  --too-wide ], 30)
1954 #  =>   --too-wide
1955 #  =>      Super long
1956 #  =>      documentation.
1958 # FIXME: there is no checking of a longer PREFIX than WIDTH, but do
1959 # we really want to bother with people trying each single corner
1960 # of a software?
1962 # This macro does not leave a trailing space behind the last word of a line,
1963 # which complicates it a bit.  The algorithm is otherwise stupid and simple:
1964 # all the words are preceded by m4_Separator which is defined to empty for
1965 # the first word, and then ` ' (single space) for all the others.
1967 # The algorithm overquotes m4_Prefix and m4_Prefix1 to avoid m4_defn
1968 # overhead, and bypasses m4_popdef overhead with m4_builtin since no user
1969 # macro expansion occurs in the meantime.  Also, the definition is written
1970 # with m4_do, to avoid time wasted on dnl during expansion (since this is
1971 # already a time-consuming macro).
1972 m4_define([m4_text_wrap],
1973 m4_do(dnl set up local variables, to avoid repeated calculations
1974 [[m4_pushdef([m4_Prefix], [[$2]])]],
1975 [[m4_pushdef([m4_Prefix1], m4_if([$3], [], [m4_Prefix], [[[$3]]]))]],
1976 [[m4_pushdef([m4_Width], m4_default([$4], 79))]],
1977 [[m4_pushdef([m4_Indent], m4_qlen(m4_Prefix))]],
1978 [[m4_pushdef([m4_Cursor], m4_qlen(m4_Prefix1))]],
1979 [[m4_pushdef([m4_Separator], [m4_define([m4_Separator], [ ])])]],
1980 dnl expand the first prefix, then check its length vs. regular prefix
1981 dnl same length: nothing special
1982 dnl prefix1 longer: output on line by itself, and reset cursor
1983 dnl prefix1 shorter: pad to length of prefix, and reset cursor
1984 [[m4_Prefix1[]m4_cond([m4_Cursor], m4_Indent, [],
1985                       [m4_eval(m4_Cursor > m4_Indent)], [1], [
1986 m4_Prefix[]m4_define([m4_Cursor], m4_Indent)],
1987                       [m4_format([%*s], m4_max([0],
1988   m4_eval(m4_Indent - m4_Cursor)), [])m4_define([m4_Cursor], m4_Indent)])]],
1989 dnl now, for each word, compute the curser after the word is output, then
1990 dnl check if the cursor would exceed the wrap column
1991 dnl if so, reset cursor, and insert newline and prefix
1992 dnl if not, insert the separator (usually a space)
1993 dnl either way, insert the word
1994 [[m4_foreach_w([m4_Word], [$1],
1995   [m4_define([m4_Cursor],
1996              m4_eval(m4_Cursor + m4_qlen(m4_builtin([defn], [m4_Word]))
1997                      + 1))m4_if(m4_eval(m4_Cursor > m4_Width),
1998       [1], [m4_define([m4_Cursor],
1999                       m4_eval(m4_Indent
2000                               + m4_qlen(m4_builtin([defn], [m4_Word])) + 1))
2001 m4_Prefix[]],
2002       [m4_Separator[]])m4_builtin([defn], [m4_Word])])]],
2003 dnl finally, clean up the local variabls
2004 [[m4_builtin([popdef], [m4_Separator])]],
2005 [[m4_builtin([popdef], [m4_Cursor])]],
2006 [[m4_builtin([popdef], [m4_Indent])]],
2007 [[m4_builtin([popdef], [m4_Width])]],
2008 [[m4_builtin([popdef], [m4_Prefix1])]],
2009 [[m4_builtin([popdef], [m4_Prefix])]]))
2012 # m4_text_box(MESSAGE, [FRAME-CHARACTER = `-'])
2013 # ---------------------------------------------
2014 # Turn MESSAGE into:
2015 #  ## ------- ##
2016 #  ## MESSAGE ##
2017 #  ## ------- ##
2018 # using FRAME-CHARACTER in the border.
2019 m4_define([m4_text_box],
2020 [m4_pushdef([m4_Border],
2021             m4_translit(m4_format([%*s], m4_qlen(m4_expand([$1])), []),
2022                         [ ], m4_if([$2], [], [[-]], [[$2]])))dnl
2023 @%:@@%:@ m4_Border @%:@@%:@
2024 @%:@@%:@ $1 @%:@@%:@
2025 @%:@@%:@ m4_Border @%:@@%:@dnl
2026 m4_builtin([popdef], [m4_Border])dnl
2030 # m4_qlen(STRING)
2031 # ---------------
2032 # Expands to the length of STRING after autom4te converts all quadrigraphs.
2034 # Avoid bpatsubsts for the common case of no quadrigraphs.
2035 m4_define([m4_qlen],
2036 [m4_if(m4_index([$1], [@]), [-1], [m4_len([$1])],
2037        [m4_len(m4_bpatsubsts([[$1]], [@\(<:\|:>\|S|\|%:\)@], [P], [@&t@]))])])
2040 # m4_qdelta(STRING)
2041 # -----------------
2042 # Expands to the net change in the length of STRING from autom4te converting the
2043 # quadrigraphs in STRING.  This number is always negative or zero.
2044 m4_define([m4_qdelta],
2045 [m4_eval(m4_qlen([$1]) - m4_len([$1]))])
2049 ## ----------------------- ##
2050 ## 13. Number processing.  ##
2051 ## ----------------------- ##
2053 # m4_cmp(A, B)
2054 # ------------
2055 # Compare two integer expressions.
2056 # A < B -> -1
2057 # A = B ->  0
2058 # A > B ->  1
2059 m4_define([m4_cmp],
2060 [m4_eval((([$1]) > ([$2])) - (([$1]) < ([$2])))])
2063 # m4_list_cmp(A, B)
2064 # -----------------
2066 # Compare the two lists of integer expressions A and B.  For instance:
2067 #   m4_list_cmp([1, 0],     [1])    ->  0
2068 #   m4_list_cmp([1, 0],     [1, 0]) ->  0
2069 #   m4_list_cmp([1, 2],     [1, 0]) ->  1
2070 #   m4_list_cmp([1, 2, 3],  [1, 2]) ->  1
2071 #   m4_list_cmp([1, 2, -3], [1, 2]) -> -1
2072 #   m4_list_cmp([1, 0],     [1, 2]) -> -1
2073 #   m4_list_cmp([1],        [1, 2]) -> -1
2074 #   m4_define([xa], [oops])dnl
2075 #   m4_list_cmp([[0xa]],    [5+5])  -> 0
2077 # Rather than face the overhead of m4_case, we use a helper function whose
2078 # expansion includes the name of the macro to invoke on the tail, either
2079 # m4_ignore or m4_unquote.  This is particularly useful when comparing
2080 # long lists, since less text is being expanded to determine when to recurse.
2081 m4_define([m4_list_cmp],
2082 [m4_if([$1$2], [], 0,
2083        [$1], [], [$0(0, [$2])],
2084        [$2], [], [$0([$1], 0)],
2085        [$1], [$2], 0,
2086        [_$0(m4_cmp(m4_car($1), m4_car($2)))([$0(m4_cdr($1), m4_cdr($2))])])])
2087 m4_define([_m4_list_cmp],
2088 [m4_if([$1], 0, [m4_unquote], [$1m4_ignore])])
2090 # m4_max(EXPR, ...)
2091 # m4_min(EXPR, ...)
2092 # -----------------
2093 # Return the decimal value of the maximum (or minimum) in a series of
2094 # integer expressions.
2096 # M4 1.4.x doesn't provide ?:.  Hence this huge m4_eval.  Avoid m4_eval
2097 # if both arguments are identical, but be aware of m4_max(0xa, 10) (hence
2098 # the use of <=, not just <, in the second multiply).
2099 m4_define([m4_max],
2100 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
2101        [$#], [1], [m4_eval([$1])],
2102        [$#$1], [2$2], [m4_eval([$1])],
2103        [$#], [2],
2104        [m4_eval((([$1]) > ([$2])) * ([$1]) + (([$1]) <= ([$2])) * ([$2]))],
2105        [$0($0([$1], [$2]), m4_shift2($@))])])
2106 m4_define([m4_min],
2107 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
2108        [$#], [1], [m4_eval([$1])],
2109        [$#$1], [2$2], [m4_eval([$1])],
2110        [$#], [2],
2111        [m4_eval((([$1]) < ([$2])) * ([$1]) + (([$1]) >= ([$2])) * ([$2]))],
2112        [$0($0([$1], [$2]), m4_shift2($@))])])
2115 # m4_sign(A)
2116 # ----------
2117 # The sign of the integer expression A.
2118 m4_define([m4_sign],
2119 [m4_eval((([$1]) > 0) - (([$1]) < 0))])
2123 ## ------------------------ ##
2124 ## 14. Version processing.  ##
2125 ## ------------------------ ##
2128 # m4_version_unletter(VERSION)
2129 # ----------------------------
2130 # Normalize beta version numbers with letters to numeric expressions, which
2131 # can then be handed to m4_eval for the purpose of comparison.
2133 #   Nl -> (N+1).-1.(l#)
2135 # for example:
2136 #   [2.14a] -> [2.14+1.-1.[0r36:a]] -> 2.15.-1.10
2137 #   [2.14b] -> [2.15+1.-1.[0r36:b]] -> 2.15.-1.11
2138 #   [2.61aa.b] -> [2.61+1.-1.[0r36:aa],+1.-1.[0r36:b]] -> 2.62.-1.370.1.-1.11
2140 # This macro expects reasonable version numbers, but can handle double
2141 # letters and does not expand one-letter macros.  Inline constant expansions,
2142 # to avoid m4_defn overhead.  _m4_version_unletter is the real workhorse
2143 # used by m4_version_compare, but since [0r36:a] is less readable than 10,
2144 # we provide a wrapper for human use.
2145 m4_define([m4_version_unletter],
2146 [m4_map_sep([m4_eval], [.],
2147             m4_dquote(m4_dquote_elt(m4_unquote(_$0([$1])))))])
2148 m4_define([_m4_version_unletter],
2149 [m4_translit(m4_bpatsubst([[[$1]]], ]dnl
2150 m4_dquote(m4_dquote(m4_defn([m4_cr_Letters])))[[+],
2151                           [+1.-1.[0r36:\&]]),
2152              [.], [,])])
2155 # m4_version_compare(VERSION-1, VERSION-2)
2156 # ----------------------------------------
2157 # Compare the two version numbers and expand into
2158 #  -1 if VERSION-1 < VERSION-2
2159 #   0 if           =
2160 #   1 if           >
2161 m4_define([m4_version_compare],
2162 [m4_list_cmp(_m4_version_unletter([$1]), _m4_version_unletter([$2]))])
2165 # m4_PACKAGE_NAME
2166 # m4_PACKAGE_TARNAME
2167 # m4_PACKAGE_VERSION
2168 # m4_PACKAGE_STRING
2169 # m4_PACKAGE_BUGREPORT
2170 # --------------------
2171 m4_include([m4sugar/version.m4])
2174 # m4_version_prereq(VERSION, [IF-OK], [IF-NOT = FAIL])
2175 # ----------------------------------------------------
2176 # Check this Autoconf version against VERSION.
2177 m4_define([m4_version_prereq],
2178 [m4_if(m4_version_compare(]m4_dquote(m4_defn([m4_PACKAGE_VERSION]))[, [$1]),
2179        [-1],
2180        [m4_default([$3],
2181                    [m4_fatal([Autoconf version $1 or higher is required],
2182                              [63])])],
2183        [$2])])
2187 ## ------------------- ##
2188 ## 15. File handling.  ##
2189 ## ------------------- ##
2192 # It is a real pity that M4 comes with no macros to bind a diversion
2193 # to a file.  So we have to deal without, which makes us a lot more
2194 # fragile than we should.
2197 # m4_file_append(FILE-NAME, CONTENT)
2198 # ----------------------------------
2199 m4_define([m4_file_append],
2200 [m4_syscmd([cat >>$1 <<_m4eof
2202 _m4eof
2204 m4_if(m4_sysval, [0], [],
2205       [m4_fatal([$0: cannot write: $1])])])
2209 ## ------------------------ ##
2210 ## 16. Setting M4sugar up.  ##
2211 ## ------------------------ ##
2214 # m4_init
2215 # -------
2216 m4_define([m4_init],
2217 [# All the M4sugar macros start with `m4_', except `dnl' kept as is
2218 # for sake of simplicity.
2219 m4_pattern_forbid([^_?m4_])
2220 m4_pattern_forbid([^dnl$])
2222 # _m4_divert_diversion should be defined:
2223 m4_divert_push([KILL])
2225 # Check the divert push/pop perfect balance.
2226 m4_wrap([m4_divert_pop([])
2227          m4_ifdef([_m4_divert_diversion],
2228            [m4_fatal([$0: unbalanced m4_divert_push:]_m4_divert_n_stack)])[]])