aboutsummaryrefslogtreecommitdiff
path: root/src/builtin/snarf.m4
blob: acb05ed928021c5340bf87b9b9603814ab0a3506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
m4_divert(-1)m4_dnl -*- m4 -*-
# This file is part of Mailfromd. 
# Copyright (C) 2006-2018 Sergey Poznyakoff
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

m4_changequote([<,>])
m4_changecom(/*,*/)

/* This qualifier is used when declaring C variables corresponding to
 * MFL STRING parameters.  If the MFL function is declared with MF_DSEXP
 * attribute (i.e. if it can cause dataseg expansion), __mf_dataseg is
 * defined to MFL_DATASEG (volatile), exempting the corresponding variable
 * from optimizations.
 */
m4_define([<__mf_dataseg>])

/* The MF_DSEXP attribute indicates that the MF_DEFUN that follows it
 * can cause dataseg expansion.
 */
m4_define([<MF_DSEXP>],[<m4_define([<__mf_dataseg>],[<MFL_DATASEG>])>])

/* __MF_DSEXP_REQ - an auxiliary macro which checks, whether __mf_dataseg
 * is empty.  If so, it issues a diagnostic message.
 *
 * The intent is to warn the programmer that the function in question can
 * result in dataseg being expanded.  The warning is suppressed by MF_DSEXP
 * attribute (for MF_DEFUNs) or by the use of MF_DSEXP_SUPPRESS macro (for
 * C functions).
 */
m4_define([<__MF_DSEXP_REQ>],[<m4_dnl
m4_ifelse(__mf_dataseg,[<>],[<m4_ifdef([<__MF_FUNCTION__>],[<m4_dnl
m4_errprint(m4___file__:m4___line__: Function __MF_FUNCTION__ should be defined with [<MF_DSEXP>] attribute
)>],m4_dnl
[<m4_errprint(m4___file__:m4___line__: this call can cause dataseg expansion; consider using [<MF_DSEXP_SUPPRESS>]
)>])>])>])

/* __mf_define_fun(NAME) - Defines a macro NAME, which, when invoked,
 * produces a warning if __mf_dataseg is not defined, and expands
 * to the literal NAME.
 */
