📄 genlib.c
字号:
/* -*-c++-*- *//***********************************************************Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts. All Rights ReservedPermission to use, copy, modify, and distribute this software and itsdocumentation for any purpose and without fee is hereby granted,provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear insupporting documentation, and that Alfalfa's name not be used inadvertising or publicity pertaining to distribution of the softwarewithout specific, written prior permission.ALPHALPHA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDINGALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALLALPHALPHA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES ORANY 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 THISSOFTWARE.If you make any modifications, bugfixes or other changes to this softwarewe'd appreciate it if you could send a copy to us so we can keep thingsup-to-date. Many thanks. Kee Hinckley Alfalfa Software, Inc. 267 Allston St., #3 Cambridge, MA 02139 USA nazgul@alfalfa.com$FreeBSD: src/usr.bin/gencat/genlib.c,v 1.8.6.1 2000/07/15 17:19:28 ache Exp $******************************************************************//* Edit History02/25/91 5 nazgul Added flag for MS byteorder01/14/91 4 nazgul Off by one on number specified entries*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <unistd.h>#ifdef SYSV#define L_SET SEEK_SET#define L_INCR SEEK_CUR#include <memory.h>static int bcopy(src, dst, length)char *src, *dst;int length;{ memcpy(dst, src, length);}static int bzero(b, length)char *b;int length;{ memset(b, '\0', length);}#endif#include <sys/file.h>#include <ctype.h>#include <err.h>#include "msgcat.h"#include "gencat.h"static char *curline = NULL;static long lineno = 0;static void warning(cptr, msg)char *cptr;char *msg;{ warnx("%s on line %ld\n%s", msg, lineno, curline); if (cptr) { char *tptr; for (tptr = curline; tptr < cptr; ++tptr) putc(' ', stderr); fprintf(stderr, "^\n"); }}static void error(cptr, msg)char *cptr;char *msg;{ warning(cptr, msg); exit(1);}static void corrupt() { error(NULL, "corrupt message catalog");}static void nomem() { error(NULL, "out of memory");}static char *getline(fd)int fd;{ static long curlen = BUFSIZ; static char buf[BUFSIZ], *bptr = buf, *bend = buf; char *cptr, *cend; long buflen; if (!curline) { curline = (char *) malloc(curlen); if (!curline) nomem(); } ++lineno; cptr = curline; cend = curline + curlen; while (True) { 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; } }}static char *token(cptr)char *cptr;{ static char tok[MAXTOKEN+1]; char *tptr = tok; while (*cptr && isspace((unsigned char)*cptr)) ++cptr; while (*cptr && !isspace((unsigned char)*cptr)) *tptr++ = *cptr++; *tptr = '\0'; return(tok);}static char *wskip(cptr)char *cptr;{ if (!*cptr || !isspace((unsigned char)*cptr)) { warning(cptr, "expected a space"); return(cptr); } while (*cptr && isspace((unsigned char)*cptr)) ++cptr; return(cptr);}static char *cskip(cptr)char *cptr;{ if (!*cptr || isspace((unsigned char)*cptr)) { warning(cptr, "wasn't expecting a space"); return(cptr); } while (*cptr && !isspace((unsigned char)*cptr)) ++cptr; return(cptr);}static char *getmsg(fd, cptr, quote)int fd;char *cptr;char quote;{ static char *msg = NULL; static long msglen = 0; long clen, i; char *tptr; int needq; if (quote && *cptr == quote) { needq = True; ++cptr; } else needq = False; clen = strlen(cptr) + 1; if (clen > msglen) { if (msglen) msg = (char *) realloc(msg, clen); else msg = (char *) malloc(clen); msglen = clen; } tptr = msg; while (*cptr) { if (quote && *cptr == quote) { char *tmp; tmp = cptr+1; if (*tmp && (!isspace((unsigned char)*tmp) || *wskip(tmp))) { warning(cptr, "unexpected quote character, ignoreing"); *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; case 'n': *tptr++ = '\n'; ++cptr; break; case 't': *tptr++ = '\t'; ++cptr; break; case 'v': *tptr++ = '\v'; ++cptr; break; case 'b': *tptr++ = '\b'; ++cptr; break; case 'r': *tptr++ = '\r'; ++cptr; break; case 'f': *tptr++ = '\f'; ++cptr; break; case '"': *tptr++ = '"'; ++cptr; break; case '\'': *tptr++ = '\''; ++cptr; break; case '\\': *tptr++ = '\\'; ++cptr; break; default: if (isdigit((unsigned char)*cptr)) { *tptr = 0; for (i = 0; i < 3; ++i) { if (!isdigit((unsigned char)*cptr)) break; if (*cptr > '7') warning(cptr, "octal number greater than 7?!"); *tptr *= 8; *tptr += (*cptr - '0'); ++cptr; } ++tptr; } else { warning(cptr, "unrecognized escape sequence"); } } } else { *tptr++ = *cptr++; } } *tptr = '\0'; return(msg);}static char *dupstr(ostr)char *ostr;{ char *nstr; nstr = (char *) malloc(strlen(ostr) + 1); if (!nstr) error(NULL, "unable to allocate storage"); strcpy(nstr, ostr); return(nstr);}/* * The Global Stuff */typedef struct _msgT { long msgId; char *str; char *hconst; long offset; struct _msgT *prev, *next;} msgT;typedef struct _setT { long setId; char *hconst; msgT *first, *last; struct _setT *prev, *next;} setT;typedef struct { setT *first, *last;} catT;static setT *curSet;static catT *cat;/* * 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". */long MCGetByteOrder() { long l = 0x00010203; char *cptr = (char *) &l; if (cptr[0] == 0 && cptr[1] == 1 && cptr[2] == 2 && cptr[3] == 3) return MC68KByteOrder; else return MCn86ByteOrder;}void MCParse(#if PROTO int fd)#else fd)int fd;#endif{ char *cptr, *str; int setid, msgid = 0; char hconst[MAXTOKEN+1]; char quote = 0; if (!cat) { cat = (catT *) malloc(sizeof(catT)); if (!cat) nomem(); bzero(cat, sizeof(catT)); } hconst[0] = '\0'; while (cptr = getline(fd)) { 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; } } else if (isspace((unsigned char)*cptr)) { cptr = wskip(cptr); if (*cptr == '#') { ++cptr; strcpy(hconst, token(cptr)); } } else { if (*cptr) { cptr = wskip(cptr); if (*cptr) warning(cptr, "unrecognized line"); } } } else { if (isdigit((unsigned char)*cptr) || *cptr == '#') { if (*cptr == '#') { ++msgid; ++cptr; if (!*cptr) { MCAddMsg(msgid, "", hconst); hconst[0] = '\0'; continue; } if (!isspace((unsigned char)*cptr)) warning(cptr, "expected a space"); ++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'; } } } }}void MCReadCat(#if PROTO int fd)#else fd)int fd;#endif{ MCHeaderT mcHead; MCMsgT mcMsg; MCSetT mcSet; msgT *msg; setT *set; int i; char *data; cat = (catT *) malloc(sizeof(catT)); if (!cat) nomem(); bzero(cat, sizeof(catT));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -