⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xlcload.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * $XConsortium: XlcLoad.c,v 1.44 92/12/14 09:22:48 rws Exp $ *//* * Copyright 1990, 1991 by OMRON Corporation, NTT Software Corporation, *                      and Nippon Telegraph and Telephone Corporation * Copyright 1991 by the Massachusetts Institute of Technology * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, 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 the names of OMRON, NTT Software, NTT, and M.I.T. * not be used in advertising or publicity pertaining to distribution of the * software without specific, written prior permission. OMRON, NTT Software, * NTT, and M.I.T. make no representations about the suitability of this * software for any purpose.  It is provided "as is" without express or * implied warranty. * * OMRON, NTT SOFTWARE, NTT, AND M.I.T. DISCLAIM ALL WARRANTIES WITH REGARD * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS, IN NO EVENT SHALL OMRON, NTT SOFTWARE, NTT, OR M.I.T. 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. * *	Authors: Li Yuhong		OMRON Corporation *		 Tatsuya Kato		NTT Software Corporation *		 Hiroshi Kuribayashi	OMRON Corporation *    */#include <stdio.h>#include <ctype.h>#include "Xlibint.h"#include "Xi18nint.h"#include <X11/Xos.h>#if __STDC__ && !defined(NORCONST)#define RConst const#else#define RConst /**/#endif#ifdef X_NOT_STDC_ENVextern char *getenv();#endif#ifndef X_NOT_POSIX#ifdef _POSIX_SOURCE#include <limits.h>#else#define _POSIX_SOURCE#include <limits.h>#undef _POSIX_SOURCE#endif#endif#ifndef PATH_MAX#include <sys/param.h>#ifndef PATH_MAX#ifdef MAXPATHLEN#define PATH_MAX MAXPATHLEN#else#define PATH_MAX 1024#endif#endif#endif#ifndef XNLSPATHDEFAULT#define XNLSPATHDEFAULT "/usr/lib/X11/nls"#endif#define END_LINE    "END"#define MAXLINEBUF  1024#ifndef lintstatic int lock_tbl;#endifstatic XLocaleDB *_Xlctbl_;static char    *LoadLocaleName();static _CSID    WhichCS();static Codeset *LoadCodeset();static Bool     LoadCSMappingTable();static Bool     LoadCVMappingTable();static char    *LoadDesignateSequence();static Fontset *LoadFontList();static void	GetCTidToCSid();#define	COMMENTMARK	'#'static voidrmblank(buf)    char           *buf;{    char           *ptr;    int             len;    ptr = buf;    while (*ptr != '\0' && isspace(*ptr))	ptr++;    len = strlen(ptr);    bcopy(ptr, buf, len + 1);    ptr = buf + len;    while ((ptr > buf) && isspace(*(ptr - 1))) {	*--ptr = '\0';    }}#ifdef _FSTDIO	/* do not conflict with fgetline in bsd stdio */#define fgetline fgetlineX#endifstatic char *fgetline(buf, len, fp)    char       *buf;    int		len;    FILE       *fp;{    while (fgets(buf, len, fp)) {	rmblank(buf);	if ((*buf != '\0') && (*buf != COMMENTMARK)) {	    return buf;	}    }    return NULL;}static char *fgettoken(buf, len, fp, sep, strch, endch)    char       *buf;    int		len;    FILE       *fp;    char       *sep, strch, endch;{    static char	buffer[256];    static char	*ptr = NULL;    static int	start_flag = 0, end_flag = 0;    char	*last, *p;    while (1) {	if (ptr == NULL) {            if (end_flag != 0) {                start_flag = end_flag = 0;                return NULL;            }	    if (fgetline(buffer, len, fp) == NULL) {		return NULL;	    }            if (!strncmp(buffer, "END", 3) || !strncmp(buffer, "XLC", 3))                return NULL;	    ptr = buffer;	}	if (start_flag == 0) {	    if ((ptr = index(ptr, strch)) == NULL) {		continue;	    }	    ptr++;	    start_flag = strch;	}	if (p = index(ptr, endch)) {	    end_flag = endch;	    *p = '\0';	}	for (; *ptr != '\0'; ptr++) {	    if (!index(sep, *ptr)) {		break;	    }	}		if (*ptr == '\0') {	    ptr = NULL;	    if (end_flag != 0) {		start_flag = 0;		end_flag = 0;		return NULL;	    }	    continue;	}	for (last = ptr; *ptr != '\0'; ptr++) {	    if (index(sep, *ptr)) {		break;	    }	}	if (*ptr == '\0') {	    ptr = NULL;	}	else {	    *ptr++ = '\0';	}	strcpy(buf, last);	rmblank(buf);	if (*buf != '\0') {	    return buf;	}    }}static char *escape(str)    char       *str;{    char	buf[128],                *ptr,                *ret;    ptr = buf;    while (*str) {	switch (*str) {	case '\\':	    str++;	    if ((*str >= '0') && (*str <= '7')) {		int i, sum;		sum = 0;		for (i = 0; (i < 3) && *str; i++, str++) {		    if ((*str < '0') || (*str > '7')) {			break;		    }		    sum = sum * 8 + (*str - '0');		}		*ptr++ = (sum & 0xff);		continue;	    } else if (*str == 'e' || *str == 'E') {		*ptr++ = 0x1b;		continue;	    }	    /* through into default: */	default:	    *ptr++ = *str++;	    break;	}    }    *ptr = '\0';    if (ret = (char *) Xmalloc((unsigned)strlen(buf) + 1))	strcpy(ret, buf);    return ret;}static XLocaleDB *_XlcLoadLocale(file_name, errp)    char           *file_name;    int		   *errp;{    FILE           *fp;    char            buf[MAXLINEBUF];    XLocaleDB      *xlocale;     _XInitCTEncoding();          /* should done in system initial time. */    *errp = BadAccess;    if ((fp = fopen(file_name, "r")) == NULL)        return ((XLocaleDB *)NULL);    *errp = BadAlloc;    xlocale = (XLocaleDB *) Xmalloc(sizeof(XLocaleDB));    if (!xlocale) {	fclose(fp);	return ((XLocaleDB *)NULL);    }    while (fgetline(buf, MAXLINEBUF, fp) != NULL) {        if (!strncmp(buf, "XLC_ALL", 7)) {            if ((xlocale->lc_name = LoadLocaleName(fp)) == NULL)                return (NULL);        } else if (!strncmp(buf, "XLC_ENCODING", 12)) {            if ((xlocale->lc_encoding = LoadLocaleName(fp)) == NULL)                return (NULL);        } else if (!strncmp(buf, "XLC_CODESET", 11)) {            if ((xlocale->lc_codeset = LoadCodeset(fp)) == NULL)                return (NULL);        } else if (!strncmp(buf, "XLC_FONTSET", 11)) {            if ((xlocale->lc_fontset = LoadFontList(fp)) == NULL)                return (NULL);        } else {	    *errp = BadValue;            return (NULL);        }    }    GetCTidToCSid(xlocale->lc_fontset);    /*     * set locale name for each category.     */    fclose(fp);    *errp = 0;    return (xlocale);}static char *LoadLocaleName(fp)    FILE           *fp;{    char            buf[MAXLINEBUF];    char           *s;    if (fgetline(buf, MAXLINEBUF, fp) == NULL)        return (NULL);    if (s = Xmalloc((unsigned)strlen(buf) + 1)) {	(void) strcpy(s, buf);	if ((fgetline(buf, MAXLINEBUF, fp)) == NULL)	    return (NULL);    }    return (s);}static Codeset *LoadCodeset(fp)    FILE           *fp;{    char            buf[MAXLINEBUF],                   *ptr, *nptr;    int             num;    Codeset        *codeset;    int		    mb_cur_max = 0;    _CSID	    cds_GL, cds_GR;	#define MAGIC_NO 0xff    if (fgetline(buf, MAXLINEBUF, fp) == NULL) {        return (NULL);    }    codeset = (Codeset *) Xmalloc(sizeof(Codeset));    if (!codeset)	return ((Codeset *)NULL);    ptr = buf;    if (nptr = index(ptr, ':')) *nptr = '\0';    if ((*ptr == 'l') || (*ptr == 'L')) {	codeset->cds_type = CDS_STATELESS;	cds_GL = cds_GR = CODESET0;        LoadCSMappingTable(fp, codeset);    } else if ((*ptr == 'f') || (*ptr == 'F')) {	codeset->cds_type = CDS_STATEFUL;	cds_GL = cds_GR = MAGIC_NO;	/* not indicate */    } else {	codeset->cds_type = CDS_SELFDEFINED;	cds_GL = cds_GR = MAGIC_NO;	/* not indicate */    }    codeset->cds_mb_cur_max = 1;    for (num = 0; ptr = nptr; num++) {	ptr++;	if (nptr = index(ptr, ':')) *nptr = '\0';	mb_cur_max =        codeset->cds_mblen[num] = atoi(ptr);        if (codeset->cds_type == CDS_STATEFUL) {            codeset->cds_dsg[num] = LoadDesignateSequence(fp);	    mb_cur_max += strlen(codeset->cds_dsg[num]);	    /* set inital designate */	    if (_XctisGLdsg(codeset->cds_dsg[num])) {		if (cds_GL == MAGIC_NO)		    cds_GL = num;	    } else if (cds_GR == MAGIC_NO) {		cds_GR = num;	    }        }	codeset->cds_mb_cur_max = max(codeset->cds_mb_cur_max, mb_cur_max);    }    if (cds_GR == MAGIC_NO)	cds_GR = cds_GL;    codeset->mb_init = (GL << 16) | cds_GR << 8 | cds_GL;    codeset->cds_num = num;    LoadCVMappingTable(fp, num, codeset);    while (fgetline(buf, MAXLINEBUF, fp)) { /* read trailing garbage */    }    return codeset;#undef MAGIC_NO}static          BoolLoadCSMappingTable(fp, codeset)    FILE           *fp;    Codeset        *codeset;{    char            buf[MAXLINEBUF];    int             count = 0;    while (fgettoken(buf, MAXLINEBUF, fp, ", ", '{', '}')) {        codeset->cds_map[count++] = WhichCS(buf);    }    if (count < 256) {        return False;    }    return True;}#define MAXSPLITS   8096    /* should be max. 96 x 96 */static          BoolLoadCVMappingTable(fp, num_cs, ccs)    FILE           *fp;    int            num_cs;    Codeset        *ccs;{    char            buf[MAXLINEBUF];    Range          *cnv;    int             num = 0,                    i, lastnum;    if ( ! ( cnv = (Range *)Xmalloc(MAXSPLITS*sizeof(Range))))	return False;    ccs->cds_msbon = 0;     /* use msb on */    for (i = 1, ccs->cds_cnvindex[0] = 0; i <= num_cs; i++) {        lastnum = num;        while (fgettoken(buf, MAXLINEBUF, fp, ", ", '(', ')')) {            sscanf(buf, "%x=%x:%x", &(cnv[num].mb_start),                    &(cnv[num].cs_start), &(cnv[num].cs_end));            cnv[num].mb_end = cnv[num].mb_start +                              cnv[num].cs_end - cnv[num].cs_start;	    if (cnv[num].mb_start & 0x80808080 || cnv[num].mb_end & 0x80808080)		ccs->cds_msbon = 1;     /* use msb on */            num++;        }        if (lastnum == num) /* no more conversion element */            break;        ccs->cds_cnvindex[i] = num;    }    /*     * Notes: the number of charsets >= number of fonts     *        No font, no conversion list, e.g, charset #3 of UJIS.     *     * The last one [num_cs] of index is guider.     */    for (; i <= num_cs; i++)        ccs->cds_cnvindex[i] = num;    /* set which half (GL/GR) of font should be used. */    for (i = 0; i < num_cs; i++) {	ccs->cs_offset[i] = cnv[ccs->cds_cnvindex[i]].cs_start & 0x8080;    }    ccs->cds_cnvlist = (Range *) Xmalloc((unsigned)sizeof(Range) * num);    if (!ccs->cds_cnvlist) {	Xfree((char *)cnv);	return False;    }#ifdef  FASTCOPY    /* not used */    (void) bcopy((char *)cnv, (char *)ccs->cds_cnvlist,                 sizeof(Range) * num);#else    for (i = 0; i < num; i++) {        ccs->cds_cnvlist[i].mb_start = cnv[i].mb_start;        ccs->cds_cnvlist[i].mb_end = cnv[i].mb_end;        ccs->cds_cnvlist[i].cs_start = cnv[i].cs_start;        ccs->cds_cnvlist[i].cs_end = cnv[i].cs_end;    }#endif    Xfree((char *)cnv);    return True;}static char *LoadDesignateSequence(fp)    FILE       *fp;{    char      	buf[MAXLINEBUF];    if (fgetline(buf, MAXLINEBUF, fp) == NULL) {        return False;    }    return escape(buf);}static _CSIDWhichCS(s)    char       *s;{    _CSID      	n;    if (!strncmp(s, "CS0", 3)) return CODESET0;    if (!strncmp(s, "CS1", 3)) return CODESET1;    if (!strncmp(s, "CS2", 3)) return CODESET2;    if (!strncmp(s, "CS3", 3)) return CODESET3;    if (!strncmp(s, "CS4", 3)) return CODESET4;    if (!strncmp(s, "CS5", 3)) return CODESET5;    if (!strncmp(s, "CS6", 3)) return CODESET6;    if (!strncmp(s, "CS7", 3)) return CODESET7;    if (!strncmp(s, "CS8", 3)) return CODESET8;    if (!strncmp(s, "CS9", 3)) return CODESET9;    if (!strncmp(s, "CSA", 3)) return CODESETA;    if (!strncmp(s, "CSB", 3)) return CODESETB;    if (!strncmp(s, "CSC", 3)) return CODESETC;    if (!strncmp(s, "CSD", 3)) return CODESETD;    if (!strncmp(s, "CSE", 3)) return CODESETE;    if (!strncmp(s, "CSF", 3)) return CODESETF;    if (!strncmp(s, "C0", 2)) return C0;    if (!strncmp(s, "C1", 2)) return C1;    if (!strncmp(s, "ND", 2)) return ND;    if (!strncmp(s, "-", 1)) {        s++;        n = '0' - *s;        return n;    }    return ND;}static BoolLoadOneCharset(str, cset)    char       *str;    Charset    *cset;{    wchar	woffset;    char       *esc;    char       *nstr;    extern Bool _XRegisterCharSet();    if (nstr = index(str, ':')) *nstr = '\0';    cset->cs_name = (char *) Xmalloc((unsigned)strlen(str) + 1);    if (!cset->cs_name)	return (False);    strcpy(cset->cs_name, str);    if (!(str = nstr))        return False;    str++;    if (nstr = index(str, ':')) *nstr = '\0';    cset->cs_GLorGR = (strcmp(str, "GL") == 0) ? GL : GR;    if (!nstr) {        /*         * standard CharsetRegistry-CharsetEncoding registered by X,         */        if (_XcwNameGetAll(cset->cs_name, &esc, &woffset, cset->cs_GLorGR) == False)            return False;        cset->cs_des = strcpy(Xmalloc((unsigned)strlen(esc) + 1), esc);	if (!cset->cs_des)	    return (False);        cset->cs_woff = woffset;        /*         * only 2 or 1 bytes font encoding in X.         */        cset->cs_len = (cset->cs_des[1] == '$') ? 2 : 1;        return True;    }    /*     * The form: CharsetRegistry-CharsetEncoding:woffset:escapeseq     */    if (!(str = nstr))        return False;    str++;    if (nstr = index(str, ':')) *nstr = '\0';

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -