]> git.saurik.com Git - bison.git/blame - data/m4sugar/m4sugar.m4
* doc/bison.texinfo (Actions): Make clear that `|' is not the same
[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 1685.
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],
425 m4_bregexp([$1], [$2]), -1, [$0([$1], m4_shiftn(3, $@))],
426 [$3])])
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],
435[m4_if([$2], [[]], [],
436 [$1(m4_fst($2))[]dnl
437m4_map([$1], m4_cdr($2))])])
438
ea6cfe9e
AD
439
440## ---------------------------------------- ##
441## 6. Enhanced version of some primitives. ##
442## ---------------------------------------- ##
443
444# m4_patsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...)
445# ---------------------------------------------------
446# m4 equivalent of
447#
448# $_ = STRING;
449# s/RE1/SUBST1/g;
450# s/RE2/SUBST2/g;
451# ...
452#
453# All the values are optional, and the macro is robust to active symbols
454# properly quoted.
455#
456# I would have liked to name this macro `m4_patsubst', unfortunately,
457# due to quotation problems, I need to double quote $1 below, therefore
458# the anchors are broken :( I can't let users be trapped by that.
459m4_define([m4_bpatsubsts],
460[m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
461 [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
462 [$#], 2, [m4_builtin([patsubst], $@)],
463 [$0(m4_builtin([patsubst], [[$1]], [$2], [$3]),
464 m4_shiftn(3, $@))])])
465
466
467
468# m4_do(STRING, ...)
469# ------------------
470# This macro invokes all its arguments (in sequence, of course). It is
471# useful for making your macros more structured and readable by dropping
472# unecessary dnl's and have the macros indented properly.
473m4_define([m4_do],
474[m4_if($#, 0, [],
475 $#, 1, [$1],
476 [$1[]m4_do(m4_shift($@))])])
477
478
0d8bed56
AD
479# m4_define_default(MACRO, VALUE)
480# -------------------------------
481# If MACRO is undefined, set it to VALUE.
482m4_define([m4_define_default],
483[m4_ifndef([$1], [m4_define($@)])])
484
485
ea6cfe9e
AD
486# m4_default(EXP1, EXP2)
487# ----------------------
488# Returns EXP1 if non empty, otherwise EXP2.
489m4_define([m4_default],
490[m4_ifval([$1], [$1], [$2])])
491
492
493# m4_defn(NAME)
494# -------------
495# Unlike to the original, don't tolerate popping something which is
496# undefined.
497m4_define([m4_defn],
498[m4_ifndef([$1],
499 [m4_fatal([$0: undefined macro: $1])])dnl
500m4_builtin([defn], $@)])
501
502
503# _m4_dumpdefs_up(NAME)
504# ---------------------
505m4_define([_m4_dumpdefs_up],
506[m4_ifdef([$1],
507 [m4_pushdef([_m4_dumpdefs], m4_defn([$1]))dnl
508m4_dumpdef([$1])dnl
509m4_popdef([$1])dnl
510_m4_dumpdefs_up([$1])])])
511
512
513# _m4_dumpdefs_down(NAME)
514# -----------------------
515m4_define([_m4_dumpdefs_down],
516[m4_ifdef([_m4_dumpdefs],
517 [m4_pushdef([$1], m4_defn([_m4_dumpdefs]))dnl
518m4_popdef([_m4_dumpdefs])dnl
519_m4_dumpdefs_down([$1])])])
520
521
522# m4_dumpdefs(NAME)
523# -----------------
524# Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its
525# value stack (most recent displayed first).
526m4_define([m4_dumpdefs],
527[_m4_dumpdefs_up([$1])dnl
528_m4_dumpdefs_down([$1])])
529
530
531# m4_popdef(NAME)
532# ---------------
533# Unlike to the original, don't tolerate popping something which is
534# undefined.
535m4_define([m4_popdef],
536[m4_ifndef([$1],
537 [m4_fatal([$0: undefined macro: $1])])dnl
538m4_builtin([popdef], $@)])
539
540
0d8bed56
AD
541# m4_quote(ARGS)
542# --------------
543# Return ARGS as a single arguments.
ea6cfe9e
AD
544#
545# It is important to realize the difference between `m4_quote(exp)' and
546# `[exp]': in the first case you obtain the quoted *result* of the
547# expansion of EXP, while in the latter you just obtain the string
548# `exp'.
0d8bed56
AD
549m4_define([m4_quote], [[$*]])
550m4_define([m4_dquote], [[$@]])
ea6cfe9e
AD
551
552
553# m4_noquote(STRING)
554# ------------------
555# Return the result of ignoring all quotes in STRING and invoking the
556# macros it contains. Amongst other things useful for enabling macro
557# invocations inside strings with [] blocks (for instance regexps and
558# help-strings).
559m4_define([m4_noquote],
560[m4_changequote(-=<{,}>=-)$1-=<{}>=-m4_changequote([,])])
561
562
563# m4_shiftn(N, ...)
564# -----------------
565# Returns ... shifted N times. Useful for recursive "varargs" constructs.
566m4_define([m4_shiftn],
567[m4_assert(($1 >= 0) && ($# > $1))dnl
568_m4_shiftn($@)])
569
570m4_define([_m4_shiftn],
571[m4_if([$1], 0,
572 [m4_shift($@)],
573 [_m4_shiftn(m4_eval([$1]-1), m4_shift(m4_shift($@)))])])
574
575
576# m4_undefine(NAME)
577# -----------------
578# Unlike to the original, don't tolerate undefining something which is
579# undefined.
580m4_define([m4_undefine],
581[m4_ifndef([$1],
582 [m4_fatal([$0: undefined macro: $1])])dnl
583m4_builtin([undefine], $@)])
584
585
586## -------------------------- ##
587## 7. Implementing m4 loops. ##
588## -------------------------- ##
589
590
591# m4_for(VARIABLE, FIRST, LAST, [STEP = +/-1], EXPRESSION)
592# --------------------------------------------------------
593# Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO.
594# Both limits are included, and bounds are checked for consistency.
595m4_define([m4_for],
596[m4_case(m4_sign(m4_eval($3 - $2)),
597 1, [m4_assert(m4_sign(m4_default($4, 1)) == 1)],
598 -1, [m4_assert(m4_sign(m4_default($4, -1)) == -1)])dnl
599m4_pushdef([$1], [$2])dnl
600m4_if(m4_eval([$3 > $2]), 1,
601 [_m4_for([$1], [$3], m4_default([$4], 1), [$5])],
602 [_m4_for([$1], [$3], m4_default([$4], -1), [$5])])dnl
603m4_popdef([$1])])
604
605
606# _m4_for(VARIABLE, FIRST, LAST, STEP, EXPRESSION)
607# ------------------------------------------------
608# Core of the loop, no consistency checks.
609m4_define([_m4_for],
610[$4[]dnl
611m4_if($1, [$2], [],
612 [m4_define([$1], m4_eval($1+[$3]))_m4_for([$1], [$2], [$3], [$4])])])
613
614
615# Implementing `foreach' loops in m4 is much more tricky than it may
616# seem. Actually, the example of a `foreach' loop in the m4
617# documentation is wrong: it does not quote the arguments properly,
618# which leads to undesired expansions.
619#
620# The example in the documentation is:
621#
622# | # foreach(VAR, (LIST), STMT)
623# | m4_define([foreach],
624# | [m4_pushdef([$1])_foreach([$1], [$2], [$3])m4_popdef([$1])])
625# | m4_define([_arg1], [$1])
626# | m4_define([_foreach],
627# | [m4_if([$2], [()], ,
628# | [m4_define([$1], _arg1$2)$3[]_foreach([$1],
629# | (shift$2),
630# | [$3])])])
631#
632# But then if you run
633#
634# | m4_define(a, 1)
635# | m4_define(b, 2)
636# | m4_define(c, 3)
637# | foreach([f], [([a], [(b], [c)])], [echo f
638# | ])
639#
640# it gives
641#
642# => echo 1
643# => echo (2,3)
644#
645# which is not what is expected.
646#
647# Of course the problem is that many quotes are missing. So you add
648# plenty of quotes at random places, until you reach the expected
649# result. Alternatively, if you are a quoting wizard, you directly
650# reach the following implementation (but if you really did, then
651# apply to the maintenance of m4sugar!).
652#
653# | # foreach(VAR, (LIST), STMT)
654# | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
655# | m4_define([_arg1], [[$1]])
656# | m4_define([_foreach],
657# | [m4_if($2, [()], ,
658# | [m4_define([$1], [_arg1$2])$3[]_foreach([$1],
659# | [(shift$2)],
660# | [$3])])])
661#
662# which this time answers
663#
664# => echo a
665# => echo (b
666# => echo c)
667#
668# Bingo!
669#
670# Well, not quite.
671#
672# With a better look, you realize that the parens are more a pain than
673# a help: since anyway you need to quote properly the list, you end up
674# with always using an outermost pair of parens and an outermost pair
675# of quotes. Rejecting the parens both eases the implementation, and
676# simplifies the use:
677#
678# | # foreach(VAR, (LIST), STMT)
679# | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
680# | m4_define([_arg1], [$1])
681# | m4_define([_foreach],
682# | [m4_if($2, [], ,
683# | [m4_define([$1], [_arg1($2)])$3[]_foreach([$1],
684# | [shift($2)],
685# | [$3])])])
686#
687#
688# Now, just replace the `$2' with `m4_quote($2)' in the outer `m4_if'
689# to improve robustness, and you come up with a quite satisfactory
690# implementation.
691
692
693# m4_foreach(VARIABLE, LIST, EXPRESSION)
694# --------------------------------------
695#
696# Expand EXPRESSION assigning each value of the LIST to VARIABLE.
697# LIST should have the form `item_1, item_2, ..., item_n', i.e. the
698# whole list must *quoted*. Quote members too if you don't want them
699# to be expanded.
700#
701# This macro is robust to active symbols:
702# | m4_define(active, [ACT, IVE])
703# | m4_foreach(Var, [active, active], [-Var-])
704# => -ACT--IVE--ACT--IVE-
705#
706# | m4_foreach(Var, [[active], [active]], [-Var-])
707# => -ACT, IVE--ACT, IVE-
708#
709# | m4_foreach(Var, [[[active]], [[active]]], [-Var-])
710# => -active--active-
711m4_define([m4_foreach],
712[m4_pushdef([$1])_m4_foreach($@)m4_popdef([$1])])
713
714# Low level macros used to define m4_foreach.
715m4_define([m4_car], [[$1]])
0d8bed56 716m4_define([m4_cdr], [m4_dquote(m4_shift($@))])
ea6cfe9e 717m4_define([_m4_foreach],
0d8bed56
AD
718[m4_if([$2], [[]], [],
719 [m4_define([$1], m4_car($2))$3[]_m4_foreach([$1],
720 m4_cdr($2),
721 [$3])])])
ea6cfe9e
AD
722
723
724
725## --------------------------- ##
726## 8. More diversion support. ##
727## --------------------------- ##
728
729
730# _m4_divert(DIVERSION-NAME or NUMBER)
731# ------------------------------------
732# If DIVERSION-NAME is the name of a diversion, return its number,
733# otherwise if is a NUMBER return it.
734m4_define([_m4_divert],
735[m4_ifdef([_m4_divert($1)],
736 [m4_indir([_m4_divert($1)])],
737 [$1])])
738
739# KILL is only used to suppress output.
740m4_define([_m4_divert(KILL)], -1)
741
742
743# m4_divert(DIVERSION-NAME)
744# -------------------------
745# Change the diversion stream to DIVERSION-NAME.
746m4_define([m4_divert],
747[m4_define([m4_divert_stack],
748 m4_location[: $0: $1]m4_ifdef([m4_divert_stack], [
749m4_defn([m4_divert_stack])]))dnl
750m4_builtin([divert], _m4_divert([$1]))dnl
751])
752
753
754# m4_divert_push(DIVERSION-NAME)
755# ------------------------------
756# Change the diversion stream to DIVERSION-NAME, while stacking old values.
757m4_define([m4_divert_push],
758[m4_pushdef([m4_divert_stack],
759 m4_location[: $0: $1]m4_ifdef([m4_divert_stack], [
760m4_defn([m4_divert_stack])]))dnl
761m4_pushdef([_m4_divert_diversion], [$1])dnl
762m4_builtin([divert], _m4_divert(_m4_divert_diversion))dnl
763])
764
765
766# m4_divert_pop([DIVERSION-NAME])
767# -------------------------------
768# Change the diversion stream to its previous value, unstacking it.
769# If specified, verify we left DIVERSION-NAME.
770m4_define([m4_divert_pop],
771[m4_ifval([$1],
772 [m4_if(_m4_divert([$1]), m4_divnum, [],
773 [m4_fatal([$0($1): diversion mismatch: ]
774m4_defn([m4_divert_stack]))])])dnl
775m4_popdef([_m4_divert_diversion])dnl
776dnl m4_ifndef([_m4_divert_diversion],
777dnl [m4_fatal([too many m4_divert_pop])])dnl
778m4_builtin([divert],
779 m4_ifdef([_m4_divert_diversion],
780 [_m4_divert(_m4_divert_diversion)], -1))dnl
781m4_popdef([m4_divert_stack])dnl
782])
783
784
785# m4_divert_text(DIVERSION-NAME, CONTENT)
786# ---------------------------------------
787# Output CONTENT into DIVERSION-NAME (which may be a number actually).
788# An end of line is appended for free to CONTENT.
789m4_define([m4_divert_text],
790[m4_divert_push([$1])dnl
791$2
792m4_divert_pop([$1])dnl
793])
794
795
796# m4_divert_once(DIVERSION-NAME, CONTENT)
797# ---------------------------------------
798# Output once CONTENT into DIVERSION-NAME (which may be a number
799# actually). An end of line is appended for free to CONTENT.
800m4_define([m4_divert_once],
801[m4_expand_once([m4_divert_text([$1], [$2])])])
802
803
804# m4_undivert(DIVERSION-NAME)
805# ---------------------------
806# Undivert DIVERSION-NAME.
807m4_define([m4_undivert],
808[m4_builtin([undivert], _m4_divert([$1]))])
809
810
811## -------------------------------------------- ##
812## 8. Defining macros with bells and whistles. ##
813## -------------------------------------------- ##
814
815# `m4_defun' is basically `m4_define' but it equips the macro with the
816# needed machinery for `m4_require'. A macro must be m4_defun'd if
817# either it is m4_require'd, or it m4_require's.
818#
819# Two things deserve attention and are detailed below:
820# 1. Implementation of m4_require
821# 2. Keeping track of the expansion stack
822#
823# 1. Implementation of m4_require
824# ===============================
825#
826# Of course m4_defun AC_PROVIDE's the macro, so that a macro which has
827# been expanded is not expanded again when m4_require'd, but the
828# difficult part is the proper expansion of macros when they are
829# m4_require'd.
830#
831# The implementation is based on two ideas, (i) using diversions to
832