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