aboutsummaryrefslogtreecommitdiff
path: root/src/symtab.c
blob: 54a69e2b7c87eba73ed663dbd4b280a8d5793c16 (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
/* This file is part of mailfromd.
   Copyright (C) 2006, 2007 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, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include "mailfromd.h"

/* |hash_size| defines a sequence of symbol table sizes. These are prime
   numbers, the distance between each pair of them grows exponentially,
   starting from 64. Hardly someone will need more than 16411 symbols, and
   even if someone will, it is easy enough to add more numbers to the
   sequence. */

static unsigned int hash_size[] = {
    37,   101,  229,  487, 1009, 2039, 4091, 8191, 16411
};

/* |max_rehash| keeps the number of entries in |hash_size| table. */
static unsigned int max_rehash = sizeof (hash_size) / sizeof (hash_size[0]);

/* |hash_num| is the index to |hash_size| table. Current size of the 
   |symtab| table is thus |hash_size[hash_num]| */

static unsigned int hash_num;  

union symentry {
	char *name;               /* Each of the structures below
				     must begin with char*: */
	struct function fun;      /* if state == SYM_FUNC */
	struct builtin builtin;   /* if state == SYM_BUILTIN */
	struct variable variable; /* if state == SYM_VARIABLE */
	struct literal literal;   /* if state == SYM_LITERAL */
	struct constant constant; /* if state == SYM_CONSTANT */
	struct sym_regex regex;   /* if state == SYM_REGEX */
};

struct symtab {
	int state;     /* Entry state */
	union symentry *vp;
};

#define BUCKETSIZE 16

struct symbucket {
	struct symbucket *next;
	size_t index;
	union symentry entry[BUCKETSIZE];
};

static struct symtab *symtable; /* Table of symbols */
static struct symbucket *symbucket;


/* Bucket handling */
static void
alloc_bucket(void)
{
	struct symbucket *p = xmalloc(sizeof *p);
	p->index = 0;
	p->next = symbucket;
	symbucket = p;
}

static union symentry *
alloc_entry()
{
	if (!symbucket || symbucket->index == BUCKETSIZE)
		alloc_bucket();
	return &symbucket->entry[symbucket->index++];
}

static void
free_buckets()
{
	struct symbucket *p = symbucket;
	while (p) {
		struct symbucket *next = p->next;
		free(p);
		p = next;
	}
	symbucket = NULL;
}

void
free_symbols()
{
	free_buckets();
	free(symtable);
	symtable = NULL;
	hash_num = 0;
}


static struct symtab *lookup_or_install(int type, const char *name,
					int install);

static unsigned
hash(const char *name)
{
	unsigned i;
	
	for (i = 0; *name; name++) {
		i <<= 1;
		i ^= *(unsigned char*) name;
	}
	return i % hash_size[hash_num];
}

/* |rehash| is called when the symbol table becomes full. Its purpose is
   to select new table size, allocate storage for the resized table, install
   to it all data stored so far and reclaim the unused memory.

   |rehash| returns |0| on success and |1| otherwise. */
     
static int
rehash()
{
	struct symtab *old_symtab = symtable;
	struct symtab *sp;
	unsigned int i;
	
	if (++hash_num >= max_rehash) {
		return 1;
	}
	
	symtable = calloc(hash_size[hash_num], sizeof(symtable[0]));
	if (!symtable) {
		parse_error("%s", mu_strerror(errno));
		abort();
	}
	if (old_symtab) {
		for (i = 0; i < hash_size[hash_num - 1]; i++) {
			if (old_symtab[i].state != SYM_UNDEF) {
				sp = lookup_or_install(old_symtab[i].state,
						       old_symtab[i].vp->name,
						       1);
				sp->state = old_symtab[i].state;
				sp->vp = old_symtab[i].vp;
			}
		}
		free(old_symtab);
	}
	return 0;
}

/* Look up symbol |name| in the table. If not found and |install| is not 0, 
   insert the symbol into the table.
   
   Basically it uses algorithm L from TAOCP 6.4, except that if the insertion
   fails the table is rehashed and the attempt is retried. The process 
   continues until the symbol is successfully inserted or the table reaches
   maximum allowed size. */

static struct symtab *
lookup_or_install(int state, const char *name, int install)
{
	unsigned i, pos;

	if (!symtable) {
		if (install) {
			if (rehash())
				return NULL;
		} else
			return NULL;
	}
	
	pos = hash(name);
	
	for (i = pos; symtable[i].state != SYM_UNDEF;) {
		if ((state == SYM_UNDEF || symtable[i].state == state)
		    && strcmp(symtable[i].vp->name, name) == 0)
			return &symtable[i];
		if (++i >= hash_size[hash_num])
			i = 0;
		if (i == pos)
			break;
	}
	
	if (!install || state == SYM_UNDEF)
		return NULL;
	
	if (symtable[i].state == SYM_UNDEF) {
		symtable[i].vp = alloc_entry();
		return &symtable[i];
	}
	
	if (rehash())
		return NULL;
	
	return lookup_or_install(state, name, install);
}

static void
remove_symbol(struct symtab *sym)
{
	unsigned int i, j, r;
	if (!(symtable <= sym && sym < symtable + hash_size[hash_num]))
		abort();
	for (i = sym - symtable;;) {
		symtable[i].state = SYM_UNDEF;
		j = i;

		do {
			if (++i >= hash_size[hash_num])
				i = 0;
			if (symtable[i].state == SYM_UNDEF)
				return;
			r = hash(symtable[i].vp->name);
		} while ((j < r && r <= i)
			 || (i < j && j < r) || (r <= i && i < j));

		symtable[j] = symtable[i];
	}
}

void *
find_and_remove(int state, char *name)
{
	struct symtab *sym = lookup_or_install(state, name, 0);
	if (sym) {
		remove_symbol(sym);
		return sym->vp;
	}
	return NULL;
}

int
symbol_enumerate(int state, symbol_enumerator_t fun, void *data)
{
	unsigned i;
	
	for (i = 0; i < hash_size[hash_num]; i++) {
		if (state == SYM_UNDEF || symtable[i].state == state) {
			int rc = fun(symtable[i].vp, data);
			if (rc)
				return rc;
		}
	}
	return 0;
}

	  
void
va_builtin_install (char *name,
		    void (*handler) (eval_environ_t),
		    data_type_t rettype,
		    size_t argcount,
		    ...)
{
	struct symtab *sp;
	va_list ap;
	size_t i;
	data_type_t *argtype = xmalloc (argcount * sizeof *argtype);

	va_start(ap, argcount);
	for (i = 0; i < argcount; i++) 
		argtype[i] = va_arg(ap, data_type_t);
	va_end(ap);
	sp = lookup_or_install(SYM_BUILTIN, name, 1);
	if (!sp) {
		parse_error(_("Cannot install builtin function %s"), name);
		abort();
	}

	if (sp->state != SYM_UNDEF) {
		parse_error(_("INTERNAL ERROR at %s:%d: cannot install built-in function %s"),
			    __FILE__, __LINE__, name);
		abort();
	}

	/* new entry */
	sp->state = SYM_BUILTIN;
	sp->vp->builtin.name = mf_strdup(name);
	sp->vp->builtin.handler = handler;
	sp->vp->builtin.parmcount = argcount;
	sp->vp->builtin.parmtype = argtype;
	sp->vp->builtin.rettype = rettype;
	sp->vp->builtin.statemask = 0;
	sp->vp->builtin.capture = 0;
}

void
va_builtin_install_ex (char *name,
		       void (*handler) (eval_environ_t),
		       unsigned statemsk,
		       int capture,
		       data_type_t rettype,
		       size_t argcount,
		       size_t optcount,
		       int varargs,
		       ...)
{
	struct symtab *sp;
	va_list ap;
	size_t i;
	data_type_t *argtype = xmalloc (argcount * sizeof *argtype);

 	va_start(ap, varargs);
	for (i = 0; i < argcount; i++) 
		argtype[i] = va_arg(ap, data_type_t);
	va_end(ap);
	sp = lookup_or_install(SYM_BUILTIN, name, 1);
	if (!sp) {
		parse_error(_("Cannot install builtin function %s"), name);
		abort();
	}

	if (sp->state != SYM_UNDEF) {
		parse_error(_("INTERNAL ERROR at %s:%d: cannot install built-in function %s"),
			    __FILE__, __LINE__, name);
		abort();
	}

	/* new entry */
	sp->state = SYM_BUILTIN;
	sp->vp->builtin.name = mf_strdup(name);
	sp->vp->builtin.handler = handler;
	sp->vp->builtin.parmcount = argcount;
	sp->vp->builtin.optcount = optcount;
	sp->vp->builtin.varargs = varargs;
	sp->vp->builtin.parmtype = argtype;
	sp->vp->builtin.rettype = rettype;
	sp->vp->builtin.statemask = statemsk;
	sp->vp->builtin.capture = capture;
}


const struct builtin *
builtin_lookup(const char *name)
{
	struct symtab *sp = lookup_or_install(SYM_BUILTIN, name, 0);
	return (struct builtin *)(sp ? sp->vp : NULL);
}



static void
init_variable(struct variable *var, struct symtab *sp)
{
	var->type = dtype_unspecified;
	var->storage_class = storage_extern;
	var->off = 0;
	var->shadowed = NULL;
	var->flags = 0;
	var->xref = NULL;
	var->next = NULL;
	var->owner = sp;
}	

struct variable *
variable_install(const char *name)
{
	struct symtab *sp = lookup_or_install(SYM_VARIABLE, name, 1);
	if (sp->state == SYM_UNDEF) {
		sp->state = SYM_VARIABLE;
		init_variable(&sp->vp->variable, sp);
		sp->vp->variable.name = mf_strdup(name);
	} else if (sp->state != SYM_VARIABLE)
		abort();
	return &sp->vp->variable;
}

struct variable *
variable_replace(struct variable *var, struct variable *newvar)
{
	struct symtab *sp = var->owner;
	if (!newvar) {
		union symentry *new_entry = alloc_entry();
		newvar = &new_entry->variable;
		init_variable(newvar, sp);
		newvar->name = mf_strdup(var->name);
	}
	sp->vp = (union symentry *)newvar;
	return newvar;
}

struct variable *
variable_lookup(const char *name)
{
	struct symtab *sp = lookup_or_install(SYM_VARIABLE, name, 0);
	return sp ? &sp->vp->variable : NULL;
}


struct function *
function_install(const char *name, size_t parmcnt, size_t optcnt,
		 data_type_t *parmtypes, data_type_t rettype,
		 const struct locus *locus)
{
	struct symtab *sp = lookup_or_install(SYM_FUNC, name, 1);
	if (sp->state == SYM_FUNC) {
		parse_error_locus(locus,
				  _("Redefinition of function %s"), name);
		parse_error_locus(&sp->vp->fun.locus, 
		                  _("Previously defined here"));
	} else {
		sp->state = SYM_FUNC;
		sp->vp->fun.name = mf_strdup(name);
	}
	sp->vp->fun.locus = *locus;
	sp->vp->fun.node = NULL;
	sp->vp->fun.entry = 0;
	sp->vp->fun.parmcount = parmcnt;
	sp->vp->fun.optcount = optcnt;
	sp->vp->fun.parmtype = parmtypes;
	sp->vp->fun.rettype = rettype;
	sp->vp->fun.exmask = 0;
	sp->vp->fun.statemask = 0;
	return &sp->vp->fun;
}

struct function *
function_lookup(const char *name)
{
	struct symtab *sp = lookup_or_install(SYM_FUNC, name, 0);
	return sp ? &sp->vp->fun : NULL;
}


struct literal *
literal_lookup(const char *text)
{
	struct symtab *sp = lookup_or_install(SYM_LITERAL, text, 1);
	if (sp->state == SYM_UNDEF) {
		sp->state = SYM_LITERAL;
		sp->vp->literal.text = text;
		sp->vp->literal.flags = 0;
		sp->vp->literal.regex = NULL;
	}
	return &sp->vp->literal;
}


void
define_constant(const char *name, struct value *value, struct locus *locus)
{
	struct symtab *sp = lookup_or_install(SYM_CONSTANT, name, 1);
	if (sp->state == SYM_UNDEF) {
		sp->state = SYM_CONSTANT;
		sp->vp->constant.name = mf_strdup(name);
	} else {
		parse_warning_locus(locus,
				    "%s: redefinition of constant",
				    name);
		parse_warning_locus(&sp->vp->constant.locus,
				    "this is the location of the "
				    "previous definition");
	}
	sp->vp->constant.locus = *locus;
	sp->vp->constant.value = *value;
}

struct value *
constant_lookup(const char *name)
{
	struct symtab *sp = lookup_or_install(SYM_CONSTANT, name, 0);
	return sp ? &sp->vp->constant.value : NULL;
}

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

	for (regex = lit->regex; regex; regex = regex->next)
		if (regex->regflags == regflags)
			return regex;

	regex = &alloc_entry()->regex;
	regex->next = lit->regex;
	lit->regex = regex;
	regex->lit = lit;
	regex->regflags = regflags;
	regex->flags = 0;
	regex->index = 0;
	
	return regex;
}

Return to:

Send suggestions and report system problems to the System administrator.