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