summaryrefslogtreecommitdiffstats
path: root/lib/libc/nls
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/nls')
-rw-r--r--lib/libc/nls/Makefile.inc8
-rw-r--r--lib/libc/nls/catclose.364
-rw-r--r--lib/libc/nls/catgets.369
-rw-r--r--lib/libc/nls/catopen.3157
-rw-r--r--lib/libc/nls/msgcat.c503
-rw-r--r--lib/libc/nls/msgcat.h155
6 files changed, 956 insertions, 0 deletions
diff --git a/lib/libc/nls/Makefile.inc b/lib/libc/nls/Makefile.inc
new file mode 100644
index 0000000..9adab3d
--- /dev/null
+++ b/lib/libc/nls/Makefile.inc
@@ -0,0 +1,8 @@
+# from $NetBSD: Makefile.inc,v 1.7 1995/02/27 13:06:20 cgd Exp $
+# $FreeBSD$
+
+.PATH: ${.CURDIR}/nls
+
+SRCS+= msgcat.c
+
+MAN+= catclose.3 catgets.3 catopen.3
diff --git a/lib/libc/nls/catclose.3 b/lib/libc/nls/catclose.3
new file mode 100644
index 0000000..733face
--- /dev/null
+++ b/lib/libc/nls/catclose.3
@@ -0,0 +1,64 @@
+.\" Copyright (c) 1994 Winning Strategies, Inc.
+.\" All rights reserved.
+.\"
+.\" 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 Winning Strategies, Inc.
+.\" 4. The name of the author may not be used to endorse or promote products
+.\" derived from this software without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
+.\"
+.\" $FreeBSD$
+.Dd May 29, 1994
+.Dt CATCLOSE 3
+.Os
+.Sh NAME
+.Nm catclose
+.Nd close message catalog
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In nl_types.h
+.Ft int
+.Fn catclose "nl_catd catd"
+.Sh DESCRIPTION
+The
+.Fn catclose
+function closes the message catalog specified by the argument
+.Fa catd .
+.Sh RETURN VALUES
+.Rv -std catclose
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er EBADF
+An invalid message catalog descriptor was passed by the
+.Fa catd
+argument.
+.El
+.Sh SEE ALSO
+.Xr gencat 1 ,
+.Xr catgets 3 ,
+.Xr catopen 3
+.Sh STANDARDS
+The
+.Fn catclose
+function conforms to
+.St -xpg4 .
diff --git a/lib/libc/nls/catgets.3 b/lib/libc/nls/catgets.3
new file mode 100644
index 0000000..592f57c
--- /dev/null
+++ b/lib/libc/nls/catgets.3
@@ -0,0 +1,69 @@
+.\" Copyright (c) 1994 Winning Strategies, Inc.
+.\" All rights reserved.
+.\"
+.\" 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 Winning Strategies, Inc.
+.\" 4. The name of the author may not be used to endorse or promote products
+.\" derived from this software without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
+.\"
+.\" $FreeBSD$
+.Dd May 29, 1994
+.Dt CATGETS 3
+.Os
+.Sh NAME
+.Nm catgets
+.Nd retrieve string from message catalog
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In nl_types.h
+.Ft char *
+.Fn catgets "nl_catd catd" "int set_id" "int msg_id" "const char *s"
+.Sh DESCRIPTION
+The
+.Fn catgets
+function attempts to retrieve message
+.Fa msg_id
+of set
+.Fa set_id
+from the message catalog referenced by the descriptor
+.Fa catd .
+The argument
+.Fa s
+points to a default message which is returned if the function
+is unable to retrieve the specified message.
+.Sh RETURN VALUES
+If the specified message was retrieved successfully,
+.Fn catgets
+returns a pointer to an internal buffer containing the message string;
+otherwise it returns
+.Fa s .
+.Sh SEE ALSO
+.Xr gencat 1 ,
+.Xr catclose 3 ,
+.Xr catopen 3
+.Sh STANDARDS
+The
+.Fn catgets
+function conforms to
+.St -xpg4 .
diff --git a/lib/libc/nls/catopen.3 b/lib/libc/nls/catopen.3
new file mode 100644
index 0000000..9fd8b82
--- /dev/null
+++ b/lib/libc/nls/catopen.3
@@ -0,0 +1,157 @@
+.\" Copyright (c) 1994 Winning Strategies, Inc.
+.\" All rights reserved.
+.\"
+.\" 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 Winning Strategies, Inc.
+.\" 4. The name of the author may not be used to endorse or promote products
+.\" derived from this software without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
+.\"
+.\" $FreeBSD$
+.Dd May 29, 1994
+.Dt CATOPEN 3
+.Os
+.Sh NAME
+.Nm catopen
+.Nd open message catalog
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In nl_types.h
+.Ft nl_catd
+.Fn catopen "const char *name" "int oflag"
+.Sh DESCRIPTION
+The
+.Fn catopen
+function opens the message catalog specified by
+.Fa name
+and returns a message catalog descriptor.
+If
+.Fa name
+contains a
+.Sq /
+then
+.Fa name
+specifies the full pathname for the message catalog, otherwise the value
+of the environment variable
+.Ev NLSPATH
+is used with
+the following substitutions:
+.Bl -tag -width XXX
+.It \&%N
+The value of the
+.Fa name
+argument.
+.It \&%L
+The value of the
+.Ev LANG
+environment variable or the
+.Dv LC_MESSAGES
+category (see below).
+.It \&%l
+The language element from the
+.Ev LANG
+environment variable or from the
+.Dv LC_MESSAGES
+category.
+.It \&%t
+The territory element from the
+.Ev LANG
+environment variable or from the
+.Dv LC_MESSAGES
+category.
+.It \&%c
+The codeset element from the
+.Ev LANG
+environment variable or from the
+.Dv LC_MESSAGES
+category.
+.It \&%%
+A single % character.
+.El
+.Pp
+An empty string is substituted for undefined values.
+.Pp
+Path names templates defined in
+.Ev NLSPATH
+are separated by colons
+.No ( Sq \&: ) .
+A leading or two adjacent colons
+is equivalent to specifying %N.
+.Pp
+If the
+.Fa oflag
+argument is set to the
+.Dv NL_CAT_LOCALE
+constant,
+.Dv LC_MESSAGES
+locale category used to open the message catalog; using
+.Dv NL_CAT_LOCALE
+conforms to the
+.St -xpg4
+standard.
+You can specify 0 for compatibility with
+.St -xpg3 ;
+when
+.Fa oflag
+is set to 0, the
+.Ev LANG
+environment variable
+determines the message catalog locale.
+.Pp
+A message catalog descriptor
+remains valid in a process until that process closes it, or
+until a successful call to one of the
+.Xr exec 3
+function.
+.Sh RETURN VALUES
+Upon successful completion,
+.Fn catopen
+returns a message catalog descriptor.
+Otherwise, (nl_catd) -1 is returned and
+.Va errno
+is set to indicate the error.
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er EINVAL
+Argument
+.Fa name
+does not point to a valid message catalog.
+.It Bq Er ENAMETOOLONG
+An entire path to the message catalog exceeded 1024 characters.
+.It Bq Er ENOENT
+The named message catalog does not exists, or the
+.Fa name
+argument points to an empty string.
+.It Bq Er ENOMEM
+Insufficient memory is available.
+.El
+.Sh SEE ALSO
+.Xr gencat 1 ,
+.Xr catclose 3 ,
+.Xr catgets 3 ,
+.Xr setlocale 3
+.Sh STANDARDS
+The
+.Fn catopen
+function conforms to
+.St -xpg4 .
diff --git a/lib/libc/nls/msgcat.c b/lib/libc/nls/msgcat.c
new file mode 100644
index 0000000..4d44618
--- /dev/null
+++ b/lib/libc/nls/msgcat.c
@@ -0,0 +1,503 @@
+/***********************************************************
+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/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+/*
+ * We need a better way of handling errors than printing text. I need
+ * to add an error handling routine.
+ */
+
+#include "namespace.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <locale.h>
+#include <nl_types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "un-namespace.h"
+
+#include "msgcat.h"
+#include "../locale/setlocale.h" /* for ENCODING_LEN */
+
+#define _DEFAULT_NLS_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L:/usr/local/share/nls/%L/%N.cat:/usr/local/share/nls/%N/%L"
+
+#define TRUE 1
+#define FALSE 0
+
+#define NLERR ((nl_catd) -1)
+#define NLRETERR(errc) { errno = errc; return (NLERR); }
+
+static nl_catd loadCat();
+static int loadSet();
+static void __nls_free_resources();
+
+nl_catd
+catopen(name, type)
+ __const char *name;
+ int type;
+{
+ int spcleft, saverr;
+ char path[PATH_MAX];
+ char *nlspath, *lang, *base, *cptr, *pathP, *tmpptr;
+ char *cptr1, *plang, *pter, *pcode;
+ struct stat sbuf;
+
+ if (name == NULL || *name == '\0')
+ NLRETERR(EINVAL);
+
+ /* is it absolute path ? if yes, load immediately */
+ if (strchr(name, '/') != NULL)
+ return (loadCat(name));
+
+ if (type == NL_CAT_LOCALE)
+ lang = setlocale(LC_MESSAGES, NULL);
+ else
+ lang = getenv("LANG");
+
+ if (lang == NULL || *lang == '\0' || strlen(lang) > ENCODING_LEN ||
+ (lang[0] == '.' &&
+ (lang[1] == '\0' || (lang[1] == '.' && lang[2] == '\0'))) ||
+ strchr(lang, '/') != NULL)
+ lang = "C";
+
+ if ((plang = cptr1 = strdup(lang)) == NULL)
+ return (NLERR);
+ if ((cptr = strchr(cptr1, '@')) != NULL)
+ *cptr = '\0';
+ pter = pcode = "";
+ if ((cptr = strchr(cptr1, '_')) != NULL) {
+ *cptr++ = '\0';
+ pter = cptr1 = cptr;
+ }
+ if ((cptr = strchr(cptr1, '.')) != NULL) {
+ *cptr++ = '\0';
+ pcode = cptr;
+ }
+
+ if ((nlspath = getenv("NLSPATH")) == NULL
+#ifndef __NETBSD_SYSCALLS
+ || issetugid()
+#endif
+ )
+ nlspath = _DEFAULT_NLS_PATH;
+
+ if ((base = cptr = strdup(nlspath)) == NULL) {
+ saverr = errno;
+ free(plang);
+ errno = saverr;
+ return (NLERR);
+ }
+
+ while ((nlspath = strsep(&cptr, ":")) != NULL) {
+ pathP = path;
+ if (*nlspath) {
+ for (; *nlspath; ++nlspath) {
+ if (*nlspath == '%') {
+ switch (*(nlspath + 1)) {
+ case 'l':
+ tmpptr = plang;
+ break;
+ case 't':
+ tmpptr = pter;
+ break;
+ case 'c':
+ tmpptr = pcode;
+ break;
+ case 'L':
+ tmpptr = lang;
+ break;
+ case 'N':
+ tmpptr = (char *)name;
+ break;
+ case '%':
+ ++nlspath;
+ /* fallthrough */
+ default:
+ if (pathP - path >=
+ sizeof(path) - 1)
+ goto too_long;
+ *(pathP++) = *nlspath;
+ continue;
+ }
+ ++nlspath;
+ put_tmpptr:
+ spcleft = sizeof(path) -
+ (pathP - path) - 1;
+ if (strlcpy(pathP, tmpptr, spcleft) >=
+ spcleft) {
+ too_long:
+ free(plang);
+ free(base);
+ NLRETERR(ENAMETOOLONG);
+ }
+ pathP += strlen(tmpptr);
+ } else {
+ if (pathP - path >= sizeof(path) - 1)
+ goto too_long;
+ *(pathP++) = *nlspath;
+ }
+ }
+ *pathP = '\0';
+ if (stat(path, &sbuf) == 0) {
+ free(plang);
+ free(base);
+ return (loadCat(path));
+ }
+ } else {
+ tmpptr = (char *)name;
+ --nlspath;
+ goto put_tmpptr;
+ }
+ }
+ free(plang);
+ free(base);
+ NLRETERR(ENOENT);
+}
+
+/*
+ * 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
+ *
+ */
+
+#define LOOKUP(PARENT, CHILD, ID, NUM, SET) { \
+ lo = 0; \
+ if (ID - 1 < PARENT->NUM) { \
+ cur = ID - 1; \
+ hi = ID; \
+ } else { \
+ hi = PARENT->NUM; \
+ cur = (hi - lo) / 2; \
+ } \
+ while (TRUE) { \
+ CHILD = PARENT->SET + cur; \
+ if (CHILD->ID == ID) \
+ break; \
+ if (CHILD->ID < ID) { \
+ lo = cur + 1; \
+ if (hi > cur + (ID - CHILD->ID) + 1) \
+ hi = cur + (ID - CHILD->ID) + 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; \
+ } \
+}
+
+static MCSetT *
+MCGetSet(cat, setId)
+ MCCatT *cat;
+ int setId;
+{
+ MCSetT *set;
+ long lo, hi, cur, dir;
+
+ if (cat == NULL || setId <= 0)
+ return (NULL);
+ LOOKUP(cat, set, setId, numSets, sets);
+ if (set->invalid && loadSet(cat, set) <= 0)
+ return (NULL);
+ return (set);
+}
+
+static MCMsgT *
+MCGetMsg(set, msgId)
+ MCSetT *set;
+ int msgId;
+{
+ MCMsgT *msg;
+ long lo, hi, cur, dir;
+
+ if (set == NULL || set->invalid || msgId <= 0)
+ return (NULL);
+ LOOKUP(set, msg, msgId, numMsgs, u.msgs);
+ return (msg);
+}
+
+char *
+catgets(catd, setId, msgId, dflt)
+ nl_catd catd;
+ int setId;
+ int msgId;
+ __const char *dflt;
+{
+ MCMsgT *msg;
+ MCCatT *cat = (MCCatT *)catd;
+ __const char *cptr;
+
+ if (catd == NULL || catd == NLERR)
+ return ((char *)dflt);
+ msg = MCGetMsg(MCGetSet(cat, setId), msgId);
+ if (msg != NULL)
+ cptr = msg->msg.str;
+ else
+ cptr = dflt;
+ return ((char *)cptr);
+}
+
+int
+catclose(catd)
+ nl_catd catd;
+{
+ MCCatT *cat = (MCCatT *)catd;
+
+ if (catd == NULL || catd == NLERR) {
+ errno = EBADF;
+ return (-1);
+ }
+#if 0
+ if (cat->loadType != MCLoadAll)
+#endif
+ (void)fclose(cat->fp);
+ __nls_free_resources(cat, cat->numSets);
+ free(cat);
+ return (0);
+}
+
+/*
+ * Internal routines
+ */
+
+/* Note that only malloc failures are allowed to return an error */
+static char *_errowner = "Message Catalog System";
+
+#define CORRUPT() { \
+ (void)fclose(cat->fp); \
+ (void)fprintf(stderr, "%s: corrupt file.", _errowner); \
+ free(cat); \
+ NLRETERR(EFTYPE); \
+}
+
+#define NOSPACE() { \
+ saverr = errno; \
+ (void)fclose(cat->fp); \
+ (void)fprintf(stderr, "%s: no more memory.", _errowner); \
+ free(cat); \
+ errno = saverr; \
+ return (NLERR); \
+}
+
+static void
+__nls_free_resources(cat, i)
+ MCCatT *cat;
+ int i;
+{
+ MCSetT *set;
+ int j;
+
+ for (j = 0; j < i; j++) {
+ set = cat->sets + j;
+ if (!set->invalid) {
+ free(set->data.str);
+ free(set->u.msgs);
+ }
+ }
+ free(cat->sets);
+}
+
+static nl_catd
+loadCat(catpath)
+ __const char *catpath;
+{
+ MCHeaderT header;
+ MCCatT *cat;
+ MCSetT *set;
+ long i;
+ off_t nextSet;
+ int saverr;
+
+ if ((cat = (MCCatT *)malloc(sizeof(MCCatT))) == NULL)
+ return (NLERR);
+ cat->loadType = MCLoadBySet;
+
+ if ((cat->fp = fopen(catpath, "r")) == NULL) {
+ saverr = errno;
+ free(cat);
+ errno = saverr;
+ return (NLERR);
+ }
+ (void)_fcntl(fileno(cat->fp), F_SETFD, FD_CLOEXEC);
+
+ if (fread(&header, sizeof(header), 1, cat->fp) != 1 ||
+ strncmp(header.magic, MCMagic, MCMagicLen) != 0)
+ CORRUPT();
+
+ if (header.majorVer != MCMajorVer) {
+ (void)fclose(cat->fp);
+ free(cat);
+ (void)fprintf(stderr, "%s: %s is version %ld, we need %ld.\n",
+ _errowner, catpath, header.majorVer, MCMajorVer);
+ NLRETERR(EFTYPE);
+ }
+ if (header.numSets <= 0) {
+ (void)fclose(cat->fp);
+ free(cat);
+ (void)fprintf(stderr, "%s: %s has %ld sets!\n",
+ _errowner, catpath, header.numSets);
+ NLRETERR(EFTYPE);
+ }
+
+ cat->numSets = header.numSets;
+ if ((cat->sets = (MCSetT *)malloc(sizeof(MCSetT) * header.numSets)) ==
+ NULL)
+ NOSPACE();
+
+ nextSet = header.firstSet;
+ for (i = 0; i < cat->numSets; ++i) {
+ if (fseeko(cat->fp, nextSet, SEEK_SET) == -1) {
+ __nls_free_resources(cat, i);
+ CORRUPT();
+ }
+
+ /* read in the set header */
+ set = cat->sets + i;
+ if (fread(set, sizeof(*set), 1, cat->fp) != 1) {
+ __nls_free_resources(cat, i);
+ CORRUPT();
+ }
+
+ /* if it's invalid, skip over it (and backup 'i') */
+ if (set->invalid) {
+ --i;
+ nextSet = set->nextSet;
+ continue;
+ }
+#if 0
+ if (cat->loadType == MCLoadAll) {
+ int res;
+
+ if ((res = loadSet(cat, set)) <= 0) {
+ saverr = errno;
+ __nls_free_resources(cat, i);
+ errno = saverr;
+ if (res < 0)
+ NOSPACE();
+ CORRUPT();
+ }
+ } else
+#endif
+ set->invalid = TRUE;
+ nextSet = set->nextSet;
+ }
+#if 0
+ if (cat->loadType == MCLoadAll) {
+ (void)fclose(cat->fp);
+ cat->fp = NULL;
+ }
+#endif
+ return ((nl_catd) cat);
+}
+
+static int
+loadSet(cat, set)
+ MCCatT *cat;
+ MCSetT *set;
+{
+ MCMsgT *msg;
+ int i;
+ int saverr;
+
+ /* Get the data */
+ if (fseeko(cat->fp, set->data.off, SEEK_SET) == -1)
+ return (0);
+ if ((set->data.str = malloc(set->dataLen)) == NULL)
+ return (-1);
+ if (fread(set->data.str, set->dataLen, 1, cat->fp) != 1) {
+ saverr = errno;
+ free(set->data.str);
+ errno = saverr;
+ return (0);
+ }
+
+ /* Get the messages */
+ if (fseeko(cat->fp, set->u.firstMsg, SEEK_SET) == -1) {
+ saverr = errno;
+ free(set->data.str);
+ errno = saverr;
+ return (0);
+ }
+ if ((set->u.msgs = (MCMsgT *)malloc(sizeof(MCMsgT) * set->numMsgs)) ==
+ NULL) {
+ saverr = errno;
+ free(set->data.str);
+ errno = saverr;
+ return (-1);
+ }
+
+ for (i = 0; i < set->numMsgs; ++i) {
+ msg = set->u.msgs + i;
+ if (fread(msg, sizeof(*msg), 1, cat->fp) != 1) {
+ saverr = errno;
+ free(set->u.msgs);
+ free(set->data.str);
+ errno = saverr;
+ 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
new file mode 100644
index 0000000..f13d888
--- /dev/null
+++ b/lib/libc/nls/msgcat.h
@@ -0,0 +1,155 @@
+/* $FreeBSD$ */
+
+#ifndef _MSGCAT_H_
+#define _MSGCAT_H_
+
+
+/***********************************************************
+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
+ */
+
+/* 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) )
+#define MCMagicLen 8
+#define MCMagic "*nazgul*"
+#define MCLastMsg 0
+#define MCLastSet 0
+
+#define MCMajorVer 1L
+#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) */
+ FILE *fp; /* 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 /* !_MSGCAT_H_ */
OpenPOWER on IntegriCloud