Avoid extra side effects in m4sugar list expansion.
[autoconf.git] / lib / m4sugar / m4sugar.m4
blobf2fde7ac8164e2b23808edfcb48eb1d76cfe0a39
1 divert(-1)#                                                  -*- Autoconf -*-
2 # This file is part of Autoconf.
3 # Base M4 layer.
4 # Requires GNU M4.
6 # Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7 # 2008 Free Software Foundation, Inc.
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2, or (at your option)
12 # any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 # 02110-1301, USA.
24 # As a special exception, the Free Software Foundation gives unlimited
25 # permission to copy, distribute and modify the configure scripts that
26 # are the output of Autoconf.  You need not follow the terms of the GNU
27 # General Public License when using or distributing such scripts, even
28 # though portions of the text of Autoconf appear in them.  The GNU
29 # General Public License (GPL) does govern all other use of the material
30 # that constitutes the Autoconf program.
32 # Certain portions of the Autoconf source text are designed to be copied
33 # (in certain cases, depending on the input) into the output of
34 # Autoconf.  We call these the "data" portions.  The rest of the Autoconf
35 # source text consists of comments plus executable code that decides which
36 # of the data portions to output in any given case.  We call these
37 # comments and executable code the "non-data" portions.  Autoconf never
38 # copies any of the non-data portions into its output.
40 # This special exception to the GPL applies to versions of Autoconf
41 # released by the Free Software Foundation.  When you make and
42 # distribute a modified version of Autoconf, you may extend this special
43 # exception to the GPL to apply to your modified version as well, *unless*
44 # your modified version has the potential to copy into its output some
45 # of the text that was the non-data portion of the version that you started
46 # with.  (In other words, unless your change moves or copies text from
47 # the non-data portions to the data portions.)  If your modification has
48 # such potential, you must delete any notice of this special exception
49 # to the GPL from your modified version.
51 # Written by Akim Demaille.
54 # Set the quotes, whatever the current quoting system.
55 changequote()
56 changequote([, ])
58 # Some old m4's don't support m4exit.  But they provide
59 # equivalent functionality by core dumping because of the
60 # long macros we define.
61 ifdef([__gnu__], ,
62 [errprint(M4sugar requires GNU M4. Install it before installing M4sugar or
63 set the M4 environment variable to its absolute file name.)
64 m4exit(2)])
67 ## ------------------------------- ##
68 ## 1. Simulate --prefix-builtins.  ##
69 ## ------------------------------- ##
71 # m4_define
72 # m4_defn
73 # m4_undefine
74 define([m4_define],   defn([define]))
75 define([m4_defn],     defn([defn]))
76 define([m4_undefine], defn([undefine]))
78 m4_undefine([define])
79 m4_undefine([defn])
80 m4_undefine([undefine])
83 # m4_copy(SRC, DST)
84 # -----------------
85 # Define DST as the definition of SRC.
86 # What's the difference between:
87 # 1. m4_copy([from], [to])
88 # 2. m4_define([to], [from($@)])
89 # Well, obviously 1 is more expensive in space.  Maybe 2 is more expensive
90 # in time, but because of the space cost of 1, it's not that obvious.
91 # Nevertheless, one huge difference is the handling of `$0'.  If `from'
92 # uses `$0', then with 1, `to''s `$0' is `to', while it is `from' in 2.
93 # The user would certainly prefer to see `to'.
94 m4_define([m4_copy],
95 [m4_define([$2], m4_defn([$1]))])
98 # m4_rename(SRC, DST)
99 # -------------------
100 # Rename the macro SRC to DST.
101 m4_define([m4_rename],
102 [m4_copy([$1], [$2])m4_undefine([$1])])
105 # m4_rename_m4(MACRO-NAME)
106 # ------------------------
107 # Rename MACRO-NAME to m4_MACRO-NAME.
108 m4_define([m4_rename_m4],
109 [m4_rename([$1], [m4_$1])])
112 # m4_copy_unm4(m4_MACRO-NAME)
113 # ---------------------------
114 # Copy m4_MACRO-NAME to MACRO-NAME.
115 m4_define([m4_copy_unm4],
116 [m4_copy([$1], m4_bpatsubst([$1], [^m4_\(.*\)], [[\1]]))])
119 # Some m4 internals have names colliding with tokens we might use.
120 # Rename them a` la `m4 --prefix-builtins'.  Conditionals first, since
121 # some subsequent renames are conditional.
122 m4_rename_m4([ifdef])
123 m4_rename([ifelse], [m4_if])
125 m4_rename_m4([builtin])
126 m4_rename_m4([changecom])
127 m4_rename_m4([changequote])
128 m4_ifdef([changeword],dnl conditionally available in 1.4.x
129 [m4_undefine([changeword])])
130 m4_rename_m4([debugfile])
131 m4_rename_m4([debugmode])
132 m4_rename_m4([decr])
133 m4_undefine([divert])
134 m4_rename_m4([divnum])
135 m4_rename_m4([dumpdef])
136 m4_rename_m4([errprint])
137 m4_rename_m4([esyscmd])
138 m4_rename_m4([eval])
139 m4_rename_m4([format])
140 m4_undefine([include])
141 m4_rename_m4([incr])
142 m4_rename_m4([index])
143 m4_rename_m4([indir])
144 m4_rename_m4([len])
145 m4_rename([m4exit], [m4_exit])
146 m4_undefine([m4wrap])
147 m4_ifdef([mkstemp],dnl added in M4 1.4.8
148 [m4_rename_m4([mkstemp])
149 m4_copy([m4_mkstemp], [m4_maketemp])
150 m4_undefine([maketemp])],
151 [m4_rename_m4([maketemp])
152 m4_copy([m4_maketemp], [m4_mkstemp])])
153 m4_rename([patsubst], [m4_bpatsubst])
154 m4_rename_m4([popdef])
155 m4_rename_m4([pushdef])
156 m4_rename([regexp], [m4_bregexp])
157 m4_rename_m4([shift])
158 m4_undefine([sinclude])
159 m4_rename_m4([substr])
160 m4_ifdef([symbols],dnl present only in alpha-quality 1.4o
161 [m4_rename_m4([symbols])])
162 m4_rename_m4([syscmd])
163 m4_rename_m4([sysval])
164 m4_rename_m4([traceoff])
165 m4_rename_m4([traceon])
166 m4_rename_m4([translit])
167 m4_undefine([undivert])
170 ## ------------------- ##
171 ## 2. Error messages.  ##
172 ## ------------------- ##
175 # m4_location
176 # -----------
177 m4_define([m4_location],
178 [__file__:__line__])
181 # m4_errprintn(MSG)
182 # -----------------
183 # Same as `errprint', but with the missing end of line.
184 m4_define([m4_errprintn],
185 [m4_errprint([$1
186 ])])
189 # m4_warning(MSG)
190 # ---------------
191 # Warn the user.
192 m4_define([m4_warning],
193 [m4_errprintn(m4_location[: warning: $1])])
196 # m4_fatal(MSG, [EXIT-STATUS])
197 # ----------------------------
198 # Fatal the user.                                                      :)
199 m4_define([m4_fatal],
200 [m4_errprintn(m4_location[: error: $1])dnl
201 m4_expansion_stack_dump()dnl
202 m4_exit(m4_if([$2],, 1, [$2]))])
205 # m4_assert(EXPRESSION, [EXIT-STATUS = 1])
206 # ----------------------------------------
207 # This macro ensures that EXPRESSION evaluates to true, and exits if
208 # EXPRESSION evaluates to false.
209 m4_define([m4_assert],
210 [m4_if(m4_eval([$1]), 0,
211        [m4_fatal([assert failed: $1], [$2])])])
215 ## ------------- ##
216 ## 3. Warnings.  ##
217 ## ------------- ##
220 # _m4_warn(CATEGORY, MESSAGE, STACK-TRACE)
221 # ----------------------------------------
222 # Report a MESSAGE to the user if the CATEGORY of warnings is enabled.
223 # This is for traces only.
224 # The STACK-TRACE is a \n-separated list of "LOCATION: MESSAGE".
226 # Within m4, the macro is a no-op.  This macro really matters
227 # when autom4te post-processes the trace output.
228 m4_define([_m4_warn], [])
231 # m4_warn(CATEGORY, MESSAGE)
232 # --------------------------
233 # Report a MESSAGE to the user if the CATEGORY of warnings is enabled.
234 m4_define([m4_warn],
235 [_m4_warn([$1], [$2],
236 m4_ifdef([m4_expansion_stack],
237          [_m4_defn([m4_expansion_stack])
238 m4_location[: the top level]]))dnl
243 ## ------------------- ##
244 ## 4. File inclusion.  ##
245 ## ------------------- ##
248 # We also want to neutralize include (and sinclude for symmetry),
249 # but we want to extend them slightly: warn when a file is included
250 # several times.  This is, in general, a dangerous operation, because
251 # too many people forget to quote the first argument of m4_define.
253 # For instance in the following case:
254 #   m4_define(foo, [bar])
255 # then a second reading will turn into
256 #   m4_define(bar, [bar])
257 # which is certainly not what was meant.
259 # m4_include_unique(FILE)
260 # -----------------------
261 # Declare that the FILE was loading; and warn if it has already
262 # been included.
263 m4_define([m4_include_unique],
264 [m4_ifdef([m4_include($1)],
265           [m4_warn([syntax], [file `$1' included several times])])dnl
266 m4_define([m4_include($1)])])
269 # m4_include(FILE)
270 # ----------------
271 # Like the builtin include, but warns against multiple inclusions.
272 m4_define([m4_include],
273 [m4_include_unique([$1])dnl
274 m4_builtin([include], [$1])])
277 # m4_sinclude(FILE)
278 # -----------------
279 # Like the builtin sinclude, but warns against multiple inclusions.
280 m4_define([m4_sinclude],
281 [m4_include_unique([$1])dnl
282 m4_builtin([sinclude], [$1])])
286 ## ------------------------------------ ##
287 ## 5. Additional branching constructs.  ##
288 ## ------------------------------------ ##
290 # Both `m4_ifval' and `m4_ifset' tests against the empty string.  The
291 # difference is that `m4_ifset' is specialized on macros.
293 # In case of arguments of macros, eg. $1, it makes little difference.
294 # In the case of a macro `FOO', you don't want to check `m4_ifval(FOO,
295 # TRUE)', because if `FOO' expands with commas, there is a shifting of
296 # the arguments.  So you want to run `m4_ifval([FOO])', but then you just
297 # compare the *string* `FOO' against `', which, of course fails.
299 # So you want the variation `m4_ifset' that expects a macro name as $1.
300 # If this macro is both defined and defined to a non empty value, then
301 # it runs TRUE, etc.
304 # m4_ifval(COND, [IF-TRUE], [IF-FALSE])
305 # -------------------------------------
306 # If COND is not the empty string, expand IF-TRUE, otherwise IF-FALSE.
307 # Comparable to m4_ifdef.
308 m4_define([m4_ifval],
309 [m4_if([$1], [], [$3], [$2])])
312 # m4_n(TEXT)
313 # ----------
314 # If TEXT is not empty, return TEXT and a new line, otherwise nothing.
315 m4_define([m4_n],
316 [m4_if([$1],
317        [], [],
318            [$1
319 ])])
322 # m4_ifvaln(COND, [IF-TRUE], [IF-FALSE])
323 # --------------------------------------
324 # Same as `m4_ifval', but add an extra newline to IF-TRUE or IF-FALSE
325 # unless that argument is empty.
326 m4_define([m4_ifvaln],
327 [m4_if([$1],
328        [],   [m4_n([$3])],
329              [m4_n([$2])])])
332 # m4_ifset(MACRO, [IF-TRUE], [IF-FALSE])
333 # --------------------------------------
334 # If MACRO has no definition, or of its definition is the empty string,
335 # expand IF-FALSE, otherwise IF-TRUE.
336 m4_define([m4_ifset],
337 [m4_ifdef([$1],
338           [m4_ifval(_m4_defn([$1]), [$2], [$3])],
339           [$3])])
342 # m4_ifndef(NAME, [IF-NOT-DEFINED], [IF-DEFINED])
343 # -----------------------------------------------
344 m4_define([m4_ifndef],
345 [m4_ifdef([$1], [$3], [$2])])
348 # m4_case(SWITCH, VAL1, IF-VAL1, VAL2, IF-VAL2, ..., DEFAULT)
349 # -----------------------------------------------------------
350 # m4 equivalent of
351 # switch (SWITCH)
352 # {
353 #   case VAL1:
354 #     IF-VAL1;
355 #     break;
356 #   case VAL2:
357 #     IF-VAL2;
358 #     break;
359 #   ...
360 #   default:
361 #     DEFAULT;
362 #     break;
363 # }.
364 # All the values are optional, and the macro is robust to active
365 # symbols properly quoted.
366 m4_define([m4_case],
367 [m4_if([$#], 0, [],
368        [$#], 1, [],
369        [$#], 2, [$2],
370        [$1], [$2], [$3],
371        [$0([$1], m4_shift3($@))])])
374 # m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT)
375 # -----------------------------------------------------
376 # m4 equivalent of
378 # if (SWITCH =~ RE1)
379 #   VAL1;
380 # elif (SWITCH =~ RE2)
381 #   VAL2;
382 # elif ...
383 #   ...
384 # else
385 #   DEFAULT
387 # All the values are optional, and the macro is robust to active symbols
388 # properly quoted.
389 m4_define([m4_bmatch],
390 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
391        [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
392        [$#], 2, [$2],
393        [m4_if(m4_bregexp([$1], [$2]), -1, [$0([$1], m4_shift3($@))],
394               [$3])])])
397 # m4_car(LIST)
398 # m4_cdr(LIST)
399 # ------------
400 # Manipulate m4 lists.
401 m4_define([m4_car], [[$1]])
402 m4_define([m4_cdr],
403 [m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])],
404        [$#], 1, [],
405        [m4_dquote(m4_shift($@))])])
407 # _m4_cdr(LIST)
408 # -------------
409 # Like m4_cdr, except include a leading comma unless only one element
410 # remains.  Why?  Because comparing a large list against [] is more
411 # expensive in expansion time than comparing the number of arguments; so
412 # _m4_cdr can be used to reduce the number of arguments when it is time
413 # to end recursion.
414 m4_define([_m4_cdr],
415 [m4_if([$#], 1, [],
416        [, m4_dquote(m4_shift($@))])])
420 # m4_cond(TEST1, VAL1, IF-VAL1, TEST2, VAL2, IF-VAL2, ..., [DEFAULT])
421 # -------------------------------------------------------------------
422 # Similar to m4_if, except that each TEST is expanded when encountered.
423 # If the expansion of TESTn matches the string VALn, the result is IF-VALn.
424 # The result is DEFAULT if no tests passed.  This macro allows
425 # short-circuiting of expensive tests, where it pays to arrange quick
426 # filter tests to run first.
428 # For an example, consider a previous implementation of _AS_QUOTE_IFELSE:
430 #    m4_if(m4_index([$1], [\]), [-1], [$2],
431 #          m4_eval(m4_index([$1], [\\]) >= 0), [1], [$2],
432 #          m4_eval(m4_index([$1], [\$]) >= 0), [1], [$2],
433 #          m4_eval(m4_index([$1], [\`]) >= 0), [1], [$3],
434 #          m4_eval(m4_index([$1], [\"]) >= 0), [1], [$3],
435 #          [$2])
437 # Here, m4_index is computed 5 times, and m4_eval 4, even if $1 contains
438 # no backslash.  It is more efficient to do:
440 #    m4_cond([m4_index([$1], [\])], [-1], [$2],
441 #            [m4_eval(m4_index([$1], [\\]) >= 0)], [1], [$2],
442 #            [m4_eval(m4_index([$1], [\$]) >= 0)], [1], [$2],
443 #            [m4_eval(m4_index([$1], [\`]) >= 0)], [1], [$3],
444 #            [m4_eval(m4_index([$1], [\"]) >= 0)], [1], [$3],
445 #            [$2])
447 # In the common case of $1 with no backslash, only one m4_index expansion
448 # occurs, and m4_eval is avoided altogether.
449 m4_define([m4_cond],
450 [m4_if([$#], [0], [m4_fatal([$0: cannot be called without arguments])],
451        [$#], [1], [$1],
452        m4_eval([$# % 3]), [2], [m4_fatal([$0: missing an argument])],
453        [_$0($@)])])
455 m4_define([_m4_cond],
456 [m4_if(($1), [($2)], [$3],
457        [$#], [3], [],
458        [$#], [4], [$4],
459        [$0(m4_shift3($@))])])
462 ## ---------------------------------------- ##
463 ## 6. Enhanced version of some primitives.  ##
464 ## ---------------------------------------- ##
466 # m4_bpatsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...)
467 # ----------------------------------------------------
468 # m4 equivalent of
470 #   $_ = STRING;
471 #   s/RE1/SUBST1/g;
472 #   s/RE2/SUBST2/g;
473 #   ...
475 # All the values are optional, and the macro is robust to active symbols
476 # properly quoted.
478 # I would have liked to name this macro `m4_bpatsubst', unfortunately,
479 # due to quotation problems, I need to double quote $1 below, therefore
480 # the anchors are broken :(  I can't let users be trapped by that.
482 # Recall that m4_shift3 always results in an argument.  Hence, we need
483 # to distinguish between a final deletion vs. ending recursion.
484 m4_define([m4_bpatsubsts],
485 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
486        [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
487        [$#], 2, [m4_unquote(m4_builtin([patsubst], [[$1]], [$2]))],
488        [$#], 3, [m4_unquote(m4_builtin([patsubst], [[$1]], [$2], [$3]))],
489        [_$0($@m4_if(m4_eval($# & 1), 0, [,]))])])
490 m4_define([_m4_bpatsubsts],
491 [m4_if([$#], 2, [$1],
492        [$0(m4_builtin([patsubst], [[$1]], [$2], [$3]),
493            m4_shift3($@))])])
496 # m4_define_default(MACRO, VALUE)
497 # -------------------------------
498 # If MACRO is undefined, set it to VALUE.
499 m4_define([m4_define_default],
500 [m4_ifndef([$1], [m4_define($@)])])
503 # m4_default(EXP1, EXP2)
504 # ----------------------
505 # Returns EXP1 if non empty, otherwise EXP2.
507 # This macro is called on hot paths, so inline the contents of m4_ifval,
508 # for one less round of expansion.
509 m4_define([m4_default],
510 [m4_if([$1], [], [$2], [$1])])
513 # m4_defn(NAME)
514 # -------------
515 # Like the original, except guarantee a warning when using something which is
516 # undefined (unlike M4 1.4.x).  This replacement is not a full-featured
517 # replacement: if any of the defined macros contain unbalanced quoting, but
518 # when pasted together result in a well-quoted string, then only native m4
519 # support is able to get it correct.  But that's where quadrigraphs come in
520 # handy, if you really need unbalanced quotes inside your macros.
522 # This macro is called frequently, so minimize the amount of additional
523 # expansions by skipping m4_ifndef.  Better yet, if __m4_version__ exists,
524 # (added in M4 1.6), then let m4 do the job for us.
526 # _m4_defn is for internal use only - it bypasses the wrapper, so it
527 # must only be used on one argument at a time, and only on macros
528 # known to be defined.  Make sure this still works if the user renames
529 # m4_defn but not _m4_defn.
530 m4_copy([m4_defn], [_m4_defn])
531 m4_ifdef([__m4_version__], [],
532 [m4_define([m4_defn],
533 [m4_if([$#], [0], [[$0]],
534        [$#], [1], [m4_ifdef([$1], [_m4_defn([$1])],
535                             [m4_fatal([$0: undefined macro: $1])])],
536        [m4_foreach([_m4_macro], [$@], [$0(_m4_defn([_m4_macro]))])])])])
539 # _m4_dumpdefs_up(NAME)
540 # ---------------------
541 m4_define([_m4_dumpdefs_up],
542 [m4_ifdef([$1],
543           [m4_pushdef([_m4_dumpdefs], _m4_defn([$1]))dnl
544 m4_dumpdef([$1])dnl
545 _m4_popdef([$1])dnl
546 _m4_dumpdefs_up([$1])])])
549 # _m4_dumpdefs_down(NAME)
550 # -----------------------
551 m4_define([_m4_dumpdefs_down],
552 [m4_ifdef([_m4_dumpdefs],
553           [m4_pushdef([$1], _m4_defn([_m4_dumpdefs]))dnl
554 _m4_popdef([_m4_dumpdefs])dnl
555 _m4_dumpdefs_down([$1])])])
558 # m4_dumpdefs(NAME)
559 # -----------------
560 # Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its
561 # value stack (most recent displayed first).
562 m4_define([m4_dumpdefs],
563 [_m4_dumpdefs_up([$1])dnl
564 _m4_dumpdefs_down([$1])])
567 # m4_popdef(NAME)
568 # ---------------
569 # Like the original, except guarantee a warning when using something which is
570 # undefined (unlike M4 1.4.x).
572 # This macro is called frequently, so minimize the amount of additional
573 # expansions by skipping m4_ifndef.  Better yet, if __m4_version__ exists,
574 # (added in M4 1.6), then let m4 do the job for us.
576 # _m4_popdef is for internal use only - it bypasses the wrapper, so it
577 # must only be used on macros known to be defined.  Make sure this
578 # still works if the user renames m4_popdef but not _m4_popdef.
579 m4_copy([m4_popdef], [_m4_popdef])
580 m4_ifdef([__m4_version__], [],
581 [m4_define([m4_popdef],
582 [m4_if([$#], [0], [[$0]],
583        [$#], [1], [m4_ifdef([$1], [_m4_popdef([$1])],
584                             [m4_fatal([$0: undefined macro: $1])])],
585        [m4_foreach([_m4_macro], [$@], [$0(_m4_defn([_m4_macro]))])])])])
588 # m4_shiftn(N, ...)
589 # -----------------
590 # Returns ... shifted N times.  Useful for recursive "varargs" constructs.
592 # Autoconf does not use this macro, because it is inherently slower than
593 # calling the common cases of m4_shift2 or m4_shift3 directly.  But it
594 # might as well be fast for other clients, such as Libtool.  One way to
595 # do this is to expand $@ only once in _m4_shiftn (otherwise, for long
596 # lists, the expansion of m4_if takes twice as much memory as what the
597 # list itself occupies, only to throw away the unused branch).  The end
598 # result is strictly equivalent to
599 #   m4_if([$1], 1, [m4_shift(,m4_shift(m4_shift($@)))],
600 #         [_m4_shiftn(m4_decr([$1]), m4_shift(m4_shift($@)))])
601 # but with the final `m4_shift(m4_shift($@)))' shared between the two
602 # paths.  The first leg uses a no-op m4_shift(,$@) to balance out the ().
603 m4_define([m4_shiftn],
604 [m4_assert(0 < $1 && $1 < $#)_$0($@)])
606 m4_define([_m4_shiftn],
607 [m4_if([$1], 1, [m4_shift(],
608        [$0(m4_decr([$1])]), m4_shift(m4_shift($@)))])
610 # m4_shift2(...)
611 # m4_shift3(...)
612 # -----------------
613 # Returns ... shifted twice, and three times.  Faster than m4_shiftn.
614 m4_define([m4_shift2], [m4_shift(m4_shift($@))])
615 m4_define([m4_shift3], [m4_shift(m4_shift(m4_shift($@)))])
617 # _m4_shift2(...)
618 # _m4_shift3(...)
619 # ---------------
620 # Like m4_shift2 or m4_shift3, except include a leading comma unless shifting
621 # consumes all arguments.  Why?  Because in recursion, it is nice to
622 # distinguish between 1 element left and 0 elements left, based on how many
623 # arguments this shift expands to.
624 m4_define([_m4_shift2],
625 [m4_if([$#], [2], [],
626        [, m4_shift(m4_shift($@))])])
627 m4_define([_m4_shift3],
628 [m4_if([$#], [3], [],
629        [, m4_shift(m4_shift(m4_shift($@)))])])
632 # m4_undefine(NAME)
633 # -----------------
634 # Like the original, except guarantee a warning when using something which is
635 # undefined (unlike M4 1.4.x).
637 # This macro is called frequently, so minimize the amount of additional
638 # expansions by skipping m4_ifndef.  Better yet, if __m4_version__ exists,
639 # (added in M4 1.6), then let m4 do the job for us.
641 # _m4_undefine is for internal use only - it bypasses the wrapper, so
642 # it must only be used on macros known to be defined.  Make sure this
643 # still works if the user renames m4_undefine but not _m4_undefine.
644 m4_copy([m4_undefine], [_m4_undefine])
645 m4_ifdef([__m4_version__], [],
646 [m4_define([m4_undefine],
647 [m4_if([$#], [0], [[$0]],
648        [$#], [1], [m4_ifdef([$1], [_m4_undefine([$1])],
649                             [m4_fatal([$0: undefined macro: $1])])],
650        [m4_foreach([_m4_macro], [$@], [$0(_m4_defn([_m4_macro]))])])])])
652 # _m4_wrap(PRE, POST)
653 # -------------------
654 # Helper macro for m4_wrap and m4_wrap_lifo.  Allows nested calls to
655 # m4_wrap within wrapped text.  Use _m4_defn and _m4_popdef for speed.
656 m4_define([_m4_wrap],
657 [m4_ifdef([$0_text],
658           [m4_define([$0_text], [$1]_m4_defn([$0_text])[$2])],
659           [m4_builtin([m4wrap], [m4_unquote(
660   _m4_defn([$0_text])_m4_popdef([$0_text]))])m4_define([$0_text], [$1$2])])])
662 # m4_wrap(TEXT)
663 # -------------
664 # Append TEXT to the list of hooks to be executed at the end of input.
665 # Whereas the order of the original may be LIFO in the underlying m4,
666 # this version is always FIFO.
667 m4_define([m4_wrap],
668 [_m4_wrap([], [$1[]])])
670 # m4_wrap_lifo(TEXT)
671 # ------------------
672 # Prepend TEXT to the list of hooks to be executed at the end of input.
673 # Whereas the order of m4_wrap may be FIFO in the underlying m4, this
674 # version is always LIFO.
675 m4_define([m4_wrap_lifo],
676 [_m4_wrap([$1[]])])
678 ## ------------------------- ##
679 ## 7. Quoting manipulation.  ##
680 ## ------------------------- ##
683 # m4_apply(MACRO, LIST)
684 # ---------------------
685 # Invoke MACRO, with arguments provided from the quoted list of
686 # comma-separated quoted arguments.  If LIST is empty, invoke MACRO
687 # without arguments.  The expansion will not be concatenated with
688 # subsequent text.
689 m4_define([m4_apply],
690 [m4_if([$2], [], [$1], [$1($2)])[]])
692 # _m4_apply(MACRO, LIST)
693 # ----------------------
694 # Like m4_apply, except do nothing if LIST is empty.
695 m4_define([_m4_apply],
696 [m4_if([$2], [], [], [$1($2)[]])])
699 # m4_count(ARGS)
700 # --------------
701 # Return a count of how many ARGS are present.
702 m4_define([m4_count], [$#])
705 # m4_do(STRING, ...)
706 # ------------------
707 # This macro invokes all its arguments (in sequence, of course).  It is
708 # useful for making your macros more structured and readable by dropping
709 # unnecessary dnl's and have the macros indented properly.  No concatenation
710 # occurs after a STRING; use m4_unquote(m4_join(,STRING)) for that.
711 m4_define([m4_do],
712 [m4_if([$#], 0, [],
713        [$#], 1, [$1[]],
714        [$1[]$0(m4_shift($@))])])
717 # m4_dquote(ARGS)
718 # ---------------
719 # Return ARGS as a quoted list of quoted arguments.
720 m4_define([m4_dquote],  [[$@]])
723 # m4_dquote_elt(ARGS)
724 # -------------------
725 # Return ARGS as an unquoted list of double-quoted arguments.
726 m4_define([m4_dquote_elt],
727 [m4_if([$#], [0], [],
728        [$#], [1], [[[$1]]],
729        [[[$1]],$0(m4_shift($@))])])
732 # m4_echo(ARGS)
733 # -------------
734 # Return the ARGS, with the same level of quoting.  Whitespace after
735 # unquoted commas are consumed.
736 m4_define([m4_echo], [$@])
739 # m4_expand(ARG)
740 # --------------
741 # Return the expansion of ARG as a single string.  Unlike m4_quote($1), this
742 # correctly preserves whitespace following single-quoted commas that appeared
743 # within ARG.
745 #   m4_define([active], [ACT, IVE])
746 #   m4_define([active2], [[ACT, IVE]])
747 #   m4_quote(active, active2)
748 #   => ACT,IVE,ACT, IVE
749 #   m4_expand([active, active2])
750 #   => ACT, IVE, ACT, IVE
752 # Unfortunately, due to limitations in m4, ARG must expand to something
753 # with balanced quotes (use quadrigraphs to get around this).  The input
754 # is not likely to have unbalanced -=<{(/)}>=- quotes, and it is possible
755 # to have unbalanced (), provided it was specified with proper [] quotes.
757 # Exploit that extra () will group unquoted commas and the following
758 # whitespace, then convert () to [].  m4_bpatsubst can't handle newlines
759 # inside $1, and m4_substr strips quoting.  So we (ab)use m4_changequote.
760 m4_define([m4_expand], [_$0(-=<{($1)}>=-)])
761 m4_define([_m4_expand],
762 [m4_changequote([-=<{(], [)}>=-])$1m4_changequote([, ])])
765 # m4_ignore(ARGS)
766 # ---------------
767 # Expands to nothing.  Useful for conditionally ignoring an arbitrary
768 # number of arguments (see _m4_list_cmp for an example).
769 m4_define([m4_ignore])
772 # m4_make_list(ARGS)
773 # ------------------
774 # Similar to m4_dquote, this creates a quoted list of quoted ARGS.  This
775 # version is less efficient than m4_dquote, but separates each argument
776 # with a comma and newline, rather than just comma, for readability.
777 # When developing an m4sugar algorithm, you could temporarily use
778 #   m4_pushdef([m4_dquote],m4_defn([m4_make_list]))
779 # around your code to make debugging easier.
780 m4_define([m4_make_list], [m4_join([,
781 ], m4_dquote_elt($@))])
784 # m4_noquote(STRING)
785 # ------------------
786 # Return the result of ignoring all quotes in STRING and invoking the
787 # macros it contains.  Amongst other things, this is useful for enabling
788 # macro invocations inside strings with [] blocks (for instance regexps
789 # and help-strings).  On the other hand, since all quotes are disabled,
790 # any macro expanded during this time that relies on nested [] quoting
791 # will likely crash and burn.  This macro is seldom useful; consider
792 # m4_unquote or m4_expand instead.
793 m4_define([m4_noquote],
794 [m4_changequote([-=<{(],[)}>=-])$1-=<{()}>=-m4_changequote([,])])
797 # m4_quote(ARGS)
798 # --------------
799 # Return ARGS as a single argument.  Any whitespace after unquoted commas
800 # is stripped.  There is always output, even when there were no arguments.
802 # It is important to realize the difference between `m4_quote(exp)' and
803 # `[exp]': in the first case you obtain the quoted *result* of the
804 # expansion of EXP, while in the latter you just obtain the string
805 # `exp'.
806 m4_define([m4_quote],  [[$*]])
809 # _m4_quote(ARGS)
810 # ---------------
811 # Like m4_quote, except that when there are no arguments, there is no
812 # output.  For conditional scenarios (such as passing _m4_quote as the
813 # macro name in m4_mapall), this feature can be used to distinguish between
814 # one argument of the empty string vs. no arguments.  However, in the
815 # normal case with arguments present, this is less efficient than m4_quote.
816 m4_define([_m4_quote],
817 [m4_if([$#], [0], [], [[$*]])])
820 # m4_reverse(ARGS)
821 # ----------------
822 # Output ARGS in reverse order.
823 m4_define([m4_reverse],
824 [m4_if([$#], [0], [], [$#], [1], [[$1]],
825        [$0(m4_shift($@)), [$1]])])
828 # m4_unquote(ARGS)
829 # ----------------
830 # Remove one layer of quotes from each ARG, performing one level of
831 # expansion.  For one argument, m4_unquote([arg]) is more efficient than
832 # m4_do([arg]), but for multiple arguments, the difference is that
833 # m4_unquote separates arguments with commas while m4_do concatenates.
834 # Follow this macro with [] if concatenation with subsequent text is
835 # undesired.
836 m4_define([m4_unquote], [$*])
839 ## -------------------------- ##
840 ## 8. Implementing m4 loops.  ##
841 ## -------------------------- ##
844 # m4_for(VARIABLE, FIRST, LAST, [STEP = +/-1], EXPRESSION)
845 # --------------------------------------------------------
846 # Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO with
847 # increments of STEP.  Both limits are included, and bounds are
848 # checked for consistency.  The algorithm is robust to indirect
849 # VARIABLE names.  Changing VARIABLE inside EXPRESSION will not impact
850 # the number of iterations.
852 # Uses _m4_defn for speed, and avoid dnl in the macro body.
853 m4_define([m4_for],
854 [m4_pushdef([$1], m4_eval([$2]))]dnl
855 [m4_cond([m4_eval(([$3]) > ([$2]))], 1,
856            [m4_pushdef([_m4_step], m4_eval(m4_default([$4],
857               1)))m4_assert(_m4_step > 0)_$0([$1], _m4_defn([$1]),
858   m4_eval((([$3]) - ([$2])) / _m4_step * _m4_step + ([$2])),
859   _m4_step, [$5])],
860          [m4_eval(([$3]) < ([$2]))], 1,
861            [m4_pushdef([_m4_step], m4_eval(m4_default([$4],
862               -1)))m4_assert(_m4_step < 0)_$0([$1], _m4_defn([$1]),
863   m4_eval((([$2]) - ([$3])) / -(_m4_step) * _m4_step + ([$2])),
864   _m4_step, [$5])],
865          [m4_pushdef([_m4_step])$5])[]]dnl
866 [m4_popdef([_m4_step], [$1])])
869 # _m4_for(VARIABLE, COUNT, LAST, STEP, EXPRESSION)
870 # ------------------------------------------------
871 # Core of the loop, no consistency checks, all arguments are plain
872 # numbers.  Define VARIABLE to COUNT, expand EXPRESSION, then alter
873 # COUNT by STEP and iterate if COUNT is not LAST.
874 m4_define([_m4_for],
875 [m4_define([$1], [$2])$5[]m4_if([$2], [$3], [],
876       [$0([$1], m4_eval([$2 + $4]), [$3], [$4], [$5])])])
879 # Implementing `foreach' loops in m4 is much more tricky than it may
880 # seem.  For example, the old M4 1.4.4 manual had an incorrect example,
881 # which looked like this (when translated to m4sugar):
883 # | # foreach(VAR, (LIST), STMT)
884 # | m4_define([foreach],
885 # |   [m4_pushdef([$1])_foreach([$1], [$2], [$3])m4_popdef([$1])])
886 # | m4_define([_arg1], [$1])
887 # | m4_define([_foreach],
888 # |   [m4_if([$2], [()], ,
889 # |     [m4_define([$1], _arg1$2)$3[]_foreach([$1], (m4_shift$2), [$3])])])
891 # But then if you run
893 # | m4_define(a, 1)
894 # | m4_define(b, 2)
895 # | m4_define(c, 3)
896 # | foreach([f], [([a], [(b], [c)])], [echo f
897 # | ])
899 # it gives
901 #  => echo 1
902 #  => echo (2,3)
904 # which is not what is expected.
906 # Of course the problem is that many quotes are missing.  So you add
907 # plenty of quotes at random places, until you reach the expected
908 # result.  Alternatively, if you are a quoting wizard, you directly
909 # reach the following implementation (but if you really did, then
910 # apply to the maintenance of m4sugar!).
912 # | # foreach(VAR, (LIST), STMT)
913 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
914 # | m4_define([_arg1], [[$1]])
915 # | m4_define([_foreach],
916 # |  [m4_if($2, [()], ,
917 # |    [m4_define([$1], [_arg1$2])$3[]_foreach([$1], [(m4_shift$2)], [$3])])])
919 # which this time answers
921 #  => echo a
922 #  => echo (b
923 #  => echo c)
925 # Bingo!
927 # Well, not quite.
929 # With a better look, you realize that the parens are more a pain than
930 # a help: since anyway you need to quote properly the list, you end up
931 # with always using an outermost pair of parens and an outermost pair
932 # of quotes.  Rejecting the parens both eases the implementation, and
933 # simplifies the use:
935 # | # foreach(VAR, (LIST), STMT)
936 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
937 # | m4_define([_arg1], [$1])
938 # | m4_define([_foreach],
939 # |  [m4_if($2, [], ,
940 # |    [m4_define([$1], [_arg1($2)])$3[]_foreach([$1], [m4_shift($2)], [$3])])])
943 # Now, just replace the `$2' with `m4_quote($2)' in the outer `m4_if'
944 # to improve robustness, and you come up with a nice implementation
945 # that doesn't require extra parentheses in the user's LIST.
947 # But wait -  now the algorithm is quadratic, because every recursion of
948 # the algorithm keeps the entire LIST and merely adds another m4_shift to
949 # the quoted text.  If the user has a lot of elements in LIST, you can
950 # bring the system to its knees with the memory m4 then requires, or trip
951 # the m4 --nesting-limit recursion factor.  The only way to avoid
952 # quadratic growth is ensure m4_shift is expanded prior to the recursion.
953 # Hence the design below.
955 # The M4 manual now includes a chapter devoted to this issue, with
956 # the lessons learned from m4sugar.  And still, this design is only
957 # optimal for M4 1.6; see foreach.m4 for yet more comments on why
958 # M4 1.4.x uses yet another implementation.
961 # m4_foreach(VARIABLE, LIST, EXPRESSION)
962 # --------------------------------------
964 # Expand EXPRESSION assigning each value of the LIST to VARIABLE.
965 # LIST should have the form `item_1, item_2, ..., item_n', i.e. the
966 # whole list must *quoted*.  Quote members too if you don't want them
967 # to be expanded.
969 # This macro is robust to active symbols:
970 #      | m4_define(active, [ACT, IVE])
971 #      | m4_foreach(Var, [active, active], [-Var-])
972 #     => -ACT--IVE--ACT--IVE-
974 #      | m4_foreach(Var, [[active], [active]], [-Var-])
975 #     => -ACT, IVE--ACT, IVE-
977 #      | m4_foreach(Var, [[[active]], [[active]]], [-Var-])
978 #     => -active--active-
980 # This macro is called frequently, so avoid extra expansions such as
981 # m4_ifval and dnl.  Also, since $2 might be quite large, try to use it
982 # as little as possible in _m4_foreach; each extra use requires that much
983 # more memory for expansion.  So, rather than directly compare $2 against
984 # [] and use m4_car/m4_cdr for recursion, we instead unbox the list (which
985 # requires swapping the argument order in the helper), insert an ignored
986 # third argument, and use m4_shift3 to detect when recursion is complete.
987 m4_define([m4_foreach],
988 [m4_if([$2], [], [],
989        [m4_pushdef([$1])_$0([$1], [$3], [], $2)m4_popdef([$1])])])
991 m4_define([_m4_foreach],
992 [m4_if([$#], [3], [],
993        [m4_define([$1], [$4])$2[]$0([$1], [$2], m4_shift3($@))])])
996 # m4_foreach_w(VARIABLE, LIST, EXPRESSION)
997 # ----------------------------------------
999 # Like m4_foreach, but the list is whitespace separated.
1001 # This macro is robust to active symbols:
1002 #    m4_foreach_w([Var], [ active
1003 #    b  act\
1004 #    ive  ], [-Var-])end
1005 #    => -active--b--active-end
1007 m4_define([m4_foreach_w],
1008 [m4_foreach([$1], m4_split(m4_normalize([$2]), [ ]), [$3])])
1011 # m4_map(MACRO, LIST)
1012 # m4_mapall(MACRO, LIST)
1013 # ----------------------
1014 # Invoke MACRO($1), MACRO($2) etc. where $1, $2... are the elements of
1015 # LIST.  $1, $2... must in turn be lists, appropriate for m4_apply.
1016 # If LIST contains an empty sublist, m4_map skips the expansion of
1017 # MACRO, while m4_mapall expands MACRO with no arguments.
1019 # Since LIST may be quite large, we want to minimize how often it
1020 # appears in the expansion.  Rather than use m4_car/m4_cdr iteration,
1021 # we unbox the list, ignore the second argument, and use m4_shift2 to
1022 # detect the end of recursion.  The mismatch in () is intentional; see
1023 # _m4_map.  For m4_map, an empty list behaves like an empty sublist
1024 # and gets ignored; for m4_mapall, we must special-case the empty
1025 # list.
1026 m4_define([m4_map],
1027 [_m4_map([_m4_apply([$1]], [], $2)])
1029 m4_define([m4_mapall],
1030 [m4_if([$2], [], [],
1031        [_m4_map([m4_apply([$1]], [], $2)])])
1034 # m4_map_sep(MACRO, SEPARATOR, LIST)
1035 # m4_mapall_sep(MACRO, SEPARATOR, LIST)
1036 # -------------------------------------
1037 # Invoke MACRO($1), SEPARATOR, MACRO($2), ..., MACRO($N) where $1,
1038 # $2... $N are the elements of LIST, and are in turn lists appropriate
1039 # for m4_apply.  SEPARATOR is expanded, in order to allow the creation
1040 # of a list of arguments by using a single-quoted comma as the
1041 # separator.  For each empty sublist, m4_map_sep skips the expansion
1042 # of MACRO and SEPARATOR, while m4_mapall_sep expands MACRO with no
1043 # arguments.
1045 # For m4_mapall_sep, merely expand the first iteration without the
1046 # separator, then include separator as part of subsequent recursion;
1047 # but avoid extra expansion of LIST's side-effects via a helper macro.
1048 # For m4_map_sep, things are trickier - we don't know if the first
1049 # list element is an empty sublist, so we must define a self-modifying
1050 # helper macro and use that as the separator instead.
1051 m4_define([m4_map_sep],
1052 [m4_pushdef([m4_Sep], [m4_define([m4_Sep], _m4_defn([m4_unquote]))])]dnl
1053 [_m4_map([_m4_apply([m4_Sep([$2])[]$1]], [], $3)m4_popdef([m4_Sep])])
1055 m4_define([m4_mapall_sep],
1056 [m4_if([$3], [], [], [_$0([$1], [$2], $3)])])
1058 m4_define([_m4_mapall_sep],
1059 [m4_apply([$1], [$3])_m4_map([m4_apply([$2[]$1]], m4_shift2($@))])
1061 # _m4_map(PREFIX, IGNORED, SUBLIST, ...)
1062 # --------------------------------------
1063 # Common implementation for all four m4_map variants.  The mismatch in
1064 # the number of () is intentional.  PREFIX must supply a form of
1065 # m4_apply, the open `(', and the MACRO to be applied.  Each iteration
1066 # then appends `,', the current SUBLIST and the closing `)', then
1067 # recurses to the next SUBLIST.  IGNORED is an aid to ending recursion
1068 # efficiently.
1069 m4_define([_m4_map],
1070 [m4_if([$#], [2], [],
1071        [$1, [$3])$0([$1], m4_shift2($@))])])
1073 # m4_transform(EXPRESSION, ARG...)
1074 # --------------------------------
1075 # Expand EXPRESSION([ARG]) for each argument.  More efficient than
1076 # m4_foreach([var], [ARG...], [EXPRESSION(m4_defn([var]))])
1077 m4_define([m4_transform],
1078 [m4_if([$#], [0], [m4_fatal([$0: too few arguments: $#])],
1079        [$#], [1], [],
1080        [$#], [2], [$1([$2])[]],
1081        [$1([$2])[]$0([$1], m4_shift2($@))])])
1084 # m4_transform_pair(EXPRESSION, [END-EXPR = EXPRESSION], ARG...)
1085 # --------------------------------------------------------------
1086 # Perform a pairwise grouping of consecutive ARGs, by expanding
1087 # EXPRESSION([ARG1], [ARG2]).  If there are an odd number of ARGs, the
1088 # final argument is expanded with END-EXPR([ARGn]).
1090 # For example:
1091 #   m4_define([show], [($*)m4_newline])dnl
1092 #   m4_transform_pair([show], [], [a], [b], [c], [d], [e])dnl
1093 #   => (a,b)
1094 #   => (c,d)
1095 #   => (e)
1096 m4_define([m4_transform_pair],
1097 [m4_if([$#], [0], [m4_fatal([$0: too few arguments: $#])],
1098        [$#], [1], [m4_fatal([$0: too few arguments: $#: $1])],
1099        [$#], [2], [],
1100        [$#], [3], [m4_default([$2], [$1])([$3])[]],
1101        [$#], [4], [$1([$3], [$4])[]],
1102        [$1([$3], [$4])[]$0([$1], [$2], m4_shift(m4_shift3($@)))])])
1105 ## --------------------------- ##
1106 ## 9. More diversion support.  ##
1107 ## --------------------------- ##
1110 # _m4_divert(DIVERSION-NAME or NUMBER)
1111 # ------------------------------------
1112 # If DIVERSION-NAME is the name of a diversion, return its number,
1113 # otherwise if it is a NUMBER return it.
1114 m4_define([_m4_divert],
1115 [m4_ifdef([_m4_divert($1)],
1116           [m4_indir([_m4_divert($1)])],
1117           [$1])])
1119 # KILL is only used to suppress output.
1120 m4_define([_m4_divert(KILL)],           -1)
1122 # The empty diversion name is a synonym for 0.
1123 m4_define([_m4_divert()],                0)
1126 # _m4_divert_n_stack
1127 # ------------------
1128 # Print m4_divert_stack with newline prepended, if it's nonempty.
1129 m4_define([_m4_divert_n_stack],
1130 [m4_ifdef([m4_divert_stack], [
1131 _m4_defn([m4_divert_stack])])])
1134 # m4_divert(DIVERSION-NAME)
1135 # -------------------------
1136 # Change the diversion stream to DIVERSION-NAME.
1137 m4_define([m4_divert],
1138 [m4_define([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)]dnl
1139 [m4_builtin([divert], _m4_divert([$1]))])
1142 # m4_divert_push(DIVERSION-NAME)
1143 # ------------------------------
1144 # Change the diversion stream to DIVERSION-NAME, while stacking old values.
1145 m4_define([m4_divert_push],
1146 [m4_pushdef([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)]dnl
1147 [m4_pushdef([_m4_divert_diversion], [$1])]dnl
1148 [m4_builtin([divert], _m4_divert([$1]))])
1151 # m4_divert_pop([DIVERSION-NAME])
1152 # -------------------------------
1153 # Change the diversion stream to its previous value, unstacking it.
1154 # If specified, verify we left DIVERSION-NAME.
1155 # When we pop the last value from the stack, we divert to -1.
1156 m4_define([m4_divert_pop],
1157 [m4_ifndef([_m4_divert_diversion],
1158            [m4_fatal([too many m4_divert_pop])])]dnl
1159 [m4_if([$1], [], [],
1160        [$1], _m4_defn([_m4_divert_diversion]), [],
1161        [m4_fatal([$0($1): diversion mismatch: ]_m4_divert_n_stack)])]dnl
1162 [_m4_popdef([m4_divert_stack], [_m4_divert_diversion])]dnl
1163 [m4_builtin([divert],
1164             m4_ifdef([_m4_divert_diversion],
1165                      [_m4_divert(_m4_defn([_m4_divert_diversion]))],
1166                      -1))])
1169 # m4_divert_text(DIVERSION-NAME, CONTENT)
1170 # ---------------------------------------
1171 # Output CONTENT into DIVERSION-NAME (which may be a number actually).
1172 # An end of line is appended for free to CONTENT.
1173 m4_define([m4_divert_text],
1174 [m4_divert_push([$1])$2
1175 m4_divert_pop([$1])])
1178 # m4_divert_once(DIVERSION-NAME, CONTENT)
1179 # ---------------------------------------
1180 # Output CONTENT into DIVERSION-NAME once, if not already there.
1181 # An end of line is appended for free to CONTENT.
1182 m4_define([m4_divert_once],
1183 [m4_expand_once([m4_divert_text([$1], [$2])])])
1186 # m4_undivert(DIVERSION-NAME)
1187 # ---------------------------
1188 # Undivert DIVERSION-NAME.  Unlike the M4 version, this only takes a single
1189 # diversion identifier, and should not be used to undivert files.
1190 m4_define([m4_undivert],
1191 [m4_builtin([undivert], _m4_divert([$1]))])
1194 ## --------------------------------------------- ##
1195 ## 10. Defining macros with bells and whistles.  ##
1196 ## --------------------------------------------- ##
1198 # `m4_defun' is basically `m4_define' but it equips the macro with the
1199 # needed machinery for `m4_require'.  A macro must be m4_defun'd if
1200 # either it is m4_require'd, or it m4_require's.
1202 # Two things deserve attention and are detailed below:
1203 #  1. Implementation of m4_require
1204 #  2. Keeping track of the expansion stack
1206 # 1. Implementation of m4_require
1207 # ===============================
1209 # Of course m4_defun AC_PROVIDE's the macro, so that a macro which has
1210 # been expanded is not expanded again when m4_require'd, but the
1211 # difficult part is the proper expansion of macros when they are
1212 # m4_require'd.
1214 # The implementation is based on two ideas, (i) using diversions to
1215 # prepare the expansion of the macro and its dependencies (by Franc,ois
1216 # Pinard), and (ii) expand the most recently m4_require'd macros _after_
1217 # the previous macros (by Axel Thimm).
1220 # The first idea: why use diversions?
1221 # -----------------------------------
1223 # When a macro requires another, the other macro is expanded in new
1224 # diversion, GROW.  When the outer macro is fully expanded, we first
1225 # undivert the most nested diversions (GROW - 1...), and finally
1226 # undivert GROW.  To understand why we need several diversions,
1227 # consider the following example:
1229 # | m4_defun([TEST1], [Test...REQUIRE([TEST2])1])
1230 # | m4_defun([TEST2], [Test...REQUIRE([TEST3])2])
1231 # | m4_defun([TEST3], [Test...3])
1233 # Because m4_require is not required to be first in the outer macros, we
1234 # must keep the expansions of the various levels of m4_require separated.
1235 # Right before executing the epilogue of TEST1, we have:
1237 #          GROW - 2: Test...3
1238 #          GROW - 1: Test...2
1239 #          GROW:     Test...1
1240 #          BODY:
1242 # Finally the epilogue of TEST1 undiverts GROW - 2, GROW - 1, and
1243 # GROW into the regular flow, BODY.
1245 #          GROW - 2:
1246 #          GROW - 1:
1247 #          GROW:
1248 #          BODY:        Test...3; Test...2; Test...1
1250 # (The semicolons are here for clarification, but of course are not
1251 # emitted.)  This is what Autoconf 2.0 (I think) to 2.13 (I'm sure)
1252 # implement.
1255 # The second idea: first required first out
1256 # -----------------------------------------
1258 # The natural implementation of the idea above is buggy and produces
1259 # very surprising results in some situations.  Let's consider the
1260 # following example to explain the bug:
1262 # | m4_defun([TEST1],  [REQUIRE([TEST2a])REQUIRE([TEST2b])])
1263 # | m4_defun([TEST2a], [])
1264 # | m4_defun([TEST2b], [REQUIRE([TEST3])])
1265 # | m4_defun([TEST3],  [REQUIRE([TEST2a])])
1266 # |
1267 # | AC_INIT
1268 # | TEST1
1270 # The dependencies between the macros are:
1272 #                3 --- 2b
1273 #               /        \              is m4_require'd by
1274 #              /          \       left -------------------- right
1275 #           2a ------------ 1
1277 # If you strictly apply the rules given in the previous section you get:
1279 #          GROW - 2: TEST3
1280 #          GROW - 1: TEST2a; TEST2b
1281 #          GROW:     TEST1
1282 #          BODY:
1284 # (TEST2a, although required by TEST3 is not expanded in GROW - 3
1285 # because is has already been expanded before in GROW - 1, so it has
1286 # been AC_PROVIDE'd, so it is not expanded again) so when you undivert
1287 # the stack of diversions, you get:
1289 #          GROW - 2:
1290 #          GROW - 1:
1291 #          GROW:
1292 #          BODY:        TEST3; TEST2a; TEST2b; TEST1
1294 # i.e., TEST2a is expanded after TEST3 although the latter required the
1295 # former.
1297 # Starting from 2.50, we use an implementation provided by Axel Thimm.
1298 # The idea is simple: the order in which macros are emitted must be the
1299 # same as the one in which macros are expanded.  (The bug above can
1300 # indeed be described as: a macro has been AC_PROVIDE'd before its
1301 # dependent, but it is emitted after: the lack of correlation between
1302 # emission and expansion order is guilty).
1304 # How to do that?  You keep the stack of diversions to elaborate the
1305 # macros, but each time a macro is fully expanded, emit it immediately.
1307 # In the example above, when TEST2a is expanded, but it's epilogue is
1308 # not run yet, you have:
1310 #          GROW - 2:
1311 #          GROW - 1: TEST2a
1312 #          GROW:     Elaboration of TEST1
1313 #          BODY:
1315 # The epilogue of TEST2a emits it immediately:
1317 #          GROW - 2:
1318 #          GROW - 1:
1319 #          GROW:     Elaboration of TEST1
1320 #          BODY:     TEST2a
1322 # TEST2b then requires TEST3, so right before the epilogue of TEST3, you
1323 # have:
1325 #          GROW - 2: TEST3
1326 #          GROW - 1: Elaboration of TEST2b
1327 #          GROW:     Elaboration of TEST1
1328 #          BODY:      TEST2a
1330 # The epilogue of TEST3 emits it:
1332 #          GROW - 2:
1333 #          GROW - 1: Elaboration of TEST2b
1334 #          GROW:     Elaboration of TEST1
1335 #          BODY:     TEST2a; TEST3
1337 # TEST2b is now completely expanded, and emitted:
1339 #          GROW - 2:
1340 #          GROW - 1:
1341 #          GROW:     Elaboration of TEST1
1342 #          BODY:     TEST2a; TEST3; TEST2b
1344 # and finally, TEST1 is finished and emitted:
1346 #          GROW - 2:
1347 #          GROW - 1:
1348 #          GROW:
1349 #          BODY:     TEST2a; TEST3; TEST2b: TEST1
1351 # The idea is simple, but the implementation is a bit evolved.  If you
1352 # are like me, you will want to see the actual functioning of this
1353 # implementation to be convinced.  The next section gives the full
1354 # details.
1357 # The Axel Thimm implementation at work
1358 # -------------------------------------
1360 # We consider the macros above, and this configure.ac:
1362 #           AC_INIT
1363 #           TEST1
1365 # You should keep the definitions of _m4_defun_pro, _m4_defun_epi, and
1366 # m4_require at hand to follow the steps.
1368 # This implements tries not to assume that the current diversion is
1369 # BODY, so as soon as a macro (m4_defun'd) is expanded, we first
1370 # record the current diversion under the name _m4_divert_dump (denoted
1371 # DUMP below for short).  This introduces an important difference with
1372 # the previous versions of Autoconf: you cannot use m4_require if you
1373 # are not inside an m4_defun'd macro, and especially, you cannot
1374 # m4_require directly from the top level.
1376 # We have not tried to simulate the old behavior (better yet, we
1377 # diagnose it), because it is too dangerous: a macro m4_require'd from
1378 # the top level is expanded before the body of `configure', i.e., before
1379 # any other test was run.  I let you imagine the result of requiring
1380 # AC_STDC_HEADERS for instance, before AC_PROG_CC was actually run....
1382 # After AC_INIT was run, the current diversion is BODY.
1383 # * AC_INIT was run
1384 #   DUMP:                undefined
1385 #   diversion stack:     BODY |-
1387 # * TEST1 is expanded
1388 # The prologue of TEST1 sets _m4_divert_dump, which is the diversion
1389 # where the current elaboration will be dumped, to the current
1390 # diversion.  It also m4_divert_push to GROW, where the full
1391 # expansion of TEST1 and its dependencies will be elaborated.
1392 #   DUMP:        BODY
1393 #   BODY:        empty
1394 #   diversions:  GROW, BODY |-
1396 # * TEST1 requires TEST2a
1397 # _m4_require_call m4_divert_pushes another temporary diversion,
1398 # GROW - 1, and expands TEST2a in there.
1399 #   DUMP:        BODY
1400 #   BODY:        empty
1401 #   GROW - 1:    TEST2a
1402 #   diversions:  GROW - 1, GROW, BODY |-
1403 # Than the content of the temporary diversion is moved to DUMP and the
1404 # temporary diversion is popped.
1405 #   DUMP:        BODY
1406 #   BODY:        TEST2a
1407 #   diversions:  GROW, BODY |-
1409 # * TEST1 requires TEST2b
1410 # Again, _m4_require_call pushes GROW - 1 and heads to expand TEST2b.
1411 #   DUMP:        BODY
1412 #   BODY:        TEST2a
1413 #   diversions:  GROW - 1, GROW, BODY |-
1415 # * TEST2b requires TEST3
1416 # _m4_require_call pushes GROW - 2 and expands TEST3 here.
1417 # (TEST3 requires TEST2a, but TEST2a has already been m4_provide'd, so
1418 # nothing happens.)
1419 #   DUMP:        BODY
1420 #   BODY:        TEST2a
1421 #   GROW - 2:    TEST3
1422 #   diversions:  GROW - 2, GROW - 1, GROW, BODY |-
1423 # Than the diversion is appended to DUMP, and popped.
1424 #   DUMP:        BODY
1425 #   BODY:        TEST2a; TEST3
1426 #   diversions:  GROW - 1, GROW, BODY |-
1428 # * TEST1 requires TEST2b (contd.)
1429 # The content of TEST2b is expanded...
1430 #   DUMP:        BODY
1431 #   BODY:        TEST2a; TEST3
1432 #   GROW - 1:    TEST2b,
1433 #   diversions:  GROW - 1, GROW, BODY |-
1434 # ... and moved to DUMP.
1435 #   DUMP:        BODY
1436 #   BODY:        TEST2a; TEST3; TEST2b
1437 #   diversions:  GROW, BODY |-
1439 # * TEST1 is expanded: epilogue
1440 # TEST1's own content is in GROW...
1441 #   DUMP:        BODY
1442 #   BODY:        TEST2a; TEST3; TEST2b
1443 #   GROW:        TEST1
1444 #   diversions:  BODY |-
1445 # ... and it's epilogue moves it to DUMP and then undefines DUMP.
1446 #   DUMP:       undefined
1447 #   BODY:       TEST2a; TEST3; TEST2b; TEST1
1448 #   diversions: BODY |-
1451 # 2. Keeping track of the expansion stack
1452 # =======================================
1454 # When M4 expansion goes wrong it is often extremely hard to find the
1455 # path amongst macros that drove to the failure.  What is needed is
1456 # the stack of macro `calls'. One could imagine that GNU M4 would
1457 # maintain a stack of macro expansions, unfortunately it doesn't, so
1458 # we do it by hand.  This is of course extremely costly, but the help
1459 # this stack provides is worth it.  Nevertheless to limit the
1460 # performance penalty this is implemented only for m4_defun'd macros,
1461 # not for define'd macros.
1463 # The scheme is simplistic: each time we enter an m4_defun'd macros,
1464 # we prepend its name in m4_expansion_stack, and when we exit the
1465 # macro, we remove it (thanks to pushdef/popdef).
1467 # In addition, we want to detect circular m4_require dependencies.
1468 # Each time we expand a macro FOO we define _m4_expanding(FOO); and
1469 # m4_require(BAR) simply checks whether _m4_expanding(BAR) is defined.
1472 # m4_expansion_stack_push(TEXT)
1473 # -----------------------------
1474 m4_define([m4_expansion_stack_push],
1475 [m4_pushdef([m4_expansion_stack],
1476             [$1]m4_ifdef([m4_expansion_stack], [
1477 _m4_defn([m4_expansion_stack])]))])
1480 # m4_expansion_stack_pop
1481 # ----------------------
1482 m4_define([m4_expansion_stack_pop],
1483 [m4_popdef([m4_expansion_stack])])
1486 # m4_expansion_stack_dump
1487 # -----------------------
1488 # Dump the expansion stack.
1489 m4_define([m4_expansion_stack_dump],
1490 [m4_ifdef([m4_expansion_stack],
1491           [m4_errprintn(_m4_defn([m4_expansion_stack]))])dnl
1492 m4_errprintn(m4_location[: the top level])])
1495 # _m4_divert(GROW)
1496 # ----------------
1497 # This diversion is used by the m4_defun/m4_require machinery.  It is
1498 # important to keep room before GROW because for each nested
1499 # AC_REQUIRE we use an additional diversion (i.e., two m4_require's
1500 # will use GROW - 2.  More than 3 levels has never seemed to be
1501 # needed.)
1503 # ...
1504 # - GROW - 2
1505 #   m4_require'd code, 2 level deep
1506 # - GROW - 1
1507 #   m4_require'd code, 1 level deep
1508 # - GROW
1509 #   m4_defun'd macros are elaborated here.
1511 m4_define([_m4_divert(GROW)],       10000)
1514 # _m4_defun_pro(MACRO-NAME)
1515 # -------------------------
1516 # The prologue for Autoconf macros.
1518 # This is called frequently, so minimize the number of macro invocations
1519 # by avoiding dnl and m4_defn overhead.
1520 m4_define([_m4_defun_pro],
1521 m4_do([[m4_ifdef([m4_expansion_stack], [], [_m4_defun_pro_outer[]])]],
1522       [[m4_expansion_stack_push(_m4_defn(
1523           [m4_location($1)])[: $1 is expanded from...])]],
1524       [[m4_pushdef([_m4_expanding($1)])]]))
1526 m4_define([_m4_defun_pro_outer],
1527 [m4_copy([_m4_divert_diversion], [_m4_divert_dump])m4_divert_push([GROW])])
1529 # _m4_defun_epi(MACRO-NAME)
1530 # -------------------------
1531 # The Epilogue for Autoconf macros.  MACRO-NAME only helps tracing
1532 # the PRO/EPI pairs.
1534 # This is called frequently, so minimize the number of macro invocations
1535 # by avoiding dnl and m4_popdef overhead.
1536 m4_define([_m4_defun_epi],
1537 m4_do([[_m4_popdef([_m4_expanding($1)])]],
1538       [[m4_expansion_stack_pop()]],
1539       [[m4_ifdef([m4_expansion_stack], [], [_m4_defun_epi_outer[]])]],
1540       [[m4_provide([$1])]]))
1542 m4_define([_m4_defun_epi_outer],
1543 [_m4_undefine([_m4_divert_dump])m4_divert_pop([GROW])m4_undivert([GROW])])
1546 # m4_defun(NAME, EXPANSION)
1547 # -------------------------
1548 # Define a macro which automatically provides itself.  Add machinery
1549 # so the macro automatically switches expansion to the diversion
1550 # stack if it is not already using it.  In this case, once finished,
1551 # it will bring back all the code accumulated in the diversion stack.
1552 # This, combined with m4_require, achieves the topological ordering of
1553 # macros.  We don't use this macro to define some frequently called
1554 # macros that are not involved in ordering constraints, to save m4
1555 # processing.
1556 m4_define([m4_defun],
1557 [m4_define([m4_location($1)], m4_location)dnl
1558 m4_define([$1],
1559           [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])
1562 # m4_defun_once(NAME, EXPANSION)
1563 # ------------------------------
1564 # As m4_defun, but issues the EXPANSION only once, and warns if used
1565 # several times.
1566 m4_define([m4_defun_once],
1567 [m4_define([m4_location($1)], m4_location)dnl
1568 m4_define([$1],
1569           [m4_provide_if([$1],
1570                          [m4_warn([syntax], [$1 invoked multiple times])],
1571                          [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])])
1574 # m4_pattern_forbid(ERE, [WHY])
1575 # -----------------------------
1576 # Declare that no token matching the forbidden extended regular
1577 # expression ERE should be seen in the output unless...
1578 m4_define([m4_pattern_forbid], [])
1581 # m4_pattern_allow(ERE)
1582 # ---------------------
1583 # ... that token also matches the allowed extended regular expression ERE.
1584 # Both used via traces.
1585 m4_define([m4_pattern_allow], [])
1588 ## --------------------------------- ##
1589 ## 11. Dependencies between macros.  ##
1590 ## --------------------------------- ##
1593 # m4_before(THIS-MACRO-NAME, CALLED-MACRO-NAME)
1594 # ---------------------------------------------
1595 # Issue a warning if CALLED-MACRO-NAME was called before THIS-MACRO-NAME.
1596 m4_define([m4_before],
1597 [m4_provide_if([$2],
1598                [m4_warn([syntax], [$2 was called before $1])])])
1601 # m4_require(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
1602 # -----------------------------------------------------------
1603 # If NAME-TO-CHECK has never been expanded (actually, if it is not
1604 # m4_provide'd), expand BODY-TO-EXPAND *before* the current macro
1605 # expansion.  Once expanded, emit it in _m4_divert_dump.  Keep track
1606 # of the m4_require chain in m4_expansion_stack.
1608 # The normal cases are:
1610 # - NAME-TO-CHECK == BODY-TO-EXPAND
1611 #   Which you can use for regular macros with or without arguments, e.g.,
1612 #     m4_require([AC_PROG_CC], [AC_PROG_CC])
1613 #     m4_require([AC_CHECK_HEADERS(limits.h)], [AC_CHECK_HEADERS(limits.h)])
1614 #   which is just the same as
1615 #     m4_require([AC_PROG_CC])
1616 #     m4_require([AC_CHECK_HEADERS(limits.h)])
1618 # - BODY-TO-EXPAND == m4_indir([NAME-TO-CHECK])
1619 #   In the case of macros with irregular names.  For instance:
1620 #     m4_require([AC_LANG_COMPILER(C)], [indir([AC_LANG_COMPILER(C)])])
1621 #   which means `if the macro named `AC_LANG_COMPILER(C)' (the parens are
1622 #   part of the name, it is not an argument) has not been run, then
1623 #   call it.'
1624 #   Had you used
1625 #     m4_require([AC_LANG_COMPILER(C)], [AC_LANG_COMPILER(C)])
1626 #   then m4_require would have tried to expand `AC_LANG_COMPILER(C)', i.e.,
1627 #   call the macro `AC_LANG_COMPILER' with `C' as argument.
1629 #   You could argue that `AC_LANG_COMPILER', when it receives an argument
1630 #   such as `C' should dispatch the call to `AC_LANG_COMPILER(C)'.  But this
1631 #   `extension' prevents `AC_LANG_COMPILER' from having actual arguments that
1632 #   it passes to `AC_LANG_COMPILER(C)'.
1634 # This is called frequently, so minimize the number of macro invocations
1635 # by avoiding dnl and other overhead on the common path.
1636 m4_define([m4_require],
1637 m4_do([[m4_ifdef([_m4_expanding($1)],
1638                  [m4_fatal([$0: circular dependency of $1])])]],
1639       [[m4_ifdef([_m4_divert_dump], [],
1640                  [m4_fatal([$0($1): cannot be used outside of an ]dnl
1641 m4_bmatch([$0], [^AC_], [[AC_DEFUN]], [[m4_defun]])['d macro])])]],
1642       [[m4_provide_if([$1],
1643                       [],
1644                       [_m4_require_call([$1], [$2])])]]))
1647 # _m4_require_call(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
1648 # -----------------------------------------------------------------
1649 # If m4_require decides to expand the body, it calls this macro.
1651 # This is called frequently, so minimize the number of macro invocations
1652 # by avoiding dnl and other overhead on the common path.
1653 m4_define([_m4_require_call],
1654 m4_do([[m4_define([_m4_divert_grow], m4_decr(_m4_divert_grow))]],
1655       [[m4_divert_push(_m4_divert_grow)]],
1656       [[m4_default([$2], [$1])
1657 m4_provide_if([$1],
1658               [],
1659               [m4_warn([syntax],
1660                        [$1 is m4_require'd but not m4_defun'd])])]],
1661       [[m4_divert(_m4_defn([_m4_divert_dump]))]],
1662       [[m4_undivert(_m4_divert_grow)]],
1663       [[m4_divert_pop(_m4_divert_grow)]],
1664       [[m4_define([_m4_divert_grow], m4_incr(_m4_divert_grow))]]))
1667 # _m4_divert_grow
1668 # ---------------
1669 # The counter for _m4_require_call.
1670 m4_define([_m4_divert_grow], _m4_divert([GROW]))
1673 # m4_expand_once(TEXT, [WITNESS = TEXT])
1674 # --------------------------------------
1675 # If TEXT has never been expanded, expand it *here*.  Use WITNESS as
1676 # as a memory that TEXT has already been expanded.
1677 m4_define([m4_expand_once],
1678 [m4_provide_if(m4_ifval([$2], [[$2]], [[$1]]),
1679                [],
1680                [m4_provide(m4_ifval([$2], [[$2]], [[$1]]))[]$1])])
1683 # m4_provide(MACRO-NAME)
1684 # ----------------------
1685 m4_define([m4_provide],
1686 [m4_define([m4_provide($1)])])
1689 # m4_provide_if(MACRO-NAME, IF-PROVIDED, IF-NOT-PROVIDED)
1690 # -------------------------------------------------------
1691 # If MACRO-NAME is provided do IF-PROVIDED, else IF-NOT-PROVIDED.
1692 # The purpose of this macro is to provide the user with a means to
1693 # check macros which are provided without letting her know how the
1694 # information is coded.
1695 m4_define([m4_provide_if],
1696 [m4_ifdef([m4_provide($1)],
1697           [$2], [$3])])
1700 ## --------------------- ##
1701 ## 12. Text processing.  ##
1702 ## --------------------- ##
1705 # m4_cr_letters
1706 # m4_cr_LETTERS
1707 # m4_cr_Letters
1708 # -------------
1709 m4_define([m4_cr_letters], [abcdefghijklmnopqrstuvwxyz])
1710 m4_define([m4_cr_LETTERS], [ABCDEFGHIJKLMNOPQRSTUVWXYZ])
1711 m4_define([m4_cr_Letters],
1712 m4_defn([m4_cr_letters])dnl
1713 m4_defn([m4_cr_LETTERS])dnl
1717 # m4_cr_digits
1718 # ------------
1719 m4_define([m4_cr_digits], [0123456789])
1722 # m4_cr_alnum
1723 # -----------
1724 m4_define([m4_cr_alnum],
1725 m4_defn([m4_cr_Letters])dnl
1726 m4_defn([m4_cr_digits])dnl
1730 # m4_cr_symbols1
1731 # m4_cr_symbols2
1732 # -------------------------------
1733 m4_define([m4_cr_symbols1],
1734 m4_defn([m4_cr_Letters])dnl
1737 m4_define([m4_cr_symbols2],
1738 m4_defn([m4_cr_symbols1])dnl
1739 m4_defn([m4_cr_digits])dnl
1742 # m4_cr_all
1743 # ---------
1744 # The character range representing everything, with `-' as the last
1745 # character, since it is special to m4_translit.  Use with care, because
1746 # it contains characters special to M4 (fortunately, both ASCII and EBCDIC
1747 # have [] in order, so m4_defn([m4_cr_all]) remains a valid string).  It
1748 # also contains characters special to terminals, so it should never be
1749 # displayed in an error message.  Also, attempts to map [ and ] to other
1750 # characters via m4_translit must deal with the fact that m4_translit does
1751 # not add quotes to the output.
1753 # It is mainly useful in generating inverted character range maps, for use
1754 # in places where m4_translit is faster than an equivalent m4_bpatsubst;
1755 # the regex `[^a-z]' is equivalent to:
1756 #  m4_translit(m4_dquote(m4_defn([m4_cr_all])), [a-z])
1757 m4_define([m4_cr_all],
1758 m4_translit(m4_dquote(m4_format(m4_dquote(m4_for(
1759   ,1,255,,[[%c]]))m4_for([i],1,255,,[,i]))), [-])-)
1762 # _m4_define_cr_not(CATEGORY)
1763 # ---------------------------
1764 # Define m4_cr_not_CATEGORY as the inverse of m4_cr_CATEGORY.
1765 m4_define([_m4_define_cr_not],
1766 [m4_define([m4_cr_not_$1],
1767            m4_translit(m4_dquote(m4_defn([m4_cr_all])),
1768                        m4_defn([m4_cr_$1])))])
1771 # m4_cr_not_letters
1772 # m4_cr_not_LETTERS
1773 # m4_cr_not_Letters
1774 # m4_cr_not_digits
1775 # m4_cr_not_alnum
1776 # m4_cr_not_symbols1
1777 # m4_cr_not_symbols2
1778 # ------------------
1779 # Inverse character sets
1780 _m4_define_cr_not([letters])
1781 _m4_define_cr_not([LETTERS])
1782 _m4_define_cr_not([Letters])
1783 _m4_define_cr_not([digits])
1784 _m4_define_cr_not([alnum])
1785 _m4_define_cr_not([symbols1])
1786 _m4_define_cr_not([symbols2])
1789 # m4_newline
1790 # ----------
1791 # Expands to a newline.  Exists for formatting reasons.
1792 m4_define([m4_newline], [
1796 # m4_re_escape(STRING)
1797 # --------------------
1798 # Escape RE active characters in STRING.
1799 m4_define([m4_re_escape],
1800 [m4_bpatsubst([$1],
1801               [[][*+.?\^$]], [\\\&])])
1804 # m4_re_string
1805 # ------------
1806 # Regexp for `[a-zA-Z_0-9]*'
1807 # m4_dquote provides literal [] for the character class.
1808 m4_define([m4_re_string],
1809 m4_dquote(m4_defn([m4_cr_symbols2]))dnl
1810 [*]dnl
1814 # m4_re_word
1815 # ----------
1816 # Regexp for `[a-zA-Z_][a-zA-Z_0-9]*'
1817 m4_define([m4_re_word],
1818 m4_dquote(m4_defn([m4_cr_symbols1]))dnl
1819 m4_defn([m4_re_string])dnl
1823 # m4_tolower(STRING)
1824 # m4_toupper(STRING)
1825 # ------------------
1826 # These macros convert STRING to lowercase or uppercase.
1828 # Rather than expand the m4_defn each time, we inline them up front.
1829 m4_define([m4_tolower],
1830 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_LETTERS]))[,
1831                    ]m4_dquote(m4_defn([m4_cr_letters]))[)])
1832 m4_define([m4_toupper],
1833 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_letters]))[,
1834                    ]m4_dquote(m4_defn([m4_cr_LETTERS]))[)])
1837 # m4_split(STRING, [REGEXP])
1838 # --------------------------
1840 # Split STRING into an m4 list of quoted elements.  The elements are
1841 # quoted with [ and ].  Beginning spaces and end spaces *are kept*.
1842 # Use m4_strip to remove them.
1844 # REGEXP specifies where to split.  Default is [\t ]+.
1846 # If STRING is empty, the result is an empty list.
1848 # Pay attention to the m4_changequotes.  When m4 reads the definition of
1849 # m4_split, it still has quotes set to [ and ].  Luckily, these are matched
1850 # in the macro body, so the definition is stored correctly.  Use the same
1851 # alternate quotes as m4_noquote; it must be unlikely to appear in $1.
1853 # Also, notice that $1 is quoted twice, since we want the result to
1854 # be quoted.  Then you should understand that the argument of
1855 # patsubst is -=<{(STRING)}>=- (i.e., with additional -=<{( and )}>=-).
1857 # This macro is safe on active symbols, i.e.:
1858 #   m4_define(active, ACTIVE)
1859 #   m4_split([active active ])end
1860 #   => [active], [active], []end
1862 # Optimize on regex of ` ' (space), since m4_foreach_w already guarantees
1863 # that the list contains single space separators, and a common case is
1864 # splitting a single-element list.  This macro is called frequently,
1865 # so avoid unnecessary dnl inside the definition.
1866 m4_define([m4_split],
1867 [m4_if([$1], [], [],
1868        [$2], [ ], [m4_if(m4_index([$1], [ ]), [-1], [[[$1]]], [_$0($@)])],
1869        [$2], [], [_$0([$1], [[   ]+])],
1870        [_$0($@)])])
1872 m4_define([_m4_split],
1873 [m4_changequote([-=<{(],[)}>=-])]dnl
1874 [[m4_bpatsubst(-=<{(-=<{($1)}>=-)}>=-, -=<{($2)}>=-,
1875                -=<{(], [)}>=-)]m4_changequote([, ])])
1879 # m4_flatten(STRING)
1880 # ------------------
1881 # If STRING contains end of lines, replace them with spaces.  If there
1882 # are backslashed end of lines, remove them.  This macro is safe with
1883 # active symbols.
1884 #    m4_define(active, ACTIVE)
1885 #    m4_flatten([active
1886 #    act\
1887 #    ive])end
1888 #    => active activeend
1890 # In m4, m4_bpatsubst is expensive, so first check for a newline.
1891 m4_define([m4_flatten],
1892 [m4_if(m4_index([$1], [
1893 ]), [-1], [[$1]],
1894        [m4_translit(m4_bpatsubst([[[$1]]], [\\
1895 ]), [
1896 ], [ ])])])
1899 # m4_strip(STRING)
1900 # ----------------
1901 # Expands into STRING with tabs and spaces singled out into a single
1902 # space, and removing leading and trailing spaces.
1904 # This macro is robust to active symbols.
1905 #    m4_define(active, ACTIVE)
1906 #    m4_strip([  active <tab> <tab>active ])end
1907 #    => active activeend
1909 # First, notice that we guarantee trailing space.  Why?  Because regular
1910 # expressions are greedy, and `.* ?' would always group the space into the
1911 # .* portion.  The algorithm is simpler by avoiding `?' at the end.  The
1912 # algorithm correctly strips everything if STRING is just ` '.
1914 # Then notice the second pattern: it is in charge of removing the
1915 # leading/trailing spaces.  Why not just `[^ ]'?  Because they are
1916 # applied to over-quoted strings, i.e. more or less [STRING], due
1917 # to the limitations of m4_bpatsubsts.  So the leading space in STRING
1918 # is the *second* character; equally for the trailing space.
1919 m4_define([m4_strip],
1920 [m4_bpatsubsts([$1 ],
1921                [[        ]+], [ ],
1922                [^. ?\(.*\) .$], [[[\1]]])])
1925 # m4_normalize(STRING)
1926 # --------------------
1927 # Apply m4_flatten and m4_strip to STRING.
1929 # The argument is quoted, so that the macro is robust to active symbols:
1931 #    m4_define(active, ACTIVE)
1932 #    m4_normalize([  act\
1933 #    ive
1934 #    active ])end
1935 #    => active activeend
1937 m4_define([m4_normalize],
1938 [m4_strip(m4_flatten([$1]))])
1942 # m4_join(SEP, ARG1, ARG2...)
1943 # ---------------------------
1944 # Produce ARG1SEPARG2...SEPARGn.  Avoid back-to-back SEP when a given ARG
1945 # is the empty string.  No expansion is performed on SEP or ARGs.
1947 # Since the number of arguments to join can be arbitrarily long, we
1948 # want to avoid having more than one $@ in the macro definition;
1949 # otherwise, the expansion would require twice the memory of the already
1950 # long list.  Hence, m4_join merely looks for the first non-empty element,
1951 # and outputs just that element; while _m4_join looks for all non-empty
1952 # elements, and outputs them following a separator.  The final trick to
1953 # note is that we decide between recursing with $0 or _$0 based on the
1954 # nested m4_if ending with `_'.
1955 m4_define([m4_join],
1956 [m4_if([$#], [1], [],
1957        [$#], [2], [[$2]],
1958        [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift2($@))])])
1959 m4_define([_m4_join],
1960 [m4_if([$#$2], [2], [],
1961        [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift2($@))])])
1963 # m4_joinall(SEP, ARG1, ARG2...)
1964 # ------------------------------
1965 # Produce ARG1SEPARG2...SEPARGn.  An empty ARG results in back-to-back SEP.
1966 # No expansion is performed on SEP or ARGs.
1967 m4_define([m4_joinall], [[$2]_$0([$1], m4_shift($@))])
1968 m4_define([_m4_joinall],
1969 [m4_if([$#], [2], [], [[$1$3]$0([$1], m4_shift2($@))])])
1971 # m4_combine([SEPARATOR], PREFIX-LIST, [INFIX], SUFFIX...)
1972 # --------------------------------------------------------
1973 # Produce the pairwise combination of every element in the quoted,
1974 # comma-separated PREFIX-LIST with every element from the SUFFIX arguments.
1975 # Each pair is joined with INFIX, and pairs are separated by SEPARATOR.
1976 # No expansion occurs on SEPARATOR, INFIX, or elements of either list.
1978 # For example:
1979 #   m4_combine([, ], [[a], [b], [c]], [-], [1], [2], [3])
1980 #   => a-1, a-2, a-3, b-1, b-2, b-3, c-1, c-2, c-3
1982 # In order to have the correct number of SEPARATORs, we use a temporary
1983 # variable that redefines itself after the first use.  We must use defn
1984 # rather than overquoting in case PREFIX or SUFFIX contains $1, but use
1985 # _m4_defn for speed.  Likewise, we compute the m4_shift3 only once,
1986 # rather than in each iteration of the outer m4_foreach.
1987 m4_define([m4_combine],
1988 [m4_if(m4_eval([$# > 3]), [1],
1989        [m4_pushdef([m4_Separator], [m4_define([m4_Separator],
1990                                     _m4_defn([m4_echo]))])]]dnl
1991 [[m4_foreach([m4_Prefix], [$2],
1992              [m4_foreach([m4_Suffix], ]m4_dquote(m4_dquote(m4_shift3($@)))[,
1993         [m4_Separator([$1])[]_m4_defn([m4_Prefix])[$3]_m4_defn(
1994                                                       [m4_Suffix])])])]]dnl
1995 [[_m4_popdef([m4_Separator])])])
1998 # m4_append(MACRO-NAME, STRING, [SEPARATOR])
1999 # ------------------------------------------
2000 # Redefine MACRO-NAME to hold its former content plus `SEPARATOR`'STRING'
2001 # at the end.  It is valid to use this macro with MACRO-NAME undefined,
2002 # in which case no SEPARATOR is added.  Be aware that the criterion is
2003 # `not being defined', and not `not being empty'.
2005 # Note that neither STRING nor SEPARATOR are expanded here; rather, when
2006 # you expand MACRO-NAME, they will be expanded at that point in time.
2008 # This macro is robust to active symbols.  It can be used to grow
2009 # strings.
2011 #    | m4_define(active, ACTIVE)dnl
2012 #    | m4_append([sentence], [This is an])dnl
2013 #    | m4_append([sentence], [ active ])dnl
2014 #    | m4_append([sentence], [symbol.])dnl
2015 #    | sentence
2016 #    | m4_undefine([active])dnl
2017 #    | sentence
2018 #    => This is an ACTIVE symbol.
2019 #    => This is an active symbol.
2021 # It can be used to define hooks.
2023 #    | m4_define(active, ACTIVE)dnl
2024 #    | m4_append([hooks], [m4_define([act1], [act2])])dnl
2025 #    | m4_append([hooks], [m4_define([act2], [active])])dnl
2026 #    | m4_undefine([active])dnl
2027 #    | act1
2028 #    | hooks
2029 #    | act1
2030 #    => act1
2031 #    =>
2032 #    => active
2034 # It can also be used to create lists, although this particular usage was
2035 # broken prior to autoconf 2.62.
2036 #    | m4_append([list], [one], [, ])dnl
2037 #    | m4_append([list], [two], [, ])dnl
2038 #    | m4_append([list], [three], [, ])dnl
2039 #    | list
2040 #    | m4_dquote(list)
2041 #    => one, two, three
2042 #    => [one],[two],[three]
2044 # Note that m4_append can benefit from amortized O(n) m4 behavior, if
2045 # the underlying m4 implementation is smart enough to avoid copying existing
2046 # contents when enlarging a macro's definition into any pre-allocated storage
2047 # (m4 1.4.x unfortunately does not implement this optimization).  We do
2048 # not implement m4_prepend, since it is inherently O(n^2) (pre-allocated
2049 # storage only occurs at the end of a macro, so the existing contents must
2050 # always be moved).
2052 # Use _m4_defn for speed.
2053 m4_define([m4_append],
2054 [m4_define([$1], m4_ifdef([$1], [_m4_defn([$1])[$3]])[$2])])
2057 # m4_append_uniq(MACRO-NAME, STRING, [SEPARATOR], [IF-UNIQ], [IF-DUP])
2058 # --------------------------------------------------------------------
2059 # Like `m4_append', but append only if not yet present.  Additionally,
2060 # expand IF-UNIQ if STRING was appended, or IF-DUP if STRING was already
2061 # present.  Also, warn if SEPARATOR is not empty and occurs within STRING,
2062 # as the algorithm no longer guarantees uniqueness.
2064 # Note that while m4_append can be O(n) (depending on the quality of the
2065 # underlying M4 implementation), m4_append_uniq is inherently O(n^2)
2066 # because each append operation searches the entire string.
2067 m4_define([m4_append_uniq],
2068 [m4_ifval([$3], [m4_if(m4_index([$2], [$3]), [-1], [],
2069                        [m4_warn([syntax],
2070                                 [$0: `$2' contains `$3'])])])_$0($@)])
2071 m4_define([_m4_append_uniq],
2072 [m4_ifdef([$1],
2073           [m4_if(m4_index([$3]_m4_defn([$1])[$3], [$3$2$3]), [-1],
2074                  [m4_append([$1], [$2], [$3])$4], [$5])],
2075           [m4_define([$1], [$2])$4])])
2077 # m4_append_uniq_w(MACRO-NAME, STRINGS)
2078 # -------------------------------------
2079 # For each of the words in the whitespace separated list STRINGS, append
2080 # only the unique strings to the definition of MACRO-NAME.
2082 # Use _m4_defn for speed.
2083 m4_define([m4_append_uniq_w],
2084 [m4_foreach_w([m4_Word], [$2],
2085               [_m4_append_uniq([$1], _m4_defn([m4_Word]), [ ])])])
2088 # m4_text_wrap(STRING, [PREFIX], [FIRST-PREFIX], [WIDTH])
2089 # -------------------------------------------------------
2090 # Expands into STRING wrapped to hold in WIDTH columns (default = 79).
2091 # If PREFIX is given, each line is prefixed with it.  If FIRST-PREFIX is
2092 # specified, then the first line is prefixed with it.  As a special case,
2093 # if the length of FIRST-PREFIX is greater than that of PREFIX, then
2094 # FIRST-PREFIX will be left alone on the first line.
2096 # No expansion occurs on the contents STRING, PREFIX, or FIRST-PREFIX,
2097 # although quadrigraphs are correctly recognized.
2099 # Typical outputs are:
2101 # m4_text_wrap([Short string */], [   ], [/* ], 20)
2102 #  => /* Short string */
2104 # m4_text_wrap([Much longer string */], [   ], [/* ], 20)
2105 #  => /* Much longer
2106 #  =>    string */
2108 # m4_text_wrap([Short doc.], [          ], [  --short ], 30)
2109 #  =>   --short Short doc.
2111 # m4_text_wrap([Short doc.], [          ], [  --too-wide ], 30)
2112 #  =>   --too-wide
2113 #  =>           Short doc.
2115 # m4_text_wrap([Super long documentation.], [          ], [  --too-wide ], 30)
2116 #  =>   --too-wide
2117 #  =>      Super long
2118 #  =>      documentation.
2120 # FIXME: there is no checking of a longer PREFIX than WIDTH, but do
2121 # we really want to bother with people trying each single corner
2122 # of a software?
2124 # This macro does not leave a trailing space behind the last word of a line,
2125 # which complicates it a bit.  The algorithm is otherwise stupid and simple:
2126 # all the words are preceded by m4_Separator which is defined to empty for
2127 # the first word, and then ` ' (single space) for all the others.
2129 # The algorithm uses a helper that uses $2 through $4 directly, rather than
2130 # using local variables, to avoid m4_defn overhead, or expansion swallowing
2131 # any $.  It also bypasses m4_popdef overhead with _m4_popdef since no user
2132 # macro expansion occurs in the meantime.  Also, the definition is written
2133 # with m4_do, to avoid time wasted on dnl during expansion (since this is
2134 # already a time-consuming macro).
2135 m4_define([m4_text_wrap],
2136 [_$0([$1], [$2], m4_if([$3], [], [[$2]], [[$3]]),
2137      m4_if([$4], [], [79], [[$4]]))])
2138 m4_define([_m4_text_wrap],
2139 m4_do(dnl set up local variables, to avoid repeated calculations
2140 [[m4_pushdef([m4_Indent], m4_qlen([$2]))]],
2141 [[m4_pushdef([m4_Cursor], m4_qlen([$3]))]],
2142 [[m4_pushdef([m4_Separator], [m4_define([m4_Separator], [ ])])]],
2143 dnl expand the first prefix, then check its length vs. regular prefix
2144 dnl same length: nothing special
2145 dnl prefix1 longer: output on line by itself, and reset cursor
2146 dnl prefix1 shorter: pad to length of prefix, and reset cursor
2147 [[[$3]m4_cond([m4_Cursor], m4_Indent, [],
2148               [m4_eval(m4_Cursor > m4_Indent)], [1], [
2149 [$2]m4_define([m4_Cursor], m4_Indent)],
2150               [m4_format([%*s], m4_max([0],
2151   m4_eval(m4_Indent - m4_Cursor)), [])m4_define([m4_Cursor], m4_Indent)])]],
2152 dnl now, for each word, compute the curser after the word is output, then
2153 dnl check if the cursor would exceed the wrap column
2154 dnl if so, reset cursor, and insert newline and prefix
2155 dnl if not, insert the separator (usually a space)
2156 dnl either way, insert the word
2157 [[m4_foreach_w([m4_Word], [$1],
2158   [m4_define([m4_Cursor],
2159              m4_eval(m4_Cursor + m4_qlen(_m4_defn([m4_Word]))
2160                      + 1))m4_if(m4_eval(m4_Cursor > ([$4])),
2161       [1], [m4_define([m4_Cursor],
2162                       m4_eval(m4_Indent + m4_qlen(_m4_defn([m4_Word])) + 1))
2163 [$2]],
2164       [m4_Separator[]])_m4_defn([m4_Word])])]],
2165 dnl finally, clean up the local variabls
2166 [[_m4_popdef([m4_Separator], [m4_Cursor], [m4_Indent])]]))
2169 # m4_text_box(MESSAGE, [FRAME-CHARACTER = `-'])
2170 # ---------------------------------------------
2171 # Turn MESSAGE into:
2172 #  ## ------- ##
2173 #  ## MESSAGE ##
2174 #  ## ------- ##
2175 # using FRAME-CHARACTER in the border.
2176 m4_define([m4_text_box],
2177 [m4_pushdef([m4_Border],
2178             m4_translit(m4_format([%*s], m4_qlen(m4_expand([$1])), []),
2179                         [ ], m4_if([$2], [], [[-]], [[$2]])))dnl
2180 @%:@@%:@ m4_Border @%:@@%:@
2181 @%:@@%:@ $1 @%:@@%:@
2182 @%:@@%:@ m4_Border @%:@@%:@_m4_popdef([m4_Border])dnl
2186 # m4_qlen(STRING)
2187 # ---------------
2188 # Expands to the length of STRING after autom4te converts all quadrigraphs.
2190 # Avoid bpatsubsts for the common case of no quadrigraphs.
2191 m4_define([m4_qlen],
2192 [m4_if(m4_index([$1], [@]), [-1], [m4_len([$1])],
2193        [m4_len(m4_bpatsubst([[$1]],
2194                             [@\(\(<:\|:>\|S|\|%:\|\{:\|:\}\)\(@\)\|&t@\)],
2195                             [\3]))])])
2198 # m4_qdelta(STRING)
2199 # -----------------
2200 # Expands to the net change in the length of STRING from autom4te converting the
2201 # quadrigraphs in STRING.  This number is always negative or zero.
2202 m4_define([m4_qdelta],
2203 [m4_eval(m4_qlen([$1]) - m4_len([$1]))])
2207 ## ----------------------- ##
2208 ## 13. Number processing.  ##
2209 ## ----------------------- ##
2211 # m4_cmp(A, B)
2212 # ------------
2213 # Compare two integer expressions.
2214 # A < B -> -1
2215 # A = B ->  0
2216 # A > B ->  1
2217 m4_define([m4_cmp],
2218 [m4_eval((([$1]) > ([$2])) - (([$1]) < ([$2])))])
2221 # m4_list_cmp(A, B)
2222 # -----------------
2224 # Compare the two lists of integer expressions A and B.  For instance:
2225 #   m4_list_cmp([1, 0],     [1])    ->  0
2226 #   m4_list_cmp([1, 0],     [1, 0]) ->  0
2227 #   m4_list_cmp([1, 2],     [1, 0]) ->  1
2228 #   m4_list_cmp([1, 2, 3],  [1, 2]) ->  1
2229 #   m4_list_cmp([1, 2, -3], [1, 2]) -> -1
2230 #   m4_list_cmp([1, 0],     [1, 2]) -> -1
2231 #   m4_list_cmp([1],        [1, 2]) -> -1
2232 #   m4_define([xa], [oops])dnl
2233 #   m4_list_cmp([[0xa]],    [5+5])  -> 0
2235 # Rather than face the overhead of m4_case, we use a helper function whose
2236 # expansion includes the name of the macro to invoke on the tail, either
2237 # m4_ignore or m4_unquote.  This is particularly useful when comparing
2238 # long lists, since less text is being expanded for deciding when to end
2239 # recursion.  The recursion is between a pair of macros that alternate
2240 # which list is trimmed by one element; this is more efficient than
2241 # calling m4_cdr on both lists from a single macro.  Guarantee exactly
2242 # one expansion of both lists' side effects.
2243 m4_define([m4_list_cmp],
2244 [_$0_raw(m4_dquote($1), m4_dquote($2))])
2246 m4_define([_m4_list_cmp_raw],
2247 [m4_if([$1], [$2], [0], [_m4_list_cmp_1([$1], $2)])])
2249 m4_define([_m4_list_cmp],
2250 [m4_if([$1], [], [0m4_ignore], [$2], [0], [m4_unquote], [$2m4_ignore])])
2252 m4_define([_m4_list_cmp_1],
2253 [_m4_list_cmp_2([$2], [m4_shift2($@)], $1)])
2255 m4_define([_m4_list_cmp_2],
2256 [_m4_list_cmp([$1$3], m4_cmp([$3+0], [$1+0]))(
2257   [_m4_list_cmp_1(m4_dquote(m4_shift3($@)), $2)])])
2259 # m4_max(EXPR, ...)
2260 # m4_min(EXPR, ...)
2261 # -----------------
2262 # Return the decimal value of the maximum (or minimum) in a series of
2263 # integer expressions.
2265 # M4 1.4.x doesn't provide ?:.  Hence this huge m4_eval.  Avoid m4_eval
2266 # if both arguments are identical, but be aware of m4_max(0xa, 10) (hence
2267 # the use of <=, not just <, in the second multiply).
2268 m4_define([m4_max],
2269 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
2270        [$#], [1], [m4_eval([$1])],
2271        [$#$1], [2$2], [m4_eval([$1])],
2272        [$#], [2], [_$0($@)],
2273        [_m4_minmax([_$0], $@)])])
2275 m4_define([_m4_max],
2276 [m4_eval((([$1]) > ([$2])) * ([$1]) + (([$1]) <= ([$2])) * ([$2]))])
2278 m4_define([m4_min],
2279 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
2280        [$#], [1], [m4_eval([$1])],
2281        [$#$1], [2$2], [m4_eval([$1])],
2282        [$#], [2], [_$0($@)],
2283        [_m4_minmax([_$0], $@)])])
2285 m4_define([_m4_min],
2286 [m4_eval((([$1]) < ([$2])) * ([$1]) + (([$1]) >= ([$2])) * ([$2]))])
2288 # _m4_minmax(METHOD, ARG1, ARG2...)
2289 # ---------------------------------
2290 # Common recursion code for m4_max and m4_min.  METHOD must be _m4_max
2291 # or _m4_min, and there must be at least two arguments to combine.
2292 m4_define([_m4_minmax],
2293 [m4_if([$#], [3], [$1([$2], [$3])],
2294        [$0([$1], $1([$2], [$3]), m4_shift3($@))])])
2297 # m4_sign(A)
2298 # ----------
2299 # The sign of the integer expression A.
2300 m4_define([m4_sign],
2301 [m4_eval((([$1]) > 0) - (([$1]) < 0))])
2305 ## ------------------------ ##
2306 ## 14. Version processing.  ##
2307 ## ------------------------ ##
2310 # m4_version_unletter(VERSION)
2311 # ----------------------------
2312 # Normalize beta version numbers with letters to numeric expressions, which
2313 # can then be handed to m4_eval for the purpose of comparison.
2315 #   Nl -> (N+1).-1.(l#)
2317 # for example:
2318 #   [2.14a] -> [2.14+1.-1.[0r36:a]] -> 2.15.-1.10
2319 #   [2.14b] -> [2.15+1.-1.[0r36:b]] -> 2.15.-1.11
2320 #   [2.61aa.b] -> [2.61+1.-1.[0r36:aa],+1.-1.[0r36:b]] -> 2.62.-1.370.1.-1.11
2322 # This macro expects reasonable version numbers, but can handle double
2323 # letters and does not expand any macros.  Original version strings can
2324 # use both `.' and `-' separators.
2326 # Inline constant expansions, to avoid m4_defn overhead.
2327 # _m4_version_unletter is the real workhorse used by m4_version_compare,
2328 # but since [0r36:a] is less readable than 10, we provide a wrapper for
2329 # human use.
2330 m4_define([m4_version_unletter],
2331 [m4_map_sep([m4_eval], [.],
2332             m4_dquote(m4_dquote_elt(m4_unquote(_$0([$1])))))])
2333 m4_define([_m4_version_unletter],
2334 [m4_bpatsubst(m4_translit([[[$1]]], [.-], [,,]),]dnl
2335 m4_dquote(m4_dquote(m4_defn([m4_cr_Letters])))[[+],
2336               [+1,-1,[0r36:\&]])])
2339 # m4_version_compare(VERSION-1, VERSION-2)
2340 # ----------------------------------------
2341 # Compare the two version numbers and expand into
2342 #  -1 if VERSION-1 < VERSION-2
2343 #   0 if           =
2344 #   1 if           >
2346 # Since _m4_version_unletter does not output side effects, we can
2347 # safely bypass the overhead of m4_version_cmp.
2348 m4_define([m4_version_compare],
2349 [_m4_list_cmp_raw(_m4_version_unletter([$1]), _m4_version_unletter([$2]))])
2352 # m4_PACKAGE_NAME
2353 # m4_PACKAGE_TARNAME
2354 # m4_PACKAGE_VERSION
2355 # m4_PACKAGE_STRING
2356 # m4_PACKAGE_BUGREPORT
2357 # --------------------
2358 # If m4sugar/version.m4 is present, then define version strings.  This
2359 # file is optional, provided by Autoconf but absent in Bison.
2360 m4_sinclude([m4sugar/version.m4])
2363 # m4_version_prereq(VERSION, [IF-OK], [IF-NOT = FAIL])
2364 # ----------------------------------------------------
2365 # Check this Autoconf version against VERSION.
2366 m4_define([m4_version_prereq],
2367 m4_ifdef([m4_PACKAGE_VERSION],
2368 [[m4_if(m4_version_compare(]m4_dquote(m4_defn([m4_PACKAGE_VERSION]))[, [$1]),
2369         [-1],
2370         [m4_default([$3],
2371                     [m4_fatal([Autoconf version $1 or higher is required],
2372                               [63])])],
2373         [$2])]],
2374 [[m4_fatal([m4sugar/version.m4 not found])]]))
2377 ## ------------------ ##
2378 ## 15. Set handling.  ##
2379 ## ------------------ ##
2381 # Autoconf likes to create arbitrarily large sets; for example, as of
2382 # this writing, the configure.ac for coreutils tracks a set of more
2383 # than 400 AC_SUBST.  How do we track all of these set members,
2384 # without introducing duplicates?  We could use m4_append_uniq, with
2385 # the set NAME residing in the contents of the macro NAME.
2386 # Unfortunately, m4_append_uniq is quadratic for set creation, because
2387 # it costs O(n) to search the string for each of O(n) insertions; not
2388 # to mention that with m4 1.4.x, even using m4_append is slow, costing
2389 # O(n) rather than O(1) per insertion.  Other set operations, not used
2390 # by Autoconf but still possible by manipulation of the definition
2391 # tracked in macro NAME, include O(n) deletion of one element and O(n)
2392 # computation of set size.  Because the set is exposed to the user via
2393 # the definition of a single macro, we cannot cache any data about the
2394 # set without risking the cache being invalidated by the user
2395 # redefining NAME.
2397 # Can we do better?  Yes, because m4 gives us an O(1) search function
2398 # for free: ifdef.  Additionally, even m4 1.4.x gives us an O(1)
2399 # insert operation for free: pushdef.  But to use these, we must
2400 # represent the set via a group of macros; to keep the set consistent,
2401 # we must hide the set so that the user can only manipulate it through
2402 # accessor macros.  The contents of the set are maintained through two
2403 # access points; _m4_set([name]) is a pushdef stack of values in the
2404 # set, useful for O(n) traversal of the set contents; while the
2405 # existence of _m4_set([name],value) with no particular value is
2406 # useful for O(1) querying of set membership.  And since the user
2407 # cannot externally manipulate the set, we are free to add additional
2408 # caching macros for other performance improvements.  Deletion can be
2409 # O(1) per element rather than O(n), by reworking the definition of
2410 # _m4_set([name],value) to be 0 or 1 based on current membership, and
2411 # adding _m4_set_cleanup(name) to defer the O(n) cleanup of
2412 # _m4_set([name]) until we have another reason to do an O(n)
2413 # traversal.  The existence of _m4_set_cleanup(name) can then be used
2414 # elsewhere to determine if we must dereference _m4_set([name],value),
2415 # or assume that definition implies set membership.  Finally, size can
2416 # be tracked in an O(1) fashion with _m4_set_size(name).
2418 # The quoting in _m4_set([name],value) is chosen so that there is no
2419 # ambiguity with a set whose name contains a comma, and so that we can
2420 # supply the value via _m4_defn([_m4_set([name])]) without needing any
2421 # quote manipulation.
2423 # m4_set_add(SET, VALUE, [IF-UNIQ], [IF-DUP])
2424 # -------------------------------------------
2425 # Add VALUE as an element of SET.  Expand IF-UNIQ on the first
2426 # addition, and IF-DUP if it is already in the set.  Addition of one
2427 # element is O(1), such that overall set creation is O(n).
2429 # We do not want to add a duplicate for a previously deleted but
2430 # unpruned element, but it is just as easy to check existence directly
2431 # as it is to query _m4_set_cleanup($1).
2432 m4_define([m4_set_add],
2433 [m4_ifdef([_m4_set([$1],$2)],
2434           [m4_if(m4_indir([_m4_set([$1],$2)]), [0],
2435                  [m4_define([_m4_set([$1],$2)],
2436                             [1])_m4_set_size([$1], [m4_incr])$3], [$4])],
2437           [m4_define([_m4_set([$1],$2)],
2438                      [1])m4_pushdef([_m4_set([$1])],
2439                                     [$2])_m4_set_size([$1], [m4_incr])$3])])
2441 # m4_set_add_all(SET, VALUE...)
2442 # -----------------------------
2443 # Add each VALUE into SET.  This is O(n) in the number of VALUEs, and
2444 # can be faster than calling m4_set_add for each VALUE.
2446 # Implement two recursion helpers; the check variant is slower but
2447 # handles the case where an element has previously been removed but
2448 # not pruned.  The recursion helpers ignore their second argument, so
2449 # that we can use the faster m4_shift2 and 2 arguments, rather than
2450 # _m4_shift2 and one argument, as the signal to end recursion.
2451 m4_define([m4_set_add_all],
2452 [m4_define([_m4_set_size($1)], m4_eval(m4_set_size([$1])
2453   + m4_len(m4_ifdef([_m4_set_cleanup($1)], [_$0_check], [_$0])([$1], $@))))])
2455 m4_define([_m4_set_add_all],
2456 [m4_if([$#], [2], [],
2457        [m4_ifdef([_m4_set([$1],$3)], [],
2458                  [m4_define([_m4_set([$1],$3)], [1])m4_pushdef([_m4_set([$1])],
2459            [$3])-])$0([$1], m4_shift2($@))])])
2461 m4_define([_m4_set_add_all_check],
2462 [m4_if([$#], [2], [],
2463        [m4_set_add([$1], [$3])$0([$1], m4_shift2($@))])])
2465 # m4_set_contains(SET, VALUE, [IF-PRESENT], [IF-ABSENT])
2466 # ------------------------------------------------------
2467 # Expand IF-PRESENT if SET contains VALUE, otherwise expand IF-ABSENT.
2468 # This is always O(1).
2469 m4_define([m4_set_contains],
2470 [m4_ifdef([_m4_set_cleanup($1)],
2471           [m4_if(m4_ifdef([_m4_set([$1],$2)],
2472                     [m4_indir([_m4_set([$1],$2)])], [0]), [1], [$3], [$4])],
2473           [m4_ifdef([_m4_set([$1],$2)], [$3], [$4])])])
2475 # m4_set_contents(SET, [SEP])
2476 # ---------------------------
2477 # Expand to a single string containing all the elements in SET,
2478 # separated by SEP, without modifying SET.  No provision is made for
2479 # disambiguating set elements that contain non-empty SEP as a
2480 # sub-string, or for recognizing a set that contains only the empty
2481 # string.  Order of the output is not guaranteed.  If any elements
2482 # have been previously removed from the set, this action will prune
2483 # the unused memory.  This is O(n) in the size of the set before
2484 # pruning.
2486 # Use _m4_popdef for speed.  The existence of _m4_set_cleanup($1)
2487 # determines which version of _1 helper we use.
2488 m4_define([m4_set_contents],
2489 [m4_ifdef([_m4_set_cleanup($1)], [_$0_1c], [_$0_1])([$1])_$0_2([$1],
2490     [_m4_defn([_m4_set_($1)])], [[$2]])])
2492 # _m4_set_contents_1(SET)
2493 # _m4_set_contents_1c(SET)
2494 # _m4_set_contents_2(SET, SEP, PREP)
2495 # ----------------------------------
2496 # Expand to a list of quoted elements currently in the set, separated
2497 # by SEP, and moving PREP in front of SEP on recursion.  To avoid
2498 # nesting limit restrictions, the algorithm must be broken into two
2499 # parts; _1 destructively copies the stack in reverse into
2500 # _m4_set_($1), producing no output; then _2 destructively copies
2501 # _m4_set_($1) back into the stack in reverse.  SEP is expanded while
2502 # _m4_set_($1) contains the current element, so a SEP containing
2503 # _m4_defn([_m4_set_($1)]) can produce output in the order the set was
2504 # created.  Behavior is undefined if SEP tries to recursively list or
2505 # modify SET in any way other than calling m4_set_remove on the
2506 # current element.  Use _1 if all entries in the stack are guaranteed
2507 # to be in the set, and _1c to prune removed entries.  Uses _m4_defn
2508 # and _m4_popdef for speed.
2509 m4_define([_m4_set_contents_1],
2510 [m4_ifdef([_m4_set([$1])], [m4_pushdef([_m4_set_($1)],
2511     _m4_defn([_m4_set([$1])]))_m4_popdef([_m4_set([$1])])$0([$1])])])
2513 m4_define([_m4_set_contents_1c],
2514 [m4_ifdef([_m4_set([$1])],
2515           [m4_set_contains([$1], _m4_defn([_m4_set([$1])]),
2516                    [m4_pushdef([_m4_set_($1)], _m4_defn([_m4_set([$1])]))],
2517                    [_m4_popdef([_m4_set([$1],]_m4_defn(
2518       [_m4_set([$1])])[)])])_m4_popdef([_m4_set([$1])])$0([$1])],
2519           [_m4_popdef([_m4_set_cleanup($1)])])])
2521 m4_define([_m4_set_contents_2],
2522 [m4_ifdef([_m4_set_($1)], [m4_pushdef([_m4_set([$1])],
2523     _m4_defn([_m4_set_($1)]))$2[]_m4_popdef([_m4_set_($1)])$0([$1], [$3$2])])])
2525 # m4_set_delete(SET)
2526 # ------------------
2527 # Delete all elements in SET, and reclaim any memory occupied by the
2528 # set.  This is O(n) in the set size.
2530 # Use _m4_defn and _m4_popdef for speed.
2531 m4_define([m4_set_delete],
2532 [m4_ifdef([_m4_set([$1])],
2533           [_m4_popdef([_m4_set([$1],]_m4_defn([_m4_set([$1])])[)],
2534                       [_m4_set([$1])])$0([$1])],
2535           [m4_ifdef([_m4_set_cleanup($1)],
2536                     [_m4_popdef([_m4_set_cleanup($1)])])m4_ifdef(
2537                     [_m4_set_size($1)],
2538                     [_m4_popdef([_m4_set_size($1)])])])])
2540 # m4_set_difference(SET1, SET2)
2541 # -----------------------------
2542 # Produce a LIST of quoted elements that occur in SET1 but not SET2.
2543 # Output a comma prior to any elements, to distinguish the empty
2544 # string from no elements.  This can be directly used as a series of
2545 # arguments, such as for m4_join, or wrapped inside quotes for use in
2546 # m4_foreach.  Order of the output is not guaranteed.
2548 # Short-circuit the idempotence relation.  Use _m4_defn for speed.
2549 m4_define([m4_set_difference],
2550 [m4_if([$1], [$2], [],
2551        [m4_set_foreach([$1], [_m4_element],
2552                        [m4_set_contains([$2], _m4_defn([_m4_element]), [],
2553                                         [,_m4_defn([_m4_element])])])])])
2555 # m4_set_dump(SET, [SEP])
2556 # -----------------------
2557 # Expand to a single string containing all the elements in SET,
2558 # separated by SEP, then delete SET.  In general, if you only need to
2559 # list the contents once, this is faster than m4_set_contents.  No
2560 # provision is made for disambiguating set elements that contain
2561 # non-empty SEP as a sub-string.  Order of the output is not
2562 # guaranteed.  This is O(n) in the size of the set before pruning.
2564 # Use _m4_popdef for speed.  Use existence of _m4_set_cleanup($1) to
2565 # decide if more expensive recursion is needed.
2566 m4_define([m4_set_dump],
2567 [m4_ifdef([_m4_set_size($1)],
2568           [_m4_popdef([_m4_set_size($1)])])m4_ifdef([_m4_set_cleanup($1)],
2569     [_$0_check], [_$0])([$1], [], [$2])])
2571 # _m4_set_dump(SET, SEP, PREP)
2572 # _m4_set_dump_check(SET, SEP, PREP)
2573 # ----------------------------------
2574 # Print SEP and the current element, then delete the element and
2575 # recurse with empty SEP changed to PREP.  The check variant checks
2576 # whether the element has been previously removed.  Use _m4_defn and
2577 # _m4_popdef for speed.
2578 m4_define([_m4_set_dump],
2579 [m4_ifdef([_m4_set([$1])],
2580           [[$2]_m4_defn([_m4_set([$1])])_m4_popdef([_m4_set([$1],]_m4_defn(
2581                 [_m4_set([$1])])[)], [_m4_set([$1])])$0([$1], [$2$3])])])
2583 m4_define([_m4_set_dump_check],
2584 [m4_ifdef([_m4_set([$1])],
2585           [m4_set_contains([$1], _m4_defn([_m4_set([$1])]),
2586                            [[$2]_m4_defn([_m4_set([$1])])])_m4_popdef(
2587     [_m4_set([$1],]_m4_defn([_m4_set([$1])])[)],
2588     [_m4_set([$1])])$0([$1], [$2$3])],
2589           [_m4_popdef([_m4_set_cleanup($1)])])])
2591 # m4_set_empty(SET, [IF-EMPTY], [IF-ELEMENTS])
2592 # --------------------------------------------
2593 # Expand IF-EMPTY if SET has no elements, otherwise IF-ELEMENTS.
2594 m4_define([m4_set_empty],
2595 [m4_ifdef([_m4_set_size($1)],
2596           [m4_if(m4_indir([_m4_set_size($1)]), [0], [$2], [$3])], [$2])])
2598 # m4_set_foreach(SET, VAR, ACTION)
2599 # --------------------------------
2600 # For each element of SET, define VAR to the element and expand
2601 # ACTION.  ACTION should not recursively list SET's contents, add
2602 # elements to SET, nor delete any element from SET except the one
2603 # currently in VAR.  The order that the elements are visited in is not
2604 # guaranteed.  This is faster than the corresponding m4_foreach([VAR],
2605 #   m4_indir([m4_dquote]m4_set_listc([SET])), [ACTION])
2606 m4_define([m4_set_foreach],
2607 [m4_pushdef([$2])m4_ifdef([_m4_set_cleanup($1)],
2608     [_m4_set_contents_1c], [_m4_set_contents_1])([$1])_m4_set_contents_2([$1],
2609        [m4_define([$2], _m4_defn([_m4_set_($1)]))$3[]])m4_popdef([$2])])
2611 # m4_set_intersection(SET1, SET2)
2612 # -------------------------------
2613 # Produce a LIST of quoted elements that occur in both SET1 or SET2.
2614 # Output a comma prior to any elements, to distinguish the empty
2615 # string from no elements.  This can be directly used as a series of
2616 # arguments, such as for m4_join, or wrapped inside quotes for use in
2617 # m4_foreach.  Order of the output is not guaranteed.
2619 # Iterate over the smaller set, and short-circuit the idempotence
2620 # relation.  Use _m4_defn for speed.
2621 m4_define([m4_set_intersection],
2622 [m4_if([$1], [$2], [m4_set_listc([$1])],
2623        m4_eval(m4_set_size([$2]) < m4_set_size([$1])), [1], [$0([$2], [$1])],
2624        [m4_set_foreach([$1], [_m4_element],
2625                        [m4_set_contains([$2], _m4_defn([_m4_element]),
2626                                         [,_m4_defn([_m4_element])])])])])
2628 # m4_set_list(SET)
2629 # m4_set_listc(SET)
2630 # -----------------
2631 # Produce a LIST of quoted elements of SET.  This can be directly used
2632 # as a series of arguments, such as for m4_join or m4_set_add_all, or
2633 # wrapped inside quotes for use in m4_foreach or m4_map.  With
2634 # m4_set_list, there is no way to distinguish an empty set from a set
2635 # containing only the empty string; with m4_set_listc, a leading comma
2636 # is output if there are any elements.
2637 m4_define([m4_set_list],
2638 [m4_ifdef([_m4_set_cleanup($1)], [_m4_set_contents_1c],
2639           [_m4_set_contents_1])([$1])_m4_set_contents_2([$1],
2640                [_m4_defn([_m4_set_($1)])], [,])])
2642 m4_define([m4_set_listc],
2643 [m4_ifdef([_m4_set_cleanup($1)], [_m4_set_contents_1c],
2644           [_m4_set_contents_1])([$1])_m4_set_contents_2([$1],
2645                [,_m4_defn([_m4_set_($1)])])])
2647 # m4_set_remove(SET, VALUE, [IF-PRESENT], [IF-ABSENT])
2648 # ----------------------------------------------------
2649 # If VALUE is an element of SET, delete it and expand IF-PRESENT.
2650 # Otherwise expand IF-ABSENT.  Deleting a single value is O(1),
2651 # although it leaves memory occupied until the next O(n) traversal of
2652 # the set which will compact the set.
2654 # Optimize if the element being removed is the most recently added,
2655 # since defining _m4_set_cleanup($1) slows down so many other macros.
2656 # In particular, this plays well with m4_set_foreach.
2657 m4_define([m4_set_remove],
2658 [m4_set_contains([$1], [$2], [_m4_set_size([$1],
2659     [m4_decr])m4_if(_m4_defn([_m4_set([$1])]), [$2],
2660                     [_m4_popdef([_m4_set([$1],$2)], [_m4_set([$1])])],
2661                     [m4_define([_m4_set_cleanup($1)])m4_define(
2662                       [_m4_set([$1],$2)], [0])])$3], [$4])])
2664 # m4_set_size(SET)
2665 # ----------------
2666 # Expand to the number of elements currently in SET.  This operation
2667 # is O(1), and thus more efficient than m4_count(m4_set_list([SET])).
2668 m4_define([m4_set_size],
2669 [m4_ifdef([_m4_set_size($1)], [m4_indir([_m4_set_size($1)])], [0])])
2671 # _m4_set_size(SET, ACTION)
2672 # -------------------------
2673 # ACTION must be either m4_incr or m4_decr, and the size of SET is
2674 # changed accordingly.  If the set is empty, ACTION must not be
2675 # m4_decr.
2676 m4_define([_m4_set_size],
2677 [m4_define([_m4_set_size($1)],
2678            m4_ifdef([_m4_set_size($1)], [$2(m4_indir([_m4_set_size($1)]))],
2679                     [1]))])
2681 # m4_set_union(SET1, SET2)
2682 # ------------------------
2683 # Produce a LIST of double quoted elements that occur in either SET1
2684 # or SET2, without duplicates.  Output a comma prior to any elements,
2685 # to distinguish the empty string from no elements.  This can be
2686 # directly used as a series of arguments, such as for m4_join, or
2687 # wrapped inside quotes for use in m4_foreach.  Order of the output is
2688 # not guaranteed.
2690 # We can rely on the fact that m4_set_listc prunes SET1, so we don't
2691 # need to check _m4_set([$1],element) for 0.  Use _m4_defn for speed.
2692 # Short-circuit the idempotence relation.
2693 m4_define([m4_set_union],
2694 [m4_set_listc([$1])m4_if([$1], [$2], [], [m4_set_foreach([$2], [_m4_element],
2695   [m4_ifdef([_m4_set([$1],]_m4_defn([_m4_element])[)], [],
2696             [,_m4_defn([_m4_element])])])])])
2699 ## ------------------- ##
2700 ## 16. File handling.  ##
2701 ## ------------------- ##
2704 # It is a real pity that M4 comes with no macros to bind a diversion
2705 # to a file.  So we have to deal without, which makes us a lot more
2706 # fragile than we should.
2709 # m4_file_append(FILE-NAME, CONTENT)
2710 # ----------------------------------
2711 m4_define([m4_file_append],
2712 [m4_syscmd([cat >>$1 <<_m4eof
2714 _m4eof
2716 m4_if(m4_sysval, [0], [],
2717       [m4_fatal([$0: cannot write: $1])])])
2721 ## ------------------------ ##
2722 ## 17. Setting M4sugar up.  ##
2723 ## ------------------------ ##
2726 # m4_init
2727 # -------
2728 # Initialize the m4sugar language.
2729 m4_define([m4_init],
2730 [# All the M4sugar macros start with `m4_', except `dnl' kept as is
2731 # for sake of simplicity.
2732 m4_pattern_forbid([^_?m4_])
2733 m4_pattern_forbid([^dnl$])
2735 # If __m4_version__ is defined, we assume that we are being run by M4
2736 # 1.6 or newer, and thus that $@ recursion is linear; nothing further
2737 # needs to be done.  But if it is missing, we assume we are being run
2738 # by M4 1.4.x, that $@ recursion is quadratic, and that we need
2739 # foreach-based replacement macros.  Use the raw builtin to avoid
2740 # tripping up include tracing.
2741 m4_ifndef([__m4_version__], [m4_builtin([include], [m4sugar/foreach.m4])])
2743 # _m4_divert_diversion should be defined:
2744 m4_divert_push([KILL])
2746 # Check the divert push/pop perfect balance.
2747 m4_wrap([m4_divert_pop([])
2748          m4_ifdef([_m4_divert_diversion],
2749            [m4_fatal([$0: unbalanced m4_divert_push:]_m4_divert_n_stack)])[]])