2001-02-08 16:38:16 +00:00
|
|
|
/* $FreeBSD$ */
|
1995-03-30 12:49:09 +00:00
|
|
|
|
|
|
|
/***********************************************************
|
|
|
|
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
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
******************************************************************/
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <ctype.h>
|
1995-03-30 12:49:09 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "msgcat.h"
|
|
|
|
#include "gencat.h"
|
|
|
|
|
|
|
|
static char *curline = NULL;
|
|
|
|
static long lineno = 0;
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
static void
|
|
|
|
warning(char *cptr, const char *msg)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
1998-08-02 15:18:45 +00:00
|
|
|
warnx("%s on line %ld\n%s", msg, lineno, curline);
|
1995-03-30 12:49:09 +00:00
|
|
|
if (cptr) {
|
|
|
|
char *tptr;
|
|
|
|
for (tptr = curline; tptr < cptr; ++tptr) putc(' ', stderr);
|
|
|
|
fprintf(stderr, "^\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
static void
|
|
|
|
error(char *cptr, const char *msg)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
warning(cptr, msg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
static void
|
|
|
|
corrupt(void) {
|
1995-03-30 12:49:09 +00:00
|
|
|
error(NULL, "corrupt message catalog");
|
|
|
|
}
|
2001-02-08 16:38:16 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
nomem(void) {
|
1995-03-30 12:49:09 +00:00
|
|
|
error(NULL, "out of memory");
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
static char *
|
|
|
|
getline(int fd)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
2001-02-08 16:38:16 +00:00
|
|
|
static size_t curlen = BUFSIZ;
|
1995-03-30 12:49:09 +00:00
|
|
|
static char buf[BUFSIZ], *bptr = buf, *bend = buf;
|
|
|
|
char *cptr, *cend;
|
|
|
|
long buflen;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
if (!curline) {
|
|
|
|
curline = (char *) malloc(curlen);
|
|
|
|
if (!curline) nomem();
|
|
|
|
}
|
|
|
|
++lineno;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
cptr = curline;
|
|
|
|
cend = curline + curlen;
|
2001-02-08 16:38:16 +00:00
|
|
|
while (TRUE) {
|
1995-03-30 12:49:09 +00:00
|
|
|
for (; bptr < bend && cptr < cend; ++cptr, ++bptr) {
|
|
|
|
if (*bptr == '\n') {
|
|
|
|
*cptr = '\0';
|
|
|
|
++bptr;
|
|
|
|
return(curline);
|
|
|
|
} else *cptr = *bptr;
|
|
|
|
}
|
|
|
|
if (bptr == bend) {
|
|
|
|
buflen = read(fd, buf, BUFSIZ);
|
|
|
|
if (buflen <= 0) {
|
|
|
|
if (cptr > curline) {
|
|
|
|
*cptr = '\0';
|
|
|
|
return(curline);
|
|
|
|
}
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
bend = buf + buflen;
|
|
|
|
bptr = buf;
|
|
|
|
}
|
|
|
|
if (cptr == cend) {
|
|
|
|
cptr = curline = (char *) realloc(curline, curlen *= 2);
|
|
|
|
cend = curline + curlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
static char *
|
|
|
|
token(char *cptr)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
static char tok[MAXTOKEN+1];
|
|
|
|
char *tptr = tok;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1998-04-30 10:31:38 +00:00
|
|
|
while (*cptr && isspace((unsigned char)*cptr)) ++cptr;
|
|
|
|
while (*cptr && !isspace((unsigned char)*cptr)) *tptr++ = *cptr++;
|
1995-03-30 12:49:09 +00:00
|
|
|
*tptr = '\0';
|
|
|
|
return(tok);
|
|
|
|
}
|
2001-02-08 16:38:16 +00:00
|
|
|
|
|
|
|
static char *
|
|
|
|
wskip(char *cptr)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
1998-04-30 10:31:38 +00:00
|
|
|
if (!*cptr || !isspace((unsigned char)*cptr)) {
|
1995-03-30 12:49:09 +00:00
|
|
|
warning(cptr, "expected a space");
|
|
|
|
return(cptr);
|
|
|
|
}
|
1998-04-30 10:31:38 +00:00
|
|
|
while (*cptr && isspace((unsigned char)*cptr)) ++cptr;
|
1995-03-30 12:49:09 +00:00
|
|
|
return(cptr);
|
|
|
|
}
|
2001-02-08 16:38:16 +00:00
|
|
|
|
|
|
|
static char *
|
|
|
|
cskip(char *cptr)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
1998-04-30 10:31:38 +00:00
|
|
|
if (!*cptr || isspace((unsigned char)*cptr)) {
|
1995-03-30 12:49:09 +00:00
|
|
|
warning(cptr, "wasn't expecting a space");
|
|
|
|
return(cptr);
|
|
|
|
}
|
1998-04-30 10:31:38 +00:00
|
|
|
while (*cptr && !isspace((unsigned char)*cptr)) ++cptr;
|
1995-03-30 12:49:09 +00:00
|
|
|
return(cptr);
|
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
static char *
|
|
|
|
getmsg(int fd, char *cptr, char quote)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
2001-02-08 16:38:16 +00:00
|
|
|
static char *msg = NULL;
|
|
|
|
static size_t msglen = 0;
|
|
|
|
size_t clen, i;
|
|
|
|
char *tptr;
|
|
|
|
int needq;
|
1995-03-30 12:49:09 +00:00
|
|
|
|
|
|
|
if (quote && *cptr == quote) {
|
2001-02-08 16:38:16 +00:00
|
|
|
needq = TRUE;
|
1995-03-30 12:49:09 +00:00
|
|
|
++cptr;
|
2001-02-08 16:38:16 +00:00
|
|
|
} else needq = FALSE;
|
1995-03-30 12:49:09 +00:00
|
|
|
|
|
|
|
clen = strlen(cptr) + 1;
|
|
|
|
if (clen > msglen) {
|
|
|
|
if (msglen) msg = (char *) realloc(msg, clen);
|
|
|
|
else msg = (char *) malloc(clen);
|
|
|
|
msglen = clen;
|
|
|
|
}
|
|
|
|
tptr = msg;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
while (*cptr) {
|
|
|
|
if (quote && *cptr == quote) {
|
|
|
|
char *tmp;
|
|
|
|
tmp = cptr+1;
|
1998-04-30 10:31:38 +00:00
|
|
|
if (*tmp && (!isspace((unsigned char)*tmp) || *wskip(tmp))) {
|
2001-02-08 16:38:16 +00:00
|
|
|
warning(cptr, "unexpected quote character, ignoring");
|
1995-03-30 12:49:09 +00:00
|
|
|
*tptr++ = *cptr++;
|
|
|
|
} else {
|
|
|
|
*cptr = '\0';
|
|
|
|
}
|
|
|
|
} else if (*cptr == '\\') {
|
|
|
|
++cptr;
|
|
|
|
switch (*cptr) {
|
|
|
|
case '\0':
|
|
|
|
cptr = getline(fd);
|
|
|
|
if (!cptr) error(NULL, "premature end of file");
|
|
|
|
msglen += strlen(cptr);
|
|
|
|
i = tptr - msg;
|
|
|
|
msg = (char *) realloc(msg, msglen);
|
|
|
|
tptr = msg + i;
|
|
|
|
break;
|
2001-02-08 16:38:16 +00:00
|
|
|
|
|
|
|
#define CASEOF(CS, CH) \
|
|
|
|
case CS: \
|
|
|
|
*tptr++ = CH; \
|
|
|
|
++cptr; \
|
1995-03-30 12:49:09 +00:00
|
|
|
break;
|
2001-02-08 16:38:16 +00:00
|
|
|
|
|
|
|
CASEOF('n', '\n')
|
|
|
|
CASEOF('t', '\t')
|
|
|
|
CASEOF('v', '\v')
|
|
|
|
CASEOF('b', '\b')
|
|
|
|
CASEOF('r', '\r')
|
|
|
|
CASEOF('f', '\f')
|
|
|
|
CASEOF('"', '"')
|
|
|
|
CASEOF('\'', '\'')
|
|
|
|
CASEOF('\\', '\\')
|
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
default:
|
1998-04-30 10:31:38 +00:00
|
|
|
if (isdigit((unsigned char)*cptr)) {
|
1995-03-30 12:49:09 +00:00
|
|
|
*tptr = 0;
|
|
|
|
for (i = 0; i < 3; ++i) {
|
1998-04-30 10:31:38 +00:00
|
|
|
if (!isdigit((unsigned char)*cptr)) break;
|
1995-03-30 12:49:09 +00:00
|
|
|
if (*cptr > '7') warning(cptr, "octal number greater than 7?!");
|
|
|
|
*tptr *= 8;
|
|
|
|
*tptr += (*cptr - '0');
|
|
|
|
++cptr;
|
|
|
|
}
|
2000-07-15 17:17:33 +00:00
|
|
|
++tptr;
|
1995-03-30 12:49:09 +00:00
|
|
|
} else {
|
|
|
|
warning(cptr, "unrecognized escape sequence");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*tptr++ = *cptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*tptr = '\0';
|
|
|
|
return(msg);
|
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
static char *
|
|
|
|
dupstr(const char *ostr)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
char *nstr;
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
nstr = strdup(ostr);
|
1995-03-30 12:49:09 +00:00
|
|
|
if (!nstr) error(NULL, "unable to allocate storage");
|
|
|
|
return(nstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The Global Stuff
|
1995-05-30 06:41:30 +00:00
|
|
|
*/
|
1995-03-30 12:49:09 +00:00
|
|
|
|
|
|
|
typedef struct _msgT {
|
|
|
|
long msgId;
|
|
|
|
char *str;
|
|
|
|
char *hconst;
|
|
|
|
long offset;
|
|
|
|
struct _msgT *prev, *next;
|
|
|
|
} msgT;
|
2001-02-08 16:38:16 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
typedef struct _setT {
|
|
|
|
long setId;
|
|
|
|
char *hconst;
|
|
|
|
msgT *first, *last;
|
|
|
|
struct _setT *prev, *next;
|
|
|
|
} setT;
|
2001-02-08 16:38:16 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
typedef struct {
|
|
|
|
setT *first, *last;
|
|
|
|
} catT;
|
|
|
|
|
|
|
|
static setT *curSet;
|
|
|
|
static catT *cat;
|
|
|
|
|
|
|
|
/*
|
2001-02-08 16:38:16 +00:00
|
|
|
* Find the current byte order. There are of course some others, but
|
|
|
|
* this will do for now. Note that all we care about is "long".
|
1995-03-30 12:49:09 +00:00
|
|
|
*/
|
2001-02-08 16:38:16 +00:00
|
|
|
long
|
|
|
|
MCGetByteOrder(void) {
|
1995-03-30 12:49:09 +00:00
|
|
|
long l = 0x00010203;
|
|
|
|
char *cptr = (char *) &l;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
if (cptr[0] == 0 && cptr[1] == 1 && cptr[2] == 2 && cptr[3] == 3)
|
|
|
|
return MC68KByteOrder;
|
|
|
|
else return MCn86ByteOrder;
|
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
void
|
|
|
|
MCParse(int fd)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
char *cptr, *str;
|
2001-02-08 16:38:16 +00:00
|
|
|
int setid, msgid = 0;
|
1995-03-30 12:49:09 +00:00
|
|
|
char hconst[MAXTOKEN+1];
|
|
|
|
char quote = 0;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
if (!cat) {
|
|
|
|
cat = (catT *) malloc(sizeof(catT));
|
|
|
|
if (!cat) nomem();
|
|
|
|
bzero(cat, sizeof(catT));
|
|
|
|
}
|
|
|
|
|
|
|
|
hconst[0] = '\0';
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
while ((cptr = getline(fd)) != NULL) {
|
1995-03-30 12:49:09 +00:00
|
|
|
if (*cptr == '$') {
|
|
|
|
++cptr;
|
|
|
|
if (strncmp(cptr, "set", 3) == 0) {
|
|
|
|
cptr += 3;
|
|
|
|
cptr = wskip(cptr);
|
|
|
|
setid = atoi(cptr);
|
|
|
|
cptr = cskip(cptr);
|
|
|
|
if (*cptr) cptr = wskip(cptr);
|
|
|
|
if (*cptr == '#') {
|
|
|
|
++cptr;
|
|
|
|
MCAddSet(setid, token(cptr));
|
|
|
|
} else MCAddSet(setid, NULL);
|
|
|
|
msgid = 0;
|
|
|
|
} else if (strncmp(cptr, "delset", 6) == 0) {
|
|
|
|
cptr += 6;
|
|
|
|
cptr = wskip(cptr);
|
|
|
|
setid = atoi(cptr);
|
|
|
|
MCDelSet(setid);
|
|
|
|
} else if (strncmp(cptr, "quote", 5) == 0) {
|
|
|
|
cptr += 5;
|
|
|
|
if (!*cptr) quote = 0;
|
|
|
|
else {
|
|
|
|
cptr = wskip(cptr);
|
|
|
|
if (!*cptr) quote = 0;
|
|
|
|
else quote = *cptr;
|
|
|
|
}
|
1998-04-30 10:31:38 +00:00
|
|
|
} else if (isspace((unsigned char)*cptr)) {
|
1995-03-30 12:49:09 +00:00
|
|
|
cptr = wskip(cptr);
|
|
|
|
if (*cptr == '#') {
|
|
|
|
++cptr;
|
|
|
|
strcpy(hconst, token(cptr));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (*cptr) {
|
|
|
|
cptr = wskip(cptr);
|
|
|
|
if (*cptr) warning(cptr, "unrecognized line");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
1998-04-30 10:31:38 +00:00
|
|
|
if (isdigit((unsigned char)*cptr) || *cptr == '#') {
|
1995-03-30 12:49:09 +00:00
|
|
|
if (*cptr == '#') {
|
|
|
|
++msgid;
|
|
|
|
++cptr;
|
|
|
|
if (!*cptr) {
|
|
|
|
MCAddMsg(msgid, "", hconst);
|
|
|
|
hconst[0] = '\0';
|
|
|
|
continue;
|
|
|
|
}
|
1998-04-30 10:31:38 +00:00
|
|
|
if (!isspace((unsigned char)*cptr)) warning(cptr, "expected a space");
|
1995-03-30 12:49:09 +00:00
|
|
|
++cptr;
|
|
|
|
if (!*cptr) {
|
|
|
|
MCAddMsg(msgid, "", hconst);
|
|
|
|
hconst[0] = '\0';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msgid = atoi(cptr);
|
|
|
|
cptr = cskip(cptr);
|
|
|
|
cptr = wskip(cptr);
|
|
|
|
/* if (*cptr) ++cptr; */
|
|
|
|
}
|
|
|
|
if (!*cptr) MCDelMsg(msgid);
|
|
|
|
else {
|
|
|
|
str = getmsg(fd, cptr, quote);
|
|
|
|
MCAddMsg(msgid, str, hconst);
|
|
|
|
hconst[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
void
|
|
|
|
MCReadCat(int fd)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
MCHeaderT mcHead;
|
|
|
|
MCMsgT mcMsg;
|
|
|
|
MCSetT mcSet;
|
|
|
|
msgT *msg;
|
|
|
|
setT *set;
|
|
|
|
int i;
|
|
|
|
char *data;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
cat = (catT *) malloc(sizeof(catT));
|
|
|
|
if (!cat) nomem();
|
|
|
|
bzero(cat, sizeof(catT));
|
|
|
|
|
|
|
|
if (read(fd, &mcHead, sizeof(mcHead)) != sizeof(mcHead)) corrupt();
|
|
|
|
if (strncmp(mcHead.magic, MCMagic, MCMagicLen) != 0) corrupt();
|
|
|
|
if (mcHead.majorVer != MCMajorVer) error(NULL, "unrecognized catalog version");
|
|
|
|
if ((mcHead.flags & MCGetByteOrder()) == 0) error(NULL, "wrong byte order");
|
|
|
|
|
|
|
|
if (lseek(fd, mcHead.firstSet, L_SET) == -1) corrupt();
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
while (TRUE) {
|
1995-03-30 12:49:09 +00:00
|
|
|
if (read(fd, &mcSet, sizeof(mcSet)) != sizeof(mcSet)) corrupt();
|
|
|
|
if (mcSet.invalid) continue;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
set = (setT *) malloc(sizeof(setT));
|
|
|
|
if (!set) nomem();
|
|
|
|
bzero(set, sizeof(*set));
|
|
|
|
if (cat->first) {
|
|
|
|
cat->last->next = set;
|
|
|
|
set->prev = cat->last;
|
|
|
|
cat->last = set;
|
|
|
|
} else cat->first = cat->last = set;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
set->setId = mcSet.setId;
|
|
|
|
|
|
|
|
/* Get the data */
|
|
|
|
if (mcSet.dataLen) {
|
2001-02-08 16:38:16 +00:00
|
|
|
data = (char *) malloc((size_t)mcSet.dataLen);
|
1995-03-30 12:49:09 +00:00
|
|
|
if (!data) nomem();
|
|
|
|
if (lseek(fd, mcSet.data.off, L_SET) == -1) corrupt();
|
2001-02-08 16:38:16 +00:00
|
|
|
if (read(fd, data, (size_t)mcSet.dataLen) != mcSet.dataLen) corrupt();
|
1995-03-30 12:49:09 +00:00
|
|
|
if (lseek(fd, mcSet.u.firstMsg, L_SET) == -1) corrupt();
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
for (i = 0; i < mcSet.numMsgs; ++i) {
|
|
|
|
if (read(fd, &mcMsg, sizeof(mcMsg)) != sizeof(mcMsg)) corrupt();
|
|
|
|
if (mcMsg.invalid) {
|
|
|
|
--i;
|
|
|
|
continue;
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
msg = (msgT *) malloc(sizeof(msgT));
|
|
|
|
if (!msg) nomem();
|
|
|
|
bzero(msg, sizeof(*msg));
|
|
|
|
if (set->first) {
|
|
|
|
set->last->next = msg;
|
|
|
|
msg->prev = set->last;
|
|
|
|
set->last = msg;
|
|
|
|
} else set->first = set->last = msg;
|
|
|
|
|
|
|
|
msg->msgId = mcMsg.msgId;
|
|
|
|
msg->str = dupstr((char *) (data + mcMsg.msg.off));
|
|
|
|
}
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
if (!mcSet.nextSet) break;
|
|
|
|
if (lseek(fd, mcSet.nextSet, L_SET) == -1) corrupt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
static void
|
|
|
|
printS(int fd, const char *str)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
write(fd, str, strlen(str));
|
|
|
|
}
|
2001-02-08 16:38:16 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
printL(int fd, long l)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
sprintf(buf, "%ld", l);
|
|
|
|
write(fd, buf, strlen(buf));
|
|
|
|
}
|
2001-02-08 16:38:16 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
printLX(int fd, long l)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
sprintf(buf, "%lx", l);
|
|
|
|
write(fd, buf, strlen(buf));
|
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
static void
|
|
|
|
genconst(int fd, int type, char *setConst, char *msgConst, long val)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case MCLangC:
|
|
|
|
if (!msgConst) {
|
|
|
|
printS(fd, "\n#define ");
|
|
|
|
printS(fd, setConst);
|
|
|
|
printS(fd, "Set");
|
|
|
|
} else {
|
|
|
|
printS(fd, "#define ");
|
|
|
|
printS(fd, setConst);
|
|
|
|
printS(fd, msgConst);
|
|
|
|
}
|
|
|
|
printS(fd, "\t0x");
|
|
|
|
printLX(fd, val);
|
|
|
|
printS(fd, "\n");
|
|
|
|
break;
|
|
|
|
case MCLangCPlusPlus:
|
|
|
|
case MCLangANSIC:
|
|
|
|
if (!msgConst) {
|
|
|
|
printS(fd, "\nconst long ");
|
|
|
|
printS(fd, setConst);
|
|
|
|
printS(fd, "Set");
|
|
|
|
} else {
|
|
|
|
printS(fd, "const long ");
|
|
|
|
printS(fd, setConst);
|
|
|
|
printS(fd, msgConst);
|
|
|
|
}
|
|
|
|
printS(fd, "\t= ");
|
|
|
|
printL(fd, val);
|
|
|
|
printS(fd, ";\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error(NULL, "not a recognized (programming) language type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
void
|
|
|
|
MCWriteConst(int fd, int type, int orConsts)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
msgT *msg;
|
|
|
|
setT *set;
|
|
|
|
long id;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
if (orConsts && (type == MCLangC || type == MCLangCPlusPlus || type == MCLangANSIC)) {
|
|
|
|
printS(fd, "/* Use these Macros to compose and decompose setId's and msgId's */\n");
|
|
|
|
printS(fd, "#ifndef MCMakeId\n");
|
|
|
|
printS(fd, "# define MCMakeId(s,m)\t(unsigned long)(((unsigned short)s<<(sizeof(short)*8))\\\n");
|
|
|
|
printS(fd, "\t\t\t\t\t|(unsigned short)m)\n");
|
|
|
|
printS(fd, "# define MCSetId(id)\t(unsigned int) (id >> (sizeof(short) * 8))\n");
|
|
|
|
printS(fd, "# define MCMsgId(id)\t(unsigned int) ((id << (sizeof(short) * 8))\\\n");
|
|
|
|
printS(fd, "\t\t\t\t\t>> (sizeof(short) * 8))\n");
|
|
|
|
printS(fd, "#endif\n");
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
for (set = cat->first; set; set = set->next) {
|
|
|
|
if (set->hconst) genconst(fd, type, set->hconst, NULL, set->setId);
|
|
|
|
|
|
|
|
for (msg = set->first; msg; msg = msg->next) {
|
|
|
|
if (msg->hconst) {
|
|
|
|
if (orConsts) id = MCMakeId(set->setId, msg->msgId);
|
|
|
|
else id = msg->msgId;
|
|
|
|
genconst(fd, type, set->hconst, msg->hconst, id);
|
|
|
|
free(msg->hconst);
|
|
|
|
msg->hconst = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (set->hconst) {
|
|
|
|
free(set->hconst);
|
|
|
|
set->hconst = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
void
|
|
|
|
MCWriteCat(int fd)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
MCHeaderT mcHead;
|
|
|
|
int cnt;
|
|
|
|
setT *set;
|
|
|
|
msgT *msg;
|
|
|
|
MCSetT mcSet;
|
|
|
|
MCMsgT mcMsg;
|
|
|
|
off_t pos;
|
|
|
|
|
|
|
|
bcopy(MCMagic, mcHead.magic, MCMagicLen);
|
|
|
|
mcHead.majorVer = MCMajorVer;
|
|
|
|
mcHead.minorVer = MCMinorVer;
|
|
|
|
mcHead.flags = MCGetByteOrder();
|
|
|
|
mcHead.firstSet = 0; /* We'll be back to set this in a minute */
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
for (cnt = 0, set = cat->first; set; set = set->next) ++cnt;
|
|
|
|
mcHead.numSets = cnt;
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
lseek(fd, (off_t)0L, L_SET);
|
1995-03-30 12:49:09 +00:00
|
|
|
write(fd, &mcHead, sizeof(mcHead));
|
2001-02-08 16:38:16 +00:00
|
|
|
mcHead.firstSet = lseek(fd, (off_t)0L, L_INCR);
|
|
|
|
lseek(fd, (off_t)0L, L_SET);
|
1995-03-30 12:49:09 +00:00
|
|
|
write(fd, &mcHead, sizeof(mcHead));
|
|
|
|
|
|
|
|
for (set = cat->first; set; set = set->next) {
|
|
|
|
bzero(&mcSet, sizeof(mcSet));
|
|
|
|
|
|
|
|
mcSet.setId = set->setId;
|
2001-02-08 16:38:16 +00:00
|
|
|
mcSet.invalid = FALSE;
|
1995-03-30 12:49:09 +00:00
|
|
|
|
|
|
|
/* The rest we'll have to come back and change in a moment */
|
2001-02-08 16:38:16 +00:00
|
|
|
pos = lseek(fd, (off_t)0L, L_INCR);
|
1995-03-30 12:49:09 +00:00
|
|
|
write(fd, &mcSet, sizeof(mcSet));
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
/* Now write all the string data */
|
2001-02-08 16:38:16 +00:00
|
|
|
mcSet.data.off = lseek(fd, (off_t)0L, L_INCR);
|
1995-03-30 12:49:09 +00:00
|
|
|
cnt = 0;
|
|
|
|
for (msg = set->first; msg; msg = msg->next) {
|
2001-02-08 16:38:16 +00:00
|
|
|
msg->offset = lseek(fd, (off_t)0L, L_INCR) - mcSet.data.off;
|
1995-03-30 12:49:09 +00:00
|
|
|
mcSet.dataLen += write(fd, msg->str, strlen(msg->str) + 1);
|
|
|
|
++cnt;
|
|
|
|
}
|
2001-02-08 16:38:16 +00:00
|
|
|
mcSet.u.firstMsg = lseek(fd, (off_t)0L, L_INCR);
|
1995-03-30 12:49:09 +00:00
|
|
|
mcSet.numMsgs = cnt;
|
|
|
|
|
|
|
|
/* Now write the message headers */
|
|
|
|
for (msg = set->first; msg; msg = msg->next) {
|
|
|
|
mcMsg.msgId = msg->msgId;
|
|
|
|
mcMsg.msg.off = msg->offset;
|
2001-02-08 16:38:16 +00:00
|
|
|
mcMsg.invalid = FALSE;
|
1995-03-30 12:49:09 +00:00
|
|
|
write(fd, &mcMsg, sizeof(mcMsg));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Go back and fix things up */
|
|
|
|
|
|
|
|
if (set == cat->last) {
|
|
|
|
mcSet.nextSet = 0;
|
|
|
|
lseek(fd, pos, L_SET);
|
|
|
|
write(fd, &mcSet, sizeof(mcSet));
|
|
|
|
} else {
|
2001-02-08 16:38:16 +00:00
|
|
|
mcSet.nextSet = lseek(fd, (off_t)0L, L_INCR);
|
1995-03-30 12:49:09 +00:00
|
|
|
lseek(fd, pos, L_SET);
|
|
|
|
write(fd, &mcSet, sizeof(mcSet));
|
|
|
|
lseek(fd, mcSet.nextSet, L_SET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
void
|
|
|
|
MCAddSet(int setId, char *hconst)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
setT *set;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
if (setId <= 0) {
|
|
|
|
error(NULL, "setId's must be greater than zero");
|
|
|
|
return;
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
if (hconst && !*hconst) hconst = NULL;
|
|
|
|
for (set = cat->first; set; set = set->next) {
|
|
|
|
if (set->setId == setId) {
|
|
|
|
if (set->hconst && hconst) free(set->hconst);
|
|
|
|
set->hconst = NULL;
|
|
|
|
break;
|
|
|
|
} else if (set->setId > setId) {
|
|
|
|
setT *newSet;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
newSet = (setT *) malloc(sizeof(setT));
|
|
|
|
if (!newSet) nomem();
|
|
|
|
bzero(newSet, sizeof(setT));
|
|
|
|
newSet->prev = set->prev;
|
|
|
|
newSet->next = set;
|
|
|
|
if (set->prev) set->prev->next = newSet;
|
|
|
|
else cat->first = newSet;
|
|
|
|
set->prev = newSet;
|
|
|
|
set = newSet;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!set) {
|
|
|
|
set = (setT *) malloc(sizeof(setT));
|
|
|
|
if (!set) nomem();
|
|
|
|
bzero(set, sizeof(setT));
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
if (cat->first) {
|
|
|
|
set->prev = cat->last;
|
|
|
|
set->next = NULL;
|
|
|
|
cat->last->next = set;
|
|
|
|
cat->last = set;
|
|
|
|
} else {
|
|
|
|
set->prev = set->next = NULL;
|
|
|
|
cat->first = cat->last = set;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set->setId = setId;
|
|
|
|
if (hconst) set->hconst = dupstr(hconst);
|
|
|
|
curSet = set;
|
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
void
|
|
|
|
MCAddMsg(int msgId, const char *str, char *hconst)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
msgT *msg;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
if (!curSet)
|
|
|
|
error(NULL, "can't specify a message when no set exists");
|
1995-03-30 12:49:09 +00:00
|
|
|
|
|
|
|
if (msgId <= 0) {
|
|
|
|
error(NULL, "msgId's must be greater than zero");
|
|
|
|
return;
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
if (hconst && !*hconst) hconst = NULL;
|
|
|
|
for (msg = curSet->first; msg; msg = msg->next) {
|
|
|
|
if (msg->msgId == msgId) {
|
|
|
|
if (msg->hconst && hconst) free(msg->hconst);
|
|
|
|
if (msg->str) free(msg->str);
|
|
|
|
msg->hconst = msg->str = NULL;
|
|
|
|
break;
|
|
|
|
} else if (msg->msgId > msgId) {
|
|
|
|
msgT *newMsg;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
newMsg = (msgT *) malloc(sizeof(msgT));
|
|
|
|
if (!newMsg) nomem();
|
|
|
|
bzero(newMsg, sizeof(msgT));
|
|
|
|
newMsg->prev = msg->prev;
|
|
|
|
newMsg->next = msg;
|
|
|
|
if (msg->prev) msg->prev->next = newMsg;
|
|
|
|
else curSet->first = newMsg;
|
|
|
|
msg->prev = newMsg;
|
|
|
|
msg = newMsg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!msg) {
|
|
|
|
msg = (msgT *) malloc(sizeof(msgT));
|
|
|
|
if (!msg) nomem();
|
|
|
|
bzero(msg, sizeof(msgT));
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
if (curSet->first) {
|
|
|
|
msg->prev = curSet->last;
|
|
|
|
msg->next = NULL;
|
|
|
|
curSet->last->next = msg;
|
|
|
|
curSet->last = msg;
|
|
|
|
} else {
|
|
|
|
msg->prev = msg->next = NULL;
|
|
|
|
curSet->first = curSet->last = msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg->msgId = msgId;
|
|
|
|
if (hconst) msg->hconst = dupstr(hconst);
|
|
|
|
msg->str = dupstr(str);
|
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
void
|
|
|
|
MCDelSet(int setId)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
setT *set;
|
|
|
|
msgT *msg;
|
|
|
|
|
|
|
|
for (set = cat->first; set; set = set->next) {
|
|
|
|
if (set->setId == setId) {
|
|
|
|
for (msg = set->first; msg; msg = msg->next) {
|
|
|
|
if (msg->hconst) free(msg->hconst);
|
|
|
|
if (msg->str) free(msg->str);
|
|
|
|
free(msg);
|
|
|
|
}
|
|
|
|
if (set->hconst) free(set->hconst);
|
|
|
|
|
|
|
|
if (set->prev) set->prev->next = set->next;
|
|
|
|
else cat->first = set->next;
|
|
|
|
|
|
|
|
if (set->next) set->next->prev = set->prev;
|
|
|
|
else cat->last = set->prev;
|
|
|
|
|
|
|
|
free(set);
|
|
|
|
return;
|
|
|
|
} else if (set->setId > setId) break;
|
|
|
|
}
|
|
|
|
warning(NULL, "specified set doesn't exist");
|
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
void
|
|
|
|
MCDelMsg(int msgId)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
msgT *msg;
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
if (!curSet)
|
|
|
|
error(NULL, "you can't delete a message before defining the set");
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
for (msg = curSet->first; msg; msg = msg->next) {
|
|
|
|
if (msg->msgId == msgId) {
|
|
|
|
if (msg->hconst) free(msg->hconst);
|
|
|
|
if (msg->str) free(msg->str);
|
|
|
|
|
|
|
|
if (msg->prev) msg->prev->next = msg->next;
|
|
|
|
else curSet->first = msg->next;
|
|
|
|
|
|
|
|
if (msg->next) msg->next->prev = msg->prev;
|
|
|
|
else curSet->last = msg->prev;
|
|
|
|
|
|
|
|
free(msg);
|
|
|
|
return;
|
|
|
|
} else if (msg->msgId > msgId) break;
|
1995-05-30 06:41:30 +00:00
|
|
|
}
|
1995-03-30 12:49:09 +00:00
|
|
|
warning(NULL, "specified msg doesn't exist");
|
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
#if 0 /* this function is unsed and looks like debug thing */
|
|
|
|
|
|
|
|
void
|
|
|
|
MCDumpcat(fp)
|
1995-03-30 12:49:09 +00:00
|
|
|
FILE *fp;
|
|
|
|
{
|
|
|
|
msgT *msg;
|
|
|
|
setT *set;
|
|
|
|
|
1997-07-08 11:05:59 +00:00
|
|
|
if (!cat)
|
2001-02-08 16:38:16 +00:00
|
|
|
errx(1, "no catalog open");
|
1995-03-30 12:49:09 +00:00
|
|
|
|
|
|
|
for (set = cat->first; set; set = set->next) {
|
1998-08-02 15:18:45 +00:00
|
|
|
fprintf(fp, "$set %ld", set->setId);
|
1995-03-30 12:49:09 +00:00
|
|
|
if (set->hconst)
|
|
|
|
fprintf(fp, " # %s", set->hconst);
|
|
|
|
fprintf(fp, "\n\n");
|
|
|
|
|
|
|
|
for (msg = set->first; msg; msg = msg->next) {
|
|
|
|
if (msg->hconst)
|
|
|
|
fprintf(fp, "# %s\n", msg->hconst);
|
1998-08-02 15:18:45 +00:00
|
|
|
fprintf(fp, "%ld\t%s\n", msg->msgId, msg->str);
|
1995-03-30 12:49:09 +00:00
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2001-02-08 16:38:16 +00:00
|
|
|
#endif /* 0 */
|