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