]> git.saurik.com Git - bison.git/blame - data/m4sugar/m4sugar.m4
(NUMBER_UNDEFINED): Remove unnecessary cast.
[bison.git] / data / m4sugar / m4sugar.m4
Content-type: text/html ]> git.saurik.com Git - bison.git/blame - data/m4sugar/m4sugar.m4


500 - Internal Server Error

Malformed UTF-8 character (fatal) at /usr/lib/x86_64-linux-gnu/perl5/5.40/HTML/Entities.pm line 485, <$fd> line 1737.
CommitLineData
ea6cfe9e
AD
1divert(-1)# -*- Autoconf -*-
2# This file is part of Autoconf.
3# Base M4 layer.
4# Requires GNU M4.
0d8bed56 5# Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
ea6cfe9e
AD
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2, or (at your option)
10# any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20# 02111-1307, USA.
21#
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
61set the M4 environment variable to its path name.)
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])
86# 2. m4_define([from], [to($@)])
87# Well, obviously 1 is more expansive in space. Maybe 2 is more expansive
88# in time, but because of the space cost of 1, it's not that obvious.
89# Nevertheless, one huge difference is the handling of `$0'. If `from'
90# uses `$0', then with 1, `to''s `$0' is `to', while it is `from' in 2.
91# The user will certainly prefer see `from'.
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])
134m4_rename_m4([include])
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])
147m4_rename_m4([sinclude])
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
202## ------------- ##
203## 3. Warnings. ##
204## ------------- ##
205
206
207# m4_warning_ifelse(CATEGORY, IF-TRUE, IF-FALSE)
208# ----------------------------------------------
209# If the CATEGORY of warnings is enabled, expand IF_TRUE otherwise
210# IF-FALSE.
211#
212# The variable `m4_warnings' contains a comma separated list of
213# warnings which order is the converse from the one specified by
214# the user, i.e., if she specified `-W error,none,obsolete',
215# `m4_warnings' is `obsolete,none,error'. We read it from left to
216# right, and:
217# - if none or noCATEGORY is met, run IF-FALSE
218# - if all or CATEGORY is met, run IF-TRUE
219# - if there is nothing left, run IF-FALSE.
220m4_define([m4_warning_ifelse],
221[_m4_warning_ifelse([$1], [$2], [$3], m4_warnings)])
222
223
224# _m4_warning_ifelse(CATEGORY, IF-TRUE, IF-FALSE, WARNING1, ...)
225# --------------------------------------------------------------
226# Implementation of the loop described above.
227m4_define([_m4_warning_ifelse],
228[m4_case([$4],
229 [$1], [$2],
230 [all], [$2],
231 [], [$3],
232 [none], [$3],
233 [no-$1], [$3],
234 [$0([$1], [$2], [$3], m4_shiftn(4, $@))])])
235
236
237# _m4_warning_error_ifelse(IF-TRUE, IF-FALSE)
238# -------------------------------------------
239# The same as m4_warning_ifelse, but scan for `error' only.
240m4_define([_m4_warning_error_ifelse],
241[__m4_warning_error_ifelse([$1], [$2], m4_warnings)])
242
243
244# __m4_warning_error_ifelse(IF-TRUE, IF-FALSE)
245# --------------------------------------------
246# The same as _m4_warning_ifelse, but scan for `error' only.
247m4_define([__m4_warning_error_ifelse],
248[m4_case([$3],
249 [error], [$1],
250 [], [$2],
251 [no-error], [$2],
252 [$0([$1], [$2], m4_shiftn(3, $@))])])
253
254
255
256# _m4_warn(MESSAGE)
257# -----------------
258# Report MESSAGE as a warning, unless the user requested -W error,
259# in which case report a fatal error.
260m4_define([_m4_warn],
261[_m4_warning_error_ifelse([m4_fatal([$1])],
262 [m4_warning([$1])])])
263
264
265# m4_warn(CATEGORY, MESSAGE)
266# --------------------------
267# Report a MESSAGE to the autoconf user if the CATEGORY of warnings
268# is requested (in fact, not disabled).
269m4_define([m4_warn],
270[m4_warning_ifelse([$1], [_m4_warn([$2])])])
271
272
273
274
275## ------------------- ##
276## 4. File inclusion. ##
277## ------------------- ##
278
279
280# We also want to neutralize include (and sinclude for symmetry),
281# but we want to extend them slightly: warn when a file is included
282# several times. This is in general a dangerous operation because
283# quite nobody quotes the first argument of m4_define.
284#
285# For instance in the following case:
286# m4_define(foo, [bar])
287# then a second reading will turn into
288# m4_define(bar, [bar])
289# which is certainly not what was meant.
290
291# m4_include_unique(FILE)
292# -----------------------
293# Declare that the FILE was loading; and warn if it has already
294# been included.
295m4_define([m4_include_unique],
296[m4_ifdef([m4_include($1)],
297 [m4_warn([syntax], [file `$1' included several times])])dnl
298m4_define([m4_include($1)])])
299
300
301# m4_include(FILE)
302# ----------------
303# As the builtin include, but warns against multiple inclusions.
304m4_define([m4_include],
305[m4_include_unique([$1])dnl
306m4_builtin([include], [$1])])
307
308
309# m4_sinclude(FILE)
310# -----------------
311# As the builtin sinclude, but warns against multiple inclusions.
312m4_define([m4_sinclude],
313[m4_include_unique([$1])dnl
314m4_builtin([sinclude], [$1])])
315
316
317
318## ------------------------------------ ##
319## 5. Additional branching constructs. ##
320## ------------------------------------ ##
321
322# Both `m4_ifval' and `m4_ifset' tests against the empty string. The
323# difference is that `m4_ifset' is specialized on macros.
324#
325# In case of arguments of macros, eg $[1], it makes little difference.
326# In the case of a macro `FOO', you don't want to check `m4_ifval(FOO,
327# TRUE)', because if `FOO' expands with commas, there is a shifting of
328# the arguments. So you want to run `m4_ifval([FOO])', but then you just
329# compare the *string* `FOO' against `', which, of course fails.
330#
331# So you want a variation of `m4_ifset' that expects a macro name as $[1].
332# If this macro is both defined and defined to a non empty value, then
333# it runs TRUE etc.
334
335
336# m4_ifval(COND, [IF-TRUE], [IF-FALSE])
337# -------------------------------------
338# If COND is not the empty string, expand IF-TRUE, otherwise IF-FALSE.
339# Comparable to m4_ifdef.
340m4_define([m4_ifval],
341[m4_if([$1], [], [$3], [$2])])
342
343
344# m4_n(TEXT)
345# ----------
346# If TEXT is not empty, return TEXT and a new line, otherwise nothing.
347m4_define([m4_n],
348[m4_if([$1],
349 [], [],
350 [$1
351])])
352
353
354# m4_ifvaln(COND, [IF-TRUE], [IF-FALSE])
355# --------------------------------------
356# Same as `m4_ifval', but add an extra newline to IF-TRUE or IF-FALSE
357# unless that argument is empty.
358m4_define([m4_ifvaln],
359[m4_if([$1],
360 [], [m4_n([$3])],
361 [m4_n([$2])])])
362
363
364# m4_ifset(MACRO, [IF-TRUE], [IF-FALSE])
365# --------------------------------------
366# If MACRO has no definition, or of its definition is the empty string,
367# expand IF-FALSE, otherwise IF-TRUE.
368m4_define([m4_ifset],
369[m4_ifdef([$1],
370 [m4_if(m4_defn([$1]), [], [$3], [$2])],
371 [$3])])
372
373
374# m4_ifndef(NAME, [IF-NOT-DEFINED], [IF-DEFINED])
375# -----------------------------------------------
376m4_define([m4_ifndef],
377[m4_ifdef([$1], [$3], [$2])])
378
379
380# m4_case(SWITCH, VAL1, IF-VAL1, VAL2, IF-VAL2, ..., DEFAULT)
381# -----------------------------------------------------------
382# m4 equivalent of
383# switch (SWITCH)
384# {
385# case VAL1:
386# IF-VAL1;
387# break;
388# case VAL2:
389# IF-VAL2;
390# break;
391# ...
392# default:
393# DEFAULT;
394# break;
395# }.
396# All the values are optional, and the macro is robust to active
397# symbols properly quoted.
398m4_define([m4_case],
399[m4_if([$#], 0, [],
400 [$#], 1, [],
401 [$#], 2, [$2],
402 [$1], [$2], [$3],
403 [$0([$1], m4_shiftn(3, $@))])])
404
405
406# m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT)
407# -----------------------------------------------------
408# m4 equivalent of
409#
410# if (SWITCH =~ RE1)
411# VAL1;
412# elif (SWITCH =~ RE2)
413# VAL2;
414# elif ...
415# ...
416# else
417# DEFAULT
418#
419# All the values are optional, and the macro is robust to active symbols
420# properly quoted.
421m4_define([m4_bmatch],
422[m4_if([$#], 0, [],
423 [$#], 1, [],
424 [$#], 2, [$2],
1f418995
AD
425 [m4_if(m4_bregexp([$1], [$2]), -1, [$0([$1], m4_shiftn(3, $@))],
426 [$3])])])
ea6cfe9e
AD
427
428
0d8bed56
AD
429# m4_map(MACRO, LIST)
430# -------------------
431# Invoke MACRO($1), MACRO($2) etc. where $1, $2... are the elements
432# of LIST (which can be lists themselves, for multiple arguments MACROs).
433m4_define([m4_fst], [$1])
434m4_define([m4_map],
9280d3ef
AD
435[m4_if([$2], [], [],
436 [$2], [[]], [],
0d8bed56
AD
437 [$1(m4_fst($2))[]dnl
438m4_map([$1], m4_cdr($2))])])
439
ea6cfe9e 440
1f418995
AD
441# m4_map_sep(MACRO, SEPARATOR, LIST)
442# ----------------------------------
443# Invoke MACRO($1), SEPARATOR, MACRO($2), ..., MACRO($N) where $1, $2... $N
444# are the elements of LIST (which can be lists themselves, for multiple
445# arguments MACROs).
446m4_define([m4_map_sep],
447[m4_if([$3], [[]], [],
448 [$1(m4_fst($3))[]dnl
449m4_if(m4_cdr($3),
450 [[]], [],
451 [$2])[]dnl
452m4_map_sep([$1], [$2], m4_cdr($3))])])
453
454
ea6cfe9e
AD
455## ---------------------------------------- ##
456## 6. Enhanced version of some primitives. ##
457## ---------------------------------------- ##
458
459# m4_patsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...)
460# ---------------------------------------------------
461# m4 equivalent of
462#
463# $_ = STRING;
464# s/RE1/SUBST1/g;
465# s/RE2/SUBST2/g;
466# ...
467#
468# All the values are optional, and the macro is robust to active symbols
469# properly quoted.
470#
471# I would have liked to name this macro `m4_patsubst', unfortunately,
472# due to quotation problems, I need to double quote $1 below, therefore
473# the anchors are broken :( I can't let users be trapped by that.
474m4_define([m4_bpatsubsts],
475[m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
476 [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
477 [$#], 2, [m4_builtin([patsubst], $@)],
478 [$0(m4_builtin([patsubst], [[$1]], [$2], [$3]),
479 m4_shiftn(3, $@))])])
480
481
482
483# m4_do(STRING, ...)
484# ------------------
485# This macro invokes all its arguments (in sequence, of course). It is
486# useful for making your macros more structured and readable by dropping
1f418995 487# unnecessary dnl's and have the macros indented properly.
ea6cfe9e
AD
488m4_define([m4_do],
489[m4_if($#, 0, [],
490 $#, 1, [$1],
491 [$1[]m4_do(m4_shift($@))])])
492
493
0d8bed56
AD
494# m4_define_default(MACRO, VALUE)
495# -------------------------------
496# If MACRO is undefined, set it to VALUE.
497m4_define([m4_define_default],
498[m4_ifndef([$1], [m4_define($@)])])
499
500
ea6cfe9e
AD
501# m4_default(EXP1, EXP2)
502# ----------------------
503# Returns EXP1 if non empty, otherwise EXP2.
504m4_define([m4_default],
505[m4_ifval([$1], [$1], [$2])])
506
507
508# m4_defn(NAME)
509# -------------
510# Unlike to the original, don't tolerate popping something which is
511# undefined.
512m4_define([m4_defn],
513[m4_ifndef([$1],
514 [m4_fatal([$0: undefined macro: $1])])dnl
515m4_builtin([defn], $@)])
516
517
518# _m4_dumpdefs_up(NAME)
519# ---------------------
520m4_define([_m4_dumpdefs_up],
521[m4_ifdef([$1],
522 [m4_pushdef([_m4_dumpdefs], m4_defn([$1]))dnl
523m4_dumpdef([$1])dnl
524m4_popdef([$1])dnl
525_m4_dumpdefs_up([$1])])])
526
527
528# _m4_dumpdefs_down(NAME)
529# -----------------------
530m4_define([_m4_dumpdefs_down],
531[m4_ifdef([_m4_dumpdefs],
532 [m4_pushdef([$1], m4_defn([_m4_dumpdefs]))dnl
533m4_popdef([_m4_dumpdefs])dnl
534_m4_dumpdefs_down([$1])])])
535
536
537# m4_dumpdefs(NAME)
538# -----------------
539# Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its
540# value stack (most recent displayed first).
541m4_define([m4_dumpdefs],
542[_m4_dumpdefs_up([$1])dnl
543_m4_dumpdefs_down([$1])])
544
545
546# m4_popdef(NAME)
547# ---------------
548# Unlike to the original, don't tolerate popping something which is
549# undefined.
550m4_define([m4_popdef],
551[m4_ifndef([$1],
552 [m4_fatal([$0: undefined macro: $1])])dnl
553m4_builtin([popdef], $@)])
554
555
0d8bed56
AD
556# m4_quote(ARGS)
557# --------------
558# Return ARGS as a single arguments.
ea6cfe9e
AD
559#
560# It is important to realize the difference between `m4_quote(exp)' and
561# `[exp]': in the first case you obtain the quoted *result* of the
562# expansion of EXP, while in the latter you just obtain the string
563# `exp'.
0d8bed56
AD
564m4_define([m4_quote], [[$*]])
565m4_define([m4_dquote], [[$@]])
ea6cfe9e
AD
566
567
568# m4_noquote(STRING)
569# ------------------
570# Return the result of ignoring all quotes in STRING and invoking the
571# macros it contains. Amongst other things useful for enabling macro
572# invocations inside strings with [] blocks (for instance regexps and
573# help-strings).
574m4_define([m4_noquote],
575[m4_changequote(-=<{,}>=-)$1-=<{}>=-m4_changequote([,])])
576
577
578# m4_shiftn(N, ...)
579# -----------------
580# Returns ... shifted N times. Useful for recursive "varargs" constructs.
581m4_define([m4_shiftn],
582[m4_assert(($1 >= 0) && ($# > $1))dnl
583_m4_shiftn($@)])
584
585m4_define([_m4_shiftn],
586[m4_if([$1], 0,
587 [m4_shift($@)],
588 [_m4_shiftn(m4_eval([$1]-1), m4_shift(m4_shift($@)))])])
589
590
591# m4_undefine(NAME)
592# -----------------
593# Unlike to the original, don't tolerate undefining something which is
594# undefined.
595m4_define([m4_undefine],
596[m4_ifndef([$1],
597 [m4_fatal([$0: undefined macro: $1])])dnl
598m4_builtin([undefine], $@)])
599
600
601## -------------------------- ##
602## 7. Implementing m4 loops. ##
603## -------------------------- ##
604
605
606# m4_for(VARIABLE, FIRST, LAST, [STEP = +/-1], EXPRESSION)
607# --------------------------------------------------------
608# Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO.
609# Both limits are included, and bounds are checked for consistency.
610m4_define([m4_for],
611[m4_case(m4_sign(m4_eval($3 - $2)),
612 1, [m4_assert(m4_sign(m4_default($4, 1)) == 1)],
613 -1, [m4_assert(m4_sign(m4_default($4, -1)) == -1)])dnl
614m4_pushdef([$1], [$2])dnl
615m4_if(m4_eval([$3 > $2]), 1,
616 [_m4_for([$1], [$3], m4_default([$4], 1), [$5])],
617 [_m4_for([$1], [$3], m4_default([$4], -1), [$5])])dnl
618m4_popdef([$1])])
619
620
621# _m4_for(VARIABLE, FIRST, LAST, STEP, EXPRESSION)
622# ------------------------------------------------
623# Core of the loop, no consistency checks.
624m4_define([_m4_for],
625[$4[]dnl
626m4_if($1, [$2], [],
627 [m4_define([$1], m4_eval($1+[$3]))_m4_for([$1], [$2], [$3], [$4])])])
628
629
630# Implementing `foreach' loops in m4 is much more tricky than it may
631# seem. Actually, the example of a `foreach' loop in the m4
632# documentation is wrong: it does not quote the arguments properly,
1f418995 633# which leads to undesirable expansions.
ea6cfe9e
AD
634#
635# The example in the documentation is:
636#
637# | # foreach(VAR, (LIST), STMT)
638# | m4_define([foreach],
639# | [m4_pushdef([$1])_foreach([$1], [$2], [$3])m4_popdef([$1])])
640# | m4_define([_arg1], [$1])
641# | m4_define([_foreach],
642# | [m4_if([$2], [()], ,
643# | [m4_define([$1], _arg1$2)$3[]_foreach([$1],
644# | (shift$2),
645# | [$3])])])
646#
647# But then if you run
648#
649# | m4_define(a, 1)
650# | m4_define(b, 2)
651# | m4_define(c, 3)
652# | foreach([f], [([a], [(b], [c)])], [echo f
653# | ])
654#
655# it gives
656#
657# => echo 1
658# => echo (2,3)
659#
660# which is not what is expected.
661#
662# Of course the problem is that many quotes are missing. So you add
663# plenty of quotes at random places, until you reach the expected
664# result. Alternatively, if you are a quoting wizard, you directly
665# reach the following implementation (but if you really did, then
666# apply to the maintenance of m4sugar!).
667#
668# | # foreach(VAR, (LIST), STMT)
669# | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
670# | m4_define([_arg1], [[$1]])
671# | m4_define([_foreach],
672# | [m4_if($2, [()], ,
673# | [m4_define([$1], [_arg1$2])$3[]_foreach([$1],
674# | [(shift$2)],
675# | [$3])])])
676#
677# which this time answers
678#
679# => echo a
680# => echo (b
681# => echo c)
682#
683# Bingo!
684#
685# Well, not quite.
686#
687# With a better look, you realize that the parens are more a pain than
688# a help: since anyway you need to quote properly the list, you end up
689# with always using an outermost pair of parens and an outermost pair
690# of quotes. Rejecting the parens both eases the implementation, and
691# simplifies the use:
692#
693# | # foreach(VAR, (LIST), STMT)
694# | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
695# | m4_define([_arg1], [$1])
696# | m4_define([_foreach],
697# | [m4_if($2, [], ,
698# | [m4_define([$1], [_arg1($2)])$3[]_foreach([$1],
699# | [shift($2)],
700# | [$3])])])
701#
702#
703# Now, just replace the `$2' with `m4_quote($2)' in the outer `m4_if'
704# to improve robustness, and you come up with a quite satisfactory
705# implementation.
706
707
708# m4_foreach(VARIABLE, LIST, EXPRESSION)
709# --------------------------------------
710#
711# Expand EXPRESSION assigning each value of the LIST to VARIABLE.
712# LIST should have the form `item_1, item_2, ..., item_n', i.e. the
713# whole list must *quoted*. Quote members too if you don't want them
714# to be expanded.
715#
716# This macro is robust to active symbols:
717# | m4_define(active, [ACT, IVE])
718# | m4_foreach(Var, [active, active], [-Var-])
719# => -ACT--IVE--ACT--IVE-
720#
721# | m4_foreach(Var, [[active], [active]], [-Var-])
722# => -ACT, IVE--ACT, IVE-
723#
724# | m4_foreach(Var, [[[active]], [[active]]], [-Var-])
725# => -active--active-
726m4_define([m4_foreach],
727[m4_pushdef([$1])_m4_foreach($@)m4_popdef([$1])])
728
729# Low level macros used to define m4_foreach.
730m4_define([m4_car], [[$1]])
0d8bed56 731m4_define([m4_cdr], [m4_dquote(m4_shift($@))])
ea6cfe9e 732m4_define([_m4_foreach],
0d8bed56
AD
733[m4_if([$2], [[]], [],
734 [m4_define([$1], m4_car($2))$3[]_m4_foreach([$1],
735 m4_cdr($2),
736 [$3])])])
ea6cfe9e
AD
737
738
739
740## --------------------------- ##
741## 8. More diversion support. ##
742## --------------------------- ##
743
744
745# _m4_divert(DIVERSION-NAME or NUMBER)
746# ------------------------------------
747# If DIVERSION-NAME is the name of a diversion, return its number,
748# otherwise if is a NUMBER return it.
749m4_define([_m4_divert],
750[m4_ifdef([_m4_divert($1)],
751 [m4_indir([_m4_divert($1)])],
752 [$1])])
753
754# KILL is only used to suppress output.
755m4_define([_m4_divert(KILL)], -1)
756
757
758# m4_divert(DIVERSION-NAME)
759# -------------------------
760# Change the diversion stream to DIVERSION-NAME.
761m4_define([m4_divert],
762[m4_define([m4_divert_stack],
763 m4_location[: $0: $1]m4_ifdef([m4_divert_stack], [
764m4_defn([m4_divert_stack])]))dnl
765m4_builtin([divert], _m4_divert([$1]))dnl
766])
767
768
769# m4_divert_push(DIVERSION-NAME)
770# ------------------------------
771# Change the diversion stream to DIVERSION-NAME, while stacking old values.
772m4_define([m4_divert_push],
773[m4_pushdef([m4_divert_stack],
774 m4_location[: $0: $1]m4_ifdef([m4_divert_stack], [
775m4_defn([m4_divert_stack])]))dnl
776m4_pushdef([_m4_divert_diversion], [$1])dnl
777m4_builtin([divert], _m4_divert(_m4_divert_diversion))dnl
778])
779
780
781# m4_divert_pop([DIVERSION-NAME])
782# -------------------------------
783# Change the diversion stream to its previous value, unstacking it.
784# If specified, verify we left DIVERSION-NAME.
785m4_define([m4_divert_pop],
786[m4_ifval([$1],
787 [m4_if(_m4_divert([$1]), m4_divnum, [],
788 [m4_fatal([$0($1): diversion mismatch: ]
789m4_defn([m4_divert_stack]))])])dnl
790m4_popdef([_m4_divert_diversion])dnl
791dnl m4_ifndef([_m4_divert_diversion],
792dnl [m4_fatal([too many m4_divert_pop])])dnl
793m4_builtin([divert],
794 m4_ifdef([_m4_divert_diversion],
795 [_m4_divert(_m4_divert_diversion)], -1))dnl
796m4_popdef([m4_divert_stack])dnl
797])
798
799
800# m4_divert_text(DIVERSION-NAME, CONTENT)
801# ---------------------------------------
802# Output CONTENT into DIVERSION-NAME (which may be a number actually).
803# An end of line is appended for free to CONTENT.
804m4_define([m4_divert_text],
805[m4_divert_push([$1])dnl
806$2
807m4_divert_pop([$1])dnl
808])
809
810
811# m4_divert_once(DIVERSION-NAME, CONTENT)
812# ---------------------------------------
813# Output once CONTENT into DIVERSION-NAME (which may be a number
814# actually). An end of line is appended for free to CONTENT.
815m4_define([m4_divert_once],
816[m4_expand_once([m4_divert_text([$1], [$2])])])
817
818
819# m4_undivert(DIVERSION-NAME)
820# ---------------------------
821# Undivert DIVERSION-NAME.
822m4_define([m4_undivert],
823[m4_builtin([undivert], _m4_divert([$1]))])
824
825
826## -------------------------------------------- ##
827## 8. Defining macros with bells and whistles. ##
828## -------------------------------------------- ##
829
830# `m4_defun' is basically `m4_define' but it equips the macro with the
831# needed machinery for `m4_require'. A macro must be m4_defun'd if
832# either it is m4_require'd, or it m4_require's.
833#
834# Two things deserve attention and are detailed below:
835# 1. Implementation of m4_require
836# 2. Keeping track of the expansion stack
837#
838# 1. Implementation of m4_require
839# ===============================
840#
841# Of course m4_defun AC_PROVIDE's the macro, so that a macro which has
842# been expanded is not expanded again when m4_require'd, but the
843# difficult part is the proper expansion of macros when they are
844# m4_require'd.
845#
846# The implementation is based on two ideas, (i) using diversions to
847