* Copyright (c) 1992 Eric P. Allman.
* Copyright (c) 1992 Regents of the University of California.
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)map.c 5.2 (Berkeley) %G%";
#if defined(HASH_MAP) || defined(BTREE_MAP)
** DBM_MAP_INIT -- DBM-style map initialization
** map -- the pointer to the actual map
** mapname -- the name of the map (for error messages)
** args -- a pointer to the config file line arguments
** TRUE -- if it could successfully open the map.
** Gives an error if it can't open the map.
dbm_map_init(map
, mapname
, args
)
map_parseargs(map
, &args
, mapname
);
if (map
->map_file
== NULL
)
dbm
= dbm_open(map
->map_file
, O_RDONLY
, 0644);
if (!bitset(MF_OPTIONAL
, map
->map_flags
))
syserr("Cannot open DBM database %s", map
->map_file
);
map
->map_db
= (void *) dbm
;
** DBM_MAP_LOOKUP -- look up a datum in a DBM-type map
** map -- the map to look up in.
** buf -- a pointer to to the buffer containing the key.
** This is a null terminated string.
** bufsiz -- the size of buf -- note that this is in general
** larger that strlen(buf), and buf can be changed
** av -- arguments from the config file (can be interpolated
** into the final result).
** A pointer to the rewritten result.
** NULL if not found in the map.
dbm_map_lookup(map
, buf
, bufsiz
, av
)
if (bitset(MF_INCLNULL
, map
->map_flags
))
val
= dbm_fetch(map
->map_db
, key
);
map_rewrite(val
.dptr
, val
.dsize
, buf
, bufsiz
, av
);
** BTREE_MAP_INIT -- BTREE-style map initialization
** map -- the pointer to the actual map
** mapname -- the name of the map (for error messages)
** args -- a pointer to the config file line arguments
** TRUE -- if it could successfully open the map.
** Gives an error if it can't open the map.
bt_map_init(map
, mapname
, args
)
map_parseargs(map
, &args
, mapname
);
if (map
->map_file
== NULL
)
db
= dbopen(map
->map_file
, O_RDONLY
, 0644, DB_BTREE
, NULL
);
if (!bitset(MF_OPTIONAL
, map
->map_flags
))
syserr("Cannot open BTREE database %s", map
->map_file
);
map
->map_db
= (void *) db
;
** HASH_MAP_INIT -- HASH-style map initialization
** map -- the pointer to the actual map
** mapname -- the name of the map (for error messages)
** args -- a pointer to the config file line arguments
** TRUE -- if it could successfully open the map.
** Gives an error if it can't open the map.
hash_map_init(map
, mapname
, args
)
map_parseargs(map
, &args
, mapname
);
if (map
->map_file
== NULL
)
db
= dbopen(map
->map_file
, O_RDONLY
, 0644, DB_HASH
, NULL
);
if (!bitset(MF_OPTIONAL
, map
->map_flags
))
syserr("Cannot open HASH database %s", map
->map_file
);
map
->map_db
= (void *) db
;
#if defined(BTREE_MAP) || defined(HASH_MAP)
** DB_MAP_LOOKUP -- look up a datum in a BTREE- or HASH-type map
** map -- the map to look up in.
** buf -- a pointer to to the buffer containing the key.
** This is a null terminated string.
** bufsiz -- the size of buf -- note that this is in general
** larger that strlen(buf), and buf can be changed
** av -- arguments from the config file (can be interpolated
** into the final result).
** A pointer to the rewritten result.
** NULL if not found in the map.
db_map_lookup(map
, buf
, bufsiz
, av
)
if (bitset(MF_INCLNULL
, map
->map_flags
))
if (((DB
*) map
->map_db
)->get((DB
*) map
->map_db
, &key
, &val
, 0) != 0)
map_rewrite(val
.data
, val
.size
, buf
, bufsiz
, av
);
#endif /* BTREE_MAP || HASH_MAP */
** MAP_PARSEARGS -- parse config line arguments for database lookup
** map -- the map being initialized.
** pp -- an indirect pointer to the config line. It will
** be replaced with a pointer to the next field
** mapname -- the name of the map (for errors).
** null terminates the filename; stores it in map
map_parseargs(map
, pp
, mapname
)
map
->map_flags
|= MF_INCLNULL
;
map
->map_flags
|= MF_OPTIONAL
;
while (*p
!= '\0' && !isspace(*p
))
if (map
->map_app
!= NULL
)
map
->map_app
= newstr(map
->map_app
);
syserr("No file name for map %s", mapname
);
while (*p
!= '\0' && !isspace(*p
))
map
->map_file
= newstr(map
->map_file
);
** MAP_REWRITE -- rewrite a database key, interpolating %n indications.
** s -- the string to rewrite, NOT necessarily null terminated.
** slen -- the length of s.
** buf -- the place to write it.
** buflen -- the length of buf.
** av -- arguments to interpolate into buf.
map_rewrite(s
, slen
, buf
, buflen
, av
)
printf("map_rewrite(%.*s), av =\n", slen
, s
);
for (avp
= av
; *avp
!= NULL
; avp
++)
buflim
= &buf
[buflen
- 2];
while (--slen
>= 0 && (c
= *s
++) != '\0')
if (--slen
< 0 || (c
= *s
++) == '\0')
for (avp
= av
; --c
>= 0 && *avp
!= NULL
; avp
++)
/* transliterate argument into output string */
for (ap
= *avp
; (c
= *ap
++) != '\0'; )
printf("map_rewrite => %s\n", buf
);