Support unbalanced () in AT_SETUP by adding two new quadrigraphs.
[autoconf.git] / lib / m4sugar / m4sugar.m4
blob841ce727343b1cc0b507962ebb81f9e8a0f8798a
1 divert(-1)#                                                  -*- Autoconf -*-
2 # This file is part of Autoconf.
3 # Base M4 layer.
4 # Requires GNU M4.
6 # Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7 # 2008 Free Software Foundation, Inc.
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2, or (at your option)
12 # any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 # 02110-1301, USA.
24 # As a special exception, the Free Software Foundation gives unlimited
25 # permission to copy, distribute and modify the configure scripts that
26 # are the output of Autoconf.  You need not follow the terms of the GNU
27 # General Public License when using or distributing such scripts, even
28 # though portions of the text of Autoconf appear in them.  The GNU
29 # General Public License (GPL) does govern all other use of the material
30 # that constitutes the Autoconf program.
32 # Certain portions of the Autoconf source text are designed to be copied
33 # (in certain cases, depending on the input) into the output of
34 # Autoconf.  We call these the "data" portions.  The rest of the Autoconf
35 # source text consists of comments plus executable code that decides which
36 # of the data portions to output in any given case.  We call these
37 # comments and executable code the "non-data" portions.  Autoconf never
38 # copies any of the non-data portions into its output.
40 # This special exception to the GPL applies to versions of Autoconf
41 # released by the Free Software Foundation.  When you make and
42 # distribute a modified version of Autoconf, you may extend this special
43 # exception to the GPL to apply to your modified version as well, *unless*
44 # your modified version has the potential to copy into its output some
45 # of the text that was the non-data portion of the version that you started
46 # with.  (In other words, unless your change moves or copies text from
47 # the non-data portions to the data portions.)  If your modification has
48 # such potential, you must delete any notice of this special exception
49 # to the GPL from your modified version.
51 # Written by Akim Demaille.
54 # Set the quotes, whatever the current quoting system.
55 changequote()
56 changequote([, ])
58 # Some old m4's don't support m4exit.  But they provide
59 # equivalent functionality by core dumping because of the
60 # long macros we define.
61 ifdef([__gnu__], ,
62 [errprint(M4sugar requires GNU M4. Install it before installing M4sugar or
63 set the M4 environment variable to its absolute file name.)
64 m4exit(2)])
67 ## ------------------------------- ##
68 ## 1. Simulate --prefix-builtins.  ##
69 ## ------------------------------- ##
71 # m4_define
72 # m4_defn
73 # m4_undefine
74 define([m4_define],   defn([define]))
75 define([m4_defn],     defn([defn]))
76 define([m4_undefine], defn([undefine]))
78 m4_undefine([define])
79 m4_undefine([defn])
80 m4_undefine([undefine])
83 # m4_copy(SRC, DST)
84 # -----------------
85 # Define DST as the definition of SRC.
86 # What's the difference between:
87 # 1. m4_copy([from], [to])
88 # 2. m4_define([to], [from($@)])
89 # Well, obviously 1 is more expensive in space.  Maybe 2 is more expensive
90 # in time, but because of the space cost of 1, it's not that obvious.
91 # Nevertheless, one huge difference is the handling of `$0'.  If `from'
92 # uses `$0', then with 1, `to''s `$0' is `to', while it is `from' in 2.
93 # The user would certainly prefer to see `to'.
94 m4_define([m4_copy],
95 [m4_define([$2], m4_defn([$1]))])
98 # m4_rename(SRC, DST)
99 # -------------------
100 # Rename the macro SRC to DST.
101 m4_define([m4_rename],
102 [m4_copy([$1], [$2])m4_undefine([$1])])
105 # m4_rename_m4(MACRO-NAME)
106 # ------------------------
107 # Rename MACRO-NAME to m4_MACRO-NAME.
108 m4_define([m4_rename_m4],
109 [m4_rename([$1], [m4_$1])])
112 # m4_copy_unm4(m4_MACRO-NAME)
113 # ---------------------------
114 # Copy m4_MACRO-NAME to MACRO-NAME.
115 m4_define([m4_copy_unm4],
116 [m4_copy([$1], m4_bpatsubst([$1], [^m4_\(.*\)], [[\1]]))])
119 # Some m4 internals have names colliding with tokens we might use.
120 # Rename them a` la `m4 --prefix-builtins'.
121 m4_rename_m4([builtin])
122 m4_rename_m4([changecom])
123 m4_rename_m4([changequote])
124 m4_undefine([changeword])
125 m4_rename_m4([debugfile])
126 m4_rename_m4([debugmode])
127 m4_rename_m4([decr])
128 m4_undefine([divert])
129 m4_rename_m4([divnum])
130 m4_rename_m4([dumpdef])
131 m4_rename_m4([errprint])
132 m4_rename_m4([esyscmd])
133 m4_rename_m4([eval])
134 m4_rename_m4([format])
135 m4_rename_m4([ifdef])
136 m4_rename([ifelse], [m4_if])
137 m4_undefine([include])
138 m4_rename_m4([incr])
139 m4_rename_m4([index])
140 m4_rename_m4([indir])
141 m4_rename_m4([len])
142 m4_rename([m4exit], [m4_exit])
143 m4_undefine([m4wrap])
144 m4_ifdef([mkstemp],dnl added in M4 1.4.8
145 [m4_rename_m4([mkstemp])
146 m4_copy([m4_mkstemp], [m4_maketemp])
147 m4_undefine([maketemp])],
148 [m4_rename_m4([maketemp])
149 m4_copy([m4_maketemp], [m4_mkstemp])])
150 m4_rename([patsubst], [m4_bpatsubst])
151 m4_undefine([popdef])
152 m4_rename_m4([pushdef])
153 m4_rename([regexp], [m4_bregexp])
154 m4_rename_m4([shift])
155 m4_undefine([sinclude])
156 m4_rename_m4([substr])
157 m4_rename_m4([symbols])
158 m4_rename_m4([syscmd])
159 m4_rename_m4([sysval])
160 m4_rename_m4([traceoff])
161 m4_rename_m4([traceon])
162 m4_rename_m4([translit])
163 m4_undefine([undivert])
166 ## ------------------- ##
167 ## 2. Error messages.  ##
168 ## ------------------- ##
171 # m4_location
172 # -----------
173 m4_define([m4_location],
174 [__file__:__line__])
177 # m4_errprintn(MSG)
178 # -----------------
179 # Same as `errprint', but with the missing end of line.
180 m4_define([m4_errprintn],
181 [m4_errprint([$1
182 ])])
185 # m4_warning(MSG)
186 # ---------------
187 # Warn the user.
188 m4_define([m4_warning],
189 [m4_errprintn(m4_location[: warning: $1])])
192 # m4_fatal(MSG, [EXIT-STATUS])
193 # ----------------------------
194 # Fatal the user.                                                      :)
195 m4_define([m4_fatal],
196 [m4_errprintn(m4_location[: error: $1])dnl
197 m4_expansion_stack_dump()dnl
198 m4_exit(m4_if([$2],, 1, [$2]))])
201 # m4_assert(EXPRESSION, [EXIT-STATUS = 1])
202 # ----------------------------------------
203 # This macro ensures that EXPRESSION evaluates to true, and exits if
204 # EXPRESSION evaluates to false.
205 m4_define([m4_assert],
206 [m4_if(m4_eval([$1]), 0,
207        [m4_fatal([assert failed: $1], [$2])])])
211 ## ------------- ##
212 ## 3. Warnings.  ##
213 ## ------------- ##
216 # _m4_warn(CATEGORY, MESSAGE, STACK-TRACE)
217 # ----------------------------------------
218 # Report a MESSAGE to the user if the CATEGORY of warnings is enabled.
219 # This is for traces only.
220 # The STACK-TRACE is a \n-separated list of "LOCATION: MESSAGE".
222 # Within m4, the macro is a no-op.  This macro really matters
223 # when autom4te post-processes the trace output.
224 m4_define([_m4_warn], [])
227 # m4_warn(CATEGORY, MESSAGE)
228 # --------------------------
229 # Report a MESSAGE to the user if the CATEGORY of warnings is enabled.
230 m4_define([m4_warn],
231 [_m4_warn([$1], [$2],
232 m4_ifdef([m4_expansion_stack],
233          [m4_defn([m4_expansion_stack])
234 m4_location[: the top level]]))dnl
239 ## ------------------- ##
240 ## 4. File inclusion.  ##
241 ## ------------------- ##
244 # We also want to neutralize include (and sinclude for symmetry),
245 # but we want to extend them slightly: warn when a file is included
246 # several times.  This is, in general, a dangerous operation, because
247 # too many people forget to quote the first argument of m4_define.
249 # For instance in the following case:
250 #   m4_define(foo, [bar])
251 # then a second reading will turn into
252 #   m4_define(bar, [bar])
253 # which is certainly not what was meant.
255 # m4_include_unique(FILE)
256 # -----------------------
257 # Declare that the FILE was loading; and warn if it has already
258 # been included.
259 m4_define([m4_include_unique],
260 [m4_ifdef([m4_include($1)],
261           [m4_warn([syntax], [file `$1' included several times])])dnl
262 m4_define([m4_include($1)])])
265 # m4_include(FILE)
266 # ----------------
267 # Like the builtin include, but warns against multiple inclusions.
268 m4_define([m4_include],
269 [m4_include_unique([$1])dnl
270 m4_builtin([include], [$1])])
273 # m4_sinclude(FILE)
274 # -----------------
275 # Like the builtin sinclude, but warns against multiple inclusions.
276 m4_define([m4_sinclude],
277 [m4_include_unique([$1])dnl
278 m4_builtin([sinclude], [$1])])
282 ## ------------------------------------ ##
283 ## 5. Additional branching constructs.  ##
284 ## ------------------------------------ ##
286 # Both `m4_ifval' and `m4_ifset' tests against the empty string.  The
287 # difference is that `m4_ifset' is specialized on macros.
289 # In case of arguments of macros, eg. $1, it makes little difference.
290 # In the case of a macro `FOO', you don't want to check `m4_ifval(FOO,
291 # TRUE)', because if `FOO' expands with commas, there is a shifting of
292 # the arguments.  So you want to run `m4_ifval([FOO])', but then you just
293 # compare the *string* `FOO' against `', which, of course fails.
295 # So you want the variation `m4_ifset' that expects a macro name as $1.
296 # If this macro is both defined and defined to a non empty value, then
297 # it runs TRUE, etc.
300 # m4_ifval(COND, [IF-TRUE], [IF-FALSE])
301 # -------------------------------------
302 # If COND is not the empty string, expand IF-TRUE, otherwise IF-FALSE.
303 # Comparable to m4_ifdef.
304 m4_define([m4_ifval],
305 [m4_if([$1], [], [$3], [$2])])
308 # m4_n(TEXT)
309 # ----------
310 # If TEXT is not empty, return TEXT and a new line, otherwise nothing.
311 m4_define([m4_n],
312 [m4_if([$1],
313        [], [],
314            [$1
315 ])])
318 # m4_ifvaln(COND, [IF-TRUE], [IF-FALSE])
319 # --------------------------------------
320 # Same as `m4_ifval', but add an extra newline to IF-TRUE or IF-FALSE
321 # unless that argument is empty.
322 m4_define([m4_ifvaln],
323 [m4_if([$1],
324        [],   [m4_n([$3])],
325              [m4_n([$2])])])
328 # m4_ifset(MACRO, [IF-TRUE], [IF-FALSE])
329 # --------------------------------------
330 # If MACRO has no definition, or of its definition is the empty string,
331 # expand IF-FALSE, otherwise IF-TRUE.
332 m4_define([m4_ifset],
333 [m4_ifdef([$1],
334           [m4_ifval(m4_defn([$1]), [$2], [$3])],
335           [$3])])
338 # m4_ifndef(NAME, [IF-NOT-DEFINED], [IF-DEFINED])
339 # -----------------------------------------------
340 m4_define([m4_ifndef],
341 [m4_ifdef([$1], [$3], [$2])])
344 # m4_case(SWITCH, VAL1, IF-VAL1, VAL2, IF-VAL2, ..., DEFAULT)
345 # -----------------------------------------------------------
346 # m4 equivalent of
347 # switch (SWITCH)
348 # {
349 #   case VAL1:
350 #     IF-VAL1;
351 #     break;
352 #   case VAL2:
353 #     IF-VAL2;
354 #     break;
355 #   ...
356 #   default:
357 #     DEFAULT;
358 #     break;
359 # }.
360 # All the values are optional, and the macro is robust to active
361 # symbols properly quoted.
362 m4_define([m4_case],
363 [m4_if([$#], 0, [],
364        [$#], 1, [],
365        [$#], 2, [$2],
366        [$1], [$2], [$3],
367        [$0([$1], m4_shift3($@))])])
370 # m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT)
371 # -----------------------------------------------------
372 # m4 equivalent of
374 # if (SWITCH =~ RE1)
375 #   VAL1;
376 # elif (SWITCH =~ RE2)
377 #   VAL2;
378 # elif ...
379 #   ...
380 # else
381 #   DEFAULT
383 # All the values are optional, and the macro is robust to active symbols
384 # properly quoted.
385 m4_define([m4_bmatch],
386 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
387        [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
388        [$#], 2, [$2],
389        [m4_if(m4_bregexp([$1], [$2]), -1, [$0([$1], m4_shift3($@))],
390               [$3])])])
393 # m4_car(LIST)
394 # m4_cdr(LIST)
395 # ------------
396 # Manipulate m4 lists.
397 m4_define([m4_car], [[$1]])
398 m4_define([m4_cdr],
399 [m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])],
400        [$#], 1, [],
401        [m4_dquote(m4_shift($@))])])
403 # _m4_cdr(LIST)
404 # -------------
405 # Like m4_cdr, except include a leading comma unless only one element
406 # remains.  Why?  Because comparing a large list against [] is more
407 # expensive in expansion time than comparing the number of arguments; so
408 # _m4_cdr can be used to reduce the number of arguments when it is time
409 # to end recursion.
410 m4_define([_m4_cdr],
411 [m4_if([$#], 1, [],
412        [, m4_dquote(m4_shift($@))])])
416 # m4_cond(TEST1, VAL1, IF-VAL1, TEST2, VAL2, IF-VAL2, ..., [DEFAULT])
417 # -------------------------------------------------------------------
418 # Similar to m4_if, except that each TEST is expanded when encountered.
419 # If the expansion of TESTn matches the string VALn, the result is IF-VALn.
420 # The result is DEFAULT if no tests passed.  This macro allows
421 # short-circuiting of expensive tests, where it pays to arrange quick
422 # filter tests to run first.
424 # For an example, consider a previous implementation of _AS_QUOTE_IFELSE:
426 #    m4_if(m4_index([$1], [\]), [-1], [$2],
427 #          m4_eval(m4_index([$1], [\\]) >= 0), [1], [$2],
428 #          m4_eval(m4_index([$1], [\$]) >= 0), [1], [$2],
429 #          m4_eval(m4_index([$1], [\`]) >= 0), [1], [$3],
430 #          m4_eval(m4_index([$1], [\"]) >= 0), [1], [$3],
431 #          [$2])
433 # Here, m4_index is computed 5 times, and m4_eval 4, even if $1 contains
434 # no backslash.  It is more efficient to do:
436 #    m4_cond([m4_index([$1], [\])], [-1], [$2],
437 #            [m4_eval(m4_index([$1], [\\]) >= 0)], [1], [$2],
438 #            [m4_eval(m4_index([$1], [\$]) >= 0)], [1], [$2],
439 #            [m4_eval(m4_index([$1], [\`]) >= 0)], [1], [$3],
440 #            [m4_eval(m4_index([$1], [\"]) >= 0)], [1], [$3],
441 #            [$2])
443 # In the common case of $1 with no backslash, only one m4_index expansion
444 # occurs, and m4_eval is avoided altogether.
445 m4_define([m4_cond],
446 [m4_if([$#], [0], [m4_fatal([$0: cannot be called without arguments])],
447        [$#], [1], [$1],
448        [$#], [2], [m4_fatal([$0: missing an argument])],
449        [m4_if($1, [$2], [$3], [$0(m4_shift3($@))])])])
452 ## ---------------------------------------- ##
453 ## 6. Enhanced version of some primitives.  ##
454 ## ---------------------------------------- ##
456 # m4_bpatsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...)
457 # ----------------------------------------------------
458 # m4 equivalent of
460 #   $_ = STRING;
461 #   s/RE1/SUBST1/g;
462 #   s/RE2/SUBST2/g;
463 #   ...
465 # All the values are optional, and the macro is robust to active symbols
466 # properly quoted.
468 # I would have liked to name this macro `m4_bpatsubst', unfortunately,
469 # due to quotation problems, I need to double quote $1 below, therefore
470 # the anchors are broken :(  I can't let users be trapped by that.
472 # Recall that m4_shift3 always results in an argument.  Hence, we need
473 # to distinguish between a final deletion vs. ending recursion.
474 m4_define([m4_bpatsubsts],
475 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
476        [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
477        [$#], 2, [m4_builtin([patsubst], [$1], [$2])],
478        [_$0($@m4_if(m4_eval($# & 1), 0, [,]))])])
479 m4_define([_m4_bpatsubsts],
480 [m4_if([$#], 2, [$1],
481        [$0(m4_builtin([patsubst], [[$1]], [$2], [$3]),
482            m4_shift3($@))])])
485 # m4_define_default(MACRO, VALUE)
486 # -------------------------------
487 # If MACRO is undefined, set it to VALUE.
488 m4_define([m4_define_default],
489 [m4_ifndef([$1], [m4_define($@)])])
492 # m4_default(EXP1, EXP2)
493 # ----------------------
494 # Returns EXP1 if non empty, otherwise EXP2.
496 # This macro is called on hot paths, so inline the contents of m4_ifval,
497 # for one less round of expansion.
498 m4_define([m4_default],
499 [m4_if([$1], [], [$2], [$1])])
502 # m4_defn(NAME)
503 # -------------
504 # Like the original, except don't tolerate popping something which is
505 # undefined, and only support one argument.
507 # This macro is called frequently, so minimize the amount of additional
508 # expansions by skipping m4_ifndef.
509 m4_define([m4_defn],
510 [m4_ifdef([$1], [],
511           [m4_fatal([$0: undefined macro: $1])])]dnl
512 [m4_builtin([defn], [$1])])
515 # _m4_dumpdefs_up(NAME)
516 # ---------------------
517 m4_define([_m4_dumpdefs_up],
518 [m4_ifdef([$1],
519           [m4_pushdef([_m4_dumpdefs], m4_defn([$1]))dnl
520 m4_dumpdef([$1])dnl
521 m4_popdef([$1])dnl
522 _m4_dumpdefs_up([$1])])])
525 # _m4_dumpdefs_down(NAME)
526 # -----------------------
527 m4_define([_m4_dumpdefs_down],
528 [m4_ifdef([_m4_dumpdefs],
529           [m4_pushdef([$1], m4_defn([_m4_dumpdefs]))dnl
530 m4_popdef([_m4_dumpdefs])dnl
531 _m4_dumpdefs_down([$1])])])
534 # m4_dumpdefs(NAME)
535 # -----------------
536 # Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its
537 # value stack (most recent displayed first).
538 m4_define([m4_dumpdefs],
539 [_m4_dumpdefs_up([$1])dnl
540 _m4_dumpdefs_down([$1])])
543 # m4_popdef(NAME)
544 # ---------------
545 # Like the original, except don't tolerate popping something which is
546 # undefined, and only support one argument.
548 # This macro is called frequently, so minimize the amount of additional
549 # expansions by skipping m4_ifndef.
550 m4_define([m4_popdef],
551 [m4_ifdef([$1], [],
552           [m4_fatal([$0: undefined macro: $1])])]dnl
553 [m4_builtin([popdef], [$1])])
556 # m4_shiftn(N, ...)
557 # -----------------
558 # Returns ... shifted N times.  Useful for recursive "varargs" constructs.
560 # Autoconf does not use this macro, because it is inherently slower than
561 # calling the common cases of m4_shift2 or m4_shift3 directly.  But it
562 # might as well be fast for other clients, such as Libtool.  One way to
563 # do this is to expand $@ only once in _m4_shiftn (otherwise, for long
564 # lists, the expansion of m4_if takes twice as much memory as what the
565 # list itself occupies, only to throw away the unused branch).  The end
566 # result is strictly equivalent to
567 #   m4_if([$1], 1, [m4_shift(,m4_shift(m4_shift($@)))],
568 #         [_m4_shiftn(m4_decr([$1]), m4_shift(m4_shift($@)))])
569 # but with the final `m4_shift(m4_shift($@)))' shared between the two
570 # paths.  The first leg uses a no-op m4_shift(,$@) to balance out the ().
571 m4_define([m4_shiftn],
572 [m4_assert(0 < $1 && $1 < $#)_$0($@)])
574 m4_define([_m4_shiftn],
575 [m4_if([$1], 1, [m4_shift(],
576        [$0(m4_decr([$1])]), m4_shift(m4_shift($@)))])
578 # m4_shift2(...)
579 # m4_shift3(...)
580 # -----------------
581 # Returns ... shifted twice, and three times.  Faster than m4_shiftn.
582 m4_define([m4_shift2], [m4_shift(m4_shift($@))])
583 m4_define([m4_shift3], [m4_shift(m4_shift(m4_shift($@)))])
585 # _m4_shift2(...)
586 # _m4_shift3(...)
587 # ---------------
588 # Like m4_shift2 or m4_shift3, except include a leading comma unless shifting
589 # consumes all arguments.  Why?  Because in recursion, it is nice to
590 # distinguish between 1 element left and 0 elements left, based on how many
591 # arguments this shift expands to.
592 m4_define([_m4_shift2],
593 [m4_if([$#], [2], [],
594        [, m4_shift(m4_shift($@))])])
595 m4_define([_m4_shift3],
596 [m4_if([$#], [3], [],
597        [, m4_shift(m4_shift(m4_shift($@)))])])
600 # m4_undefine(NAME)
601 # -----------------
602 # Like the original, except don't tolerate undefining something which is
603 # undefined, and only support one argument.
605 # This macro is called frequently, so minimize the amount of additional
606 # expansions by skipping m4_ifndef.
607 m4_define([m4_undefine],
608 [m4_ifdef([$1], [],
609           [m4_fatal([$0: undefined macro: $1])])]dnl
610 [m4_builtin([undefine], [$1])])
612 # _m4_wrap(PRE, POST)
613 # -------------------
614 # Helper macro for m4_wrap and m4_wrap_lifo.  Allows nested calls to
615 # m4_wrap within wrapped text.
616 # Skip m4_defn and m4_popdef for speed.
617 m4_define([_m4_wrap],
618 [m4_ifdef([$0_text],
619           [m4_define([$0_text], [$1]m4_builtin([defn], [$0_text])[$2])],
620           [m4_builtin([m4wrap], [m4_unquote(m4_builtin([defn],
621   [$0_text])m4_builtin([popdef], [$0_text]))])m4_define([$0_text], [$1$2])])])
623 # m4_wrap(TEXT)
624 # -------------
625 # Append TEXT to the list of hooks to be executed at the end of input.
626 # Whereas the order of the original may be LIFO in the underlying m4,
627 # this version is always FIFO.
628 m4_define([m4_wrap],
629 [_m4_wrap([], [$1[]])])
631 # m4_wrap_lifo(TEXT)
632 # ------------------
633 # Prepend TEXT to the list of hooks to be executed at the end of input.
634 # Whereas the order of m4_wrap may be FIFO in the underlying m4, this
635 # version is always LIFO.
636 m4_define([m4_wrap_lifo],
637 [_m4_wrap([$1[]])])
639 ## ------------------------- ##
640 ## 7. Quoting manipulation.  ##
641 ## ------------------------- ##
644 # m4_apply(MACRO, LIST)
645 # ---------------------
646 # Invoke MACRO, with arguments provided from the quoted list of
647 # comma-separated quoted arguments.  If LIST is empty, invoke MACRO
648 # without arguments.
649 m4_define([m4_apply],
650 [m4_if([$2], [], [$1], [$1($2)])[]])
653 # m4_count(ARGS)
654 # --------------
655 # Return a count of how many ARGS are present.
656 m4_define([m4_count], [$#])
659 # m4_do(STRING, ...)
660 # ------------------
661 # This macro invokes all its arguments (in sequence, of course).  It is
662 # useful for making your macros more structured and readable by dropping
663 # unnecessary dnl's and have the macros indented properly.
664 m4_define([m4_do],
665 [m4_if([$#], 0, [],
666        [$#], 1, [$1],
667        [$1[]m4_do(m4_shift($@))])])
670 # m4_dquote(ARGS)
671 # ---------------
672 # Return ARGS as a quoted list of quoted arguments.
673 m4_define([m4_dquote],  [[$@]])
676 # m4_dquote_elt(ARGS)
677 # -------------------
678 # Return ARGS as an unquoted list of double-quoted arguments.
679 m4_define([m4_dquote_elt],
680 [m4_if([$#], [0], [],
681        [$#], [1], [[[$1]]],
682        [[[$1]],$0(m4_shift($@))])])
685 # m4_echo(ARGS)
686 # -------------
687 # Return the ARGS, with the same level of quoting.  Whitespace after
688 # unquoted commas are consumed.
689 m4_define([m4_echo], [$@])
692 # m4_expand(ARG)
693 # --------------
694 # Return the expansion of ARG as a single string.  Unlike m4_quote($1), this
695 # correctly preserves whitespace following single-quoted commas that appeared
696 # within ARG.
698 #   m4_define([active], [ACT, IVE])
699 #   m4_define([active2], [[ACT, IVE]])
700 #   m4_quote(active, active2)
701 #   => ACT,IVE,ACT, IVE
702 #   m4_expand([active, active2])
703 #   => ACT, IVE, ACT, IVE
705 # Unfortunately, due to limitations in m4, ARG must contain balanced quotes
706 # (use quadrigraphs) and balanced parentheses (use creative shell comments
707 # when writing shell case statements).
709 # Exploit that extra () will group unquoted commas and the following
710 # whitespace, then convert () to [].  m4_bpatsubst can't handle newlines
711 # inside $1, and m4_substr strips quoting.  So we (ab)use m4_changequote.
712 m4_define([m4_expand], [_$0(($1))])
713 m4_define([_m4_expand],
714 [m4_changequote([(], [)])$1m4_changequote`'m4_changequote(`[', `]')])
717 # m4_ignore(ARGS)
718 # ---------------
719 # Expands to nothing.  Useful for conditionally ignoring an arbitrary
720 # number of arguments (see _m4_list_cmp for an example).
721 m4_define([m4_ignore])
724 # m4_make_list(ARGS)
725 # ------------------
726 # Similar to m4_dquote, this creates a quoted list of quoted ARGS.  This
727 # version is less efficient than m4_dquote, but separates each argument
728 # with a comma and newline, rather than just comma, for readability.
729 # When developing an m4sugar algorithm, you could temporarily use
730 #   m4_pushdef([m4_dquote],m4_defn([m4_make_list]))
731 # around your code to make debugging easier.
732 m4_define([m4_make_list], [m4_join([,
733 ], m4_dquote_elt($@))])
736 # m4_noquote(STRING)
737 # ------------------
738 # Return the result of ignoring all quotes in STRING and invoking the
739 # macros it contains.  Amongst other things, this is useful for enabling
740 # macro invocations inside strings with [] blocks (for instance regexps
741 # and help-strings).  On the other hand, since all quotes are disabled,
742 # any macro expanded during this time that relies on nested [] quoting
743 # will likely crash and burn.  This macro is seldom useful; consider
744 # m4_unquote instead.
745 m4_define([m4_noquote],
746 [m4_changequote(-=<{,}>=-)$1-=<{}>=-m4_changequote([,])])
749 # m4_quote(ARGS)
750 # --------------
751 # Return ARGS as a single argument.  Any whitespace after unquoted commas
752 # is stripped.  There is always output, even when there were no arguments.
754 # It is important to realize the difference between `m4_quote(exp)' and
755 # `[exp]': in the first case you obtain the quoted *result* of the
756 # expansion of EXP, while in the latter you just obtain the string
757 # `exp'.
758 m4_define([m4_quote],  [[$*]])
761 # _m4_quote(ARGS)
762 # ---------------
763 # Like m4_quote, except that when there are no arguments, there is no
764 # output.  For conditional scenarios (such as passing _m4_quote as the
765 # macro name in m4_map), this feature can be used to distinguish between
766 # one argument of the empty string vs. no arguments.  However, in the
767 # normal case with arguments present, this is less efficient than m4_quote.
768 m4_define([_m4_quote],
769 [m4_if([$#], [0], [], [[$*]])])
772 # m4_unquote(ARGS)
773 # ----------------
774 # Remove one layer of quotes from each ARG, performing one level of
775 # expansion.  For one argument, m4_unquote([arg]) is more efficient than
776 # m4_do([arg]), but for multiple arguments, the difference is that
777 # m4_unquote separates arguments with commas while m4_do concatenates.
778 m4_define([m4_unquote], [$*])
781 ## -------------------------- ##
782 ## 8. Implementing m4 loops.  ##
783 ## -------------------------- ##
786 # m4_for(VARIABLE, FIRST, LAST, [STEP = +/-1], EXPRESSION)
787 # --------------------------------------------------------
788 # Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO with
789 # increments of STEP.
790 # Both limits are included, and bounds are checked for consistency.
791 # The algorithm is robust to indirect VARIABLE names, and uses m4_builtin
792 # to avoid some of the m4_defn overhead.
793 m4_define([m4_for],
794 [m4_pushdef([$1], m4_eval([$2]))dnl
795 m4_cond([m4_eval(([$3]) > m4_builtin([defn], [$1]))], 1,
796 [m4_pushdef([_m4_step], m4_eval(m4_default([$4], 1)))dnl
797 m4_assert(_m4_step > 0)dnl
798 _m4_for([$1], m4_eval((([$3]) - m4_builtin([defn], [$1]))
799                       / _m4_step * _m4_step + m4_builtin([defn], [$1])),
800         _m4_step, [$5])],
801         [m4_eval(([$3]) < m4_builtin([defn], [$1]))], 1,
802 [m4_pushdef([_m4_step], m4_eval(m4_default([$4], -1)))dnl
803 m4_assert(_m4_step < 0)dnl
804 _m4_for([$1], m4_eval((m4_builtin([defn], [$1]) - ([$3]))
805                       / -(_m4_step) * _m4_step + m4_builtin([defn], [$1])),
806         _m4_step, [$5])],
807         [m4_pushdef([_m4_step])dnl
808 $5])[]dnl
809 m4_popdef([_m4_step])dnl
810 m4_popdef([$1])])
813 # _m4_for(VARIABLE, LAST, STEP, EXPRESSION)
814 # -----------------------------------------
815 # Core of the loop, no consistency checks, all arguments are plain numbers.
816 m4_define([_m4_for],
817 [$4[]dnl
818 m4_if(m4_defn([$1]), [$2], [],
819       [m4_define([$1], m4_eval(m4_defn([$1])+[$3]))$0($@)])])
822 # Implementing `foreach' loops in m4 is much more tricky than it may
823 # seem.  For example, the old M4 1.4.4 manual had an incorrect example,
824 # which looked like this (when translated to m4sugar):
826 # | # foreach(VAR, (LIST), STMT)
827 # | m4_define([foreach],
828 # |   [m4_pushdef([$1])_foreach([$1], [$2], [$3])m4_popdef([$1])])
829 # | m4_define([_arg1], [$1])
830 # | m4_define([_foreach],
831 # |   [m4_if([$2], [()], ,
832 # |     [m4_define([$1], _arg1$2)$3[]_foreach([$1], (m4_shift$2), [$3])])])
834 # But then if you run
836 # | m4_define(a, 1)
837 # | m4_define(b, 2)
838 # | m4_define(c, 3)
839 # | foreach([f], [([a], [(b], [c)])], [echo f
840 # | ])
842 # it gives
844 #  => echo 1
845 #  => echo (2,3)
847 # which is not what is expected.
849 # Of course the problem is that many quotes are missing.  So you add
850 # plenty of quotes at random places, until you reach the expected
851 # result.  Alternatively, if you are a quoting wizard, you directly
852 # reach the following implementation (but if you really did, then
853 # apply to the maintenance of m4sugar!).
855 # | # foreach(VAR, (LIST), STMT)
856 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
857 # | m4_define([_arg1], [[$1]])
858 # | m4_define([_foreach],
859 # |  [m4_if($2, [()], ,
860 # |    [m4_define([$1], [_arg1$2])$3[]_foreach([$1], [(m4_shift$2)], [$3])])])
862 # which this time answers
864 #  => echo a
865 #  => echo (b
866 #  => echo c)
868 # Bingo!
870 # Well, not quite.
872 # With a better look, you realize that the parens are more a pain than
873 # a help: since anyway you need to quote properly the list, you end up
874 # with always using an outermost pair of parens and an outermost pair
875 # of quotes.  Rejecting the parens both eases the implementation, and
876 # simplifies the use:
878 # | # foreach(VAR, (LIST), STMT)
879 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
880 # | m4_define([_arg1], [$1])
881 # | m4_define([_foreach],
882 # |  [m4_if($2, [], ,
883 # |    [m4_define([$1], [_arg1($2)])$3[]_foreach([$1], [m4_shift($2)], [$3])])])
886 # Now, just replace the `$2' with `m4_quote($2)' in the outer `m4_if'
887 # to improve robustness, and you come up with a nice implementation
888 # that doesn't require extra parentheses in the user's LIST.
890 # But wait -  now the algorithm is quadratic, because every recursion of
891 # the algorithm keeps the entire LIST and merely adds another m4_shift to
892 # the quoted text.  If the user has a lot of elements in LIST, you can
893 # bring the system to its knees with the memory m4 then requires, or trip
894 # the m4 --nesting-limit recursion factor.  The only way to avoid
895 # quadratic growth is ensure m4_shift is expanded prior to the recursion.
896 # Hence the design below.
898 # The M4 manual now includes a chapter devoted to this issue, with
899 # the lessons learned from m4sugar.
902 # m4_foreach(VARIABLE, LIST, EXPRESSION)
903 # --------------------------------------
905 # Expand EXPRESSION assigning each value of the LIST to VARIABLE.
906 # LIST should have the form `item_1, item_2, ..., item_n', i.e. the
907 # whole list must *quoted*.  Quote members too if you don't want them
908 # to be expanded.
910 # This macro is robust to active symbols:
911 #      | m4_define(active, [ACT, IVE])
912 #      | m4_foreach(Var, [active, active], [-Var-])
913 #     => -ACT--IVE--ACT--IVE-
915 #      | m4_foreach(Var, [[active], [active]], [-Var-])
916 #     => -ACT, IVE--ACT, IVE-
918 #      | m4_foreach(Var, [[[active]], [[active]]], [-Var-])
919 #     => -active--active-
921 # This macro is called frequently, so avoid extra expansions such as
922 # m4_ifval and dnl.  Also, since $2 might be quite large, try to use it
923 # as little as possible in _m4_foreach; each extra use requires that much
924 # more memory for expansion.  So, rather than directly compare $2 against
925 # [] and use m4_car/m4_cdr for recursion, we instead unbox the list (which
926 # requires swapping the argument order in the helper) and use _m4_shift3
927 # to detect when recursion is complete.
928 m4_define([m4_foreach],
929 [m4_pushdef([$1])_$0([$1], [$3]m4_if([$2], [], [], [, $2]))m4_popdef([$1])])
931 m4_define([_m4_foreach],
932 [m4_if([$#], [2], [],
933        [m4_define([$1], [$3])$2[]$0([$1], [$2]_m4_shift3($@))])])
936 # m4_foreach_w(VARIABLE, LIST, EXPRESSION)
937 # ----------------------------------------
939 # Like m4_foreach, but the list is whitespace separated.
941 # This macro is robust to active symbols:
942 #    m4_foreach_w([Var], [ active
943 #    b  act\
944 #    ive  ], [-Var-])end
945 #    => -active--b--active-end
947 m4_define([m4_foreach_w],
948 [m4_foreach([$1], m4_split(m4_normalize([$2]), [ ]), [$3])])
951 # m4_map(MACRO, LIST)
952 # -------------------
953 # Invoke MACRO($1), MACRO($2) etc. where $1, $2... are the elements
954 # of LIST.  $1, $2... must in turn be lists, appropriate for m4_apply.
956 # Since LIST may be quite large, we want to minimize how often it appears
957 # in the expansion.  Rather than use m4_car/m4_cdr iteration, we unbox the
958 # list, and use _m4_shift2 to detect the end of recursion.
959 m4_define([m4_map],
960 [m4_if([$2], [], [],
961        [_$0([$1], $2)])])
962 m4_define([_m4_map],
963 [m4_if([$#], [1], [],
964        [m4_apply([$1], [$2])$0([$1]_m4_shift2($@))])])
967 # m4_map_sep(MACRO, SEPARATOR, LIST)
968 # ----------------------------------
969 # Invoke MACRO($1), SEPARATOR, MACRO($2), ..., MACRO($N) where $1, $2... $N
970 # are the elements of LIST, and are in turn lists appropriate for m4_apply.
971 # SEPARATOR is not further expanded.
972 m4_define([m4_map_sep],
973 [m4_if([$3], [], [],
974        [m4_apply([$1], m4_car($3))m4_map([[$2]$1]_m4_cdr($3))])])
977 ## --------------------------- ##
978 ## 9. More diversion support.  ##
979 ## --------------------------- ##
982 # _m4_divert(DIVERSION-NAME or NUMBER)
983 # ------------------------------------
984 # If DIVERSION-NAME is the name of a diversion, return its number,
985 # otherwise if it is a NUMBER return it.
986 m4_define([_m4_divert],
987 [m4_ifdef([_m4_divert($1)],
988           [m4_indir([_m4_divert($1)])],
989           [$1])])
991 # KILL is only used to suppress output.
992 m4_define([_m4_divert(KILL)],           -1)
994 # The empty diversion name is a synonym for 0.
995 m4_define([_m4_divert()],                0)
998 # _m4_divert_n_stack
999 # ------------------
1000 # Print m4_divert_stack with newline prepended, if it's nonempty.
1001 m4_define([_m4_divert_n_stack],
1002 [m4_ifdef([m4_divert_stack], [
1003 m4_defn([m4_divert_stack])])])
1006 # m4_divert(DIVERSION-NAME)
1007 # -------------------------
1008 # Change the diversion stream to DIVERSION-NAME.
1009 m4_define([m4_divert],
1010 [m4_define([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
1011 m4_builtin([divert], _m4_divert([$1]))dnl
1015 # m4_divert_push(DIVERSION-NAME)
1016 # ------------------------------
1017 # Change the diversion stream to DIVERSION-NAME, while stacking old values.
1018 m4_define([m4_divert_push],
1019 [m4_pushdef([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
1020 m4_pushdef([_m4_divert_diversion], [$1])dnl
1021 m4_builtin([divert], _m4_divert([$1]))dnl
1025 # m4_divert_pop([DIVERSION-NAME])
1026 # -------------------------------
1027 # Change the diversion stream to its previous value, unstacking it.
1028 # If specified, verify we left DIVERSION-NAME.
1029 # When we pop the last value from the stack, we divert to -1.
1030 m4_define([m4_divert_pop],
1031 [m4_ifndef([_m4_divert_diversion],
1032            [m4_fatal([too many m4_divert_pop])])dnl
1033 m4_if([$1], [], [],
1034       [$1], m4_defn([_m4_divert_diversion]), [],
1035       [m4_fatal([$0($1): diversion mismatch: ]_m4_divert_n_stack)])dnl
1036 m4_popdef([m4_divert_stack])dnl
1037 m4_popdef([_m4_divert_diversion])dnl
1038 m4_builtin([divert],
1039            m4_ifdef([_m4_divert_diversion],
1040                     [_m4_divert(m4_defn([_m4_divert_diversion]))],
1041                     -1))dnl
1045 # m4_divert_text(DIVERSION-NAME, CONTENT)
1046 # ---------------------------------------
1047 # Output CONTENT into DIVERSION-NAME (which may be a number actually).
1048 # An end of line is appended for free to CONTENT.
1049 m4_define([m4_divert_text],
1050 [m4_divert_push([$1])dnl
1052 m4_divert_pop([$1])dnl
1056 # m4_divert_once(DIVERSION-NAME, CONTENT)
1057 # ---------------------------------------
1058 # Output CONTENT into DIVERSION-NAME once, if not already there.
1059 # An end of line is appended for free to CONTENT.
1060 m4_define([m4_divert_once],
1061 [m4_expand_once([m4_divert_text([$1], [$2])])])
1064 # m4_undivert(DIVERSION-NAME)
1065 # ---------------------------
1066 # Undivert DIVERSION-NAME.  Unlike the M4 version, this only takes a single
1067 # diversion identifier, and should not be used to undivert files.
1068 m4_define([m4_undivert],
1069 [m4_builtin([undivert], _m4_divert([$1]))])
1072 ## --------------------------------------------- ##
1073 ## 10. Defining macros with bells and whistles.  ##
1074 ## --------------------------------------------- ##
1076 # `m4_defun' is basically `m4_define' but it equips the macro with the
1077 # needed machinery for `m4_require'.  A macro must be m4_defun'd if
1078 # either it is m4_require'd, or it m4_require's.
1080 # Two things deserve attention and are detailed below:
1081 #  1. Implementation of m4_require
1082 #  2. Keeping track of the expansion stack
1084 # 1. Implementation of m4_require
1085 # ===============================
1087 # Of course m4_defun AC_PROVIDE's the macro, so that a macro which has
1088 # been expanded is not expanded again when m4_require'd, but the
1089 # difficult part is the proper expansion of macros when they are
1090 # m4_require'd.
1092 # The implementation is based on two ideas, (i) using diversions to
1093 # prepare the expansion of the macro and its dependencies (by Franc,ois
1094 # Pinard), and (ii) expand the most recently m4_require'd macros _after_
1095 # the previous macros (by Axel Thimm).
1098 # The first idea: why use diversions?
1099 # -----------------------------------
1101 # When a macro requires another, the other macro is expanded in new
1102 # diversion, GROW.  When the outer macro is fully expanded, we first
1103 # undivert the most nested diversions (GROW - 1...), and finally
1104 # undivert GROW.  To understand why we need several diversions,
1105 # consider the following example:
1107 # | m4_defun([TEST1], [Test...REQUIRE([TEST2])1])
1108 # | m4_defun([TEST2], [Test...REQUIRE([TEST3])2])
1109 # | m4_defun([TEST3], [Test...3])
1111 # Because m4_require is not required to be first in the outer macros, we
1112 # must keep the expansions of the various levels of m4_require separated.
1113 # Right before executing the epilogue of TEST1, we have:
1115 #          GROW - 2: Test...3
1116 #          GROW - 1: Test...2
1117 #          GROW:     Test...1
1118 #          BODY:
1120 # Finally the epilogue of TEST1 undiverts GROW - 2, GROW - 1, and
1121 # GROW into the regular flow, BODY.
1123 #          GROW - 2:
1124 #          GROW - 1:
1125 #          GROW:
1126 #          BODY:        Test...3; Test...2; Test...1
1128 # (The semicolons are here for clarification, but of course are not
1129 # emitted.)  This is what Autoconf 2.0 (I think) to 2.13 (I'm sure)
1130 # implement.
1133 # The second idea: first required first out
1134 # -----------------------------------------
1136 # The natural implementation of the idea above is buggy and produces
1137 # very surprising results in some situations.  Let's consider the
1138 # following example to explain the bug:
1140 # | m4_defun([TEST1],  [REQUIRE([TEST2a])REQUIRE([TEST2b])])
1141 # | m4_defun([TEST2a], [])
1142 # | m4_defun([TEST2b], [REQUIRE([TEST3])])
1143 # | m4_defun([TEST3],  [REQUIRE([TEST2a])])
1144 # |
1145 # | AC_INIT
1146 # | TEST1
1148 # The dependencies between the macros are:
1150 #                3 --- 2b
1151 #               /        \              is m4_require'd by
1152 #              /          \       left -------------------- right
1153 #           2a ------------ 1
1155 # If you strictly apply the rules given in the previous section you get:
1157 #          GROW - 2: TEST3
1158 #          GROW - 1: TEST2a; TEST2b
1159 #          GROW:     TEST1
1160 #          BODY:
1162 # (TEST2a, although required by TEST3 is not expanded in GROW - 3
1163 # because is has already been expanded before in GROW - 1, so it has
1164 # been AC_PROVIDE'd, so it is not expanded again) so when you undivert
1165 # the stack of diversions, you get:
1167 #          GROW - 2:
1168 #          GROW - 1:
1169 #          GROW:
1170 #          BODY:        TEST3; TEST2a; TEST2b; TEST1
1172 # i.e., TEST2a is expanded after TEST3 although the latter required the
1173 # former.
1175 # Starting from 2.50, we use an implementation provided by Axel Thimm.
1176 # The idea is simple: the order in which macros are emitted must be the
1177 # same as the one in which macros are expanded.  (The bug above can
1178 # indeed be described as: a macro has been AC_PROVIDE'd before its
1179 # dependent, but it is emitted after: the lack of correlation between
1180 # emission and expansion order is guilty).
1182 # How to do that?  You keep the stack of diversions to elaborate the
1183 # macros, but each time a macro is fully expanded, emit it immediately.
1185 # In the example above, when TEST2a is expanded, but it's epilogue is
1186 # not run yet, you have:
1188 #          GROW - 2:
1189 #          GROW - 1: TEST2a
1190 #          GROW:     Elaboration of TEST1
1191 #          BODY:
1193 # The epilogue of TEST2a emits it immediately:
1195 #          GROW - 2:
1196 #          GROW - 1:
1197 #          GROW:     Elaboration of TEST1
1198 #          BODY:     TEST2a
1200 # TEST2b then requires TEST3, so right before the epilogue of TEST3, you
1201 # have:
1203 #          GROW - 2: TEST3
1204 #          GROW - 1: Elaboration of TEST2b
1205 #          GROW:     Elaboration of TEST1
1206 #          BODY:      TEST2a
1208 # The epilogue of TEST3 emits it:
1210 #          GROW - 2:
1211 #          GROW - 1: Elaboration of TEST2b
1212 #          GROW:     Elaboration of TEST1
1213 #          BODY:     TEST2a; TEST3
1215 # TEST2b is now completely expanded, and emitted:
1217 #          GROW - 2:
1218 #          GROW - 1:
1219 #          GROW:     Elaboration of TEST1
1220 #          BODY:     TEST2a; TEST3; TEST2b
1222 # and finally, TEST1 is finished and emitted:
1224 #          GROW - 2:
1225 #          GROW - 1:
1226 #          GROW:
1227 #          BODY:     TEST2a; TEST3; TEST2b: TEST1
1229 # The idea is simple, but the implementation is a bit evolved.  If you
1230 # are like me, you will want to see the actual functioning of this
1231 # implementation to be convinced.  The next section gives the full
1232 # details.
1235 # The Axel Thimm implementation at work
1236 # -------------------------------------
1238 # We consider the macros above, and this configure.ac:
1240 #           AC_INIT
1241 #           TEST1
1243 # You should keep the definitions of _m4_defun_pro, _m4_defun_epi, and
1244 # m4_require at hand to follow the steps.
1246 # This implements tries not to assume that the current diversion is
1247 # BODY, so as soon as a macro (m4_defun'd) is expanded, we first
1248 # record the current diversion under the name _m4_divert_dump (denoted
1249 # DUMP below for short).  This introduces an important difference with
1250 # the previous versions of Autoconf: you cannot use m4_require if you
1251 # are not inside an m4_defun'd macro, and especially, you cannot
1252 # m4_require directly from the top level.
1254 # We have not tried to simulate the old behavior (better yet, we
1255 # diagnose it), because it is too dangerous: a macro m4_require'd from
1256 # the top level is expanded before the body of `configure', i.e., before
1257 # any other test was run.  I let you imagine the result of requiring
1258 # AC_STDC_HEADERS for instance, before AC_PROG_CC was actually run....
1260 # After AC_INIT was run, the current diversion is BODY.
1261 # * AC_INIT was run
1262 #   DUMP:                undefined
1263 #   diversion stack:     BODY |-
1265 # * TEST1 is expanded
1266 # The prologue of TEST1 sets _m4_divert_dump, which is the diversion
1267 # where the current elaboration will be dumped, to the current
1268 # diversion.  It also m4_divert_push to GROW, where the full
1269 # expansion of TEST1 and its dependencies will be elaborated.
1270 #   DUMP:        BODY
1271 #   BODY:        empty
1272 #   diversions:  GROW, BODY |-
1274 # * TEST1 requires TEST2a
1275 # _m4_require_call m4_divert_pushes another temporary diversion,
1276 # GROW - 1, and expands TEST2a in there.
1277 #   DUMP:        BODY
1278 #   BODY:        empty
1279 #   GROW - 1:    TEST2a
1280 #   diversions:  GROW - 1, GROW, BODY |-
1281 # Than the content of the temporary diversion is moved to DUMP and the
1282 # temporary diversion is popped.
1283 #   DUMP:        BODY
1284 #   BODY:        TEST2a
1285 #   diversions:  GROW, BODY |-
1287 # * TEST1 requires TEST2b
1288 # Again, _m4_require_call pushes GROW - 1 and heads to expand TEST2b.
1289 #   DUMP:        BODY
1290 #   BODY:        TEST2a
1291 #   diversions:  GROW - 1, GROW, BODY |-
1293 # * TEST2b requires TEST3
1294 # _m4_require_call pushes GROW - 2 and expands TEST3 here.
1295 # (TEST3 requires TEST2a, but TEST2a has already been m4_provide'd, so
1296 # nothing happens.)
1297 #   DUMP:        BODY
1298 #   BODY:        TEST2a
1299 #   GROW - 2:    TEST3
1300 #   diversions:  GROW - 2, GROW - 1, GROW, BODY |-
1301 # Than the diversion is appended to DUMP, and popped.
1302 #   DUMP:        BODY
1303 #   BODY:        TEST2a; TEST3
1304 #   diversions:  GROW - 1, GROW, BODY |-
1306 # * TEST1 requires TEST2b (contd.)
1307 # The content of TEST2b is expanded...
1308 #   DUMP:        BODY
1309 #   BODY:        TEST2a; TEST3
1310 #   GROW - 1:    TEST2b,
1311 #   diversions:  GROW - 1, GROW, BODY |-
1312 # ... and moved to DUMP.
1313 #   DUMP:        BODY
1314 #   BODY:        TEST2a; TEST3; TEST2b
1315 #   diversions:  GROW, BODY |-
1317 # * TEST1 is expanded: epilogue
1318 # TEST1's own content is in GROW...
1319 #   DUMP:        BODY
1320 #   BODY:        TEST2a; TEST3; TEST2b
1321 #   GROW:        TEST1
1322 #   diversions:  BODY |-
1323 # ... and it's epilogue moves it to DUMP and then undefines DUMP.
1324 #   DUMP:       undefined
1325 #   BODY:       TEST2a; TEST3; TEST2b; TEST1
1326 #   diversions: BODY |-
1329 # 2. Keeping track of the expansion stack
1330 # =======================================
1332 # When M4 expansion goes wrong it is often extremely hard to find the
1333 # path amongst macros that drove to the failure.  What is needed is
1334 # the stack of macro `calls'. One could imagine that GNU M4 would
1335 # maintain a stack of macro expansions, unfortunately it doesn't, so
1336 # we do it by hand.  This is of course extremely costly, but the help
1337 # this stack provides is worth it.  Nevertheless to limit the
1338 # performance penalty this is implemented only for m4_defun'd macros,
1339 # not for define'd macros.
1341 # The scheme is simplistic: each time we enter an m4_defun'd macros,
1342 # we prepend its name in m4_expansion_stack, and when we exit the
1343 # macro, we remove it (thanks to pushdef/popdef).
1345 # In addition, we want to detect circular m4_require dependencies.
1346 # Each time we expand a macro FOO we define _m4_expanding(FOO); and
1347 # m4_require(BAR) simply checks whether _m4_expanding(BAR) is defined.
1350 # m4_expansion_stack_push(TEXT)
1351 # -----------------------------
1352 # Use m4_builtin to avoid m4_defn overhead.
1353 m4_define([m4_expansion_stack_push],
1354 [m4_pushdef([m4_expansion_stack],
1355             [$1]m4_ifdef([m4_expansion_stack], [
1356 m4_builtin([defn], [m4_expansion_stack])]))])
1359 # m4_expansion_stack_pop
1360 # ----------------------
1361 m4_define([m4_expansion_stack_pop],
1362 [m4_popdef([m4_expansion_stack])])
1365 # m4_expansion_stack_dump
1366 # -----------------------
1367 # Dump the expansion stack.
1368 m4_define([m4_expansion_stack_dump],
1369 [m4_ifdef([m4_expansion_stack],
1370           [m4_errprintn(m4_defn([m4_expansion_stack]))])dnl
1371 m4_errprintn(m4_location[: the top level])])
1374 # _m4_divert(GROW)
1375 # ----------------
1376 # This diversion is used by the m4_defun/m4_require machinery.  It is
1377 # important to keep room before GROW because for each nested
1378 # AC_REQUIRE we use an additional diversion (i.e., two m4_require's
1379 # will use GROW - 2.  More than 3 levels has never seemed to be
1380 # needed.)
1382 # ...
1383 # - GROW - 2
1384 #   m4_require'd code, 2 level deep
1385 # - GROW - 1
1386 #   m4_require'd code, 1 level deep
1387 # - GROW
1388 #   m4_defun'd macros are elaborated here.
1390 m4_define([_m4_divert(GROW)],       10000)
1393 # _m4_defun_pro(MACRO-NAME)
1394 # -------------------------
1395 # The prologue for Autoconf macros.
1397 # This is called frequently, so minimize the number of macro invocations
1398 # by avoiding dnl and m4_defn overhead.
1399 m4_define([_m4_defun_pro],
1400 m4_do([[m4_ifdef([m4_expansion_stack], [], [_m4_defun_pro_outer[]])]],
1401       [[m4_expansion_stack_push(m4_builtin([defn],
1402           [m4_location($1)])[: $1 is expanded from...])]],
1403       [[m4_pushdef([_m4_expanding($1)])]]))
1405 m4_define([_m4_defun_pro_outer],
1406 [m4_copy([_m4_divert_diversion], [_m4_divert_dump])m4_divert_push([GROW])])
1408 # _m4_defun_epi(MACRO-NAME)
1409 # -------------------------
1410 # The Epilogue for Autoconf macros.  MACRO-NAME only helps tracing
1411 # the PRO/EPI pairs.
1413 # This is called frequently, so minimize the number of macro invocations
1414 # by avoiding dnl and m4_popdef overhead.
1415 m4_define([_m4_defun_epi],
1416 m4_do([[m4_builtin([popdef], [_m4_expanding($1)])]],
1417       [[m4_expansion_stack_pop()]],
1418       [[m4_ifdef([m4_expansion_stack], [], [_m4_defun_epi_outer[]])]],
1419       [[m4_provide([$1])]]))
1421 m4_define([_m4_defun_epi_outer],
1422 m4_do([[m4_builtin([undefine], [_m4_divert_dump])]],
1423       [[m4_divert_pop([GROW])]],
1424       [[m4_undivert([GROW])]]))
1427 # m4_defun(NAME, EXPANSION)
1428 # -------------------------
1429 # Define a macro which automatically provides itself.  Add machinery
1430 # so the macro automatically switches expansion to the diversion
1431 # stack if it is not already using it.  In this case, once finished,
1432 # it will bring back all the code accumulated in the diversion stack.
1433 # This, combined with m4_require, achieves the topological ordering of
1434 # macros.  We don't use this macro to define some frequently called
1435 # macros that are not involved in ordering constraints, to save m4
1436 # processing.
1437 m4_define([m4_defun],
1438 [m4_define([m4_location($1)], m4_location)dnl
1439 m4_define([$1],
1440           [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])
1443 # m4_defun_once(NAME, EXPANSION)
1444 # ------------------------------
1445 # As m4_defun, but issues the EXPANSION only once, and warns if used
1446 # several times.
1447 m4_define([m4_defun_once],
1448 [m4_define([m4_location($1)], m4_location)dnl
1449 m4_define([$1],
1450           [m4_provide_if([$1],
1451                          [m4_warn([syntax], [$1 invoked multiple times])],
1452                          [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])])
1455 # m4_pattern_forbid(ERE, [WHY])
1456 # -----------------------------
1457 # Declare that no token matching the forbidden extended regular
1458 # expression ERE should be seen in the output unless...
1459 m4_define([m4_pattern_forbid], [])
1462 # m4_pattern_allow(ERE)
1463 # ---------------------
1464 # ... that token also matches the allowed extended regular expression ERE.
1465 # Both used via traces.
1466 m4_define([m4_pattern_allow], [])
1469 ## --------------------------------- ##
1470 ## 11. Dependencies between macros.  ##
1471 ## --------------------------------- ##
1474 # m4_before(THIS-MACRO-NAME, CALLED-MACRO-NAME)
1475 # ---------------------------------------------
1476 # Issue a warning if CALLED-MACRO-NAME was called before THIS-MACRO-NAME.
1477 m4_define([m4_before],
1478 [m4_provide_if([$2],
1479                [m4_warn([syntax], [$2 was called before $1])])])
1482 # m4_require(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
1483 # -----------------------------------------------------------
1484 # If NAME-TO-CHECK has never been expanded (actually, if it is not
1485 # m4_provide'd), expand BODY-TO-EXPAND *before* the current macro
1486 # expansion.  Once expanded, emit it in _m4_divert_dump.  Keep track
1487 # of the m4_require chain in m4_expansion_stack.
1489 # The normal cases are:
1491 # - NAME-TO-CHECK == BODY-TO-EXPAND
1492 #   Which you can use for regular macros with or without arguments, e.g.,
1493 #     m4_require([AC_PROG_CC], [AC_PROG_CC])
1494 #     m4_require([AC_CHECK_HEADERS(limits.h)], [AC_CHECK_HEADERS(limits.h)])
1495 #   which is just the same as
1496 #     m4_require([AC_PROG_CC])
1497 #     m4_require([AC_CHECK_HEADERS(limits.h)])
1499 # - BODY-TO-EXPAND == m4_indir([NAME-TO-CHECK])
1500 #   In the case of macros with irregular names.  For instance:
1501 #     m4_require([AC_LANG_COMPILER(C)], [indir([AC_LANG_COMPILER(C)])])
1502 #   which means `if the macro named `AC_LANG_COMPILER(C)' (the parens are
1503 #   part of the name, it is not an argument) has not been run, then
1504 #   call it.'
1505 #   Had you used
1506 #     m4_require([AC_LANG_COMPILER(C)], [AC_LANG_COMPILER(C)])
1507 #   then m4_require would have tried to expand `AC_LANG_COMPILER(C)', i.e.,
1508 #   call the macro `AC_LANG_COMPILER' with `C' as argument.
1510 #   You could argue that `AC_LANG_COMPILER', when it receives an argument
1511 #   such as `C' should dispatch the call to `AC_LANG_COMPILER(C)'.  But this
1512 #   `extension' prevents `AC_LANG_COMPILER' from having actual arguments that
1513 #   it passes to `AC_LANG_COMPILER(C)'.
1515 # This is called frequently, so minimize the number of macro invocations
1516 # by avoiding dnl and other overhead on the common path.
1517 m4_define([m4_require],
1518 m4_do([[m4_ifdef([_m4_expanding($1)],
1519                  [m4_fatal([$0: circular dependency of $1])])]],
1520       [[m4_ifdef([_m4_divert_dump], [],
1521                  [m4_fatal([$0($1): cannot be used outside of an ]dnl
1522 m4_bmatch([$0], [^AC_], [[AC_DEFUN]], [[m4_defun]])['d macro])])]],
1523       [[m4_provide_if([$1],
1524                       [],
1525                       [_m4_require_call([$1], [$2])])]]))
1528 # _m4_require_call(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
1529 # -----------------------------------------------------------------
1530 # If m4_require decides to expand the body, it calls this macro.
1532 # This is called frequently, so minimize the number of macro invocations
1533 # by avoiding dnl and other overhead on the common path.
1534 m4_define([_m4_require_call],
1535 m4_do([[m4_define([_m4_divert_grow], m4_decr(_m4_divert_grow))]],
1536       [[m4_divert_push(_m4_divert_grow)]],
1537       [[m4_default([$2], [$1])
1538 m4_provide_if([$1],
1539               [],
1540               [m4_warn([syntax],
1541                        [$1 is m4_require'd but not m4_defun'd])])]],
1542       [[m4_divert(m4_builtin([defn], [_m4_divert_dump]))]],
1543       [[m4_undivert(_m4_divert_grow)]],
1544       [[m4_divert_pop(_m4_divert_grow)]],
1545       [[m4_define([_m4_divert_grow], m4_incr(_m4_divert_grow))]]))
1548 # _m4_divert_grow
1549 # ---------------
1550 # The counter for _m4_require_call.
1551 m4_define([_m4_divert_grow], _m4_divert([GROW]))
1554 # m4_expand_once(TEXT, [WITNESS = TEXT])
1555 # --------------------------------------
1556 # If TEXT has never been expanded, expand it *here*.  Use WITNESS as
1557 # as a memory that TEXT has already been expanded.
1558 m4_define([m4_expand_once],
1559 [m4_provide_if(m4_ifval([$2], [[$2]], [[$1]]),
1560                [],
1561                [m4_provide(m4_ifval([$2], [[$2]], [[$1]]))[]$1])])
1564 # m4_provide(MACRO-NAME)
1565 # ----------------------
1566 m4_define([m4_provide],
1567 [m4_define([m4_provide($1)])])
1570 # m4_provide_if(MACRO-NAME, IF-PROVIDED, IF-NOT-PROVIDED)
1571 # -------------------------------------------------------
1572 # If MACRO-NAME is provided do IF-PROVIDED, else IF-NOT-PROVIDED.
1573 # The purpose of this macro is to provide the user with a means to
1574 # check macros which are provided without letting her know how the
1575 # information is coded.
1576 m4_define([m4_provide_if],
1577 [m4_ifdef([m4_provide($1)],
1578           [$2], [$3])])
1581 ## --------------------- ##
1582 ## 12. Text processing.  ##
1583 ## --------------------- ##
1586 # m4_cr_letters
1587 # m4_cr_LETTERS
1588 # m4_cr_Letters
1589 # -------------
1590 m4_define([m4_cr_letters], [abcdefghijklmnopqrstuvwxyz])
1591 m4_define([m4_cr_LETTERS], [ABCDEFGHIJKLMNOPQRSTUVWXYZ])
1592 m4_define([m4_cr_Letters],
1593 m4_defn([m4_cr_letters])dnl
1594 m4_defn([m4_cr_LETTERS])dnl
1598 # m4_cr_digits
1599 # ------------
1600 m4_define([m4_cr_digits], [0123456789])
1603 # m4_cr_alnum
1604 # -----------
1605 m4_define([m4_cr_alnum],
1606 m4_defn([m4_cr_Letters])dnl
1607 m4_defn([m4_cr_digits])dnl
1611 # m4_cr_symbols1
1612 # m4_cr_symbols2
1613 # -------------------------------
1614 m4_define([m4_cr_symbols1],
1615 m4_defn([m4_cr_Letters])dnl
1618 m4_define([m4_cr_symbols2],
1619 m4_defn([m4_cr_symbols1])dnl
1620 m4_defn([m4_cr_digits])dnl
1623 # m4_cr_all
1624 # ---------
1625 # The character range representing everything, with `-' as the last
1626 # character, since it is special to m4_translit.  Use with care, because
1627 # it contains characters special to M4 (fortunately, both ASCII and EBCDIC
1628 # have [] in order, so m4_defn([m4_cr_all]) remains a valid string).  It
1629 # also contains characters special to terminals, so it should never be
1630 # displayed in an error message.  Also, attempts to map [ and ] to other
1631 # characters via m4_translit must deal with the fact that m4_translit does
1632 # not add quotes to the output.
1634 # It is mainly useful in generating inverted character range maps, for use
1635 # in places where m4_translit is faster than an equivalent m4_bpatsubst;
1636 # the regex `[^a-z]' is equivalent to:
1637 #  m4_translit(m4_dquote(m4_defn([m4_cr_all])), [a-z])
1638 m4_define([m4_cr_all],
1639 m4_translit(m4_dquote(m4_format(m4_dquote(m4_for(
1640   ,1,255,,[[%c]]))m4_for([i],1,255,,[,i]))), [-])-)
1643 # _m4_define_cr_not(CATEGORY)
1644 # ---------------------------
1645 # Define m4_cr_not_CATEGORY as the inverse of m4_cr_CATEGORY.
1646 m4_define([_m4_define_cr_not],
1647 [m4_define([m4_cr_not_$1],
1648            m4_translit(m4_dquote(m4_defn([m4_cr_all])),
1649                        m4_defn([m4_cr_$1])))])
1652 # m4_cr_not_letters
1653 # m4_cr_not_LETTERS
1654 # m4_cr_not_Letters
1655 # m4_cr_not_digits
1656 # m4_cr_not_alnum
1657 # m4_cr_not_symbols1
1658 # m4_cr_not_symbols2
1659 # ------------------
1660 # Inverse character sets
1661 _m4_define_cr_not([letters])
1662 _m4_define_cr_not([LETTERS])
1663 _m4_define_cr_not([Letters])
1664 _m4_define_cr_not([digits])
1665 _m4_define_cr_not([alnum])
1666 _m4_define_cr_not([symbols1])
1667 _m4_define_cr_not([symbols2])
1670 # m4_newline
1671 # ----------
1672 # Expands to a newline.  Exists for formatting reasons.
1673 m4_define([m4_newline], [
1677 # m4_re_escape(STRING)
1678 # --------------------
1679 # Escape RE active characters in STRING.
1680 m4_define([m4_re_escape],
1681 [m4_bpatsubst([$1],
1682               [[][*+.?\^$]], [\\\&])])
1685 # m4_re_string
1686 # ------------
1687 # Regexp for `[a-zA-Z_0-9]*'
1688 # m4_dquote provides literal [] for the character class.
1689 m4_define([m4_re_string],
1690 m4_dquote(m4_defn([m4_cr_symbols2]))dnl
1691 [*]dnl
1695 # m4_re_word
1696 # ----------
1697 # Regexp for `[a-zA-Z_][a-zA-Z_0-9]*'
1698 m4_define([m4_re_word],
1699 m4_dquote(m4_defn([m4_cr_symbols1]))dnl
1700 m4_defn([m4_re_string])dnl
1704 # m4_tolower(STRING)
1705 # m4_toupper(STRING)
1706 # ------------------
1707 # These macros convert STRING to lowercase or uppercase.
1709 # Rather than expand the m4_defn each time, we inline them up front.
1710 m4_define([m4_tolower],
1711 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_LETTERS]))[,
1712                    ]m4_dquote(m4_defn([m4_cr_letters]))[)])
1713 m4_define([m4_toupper],
1714 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_letters]))[,
1715                    ]m4_dquote(m4_defn([m4_cr_LETTERS]))[)])
1718 # m4_split(STRING, [REGEXP])
1719 # --------------------------
1721 # Split STRING into an m4 list of quoted elements.  The elements are
1722 # quoted with [ and ].  Beginning spaces and end spaces *are kept*.
1723 # Use m4_strip to remove them.
1725 # REGEXP specifies where to split.  Default is [\t ]+.
1727 # If STRING is empty, the result is an empty list.
1729 # Pay attention to the m4_changequotes.  When m4 reads the definition of
1730 # m4_split, it still has quotes set to [ and ].  Luckily, these are matched
1731 # in the macro body, so the definition is stored correctly.  Use the same
1732 # alternate quotes as m4_noquote; it must be unlikely to appear in $1.
1734 # Also, notice that $1 is quoted twice, since we want the result to
1735 # be quoted.  Then you should understand that the argument of
1736 # patsubst is -=<{STRING}>=- (i.e., with additional -=<{ and }>=-).
1738 # This macro is safe on active symbols, i.e.:
1739 #   m4_define(active, ACTIVE)
1740 #   m4_split([active active ])end
1741 #   => [active], [active], []end
1743 # Optimize on regex of ` ' (space), since m4_foreach_w already guarantees
1744 # that the list contains single space separators, and a common case is
1745 # splitting a single-element list.  This macro is called frequently,
1746 # so avoid unnecessary dnl inside the definition.
1747 m4_define([m4_split],
1748 [m4_if([$1], [], [],
1749        [$2], [ ], [m4_if(m4_index([$1], [ ]), [-1], [[[$1]]], [_$0($@)])],
1750        [$2], [], [_$0([$1], [[   ]+])],
1751        [_$0($@)])])
1753 m4_define([_m4_split],
1754 [m4_changequote(-=<{,}>=-)]dnl
1755 [[m4_bpatsubst(-=<{-=<{$1}>=-}>=-, -=<{$2}>=-,
1756                -=<{], [}>=-)]m4_changequote([, ])])
1760 # m4_flatten(STRING)
1761 # ------------------
1762 # If STRING contains end of lines, replace them with spaces.  If there
1763 # are backslashed end of lines, remove them.  This macro is safe with
1764 # active symbols.
1765 #    m4_define(active, ACTIVE)
1766 #    m4_flatten([active
1767 #    act\
1768 #    ive])end
1769 #    => active activeend
1771 # In m4, m4_bpatsubst is expensive, so first check for a newline.
1772 m4_define([m4_flatten],
1773 [m4_if(m4_index([$1], [
1774 ]), [-1], [[$1]],
1775        [m4_translit(m4_bpatsubst([[[$1]]], [\\
1776 ]), [
1777 ], [ ])])])
1780 # m4_strip(STRING)
1781 # ----------------
1782 # Expands into STRING with tabs and spaces singled out into a single
1783 # space, and removing leading and trailing spaces.
1785 # This macro is robust to active symbols.
1786 #    m4_define(active, ACTIVE)
1787 #    m4_strip([  active <tab> <tab>active ])end
1788 #    => active activeend
1790 # First, notice that we guarantee trailing space.  Why?  Because regular
1791 # expressions are greedy, and `.* ?' would always group the space into the
1792 # .* portion.  The algorithm is simpler by avoiding `?' at the end.  The
1793 # algorithm correctly strips everything if STRING is just ` '.
1795 # Then notice the second pattern: it is in charge of removing the
1796 # leading/trailing spaces.  Why not just `[^ ]'?  Because they are
1797 # applied to over-quoted strings, i.e. more or less [STRING], due
1798 # to the limitations of m4_bpatsubsts.  So the leading space in STRING
1799 # is the *second* character; equally for the trailing space.
1800 m4_define([m4_strip],
1801 [m4_bpatsubsts([$1 ],
1802                [[        ]+], [ ],
1803                [^. ?\(.*\) .$], [[[\1]]])])
1806 # m4_normalize(STRING)
1807 # --------------------
1808 # Apply m4_flatten and m4_strip to STRING.
1810 # The argument is quoted, so that the macro is robust to active symbols:
1812 #    m4_define(active, ACTIVE)
1813 #    m4_normalize([  act\
1814 #    ive
1815 #    active ])end
1816 #    => active activeend
1818 m4_define([m4_normalize],
1819 [m4_strip(m4_flatten([$1]))])
1823 # m4_join(SEP, ARG1, ARG2...)
1824 # ---------------------------
1825 # Produce ARG1SEPARG2...SEPARGn.  Avoid back-to-back SEP when a given ARG
1826 # is the empty string.  No expansion is performed on SEP or ARGs.
1828 # Since the number of arguments to join can be arbitrarily long, we
1829 # want to avoid having more than one $@ in the macro definition;
1830 # otherwise, the expansion would require twice the memory of the already
1831 # long list.  Hence, m4_join merely looks for the first non-empty element,
1832 # and outputs just that element; while _m4_join looks for all non-empty
1833 # elements, and outputs them following a separator.  The final trick to
1834 # note is that we decide between recursing with $0 or _$0 based on the
1835 # nested m4_if ending with `_'.
1836 m4_define([m4_join],
1837 [m4_if([$#], [1], [],
1838        [$#], [2], [[$2]],
1839        [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift2($@))])])
1840 m4_define([_m4_join],
1841 [m4_if([$#$2], [2], [],
1842        [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift2($@))])])
1845 # m4_combine([SEPARATOR], PREFIX-LIST, [INFIX], SUFFIX...)
1846 # --------------------------------------------------------
1847 # Produce the pairwise combination of every element in the quoted,
1848 # comma-separated PREFIX-LIST with every element from the SUFFIX arguments.
1849 # Each pair is joined with INFIX, and pairs are separated by SEPARATOR.
1850 # No expansion occurs on SEPARATOR, INFIX, or elements of either list.
1852 # For example:
1853 #   m4_combine([, ], [[a], [b], [c]], [-], [1], [2], [3])
1854 #   => a-1, a-2, a-3, b-1, b-2, b-3, c-1, c-2, c-3
1856 # In order to have the correct number of SEPARATORs, we use a temporary
1857 # variable that redefines itself after the first use.  We use m4_builtin
1858 # to avoid m4_defn overhead, but must use defn rather than overquoting
1859 # in case PREFIX or SUFFIX contains $1.  Likewise, we compute the m4_shift3
1860 # only once, rather than in each iteration of the outer m4_foreach.
1861 m4_define([m4_combine],
1862 [m4_if(m4_eval([$# > 3]), [1],
1863        [m4_pushdef([m4_Separator], [m4_define([m4_Separator],
1864                                     m4_builtin([defn], [m4_echo]))])]]dnl
1865 [[m4_foreach([m4_Prefix], [$2],
1866              [m4_foreach([m4_Suffix], ]m4_dquote(m4_dquote(m4_shift3($@)))[,
1867                          [m4_Separator([$1])[]m4_builtin([defn],
1868                                       [m4_Prefix])[$3]m4_builtin([defn],
1869                                                       [m4_Suffix])])])]]dnl
1870 [[m4_builtin([popdef], [m4_Separator])])])
1873 # m4_append(MACRO-NAME, STRING, [SEPARATOR])
1874 # ------------------------------------------
1875 # Redefine MACRO-NAME to hold its former content plus `SEPARATOR`'STRING'
1876 # at the end.  It is valid to use this macro with MACRO-NAME undefined,
1877 # in which case no SEPARATOR is added.  Be aware that the criterion is
1878 # `not being defined', and not `not being empty'.
1880 # Note that neither STRING nor SEPARATOR are expanded here; rather, when
1881 # you expand MACRO-NAME, they will be expanded at that point in time.
1883 # This macro is robust to active symbols.  It can be used to grow
1884 # strings.
1886 #    | m4_define(active, ACTIVE)dnl
1887 #    | m4_append([sentence], [This is an])dnl
1888 #    | m4_append([sentence], [ active ])dnl
1889 #    | m4_append([sentence], [symbol.])dnl
1890 #    | sentence
1891 #    | m4_undefine([active])dnl
1892 #    | sentence
1893 #    => This is an ACTIVE symbol.
1894 #    => This is an active symbol.
1896 # It can be used to define hooks.
1898 #    | m4_define(active, ACTIVE)dnl
1899 #    | m4_append([hooks], [m4_define([act1], [act2])])dnl
1900 #    | m4_append([hooks], [m4_define([act2], [active])])dnl
1901 #    | m4_undefine([active])dnl
1902 #    | act1
1903 #    | hooks
1904 #    | act1
1905 #    => act1
1906 #    =>
1907 #    => active
1909 # It can also be used to create lists, although this particular usage was
1910 # broken prior to autoconf 2.62.
1911 #    | m4_append([list], [one], [, ])dnl
1912 #    | m4_append([list], [two], [, ])dnl
1913 #    | m4_append([list], [three], [, ])dnl
1914 #    | list
1915 #    | m4_dquote(list)
1916 #    => one, two, three
1917 #    => [one],[two],[three]
1919 # Use m4_builtin to avoid overhead of m4_defn.
1920 m4_define([m4_append],
1921 [m4_define([$1],
1922            m4_ifdef([$1], [m4_builtin([defn], [$1])[$3]])[$2])])
1925 # m4_append_uniq(MACRO-NAME, STRING, [SEPARATOR], [IF-UNIQ], [IF-DUP])
1926 # --------------------------------------------------------------------
1927 # Like `m4_append', but append only if not yet present.  Additionally,
1928 # expand IF-UNIQ if STRING was appended, or IF-DUP if STRING was already
1929 # present.  Also, warn if SEPARATOR is not empty and occurs within STRING,
1930 # as the algorithm no longer guarantees uniqueness.
1931 m4_define([m4_append_uniq],
1932 [m4_ifval([$3], [m4_if(m4_index([$2], [$3]), [-1], [],
1933                        [m4_warn([syntax],
1934                                 [$0: `$2' contains `$3'])])])_$0($@)])
1935 m4_define([_m4_append_uniq],
1936 [m4_ifdef([$1],
1937           [m4_if(m4_index([$3]m4_builtin([defn], [$1])[$3], [$3$2$3]), [-1],
1938                  [m4_append([$1], [$2], [$3])$4], [$5])],
1939           [m4_append([$1], [$2], [$3])$4])])
1941 # m4_append_uniq_w(MACRO-NAME, STRINGS)
1942 # -------------------------------------
1943 # For each of the words in the whitespace separated list STRINGS, append
1944 # only the unique strings to the definition of MACRO-NAME.
1946 # Avoid overhead of m4_defn by using m4_builtin.
1947 m4_define([m4_append_uniq_w],
1948 [m4_foreach_w([m4_Word], [$2],
1949               [_m4_append_uniq([$1], m4_builtin([defn], [m4_Word]), [ ])])])
1952 # m4_text_wrap(STRING, [PREFIX], [FIRST-PREFIX], [WIDTH])
1953 # -------------------------------------------------------
1954 # Expands into STRING wrapped to hold in WIDTH columns (default = 79).
1955 # If PREFIX is given, each line is prefixed with it.  If FIRST-PREFIX is
1956 # specified, then the first line is prefixed with it.  As a special case,
1957 # if the length of FIRST-PREFIX is greater than that of PREFIX, then
1958 # FIRST-PREFIX will be left alone on the first line.
1960 # No expansion occurs on the contents STRING, PREFIX, or FIRST-PREFIX,
1961 # although quadrigraphs are correctly recognized.
1963 # Typical outputs are:
1965 # m4_text_wrap([Short string */], [   ], [/* ], 20)
1966 #  => /* Short string */
1968 # m4_text_wrap([Much longer string */], [   ], [/* ], 20)
1969 #  => /* Much longer
1970 #  =>    string */
1972 # m4_text_wrap([Short doc.], [          ], [  --short ], 30)
1973 #  =>   --short Short doc.
1975 # m4_text_wrap([Short doc.], [          ], [  --too-wide ], 30)
1976 #  =>   --too-wide
1977 #  =>           Short doc.
1979 # m4_text_wrap([Super long documentation.], [          ], [  --too-wide ], 30)
1980 #  =>   --too-wide
1981 #  =>      Super long
1982 #  =>      documentation.
1984 # FIXME: there is no checking of a longer PREFIX than WIDTH, but do
1985 # we really want to bother with people trying each single corner
1986 # of a software?
1988 # This macro does not leave a trailing space behind the last word of a line,
1989 # which complicates it a bit.  The algorithm is otherwise stupid and simple:
1990 # all the words are preceded by m4_Separator which is defined to empty for
1991 # the first word, and then ` ' (single space) for all the others.
1993 # The algorithm uses a helper that uses $2 through $4 directly, rather than
1994 # using local variables, to avoid m4_defn overhead, or expansion swallowing
1995 # any $.  It also bypasses m4_popdef overhead with m4_builtin since no user
1996 # macro expansion occurs in the meantime.  Also, the definition is written
1997 # with m4_do, to avoid time wasted on dnl during expansion (since this is
1998 # already a time-consuming macro).
1999 m4_define([m4_text_wrap],
2000 [_$0([$1], [$2], m4_if([$3], [], [[$2]], [[$3]]),
2001      m4_if([$4], [], [79], [[$4]]))])
2002 m4_define([_m4_text_wrap],
2003 m4_do(dnl set up local variables, to avoid repeated calculations
2004 [[m4_pushdef([m4_Indent], m4_qlen([$2]))]],
2005 [[m4_pushdef([m4_Cursor], m4_qlen([$3]))]],
2006 [[m4_pushdef([m4_Separator], [m4_define([m4_Separator], [ ])])]],
2007 dnl expand the first prefix, then check its length vs. regular prefix
2008 dnl same length: nothing special
2009 dnl prefix1 longer: output on line by itself, and reset cursor
2010 dnl prefix1 shorter: pad to length of prefix, and reset cursor
2011 [[[$3]m4_cond([m4_Cursor], m4_Indent, [],
2012               [m4_eval(m4_Cursor > m4_Indent)], [1], [
2013 [$2]m4_define([m4_Cursor], m4_Indent)],
2014               [m4_format([%*s], m4_max([0],
2015   m4_eval(m4_Indent - m4_Cursor)), [])m4_define([m4_Cursor], m4_Indent)])]],
2016 dnl now, for each word, compute the curser after the word is output, then
2017 dnl check if the cursor would exceed the wrap column
2018 dnl if so, reset cursor, and insert newline and prefix
2019 dnl if not, insert the separator (usually a space)
2020 dnl either way, insert the word
2021 [[m4_foreach_w([m4_Word], [$1],
2022   [m4_define([m4_Cursor],
2023              m4_eval(m4_Cursor + m4_qlen(m4_builtin([defn], [m4_Word]))
2024                      + 1))m4_if(m4_eval(m4_Cursor > ([$4])),
2025       [1], [m4_define([m4_Cursor],
2026                       m4_eval(m4_Indent
2027                               + m4_qlen(m4_builtin([defn], [m4_Word])) + 1))
2028 [$2]],
2029       [m4_Separator[]])m4_builtin([defn], [m4_Word])])]],
2030 dnl finally, clean up the local variabls
2031 [[m4_builtin([popdef], [m4_Separator])]],
2032 [[m4_builtin([popdef], [m4_Cursor])]],
2033 [[m4_builtin([popdef], [m4_Indent])]]))
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]],
2062                             [@\(\(<:\|:>\|S|\|%:\|\{:\|:\}\)\(@\)\|&t@\)],
2063                             [\3]))])])
2066 # m4_qdelta(STRING)
2067 # -----------------
2068 # Expands to the net change in the length of STRING from autom4te converting the
2069 # quadrigraphs in STRING.  This number is always negative or zero.
2070 m4_define([m4_qdelta],
2071 [m4_eval(m4_qlen([$1]) - m4_len([$1]))])
2075 ## ----------------------- ##
2076 ## 13. Number processing.  ##
2077 ## ----------------------- ##
2079 # m4_cmp(A, B)
2080 # ------------
2081 # Compare two integer expressions.
2082 # A < B -> -1
2083 # A = B ->  0
2084 # A > B ->  1
2085 m4_define([m4_cmp],
2086 [m4_eval((([$1]) > ([$2])) - (([$1]) < ([$2])))])
2089 # m4_list_cmp(A, B)
2090 # -----------------
2092 # Compare the two lists of integer expressions A and B.  For instance:
2093 #   m4_list_cmp([1, 0],     [1])    ->  0
2094 #   m4_list_cmp([1, 0],     [1, 0]) ->  0
2095 #   m4_list_cmp([1, 2],     [1, 0]) ->  1
2096 #   m4_list_cmp([1, 2, 3],  [1, 2]) ->  1
2097 #   m4_list_cmp([1, 2, -3], [1, 2]) -> -1
2098 #   m4_list_cmp([1, 0],     [1, 2]) -> -1
2099 #   m4_list_cmp([1],        [1, 2]) -> -1
2100 #   m4_define([xa], [oops])dnl
2101 #   m4_list_cmp([[0xa]],    [5+5])  -> 0
2103 # Rather than face the overhead of m4_case, we use a helper function whose
2104 # expansion includes the name of the macro to invoke on the tail, either
2105 # m4_ignore or m4_unquote.  This is particularly useful when comparing
2106 # long lists, since less text is being expanded for deciding when to end
2107 # recursion.
2108 m4_define([m4_list_cmp],
2109 [m4_if([$1$2], [], 0,
2110        [$1], [], [$0(0, [$2])],
2111        [$2], [], [$0([$1], 0)],
2112        [$1], [$2], 0,
2113        [_$0(m4_cmp(m4_car($1), m4_car($2)))([$0(m4_cdr($1), m4_cdr($2))])])])
2114 m4_define([_m4_list_cmp],
2115 [m4_if([$1], 0, [m4_unquote], [$1m4_ignore])])
2117 # m4_max(EXPR, ...)
2118 # m4_min(EXPR, ...)
2119 # -----------------
2120 # Return the decimal value of the maximum (or minimum) in a series of
2121 # integer expressions.
2123 # M4 1.4.x doesn't provide ?:.  Hence this huge m4_eval.  Avoid m4_eval
2124 # if both arguments are identical, but be aware of m4_max(0xa, 10) (hence
2125 # the use of <=, not just <, in the second multiply).
2126 m4_define([m4_max],
2127 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
2128        [$#], [1], [m4_eval([$1])],
2129        [$#$1], [2$2], [m4_eval([$1])],
2130        [$#], [2],
2131        [m4_eval((([$1]) > ([$2])) * ([$1]) + (([$1]) <= ([$2])) * ([$2]))],
2132        [$0($0([$1], [$2]), m4_shift2($@))])])
2133 m4_define([m4_min],
2134 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
2135        [$#], [1], [m4_eval([$1])],
2136        [$#$1], [2$2], [m4_eval([$1])],
2137        [$#], [2],
2138        [m4_eval((([$1]) < ([$2])) * ([$1]) + (([$1]) >= ([$2])) * ([$2]))],
2139        [$0($0([$1], [$2]), m4_shift2($@))])])
2142 # m4_sign(A)
2143 # ----------
2144 # The sign of the integer expression A.
2145 m4_define([m4_sign],
2146 [m4_eval((([$1]) > 0) - (([$1]) < 0))])
2150 ## ------------------------ ##
2151 ## 14. Version processing.  ##
2152 ## ------------------------ ##
2155 # m4_version_unletter(VERSION)
2156 # ----------------------------
2157 # Normalize beta version numbers with letters to numeric expressions, which
2158 # can then be handed to m4_eval for the purpose of comparison.
2160 #   Nl -> (N+1).-1.(l#)
2162 # for example:
2163 #   [2.14a] -> [2.14+1.-1.[0r36:a]] -> 2.15.-1.10
2164 #   [2.14b] -> [2.15+1.-1.[0r36:b]] -> 2.15.-1.11
2165 #   [2.61aa.b] -> [2.61+1.-1.[0r36:aa],+1.-1.[0r36:b]] -> 2.62.-1.370.1.-1.11
2167 # This macro expects reasonable version numbers, but can handle double
2168 # letters and does not expand any macros.  Original version strings can
2169 # use both `.' and `-' separators.
2171 # Inline constant expansions, to avoid m4_defn overhead.
2172 # _m4_version_unletter is the real workhorse used by m4_version_compare,
2173 # but since [0r36:a] is less readable than 10, we provide a wrapper for
2174 # human use.
2175 m4_define([m4_version_unletter],
2176 [m4_map_sep([m4_eval], [.],
2177             m4_dquote(m4_dquote_elt(m4_unquote(_$0([$1])))))])
2178 m4_define([_m4_version_unletter],
2179 [m4_bpatsubst(m4_translit([[[$1]]], [.-], [,,]),]dnl
2180 m4_dquote(m4_dquote(m4_defn([m4_cr_Letters])))[[+],
2181               [+1,-1,[0r36:\&]])])
2184 # m4_version_compare(VERSION-1, VERSION-2)
2185 # ----------------------------------------
2186 # Compare the two version numbers and expand into
2187 #  -1 if VERSION-1 < VERSION-2
2188 #   0 if           =
2189 #   1 if           >
2190 m4_define([m4_version_compare],
2191 [m4_list_cmp(_m4_version_unletter([$1]), _m4_version_unletter([$2]))])
2194 # m4_PACKAGE_NAME
2195 # m4_PACKAGE_TARNAME
2196 # m4_PACKAGE_VERSION
2197 # m4_PACKAGE_STRING
2198 # m4_PACKAGE_BUGREPORT
2199 # --------------------
2200 m4_include([m4sugar/version.m4])
2203 # m4_version_prereq(VERSION, [IF-OK], [IF-NOT = FAIL])
2204 # ----------------------------------------------------
2205 # Check this Autoconf version against VERSION.
2206 m4_define([m4_version_prereq],
2207 [m4_if(m4_version_compare(]m4_dquote(m4_defn([m4_PACKAGE_VERSION]))[, [$1]),
2208        [-1],
2209        [m4_default([$3],
2210                    [m4_fatal([Autoconf version $1 or higher is required],
2211                              [63])])],
2212        [$2])])
2216 ## ------------------- ##
2217 ## 15. File handling.  ##
2218 ## ------------------- ##
2221 # It is a real pity that M4 comes with no macros to bind a diversion
2222 # to a file.  So we have to deal without, which makes us a lot more
2223 # fragile than we should.
2226 # m4_file_append(FILE-NAME, CONTENT)
2227 # ----------------------------------
2228 m4_define([m4_file_append],
2229 [m4_syscmd([cat >>$1 <<_m4eof
2231 _m4eof
2233 m4_if(m4_sysval, [0], [],
2234       [m4_fatal([$0: cannot write: $1])])])
2238 ## ------------------------ ##
2239 ## 16. Setting M4sugar up.  ##
2240 ## ------------------------ ##
2243 # m4_init
2244 # -------
2245 # Initialize the m4sugar language.
2246 m4_define([m4_init],
2247 [# All the M4sugar macros start with `m4_', except `dnl' kept as is
2248 # for sake of simplicity.
2249 m4_pattern_forbid([^_?m4_])
2250 m4_pattern_forbid([^dnl$])
2252 # _m4_divert_diversion should be defined:
2253 m4_divert_push([KILL])
2255 # Check the divert push/pop perfect balance.
2256 m4_wrap([m4_divert_pop([])
2257          m4_ifdef([_m4_divert_diversion],
2258            [m4_fatal([$0: unbalanced m4_divert_push:]_m4_divert_n_stack)])[]])