Reword the copyright notices to match what's suggested in GPLv3.
[autoconf.git] / lib / m4sugar / m4sugar.m4
blob0e34d4474065c23247532ed100a629b9e55cfcab
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 Free Software
7 # Foundation, Inc.
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 # As a special exception, the Free Software Foundation gives unlimited
23 # permission to copy, distribute and modify the configure scripts that
24 # are the output of Autoconf.  You need not follow the terms of the GNU
25 # General Public License when using or distributing such scripts, even
26 # though portions of the text of Autoconf appear in them.  The GNU
27 # General Public License (GPL) does govern all other use of the material
28 # that constitutes the Autoconf program.
30 # Certain portions of the Autoconf source text are designed to be copied
31 # (in certain cases, depending on the input) into the output of
32 # Autoconf.  We call these the "data" portions.  The rest of the Autoconf
33 # source text consists of comments plus executable code that decides which
34 # of the data portions to output in any given case.  We call these
35 # comments and executable code the "non-data" portions.  Autoconf never
36 # copies any of the non-data portions into its output.
38 # This special exception to the GPL applies to versions of Autoconf
39 # released by the Free Software Foundation.  When you make and
40 # distribute a modified version of Autoconf, you may extend this special
41 # exception to the GPL to apply to your modified version as well, *unless*
42 # your modified version has the potential to copy into its output some
43 # of the text that was the non-data portion of the version that you started
44 # with.  (In other words, unless your change moves or copies text from
45 # the non-data portions to the data portions.)  If your modification has
46 # such potential, you must delete any notice of this special exception
47 # to the GPL from your modified version.
49 # Written by Akim Demaille.
52 # Set the quotes, whatever the current quoting system.
53 changequote()
54 changequote([, ])
56 # Some old m4's don't support m4exit.  But they provide
57 # equivalent functionality by core dumping because of the
58 # long macros we define.
59 ifdef([__gnu__], ,
60 [errprint(M4sugar requires GNU M4. Install it before installing M4sugar or
61 set the M4 environment variable to its absolute file name.)
62 m4exit(2)])
65 ## ------------------------------- ##
66 ## 1. Simulate --prefix-builtins.  ##
67 ## ------------------------------- ##
69 # m4_define
70 # m4_defn
71 # m4_undefine
72 define([m4_define],   defn([define]))
73 define([m4_defn],     defn([defn]))
74 define([m4_undefine], defn([undefine]))
76 m4_undefine([define])
77 m4_undefine([defn])
78 m4_undefine([undefine])
81 # m4_copy(SRC, DST)
82 # -----------------
83 # Define DST as the definition of SRC.
84 # What's the difference between:
85 # 1. m4_copy([from], [to])
86 # 2. m4_define([to], [from($@)])
87 # Well, obviously 1 is more expensive in space.  Maybe 2 is more expensive
88 # in time, but because of the space cost of 1, it's not that obvious.
89 # Nevertheless, one huge difference is the handling of `$0'.  If `from'
90 # uses `$0', then with 1, `to''s `$0' is `to', while it is `from' in 2.
91 # The user will certainly prefer to see `to'.
92 m4_define([m4_copy],
93 [m4_define([$2], m4_defn([$1]))])
96 # m4_rename(SRC, DST)
97 # -------------------
98 # Rename the macro SRC as DST.
99 m4_define([m4_rename],
100 [m4_copy([$1], [$2])m4_undefine([$1])])
103 # m4_rename_m4(MACRO-NAME)
104 # ------------------------
105 # Rename MACRO-NAME as m4_MACRO-NAME.
106 m4_define([m4_rename_m4],
107 [m4_rename([$1], [m4_$1])])
110 # m4_copy_unm4(m4_MACRO-NAME)
111 # ---------------------------
112 # Copy m4_MACRO-NAME as MACRO-NAME.
113 m4_define([m4_copy_unm4],
114 [m4_copy([$1], m4_bpatsubst([$1], [^m4_\(.*\)], [[\1]]))])
117 # Some m4 internals have names colliding with tokens we might use.
118 # Rename them a` la `m4 --prefix-builtins'.
119 m4_rename_m4([builtin])
120 m4_rename_m4([changecom])
121 m4_rename_m4([changequote])
122 m4_rename_m4([debugfile])
123 m4_rename_m4([debugmode])
124 m4_rename_m4([decr])
125 m4_undefine([divert])
126 m4_rename_m4([divnum])
127 m4_rename_m4([dumpdef])
128 m4_rename_m4([errprint])
129 m4_rename_m4([esyscmd])
130 m4_rename_m4([eval])
131 m4_rename_m4([format])
132 m4_rename_m4([ifdef])
133 m4_rename([ifelse], [m4_if])
134 m4_undefine([include])
135 m4_rename_m4([incr])
136 m4_rename_m4([index])
137 m4_rename_m4([indir])
138 m4_rename_m4([len])
139 m4_rename([m4exit], [m4_exit])
140 m4_rename([m4wrap], [m4_wrap])
141 m4_ifdef([mkstemp],dnl added in M4 1.4.8
142 [m4_rename_m4([mkstemp])
143 m4_copy([m4_mkstemp], [m4_maketemp])
144 m4_undefine([maketemp])],
145 [m4_rename_m4([maketemp])
146 m4_copy([m4_maketemp], [m4_mkstemp])])
147 m4_rename([patsubst], [m4_bpatsubst])
148 m4_undefine([popdef])
149 m4_rename_m4([pushdef])
150 m4_rename([regexp], [m4_bregexp])
151 m4_rename_m4([shift])
152 m4_undefine([sinclude])
153 m4_rename_m4([substr])
154 m4_rename_m4([symbols])
155 m4_rename_m4([syscmd])
156 m4_rename_m4([sysval])
157 m4_rename_m4([traceoff])
158 m4_rename_m4([traceon])
159 m4_rename_m4([translit])
160 m4_undefine([undivert])
163 ## ------------------- ##
164 ## 2. Error messages.  ##
165 ## ------------------- ##
168 # m4_location
169 # -----------
170 m4_define([m4_location],
171 [__file__:__line__])
174 # m4_errprintn(MSG)
175 # -----------------
176 # Same as `errprint', but with the missing end of line.
177 m4_define([m4_errprintn],
178 [m4_errprint([$1
179 ])])
182 # m4_warning(MSG)
183 # ---------------
184 # Warn the user.
185 m4_define([m4_warning],
186 [m4_errprintn(m4_location[: warning: $1])])
189 # m4_fatal(MSG, [EXIT-STATUS])
190 # ----------------------------
191 # Fatal the user.                                                      :)
192 m4_define([m4_fatal],
193 [m4_errprintn(m4_location[: error: $1])dnl
194 m4_expansion_stack_dump()dnl
195 m4_exit(m4_if([$2],, 1, [$2]))])
198 # m4_assert(EXPRESSION, [EXIT-STATUS = 1])
199 # ----------------------------------------
200 # This macro ensures that EXPRESSION evaluates to true, and exits if
201 # EXPRESSION evaluates to false.
202 m4_define([m4_assert],
203 [m4_if(m4_eval([$1]), 0,
204        [m4_fatal([assert failed: $1], [$2])])])
208 ## ------------- ##
209 ## 3. Warnings.  ##
210 ## ------------- ##
213 # _m4_warn(CATEGORY, MESSAGE, STACK-TRACE)
214 # ----------------------------------------
215 # Report a MESSAGE to the user if the CATEGORY of warnings is enabled.
216 # This is for traces only.
217 # The STACK-TRACE is a \n-separated list of "LOCATION: MESSAGE".
218 m4_define([_m4_warn], [])
221 # m4_warn(CATEGORY, MESSAGE)
222 # --------------------------
223 # Report a MESSAGE to the user if the CATEGORY of warnings is enabled.
224 m4_define([m4_warn],
225 [_m4_warn([$1], [$2],
226 m4_ifdef([m4_expansion_stack],
227          [m4_defn([m4_expansion_stack])
228 m4_location[: the top level]]))dnl
233 ## ------------------- ##
234 ## 4. File inclusion.  ##
235 ## ------------------- ##
238 # We also want to neutralize include (and sinclude for symmetry),
239 # but we want to extend them slightly: warn when a file is included
240 # several times.  This is in general a dangerous operation because
241 # quite nobody quotes the first argument of m4_define.
243 # For instance in the following case:
244 #   m4_define(foo, [bar])
245 # then a second reading will turn into
246 #   m4_define(bar, [bar])
247 # which is certainly not what was meant.
249 # m4_include_unique(FILE)
250 # -----------------------
251 # Declare that the FILE was loading; and warn if it has already
252 # been included.
253 m4_define([m4_include_unique],
254 [m4_ifdef([m4_include($1)],
255           [m4_warn([syntax], [file `$1' included several times])])dnl
256 m4_define([m4_include($1)])])
259 # m4_include(FILE)
260 # ----------------
261 # As the builtin include, but warns against multiple inclusions.
262 m4_define([m4_include],
263 [m4_include_unique([$1])dnl
264 m4_builtin([include], [$1])])
267 # m4_sinclude(FILE)
268 # -----------------
269 # As the builtin sinclude, but warns against multiple inclusions.
270 m4_define([m4_sinclude],
271 [m4_include_unique([$1])dnl
272 m4_builtin([sinclude], [$1])])
276 ## ------------------------------------ ##
277 ## 5. Additional branching constructs.  ##
278 ## ------------------------------------ ##
280 # Both `m4_ifval' and `m4_ifset' tests against the empty string.  The
281 # difference is that `m4_ifset' is specialized on macros.
283 # In case of arguments of macros, eg $[1], it makes little difference.
284 # In the case of a macro `FOO', you don't want to check `m4_ifval(FOO,
285 # TRUE)', because if `FOO' expands with commas, there is a shifting of
286 # the arguments.  So you want to run `m4_ifval([FOO])', but then you just
287 # compare the *string* `FOO' against `', which, of course fails.
289 # So you want a variation of `m4_ifset' that expects a macro name as $[1].
290 # If this macro is both defined and defined to a non empty value, then
291 # it runs TRUE etc.
294 # m4_ifval(COND, [IF-TRUE], [IF-FALSE])
295 # -------------------------------------
296 # If COND is not the empty string, expand IF-TRUE, otherwise IF-FALSE.
297 # Comparable to m4_ifdef.
298 m4_define([m4_ifval],
299 [m4_if([$1], [], [$3], [$2])])
302 # m4_n(TEXT)
303 # ----------
304 # If TEXT is not empty, return TEXT and a new line, otherwise nothing.
305 m4_define([m4_n],
306 [m4_if([$1],
307        [], [],
308            [$1
309 ])])
312 # m4_ifvaln(COND, [IF-TRUE], [IF-FALSE])
313 # --------------------------------------
314 # Same as `m4_ifval', but add an extra newline to IF-TRUE or IF-FALSE
315 # unless that argument is empty.
316 m4_define([m4_ifvaln],
317 [m4_if([$1],
318        [],   [m4_n([$3])],
319              [m4_n([$2])])])
322 # m4_ifset(MACRO, [IF-TRUE], [IF-FALSE])
323 # --------------------------------------
324 # If MACRO has no definition, or of its definition is the empty string,
325 # expand IF-FALSE, otherwise IF-TRUE.
326 m4_define([m4_ifset],
327 [m4_ifdef([$1],
328           [m4_ifval(m4_defn([$1]), [$2], [$3])],
329           [$3])])
332 # m4_ifndef(NAME, [IF-NOT-DEFINED], [IF-DEFINED])
333 # -----------------------------------------------
334 m4_define([m4_ifndef],
335 [m4_ifdef([$1], [$3], [$2])])
338 # m4_case(SWITCH, VAL1, IF-VAL1, VAL2, IF-VAL2, ..., DEFAULT)
339 # -----------------------------------------------------------
340 # m4 equivalent of
341 # switch (SWITCH)
342 # {
343 #   case VAL1:
344 #     IF-VAL1;
345 #     break;
346 #   case VAL2:
347 #     IF-VAL2;
348 #     break;
349 #   ...
350 #   default:
351 #     DEFAULT;
352 #     break;
353 # }.
354 # All the values are optional, and the macro is robust to active
355 # symbols properly quoted.
356 m4_define([m4_case],
357 [m4_if([$#], 0, [],
358        [$#], 1, [],
359        [$#], 2, [$2],
360        [$1], [$2], [$3],
361        [$0([$1], m4_shiftn(3, $@))])])
364 # m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT)
365 # -----------------------------------------------------
366 # m4 equivalent of
368 # if (SWITCH =~ RE1)
369 #   VAL1;
370 # elif (SWITCH =~ RE2)
371 #   VAL2;
372 # elif ...
373 #   ...
374 # else
375 #   DEFAULT
377 # All the values are optional, and the macro is robust to active symbols
378 # properly quoted.
379 m4_define([m4_bmatch],
380 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
381        [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
382        [$#], 2, [$2],
383        [m4_if(m4_bregexp([$1], [$2]), -1, [$0([$1], m4_shiftn(3, $@))],
384               [$3])])])
387 # m4_car(LIST)
388 # m4_cdr(LIST)
389 # ------------
390 # Manipulate m4 lists.
391 m4_define([m4_car], [[$1]])
392 m4_define([m4_cdr],
393 [m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])],
394        [$#], 1, [],
395        [m4_dquote(m4_shift($@))])])
398 # m4_map(MACRO, LIST)
399 # -------------------
400 # Invoke MACRO($1), MACRO($2) etc. where $1, $2... are the elements
401 # of LIST (which can be lists themselves, for multiple arguments MACROs).
402 m4_define([m4_fst], [$1])
403 m4_define([m4_map],
404 [m4_if([$2], [[]], [],
405        [_m4_map([$1], [$2])])])
406 m4_define([_m4_map],
407 [m4_ifval([$2],
408           [$1(m4_fst($2))[]_m4_map([$1], m4_cdr($2))])])
411 # m4_map_sep(MACRO, SEPARATOR, LIST)
412 # ----------------------------------
413 # Invoke MACRO($1), SEPARATOR, MACRO($2), ..., MACRO($N) where $1, $2... $N
414 # are the elements of LIST (which can be lists themselves, for multiple
415 # arguments MACROs).
416 m4_define([m4_map_sep],
417 [m4_if([$3], [[]], [],
418        [$1(m4_fst($3))[]_m4_map([$2[]$1], m4_cdr($3))])])
421 ## ---------------------------------------- ##
422 ## 6. Enhanced version of some primitives.  ##
423 ## ---------------------------------------- ##
425 # m4_bpatsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...)
426 # ----------------------------------------------------
427 # m4 equivalent of
429 #   $_ = STRING;
430 #   s/RE1/SUBST1/g;
431 #   s/RE2/SUBST2/g;
432 #   ...
434 # All the values are optional, and the macro is robust to active symbols
435 # properly quoted.
437 # I would have liked to name this macro `m4_bpatsubst', unfortunately,
438 # due to quotation problems, I need to double quote $1 below, therefore
439 # the anchors are broken :(  I can't let users be trapped by that.
440 m4_define([m4_bpatsubsts],
441 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
442        [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
443        [$#], 2, [m4_builtin([patsubst], $@)],
444        [$0(m4_builtin([patsubst], [[$1]], [$2], [$3]),
445            m4_shiftn(3, $@))])])
449 # m4_do(STRING, ...)
450 # ------------------
451 # This macro invokes all its arguments (in sequence, of course).  It is
452 # useful for making your macros more structured and readable by dropping
453 # unnecessary dnl's and have the macros indented properly.
454 m4_define([m4_do],
455 [m4_if($#, 0, [],
456        $#, 1, [$1],
457        [$1[]m4_do(m4_shift($@))])])
460 # m4_define_default(MACRO, VALUE)
461 # -------------------------------
462 # If MACRO is undefined, set it to VALUE.
463 m4_define([m4_define_default],
464 [m4_ifndef([$1], [m4_define($@)])])
467 # m4_default(EXP1, EXP2)
468 # ----------------------
469 # Returns EXP1 if non empty, otherwise EXP2.
470 m4_define([m4_default],
471 [m4_ifval([$1], [$1], [$2])])
474 # m4_defn(NAME)
475 # -------------
476 # Unlike to the original, don't tolerate popping something which is
477 # undefined.
478 m4_define([m4_defn],
479 [m4_ifndef([$1],
480            [m4_fatal([$0: undefined macro: $1])])dnl
481 m4_builtin([defn], $@)])
484 # _m4_dumpdefs_up(NAME)
485 # ---------------------
486 m4_define([_m4_dumpdefs_up],
487 [m4_ifdef([$1],
488           [m4_pushdef([_m4_dumpdefs], m4_defn([$1]))dnl
489 m4_dumpdef([$1])dnl
490 m4_popdef([$1])dnl
491 _m4_dumpdefs_up([$1])])])
494 # _m4_dumpdefs_down(NAME)
495 # -----------------------
496 m4_define([_m4_dumpdefs_down],
497 [m4_ifdef([_m4_dumpdefs],
498           [m4_pushdef([$1], m4_defn([_m4_dumpdefs]))dnl
499 m4_popdef([_m4_dumpdefs])dnl
500 _m4_dumpdefs_down([$1])])])
503 # m4_dumpdefs(NAME)
504 # -----------------
505 # Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its
506 # value stack (most recent displayed first).
507 m4_define([m4_dumpdefs],
508 [_m4_dumpdefs_up([$1])dnl
509 _m4_dumpdefs_down([$1])])
512 # m4_popdef(NAME)
513 # ---------------
514 # Unlike to the original, don't tolerate popping something which is
515 # undefined.
516 m4_define([m4_popdef],
517 [m4_ifndef([$1],
518            [m4_fatal([$0: undefined macro: $1])])dnl
519 m4_builtin([popdef], $@)])
522 # m4_quote(ARGS)
523 # --------------
524 # Return ARGS as a single arguments.
526 # It is important to realize the difference between `m4_quote(exp)' and
527 # `[exp]': in the first case you obtain the quoted *result* of the
528 # expansion of EXP, while in the latter you just obtain the string
529 # `exp'.
530 m4_define([m4_quote],  [[$*]])
531 m4_define([m4_dquote],  [[$@]])
534 # m4_noquote(STRING)
535 # ------------------
536 # Return the result of ignoring all quotes in STRING and invoking the
537 # macros it contains.  Amongst other things useful for enabling macro
538 # invocations inside strings with [] blocks (for instance regexps and
539 # help-strings).
540 m4_define([m4_noquote],
541 [m4_changequote(-=<{,}>=-)$1-=<{}>=-m4_changequote([,])])
544 # m4_shiftn(N, ...)
545 # -----------------
546 # Returns ... shifted N times.  Useful for recursive "varargs" constructs.
547 m4_define([m4_shiftn],
548 [m4_assert(($1 >= 0) && ($# > $1))dnl
549 _m4_shiftn($@)])
551 m4_define([_m4_shiftn],
552 [m4_if([$1], 0,
553        [m4_shift($@)],
554        [_m4_shiftn(m4_eval([$1]-1), m4_shift(m4_shift($@)))])])
557 # m4_undefine(NAME)
558 # -----------------
559 # Unlike to the original, don't tolerate undefining something which is
560 # undefined.
561 m4_define([m4_undefine],
562 [m4_ifndef([$1],
563            [m4_fatal([$0: undefined macro: $1])])dnl
564 m4_builtin([undefine], $@)])
567 ## -------------------------- ##
568 ## 7. Implementing m4 loops.  ##
569 ## -------------------------- ##
572 # m4_for(VARIABLE, FIRST, LAST, [STEP = +/-1], EXPRESSION)
573 # --------------------------------------------------------
574 # Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO.
575 # Both limits are included, and bounds are checked for consistency.
576 m4_define([m4_for],
577 [m4_pushdef([$1], m4_eval([$2]))dnl
578 m4_if(m4_eval(([$3]) > $1), 1,
579 [m4_pushdef([_m4_step], m4_eval(m4_default([$4], 1)))dnl
580 m4_assert(_m4_step > 0)dnl
581 _m4_for([$1], m4_eval((([$3]) - $1) / _m4_step * _m4_step + $1), _m4_step, [$5])],
582       m4_eval(([$3]) < $1), 1,
583 [m4_pushdef([_m4_step], m4_eval(m4_default([$4], -1)))dnl
584 m4_assert(_m4_step < 0)dnl
585 _m4_for([$1], m4_eval(($1 - ([$3])) / -(_m4_step) * _m4_step + $1), _m4_step, [$5])],
586       [m4_pushdef(_m4_step,[])dnl
587 $5])[]dnl
588 m4_popdef([_m4_step])dnl
589 m4_popdef([$1])])
592 # _m4_for(VARIABLE, LAST, STEP, EXPRESSION)
593 # -----------------------------------------
594 # Core of the loop, no consistency checks, all arguments are plain numbers.
595 m4_define([_m4_for],
596 [$4[]dnl
597 m4_if($1, [$2], [],
598       [m4_define([$1], m4_eval($1+[$3]))_m4_for([$1], [$2], [$3], [$4])])])
601 # Implementing `foreach' loops in m4 is much more tricky than it may
602 # seem.  Actually, the example of a `foreach' loop in the m4
603 # documentation is wrong: it does not quote the arguments properly,
604 # which leads to undesirable expansions.
606 # The example in the documentation is:
608 # | # foreach(VAR, (LIST), STMT)
609 # | m4_define([foreach],
610 # |        [m4_pushdef([$1])_foreach([$1], [$2], [$3])m4_popdef([$1])])
611 # | m4_define([_arg1], [$1])
612 # | m4_define([_foreach],
613 # |            [m4_if([$2], [()], ,
614 # |                  [m4_define([$1], _arg1$2)$3[]_foreach([$1],
615 # |                                                        (shift$2),
616 # |                                                        [$3])])])
618 # But then if you run
620 # | m4_define(a, 1)
621 # | m4_define(b, 2)
622 # | m4_define(c, 3)
623 # | foreach([f], [([a], [(b], [c)])], [echo f
624 # | ])
626 # it gives
628 #  => echo 1
629 #  => echo (2,3)
631 # which is not what is expected.
633 # Of course the problem is that many quotes are missing.  So you add
634 # plenty of quotes at random places, until you reach the expected
635 # result.  Alternatively, if you are a quoting wizard, you directly
636 # reach the following implementation (but if you really did, then
637 # apply to the maintenance of m4sugar!).
639 # | # foreach(VAR, (LIST), STMT)
640 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
641 # | m4_define([_arg1], [[$1]])
642 # | m4_define([_foreach],
643 # |  [m4_if($2, [()], ,
644 # |          [m4_define([$1], [_arg1$2])$3[]_foreach([$1],
645 # |                                                 [(shift$2)],
646 # |                                                 [$3])])])
648 # which this time answers
650 #  => echo a
651 #  => echo (b
652 #  => echo c)
654 # Bingo!
656 # Well, not quite.
658 # With a better look, you realize that the parens are more a pain than
659 # a help: since anyway you need to quote properly the list, you end up
660 # with always using an outermost pair of parens and an outermost pair
661 # of quotes.  Rejecting the parens both eases the implementation, and
662 # simplifies the use:
664 # | # foreach(VAR, (LIST), STMT)
665 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
666 # | m4_define([_arg1], [$1])
667 # | m4_define([_foreach],
668 # |  [m4_if($2, [], ,
669 # |          [m4_define([$1], [_arg1($2)])$3[]_foreach([$1],
670 # |                                                   [shift($2)],
671 # |                                                   [$3])])])
674 # Now, just replace the `$2' with `m4_quote($2)' in the outer `m4_if'
675 # to improve robustness, and you come up with a quite satisfactory
676 # implementation.
679 # m4_foreach(VARIABLE, LIST, EXPRESSION)
680 # --------------------------------------
682 # Expand EXPRESSION assigning each value of the LIST to VARIABLE.
683 # LIST should have the form `item_1, item_2, ..., item_n', i.e. the
684 # whole list must *quoted*.  Quote members too if you don't want them
685 # to be expanded.
687 # This macro is robust to active symbols:
688 #      | m4_define(active, [ACT, IVE])
689 #      | m4_foreach(Var, [active, active], [-Var-])
690 #     => -ACT--IVE--ACT--IVE-
692 #      | m4_foreach(Var, [[active], [active]], [-Var-])
693 #     => -ACT, IVE--ACT, IVE-
695 #      | m4_foreach(Var, [[[active]], [[active]]], [-Var-])
696 #     => -active--active-
697 m4_define([m4_foreach],
698 [m4_pushdef([$1])_m4_foreach($@)m4_popdef([$1])])
700 m4_define([_m4_foreach],
701 [m4_ifval([$2],
702           [m4_define([$1], m4_car($2))$3[]dnl
703 _m4_foreach([$1], m4_cdr($2), [$3])])])
706 # m4_foreach_w(VARIABLE, LIST, EXPRESSION)
707 # ----------------------------------------
709 # Like m4_foreach, but the list is whitespace separated.
711 # This macro is robust to active symbols:
712 #    m4_foreach_w([Var], [ active
713 #    b  act\
714 #    ive  ], [-Var-])end
715 #    => -active--b--active-end
717 m4_define([m4_foreach_w],
718 [m4_foreach([$1], m4_split(m4_normalize([$2])), [$3])])
722 ## --------------------------- ##
723 ## 8. More diversion support.  ##
724 ## --------------------------- ##
727 # _m4_divert(DIVERSION-NAME or NUMBER)
728 # ------------------------------------
729 # If DIVERSION-NAME is the name of a diversion, return its number,
730 # otherwise if it is a NUMBER return it.
731 m4_define([_m4_divert],
732 [m4_ifdef([_m4_divert($1)],
733           [m4_indir([_m4_divert($1)])],
734           [$1])])
736 # KILL is only used to suppress output.
737 m4_define([_m4_divert(KILL)],           -1)
740 # _m4_divert_n_stack
741 # ------------------
742 # Print m4_divert_stack with newline prepended, if it's nonempty.
743 m4_define([_m4_divert_n_stack],
744 [m4_ifdef([m4_divert_stack], [
745 m4_defn([m4_divert_stack])])])
748 # m4_divert(DIVERSION-NAME)
749 # -------------------------
750 # Change the diversion stream to DIVERSION-NAME.
751 m4_define([m4_divert],
752 [m4_define([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
753 m4_builtin([divert], _m4_divert([$1]))dnl
757 # m4_divert_push(DIVERSION-NAME)
758 # ------------------------------
759 # Change the diversion stream to DIVERSION-NAME, while stacking old values.
760 m4_define([m4_divert_push],
761 [m4_pushdef([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl
762 m4_pushdef([_m4_divert_diversion], [$1])dnl
763 m4_builtin([divert], _m4_divert([$1]))dnl
767 # m4_divert_pop([DIVERSION-NAME])
768 # -------------------------------
769 # Change the diversion stream to its previous value, unstacking it.
770 # If specified, verify we left DIVERSION-NAME.
771 # When we pop the last value from the stack, we divert to -1.
772 m4_define([m4_divert_pop],
773 [m4_ifndef([_m4_divert_diversion],
774            [m4_fatal([too many m4_divert_pop])])dnl
775 m4_if([$1], [], [],
776       [$1], m4_defn([_m4_divert_diversion]), [],
777       [m4_fatal([$0($1): diversion mismatch: ]_m4_divert_n_stack)])dnl
778 m4_popdef([m4_divert_stack])dnl
779 m4_popdef([_m4_divert_diversion])dnl
780 m4_builtin([divert],
781            m4_ifdef([_m4_divert_diversion],
782                     [_m4_divert(m4_defn([_m4_divert_diversion]))],
783                     -1))dnl
787 # m4_divert_text(DIVERSION-NAME, CONTENT)
788 # ---------------------------------------
789 # Output CONTENT into DIVERSION-NAME (which may be a number actually).
790 # An end of line is appended for free to CONTENT.
791 m4_define([m4_divert_text],
792 [m4_divert_push([$1])dnl
794 m4_divert_pop([$1])dnl
798 # m4_divert_once(DIVERSION-NAME, CONTENT)
799 # ---------------------------------------
800 # Output once CONTENT into DIVERSION-NAME (which may be a number
801 # actually).  An end of line is appended for free to CONTENT.
802 m4_define([m4_divert_once],
803 [m4_expand_once([m4_divert_text([$1], [$2])])])
806 # m4_undivert(DIVERSION-NAME)
807 # ---------------------------
808 # Undivert DIVERSION-NAME.
809 m4_define([m4_undivert],
810 [m4_builtin([undivert], _m4_divert([$1]))])
813 ## -------------------------------------------- ##
814 ## 8. Defining macros with bells and whistles.  ##
815 ## -------------------------------------------- ##
817 # `m4_defun' is basically `m4_define' but it equips the macro with the
818 # needed machinery for `m4_require'.  A macro must be m4_defun'd if
819 # either it is m4_require'd, or it m4_require's.
821 # Two things deserve attention and are detailed below:
822 #  1. Implementation of m4_require
823 #  2. Keeping track of the expansion stack
825 # 1. Implementation of m4_require
826 # ===============================
828 # Of course m4_defun AC_PROVIDE's the macro, so that a macro which has
829 # been expanded is not expanded again when m4_require'd, but the
830 # difficult part is the proper expansion of macros when they are
831 # m4_require'd.
833 # The implementation is based on two ideas, (i) using diversions to
834 # prepare the expansion of the macro and its dependencies (by Franc,ois
835 # Pinard), and (ii) expand the most recently m4_require'd macros _after_
836 # the previous macros (by Axel Thimm).
839 # The first idea: why using diversions?
840 # -------------------------------------
842 # When a macro requires another, the other macro is expanded in new
843 # diversion, GROW.  When the outer macro is fully expanded, we first
844 # undivert the most nested diversions (GROW - 1...), and finally
845 # undivert GROW.  To understand why we need several diversions,
846 # consider the following example:
848 # | m4_defun([TEST1], [Test...REQUIRE([TEST2])1])
849 # | m4_defun([TEST2], [Test...REQUIRE([TEST3])2])
850 # | m4_defun([TEST3], [Test...3])
852 # Because m4_require is not required to be first in the outer macros, we
853 # must keep the expansions of the various level of m4_require separated.
854 # Right before executing the epilogue of TEST1, we have:
856 #          GROW - 2: Test...3
857 #          GROW - 1: Test...2
858 #          GROW:     Test...1
859 #          BODY:
861 # Finally the epilogue of TEST1 undiverts GROW - 2, GROW - 1, and
862 # GROW into the regular flow, BODY.
864 #          GROW - 2:
865 #          GROW - 1:
866 #          GROW:
867 #          BODY:        Test...3; Test...2; Test...1
869 # (The semicolons are here for clarification, but of course are not
870 # emitted.)  This is what Autoconf 2.0 (I think) to 2.13 (I'm sure)
871 # implement.
874 # The second idea: first required first out
875 # -----------------------------------------
877 # The natural implementation of the idea above is buggy and produces
878 # very surprising results in some situations.  Let's consider the
879 # following example to explain the bug:
881 # | m4_defun([TEST1],  [REQUIRE([TEST2a])REQUIRE([TEST2b])])
882 # | m4_defun([TEST2a], [])
883 # | m4_defun([TEST2b], [REQUIRE([TEST3])])
884 # | m4_defun([TEST3],  [REQUIRE([TEST2a])])
885 # |
886 # | AC_INIT
887 # | TEST1
889 # The dependencies between the macros are:
891 #                3 --- 2b
892 #               /        \              is m4_require'd by
893 #              /          \       left -------------------- right
894 #           2a ------------ 1
896 # If you strictly apply the rules given in the previous section you get:
898 #          GROW - 2: TEST3
899 #          GROW - 1: TEST2a; TEST2b
900 #          GROW:     TEST1
901 #          BODY:
903 # (TEST2a, although required by TEST3 is not expanded in GROW - 3
904 # because is has already been expanded before in GROW - 1, so it has
905 # been AC_PROVIDE'd, so it is not expanded again) so when you undivert
906 # the stack of diversions, you get:
908 #          GROW - 2:
909 #          GROW - 1:
910 #          GROW:
911 #          BODY:        TEST3; TEST2a; TEST2b; TEST1
913 # i.e., TEST2a is expanded after TEST3 although the latter required the
914 # former.
916 # Starting from 2.50, uses an implementation provided by Axel Thimm.
917 # The idea is simple: the order in which macros are emitted must be the
918 # same as the one in which macro are expanded.  (The bug above can
919 # indeed be described as: a macro has been AC_PROVIDE'd, but it is
920 # emitted after: the lack of correlation between emission and expansion
921 # order is guilty).
923 # How to do that?  You keeping the stack of diversions to elaborate the
924 # macros, but each time a macro is fully expanded, emit it immediately.
926 # In the example above, when TEST2a is expanded, but it's epilogue is
927 # not run yet, you have:
929 #          GROW - 2:
930 #          GROW - 1: TEST2a
931 #          GROW:     Elaboration of TEST1
932 #          BODY:
934 # The epilogue of TEST2a emits it immediately:
936 #          GROW - 2:
937 #          GROW - 1:
938 #          GROW:     Elaboration of TEST1
939 #          BODY:     TEST2a
941 # TEST2b then requires TEST3, so right before the epilogue of TEST3, you
942 # have:
944 #          GROW - 2: TEST3
945 #          GROW - 1: Elaboration of TEST2b
946 #          GROW:     Elaboration of TEST1
947 #          BODY:      TEST2a
949 # The epilogue of TEST3 emits it:
951 #          GROW - 2:
952 #          GROW - 1: Elaboration of TEST2b
953 #          GROW:     Elaboration of TEST1
954 #          BODY:     TEST2a; TEST3
956 # TEST2b is now completely expanded, and emitted:
958 #          GROW - 2:
959 #          GROW - 1:
960 #          GROW:     Elaboration of TEST1
961 #          BODY:     TEST2a; TEST3; TEST2b
963 # and finally, TEST1 is finished and emitted:
965 #          GROW - 2:
966 #          GROW - 1:
967 #          GROW:
968 #          BODY:     TEST2a; TEST3; TEST2b: TEST1
970 # The idea is simple, but the implementation is a bit evolved.  If you
971 # are like me, you will want to see the actual functioning of this
972 # implementation to be convinced.  The next section gives the full
973 # details.
976 # The Axel Thimm implementation at work
977 # -------------------------------------
979 # We consider the macros above, and this configure.ac:
981 #           AC_INIT
982 #           TEST1
984 # You should keep the definitions of _m4_defun_pro, _m4_defun_epi, and
985 # m4_require at hand to follow the steps.
987 # This implements tries not to assume that the current diversion is
988 # BODY, so as soon as a macro (m4_defun'd) is expanded, we first
989 # record the current diversion under the name _m4_divert_dump (denoted
990 # DUMP below for short).  This introduces an important difference with
991 # the previous versions of Autoconf: you cannot use m4_require if you
992 # are not inside an m4_defun'd macro, and especially, you cannot
993 # m4_require directly from the top level.
995 # We have not tried to simulate the old behavior (better yet, we
996 # diagnose it), because it is too dangerous: a macro m4_require'd from
997 # the top level is expanded before the body of `configure', i.e., before
998 # any other test was run.  I let you imagine the result of requiring
999 # AC_STDC_HEADERS for instance, before AC_PROG_CC was actually run....
1001 # After AC_INIT was run, the current diversion is BODY.
1002 # * AC_INIT was run
1003 #   DUMP:                undefined
1004 #   diversion stack:     BODY |-
1006 # * TEST1 is expanded
1007 # The prologue of TEST1 sets _m4_divert_dump, which is the diversion
1008 # where the current elaboration will be dumped, to the current
1009 # diversion.  It also m4_divert_push to GROW, where the full
1010 # expansion of TEST1 and its dependencies will be elaborated.
1011 #   DUMP:        BODY
1012 #   BODY:        empty
1013 #   diversions:  GROW, BODY |-
1015 # * TEST1 requires TEST2a
1016 # _m4_require_call m4_divert_pushes another temporary diversion,
1017 # GROW - 1, and expands TEST2a in there.
1018 #   DUMP:        BODY
1019 #   BODY:        empty
1020 #   GROW - 1:    TEST2a
1021 #   diversions:  GROW - 1, GROW, BODY |-
1022 # Than the content of the temporary diversion is moved to DUMP and the
1023 # temporary diversion is popped.
1024 #   DUMP:        BODY
1025 #   BODY:        TEST2a
1026 #   diversions:  GROW, BODY |-
1028 # * TEST1 requires TEST2b
1029 # Again, _m4_require_call pushes GROW - 1 and heads to expand TEST2b.
1030 #   DUMP:        BODY
1031 #   BODY:        TEST2a
1032 #   diversions:  GROW - 1, GROW, BODY |-
1034 # * TEST2b requires TEST3
1035 # _m4_require_call pushes GROW - 2 and expands TEST3 here.
1036 # (TEST3 requires TEST2a, but TEST2a has already been m4_provide'd, so
1037 # nothing happens.)
1038 #   DUMP:        BODY
1039 #   BODY:        TEST2a
1040 #   GROW - 2:    TEST3
1041 #   diversions:  GROW - 2, GROW - 1, GROW, BODY |-
1042 # Than the diversion is appended to DUMP, and popped.
1043 #   DUMP:        BODY
1044 #   BODY:        TEST2a; TEST3
1045 #   diversions:  GROW - 1, GROW, BODY |-
1047 # * TEST1 requires TEST2b (contd.)
1048 # The content of TEST2b is expanded...
1049 #   DUMP:        BODY
1050 #   BODY:        TEST2a; TEST3
1051 #   GROW - 1:    TEST2b,
1052 #   diversions:  GROW - 1, GROW, BODY |-
1053 # ... and moved to DUMP.
1054 #   DUMP:        BODY
1055 #   BODY:        TEST2a; TEST3; TEST2b
1056 #   diversions:  GROW, BODY |-
1058 # * TEST1 is expanded: epilogue
1059 # TEST1's own content is in GROW...
1060 #   DUMP:        BODY
1061 #   BODY:        TEST2a; TEST3; TEST2b
1062 #   GROW:        TEST1
1063 #   diversions:  BODY |-
1064 # ... and it's epilogue moves it to DUMP and then undefines DUMP.
1065 #   DUMP:       undefined
1066 #   BODY:       TEST2a; TEST3; TEST2b; TEST1
1067 #   diversions: BODY |-
1070 # 2. Keeping track of the expansion stack
1071 # =======================================
1073 # When M4 expansion goes wrong it is often extremely hard to find the
1074 # path amongst macros that drove to the failure.  What is needed is
1075 # the stack of macro `calls'. One could imagine that GNU M4 would
1076 # maintain a stack of macro expansions, unfortunately it doesn't, so
1077 # we do it by hand.  This is of course extremely costly, but the help
1078 # this stack provides is worth it.  Nevertheless to limit the
1079 # performance penalty this is implemented only for m4_defun'd macros,
1080 # not for define'd macros.
1082 # The scheme is simplistic: each time we enter an m4_defun'd macros,
1083 # we prepend its name in m4_expansion_stack, and when we exit the
1084 # macro, we remove it (thanks to pushdef/popdef).
1086 # In addition, we want to detect circular m4_require dependencies.
1087 # Each time we expand a macro FOO we define _m4_expanding(FOO); and
1088 # m4_require(BAR) simply checks whether _m4_expanding(BAR) is defined.
1091 # m4_expansion_stack_push(TEXT)
1092 # -----------------------------
1093 m4_define([m4_expansion_stack_push],
1094 [m4_pushdef([m4_expansion_stack],
1095             [$1]m4_ifdef([m4_expansion_stack], [
1096 m4_defn([m4_expansion_stack])]))])
1099 # m4_expansion_stack_pop
1100 # ----------------------
1101 m4_define([m4_expansion_stack_pop],
1102 [m4_popdef([m4_expansion_stack])])
1105 # m4_expansion_stack_dump
1106 # -----------------------
1107 # Dump the expansion stack.
1108 m4_define([m4_expansion_stack_dump],
1109 [m4_ifdef([m4_expansion_stack],
1110           [m4_errprintn(m4_defn([m4_expansion_stack]))])dnl
1111 m4_errprintn(m4_location[: the top level])])
1114 # _m4_divert(GROW)
1115 # ----------------
1116 # This diversion is used by the m4_defun/m4_require machinery.  It is
1117 # important to keep room before GROW because for each nested
1118 # AC_REQUIRE we use an additional diversion (i.e., two m4_require's
1119 # will use GROW - 2.  More than 3 levels has never seemed to be
1120 # needed.)
1122 # ...
1123 # - GROW - 2
1124 #   m4_require'd code, 2 level deep
1125 # - GROW - 1
1126 #   m4_require'd code, 1 level deep
1127 # - GROW
1128 #   m4_defun'd macros are elaborated here.
1130 m4_define([_m4_divert(GROW)],       10000)
1133 # _m4_defun_pro(MACRO-NAME)
1134 # -------------------------
1135 # The prologue for Autoconf macros.
1136 m4_define([_m4_defun_pro],
1137 [m4_ifndef([m4_expansion_stack], [_m4_defun_pro_outer[]])dnl
1138 m4_expansion_stack_push(m4_defn([m4_location($1)])[: $1 is expanded from...])dnl
1139 m4_pushdef([_m4_expanding($1)])dnl
1142 m4_define([_m4_defun_pro_outer],
1143 [m4_copy([_m4_divert_diversion], [_m4_divert_dump])dnl
1144 m4_divert_push([GROW])dnl
1147 # _m4_defun_epi(MACRO-NAME)
1148 # -------------------------
1149 # The Epilogue for Autoconf macros.  MACRO-NAME only helps tracing
1150 # the PRO/EPI pairs.
1151 m4_define([_m4_defun_epi],
1152 [m4_popdef([_m4_expanding($1)])dnl
1153 m4_expansion_stack_pop()dnl
1154 m4_ifndef([m4_expansion_stack], [_m4_defun_epi_outer[]])dnl
1155 m4_provide([$1])dnl
1158 m4_define([_m4_defun_epi_outer],
1159 [m4_undefine([_m4_divert_dump])dnl
1160 m4_divert_pop([GROW])dnl
1161 m4_undivert([GROW])dnl
1165 # m4_defun(NAME, EXPANSION)
1166 # -------------------------
1167 # Define a macro which automatically provides itself.  Add machinery
1168 # so the macro automatically switches expansion to the diversion
1169 # stack if it is not already using it.  In this case, once finished,
1170 # it will bring back all the code accumulated in the diversion stack.
1171 # This, combined with m4_require, achieves the topological ordering of
1172 # macros.  We don't use this macro to define some frequently called
1173 # macros that are not involved in ordering constraints, to save m4
1174 # processing.
1175 m4_define([m4_defun],
1176 [m4_define([m4_location($1)], m4_location)dnl
1177 m4_define([$1],
1178           [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])
1181 # m4_defun_once(NAME, EXPANSION)
1182 # ------------------------------
1183 # As m4_defun, but issues the EXPANSION only once, and warns if used
1184 # several times.
1185 m4_define([m4_defun_once],
1186 [m4_define([m4_location($1)], m4_location)dnl
1187 m4_define([$1],
1188           [m4_provide_if([$1],
1189                          [m4_warn([syntax], [$1 invoked multiple times])],
1190                          [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])])
1193 # m4_pattern_forbid(ERE, [WHY])
1194 # -----------------------------
1195 # Declare that no token matching the extended regular expression ERE
1196 # should be seen in the output but if...
1197 m4_define([m4_pattern_forbid], [])
1200 # m4_pattern_allow(ERE)
1201 # ---------------------
1202 # ... but if that token matches the extended regular expression ERE.
1203 # Both used via traces.
1204 m4_define([m4_pattern_allow], [])
1207 ## ----------------------------- ##
1208 ## Dependencies between macros.  ##
1209 ## ----------------------------- ##
1212 # m4_before(THIS-MACRO-NAME, CALLED-MACRO-NAME)
1213 # ---------------------------------------------
1214 m4_define([m4_before],
1215 [m4_provide_if([$2],
1216                [m4_warn([syntax], [$2 was called before $1])])])
1219 # m4_require(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
1220 # -----------------------------------------------------------
1221 # If NAME-TO-CHECK has never been expanded (actually, if it is not
1222 # m4_provide'd), expand BODY-TO-EXPAND *before* the current macro
1223 # expansion.  Once expanded, emit it in _m4_divert_dump.  Keep track
1224 # of the m4_require chain in m4_expansion_stack.
1226 # The normal cases are:
1228 # - NAME-TO-CHECK == BODY-TO-EXPAND
1229 #   Which you can use for regular macros with or without arguments, e.g.,
1230 #     m4_require([AC_PROG_CC], [AC_PROG_CC])
1231 #     m4_require([AC_CHECK_HEADERS(limits.h)], [AC_CHECK_HEADERS(limits.h)])
1232 #   which is just the same as
1233 #     m4_require([AC_PROG_CC])
1234 #     m4_require([AC_CHECK_HEADERS(limits.h)])
1236 # - BODY-TO-EXPAND == m4_indir([NAME-TO-CHECK])
1237 #   In the case of macros with irregular names.  For instance:
1238 #     m4_require([AC_LANG_COMPILER(C)], [indir([AC_LANG_COMPILER(C)])])
1239 #   which means `if the macro named `AC_LANG_COMPILER(C)' (the parens are
1240 #   part of the name, it is not an argument) has not been run, then
1241 #   call it.'
1242 #   Had you used
1243 #     m4_require([AC_LANG_COMPILER(C)], [AC_LANG_COMPILER(C)])
1244 #   then m4_require would have tried to expand `AC_LANG_COMPILER(C)', i.e.,
1245 #   call the macro `AC_LANG_COMPILER' with `C' as argument.
1247 #   You could argue that `AC_LANG_COMPILER', when it receives an argument
1248 #   such as `C' should dispatch the call to `AC_LANG_COMPILER(C)'.  But this
1249 #   `extension' prevents `AC_LANG_COMPILER' from having actual arguments that
1250 #   it passes to `AC_LANG_COMPILER(C)'.
1251 m4_define([m4_require],
1252 [m4_ifdef([_m4_expanding($1)],
1253          [m4_fatal([$0: circular dependency of $1])])dnl
1254 m4_ifndef([_m4_divert_dump],
1255           [m4_fatal([$0($1): cannot be used outside of an ]dnl
1256 m4_bmatch([$0], [^AC_], [[AC_DEFUN]], [[m4_defun]])['d macro])])dnl
1257 m4_provide_if([$1],
1258               [],
1259               [_m4_require_call([$1], [$2])])dnl
1263 # _m4_require_call(BODY-TO-EXPAND)
1264 # --------------------------------
1265 # If m4_require decides to expand the body, it calls this macro.
1266 m4_define([_m4_require_call],
1267 [m4_define([_m4_divert_grow], m4_decr(_m4_divert_grow))dnl
1268 m4_divert_push(_m4_divert_grow)dnl
1269 m4_default([$2], [$1])
1270 m4_provide_if([$1],
1271               [],
1272               [m4_warn([syntax],
1273                        [$1 is m4_require'd but not m4_defun'd])])dnl
1274 m4_divert(m4_defn([_m4_divert_dump]))dnl
1275 m4_undivert(_m4_divert_grow)dnl
1276 m4_divert_pop(_m4_divert_grow)dnl
1277 m4_define([_m4_divert_grow], m4_incr(_m4_divert_grow))dnl
1281 # _m4_divert_grow
1282 # ---------------
1283 # The counter for _m4_require_call.
1284 m4_define([_m4_divert_grow], _m4_divert([GROW]))
1287 # m4_expand_once(TEXT, [WITNESS = TEXT])
1288 # --------------------------------------
1289 # If TEXT has never been expanded, expand it *here*.  Use WITNESS as
1290 # as a memory that TEXT has already been expanded.
1291 m4_define([m4_expand_once],
1292 [m4_provide_if(m4_ifval([$2], [[$2]], [[$1]]),
1293                [],
1294                [m4_provide(m4_ifval([$2], [[$2]], [[$1]]))[]$1])])
1297 # m4_provide(MACRO-NAME)
1298 # ----------------------
1299 m4_define([m4_provide],
1300 [m4_define([m4_provide($1)])])
1303 # m4_provide_if(MACRO-NAME, IF-PROVIDED, IF-NOT-PROVIDED)
1304 # -------------------------------------------------------
1305 # If MACRO-NAME is provided do IF-PROVIDED, else IF-NOT-PROVIDED.
1306 # The purpose of this macro is to provide the user with a means to
1307 # check macros which are provided without letting her know how the
1308 # information is coded.
1309 m4_define([m4_provide_if],
1310 [m4_ifdef([m4_provide($1)],
1311           [$2], [$3])])
1314 ## -------------------- ##
1315 ## 9. Text processing.  ##
1316 ## -------------------- ##
1319 # m4_cr_letters
1320 # m4_cr_LETTERS
1321 # m4_cr_Letters
1322 # -------------
1323 m4_define([m4_cr_letters], [abcdefghijklmnopqrstuvwxyz])
1324 m4_define([m4_cr_LETTERS], [ABCDEFGHIJKLMNOPQRSTUVWXYZ])
1325 m4_define([m4_cr_Letters],
1326 m4_defn([m4_cr_letters])dnl
1327 m4_defn([m4_cr_LETTERS])dnl
1331 # m4_cr_digits
1332 # ------------
1333 m4_define([m4_cr_digits], [0123456789])
1336 # m4_cr_symbols1 & m4_cr_symbols2
1337 # -------------------------------
1338 m4_define([m4_cr_symbols1],
1339 m4_defn([m4_cr_Letters])dnl
1342 m4_define([m4_cr_symbols2],
1343 m4_defn([m4_cr_symbols1])dnl
1344 m4_defn([m4_cr_digits])dnl
1348 # m4_re_escape(STRING)
1349 # --------------------
1350 # Escape RE active characters in STRING.
1351 m4_define([m4_re_escape],
1352 [m4_bpatsubst([$1],
1353               [[][*+.?\^$]], [\\\&])])
1356 # m4_re_string
1357 # ------------
1358 # Regexp for `[a-zA-Z_0-9]*'
1359 # m4_dquote provides literal [] for the character class.
1360 m4_define([m4_re_string],
1361 m4_dquote(m4_defn([m4_cr_symbols2]))dnl
1362 [*]dnl
1366 # m4_re_word
1367 # ----------
1368 # Regexp for `[a-zA-Z_][a-zA-Z_0-9]*'
1369 m4_define([m4_re_word],
1370 m4_dquote(m4_defn([m4_cr_symbols1]))dnl
1371 m4_defn([m4_re_string])dnl
1375 # m4_tolower(STRING)
1376 # m4_toupper(STRING)
1377 # ------------------
1378 # These macros lowercase and uppercase strings.
1379 m4_define([m4_tolower],
1380 [m4_translit([$1], m4_defn([m4_cr_LETTERS]), m4_defn([m4_cr_letters]))])
1381 m4_define([m4_toupper],
1382 [m4_translit([$1], m4_defn([m4_cr_letters]), m4_defn([m4_cr_LETTERS]))])
1385 # m4_split(STRING, [REGEXP])
1386 # --------------------------
1388 # Split STRING into an m4 list of quoted elements.  The elements are
1389 # quoted with [ and ].  Beginning spaces and end spaces *are kept*.
1390 # Use m4_strip to remove them.
1392 # REGEXP specifies where to split.  Default is [\t ]+.
1394 # If STRING is empty, the result is an empty list.
1396 # Pay attention to the m4_changequotes.  When m4 reads the definition of
1397 # m4_split, it still has quotes set to [ and ].  Luckily, these are matched
1398 # in the macro body, so the definition is stored correctly.
1400 # Also, notice that $1 is quoted twice, since we want the result to
1401 # be quoted.  Then you should understand that the argument of
1402 # patsubst is ``STRING'' (i.e., with additional `` and '').
1404 # This macro is safe on active symbols, i.e.:
1405 #   m4_define(active, ACTIVE)
1406 #   m4_split([active active ])end
1407 #   => [active], [active], []end
1409 m4_define([m4_split],
1410 [m4_ifval([$1], [_m4_split($@)])])
1412 m4_define([_m4_split],
1413 [m4_changequote(``, '')dnl
1414 [dnl Can't use m4_default here instead of m4_if, because m4_default uses
1415 dnl [ and ] as quotes.
1416 m4_bpatsubst(````$1'''',
1417              m4_if(``$2'',, ``[  ]+'', ``$2''),
1418              ``], ['')]dnl
1419 m4_changequote([, ])])
1423 # m4_flatten(STRING)
1424 # ------------------
1425 # If STRING contains end of lines, replace them with spaces.  If there
1426 # are backslashed end of lines, remove them.  This macro is safe with
1427 # active symbols.
1428 #    m4_define(active, ACTIVE)
1429 #    m4_flatten([active
1430 #    act\
1431 #    ive])end
1432 #    => active activeend
1433 m4_define([m4_flatten],
1434 [m4_translit(m4_bpatsubst([[[$1]]], [\\
1435 ]), [
1436 ], [ ])])
1439 # m4_strip(STRING)
1440 # ----------------
1441 # Expands into STRING with tabs and spaces singled out into a single
1442 # space, and removing leading and trailing spaces.
1444 # This macro is robust to active symbols.
1445 #    m4_define(active, ACTIVE)
1446 #    m4_strip([  active <tab> <tab>active ])end
1447 #    => active activeend
1449 # Because we want to preserve active symbols, STRING must be double-quoted.
1451 # Then notice the 2 last patterns: they are in charge of removing the
1452 # leading/trailing spaces.  Why not just `[^ ]'?  Because they are
1453 # applied to doubly quoted strings, i.e. more or less [[STRING]].  So
1454 # if there is a leading space in STRING, then it is the *third*
1455 # character, since there are two leading `['; equally for the last pattern.
1456 m4_define([m4_strip],
1457 [m4_bpatsubsts([[$1]],
1458                [[        ]+], [ ],
1459                [^\(..\) ],    [\1],
1460                [ \(..\)$],    [\1])])
1463 # m4_normalize(STRING)
1464 # --------------------
1465 # Apply m4_flatten and m4_strip to STRING.
1467 # The argument is quoted, so that the macro is robust to active symbols:
1469 #    m4_define(active, ACTIVE)
1470 #    m4_normalize([  act\
1471 #    ive
1472 #    active ])end
1473 #    => active activeend
1475 m4_define([m4_normalize],
1476 [m4_strip(m4_flatten([$1]))])
1480 # m4_join(SEP, ARG1, ARG2...)
1481 # ---------------------------
1482 # Produce ARG1SEPARG2...SEPARGn.
1483 m4_defun([m4_join],
1484 [m4_case([$#],
1485          [1], [],
1486          [2], [[$2]],
1487          [[$2][$1]$0([$1], m4_shiftn(2, $@))])])
1491 # m4_append(MACRO-NAME, STRING, [SEPARATOR])
1492 # ------------------------------------------
1493 # Redefine MACRO-NAME to hold its former content plus `SEPARATOR`'STRING'
1494 # at the end.  It is valid to use this macro with MACRO-NAME undefined,
1495 # in which case no SEPARATOR is added.  Be aware that the criterion is
1496 # `not being defined', and not `not being empty'.
1498 # This macro is robust to active symbols.  It can be used to grow
1499 # strings.
1501 #    | m4_define(active, ACTIVE)
1502 #    | m4_append([sentence], [This is an])
1503 #    | m4_append([sentence], [ active ])
1504 #    | m4_append([sentence], [symbol.])
1505 #    | sentence
1506 #    | m4_undefine([active])dnl
1507 #    | sentence
1508 #    => This is an ACTIVE symbol.
1509 #    => This is an active symbol.
1511 # It can be used to define hooks.
1513 #    | m4_define(active, ACTIVE)
1514 #    | m4_append([hooks], [m4_define([act1], [act2])])
1515 #    | m4_append([hooks], [m4_define([act2], [active])])
1516 #    | m4_undefine([active])
1517 #    | act1
1518 #    | hooks
1519 #    | act1
1520 #    => act1
1521 #    =>
1522 #    => active
1523 m4_define([m4_append],
1524 [m4_define([$1],
1525            m4_ifdef([$1], [m4_defn([$1])$3])[$2])])
1528 # m4_append_uniq(MACRO-NAME, STRING, [SEPARATOR])
1529 # -----------------------------------------------
1530 # As `m4_append', but append only if not yet present.
1531 m4_define([m4_append_uniq],
1532 [m4_ifdef([$1],
1533           [m4_bmatch([$3]m4_defn([$1])[$3], m4_re_escape([$3$2$3]), [],
1534                      [m4_append($@)])],
1535           [m4_append($@)])])
1538 # m4_text_wrap(STRING, [PREFIX], [FIRST-PREFIX], [WIDTH])
1539 # -------------------------------------------------------
1540 # Expands into STRING wrapped to hold in WIDTH columns (default = 79).
1541 # If PREFIX is given, each line is prefixed with it.  If FIRST-PREFIX is
1542 # specified, then the first line is prefixed with it.  As a special case,
1543 # if the length of FIRST-PREFIX is greater than that of PREFIX, then
1544 # FIRST-PREFIX will be left alone on the first line.
1546 # Typical outputs are:
1548 # m4_text_wrap([Short string */], [   ], [/* ], 20)
1549 #  => /* Short string */
1551 # m4_text_wrap([Much longer string */], [   ], [/* ], 20)
1552 #  => /* Much longer
1553 #  =>    string */
1555 # m4_text_wrap([Short doc.], [          ], [  --short ], 30)
1556 #  =>   --short Short doc.
1558 # m4_text_wrap([Short doc.], [          ], [  --too-wide ], 30)
1559 #  =>   --too-wide
1560 #  =>           Short doc.
1562 # m4_text_wrap([Super long documentation.], [          ], [  --too-wide ], 30)
1563 #  =>   --too-wide
1564 #  =>      Super long
1565 #  =>      documentation.
1567 # FIXME: there is no checking of a longer PREFIX than WIDTH, but do
1568 # we really want to bother with people trying each single corner
1569 # of a software?
1571 # This macro does not leave a trailing space behind the last word,
1572 # what complicates it a bit.  The algorithm is stupid simple: all the
1573 # words are preceded by m4_Separator which is defined to empty for the
1574 # first word, and then ` ' (single space) for all the others.
1575 m4_define([m4_text_wrap],
1576 [m4_pushdef([m4_Prefix], [$2])dnl
1577 m4_pushdef([m4_Prefix1], m4_default([$3], [m4_Prefix]))dnl
1578 m4_pushdef([m4_Width], m4_default([$4], 79))dnl
1579 m4_pushdef([m4_Cursor], m4_qlen(m4_Prefix1))dnl
1580 m4_pushdef([m4_Separator], [])dnl
1581 m4_Prefix1[]dnl
1582 m4_if(m4_eval(m4_qlen(m4_Prefix1) > m4_len(m4_Prefix)),
1583       1, [m4_define([m4_Cursor], m4_len(m4_Prefix))
1584 m4_Prefix],
1585       m4_if(m4_eval(m4_qlen(m4_Prefix1) < m4_len(m4_Prefix)),
1586             [0], [],
1587             [m4_define([m4_Cursor], m4_len(m4_Prefix))[]dnl
1588 m4_for(m4_Space, m4_qlen(m4_Prefix1), m4_eval(m4_len(m4_Prefix) - 1),
1589                     [], [ ])])[]dnl
1590 )[]dnl
1591 m4_foreach_w([m4_Word], [$1],
1592 [m4_define([m4_Cursor], m4_eval(m4_Cursor + m4_qlen(m4_defn([m4_Word])) + 1))dnl
1593 dnl New line if too long, else insert a space unless it is the first
1594 dnl of the words.
1595 m4_if(m4_eval(m4_Cursor > m4_Width),
1596       1, [m4_define([m4_Cursor],
1597                     m4_eval(m4_len(m4_Prefix) + m4_qlen(m4_defn([m4_Word])) + 1))]
1598 m4_Prefix,
1599        [m4_Separator])[]dnl
1600 m4_defn([m4_Word])[]dnl
1601 m4_define([m4_Separator], [ ])])dnl
1602 m4_popdef([m4_Separator])dnl
1603 m4_popdef([m4_Cursor])dnl
1604 m4_popdef([m4_Width])dnl
1605 m4_popdef([m4_Prefix1])dnl
1606 m4_popdef([m4_Prefix])dnl
1610 # m4_text_box(MESSAGE, [FRAME-CHARACTER = `-'])
1611 # ---------------------------------------------
1612 m4_define([m4_text_box],
1613 [@%:@@%:@ m4_bpatsubst([$1], [.], m4_if([$2], [], [[-]], [[$2]])) @%:@@%:@
1614 @%:@@%:@ $1 @%:@@%:@
1615 @%:@@%:@ m4_bpatsubst([$1], [.], m4_if([$2], [], [[-]], [[$2]])) @%:@@%:@[]dnl
1619 # m4_qlen(STRING)
1620 # ---------------
1621 # Expands to the length of STRING after autom4te converts all quadrigraphs.
1622 m4_define([m4_qlen],
1623 [m4_len(m4_bpatsubsts([[$1]], [@\(<:\|:>\|S|\|%:\)@], [P], [@&t@]))])
1626 # m4_qdelta(STRING)
1627 # -----------------
1628 # Expands to the net change in the length of STRING from autom4te converting the
1629 # quadrigraphs in STRING.  This number is always negative or zero.
1630 m4_define([m4_qdelta],
1631 [m4_eval(m4_qlen([$1]) - m4_len([$1]))])
1635 ## ----------------------- ##
1636 ## 10. Number processing.  ##
1637 ## ----------------------- ##
1639 # m4_sign(A)
1640 # ----------
1642 # The sign of the integer A.
1643 m4_define([m4_sign],
1644 [m4_bmatch([$1],
1645            [^-], -1,
1646            [^0+], 0,
1647                   1)])
1649 # m4_cmp(A, B)
1650 # ------------
1652 # Compare two integers.
1653 # A < B -> -1
1654 # A = B ->  0
1655 # A > B ->  1
1656 m4_define([m4_cmp],
1657 [m4_sign(m4_eval([$1 - $2]))])
1660 # m4_list_cmp(A, B)
1661 # -----------------
1663 # Compare the two lists of integers A and B.  For instance:
1664 #   m4_list_cmp((1, 0),     (1))    ->  0
1665 #   m4_list_cmp((1, 0),     (1, 0)) ->  0
1666 #   m4_list_cmp((1, 2),     (1, 0)) ->  1
1667 #   m4_list_cmp((1, 2, 3),  (1, 2)) ->  1
1668 #   m4_list_cmp((1, 2, -3), (1, 2)) -> -1
1669 #   m4_list_cmp((1, 0),     (1, 2)) -> -1
1670 #   m4_list_cmp((1),        (1, 2)) -> -1
1671 m4_define([m4_list_cmp],
1672 [m4_if([$1$2], [()()], 0,
1673        [$1], [()], [$0((0), [$2])],
1674        [$2], [()], [$0([$1], (0))],
1675        [m4_case(m4_cmp(m4_car$1, m4_car$2),
1676                 -1, -1,
1677                  1, 1,
1678                  0, [$0((m4_shift$1), (m4_shift$2))])])])
1682 ## ------------------------ ##
1683 ## 11. Version processing.  ##
1684 ## ------------------------ ##
1687 # m4_version_unletter(VERSION)
1688 # ----------------------------
1689 # Normalize beta version numbers with letters to numbers only for comparison.
1691 #   Nl -> (N+1).-1.(l#)
1693 #i.e., 2.14a -> 2.15.-1.1, 2.14b -> 2.15.-1.2, etc.
1694 # This macro is absolutely not robust to active macro, it expects
1695 # reasonable version numbers and is valid up to `z', no double letters.
1696 m4_define([m4_version_unletter],
1697 [m4_translit(m4_bpatsubsts([$1],
1698                            [\([0-9]+\)\([abcdefghi]\)],
1699                              [m4_eval(\1 + 1).-1.\2],
1700                            [\([0-9]+\)\([jklmnopqrs]\)],
1701                              [m4_eval(\1 + 1).-1.1\2],
1702                            [\([0-9]+\)\([tuvwxyz]\)],
1703                              [m4_eval(\1 + 1).-1.2\2]),
1704              [abcdefghijklmnopqrstuvwxyz],
1705              [12345678901234567890123456])])
1708 # m4_version_compare(VERSION-1, VERSION-2)
1709 # ----------------------------------------
1710 # Compare the two version numbers and expand into
1711 #  -1 if VERSION-1 < VERSION-2
1712 #   0 if           =
1713 #   1 if           >
1714 m4_define([m4_version_compare],
1715 [m4_list_cmp((m4_split(m4_version_unletter([$1]), [\.])),
1716              (m4_split(m4_version_unletter([$2]), [\.])))])
1719 # m4_PACKAGE_NAME
1720 # m4_PACKAGE_TARNAME
1721 # m4_PACKAGE_VERSION
1722 # m4_PACKAGE_STRING
1723 # m4_PACKAGE_BUGREPORT
1724 # --------------------
1725 m4_include([m4sugar/version.m4])
1728 # m4_version_prereq(VERSION, [IF-OK], [IF-NOT = FAIL])
1729 # ----------------------------------------------------
1730 # Check this Autoconf version against VERSION.
1731 m4_define([m4_version_prereq],
1732 [m4_if(m4_version_compare(m4_defn([m4_PACKAGE_VERSION]), [$1]), -1,
1733        [m4_default([$3],
1734                    [m4_fatal([Autoconf version $1 or higher is required],
1735                              63)])],
1736        [$2])[]dnl
1741 ## ------------------- ##
1742 ## 12. File handling.  ##
1743 ## ------------------- ##
1746 # It is a real pity that M4 comes with no macros to bind a diversion
1747 # to a file.  So we have to deal without, which makes us a lot more
1748 # fragile that we should.
1751 # m4_file_append(FILE-NAME, CONTENT)
1752 # ----------------------------------
1753 m4_define([m4_file_append],
1754 [m4_syscmd([cat >>$1 <<_m4eof
1756 _m4eof
1758 m4_if(m4_sysval, [0], [],
1759       [m4_fatal([$0: cannot write: $1])])])
1763 ## ------------------------ ##
1764 ## 13. Setting M4sugar up.  ##
1765 ## ------------------------ ##
1768 # m4_init
1769 # -------
1770 m4_define([m4_init],
1771 [# All the M4sugar macros start with `m4_', except `dnl' kept as is
1772 # for sake of simplicity.
1773 m4_pattern_forbid([^_?m4_])
1774 m4_pattern_forbid([^dnl$])
1776 # _m4_divert_diversion should be defined:
1777 m4_divert_push([KILL])
1779 # Check the divert push/pop perfect balance.
1780 m4_wrap([m4_divert_pop([])
1781          m4_ifdef([_m4_divert_diversion],
1782            [m4_fatal([$0: unbalanced m4_divert_push:]_m4_divert_n_stack)])[]])