1divert(-1)# -*- Autoconf -*- 2# $MirOS: src/gnu/share/m4sugar.inc,v 1.5 2008/05/03 22:27:28 tg Exp $ 3# $miros: contrib/gnu/libtool/m4sugar.inc,v 1.9 2008/05/03 20:24:30 tg Exp $ 4#- 5# This file is part of Autoconf. 6# Base M4 layer. 7# Hacked for MirOS m4. 8# 9# Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 10# 2008 Free Software Foundation, Inc. 11# 12# This program is free software: you can redistribute it and/or modify 13# it under the terms of the GNU General Public License as published by 14# the Free Software Foundation; either version 2, or (at your option) 15# any later version. 16# 17# This program is distributed in the hope that it will be useful, 18# but WITHOUT ANY WARRANTY; without even the implied warranty of 19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20# GNU General Public License for more details. 21# 22# You should have received a copy of the GNU General Public License 23# along with this program; if not, write to the Free Software 24# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 25# 02110-1301, USA. 26# 27# As a special exception, the Free Software Foundation gives unlimited 28# permission to copy, distribute and modify the configure scripts that 29# are the output of Autoconf. You need not follow the terms of the GNU 30# General Public License when using or distributing such scripts, even 31# though portions of the text of Autoconf appear in them. The GNU 32# General Public License (GPL) does govern all other use of the material 33# that constitutes the Autoconf program. 34# 35# Certain portions of the Autoconf source text are designed to be copied 36# (in certain cases, depending on the input) into the output of 37# Autoconf. We call these the "data" portions. The rest of the Autoconf 38# source text consists of comments plus executable code that decides which 39# of the data portions to output in any given case. We call these 40# comments and executable code the "non-data" portions. Autoconf never 41# copies any of the non-data portions into its output. 42# 43# This special exception to the GPL applies to versions of Autoconf 44# released by the Free Software Foundation. When you make and 45# distribute a modified version of Autoconf, you may extend this special 46# exception to the GPL to apply to your modified version as well, *unless* 47# your modified version has the potential to copy into its output some 48# of the text that was the non-data portion of the version that you started 49# with. (In other words, unless your change moves or copies text from 50# the non-data portions to the data portions.) If your modification has 51# such potential, you must delete any notice of this special exception 52# to the GPL from your modified version. 53# 54# Written by Akim Demaille. 55# 56 57# Set the quotes, whatever the current quoting system. 58changequote() 59changequote([, ]) 60 61# Some old m4's don't support m4exit. But they provide 62# equivalent functionality by core dumping because of the 63# long macros we define. 64ifdef([__gnu__], , 65[errprint(M4sugar requires GNU M4. Install it before installing M4sugar or 66set the M4 environment variable to its absolute file name.) 67m4exit(2)]) 68 69 70## ------------------------------- ## 71## 1. Simulate --prefix-builtins. ## 72## ------------------------------- ## 73 74# m4_define 75# m4_defn 76# m4_undefine 77define([m4_define], defn([define])) 78define([m4_defn], defn([defn])) 79define([m4_undefine], defn([undefine])) 80 81m4_undefine([define]) 82m4_undefine([defn]) 83m4_undefine([undefine]) 84 85 86# m4_copy(SRC, DST) 87# ----------------- 88# Define DST as the definition of SRC. 89# What's the difference between: 90# 1. m4_copy([from], [to]) 91# 2. m4_define([to], [from($@)]) 92# Well, obviously 1 is more expensive in space. Maybe 2 is more expensive 93# in time, but because of the space cost of 1, it's not that obvious. 94# Nevertheless, one huge difference is the handling of `$0'. If `from' 95# uses `$0', then with 1, `to''s `$0' is `to', while it is `from' in 2. 96# The user would certainly prefer to see `to'. 97m4_define([m4_copy], 98[m4_define([$2], m4_defn([$1]))]) 99 100 101# m4_rename(SRC, DST) 102# ------------------- 103# Rename the macro SRC to DST. 104m4_define([m4_rename], 105[m4_copy([$1], [$2])m4_undefine([$1])]) 106 107 108# m4_rename_m4(MACRO-NAME) 109# ------------------------ 110# Rename MACRO-NAME to m4_MACRO-NAME. 111m4_define([m4_rename_m4], 112[m4_rename([$1], [m4_$1])]) 113 114 115# m4_copy_unm4(m4_MACRO-NAME) 116# --------------------------- 117# Copy m4_MACRO-NAME to MACRO-NAME. 118m4_define([m4_copy_unm4], 119[m4_copy([$1], m4_bpatsubst([$1], [^m4_\(.*\)], [[\1]]))]) 120 121 122# Some m4 internals have names colliding with tokens we might use. 123# Rename them a` la `m4 --prefix-builtins'. 124m4_rename_m4([builtin]) 125m4_rename_m4([changecom]) 126m4_rename_m4([changequote]) 127m4_undefine([changeword]) 128m4_rename_m4([debugfile]) 129m4_rename_m4([debugmode]) 130m4_rename_m4([decr]) 131m4_undefine([divert]) 132m4_rename_m4([divnum]) 133m4_rename_m4([dumpdef]) 134m4_rename_m4([errprint]) 135m4_rename_m4([esyscmd]) 136 137m4_define([m4_eval], [builtin([eval], $@)]) 138 139m4_rename_m4([format]) 140m4_rename_m4([ifdef]) 141m4_rename([ifelse], [m4_if]) 142m4_undefine([include]) 143m4_rename_m4([incr]) 144m4_rename_m4([index]) 145m4_rename_m4([indir]) 146m4_rename_m4([len]) 147m4_rename([m4exit], [m4_exit]) 148m4_undefine([m4wrap]) 149m4_ifdef([mkstemp],dnl added in M4 1.4.8 150[m4_rename_m4([mkstemp]) 151m4_copy([m4_mkstemp], [m4_maketemp]) 152m4_undefine([maketemp])], 153[m4_rename_m4([maketemp]) 154m4_copy([m4_maketemp], [m4_mkstemp])]) 155m4_copy([patsubst], [m4_patsubst]) 156m4_rename([patsubst], [m4_bpatsubst]) 157m4_undefine([popdef]) 158m4_rename_m4([pushdef]) 159m4_copy([regexp], [m4_regexp]) 160m4_rename([regexp], [m4_bregexp]) 161 162m4_define([m4_shift], [builtin([shift], $@)]) 163 164m4_rename_m4([sinclude]) 165dnl m4_undefine([sinclude]) 166 167m4_rename_m4([substr]) 168m4_rename_m4([symbols]) 169m4_rename_m4([syscmd]) 170m4_rename_m4([sysval]) 171m4_rename_m4([traceoff]) 172m4_rename_m4([traceon]) 173m4_rename_m4([translit]) 174m4_undefine([undivert]) 175 176 177## ------------------- ## 178## 2. Error messages. ## 179## ------------------- ## 180 181 182# m4_location 183# ----------- 184m4_define([m4_location], 185[__file__:__line__]) 186 187 188# m4_errprintn(MSG) 189# ----------------- 190# Same as `errprint', but with the missing end of line. 191m4_define([m4_errprintn], 192[m4_errprint([$1 193])]) 194 195 196# m4_warning(MSG) 197# --------------- 198# Warn the user. 199m4_define([m4_warning], 200[m4_errprintn(m4_location[: warning: $1])]) 201 202 203# m4_fatal(MSG, [EXIT-STATUS]) 204# ---------------------------- 205# Fatal the user. :) 206m4_define([m4_fatal], 207[m4_errprintn(m4_location[: error: $1])dnl 208m4_expansion_stack_dump()dnl 209m4_exit(m4_if([$2],, 1, [$2]))]) 210 211 212# m4_assert(EXPRESSION, [EXIT-STATUS = 1]) 213# ---------------------------------------- 214# This macro ensures that EXPRESSION evaluates to true, and exits if 215# EXPRESSION evaluates to false. 216m4_define([m4_assert], 217[m4_if(m4_eval([$1]), 0, 218 [m4_fatal([assert failed: $1], [$2])])]) 219 220 221 222## ------------- ## 223## 3. Warnings. ## 224## ------------- ## 225 226 227# _m4_warn(CATEGORY, MESSAGE, STACK-TRACE) 228# ---------------------------------------- 229# Report a MESSAGE to the user if the CATEGORY of warnings is enabled. 230# This is for traces only. 231# The STACK-TRACE is a \n-separated list of "LOCATION: MESSAGE". 232# 233# Within m4, the macro is a no-op. This macro really matters 234# when autom4te post-processes the trace output. 235m4_define([_m4_warn], []) 236 237 238# m4_warn(CATEGORY, MESSAGE) 239# -------------------------- 240# Report a MESSAGE to the autoconf user if the CATEGORY of warnings 241# is requested (in fact, not disabled). 242m4_define([m4_warn], 243[_m4_warn([$1], [$2], 244m4_ifdef([m4_expansion_stack], 245 [m4_defn([m4_expansion_stack]) 246m4_location[: the top level]]))dnl 247]) 248 249 250 251## ------------------- ## 252## 4. File inclusion. ## 253## ------------------- ## 254 255 256# We also want to neutralize include (and sinclude for symmetry), 257# but we want to extend them slightly: warn when a file is included 258# several times. This is, in general, a dangerous operation, because 259# too many people forget to quote the first argument of m4_define. 260# 261# For instance in the following case: 262# m4_define(foo, [bar]) 263# then a second reading will turn into 264# m4_define(bar, [bar]) 265# which is certainly not what was meant. 266 267# m4_include_unique(FILE) 268# ----------------------- 269# Declare that the FILE was loading; and warn if it has already 270# been included. 271m4_define([m4_include_unique], 272[m4_ifdef([m4_include($1)], 273 [m4_warn([syntax], [file `$1' included several times])])dnl 274m4_define([m4_include($1)])]) 275 276 277# m4_include(FILE) 278# ---------------- 279# Like the builtin include, but warns against multiple inclusions. 280m4_define([m4_include], 281[m4_include_unique([$1])dnl 282m4_builtin([include], [$1])]) 283 284 285# m4_sinclude(FILE) 286# ----------------- 287# Like the builtin sinclude, but warns against multiple inclusions. 288m4_define([m4_sinclude], 289[m4_include_unique([$1])dnl 290m4_builtin([sinclude], [$1])]) 291 292 293 294## ------------------------------------ ## 295## 5. Additional branching constructs. ## 296## ------------------------------------ ## 297 298# Both `m4_ifval' and `m4_ifset' tests against the empty string. The 299# difference is that `m4_ifset' is specialized on macros. 300# 301# In case of arguments of macros, eg. $1, it makes little difference. 302# In the case of a macro `FOO', you don't want to check `m4_ifval(FOO, 303# TRUE)', because if `FOO' expands with commas, there is a shifting of 304# the arguments. So you want to run `m4_ifval([FOO])', but then you just 305# compare the *string* `FOO' against `', which, of course fails. 306# 307# So you want the variation `m4_ifset' that expects a macro name as $1. 308# If this macro is both defined and defined to a non empty value, then 309# it runs TRUE, etc. 310 311 312# m4_ifval(COND, [IF-TRUE], [IF-FALSE]) 313# ------------------------------------- 314# If COND is not the empty string, expand IF-TRUE, otherwise IF-FALSE. 315# Comparable to m4_ifdef. 316m4_define([m4_ifval], 317[m4_if([$1], [], [$3], [$2])]) 318 319 320# m4_n(TEXT) 321# ---------- 322# If TEXT is not empty, return TEXT and a new line, otherwise nothing. 323m4_define([m4_n], 324[m4_if([$1], 325 [], [], 326 [$1 327])]) 328 329 330# m4_ifvaln(COND, [IF-TRUE], [IF-FALSE]) 331# -------------------------------------- 332# Same as `m4_ifval', but add an extra newline to IF-TRUE or IF-FALSE 333# unless that argument is empty. 334m4_define([m4_ifvaln], 335[m4_if([$1], 336 [], [m4_n([$3])], 337 [m4_n([$2])])]) 338 339 340# m4_ifset(MACRO, [IF-TRUE], [IF-FALSE]) 341# -------------------------------------- 342# If MACRO has no definition, or of its definition is the empty string, 343# expand IF-FALSE, otherwise IF-TRUE. 344m4_define([m4_ifset], 345[m4_ifdef([$1], 346 [m4_ifval(m4_defn([$1]), [$2], [$3])], 347 [$3])]) 348 349 350# m4_ifndef(NAME, [IF-NOT-DEFINED], [IF-DEFINED]) 351# ----------------------------------------------- 352m4_define([m4_ifndef], 353[m4_ifdef([$1], [$3], [$2])]) 354 355 356# m4_case(SWITCH, VAL1, IF-VAL1, VAL2, IF-VAL2, ..., DEFAULT) 357# ----------------------------------------------------------- 358# m4 equivalent of 359# switch (SWITCH) 360# { 361# case VAL1: 362# IF-VAL1; 363# break; 364# case VAL2: 365# IF-VAL2; 366# break; 367# ... 368# default: 369# DEFAULT; 370# break; 371# }. 372# All the values are optional, and the macro is robust to active 373# symbols properly quoted. 374m4_define([m4_case], 375[m4_if([$#], 0, [], 376 [$#], 1, [], 377 [$#], 2, [$2], 378 [$1], [$2], [$3], 379 [m4_case([$1], m4_shift3($@))])]) 380 381 382# m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT) 383# ----------------------------------------------------- 384# m4 equivalent of 385# 386# if (SWITCH =~ RE1) 387# VAL1; 388# elif (SWITCH =~ RE2) 389# VAL2; 390# elif ... 391# ... 392# else 393# DEFAULT 394# 395# All the values are optional, and the macro is robust to active symbols 396# properly quoted. 397m4_define([m4_bmatch], 398[m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])], 399 [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])], 400 [$#], 2, [$2], 401 [m4_if(m4_bregexp([$1], [$2]), -1, [$0([$1], m4_shift3($@))], 402 [$3])])]) 403m4_copy([m4_bmatch], [m4_match]) 404 405 406# m4_car(LIST) 407# m4_cdr(LIST) 408# ------------ 409# Manipulate m4 lists. 410m4_define([m4_car], [[$1]]) 411m4_define([m4_cdr], 412[m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])], 413 [$#], 1, [], 414 [m4_dquote(m4_shift($@))])]) 415 416# _m4_cdr(LIST) 417# ------------- 418# Like m4_cdr, except include a leading comma unless only one element 419# remains. Why? Because comparing a large list against [] is more 420# expensive in expansion time than comparing the number of arguments; so 421# _m4_cdr can be used to reduce the number of arguments when it is time 422# to end recursion. 423m4_define([_m4_cdr], 424[m4_if([$#], 1, [], 425 [, m4_dquote(m4_shift($@))])]) 426 427 428 429# m4_cond(TEST1, VAL1, IF-VAL1, TEST2, VAL2, IF-VAL2, ..., [DEFAULT]) 430# ------------------------------------------------------------------- 431# Similar to m4_if, except that each TEST is expanded when encountered. 432# If the expansion of TESTn matches the string VALn, the result is IF-VALn. 433# The result is DEFAULT if no tests passed. This macro allows 434# short-circuiting of expensive tests, where it pays to arrange quick 435# filter tests to run first. 436# 437# For an example, consider a previous implementation of _AS_QUOTE_IFELSE: 438# 439# m4_if(m4_index([$1], [\]), [-1], [$2], 440# m4_eval(m4_index([$1], [\\]) >= 0), [1], [$2], 441# m4_eval(m4_index([$1], [\$]) >= 0), [1], [$2], 442# m4_eval(m4_index([$1], [\`]) >= 0), [1], [$3], 443# m4_eval(m4_index([$1], [\"]) >= 0), [1], [$3], 444# [$2]) 445# 446# Here, m4_index is computed 5 times, and m4_eval 4, even if $1 contains 447# no backslash. It is more efficient to do: 448# 449# m4_cond([m4_index([$1], [\])], [-1], [$2], 450# [m4_eval(m4_index([$1], [\\]) >= 0)], [1], [$2], 451# [m4_eval(m4_index([$1], [\$]) >= 0)], [1], [$2], 452# [m4_eval(m4_index([$1], [\`]) >= 0)], [1], [$3], 453# [m4_eval(m4_index([$1], [\"]) >= 0)], [1], [$3], 454# [$2]) 455# 456# In the common case of $1 with no backslash, only one m4_index expansion 457# occurs, and m4_eval is avoided altogether. 458m4_define([m4_cond], 459[m4_if([$#], [0], [m4_fatal([$0: cannot be called without arguments])], 460 [$#], [1], [$1], 461 [$#], [2], [m4_fatal([$0: missing an argument])], 462 [m4_if($1, [$2], [$3], [$0(m4_shift3($@))])])]) 463 464 465## ---------------------------------------- ## 466## 6. Enhanced version of some primitives. ## 467## ---------------------------------------- ## 468 469# m4_bpatsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...) 470# ---------------------------------------------------- 471# m4 equivalent of 472# 473# $_ = STRING; 474# s/RE1/SUBST1/g; 475# s/RE2/SUBST2/g; 476# ... 477# 478# All the values are optional, and the macro is robust to active symbols 479# properly quoted. 480# 481# I would have liked to name this macro `m4_bpatsubst', unfortunately, 482# due to quotation problems, I need to double quote $1 below, therefore 483# the anchors are broken :( I can't let users be trapped by that. 484# 485# Recall that m4_shift3 always results in an argument. Hence, we need 486# to distinguish between a final deletion vs. ending recursion. 487m4_define([m4_bpatsubsts], 488[m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])], 489 [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])], 490 [$#], 2, [m4_builtin([patsubst], [$1], [$2])], 491 [_$0($@m4_if(m4_eval($# & 1), 0, [,]))])]) 492m4_define([_m4_bpatsubsts], 493[m4_if([$#], 2, [$1], 494 [$0(m4_builtin([patsubst], [[$1]], [$2], [$3]), 495 m4_shift3($@))])]) 496 497 498# m4_define_default(MACRO, VALUE) 499# ------------------------------- 500# If MACRO is undefined, set it to VALUE. 501m4_define([m4_define_default], 502[m4_ifndef([$1], [m4_define($@)])]) 503 504 505# m4_default(EXP1, EXP2) 506# ---------------------- 507# Returns EXP1 if non empty, otherwise EXP2. 508# 509# This macro is called on hot paths, so inline the contents of m4_ifval, 510# for one less round of expansion. 511m4_define([m4_default], 512[m4_if([$1], [], [$2], [$1])]) 513 514 515# m4_defn(NAME) 516# ------------- 517# Like the original, except don't tolerate popping something which is 518# undefined, and only support one argument. 519# 520# This macro is called frequently, so minimize the amount of additional 521# expansions by skipping m4_ifndef. 522m4_define([m4_defn], 523[m4_ifdef([$1], [], 524 [m4_fatal([$0: undefined macro: $1])])]dnl 525[m4_builtin([defn], [$1])]) 526 527 528# _m4_dumpdefs_up(NAME) 529# --------------------- 530m4_define([_m4_dumpdefs_up], 531[m4_ifdef([$1], 532 [m4_pushdef([_m4_dumpdefs], m4_defn([$1]))dnl 533m4_dumpdef([$1])dnl 534m4_popdef([$1])dnl 535_m4_dumpdefs_up([$1])])]) 536 537 538# _m4_dumpdefs_down(NAME) 539# ----------------------- 540m4_define([_m4_dumpdefs_down], 541[m4_ifdef([_m4_dumpdefs], 542 [m4_pushdef([$1], m4_defn([_m4_dumpdefs]))dnl 543m4_popdef([_m4_dumpdefs])dnl 544_m4_dumpdefs_down([$1])])]) 545 546 547# m4_dumpdefs(NAME) 548# ----------------- 549# Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its 550# value stack (most recent displayed first). 551m4_define([m4_dumpdefs], 552[_m4_dumpdefs_up([$1])dnl 553_m4_dumpdefs_down([$1])]) 554 555 556# m4_popdef(NAME) 557# --------------- 558# Like the original, except don't tolerate popping something which is 559# undefined, and only support one argument. 560# 561# This macro is called frequently, so minimize the amount of additional 562# expansions by skipping m4_ifndef. 563m4_define([m4_popdef], 564[m4_ifdef([$1], [], 565 [m4_fatal([$0: undefined macro: $1])])]dnl 566[m4_builtin([popdef], [$1])]) 567 568 569# m4_shiftn(N, ...) 570# ----------------- 571# Returns ... shifted N times. Useful for recursive "varargs" constructs. 572# 573# Autoconf does not use this macro, because it is inherently slower than 574# calling the common cases of m4_shift2 or m4_shift3 directly. But it 575# might as well be fast for other clients, such as Libtool. One way to 576# do this is to expand $@ only once in _m4_shiftn (otherwise, for long 577# lists, the expansion of m4_if takes twice as much memory as what the 578# list itself occupies, only to throw away the unused branch). The end 579# result is strictly equivalent to 580# m4_if([$1], 1, [m4_shift(,m4_shift(m4_shift($@)))], 581# [_m4_shiftn(m4_decr([$1]), m4_shift(m4_shift($@)))]) 582# but with the final `m4_shift(m4_shift($@)))' shared between the two 583# paths. The first leg uses a no-op m4_shift(,$@) to balance out the (). 584m4_define([m4_shiftn], 585[m4_assert(0 < $1 && $1 < $#)_$0($@)]) 586 587m4_define([_m4_shiftn], 588[m4_if([$1], 1, [m4_shift(], 589 [$0(m4_decr([$1])]), m4_shift(m4_shift($@)))]) 590 591# m4_shift2(...) 592# m4_shift3(...) 593# ----------------- 594# Returns ... shifted twice, and three times. Faster than m4_shiftn. 595m4_define([m4_shift2], [m4_shift(m4_shift($@))]) 596m4_define([m4_shift3], [m4_shift(m4_shift(m4_shift($@)))]) 597 598# _m4_shift2(...) 599# _m4_shift3(...) 600# --------------- 601# Like m4_shift2 or m4_shift3, except include a leading comma unless shifting 602# consumes all arguments. Why? Because in recursion, it is nice to 603# distinguish between 1 element left and 0 elements left, based on how many 604# arguments this shift expands to. 605m4_define([_m4_shift2], 606[m4_if([$#], [2], [], 607 [, m4_shift(m4_shift($@))])]) 608m4_define([_m4_shift3], 609[m4_if([$#], [3], [], 610 [, m4_shift(m4_shift(m4_shift($@)))])]) 611 612 613# m4_undefine(NAME) 614# ----------------- 615# Like the original, except don't tolerate undefining something which is 616# undefined, and only support one argument. 617# 618# This macro is called frequently, so minimize the amount of additional 619# expansions by skipping m4_ifndef. 620m4_define([m4_undefine], 621[m4_ifdef([$1], [], 622 [m4_fatal([$0: undefined macro: $1])])]dnl 623[m4_builtin([undefine], [$1])]) 624 625# _m4_wrap(PRE, POST) 626# ------------------- 627# Helper macro for m4_wrap and m4_wrap_lifo. Allows nested calls to 628# m4_wrap within wrapped text. 629# Skip m4_defn and m4_popdef for speed. 630m4_define([_m4_wrap], 631[m4_ifdef([$0_text], 632 [m4_define([$0_text], [$1]m4_builtin([defn], [$0_text])[$2])], 633 [m4_builtin([m4wrap], [m4_unquote(m4_builtin([defn], 634 [$0_text])m4_builtin([popdef], [$0_text]))])m4_define([$0_text], [$1$2])])]) 635 636# m4_wrap(TEXT) 637# ------------- 638# Append TEXT to the list of hooks to be executed at the end of input. 639# Whereas the order of the original may be LIFO in the underlying m4, 640# this version is always FIFO. 641m4_define([m4_wrap], 642[_m4_wrap([], [$1[]])]) 643 644# m4_wrap_lifo(TEXT) 645# ------------------ 646# Prepend TEXT to the list of hooks to be executed at the end of input. 647# Whereas the order of m4_wrap may be FIFO in the underlying m4, this 648# version is always LIFO. 649m4_define([m4_wrap_lifo], 650[_m4_wrap([$1[]])]) 651 652## ------------------------- ## 653## 7. Quoting manipulation. ## 654## ------------------------- ## 655 656 657# m4_apply(MACRO, LIST) 658# --------------------- 659# Invoke MACRO, with arguments provided from the quoted list of 660# comma-separated quoted arguments. If LIST is empty, invoke MACRO 661# without arguments. 662m4_define([m4_apply], 663[m4_if([$2], [], [$1], [$1($2)])[]]) 664 665 666# m4_count(ARGS) 667# -------------- 668# Return a count of how many ARGS are present. 669m4_define([m4_count], [$#]) 670 671 672# m4_do(STRING, ...) 673# ------------------ 674# This macro invokes all its arguments (in sequence, of course). It is 675# useful for making your macros more structured and readable by dropping 676# unnecessary dnl's and have the macros indented properly. 677m4_define([m4_do], 678[m4_if([$#], 0, [], 679 [$#], 1, [$1], 680 [$1[]m4_do(m4_shift($@))])]) 681 682 683# m4_dquote(ARGS) 684# --------------- 685# Return ARGS as a quoted list of quoted arguments. 686m4_define([m4_dquote], [[$@]]) 687 688 689# m4_dquote_elt(ARGS) 690# ------------------- 691# Return ARGS as an unquoted list of double-quoted arguments. 692m4_define([m4_dquote_elt], 693[m4_if([$#], [0], [], 694 [$#], [1], [[[$1]]], 695 [[[$1]],$0(m4_shift($@))])]) 696 697 698# m4_echo(ARGS) 699# ------------- 700# Return the ARGS, with the same level of quoting. Whitespace after 701# unquoted commas are consumed. 702m4_define([m4_echo], [$@]) 703 704 705# m4_expand(ARG) 706# -------------- 707# Return the expansion of ARG as a single string. Unlike m4_quote($1), this 708# correctly preserves whitespace following single-quoted commas that appeared 709# within ARG. 710# 711# m4_define([active], [ACT, IVE]) 712# m4_define([active2], [[ACT, IVE]]) 713# m4_quote(active, active2) 714# => ACT,IVE,ACT, IVE 715# m4_expand([active, active2]) 716# => ACT, IVE, ACT, IVE 717# 718# Unfortunately, due to limitations in m4, ARG must expand to something 719# with balanced quotes (use quadrigraphs to get around this). The input 720# is not likely to have unbalanced -=<{(/)}>=- quotes, and it is possible 721# to have unbalanced (), provided it was specified with proper [] quotes. 722# 723# Exploit that extra () will group unquoted commas and the following 724# whitespace, then convert () to []. m4_bpatsubst can't handle newlines 725# inside $1, and m4_substr strips quoting. So we (ab)use m4_changequote. 726m4_define([m4_expand], [_$0(-=<{($1)}>=-)]) 727m4_define([_m4_expand], 728[m4_changequote([-=<{(], [)}>=-])$1m4_changequote([, ])]) 729 730 731# m4_ignore(ARGS) 732# --------------- 733# Expands to nothing. Useful for conditionally ignoring an arbitrary 734# number of arguments (see _m4_list_cmp for an example). 735m4_define([m4_ignore]) 736 737 738# m4_make_list(ARGS) 739# ------------------ 740# Similar to m4_dquote, this creates a quoted list of quoted ARGS. This 741# version is less efficient than m4_dquote, but separates each argument 742# with a comma and newline, rather than just comma, for readability. 743# When developing an m4sugar algorithm, you could temporarily use 744# m4_pushdef([m4_dquote],m4_defn([m4_make_list])) 745# around your code to make debugging easier. 746m4_define([m4_make_list], [m4_join([, 747], m4_dquote_elt($@))]) 748 749 750# m4_noquote(STRING) 751# ------------------ 752# Return the result of ignoring all quotes in STRING and invoking the 753# macros it contains. Amongst other things, this is useful for enabling 754# macro invocations inside strings with [] blocks (for instance regexps 755# and help-strings). On the other hand, since all quotes are disabled, 756# any macro expanded during this time that relies on nested [] quoting 757# will likely crash and burn. This macro is seldom useful; consider 758# m4_unquote or m4_expand instead. 759m4_define([m4_noquote], 760[m4_changequote([-=<{(],[)}>=-])$1-=<{()}>=-m4_changequote([,])]) 761 762 763# m4_quote(ARGS) 764# -------------- 765# Return ARGS as a single argument. Any whitespace after unquoted commas 766# is stripped. There is always output, even when there were no arguments. 767# 768# It is important to realize the difference between `m4_quote(exp)' and 769# `[exp]': in the first case you obtain the quoted *result* of the 770# expansion of EXP, while in the latter you just obtain the string 771# `exp'. 772m4_define([m4_quote], [[$*]]) 773 774 775# _m4_quote(ARGS) 776# --------------- 777# Like m4_quote, except that when there are no arguments, there is no 778# output. For conditional scenarios (such as passing _m4_quote as the 779# macro name in m4_map), this feature can be used to distinguish between 780# one argument of the empty string vs. no arguments. However, in the 781# normal case with arguments present, this is less efficient than m4_quote. 782m4_define([_m4_quote], 783[m4_if([$#], [0], [], [[$*]])]) 784 785 786# m4_unquote(ARGS) 787# ---------------- 788# Remove one layer of quotes from each ARG, performing one level of 789# expansion. For one argument, m4_unquote([arg]) is more efficient than 790# m4_do([arg]), but for multiple arguments, the difference is that 791# m4_unquote separates arguments with commas while m4_do concatenates. 792m4_define([m4_unquote], [$*]) 793 794 795## -------------------------- ## 796## 8. Implementing m4 loops. ## 797## -------------------------- ## 798 799 800# m4_for(VARIABLE, FIRST, LAST, [STEP = +/-1], EXPRESSION) 801# -------------------------------------------------------- 802# Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO with 803# increments of STEP. 804# Both limits are included, and bounds are checked for consistency. 805# The algorithm is robust to indirect VARIABLE names, and uses m4_builtin 806# to avoid some of the m4_defn overhead. 807m4_define([m4_for], 808[m4_pushdef([$1], m4_eval([$2]))dnl 809m4_cond([m4_eval(([$3]) > m4_builtin([defn], [$1]))], 1, 810[m4_pushdef([_m4_step], m4_eval(m4_default([$4], 1)))dnl 811m4_assert(_m4_step > 0)dnl 812_m4_for([$1], m4_eval((([$3]) - m4_builtin([defn], [$1])) 813 / _m4_step * _m4_step + m4_builtin([defn], [$1])), 814 _m4_step, [$5])], 815 [m4_eval(([$3]) < m4_builtin([defn], [$1]))], 1, 816[m4_pushdef([_m4_step], m4_eval(m4_default([$4], -1)))dnl 817m4_assert(_m4_step < 0)dnl 818_m4_for([$1], m4_eval((m4_builtin([defn], [$1]) - ([$3])) 819 / -(_m4_step) * _m4_step + m4_builtin([defn], [$1])), 820 _m4_step, [$5])], 821 [m4_pushdef([_m4_step])dnl 822$5])[]dnl 823m4_popdef([_m4_step])dnl 824m4_popdef([$1])]) 825 826 827# _m4_for(VARIABLE, LAST, STEP, EXPRESSION) 828# ----------------------------------------- 829# Core of the loop, no consistency checks, all arguments are plain numbers. 830m4_define([_m4_for], 831[$4[]dnl 832m4_if(m4_defn([$1]), [$2], [], 833 [m4_define([$1], m4_eval(m4_defn([$1])+[$3]))$0($@)])]) 834 835 836# Implementing `foreach' loops in m4 is much more tricky than it may 837# seem. For example, the old M4 1.4.4 manual had an incorrect example, 838# which looked like this (when translated to m4sugar): 839# 840# | # foreach(VAR, (LIST), STMT) 841# | m4_define([foreach], 842# | [m4_pushdef([$1])_foreach([$1], [$2], [$3])m4_popdef([$1])]) 843# | m4_define([_arg1], [$1]) 844# | m4_define([_foreach], 845# | [m4_if([$2], [()], , 846# | [m4_define([$1], _arg1$2)$3[]_foreach([$1], (m4_shift$2), [$3])])]) 847# 848# But then if you run 849# 850# | m4_define(a, 1) 851# | m4_define(b, 2) 852# | m4_define(c, 3) 853# | foreach([f], [([a], [(b], [c)])], [echo f 854# | ]) 855# 856# it gives 857# 858# => echo 1 859# => echo (2,3) 860# 861# which is not what is expected. 862# 863# Of course the problem is that many quotes are missing. So you add 864# plenty of quotes at random places, until you reach the expected 865# result. Alternatively, if you are a quoting wizard, you directly 866# reach the following implementation (but if you really did, then 867# apply to the maintenance of m4sugar!). 868# 869# | # foreach(VAR, (LIST), STMT) 870# | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])]) 871# | m4_define([_arg1], [[$1]]) 872# | m4_define([_foreach], 873# | [m4_if($2, [()], , 874# | [m4_define([$1], [_arg1$2])$3[]_foreach([$1], [(m4_shift$2)], [$3])])]) 875# 876# which this time answers 877# 878# => echo a 879# => echo (b 880# => echo c) 881# 882# Bingo! 883# 884# Well, not quite. 885# 886# With a better look, you realize that the parens are more a pain than 887# a help: since anyway you need to quote properly the list, you end up 888# with always using an outermost pair of parens and an outermost pair 889# of quotes. Rejecting the parens both eases the implementation, and 890# simplifies the use: 891# 892# | # foreach(VAR, (LIST), STMT) 893# | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])]) 894# | m4_define([_arg1], [$1]) 895# | m4_define([_foreach], 896# | [m4_if($2, [], , 897# | [m4_define([$1], [_arg1($2)])$3[]_foreach([$1], [m4_shift($2)], [$3])])]) 898# 899# 900# Now, just replace the `$2' with `m4_quote($2)' in the outer `m4_if' 901# to improve robustness, and you come up with a nice implementation 902# that doesn't require extra parentheses in the user's LIST. 903# 904# But wait - now the algorithm is quadratic, because every recursion of 905# the algorithm keeps the entire LIST and merely adds another m4_shift to 906# the quoted text. If the user has a lot of elements in LIST, you can 907# bring the system to its knees with the memory m4 then requires, or trip 908# the m4 --nesting-limit recursion factor. The only way to avoid 909# quadratic growth is ensure m4_shift is expanded prior to the recursion. 910# Hence the design below. 911# 912# The M4 manual now includes a chapter devoted to this issue, with 913# the lessons learned from m4sugar. 914 915 916# m4_foreach(VARIABLE, LIST, EXPRESSION) 917# -------------------------------------- 918# 919# Expand EXPRESSION assigning each value of the LIST to VARIABLE. 920# LIST should have the form `item_1, item_2, ..., item_n', i.e. the 921# whole list must *quoted*. Quote members too if you don't want them 922# to be expanded. 923# 924# This macro is robust to active symbols: 925# | m4_define(active, [ACT, IVE]) 926# | m4_foreach(Var, [active, active], [-Var-]) 927# => -ACT--IVE--ACT--IVE- 928# 929# | m4_foreach(Var, [[active], [active]], [-Var-]) 930# => -ACT, IVE--ACT, IVE- 931# 932# | m4_foreach(Var, [[[active]], [[active]]], [-Var-]) 933# => -active--active- 934# 935# This macro is called frequently, so avoid extra expansions such as 936# m4_ifval and dnl. Also, since $2 might be quite large, try to use it 937# as little as possible in _m4_foreach; each extra use requires that much 938# more memory for expansion. So, rather than directly compare $2 against 939# [] and use m4_car/m4_cdr for recursion, we instead unbox the list (which 940# requires swapping the argument order in the helper) and use _m4_shift3 941# to detect when recursion is complete. 942m4_define([m4_foreach], 943[m4_pushdef([$1])_$0([$1], [$3]m4_if([$2], [], [], [, $2]))m4_popdef([$1])]) 944 945m4_define([_m4_foreach], 946[m4_if([$#], [2], [], 947 [m4_define([$1], [$3])$2[]$0([$1], [$2]_m4_shift3($@))])]) 948 949 950# m4_foreach_w(VARIABLE, LIST, EXPRESSION) 951# ---------------------------------------- 952# 953# Like m4_foreach, but the list is whitespace separated. 954# 955# This macro is robust to active symbols: 956# m4_foreach_w([Var], [ active 957# b act\ 958# ive ], [-Var-])end 959# => -active--b--active-end 960# 961m4_define([m4_foreach_w], 962[m4_foreach([$1], m4_split(m4_normalize([$2]), [ ]), [$3])]) 963 964 965# m4_map(MACRO, LIST) 966# ------------------- 967# Invoke MACRO($1), MACRO($2) etc. where $1, $2... are the elements 968# of LIST. $1, $2... must in turn be lists, appropriate for m4_apply. 969# 970# Since LIST may be quite large, we want to minimize how often it appears 971# in the expansion. Rather than use m4_car/m4_cdr iteration, we unbox the 972# list, and use _m4_shift2 to detect the end of recursion. 973m4_define([m4_map], 974[m4_if([$2], [], [], 975 [_$0([$1], $2)])]) 976m4_define([_m4_map], 977[m4_if([$#], [1], [], 978 [m4_apply([$1], [$2])$0([$1]_m4_shift2($@))])]) 979 980 981# m4_map_sep(MACRO, SEPARATOR, LIST) 982# ---------------------------------- 983# Invoke MACRO($1), SEPARATOR, MACRO($2), ..., MACRO($N) where $1, $2... $N 984# are the elements of LIST, and are in turn lists appropriate for m4_apply. 985# SEPARATOR is not further expanded. 986m4_define([m4_map_sep], 987[m4_if([$3], [], [], 988 [m4_apply([$1], m4_car($3))m4_map([[$2]$1]_m4_cdr($3))])]) 989 990 991## --------------------------- ## 992## 9. More diversion support. ## 993## --------------------------- ## 994 995 996# _m4_divert(DIVERSION-NAME or NUMBER) 997# ------------------------------------ 998# If DIVERSION-NAME is the name of a diversion, return its number, 999# otherwise if it is a NUMBER return it. 1000m4_define([_m4_divert], 1001[m4_ifdef([_m4_divert($1)], 1002 [m4_indir([_m4_divert($1)])], 1003 [$1])]) 1004 1005# KILL is only used to suppress output. 1006m4_define([_m4_divert(KILL)], -1) 1007 1008# The empty diversion name is a synonym for 0. 1009m4_define([_m4_divert()], 0) 1010 1011 1012# _m4_divert_n_stack 1013# ------------------ 1014# Print m4_divert_stack with newline prepended, if it's nonempty. 1015m4_define([_m4_divert_n_stack], 1016[m4_ifdef([m4_divert_stack], [ 1017m4_defn([m4_divert_stack])])]) 1018 1019 1020# m4_divert(DIVERSION-NAME) 1021# ------------------------- 1022# Change the diversion stream to DIVERSION-NAME. 1023m4_define([m4_divert], 1024[m4_define([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl 1025m4_builtin([divert], _m4_divert([$1]))dnl 1026]) 1027 1028 1029# m4_divert_push(DIVERSION-NAME) 1030# ------------------------------ 1031# Change the diversion stream to DIVERSION-NAME, while stacking old values. 1032m4_define([m4_divert_push], 1033[m4_pushdef([m4_divert_stack], m4_location[: $0: $1]_m4_divert_n_stack)dnl 1034m4_pushdef([_m4_divert_diversion], [$1])dnl 1035m4_builtin([divert], _m4_divert([$1]))dnl 1036]) 1037 1038 1039# m4_divert_pop([DIVERSION-NAME]) 1040# ------------------------------- 1041# Change the diversion stream to its previous value, unstacking it. 1042# If specified, verify we left DIVERSION-NAME. 1043# When we pop the last value from the stack, we divert to -1. 1044m4_define([m4_divert_pop], 1045[m4_ifndef([_m4_divert_diversion], 1046 [m4_fatal([too many m4_divert_pop])])dnl 1047m4_if([$1], [], [], 1048 [$1], m4_defn([_m4_divert_diversion]), [], 1049 [m4_fatal([$0($1): diversion mismatch: ]_m4_divert_n_stack)])dnl 1050m4_popdef([m4_divert_stack])dnl 1051m4_popdef([_m4_divert_diversion])dnl 1052m4_builtin([divert], 1053 m4_ifdef([_m4_divert_diversion], 1054 [_m4_divert(m4_defn([_m4_divert_diversion]))], 1055 -1))dnl 1056]) 1057 1058 1059# m4_divert_text(DIVERSION-NAME, CONTENT) 1060# --------------------------------------- 1061# Output CONTENT into DIVERSION-NAME (which may be a number actually). 1062# An end of line is appended for free to CONTENT. 1063m4_define([m4_divert_text], 1064[m4_divert_push([$1])dnl 1065$2 1066m4_divert_pop([$1])dnl 1067]) 1068 1069 1070# m4_divert_once(DIVERSION-NAME, CONTENT) 1071# --------------------------------------- 1072# Output CONTENT into DIVERSION-NAME once, if not already there. 1073# An end of line is appended for free to CONTENT. 1074m4_define([m4_divert_once], 1075[m4_expand_once([m4_divert_text([$1], [$2])])]) 1076 1077 1078# m4_undivert(DIVERSION-NAME) 1079# --------------------------- 1080# Undivert DIVERSION-NAME. Unlike the M4 version, this only takes a single 1081# diversion identifier, and should not be used to undivert files. 1082m4_define([m4_undivert], 1083[m4_builtin([undivert], _m4_divert([$1]))]) 1084 1085 1086## --------------------------------------------- ## 1087## 10. Defining macros with bells and whistles. ## 1088## --------------------------------------------- ## 1089 1090# `m4_defun' is basically `m4_define' but it equips the macro with the 1091# needed machinery for `m4_require'. A macro must be m4_defun'd if 1092# either it is m4_require'd, or it m4_require's. 1093# 1094# Two things deserve attention and are detailed below: 1095# 1. Implementation of m4_require 1096# 2. Keeping track of the expansion stack 1097# 1098# 1. Implementation of m4_require 1099# =============================== 1100# 1101# Of course m4_defun AC_PROVIDE's the macro, so that a macro which has 1102# been expanded is not expanded again when m4_require'd, but the 1103# difficult part is the proper expansion of macros when they are 1104# m4_require'd. 1105# 1106# The implementation is based on two ideas, (i) using diversions to 1107# prepare the expansion of the macro and its dependencies (by Franc,ois 1108# Pinard), and (ii) expand the most recently m4_require'd macros _after_ 1109# the previous macros (by Axel Thimm). 1110# 1111# 1112# The first idea: why use diversions? 1113# ----------------------------------- 1114# 1115# When a macro requires another, the other macro is expanded in new 1116# diversion, GROW. When the outer macro is fully expanded, we first 1117# undivert the most nested diversions (GROW - 1...), and finally 1118# undivert GROW. To understand why we need several diversions, 1119# consider the following example: 1120# 1121# | m4_defun([TEST1], [Test...REQUIRE([TEST2])1]) 1122# | m4_defun([TEST2], [Test...REQUIRE([TEST3])2]) 1123# | m4_defun([TEST3], [Test...3]) 1124# 1125# Because m4_require is not required to be first in the outer macros, we 1126# must keep the expansions of the various levels of m4_require separated. 1127# Right before executing the epilogue of TEST1, we have: 1128# 1129# GROW - 2: Test...3 1130# GROW - 1: Test...2 1131# GROW: Test...1 1132# BODY: 1133# 1134# Finally the epilogue of TEST1 undiverts GROW - 2, GROW - 1, and 1135# GROW into the regular flow, BODY. 1136# 1137# GROW - 2: 1138# GROW - 1: 1139# GROW: 1140# BODY: Test...3; Test...2; Test...1 1141# 1142# (The semicolons are here for clarification, but of course are not 1143# emitted.) This is what Autoconf 2.0 (I think) to 2.13 (I'm sure) 1144# implement. 1145# 1146# 1147# The second idea: first required first out 1148# ----------------------------------------- 1149# 1150# The natural implementation of the idea above is buggy and produces 1151# very surprising results in some situations. Let's consider the 1152# following example to explain the bug: 1153# 1154# | m4_defun([TEST1], [REQUIRE([TEST2a])REQUIRE([TEST2b])]) 1155# | m4_defun([TEST2a], []) 1156# | m4_defun([TEST2b], [REQUIRE([TEST3])]) 1157# | m4_defun([TEST3], [REQUIRE([TEST2a])]) 1158# | 1159# | AC_INIT 1160# | TEST1 1161# 1162# The dependencies between the macros are: 1163# 1164# 3 --- 2b 1165# / \ is m4_require'd by 1166# / \ left -------------------- right 1167# 2a ------------ 1 1168# 1169# If you strictly apply the rules given in the previous section you get: 1170# 1171# GROW - 2: TEST3 1172# GROW - 1: TEST2a; TEST2b 1173# GROW: TEST1 1174# BODY: 1175# 1176# (TEST2a, although required by TEST3 is not expanded in GROW - 3 1177# because is has already been expanded before in GROW - 1, so it has 1178# been AC_PROVIDE'd, so it is not expanded again) so when you undivert 1179# the stack of diversions, you get: 1180# 1181# GROW - 2: 1182# GROW - 1: 1183# GROW: 1184# BODY: TEST3; TEST2a; TEST2b; TEST1 1185# 1186# i.e., TEST2a is expanded after TEST3 although the latter required the 1187# former. 1188# 1189# Starting from 2.50, we use an implementation provided by Axel Thimm. 1190# The idea is simple: the order in which macros are emitted must be the 1191# same as the one in which macros are expanded. (The bug above can 1192# indeed be described as: a macro has been AC_PROVIDE'd before its 1193# dependent, but it is emitted after: the lack of correlation between 1194# emission and expansion order is guilty). 1195# 1196# How to do that? You keep the stack of diversions to elaborate the 1197# macros, but each time a macro is fully expanded, emit it immediately. 1198# 1199# In the example above, when TEST2a is expanded, but it's epilogue is 1200# not run yet, you have: 1201# 1202# GROW - 2: 1203# GROW - 1: TEST2a 1204# GROW: Elaboration of TEST1 1205# BODY: 1206# 1207# The epilogue of TEST2a emits it immediately: 1208# 1209# GROW - 2: 1210# GROW - 1: 1211# GROW: Elaboration of TEST1 1212# BODY: TEST2a 1213# 1214# TEST2b then requires TEST3, so right before the epilogue of TEST3, you 1215# have: 1216# 1217# GROW - 2: TEST3 1218# GROW - 1: Elaboration of TEST2b 1219# GROW: Elaboration of TEST1 1220# BODY: TEST2a 1221# 1222# The epilogue of TEST3 emits it: 1223# 1224# GROW - 2: 1225# GROW - 1: Elaboration of TEST2b 1226# GROW: Elaboration of TEST1 1227# BODY: TEST2a; TEST3 1228# 1229# TEST2b is now completely expanded, and emitted: 1230# 1231# GROW - 2: 1232# GROW - 1: 1233# GROW: Elaboration of TEST1 1234# BODY: TEST2a; TEST3; TEST2b 1235# 1236# and finally, TEST1 is finished and emitted: 1237# 1238# GROW - 2: 1239# GROW - 1: 1240# GROW: 1241# BODY: TEST2a; TEST3; TEST2b: TEST1 1242# 1243# The idea is simple, but the implementation is a bit evolved. If you 1244# are like me, you will want to see the actual functioning of this 1245# implementation to be convinced. The next section gives the full 1246# details. 1247# 1248# 1249# The Axel Thimm implementation at work 1250# ------------------------------------- 1251# 1252# We consider the macros above, and this configure.ac: 1253# 1254# AC_INIT 1255# TEST1 1256# 1257# You should keep the definitions of _m4_defun_pro, _m4_defun_epi, and 1258# m4_require at hand to follow the steps. 1259# 1260# This implements tries not to assume that the current diversion is 1261# BODY, so as soon as a macro (m4_defun'd) is expanded, we first 1262# record the current diversion under the name _m4_divert_dump (denoted 1263# DUMP below for short). This introduces an important difference with 1264# the previous versions of Autoconf: you cannot use m4_require if you 1265# are not inside an m4_defun'd macro, and especially, you cannot 1266# m4_require directly from the top level. 1267# 1268# We have not tried to simulate the old behavior (better yet, we 1269# diagnose it), because it is too dangerous: a macro m4_require'd from 1270# the top level is expanded before the body of `configure', i.e., before 1271# any other test was run. I let you imagine the result of requiring 1272# AC_STDC_HEADERS for instance, before AC_PROG_CC was actually run.... 1273# 1274# After AC_INIT was run, the current diversion is BODY. 1275# * AC_INIT was run 1276# DUMP: undefined 1277# diversion stack: BODY |- 1278# 1279# * TEST1 is expanded 1280# The prologue of TEST1 sets _m4_divert_dump, which is the diversion 1281# where the current elaboration will be dumped, to the current 1282# diversion. It also m4_divert_push to GROW, where the full 1283# expansion of TEST1 and its dependencies will be elaborated. 1284# DUMP: BODY 1285# BODY: empty 1286# diversions: GROW, BODY |- 1287# 1288# * TEST1 requires TEST2a 1289# _m4_require_call m4_divert_pushes another temporary diversion, 1290# GROW - 1, and expands TEST2a in there. 1291# DUMP: BODY 1292# BODY: empty 1293# GROW - 1: TEST2a 1294# diversions: GROW - 1, GROW, BODY |- 1295# Than the content of the temporary diversion is moved to DUMP and the 1296# temporary diversion is popped. 1297# DUMP: BODY 1298# BODY: TEST2a 1299# diversions: GROW, BODY |- 1300# 1301# * TEST1 requires TEST2b 1302# Again, _m4_require_call pushes GROW - 1 and heads to expand TEST2b. 1303# DUMP: BODY 1304# BODY: TEST2a 1305# diversions: GROW - 1, GROW, BODY |- 1306# 1307# * TEST2b requires TEST3 1308# _m4_require_call pushes GROW - 2 and expands TEST3 here. 1309# (TEST3 requires TEST2a, but TEST2a has already been m4_provide'd, so 1310# nothing happens.) 1311# DUMP: BODY 1312# BODY: TEST2a 1313# GROW - 2: TEST3 1314# diversions: GROW - 2, GROW - 1, GROW, BODY |- 1315# Than the diversion is appended to DUMP, and popped. 1316# DUMP: BODY 1317# BODY: TEST2a; TEST3 1318# diversions: GROW - 1, GROW, BODY |- 1319# 1320# * TEST1 requires TEST2b (contd.) 1321# The content of TEST2b is expanded... 1322# DUMP: BODY 1323# BODY: TEST2a; TEST3 1324# GROW - 1: TEST2b, 1325# diversions: GROW - 1, GROW, BODY |- 1326# ... and moved to DUMP. 1327# DUMP: BODY 1328# BODY: TEST2a; TEST3; TEST2b 1329# diversions: GROW, BODY |- 1330# 1331# * TEST1 is expanded: epilogue 1332# TEST1's own content is in GROW... 1333# DUMP: BODY 1334# BODY: TEST2a; TEST3; TEST2b 1335# GROW: TEST1 1336# diversions: BODY |- 1337# ... and it's epilogue moves it to DUMP and then undefines DUMP. 1338# DUMP: undefined 1339# BODY: TEST2a; TEST3; TEST2b; TEST1 1340# diversions: BODY |- 1341# 1342# 1343# 2. Keeping track of the expansion stack 1344# ======================================= 1345# 1346# When M4 expansion goes wrong it is often extremely hard to find the 1347# path amongst macros that drove to the failure. What is needed is 1348# the stack of macro `calls'. One could imagine that GNU M4 would 1349# maintain a stack of macro expansions, unfortunately it doesn't, so 1350# we do it by hand. This is of course extremely costly, but the help 1351# this stack provides is worth it. Nevertheless to limit the 1352# performance penalty this is implemented only for m4_defun'd macros, 1353# not for define'd macros. 1354# 1355# The scheme is simplistic: each time we enter an m4_defun'd macros, 1356# we prepend its name in m4_expansion_stack, and when we exit the 1357# macro, we remove it (thanks to pushdef/popdef). 1358# 1359# In addition, we want to detect circular m4_require dependencies. 1360# Each time we expand a macro FOO we define _m4_expanding(FOO); and 1361# m4_require(BAR) simply checks whether _m4_expanding(BAR) is defined. 1362 1363 1364# m4_expansion_stack_push(TEXT) 1365# ----------------------------- 1366# Use m4_builtin to avoid m4_defn overhead. 1367m4_define([m4_expansion_stack_push], 1368[m4_pushdef([m4_expansion_stack], 1369 [$1]m4_ifdef([m4_expansion_stack], [ 1370m4_builtin([defn], [m4_expansion_stack])]))]) 1371 1372 1373# m4_expansion_stack_pop 1374# ---------------------- 1375m4_define([m4_expansion_stack_pop], 1376[m4_popdef([m4_expansion_stack])]) 1377 1378 1379# m4_expansion_stack_dump 1380# ----------------------- 1381# Dump the expansion stack. 1382m4_define([m4_expansion_stack_dump], 1383[m4_ifdef([m4_expansion_stack], 1384 [m4_errprintn(m4_defn([m4_expansion_stack]))])dnl 1385m4_errprintn(m4_location[: the top level])]) 1386 1387 1388# _m4_divert(GROW) 1389# ---------------- 1390# This diversion is used by the m4_defun/m4_require machinery. It is 1391# important to keep room before GROW because for each nested 1392# AC_REQUIRE we use an additional diversion (i.e., two m4_require's 1393# will use GROW - 2. More than 3 levels has never seemed to be 1394# needed.) 1395# 1396# ... 1397# - GROW - 2 1398# m4_require'd code, 2 level deep 1399# - GROW - 1 1400# m4_require'd code, 1 level deep 1401# - GROW 1402# m4_defun'd macros are elaborated here. 1403 1404m4_define([_m4_divert(GROW)], 10000) 1405 1406 1407# _m4_defun_pro(MACRO-NAME) 1408# ------------------------- 1409# The prologue for Autoconf macros. 1410# 1411# This is called frequently, so minimize the number of macro invocations 1412# by avoiding dnl and m4_defn overhead. 1413m4_define([_m4_defun_pro], 1414m4_do([[m4_ifdef([m4_expansion_stack], [], [_m4_defun_pro_outer[]])]], 1415 [[m4_expansion_stack_push(m4_builtin([defn], 1416 [m4_location($1)])[: $1 is expanded from...])]], 1417 [[m4_pushdef([_m4_expanding($1)])]])) 1418 1419m4_define([_m4_defun_pro_outer], 1420[m4_copy([_m4_divert_diversion], [_m4_divert_dump])m4_divert_push([GROW])]) 1421 1422# _m4_defun_epi(MACRO-NAME) 1423# ------------------------- 1424# The Epilogue for Autoconf macros. MACRO-NAME only helps tracing 1425# the PRO/EPI pairs. 1426# 1427# This is called frequently, so minimize the number of macro invocations 1428# by avoiding dnl and m4_popdef overhead. 1429m4_define([_m4_defun_epi], 1430m4_do([[m4_builtin([popdef], [_m4_expanding($1)])]], 1431 [[m4_expansion_stack_pop()]], 1432 [[m4_ifdef([m4_expansion_stack], [], [_m4_defun_epi_outer[]])]], 1433 [[m4_provide([$1])]])) 1434 1435m4_define([_m4_defun_epi_outer], 1436m4_do([[m4_builtin([undefine], [_m4_divert_dump])]], 1437 [[m4_divert_pop([GROW])]], 1438 [[m4_undivert([GROW])]])) 1439 1440 1441# m4_defun(NAME, EXPANSION) 1442# ------------------------- 1443# Define a macro which automatically provides itself. Add machinery 1444# so the macro automatically switches expansion to the diversion 1445# stack if it is not already using it. In this case, once finished, 1446# it will bring back all the code accumulated in the diversion stack. 1447# This, combined with m4_require, achieves the topological ordering of 1448# macros. We don't use this macro to define some frequently called 1449# macros that are not involved in ordering constraints, to save m4 1450# processing. 1451m4_define([m4_defun], 1452[m4_define([m4_location($1)], m4_location)dnl 1453m4_define([$1], 1454 [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])]) 1455 1456 1457# m4_defun_once(NAME, EXPANSION) 1458# ------------------------------ 1459# As m4_defun, but issues the EXPANSION only once, and warns if used 1460# several times. 1461m4_define([m4_defun_once], 1462[m4_define([m4_location($1)], m4_location)dnl 1463m4_define([$1], 1464 [m4_provide_if([$1], 1465 [m4_warn([syntax], [$1 invoked multiple times])], 1466 [_m4_defun_pro([$1])$2[]_m4_defun_epi([$1])])])]) 1467 1468 1469# m4_pattern_forbid(ERE, [WHY]) 1470# ----------------------------- 1471# Declare that no token matching the forbidden extended regular 1472# expression ERE should be seen in the output unless... 1473m4_define([m4_pattern_forbid], 1474[m4_file_append(m4_defn([m4_tmpdir])/forbidden.rx, [$1])]) 1475 1476 1477# m4_pattern_allow(ERE) 1478# --------------------- 1479# ... that token also matches the allowed extended regular expression ERE. 1480m4_define([m4_pattern_allow], 1481[m4_file_append(m4_defn([m4_tmpdir])/allowed.rx, [$1])]) 1482 1483 1484## --------------------------------- ## 1485## 11. Dependencies between macros. ## 1486## --------------------------------- ## 1487 1488 1489# m4_before(THIS-MACRO-NAME, CALLED-MACRO-NAME) 1490# --------------------------------------------- 1491# Issue a warning if CALLED-MACRO-NAME was called before THIS-MACRO-NAME. 1492m4_define([m4_before], 1493[m4_provide_if([$2], 1494 [m4_warn([syntax], [$2 was called before $1])])]) 1495 1496 1497# m4_require(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK]) 1498# ----------------------------------------------------------- 1499# If NAME-TO-CHECK has never been expanded (actually, if it is not 1500# m4_provide'd), expand BODY-TO-EXPAND *before* the current macro 1501# expansion. Once expanded, emit it in _m4_divert_dump. Keep track 1502# of the m4_require chain in m4_expansion_stack. 1503# 1504# The normal cases are: 1505# 1506# - NAME-TO-CHECK == BODY-TO-EXPAND 1507# Which you can use for regular macros with or without arguments, e.g., 1508# m4_require([AC_PROG_CC], [AC_PROG_CC]) 1509# m4_require([AC_CHECK_HEADERS(limits.h)], [AC_CHECK_HEADERS(limits.h)]) 1510# which is just the same as 1511# m4_require([AC_PROG_CC]) 1512# m4_require([AC_CHECK_HEADERS(limits.h)]) 1513# 1514# - BODY-TO-EXPAND == m4_indir([NAME-TO-CHECK]) 1515# In the case of macros with irregular names. For instance: 1516# m4_require([AC_LANG_COMPILER(C)], [indir([AC_LANG_COMPILER(C)])]) 1517# which means `if the macro named `AC_LANG_COMPILER(C)' (the parens are 1518# part of the name, it is not an argument) has not been run, then 1519# call it.' 1520# Had you used 1521# m4_require([AC_LANG_COMPILER(C)], [AC_LANG_COMPILER(C)]) 1522# then m4_require would have tried to expand `AC_LANG_COMPILER(C)', i.e., 1523# call the macro `AC_LANG_COMPILER' with `C' as argument. 1524# 1525# You could argue that `AC_LANG_COMPILER', when it receives an argument 1526# such as `C' should dispatch the call to `AC_LANG_COMPILER(C)'. But this 1527# `extension' prevents `AC_LANG_COMPILER' from having actual arguments that 1528# it passes to `AC_LANG_COMPILER(C)'. 1529# 1530# This is called frequently, so minimize the number of macro invocations 1531# by avoiding dnl and other overhead on the common path. 1532m4_define([m4_require], 1533m4_do([[m4_ifdef([_m4_expanding($1)], 1534 [m4_fatal([$0: circular dependency of $1])])]], 1535 [[m4_ifdef([_m4_divert_dump], [], 1536 [m4_fatal([$0($1): cannot be used outside of an ]dnl 1537m4_bmatch([$0], [^AC_], [[AC_DEFUN]], [[m4_defun]])['d macro])])]], 1538 [[m4_provide_if([$1], 1539 [], 1540 [_m4_require_call([$1], [$2])])]])) 1541 1542 1543# _m4_require_call(NAME-TO-CHECK, [BODY-TO-EXPAND = NAME-TO-CHECK]) 1544# ----------------------------------------------------------------- 1545# If m4_require decides to expand the body, it calls this macro. 1546# 1547# This is called frequently, so minimize the number of macro invocations 1548# by avoiding dnl and other overhead on the common path. 1549m4_define([_m4_require_call], 1550m4_do([[m4_define([_m4_divert_grow], m4_decr(_m4_divert_grow))]], 1551 [[m4_divert_push(_m4_divert_grow)]], 1552 [[m4_default([$2], [$1]) 1553m4_provide_if([$1], 1554 [], 1555 [m4_warn([syntax], 1556 [$1 is m4_require'd but not m4_defun'd])])]], 1557 [[m4_divert(m4_builtin([defn], [_m4_divert_dump]))]], 1558 [[m4_undivert(_m4_divert_grow)]], 1559 [[m4_divert_pop(_m4_divert_grow)]], 1560 [[m4_define([_m4_divert_grow], m4_incr(_m4_divert_grow))]])) 1561 1562 1563# _m4_divert_grow 1564# --------------- 1565# The counter for _m4_require_call. 1566m4_define([_m4_divert_grow], _m4_divert([GROW])) 1567 1568 1569# m4_expand_once(TEXT, [WITNESS = TEXT]) 1570# -------------------------------------- 1571# If TEXT has never been expanded, expand it *here*. Use WITNESS as 1572# as a memory that TEXT has already been expanded. 1573m4_define([m4_expand_once], 1574[m4_provide_if(m4_ifval([$2], [[$2]], [[$1]]), 1575 [], 1576 [m4_provide(m4_ifval([$2], [[$2]], [[$1]]))[]$1])]) 1577 1578 1579# m4_provide(MACRO-NAME) 1580# ---------------------- 1581m4_define([m4_provide], 1582[m4_define([m4_provide($1)])]) 1583 1584 1585# m4_provide_if(MACRO-NAME, IF-PROVIDED, IF-NOT-PROVIDED) 1586# ------------------------------------------------------- 1587# If MACRO-NAME is provided do IF-PROVIDED, else IF-NOT-PROVIDED. 1588# The purpose of this macro is to provide the user with a means to 1589# check macros which are provided without letting her know how the 1590# information is coded. 1591m4_define([m4_provide_if], 1592[m4_ifdef([m4_provide($1)], 1593 [$2], [$3])]) 1594m4_copy([m4_provide_if], [m4_provide_ifelse]) 1595 1596 1597## --------------------- ## 1598## 12. Text processing. ## 1599## --------------------- ## 1600 1601 1602# m4_cr_letters 1603# m4_cr_LETTERS 1604# m4_cr_Letters 1605# ------------- 1606m4_define([m4_cr_letters], [abcdefghijklmnopqrstuvwxyz]) 1607m4_define([m4_cr_LETTERS], [ABCDEFGHIJKLMNOPQRSTUVWXYZ]) 1608m4_define([m4_cr_Letters], 1609m4_defn([m4_cr_letters])dnl 1610m4_defn([m4_cr_LETTERS])dnl 1611) 1612 1613 1614# m4_cr_digits 1615# ------------ 1616m4_define([m4_cr_digits], [0123456789]) 1617 1618 1619# m4_cr_alnum 1620# ----------- 1621m4_define([m4_cr_alnum], 1622m4_defn([m4_cr_Letters])dnl 1623m4_defn([m4_cr_digits])dnl 1624) 1625 1626 1627# m4_cr_symbols1 1628# m4_cr_symbols2 1629# ------------------------------- 1630m4_define([m4_cr_symbols1], 1631m4_defn([m4_cr_Letters])dnl 1632_) 1633 1634m4_define([m4_cr_symbols2], 1635m4_defn([m4_cr_symbols1])dnl 1636m4_defn([m4_cr_digits])dnl 1637) 1638 1639 1640# m4_newline 1641# ---------- 1642# Expands to a newline. Exists for formatting reasons. 1643m4_define([m4_newline], [ 1644]) 1645 1646 1647# m4_re_escape(STRING) 1648# -------------------- 1649# Escape RE active characters in STRING. 1650m4_define([m4_re_escape], 1651[m4_bpatsubst([$1], 1652 [[][*+.?\^$]], [\\\&])]) 1653 1654 1655# m4_re_string 1656# ------------ 1657# Regexp for `[a-zA-Z_0-9]*' 1658# m4_dquote provides literal [] for the character class. 1659m4_define([m4_re_string], 1660m4_dquote(m4_defn([m4_cr_symbols2]))dnl 1661[*]dnl 1662) 1663 1664 1665# m4_re_word 1666# ---------- 1667# Regexp for `[a-zA-Z_][a-zA-Z_0-9]*' 1668m4_define([m4_re_word], 1669m4_dquote(m4_defn([m4_cr_symbols1]))dnl 1670m4_defn([m4_re_string])dnl 1671) 1672 1673# m4_tolower(STRING) 1674# m4_toupper(STRING) 1675# ------------------ 1676# These macros convert STRING to lowercase or uppercase. 1677# 1678# Rather than expand the m4_defn each time, we inline them up front. 1679m4_define([m4_tolower], 1680[m4_translit([$1], 1681 [ABCDEFGHIJKLMNOPQRSTUVWXYZ], 1682 [abcdefghijklmnopqrstuvwxyz])]) 1683 1684m4_define([m4_toupper], 1685[m4_translit([$1], 1686 [abcdefghijklmnopqrstuvwxyz], 1687 [ABCDEFGHIJKLMNOPQRSTUVWXYZ])]) 1688 1689 1690# m4_split(STRING, [REGEXP]) 1691# -------------------------- 1692# 1693# Split STRING into an m4 list of quoted elements. The elements are 1694# quoted with [ and ]. Beginning spaces and end spaces *are kept*. 1695# Use m4_strip to remove them. 1696# 1697# REGEXP specifies where to split. Default is [\t ]+. 1698# 1699# If STRING is empty, the result is an empty list. 1700# 1701# Pay attention to the m4_changequotes. When m4 reads the definition of 1702# m4_split, it still has quotes set to [ and ]. Luckily, these are matched 1703# in the macro body, so the definition is stored correctly. Use the same 1704# alternate quotes as m4_noquote; it must be unlikely to appear in $1. 1705# 1706# Also, notice that $1 is quoted twice, since we want the result to 1707# be quoted. Then you should understand that the argument of 1708# patsubst is -=<{(STRING)}>=- (i.e., with additional -=<{( and )}>=-). 1709# 1710# This macro is safe on active symbols, i.e.: 1711# m4_define(active, ACTIVE) 1712# m4_split([active active ])end 1713# => [active], [active], []end 1714# 1715# Optimize on regex of ` ' (space), since m4_foreach_w already guarantees 1716# that the list contains single space separators, and a common case is 1717# splitting a single-element list. This macro is called frequently, 1718# so avoid unnecessary dnl inside the definition. 1719m4_define([m4_split], 1720[m4_if([$1], [], [], 1721 [$2], [ ], [m4_if(m4_index([$1], [ ]), [-1], [[[$1]]], [_$0($@)])], 1722 [$2], [], [_$0([$1], [[ ]+])], 1723 [_$0($@)])]) 1724 1725m4_define([_m4_split], 1726[m4_changequote([-=<{(],[)}>=-])]dnl 1727[[m4_bpatsubst(-=<{(-=<{($1)}>=-)}>=-, -=<{($2)}>=-, 1728 -=<{(], [)}>=-)]m4_changequote([, ])]) 1729 1730 1731 1732# m4_flatten(STRING) 1733# ------------------ 1734# If STRING contains end of lines, replace them with spaces. If there 1735# are backslashed end of lines, remove them. This macro is safe with 1736# active symbols. 1737# m4_define(active, ACTIVE) 1738# m4_flatten([active 1739# act\ 1740# ive])end 1741# => active activeend 1742# 1743# In m4, m4_bpatsubst is expensive, so first check for a newline. 1744m4_define([m4_flatten], 1745[m4_if(m4_index([$1], [ 1746]), [-1], [[$1]], 1747 [m4_translit(m4_bpatsubst([[[$1]]], [\\ 1748]), [ 1749], [ ])])]) 1750 1751 1752# m4_strip(STRING) 1753# ---------------- 1754# Expands into STRING with tabs and spaces singled out into a single 1755# space, and removing leading and trailing spaces. 1756# 1757# This macro is robust to active symbols. 1758# m4_define(active, ACTIVE) 1759# m4_strip([ active <tab> <tab>active ])end 1760# => active activeend 1761# 1762# First, notice that we guarantee trailing space. Why? Because regular 1763# expressions are greedy, and `.* ?' would always group the space into the 1764# .* portion. The algorithm is simpler by avoiding `?' at the end. The 1765# algorithm correctly strips everything if STRING is just ` '. 1766# 1767# Then notice the second pattern: it is in charge of removing the 1768# leading/trailing spaces. Why not just `[^ ]'? Because they are 1769# applied to over-quoted strings, i.e. more or less [STRING], due 1770# to the limitations of m4_bpatsubsts. So the leading space in STRING 1771# is the *second* character; equally for the trailing space. 1772m4_define([m4_strip], 1773[m4_bpatsubsts([$1 ], 1774 [[ ]+], [ ], 1775 [^. ?\(.*\) .$], [[[\1]]])]) 1776 1777 1778# m4_normalize(STRING) 1779# -------------------- 1780# Apply m4_flatten and m4_strip to STRING. 1781# 1782# The argument is quoted, so that the macro is robust to active symbols: 1783# 1784# m4_define(active, ACTIVE) 1785# m4_normalize([ act\ 1786# ive 1787# active ])end 1788# => active activeend 1789 1790m4_define([m4_normalize], 1791[m4_strip(m4_flatten([$1]))]) 1792 1793 1794 1795# m4_join(SEP, ARG1, ARG2...) 1796# --------------------------- 1797# Produce ARG1SEPARG2...SEPARGn. Avoid back-to-back SEP when a given ARG 1798# is the empty string. No expansion is performed on SEP or ARGs. 1799# 1800# Since the number of arguments to join can be arbitrarily long, we 1801# want to avoid having more than one $@ in the macro definition; 1802# otherwise, the expansion would require twice the memory of the already 1803# long list. Hence, m4_join merely looks for the first non-empty element, 1804# and outputs just that element; while _m4_join looks for all non-empty 1805# elements, and outputs them following a separator. The final trick to 1806# note is that we decide between recursing with $0 or _$0 based on the 1807# nested m4_if ending with `_'. 1808m4_define([m4_join], 1809[m4_if([$#], [1], [], 1810 [$#], [2], [[$2]], 1811 [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift2($@))])]) 1812m4_define([_m4_join], 1813[m4_if([$#$2], [2], [], 1814 [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift2($@))])]) 1815 1816 1817# m4_combine([SEPARATOR], PREFIX-LIST, [INFIX], SUFFIX...) 1818# -------------------------------------------------------- 1819# Produce the pairwise combination of every element in the quoted, 1820# comma-separated PREFIX-LIST with every element from the SUFFIX arguments. 1821# Each pair is joined with INFIX, and pairs are separated by SEPARATOR. 1822# No expansion occurs on SEPARATOR, INFIX, or elements of either list. 1823# 1824# For example: 1825# m4_combine([, ], [[a], [b], [c]], [-], [1], [2], [3]) 1826# => a-1, a-2, a-3, b-1, b-2, b-3, c-1, c-2, c-3 1827# 1828# In order to have the correct number of SEPARATORs, we use a temporary 1829# variable that redefines itself after the first use. We use m4_builtin 1830# to avoid m4_defn overhead, but must use defn rather than overquoting 1831# in case PREFIX or SUFFIX contains $1. Likewise, we compute the m4_shift3 1832# only once, rather than in each iteration of the outer m4_foreach. 1833m4_define([m4_combine], 1834[m4_if(m4_eval([$# > 3]), [1], 1835 [m4_pushdef([m4_Separator], [m4_define([m4_Separator], 1836 m4_builtin([defn], [m4_echo]))])]]dnl 1837[[m4_foreach([m4_Prefix], [$2], 1838 [m4_foreach([m4_Suffix], ]m4_dquote(m4_dquote(m4_shift3($@)))[, 1839 [m4_Separator([$1])[]m4_builtin([defn], 1840 [m4_Prefix])[$3]m4_builtin([defn], 1841 [m4_Suffix])])])]]dnl 1842[[m4_builtin([popdef], [m4_Separator])])]) 1843 1844 1845# m4_append(MACRO-NAME, STRING, [SEPARATOR]) 1846# ------------------------------------------ 1847# Redefine MACRO-NAME to hold its former content plus `SEPARATOR`'STRING' 1848# at the end. It is valid to use this macro with MACRO-NAME undefined, 1849# in which case no SEPARATOR is added. Be aware that the criterion is 1850# `not being defined', and not `not being empty'. 1851# 1852# Note that neither STRING nor SEPARATOR are expanded here; rather, when 1853# you expand MACRO-NAME, they will be expanded at that point in time. 1854# 1855# This macro is robust to active symbols. It can be used to grow 1856# strings. 1857# 1858# | m4_define(active, ACTIVE)dnl 1859# | m4_append([sentence], [This is an])dnl 1860# | m4_append([sentence], [ active ])dnl 1861# | m4_append([sentence], [symbol.])dnl 1862# | sentence 1863# | m4_undefine([active])dnl 1864# | sentence 1865# => This is an ACTIVE symbol. 1866# => This is an active symbol. 1867# 1868# It can be used to define hooks. 1869# 1870# | m4_define(active, ACTIVE)dnl 1871# | m4_append([hooks], [m4_define([act1], [act2])])dnl 1872# | m4_append([hooks], [m4_define([act2], [active])])dnl 1873# | m4_undefine([active])dnl 1874# | act1 1875# | hooks 1876# | act1 1877# => act1 1878# => 1879# => active 1880# 1881# It can also be used to create lists, although this particular usage was 1882# broken prior to autoconf 2.62. 1883# | m4_append([list], [one], [, ])dnl 1884# | m4_append([list], [two], [, ])dnl 1885# | m4_append([list], [three], [, ])dnl 1886# | list 1887# | m4_dquote(list) 1888# => one, two, three 1889# => [one],[two],[three] 1890# 1891# Use m4_builtin to avoid overhead of m4_defn. 1892m4_define([m4_append], 1893[m4_define([$1], 1894 m4_ifdef([$1], [m4_builtin([defn], [$1])[$3]])[$2])]) 1895 1896 1897# m4_list_append(MACRO-NAME, STRING) 1898# ---------------------------------- 1899# Same as `m4_append', but each element is separated by `, '. 1900m4_define([m4_list_append], 1901[m4_define([$1], 1902 m4_ifdef([$1], [m4_defn([$1]), ])[$2])]) 1903 1904 1905# m4_foreach_quoted(VARIABLE, LIST, EXPRESSION) 1906# --------------------------------------------- 1907# FIXME: This macro should not exists. Currently it's used only in 1908# m4_wrap, which needs to be rewritten. But it's godam hard. 1909m4_define([m4_foreach_quoted], 1910[m4_pushdef([$1], [])_m4_foreach_quoted($@)m4_popdef([$1])]) 1911 1912# Low level macros used to define m4_foreach. 1913m4_define([m4_car_quoted], [[$1]]) 1914m4_define([_m4_foreach_quoted], 1915[m4_if($2, [()], , 1916 [m4_define([$1], [m4_car_quoted$2])$3[]_m4_foreach_quoted([$1], 1917 [(m4_shift$2)], 1918 [$3])])]) 1919 1920 1921# m4_append_uniq(MACRO-NAME, STRING, [SEPARATOR], [IF-UNIQ], [IF-DUP]) 1922# -------------------------------------------------------------------- 1923# Like `m4_append', but append only if not yet present. Additionally, 1924# expand IF-UNIQ if STRING was appended, or IF-DUP if STRING was already 1925# present. Also, warn if SEPARATOR is not empty and occurs within STRING, 1926# as the algorithm no longer guarantees uniqueness. 1927m4_define([m4_append_uniq], 1928[m4_ifval([$3], [m4_if(m4_index([$2], [$3]), [-1], [], 1929 [m4_warn([syntax], 1930 [$0: `$2' contains `$3'])])])_$0($@)]) 1931m4_define([_m4_append_uniq], 1932[m4_ifdef([$1], 1933 [m4_if(m4_index([$3]m4_builtin([defn], [$1])[$3], [$3$2$3]), [-1], 1934 [m4_append([$1], [$2], [$3])$4], [$5])], 1935 [m4_append([$1], [$2], [$3])$4])]) 1936 1937# m4_append_uniq_w(MACRO-NAME, STRINGS) 1938# ------------------------------------- 1939# For each of the words in the whitespace separated list STRINGS, append 1940# only the unique strings to the definition of MACRO-NAME. 1941# 1942# Avoid overhead of m4_defn by using m4_builtin. 1943m4_define([m4_append_uniq_w], 1944[m4_foreach_w([m4_Word], [$2], 1945 [_m4_append_uniq([$1], m4_builtin([defn], [m4_Word]), [ ])])]) 1946 1947 1948# m4_text_wrap(STRING, [PREFIX], [FIRST-PREFIX], [WIDTH]) 1949# ------------------------------------------------------- 1950# Expands into STRING wrapped to hold in WIDTH columns (default = 79). 1951# If PREFIX is given, each line is prefixed with it. If FIRST-PREFIX is 1952# specified, then the first line is prefixed with it. As a special case, 1953# if the length of FIRST-PREFIX is greater than that of PREFIX, then 1954# FIRST-PREFIX will be left alone on the first line. 1955# 1956# No expansion occurs on the contents STRING, PREFIX, or FIRST-PREFIX, 1957# although quadrigraphs are correctly recognized. 1958# 1959# Typical outputs are: 1960# 1961# m4_text_wrap([Short string */], [ ], [/* ], 20) 1962# => /* Short string */ 1963# 1964# m4_text_wrap([Much longer string */], [ ], [/* ], 20) 1965# => /* Much longer 1966# => string */ 1967# 1968# m4_text_wrap([Short doc.], [ ], [ --short ], 30) 1969# => --short Short doc. 1970# 1971# m4_text_wrap([Short doc.], [ ], [ --too-wide ], 30) 1972# => --too-wide 1973# => Short doc. 1974# 1975# m4_text_wrap([Super long documentation.], [ ], [ --too-wide ], 30) 1976# => --too-wide 1977# => Super long 1978# => documentation. 1979# 1980# FIXME: there is no checking of a longer PREFIX than WIDTH, but do 1981# we really want to bother with people trying each single corner 1982# of a software? 1983# 1984# This macro does not leave a trailing space behind the last word of a line, 1985# which complicates it a bit. The algorithm is otherwise stupid and simple: 1986# all the words are preceded by m4_Separator which is defined to empty for 1987# the first word, and then ` ' (single space) for all the others. 1988# 1989# The algorithm uses a helper that uses $2 through $4 directly, rather than 1990# using local variables, to avoid m4_defn overhead, or expansion swallowing 1991# any $. It also bypasses m4_popdef overhead with m4_builtin since no user 1992# macro expansion occurs in the meantime. Also, the definition is written 1993# with m4_do, to avoid time wasted on dnl during expansion (since this is 1994# already a time-consuming macro). 1995m4_define([m4_text_wrap], 1996[_$0([$1], [$2], m4_if([$3], [], [[$2]], [[$3]]), 1997 m4_if([$4], [], [79], [[$4]]))]) 1998m4_define([_m4_text_wrap], 1999m4_do(dnl set up local variables, to avoid repeated calculations 2000[[m4_pushdef([m4_Indent], m4_qlen([$2]))]], 2001[[m4_pushdef([m4_Cursor], m4_qlen([$3]))]], 2002[[m4_pushdef([m4_Separator], [m4_define([m4_Separator], [ ])])]], 2003dnl expand the first prefix, then check its length vs. regular prefix 2004dnl same length: nothing special 2005dnl prefix1 longer: output on line by itself, and reset cursor 2006dnl prefix1 shorter: pad to length of prefix, and reset cursor 2007[[[$3]m4_cond([m4_Cursor], m4_Indent, [], 2008 [m4_eval(m4_Cursor > m4_Indent)], [1], [ 2009[$2]m4_define([m4_Cursor], m4_Indent)], 2010 [m4_format([%*s], m4_max([0], 2011 m4_eval(m4_Indent - m4_Cursor)), [])m4_define([m4_Cursor], m4_Indent)])]], 2012dnl now, for each word, compute the curser after the word is output, then 2013dnl check if the cursor would exceed the wrap column 2014dnl if so, reset cursor, and insert newline and prefix 2015dnl if not, insert the separator (usually a space) 2016dnl either way, insert the word 2017[[m4_foreach_w([m4_Word], [$1], 2018 [m4_define([m4_Cursor], 2019 m4_eval(m4_Cursor + m4_qlen(m4_builtin([defn], [m4_Word])) 2020 + 1))m4_if(m4_eval(m4_Cursor > ([$4])), 2021 [1], [m4_define([m4_Cursor], 2022 m4_eval(m4_Indent 2023 + m4_qlen(m4_builtin([defn], [m4_Word])) + 1)) 2024[$2]], 2025 [m4_Separator[]])m4_builtin([defn], [m4_Word])])]], 2026dnl finally, clean up the local variabls 2027[[m4_builtin([popdef], [m4_Separator])]], 2028[[m4_builtin([popdef], [m4_Cursor])]], 2029[[m4_builtin([popdef], [m4_Indent])]])) 2030 2031 2032# m4_text_box(MESSAGE, [FRAME-CHARACTER = `-']) 2033# --------------------------------------------- 2034# Turn MESSAGE into: 2035# ## ------- ## 2036# ## MESSAGE ## 2037# ## ------- ## 2038# using FRAME-CHARACTER in the border. 2039m4_define([m4_text_box], 2040[m4_pushdef([m4_Border], 2041 m4_translit(m4_format([%*s], m4_qlen(m4_expand([$1])), []), 2042 [ ], m4_if([$2], [], [[-]], [[$2]])))dnl 2043@%:@@%:@ m4_Border @%:@@%:@ 2044@%:@@%:@ $1 @%:@@%:@ 2045@%:@@%:@ m4_Border @%:@@%:@dnl 2046m4_builtin([popdef], [m4_Border])dnl 2047]) 2048 2049 2050# m4_qlen(STRING) 2051# --------------- 2052# Expands to the length of STRING after autom4te converts all quadrigraphs. 2053# 2054# Avoid bpatsubsts for the common case of no quadrigraphs. 2055m4_define([m4_qlen], 2056[m4_if(m4_index([$1], [@]), [-1], [m4_len([$1])], 2057 [m4_len(m4_bpatsubst([[$1]], 2058 [@\(\(<:\|:>\|S|\|%:\|\{:\|:\}\)\(@\)\|&t@\)], 2059 [\3]))])]) 2060 2061 2062# m4_qdelta(STRING) 2063# ----------------- 2064# Expands to the net change in the length of STRING from autom4te converting the 2065# quadrigraphs in STRING. This number is always negative or zero. 2066m4_define([m4_qdelta], 2067[m4_eval(m4_qlen([$1]) - m4_len([$1]))]) 2068 2069 2070 2071## ----------------------- ## 2072## 13. Number processing. ## 2073## ----------------------- ## 2074 2075# m4_cmp(A, B) 2076# ------------ 2077# Compare two integer expressions. 2078# A < B -> -1 2079# A = B -> 0 2080# A > B -> 1 2081m4_define([m4_cmp], 2082[m4_eval((([$1]) > ([$2])) - (([$1]) < ([$2])))]) 2083 2084 2085# m4_list_cmp(A, B) 2086# ----------------- 2087# 2088# Compare the two lists of integer expressions A and B. For instance: 2089# m4_list_cmp([1, 0], [1]) -> 0 2090# m4_list_cmp([1, 0], [1, 0]) -> 0 2091# m4_list_cmp([1, 2], [1, 0]) -> 1 2092# m4_list_cmp([1, 2, 3], [1, 2]) -> 1 2093# m4_list_cmp([1, 2, -3], [1, 2]) -> -1 2094# m4_list_cmp([1, 0], [1, 2]) -> -1 2095# m4_list_cmp([1], [1, 2]) -> -1 2096# m4_define([xa], [oops])dnl 2097# m4_list_cmp([[0xa]], [5+5]) -> 0 2098# 2099# Rather than face the overhead of m4_case, we use a helper function whose 2100# expansion includes the name of the macro to invoke on the tail, either 2101# m4_ignore or m4_unquote. This is particularly useful when comparing 2102# long lists, since less text is being expanded for deciding when to end 2103# recursion. 2104m4_define([m4_list_cmp], 2105[m4_if([$1$2], [], 0, 2106 [$1], [], [$0(0, [$2])], 2107 [$2], [], [$0([$1], 0)], 2108 [$1], [$2], 0, 2109 [_$0(m4_cmp(m4_car($1), m4_car($2)))([$0(m4_cdr($1), m4_cdr($2))])])]) 2110m4_define([_m4_list_cmp], 2111[m4_if([$1], 0, [m4_unquote], [$1m4_ignore])]) 2112 2113# m4_max(EXPR, ...) 2114# m4_min(EXPR, ...) 2115# ----------------- 2116# Return the decimal value of the maximum (or minimum) in a series of 2117# integer expressions. 2118# 2119# M4 1.4.x doesn't provide ?:. Hence this huge m4_eval. Avoid m4_eval 2120# if both arguments are identical, but be aware of m4_max(0xa, 10) (hence 2121# the use of <=, not just <, in the second multiply). 2122m4_define([m4_max], 2123[m4_if([$#], [0], [m4_fatal([too few arguments to $0])], 2124 [$#], [1], [m4_eval([$1])], 2125 [$#$1], [2$2], [m4_eval([$1])], 2126 [$#], [2], 2127 [m4_eval((([$1]) > ([$2])) * ([$1]) + (([$1]) <= ([$2])) * ([$2]))], 2128 [$0($0([$1], [$2]), m4_shift2($@))])]) 2129m4_define([m4_min], 2130[m4_if([$#], [0], [m4_fatal([too few arguments to $0])], 2131 [$#], [1], [m4_eval([$1])], 2132 [$#$1], [2$2], [m4_eval([$1])], 2133 [$#], [2], 2134 [m4_eval((([$1]) < ([$2])) * ([$1]) + (([$1]) >= ([$2])) * ([$2]))], 2135 [$0($0([$1], [$2]), m4_shift2($@))])]) 2136 2137 2138# m4_sign(A) 2139# ---------- 2140# The sign of the integer expression A. 2141m4_define([m4_sign], 2142[m4_eval((([$1]) > 0) - (([$1]) < 0))]) 2143 2144 2145 2146## ------------------------ ## 2147## 14. Version processing. ## 2148## ------------------------ ## 2149 2150 2151# m4_version_unletter(VERSION) 2152# ---------------------------- 2153# Normalize beta version numbers with letters to numeric expressions, which 2154# can then be handed to m4_eval for the purpose of comparison. 2155# 2156# Nl -> (N+1).-1.(l#) 2157# 2158# for example: 2159# [2.14a] -> [2.14+1.-1.[0r36:a]] -> 2.15.-1.10 2160# [2.14b] -> [2.15+1.-1.[0r36:b]] -> 2.15.-1.11 2161# [2.61aa.b] -> [2.61+1.-1.[0r36:aa],+1.-1.[0r36:b]] -> 2.62.-1.370.1.-1.11 2162# 2163# This macro expects reasonable version numbers, but can handle double 2164# letters and does not expand any macros. Original version strings can 2165# use both `.' and `-' separators. 2166# 2167# Inline constant expansions, to avoid m4_defn overhead. 2168# _m4_version_unletter is the real workhorse used by m4_version_compare, 2169# but since [0r36:a] is less readable than 10, we provide a wrapper for 2170# human use. 2171m4_define([m4_version_unletter], 2172[m4_map_sep([m4_eval], [.], 2173 m4_dquote(m4_dquote_elt(m4_unquote(_$0([$1])))))]) 2174m4_define([_m4_version_unletter], 2175[m4_bpatsubst(m4_translit([[[$1]]], [.-], [,,]),]dnl 2176m4_dquote(m4_dquote(m4_defn([m4_cr_Letters])))[[+], 2177 [+1,-1,[0r36:\&]])]) 2178 2179 2180# m4_version_compare(VERSION-1, VERSION-2) 2181# ---------------------------------------- 2182# Compare the two version numbers and expand into 2183# -1 if VERSION-1 < VERSION-2 2184# 0 if = 2185# 1 if > 2186m4_define([m4_version_compare], 2187[m4_list_cmp(_m4_version_unletter([$1]), _m4_version_unletter([$2]))]) 2188 2189 2190## ------------------- ## 2191## 15. File handling. ## 2192## ------------------- ## 2193 2194 2195# It is a real pity that M4 comes with no macros to bind a diversion 2196# to a file. So we have to deal without, which makes us a lot more 2197# fragile than we should. 2198 2199 2200# m4_file_append(FILE-NAME, CONTENT) 2201# ---------------------------------- 2202m4_define([m4_file_append], 2203[m4_syscmd([cat >>$1 <<_m4eof 2204$2 2205_m4eof 2206]) 2207m4_if(m4_sysval, [0], [], 2208 [m4_fatal([$0: cannot write: $1])])]) 2209 2210 2211 2212## ------------------------ ## 2213## 16. Setting M4sugar up. ## 2214## ------------------------ ## 2215 2216 2217# m4_init 2218# ------- 2219# Initialize the m4sugar language. 2220m4_define([m4_init], 2221[# We need a tmp directory. 2222m4_ifndef([m4_tmpdir], 2223 [m4_define([m4_tmpdir], [/tmp])]) 2224 2225# M4sugar reserves `m4_[A-Za-z0-9_]*'. We'd need \b and +, 2226# but they are not portable. 2227m4_pattern_forbid([^m4_]) 2228m4_pattern_forbid([^dnl$]) 2229 2230# _m4_divert_diversion should be defined: 2231m4_divert_push([KILL]) 2232 2233# Check the divert push/pop perfect balance. 2234m4_wrap([m4_divert_pop([]) 2235 m4_ifdef([_m4_divert_diversion], 2236 [m4_fatal([$0: unbalanced m4_divert_push:]_m4_divert_n_stack)])[]]) 2237]) 2238