diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 1996-05-22 11:38:55 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 1996-05-22 11:38:55 +0000 |
commit | cebe7bf71ac41caf0b9a107991a848b91aa92a11 (patch) | |
tree | a2908b2125c81277f81f1cd4b6fa3258ef770c85 /lib | |
parent | 0157a77a51c5e35e093ae03581f66dea010edcc8 (diff) |
svr4-style gencat
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/nls/Makefile.inc | 7 | ||||
-rw-r--r-- | lib/libc/nls/_catclose.c | 25 | ||||
-rw-r--r-- | lib/libc/nls/_catgets.c | 28 | ||||
-rw-r--r-- | lib/libc/nls/_catopen.c | 26 | ||||
-rw-r--r-- | lib/libc/nls/catclose.c | 66 | ||||
-rw-r--r-- | lib/libc/nls/catgets.c | 110 | ||||
-rw-r--r-- | lib/libc/nls/catopen.c | 154 | ||||
-rw-r--r-- | lib/libc/nls/msgcat.c | 396 | ||||
-rw-r--r-- | lib/libc/nls/msgcat.h | 170 |
9 files changed, 368 insertions, 614 deletions
diff --git a/lib/libc/nls/Makefile.inc b/lib/libc/nls/Makefile.inc index f72d0917f33..c1fef07abaf 100644 --- a/lib/libc/nls/Makefile.inc +++ b/lib/libc/nls/Makefile.inc @@ -1,6 +1,9 @@ -# $NetBSD: Makefile.inc,v 1.7 1995/02/27 13:06:20 cgd Exp $ +# $NetBSD: Makefile.inc,v 1.8 1996/05/13 23:29:32 jtc Exp $ .PATH: ${.CURDIR}/nls -SRCS+= catclose.c catgets.c catopen.c msgcat.c +SRCS+= catclose.c catgets.c catopen.c MAN+= catclose.3 catgets.3 catopen.3 + +# indirect reference stubs, to be removed soon. +SRCS+= _catclose.c _catgets.c _catopen.c diff --git a/lib/libc/nls/_catclose.c b/lib/libc/nls/_catclose.c new file mode 100644 index 00000000000..74c214d97a4 --- /dev/null +++ b/lib/libc/nls/_catclose.c @@ -0,0 +1,25 @@ +/* $NetBSD: _catclose.c,v 1.1 1996/05/13 23:29:34 jtc Exp $ */ + +/* + * Written by J.T. Conklin, 10/05/94 + * Public domain. + */ + +#include <sys/cdefs.h> + +#ifdef __indr_reference +__indr_reference(_catclose,catclose); +#else + +#include <nl_types.h> + +extern int _catclose __P((nl_catd)); + +int +catclose(catd) + nl_catd catd; +{ + return _catclose(catd); +} + +#endif diff --git a/lib/libc/nls/_catgets.c b/lib/libc/nls/_catgets.c new file mode 100644 index 00000000000..2648bfb0222 --- /dev/null +++ b/lib/libc/nls/_catgets.c @@ -0,0 +1,28 @@ +/* $NetBSD: _catgets.c,v 1.2 1996/05/16 21:51:22 cgd Exp $ */ + +/* + * Written by J.T. Conklin, 10/05/94 + * Public domain. + */ + +#include <sys/cdefs.h> + +#ifdef __indr_reference +__indr_reference(_catgets,catgets); +#else + +#include <nl_types.h> + +extern char * _catgets __P((nl_catd, int, int, const char *)); + +char * +catgets(catd, set_id, msg_id, s) + nl_catd catd; + int set_id; + int msg_id; + const char *s; +{ + return _catgets(catd, set_id, msg_id, s); +} + +#endif diff --git a/lib/libc/nls/_catopen.c b/lib/libc/nls/_catopen.c new file mode 100644 index 00000000000..30cd40c7375 --- /dev/null +++ b/lib/libc/nls/_catopen.c @@ -0,0 +1,26 @@ +/* $NetBSD: _catopen.c,v 1.1 1996/05/13 23:29:36 jtc Exp $ */ + +/* + * Written by J.T. Conklin, 10/05/94 + * Public domain. + */ + +#include <sys/cdefs.h> + +#ifdef __indr_reference +__indr_reference(_catopen,catopen); +#else + +#include <nl_types.h> + +extern nl_catd _catopen __P((__const char *, int)); + +nl_catd +catopen(name, oflag) + __const char *name; + int oflag; +{ + return _catopen(name, oflag); +} + +#endif diff --git a/lib/libc/nls/catclose.c b/lib/libc/nls/catclose.c index d10d1826b35..7054ada5f2a 100644 --- a/lib/libc/nls/catclose.c +++ b/lib/libc/nls/catclose.c @@ -1,25 +1,61 @@ -/* $NetBSD: catclose.c,v 1.6 1995/03/23 19:59:03 jtc Exp $ */ +/* $NetBSD: catclose.c,v 1.7 1996/05/13 23:29:37 jtc Exp $ */ -/* - * Written by J.T. Conklin, 10/05/94 - * Public domain. +/*- + * Copyright (c) 1996 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ -#include <sys/cdefs.h> - -#ifdef __indr_reference -__indr_reference(_catclose,catclose); -#else +#define _NLS_PRIVATE +#include <sys/types.h> +#include <sys/mman.h> +#include <errno.h> #include <nl_types.h> -extern int _catclose __P((nl_catd)); - int -catclose(catd) +_catclose(catd) nl_catd catd; { - return _catclose(catd); -} + if (catd == (nl_catd) -1) { + errno = EBADF; + return -1; + } + + if (catd) { + munmap(catd->__data, catd->__size); + free (catd); + } -#endif + return 0; +} diff --git a/lib/libc/nls/catgets.c b/lib/libc/nls/catgets.c index 9f361ea8490..248e6ffd4ef 100644 --- a/lib/libc/nls/catgets.c +++ b/lib/libc/nls/catgets.c @@ -1,28 +1,106 @@ -/* $NetBSD: catgets.c,v 1.7 1995/03/23 19:59:05 jtc Exp $ */ +/* $NetBSD: catgets.c,v 1.8 1996/05/13 23:29:38 jtc Exp $ */ -/* - * Written by J.T. Conklin, 10/05/94 - * Public domain. +/*- + * Copyright (c) 1996 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ -#include <sys/cdefs.h> - -#ifdef __indr_reference -__indr_reference(_catgets,catgets); -#else +#define _NLS_PRIVATE +#include <stdlib.h> +#include <string.h> #include <nl_types.h> -extern char * _catgets __P((nl_catd, int, int, char *)); - char * -catgets(catd, set_id, msg_id, s) +_catgets(catd, set_id, msg_id, s) nl_catd catd; int set_id; int msg_id; - char *s; + const char *s; { - return _catgets(catd, set_id, msg_id, s); -} + struct _nls_cat_hdr *cat_hdr; + struct _nls_set_hdr *set_hdr; + struct _nls_msg_hdr *msg_hdr; + int l, u, i, r; + + if (catd == (nl_catd) 0 || catd == (nl_catd) -1) { + return (char *) s; + } + + cat_hdr = (struct _nls_cat_hdr *) catd->__data; + set_hdr = (struct _nls_set_hdr *) (catd->__data + + sizeof(struct _nls_cat_hdr)); + + /* binary search, see knuth algorithm b */ + l = 0; + u = ntohl(cat_hdr->__nsets) - 1; + while (l <= u) { + i = (l + u) / 2; + r = set_id - ntohl(set_hdr[i].__setno); -#endif + if (r == 0) { + msg_hdr = (struct _nls_msg_hdr *) (catd->__data + + sizeof(struct _nls_cat_hdr) + + ntohl(cat_hdr->__msg_hdr_offset)); + + l = ntohl(set_hdr[i].__index); + u = l + ntohl(set_hdr[i].__nmsgs) - 1; + while (l <= u) { + i = (l + u) / 2; + r = msg_id - ntohl(msg_hdr[i].__msgno); + if (r == 0) { + return (char *) (catd->__data + + sizeof(struct _nls_cat_hdr) + + ntohl(cat_hdr->__msg_txt_offset) + + ntohl(msg_hdr[i].__offset)); + } else if (r < 0) { + u = i - 1; + } else { + l = i + 1; + } + } + + /* not found */ + return (char *) s; + + } else if (r < 0) { + u = i - 1; + } else { + l = i + 1; + } + } + + /* not found */ + return (char *) s; +} diff --git a/lib/libc/nls/catopen.c b/lib/libc/nls/catopen.c index 0e2f9716fbb..225f35515b6 100644 --- a/lib/libc/nls/catopen.c +++ b/lib/libc/nls/catopen.c @@ -1,26 +1,150 @@ -/* $NetBSD: catopen.c,v 1.5 1995/03/23 19:59:06 jtc Exp $ */ +/* $NetBSD: catopen.c,v 1.6 1996/05/13 23:29:39 jtc Exp $ */ -/* - * Written by J.T. Conklin, 10/05/94 - * Public domain. +/*- + * Copyright (c) 1996 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by J.T. Conklin. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ -#include <sys/cdefs.h> - -#ifdef __indr_reference -__indr_reference(_catopen,catopen); -#else +#define _NLS_PRIVATE +#include <limits.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <sys/param.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <unistd.h> +#include <fcntl.h> #include <nl_types.h> -extern nl_catd _catopen __P((__const char *, int)); +#define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L" +#define NLS_DEFAULT_LANG "C" nl_catd -catopen(name, oflag) - __const char *name; +_catopen(name, oflag) + const char *name; int oflag; { - return _catopen(name, oflag); -} + const char *path; + struct stat st; + nl_catd catd; + int fd; + + struct _nls_cat_hdr *cat_hdr; + + if (name == NULL || *name == '\0') + return (nl_catd) -1; + + /* absolute or relative path? */ + if (strchr (name, '/')) { + if (stat (name, &st)) { + return (nl_catd) 0; + } + path = name; + } else { + char tmppath[PATH_MAX]; + char *nlspath; + char *lang; + char *s, *t; + + if ((nlspath = getenv ("NLSPATH")) == NULL) { + nlspath = NLS_DEFAULT_PATH; + } + if ((lang = getenv ("LANG")) == NULL) { + lang = NLS_DEFAULT_LANG; + } + + for (s = nlspath, t = tmppath; *s; ) { + if (*s == '%') { + if (*(s + 1) == 'L') { + strcpy(t, lang); + t += strlen(lang); + s += 2; + } else if (*(s + 1) == 'N') { + strcpy(t, name); + t += strlen(name); + s += 2; + } else { + *t++ = *s++; + } + } else if (*s == ':') { + *t = '\0'; + + if (stat (tmppath, &st) == 0) { + path = tmppath; + goto load_msgcat; + } + + t = tmppath; + } else { + *t++ = *s++; + } + } -#endif + return (nl_catd) 0; + } + +load_msgcat: + if ((fd = open (path, O_RDONLY)) == -1) + return (nl_catd) 0; + + if (fstat(fd, &st) != 0) { + close (fd); + return (nl_catd) 0; + } + + if ((catd = malloc (sizeof (*catd))) == 0) { + close (fd); + return (nl_catd) 0; + } + + catd->__data = mmap(0, (size_t) st.st_size, PROT_READ, MAP_SHARED, fd, 0); + close (fd); + + if (catd->__data == (void *) -1) { + free (catd); + return (nl_catd) 0; + } + catd->__size = st.st_size; + + cat_hdr = (struct _nls_cat_hdr *) catd->__data; + if (ntohl(cat_hdr->__magic) != _NLS_MAGIC) { + free (catd); + close (fd); + return (nl_catd) 0; + } + + return catd; +} diff --git a/lib/libc/nls/msgcat.c b/lib/libc/nls/msgcat.c deleted file mode 100644 index 2c773f4be20..00000000000 --- a/lib/libc/nls/msgcat.c +++ /dev/null @@ -1,396 +0,0 @@ -/* $NetBSD: msgcat.c,v 1.11 1995/02/27 13:06:51 cgd Exp $ */ - -/*********************************************************** -Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts. - - All Rights Reserved - -Permission to use, copy, modify, and distribute this software and its -documentation for any purpose and without fee is hereby granted, -provided that the above copyright notice appear in all copies and that -both that copyright notice and this permission notice appear in -supporting documentation, and that Alfalfa's name not be used in -advertising or publicity pertaining to distribution of the software -without specific, written prior permission. - -ALPHALPHA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING -ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL -ALPHALPHA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR -ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, -WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS -SOFTWARE. - -If you make any modifications, bugfixes or other changes to this software -we'd appreciate it if you could send a copy to us so we can keep things -up-to-date. Many thanks. - Kee Hinckley - Alfalfa Software, Inc. - 267 Allston St., #3 - Cambridge, MA 02139 USA - nazgul@alfalfa.com - -******************************************************************/ - -#if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$NetBSD: msgcat.c,v 1.11 1995/02/27 13:06:51 cgd Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* Edit History - -03/06/91 4 schulert remove working directory from nlspath -01/18/91 2 hamilton #if not rescanned -01/12/91 3 schulert conditionally use prototypes -11/03/90 1 hamilton Alphalpha->Alfalfa & OmegaMail->Poste -10/15/90 2 schulert > #include <unistd.h> if MIPS -08/13/90 1 schulert move from ua to omu -*/ - -/* - * We need a better way of handling errors than printing text. I need - * to add an error handling routine. - */ - -#include "nl_types.h" -#include "msgcat.h" - -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#ifndef True -# define True ~0 -# define False 0 -#endif - -/* take care of sysv diffs */ -#ifndef MAXPATHLEN -#define MAXPATHLEN 1024 -#endif - -#ifndef FD_CLOEXEC -#define FD_CLOEXEC 1 -#endif - -#define NLERR ((nl_catd) -1) - -static nl_catd loadCat(); -static nl_catd loadSet(); - -nl_catd _catopen( name, type) -__const char *name; -int type; -{ - char path[MAXPATHLEN]; - __const char *catpath = NULL; - char *nlspath, *tmppath = NULL; - char *lang; - long len; - char *base, *cptr, *pathP; - struct stat sbuf; - - if (!name || !*name) return(NLERR); - - if (strchr(name, '/')) { - catpath = name; - if (stat(catpath, &sbuf)) return(0); - } else { - if ((lang = (char *) getenv("LANG")) == NULL) lang = "C"; - if ((nlspath = (char *) getenv("NLSPATH")) == NULL) { - nlspath = "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L"; - } - - len = strlen(nlspath); - base = cptr = (char *) malloc(len + 2); - if (!base) return(NLERR); - strcpy(cptr, nlspath); - cptr[len] = ':'; - cptr[len+1] = '\0'; - - for (nlspath = cptr; *cptr; ++cptr) { - if (*cptr == ':') { - *cptr = '\0'; - for (pathP = path; *nlspath; ++nlspath) { - if (*nlspath == '%') { - if (*(nlspath + 1) == 'L') { - ++nlspath; - strcpy(pathP, lang); - pathP += strlen(lang); - } else if (*(nlspath + 1) == 'N') { - ++nlspath; - strcpy(pathP, name); - pathP += strlen(name); - } else *(pathP++) = *nlspath; - } else *(pathP++) = *nlspath; - } - *pathP = '\0'; - if (stat(path, &sbuf) == 0) { - catpath = path; - break; - } - nlspath = cptr+1; - } - } - free(base); - if (tmppath) free(tmppath); - - if (!catpath) return(0); - } - - return(loadCat(catpath, type)); -} - -/* - * We've got an odd situation here. The odds are real good that the - * number we are looking for is almost the same as the index. We could - * use the index, check the difference and do something intelligent, but - * I haven't quite figured out what's intelligent. - * - * Here's a start. - * Take an id N. If there are > N items in the list, then N cannot - * be more than N items from the start, since otherwise there would - * have to be duplicate items. So we can safely set the top to N+1 - * (after taking into account that ids start at 1, and arrays at 0) - * - * Let's say we are at position P, and we are looking for N, but have - * V. If N > V, then the furthest away that N could be is - * P + (N-V). So we can safely set hi to P+(N-V)+1. For example: - * We are looking for 10, but have 8 - * 8 ? ? ? ? - * >=9 >=10 >=11 - * - */ -static MCSetT *MCGetSet( cat, setId) -MCCatT *cat; -int setId; -{ - MCSetT *set; - long lo, hi, cur, dir; - - if (!cat || setId <= 0) return(NULL); - - lo = 0; - if (setId - 1 < cat->numSets) { - cur = setId - 1; - hi = setId; - } else { - hi = cat->numSets; - cur = (hi - lo) / 2; - } - - while (True) { - set = cat->sets + cur; - if (set->setId == setId) break; - if (set->setId < setId) { - lo = cur+1; - if (hi > cur + (setId - set->setId) + 1) hi = cur+(setId-set->setId)+1; - dir = 1; - } else { - hi = cur; - dir = -1; - } - if (lo >= hi) return(NULL); - if (hi - lo == 1) cur += dir; - else cur += ((hi - lo) / 2) * dir; - } - if (set->invalid) loadSet(cat, set); - return(set); -} - - -static MCMsgT *MCGetMsg( set, msgId) -MCSetT *set; -int msgId; -{ - MCMsgT *msg; - long lo, hi, cur, dir; - - if (!set || set->invalid || msgId <= 0) return(NULL); - - lo = 0; - if (msgId - 1 < set->numMsgs) { - cur = msgId - 1; - hi = msgId; - } else { - hi = set->numMsgs; - cur = (hi - lo) / 2; - } - - while (True) { - msg = set->u.msgs + cur; - if (msg->msgId == msgId) break; - if (msg->msgId < msgId) { - lo = cur+1; - if (hi > cur + (msgId - msg->msgId) + 1) hi = cur+(msgId-msg->msgId)+1; - dir = 1; - } else { - hi = cur; - dir = -1; - } - if (lo >= hi) return(NULL); - if (hi - lo == 1) cur += dir; - else cur += ((hi - lo) / 2) * dir; - } - return(msg); -} - -char *_catgets( catd, setId, msgId, dflt) -nl_catd catd; -int setId; -int msgId; -char *dflt; -{ - MCMsgT *msg; - MCCatT *cat = (MCCatT *) catd; - char *cptr; - - msg = MCGetMsg(MCGetSet(cat, setId), msgId); - if (msg) cptr = msg->msg.str; - else cptr = dflt; - return(cptr); -} - - -int _catclose( catd) -nl_catd catd; -{ - MCCatT *cat = (MCCatT *) catd; - MCSetT *set; - MCMsgT *msg; - int i, j; - - if (!cat) return -1; - - if (cat->loadType != MCLoadAll) close(cat->fd); - for (i = 0; i < cat->numSets; ++i) { - set = cat->sets + i; - if (!set->invalid) { - free(set->data.str); - free(set->u.msgs); - } - } - free(cat->sets); - free(cat); - - return 0; -} - -/* - * Internal routines - */ - -/* Note that only malloc failures are allowed to return an error */ -#define ERRNAME "Message Catalog System" -#define CORRUPT() {fprintf(stderr, "%s: corrupt file.\n", ERRNAME); return(0);} -#define NOSPACE() {fprintf(stderr, "%s: no more memory.\n", ERRNAME); return(NLERR);} - -static nl_catd loadCat( catpath, type) -__const char *catpath; -int type; -{ - MCHeaderT header; - MCCatT *cat; - MCSetT *set; - MCMsgT *msg; - long i, j; - off_t nextSet; - - cat = (MCCatT *) malloc(sizeof(MCCatT)); - if (!cat) return(NLERR); - cat->loadType = type; - - if ((cat->fd = open(catpath, O_RDONLY)) < 0) { - return(0); - } - - fcntl(cat->fd, F_SETFD, FD_CLOEXEC); - - if (read(cat->fd, &header, sizeof(header)) != sizeof(header)) CORRUPT(); - - if (strncmp(header.magic, MCMagic, MCMagicLen) != 0) CORRUPT(); - - if (header.majorVer != MCMajorVer) { - fprintf(stderr, "%s: %s is version %d, we need %d.\n", ERRNAME, - catpath, header.majorVer, MCMajorVer); - return(0); - } - - if (header.numSets <= 0) { - fprintf(stderr, "%s: %s has %d sets!\n", ERRNAME, catpath, - header.numSets); - return(0); - } - - cat->numSets = header.numSets; - cat->sets = (MCSetT *) malloc(sizeof(MCSetT) * header.numSets); - if (!cat->sets) NOSPACE(); - - nextSet = header.firstSet; - for (i = 0; i < cat->numSets; ++i) { - if (lseek(cat->fd, nextSet, 0) == -1) CORRUPT(); - - /* read in the set header */ - set = cat->sets + i; - if (read(cat->fd, set, sizeof(*set)) != sizeof(*set)) CORRUPT(); - - /* if it's invalid, skip over it (and backup 'i') */ - - if (set->invalid) { - --i; - nextSet = set->nextSet; - continue; - } - - if (cat->loadType == MCLoadAll) { - nl_catd res; - if ((res = loadSet(cat, set)) <= 0) { - if (res == -1) NOSPACE(); - CORRUPT(); - } - } else set->invalid = True; - nextSet = set->nextSet; - } - if (cat->loadType == MCLoadAll) { - close(cat->fd); - cat->fd = -1; - } - return((nl_catd) cat); -} - -static nl_catd loadSet( cat, set) -MCCatT *cat; -MCSetT *set; -{ - MCMsgT *msg; - int i; - - /* Get the data */ - if (lseek(cat->fd, set->data.off, 0) == -1) return(0); - if ((set->data.str = (char *) malloc(set->dataLen)) == NULL) return(-1); - if (read(cat->fd, set->data.str, set->dataLen) != set->dataLen) return(0); - - /* Get the messages */ - if (lseek(cat->fd, set->u.firstMsg, 0) == -1) return(0); - if ((set->u.msgs = (MCMsgT *) malloc(sizeof(MCMsgT) * set->numMsgs)) == NULL) return(-1); - - for (i = 0; i < set->numMsgs; ++i) { - msg = set->u.msgs + i; - if (read(cat->fd, msg, sizeof(*msg)) != sizeof(*msg)) return(0); - if (msg->invalid) { - --i; - continue; - } - msg->msg.str = (char *) (set->data.str + msg->msg.off); - } - set->invalid = False; - return(1); -} - - - - - diff --git a/lib/libc/nls/msgcat.h b/lib/libc/nls/msgcat.h index 82f411f28a0..e69de29bb2d 100644 --- a/lib/libc/nls/msgcat.h +++ b/lib/libc/nls/msgcat.h @@ -1,170 +0,0 @@ -/* $NetBSD: msgcat.h,v 1.3 1995/02/27 13:06:55 cgd Exp $ */ - -/* -*-c++-*- */ - -#ifndef __msgcath - - -/*********************************************************** -Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts. - - All Rights Reserved - -Permission to use, copy, modify, and distribute this software and its -documentation for any purpose and without fee is hereby granted, -provided that the above copyright notice appear in all copies and that -both that copyright notice and this permission notice appear in -supporting documentation, and that Alfalfa's name not be used in -advertising or publicity pertaining to distribution of the software -without specific, written prior permission. - -ALPHALPHA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING -ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL -ALPHALPHA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR -ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, -WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS -SOFTWARE. - -If you make any modifications, bugfixes or other changes to this software -we'd appreciate it if you could send a copy to us so we can keep things -up-to-date. Many thanks. - Kee Hinckley - Alfalfa Software, Inc. - 267 Allston St., #3 - Cambridge, MA 02139 USA - nazgul@alfalfa.com - -******************************************************************/ - - -#include <sys/types.h> - -/* - * On disk data structures - */ - -/* Edit History - -02/25/91 2 nazgul Byte order flags, upped the version number -11/03/90 1 hamilton Alphalpha->Alfalfa & OmegaMail->Poste -08/13/90 1 schulert move from ua to omu -*/ - -/* For or'd constants */ -#define MCMakeId(s,m) (unsigned long) ( ((unsigned short)s << (sizeof(short)*8)) \ - | (unsigned short)m ) -#define MCSetId(id) (unsigned int) ( id >> (sizeof(short) * 8) ) -#define MCMsgId(id) (unsigned int) ( (id << (sizeof(short) * 8)) \ - >> (sizeof(short) * 8) ) -#undef S -#undef UI -#undef UL - -#define MCMagicLen 8 -#define MCMagic "*nazgul*" -#define MCLastMsg 0 -#define MCLastSet 0 - -#define MCMajorVer 1 -#define MCMinorVer 0 - -/* - * Critical note here. Sets and Messages *MUST* be stored in ascending - * order. There are stored that way (by specification) in the original - * data file, however in the process of merging in new stuff you might - * mix that up. Don't! The catget stuff does a binary search and will - * totally lose it if these aren't in order (not contiguous mind you, just - * in order. If this turns out to be a major problem this could be enhanced - * by adding a 'sorted' flag to the db, and sorting msgs and sets at load - * time if things aren't sorted, but I'd like not to have to do that. - */ - -/* - * I have tried here to define data structures which can be used - * while the catalog is on disk, and at runtime. - * This is rather dangerous of course, but I think it can be done without - * overly increasing the memory usage, and it makes loading and storing - * somewhat simpler and less prone to accidents. I have also tried to - * define on disk data structures which can be updated in place, so that - * with a very large catalog (e.g. all system errors) you don't have to - * load everything in memory in order to add or update one set. With - * this in mind there are "invalid" flags which allow items to be - * invalidated and thus not loaded at runtime. Note however that although - * I pay attention to these when I load the DB, I do not currently use - * them in gencat (it just reads everything into memory), so there is - * no guarantee that this will all work. - */ - -/* These should be publicly available */ - -#define MCLoadBySet 0 /* Load entire sets as they are used */ -#define MCLoadAll 1 /* Load entire DB on catopen */ - -/* - * MCOffsetT - Union to handle both disk and runtime pointers - */ -typedef union { - off_t off; - char *str; - void *ptr; - struct _MCMsgT *msg; - struct _MCSetT *set; -} MCOffsetT; - -/* - * MCMsgT - Message structure (disk and runtime) - */ -typedef struct _MCMsgT { - long msgId; /* Id of this message */ - MCOffsetT msg; /* Relative offset on disk or pointer in memory */ - long invalid; /* Valid on disk, loaded in memory */ -} MCMsgT; - -/* - * MCSetT - Set structure (disk and runtime) - */ -typedef struct _MCSetT { - long setId; /* Id of this set */ - off_t nextSet; /* Offset of next set on disk */ - union { - off_t firstMsg; /* Offset to first Msg (while on disk) */ - MCMsgT *msgs; /* Pointer to array of msgs (in mem, loaded) */ - } u; - MCOffsetT data; /* Offset to data, or pointer to data */ - long dataLen; /* Length of data area on disk */ - long numMsgs; /* Number of messages */ - long invalid; /* Valid on disk, loaded in memory */ -} MCSetT; - -/* - * MCCatT - Runtime catalog pointer - */ -typedef struct { - long loadType; /* How to load the messages (see MSLoadType) */ - int fd; /* File descriptor of catalog (if load-on-demand) */ - long numSets; /* Number of sets */ - MCSetT *sets; /* Pointer to the sets */ - off_t firstSet; /* Offset of first set on disk */ -} MCCatT; - -/* - * MCHeaderT - Disk file header - */ -typedef struct { - char magic[MCMagicLen]; /* Magic cookie "*nazgul*" */ - long majorVer; /* ++ on incompatible changes */ - long minorVer; /* ++ on compatible changes */ - long flags; /* Informational flags */ - long numSets; /* Number of valid Sets */ - off_t firstSet; /* Offset of first set on disk */ -} MCHeaderT; - -/* Some flags */ -#define MC68KByteOrder 0x01 -#define MCn86ByteOrder 0x02 - - - - -#endif |