/* 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, see . */ #ifdef HAVE_CONFIG_H # include #endif #include #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; }