31 #include <sys/cdefs.h> 33 #if defined(__RCSID) && !defined(lint) 34 __RCSID (
"$NetBSD: gencat.c,v 1.26 2008/11/04 03:14:46 ginsbach Exp $");
69 #if defined(__USE_GNU) 73 #if HAVE_NBTOOL_CONFIG_H 74 #include "nbtool_config.h" 79 #include <sys/types.h> 83 #include <sys/queue.h> 87 #include <arpa/inet.h> 104 #include <winsock2.h> 105 #if !defined(_MSC_VER) || _MSC_VER < 1800 118 #define NL_SETMAX 255 121 #define NL_MSGMAX 2048 128 LIST_ENTRY (
_msgT) entries;
135 LIST_ENTRY (_setT) entries;
138 #define _NLS_MAGIC 0xff88ff89 164 #define NL_CAT_LOCALE 1 167 static struct _setT *curSet;
169 static
const char *curfile;
170 static
char *curline =
NULL;
171 static
long lineno = 0;
173 static
char *
cskip (
char *);
176 static
char *
getmsg (
int,
char *,
char);
178 static
char *
wskip (
char *);
181 static
void *
xrealloc (
void *,
size_t);
190 int main (
int,
char **);
195 #define CORRUPT "corrupt message catalog" 196 #define NOMEMORY "out of memory" 200 fprintf (stderr,
"usage: %s catfile msgfile ...\n", progname);
208 char *catfile =
NULL;
214 while ((c =
getopt (argc, argv,
"")) != -1)
234 if ((catfile[0] ==
'-') && (catfile[1] ==
'\0'))
237 ofd = _fileno (stdout);
245 ofd = open (catfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
250 if ((ofd = open (catfile, O_RDWR)) < 0)
252 err (1,
"Unable to open %s", catfile);
258 err (1,
"Unable to create new %s", catfile);
264 if (lseek (ofd, SEEK_SET, 0) < 0)
266 err (1,
"Unable to seek on %s", catfile);
272 if (((*argv)[0] ==
'-') && ((*argv)[1] ==
'\0'))
285 for (; *
argv; argv++)
287 if ((ifd = open (*argv, O_RDONLY)) < 0)
288 err (1,
"Unable to read %s", *argv);
298 if (ftruncate (ofd, 0) != 0)
300 err (1,
"Unable to truncate %s", catfile);
314 fprintf (stderr,
"%s: %s on line %ld, %s\n", progname, msg, lineno, curfile);
315 fprintf (stderr,
"%s\n", curline);
319 for (tptr = curline; tptr < cptr; ++tptr)
321 fprintf (stderr,
"^\n");
326 fprintf (stderr,
"%s: %s, %s\n", progname, msg, curfile);
342 if ((p = malloc (len)) == NULL)
356 void *
const realloc_ptr = realloc (ptr, size);
357 if (realloc_ptr == NULL)
377 if ((nstr =
strdup (str)) == NULL)
391 static long curlen = BUFSIZ;
392 static char buf[BUFSIZ], *bptr = buf, *bend = buf;
398 curline = (
char *)
xmalloc (curlen);
403 cend = curline + curlen;
406 for (; bptr < bend && cptr < cend; ++cptr, ++bptr)
414 else if (bptr + 1 < bend && *bptr ==
'\r' && *(bptr + 1) ==
'\n')
429 int offset = (int) (cptr - curline);
430 curline = (
char *)
xrealloc (curline, curlen *= 2);
431 cptr = curline + offset;
432 cend = curline + curlen;
436 char last_char_prev_buf = 0;
440 last_char_prev_buf = *(bptr - 1);
443 buflen = read (fd, buf, BUFSIZ);
457 if (last_char_prev_buf ==
'\r' && *bptr ==
'\n')
474 if (!*cptr || !isspace ((
unsigned char) *cptr))
476 warning (cptr,
"expected a space");
479 while (*cptr && isspace ((
unsigned char) *cptr))
487 if (!*cptr || isspace ((
unsigned char) *cptr))
489 warning (cptr,
"wasn't expecting a space");
492 while (*cptr && !isspace ((
unsigned char) *cptr))
500 static char *msg =
NULL;
501 static size_t msglen = 0;
506 if (quote && *cptr == quote)
515 msg = (
char *)
xrealloc (msg, clen);
524 if (quote && *cptr == quote)
528 if (*tmp && (!isspace ((
unsigned char) *tmp) || *
wskip (tmp)))
530 warning (cptr,
"unexpected quote character, ignoring");
548 error (
"premature end of file");
550 i = (int) (tptr - msg);
551 msg = (
char *)
xrealloc (msg, msglen);
587 if (quote && *cptr == quote)
591 else if (isdigit ((
unsigned char) *cptr))
594 for (i = 0; i < 3; ++
i)
596 if (!isdigit ((
unsigned char) *cptr))
599 warning (cptr,
"octal number greater than 7?!");
601 *tptr += (*cptr -
'0');
607 warning (cptr,
"unrecognized escape sequence");
637 if (strncmp (cptr,
"set", 3) == 0)
645 else if (strncmp (cptr,
"delset", 6) == 0)
652 else if (strncmp (cptr,
"quote", 5) == 0)
666 else if (isspace ((
unsigned char) *cptr))
676 warning (cptr,
"unrecognized line");
691 if (isdigit ((
unsigned char) *cptr))
707 warning (cptr,
"neither blank line nor start of a message id");
730 str =
getmsg (fd, cptr, quote);
750 n = read (fd, &cat_hdr,
sizeof (cat_hdr));
751 if (n < (
int)
sizeof (cat_hdr))
756 err (1,
"header read");
793 n = read (fd, msgcat, cat_hdr.
__mem);
794 if (n < cat_hdr.
__mem)
797 err (1,
"data read");
813 for (s = 0; s < cat_hdr.
__nsets; s++, set_hdr++)
842 for (m = 0; m < set_hdr->
__nmsgs; m++, msg_hdr++)
903 for (
set = sethead.lh_first;
set != NULL;
set = set->entries.le_next)
907 for (msg = set->msghead.lh_first; msg != NULL; msg = msg->entries.le_next)
915 printf (
"number of sets: %d\n", nsets);
916 printf (
"number of msgs: %d\n", nmsgs);
917 printf (
"string pool size: %d\n", string_size);
925 msgcat =
xmalloc (msgcat_size);
930 memset (msgcat,
'\0', msgcat_size);
950 for (
set = sethead.lh_first;
set != NULL;
set = set->entries.le_next)
954 for (msg = set->msghead.lh_first; msg != NULL; msg = msg->entries.le_next)
956 int msg_len = (int)
strlen (msg->
str) + 1;
962 memcpy (strings, msg->
str, msg_len);
964 msg_offset += msg_len;
978 write (fd, msgcat, msgcat_size);
989 error (
"setId's must be greater than zero");
994 error (
"setId exceeds limit");
998 p = sethead.lh_first;
1000 for (; p != NULL && p->
setId <
setId; q =
p, p = p->entries.le_next);
1002 if (p && p->setId == setId)
1008 p = (
struct _setT *)
xmalloc (
sizeof (
struct _setT));
1009 memset (p,
'\0',
sizeof (
struct _setT));
1010 LIST_INIT (&p->msghead);
1016 LIST_INSERT_HEAD (&sethead, p, entries);
1020 LIST_INSERT_AFTER (q, p, entries);
1033 error (
"can't specify a message when no set exists");
1037 error (
"msgId's must be greater than zero");
1042 error (
"msgID exceeds limit");
1046 p = curSet->msghead.lh_first;
1048 for (; p != NULL && p->
msgId <
msgId; q =
p, p = p->entries.le_next);
1050 if (p && p->msgId == msgId)
1057 memset (p,
'\0',
sizeof (
struct _msgT));
1061 LIST_INSERT_HEAD (&curSet->msghead, p, entries);
1065 LIST_INSERT_AFTER (q, p, entries);
1081 error (
"setId's must be greater than zero");
1086 error (
"setId exceeds limit");
1090 set = sethead.lh_first;
1091 for (;
set != NULL &&
set->setId < setId;
set =
set->entries.le_next);
1093 if (
set && set->setId == setId)
1095 LIST_REMOVE (
set, entries);
1096 while ((msg = set->msghead.lh_first) != NULL)
1098 LIST_REMOVE (msg, entries);
1105 warning (NULL,
"specified set doesn't exist");
1114 error (
"you can't delete a message before defining the set");
1116 msg = curSet->msghead.lh_first;
1117 for (; msg != NULL && msg->
msgId <
msgId; msg = msg->entries.le_next);
1119 if (msg && msg->
msgId == msgId)
1121 LIST_REMOVE (msg, entries);
1126 warning (NULL,
"specified msg doesn't exist");
int main(int argc, char *argv[])
static char * getmsg(int fd, char *cptr, char quote)
int getopt(int, char *const *, const char *)
unsigned int htonl(unsigned int from)
static const char * progname
static char * wskip(char *cptr)
static char * get_line(int fd)
LIST_HEAD(static void warning(const char *, _setT)
static void error(const char *msg)
static char * cskip(char *cptr)
static void * xrealloc(void *ptr, size_t size)
void MCAddMsg(int msgId, const char *str)
static void warning(const char *cptr, const char *msg)
char * strdup(const char *str)
static void * xmalloc(size_t len)
unsigned int ntohl(unsigned int from)
static char * xstrdup(const char *str)