]> git.saurik.com Git - bison.git/blob - data/m4sugar/m4sugar.m4
Resync m4sugar from autoconf.
[bison.git] / data / m4sugar / m4sugar.m4
1 divert(-1)# -*- Autoconf -*-
2 # This file is part of Autoconf.
3 # Base M4 layer.
4 # Requires GNU M4.
5 #
6 # Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7 # 2008 Free Software Foundation, Inc.
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
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.
53 changequote()
54 changequote([, ])
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.
59 ifdef([__gnu__], ,
60 [errprint(M4sugar requires GNU M4. Install it before installing M4sugar or
61 set the M4 environment variable to its absolute file name.)
62 m4exit(2)])
63
64
65 ## ------------------------------- ##
66 ## 1. Simulate --prefix-builtins. ##
67 ## ------------------------------- ##
68
69 # m4_define
70 # m4_defn
71 # m4_undefine
72 define([m4_define], defn([define]))
73 define([m4_defn], defn([defn]))
74 define([m4_undefine], defn([undefine]))
75
76 m4_undefine([define])
77 m4_undefine([defn])
78 m4_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([to], [from($@)])
87 # Well, obviously 1 is more expensive in space. Maybe 2 is more expensive
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 would certainly prefer to see `to'.
92 m4_define([m4_copy],
93 [m4_define([$2], m4_defn([$1]))])
94
95
96 # m4_rename(SRC, DST)
97 # -------------------
98 # Rename the macro SRC to DST.
99 m4_define([m4_rename],
100 [m4_copy([$1], [$2])m4_undefine([$1])])
101
102
103 # m4_rename_m4(MACRO-NAME)
104 # ------------------------
105 # Rename MACRO-NAME to m4_MACRO-NAME.
106 m4_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 to MACRO-NAME.
113 m4_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'. Conditionals first, since
119 # some subsequent renames are conditional.
120 m4_rename_m4([ifdef])
121 m4_rename([ifelse], [m4_if])
122
123 m4_rename_m4([builtin])
124 m4_rename_m4([changecom])
125 m4_rename_m4([changequote])
126 m4_ifdef([changeword],dnl conditionally available in 1.4.x
127 [m4_undefine([changeword])])
128 m4_rename_m4([debugfile])
129 m4_rename_m4([debugmode])
130 m4_rename_m4([decr])
131 m4_undefine([divert])
132 m4_rename_m4([divnum])
133 m4_rename_m4([dumpdef])
134 m4_rename_m4([errprint])
135 m4_rename_m4([esyscmd])
136 m4_rename_m4([eval])
137 m4_rename_m4([format])
138 m4_undefine([include])
139 m4_rename_m4([incr])
140 m4_rename_m4([index])
141 m4_rename_m4([indir])
142 m4_rename_m4([len])
143 m4_rename([m4exit], [m4_exit])
144 m4_undefine([m4wrap])
145 m4_ifdef([mkstemp],dnl added in M4 1.4.8
146 [m4_rename_m4([mkstemp])
147 m4_copy([m4_mkstemp], [m4_maketemp])
148 m4_undefine([maketemp])],
149 [m4_rename_m4([maketemp])
150 m4_copy([m4_maketemp], [m4_mkstemp])])
151 m4_rename([patsubst], [m4_bpatsubst])
152 m4_rename_m4([popdef])
153 m4_rename_m4([pushdef])
154 m4_rename([regexp], [m4_bregexp])
155 m4_rename_m4([shift])
156 m4_undefine([sinclude])
157 m4_rename_m4([substr])
158 m4_ifdef([symbols],dnl present only in alpha-quality 1.4o
159 [m4_rename_m4([symbols])])
160 m4_rename_m4([syscmd])
161 m4_rename_m4([sysval])
162 m4_rename_m4([traceoff])
163 m4_rename_m4([traceon])
164 m4_rename_m4([translit])
165 m4_undefine([undivert])
166
167
168 ## ------------------- ##
169 ## 2. Error messages. ##
170 ## ------------------- ##
171
172
173 # m4_location
174 # -----------
175 m4_define([m4_location],
176 [__file__:__line__])
177
178
179 # m4_errprintn(MSG)
180 # -----------------
181 # Same as `errprint', but with the missing end of line.
182 m4_define([m4_errprintn],
183 [m4_errprint([$1
184 ])])
185
186
187 # m4_warning(MSG)
188 # ---------------
189 # Warn the user.
190 m4_define([m4_warning],
191 [m4_errprintn(m4_location[: warning: $1])])
192
193
194 # m4_fatal(MSG, [EXIT-STATUS])
195 # ----------------------------
196 # Fatal the user. :)
197 m4_define([m4_fatal],
198 [m4_errprintn(m4_location[: error: $1])dnl
199 m4_expansion_stack_dump()dnl
200 m4_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.
207 m4_define([m4_assert],
208 [m4_if(m4_eval([$1]), 0,
209 [m4_fatal([assert failed: $1], [$2])])])
210
211
212
213 ## ------------- ##
214 ## 3. Warnings. ##
215 ## ------------- ##
216
217
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".
223 #
224 # Within m4, the macro is a no-op. This macro really matters
225 # when autom4te post-processes the trace output.
226 m4_define([_m4_warn], [])
227
228
229 # m4_warn(CATEGORY, MESSAGE)
230 # --------------------------
231 # Report a MESSAGE to the user if the CATEGORY of warnings is enabled.
232 m4_define([m4_warn],
233 [_m4_warn([$1], [$2],
234 m4_ifdef([m4_expansion_stack],
235 [_m4_defn([m4_expansion_stack])
236 m4_location[: the top level]]))dnl
237 ])
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
248 # several times. This is, in general, a dangerous operation, because
249 # too many people forget to quote the first argument of m4_define.
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.
261 m4_define([m4_include_unique],
262 [m4_ifdef([m4_include($1)],
263 [m4_warn([syntax], [file `$1' included several times])])dnl
264 m4_define([m4_include($1)])])
265
266
267 # m4_include(FILE)
268 # ----------------
269 # Like the builtin include, but warns against multiple inclusions.
270 m4_define([m4_include],
271 [m4_include_unique([$1])dnl
272 m4_builtin([include], [$1])])
273
274
275 # m4_sinclude(FILE)
276 # -----------------
277 # Like the builtin sinclude, but warns against multiple inclusions.
278 m4_define([m4_sinclude],
279 [m4_include_unique([$1])dnl
280 m4_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 #
291 # In case of arguments of macros, eg. $1, it makes little difference.
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 #
297 # So you want the variation `m4_ifset' that expects a macro name as $1.
298 # If this macro is both defined and defined to a non empty value, then
299 # it runs TRUE, etc.
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.
306 m4_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.
313 m4_define([m4_n],
314 [m4_if([$1],
315 [], [],
316 [$1
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.
324 m4_define([m4_ifvaln],
325 [m4_if([$1],
326 [], [m4_n([$3])],
327 [m4_n([$2])])])
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.
334 m4_define([m4_ifset],
335 [m4_ifdef([$1],
336 [m4_ifval(_m4_defn([$1]), [$2], [$3])],
337 [$3])])
338
339
340 # m4_ifndef(NAME, [IF-NOT-DEFINED], [IF-DEFINED])
341 # -----------------------------------------------
342 m4_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.
364 #
365 # Please keep foreach.m4 in sync with any adjustments made here.
366 m4_define([m4_case],
367 [m4_if([$#], 0, [],
368 [$#], 1, [],
369 [$#], 2, [$2],
370 [$1], [$2], [$3],
371 [$0([$1], m4_shift3($@))])])
372
373
374 # m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT)
375 # -----------------------------------------------------
376 # m4 equivalent of
377 #
378 # if (SWITCH =~ RE1)
379 # VAL1;
380 # elif (SWITCH =~ RE2)
381 # VAL2;
382 # elif ...
383 # ...
384 # else
385 # DEFAULT
386 #
387 # All the values are optional, and the macro is robust to active symbols
388 # properly quoted.
389 #
390 # Please keep foreach.m4 in sync with any adjustments made here.
391 m4_define([m4_bmatch],
392 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
393 [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
394 [$#], 2, [$2],
395 [m4_if(m4_bregexp([$1], [$2]), -1, [$0([$1], m4_shift3($@))],
396 [$3])])])
397
398
399 # m4_car(LIST)
400 # m4_cdr(LIST)
401 # ------------
402 # Manipulate m4 lists.
403 m4_define([m4_car], [[$1]])
404 m4_define([m4_cdr],
405 [m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])],
406 [$#], 1, [],
407 [m4_dquote(m4_shift($@))])])
408
409 # _m4_cdr(LIST)
410 # -------------
411 # Like m4_cdr, except include a leading comma unless only one element
412 # remains. Why? Because comparing a large list against [] is more
413 # expensive in expansion time than comparing the number of arguments; so
414 # _m4_cdr can be used to reduce the number of arguments when it is time
415 # to end recursion.
416 m4_define([_m4_cdr],
417 [m4_if([$#], 1, [],
418 [, m4_dquote(m4_shift($@))])])
419
420
421
422 # m4_cond(TEST1, VAL1, IF-VAL1, TEST2, VAL2, IF-VAL2, ..., [DEFAULT])
423 # -------------------------------------------------------------------
424 # Similar to m4_if, except that each TEST is expanded when encountered.
425 # If the expansion of TESTn matches the string VALn, the result is IF-VALn.
426 # The result is DEFAULT if no tests passed. This macro allows
427 # short-circuiting of expensive tests, where it pays to arrange quick
428 # filter tests to run first.
429 #
430 # For an example, consider a previous implementation of _AS_QUOTE_IFELSE:
431 #
432 # m4_if(m4_index([$1], [\]), [-1], [$2],
433 # m4_eval(m4_index([$1], [\\]) >= 0), [1], [$2],
434 # m4_eval(m4_index([$1], [\$]) >= 0), [1], [$2],
435 # m4_eval(m4_index([$1], [\`]) >= 0), [1], [$3],
436 # m4_eval(m4_index([$1], [\"]) >= 0), [1], [$3],
437 # [$2])
438 #
439 # Here, m4_index is computed 5 times, and m4_eval 4, even if $1 contains
440 # no backslash. It is more efficient to do:
441 #
442 # m4_cond([m4_index([$1], [\])], [-1], [$2],
443 # [m4_eval(m4_index([$1], [\\]) >= 0)], [1], [$2],
444 # [m4_eval(m4_index([$1], [\$]) >= 0)], [1], [$2],
445 # [m4_eval(m4_index([$1], [\`]) >= 0)], [1], [$3],
446 # [m4_eval(m4_index([$1], [\"]) >= 0)], [1], [$3],
447 # [$2])
448 #
449 # In the common case of $1 with no backslash, only one m4_index expansion
450 # occurs, and m4_eval is avoided altogether.
451 #
452 # Please keep foreach.m4 in sync with any adjustments made here.
453 m4_define([m4_cond],
454 [m4_if([$#], [0], [m4_fatal([$0: cannot be called without arguments])],
455 [$#], [1], [$1],
456 m4_eval([$# % 3]), [2], [m4_fatal([$0: missing an argument])],
457 [_$0($@)])])
458
459 m4_define([_m4_cond],
460 [m4_if(($1), [($2)], [$3],
461 [$#], [3], [],
462 [$#], [4], [$4],
463 [$0(m4_shift3($@))])])
464
465
466 ## ---------------------------------------- ##
467 ## 6. Enhanced version of some primitives. ##
468 ## ---------------------------------------- ##
469
470 # m4_bpatsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...)
471 # ----------------------------------------------------
472 # m4 equivalent of
473 #
474 # $_ = STRING;
475 # s/RE1/SUBST1/g;
476 # s/RE2/SUBST2/g;
477 # ...
478 #
479 # All the values are optional, and the macro is robust to active symbols
480 # properly quoted.
481 #
482 # I would have liked to name this macro `m4_bpatsubst', unfortunately,
483 # due to quotation problems, I need to double quote $1 below, therefore
484 # the anchors are broken :( I can't let users be trapped by that.
485 #
486 # Recall that m4_shift3 always results in an argument. Hence, we need
487 # to distinguish between a final deletion vs. ending recursion.
488 #
489 # Please keep foreach.m4 in sync with any adjustments made here.
490 m4_define([m4_bpatsubsts],
491 [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
492 [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
493 [$#], 2, [m4_unquote(m4_builtin([patsubst], [[$1]], [$2]))],
494 [$#], 3, [m4_unquote(m4_builtin([patsubst], [[$1]], [$2], [$3]))],
495 [_$0($@m4_if(m4_eval($# & 1), 0, [,]))])])
496 m4_define([_m4_bpatsubsts],
497 [m4_if([$#], 2, [$1],
498 [$0(m4_builtin([patsubst], [[$1]], [$2], [$3]),
499 m4_shift3($@))])])
500
501
502 # m4_define_default(MACRO, VALUE)
503 # -------------------------------
504 # If MACRO is undefined, set it to VALUE.
505 m4_define([m4_define_default],
506 [m4_ifndef([$1], [m4_define($@)])])
507
508
509 # m4_default(EXP1, EXP2)
510 # ----------------------
511 # Returns EXP1 if non empty, otherwise EXP2.
512 #
513 # This macro is called on hot paths, so inline the contents of m4_ifval,
514 # for one less round of expansion.
515 m4_define([m4_default],
516 [m4_if([$1], [], [$2], [$1])])
517
518
519 # m4_defn(NAME)
520 # -------------
521 # Like the original, except guarantee a warning when using something which is
522 # undefined (unlike M4 1.4.x). This replacement is not a full-featured
523 # replacement: if any of the defined macros contain unbalanced quoting, but
524 # when pasted together result in a well-quoted string, then only native m4
525 # support is able to get it correct. But that's where quadrigraphs come in
526 # handy, if you really need unbalanced quotes inside your macros.
527 #
528 # This macro is called frequently, so minimize the amount of additional
529 # expansions by skipping m4_ifndef. Better yet, if __m4_version__ exists,
530 # (added in M4 1.6), then let m4 do the job for us (see m4_init).
531 #
532 # _m4_defn is for internal use only - it bypasses the wrapper, so it
533 # must only be used on one argument at a time, and only on macros
534 # known to be defined. Make sure this still works if the user renames
535 # m4_defn but not _m4_defn.
536 m4_copy([m4_defn], [_m4_defn])
537 m4_define([m4_defn],
538 [m4_if([$#], [0], [[$0]],
539 [$#], [1], [m4_ifdef([$1], [_m4_defn([$1])],
540 [m4_fatal([$0: undefined macro: $1])])],
541 [m4_foreach([_m4_macro], [$@], [$0(_m4_defn([_m4_macro]))])])])
542
543
544 # _m4_dumpdefs_up(NAME)
545 # ---------------------
546 m4_define([_m4_dumpdefs_up],
547 [m4_ifdef([$1],
548 [m4_pushdef([_m4_dumpdefs], _m4_defn([$1]))dnl
549 m4_dumpdef([$1])dnl
550 _m4_popdef([$1])dnl
551 _m4_dumpdefs_up([$1])])])
552
553
554 # _m4_dumpdefs_down(NAME)
555 # -----------------------
556 m4_define([_m4_dumpdefs_down],
557 [m4_ifdef([_m4_dumpdefs],
558 [m4_pushdef([$1], _m4_defn([_m4_dumpdefs]))dnl
559 _m4_popdef([_m4_dumpdefs])dnl
560 _m4_dumpdefs_down([$1])])])
561
562
563 # m4_dumpdefs(NAME)
564 # -----------------
565 # Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its
566 # value stack (most recent displayed first).
567 m4_define([m4_dumpdefs],
568 [_m4_dumpdefs_up([$1])dnl
569 _m4_dumpdefs_down([$1])])
570
571
572 # m4_popdef(NAME)
573 # ---------------
574 # Like the original, except guarantee a warning when using something which is
575 # undefined (unlike M4 1.4.x).
576 #
577 # This macro is called frequently, so minimize the amount of additional
578 # expansions by skipping m4_ifndef. Better yet, if __m4_version__ exists,
579 # (added in M4 1.6), then let m4 do the job for us (see m4_init).
580 #
581 # _m4_popdef is for internal use only - it bypasses the wrapper, so it
582 # must only be used on macros known to be defined. Make sure this
583 # still works if the user renames m4_popdef but not _m4_popdef.
584 m4_copy([m4_popdef], [_m4_popdef])
585 m4_define([m4_popdef],
586 [m4_if([$#], [0], [[$0]],
587 [$#], [1], [m4_ifdef([$1], [_m4_popdef([$1])],
588 [m4_fatal([$0: undefined macro: $1])])],
589 [m4_foreach([_m4_macro], [$@], [$0(_m4_defn([_m4_macro]))])])])
590
591
592 # m4_shiftn(N, ...)
593 # -----------------
594 # Returns ... shifted N times. Useful for recursive "varargs" constructs.
595 #
596 # Autoconf does not use this macro, because it is inherently slower than
597 # calling the common cases of m4_shift2 or m4_shift3 directly. But it
598 # might as well be fast for other clients, such as Libtool. One way to
599 # do this is to expand $@ only once in _m4_shiftn (otherwise, for long
600 # lists, the expansion of m4_if takes twice as much memory as what the
601 # list itself occupies, only to throw away the unused branch). The end
602 # result is strictly equivalent to
603 # m4_if([$1], 1, [m4_shift(,m4_shift(m4_shift($@)))],
604 # [_m4_shiftn(m4_decr([$1]), m4_shift(m4_shift($@)))])
605 # but with the final `m4_shift(m4_shift($@)))' shared between the two
606 # paths. The first leg uses a no-op m4_shift(,$@) to balance out the ().
607 #
608 # Please keep foreach.m4 in sync with any adjustments made here.
609 m4_define([m4_shiftn],
610 [m4_assert(0 < $1 && $1 < $#)_$0($@)])
611
612 m4_define([_m4_shiftn],
613 [m4_if([$1], 1, [m4_shift(],
614 [$0(m4_decr([$1])]), m4_shift(m4_shift($@)))])
615
616 # m4_shift2(...)
617 # m4_shift3(...)
618 # -----------------
619 # Returns ... shifted twice, and three times. Faster than m4_shiftn.
620 m4_define([m4_shift2], [m4_shift(m4_shift($@))])
621 m4_define([m4_shift3], [m4_shift(m4_shift(m4_shift($@)))])
622
623 # _m4_shift2(...)
624 # _m4_shift3(...)
625 # ---------------
626 # Like m4_shift2 or m4_shift3, except include a leading comma unless shifting
627 # consumes all arguments. Why? Because in recursion, it is nice to
628 # distinguish between 1 element left and 0 elements left, based on how many
629 # arguments this shift expands to.
630 m4_define([_m4_shift2],
631 [m4_if([$#], [2], [],
632 [, m4_shift(m4_shift($@))])])
633 m4_define([_m4_shift3],
634 [m4_if([$#], [3], [],
635 [, m4_shift(m4_shift(m4_shift($@)))])])
636
637
638 # m4_undefine(NAME)
639 # -----------------
640 # Like the original, except guarantee a warning when using something which is
641 # undefined (unlike M4 1.4.x).
642 #
643 # This macro is called frequently, so minimize the amount of additional
644 # expansions by skipping m4_ifndef. Better yet, if __m4_version__ exists,
645 # (added in M4 1.6), then let m4 do the job for us (see m4_init).
646 #
647 # _m4_undefine is for internal use only - it bypasses the wrapper, so
648 # it must only be used on macros known to be defined. Make sure this
649 # still works if the user renames m4_undefine but not _m4_undefine.
650 m4_copy([m4_undefine], [_m4_undefine])
651 m4_define([m4_undefine],
652 [m4_if([$#], [0], [[$0]],
653 [$#], [1], [m4_ifdef([$1], [_m4_undefine([$1])],
654 [m4_fatal([$0: undefined macro: $1])])],
655 [m4_foreach([_m4_macro], [$@], [$0(_m4_defn([_m4_macro]))])])])
656
657 # _m4_wrap(PRE, POST)
658 # -------------------
659 # Helper macro for m4_wrap and m4_wrap_lifo. Allows nested calls to
660 # m4_wrap within wrapped text. Use _m4_defn and _m4_popdef for speed.
661 m4_define([_m4_wrap],
662 [m4_ifdef([$0_text],
663 [m4_define([$0_text], [$1]_m4_defn([$0_text])[$2])],
664 [m4_builtin([m4wrap], [m4_unquote(
665 _m4_defn([$0_text])_m4_popdef([$0_text]))])m4_define([$0_text], [$1$2])])])
666
667 # m4_wrap(TEXT)
668 # -------------
669 # Append TEXT to the list of hooks to be executed at the end of input.
670 # Whereas the order of the original may be LIFO in the underlying m4,
671 # this version is always FIFO.
672 m4_define([m4_wrap],
673 [_m4_wrap([], [$1[]])])
674
675 # m4_wrap_lifo(TEXT)
676 # ------------------
677 # Prepend TEXT to the list of hooks to be executed at the end of input.
678 # Whereas the order of m4_wrap may be FIFO in the underlying m4, this
679 # version is always LIFO.
680 m4_define([m4_wrap_lifo],
681 [_m4_wrap([$1[]])])
682
683 ## ------------------------- ##
684 ## 7. Quoting manipulation. ##
685 ## ------------------------- ##
686
687
688 # m4_apply(MACRO, LIST)
689 # ---------------------
690 # Invoke MACRO, with arguments provided from the quoted list of
691 # comma-separated quoted arguments. If LIST is empty, invoke MACRO
692 # without arguments. The expansion will not be concatenated with
693 # subsequent text.
694 m4_define([m4_apply],
695 [m4_if([$2], [], [$1], [$1($2)])[]])
696
697 # _m4_apply(MACRO, LIST)
698 # ----------------------
699 # Like m4_apply, except do nothing if LIST is empty.
700 m4_define([_m4_apply],
701 [m4_if([$2], [], [], [$1($2)[]])])
702
703
704 # m4_count(ARGS)
705 # --------------
706 # Return a count of how many ARGS are present.
707 m4_define([m4_count], [$#])
708
709
710 # m4_do(STRING, ...)
711 # ------------------
712 # This macro invokes all its arguments (in sequence, of course). It is
713 # useful for making your macros more structured and readable by dropping
714 # unnecessary dnl's and have the macros indented properly. No concatenation
715 # occurs after a STRING; use m4_unquote(m4_join(,STRING)) for that.
716 #
717 # Please keep foreach.m4 in sync with any adjustments made here.
718 m4_define([m4_do],
719 [m4_if([$#], 0, [],
720 [$#], 1, [$1[]],
721 [$1[]$0(m4_shift($@))])])
722
723
724 # m4_dquote(ARGS)
725 # ---------------
726 # Return ARGS as a quoted list of quoted arguments.
727 m4_define([m4_dquote], [[$@]])
728
729
730 # m4_dquote_elt(ARGS)
731 # -------------------
732 # Return ARGS as an unquoted list of double-quoted arguments.
733 #
734 # Please keep foreach.m4 in sync with any adjustments made here.
735 m4_define([m4_dquote_elt],
736 [m4_if([$#], [0], [],
737 [$#], [1], [[[$1]]],
738 [[[$1]],$0(m4_shift($@))])])
739
740
741 # m4_echo(ARGS)
742 # -------------
743 # Return the ARGS, with the same level of quoting. Whitespace after
744 # unquoted commas are consumed.
745 m4_define([m4_echo], [$@])
746
747
748 # m4_expand(ARG)
749 # --------------
750 # Return the expansion of ARG as a single string. Unlike m4_quote($1), this
751 # correctly preserves whitespace following single-quoted commas that appeared
752 # within ARG.
753 #
754 # m4_define([active], [ACT, IVE])
755 # m4_define([active2], [[ACT, IVE]])
756 # m4_quote(active, active2)
757 # => ACT,IVE,ACT, IVE
758 # m4_expand([active, active2])
759 # => ACT, IVE, ACT, IVE
760 #
761 # Unfortunately, due to limitations in m4, ARG must expand to something
762 # with balanced quotes (use quadrigraphs to get around this). The input
763 # is not likely to have unbalanced -=<{(/)}>=- quotes, and it is possible
764 # to have unbalanced (), provided it was specified with proper [] quotes.
765 #
766 # Exploit that extra () will group unquoted commas and the following
767 # whitespace, then convert () to []. m4_bpatsubst can't handle newlines
768 # inside $1, and m4_substr strips quoting. So we (ab)use m4_changequote.
769 m4_define([m4_expand], [_$0(-=<{($1)}>=-)])
770 m4_define([_m4_expand],
771 [m4_changequote([-=<{(], [)}>=-])$1m4_changequote([, ])])
772
773
774 # m4_ignore(ARGS)
775 # ---------------
776 # Expands to nothing. Useful for conditionally ignoring an arbitrary
777 # number of arguments (see _m4_list_cmp for an example).
778 m4_define([m4_ignore])
779
780
781 # m4_make_list(ARGS)
782 # ------------------
783 # Similar to m4_dquote, this creates a quoted list of quoted ARGS. This
784 # version is less efficient than m4_dquote, but separates each argument
785 # with a comma and newline, rather than just comma, for readability.
786 # When developing an m4sugar algorithm, you could temporarily use
787 # m4_pushdef([m4_dquote],m4_defn([m4_make_list]))
788 # around your code to make debugging easier.
789 m4_define([m4_make_list], [m4_join([,
790 ], m4_dquote_elt($@))])
791
792
793 # m4_noquote(STRING)
794 # ------------------
795 # Return the result of ignoring all quotes in STRING and invoking the
796 # macros it contains. Amongst other things, this is useful for enabling
797 # macro invocations inside strings with [] blocks (for instance regexps
798 # and help-strings). On the other hand, since all quotes are disabled,
799 # any macro expanded during this time that relies on nested [] quoting
800 # will likely crash and burn. This macro is seldom useful; consider
801 # m4_unquote or m4_expand instead.
802 m4_define([m4_noquote],
803 [m4_changequote([-=<{(],[)}>=-])$1-=<{()}>=-m4_changequote([,])])
804
805
806 # m4_quote(ARGS)
807 # --------------
808 # Return ARGS as a single argument. Any whitespace after unquoted commas
809 # is stripped. There is always output, even when there were no arguments.
810 #
811 # It is important to realize the difference between `m4_quote(exp)' and
812 # `[exp]': in the first case you obtain the quoted *result* of the
813 # expansion of EXP, while in the latter you just obtain the string
814 # `exp'.
815 m4_define([m4_quote], [[$*]])
816
817
818 # _m4_quote(ARGS)
819 # ---------------
820 # Like m4_quote, except that when there are no arguments, there is no
821 # output. For conditional scenarios (such as passing _m4_quote as the
822 # macro name in m4_mapall), this feature can be used to distinguish between
823 # one argument of the empty string vs. no arguments. However, in the
824 # normal case with arguments present, this is less efficient than m4_quote.
825 m4_define([_m4_quote],
826 [m4_if([$#], [0], [], [[$*]])])
827
828
829 # m4_reverse(ARGS)
830 # ----------------
831 # Output ARGS in reverse order.
832 #
833 # Please keep foreach.m4 in sync with any adjustments made here.
834 m4_define([m4_reverse],
835 [m4_if([$#], [0], [], [$#], [1], [[$1]],
836 [$0(m4_shift($@)), [$1]])])
837
838
839 # m4_unquote(ARGS)
840 # ----------------
841 # Remove one layer of quotes from each ARG, performing one level of
842 # expansion. For one argument, m4_unquote([arg]) is more efficient than
843 # m4_do([arg]), but for multiple arguments, the difference is that
844 # m4_unquote separates arguments with commas while m4_do concatenates.
845 # Follow this macro with [] if concatenation with subsequent text is
846 # undesired.
847 m4_define([m4_unquote], [$*])
848
849
850 ## -------------------------- ##
851 ## 8. Implementing m4 loops. ##
852 ## -------------------------- ##
853
854
855 # m4_for(VARIABLE, FIRST, LAST, [STEP = +/-1], EXPRESSION)
856 # --------------------------------------------------------
857 # Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO with
858 # increments of STEP. Both limits are included, and bounds are
859 # checked for consistency. The algorithm is robust to indirect
860 # VARIABLE names. Changing VARIABLE inside EXPRESSION will not impact
861 # the number of iterations.
862 #
863 # Uses _m4_defn for speed, and avoid dnl in the macro body.
864 m4_define([m4_for],
865 [m4_pushdef([$1], m4_eval([$2]))]dnl
866 [m4_cond([m4_eval(([$3]) > ([$2]))], 1,
867 [m4_pushdef([_m4_step], m4_eval(m4_default([$4],
868 1)))m4_assert(_m4_step > 0)_$0([$1], _m4_defn([$1]),
869 m4_eval((([$3]) - ([$2])) / _m4_step * _m4_step + ([$2])),
870 _m4_step, [$5])],
871 [m4_eval(([$3]) < ([$2]))], 1,
872 [m4_pushdef([_m4_step], m4_eval(m4_default([$4],
873 -1)))m4_assert(_m4_step < 0)_$0([$1], _m4_defn([$1]),
874 m4_eval((([$2]) - ([$3])) / -(_m4_step) * _m4_step + ([$2])),
875 _m4_step, [$5])],
876 [m4_pushdef([_m4_step])$5])[]]dnl
877 [m4_popdef([_m4_step], [$1])])
878
879
880 # _m4_for(VARIABLE, COUNT, LAST, STEP, EXPRESSION)
881 # ------------------------------------------------
882 # Core of the loop, no consistency checks, all arguments are plain
883 # numbers. Define VARIABLE to COUNT, expand EXPRESSION, then alter
884 # COUNT by STEP and iterate if COUNT is not LAST.
885 m4_define([_m4_for],
886 [m4_define([$1], [$2])$5[]m4_if([$2], [$3], [],
887 [$0([$1], m4_eval([$2 + $4]), [$3], [$4], [$5])])])
888
889
890 # Implementing `foreach' loops in m4 is much more tricky than it may
891 # seem. For example, the old M4 1.4.4 manual had an incorrect example,
892 # which looked like this (when translated to m4sugar):
893 #
894 # | # foreach(VAR, (LIST), STMT)
895 # | m4_define([foreach],
896 # | [m4_pushdef([$1])_foreach([$1], [$2], [$3])m4_popdef([$1])])
897 # | m4_define([_arg1], [$1])
898 # | m4_define([_foreach],
899 # | [m4_if([$2], [()], ,
900 # | [m4_define([$1], _arg1$2)$3[]_foreach([$1], (m4_shift$2), [$3])])])
901 #
902 # But then if you run
903 #
904 # | m4_define(a, 1)
905 # | m4_define(b, 2)
906 # | m4_define(c, 3)
907 # | foreach([f], [([a], [(b], [c)])], [echo f
908 # | ])
909 #
910 # it gives
911 #
912 # => echo 1
913 # => echo (2,3)
914 #
915 # which is not what is expected.
916 #
917 # Of course the problem is that many quotes are missing. So you add
918 # plenty of quotes at random places, until you reach the expected
919 # result. Alternatively, if you are a quoting wizard, you directly
920 # reach the following implementation (but if you really did, then
921 # apply to the maintenance of m4sugar!).
922 #
923 # | # foreach(VAR, (LIST), STMT)
924 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
925 # | m4_define([_arg1], [[$1]])
926 # | m4_define([_foreach],
927 # | [m4_if($2, [()], ,
928 # | [m4_define([$1], [_arg1$2])$3[]_foreach([$1], [(m4_shift$2)], [$3])])])
929 #
930 # which this time answers
931 #
932 # => echo a
933 # => echo (b
934 # => echo c)
935 #
936 # Bingo!
937 #
938 # Well, not quite.
939 #
940 # With a better look, you realize that the parens are more a pain than
941 # a help: since anyway you need to quote properly the list, you end up
942 # with always using an outermost pair of parens and an outermost pair
943 # of quotes. Rejecting the parens both eases the implementation, and
944 # simplifies the use:
945 #
946 # | # foreach(VAR, (LIST), STMT)
947 # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])])
948 # | m4_define([_arg1], [$1])
949 # | m4_define([_foreach],
950 # | [m4_if($2, [], ,
951 # | [m4_define([$1], [_arg1($2)])$3[]_foreach([$1], [m4_shift($2)], [$3])])])
952 #
953 #
954 # Now, just replace the `$2' with `m4_quote($2)' in the outer `m4_if'
955 # to improve robustness, and you come up with a nice implementation
956 # that doesn't require extra parentheses in the user's LIST.
957 #
958 # But wait - now the algorithm is quadratic, because every recursion of
959 # the algorithm keeps the entire LIST and merely adds another m4_shift to
960 # the quoted text. If the user has a lot of elements in LIST, you can
961 # bring the system to its knees with the memory m4 then requires, or trip
962 # the m4 --nesting-limit recursion factor. The only way to avoid
963 # quadratic growth is ensure m4_shift is expanded prior to the recursion.
964 # Hence the design below.
965 #
966 # The M4 manual now includes a chapter devoted to this issue, with
967 # the lessons learned from m4sugar. And still, this design is only
968 # optimal for M4 1.6; see foreach.m4 for yet more comments on why
969 # M4 1.4.x uses yet another implementation.
970
971
972 # m4_foreach(VARIABLE, LIST, EXPRESSION)
973 # --------------------------------------
974 #
975 # Expand EXPRESSION assigning each value of the LIST to VARIABLE.
976 # LIST should have the form `item_1, item_2, ..., item_n', i.e. the
977 # whole list must *quoted*. Quote members too if you don't want them
978 # to be expanded.
979 #
980 # This macro is robust to active symbols:
981 # | m4_define(active, [ACT, IVE])
982 # | m4_foreach(Var, [active, active], [-Var-])
983 # => -ACT--IVE--ACT--IVE-
984 #
985 # | m4_foreach(Var, [[active], [active]], [-Var-])
986 # => -ACT, IVE--ACT, IVE-
987 #
988 # | m4_foreach(Var, [[[active]], [[active]]], [-Var-])
989 # => -active--active-
990 #
991 # This macro is called frequently, so avoid extra expansions such as
992 # m4_ifval and dnl. Also, since $2 might be quite large, try to use it
993 # as little as possible in _m4_foreach; each extra use requires that much
994 # more memory for expansion. So, rather than directly compare $2 against
995 # [] and use m4_car/m4_cdr for recursion, we instead unbox the list (which
996 # requires swapping the argument order in the helper), insert an ignored
997 # third argument, and use m4_shift3 to detect when recursion is complete.
998 #
999 # Please keep foreach.m4 in sync with any adjustments made here.
1000 m4_define([m4_foreach],
1001 [m4_if([$2], [], [],
1002 [m4_pushdef([$1])_$0([$1], [$3], [], $2)m4_popdef([$1])])])
1003
1004 m4_define([_m4_foreach],
1005 [m4_if([$#], [3], [],
1006 [m4_define([$1], [$4])$2[]$0([$1], [$2], m4_shift3($@))])])
1007
1008
1009 # m4_foreach_w(VARIABLE, LIST, EXPRESSION)
1010 # ----------------------------------------
1011 #
1012 # Like m4_foreach, but the list is whitespace separated.
1013 #
1014 # This macro is robust to active symbols:
1015 # m4_foreach_w([Var], [ active
1016 # b act\
1017 # ive ], [-Var-])end
1018 # => -active--b--active-end
1019 #
1020 m4_define([m4_foreach_w],
1021 [m4_foreach([$1], m4_split(m4_normalize([$2]), [ ]), [$3])])
1022
1023
1024 # m4_map(MACRO, LIST)
1025 # m4_mapall(MACRO, LIST)
1026 # ----------------------
1027 # Invoke MACRO($1), MACRO($2) etc. where $1, $2... are the elements of
1028 # LIST. $1, $2... must in turn be lists, appropriate for m4_apply.
1029 # If LIST contains an empty sublist, m4_map skips the expansion of
1030 # MACRO, while m4_mapall expands MACRO with no arguments.
1031 #
1032 # Since LIST may be quite large, we want to minimize how often it
1033 # appears in the expansion. Rather than use m4_car/m4_cdr iteration,
1034 # we unbox the list, ignore the second argument, and use m4_shift2 to
1035 # detect the end of recursion. The mismatch in () is intentional; see
1036 # _m4_map. For m4_map, an empty list behaves like an empty sublist
1037 # and gets ignored; for m4_mapall, we must special-case the empty
1038 # list.
1039 #
1040 # Please keep foreach.m4 in sync with any adjustments made here.
1041 m4_define([m4_map],
1042 [_m4_map([_m4_apply([$1]], [], $2)])
1043
1044 m4_define([m4_mapall],
1045 [m4_if([$2], [], [],
1046 [_m4_map([m4_apply([$1]], [], $2)])])
1047
1048
1049 # m4_map_sep(MACRO, SEPARATOR, LIST)
1050 # m4_mapall_sep(MACRO, SEPARATOR, LIST)
1051 # -------------------------------------
1052 # Invoke MACRO($1), SEPARATOR, MACRO($2), ..., MACRO($N) where $1,
1053 # $2... $N are the elements of LIST, and are in turn lists appropriate
1054 # for m4_apply. SEPARATOR is expanded, in order to allow the creation
1055 # of a list of arguments by using a single-quoted comma as the
1056 # separator. For each empty sublist, m4_map_sep skips the expansion
1057 # of MACRO and SEPARATOR, while m4_mapall_sep expands MACRO with no
1058 # arguments.
1059 #
1060 # For m4_mapall_sep, merely expand the first iteration without the
1061 # separator, then include separator as part of subsequent recursion;
1062 # but avoid extra expansion of LIST's side-effects via a helper macro.
1063 # For m4_map_sep, things are trickier - we don't know if the first
1064 # list element is an empty sublist, so we must define a self-modifying
1065 # helper macro and use that as the separator instead.
1066 m4_define([m4_map_sep],
1067 [m4_pushdef([m4_Sep], [m4_define([m4_Sep], _m4_defn([m4_unquote]))])]dnl
1068 [_m4_map([_m4_apply([m4_Sep([$2])[]$1]], [], $3)m4_popdef([m4_Sep])])
1069
1070 m4_define([m4_mapall_sep],
1071 [m4_if([$3], [], [], [_$0([$1], [$2], $3)])])
1072
1073 m4_define([_m4_mapall_sep],
1074 [m4_apply([$1], [$3])_m4_map([m4_apply([$2[]$1]], m4_shift2($@))])
1075
1076 # _m4_map(PREFIX, IGNORED, SUBLIST, ...)
1077 # --------------------------------------
1078 # Common implementation for all four m4_map variants. The mismatch in
1079 # the number of () is intentional. PREFIX must supply a form of
1080 # m4_apply, the open `(', and the MACRO to be applied. Each iteration
1081 # then appends `,', the current SUBLIST and the closing `)', then
1082 # recurses to the next SUBLIST. IGNORED is an aid to ending recursion
1083 # efficiently.
1084 #
1085 # Please keep foreach.m4 in sync with any adjustments made here.
1086 m4_define([_m4_map],
1087 [m4_if([$#], [2], [],
1088 [$1, [$3])$0([$1], m4_shift2($@))])])
1089
1090 # m4_transform(EXPRESSION, ARG...)
1091 # --------------------------------
1092 # Expand EXPRESSION([ARG]) for each argument. More efficient than
1093 # m4_foreach([var], [ARG...], [EXPRESSION(m4_defn([var]))])
1094 #
1095 # Please keep foreach.m4 in sync with any adjustments made here.
1096 m4_define([m4_transform],
1097 [m4_if([$#], [0], [m4_fatal([$0: too few arguments: $#])],
1098 [$#], [1], [],
1099 [$#], [2], [$1([$2])[]],
1100 [$1([$2])[]$0([$1], m4_shift2($@))])])
1101
1102
1103 # m4_transform_pair(EXPRESSION, [END-EXPR = EXPRESSION], ARG...)
1104 # --------------------------------------------------------------
1105 # Perform a pairwise grouping of consecutive ARGs, by expanding
1106 # EXPRESSION([ARG1], [ARG2]). If there are an odd number of ARGs, the
1107 # final argument is expanded with END-EXPR([ARGn]).
1108 #
1109 # For example:
1110 # m4_define([show], [($*)m4_newline])dnl
1111 # m4_transform_pair([show], [], [a], [b], [c], [d], [e])dnl
1112 # => (a,b)
1113 # => (c,d)
1114 # => (e)
1115 #
1116 # Please keep foreach.m4 in sync with any adjustments made here.
1117 m4_define([m4_transform_pair],
1118 [m4_if([$#], [0], [m4_fatal([$0: too few arguments: $#])],
1119 [$#], [1], [m4_fatal([$0: too few arguments: $#: $1])],
1120 [$#], [2], [],
1121 [$#], [3], [m4_default([$2], [$1])([$3])[]],
1122 [$#], [4], [$1([$3], [$4])[]],
1123 [$1([$3], [$4])[]$0([$1], [$2], m4_shift(m4_shift3($@)))])])
1124
1125
1126 ## --------------------------- ##
1127 ## 9. More diversion support. ##
1128 ## --------------------------- ##
1129
1130
1131 # _m4_divert(DIVERSION-NAME or NUMBER)
1132 # ------------------------------------
1133 # If DIVERSION-NAME is the name of a diversion, return its number,
1134 # otherwise if it is a NUMBER return it.
1135 m4_define([_m4_divert],
1136 [m4_ifdef([_m4_divert($1)],
1137 [m4_indir([_m4_divert($1)])],
1138 [$1])])
1139
1140 # KILL is only used to suppress output.
1141 m4_define([_m4_divert(KILL)], -1)
1142
1143 # The empty diversion name is a synonym for 0.
1144 m4_define([_m4_divert()], 0)
1145
1146
1147 # _m4_divert_n_stack
1148 # ------------------
1149 # Print m4_divert_stack with newline prepended, if it's nonempty.
1150 m4_define([_m4_divert_n_stack],
1151 [m4_ifdef([m4_divert_stack], [
1152 _m4_defn([m4_divert_stack])])])
1153
1154
1155 # m4_divert(DIVERSION-NAME)
1156 # -------------------------
1157 # Change the diversion stream to DIVERSION-NAME.
1158 m4_define([m4_divert],
1159 [m4_define([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)]dnl
1160 [m4_builtin([divert], _m4_divert([$1]))])
1161
1162
1163 # m4_divert_push(DIVERSION-NAME)
1164 # ------------------------------
1165 # Change the diversion stream to DIVERSION-NAME, while stacking old values.
1166 m4_define([m4_divert_push],
1167 [m4_pushdef([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)]dnl
1168 [m4_pushdef([_m4_divert_diversion], [$1])]dnl
1169 [m4_builtin([divert], _m4_divert([$1]))])
1170
1171
1172 # m4_divert_pop([DIVERSION-NAME])
1173 # -------------------------------
1174 # Change the diversion stream to its previous value, unstacking it.
1175 # If specified, verify we left DIVERSION-NAME.
1176 # When we pop the last value from the stack, we divert to -1.
1177 m4_define([m4_divert_pop],
1178 [m4_ifndef([_m4_divert_diversion],
1179 [m4_fatal([too many m4_divert_pop])])]dnl
1180 [m4_if([$1], [], [],
1181 [$1], _m4_defn([_m4_divert_diversion]), [],
1182 [m4_fatal([$0($1): diversion mismatch: ]_m4_divert_n_stack)])]dnl
1183 [_m4_popdef([m4_divert_stack], [_m4_divert_diversion])]dnl
1184 [m4_builtin([divert],
1185 m4_ifdef([_m4_divert_diversion],
1186 [_m4_divert(_m4_defn([_m4_divert_diversion]))],
1187 -1))])
1188
1189
1190 # m4_divert_text(DIVERSION-NAME, CONTENT)
1191 # ---------------------------------------
1192 # Output CONTENT into DIVERSION-NAME (which may be a number actually).
1193 # An end of line is appended for free to CONTENT.
1194 m4_define([m4_divert_text],
1195 [m4_divert_push([$1])$2
1196 m4_divert_pop([$1])])
1197
1198
1199 # m4_divert_once(DIVERSION-NAME, CONTENT)
1200 # ---------------------------------------
1201 # Output CONTENT into DIVERSION-NAME once, if not already there.
1202 # An end of line is appended for free to CONTENT.
1203 m4_define([m4_divert_once],
1204 [m4_expand_once([m4_divert_text([$1], [$2])])])
1205
1206
1207 # m4_undivert(DIVERSION-NAME)
1208 # ---------------------------
1209 # Undivert DIVERSION-NAME. Unlike the M4 version, this only takes a single
1210 # diversion identifier, and should not be used to undivert files.
1211 m4_define([m4_undivert],
1212 [m4_builtin([undivert], _m4_divert([$1]))])
1213
1214
1215 ## --------------------------------------------- ##
1216 ## 10. Defining macros with bells and whistles. ##
1217 ## --------------------------------------------- ##
1218
1219 # `m4_defun' is basically `m4_define' but it equips the macro with the
1220 # needed machinery for `m4_require'. A macro must be m4_defun'd if
1221 # either it is m4_require'd, or it m4_require's.
1222 #
1223 # Two things deserve attention and are detailed below:
1224 # 1. Implementation of m4_require
1225 # 2. Keeping track of the expansion stack
1226 #
1227 # 1. Implementation of m4_require
1228 # ===============================
1229 #
1230 # Of course m4_defun AC_PROVIDE's the macro, so that a macro which has
1231 # been expanded is not expanded again when m4_require'd, but the
1232 # difficult part is the proper expansion of macros when they are
1233 # m4_require'd.
1234 #
1235 # The implementation is based on two ideas, (i) using diversions to
1236 # prepare the expansion of the macro and its dependencies (by Franc,ois
1237 # Pinard), and (ii) expand the most recently m4_require'd macros _after_
1238 # the previous macros (by Axel Thimm).
1239 #
1240 #
1241 # The first idea: why use diversions?
1242 # -----------------------------------
1243 #
1244 # When a macro requires another, the other macro is expanded in new
1245 # diversion, GROW. When the outer macro is fully expanded, we first
1246 # undivert the most nested diversions (GROW - 1...), and finally
1247 # undivert GROW. To understand why we need several diversions,
1248 # consider the following example:
1249 #
1250 # | m4_defun([TEST1], [Test...REQUIRE([TEST2])1])
1251 # | m4_defun([TEST2], [Test...REQUIRE([TEST3])2])
1252 # | m4_defun([TEST3], [Test...3])
1253 #
1254 # Because m4_require is not required to be first in the outer macros, we
1255 # must keep the expansions of the various levels of m4_require separated.
1256 # Right before executing the epilogue of TEST1, we have:
1257 #
1258 # GROW - 2: Test...3
1259 # GROW - 1: Test...2
1260 # GROW: Test...1
1261 # BODY:
1262 #
1263 # Finally the epilogue of TEST1 undiverts GROW - 2, GROW - 1, and
1264 # GROW into the regular flow, BODY.
1265 #
1266 # GROW - 2:
1267 # GROW - 1:
1268 # GROW:
1269 # BODY: Test...3; Test...2; Test...1
1270 #
1271 # (The semicolons are here for clarification, but of course are not
1272 # emitted.) This is what Autoconf 2.0 (I think) to 2.13 (I'm sure)
1273 # implement.
1274 #
1275 #
1276 # The second idea: first required first out
1277 # -----------------------------------------
1278 #
1279 # The natural implementation of the idea above is buggy and produces
1280 # very surprising results in some situations. Let's consider the
1281 # following example to explain the bug:
1282 #
1283 # | m4_defun([TEST1], [REQUIRE([TEST2a])REQUIRE([TEST2b])])
1284 # | m4_defun([TEST2a], [])
1285 # | m4_defun([TEST2b], [REQUIRE([TEST3])])
1286 # | m4_defun([TEST3], [REQUIRE([TEST2a])])
1287 # |
1288 # | AC_INIT
1289 # | TEST1
1290 #
1291 # The dependencies between the macros are:
1292 #
1293 # 3 --- 2b
1294 # / \ is m4_require'd by
1295 # / \ left -------------------- right
1296 # 2a ------------ 1
1297 #
1298 # If you strictly apply the rules given in the previous section you get:
1299 #
1300 # GROW - 2: TEST3
1301 # GROW - 1: TEST2a; TEST2b
1302 # GROW: TEST1
1303 # BODY:
1304 #
1305 # (TEST2a, although required by TEST3 is not expanded in GROW - 3
1306 # because is has already been expanded before in GROW - 1, so it has
1307 # been AC_PROVIDE'd, so it is not expanded again) so when you undivert
1308 # the stack of diversions, you get:
1309 #
1310 # GROW - 2:
1311 # GROW - 1:
1312 # GROW:
1313 # BODY: TEST3; TEST2a; TEST2b; TEST1
1314 #
1315 # i.e., TEST2a is expanded after TEST3 although the latter required the
1316 # former.
1317 #
1318 # Starting from 2.50, we use an implementation provided by Axel Thimm.
1319 # The idea is simple: the order in which macros are emitted must be the
1320 # same as the one in which macros are expanded. (The bug above can
1321 # indeed be described as: a macro has been AC_PROVIDE'd before its
1322 # dependent, but it is emitted after: the lack of correlation between
1323 # emission and expansion order is guilty).
1324 #
1325 # How to do that? You keep the stack of diversions to elaborate the
1326 # macros, but each time a macro is fully expanded, emit it immediately.
1327 #
1328 # In the example above, when TEST2a is expanded, but it's epilogue is
1329 # not run yet, you have:
1330 #
1331 # GROW - 2:
1332 # GROW - 1: TEST2a
1333 # GROW: Elaboration of TEST1
1334 # BODY:
1335 #
1336 # The epilogue of TEST2a emits it immediately:
1337 #
1338 # GROW - 2:
1339 # GROW - 1:
1340 # GROW: Elaboration of TEST1
1341 # BODY: TEST2a
1342 #
1343 # TEST2b then requires TEST3, so right before the epilogue of TEST3, you
1344 # have:
1345 #
1346 # GROW - 2: TEST3
1347 # GROW - 1: Elaboration of TEST2b
1348 # GROW: Elaboration of TEST1
1349 # BODY: TEST2a
1350 #
1351 # The epilogue of TEST3 emits it:
1352 #
1353 # GROW - 2:
1354 # GROW - 1: Elaboration of TEST2b
1355 # GROW: Elaboration of TEST1
1356 # BODY: TEST2a; TEST3
1357 #
1358 # TEST2b is now completely expanded, and emitted:
1359 #
1360 # GROW - 2:
1361 # GROW - 1:
1362 # GROW: Elaboration of TEST1
1363 # BODY: TEST2a; TEST3; TEST2b
1364 #
1365 # and finally, TEST1 is finished and emitted:
1366 #
1367 # GROW - 2:
1368 # GROW - 1:
1369 # GROW:
1370 # BODY: TEST2a; TEST3; TEST2b: TEST1
1371 #
1372 # The idea is simple, but the implementation is a bit evolved. If you
1373 # are like me, you will want to see the actual functioning of this
1374 # implementation to be convinced. The next section gives the full
1375 # details.
1376 #
1377 #
1378 # The Axel Thimm implementation at work
1379 # -------------------------------------
1380 #
1381 # We consider the macros above, and this configure.ac:
1382 #
1383 # AC_INIT
1384 # TEST1
1385 #
1386 # You should keep the definitions of _m4_defun_pro, _m4_defun_epi, and
1387 # m4_require at hand to follow the steps.
1388 #
1389 # This implements tries not to assume that the current diversion is
1390 # BODY, so as soon as a macro (m4_defun'd) is expanded, we first
1391 # record the current diversion under the name _m4_divert_dump (denoted
1392 # DUMP below for short). This introduces an important difference with
1393 # the previous versions of Autoconf: you cannot use m4_require if you
1394 # are not inside an m4_defun'd macro, and especially, you cannot
1395 # m4_require directly from the top level.
1396 #
1397 # We have not tried to simulate the old behavior (better yet, we
1398 # diagnose it), because it is too dangerous: a macro m4_require'd from
1399 # the top level is expanded before the body of `configure', i.e., before
1400 # any other test was run. I let you imagine the result of requiring
1401 # AC_STDC_HEADERS for instance, before AC_PROG_CC was actually run....
1402 #
1403 # After AC_INIT was run, the current diversion is BODY.
1404 # * AC_INIT was run
1405 # DUMP: undefined
1406 # diversion stack: BODY |-
1407 #
1408 # * TEST1 is expanded
1409 # The prologue of TEST1 sets _m4_divert_dump, which is the diversion
1410 # where the current elaboration will be dumped, to the current
1411 # diversion. It also m4_divert_push to GROW, where the full
1412 # expansion of TEST1 and its dependencies will be elaborated.
1413 # DUMP: BODY
1414 # BODY: empty
1415 # diversions: GROW, BODY |-
1416 #
1417 # * TEST1 requires TEST2a
1418 # _m4_require_call m4_divert_pushes another temporary diversion,
1419 # GROW - 1, and expands TEST2a in there.
1420 # DUMP: BODY
1421 # BODY: empty
1422 # GROW - 1: TEST2a
1423 # diversions: GROW - 1, GROW, BODY |-
1424 # Than the content of the temporary diversion is moved to DUMP and the
1425 # temporary diversion is popped.
1426 # DUMP: BODY
1427 # BODY: TEST2a
1428 # diversions: GROW, BODY |-
1429 #
1430 # * TEST1 requires TEST2b
1431 # Again, _m4_require_call pushes GROW - 1 and heads to expand TEST2b.
1432 # DUMP: BODY
1433 # BODY: TEST2a
1434 # diversions: GROW - 1, GROW, BODY |-
1435 #
1436 # * TEST2b requires TEST3
1437 # _m4_require_call pushes GROW - 2 and expands TEST3 here.
1438 # (TEST3 requires TEST2a, but TEST2a has already been m4_provide'd, so
1439 # nothing happens.)
1440 # DUMP: BODY
1441 # BODY: TEST2a
1442 # GROW - 2: TEST3
1443 # diversions: GROW - 2, GROW - 1, GROW, BODY |-
1444 # Than the diversion is appended to DUMP, and popped.
1445 # DUMP: BODY
1446 # BODY: TEST2a; TEST3
1447 # diversions: GROW - 1, GROW, BODY |-
1448 #
1449 # * TEST1 requires TEST2b (contd.)
1450 # The content of TEST2b is expanded...
1451 # DUMP: BODY
1452 # BODY: TEST2a; TEST3
1453 # GROW - 1: TEST2b,
1454 # diversions: GROW - 1, GROW, BODY |-
1455 # ... and moved to DUMP.
1456 # DUMP: BODY
1457 # BODY: TEST2a; TEST3; TEST2b
1458 # diversions: GROW, BODY |-
1459 #
1460 # * TEST1 is expanded: epilogue
1461 # TEST1's own content is in GROW...
1462 # DUMP: BODY
1463 # BODY: TEST2a; TEST3; TEST2b
1464 # GROW: TEST1
1465 # diversions: BODY |-
1466 # ... and it's epilogue moves it to DUMP and then undefines DUMP.
1467 # DUMP: undefined
1468 # BODY: TEST2a; TEST3; TEST2b; TEST1
1469 # diversions: BODY |-
1470 #
1471 #
1472 # 2. Keeping track of the expansion stack
1473 # =======================================
1474 #
1475 # When M4 expansion goes wrong it is often extremely hard to find the
1476 # path amongst macros that drove to the failure. What is needed is
1477 # the stack of macro `calls'. One could imagine that GNU M4 would
1478 # maintain a stack of macro expansions, unfortunately it doesn't, so
1479 # we do it by hand. This is of course extremely costly, but the help
1480 # this stack provides is worth it. Nevertheless to limit the
1481 # performance penalty this is implemented only for m4_defun'd macros,
1482 # not for define'd macros.
1483 #
1484 # The scheme is simplistic: each time we enter an m4_defun'd macros,
1485 # we prepend its name in m4_expansion_stack, and when we exit the
1486 # macro, we remove it (thanks to pushdef/popdef).
1487 #
1488 # In addition, we want to detect circular m4_require dependencies.
1489 # Each time we expand a macro FOO we define _m4_expanding(FOO); and
1490 # m4_require(BAR) simply checks whether _m4_expanding(BAR) is defined.
1491
1492
1493 # m4_expansion_stack_push(TEXT)
1494 # -----------------------------
1495 m4_define([m4_expansion_stack_push],
1496 [m4_pushdef([m4_expansion_stack],
1497 [$1]m4_ifdef([m4_expansion_stack], [
1498 _m4_defn([m4_expansion_stack])]))])
1499
1500
1501 # m4_expansion_stack_pop
1502 # ----------------------
1503 m4_define([m4_expansion_stack_pop],
1504 [m4_popdef([m4_expansion_stack])])
1505
1506
1507 # m4_expansion_stack_dump
1508 # -----------------------
1509 # Dump the expansion stack.
1510 m4_define([m4_expansion_stack_dump],
1511 [m4_ifdef([m4_expansion_stack],
1512 [m4_errprintn(_m4_defn([m4_expansion_stack]))])dnl
1513 m4_errprintn(m4_location[: the top level])])
1514
1515
1516 # _m4_divert(GROW)
1517 # ----------------
1518 # This diversion is used by the m4_defun/m4_require machinery. It is
1519 # important to keep room before GROW because for each nested
1520 # AC_REQUIRE we use an additional diversion (i.e., two m4_require's
1521 # will use GROW - 2. More than 3 levels has never seemed to be
1522 # needed.)
1523 #
1524 # ...
1525 # - GROW - 2
1526 # m4_require'd code, 2 level deep
1527 # - GROW - 1
1528 # m4_require'd code, 1 level deep
1529 # - GROW
1530 # m4_defun'd macros are elaborated here.
1531
1532 m4_define([_m4_divert(GROW)], 10000)
1533
1534
1535 # _m4_defun_pro(MACRO-NAME)
1536 # -------------------------
1537 # The prologue for Autoconf macros.
1538 #
1539 # This is called frequently, so minimize the number of macro invocations
1540 # by avoiding dnl and m4_defn overhead.
1541 m4_define([_m4_defun_pro],
1542 m4_do([[m4_ifdef([m4_expansion_stack], [], [_m4_defun_pro_outer[]])]],
1543 [[m4_expansion_stack_push(_m4_defn(
1544 [m4_location($1)])[: $1 is expanded from...])]],
1545 [[m4_pushdef([_m4_expanding($1)])]]))
1546
1547 m4_define([_m4_defun_pro_outer],
1548 [m4_copy([_m4_divert_diversion], [_m4_divert_dump])m4_divert_push([GROW])])
1549
1550 # _m4_defun_epi(MACRO-NAME)
1551 # -------------------------
1552 # The Epilogue for Autoconf macros. MACRO-NAME only helps tracing
1553 # the PRO/EPI pairs.
1554 #
1555 # This is called frequently, so minimize the number of macro invocations
1556 # by avoiding dnl and m4_popdef overhead.
1557 m4_define([_m4_defun_epi],
1558 m4_do([[_m4_popdef([_m4_expanding($1)])]],
1559 [[m4_expansion_stack_pop()]],
1560 [[m4_ifdef([m4_expansion_stack], [], [_m4_defun_epi_outer[]])]],
1561 [[m4_provide([$1])]]))
1562
1563 m4_define([_m4_defun_epi_outer],
1564 [_m4_undefine([_m4_divert_dump])m4_divert_pop([GROW])m4_undivert([GROW])])
1565
1566
1567 # m4_defun(NAME, EXPANSION)
1568 # -------------------------
1569 # Define a macro which automatically provides itself. Add machinery
1570 # so the macro automatically switches expansion to the diversion
1571 # stack if it is not already using it. In this case, once finished,
1572 # it will bring back all the code accumulated in the diversion stack.
1573 # This, combined with m4_require, achieves the topological ordering of
1574 # macros. We don't use this macro to define some frequently called
1575 # macros that are not involved in ordering constraints, to save m4
1576 # processing.
1577 m4_define([m4_defun],
1578 [m4_define([m4_location($1)], m4_location)dnl
1579 m4_define([$1],
1580 [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])
1581
1582
1583 # m4_defun_once(NAME, EXPANSION)
1584 # ------------------------------
1585 # As m4_defun, but issues the EXPANSION only once, and warns if used
1586 # several times.
1587 m4_define([m4_defun_once],
1588 [m4_define([m4_location($1)], m4_location)dnl
1589 m4_define([$1],
1590 [m4_provide_if([$1],
1591 [m4_warn([syntax], [$1 invoked multiple times])],
1592 [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])])
1593
1594
1595 # m4_pattern_forbid(ERE, [WHY])
1596 # -----------------------------
1597 # Declare that no token matching the forbidden extended regular
1598 # expression ERE should be seen in the output unless...
1599 m4_define([m4_pattern_forbid], [])
1600
1601
1602 # m4_pattern_allow(ERE)
1603 # ---------------------
1604 # ... that token also matches the allowed extended regular expression ERE.
1605 # Both used via traces.
1606 m4_define([m4_pattern_allow], [])
1607
1608
1609 ## --------------------------------- ##
1610 ## 11. Dependencies between macros. ##
1611 ## --------------------------------- ##
1612
1613
1614 # m4_before(THIS-MACRO-NAME, CALLED-MACRO-NAME)
1615 # ---------------------------------------------
1616 # Issue a warning if CALLED-MACRO-NAME was called before THIS-MACRO-NAME.
1617 m4_define([m4_before],
1618 [m4_provide_if([$2],
1619 [m4_warn([syntax], [$2 was called before $1])])])
1620
1621
1622 # m4_require(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
1623 # -----------------------------------------------------------
1624 # If NAME-TO-CHECK has never been expanded (actually, if it is not
1625 # m4_provide'd), expand BODY-TO-EXPAND *before* the current macro
1626 # expansion. Once expanded, emit it in _m4_divert_dump. Keep track
1627 # of the m4_require chain in m4_expansion_stack.
1628 #
1629 # The normal cases are:
1630 #
1631 # - NAME-TO-CHECK == BODY-TO-EXPAND
1632 # Which you can use for regular macros with or without arguments, e.g.,
1633 # m4_require([AC_PROG_CC], [AC_PROG_CC])
1634 # m4_require([AC_CHECK_HEADERS(limits.h)], [AC_CHECK_HEADERS(limits.h)])
1635 # which is just the same as
1636 # m4_require([AC_PROG_CC])
1637 # m4_require([AC_CHECK_HEADERS(limits.h)])
1638 #
1639 # - BODY-TO-EXPAND == m4_indir([NAME-TO-CHECK])
1640 # In the case of macros with irregular names. For instance:
1641 # m4_require([AC_LANG_COMPILER(C)], [indir([AC_LANG_COMPILER(C)])])
1642 # which means `if the macro named `AC_LANG_COMPILER(C)' (the parens are
1643 # part of the name, it is not an argument) has not been run, then
1644 # call it.'
1645 # Had you used
1646 # m4_require([AC_LANG_COMPILER(C)], [AC_LANG_COMPILER(C)])
1647 # then m4_require would have tried to expand `AC_LANG_COMPILER(C)', i.e.,
1648 # call the macro `AC_LANG_COMPILER' with `C' as argument.
1649 #
1650 # You could argue that `AC_LANG_COMPILER', when it receives an argument
1651 # such as `C' should dispatch the call to `AC_LANG_COMPILER(C)'. But this
1652 # `extension' prevents `AC_LANG_COMPILER' from having actual arguments that
1653 # it passes to `AC_LANG_COMPILER(C)'.
1654 #
1655 # This is called frequently, so minimize the number of macro invocations
1656 # by avoiding dnl and other overhead on the common path.
1657 m4_define([m4_require],
1658 m4_do([[m4_ifdef([_m4_expanding($1)],
1659 [m4_fatal([$0: circular dependency of $1])])]],
1660 [[m4_ifdef([_m4_divert_dump], [],
1661 [m4_fatal([$0($1): cannot be used outside of an ]dnl
1662 m4_bmatch([$0], [^AC_], [[AC_DEFUN]], [[m4_defun]])['d macro])])]],
1663 [[m4_provide_if([$1],
1664 [],
1665 [_m4_require_call([$1], [$2])])]]))
1666
1667
1668 # _m4_require_call(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK])
1669 # -----------------------------------------------------------------
1670 # If m4_require decides to expand the body, it calls this macro.
1671 #
1672 # This is called frequently, so minimize the number of macro invocations
1673 # by avoiding dnl and other overhead on the common path.
1674 m4_define([_m4_require_call],
1675 m4_do([[m4_define([_m4_divert_grow], m4_decr(_m4_divert_grow))]],
1676 [[m4_divert_push(_m4_divert_grow)]],
1677 [[m4_default([$2], [$1])
1678 m4_provide_if([$1],
1679 [],
1680 [m4_warn([syntax],
1681 [$1 is m4_require'd but not m4_defun'd])])]],
1682 [[m4_divert(_m4_defn([_m4_divert_dump]))]],
1683 [[m4_undivert(_m4_divert_grow)]],
1684 [[m4_divert_pop(_m4_divert_grow)]],
1685 [[m4_define([_m4_divert_grow], m4_incr(_m4_divert_grow))]]))
1686
1687
1688 # _m4_divert_grow
1689 # ---------------
1690 # The counter for _m4_require_call.
1691 m4_define([_m4_divert_grow], _m4_divert([GROW]))
1692
1693
1694 # m4_expand_once(TEXT, [WITNESS = TEXT])
1695 # --------------------------------------
1696 # If TEXT has never been expanded, expand it *here*. Use WITNESS as
1697 # as a memory that TEXT has already been expanded.
1698 m4_define([m4_expand_once],
1699 [m4_provide_if(m4_ifval([$2], [[$2]], [[$1]]),
1700 [],
1701 [m4_provide(m4_ifval([$2], [[$2]], [[$1]]))[]$1])])
1702
1703
1704 # m4_provide(MACRO-NAME)
1705 # ----------------------
1706 m4_define([m4_provide],
1707 [m4_define([m4_provide($1)])])
1708
1709
1710 # m4_provide_if(MACRO-NAME, IF-PROVIDED, IF-NOT-PROVIDED)
1711 # -------------------------------------------------------
1712 # If MACRO-NAME is provided do IF-PROVIDED, else IF-NOT-PROVIDED.
1713 # The purpose of this macro is to provide the user with a means to
1714 # check macros which are provided without letting her know how the
1715 # information is coded.
1716 m4_define([m4_provide_if],
1717 [m4_ifdef([m4_provide($1)],
1718 [$2], [$3])])
1719
1720
1721 ## --------------------- ##
1722 ## 12. Text processing. ##
1723 ## --------------------- ##
1724
1725
1726 # m4_cr_letters
1727 # m4_cr_LETTERS
1728 # m4_cr_Letters
1729 # -------------
1730 m4_define([m4_cr_letters], [abcdefghijklmnopqrstuvwxyz])
1731 m4_define([m4_cr_LETTERS], [ABCDEFGHIJKLMNOPQRSTUVWXYZ])
1732 m4_define([m4_cr_Letters],
1733 m4_defn([m4_cr_letters])dnl
1734 m4_defn([m4_cr_LETTERS])dnl
1735 )
1736
1737
1738 # m4_cr_digits
1739 # ------------
1740 m4_define([m4_cr_digits], [0123456789])
1741
1742
1743 # m4_cr_alnum
1744 # -----------
1745 m4_define([m4_cr_alnum],
1746 m4_defn([m4_cr_Letters])dnl
1747 m4_defn([m4_cr_digits])dnl
1748 )
1749
1750
1751 # m4_cr_symbols1
1752 # m4_cr_symbols2
1753 # -------------------------------
1754 m4_define([m4_cr_symbols1],
1755 m4_defn([m4_cr_Letters])dnl
1756 _)
1757
1758 m4_define([m4_cr_symbols2],
1759 m4_defn([m4_cr_symbols1])dnl
1760 m4_defn([m4_cr_digits])dnl
1761 )
1762
1763 # m4_cr_all
1764 # ---------
1765 # The character range representing everything, with `-' as the last
1766 # character, since it is special to m4_translit. Use with care, because
1767 # it contains characters special to M4 (fortunately, both ASCII and EBCDIC
1768 # have [] in order, so m4_defn([m4_cr_all]) remains a valid string). It
1769 # also contains characters special to terminals, so it should never be
1770 # displayed in an error message. Also, attempts to map [ and ] to other
1771 # characters via m4_translit must deal with the fact that m4_translit does
1772 # not add quotes to the output.
1773 #
1774 # It is mainly useful in generating inverted character range maps, for use
1775 # in places where m4_translit is faster than an equivalent m4_bpatsubst;
1776 # the regex `[^a-z]' is equivalent to:
1777 # m4_translit(m4_dquote(m4_defn([m4_cr_all])), [a-z])
1778 m4_define([m4_cr_all],
1779 m4_translit(m4_dquote(m4_format(m4_dquote(m4_for(
1780 ,1,255,,[[%c]]))m4_for([i],1,255,,[,i]))), [-])-)
1781
1782
1783 # _m4_define_cr_not(CATEGORY)
1784 # ---------------------------
1785 # Define m4_cr_not_CATEGORY as the inverse of m4_cr_CATEGORY.
1786 m4_define([_m4_define_cr_not],
1787 [m4_define([m4_cr_not_$1],
1788 m4_translit(m4_dquote(m4_defn([m4_cr_all])),
1789 m4_defn([m4_cr_$1])))])
1790
1791
1792 # m4_cr_not_letters
1793 # m4_cr_not_LETTERS
1794 # m4_cr_not_Letters
1795 # m4_cr_not_digits
1796 # m4_cr_not_alnum
1797 # m4_cr_not_symbols1
1798 # m4_cr_not_symbols2
1799 # ------------------
1800 # Inverse character sets
1801 _m4_define_cr_not([letters])
1802 _m4_define_cr_not([LETTERS])
1803 _m4_define_cr_not([Letters])
1804 _m4_define_cr_not([digits])
1805 _m4_define_cr_not([alnum])
1806 _m4_define_cr_not([symbols1])
1807 _m4_define_cr_not([symbols2])
1808
1809
1810 # m4_newline
1811 # ----------
1812 # Expands to a newline. Exists for formatting reasons.
1813 m4_define([m4_newline], [
1814 ])
1815
1816
1817 # m4_re_escape(STRING)
1818 # --------------------
1819 # Escape RE active characters in STRING.
1820 m4_define([m4_re_escape],
1821 [m4_bpatsubst([$1],
1822 [[][*+.?\^$]], [\\\&])])
1823
1824
1825 # m4_re_string
1826 # ------------
1827 # Regexp for `[a-zA-Z_0-9]*'
1828 # m4_dquote provides literal [] for the character class.
1829 m4_define([m4_re_string],
1830 m4_dquote(m4_defn([m4_cr_symbols2]))dnl
1831 [*]dnl
1832 )
1833
1834
1835 # m4_re_word
1836 # ----------
1837 # Regexp for `[a-zA-Z_][a-zA-Z_0-9]*'
1838 m4_define([m4_re_word],
1839 m4_dquote(m4_defn([m4_cr_symbols1]))dnl
1840 m4_defn([m4_re_string])dnl
1841 )
1842
1843
1844 # m4_tolower(STRING)
1845 # m4_toupper(STRING)
1846 # ------------------
1847 # These macros convert STRING to lowercase or uppercase.
1848 #
1849 # Rather than expand the m4_defn each time, we inline them up front.
1850 m4_define([m4_tolower],
1851 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_LETTERS]))[,
1852 ]m4_dquote(m4_defn([m4_cr_letters]))[)])
1853 m4_define([m4_toupper],
1854 [m4_translit([$1], ]m4_dquote(m4_defn([m4_cr_letters]))[,
1855 ]m4_dquote(m4_defn([m4_cr_LETTERS]))[)])
1856
1857
1858 # m4_split(STRING, [REGEXP])
1859 # --------------------------
1860 #
1861 # Split STRING into an m4 list of quoted elements. The elements are
1862 # quoted with [ and ]. Beginning spaces and end spaces *are kept*.
1863 # Use m4_strip to remove them.
1864 #
1865 # REGEXP specifies where to split. Default is [\t ]+.
1866 #
1867 # If STRING is empty, the result is an empty list.
1868 #
1869 # Pay attention to the m4_changequotes. When m4 reads the definition of
1870 # m4_split, it still has quotes set to [ and ]. Luckily, these are matched
1871 # in the macro body, so the definition is stored correctly. Use the same
1872 # alternate quotes as m4_noquote; it must be unlikely to appear in $1.
1873 #
1874 # Also, notice that $1 is quoted twice, since we want the result to
1875 # be quoted. Then you should understand that the argument of
1876 # patsubst is -=<{(STRING)}>=- (i.e., with additional -=<{( and )}>=-).
1877 #
1878 # This macro is safe on active symbols, i.e.:
1879 # m4_define(active, ACTIVE)
1880 # m4_split([active active ])end
1881 # => [active], [active], []end
1882 #
1883 # Optimize on regex of ` ' (space), since m4_foreach_w already guarantees
1884 # that the list contains single space separators, and a common case is
1885 # splitting a single-element list. This macro is called frequently,
1886 # so avoid unnecessary dnl inside the definition.
1887 m4_define([m4_split],
1888 [m4_if([$1], [], [],
1889 [$2], [ ], [m4_if(m4_index([$1], [ ]), [-1], [[[$1]]], [_$0($@)])],
1890 [$2], [], [_$0([$1], [[ ]+])],
1891 [_$0($@)])])
1892
1893 m4_define([_m4_split],
1894 [m4_changequote([-=<{(],[)}>=-])]dnl
1895 [[m4_bpatsubst(-=<{(-=<{($1)}>=-)}>=-, -=<{($2)}>=-,
1896 -=<{(], [)}>=-)]m4_changequote([, ])])
1897
1898
1899
1900 # m4_flatten(STRING)
1901 # ------------------
1902 # If STRING contains end of lines, replace them with spaces. If there
1903 # are backslashed end of lines, remove them. This macro is safe with
1904 # active symbols.
1905 # m4_define(active, ACTIVE)
1906 # m4_flatten([active
1907 # act\
1908 # ive])end
1909 # => active activeend
1910 #
1911 # In m4, m4_bpatsubst is expensive, so first check for a newline.
1912 m4_define([m4_flatten],
1913 [m4_if(m4_index([$1], [
1914 ]), [-1], [[$1]],
1915 [m4_translit(m4_bpatsubst([[[$1]]], [\\
1916 ]), [
1917 ], [ ])])])
1918
1919
1920 # m4_strip(STRING)
1921 # ----------------
1922 # Expands into STRING with tabs and spaces singled out into a single
1923 # space, and removing leading and trailing spaces.
1924 #
1925 # This macro is robust to active symbols.
1926 # m4_define(active, ACTIVE)
1927 # m4_strip([ active <tab> <tab>active ])end
1928 # => active activeend
1929 #
1930 # First, notice that we guarantee trailing space. Why? Because regular
1931 # expressions are greedy, and `.* ?' would always group the space into the
1932 # .* portion. The algorithm is simpler by avoiding `?' at the end. The
1933 # algorithm correctly strips everything if STRING is just ` '.
1934 #
1935 # Then notice the second pattern: it is in charge of removing the
1936 # leading/trailing spaces. Why not just `[^ ]'? Because they are
1937 # applied to over-quoted strings, i.e. more or less [STRING], due
1938 # to the limitations of m4_bpatsubsts. So the leading space in STRING
1939 # is the *second* character; equally for the trailing space.
1940 m4_define([m4_strip],
1941 [m4_bpatsubsts([$1 ],
1942 [[ ]+], [ ],
1943 [^. ?\(.*\) .$], [[[\1]]])])
1944
1945
1946 # m4_normalize(STRING)
1947 # --------------------
1948 # Apply m4_flatten and m4_strip to STRING.
1949 #
1950 # The argument is quoted, so that the macro is robust to active symbols:
1951 #
1952 # m4_define(active, ACTIVE)
1953 # m4_normalize([ act\
1954 # ive
1955 # active ])end
1956 # => active activeend
1957
1958 m4_define([m4_normalize],
1959 [m4_strip(m4_flatten([$1]))])
1960
1961
1962
1963 # m4_join(SEP, ARG1, ARG2...)
1964 # ---------------------------
1965 # Produce ARG1SEPARG2...SEPARGn. Avoid back-to-back SEP when a given ARG
1966 # is the empty string. No expansion is performed on SEP or ARGs.
1967 #
1968 # Since the number of arguments to join can be arbitrarily long, we
1969 # want to avoid having more than one $@ in the macro definition;
1970 # otherwise, the expansion would require twice the memory of the already
1971 # long list. Hence, m4_join merely looks for the first non-empty element,
1972 # and outputs just that element; while _m4_join looks for all non-empty
1973 # elements, and outputs them following a separator. The final trick to
1974 # note is that we decide between recursing with $0 or _$0 based on the
1975 # nested m4_if ending with `_'.
1976 #
1977 # Please keep foreach.m4 in sync with any adjustments made here.
1978 m4_define([m4_join],
1979 [m4_if([$#], [1], [],
1980 [$#], [2], [[$2]],
1981 [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift2($@))])])
1982 m4_define([_m4_join],
1983 [m4_if([$#$2], [2], [],
1984 [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift2($@))])])
1985
1986 # m4_joinall(SEP, ARG1, ARG2...)
1987 # ------------------------------
1988 # Produce ARG1SEPARG2...SEPARGn. An empty ARG results in back-to-back SEP.
1989 # No expansion is performed on SEP or ARGs.
1990 #
1991 # Please keep foreach.m4 in sync with any adjustments made here.
1992 m4_define([m4_joinall], [[$2]_$0([$1], m4_shift($@))])
1993 m4_define([_m4_joinall],
1994 [m4_if([$#], [2], [], [[$1$3]$0([$1], m4_shift2($@))])])
1995
1996 # m4_combine([SEPARATOR], PREFIX-LIST, [INFIX], SUFFIX...)
1997 # --------------------------------------------------------
1998 # Produce the pairwise combination of every element in the quoted,
1999 # comma-separated PREFIX-LIST with every element from the SUFFIX arguments.
2000 # Each pair is joined with INFIX, and pairs are separated by SEPARATOR.
2001 # No expansion occurs on SEPARATOR, INFIX, or elements of either list.
2002 #
2003 # For example:
2004 # m4_combine([, ], [[a], [b], [c]], [-], [1], [2], [3])
2005 # => a-1, a-2, a-3, b-1, b-2, b-3, c-1, c-2, c-3
2006 #
2007 # In order to have the correct number of SEPARATORs, we use a temporary
2008 # variable that redefines itself after the first use. We must use defn
2009 # rather than overquoting in case PREFIX or SUFFIX contains $1, but use
2010 # _m4_defn for speed. Likewise, we compute the m4_shift3 only once,
2011 # rather than in each iteration of the outer m4_foreach.
2012 m4_define([m4_combine],
2013 [m4_if(m4_eval([$# > 3]), [1],
2014 [m4_pushdef([m4_Separator], [m4_define([m4_Separator],
2015 _m4_defn([m4_echo]))])]]dnl
2016 [[m4_foreach([m4_Prefix], [$2],
2017 [m4_foreach([m4_Suffix], ]m4_dquote(m4_dquote(m4_shift3($@)))[,
2018 [m4_Separator([$1])[]_m4_defn([m4_Prefix])[$3]_m4_defn(
2019 [m4_Suffix])])])]]dnl
2020 [[_m4_popdef([m4_Separator])])])
2021
2022
2023 # m4_append(MACRO-NAME, STRING, [SEPARATOR])
2024 # ------------------------------------------
2025 # Redefine MACRO-NAME to hold its former content plus `SEPARATOR`'STRING'
2026 # at the end. It is valid to use this macro with MACRO-NAME undefined,
2027 # in which case no SEPARATOR is added. Be aware that the criterion is
2028 # `not being defined', and not `not being empty'.
2029 #
2030 # Note that neither STRING nor SEPARATOR are expanded here; rather, when
2031 # you expand MACRO-NAME, they will be expanded at that point in time.
2032 #
2033 # This macro is robust to active symbols. It can be used to grow
2034 # strings.
2035 #
2036 # | m4_define(active, ACTIVE)dnl
2037 # | m4_append([sentence], [This is an])dnl
2038 # | m4_append([sentence], [ active ])dnl
2039 # | m4_append([sentence], [symbol.])dnl
2040 # | sentence
2041 # | m4_undefine([active])dnl
2042 # | sentence
2043 # => This is an ACTIVE symbol.
2044 # => This is an active symbol.
2045 #
2046 # It can be used to define hooks.
2047 #
2048 # | m4_define(active, ACTIVE)dnl
2049 # | m4_append([hooks], [m4_define([act1], [act2])])dnl
2050 # | m4_append([hooks], [m4_define([act2], [active])])dnl
2051 # | m4_undefine([active])dnl
2052 # | act1
2053 # | hooks
2054 # | act1
2055 # => act1
2056 # =>
2057 # => active
2058 #
2059 # It can also be used to create lists, although this particular usage was
2060 # broken prior to autoconf 2.62.
2061 # | m4_append([list], [one], [, ])dnl
2062 # | m4_append([list], [two], [, ])dnl
2063 # | m4_append([list], [three], [, ])dnl
2064 # | list
2065 # | m4_dquote(list)
2066 # => one, two, three
2067 # => [one],[two],[three]
2068 #
2069 # Note that m4_append can benefit from amortized O(n) m4 behavior, if
2070 # the underlying m4 implementation is smart enough to avoid copying existing
2071 # contents when enlarging a macro's definition into any pre-allocated storage
2072 # (m4 1.4.x unfortunately does not implement this optimization). We do
2073 # not implement m4_prepend, since it is inherently O(n^2) (pre-allocated
2074 # storage only occurs at the end of a macro, so the existing contents must
2075 # always be moved).
2076 #
2077 # Use _m4_defn for speed.
2078 m4_define([m4_append],
2079 [m4_define([$1], m4_ifdef([$1], [_m4_defn([$1])[$3]])[$2])])
2080
2081
2082 # m4_append_uniq(MACRO-NAME, STRING, [SEPARATOR], [IF-UNIQ], [IF-DUP])
2083 # --------------------------------------------------------------------
2084 # Like `m4_append', but append only if not yet present. Additionally,
2085 # expand IF-UNIQ if STRING was appended, or IF-DUP if STRING was already
2086 # present. Also, warn if SEPARATOR is not empty and occurs within STRING,
2087 # as the algorithm no longer guarantees uniqueness.
2088 #
2089 # Note that while m4_append can be O(n) (depending on the quality of the
2090 # underlying M4 implementation), m4_append_uniq is inherently O(n^2)
2091 # because each append operation searches the entire string.
2092 m4_define([m4_append_uniq],
2093 [m4_ifval([$3], [m4_if(m4_index([$2], [$3]), [-1], [],
2094 [m4_warn([syntax],
2095 [$0: `$2' contains `$3'])])])_$0($@)])
2096 m4_define([_m4_append_uniq],
2097 [m4_ifdef([$1],
2098 [m4_if(m4_index([$3]_m4_defn([$1])[$3], [$3$2$3]), [-1],
2099 [m4_append([$1], [$2], [$3])$4], [$5])],
2100 [m4_define([$1], [$2])$4])])
2101
2102 # m4_append_uniq_w(MACRO-NAME, STRINGS)
2103 # -------------------------------------
2104 # For each of the words in the whitespace separated list STRINGS, append
2105 # only the unique strings to the definition of MACRO-NAME.
2106 #
2107 # Use _m4_defn for speed.
2108 m4_define([m4_append_uniq_w],
2109 [m4_foreach_w([m4_Word], [$2],
2110 [_m4_append_uniq([$1], _m4_defn([m4_Word]), [ ])])])
2111
2112
2113 # m4_text_wrap(STRING, [PREFIX], [FIRST-PREFIX], [WIDTH])
2114 # -------------------------------------------------------
2115 # Expands into STRING wrapped to hold in WIDTH columns (default = 79).
2116 # If PREFIX is given, each line is prefixed with it. If FIRST-PREFIX is
2117 # specified, then the first line is prefixed with it. As a special case,
2118 # if the length of FIRST-PREFIX is greater than that of PREFIX, then
2119 # FIRST-PREFIX will be left alone on the first line.
2120 #
2121 # No expansion occurs on the contents STRING, PREFIX, or FIRST-PREFIX,
2122 # although quadrigraphs are correctly recognized.
2123 #
2124 # Typical outputs are:
2125 #
2126 # m4_text_wrap([Short string */], [ ], [/* ], 20)
2127 # => /* Short string */
2128 #
2129 # m4_text_wrap([Much longer string */], [ ], [/* ], 20)
2130 # => /* Much longer
2131 # => string */
2132 #
2133 # m4_text_wrap([Short doc.], [ ], [ --short ], 30)
2134 # => --short Short doc.
2135 #
2136 # m4_text_wrap([Short doc.], [ ], [ --too-wide ], 30)
2137 # => --too-wide
2138 # => Short doc.
2139 #
2140 # m4_text_wrap([Super long documentation.], [ ], [ --too-wide ], 30)
2141 # => --too-wide
2142 # => Super long
2143 # => documentation.
2144 #
2145 # FIXME: there is no checking of a longer PREFIX than WIDTH, but do
2146 # we really want to bother with people trying each single corner
2147 # of a software?
2148 #
2149 # This macro does not leave a trailing space behind the last word of a line,
2150 # which complicates it a bit. The algorithm is otherwise stupid and simple:
2151 # all the words are preceded by m4_Separator which is defined to empty for
2152 # the first word, and then ` ' (single space) for all the others.
2153 #
2154 # The algorithm uses a helper that uses $2 through $4 directly, rather than
2155 # using local variables, to avoid m4_defn overhead, or expansion swallowing
2156 # any $. It also bypasses m4_popdef overhead with _m4_popdef since no user
2157 # macro expansion occurs in the meantime. Also, the definition is written
2158 # with m4_do, to avoid time wasted on dnl during expansion (since this is
2159 # already a time-consuming macro).
2160 m4_define([m4_text_wrap],
2161 [_$0([$1], [$2], m4_if([$3], [], [[$2]], [[$3]]),
2162 m4_if([$4], [], [79], [[$4]]))])
2163 m4_define([_m4_text_wrap],
2164 m4_do(dnl set up local variables, to avoid repeated calculations
2165 [[m4_pushdef([m4_Indent], m4_qlen([$2]))]],
2166 [[m4_pushdef([m4_Cursor], m4_qlen([$3]))]],
2167 [[m4_pushdef([m4_Separator], [m4_define([m4_Separator], [ ])])]],
2168 dnl expand the first prefix, then check its length vs. regular prefix
2169 dnl same length: nothing special
2170 dnl prefix1 longer: output on line by itself, and reset cursor
2171 dnl prefix1 shorter: pad to length of prefix, and reset cursor
2172 [[[$3]m4_cond([m4_Cursor], m4_Indent, [],
2173 [m4_eval(m4_Cursor > m4_Indent)], [1], [
2174 [$2]m4_define([m4_Cursor], m4_Indent)],
2175 [m4_format([%*s], m4_max([0],
2176 m4_eval(m4_Indent - m4_Cursor)), [])m4_define([m4_Cursor], m4_Indent)])]],
2177 dnl now, for each word, compute the curser after the word is output, then
2178 dnl check if the cursor would exceed the wrap column
2179 dnl if so, reset cursor, and insert newline and prefix
2180 dnl if not, insert the separator (usually a space)
2181 dnl either way, insert the word
2182 [[m4_foreach_w([m4_Word], [$1],
2183 [m4_define([m4_Cursor],
2184 m4_eval(m4_Cursor + m4_qlen(_m4_defn([m4_Word]))
2185 + 1))m4_if(m4_eval(m4_Cursor > ([$4])),
2186 [1], [m4_define([m4_Cursor],
2187 m4_eval(m4_Indent + m4_qlen(_m4_defn([m4_Word])) + 1))
2188 [$2]],
2189 [m4_Separator[]])_m4_defn([m4_Word])])]],
2190 dnl finally, clean up the local variabls
2191 [[_m4_popdef([m4_Separator], [m4_Cursor], [m4_Indent])]]))
2192
2193
2194 # m4_text_box(MESSAGE, [FRAME-CHARACTER = `-'])
2195 # ---------------------------------------------
2196 # Turn MESSAGE into:
2197 # ## ------- ##
2198 # ## MESSAGE ##
2199 # ## ------- ##
2200 # using FRAME-CHARACTER in the border.
2201 m4_define([m4_text_box],
2202 [m4_pushdef([m4_Border],
2203 m4_translit(m4_format([%*s], m4_qlen(m4_expand([$1])), []),
2204 [ ], m4_if([$2], [], [[-]], [[$2]])))dnl
2205 @%:@@%:@ m4_Border @%:@@%:@
2206 @%:@@%:@ $1 @%:@@%:@
2207 @%:@@%:@ m4_Border @%:@@%:@_m4_popdef([m4_Border])dnl
2208 ])
2209
2210
2211 # m4_qlen(STRING)
2212 # ---------------
2213 # Expands to the length of STRING after autom4te converts all quadrigraphs.
2214 #
2215 # Avoid bpatsubsts for the common case of no quadrigraphs.
2216 m4_define([m4_qlen],
2217 [m4_if(m4_index([$1], [@]), [-1], [m4_len([$1])],
2218 [m4_len(m4_bpatsubst([[$1]],
2219 [@\(\(<:\|:>\|S|\|%:\|\{:\|:\}\)\(@\)\|&t@\)],
2220 [\3]))])])
2221
2222
2223 # m4_qdelta(STRING)
2224 # -----------------
2225 # Expands to the net change in the length of STRING from autom4te converting the
2226 # quadrigraphs in STRING. This number is always negative or zero.
2227 m4_define([m4_qdelta],
2228 [m4_eval(m4_qlen([$1]) - m4_len([$1]))])
2229
2230
2231
2232 ## ----------------------- ##
2233 ## 13. Number processing. ##
2234 ## ----------------------- ##
2235
2236 # m4_cmp(A, B)
2237 # ------------
2238 # Compare two integer expressions.
2239 # A < B -> -1
2240 # A = B -> 0
2241 # A > B -> 1
2242 m4_define([m4_cmp],
2243 [m4_eval((([$1]) > ([$2])) - (([$1]) < ([$2])))])
2244
2245
2246 # m4_list_cmp(A, B)
2247 # -----------------
2248 #
2249 # Compare the two lists of integer expressions A and B. For instance:
2250 # m4_list_cmp([1, 0], [1]) -> 0
2251 # m4_list_cmp([1, 0], [1, 0]) -> 0
2252 # m4_list_cmp([1, 2], [1, 0]) -> 1
2253 # m4_list_cmp([1, 2, 3], [1, 2]) -> 1
2254 # m4_list_cmp([1, 2, -3], [1, 2]) -> -1
2255 # m4_list_cmp([1, 0], [1, 2]) -> -1
2256 # m4_list_cmp([1], [1, 2]) -> -1
2257 # m4_define([xa], [oops])dnl
2258 # m4_list_cmp([[0xa]], [5+5]) -> 0
2259 #
2260 # Rather than face the overhead of m4_case, we use a helper function whose
2261 # expansion includes the name of the macro to invoke on the tail, either
2262 # m4_ignore or m4_unquote. This is particularly useful when comparing
2263 # long lists, since less text is being expanded for deciding when to end
2264 # recursion. The recursion is between a pair of macros that alternate
2265 # which list is trimmed by one element; this is more efficient than
2266 # calling m4_cdr on both lists from a single macro. Guarantee exactly
2267 # one expansion of both lists' side effects.
2268 #
2269 # Please keep foreach.m4 in sync with any adjustments made here.
2270 m4_define([m4_list_cmp],
2271 [_$0_raw(m4_dquote($1), m4_dquote($2))])
2272
2273 m4_define([_m4_list_cmp_raw],
2274 [m4_if([$1], [$2], [0], [_m4_list_cmp_1([$1], $2)])])
2275
2276 m4_define([_m4_list_cmp],
2277 [m4_if([$1], [], [0m4_ignore], [$2], [0], [m4_unquote], [$2m4_ignore])])
2278
2279 m4_define([_m4_list_cmp_1],
2280 [_m4_list_cmp_2([$2], [m4_shift2($@)], $1)])
2281
2282 m4_define([_m4_list_cmp_2],
2283 [_m4_list_cmp([$1$3], m4_cmp([$3+0], [$1+0]))(
2284 [_m4_list_cmp_1(m4_dquote(m4_shift3($@)), $2)])])
2285
2286 # m4_max(EXPR, ...)
2287 # m4_min(EXPR, ...)
2288 # -----------------
2289 # Return the decimal value of the maximum (or minimum) in a series of
2290 # integer expressions.
2291 #
2292 # M4 1.4.x doesn't provide ?:. Hence this huge m4_eval. Avoid m4_eval
2293 # if both arguments are identical, but be aware of m4_max(0xa, 10) (hence
2294 # the use of <=, not just <, in the second multiply).
2295 #
2296 # Please keep foreach.m4 in sync with any adjustments made here.
2297 m4_define([m4_max],
2298 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
2299 [$#], [1], [m4_eval([$1])],
2300 [$#$1], [2$2], [m4_eval([$1])],
2301 [$#], [2], [_$0($@)],
2302 [_m4_minmax([_$0], $@)])])
2303
2304 m4_define([_m4_max],
2305 [m4_eval((([$1]) > ([$2])) * ([$1]) + (([$1]) <= ([$2])) * ([$2]))])
2306
2307 m4_define([m4_min],
2308 [m4_if([$#], [0], [m4_fatal([too few arguments to $0])],
2309 [$#], [1], [m4_eval([$1])],
2310 [$#$1], [2$2], [m4_eval([$1])],
2311 [$#], [2], [_$0($@)],
2312 [_m4_minmax([_$0], $@)])])
2313
2314 m4_define([_m4_min],
2315 [m4_eval((([$1]) < ([$2])) * ([$1]) + (([$1]) >= ([$2])) * ([$2]))])
2316
2317 # _m4_minmax(METHOD, ARG1, ARG2...)
2318 # ---------------------------------
2319 # Common recursion code for m4_max and m4_min. METHOD must be _m4_max
2320 # or _m4_min, and there must be at least two arguments to combine.
2321 #
2322 # Please keep foreach.m4 in sync with any adjustments made here.
2323 m4_define([_m4_minmax],
2324 [m4_if([$#], [3], [$1([$2], [$3])],
2325 [$0([$1], $1([$2], [$3]), m4_shift3($@))])])
2326
2327
2328 # m4_sign(A)
2329 # ----------
2330 # The sign of the integer expression A.
2331 m4_define([m4_sign],
2332 [m4_eval((([$1]) > 0) - (([$1]) < 0))])
2333
2334
2335
2336 ## ------------------------ ##
2337 ## 14. Version processing. ##
2338 ## ------------------------ ##
2339
2340
2341 # m4_version_unletter(VERSION)
2342 # ----------------------------
2343 # Normalize beta version numbers with letters to numeric expressions, which
2344 # can then be handed to m4_eval for the purpose of comparison.
2345 #
2346 # Nl -> (N+1).-1.(l#)
2347 #
2348 # for example:
2349 # [2.14a] -> [2.14+1.-1.[0r36:a]] -> 2.15.-1.10
2350 # [2.14b] -> [2.15+1.-1.[0r36:b]] -> 2.15.-1.11
2351 # [2.61aa.b] -> [2.61+1.-1.[0r36:aa],+1.-1.[0r36:b]] -> 2.62.-1.370.1.-1.11
2352 #
2353 # This macro expects reasonable version numbers, but can handle double
2354 # letters and does not expand any macros. Original version strings can
2355 # use both `.' and `-' separators.
2356 #
2357 # Inline constant expansions, to avoid m4_defn overhead.
2358 # _m4_version_unletter is the real workhorse used by m4_version_compare,
2359 # but since [0r36:a] is less readable than 10, we provide a wrapper for
2360 # human use.
2361 m4_define([m4_version_unletter],
2362 [m4_map_sep([m4_eval], [.],
2363 m4_dquote(m4_dquote_elt(m4_unquote(_$0([$1])))))])
2364 m4_define([_m4_version_unletter],
2365 [m4_bpatsubst(m4_translit([[[$1]]], [.-], [,,]),]dnl
2366 m4_dquote(m4_dquote(m4_defn([m4_cr_Letters])))[[+],
2367 [+1,-1,[0r36:\&]])])
2368
2369
2370 # m4_version_compare(VERSION-1, VERSION-2)
2371 # ----------------------------------------
2372 # Compare the two version numbers and expand into
2373 # -1 if VERSION-1 < VERSION-2
2374 # 0 if =
2375 # 1 if >
2376 #
2377 # Since _m4_version_unletter does not output side effects, we can
2378 # safely bypass the overhead of m4_version_cmp.
2379 m4_define([m4_version_compare],
2380 [_m4_list_cmp_raw(_m4_version_unletter([$1]), _m4_version_unletter([$2]))])
2381
2382
2383 # m4_PACKAGE_NAME
2384 # m4_PACKAGE_TARNAME
2385 # m4_PACKAGE_VERSION
2386 # m4_PACKAGE_STRING
2387 # m4_PACKAGE_BUGREPORT
2388 # --------------------
2389 # If m4sugar/version.m4 is present, then define version strings. This
2390 # file is optional, provided by Autoconf but absent in Bison.
2391 m4_sinclude([m4sugar/version.m4])
2392
2393
2394 # m4_version_prereq(VERSION, [IF-OK], [IF-NOT = FAIL])
2395 # ----------------------------------------------------
2396 # Check this Autoconf version against VERSION.
2397 m4_define([m4_version_prereq],
2398 m4_ifdef([m4_PACKAGE_VERSION],
2399 [[m4_if(m4_version_compare(]m4_dquote(m4_defn([m4_PACKAGE_VERSION]))[, [$1]),
2400 [-1],
2401 [m4_default([$3],
2402 [m4_fatal([Autoconf version $1 or higher is required],
2403 [63])])],
2404 [$2])]],
2405 [[m4_fatal([m4sugar/version.m4 not found])]]))
2406
2407
2408 ## ------------------ ##
2409 ## 15. Set handling. ##
2410 ## ------------------ ##
2411
2412 # Autoconf likes to create arbitrarily large sets; for example, as of
2413 # this writing, the configure.ac for coreutils tracks a set of more
2414 # than 400 AC_SUBST. How do we track all of these set members,
2415 # without introducing duplicates? We could use m4_append_uniq, with
2416 # the set NAME residing in the contents of the macro NAME.
2417 # Unfortunately, m4_append_uniq is quadratic for set creation, because
2418 # it costs O(n) to search the string for each of O(n) insertions; not
2419 # to mention that with m4 1.4.x, even using m4_append is slow, costing
2420 # O(n) rather than O(1) per insertion. Other set operations, not used
2421 # by Autoconf but still possible by manipulation of the definition
2422 # tracked in macro NAME, include O(n) deletion of one element and O(n)
2423 # computation of set size. Because the set is exposed to the user via
2424 # the definition of a single macro, we cannot cache any data about the
2425 # set without risking the cache being invalidated by the user
2426 # redefining NAME.
2427 #
2428 # Can we do better? Yes, because m4 gives us an O(1) search function
2429 # for free: ifdef. Additionally, even m4 1.4.x gives us an O(1)
2430 # insert operation for free: pushdef. But to use these, we must
2431 # represent the set via a group of macros; to keep the set consistent,
2432 # we must hide the set so that the user can only manipulate it through
2433 # accessor macros. The contents of the set are maintained through two
2434 # access points; _m4_set([name]) is a pushdef stack of values in the
2435 # set, useful for O(n) traversal of the set contents; while the
2436 # existence of _m4_set([name],value) with no particular value is
2437 # useful for O(1) querying of set membership. And since the user
2438 # cannot externally manipulate the set, we are free to add additional
2439 # caching macros for other performance improvements. Deletion can be
2440 # O(1) per element rather than O(n), by reworking the definition of
2441 # _m4_set([name],value) to be 0 or 1 based on current membership, and
2442 # adding _m4_set_cleanup(name) to defer the O(n) cleanup of
2443 # _m4_set([name]) until we have another reason to do an O(n)
2444 # traversal. The existence of _m4_set_cleanup(name) can then be used
2445 # elsewhere to determine if we must dereference _m4_set([name],value),
2446 # or assume that definition implies set membership. Finally, size can
2447 # be tracked in an O(1) fashion with _m4_set_size(name).
2448 #
2449 # The quoting in _m4_set([name],value) is chosen so that there is no
2450 # ambiguity with a set whose name contains a comma, and so that we can
2451 # supply the value via _m4_defn([_m4_set([name])]) without needing any
2452 # quote manipulation.
2453
2454 # m4_set_add(SET, VALUE, [IF-UNIQ], [IF-DUP])
2455 # -------------------------------------------
2456 # Add VALUE as an element of SET. Expand IF-UNIQ on the first
2457 # addition, and IF-DUP if it is already in the set. Addition of one
2458 # element is O(1), such that overall set creation is O(n).
2459 #
2460 # We do not want to add a duplicate for a previously deleted but
2461 # unpruned element, but it is just as easy to check existence directly
2462 # as it is to query _m4_set_cleanup($1).
2463 m4_define([m4_set_add],
2464 [m4_ifdef([_m4_set([$1],$2)],
2465 [m4_if(m4_indir([_m4_set([$1],$2)]), [0],
2466 [m4_define([_m4_set([$1],$2)],
2467 [1])_m4_set_size([$1], [m4_incr])$3], [$4])],
2468 [m4_define([_m4_set([$1],$2)],
2469 [1])m4_pushdef([_m4_set([$1])],
2470 [$2])_m4_set_size([$1], [m4_incr])$3])])
2471
2472 # m4_set_add_all(SET, VALUE...)
2473 # -----------------------------
2474 # Add each VALUE into SET. This is O(n) in the number of VALUEs, and
2475 # can be faster than calling m4_set_add for each VALUE.
2476 #
2477 # Implement two recursion helpers; the check variant is slower but
2478 # handles the case where an element has previously been removed but
2479 # not pruned. The recursion helpers ignore their second argument, so
2480 # that we can use the faster m4_shift2 and 2 arguments, rather than
2481 # _m4_shift2 and one argument, as the signal to end recursion.
2482 #
2483 # Please keep foreach.m4 in sync with any adjustments made here.
2484 m4_define([m4_set_add_all],
2485 [m4_define([_m4_set_size($1)], m4_eval(m4_set_size([$1])
2486 + m4_len(m4_ifdef([_m4_set_cleanup($1)], [_$0_check], [_$0])([$1], $@))))])
2487
2488 m4_define([_m4_set_add_all],
2489 [m4_if([$#], [2], [],
2490 [m4_ifdef([_m4_set([$1],$3)], [],
2491 [m4_define([_m4_set([$1],$3)], [1])m4_pushdef([_m4_set([$1])],
2492 [$3])-])$0([$1], m4_shift2($@))])])
2493
2494 m4_define([_m4_set_add_all_check],
2495 [m4_if([$#], [2], [],
2496 [m4_set_add([$1], [$3])$0([$1], m4_shift2($@))])])
2497
2498 # m4_set_contains(SET, VALUE, [IF-PRESENT], [IF-ABSENT])
2499 # ------------------------------------------------------
2500 # Expand IF-PRESENT if SET contains VALUE, otherwise expand IF-ABSENT.
2501 # This is always O(1).
2502 m4_define([m4_set_contains],
2503 [m4_ifdef([_m4_set_cleanup($1)],
2504 [m4_if(m4_ifdef([_m4_set([$1],$2)],
2505 [m4_indir([_m4_set([$1],$2)])], [0]), [1], [$3], [$4])],
2506 [m4_ifdef([_m4_set([$1],$2)], [$3], [$4])])])
2507
2508 # m4_set_contents(SET, [SEP])
2509 # ---------------------------
2510 # Expand to a single string containing all the elements in SET,
2511 # separated by SEP, without modifying SET. No provision is made for
2512 # disambiguating set elements that contain non-empty SEP as a
2513 # sub-string, or for recognizing a set that contains only the empty
2514 # string. Order of the output is not guaranteed. If any elements
2515 # have been previously removed from the set, this action will prune
2516 # the unused memory. This is O(n) in the size of the set before
2517 # pruning.
2518 #
2519 # Use _m4_popdef for speed. The existence of _m4_set_cleanup($1)
2520 # determines which version of _1 helper we use.
2521 m4_define([m4_set_contents],
2522 [m4_ifdef([_m4_set_cleanup($1)], [_$0_1c], [_$0_1])([$1])_$0_2([$1],
2523 [_m4_defn([_m4_set_($1)])], [[$2]])])
2524
2525 # _m4_set_contents_1(SET)
2526 # _m4_set_contents_1c(SET)
2527 # _m4_set_contents_2(SET, SEP, PREP)
2528 # ----------------------------------
2529 # Expand to a list of quoted elements currently in the set, separated
2530 # by SEP, and moving PREP in front of SEP on recursion. To avoid
2531 # nesting limit restrictions, the algorithm must be broken into two
2532 # parts; _1 destructively copies the stack in reverse into
2533 # _m4_set_($1), producing no output; then _2 destructively copies
2534 # _m4_set_($1) back into the stack in reverse. SEP is expanded while
2535 # _m4_set_($1) contains the current element, so a SEP containing
2536 # _m4_defn([_m4_set_($1)]) can produce output in the order the set was
2537 # created. Behavior is undefined if SEP tries to recursively list or
2538 # modify SET in any way other than calling m4_set_remove on the
2539 # current element. Use _1 if all entries in the stack are guaranteed
2540 # to be in the set, and _1c to prune removed entries. Uses _m4_defn
2541 # and _m4_popdef for speed.
2542 m4_define([_m4_set_contents_1],
2543 [m4_ifdef([_m4_set([$1])], [m4_pushdef([_m4_set_($1)],
2544 _m4_defn([_m4_set([$1])]))_m4_popdef([_m4_set([$1])])$0([$1])])])
2545
2546 m4_define([_m4_set_contents_1c],
2547 [m4_ifdef([_m4_set([$1])],
2548 [m4_set_contains([$1], _m4_defn([_m4_set([$1])]),
2549 [m4_pushdef([_m4_set_($1)], _m4_defn([_m4_set([$1])]))],
2550 [_m4_popdef([_m4_set([$1],]_m4_defn(
2551 [_m4_set([$1])])[)])])_m4_popdef([_m4_set([$1])])$0([$1])],
2552 [_m4_popdef([_m4_set_cleanup($1)])])])
2553
2554 m4_define([_m4_set_contents_2],
2555 [m4_ifdef([_m4_set_($1)], [m4_pushdef([_m4_set([$1])],
2556 _m4_defn([_m4_set_($1)]))$2[]_m4_popdef([_m4_set_($1)])$0([$1], [$3$2])])])
2557
2558 # m4_set_delete(SET)
2559 # ------------------
2560 # Delete all elements in SET, and reclaim any memory occupied by the
2561 # set. This is O(n) in the set size.
2562 #
2563 # Use _m4_defn and _m4_popdef for speed.
2564 m4_define([m4_set_delete],
2565 [m4_ifdef([_m4_set([$1])],
2566 [_m4_popdef([_m4_set([$1],]_m4_defn([_m4_set([$1])])[)],
2567 [_m4_set([$1])])$0([$1])],
2568 [m4_ifdef([_m4_set_cleanup($1)],
2569 [_m4_popdef([_m4_set_cleanup($1)])])m4_ifdef(
2570 [_m4_set_size($1)],
2571 [_m4_popdef([_m4_set_size($1)])])])])
2572
2573 # m4_set_difference(SET1, SET2)
2574 # -----------------------------
2575 # Produce a LIST of quoted elements that occur in SET1 but not SET2.
2576 # Output a comma prior to any elements, to distinguish the empty
2577 # string from no elements. This can be directly used as a series of
2578 # arguments, such as for m4_join, or wrapped inside quotes for use in
2579 # m4_foreach. Order of the output is not guaranteed.
2580 #
2581 # Short-circuit the idempotence relation. Use _m4_defn for speed.
2582 m4_define([m4_set_difference],
2583 [m4_if([$1], [$2], [],
2584 [m4_set_foreach([$1], [_m4_element],
2585 [m4_set_contains([$2], _m4_defn([_m4_element]), [],
2586 [,_m4_defn([_m4_element])])])])])
2587
2588 # m4_set_dump(SET, [SEP])
2589 # -----------------------
2590 # Expand to a single string containing all the elements in SET,
2591 # separated by SEP, then delete SET. In general, if you only need to
2592 # list the contents once, this is faster than m4_set_contents. No
2593 # provision is made for disambiguating set elements that contain
2594 # non-empty SEP as a sub-string. Order of the output is not
2595 # guaranteed. This is O(n) in the size of the set before pruning.
2596 #
2597 # Use _m4_popdef for speed. Use existence of _m4_set_cleanup($1) to
2598 # decide if more expensive recursion is needed.
2599 m4_define([m4_set_dump],
2600 [m4_ifdef([_m4_set_size($1)],
2601 [_m4_popdef([_m4_set_size($1)])])m4_ifdef([_m4_set_cleanup($1)],
2602 [_$0_check], [_$0])([$1], [], [$2])])
2603
2604 # _m4_set_dump(SET, SEP, PREP)
2605 # _m4_set_dump_check(SET, SEP, PREP)
2606 # ----------------------------------
2607 # Print SEP and the current element, then delete the element and
2608 # recurse with empty SEP changed to PREP. The check variant checks
2609 # whether the element has been previously removed. Use _m4_defn and
2610 # _m4_popdef for speed.
2611 m4_define([_m4_set_dump],
2612 [m4_ifdef([_m4_set([$1])],
2613 [[$2]_m4_defn([_m4_set([$1])])_m4_popdef([_m4_set([$1],]_m4_defn(
2614 [_m4_set([$1])])[)], [_m4_set([$1])])$0([$1], [$2$3])])])
2615
2616 m4_define([_m4_set_dump_check],
2617 [m4_ifdef([_m4_set([$1])],
2618 [m4_set_contains([$1], _m4_defn([_m4_set([$1])]),
2619 [[$2]_m4_defn([_m4_set([$1])])])_m4_popdef(
2620 [_m4_set([$1],]_m4_defn([_m4_set([$1])])[)],
2621 [_m4_set([$1])])$0([$1], [$2$3])],
2622 [_m4_popdef([_m4_set_cleanup($1)])])])
2623
2624 # m4_set_empty(SET, [IF-EMPTY], [IF-ELEMENTS])
2625 # --------------------------------------------
2626 # Expand IF-EMPTY if SET has no elements, otherwise IF-ELEMENTS.
2627 m4_define([m4_set_empty],
2628 [m4_ifdef([_m4_set_size($1)],
2629 [m4_if(m4_indir([_m4_set_size($1)]), [0], [$2], [$3])], [$2])])
2630
2631 # m4_set_foreach(SET, VAR, ACTION)
2632 # --------------------------------
2633 # For each element of SET, define VAR to the element and expand
2634 # ACTION. ACTION should not recursively list SET's contents, add
2635 # elements to SET, nor delete any element from SET except the one
2636 # currently in VAR. The order that the elements are visited in is not
2637 # guaranteed. This is faster than the corresponding m4_foreach([VAR],
2638 # m4_indir([m4_dquote]m4_set_listc([SET])), [ACTION])
2639 m4_define([m4_set_foreach],
2640 [m4_pushdef([$2])m4_ifdef([_m4_set_cleanup($1)],
2641 [_m4_set_contents_1c], [_m4_set_contents_1])([$1])_m4_set_contents_2([$1],
2642 [m4_define([$2], _m4_defn([_m4_set_($1)]))$3[]])m4_popdef([$2])])
2643
2644 # m4_set_intersection(SET1, SET2)
2645 # -------------------------------
2646 # Produce a LIST of quoted elements that occur in both SET1 or SET2.
2647 # Output a comma prior to any elements, to distinguish the empty
2648 # string from no elements. This can be directly used as a series of
2649 # arguments, such as for m4_join, or wrapped inside quotes for use in
2650 # m4_foreach. Order of the output is not guaranteed.
2651 #
2652 # Iterate over the smaller set, and short-circuit the idempotence
2653 # relation. Use _m4_defn for speed.
2654 m4_define([m4_set_intersection],
2655 [m4_if([$1], [$2], [m4_set_listc([$1])],
2656 m4_eval(m4_set_size([$2]) < m4_set_size([$1])), [1], [$0([$2], [$1])],
2657 [m4_set_foreach([$1], [_m4_element],
2658 [m4_set_contains([$2], _m4_defn([_m4_element]),
2659 [,_m4_defn([_m4_element])])])])])
2660
2661 # m4_set_list(SET)
2662 # m4_set_listc(SET)
2663 # -----------------
2664 # Produce a LIST of quoted elements of SET. This can be directly used
2665 # as a series of arguments, such as for m4_join or m4_set_add_all, or
2666 # wrapped inside quotes for use in m4_foreach or m4_map. With
2667 # m4_set_list, there is no way to distinguish an empty set from a set
2668 # containing only the empty string; with m4_set_listc, a leading comma
2669 # is output if there are any elements.
2670 m4_define([m4_set_list],
2671 [m4_ifdef([_m4_set_cleanup($1)], [_m4_set_contents_1c],
2672 [_m4_set_contents_1])([$1])_m4_set_contents_2([$1],
2673 [_m4_defn([_m4_set_($1)])], [,])])
2674
2675 m4_define([m4_set_listc],
2676 [m4_ifdef([_m4_set_cleanup($1)], [_m4_set_contents_1c],
2677 [_m4_set_contents_1])([$1])_m4_set_contents_2([$1],
2678 [,_m4_defn([_m4_set_($1)])])])
2679
2680 # m4_set_remove(SET, VALUE, [IF-PRESENT], [IF-ABSENT])
2681 # ----------------------------------------------------
2682 # If VALUE is an element of SET, delete it and expand IF-PRESENT.
2683 # Otherwise expand IF-ABSENT. Deleting a single value is O(1),
2684 # although it leaves memory occupied until the next O(n) traversal of
2685 # the set which will compact the set.
2686 #
2687 # Optimize if the element being removed is the most recently added,
2688 # since defining _m4_set_cleanup($1) slows down so many other macros.
2689 # In particular, this plays well with m4_set_foreach.
2690 m4_define([m4_set_remove],
2691 [m4_set_contains([$1], [$2], [_m4_set_size([$1],
2692 [m4_decr])m4_if(_m4_defn([_m4_set([$1])]), [$2],
2693 [_m4_popdef([_m4_set([$1],$2)], [_m4_set([$1])])],
2694 [m4_define([_m4_set_cleanup($1)])m4_define(
2695 [_m4_set([$1],$2)], [0])])$3], [$4])])
2696
2697 # m4_set_size(SET)
2698 # ----------------
2699 # Expand to the number of elements currently in SET. This operation
2700 # is O(1), and thus more efficient than m4_count(m4_set_list([SET])).
2701 m4_define([m4_set_size],
2702 [m4_ifdef([_m4_set_size($1)], [m4_indir([_m4_set_size($1)])], [0])])
2703
2704 # _m4_set_size(SET, ACTION)
2705 # -------------------------
2706 # ACTION must be either m4_incr or m4_decr, and the size of SET is
2707 # changed accordingly. If the set is empty, ACTION must not be
2708 # m4_decr.
2709 m4_define([_m4_set_size],
2710 [m4_define([_m4_set_size($1)],
2711 m4_ifdef([_m4_set_size($1)], [$2(m4_indir([_m4_set_size($1)]))],
2712 [1]))])
2713
2714 # m4_set_union(SET1, SET2)
2715 # ------------------------
2716 # Produce a LIST of double quoted elements that occur in either SET1
2717 # or SET2, without duplicates. Output a comma prior to any elements,
2718 # to distinguish the empty string from no elements. This can be
2719 # directly used as a series of arguments, such as for m4_join, or
2720 # wrapped inside quotes for use in m4_foreach. Order of the output is
2721 # not guaranteed.
2722 #
2723 # We can rely on the fact that m4_set_listc prunes SET1, so we don't
2724 # need to check _m4_set([$1],element) for 0. Use _m4_defn for speed.
2725 # Short-circuit the idempotence relation.
2726 m4_define([m4_set_union],
2727 [m4_set_listc([$1])m4_if([$1], [$2], [], [m4_set_foreach([$2], [_m4_element],
2728 [m4_ifdef([_m4_set([$1],]_m4_defn([_m4_element])[)], [],
2729 [,_m4_defn([_m4_element])])])])])
2730
2731
2732 ## ------------------- ##
2733 ## 16. File handling. ##
2734 ## ------------------- ##
2735
2736
2737 # It is a real pity that M4 comes with no macros to bind a diversion
2738 # to a file. So we have to deal without, which makes us a lot more
2739 # fragile than we should.
2740
2741
2742 # m4_file_append(FILE-NAME, CONTENT)
2743 # ----------------------------------
2744 m4_define([m4_file_append],
2745 [m4_syscmd([cat >>$1 <<_m4eof
2746 $2
2747 _m4eof
2748 ])
2749 m4_if(m4_sysval, [0], [],
2750 [m4_fatal([$0: cannot write: $1])])])
2751
2752
2753
2754 ## ------------------------ ##
2755 ## 17. Setting M4sugar up. ##
2756 ## ------------------------ ##
2757
2758
2759 # m4_init
2760 # -------
2761 # Initialize the m4sugar language.
2762 m4_define([m4_init],
2763 [# All the M4sugar macros start with `m4_', except `dnl' kept as is
2764 # for sake of simplicity.
2765 m4_pattern_forbid([^_?m4_])
2766 m4_pattern_forbid([^dnl$])
2767
2768 # If __m4_version__ is defined, we assume that we are being run by M4
2769 # 1.6 or newer, and thus that $@ recursion is linear and debugmode(d)
2770 # is available for faster checks of dereferencing undefined macros.
2771 # But if it is missing, we assume we are being run by M4 1.4.x, that
2772 # $@ recursion is quadratic, and that we need foreach-based
2773 # replacement macros. Use the raw builtin to avoid tripping up
2774 # include tracing.
2775 m4_ifdef([__m4_version__],
2776 [m4_debugmode([+d])
2777 m4_copy([_m4_defn], [m4_defn])
2778 m4_copy([_m4_popdef], [m4_popdef])
2779 m4_copy([_m4_undefine], [m4_undefine])],
2780 [m4_builtin([include], [m4sugar/foreach.m4])])
2781
2782 # _m4_divert_diversion should be defined:
2783 m4_divert_push([KILL])
2784
2785 # Check the divert push/pop perfect balance.
2786 m4_wrap([m4_divert_pop([])
2787 m4_ifdef([_m4_divert_diversion],
2788 [m4_fatal([$0: unbalanced m4_divert_push:]_m4_divert_n_stack)])[]])
2789 ])