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