Home | History | Annotate | Line # | Download | only in gencat
gencat.c revision 1.22
      1  1.22  ginsbach /*	$NetBSD: gencat.c,v 1.22 2007/10/10 02:34:18 ginsbach 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.14        tv #if defined(__RCSID) && !defined(lint)
     41  1.22  ginsbach __RCSID("$NetBSD: gencat.c,v 1.22 2007/10/10 02:34:18 ginsbach 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.18     lukem #if HAVE_NBTOOL_CONFIG_H
     77  1.18     lukem #include "nbtool_config.h"
     78  1.13        tv #endif
     79  1.13        tv 
     80   1.3       jtc #define _NLS_PRIVATE
     81   1.1       jtc 
     82  1.17     bjh21 #include <sys/types.h>
     83   1.3       jtc #include <sys/queue.h>
     84  1.15     bjh21 
     85  1.16     bjh21 #include <netinet/in.h>	/* Needed by arpa/inet.h on NetBSD */
     86  1.16     bjh21 #include <arpa/inet.h>	/* Needed for htonl() on POSIX systems */
     87  1.15     bjh21 
     88   1.3       jtc #include <ctype.h>
     89  1.14        tv #include <err.h>
     90   1.7     lukem #include <fcntl.h>
     91  1.12      yamt #include <limits.h>
     92   1.7     lukem #include <nl_types.h>
     93   1.1       jtc #include <stdio.h>
     94   1.3       jtc #include <stdlib.h>
     95   1.3       jtc #include <string.h>
     96   1.3       jtc #include <unistd.h>
     97  1.13        tv 
     98  1.14        tv #ifndef NL_SETMAX
     99  1.14        tv #define NL_SETMAX 255
    100  1.14        tv #endif
    101  1.14        tv #ifndef NL_MSGMAX
    102  1.14        tv #define NL_MSGMAX 2048
    103  1.13        tv #endif
    104   1.3       jtc 
    105   1.3       jtc struct _msgT {
    106   1.3       jtc 	long    msgId;
    107   1.3       jtc 	char   *str;
    108   1.3       jtc         LIST_ENTRY(_msgT) entries;
    109   1.3       jtc };
    110   1.3       jtc 
    111   1.3       jtc struct _setT {
    112   1.3       jtc 	long    setId;
    113   1.3       jtc         LIST_HEAD(msghead, _msgT) msghead;
    114   1.3       jtc         LIST_ENTRY(_setT) entries;
    115   1.3       jtc };
    116   1.1       jtc 
    117   1.3       jtc LIST_HEAD(sethead, _setT) sethead;
    118   1.3       jtc static struct _setT *curSet;
    119   1.1       jtc 
    120   1.3       jtc static char *curline = NULL;
    121   1.3       jtc static long lineno = 0;
    122   1.1       jtc 
    123   1.7     lukem #if 0	/* XXX unused */
    124   1.7     lukem static	void	corrupt __P((void));
    125   1.7     lukem #endif
    126   1.7     lukem static	char   *cskip __P((char *));
    127  1.22  ginsbach static	void	error __P((const char *));
    128   1.7     lukem static	void	nomem __P((void));
    129   1.7     lukem static	char   *getline __P((int));
    130   1.7     lukem static	char   *getmsg __P((int, char *, char));
    131  1.22  ginsbach static	void	warning __P((const char *, const char *));
    132   1.7     lukem static	char   *wskip __P((char *));
    133   1.7     lukem static	char   *xstrdup __P((const char *));
    134   1.7     lukem static	void   *xmalloc __P((size_t));
    135   1.7     lukem static	void   *xrealloc __P((void *, size_t));
    136   1.7     lukem 
    137   1.7     lukem void	MCParse __P((int fd));
    138   1.7     lukem void	MCReadCat __P((int fd));
    139   1.7     lukem void	MCWriteCat __P((int fd));
    140   1.7     lukem void	MCDelMsg __P((int msgId));
    141   1.7     lukem void	MCAddMsg __P((int msgId, const char *msg));
    142   1.7     lukem void	MCAddSet __P((int setId));
    143   1.7     lukem void	MCDelSet __P((int setId));
    144   1.7     lukem int	main __P((int, char **));
    145   1.7     lukem void	usage __P((void));
    146   1.7     lukem 
    147   1.7     lukem 
    148   1.3       jtc void
    149   1.3       jtc usage()
    150   1.3       jtc {
    151  1.19      jmmv 	fprintf(stderr, "usage: %s catfile msgfile ...\n", getprogname());
    152   1.3       jtc 	exit(1);
    153   1.3       jtc }
    154   1.1       jtc 
    155   1.3       jtc int
    156   1.3       jtc main(argc, argv)
    157   1.3       jtc 	int     argc;
    158   1.3       jtc 	char   *argv[];
    159   1.3       jtc {
    160   1.3       jtc 	int     ofd, ifd;
    161   1.3       jtc 	char   *catfile = NULL;
    162   1.3       jtc 	int     c;
    163   1.3       jtc 
    164   1.3       jtc 	while ((c = getopt(argc, argv, "")) != -1) {
    165   1.3       jtc 		switch (c) {
    166   1.3       jtc 		case '?':
    167   1.3       jtc 		default:
    168   1.3       jtc 			usage();
    169   1.3       jtc 			/* NOTREACHED */
    170   1.3       jtc 		}
    171   1.3       jtc 	}
    172   1.3       jtc 	argc -= optind;
    173   1.3       jtc 	argv += optind;
    174   1.1       jtc 
    175   1.3       jtc 	if (argc < 2) {
    176   1.1       jtc 		usage();
    177   1.3       jtc 		/* NOTREACHED */
    178   1.3       jtc 	}
    179   1.3       jtc 	catfile = *argv++;
    180   1.3       jtc 
    181   1.3       jtc 	for (; *argv; argv++) {
    182   1.7     lukem 		if ((ifd = open(*argv, O_RDONLY)) < 0)
    183   1.7     lukem 			err(1, "Unable to read %s", *argv);
    184   1.1       jtc 		MCParse(ifd);
    185   1.1       jtc 		close(ifd);
    186   1.1       jtc 	}
    187   1.3       jtc 
    188   1.7     lukem 	if ((ofd = open(catfile, O_WRONLY | O_TRUNC | O_CREAT, 0666)) < 0)
    189   1.7     lukem 		err(1, "Unable to create a new %s", catfile);
    190   1.1       jtc 	MCWriteCat(ofd);
    191   1.1       jtc 	exit(0);
    192   1.3       jtc }
    193   1.3       jtc 
    194   1.3       jtc static void
    195   1.3       jtc warning(cptr, msg)
    196  1.22  ginsbach 	const char   *cptr;
    197  1.22  ginsbach 	const char   *msg;
    198   1.3       jtc {
    199  1.10       cgd 	fprintf(stderr, "%s: %s on line %ld\n", getprogname(), msg, lineno);
    200   1.3       jtc 	fprintf(stderr, "%s\n", curline);
    201   1.3       jtc 	if (cptr) {
    202   1.3       jtc 		char   *tptr;
    203   1.3       jtc 		for (tptr = curline; tptr < cptr; ++tptr)
    204   1.3       jtc 			putc(' ', stderr);
    205   1.3       jtc 		fprintf(stderr, "^\n");
    206   1.3       jtc 	}
    207   1.3       jtc }
    208   1.3       jtc 
    209   1.3       jtc static void
    210  1.22  ginsbach error(msg)
    211  1.22  ginsbach 	const char   *msg;
    212   1.3       jtc {
    213  1.22  ginsbach 	warning(NULL, msg);
    214   1.1       jtc 	exit(1);
    215   1.1       jtc }
    216   1.1       jtc 
    217   1.7     lukem #if 0	/* XXX unused */
    218   1.3       jtc static void
    219   1.3       jtc corrupt()
    220   1.3       jtc {
    221  1.22  ginsbach 	error("corrupt message catalog");
    222   1.3       jtc }
    223   1.7     lukem #endif
    224   1.3       jtc 
    225   1.3       jtc static void
    226   1.3       jtc nomem()
    227   1.3       jtc {
    228  1.22  ginsbach 	error("out of memory");
    229   1.3       jtc }
    230   1.3       jtc 
    231   1.3       jtc static void *
    232   1.3       jtc xmalloc(len)
    233   1.3       jtc 	size_t  len;
    234   1.3       jtc {
    235   1.3       jtc 	void   *p;
    236   1.3       jtc 
    237   1.3       jtc 	if ((p = malloc(len)) == NULL)
    238   1.3       jtc 		nomem();
    239   1.3       jtc 	return (p);
    240   1.3       jtc }
    241   1.3       jtc 
    242   1.3       jtc static void *
    243   1.3       jtc xrealloc(ptr, size)
    244   1.3       jtc 	void   *ptr;
    245   1.3       jtc 	size_t  size;
    246   1.3       jtc {
    247   1.3       jtc 	if ((ptr = realloc(ptr, size)) == NULL)
    248   1.3       jtc 		nomem();
    249   1.3       jtc 	return (ptr);
    250   1.3       jtc }
    251   1.3       jtc 
    252   1.3       jtc static char *
    253   1.3       jtc xstrdup(str)
    254   1.7     lukem 	const char   *str;
    255   1.3       jtc {
    256   1.7     lukem 	char *nstr;
    257   1.7     lukem 
    258   1.7     lukem 	if ((nstr = strdup(str)) == NULL)
    259   1.3       jtc 		nomem();
    260   1.7     lukem 	return (nstr);
    261   1.3       jtc }
    262   1.3       jtc 
    263   1.3       jtc static char *
    264   1.3       jtc getline(fd)
    265   1.3       jtc 	int     fd;
    266   1.3       jtc {
    267   1.3       jtc 	static long curlen = BUFSIZ;
    268   1.3       jtc 	static char buf[BUFSIZ], *bptr = buf, *bend = buf;
    269   1.3       jtc 	char   *cptr, *cend;
    270   1.3       jtc 	long    buflen;
    271   1.3       jtc 
    272   1.3       jtc 	if (!curline) {
    273   1.3       jtc 		curline = xmalloc(curlen);
    274   1.3       jtc 	}
    275   1.3       jtc 	++lineno;
    276   1.3       jtc 
    277   1.3       jtc 	cptr = curline;
    278   1.3       jtc 	cend = curline + curlen;
    279   1.3       jtc 	for (;;) {
    280   1.3       jtc 		for (; bptr < bend && cptr < cend; ++cptr, ++bptr) {
    281   1.3       jtc 			if (*bptr == '\n') {
    282   1.3       jtc 				*cptr = '\0';
    283   1.3       jtc 				++bptr;
    284   1.3       jtc 				return (curline);
    285   1.3       jtc 			} else
    286   1.3       jtc 				*cptr = *bptr;
    287   1.3       jtc 		}
    288  1.11      yamt 		if (cptr == cend) {
    289  1.11      yamt 			cptr = curline = xrealloc(curline, curlen *= 2);
    290  1.11      yamt 			cend = curline + curlen;
    291  1.11      yamt 		}
    292   1.3       jtc 		if (bptr == bend) {
    293   1.3       jtc 			buflen = read(fd, buf, BUFSIZ);
    294   1.3       jtc 			if (buflen <= 0) {
    295   1.3       jtc 				if (cptr > curline) {
    296   1.3       jtc 					*cptr = '\0';
    297   1.3       jtc 					return (curline);
    298   1.3       jtc 				}
    299   1.3       jtc 				return (NULL);
    300   1.3       jtc 			}
    301   1.3       jtc 			bend = buf + buflen;
    302   1.3       jtc 			bptr = buf;
    303   1.3       jtc 		}
    304   1.3       jtc 	}
    305   1.3       jtc }
    306   1.3       jtc 
    307   1.3       jtc static char *
    308   1.3       jtc wskip(cptr)
    309   1.3       jtc 	char   *cptr;
    310   1.3       jtc {
    311   1.9     itohy 	if (!*cptr || !isspace((unsigned char) *cptr)) {
    312   1.3       jtc 		warning(cptr, "expected a space");
    313   1.3       jtc 		return (cptr);
    314   1.3       jtc 	}
    315   1.9     itohy 	while (*cptr && isspace((unsigned char) *cptr))
    316   1.3       jtc 		++cptr;
    317   1.3       jtc 	return (cptr);
    318   1.3       jtc }
    319   1.3       jtc 
    320   1.3       jtc static char *
    321   1.3       jtc cskip(cptr)
    322   1.3       jtc 	char   *cptr;
    323   1.3       jtc {
    324   1.9     itohy 	if (!*cptr || isspace((unsigned char) *cptr)) {
    325   1.3       jtc 		warning(cptr, "wasn't expecting a space");
    326   1.3       jtc 		return (cptr);
    327   1.3       jtc 	}
    328   1.9     itohy 	while (*cptr && !isspace((unsigned char) *cptr))
    329   1.3       jtc 		++cptr;
    330   1.3       jtc 	return (cptr);
    331   1.3       jtc }
    332   1.3       jtc 
    333   1.3       jtc static char *
    334   1.3       jtc getmsg(fd, cptr, quote)
    335   1.3       jtc 	int     fd;
    336   1.3       jtc 	char   *cptr;
    337   1.3       jtc 	char    quote;
    338   1.3       jtc {
    339   1.3       jtc 	static char *msg = NULL;
    340   1.3       jtc 	static long msglen = 0;
    341   1.3       jtc 	long    clen, i;
    342   1.3       jtc 	char   *tptr;
    343   1.3       jtc 
    344   1.3       jtc 	if (quote && *cptr == quote) {
    345   1.3       jtc 		++cptr;
    346   1.3       jtc 	}
    347   1.3       jtc 
    348   1.3       jtc 	clen = strlen(cptr) + 1;
    349   1.3       jtc 	if (clen > msglen) {
    350   1.3       jtc 		if (msglen)
    351   1.3       jtc 			msg = xrealloc(msg, clen);
    352   1.3       jtc 		else
    353   1.3       jtc 			msg = xmalloc(clen);
    354   1.3       jtc 		msglen = clen;
    355   1.3       jtc 	}
    356   1.3       jtc 	tptr = msg;
    357   1.3       jtc 
    358   1.3       jtc 	while (*cptr) {
    359   1.3       jtc 		if (quote && *cptr == quote) {
    360   1.3       jtc 			char   *tmp;
    361   1.3       jtc 			tmp = cptr + 1;
    362   1.9     itohy 			if (*tmp && (!isspace((unsigned char) *tmp) || *wskip(tmp))) {
    363   1.8    mjacob 				warning(cptr, "unexpected quote character, ignoring");
    364   1.3       jtc 				*tptr++ = *cptr++;
    365   1.3       jtc 			} else {
    366   1.3       jtc 				*cptr = '\0';
    367   1.3       jtc 			}
    368   1.3       jtc 		} else
    369   1.3       jtc 			if (*cptr == '\\') {
    370   1.3       jtc 				++cptr;
    371   1.3       jtc 				switch (*cptr) {
    372   1.3       jtc 				case '\0':
    373   1.3       jtc 					cptr = getline(fd);
    374   1.3       jtc 					if (!cptr)
    375  1.22  ginsbach 						error("premature end of file");
    376   1.3       jtc 					msglen += strlen(cptr);
    377   1.3       jtc 					i = tptr - msg;
    378   1.3       jtc 					msg = xrealloc(msg, msglen);
    379   1.3       jtc 					tptr = msg + i;
    380   1.3       jtc 					break;
    381   1.3       jtc 				case 'n':
    382   1.3       jtc 					*tptr++ = '\n';
    383   1.3       jtc 					++cptr;
    384   1.3       jtc 					break;
    385   1.3       jtc 				case 't':
    386   1.3       jtc 					*tptr++ = '\t';
    387   1.3       jtc 					++cptr;
    388   1.3       jtc 					break;
    389   1.3       jtc 				case 'v':
    390   1.3       jtc 					*tptr++ = '\v';
    391   1.3       jtc 					++cptr;
    392   1.3       jtc 					break;
    393   1.3       jtc 				case 'b':
    394   1.3       jtc 					*tptr++ = '\b';
    395   1.3       jtc 					++cptr;
    396   1.3       jtc 					break;
    397   1.3       jtc 				case 'r':
    398   1.3       jtc 					*tptr++ = '\r';
    399   1.3       jtc 					++cptr;
    400   1.3       jtc 					break;
    401   1.3       jtc 				case 'f':
    402   1.3       jtc 					*tptr++ = '\f';
    403   1.3       jtc 					++cptr;
    404   1.3       jtc 					break;
    405   1.3       jtc 				case '\\':
    406   1.3       jtc 					*tptr++ = '\\';
    407   1.3       jtc 					++cptr;
    408   1.3       jtc 					break;
    409   1.3       jtc 				default:
    410   1.8    mjacob 					if (quote && *cptr == quote) {
    411   1.8    mjacob 						*tptr++ = *cptr++;
    412   1.9     itohy 					} else if (isdigit((unsigned char) *cptr)) {
    413   1.3       jtc 						*tptr = 0;
    414   1.3       jtc 						for (i = 0; i < 3; ++i) {
    415   1.9     itohy 							if (!isdigit((unsigned char) *cptr))
    416   1.3       jtc 								break;
    417   1.3       jtc 							if (*cptr > '7')
    418   1.3       jtc 								warning(cptr, "octal number greater than 7?!");
    419   1.3       jtc 							*tptr *= 8;
    420   1.3       jtc 							*tptr += (*cptr - '0');
    421   1.3       jtc 							++cptr;
    422   1.3       jtc 						}
    423   1.3       jtc 					} else {
    424   1.3       jtc 						warning(cptr, "unrecognized escape sequence");
    425   1.3       jtc 					}
    426   1.8    mjacob 					break;
    427   1.3       jtc 				}
    428   1.3       jtc 			} else {
    429   1.3       jtc 				*tptr++ = *cptr++;
    430   1.3       jtc 			}
    431   1.3       jtc 	}
    432   1.3       jtc 	*tptr = '\0';
    433   1.3       jtc 	return (msg);
    434   1.3       jtc }
    435   1.3       jtc 
    436   1.3       jtc void
    437   1.3       jtc MCParse(fd)
    438   1.3       jtc 	int     fd;
    439   1.3       jtc {
    440   1.3       jtc 	char   *cptr, *str;
    441  1.21  ginsbach 	int	msgid = 0;
    442  1.21  ginsbach 	int     setid = 0;
    443   1.3       jtc 	char    quote = 0;
    444   1.3       jtc 
    445   1.3       jtc 	/* XXX: init sethead? */
    446   1.3       jtc 
    447   1.3       jtc 	while ((cptr = getline(fd))) {
    448   1.3       jtc 		if (*cptr == '$') {
    449   1.3       jtc 			++cptr;
    450   1.3       jtc 			if (strncmp(cptr, "set", 3) == 0) {
    451   1.3       jtc 				cptr += 3;
    452   1.3       jtc 				cptr = wskip(cptr);
    453   1.3       jtc 				setid = atoi(cptr);
    454   1.3       jtc 				MCAddSet(setid);
    455   1.3       jtc 				msgid = 0;
    456   1.3       jtc 			} else if (strncmp(cptr, "delset", 6) == 0) {
    457   1.3       jtc 				cptr += 6;
    458   1.3       jtc 				cptr = wskip(cptr);
    459   1.3       jtc 				setid = atoi(cptr);
    460   1.3       jtc 				MCDelSet(setid);
    461   1.3       jtc 			} else if (strncmp(cptr, "quote", 5) == 0) {
    462   1.3       jtc 				cptr += 5;
    463   1.3       jtc 				if (!*cptr)
    464   1.3       jtc 					quote = 0;
    465   1.3       jtc 				else {
    466   1.3       jtc 					cptr = wskip(cptr);
    467   1.3       jtc 					if (!*cptr)
    468   1.3       jtc 						quote = 0;
    469   1.3       jtc 					else
    470   1.3       jtc 						quote = *cptr;
    471   1.3       jtc 				}
    472   1.9     itohy 			} else if (isspace((unsigned char) *cptr)) {
    473   1.3       jtc 				;
    474   1.3       jtc 			} else {
    475   1.3       jtc 				if (*cptr) {
    476   1.3       jtc 					cptr = wskip(cptr);
    477   1.3       jtc 					if (*cptr)
    478   1.3       jtc 						warning(cptr, "unrecognized line");
    479   1.3       jtc 				}
    480   1.3       jtc 			}
    481   1.3       jtc 		} else {
    482   1.8    mjacob 			/*
    483   1.8    mjacob 			 * First check for (and eat) empty lines....
    484   1.8    mjacob 			 */
    485   1.8    mjacob 			if (!*cptr)
    486   1.8    mjacob 				continue;
    487   1.8    mjacob 			/*
    488   1.8    mjacob 			 * We have a digit? Start of a message. Else,
    489   1.8    mjacob 			 * syntax error.
    490   1.8    mjacob 			 */
    491   1.9     itohy 			if (isdigit((unsigned char) *cptr)) {
    492   1.3       jtc 				msgid = atoi(cptr);
    493   1.3       jtc 				cptr = cskip(cptr);
    494   1.3       jtc 				cptr = wskip(cptr);
    495   1.3       jtc 				/* if (*cptr) ++cptr; */
    496   1.8    mjacob 			} else {
    497   1.8    mjacob 				warning(cptr, "neither blank line nor start of a message id");
    498   1.8    mjacob 				continue;
    499   1.3       jtc 			}
    500   1.8    mjacob 			/*
    501  1.21  ginsbach 			 * If no set directive specified, all messages
    502  1.21  ginsbach 			 * shall be in default message set NL_SETD.
    503  1.21  ginsbach 			 */
    504  1.21  ginsbach 			if (setid == 0) {
    505  1.21  ginsbach 				setid = NL_SETD;
    506  1.21  ginsbach 				MCAddSet(setid);
    507  1.21  ginsbach 			}
    508  1.21  ginsbach 			/*
    509   1.8    mjacob 			 * If we have a message ID, but no message,
    510   1.8    mjacob 			 * then this means "delete this message id
    511   1.8    mjacob 			 * from the catalog".
    512   1.8    mjacob 			 */
    513   1.8    mjacob 			if (!*cptr) {
    514   1.3       jtc 				MCDelMsg(msgid);
    515   1.8    mjacob 			} else {
    516   1.3       jtc 				str = getmsg(fd, cptr, quote);
    517   1.3       jtc 				MCAddMsg(msgid, str);
    518   1.3       jtc 			}
    519   1.3       jtc 		}
    520   1.3       jtc 	}
    521   1.3       jtc }
    522   1.3       jtc 
    523   1.3       jtc void
    524   1.3       jtc MCReadCat(fd)
    525   1.3       jtc 	int     fd;
    526   1.3       jtc {
    527   1.3       jtc #if 0
    528   1.3       jtc 	MCHeaderT mcHead;
    529   1.3       jtc 	MCMsgT  mcMsg;
    530   1.3       jtc 	MCSetT  mcSet;
    531   1.3       jtc 	msgT   *msg;
    532   1.3       jtc 	setT   *set;
    533   1.3       jtc 	int     i;
    534   1.3       jtc 	char   *data;
    535   1.3       jtc 
    536   1.3       jtc 	/* XXX init sethead? */
    537   1.3       jtc 
    538   1.3       jtc 	if (read(fd, &mcHead, sizeof(mcHead)) != sizeof(mcHead))
    539   1.3       jtc 		corrupt();
    540   1.3       jtc 	if (strncmp(mcHead.magic, MCMagic, MCMagicLen) != 0)
    541   1.3       jtc 		corrupt();
    542   1.3       jtc 	if (mcHead.majorVer != MCMajorVer)
    543  1.22  ginsbach 		error("unrecognized catalog version");
    544   1.3       jtc 	if ((mcHead.flags & MCGetByteOrder()) == 0)
    545  1.22  ginsbach 		error("wrong byte order");
    546   1.3       jtc 
    547   1.3       jtc 	if (lseek(fd, mcHead.firstSet, SEEK_SET) == -1)
    548   1.3       jtc 		corrupt();
    549   1.3       jtc 
    550   1.3       jtc 	for (;;) {
    551   1.3       jtc 		if (read(fd, &mcSet, sizeof(mcSet)) != sizeof(mcSet))
    552   1.3       jtc 			corrupt();
    553   1.3       jtc 		if (mcSet.invalid)
    554   1.3       jtc 			continue;
    555   1.3       jtc 
    556   1.3       jtc 		set = xmalloc(sizeof(setT));
    557   1.3       jtc 		memset(set, '\0', sizeof(*set));
    558   1.3       jtc 		if (cat->first) {
    559   1.3       jtc 			cat->last->next = set;
    560   1.3       jtc 			set->prev = cat->last;
    561   1.3       jtc 			cat->last = set;
    562   1.3       jtc 		} else
    563   1.3       jtc 			cat->first = cat->last = set;
    564   1.3       jtc 
    565   1.3       jtc 		set->setId = mcSet.setId;
    566   1.3       jtc 
    567   1.3       jtc 		/* Get the data */
    568   1.3       jtc 		if (mcSet.dataLen) {
    569   1.3       jtc 			data = xmalloc(mcSet.dataLen);
    570   1.3       jtc 			if (lseek(fd, mcSet.data.off, SEEK_SET) == -1)
    571   1.3       jtc 				corrupt();
    572   1.3       jtc 			if (read(fd, data, mcSet.dataLen) != mcSet.dataLen)
    573   1.3       jtc 				corrupt();
    574   1.3       jtc 			if (lseek(fd, mcSet.u.firstMsg, SEEK_SET) == -1)
    575   1.3       jtc 				corrupt();
    576   1.3       jtc 
    577   1.3       jtc 			for (i = 0; i < mcSet.numMsgs; ++i) {
    578   1.3       jtc 				if (read(fd, &mcMsg, sizeof(mcMsg)) != sizeof(mcMsg))
    579   1.3       jtc 					corrupt();
    580   1.3       jtc 				if (mcMsg.invalid) {
    581   1.3       jtc 					--i;
    582   1.3       jtc 					continue;
    583   1.3       jtc 				}
    584   1.3       jtc 				msg = xmalloc(sizeof(msgT));
    585   1.3       jtc 				memset(msg, '\0', sizeof(*msg));
    586   1.3       jtc 				if (set->first) {
    587   1.3       jtc 					set->last->next = msg;
    588   1.3       jtc 					msg->prev = set->last;
    589   1.3       jtc 					set->last = msg;
    590   1.3       jtc 				} else
    591   1.3       jtc 					set->first = set->last = msg;
    592   1.3       jtc 
    593   1.3       jtc 				msg->msgId = mcMsg.msgId;
    594   1.3       jtc 				msg->str = xstrdup((char *) (data + mcMsg.msg.off));
    595   1.3       jtc 			}
    596   1.3       jtc 			free(data);
    597   1.3       jtc 		}
    598   1.3       jtc 		if (!mcSet.nextSet)
    599   1.3       jtc 			break;
    600   1.3       jtc 		if (lseek(fd, mcSet.nextSet, SEEK_SET) == -1)
    601   1.3       jtc 			corrupt();
    602   1.3       jtc 	}
    603   1.1       jtc #endif
    604   1.3       jtc }
    605   1.3       jtc 
    606   1.3       jtc /*
    607   1.3       jtc  * Write message catalog.
    608   1.3       jtc  *
    609   1.3       jtc  * The message catalog is first converted from its internal to its
    610   1.3       jtc  * external representation in a chunk of memory allocated for this
    611   1.3       jtc  * purpose.  Then the completed catalog is written.  This approach
    612   1.3       jtc  * avoids additional housekeeping variables and/or a lot of seeks
    613   1.3       jtc  * that would otherwise be required.
    614   1.3       jtc  */
    615   1.3       jtc void
    616   1.3       jtc MCWriteCat(fd)
    617   1.3       jtc 	int     fd;
    618   1.1       jtc {
    619   1.3       jtc 	int     nsets;		/* number of sets */
    620   1.3       jtc 	int     nmsgs;		/* number of msgs */
    621   1.3       jtc 	int     string_size;	/* total size of string pool */
    622   1.3       jtc 	int     msgcat_size;	/* total size of message catalog */
    623   1.3       jtc 	void   *msgcat;		/* message catalog data */
    624   1.3       jtc 	struct _nls_cat_hdr *cat_hdr;
    625   1.3       jtc 	struct _nls_set_hdr *set_hdr;
    626   1.3       jtc 	struct _nls_msg_hdr *msg_hdr;
    627   1.3       jtc 	char   *strings;
    628   1.3       jtc 	struct _setT *set;
    629   1.3       jtc 	struct _msgT *msg;
    630   1.3       jtc 	int     msg_index;
    631   1.3       jtc 	int     msg_offset;
    632   1.3       jtc 
    633   1.3       jtc 	/* determine number of sets, number of messages, and size of the
    634   1.3       jtc 	 * string pool */
    635   1.3       jtc 	nsets = 0;
    636   1.3       jtc 	nmsgs = 0;
    637   1.3       jtc 	string_size = 0;
    638   1.3       jtc 
    639   1.3       jtc 	for (set = sethead.lh_first; set != NULL;
    640   1.3       jtc 	    set = set->entries.le_next) {
    641   1.3       jtc 		nsets++;
    642   1.3       jtc 
    643   1.3       jtc 		for (msg = set->msghead.lh_first; msg != NULL;
    644   1.3       jtc 		    msg = msg->entries.le_next) {
    645   1.3       jtc 			nmsgs++;
    646   1.3       jtc 			string_size += strlen(msg->str) + 1;
    647   1.3       jtc 		}
    648   1.3       jtc 	}
    649   1.3       jtc 
    650   1.3       jtc #ifdef DEBUG
    651   1.3       jtc 	printf("number of sets: %d\n", nsets);
    652   1.3       jtc 	printf("number of msgs: %d\n", nmsgs);
    653   1.3       jtc 	printf("string pool size: %d\n", string_size);
    654   1.3       jtc #endif
    655   1.3       jtc 
    656   1.3       jtc 	/* determine size and then allocate buffer for constructing external
    657   1.3       jtc 	 * message catalog representation */
    658   1.3       jtc 	msgcat_size = sizeof(struct _nls_cat_hdr)
    659   1.3       jtc 	    + (nsets * sizeof(struct _nls_set_hdr))
    660   1.3       jtc 	    + (nmsgs * sizeof(struct _nls_msg_hdr))
    661   1.3       jtc 	    + string_size;
    662   1.3       jtc 
    663   1.3       jtc 	msgcat = xmalloc(msgcat_size);
    664   1.3       jtc 	memset(msgcat, '\0', msgcat_size);
    665   1.3       jtc 
    666   1.3       jtc 	/* fill in msg catalog header */
    667   1.3       jtc 	cat_hdr = (struct _nls_cat_hdr *) msgcat;
    668   1.3       jtc 	cat_hdr->__magic = htonl(_NLS_MAGIC);
    669   1.3       jtc 	cat_hdr->__nsets = htonl(nsets);
    670   1.3       jtc 	cat_hdr->__mem = htonl(msgcat_size - sizeof(struct _nls_cat_hdr));
    671   1.3       jtc 	cat_hdr->__msg_hdr_offset =
    672   1.3       jtc 	    htonl(nsets * sizeof(struct _nls_set_hdr));
    673   1.3       jtc 	cat_hdr->__msg_txt_offset =
    674   1.3       jtc 	    htonl(nsets * sizeof(struct _nls_set_hdr) +
    675   1.3       jtc 	    nmsgs * sizeof(struct _nls_msg_hdr));
    676   1.3       jtc 
    677   1.3       jtc 	/* compute offsets for set & msg header tables and string pool */
    678   1.3       jtc 	set_hdr = (struct _nls_set_hdr *) ((char *) msgcat +
    679   1.3       jtc 	    sizeof(struct _nls_cat_hdr));
    680   1.3       jtc 	msg_hdr = (struct _nls_msg_hdr *) ((char *) msgcat +
    681   1.3       jtc 	    sizeof(struct _nls_cat_hdr) +
    682   1.3       jtc 	    nsets * sizeof(struct _nls_set_hdr));
    683   1.3       jtc 	strings = (char *) msgcat +
    684   1.3       jtc 	    sizeof(struct _nls_cat_hdr) +
    685   1.3       jtc 	    nsets * sizeof(struct _nls_set_hdr) +
    686   1.3       jtc 	    nmsgs * sizeof(struct _nls_msg_hdr);
    687   1.3       jtc 
    688   1.3       jtc 	msg_index = 0;
    689   1.3       jtc 	msg_offset = 0;
    690   1.3       jtc 	for (set = sethead.lh_first; set != NULL;
    691   1.3       jtc 	    set = set->entries.le_next) {
    692   1.3       jtc 
    693   1.3       jtc 		nmsgs = 0;
    694   1.3       jtc 		for (msg = set->msghead.lh_first; msg != NULL;
    695   1.3       jtc 		    msg = msg->entries.le_next) {
    696   1.3       jtc 			int     msg_len = strlen(msg->str) + 1;
    697   1.3       jtc 
    698   1.3       jtc 			msg_hdr->__msgno = htonl(msg->msgId);
    699   1.3       jtc 			msg_hdr->__msglen = htonl(msg_len);
    700   1.3       jtc 			msg_hdr->__offset = htonl(msg_offset);
    701   1.3       jtc 
    702   1.3       jtc 			memcpy(strings, msg->str, msg_len);
    703   1.3       jtc 			strings += msg_len;
    704   1.3       jtc 			msg_offset += msg_len;
    705   1.3       jtc 
    706   1.3       jtc 			nmsgs++;
    707   1.3       jtc 			msg_hdr++;
    708   1.3       jtc 		}
    709   1.3       jtc 
    710   1.3       jtc 		set_hdr->__setno = htonl(set->setId);
    711   1.3       jtc 		set_hdr->__nmsgs = htonl(nmsgs);
    712   1.3       jtc 		set_hdr->__index = htonl(msg_index);
    713   1.3       jtc 		msg_index += nmsgs;
    714   1.3       jtc 		set_hdr++;
    715   1.3       jtc 	}
    716   1.3       jtc 
    717   1.3       jtc 	/* write out catalog.  XXX: should this be done in small chunks? */
    718   1.3       jtc 	write(fd, msgcat, msgcat_size);
    719   1.3       jtc }
    720   1.3       jtc 
    721   1.3       jtc void
    722   1.3       jtc MCAddSet(setId)
    723   1.3       jtc 	int     setId;
    724   1.3       jtc {
    725   1.3       jtc 	struct _setT *p, *q;
    726   1.3       jtc 
    727   1.3       jtc 	if (setId <= 0) {
    728  1.22  ginsbach 		error("setId's must be greater than zero");
    729   1.3       jtc 		/* NOTREACHED */
    730   1.3       jtc 	}
    731   1.3       jtc 	if (setId > NL_SETMAX) {
    732  1.22  ginsbach 		error("setId exceeds limit");
    733   1.3       jtc 		/* NOTREACHED */
    734   1.3       jtc 	}
    735   1.3       jtc 
    736   1.3       jtc 	p = sethead.lh_first;
    737   1.3       jtc 	q = NULL;
    738   1.3       jtc 	for (; p != NULL && p->setId < setId; q = p, p = p->entries.le_next);
    739   1.3       jtc 
    740   1.3       jtc 	if (p && p->setId == setId) {
    741   1.3       jtc 		;
    742   1.3       jtc 	} else {
    743   1.3       jtc 		p = xmalloc(sizeof(struct _setT));
    744   1.3       jtc 		memset(p, '\0', sizeof(struct _setT));
    745   1.3       jtc 		LIST_INIT(&p->msghead);
    746   1.3       jtc 
    747   1.3       jtc 		p->setId = setId;
    748   1.3       jtc 
    749   1.3       jtc 		if (q == NULL) {
    750   1.3       jtc 			LIST_INSERT_HEAD(&sethead, p, entries);
    751   1.3       jtc 		} else {
    752   1.3       jtc 			LIST_INSERT_AFTER(q, p, entries);
    753   1.3       jtc 		}
    754   1.3       jtc 	}
    755   1.3       jtc 
    756   1.3       jtc 	curSet = p;
    757   1.3       jtc }
    758   1.3       jtc 
    759   1.3       jtc void
    760   1.3       jtc MCAddMsg(msgId, str)
    761   1.3       jtc 	int     msgId;
    762   1.3       jtc 	const char *str;
    763   1.3       jtc {
    764   1.3       jtc 	struct _msgT *p, *q;
    765   1.3       jtc 
    766   1.3       jtc 	if (!curSet)
    767  1.22  ginsbach 		error("can't specify a message when no set exists");
    768   1.3       jtc 
    769   1.3       jtc 	if (msgId <= 0) {
    770  1.22  ginsbach 		error("msgId's must be greater than zero");
    771   1.3       jtc 		/* NOTREACHED */
    772   1.3       jtc 	}
    773  1.12      yamt 	if (msgId > NL_MSGMAX) {
    774  1.22  ginsbach 		error("msgID exceeds limit");
    775   1.3       jtc 		/* NOTREACHED */
    776   1.3       jtc 	}
    777   1.3       jtc 
    778   1.3       jtc 	p = curSet->msghead.lh_first;
    779   1.3       jtc 	q = NULL;
    780   1.3       jtc 	for (; p != NULL && p->msgId < msgId; q = p, p = p->entries.le_next);
    781   1.3       jtc 
    782   1.3       jtc 	if (p && p->msgId == msgId) {
    783   1.3       jtc 		free(p->str);
    784   1.3       jtc 	} else {
    785   1.3       jtc 		p = xmalloc(sizeof(struct _msgT));
    786   1.3       jtc 		memset(p, '\0', sizeof(struct _msgT));
    787   1.3       jtc 
    788   1.3       jtc 		if (q == NULL) {
    789   1.3       jtc 			LIST_INSERT_HEAD(&curSet->msghead, p, entries);
    790   1.3       jtc 		} else {
    791   1.3       jtc 			LIST_INSERT_AFTER(q, p, entries);
    792   1.3       jtc 		}
    793   1.3       jtc 	}
    794   1.3       jtc 
    795   1.3       jtc 	p->msgId = msgId;
    796   1.3       jtc 	p->str = xstrdup(str);
    797   1.3       jtc }
    798   1.3       jtc 
    799   1.3       jtc void
    800   1.3       jtc MCDelSet(setId)
    801   1.3       jtc 	int     setId;
    802   1.3       jtc {
    803   1.3       jtc 	struct _setT *set;
    804   1.3       jtc 	struct _msgT *msg;
    805   1.3       jtc 
    806  1.21  ginsbach 	if (setId <= 0) {
    807  1.22  ginsbach 		error("setId's must be greater than zero");
    808  1.21  ginsbach 		/* NOTREACHED */
    809  1.21  ginsbach 	}
    810  1.21  ginsbach 	if (setId > NL_SETMAX) {
    811  1.22  ginsbach 		error("setId exceeds limit");
    812  1.21  ginsbach 		/* NOTREACHED */
    813  1.21  ginsbach 	}
    814  1.21  ginsbach 
    815   1.3       jtc 	set = sethead.lh_first;
    816   1.3       jtc 	for (; set != NULL && set->setId < setId; set = set->entries.le_next);
    817   1.3       jtc 
    818   1.3       jtc 	if (set && set->setId == setId) {
    819  1.20       dsl 		LIST_REMOVE(set, entries);
    820  1.20       dsl 		while ((msg = set->msghead.lh_first) != NULL) {
    821  1.20       dsl 			LIST_REMOVE(msg, entries);
    822   1.3       jtc 			free(msg->str);
    823  1.20       dsl 			free(msg);
    824   1.3       jtc 		}
    825  1.20       dsl 		free(set);
    826   1.3       jtc 		return;
    827   1.3       jtc 	}
    828   1.3       jtc 	warning(NULL, "specified set doesn't exist");
    829   1.3       jtc }
    830   1.1       jtc 
    831   1.3       jtc void
    832   1.3       jtc MCDelMsg(msgId)
    833   1.3       jtc 	int     msgId;
    834   1.3       jtc {
    835   1.3       jtc 	struct _msgT *msg;
    836   1.1       jtc 
    837   1.3       jtc 	if (!curSet)
    838  1.22  ginsbach 		error("you can't delete a message before defining the set");
    839   1.1       jtc 
    840   1.3       jtc 	msg = curSet->msghead.lh_first;
    841   1.3       jtc 	for (; msg != NULL && msg->msgId < msgId; msg = msg->entries.le_next);
    842   1.1       jtc 
    843   1.3       jtc 	if (msg && msg->msgId == msgId) {
    844  1.20       dsl 		LIST_REMOVE(msg, entries);
    845   1.3       jtc 		free(msg->str);
    846  1.20       dsl 		free(msg);
    847   1.3       jtc 		return;
    848   1.3       jtc 	}
    849   1.3       jtc 	warning(NULL, "specified msg doesn't exist");
    850   1.1       jtc }
    851