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