m4sugar.m4 120 KB

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