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