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