aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/nls/msgcat.c
blob: b4a465290c1230c3d6312288818ee9985b290ece (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*	$Id: msgcat.c,v 1.1 1995/03/30 12:47:26 jkh 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);
}