]>
git.saurik.com Git - apple/libc.git/blob - nls/FreeBSD/msgcat.c
fd9e820fd299d6dc47c6cc0873cf537e38c8415d
1 /***********************************************************
2 Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts.
6 Permission to use, copy, modify, and distribute this software and its
7 documentation for any purpose and without fee is hereby granted,
8 provided that the above copyright notice appear in all copies and that
9 both that copyright notice and this permission notice appear in
10 supporting documentation, and that Alfalfa's name not be used in
11 advertising or publicity pertaining to distribution of the software
12 without specific, written prior permission.
14 ALPHALPHA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
15 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
16 ALPHALPHA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
17 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
19 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
22 If you make any modifications, bugfixes or other changes to this software
23 we'd appreciate it if you could send a copy to us so we can keep things
24 up-to-date. Many thanks.
26 Alfalfa Software, Inc.
28 Cambridge, MA 02139 USA
31 ******************************************************************/
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: src/lib/libc/nls/msgcat.c,v 1.45 2002/10/27 17:44:33 wollman Exp $");
37 * We need a better way of handling errors than printing text. I need
38 * to add an error handling routine.
41 #include "namespace.h"
42 #include <sys/types.h>
54 #include "un-namespace.h"
57 #include "../locale/setlocale.h" /* for ENCODING_LEN */
59 #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"
64 #define NLERR ((nl_catd) -1)
65 #define NLRETERR(errc) { errno = errc; return (NLERR); }
67 static nl_catd
loadCat();
69 static void __nls_free_resources();
78 char *nlspath
, *lang
, *base
, *cptr
, *pathP
, *tmpptr
;
79 char *cptr1
, *plang
, *pter
, *pcode
;
82 if (name
== NULL
|| *name
== '\0')
85 /* is it absolute path ? if yes, load immediately */
86 if (strchr(name
, '/') != NULL
)
87 return (loadCat(name
));
89 if (type
== NL_CAT_LOCALE
)
90 lang
= setlocale(LC_MESSAGES
, NULL
);
92 lang
= getenv("LANG");
94 if (lang
== NULL
|| *lang
== '\0' || strlen(lang
) > ENCODING_LEN
||
96 (lang
[1] == '\0' || (lang
[1] == '.' && lang
[2] == '\0'))) ||
97 strchr(lang
, '/') != NULL
)
100 if ((plang
= cptr1
= strdup(lang
)) == NULL
)
102 if ((cptr
= strchr(cptr1
, '@')) != NULL
)
105 if ((cptr
= strchr(cptr1
, '_')) != NULL
) {
109 if ((cptr
= strchr(cptr1
, '.')) != NULL
) {
114 if ((nlspath
= getenv("NLSPATH")) == NULL
115 #ifndef __NETBSD_SYSCALLS
119 nlspath
= _DEFAULT_NLS_PATH
;
121 if ((base
= cptr
= strdup(nlspath
)) == NULL
) {
128 while ((nlspath
= strsep(&cptr
, ":")) != NULL
) {
131 for (; *nlspath
; ++nlspath
) {
132 if (*nlspath
== '%') {
133 switch (*(nlspath
+ 1)) {
147 tmpptr
= (char *)name
;
156 *(pathP
++) = *nlspath
;
161 spcleft
= sizeof(path
) -
163 if (strlcpy(pathP
, tmpptr
, spcleft
) >=
168 NLRETERR(ENAMETOOLONG
);
170 pathP
+= strlen(tmpptr
);
172 if (pathP
- path
>= sizeof(path
) - 1)
174 *(pathP
++) = *nlspath
;
178 if (stat(path
, &sbuf
) == 0) {
181 return (loadCat(path
));
184 tmpptr
= (char *)name
;
195 * We've got an odd situation here. The odds are real good that the
196 * number we are looking for is almost the same as the index. We could
197 * use the index, check the difference and do something intelligent, but
198 * I haven't quite figured out what's intelligent.
201 * Take an id N. If there are > N items in the list, then N cannot
202 * be more than N items from the start, since otherwise there would
203 * have to be duplicate items. So we can safely set the top to N+1
204 * (after taking into account that ids start at 1, and arrays at 0)
206 * Let's say we are at position P, and we are looking for N, but have
207 * V. If N > V, then the furthest away that N could be is
208 * P + (N-V). So we can safely set hi to P+(N-V)+1. For example:
209 * We are looking for 10, but have 8
215 #define LOOKUP(PARENT, CHILD, ID, NUM, SET) { \
217 if (ID - 1 < PARENT->NUM) { \
222 cur = (hi - lo) / 2; \
225 CHILD = PARENT->SET + cur; \
226 if (CHILD->ID == ID) \
228 if (CHILD->ID < ID) { \
230 if (hi > cur + (ID - CHILD->ID) + 1) \
231 hi = cur + (ID - CHILD->ID) + 1; \
242 cur += ((hi - lo) / 2) * dir; \
252 long lo
, hi
, cur
, dir
;
254 if (cat
== NULL
|| setId
<= 0)
256 LOOKUP(cat
, set
, setId
, numSets
, sets
);
257 if (set
->invalid
&& loadSet(cat
, set
) <= 0)
268 long lo
, hi
, cur
, dir
;
270 if (set
== NULL
|| set
->invalid
|| msgId
<= 0)
272 LOOKUP(set
, msg
, msgId
, numMsgs
, u
.msgs
);
277 catgets(catd
, setId
, msgId
, dflt
)
284 MCCatT
*cat
= (MCCatT
*)catd
;
287 if (catd
== NULL
|| catd
== NLERR
)
288 return ((char *)dflt
);
289 msg
= MCGetMsg(MCGetSet(cat
, setId
), msgId
);
294 return ((char *)cptr
);
301 MCCatT
*cat
= (MCCatT
*)catd
;
303 if (catd
== NULL
|| catd
== NLERR
) {
308 if (cat
->loadType
!= MCLoadAll
)
310 (void)fclose(cat
->fp
);
311 __nls_free_resources(cat
, cat
->numSets
);
320 /* Note that only malloc failures are allowed to return an error */
321 static char *_errowner
= "Message Catalog System";
323 #define CORRUPT() { \
324 (void)fclose(cat->fp); \
325 (void)fprintf(stderr, "%s: corrupt file.", _errowner); \
330 #define NOSPACE() { \
332 (void)fclose(cat->fp); \
333 (void)fprintf(stderr, "%s: no more memory.", _errowner); \
340 __nls_free_resources(cat
, i
)
347 for (j
= 0; j
< i
; j
++) {
359 __const
char *catpath
;
368 if ((cat
= (MCCatT
*)malloc(sizeof(MCCatT
))) == NULL
)
370 cat
->loadType
= MCLoadBySet
;
372 if ((cat
->fp
= fopen(catpath
, "r")) == NULL
) {
378 (void)_fcntl(fileno(cat
->fp
), F_SETFD
, FD_CLOEXEC
);
380 if (fread(&header
, sizeof(header
), 1, cat
->fp
) != 1 ||
381 strncmp(header
.magic
, MCMagic
, MCMagicLen
) != 0)
384 if (header
.majorVer
!= MCMajorVer
) {
385 (void)fclose(cat
->fp
);
387 (void)fprintf(stderr
, "%s: %s is version %ld, we need %ld.\n",
388 _errowner
, catpath
, header
.majorVer
, MCMajorVer
);
391 if (header
.numSets
<= 0) {
392 (void)fclose(cat
->fp
);
394 (void)fprintf(stderr
, "%s: %s has %ld sets!\n",
395 _errowner
, catpath
, header
.numSets
);
399 cat
->numSets
= header
.numSets
;
400 if ((cat
->sets
= (MCSetT
*)malloc(sizeof(MCSetT
) * header
.numSets
)) ==
404 nextSet
= header
.firstSet
;
405 for (i
= 0; i
< cat
->numSets
; ++i
) {
406 if (fseeko(cat
->fp
, nextSet
, SEEK_SET
) == -1) {
407 __nls_free_resources(cat
, i
);
411 /* read in the set header */
413 if (fread(set
, sizeof(*set
), 1, cat
->fp
) != 1) {
414 __nls_free_resources(cat
, i
);
418 /* if it's invalid, skip over it (and backup 'i') */
421 nextSet
= set
->nextSet
;
425 if (cat
->loadType
== MCLoadAll
) {
428 if ((res
= loadSet(cat
, set
)) <= 0) {
430 __nls_free_resources(cat
, i
);
439 nextSet
= set
->nextSet
;
442 if (cat
->loadType
== MCLoadAll
) {
443 (void)fclose(cat
->fp
);
447 return ((nl_catd
) cat
);
460 if (fseeko(cat
->fp
, set
->data
.off
, SEEK_SET
) == -1)
462 if ((set
->data
.str
= malloc(set
->dataLen
)) == NULL
)
464 if (fread(set
->data
.str
, set
->dataLen
, 1, cat
->fp
) != 1) {
471 /* Get the messages */
472 if (fseeko(cat
->fp
, set
->u
.firstMsg
, SEEK_SET
) == -1) {
478 if ((set
->u
.msgs
= (MCMsgT
*)malloc(sizeof(MCMsgT
) * set
->numMsgs
)) ==
486 for (i
= 0; i
< set
->numMsgs
; ++i
) {
487 msg
= set
->u
.msgs
+ i
;
488 if (fread(msg
, sizeof(*msg
), 1, cat
->fp
) != 1) {
499 msg
->msg
.str
= (char *)(set
->data
.str
+ msg
->msg
.off
);
501 set
->invalid
= FALSE
;