aboutsummaryrefslogtreecommitdiff
path: root/mfd/mailfromd.h
blob: 74cf10792bdb5da3333b73a723a0e8cebe83227b (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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
/* This file is part of Mailfromd.
   Copyright (C) 2005, 2006, 2007, 2008, 2009 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/>. */

#include <sys/types.h>
#include <regex.h>
#include <c-ctype.h>
#include <mailutils/mailutils.h>
#include <mfapi.h>
#include <dns.h>
#include <xalloc.h>
#include <argp.h>
#include <sysexits.h>
#define obstack_chunk_alloc malloc
#define obstack_chunk_free free
#include <obstack.h>

#include "libmf.h"
#include "bitmask.h"

/* A simplified version of the bitmask above, used for SMTP state mask */
#define STATMASK(n) ((unsigned)1<<(n))

#define SP(s) ((s) ? (s) : "(null)")

#ifndef INADDR_NONE
# define INADDR_NONE ((unsigned long) -1)
#endif

/* Status values used throughout the program */
typedef enum mf_status_code {
	mf_success,
	mf_not_found,
	mf_failure,
	mf_temp_failure,

	mf_status_count
} mf_status;

#define mf_resolved(c) ((c) == mf_success || (c) == mf_not_found)

/* Exception codes. */
typedef enum mf_exception_code {
	mfe_success,
	mfe_not_found,
	mfe_failure,
	mfe_temp_failure,
	
	mfe_ston_conv,
        mfe_divzero,
        mfe_regcomp,
        mfe_invip,
        mfe_invcidr,
        mfe_invtime,
        mfe_dbfailure,
	mfe_range,
	mfe_url,
	mfe_noresolve,
	mfe_io,
	mfe_macroundef,
	mfe_eof,
	
	mf_exception_count
} mf_exception;

#define mf_status_to_exception(s) ((mf_exception)(s))

/* SMTP states */
enum smtp_state {
	smtp_state_none,
	smtp_state_begin,
	smtp_state_first=smtp_state_begin,
	smtp_state_connect,
	smtp_state_helo,
	smtp_state_envfrom,
	smtp_state_envrcpt,
	smtp_state_data,
	smtp_state_header,
	smtp_state_eoh,
	smtp_state_body,
	smtp_state_eom,

	smtp_state_end,
	
	smtp_state_count
};

#define MFAM_STDIO 0
#define MFAM_UNIX  1
#define MFAM_INET  2


/* The following defines are borrowed from intprops.h (gnulib) */

/* True if the arithmetic type T is signed.  */
#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))

/* Return zero if T can be determined to be an unsigned type.
   Otherwise, return 1.
   When compiling with GCC, INT_STRLEN_BOUND uses this macro to obtain a
   tighter bound.  Otherwise, it overestimates the true bound by one byte
   when applied to unsigned types of size 2, 4, 16, ... bytes.
   The symbol signed_type_or_expr__ is private to this header file.  */
#if __GNUC__ >= 2
# define signed_type_or_expr__(t) TYPE_SIGNED (__typeof__ (t))
#else
# define signed_type_or_expr__(t) 1
#endif

/* Bound on length of the string representing an integer type or expression T.
   Subtract 1 for the sign bit if T is signed; log10 (2.0) < 146/485;
   add 1 for integer division truncation; add 1 more for a minus sign
   if needed.  */
#define INT_STRLEN_BOUND(t) \
  ((sizeof (t) * CHAR_BIT - signed_type_or_expr__ (t)) * 146 / 485 \
   + signed_type_or_expr__ (t) + 1)

/* Bound on buffer size needed to represent an integer type or expression T,
   including the terminating null.  */
#define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)

/* Size of the buffer able to accomodate mailfromd numeric type */
#define NUMERIC_BUFSIZE_BOUND INT_BUFSIZE_BOUND(long)


#if defined HAVE_SYSCONF && defined _SC_OPEN_MAX
# define getmaxfd() sysconf(_SC_OPEN_MAX)
#elif defined (HAVE_GETDTABLESIZE)
# define getmaxfd() getdtablesize()
#else
# define getmaxfd() 256
#endif


mf_status dns_to_mf_status(dns_status stat);

mf_status resolve_ipstr(const char *ipstr, char **hbuf);
mf_status resolve_ipstr_domain(const char *ipstr, const char *domain,
			       char **phbuf);
mf_status resolve_hostname(const char *host, char **pipbuf);
	
mf_status getmx(const char *ipstr, mxbuf_t mxbuf);
mf_status getmxip(char *host, GACOPYZ_UINT32_T ipbuf[MAXMXCOUNT],
		  size_t *pcount);


/* Default file names */
#define DEFAULT_PIDFILE  "mailfromd.pid"
#define DEFAULT_DATABASE "mailfromd.db"
#define DEFAULT_RATE_DATABASE "rates.db"
#define DEFAULT_GREYLIST_DATABASE "greylist.db"
#define DEFAULT_DNS_DATABASE "dns.db"
#define DEFAULT_SCRIPT_FILE SYSCONFDIR "/mailfromd.mf"
#define DEFAULT_CONFIG_FILE SYSCONFDIR "/mailfromd.conf"
#define DEFAULT_FROM_ADDRESS "<>"
#ifdef DEFAULT_PREPROCESSOR
# define DEF_EXT_PP DEFAULT_PREPROCESSOR
#else
# define DEF_EXT_PP NULL
#endif


/* Debugging macros */
#include "debug.h"


/* Syslog flavor */
#ifdef USE_SYSLOG_ASYNC
# include <syslog_async.h>
#endif


/* FIXME */
void enable_prog_trace(const char *modlist);
void disable_prog_trace(const char *modlist);


/* Global variables declarations */
#define MAILFROMD_DAEMON        0
#define MAILFROMD_TEST          1
#define MAILFROMD_DELETE        2
#define MAILFROMD_LIST          3
#define MAILFROMD_EXPIRE        4
#define MAILFROMD_COMPACT       5    
#define MAILFROMD_SHOW_DEFAULTS 6
#define MAILFROMD_RUN           7

extern char *script_file;
extern char *ext_pp;
extern char *ext_pp_options;
extern int ext_pp_options_given;
extern int location_column_option;
extern int mode;
extern int mtasim_option;
extern unsigned optimization_level;
extern enum smtp_state test_state;
extern time_t negative_expire_interval;
extern int predict_next_option;
extern double predict_rate;
extern char *smtp_domain;
extern char *postmaster_email;
extern int regex_flags;
extern size_t variable_count;
extern int ignore_failed_reads_option;
extern int source_info_option;
extern int do_transcript;
extern unsigned long source_address;
extern char *pidfile;
extern time_t smtp_timeout_soft[], smtp_timeout_hard[];
extern time_t io_timeout;
extern int foreground;
extern int single_process_option;
extern int force_remove;
extern int script_dump_macros;
extern int script_dump_xref;
extern size_t lock_retry_count_option;
extern time_t lock_retry_timeout_option;
extern char *mailfromd_state_dir;
extern int stack_trace_option;
extern int log_to_stderr;
extern const char *program_version;
extern mode_t mailfromd_umask;

extern size_t stack_size;
extern size_t stack_max_size;
extern size_t stack_expand_incr;
enum stack_expand_policy {
	stack_expand_twice,
	stack_expand_add
};
extern enum stack_expand_policy stack_expand_policy;


/* Filter script parser */

typedef enum {
	dtype_unspecified,
	dtype_string,
	dtype_number,
	dtype_pointer
} data_type_t;

/* Parse tree node */
typedef struct node NODE;

/* Input location */
struct locus {
	const char *file;     /* File name */
	unsigned long line;   /* Line number */
	unsigned long point;  /* Offset in the input line */
	unsigned long leng;   /* Length of the input token */
};

#define LOCUS_EQ(a,b) \
	((((a)->file == (b)->file)					\
	  || ((a)->file && (b)->file && strcmp((a)->file, (b)->file) == 0))	\
	 && (a)->line == (b)->line)

/* Input location for the parser */
typedef struct mf_yyltype {
	struct locus beg;
	struct locus end;
} mf_yyltype_t;

#define YYLTYPE mf_yyltype_t

struct if_node {            /* if statement */
	NODE *cond;         /* Condition */
	NODE *if_true;      /* True branch */
	NODE *if_false;     /* False branch */
};

struct poll_data {                 /* Poll test */
	NODE *email;               
	NODE *client_addr;
	NODE *ehlo;
	NODE *mailfrom;
};

/* Opcodes for available binary operations */
enum bin_opcode {
	bin_and,
	bin_or,
	bin_eq,
	bin_ne,
	bin_lt,
	bin_le,
	bin_gt,
	bin_ge,
	bin_match,
	bin_fnmatch,
	bin_add,
	bin_sub,
	bin_mul,
	bin_div,
	bin_logand,
	bin_logor,
	bin_logxor
};

#define QUALIFIER_MX      0x1

struct bin_node {                  /* A binary operation */
	enum bin_opcode opcode;    /* Operation code */
	int qualifier;             /* Used to indicate MX MATCHES/FNMATCHES */
	NODE *arg[2];              /* arg[0] - left side argument,
				      arg[1] - right side argument */
};

/* Unary operations */
enum un_opcode {
	unary_not,
	unary_minus,
	unary_lognot
};

struct un_node {                   /* A unary operation node */ 
	enum un_opcode opcode;     /* Operation code */
	data_type_t result_type;   /* Result type */
	NODE *arg;                 /* Argument */ 
};

/* Return action node: accept/reject/tempfail/continue */
struct return_node {              
	sfsistat stat;             /* Return status */
	NODE *code;                /* Code */
	NODE *xcode;               /* Extended code */
	NODE *message;             /* Textual message */
};

enum msgmod_opcode {               /* Message modification operation */
	header_add,                /* Add a header */
	header_replace,            /* Replace a header value */
	header_delete,             /* Delete a header */
	header_insert,             /* Insert a header */
	rcpt_add,                  /* Add a recipient */
	rcpt_delete,               /* Delete a recipient */
	quarantine,                /* Quarantine a message */
	body_repl,                 /* Replace message body */ 
};

struct msgmod_closure {              
	enum msgmod_opcode opcode;   /* Operation code */
	char *name;                  /* Object name */
	char *value;                 /* Object value */
	unsigned idx;                /* Object index */
};

struct header_node {              
	enum msgmod_opcode opcode;   /* Operation code */
	struct literal *name;        /* Header name */
	NODE *value;                 /* Header value */
};

struct builtin_node {              /* Call to a built-in function */
	const struct builtin *builtin; /* Buit-in function */ 
	NODE *args;                    /* Actual arguments */
};

struct concat_node {              /* Concatenation of arg[0] and arg[1] */ 
	NODE *arg[2];
};

struct asgn_node {                /* Assignment */ 
	struct variable *var;     /* Variable */
	size_t nframes;           /* Number of frames to skip */
	NODE *node;               /* Expression to be evaluated and assigned
				     to it */ 
};

enum lexical_context {
	context_none,        
	context_handler,
	context_catch,       
	context_function
};

struct catch_node {
	struct exmask *exmask;    /* Exception mask */
	enum lexical_context context; /* Context in which it occurs */
	NODE *node;               /* Subtree */
};

struct try_node {
	NODE *node;               /* Subtree */
	NODE *catch;              /* Catch node */ 
};

struct function_call {
	struct function *func;    /* Function description */
	NODE *args;               /* List of actual arguments */
};

struct value {
	data_type_t type;
	union {
		long number;
		struct literal *literal;
	} v;
};

struct valist {
	struct valist *next;
	struct value value;
};

struct case_stmt {
	struct case_stmt *next;
	struct locus locus;
	struct valist *valist;
	NODE *node;
};

struct switch_stmt {
	NODE *node;                 /* Condition */
	struct case_stmt *cases;    /* Switch branches */
	/* Auxiliary data for code generation */
	size_t tabsize;             /* Size of the XLAT table */
	size_t off;                 /* Offset of the table in DS */
	struct switch_stmt *next;   /* Link to the next switch statement */
};

struct cast_node {
	data_type_t data_type;
	NODE *node;
};

struct positional_arg {
	data_type_t data_type;
	int number;
};

struct progdecl {
	enum smtp_state tag;
	size_t auto_count;
	NODE *tree;
};

struct funcdecl {
	struct function *func;
	size_t auto_count;
	NODE *tree;
};

struct throw_node {
	int code;
	NODE *expr;
};

struct var_ref {
	struct variable *variable;  /* Variable being referenced */
	size_t nframes;             /* Number of frames to skip */
};

struct regcomp_data {
	NODE *expr;
	int flags;
	size_t regind;
};

struct loop_node {
	struct literal *ident;
	NODE *stmt;
	NODE *for_stmt;
	NODE *beg_while;
	NODE *end_while;
	NODE *body;
};

struct argx_node {
	int nargs;
	NODE *node;
};
#include "node-type.h"

/* Parse tree node */
struct node {
	NODE *next;                   /* Link to the next node */
	enum node_type type;          /* Node type */
	struct locus locus;           /* Corresponding input location */
	union {
		struct literal *literal;
		long number;
		struct if_node cond;
		struct bin_node bin;
		struct un_node un;
		struct return_node ret;
		struct header_node hdr;
		struct builtin_node builtin;
		NODE *node;
		struct concat_node concat;
		struct var_ref var_ref;
		struct asgn_node asgn;
		void *val;
		struct catch_node catch;
		struct try_node try;
		struct throw_node throw;
		struct function_call call;
		struct switch_stmt switch_stmt;
		struct cast_node cast;
		struct positional_arg arg;
		struct progdecl progdecl;
		struct funcdecl funcdecl;
		struct regcomp_data regcomp_data;
		struct sym_regex *regex;
		struct loop_node loop;
		struct argx_node argx;
	} v;
};

struct stmtlist {
	NODE *head;
	NODE *tail;
};

struct exmask {
	struct exmask *next;
	struct bitmask bm;
	int all;                /* If 1, set all bits in the exmask */ 
	size_t off;             /* Offset of the table in DS */
};


/* Expressions, built-in functions and variables */

typedef struct eval_environ *eval_environ_t;  /* Evaluation environment */
typedef unsigned long prog_counter_t;         /* Program counter */  

/* Data types */

#define SYMENT_STRUCT(name)			\
	char *name;\
	unsigned refcnt

#define SYM_VOLATILE   0x01   /* Variable can change outside of the compiled
			 	 code */
#define SYM_REFERENCED 0x02   /* Variable is referenced */ 
#define SYM_PRECIOUS   0x04   /* Variable is precious, i.e. is not affected
				 by rset */
#define SYM_EXTERN     0x08   /* Symbol is external (for future use) */
#define SYM_STATIC     0x10   /* Symbol is static */
#define SYM_PUBLIC     0x20   /* Symbol is public */

struct mf_symbol {
	SYMENT_STRUCT(name);       /* Variable name */
	struct mf_symbol *alias;
	struct module *module;     /* Backlink to the module where it is
				      defined */
	struct locus locus;        /* Location of the definition */
	unsigned flags;            /* SYM_ flags */
};

#define MFD_BUILTIN_CAPTURE    0x01  /* Builtin needs message capturing */     
#define MFD_BUILTIN_VARIADIC   0x02  /* Builtin is variadic */
#define MFD_BUILTIN_NO_PROMOTE 0x04  /* For variadic functions:
					do not promote varargs to string */

struct builtin {
	SYMENT_STRUCT(name);
	void (*handler) (eval_environ_t); /* C handler */
	size_t parmcount;                 /* Number of parameters */
	data_type_t *parmtype;            /* Parameter types */
	size_t optcount;                  /* Number of optional parameters
					     (size_t) -1 for varargs */
	data_type_t rettype;              /* Return type */
	unsigned statemask;               /* States where the function can
					     be used */
	int flags;                        /* Flags */
};

struct function {      /* User-defined function */
	struct mf_symbol sym;
	NODE *node;              /* Function definition (syntax tree) */
	prog_counter_t entry;    /* Entry point to the function code */
	struct exmask *exmask;   /* Exception mask */
	unsigned statemask;      /* States where the function can
				    be used */
	size_t parmcount;        /* Number of parameters */
	size_t optcount;         /* Number of optional parameters */
	int varargs;             /* 1 if function takes variable number of
				    arguments */
	data_type_t *parmtype;   /* Parameter types */
	data_type_t rettype;     /* Return type */
};	

typedef enum {
	storage_extern,
	storage_auto,
	storage_param
} storage_class_t;

struct variable {
	struct mf_symbol sym;
	data_type_t type;          /* Variable type */
	storage_class_t storage_class; /* Storage class */
	size_t off;                /* Offset in the data segment */
	size_t ord;                /* Ordinal number in the parmlist
				      (for parameters only) */
	size_t *addrptr;           /* Address pointer (for built-in vars) */ 
	struct variable *shadowed; /* Points to the variable shadowed by
				      this one */
	mu_list_t xref;            /* List of struct locus */
	struct variable *next;     /* Next variable in this class */
};

struct literal {
	SYMENT_STRUCT(text);
	unsigned flags;            /* SYM_* flags */
	size_t off;                /* Offset in DS */
	struct sym_regex *regex;   /* Any correspondig regexes */
};

struct constant {
	struct mf_symbol sym;
	struct value value;        /* Constant value */
};

#define REG_EXTENDED_NAME "extended"
#define REG_ICASE_NAME "icase"
#define REG_NEWLINE_NAME "newline"

#define REGEX_STRING_BUFSIZE \
 (sizeof(REG_EXTENDED_NAME) + \
  sizeof(REG_ICASE_NAME) + \
  sizeof(REG_NEWLINE_NAME) + 1)

char *regex_flags_to_string(int flags, char *buf, size_t size);

int add_legacy_milter_port(const char *str, mu_debug_t dbg);
void log_setup(int want_stderr);
void builtin_setup(void);
void builtin_post_setup(void);
void pragma_setup(void);

void print_code(void);
void print_xref(void);
void print_used_macros(void);

/* symbols.c */
extern struct symtab *stab_module;
extern struct symtab *stab_builtin;
extern struct symtab *stab_pragma;
extern struct symtab *stab_literal;

void init_symbols(void);
void free_symbols(void);

struct mf_symbol *symbol_resolve_alias(struct mf_symbol *sp);
void va_builtin_install (char *name, void (*handler) (eval_environ_t),
			 data_type_t rettype, size_t argcount, ...);
void va_builtin_install_ex (char *name, void (*handler) (eval_environ_t),
			    unsigned statemsk, 
			    data_type_t rettype, size_t argcount,
			    size_t optcount, int flags, ...);

const struct builtin *builtin_lookup(const char *name);
struct variable *variable_install(const char *name);
struct variable *variable_lookup(const char *name);
struct variable *variable_replace(const char *name, struct variable *newvar);
void variable_remove(struct variable *var);
struct variable *builtin_variable_install(const char *name,
					  data_type_t type,
					  unsigned flags,
					  size_t *addrptr);
void defer_initialize_variable(const char *arg, const char *val);

int variable_or_constant_lookup(const char *name, void **dptr);

struct function *function_install(const char *name,
				  size_t parmcnt, size_t optcnt, int varargs,
				  data_type_t *parmtypes,
				  data_type_t rettype,
				  const struct locus *locus);
struct function *function_lookup(const char *name);
struct literal *literal_lookup(const char *text);
void define_constant(const char *name, struct value *value,
		     unsigned flags, struct locus *loc);
const struct constant *constant_lookup(const char *name);
const struct value *constant_lookup_value(const char *name);

struct sym_regex {
	struct literal *lit;        /* Corresponding literal */
	int regflags;               /* Compilation flags */
	unsigned flags;             /* VAR_* flags */
	size_t index;               /* Index in the regtab */
	struct sym_regex *next; /* Next sym_regex with the same name, but
				   different regflags */
};

struct sym_regex *install_regex(struct literal *lit, unsigned regflags);

void install_alias(const char *name, struct function *fun,
		   const struct locus *locus);


struct rt_regex {
	int compiled;
	regex_t re;
	size_t expr;
	int regflags;
};

void register_regex(struct sym_regex *rp);
void finalize_regex(void);


struct pragma {
	SYMENT_STRUCT(name);
	int minargs;
	int maxargs;
	void (*handler) (int, char **, const char *);
};

void install_pragma(const char *name, int minargs, int maxargs,
		    void (*handler) (int, char **, const char *));
const struct pragma *lookup_pragma(const char *name);


enum import_type {
	import_literal,
	import_regex,
	import_transform
};

struct import_rule {
	struct import_rule *next;  /* Next rule */
	enum import_type type;     /* Type of this rule */
	struct literal *literal;   /* Literal */
	int neg;                   /* Negate regex */
	regex_t re;                /* Regular expression */
	transform_t xform;         /* Transform expression */  
};

struct import_rule_list {
	struct import_rule *head, *tail;
};

struct import_rule *import_rule_create(struct literal *lit);


enum module_namespace {
	namespace_function,
	namespace_variable,
	namespace_constant
};
#define MODULE_NAMESPACE_COUNT 3

struct module {
	SYMENT_STRUCT(name);
	const char *file;
	const char *dclname;
	unsigned flags;
	struct symtab *symtab[MODULE_NAMESPACE_COUNT];
	struct import_rule *import_rules;
	struct module_list *submodule_head, *submodule_tail;
	mu_list_t /* of struct input_file_ident */ incl_sources;
};

struct module_list {
	struct module *module;
	struct module_list *next;
};

extern struct module *top_module;

#define MODULE_SYMTAB(m,ns) ((m)->symtab[ns])
#define TOP_MODULE_SYMTAB(ns) MODULE_SYMTAB(top_module,ns)
int set_top_module(const char *name, const char *file,
		   struct import_rule *import_rules,
		   const struct locus *locus);
void pop_top_module(void);
void collect_modules(struct module ***p