gencat.c revision 1.7 1 1.7 lukem /* $NetBSD: gencat.c,v 1.7 1997/10/19 02:12:40 lukem Exp $ */
2 1.5 veego
3 1.5 veego /*
4 1.3 jtc * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 1.3 jtc * All rights reserved.
6 1.3 jtc *
7 1.3 jtc * This code is derived from software contributed to The NetBSD Foundation
8 1.3 jtc * by J.T. Conklin.
9 1.3 jtc *
10 1.3 jtc * Redistribution and use in source and binary forms, with or without
11 1.3 jtc * modification, are permitted provided that the following conditions
12 1.3 jtc * are met:
13 1.3 jtc * 1. Redistributions of source code must retain the above copyright
14 1.3 jtc * notice, this list of conditions and the following disclaimer.
15 1.3 jtc * 2. Redistributions in binary form must reproduce the above copyright
16 1.3 jtc * notice, this list of conditions and the following disclaimer in the
17 1.3 jtc * documentation and/or other materials provided with the distribution.
18 1.3 jtc * 3. All advertising materials mentioning features or use of this software
19 1.3 jtc * must display the following acknowledgement:
20 1.3 jtc * This product includes software developed by the NetBSD
21 1.3 jtc * Foundation, Inc. and its contributors.
22 1.3 jtc * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3 jtc * contributors may be used to endorse or promote products derived
24 1.3 jtc * from this software without specific prior written permission.
25 1.3 jtc *
26 1.3 jtc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3 jtc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3 jtc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.6 jtc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.6 jtc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3 jtc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3 jtc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3 jtc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3 jtc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3 jtc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.3 jtc * POSSIBILITY OF SUCH DAMAGE.
37 1.3 jtc */
38 1.1 jtc
39 1.7 lukem #include <sys/cdefs.h>
40 1.7 lukem #ifndef lint
41 1.7 lukem __RCSID("$NetBSD: gencat.c,v 1.7 1997/10/19 02:12:40 lukem Exp $");
42 1.7 lukem #endif
43 1.7 lukem
44 1.1 jtc /***********************************************************
45 1.1 jtc Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts.
46 1.1 jtc
47 1.1 jtc All Rights Reserved
48 1.1 jtc
49 1.1 jtc Permission to use, copy, modify, and distribute this software and its
50 1.1 jtc documentation for any purpose and without fee is hereby granted,
51 1.1 jtc provided that the above copyright notice appear in all copies and that
52 1.1 jtc both that copyright notice and this permission notice appear in
53 1.1 jtc supporting documentation, and that Alfalfa's name not be used in
54 1.1 jtc advertising or publicity pertaining to distribution of the software
55 1.1 jtc without specific, written prior permission.
56 1.1 jtc
57 1.1 jtc ALPHALPHA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
58 1.1 jtc ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
59 1.1 jtc ALPHALPHA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
60 1.1 jtc ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
61 1.1 jtc WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
62 1.1 jtc ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
63 1.1 jtc SOFTWARE.
64 1.1 jtc
65 1.1 jtc If you make any modifications, bugfixes or other changes to this software
66 1.1 jtc we'd appreciate it if you could send a copy to us so we can keep things
67 1.1 jtc up-to-date. Many thanks.
68 1.1 jtc Kee Hinckley
69 1.1 jtc Alfalfa Software, Inc.
70 1.1 jtc 267 Allston St., #3
71 1.1 jtc Cambridge, MA 02139 USA
72 1.1 jtc nazgul (at) alfalfa.com
73 1.3 jtc
74 1.1 jtc ******************************************************************/
75 1.1 jtc
76 1.3 jtc #define _NLS_PRIVATE
77 1.1 jtc
78 1.3 jtc #include <sys/queue.h>
79 1.3 jtc #include <ctype.h>
80 1.7 lukem #include <err.h>
81 1.7 lukem #include <fcntl.h>
82 1.7 lukem #include <nl_types.h>
83 1.1 jtc #include <stdio.h>
84 1.3 jtc #include <stdlib.h>
85 1.3 jtc #include <string.h>
86 1.3 jtc #include <unistd.h>
87 1.3 jtc
88 1.3 jtc struct _msgT {
89 1.3 jtc long msgId;
90 1.3 jtc char *str;
91 1.3 jtc LIST_ENTRY(_msgT) entries;
92 1.3 jtc };
93 1.3 jtc
94 1.3 jtc struct _setT {
95 1.3 jtc long setId;
96 1.3 jtc LIST_HEAD(msghead, _msgT) msghead;
97 1.3 jtc LIST_ENTRY(_setT) entries;
98 1.3 jtc };
99 1.1 jtc
100 1.3 jtc LIST_HEAD(sethead, _setT) sethead;
101 1.3 jtc static struct _setT *curSet;
102 1.1 jtc
103 1.3 jtc static char *curline = NULL;
104 1.3 jtc static long lineno = 0;
105 1.1 jtc
106 1.7 lukem extern char *__progname; /* from crt0.o */
107 1.7 lukem
108 1.7 lukem #if 0 /* XXX unused */
109 1.7 lukem static void corrupt __P((void));
110 1.7 lukem #endif
111 1.7 lukem static char *cskip __P((char *));
112 1.7 lukem static void error __P((char *, char *));
113 1.7 lukem static void nomem __P((void));
114 1.7 lukem static char *getline __P((int));
115 1.7 lukem static char *getmsg __P((int, char *, char));
116 1.7 lukem static void warning __P((char *, char *));
117 1.7 lukem static char *wskip __P((char *));
118 1.7 lukem static char *xstrdup __P((const char *));
119 1.7 lukem static void *xmalloc __P((size_t));
120 1.7 lukem static void *xrealloc __P((void *, size_t));
121 1.7 lukem
122 1.7 lukem void MCParse __P((int fd));
123 1.7 lukem void MCReadCat __P((int fd));
124 1.7 lukem void MCWriteCat __P((int fd));
125 1.7 lukem void MCDelMsg __P((int msgId));
126 1.7 lukem void MCAddMsg __P((int msgId, const char *msg));
127 1.7 lukem void MCAddSet __P((int setId));
128 1.7 lukem void MCDelSet __P((int setId));
129 1.7 lukem int main __P((int, char **));
130 1.7 lukem void usage __P((void));
131 1.7 lukem
132 1.7 lukem
133 1.3 jtc void
134 1.3 jtc usage()
135 1.3 jtc {
136 1.7 lukem fprintf(stderr, "Usage: %s catfile msgfile ...\n", __progname);
137 1.3 jtc exit(1);
138 1.3 jtc }
139 1.1 jtc
140 1.3 jtc int
141 1.3 jtc main(argc, argv)
142 1.3 jtc int argc;
143 1.3 jtc char *argv[];
144 1.3 jtc {
145 1.3 jtc int ofd, ifd;
146 1.3 jtc char *catfile = NULL;
147 1.3 jtc int c;
148 1.3 jtc
149 1.3 jtc while ((c = getopt(argc, argv, "")) != -1) {
150 1.3 jtc switch (c) {
151 1.3 jtc case '?':
152 1.3 jtc default:
153 1.3 jtc usage();
154 1.3 jtc /* NOTREACHED */
155 1.3 jtc }
156 1.3 jtc }
157 1.3 jtc argc -= optind;
158 1.3 jtc argv += optind;
159 1.1 jtc
160 1.3 jtc if (argc < 2) {
161 1.1 jtc usage();
162 1.3 jtc /* NOTREACHED */
163 1.3 jtc }
164 1.3 jtc catfile = *argv++;
165 1.3 jtc
166 1.3 jtc for (; *argv; argv++) {
167 1.7 lukem if ((ifd = open(*argv, O_RDONLY)) < 0)
168 1.7 lukem err(1, "Unable to read %s", *argv);
169 1.1 jtc MCParse(ifd);
170 1.1 jtc close(ifd);
171 1.1 jtc }
172 1.3 jtc
173 1.7 lukem if ((ofd = open(catfile, O_WRONLY | O_TRUNC | O_CREAT, 0666)) < 0)
174 1.7 lukem err(1, "Unable to create a new %s", catfile);
175 1.1 jtc MCWriteCat(ofd);
176 1.1 jtc exit(0);
177 1.3 jtc }
178 1.3 jtc
179 1.3 jtc static void
180 1.3 jtc warning(cptr, msg)
181 1.3 jtc char *cptr;
182 1.3 jtc char *msg;
183 1.3 jtc {
184 1.7 lukem fprintf(stderr, "%s: %s on line %ld\n", __progname, msg, lineno);
185 1.3 jtc fprintf(stderr, "%s\n", curline);
186 1.3 jtc if (cptr) {
187 1.3 jtc char *tptr;
188 1.3 jtc for (tptr = curline; tptr < cptr; ++tptr)
189 1.3 jtc putc(' ', stderr);
190 1.3 jtc fprintf(stderr, "^\n");
191 1.3 jtc }
192 1.3 jtc }
193 1.3 jtc
194 1.3 jtc static void
195 1.3 jtc error(cptr, msg)
196 1.3 jtc char *cptr;
197 1.3 jtc char *msg;
198 1.3 jtc {
199 1.3 jtc warning(cptr, msg);
200 1.1 jtc exit(1);
201 1.1 jtc }
202 1.1 jtc
203 1.7 lukem #if 0 /* XXX unused */
204 1.3 jtc static void
205 1.3 jtc corrupt()
206 1.3 jtc {
207 1.3 jtc error(NULL, "corrupt message catalog");
208 1.3 jtc }
209 1.7 lukem #endif
210 1.3 jtc
211 1.3 jtc static void
212 1.3 jtc nomem()
213 1.3 jtc {
214 1.3 jtc error(NULL, "out of memory");
215 1.3 jtc }
216 1.3 jtc
217 1.3 jtc static void *
218 1.3 jtc xmalloc(len)
219 1.3 jtc size_t len;
220 1.3 jtc {
221 1.3 jtc void *p;
222 1.3 jtc
223 1.3 jtc if ((p = malloc(len)) == NULL)
224 1.3 jtc nomem();
225 1.3 jtc return (p);
226 1.3 jtc }
227 1.3 jtc
228 1.3 jtc static void *
229 1.3 jtc xrealloc(ptr, size)
230 1.3 jtc void *ptr;
231 1.3 jtc size_t size;
232 1.3 jtc {
233 1.3 jtc if ((ptr = realloc(ptr, size)) == NULL)
234 1.3 jtc nomem();
235 1.3 jtc return (ptr);
236 1.3 jtc }
237 1.3 jtc
238 1.3 jtc static char *
239 1.3 jtc xstrdup(str)
240 1.7 lukem const char *str;
241 1.3 jtc {
242 1.7 lukem char *nstr;
243 1.7 lukem
244 1.7 lukem if ((nstr = strdup(str)) == NULL)
245 1.3 jtc nomem();
246 1.7 lukem return (nstr);
247 1.3 jtc }
248 1.3 jtc
249 1.3 jtc static char *
250 1.3 jtc getline(fd)
251 1.3 jtc int fd;
252 1.3 jtc {
253 1.3 jtc static long curlen = BUFSIZ;
254 1.3 jtc static char buf[BUFSIZ], *bptr = buf, *bend = buf;
255 1.3 jtc char *cptr, *cend;
256 1.3 jtc long buflen;
257 1.3 jtc
258 1.3 jtc if (!curline) {
259 1.3 jtc curline = xmalloc(curlen);
260 1.3 jtc }
261 1.3 jtc ++lineno;
262 1.3 jtc
263 1.3 jtc cptr = curline;
264 1.3 jtc cend = curline + curlen;
265 1.3 jtc for (;;) {
266 1.3 jtc for (; bptr < bend && cptr < cend; ++cptr, ++bptr) {
267 1.3 jtc if (*bptr == '\n') {
268 1.3 jtc *cptr = '\0';
269 1.3 jtc ++bptr;
270 1.3 jtc return (curline);
271 1.3 jtc } else
272 1.3 jtc *cptr = *bptr;
273 1.3 jtc }
274 1.3 jtc if (bptr == bend) {
275 1.3 jtc buflen = read(fd, buf, BUFSIZ);
276 1.3 jtc if (buflen <= 0) {
277 1.3 jtc if (cptr > curline) {
278 1.3 jtc *cptr = '\0';
279 1.3 jtc return (curline);
280 1.3 jtc }
281 1.3 jtc return (NULL);
282 1.3 jtc }
283 1.3 jtc bend = buf + buflen;
284 1.3 jtc bptr = buf;
285 1.3 jtc }
286 1.3 jtc if (cptr == cend) {
287 1.3 jtc cptr = curline = xrealloc(curline, curlen *= 2);
288 1.3 jtc cend = curline + curlen;
289 1.3 jtc }
290 1.3 jtc }
291 1.3 jtc }
292 1.3 jtc
293 1.3 jtc static char *
294 1.3 jtc wskip(cptr)
295 1.3 jtc char *cptr;
296 1.3 jtc {
297 1.3 jtc if (!*cptr || !isspace(*cptr)) {
298 1.3 jtc warning(cptr, "expected a space");
299 1.3 jtc return (cptr);
300 1.3 jtc }
301 1.3 jtc while (*cptr && isspace(*cptr))
302 1.3 jtc ++cptr;
303 1.3 jtc return (cptr);
304 1.3 jtc }
305 1.3 jtc
306 1.3 jtc static char *
307 1.3 jtc cskip(cptr)
308 1.3 jtc char *cptr;
309 1.3 jtc {
310 1.3 jtc if (!*cptr || isspace(*cptr)) {
311 1.3 jtc warning(cptr, "wasn't expecting a space");
312 1.3 jtc return (cptr);
313 1.3 jtc }
314 1.3 jtc while (*cptr && !isspace(*cptr))
315 1.3 jtc ++cptr;
316 1.3 jtc return (cptr);
317 1.3 jtc }
318 1.3 jtc
319 1.3 jtc static char *
320 1.3 jtc getmsg(fd, cptr, quote)
321 1.3 jtc int fd;
322 1.3 jtc char *cptr;
323 1.3 jtc char quote;
324 1.3 jtc {
325 1.3 jtc static char *msg = NULL;
326 1.3 jtc static long msglen = 0;
327 1.3 jtc long clen, i;
328 1.3 jtc char *tptr;
329 1.3 jtc
330 1.3 jtc if (quote && *cptr == quote) {
331 1.3 jtc ++cptr;
332 1.3 jtc }
333 1.3 jtc
334 1.3 jtc clen = strlen(cptr) + 1;
335 1.3 jtc if (clen > msglen) {
336 1.3 jtc if (msglen)
337 1.3 jtc msg = xrealloc(msg, clen);
338 1.3 jtc else
339 1.3 jtc msg = xmalloc(clen);
340 1.3 jtc msglen = clen;
341 1.3 jtc }
342 1.3 jtc tptr = msg;
343 1.3 jtc
344 1.3 jtc while (*cptr) {
345 1.3 jtc if (quote && *cptr == quote) {
346 1.3 jtc char *tmp;
347 1.3 jtc tmp = cptr + 1;
348 1.3 jtc if (*tmp && (!isspace(*tmp) || *wskip(tmp))) {
349 1.3 jtc warning(cptr, "unexpected quote character, ignoreing");
350 1.3 jtc *tptr++ = *cptr++;
351 1.3 jtc } else {
352 1.3 jtc *cptr = '\0';
353 1.3 jtc }
354 1.3 jtc } else
355 1.3 jtc if (*cptr == '\\') {
356 1.3 jtc ++cptr;
357 1.3 jtc switch (*cptr) {
358 1.3 jtc case '\0':
359 1.3 jtc cptr = getline(fd);
360 1.3 jtc if (!cptr)
361 1.3 jtc error(NULL, "premature end of file");
362 1.3 jtc msglen += strlen(cptr);
363 1.3 jtc i = tptr - msg;
364 1.3 jtc msg = xrealloc(msg, msglen);
365 1.3 jtc tptr = msg + i;
366 1.3 jtc break;
367 1.3 jtc case 'n':
368 1.3 jtc *tptr++ = '\n';
369 1.3 jtc ++cptr;
370 1.3 jtc break;
371 1.3 jtc case 't':
372 1.3 jtc *tptr++ = '\t';
373 1.3 jtc ++cptr;
374 1.3 jtc break;
375 1.3 jtc case 'v':
376 1.3 jtc *tptr++ = '\v';
377 1.3 jtc ++cptr;
378 1.3 jtc break;
379 1.3 jtc case 'b':
380 1.3 jtc *tptr++ = '\b';
381 1.3 jtc ++cptr;
382 1.3 jtc break;
383 1.3 jtc case 'r':
384 1.3 jtc *tptr++ = '\r';
385 1.3 jtc ++cptr;
386 1.3 jtc break;
387 1.3 jtc case 'f':
388 1.3 jtc *tptr++ = '\f';
389 1.3 jtc ++cptr;
390 1.3 jtc break;
391 1.3 jtc case '\\':
392 1.3 jtc *tptr++ = '\\';
393 1.3 jtc ++cptr;
394 1.3 jtc break;
395 1.3 jtc default:
396 1.3 jtc if (isdigit(*cptr)) {
397 1.3 jtc *tptr = 0;
398 1.3 jtc for (i = 0; i < 3; ++i) {
399 1.3 jtc if (!isdigit(*cptr))
400 1.3 jtc break;
401 1.3 jtc if (*cptr > '7')
402 1.3 jtc warning(cptr, "octal number greater than 7?!");
403 1.3 jtc *tptr *= 8;
404 1.3 jtc *tptr += (*cptr - '0');
405 1.3 jtc ++cptr;
406 1.3 jtc }
407 1.3 jtc } else {
408 1.3 jtc warning(cptr, "unrecognized escape sequence");
409 1.3 jtc }
410 1.3 jtc }
411 1.3 jtc } else {
412 1.3 jtc *tptr++ = *cptr++;
413 1.3 jtc }
414 1.3 jtc }
415 1.3 jtc *tptr = '\0';
416 1.3 jtc return (msg);
417 1.3 jtc }
418 1.3 jtc
419 1.3 jtc void
420 1.3 jtc MCParse(fd)
421 1.3 jtc int fd;
422 1.3 jtc {
423 1.3 jtc char *cptr, *str;
424 1.3 jtc int setid, msgid = 0;
425 1.3 jtc char quote = 0;
426 1.3 jtc
427 1.3 jtc /* XXX: init sethead? */
428 1.3 jtc
429 1.3 jtc while ((cptr = getline(fd))) {
430 1.3 jtc if (*cptr == '$') {
431 1.3 jtc ++cptr;
432 1.3 jtc if (strncmp(cptr, "set", 3) == 0) {
433 1.3 jtc cptr += 3;
434 1.3 jtc cptr = wskip(cptr);
435 1.3 jtc setid = atoi(cptr);
436 1.3 jtc MCAddSet(setid);
437 1.3 jtc msgid = 0;
438 1.3 jtc } else if (strncmp(cptr, "delset", 6) == 0) {
439 1.3 jtc cptr += 6;
440 1.3 jtc cptr = wskip(cptr);
441 1.3 jtc setid = atoi(cptr);
442 1.3 jtc MCDelSet(setid);
443 1.3 jtc } else if (strncmp(cptr, "quote", 5) == 0) {
444 1.3 jtc cptr += 5;
445 1.3 jtc if (!*cptr)
446 1.3 jtc quote = 0;
447 1.3 jtc else {
448 1.3 jtc cptr = wskip(cptr);
449 1.3 jtc if (!*cptr)
450 1.3 jtc quote = 0;
451 1.3 jtc else
452 1.3 jtc quote = *cptr;
453 1.3 jtc }
454 1.3 jtc } else if (isspace(*cptr)) {
455 1.3 jtc ;
456 1.3 jtc } else {
457 1.3 jtc if (*cptr) {
458 1.3 jtc cptr = wskip(cptr);
459 1.3 jtc if (*cptr)
460 1.3 jtc warning(cptr, "unrecognized line");
461 1.3 jtc }
462 1.3 jtc }
463 1.3 jtc } else {
464 1.3 jtc if (isdigit(*cptr)) {
465 1.3 jtc msgid = atoi(cptr);
466 1.3 jtc cptr = cskip(cptr);
467 1.3 jtc cptr = wskip(cptr);
468 1.3 jtc /* if (*cptr) ++cptr; */
469 1.3 jtc }
470 1.3 jtc if (!*cptr)
471 1.3 jtc MCDelMsg(msgid);
472 1.3 jtc else {
473 1.3 jtc str = getmsg(fd, cptr, quote);
474 1.3 jtc MCAddMsg(msgid, str);
475 1.3 jtc }
476 1.3 jtc }
477 1.3 jtc }
478 1.3 jtc }
479 1.3 jtc
480 1.3 jtc void
481 1.3 jtc MCReadCat(fd)
482 1.3 jtc int fd;
483 1.3 jtc {
484 1.3 jtc #if 0
485 1.3 jtc MCHeaderT mcHead;
486 1.3 jtc MCMsgT mcMsg;
487 1.3 jtc MCSetT mcSet;
488 1.3 jtc msgT *msg;
489 1.3 jtc setT *set;
490 1.3 jtc int i;
491 1.3 jtc char *data;
492 1.3 jtc
493 1.3 jtc /* XXX init sethead? */
494 1.3 jtc
495 1.3 jtc if (read(fd, &mcHead, sizeof(mcHead)) != sizeof(mcHead))
496 1.3 jtc corrupt();
497 1.3 jtc if (strncmp(mcHead.magic, MCMagic, MCMagicLen) != 0)
498 1.3 jtc corrupt();
499 1.3 jtc if (mcHead.majorVer != MCMajorVer)
500 1.3 jtc error(NULL, "unrecognized catalog version");
501 1.3 jtc if ((mcHead.flags & MCGetByteOrder()) == 0)
502 1.3 jtc error(NULL, "wrong byte order");
503 1.3 jtc
504 1.3 jtc if (lseek(fd, mcHead.firstSet, SEEK_SET) == -1)
505 1.3 jtc corrupt();
506 1.3 jtc
507 1.3 jtc for (;;) {
508 1.3 jtc if (read(fd, &mcSet, sizeof(mcSet)) != sizeof(mcSet))
509 1.3 jtc corrupt();
510 1.3 jtc if (mcSet.invalid)
511 1.3 jtc continue;
512 1.3 jtc
513 1.3 jtc set = xmalloc(sizeof(setT));
514 1.3 jtc memset(set, '\0', sizeof(*set));
515 1.3 jtc if (cat->first) {
516 1.3 jtc cat->last->next = set;
517 1.3 jtc set->prev = cat->last;
518 1.3 jtc cat->last = set;
519 1.3 jtc } else
520 1.3 jtc cat->first = cat->last = set;
521 1.3 jtc
522 1.3 jtc set->setId = mcSet.setId;
523 1.3 jtc
524 1.3 jtc /* Get the data */
525 1.3 jtc if (mcSet.dataLen) {
526 1.3 jtc data = xmalloc(mcSet.dataLen);
527 1.3 jtc if (lseek(fd, mcSet.data.off, SEEK_SET) == -1)
528 1.3 jtc corrupt();
529 1.3 jtc if (read(fd, data, mcSet.dataLen) != mcSet.dataLen)
530 1.3 jtc corrupt();
531 1.3 jtc if (lseek(fd, mcSet.u.firstMsg, SEEK_SET) == -1)
532 1.3 jtc corrupt();
533 1.3 jtc
534 1.3 jtc for (i = 0; i < mcSet.numMsgs; ++i) {
535 1.3 jtc if (read(fd, &mcMsg, sizeof(mcMsg)) != sizeof(mcMsg))
536 1.3 jtc corrupt();
537 1.3 jtc if (mcMsg.invalid) {
538 1.3 jtc --i;
539 1.3 jtc continue;
540 1.3 jtc }
541 1.3 jtc msg = xmalloc(sizeof(msgT));
542 1.3 jtc memset(msg, '\0', sizeof(*msg));
543 1.3 jtc if (set->first) {
544 1.3 jtc set->last->next = msg;
545 1.3 jtc msg->prev = set->last;
546 1.3 jtc set->last = msg;
547 1.3 jtc } else
548 1.3 jtc set->first = set->last = msg;
549 1.3 jtc
550 1.3 jtc msg->msgId = mcMsg.msgId;
551 1.3 jtc msg->str = xstrdup((char *) (data + mcMsg.msg.off));
552 1.3 jtc }
553 1.3 jtc free(data);
554 1.3 jtc }
555 1.3 jtc if (!mcSet.nextSet)
556 1.3 jtc break;
557 1.3 jtc if (lseek(fd, mcSet.nextSet, SEEK_SET) == -1)
558 1.3 jtc corrupt();
559 1.3 jtc }
560 1.1 jtc #endif
561 1.3 jtc }
562 1.3 jtc
563 1.3 jtc /*
564 1.3 jtc * Write message catalog.
565 1.3 jtc *
566 1.3 jtc * The message catalog is first converted from its internal to its
567 1.3 jtc * external representation in a chunk of memory allocated for this
568 1.3 jtc * purpose. Then the completed catalog is written. This approach
569 1.3 jtc * avoids additional housekeeping variables and/or a lot of seeks
570 1.3 jtc * that would otherwise be required.
571 1.3 jtc */
572 1.3 jtc void
573 1.3 jtc MCWriteCat(fd)
574 1.3 jtc int fd;
575 1.1 jtc {
576 1.3 jtc int nsets; /* number of sets */
577 1.3 jtc int nmsgs; /* number of msgs */
578 1.3 jtc int string_size; /* total size of string pool */
579 1.3 jtc int msgcat_size; /* total size of message catalog */
580 1.3 jtc void *msgcat; /* message catalog data */
581 1.3 jtc struct _nls_cat_hdr *cat_hdr;
582 1.3 jtc struct _nls_set_hdr *set_hdr;
583 1.3 jtc struct _nls_msg_hdr *msg_hdr;
584 1.3 jtc char *strings;
585 1.3 jtc struct _setT *set;
586 1.3 jtc struct _msgT *msg;
587 1.3 jtc int msg_index;
588 1.3 jtc int msg_offset;
589 1.3 jtc
590 1.3 jtc /* determine number of sets, number of messages, and size of the
591 1.3 jtc * string pool */
592 1.3 jtc nsets = 0;
593 1.3 jtc nmsgs = 0;
594 1.3 jtc string_size = 0;
595 1.3 jtc
596 1.3 jtc for (set = sethead.lh_first; set != NULL;
597 1.3 jtc set = set->entries.le_next) {
598 1.3 jtc nsets++;
599 1.3 jtc
600 1.3 jtc for (msg = set->msghead.lh_first; msg != NULL;
601 1.3 jtc msg = msg->entries.le_next) {
602 1.3 jtc nmsgs++;
603 1.3 jtc string_size += strlen(msg->str) + 1;
604 1.3 jtc }
605 1.3 jtc }
606 1.3 jtc
607 1.3 jtc #ifdef DEBUG
608 1.3 jtc printf("number of sets: %d\n", nsets);
609 1.3 jtc printf("number of msgs: %d\n", nmsgs);
610 1.3 jtc printf("string pool size: %d\n", string_size);
611 1.3 jtc #endif
612 1.3 jtc
613 1.3 jtc /* determine size and then allocate buffer for constructing external
614 1.3 jtc * message catalog representation */
615 1.3 jtc msgcat_size = sizeof(struct _nls_cat_hdr)
616 1.3 jtc + (nsets * sizeof(struct _nls_set_hdr))
617 1.3 jtc + (nmsgs * sizeof(struct _nls_msg_hdr))
618 1.3 jtc + string_size;
619 1.3 jtc
620 1.3 jtc msgcat = xmalloc(msgcat_size);
621 1.3 jtc memset(msgcat, '\0', msgcat_size);
622 1.3 jtc
623 1.3 jtc /* fill in msg catalog header */
624 1.3 jtc cat_hdr = (struct _nls_cat_hdr *) msgcat;
625 1.3 jtc cat_hdr->__magic = htonl(_NLS_MAGIC);
626 1.3 jtc cat_hdr->__nsets = htonl(nsets);
627 1.3 jtc cat_hdr->__mem = htonl(msgcat_size - sizeof(struct _nls_cat_hdr));
628 1.3 jtc cat_hdr->__msg_hdr_offset =
629 1.3 jtc htonl(nsets * sizeof(struct _nls_set_hdr));
630 1.3 jtc cat_hdr->__msg_txt_offset =
631 1.3 jtc htonl(nsets * sizeof(struct _nls_set_hdr) +
632 1.3 jtc nmsgs * sizeof(struct _nls_msg_hdr));
633 1.3 jtc
634 1.3 jtc /* compute offsets for set & msg header tables and string pool */
635 1.3 jtc set_hdr = (struct _nls_set_hdr *) ((char *) msgcat +
636 1.3 jtc sizeof(struct _nls_cat_hdr));
637 1.3 jtc msg_hdr = (struct _nls_msg_hdr *) ((char *) msgcat +
638 1.3 jtc sizeof(struct _nls_cat_hdr) +
639 1.3 jtc nsets * sizeof(struct _nls_set_hdr));
640 1.3 jtc strings = (char *) msgcat +
641 1.3 jtc sizeof(struct _nls_cat_hdr) +
642 1.3 jtc nsets * sizeof(struct _nls_set_hdr) +
643 1.3 jtc nmsgs * sizeof(struct _nls_msg_hdr);
644 1.3 jtc
645 1.3 jtc msg_index = 0;
646 1.3 jtc msg_offset = 0;
647 1.3 jtc for (set = sethead.lh_first; set != NULL;
648 1.3 jtc set = set->entries.le_next) {
649 1.3 jtc
650 1.3 jtc nmsgs = 0;
651 1.3 jtc for (msg = set->msghead.lh_first; msg != NULL;
652 1.3 jtc msg = msg->entries.le_next) {
653 1.3 jtc int msg_len = strlen(msg->str) + 1;
654 1.3 jtc
655 1.3 jtc msg_hdr->__msgno = htonl(msg->msgId);
656 1.3 jtc msg_hdr->__msglen = htonl(msg_len);
657 1.3 jtc msg_hdr->__offset = htonl(msg_offset);
658 1.3 jtc
659 1.3 jtc memcpy(strings, msg->str, msg_len);
660 1.3 jtc strings += msg_len;
661 1.3 jtc msg_offset += msg_len;
662 1.3 jtc
663 1.3 jtc nmsgs++;
664 1.3 jtc msg_hdr++;
665 1.3 jtc }
666 1.3 jtc
667 1.3 jtc set_hdr->__setno = htonl(set->setId);
668 1.3 jtc set_hdr->__nmsgs = htonl(nmsgs);
669 1.3 jtc set_hdr->__index = htonl(msg_index);
670 1.3 jtc msg_index += nmsgs;
671 1.3 jtc set_hdr++;
672 1.3 jtc }
673 1.3 jtc
674 1.3 jtc /* write out catalog. XXX: should this be done in small chunks? */
675 1.3 jtc write(fd, msgcat, msgcat_size);
676 1.3 jtc }
677 1.3 jtc
678 1.3 jtc void
679 1.3 jtc MCAddSet(setId)
680 1.3 jtc int setId;
681 1.3 jtc {
682 1.3 jtc struct _setT *p, *q;
683 1.3 jtc
684 1.3 jtc if (setId <= 0) {
685 1.3 jtc error(NULL, "setId's must be greater than zero");
686 1.3 jtc /* NOTREACHED */
687 1.3 jtc }
688 1.3 jtc #if 0
689 1.3 jtc /* XXX */
690 1.3 jtc if (setId > NL_SETMAX) {
691 1.3 jtc error(NULL, "setId %d exceeds limit (%d)");
692 1.3 jtc /* NOTREACHED */
693 1.3 jtc }
694 1.3 jtc #endif
695 1.3 jtc
696 1.3 jtc p = sethead.lh_first;
697 1.3 jtc q = NULL;
698 1.3 jtc for (; p != NULL && p->setId < setId; q = p, p = p->entries.le_next);
699 1.3 jtc
700 1.3 jtc if (p && p->setId == setId) {
701 1.3 jtc ;
702 1.3 jtc } else {
703 1.3 jtc p = xmalloc(sizeof(struct _setT));
704 1.3 jtc memset(p, '\0', sizeof(struct _setT));
705 1.3 jtc LIST_INIT(&p->msghead);
706 1.3 jtc
707 1.3 jtc p->setId = setId;
708 1.3 jtc
709 1.3 jtc if (q == NULL) {
710 1.3 jtc LIST_INSERT_HEAD(&sethead, p, entries);
711 1.3 jtc } else {
712 1.3 jtc LIST_INSERT_AFTER(q, p, entries);
713 1.3 jtc }
714 1.3 jtc }
715 1.3 jtc
716 1.3 jtc curSet = p;
717 1.3 jtc }
718 1.3 jtc
719 1.3 jtc void
720 1.3 jtc MCAddMsg(msgId, str)
721 1.3 jtc int msgId;
722 1.3 jtc const char *str;
723 1.3 jtc {
724 1.3 jtc struct _msgT *p, *q;
725 1.3 jtc
726 1.3 jtc if (!curSet)
727 1.3 jtc error(NULL, "can't specify a message when no set exists");
728 1.3 jtc
729 1.3 jtc if (msgId <= 0) {
730 1.3 jtc error(NULL, "msgId's must be greater than zero");
731 1.3 jtc /* NOTREACHED */
732 1.3 jtc }
733 1.3 jtc #if 0
734 1.3 jtc /* XXX */
735 1.3 jtc if (msgId > NL_SETMAX) {
736 1.3 jtc error(NULL, "msgID %d exceeds limit (%d)");
737 1.3 jtc /* NOTREACHED */
738 1.3 jtc }
739 1.3 jtc #endif
740 1.3 jtc
741 1.3 jtc p = curSet->msghead.lh_first;
742 1.3 jtc q = NULL;
743 1.3 jtc for (; p != NULL && p->msgId < msgId; q = p, p = p->entries.le_next);
744 1.3 jtc
745 1.3 jtc if (p && p->msgId == msgId) {
746 1.3 jtc free(p->str);
747 1.3 jtc } else {
748 1.3 jtc p = xmalloc(sizeof(struct _msgT));
749 1.3 jtc memset(p, '\0', sizeof(struct _msgT));
750 1.3 jtc
751 1.3 jtc if (q == NULL) {
752 1.3 jtc LIST_INSERT_HEAD(&curSet->msghead, p, entries);
753 1.3 jtc } else {
754 1.3 jtc LIST_INSERT_AFTER(q, p, entries);
755 1.3 jtc }
756 1.3 jtc }
757 1.3 jtc
758 1.3 jtc p->msgId = msgId;
759 1.3 jtc p->str = xstrdup(str);
760 1.3 jtc }
761 1.3 jtc
762 1.3 jtc void
763 1.3 jtc MCDelSet(setId)
764 1.3 jtc int setId;
765 1.3 jtc {
766 1.3 jtc struct _setT *set;
767 1.3 jtc struct _msgT *msg;
768 1.3 jtc
769 1.3 jtc set = sethead.lh_first;
770 1.3 jtc for (; set != NULL && set->setId < setId; set = set->entries.le_next);
771 1.3 jtc
772 1.3 jtc if (set && set->setId == setId) {
773 1.3 jtc
774 1.3 jtc msg = set->msghead.lh_first;
775 1.3 jtc while (msg) {
776 1.3 jtc free(msg->str);
777 1.4 veego LIST_REMOVE(msg, entries);
778 1.3 jtc }
779 1.3 jtc
780 1.3 jtc LIST_REMOVE(set, entries);
781 1.3 jtc return;
782 1.3 jtc }
783 1.3 jtc warning(NULL, "specified set doesn't exist");
784 1.3 jtc }
785 1.1 jtc
786 1.3 jtc void
787 1.3 jtc MCDelMsg(msgId)
788 1.3 jtc int msgId;
789 1.3 jtc {
790 1.3 jtc struct _msgT *msg;
791 1.1 jtc
792 1.3 jtc if (!curSet)
793 1.3 jtc error(NULL, "you can't delete a message before defining the set");
794 1.1 jtc
795 1.3 jtc msg = curSet->msghead.lh_first;
796 1.3 jtc for (; msg != NULL && msg->msgId < msgId; msg = msg->entries.le_next);
797 1.1 jtc
798 1.3 jtc if (msg && msg->msgId == msgId) {
799 1.3 jtc free(msg->str);
800 1.3 jtc LIST_REMOVE(msg, entries);
801 1.3 jtc return;
802 1.3 jtc }
803 1.3 jtc warning(NULL, "specified msg doesn't exist");
804 1.1 jtc }
805