m4_define([<__mf_define_fun>],[<m4_ifelse([<$#>],[<0>],[<[<$0>]>],
[<_$0([<$1>],[<$>][<*>])>])>])
m4_define([<___mf_define_fun>],[<
m4_define([<$1>],[<__MF_DSEXP_REQ()
[<$1>]([<$2>])>])>])

/* MF_DSEXP_SUPPRESS(NAME, DEFN)
 * -----------------------------
 * Suppresses dataseg expansion warnings within DEFN and redeclares NAME to
 * produce a warning if used in a context which is not safe for dataseg
 * expansion.
 */
m4_define([<MF_DSEXP_SUPPRESS>],[<m4_pushdef([<__mf_dataseg>],[</**/>])
$2
m4_popdef([<__mf_dataseg>])
__mf_define_fun([<$1>])>])

/* MF_MODULE_NAME()
 * ----------------
 * Expand to the name of this module.
 */
m4_pushdef([<MF_MODULE_NAME>])

/* MF_BUILTIN_MODULE([NAME])
 * -------------------------
 * Start MFL built-in module text.  Optional NAME is the name of the module.
 * If not supplied, the name is defined as the base name of the module file.
 */
m4_define([<MF_BUILTIN_MODULE>],m4_dnl
[<m4_pushdef([<MF_MODULE_NAME>],m4_dnl
m4_ifelse([<$1>],,[<m4_dnl
m4_patsubst(m4___file__,\(.*/\)?\(.*\)\.bi,\2)>],[<$1>]))>])m4_dnl

/* MF_MODULE_DEBUG_NAME
 * --------------------
 * Expand to the "debug" name for the MODULE.
 */
m4_define([<MF_MODULE_DEBUG_NAME>],[<m4_dnl
[<bi_>]m4_translit(MF_MODULE_NAME,[<A-Z->],[<a-z_>])>])

/* MF_MODULE_IDX()
 * ---------------
 * Expand to the BUILTIN_IDX_ constant for the current module.
 */
m4_define([<MF_MODULE_IDX>],[<m4_dnl
[<BUILTIN_IDX_>]MF_MODULE_NAME()>])

/* __mf_argtype(arg)
 * -----------------
 * Expand to Mailfromd value type code corresponding to ARG:
 *
 *   __mf_argtype(STRING) => dtype_string
 *   __mf_argtype(NUMBER) => dtype_number
 *   __mf_argtype(POINTER) => dtype_pointer
 *   __mf_argtype(anything) => dtype_unspecified
 */
m4_define([<__mf_argtype>],m4_dnl
[<m4_ifelse($1,STRING,dtype_string,$1,NUMBER,dtype_number,m4_dnl
$1,POINTER,dtype_pointer,m4_dnl
dtype_unspecified)>])

/* mf_argtype(TYPE NAME)
 * ---------------------
 * Expand to the Mailfromd value type code corresponding to
 * TYPE. See __mf_argtype above.
 */
m4_define([<mf_argtype>],m4_dnl
[<__mf_argtype(m4_patsubst([<$1>],[<[ \t].*>],))>])

/* mf_typelist(ARGLIST)
 * --------------------
 * Convert ARGLIST into a list of corresponding Mailfromd value type codes.
 * E.g.:
 *   mf_typelist(STRING a, NUMBER b) => dtype_string, dtype_number
 */
m4_define([<mf_typelist>],m4_dnl
[<m4_ifelse($#, 1, [<mf_argtype($1)>], m4_dnl,
$1, [<OPTIONAL>], [<mf_typelist(m4_shift($@))>],
[<mf_argtype($1), >]m4_dnl
[<mf_typelist(m4_shift($@))>])>])

/* __mf_has_optarg(ARGS...)
 * ------------------------
 * Expand to 1 if ARGS contain OPTIONAL keyword, and to 0 otherwise
 */
m4_define([<__mf_has_optarg>],m4_dnl
[<m4_ifelse($1, , 0,$1,[<OPTIONAL>], 1,[<m4_dnl
__mf_has_optarg(m4_shift($@))>])>])

/* __mf_c_type(TYPE)
 * -----------------
 * Expand to a C type corresponding to the Mailfromd TYPE
 */
m4_define([<__mf_c_type>],m4_dnl
[<m4_ifelse($1,STRING,char * __mf_dataseg,$1,NUMBER,long ,$1,POINTER,void * __mf_dataseg, ERROR )>])

/* __mf_c_getarg(TYPE)
 * -------------------
 * Expand to the get_(.*)_arg function call for the given MFL TYPE
 */
m4_define([<__mf_c_getarg>],m4_dnl
[<m4_ifelse($1,STRING,get_string_arg,$1,NUMBER,get_numeric_arg,m4_dnl
$1,POINTER,get_pointer_arg, ERROR )>])

/* mf_c_argdcl(TYPE NAME)
 * ----------------------
 * Translate MFL declaration "TYPE NAME" to the corresponding C one:
 * 
 *   mf_c_argdcl(STRING str) => char *str
 */
m4_define([<mf_c_argdcl>],m4_dnl
[<m4_regexp([<$1>],[<\(\w+\)\W+\(\w+\)>],[<__mf_c_type(\1)>] \2)>])

/* mf_c_arginit(TYPE NAME, NUM)
 * ----------------------------
 * Translate MFL declaration "TYPE NAME" to the corresponding C initialization:
 * 
 *   mf_c_arginit(STRING str, NUM) => get_string_arg(env, NUM, &str)
 */
m4_define([<mf_c_arginit>],m4_dnl
[<m4_regexp([<$1>],[<\(\w+\)\W+\(\w+\)>],[<__mf_c_getarg(\1)(env, $2, &\2)>])>])

/* __mf_c_argdcl_list(NARG, LIST)
 * ------------------------------
 * Translate MFL declaration list to a set of corresponding C variable
 * declarations.
 * For more details, see mf_c_arglist below.
 */
m4_define([<__mf_c_argdcl_list>],m4_dnl
[<m4_ifelse($2, , ,$2,[<OPTIONAL>],[<m4_dnl
__mf_c_argdcl_list($1, m4_shift(m4_shift($@)))>],
[<mf_c_argdcl($2);
        __mf_c_argdcl_list(m4_incr($1), m4_shift(m4_shift($@)))>])>])

/* __mf_c_arginit_list(NARG, LIST)
 * -------------------------------
 * Translate MFL declaration list to a set of corresponding C variable
 * initializations.
 * For more details, see mf_c_arglist below.
 */
m4_define([<__mf_c_arginit_list>],m4_dnl
[<m4_ifelse($2, , ,$2,[<OPTIONAL>],[<m4_dnl
__mf_c_arginit_list($1, m4_shift(m4_shift($@)))>],
[<mf_c_arginit($2, m4_eval($1));
        __mf_c_arginit_list(m4_incr($1), m4_shift(m4_shift($@)))>])>])

/* __mf_c_arglist(NARG, LIST)
 * --------------------------
 * Translate MFL declaration list to a set of corresponding C variable
 * declarations and initializations.
 * Arguments:
 *  NARG -- ordinal number of the first variable in LIST
 *  LIST -- comma-separated list of Mailfromd declarations
 */
m4_define([<__mf_c_arglist>],[<__mf_c_argdcl_list($@)
__mf_c_arginit_list($@)>])

/* mf_c_arglist(LIST)
 * ------------------
 * Translate MFL declaration list to a set of corresponding C variable
 * declarations with initializations.
 * Insert an instruction to adjust the stack parameters after obtaining the
 * variables.
 * E.g.:
 *   mf_c_arglist(STRING a, NUMBER n) =>
 *         char *a;
 *         long n; 
 *         get_string_arg(env, 0, &a);
 *         get_numeric_arg(env, 1, &n);
 *         adjust_stack(env, 2);
 *
 * Or, if the builtin takes optional parameters:
 *
 *  mf_c_arglist(STRING a, NUMBER n) =>
 *         long __bi_argcnt;
 *         char *a;
 *         long n;
 *         get_string_arg(env, 1, &a);
 *         get_numeric_arg(env, 2, &n);
 *         get_numeric_arg(env, 0, &__bi_argcnt);
 *         adjust_stack(env, __bi_argcnt + 1);
 */
m4_define([<mf_c_arglist>],m4_dnl
[<
m4_pushdef([<__ARG1__>], m4_ifelse(__MF_VARARGS__,1,1,[<__mf_has_optarg($@)>]))
m4_ifelse(__ARG1__,0,,[<long __bi_argcnt;>])
__mf_c_arglist(__ARG1__, $@)
m4_ifelse(__ARG1__,0,,[<get_numeric_arg(env, 0, &__bi_argcnt);>])
        adjust_stack(env, m4_ifelse(__ARG1__,0,mf_argcount($@),__bi_argcnt + 1));
m4_popdef([<__ARG1__>])m4_dnl
>])

/* __mf_printf_type(TYPE)
 * ----------------------
 * Translate the MFL data type TYPE to the printf conversion specification
 * suitable for outputting it.
 */
m4_define([<__mf_printf_type>],m4_dnl
[<m4_ifelse($1,STRING,s,$1,NUMBER,lu,$1,POINTER,p,%?)>])

/* mf_printf_macro(TYPE NAME)
 * --------------------------
 * Translate TYPE to the printf conversion
 */
m4_define([<mf_printf_macro>],m4_dnl
[<%[<>]m4_regexp([<$1>],[<\(\w+\)\W+.*>],[<__mf_printf_type(\1)>])>])

/* mf_printf_list(LIST)
 * --------------------
 * Convert the list of MFL variable declarations to a space-delimited
 * list of printf conversion specifications:
 *   mf_printf_list(STRING a, NUMBER b) =>  %s %lu
 * Notice, that the expansion begins with the space character.
 */ 
m4_define([<mf_printf_list>],m4_dnl
[<m4_ifelse($1,,,$1,[<OPTIONAL>],m4_dnl
[<mf_printf_list(m4_shift($@))>],m4_dnl
[< mf_printf_macro($1)[<>]mf_printf_list(m4_shift($@))>])>])

/* __mf_argname(TYPE NAME)
 * -----------------------
 * Expand to NAME
 */ 
m4_define([<__mf_argname>],m4_dnl
[<m4_regexp($1,[<\w+\W+\(\w+\)>],\1)>])

/* mf_argnames(LIST)
 * -----------------
 * Extract names from the Mailfromd declaration list:
 *  mf_argnames(STRING a, NUMBER b) => a, b
 */ 
m4_define([<mf_argnames>],m4_dnl
[<m4_ifelse($#,1, [<__mf_argname($1)>],m4_dnl
$1,[<OPTIONAL>],[<mf_argnames(m4_shift($@))>],m4_dnl
[<__mf_argname($1), mf_argnames(m4_shift($@))>])>])

/* __mf_defined_argname(OPT, TYPE NAME)
 * ------------------------------------
 * Same as __mf_argname, but wrap the argument into MF_OPTVAL if OPT is 1.
 */ 
m4_define([<__mf_defined_argname>],m4_dnl
[<m4_ifelse($1,1,[<m4_dnl
MF_OPTVAL(__mf_argname($2),m4_ifelse(mf_argtype($2),[<dtype_string>],"",0))>],m4_dnl
[<__mf_argname($2)>])>])

/* __mf_defined_argnames(OPT, LIST)
 * --------------------------------
 * Same as mf_argnames, if OPT is 0.
 * Otherwise, if OPT is 1, expands to list of MF_OPTVAL constructs with
 * consecutive parameter names as arguments.
 * After encountering the OPTIONAL keyword, changes OPT to 1.
 */ 
m4_define([<__mf_defined_argnames>],m4_dnl
[<m4_ifelse($#,2, [<__mf_defined_argname($1,$2)>],m4_dnl
$2,[<OPTIONAL>],[<__mf_defined_argnames(1,m4_shift(m4_shift($@)))>],m4_dnl
[<__mf_defined_argname($1,$2), m4_dnl
__mf_defined_argnames($1,m4_shift(m4_shift($@)))>])>])

/* mf_defined_argnames(LIST)
 * -------------------------
 * Same as mf_argnames, but arguments after the OPTIONAL keywords are
 * protected by MF_OPTVAL
 */ 
m4_define([<mf_defined_argnames>],m4_dnl
[<__mf_defined_argnames(0,$@)>])

/* __mf_argpos(POS,NEEDLE,STACK...)
 * --------------------------------
 * Expands to position at which NEEDLE occurs in STACK
 * Arguments:
 *  POS    -  Current position
 *  NEEDLE -  String to find
 *  STACK  -  Argument list
 * Example:
 *  __mf_argpos(0, x, a, b, x) => 2
 */ 
m4_define([<__mf_argpos>],m4_dnl
[<m4_ifelse($2,$3,$1,$3,,[<m4_errprint(m4___file__:m4___line__: No such variable >]$2[<
)>],[<__mf_argpos(m4_incr($1), $2, m4_shift(m4_shift(m4_shift($@))))>])>])

/* mf_argpos(ARG, TYPE1 ARG1, TYPE2 ARG2 ...)
 * ------------------------------------------
 * Expand to the (zero-based) position of ARG in the argument list:
 *
 *  mf_argpos(x, STRING a, NUMBER b, OPTIONAL, STRING x) => 3
 */
m4_define([<mf_argpos>],m4_dnl
[<__mf_argpos(0, $1, mf_argnames(m4_shift($@)))>])

/* __mf_defined(NAME, ARGS...)
 * ---------------------------
 * Scan ARGS... for the definition of the built-in function parameter NAME,
 * and expand to a C conditional expression that yields true if it is defined.
 * ARGS are parameter declarations in the form:
 *      TYPE NAME
 */ 
m4_define([<__mf_defined>],
[<m4_ifelse(__mf_has_optarg($@),0,1,m4_dnl
[<__bi_argcnt > mf_argpos($1,__MF_ARGLIST__)>])>])

/* MF_DEFINED(NAME)
 * ----------------
 * Expand to a C conditional expression that yields true if the parameter
 * NAME is defined.
 *
 *  __MF_ARGLIST__ => STRING a, NUMBER b, OPTIONAL, STRING x
 *  MF_DEFINED(x) => (__bi_argcnt > 2)
 *
 *  __MF_ARGLIST__ => STRING a, NUMBER b, STRING x
 *  MF_DEFINED(x) => (1)
 */
m4_define([<MF_DEFINED>],
[<m4_ifdef([<__MF_ARGLIST__>],([<__mf_defined($1, __MF_ARGLIST__)>]),m4_dnl
[<m4_errprint(m4___file__:m4___line__: [<MF_DEFINED used out of functional context
>])>])>])

/* MF_OPTVAL(NAME[, DEFVAL])
 * -------------------------
 * If the parameter NAME is defined, expand to its value, otherwise expand
 * to DEFVAL or 0
 */ 
m4_define([<MF_OPTVAL>],
[<m4_ifdef([<__MF_ARGLIST__>],m4_dnl
[<(MF_DEFINED($1) ? $1 : m4_ifelse([<$2>],,0,$2))>],m4_dnl
[<m4_errprint(m4___file__:m4___line__: [<MF_OPTVAL used out of functional context
>])>])>])

/* __mf_check_end()
 * ----------------
 * Signal error if the previous MF_DEFUN statement was not properly closed
 * with END
 */ 
m4_define([<__mf_check_end>],m4_dnl
[<m4_ifdef([<__MF_FUNCTION__>],m4_dnl
[<m4_errprint(m4___file__:m4___line__: Function '>]__MF_FUNCTION__[<' was not closed
)
m4_popdef([<__MF_FUNCTION__>])
m4_define([<__mf_error_code>],1)>])>])

/* MF_STATE(state)
 * ---------------
 * Declare next MF_DEFUN as valid only in the given state.
 * The state argument is any valid milter state, as declared in
 * enum smtp_state (see mailfromd.h around line 74--87), but without the
 * `smtp_state_' prefix.
 *
 * Multiple occurrences of MF_STATE accumulate.
 */
m4_define([<MF_STATE>],
[<m4_ifdef([<__MF_STATE__>],m4_dnl
[<m4_define([<__MF_STATE__>],__MF_STATE__[< | STATMASK(smtp_state_$1)>])>],m4_dnl
[<m4_define([<__MF_STATE__>],[<STATMASK(smtp_state_$1)>])>])>])

/* MF_CAPTURE([str])
 * -----------------
 * Declare next MF_DEFUN as requiring message capturing.
 * The form with the STR argument can be used only in MF_STATE(eom)
 * functions (FIXME: this should be enforced at compile time).  In that
 * case a reference to the capture stream is stored in STR.
 */
m4_define([<MF_CAPTURE>],m4_dnl
[<m4_define([<__MF_NEEDS_CAPTURE__>],1)m4_dnl
m4_ifelse([<$1>],,,[<m4_define([<__MF_CAPTURE__>],[<$1>])>])>])

/* env_get_stream()
 * ----------------
 * Prohibit the use of the library function of the same name.
 */ 
m4_define([<env_get_stream>],m4_dnl
[<m4_ifdef([<__MF_FUNCTION__>],m4_dnl
[<m4_ifdef([<__MF_ENV_GET_STREAM_PROHIBIT>],m4_dnl
[<m4_errprint(m4___file__:m4___line__: [<env_get_stream is illegal here>]
)
m4_define([<__mf_error_code>],1)>],[<[<env_get_stream>]($@)>])>],m4_dnl
[<env_get_stream>])>])

/* mf_optcount(ARGS...)
 * --------------------
 * Return the number of optional arguments in ARGS
 */ 
m4_define([<mf_optcount>],[<m4_dnl
m4_ifelse($#,1,0,$1,[<OPTIONAL>],m4_eval($# - 1),[<mf_optcount(m4_shift($@))>])>])

/* __mf_argcount(COUNT, ARGS...)
 * -----------------------------
 * Auxiliary function for mf_argcount
 *   COUNT is number of arguments counted so far
 *   ARGS are the rest of the arguments
 */
m4_define([<__mf_argcount>],[<m4_dnl
m4_ifelse($#,2,$1,$2,[<OPTIONAL>],[<__mf_argcount($1, m4_shift(m4_shift($@)))>],m4_dnl
[<__mf_argcount(m4_incr($1), m4_shift(m4_shift($@)))>])>])

/* mf_argcount(ARGS...)
 * --------------------
 * Return the number of arguments in ARGS, not counting eventual OPTIONAL
 * modifier.
 * FIXME: same as m4_eval($# - __mf_has_optarg($@))
 */ 
m4_define([<mf_argcount>],[<m4_dnl
m4_ifelse($1,,0,__mf_argcount(1,$@))>])

/* mf_prog_trace(FNAME[, ARGS...])
 * -------------------------------
 * Expand to the prog_trace call for function FNAME with arguments ARGS.
 */ 
m4_define([<mf_prog_trace>],[<m4_dnl
prog_trace(env, "$1[<>]mf_printf_list(m4_shift($@))"m4_dnl
m4_ifelse($2,,,[<,mf_defined_argnames(m4_shift($@))>]));>])

/* __mf_defun(VARARG, NAME, RETTYPE, ARGS...)
 * ------------------------------------------
 * Begin a built-in function declaration.
 * Arguments:
 *   VARARG
 *     Initial value for the FLAGS argument to the va_builtin_install_ex
 *     function.  Actually, MFD_BUILTIN_VARIADIC if NAME is a variadic
 *     function, and 0 otherwise.
 *   NAME
 *     Name of the function.
 *   RETTYPE
 *     Return type (STRING or NUMBER).
 *   ARGS
 *     List of arguments with types.
 */ 
m4_define([<__mf_defun>],m4_dnl
[<__mf_check_end[<>]m4_dnl
void
bi_$2(eval_environ_t env)
m4_pushdef([<__MF_FUNCTION__>], $2)m4_dnl
m4_pushdef([<__MF_RETTYPE__>], $3)m4_dnl
m4_pushdef([<__MF_ARGLIST__>], [<m4_shift(m4_shift(m4_shift($*)))>])
m4_ifelse($3,STRING,[<MF_DSEXP>])
m4_divert(1)m4_dnl
va_builtin_install_ex("$2", bi_$2,m4_dnl
 m4_ifdef([<__MF_STATE__>],__MF_STATE__,0),m4_dnl
 __mf_argtype($3),m4_dnl
 mf_argcount(m4_shift(m4_shift(m4_shift($@)))),m4_dnl
 mf_optcount(m4_shift(m4_shift(m4_shift($@)))),m4_dnl
 m4_ifdef([<__MF_NEEDS_CAPTURE__>],[<MFD_BUILTIN_CAPTURE>],0)|$1,m4_dnl
 mf_typelist(m4_shift(m4_shift(m4_shift($@)))));
m4_divert(2)m4_dnl
{m4_ifdef([<__MF_CAPTURE__>],[<
	  mu_stream_t __MF_CAPTURE__;
>])
	mf_c_arglist(m4_shift(m4_shift(m4_shift($@))))
m4_ifdef([<__MF_CAPTURE__>],[<{
	  int rc = env_get_stream(env, &__MF_CAPTURE__);
	  MF_ASSERT(rc == 0, mfe_failure,
	            "cannot obtain capture stream reference: %s",
		    mu_strerror(rc));
m4_define([<__MF_ENV_GET_STREAM_PROHIBIT>])m4_dnl
	}>])
	if (builtin_module_trace(MF_MODULE_IDX))
		mf_prog_trace($2,m4_shift(m4_shift(m4_shift($@))));
>])

/* MF_DEFUN(NAME, RETTYPE, ARGS...)
 * --------------------------------
 * Start a declaration of the built-in function NAME.  The declaration
 * must be terminated with END.
 * Arguments:
 *  NAME      - function name
 *  RETTYPE   - return type
 *  ARGS      - list of argument declarations, each one of the form
 *              TYPE ARGNAME; special argument OPTIONAL begins the list of
 *              optional parameters.
 */
m4_define([<MF_DEFUN>],[<m4_pushdef([<__MF_VARARGS__>], 0)m4_dnl
__mf_defun(0, $@)>])

m4_define([<__mf_defun_varargs>],[<m4_dnl
m4_pushdef([<__MF_VARARGS__>], 1)m4_dnl
m4_ifelse(__mf_has_optarg(m4_shift(m4_shift(m4_shift(m4_shift($@))))),0,m4_dnl
[<__mf_defun($1, m4_shift($@))>],m4_dnl
[<m4_errprint(m4___file__:m4___line__: A variadic function cannot take optional arguments
)
m4_define([<__mf_error_code>],1)>])>])

/* MF_DEFUN_VARARGS(NAME, RETTYPE. [TYPE PARAM...])
 * ------------------------------------------------
 * Start the declaration of the built-in variadic function NAME.
 * The declarationl must be terminated with END.
 * Arguments:
 *  NAME       - function name
 *  RETTYPE    - return type
 *  TYPE PARAM - declarations of mandatory parameters. 
 * Actual parameters can be retrieved using MF_VA_ARG(), which see,
 * See also MF_VA_START and MF_VA_END
 */
m4_define([<MF_DEFUN_VARARGS>],[<__mf_defun_varargs(MFD_BUILTIN_VARIADIC,$@)>])

/* MF_DEFUN_VARARGS_NO_PROM(NAME, RETTYPE, [TYPE PARAM...])
 * --------------------------------------------------------
 * Same as MF_DEFUN_VARARGS, but actual parameters are not promoted to
 * STRING.
 */
m4_define([<MF_DEFUN_VARARGS_NO_PROM>],[<m4_dnl
__mf_defun_varargs(MFD_BUILTIN_VARIADIC|MFD_BUILTIN_NO_PROMOTE,$@)>])

/* Prevent the use of the `return' statement in defuns.
 */ 
m4_define([<return>],[<m4_dnl
m4_ifdef([<__MF_FUNCTION__>],[<m4_dnl
m4_errprint(m4___file__:m4___line__: [<return used in MF_DEFUN>]
)
m4_define([<__mf_error_code>],1)>],[<[<return>]>])>])

/* MF_RETURN(VALUE[,TYPE])
 * -----------------------
 * Expand to C code for returning VALUE from the current function. If TYPE
 * is supplied, cast value to this type.
 */ 
m4_define([<MF_RETURN>],[<
m4_ifdef([<__MF_VA_START_USED__>],[<m4_dnl
m4_errprint(m4___file__:m4___line__: [<MF_RETURN>] used before [<MF_VA_END>]
)
m4_define([<__mf_error_code>],1)>],[<m4_dnl
do {
m4_pushdef([<__type>],[<m4_ifelse($2,,__MF_RETTYPE__,$2)>])m4_dnl
  m4_ifelse(__type,[<NUMBER>],[<push(env, (STKVAL)(mft_number)($1))>],m4_dnl
__type,[<STRING>],[<pushs(env, $1)>],m4_dnl
__type,[<string>],[<pushs(env, $1)>],m4_dnl
__type,[<STKVAL>],[<push(env, (STKVAL) ($1))>],m4_dnl
__type,,[<m4_errprint(m4___file__:m4___line__: return type undefined
)>],m4_dnl
[<push(env, (STKVAL) ([<mft_>]$2) ($1))>]);
m4_popdef([<__type>])m4_dnl
  goto endlab;
m4_define([<__MF_ENDLAB__>])m4_dnl  
} while (0)>])>])

/* MF_ALLOC_HEAP(OFF, LEN)
 * -----------------------
 * Allocate LEN bytes from the heap.  Return the offset of the allocated
 * space in OFF.
 */ 
m4_define([<MF_ALLOC_HEAP>],[<m4_dnl
__MF_DSEXP_REQ()m4_dnl
(char*) env_data_ref(env, ([<$1>] = heap_reserve(env, $2)))>])

/* MF_ALLOC_HEAP_TEMP(LEN)
 * -----------------------
 * Temporarly allocate LEN bytes from the heap.
 */ 
m4_define([<MF_ALLOC_HEAP_TEMP>],[<__MF_DSEXP_REQ()m4_dnl
mf_c_val(heap_tempspace(env, $1), ptr)>])

/* MF_COPY_STRING(off, string)
 * ---------------------------
 * Copy STRING to the heap.  Return the pointer to the copy.
 */ 
m4_define([<MF_COPY_STRING>],[<m4_dnl
__MF_DSEXP_REQ()m4_dnl
strcpy((char*)env_data_ref(env, $1 = heap_reserve(env, strlen($2) + 1)), $2)>])

/* MF_OBSTACK_BEGIN()
 * ------------------
 * Begin temporary space manipulations.
 * NOTE: No other heap manipulation function can be used between
 * MF_OBSTACK_BEGIN and MF_OBSTACK_CANCEL/MF_RETURN_OBSTACK/MF_OBSTACK_FINISH
 */ 
m4_define([<MF_OBSTACK_BEGIN>],[<heap_obstack_begin(env)>])

m4_define([<MF_OBSTACK_GROW>],[<m4_dnl
m4_ifelse($2,,[<
do {
  char *__s = $1;
  m4_ifelse($#,3,[<$3 = >])heap_obstack_grow(env, __s, strlen(__s));
} while (0)>],[<m4_ifelse($#,3,[<$3 = >])heap_obstack_grow(env, $1, $2)>])>])

m4_define([<MF_OBSTACK_1GROW>],[<m4_dnl
do { char __c = $1; heap_obstack_grow(env, &__c, 1); } while(0)>])

/* MF_OBSTACK_CANCEL()
 * -------------------
 * Cancel temporary heap allocation initiated by MF_OBSTACK_BEGIN
 */ 
m4_define([<MF_OBSTACK_CANCEL>],[<heap_obstack_cancel(env)>])

/* MF_OBSTACK_BASE()
 * -----------------
 * Return a C pointer to the beginning of the currently
 * allocated obstack space.
 */ 
m4_define([<MF_OBSTACK_BASE>],[<heap_obstack_base(env)>])

/* MF_RETURN_OBSTACK()
 * -------------------
 * Relocate and return temporary space 
 */ 
m4_define([<MF_RETURN_OBSTACK>],[<MF_RETURN(heap_obstack_finish(env),[<STKVAL>])>])

/* MF_OBSTACK_FINISH()
 * -------------------
 * Relocate temporary space and return its starting offset.
 */	
m4_define([<MF_OBSTACK_FINISH>],[<heap_obstack_finish(env)>])

/* MF_VA_START()
 * -------------
 * Begin a code section for handling variable number of arguments.
 */
m4_define([<MF_VA_START>],[<
m4_ifelse(__MF_VARARGS__,1,[<m4_dnl
m4_define([<__MF_VA_START_USED__>],m4___file__:m4___line__)m4_dnl
unroll_stack(env, __bi_argcnt + 1)>],
[<m4_errprint(m4___file__:m4___line__: [<MF_VA_START>] used but `__MF_FUNCTION__' does not take variable number of arguments
)m4_dnl
m4_define([<__mf_error_code>],1)>])>])

/* MF_VA_END()
 * -----------
 * End the section started with MF_VA_START
 */
m4_define([<MF_VA_END>],[<m4_dnl
m4_ifdef([<__MF_VA_START_USED__>],[<m4_dnl
m4_undefine([<__MF_VA_START_USED__>])m4_dnl
adjust_stack(env, __bi_argcnt + 1)>],m4_dnl
[<m4_errprint(m4___file__:m4___line__: [<MF_VA_END>] without previous [<MF_VA_START>]
)
m4_define([<__mf_error_code>],1)>])>])

/* __mf_va_count()
 * ---------------
 * Return number of variable arguments passed to the current vararg function
 */
m4_define([<__mf_va_count>],[<m4_dnl
(__bi_argcnt - mf_argcount(__MF_ARGLIST__))>])

/* MF_VA_COUNT()
 * -------------
 * Return actual number of variable arguments passed to the
 * function.  Bail out if the function is not a vararg one.
 */
m4_define([<MF_VA_COUNT>],[<m4_dnl
m4_ifelse(__MF_VARARGS__,1,[<__mf_va_count>],
[<m4_errprint(m4___file__:m4___line__: [<MF_VA_COUNT>] used but `__MF_FUNCTION__' does not take variable number of arguments
)m4_dnl
m4_define([<__mf_error_code>],1)>])>])

/* MF_VA_ARG(N, TYPE, VAR)
 * -----------------------
 * Produce a code for assigning to VAR the Nth
 * argument of the given TYPE in a vararg section.
 */
m4_define([<MF_VA_ARG>],[<m4_dnl
m4_ifdef([<__MF_VA_START_USED__>],m4_dnl
[<m4_pushdef([<__ARGN__>],[<($1+mf_argcount(__MF_ARGLIST__))>])
 ((__bi_argcnt > __ARGN__) ?m4_dnl 
   __mf_c_getarg($2)(env, __ARGN__ + 1, &$3) :m4_dnl
   (MF_THROW(mfe_range, "Argument %u is not supplied", (unsigned) __ARGN__),m4_dnl
(__mf_c_type($2)) 0))m4_dnl
m4_popdef([<__ARGN__>])>],
[<m4_errprint(m4___file__:m4___line__: [<MF_VA_ARG>] without previous [<MF_VA_START>]
)
m4_define([<__mf_error_code>],1)>])>])

/* MF_VAR(NAME,TYPE[,FLAG])
 * ------------------------
 * Declare a global variable NAME of type TYPE.  FLAGS are additional flags
 * (SYM_VOLATILE is always used).
 */ 
m4_define([<MF_VAR>],[<m4_dnl
static size_t $1_loc
m4_divert(1)m4_dnl
	builtin_variable_install("$1", __mf_argtype($2), m4_dnl
[<SYM_VOLATILE>]m4_ifelse($3,,,|$3), &$1_loc);
m4_divert(2)m4_dnl
>])

/* MF_VAR_REF(NAME, TYPE[, VALUE])
 * -------------------------------
 * Reference the global variable NAME.
 * In two-argument form, expand to its value.  In three-arguments form, assign
 * the VALUE to it.
 */ 
m4_define([<MF_VAR_REF>],[<m4_dnl
mf_c_val(*env_data_ref(env, $1_loc),$2) m4_ifelse($3,,,= ($3))>])

m4_define([<MF_VAR_STRING>],[<m4_dnl
(char*)env_data_ref(env, MF_VAR_REF($1, size))>])

/* MF_VAR_SET_STRING(NAME, VALUE)
 * ------------------------------
 * Set variable NAME to the string V