]>
Commit | Line | Data |
---|---|---|
ea6cfe9e AD |
1 | divert(-1)# -*- Autoconf -*- |
2 | # This file is part of Autoconf. | |
3 | # Base M4 layer. | |
4 | # Requires GNU M4. | |
5 | # Copyright 1999, 2000, 2001 Free Software Foundation, Inc. | |
6 | # | |
7 | # This program is free software; you can redistribute it and/or modify | |
8 | # it under the terms of the GNU General Public License as published by | |
9 | # the Free Software Foundation; either version 2, or (at your option) | |
10 | # any later version. | |
11 | # | |
12 | # This program is distributed in the hope that it will be useful, | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | # GNU General Public License for more details. | |
16 | # | |
17 | # You should have received a copy of the GNU General Public License | |
18 | # along with this program; if not, write to the Free Software | |
19 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
20 | # 02111-1307, USA. | |
21 | # | |
22 | # As a special exception, the Free Software Foundation gives unlimited | |
23 | # permission to copy, distribute and modify the configure scripts that | |
24 | # are the output of Autoconf. You need not follow the terms of the GNU | |
25 | # General Public License when using or distributing such scripts, even | |
26 | # though portions of the text of Autoconf appear in them. The GNU | |
27 | # General Public License (GPL) does govern all other use of the material | |
28 | # that constitutes the Autoconf program. | |
29 | # | |
30 | # Certain portions of the Autoconf source text are designed to be copied | |
31 | # (in certain cases, depending on the input) into the output of | |
32 | # Autoconf. We call these the "data" portions. The rest of the Autoconf | |
33 | # source text consists of comments plus executable code that decides which | |
34 | # of the data portions to output in any given case. We call these | |
35 | # comments and executable code the "non-data" portions. Autoconf never | |
36 | # copies any of the non-data portions into its output. | |
37 | # | |
38 | # This special exception to the GPL applies to versions of Autoconf | |
39 | # released by the Free Software Foundation. When you make and | |
40 | # distribute a modified version of Autoconf, you may extend this special | |
41 | # exception to the GPL to apply to your modified version as well, *unless* | |
42 | # your modified version has the potential to copy into its output some | |
43 | # of the text that was the non-data portion of the version that you started | |
44 | # with. (In other words, unless your change moves or copies text from | |
45 | # the non-data portions to the data portions.) If your modification has | |
46 | # such potential, you must delete any notice of this special exception | |
47 | # to the GPL from your modified version. | |
48 | # | |
49 | # Written by Akim Demaille. | |
50 | # | |
51 | ||
52 | # Set the quotes, whatever the current quoting system. | |
53 | changequote() | |
54 | changequote([, ]) | |
55 | ||
56 | # Some old m4's don't support m4exit. But they provide | |
57 | # equivalent functionality by core dumping because of the | |
58 | # long macros we define. | |
59 | ifdef([__gnu__], , | |
60 | [errprint(M4sugar requires GNU M4. Install it before installing M4sugar or | |
61 | set the M4 environment variable to its path name.) | |
62 | m4exit(2)]) | |
63 | ||
64 | ||
65 | ## ------------------------------- ## | |
66 | ## 1. Simulate --prefix-builtins. ## | |
67 | ## ------------------------------- ## | |
68 | ||
69 | # m4_define | |
70 | # m4_defn | |
71 | # m4_undefine | |
72 | define([m4_define], defn([define])) | |
73 | define([m4_defn], defn([defn])) | |
74 | define([m4_undefine], defn([undefine])) | |
75 | ||
76 | m4_undefine([define]) | |
77 | m4_undefine([defn]) | |
78 | m4_undefine([undefine]) | |
79 | ||
80 | ||
81 | # m4_copy(SRC, DST) | |
82 | # ----------------- | |
83 | # Define DST as the definition of SRC. | |
84 | # What's the difference between: | |
85 | # 1. m4_copy([from], [to]) | |
86 | # 2. m4_define([from], [to($@)]) | |
87 | # Well, obviously 1 is more expansive in space. Maybe 2 is more expansive | |
88 | # in time, but because of the space cost of 1, it's not that obvious. | |
89 | # Nevertheless, one huge difference is the handling of `$0'. If `from' | |
90 | # uses `$0', then with 1, `to''s `$0' is `to', while it is `from' in 2. | |
91 | # The user will certainly prefer see `from'. | |
92 | m4_define([m4_copy], | |
93 | [m4_define([$2], m4_defn([$1]))]) | |
94 | ||
95 | ||
96 | # m4_rename(SRC, DST) | |
97 | # ------------------- | |
98 | # Rename the macro SRC as DST. | |
99 | m4_define([m4_rename], | |
100 | [m4_copy([$1], [$2])m4_undefine([$1])]) | |
101 | ||
102 | ||
103 | # m4_rename_m4(MACRO-NAME) | |
104 | # ------------------------ | |
105 | # Rename MACRO-NAME as m4_MACRO-NAME. | |
106 | m4_define([m4_rename_m4], | |
107 | [m4_rename([$1], [m4_$1])]) | |
108 | ||
109 | ||
110 | # m4_copy_unm4(m4_MACRO-NAME) | |
111 | # --------------------------- | |
112 | # Copy m4_MACRO-NAME as MACRO-NAME. | |
113 | m4_define([m4_copy_unm4], | |
114 | [m4_copy([$1], m4_bpatsubst([$1], [^m4_\(.*\)], [[\1]]))]) | |
115 | ||
116 | ||
117 | # Some m4 internals have names colliding with tokens we might use. | |
118 | # Rename them a` la `m4 --prefix-builtins'. | |
119 | m4_rename_m4([builtin]) | |
120 | m4_rename_m4([changecom]) | |
121 | m4_rename_m4([changequote]) | |
122 | m4_rename_m4([debugfile]) | |
123 | m4_rename_m4([debugmode]) | |
124 | m4_rename_m4([decr]) | |
125 | m4_undefine([divert]) | |
126 | m4_rename_m4([divnum]) | |
127 | m4_rename_m4([dumpdef]) | |
128 | m4_rename_m4([errprint]) | |
129 | m4_rename_m4([esyscmd]) | |
130 | m4_rename_m4([eval]) | |
131 | m4_rename_m4([format]) | |
132 | m4_rename_m4([ifdef]) | |
133 | m4_rename([ifelse], [m4_if]) | |
134 | m4_rename_m4([include]) | |
135 | m4_rename_m4([incr]) | |
136 | m4_rename_m4([index]) | |
137 | m4_rename_m4([indir]) | |
138 | m4_rename_m4([len]) | |
139 | m4_rename([m4exit], [m4_exit]) | |
140 | m4_rename([m4wrap], [m4_wrap]) | |
141 | m4_rename_m4([maketemp]) | |
142 | m4_rename([patsubst], [m4_bpatsubst]) | |
143 | m4_undefine([popdef]) | |
144 | m4_rename_m4([pushdef]) | |
145 | m4_rename([regexp], [m4_bregexp]) | |
146 | m4_rename_m4([shift]) | |
147 | m4_rename_m4([sinclude]) | |
148 | m4_rename_m4([substr]) | |
149 | m4_rename_m4([symbols]) | |
150 | m4_rename_m4([syscmd]) | |
151 | m4_rename_m4([sysval]) | |
152 | m4_rename_m4([traceoff]) | |
153 | m4_rename_m4([traceon]) | |
154 | m4_rename_m4([translit]) | |
155 | m4_undefine([undivert]) | |
156 | ||
157 | ||
158 | ## ------------------- ## | |
159 | ## 2. Error messages. ## | |
160 | ## ------------------- ## | |
161 | ||
162 | ||
163 | # m4_location | |
164 | # ----------- | |
165 | m4_define([m4_location], | |
166 | [__file__:__line__]) | |
167 | ||
168 | ||
169 | # m4_errprintn(MSG) | |
170 | # ----------------- | |
171 | # Same as `errprint', but with the missing end of line. | |
172 | m4_define([m4_errprintn], | |
173 | [m4_errprint([$1 | |
174 | ])]) | |
175 | ||
176 | ||
177 | # m4_warning(MSG) | |
178 | # --------------- | |
179 | # Warn the user. | |
180 | m4_define([m4_warning], | |
181 | [m4_errprintn(m4_location[: warning: $1])]) | |
182 | ||
183 | ||
184 | # m4_fatal(MSG, [EXIT-STATUS]) | |
185 | # ---------------------------- | |
186 | # Fatal the user. :) | |
187 | m4_define([m4_fatal], | |
188 | [m4_errprintn(m4_location[: error: $1])dnl | |
189 | m4_expansion_stack_dump()dnl | |
190 | m4_exit(m4_if([$2],, 1, [$2]))]) | |
191 | ||
192 | ||
193 | # m4_assert(EXPRESSION, [EXIT-STATUS = 1]) | |
194 | # ---------------------------------------- | |
195 | # This macro ensures that EXPRESSION evaluates to true, and exits if | |
196 | # EXPRESSION evaluates to false. | |
197 | m4_define([m4_assert], | |
198 | [m4_if(m4_eval([$1]), 0, | |
199 | [m4_fatal([assert failed: $1], [$2])])]) | |
200 | ||
201 | ||
202 | ## ------------- ## | |
203 | ## 3. Warnings. ## | |
204 | ## ------------- ## | |
205 | ||
206 | ||
207 | # m4_warning_ifelse(CATEGORY, IF-TRUE, IF-FALSE) | |
208 | # ---------------------------------------------- | |
209 | # If the CATEGORY of warnings is enabled, expand IF_TRUE otherwise | |
210 | # IF-FALSE. | |
211 | # | |
212 | # The variable `m4_warnings' contains a comma separated list of | |
213 | # warnings which order is the converse from the one specified by | |
214 | # the user, i.e., if she specified `-W error,none,obsolete', | |
215 | # `m4_warnings' is `obsolete,none,error'. We read it from left to | |
216 | # right, and: | |
217 | # - if none or noCATEGORY is met, run IF-FALSE | |
218 | # - if all or CATEGORY is met, run IF-TRUE | |
219 | # - if there is nothing left, run IF-FALSE. | |
220 | m4_define([m4_warning_ifelse], | |
221 | [_m4_warning_ifelse([$1], [$2], [$3], m4_warnings)]) | |
222 | ||
223 | ||
224 | # _m4_warning_ifelse(CATEGORY, IF-TRUE, IF-FALSE, WARNING1, ...) | |
225 | # -------------------------------------------------------------- | |
226 | # Implementation of the loop described above. | |
227 | m4_define([_m4_warning_ifelse], | |
228 | [m4_case([$4], | |
229 | [$1], [$2], | |
230 | [all], [$2], | |
231 | [], [$3], | |
232 | [none], [$3], | |
233 | [no-$1], [$3], | |
234 | [$0([$1], [$2], [$3], m4_shiftn(4, $@))])]) | |
235 | ||
236 | ||
237 | # _m4_warning_error_ifelse(IF-TRUE, IF-FALSE) | |
238 | # ------------------------------------------- | |
239 | # The same as m4_warning_ifelse, but scan for `error' only. | |
240 | m4_define([_m4_warning_error_ifelse], | |
241 | [__m4_warning_error_ifelse([$1], [$2], m4_warnings)]) | |
242 | ||
243 | ||
244 | # __m4_warning_error_ifelse(IF-TRUE, IF-FALSE) | |
245 | # -------------------------------------------- | |
246 | # The same as _m4_warning_ifelse, but scan for `error' only. | |
247 | m4_define([__m4_warning_error_ifelse], | |
248 | [m4_case([$3], | |
249 | [error], [$1], | |
250 | [], [$2], | |
251 | [no-error], [$2], | |
252 | [$0([$1], [$2], m4_shiftn(3, $@))])]) | |
253 | ||
254 | ||
255 | ||
256 | # _m4_warn(MESSAGE) | |
257 | # ----------------- | |
258 | # Report MESSAGE as a warning, unless the user requested -W error, | |
259 | # in which case report a fatal error. | |
260 | m4_define([_m4_warn], | |
261 | [_m4_warning_error_ifelse([m4_fatal([$1])], | |
262 | [m4_warning([$1])])]) | |
263 | ||
264 | ||
265 | # m4_warn(CATEGORY, MESSAGE) | |
266 | # -------------------------- | |
267 | # Report a MESSAGE to the autoconf user if the CATEGORY of warnings | |
268 | # is requested (in fact, not disabled). | |
269 | m4_define([m4_warn], | |
270 | [m4_warning_ifelse([$1], [_m4_warn([$2])])]) | |
271 | ||
272 | ||
273 | ||
274 | ||
275 | ## ------------------- ## | |
276 | ## 4. File inclusion. ## | |
277 | ## ------------------- ## | |
278 | ||
279 | ||
280 | # We also want to neutralize include (and sinclude for symmetry), | |
281 | # but we want to extend them slightly: warn when a file is included | |
282 | # several times. This is in general a dangerous operation because | |
283 | # quite nobody quotes the first argument of m4_define. | |
284 | # | |
285 | # For instance in the following case: | |
286 | # m4_define(foo, [bar]) | |
287 | # then a second reading will turn into | |
288 | # m4_define(bar, [bar]) | |
289 | # which is certainly not what was meant. | |
290 | ||
291 | # m4_include_unique(FILE) | |
292 | # ----------------------- | |
293 | # Declare that the FILE was loading; and warn if it has already | |
294 | # been included. | |
295 | m4_define([m4_include_unique], | |
296 | [m4_ifdef([m4_include($1)], | |
297 | [m4_warn([syntax], [file `$1' included several times])])dnl | |
298 | m4_define([m4_include($1)])]) | |
299 | ||
300 | ||
301 | # m4_include(FILE) | |
302 | # ---------------- | |
303 | # As the builtin include, but warns against multiple inclusions. | |
304 | m4_define([m4_include], | |
305 | [m4_include_unique([$1])dnl | |
306 | m4_builtin([include], [$1])]) | |
307 | ||
308 | ||
309 | # m4_sinclude(FILE) | |
310 | # ----------------- | |
311 | # As the builtin sinclude, but warns against multiple inclusions. | |
312 | m4_define([m4_sinclude], | |
313 | [m4_include_unique([$1])dnl | |
314 | m4_builtin([sinclude], [$1])]) | |
315 | ||
316 | ||
317 | ||
318 | ## ------------------------------------ ## | |
319 | ## 5. Additional branching constructs. ## | |
320 | ## ------------------------------------ ## | |
321 | ||
322 | # Both `m4_ifval' and `m4_ifset' tests against the empty string. The | |
323 | # difference is that `m4_ifset' is specialized on macros. | |
324 | # | |
325 | # In case of arguments of macros, eg $[1], it makes little difference. | |
326 | # In the case of a macro `FOO', you don't want to check `m4_ifval(FOO, | |
327 | # TRUE)', because if `FOO' expands with commas, there is a shifting of | |
328 | # the arguments. So you want to run `m4_ifval([FOO])', but then you just | |
329 | # compare the *string* `FOO' against `', which, of course fails. | |
330 | # | |
331 | # So you want a variation of `m4_ifset' that expects a macro name as $[1]. | |
332 | # If this macro is both defined and defined to a non empty value, then | |
333 | # it runs TRUE etc. | |
334 | ||
335 | ||
336 | # m4_ifval(COND, [IF-TRUE], [IF-FALSE]) | |
337 | # ------------------------------------- | |
338 | # If COND is not the empty string, expand IF-TRUE, otherwise IF-FALSE. | |
339 | # Comparable to m4_ifdef. | |
340 | m4_define([m4_ifval], | |
341 | [m4_if([$1], [], [$3], [$2])]) | |
342 | ||
343 | ||
344 | # m4_n(TEXT) | |
345 | # ---------- | |
346 | # If TEXT is not empty, return TEXT and a new line, otherwise nothing. | |
347 | m4_define([m4_n], | |
348 | [m4_if([$1], | |
349 | [], [], | |
350 | [$1 | |
351 | ])]) | |
352 | ||
353 | ||
354 | # m4_ifvaln(COND, [IF-TRUE], [IF-FALSE]) | |
355 | # -------------------------------------- | |
356 | # Same as `m4_ifval', but add an extra newline to IF-TRUE or IF-FALSE | |
357 | # unless that argument is empty. | |
358 | m4_define([m4_ifvaln], | |
359 | [m4_if([$1], | |
360 | [], [m4_n([$3])], | |
361 | [m4_n([$2])])]) | |
362 | ||
363 | ||
364 | # m4_ifset(MACRO, [IF-TRUE], [IF-FALSE]) | |
365 | # -------------------------------------- | |
366 | # If MACRO has no definition, or of its definition is the empty string, | |
367 | # expand IF-FALSE, otherwise IF-TRUE. | |
368 | m4_define([m4_ifset], | |
369 | [m4_ifdef([$1], | |
370 | [m4_if(m4_defn([$1]), [], [$3], [$2])], | |
371 | [$3])]) | |
372 | ||
373 | ||
374 | # m4_ifndef(NAME, [IF-NOT-DEFINED], [IF-DEFINED]) | |
375 | # ----------------------------------------------- | |
376 | m4_define([m4_ifndef], | |
377 | [m4_ifdef([$1], [$3], [$2])]) | |
378 | ||
379 | ||
380 | # m4_case(SWITCH, VAL1, IF-VAL1, VAL2, IF-VAL2, ..., DEFAULT) | |
381 | # ----------------------------------------------------------- | |
382 | # m4 equivalent of | |
383 | # switch (SWITCH) | |
384 | # { | |
385 | # case VAL1: | |
386 | # IF-VAL1; | |
387 | # break; | |
388 | # case VAL2: | |
389 | # IF-VAL2; | |
390 | # break; | |
391 | # ... | |
392 | # default: | |
393 | # DEFAULT; | |
394 | # break; | |
395 | # }. | |
396 | # All the values are optional, and the macro is robust to active | |
397 | # symbols properly quoted. | |
398 | m4_define([m4_case], | |
399 | [m4_if([$#], 0, [], | |
400 | [$#], 1, [], | |
401 | [$#], 2, [$2], | |
402 | [$1], [$2], [$3], | |
403 | [$0([$1], m4_shiftn(3, $@))])]) | |
404 | ||
405 | ||
406 | # m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT) | |
407 | # ----------------------------------------------------- | |
408 | # m4 equivalent of | |
409 | # | |
410 | # if (SWITCH =~ RE1) | |
411 | # VAL1; | |
412 | # elif (SWITCH =~ RE2) | |
413 | # VAL2; | |
414 | # elif ... | |
415 | # ... | |
416 | # else | |
417 | # DEFAULT | |
418 | # | |
419 | # All the values are optional, and the macro is robust to active symbols | |
420 | # properly quoted. | |
421 | m4_define([m4_bmatch], | |
422 | [m4_if([$#], 0, [], | |
423 | [$#], 1, [], | |
424 | [$#], 2, [$2], | |
425 | m4_bregexp([$1], [$2]), -1, [$0([$1], m4_shiftn(3, $@))], | |
426 | [$3])]) | |
427 | ||
428 | ||
429 | ||
430 | ## ---------------------------------------- ## | |
431 | ## 6. Enhanced version of some primitives. ## | |
432 | ## ---------------------------------------- ## | |
433 | ||
434 | # m4_patsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...) | |
435 | # --------------------------------------------------- | |
436 | # m4 equivalent of | |
437 | # | |
438 | # $_ = STRING; | |
439 | # s/RE1/SUBST1/g; | |
440 | # s/RE2/SUBST2/g; | |
441 | # ... | |
442 | # | |
443 | # All the values are optional, and the macro is robust to active symbols | |
444 | # properly quoted. | |
445 | # | |
446 | # I would have liked to name this macro `m4_patsubst', unfortunately, | |
447 | # due to quotation problems, I need to double quote $1 below, therefore | |
448 | # the anchors are broken :( I can't let users be trapped by that. | |
449 | m4_define([m4_bpatsubsts], | |
450 | [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])], | |
451 | [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])], | |
452 | [$#], 2, [m4_builtin([patsubst], $@)], | |
453 | [$0(m4_builtin([patsubst], [[$1]], [$2], [$3]), | |
454 | m4_shiftn(3, $@))])]) | |
455 | ||
456 | ||
457 | ||
458 | # m4_do(STRING, ...) | |
459 | # ------------------ | |
460 | # This macro invokes all its arguments (in sequence, of course). It is | |
461 | # useful for making your macros more structured and readable by dropping | |
462 | # unecessary dnl's and have the macros indented properly. | |
463 | m4_define([m4_do], | |
464 | [m4_if($#, 0, [], | |
465 | $#, 1, [$1], | |
466 | [$1[]m4_do(m4_shift($@))])]) | |
467 | ||
468 | ||
469 | # m4_default(EXP1, EXP2) | |
470 | # ---------------------- | |
471 | # Returns EXP1 if non empty, otherwise EXP2. | |
472 | m4_define([m4_default], | |
473 | [m4_ifval([$1], [$1], [$2])]) | |
474 | ||
475 | ||
476 | # m4_defn(NAME) | |
477 | # ------------- | |
478 | # Unlike to the original, don't tolerate popping something which is | |
479 | # undefined. | |
480 | m4_define([m4_defn], | |
481 | [m4_ifndef([$1], | |
482 | [m4_fatal([$0: undefined macro: $1])])dnl | |
483 | m4_builtin([defn], $@)]) | |
484 | ||
485 | ||
486 | # _m4_dumpdefs_up(NAME) | |
487 | # --------------------- | |
488 | m4_define([_m4_dumpdefs_up], | |
489 | [m4_ifdef([$1], | |
490 | [m4_pushdef([_m4_dumpdefs], m4_defn([$1]))dnl | |
491 | m4_dumpdef([$1])dnl | |
492 | m4_popdef([$1])dnl | |
493 | _m4_dumpdefs_up([$1])])]) | |
494 | ||
495 | ||
496 | # _m4_dumpdefs_down(NAME) | |
497 | # ----------------------- | |
498 | m4_define([_m4_dumpdefs_down], | |
499 | [m4_ifdef([_m4_dumpdefs], | |
500 | [m4_pushdef([$1], m4_defn([_m4_dumpdefs]))dnl | |
501 | m4_popdef([_m4_dumpdefs])dnl | |
502 | _m4_dumpdefs_down([$1])])]) | |
503 | ||
504 | ||
505 | # m4_dumpdefs(NAME) | |
506 | # ----------------- | |
507 | # Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its | |
508 | # value stack (most recent displayed first). | |
509 | m4_define([m4_dumpdefs], | |
510 | [_m4_dumpdefs_up([$1])dnl | |
511 | _m4_dumpdefs_down([$1])]) | |
512 | ||
513 | ||
514 | # m4_popdef(NAME) | |
515 | # --------------- | |
516 | # Unlike to the original, don't tolerate popping something which is | |
517 | # undefined. | |
518 | m4_define([m4_popdef], | |
519 | [m4_ifndef([$1], | |
520 | [m4_fatal([$0: undefined macro: $1])])dnl | |
521 | m4_builtin([popdef], $@)]) | |
522 | ||
523 | ||
524 | # m4_quote(STRING) | |
525 | # ---------------- | |
526 | # Return STRING quoted. | |
527 | # | |
528 | # It is important to realize the difference between `m4_quote(exp)' and | |
529 | # `[exp]': in the first case you obtain the quoted *result* of the | |
530 | # expansion of EXP, while in the latter you just obtain the string | |
531 | # `exp'. | |
532 | m4_define([m4_quote], [[$*]]) | |
533 | m4_define([m4_dquote], [[[$*]]]) | |
534 | ||
535 | ||
536 | # m4_noquote(STRING) | |
537 | # ------------------ | |
538 | # Return the result of ignoring all quotes in STRING and invoking the | |
539 | # macros it contains. Amongst other things useful for enabling macro | |
540 | # invocations inside strings with [] blocks (for instance regexps and | |
541 | # help-strings). | |
542 | m4_define([m4_noquote], | |
543 | [m4_changequote(-=<{,}>=-)$1-=<{}>=-m4_changequote([,])]) | |
544 | ||
545 | ||
546 | # m4_shiftn(N, ...) | |
547 | # ----------------- | |
548 | # Returns ... shifted N times. Useful for recursive "varargs" constructs. | |
549 | m4_define([m4_shiftn], | |
550 | [m4_assert(($1 >= 0) && ($# > $1))dnl | |
551 | _m4_shiftn($@)]) | |
552 | ||
553 | m4_define([_m4_shiftn], | |
554 | [m4_if([$1], 0, | |
555 | [m4_shift($@)], | |
556 | [_m4_shiftn(m4_eval([$1]-1), m4_shift(m4_shift($@)))])]) | |
557 | ||
558 | ||
559 | # m4_undefine(NAME) | |
560 | # ----------------- | |
561 | # Unlike to the original, don't tolerate undefining something which is | |
562 | # undefined. | |
563 | m4_define([m4_undefine], | |
564 | [m4_ifndef([$1], | |
565 | [m4_fatal([$0: undefined macro: $1])])dnl | |
566 | m4_builtin([undefine], $@)]) | |
567 | ||
568 | ||
569 | ## -------------------------- ## | |
570 | ## 7. Implementing m4 loops. ## | |
571 | ## -------------------------- ## | |
572 | ||
573 | ||
574 | # m4_for(VARIABLE, FIRST, LAST, [STEP = +/-1], EXPRESSION) | |
575 | # -------------------------------------------------------- | |
576 | # Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO. | |
577 | # Both limits are included, and bounds are checked for consistency. | |
578 | m4_define([m4_for], | |
579 | [m4_case(m4_sign(m4_eval($3 - $2)), | |
580 | 1, [m4_assert(m4_sign(m4_default($4, 1)) == 1)], | |
581 | -1, [m4_assert(m4_sign(m4_default($4, -1)) == -1)])dnl | |
582 | m4_pushdef([$1], [$2])dnl | |
583 | m4_if(m4_eval([$3 > $2]), 1, | |
584 | [_m4_for([$1], [$3], m4_default([$4], 1), [$5])], | |
585 | [_m4_for([$1], [$3], m4_default([$4], -1), [$5])])dnl | |
586 | m4_popdef([$1])]) | |
587 | ||
588 | ||
589 | # _m4_for(VARIABLE, FIRST, LAST, STEP, EXPRESSION) | |
590 | # ------------------------------------------------ | |
591 | # Core of the loop, no consistency checks. | |
592 | m4_define([_m4_for], | |
593 | [$4[]dnl | |
594 | m4_if($1, [$2], [], | |
595 | [m4_define([$1], m4_eval($1+[$3]))_m4_for([$1], [$2], [$3], [$4])])]) | |
596 | ||
597 | ||
598 | # Implementing `foreach' loops in m4 is much more tricky than it may | |
599 | # seem. Actually, the example of a `foreach' loop in the m4 | |
600 | # documentation is wrong: it does not quote the arguments properly, | |
601 | # which leads to undesired expansions. | |
602 | # | |
603 | # The example in the documentation is: | |
604 | # | |
605 | # | # foreach(VAR, (LIST), STMT) | |
606 | # | m4_define([foreach], | |
607 | # | [m4_pushdef([$1])_foreach([$1], [$2], [$3])m4_popdef([$1])]) | |
608 | # | m4_define([_arg1], [$1]) | |
609 | # | m4_define([_foreach], | |
610 | # | [m4_if([$2], [()], , | |
611 | # | [m4_define([$1], _arg1$2)$3[]_foreach([$1], | |
612 | # | (shift$2), | |
613 | # | [$3])])]) | |
614 | # | |
615 | # But then if you run | |
616 | # | |
617 | # | m4_define(a, 1) | |
618 | # | m4_define(b, 2) | |
619 | # | m4_define(c, 3) | |
620 | # | foreach([f], [([a], [(b], [c)])], [echo f | |
621 | # | ]) | |
622 | # | |
623 | # it gives | |
624 | # | |
625 | # => echo 1 | |
626 | # => echo (2,3) | |
627 | # | |
628 | # which is not what is expected. | |
629 | # | |
630 | # Of course the problem is that many quotes are missing. So you add | |
631 | # plenty of quotes at random places, until you reach the expected | |
632 | # result. Alternatively, if you are a quoting wizard, you directly | |
633 | # reach the following implementation (but if you really did, then | |
634 | # apply to the maintenance of m4sugar!). | |
635 | # | |
636 | # | # foreach(VAR, (LIST), STMT) | |
637 | # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])]) | |
638 | # | m4_define([_arg1], [[$1]]) | |
639 | # | m4_define([_foreach], | |
640 | # | [m4_if($2, [()], , | |
641 | # | [m4_define([$1], [_arg1$2])$3[]_foreach([$1], | |
642 | # | [(shift$2)], | |
643 | # | [$3])])]) | |
644 | # | |
645 | # which this time answers | |
646 | # | |
647 | # => echo a | |
648 | # => echo (b | |
649 | # => echo c) | |
650 | # | |
651 | # Bingo! | |
652 | # | |
653 | # Well, not quite. | |
654 | # | |
655 | # With a better look, you realize that the parens are more a pain than | |
656 | # a help: since anyway you need to quote properly the list, you end up | |
657 | # with always using an outermost pair of parens and an outermost pair | |
658 | # of quotes. Rejecting the parens both eases the implementation, and | |
659 | # simplifies the use: | |
660 | # | |
661 | # | # foreach(VAR, (LIST), STMT) | |
662 | # | m4_define([foreach], [m4_pushdef([$1])_foreach($@)m4_popdef([$1])]) | |
663 | # | m4_define([_arg1], [$1]) | |
664 | # | m4_define([_foreach], | |
665 | # | [m4_if($2, [], , | |
666 | # | [m4_define([$1], [_arg1($2)])$3[]_foreach([$1], | |
667 | # | [shift($2)], | |
668 | # | [$3])])]) | |
669 | # | |
670 | # | |
671 | # Now, just replace the `$2' with `m4_quote($2)' in the outer `m4_if' | |
672 | # to improve robustness, and you come up with a quite satisfactory | |
673 | # implementation. | |
674 | ||
675 | ||
676 | # m4_foreach(VARIABLE, LIST, EXPRESSION) | |
677 | # -------------------------------------- | |
678 | # | |
679 | # Expand EXPRESSION assigning each value of the LIST to VARIABLE. | |
680 | # LIST should have the form `item_1, item_2, ..., item_n', i.e. the | |
681 | # whole list must *quoted*. Quote members too if you don't want them | |
682 | # to be expanded. | |
683 | # | |
684 | # This macro is robust to active symbols: | |
685 | # | m4_define(active, [ACT, IVE]) | |
686 | # | m4_foreach(Var, [active, active], [-Var-]) | |
687 | # => -ACT--IVE--ACT--IVE- | |
688 | # | |
689 | # | m4_foreach(Var, [[active], [active]], [-Var-]) | |
690 | # => -ACT, IVE--ACT, IVE- | |
691 | # | |
692 | # | m4_foreach(Var, [[[active]], [[active]]], [-Var-]) | |
693 | # => -active--active- | |
694 | m4_define([m4_foreach], | |
695 | [m4_pushdef([$1])_m4_foreach($@)m4_popdef([$1])]) | |
696 | ||
697 | # Low level macros used to define m4_foreach. | |
698 | m4_define([m4_car], [[$1]]) | |
699 | m4_define([m4_car2], [[$@]]) | |
700 | m4_define([_m4_foreach], | |
701 | [m4_if(m4_quote($2), [], [], | |
702 | [m4_define([$1], [m4_car($2)])$3[]_m4_foreach([$1], | |
703 | m4_car2(m4_shift($2)), | |
704 | [$3])])]) | |
705 | ||
706 | ||
707 | ||
708 | ## --------------------------- ## | |
709 | ## 8. More diversion support. ## | |
710 | ## --------------------------- ## | |
711 | ||
712 | ||
713 | # _m4_divert(DIVERSION-NAME or NUMBER) | |
714 | # ------------------------------------ | |
715 | # If DIVERSION-NAME is the name of a diversion, return its number, | |
716 | # otherwise if is a NUMBER return it. | |
717 | m4_define([_m4_divert], | |
718 | [m4_ifdef([_m4_divert($1)], | |
719 | [m4_indir([_m4_divert($1)])], | |
720 | [$1])]) | |
721 | ||
722 | # KILL is only used to suppress output. | |
723 | m4_define([_m4_divert(KILL)], -1) | |
724 | ||
725 | ||
726 | # m4_divert(DIVERSION-NAME) | |
727 | # ------------------------- | |
728 | # Change the diversion stream to DIVERSION-NAME. | |
729 | m4_define([m4_divert], | |
730 | [m4_define([m4_divert_stack], | |
731 | m4_location[: $0: $1]m4_ifdef([m4_divert_stack], [ | |
732 | m4_defn([m4_divert_stack])]))dnl | |
733 | m4_builtin([divert], _m4_divert([$1]))dnl | |
734 | ]) | |
735 | ||
736 | ||
737 | # m4_divert_push(DIVERSION-NAME) | |
738 | # ------------------------------ | |
739 | # Change the diversion stream to DIVERSION-NAME, while stacking old values. | |
740 | m4_define([m4_divert_push], | |
741 | [m4_pushdef([m4_divert_stack], | |
742 | m4_location[: $0: $1]m4_ifdef([m4_divert_stack], [ | |
743 | m4_defn([m4_divert_stack])]))dnl | |
744 | m4_pushdef([_m4_divert_diversion], [$1])dnl | |
745 | m4_builtin([divert], _m4_divert(_m4_divert_diversion))dnl | |
746 | ]) | |
747 | ||
748 | ||
749 | # m4_divert_pop([DIVERSION-NAME]) | |
750 | # ------------------------------- | |
751 | # Change the diversion stream to its previous value, unstacking it. | |
752 | # If specified, verify we left DIVERSION-NAME. | |
753 | m4_define([m4_divert_pop], | |
754 | [m4_ifval([$1], | |
755 | [m4_if(_m4_divert([$1]), m4_divnum, [], | |
756 | [m4_fatal([$0($1): diversion mismatch: ] | |
757 | m4_defn([m4_divert_stack]))])])dnl | |
758 | m4_popdef([_m4_divert_diversion])dnl | |
759 | dnl m4_ifndef([_m4_divert_diversion], | |
760 | dnl [m4_fatal([too many m4_divert_pop])])dnl | |
761 | m4_builtin([divert], | |
762 | m4_ifdef([_m4_divert_diversion], | |
763 | [_m4_divert(_m4_divert_diversion)], -1))dnl | |
764 | m4_popdef([m4_divert_stack])dnl | |
765 | ]) | |
766 | ||
767 | ||
768 | # m4_divert_text(DIVERSION-NAME, CONTENT) | |
769 | # --------------------------------------- | |
770 | # Output CONTENT into DIVERSION-NAME (which may be a number actually). | |
771 | # An end of line is appended for free to CONTENT. | |
772 | m4_define([m4_divert_text], | |
773 | [m4_divert_push([$1])dnl | |
774 | $2 | |
775 | m4_divert_pop([$1])dnl | |
776 | ]) | |
777 | ||
778 | ||
779 | # m4_divert_once(DIVERSION-NAME, CONTENT) | |
780 | # --------------------------------------- | |
781 | # Output once CONTENT into DIVERSION-NAME (which may be a number | |
782 | # actually). An end of line is appended for free to CONTENT. | |
783 | m4_define([m4_divert_once], | |
784 | [m4_expand_once([m4_divert_text([$1], [$2])])]) | |
785 | ||
786 | ||
787 | # m4_undivert(DIVERSION-NAME) | |
788 | # --------------------------- | |
789 | # Undivert DIVERSION-NAME. | |
790 | m4_define([m4_undivert], | |
791 | [m4_builtin([undivert], _m4_divert([$1]))]) | |
792 | ||
793 | ||
794 | ## -------------------------------------------- ## | |
795 | ## 8. Defining macros with bells and whistles. ## | |
796 | ## -------------------------------------------- ## | |
797 | ||
798 | # `m4_defun' is basically `m4_define' but it equips the macro with the | |
799 | # needed machinery for `m4_require'. A macro must be m4_defun'd if | |
800 | # either it is m4_require'd, or it m4_require's. | |
801 | # | |
802 | # Two things deserve attention and are detailed below: | |
803 | # 1. Implementation of m4_require | |
804 | # 2. Keeping track of the expansion stack | |
805 | # | |
806 | # 1. Implementation of m4_require | |
807 | # =============================== | |
808 | # | |
809 | # Of course m4_defun AC_PROVIDE's the macro, so that a macro which has | |
810 | # been expanded is not expanded again when m4_require'd, but the | |
811 | # difficult part is the proper expansion of macros when they are | |
812 | # m4_require'd. | |
813 | # | |
814 | # The implementation is based on two ideas, (i) using diversions to | |
815 | Content-type: text/html ]>