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