Commit 95bcf47f authored by Bob Halley's avatar Bob Halley
Browse files

exists policy

parent 22c7e1fb
......@@ -15,6 +15,8 @@
* SOFTWARE.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
......@@ -41,7 +43,7 @@ mem_strdup(isc_mem_t *mctx, const char *s) {
static void
undefine_action(char *key, unsigned int type, isc_symvalue_t value) {
INSIST(type == 0);
INSIST(type == 1);
isc_mem_free(mctx, key);
isc_mem_free(mctx, value.as_pointer);
}
......@@ -54,16 +56,23 @@ main(int argc, char *argv[]) {
isc_symvalue_t value;
int trace = 0;
int c;
isc_symexists_t exists_policy = isc_symexists_reject;
INSIST(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
INSIST(isc_symtab_create(mctx, 691, undefine_action, &st) ==
ISC_R_SUCCESS);
while ((c = getopt(argc, argv, "t")) != -1) {
while ((c = getopt(argc, argv, "tar")) != -1) {
switch (c) {
case 't':
trace = 1;
break;
case 'a':
exists_policy = isc_symexists_add;
break;
case 'r':
exists_policy = isc_symexists_replace;
break;
}
}
......@@ -74,7 +83,7 @@ main(int argc, char *argv[]) {
if (cp[0] == '!') {
cp++;
result = isc_symtab_undefine(st, cp, 0);
result = isc_symtab_undefine(st, cp, 1);
if (trace || result != ISC_R_SUCCESS)
printf("undefine('%s'): %s\n", cp,
isc_result_totext(result));
......@@ -97,7 +106,8 @@ main(int argc, char *argv[]) {
*cp++ = '\0';
key = mem_strdup(mctx, key);
value.as_pointer = mem_strdup(mctx, cp);
result = isc_symtab_define(st, key, 0, value);
result = isc_symtab_define(st, key, 1, value,
exists_policy);
if (trace || result != ISC_R_SUCCESS)
printf("define('%s', '%s'): %s\n",
key, cp,
......
......@@ -168,7 +168,7 @@ isc_symtab_lookup(isc_symtab_t *symtab, const char *key, unsigned int type,
isc_result_t
isc_symtab_define(isc_symtab_t *symtab, char *key, unsigned int type,
isc_symvalue_t value)
isc_symvalue_t value, isc_symexists_t exists_policy)
{
unsigned int bucket;
elt_t *elt;
......@@ -178,17 +178,29 @@ isc_symtab_define(isc_symtab_t *symtab, char *key, unsigned int type,
FIND(symtab, key, type, bucket, elt);
if (elt != NULL)
return (ISC_R_EXISTS);
elt = (elt_t *)isc_mem_get(symtab->mctx, sizeof *elt);
if (elt == NULL)
return (ISC_R_NOMEMORY);
if (exists_policy != isc_symexists_add && elt != NULL) {
if (exists_policy == isc_symexists_reject)
return (ISC_R_EXISTS);
INSIST(exists_policy == isc_symexists_replace);
UNLINK(symtab->table[bucket], elt, link);
if (symtab->undefine_action != NULL)
(symtab->undefine_action)(elt->key, elt->type,
elt->value);
} else {
elt = (elt_t *)isc_mem_get(symtab->mctx, sizeof *elt);
if (elt == NULL)
return (ISC_R_NOMEMORY);
}
elt->key = key;
elt->type = type;
elt->value = value;
APPEND(symtab->table[bucket], elt, link);
/*
* We prepend so that a 'type 0' lookup will return the most
* recent definition, and a 'type 0' undefine will undefine the
* most recent definition.
*/
PREPEND(symtab->table[bucket], elt, link);
return (ISC_R_SUCCESS);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment