* Copyright (c) 1992 Eric P. Allman.
* Copyright (c) 1992 Regents of the University of California.
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)map.c 6.12 (Berkeley) %G%";
#if defined(HASH_MAP) || defined(BTREE_MAP)
#include <rpcsvc/ypclnt.h>
** 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
);
if (map
->map_file
== NULL
)
syserr("No file name for DBM map %s", mapname
);
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).
** statp -- pointer to status word (out-parameter).
** A pointer to the rewritten result.
** NULL if not found in the map.
dbm_map_lookup(map
, buf
, bufsiz
, av
, statp
)
if (!bitset(MF_NOFOLDCASE
, map
->map_flags
))
for (p
= buf
; *p
!= '\0'; p
++)
if (isascii(*p
) && isupper(*p
))
if (bitset(MF_INCLNULL
, map
->map_flags
))
(void) lockfile(dbm_dirfno((DBM
*) map
->map_db
), map
->map_file
, LOCK_SH
);
val
= dbm_fetch((DBM
*) map
->map_db
, key
);
(void) lockfile(dbm_dirfno((DBM
*) map
->map_db
), map
->map_file
, LOCK_UN
);
if (!bitset(MF_MATCHONLY
, map
->map_flags
))
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
);
if (map
->map_file
== NULL
)
syserr("No file name for BTREE map %s", mapname
);
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
);
if (map
->map_file
== NULL
)
syserr("No file name for HASH map %s", mapname
);
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).
** statp -- pointer to status word (out-parameter).
** A pointer to the rewritten result.
** NULL if not found in the map.
db_map_lookup(map
, buf
, bufsiz
, av
, statp
)
if (!bitset(MF_NOFOLDCASE
, map
->map_flags
))
for (p
= buf
; *p
!= '\0'; p
++)
if (isascii(*p
) && isupper(*p
))
if (bitset(MF_INCLNULL
, map
->map_flags
))
if (((DB
*) map
->map_db
)->get((DB
*) map
->map_db
, &key
, &val
, 0) != 0)
if (!bitset(MF_MATCHONLY
, map
->map_flags
))
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
** null terminates the filename; stores it in map
while (isascii(*p
) && isspace(*p
))
map
->map_flags
|= MF_INCLNULL
;
map
->map_flags
|= MF_OPTIONAL
;
map
->map_flags
|= MF_NOFOLDCASE
;
map
->map_flags
|= MF_MATCHONLY
;
while (*p
!= '\0' && !(isascii(*p
) && isspace(*p
)))
if (map
->map_app
!= NULL
)
map
->map_app
= newstr(map
->map_app
);
if (map
->map_domain
!= NULL
)
map
->map_domain
= newstr(map
->map_domain
);
while (*p
!= '\0' && !(isascii(*p
) && isspace(*p
)))
map
->map_file
= newstr(map
->map_file
);
while (*p
!= '\0' && isascii(*p
) && isspace(*p
))
map
->map_rebuild
= newstr(p
);
** NIS_MAP_INIT -- initialize DBM map
** 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.
** Prints an error if it can't open the map.
nis_map_init(map
, mapname
, args
)
map_parseargs(map
, &args
);
if (map
->map_file
== NULL
)
syserr("No NIS map name for map %s", mapname
);
if (map
->map_domain
== NULL
)
yp_get_default_domain(&map
->map_domain
);
/* check to see if this map actually exists */
yperr
= yp_master(map
->map_domain
, map
->map_file
, &master
);
if (!bitset(MF_OPTIONAL
, map
->map_flags
))
syserr("Cannot bind to domain %s: %s", map
->map_domain
,
** NIS_MAP_LOOKUP -- look up a datum in a NIS 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).
** statp -- pointer to status word (out-parameter).
** A pointer to the rewritten result.
** NULL if not found in the map.
nis_map_lookup(map
, buf
, bufsiz
, av
, statp
)
if (!bitset(MF_NOFOLDCASE
, map
->map_flags
))
for (p
= buf
; *p
!= '\0'; p
++)
if (isascii(*p
) && isupper(*p
))
if (bitset(MF_INCLNULL
, map
->map_flags
))
if (yp_match(map
->map_domain
, map
->map_file
, buf
, buflen
,
if (!bitset(MF_MATCHONLY
, map
->map_flags
))
map_rewrite(vp
, vsize
, buf
, bufsiz
, av
);
** 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')
if (!(isascii(c
) && isdigit(c
)))
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
);