imLcPrs.c revision b4ee4795
11ab64890Smrg/******************************************************************
21ab64890Smrg
31ab64890Smrg              Copyright 1992 by Oki Technosystems Laboratory, Inc.
41ab64890Smrg              Copyright 1992 by Fuji Xerox Co., Ltd.
51ab64890Smrg
61ab64890SmrgPermission to use, copy, modify, distribute, and sell this software
71ab64890Smrgand its documentation for any purpose is hereby granted without fee,
81ab64890Smrgprovided that the above copyright notice appear in all copies and
91ab64890Smrgthat both that copyright notice and this permission notice appear
101ab64890Smrgin supporting documentation, and that the name of Oki Technosystems
111ab64890SmrgLaboratory and Fuji Xerox not be used in advertising or publicity
121ab64890Smrgpertaining to distribution of the software without specific, written
131ab64890Smrgprior permission.
141ab64890SmrgOki Technosystems Laboratory and Fuji Xerox make no representations
151ab64890Smrgabout the suitability of this software for any purpose.  It is provided
161ab64890Smrg"as is" without express or implied warranty.
171ab64890Smrg
181ab64890SmrgOKI TECHNOSYSTEMS LABORATORY AND FUJI XEROX DISCLAIM ALL WARRANTIES
191ab64890SmrgWITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
201ab64890SmrgMERCHANTABILITY AND FITNESS, IN NO EVENT SHALL OKI TECHNOSYSTEMS
211ab64890SmrgLABORATORY AND FUJI XEROX BE LIABLE FOR ANY SPECIAL, INDIRECT OR
221ab64890SmrgCONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
231ab64890SmrgOF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
241ab64890SmrgOR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
251ab64890SmrgOR PERFORMANCE OF THIS SOFTWARE.
261ab64890Smrg
271ab64890Smrg  Author: Yasuhiro Kawai	Oki Technosystems Laboratory
281ab64890Smrg  Author: Kazunori Nishihara	Fuji Xerox
291ab64890Smrg
301ab64890Smrg******************************************************************/
311ab64890Smrg
321ab64890Smrg
331ab64890Smrg#ifdef HAVE_CONFIG_H
341ab64890Smrg#include <config.h>
351ab64890Smrg#endif
361ab64890Smrg#include <X11/Xlib.h>
371ab64890Smrg#include <X11/Xmd.h>
381ab64890Smrg#include <X11/Xos.h>
391ab64890Smrg#include "Xlibint.h"
401ab64890Smrg#include "Xlcint.h"
411ab64890Smrg#include "Ximint.h"
421ab64890Smrg#include <sys/stat.h>
431ab64890Smrg#include <stdio.h>
441ab64890Smrg
45b4ee4795Smrg#define XLC_BUFSIZE 256
46b4ee4795Smrg
47b4ee4795Smrgextern void xlocaledir(
48b4ee4795Smrg    char *buf,
49b4ee4795Smrg    int buf_len
50b4ee4795Smrg);
51b4ee4795Smrg
521ab64890Smrgextern int _Xmbstowcs(
531ab64890Smrg    wchar_t	*wstr,
541ab64890Smrg    char	*str,
551ab64890Smrg    int		len
561ab64890Smrg);
571ab64890Smrg
581ab64890Smrgextern int _Xmbstoutf8(
591ab64890Smrg    char	*ustr,
601ab64890Smrg    const char	*str,
611ab64890Smrg    int		len
621ab64890Smrg);
631ab64890Smrg
641ab64890Smrg/*
651ab64890Smrg *	Parsing File Format:
661ab64890Smrg *
671ab64890Smrg *	FILE          ::= { [PRODUCTION] [COMMENT] "\n"}
681ab64890Smrg *	PRODUCTION    ::= LHS ":" RHS [ COMMENT ]
691ab64890Smrg *	COMMENT       ::= "#" {<any character except null or newline>}
701ab64890Smrg *	LHS           ::= EVENT { EVENT }
711ab64890Smrg *	EVENT         ::= [MODIFIER_LIST] "<" keysym ">"
721ab64890Smrg *	MODIFIER_LIST ::= ("!" {MODIFIER} ) | "None"
731ab64890Smrg *	MODIFIER      ::= ["~"] modifier_name
741ab64890Smrg *	RHS           ::= ( STRING | keysym | STRING keysym )
751ab64890Smrg *	STRING        ::= '"' { CHAR } '"'
761ab64890Smrg *	CHAR          ::= GRAPHIC_CHAR | ESCAPED_CHAR
771ab64890Smrg *	GRAPHIC_CHAR  ::= locale (codeset) dependent code
781ab64890Smrg *	ESCAPED_CHAR  ::= ('\\' | '\"' | OCTAL | HEX )
791ab64890Smrg *	OCTAL         ::= '\' OCTAL_CHAR [OCTAL_CHAR [OCTAL_CHAR]]
801ab64890Smrg *	OCTAL_CHAR    ::= (0|1|2|3|4|5|6|7)
811ab64890Smrg *	HEX           ::= '\' (x|X) HEX_CHAR [HEX_CHAR]]
821ab64890Smrg *	HEX_CHAR      ::= (0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|a|b|c|d|e|f)
831ab64890Smrg *
841ab64890Smrg */
851ab64890Smrg
861ab64890Smrgstatic int
871ab64890Smrgnextch(
881ab64890Smrg    FILE *fp,
891ab64890Smrg    int *lastch)
901ab64890Smrg{
911ab64890Smrg    int c;
921ab64890Smrg
931ab64890Smrg    if (*lastch != 0) {
941ab64890Smrg	c = *lastch;
951ab64890Smrg	*lastch = 0;
961ab64890Smrg    } else {
971ab64890Smrg	c = getc(fp);
981ab64890Smrg	if (c == '\\') {
991ab64890Smrg	    c = getc(fp);
1001ab64890Smrg	    if (c == '\n') {
1011ab64890Smrg		c = getc(fp);
1021ab64890Smrg	    } else {
1031ab64890Smrg		ungetc(c, fp);
1041ab64890Smrg		c = '\\';
1051ab64890Smrg	    }
1061ab64890Smrg	}
1071ab64890Smrg    }
1081ab64890Smrg    return(c);
1091ab64890Smrg}
1101ab64890Smrg
1111ab64890Smrgstatic void
1121ab64890Smrgputbackch(
1131ab64890Smrg    int c,
1141ab64890Smrg    int *lastch)
1151ab64890Smrg{
1161ab64890Smrg    *lastch = c;
1171ab64890Smrg}
1181ab64890Smrg
1191ab64890Smrg#define ENDOFFILE 0
1201ab64890Smrg#define ENDOFLINE 1
1211ab64890Smrg#define COLON 2
1221ab64890Smrg#define LESS 3
1231ab64890Smrg#define GREATER 4
1241ab64890Smrg#define EXCLAM 5
1251ab64890Smrg#define TILDE 6
1261ab64890Smrg#define STRING 7
1271ab64890Smrg#define KEY 8
1281ab64890Smrg#define ERROR 9
1291ab64890Smrg
1301ab64890Smrg#ifndef isalnum
1311ab64890Smrg#define isalnum(c)      \
1321ab64890Smrg    (('0' <= (c) && (c) <= '9')  || \
1331ab64890Smrg     ('A' <= (c) && (c) <= 'Z')  || \
1341ab64890Smrg     ('a' <= (c) && (c) <= 'z'))
1351ab64890Smrg#endif
1361ab64890Smrg
1371ab64890Smrgstatic int
1381ab64890Smrgnexttoken(
1391ab64890Smrg    FILE *fp,
1401ab64890Smrg    char *tokenbuf,
1411ab64890Smrg    int *lastch)
1421ab64890Smrg{
1431ab64890Smrg    int c;
1441ab64890Smrg    int token;
1451ab64890Smrg    char *p;
1461ab64890Smrg    int i, j;
1471ab64890Smrg
1481ab64890Smrg    while ((c = nextch(fp, lastch)) == ' ' || c == '\t') {
1491ab64890Smrg    }
1501ab64890Smrg    switch (c) {
1511ab64890Smrg      case EOF:
1521ab64890Smrg	token = ENDOFFILE;
1531ab64890Smrg	break;
1541ab64890Smrg      case '\n':
1551ab64890Smrg	token = ENDOFLINE;
1561ab64890Smrg	break;
1571ab64890Smrg      case '<':
1581ab64890Smrg	token = LESS;
1591ab64890Smrg	break;
1601ab64890Smrg      case '>':
1611ab64890Smrg	token = GREATER;
1621ab64890Smrg	break;
1631ab64890Smrg      case ':':
1641ab64890Smrg	token = COLON;
1651ab64890Smrg	break;
1661ab64890Smrg      case '!':
1671ab64890Smrg	token = EXCLAM;
1681ab64890Smrg	break;
1691ab64890Smrg      case '~':
1701ab64890Smrg	token = TILDE;
1711ab64890Smrg	break;
1721ab64890Smrg      case '"':
1731ab64890Smrg	p = tokenbuf;
1741ab64890Smrg	while ((c = nextch(fp, lastch)) != '"') {
1751ab64890Smrg	    if (c == '\n' || c == EOF) {
1761ab64890Smrg		putbackch(c, lastch);
1771ab64890Smrg		token = ERROR;
1781ab64890Smrg		goto string_error;
1791ab64890Smrg	    } else if (c == '\\') {
1801ab64890Smrg		c = nextch(fp, lastch);
1811ab64890Smrg		switch (c) {
1821ab64890Smrg		  case '\\':
1831ab64890Smrg		  case '"':
1841ab64890Smrg		    *p++ = c;
1851ab64890Smrg		    break;
1861ab64890Smrg		  case 'n':
1871ab64890Smrg		    *p++ = '\n';
1881ab64890Smrg		    break;
1891ab64890Smrg		  case 'r':
1901ab64890Smrg		    *p++ = '\r';
1911ab64890Smrg		    break;
1921ab64890Smrg		  case 't':
1931ab64890Smrg		    *p++ = '\t';
1941ab64890Smrg		    break;
1951ab64890Smrg		  case '0':
1961ab64890Smrg		  case '1':
1971ab64890Smrg		  case '2':
1981ab64890Smrg		  case '3':
1991ab64890Smrg		  case '4':
2001ab64890Smrg		  case '5':
2011ab64890Smrg		  case '6':
2021ab64890Smrg		  case '7':
2031ab64890Smrg		    i = c - '0';
2041ab64890Smrg		    c = nextch(fp, lastch);
2051ab64890Smrg		    for (j = 0; j < 2 && c >= '0' && c <= '7'; j++) {
2061ab64890Smrg			i <<= 3;
2071ab64890Smrg			i += c - '0';
2081ab64890Smrg			c = nextch(fp, lastch);
2091ab64890Smrg		    }
2101ab64890Smrg		    putbackch(c, lastch);
2111ab64890Smrg		    *p++ = (char)i;
2121ab64890Smrg		    break;
2131ab64890Smrg		  case 'X':
2141ab64890Smrg		  case 'x':
2151ab64890Smrg		    i = 0;
2161ab64890Smrg		    for (j = 0; j < 2; j++) {
2171ab64890Smrg			c = nextch(fp, lastch);
2181ab64890Smrg 			i <<= 4;
2191ab64890Smrg			if (c >= '0' && c <= '9') {
2201ab64890Smrg			    i += c - '0';
2211ab64890Smrg			} else if (c >= 'A' && c <= 'F') {
2221ab64890Smrg			    i += c - 'A' + 10;
2231ab64890Smrg			} else if (c >= 'a' && c <= 'f') {
2241ab64890Smrg			    i += c - 'a' + 10;
2251ab64890Smrg			} else {
2261ab64890Smrg			    putbackch(c, lastch);
2271ab64890Smrg			    i >>= 4;
2281ab64890Smrg			    break;
2291ab64890Smrg		        }
2301ab64890Smrg		    }
2311ab64890Smrg		    if (j == 0) {
2321ab64890Smrg		        token = ERROR;
2331ab64890Smrg		        goto string_error;
2341ab64890Smrg		    }
2351ab64890Smrg		    *p++ = (char)i;
2361ab64890Smrg		    break;
2371ab64890Smrg		  case EOF:
2381ab64890Smrg		    putbackch(c, lastch);
2391ab64890Smrg		    token = ERROR;
2401ab64890Smrg		    goto string_error;
2411ab64890Smrg		  default:
2421ab64890Smrg		    *p++ = c;
2431ab64890Smrg		    break;
2441ab64890Smrg		}
2451ab64890Smrg	    } else {
2461ab64890Smrg		*p++ = c;
2471ab64890Smrg	    }
2481ab64890Smrg	}
2491ab64890Smrg	*p = '\0';
2501ab64890Smrg	token = STRING;
2511ab64890Smrg	break;
2521ab64890Smrg      case '#':
2531ab64890Smrg	while ((c = nextch(fp, lastch)) != '\n' && c != EOF) {
2541ab64890Smrg	}
2551ab64890Smrg	if (c == '\n') {
2561ab64890Smrg	    token = ENDOFLINE;
2571ab64890Smrg	} else {
2581ab64890Smrg	    token = ENDOFFILE;
2591ab64890Smrg	}
2601ab64890Smrg	break;
2611ab64890Smrg      default:
2621ab64890Smrg	if (isalnum(c) || c == '_' || c == '-') {
2631ab64890Smrg	    p = tokenbuf;
2641ab64890Smrg	    *p++ = c;
2651ab64890Smrg	    c = nextch(fp, lastch);
2661ab64890Smrg	    while (isalnum(c) || c == '_' || c == '-') {
2671ab64890Smrg		*p++ = c;
2681ab64890Smrg		c = nextch(fp, lastch);
2691ab64890Smrg	    }
2701ab64890Smrg	    *p = '\0';
2711ab64890Smrg	    putbackch(c, lastch);
2721ab64890Smrg	    token = KEY;
2731ab64890Smrg	} else {
2741ab64890Smrg	    token = ERROR;
2751ab64890Smrg	}
2761ab64890Smrg	break;
2771ab64890Smrg    }
2781ab64890Smrgstring_error:
2791ab64890Smrg    return(token);
2801ab64890Smrg}
2811ab64890Smrg
2821ab64890Smrgstatic long
2831ab64890Smrgmodmask(
2841ab64890Smrg    char *name)
2851ab64890Smrg{
2861ab64890Smrg    struct _modtbl {
2871ab64890Smrg	const char name[6];
2881ab64890Smrg	long mask;
2891ab64890Smrg    };
2901ab64890Smrg
2911ab64890Smrg    static const struct _modtbl tbl[] = {
2921ab64890Smrg	{ "Ctrl",	ControlMask	},
2931ab64890Smrg        { "Lock",	LockMask	},
2941ab64890Smrg        { "Caps",	LockMask	},
2951ab64890Smrg        { "Shift",	ShiftMask	},
2961ab64890Smrg        { "Alt",	Mod1Mask	},
2971ab64890Smrg        { "Meta",	Mod1Mask	}};
2981ab64890Smrg
2991ab64890Smrg    int i, num_entries = sizeof (tbl) / sizeof (tbl[0]);
3001ab64890Smrg
3011ab64890Smrg    for (i = 0; i < num_entries; i++)
3021ab64890Smrg        if (!strcmp (name, tbl[i].name))
3031ab64890Smrg            return tbl[i].mask;
3041ab64890Smrg
3051ab64890Smrg    return 0;
3061ab64890Smrg}
3071ab64890Smrg
3081ab64890Smrgstatic char*
3091ab64890SmrgTransFileName(Xim im, char *name)
3101ab64890Smrg{
3111ab64890Smrg   char *home = NULL, *lcCompose = NULL;
312b4ee4795Smrg   char dir[XLC_BUFSIZE];
3131ab64890Smrg   char *i = name, *ret, *j;
3141ab64890Smrg   int l = 0;
3151ab64890Smrg
3161ab64890Smrg   while (*i) {
3171ab64890Smrg      if (*i == '%') {
3181ab64890Smrg   	  i++;
3191ab64890Smrg   	  switch (*i) {
3201ab64890Smrg   	      case '%':
3211ab64890Smrg                 l++;
3221ab64890Smrg   	         break;
3231ab64890Smrg   	      case 'H':
3241ab64890Smrg   	         home = getenv("HOME");
3251ab64890Smrg   	         if (home)
3261ab64890Smrg                     l += strlen(home);
3271ab64890Smrg   	         break;
3281ab64890Smrg   	      case 'L':
3291ab64890Smrg                 lcCompose = _XlcFileName(im->core.lcd, COMPOSE_FILE);
3301ab64890Smrg                 if (lcCompose)
3311ab64890Smrg                     l += strlen(lcCompose);
3321ab64890Smrg   	         break;
333b4ee4795Smrg   	      case 'S':
334b4ee4795Smrg                 xlocaledir(dir, XLC_BUFSIZE);
335b4ee4795Smrg                 l += strlen(dir);
336b4ee4795Smrg   	         break;
3371ab64890Smrg   	  }
3381ab64890Smrg      } else {
3391ab64890Smrg      	  l++;
3401ab64890Smrg      }
3411ab64890Smrg      i++;
3421ab64890Smrg   }
3431ab64890Smrg
3441ab64890Smrg   j = ret = Xmalloc(l+1);
3451ab64890Smrg   if (ret == NULL)
3461ab64890Smrg      return ret;
3471ab64890Smrg   i = name;
3481ab64890Smrg   while (*i) {
3491ab64890Smrg      if (*i == '%') {
3501ab64890Smrg   	  i++;
3511ab64890Smrg   	  switch (*i) {
3521ab64890Smrg   	      case '%':
3531ab64890Smrg                 *j++ = '%';
3541ab64890Smrg   	         break;
3551ab64890Smrg   	      case 'H':
3561ab64890Smrg   	         if (home) {
3571ab64890Smrg   	             strcpy(j, home);
3581ab64890Smrg   	             j += strlen(home);
3591ab64890Smrg   	         }
3601ab64890Smrg   	         break;
3611ab64890Smrg   	      case 'L':
3621ab64890Smrg   	         if (lcCompose) {
3631ab64890Smrg                    strcpy(j, lcCompose);
3641ab64890Smrg                    j += strlen(lcCompose);
3651ab64890Smrg                    Xfree(lcCompose);
3661ab64890Smrg                 }
3671ab64890Smrg   	         break;
368b4ee4795Smrg   	      case 'S':
369b4ee4795Smrg                 strcpy(j, dir);
370b4ee4795Smrg                 j += strlen(dir);
371b4ee4795Smrg   	         break;
3721ab64890Smrg   	  }
3731ab64890Smrg          i++;
3741ab64890Smrg      } else {
3751ab64890Smrg      	  *j++ = *i++;
3761ab64890Smrg      }
3771ab64890Smrg   }
3781ab64890Smrg   *j = '\0';
3791ab64890Smrg   return ret;
3801ab64890Smrg}
3811ab64890Smrg
3821ab64890Smrg#ifndef MB_LEN_MAX
3831ab64890Smrg#define MB_LEN_MAX 6
3841ab64890Smrg#endif
3851ab64890Smrg
3861ab64890Smrgstatic int
3871ab64890Smrgget_mb_string (Xim im, char *buf, KeySym ks)
3881ab64890Smrg{
3891ab64890Smrg    XPointer from, to;
3901ab64890Smrg    int from_len, to_len, len;
3911ab64890Smrg    XPointer args[1];
3921ab64890Smrg    XlcCharSet charset;
3931ab64890Smrg    char local_buf[MB_LEN_MAX];
3941ab64890Smrg    unsigned int ucs;
3951ab64890Smrg    ucs = KeySymToUcs4(ks);
3961ab64890Smrg
3971ab64890Smrg    from = (XPointer) &ucs;
3981ab64890Smrg    to =   (XPointer) local_buf;
3991ab64890Smrg    from_len = 1;
4001ab64890Smrg    to_len = MB_LEN_MAX;
4011ab64890Smrg    args[0] = (XPointer) &charset;
4021ab64890Smrg    if (_XlcConvert(im->private.local.ucstoc_conv,
4031ab64890Smrg                    &from, &from_len, &to, &to_len, args, 1 ) != 0) {
4041ab64890Smrg         return 0;
4051ab64890Smrg    }
4061ab64890Smrg
4071ab64890Smrg    from = (XPointer) local_buf;
4081ab64890Smrg    to =   (XPointer) buf;
4091ab64890Smrg    from_len = MB_LEN_MAX - to_len;
4101ab64890Smrg    to_len = MB_LEN_MAX + 1;
4111ab64890Smrg    args[0] = (XPointer) charset;
4121ab64890Smrg    if (_XlcConvert(im->private.local.cstomb_conv,
4131ab64890Smrg                    &from, &from_len, &to, &to_len, args, 1 ) != 0) {
4141ab64890Smrg         return 0;
4151ab64890Smrg    }
4161ab64890Smrg    len = MB_LEN_MAX + 1 - to_len;
4171ab64890Smrg    buf[len] = '\0';
4181ab64890Smrg    return len;
4191ab64890Smrg}
4201ab64890Smrg
42161b2299dSmrg#define AllMask (ShiftMask | LockMask | ControlMask | Mod1Mask)
4221ab64890Smrg#define LOCAL_WC_BUFSIZE 128
4231ab64890Smrg#define LOCAL_UTF8_BUFSIZE 256
4241ab64890Smrg#define SEQUENCE_MAX	10
4251ab64890Smrg
4261ab64890Smrgstatic int
4271ab64890Smrgparseline(
4281ab64890Smrg    FILE *fp,
4291ab64890Smrg    Xim   im,
4301ab64890Smrg    char* tokenbuf)
4311ab64890Smrg{
4321ab64890Smrg    int token;
4331ab64890Smrg    DTModifier modifier_mask;
4341ab64890Smrg    DTModifier modifier;
4351ab64890Smrg    DTModifier tmp;
4361ab64890Smrg    KeySym keysym = NoSymbol;
4371ab64890Smrg    DTIndex *top = &im->private.local.top;
4381ab64890Smrg    DefTreeBase *b   = &im->private.local.base;
4391ab64890Smrg    DTIndex t;
4401ab64890Smrg    DefTree *p = NULL;
4411ab64890Smrg    Bool exclam, tilde;
4421ab64890Smrg    KeySym rhs_keysym = 0;
4431ab64890Smrg    char *rhs_string_mb;
4441ab64890Smrg    int l;
4451ab64890Smrg    int lastch = 0;
4461ab64890Smrg    char local_mb_buf[MB_LEN_MAX+1];
4471ab64890Smrg    wchar_t local_wc_buf[LOCAL_WC_BUFSIZE], *rhs_string_wc;
4481ab64890Smrg    char local_utf8_buf[LOCAL_UTF8_BUFSIZE], *rhs_string_utf8;
4491ab64890Smrg
4501ab64890Smrg    struct DefBuffer {
4511ab64890Smrg	DTModifier modifier_mask;
4521ab64890Smrg	DTModifier modifier;
4531ab64890Smrg	KeySym keysym;
4541ab64890Smrg    };
4551ab64890Smrg
4561ab64890Smrg    struct DefBuffer buf[SEQUENCE_MAX];
4571ab64890Smrg    int i, n;
4581ab64890Smrg
4591ab64890Smrg    do {
4601ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
4611ab64890Smrg    } while (token == ENDOFLINE);
46261b2299dSmrg
4631ab64890Smrg    if (token == ENDOFFILE) {
4641ab64890Smrg	return(-1);
4651ab64890Smrg    }
4661ab64890Smrg
4671ab64890Smrg    n = 0;
4681ab64890Smrg    do {
4691ab64890Smrg    	if ((token == KEY) && (strcmp("include", tokenbuf) == 0)) {
4701ab64890Smrg            char *filename;
4711ab64890Smrg            FILE *infp;
4721ab64890Smrg            token = nexttoken(fp, tokenbuf, &lastch);
4731ab64890Smrg            if (token != KEY && token != STRING)
4741ab64890Smrg                goto error;
4751ab64890Smrg            if ((filename = TransFileName(im, tokenbuf)) == NULL)
4761ab64890Smrg                goto error;
4771ab64890Smrg            infp = _XFopenFile(filename, "r");
4781ab64890Smrg                Xfree(filename);
4791ab64890Smrg            if (infp == NULL)
4801ab64890Smrg                goto error;
4811ab64890Smrg            _XimParseStringFile(infp, im);
4821ab64890Smrg            fclose(infp);
4831ab64890Smrg            return (0);
4841ab64890Smrg	} else if ((token == KEY) && (strcmp("None", tokenbuf) == 0)) {
4851ab64890Smrg	    modifier = 0;
4861ab64890Smrg	    modifier_mask = AllMask;
4871ab64890Smrg	    token = nexttoken(fp, tokenbuf, &lastch);
4881ab64890Smrg	} else {
4891ab64890Smrg	    modifier_mask = modifier = 0;
4901ab64890Smrg	    exclam = False;
4911ab64890Smrg	    if (token == EXCLAM) {
4921ab64890Smrg		exclam = True;
4931ab64890Smrg		token = nexttoken(fp, tokenbuf, &lastch);
4941ab64890Smrg	    }
4951ab64890Smrg	    while (token == TILDE || token == KEY) {
4961ab64890Smrg		tilde = False;
4971ab64890Smrg		if (token == TILDE) {
4981ab64890Smrg		    tilde = True;
4991ab64890Smrg		    token = nexttoken(fp, tokenbuf, &lastch);
5001ab64890Smrg		    if (token != KEY)
5011ab64890Smrg			goto error;
5021ab64890Smrg		}
5031ab64890Smrg		tmp = modmask(tokenbuf);
5041ab64890Smrg		if (!tmp) {
5051ab64890Smrg		    goto error;
5061ab64890Smrg		}
5071ab64890Smrg		modifier_mask |= tmp;
5081ab64890Smrg		if (tilde) {
5091ab64890Smrg		    modifier &= ~tmp;
5101ab64890Smrg		} else {
5111ab64890Smrg		    modifier |= tmp;
5121ab64890Smrg		}
5131ab64890Smrg		token = nexttoken(fp, tokenbuf, &lastch);
5141ab64890Smrg	    }
5151ab64890Smrg	    if (exclam) {
5161ab64890Smrg		modifier_mask = AllMask;
5171ab64890Smrg	    }
5181ab64890Smrg	}
5191ab64890Smrg
5201ab64890Smrg	if (token != LESS) {
5211ab64890Smrg	    goto error;
5221ab64890Smrg	}
5231ab64890Smrg
5241ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
5251ab64890Smrg	if (token != KEY) {
5261ab64890Smrg	    goto error;
5271ab64890Smrg	}
5281ab64890Smrg
5291ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
5301ab64890Smrg	if (token != GREATER) {
5311ab64890Smrg	    goto error;
5321ab64890Smrg	}
5331ab64890Smrg
5341ab64890Smrg	keysym = XStringToKeysym(tokenbuf);
5351ab64890Smrg	if (keysym == NoSymbol) {
5361ab64890Smrg	    goto error;
5371ab64890Smrg	}
5381ab64890Smrg
5391ab64890Smrg	buf[n].keysym = keysym;
5401ab64890Smrg	buf[n].modifier = modifier;
5411ab64890Smrg	buf[n].modifier_mask = modifier_mask;
5421ab64890Smrg	n++;
5431ab64890Smrg	if( n >= SEQUENCE_MAX )
5441ab64890Smrg	    goto error;
5451ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
5461ab64890Smrg    } while (token != COLON);
5471ab64890Smrg
5481ab64890Smrg    token = nexttoken(fp, tokenbuf, &lastch);
5491ab64890Smrg    if (token == STRING) {
5501ab64890Smrg	l = strlen(tokenbuf) + 1;
5511ab64890Smrg	while (b->mbused + l > b->mbsize) {
5521ab64890Smrg	    b->mbsize = b->mbsize ? b->mbsize * 1.5 : 1024;
5531ab64890Smrg	    if (! (b->mb = Xrealloc (b->mb, b->mbsize)) )
5541ab64890Smrg		goto error;
5551ab64890Smrg	}
5561ab64890Smrg	rhs_string_mb = &b->mb[b->mbused];
5571ab64890Smrg	b->mbused    += l;
5581ab64890Smrg	strcpy(rhs_string_mb, tokenbuf);
5591ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
5601ab64890Smrg	if (token == KEY) {
5611ab64890Smrg	    rhs_keysym = XStringToKeysym(tokenbuf);
5621ab64890Smrg	    if (rhs_keysym == NoSymbol) {
5631ab64890Smrg		goto error;
5641ab64890Smrg	    }
5651ab64890Smrg	    token = nexttoken(fp, tokenbuf, &lastch);
5661ab64890Smrg	}
5671ab64890Smrg	if (token != ENDOFLINE && token != ENDOFFILE) {
5681ab64890Smrg	    goto error;
5691ab64890Smrg	}
5701ab64890Smrg    } else if (token == KEY) {
5711ab64890Smrg	rhs_keysym = XStringToKeysym(tokenbuf);
5721ab64890Smrg	if (rhs_keysym == NoSymbol) {
5731ab64890Smrg	    goto error;
5741ab64890Smrg	}
5751ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
5761ab64890Smrg	if (token != ENDOFLINE && token != ENDOFFILE) {
5771ab64890Smrg	    goto error;
5781ab64890Smrg	}
5791ab64890Smrg
5801ab64890Smrg        l = get_mb_string(im, local_mb_buf, rhs_keysym);
5811ab64890Smrg	while (b->mbused + l + 1 > b->mbsize) {
5821ab64890Smrg	    b->mbsize = b->mbsize ? b->mbsize * 1.5 : 1024;
5831ab64890Smrg	    if (! (b->mb = Xrealloc (b->mb, b->mbsize)) )
5841ab64890Smrg		goto error;
5851ab64890Smrg	}
5861ab64890Smrg	rhs_string_mb = &b->mb[b->mbused];
5871ab64890Smrg	b->mbused    += l + 1;
5881ab64890Smrg        memcpy(rhs_string_mb, local_mb_buf, l);
5891ab64890Smrg	rhs_string_mb[l] = '\0';
5901ab64890Smrg    } else {
5911ab64890Smrg	goto error;
5921ab64890Smrg    }
5931ab64890Smrg
5941ab64890Smrg    l = _Xmbstowcs(local_wc_buf, rhs_string_mb, LOCAL_WC_BUFSIZE - 1);
5951ab64890Smrg    if (l == LOCAL_WC_BUFSIZE - 1) {
5961ab64890Smrg	local_wc_buf[l] = (wchar_t)'\0';
5971ab64890Smrg    }
5981ab64890Smrg    while (b->wcused + l + 1 > b->wcsize) {
5991ab64890Smrg	b->wcsize = b->wcsize ? b->wcsize * 1.5 : 512;
6001ab64890Smrg	if (! (b->wc = Xrealloc (b->wc, sizeof(wchar_t) * b->wcsize)) )
6011ab64890Smrg	    goto error;
6021ab64890Smrg    }
6031ab64890Smrg    rhs_string_wc = &b->wc[b->wcused];
6041ab64890Smrg    b->wcused    += l + 1;
6051ab64890Smrg    memcpy((char *)rhs_string_wc, (char *)local_wc_buf, (l + 1) * sizeof(wchar_t) );
6061ab64890Smrg
6071ab64890Smrg    l = _Xmbstoutf8(local_utf8_buf, rhs_string_mb, LOCAL_UTF8_BUFSIZE - 1);
6081ab64890Smrg    if (l == LOCAL_UTF8_BUFSIZE - 1) {
60961b2299dSmrg	local_utf8_buf[l] = '\0';
6101ab64890Smrg    }
6111ab64890Smrg    while (b->utf8used + l + 1 > b->utf8size) {
6121ab64890Smrg	b->utf8size = b->utf8size ? b->utf8size * 1.5 : 1024;
6131ab64890Smrg	if (! (b->utf8 = Xrealloc (b->utf8, b->utf8size)) )
6141ab64890Smrg	    goto error;
6151ab64890Smrg    }
6161ab64890Smrg    rhs_string_utf8 = &b->utf8[b->utf8used];
6171ab64890Smrg    b->utf8used    += l + 1;
6181ab64890Smrg    memcpy(rhs_string_utf8, local_utf8_buf, l + 1);
6191ab64890Smrg
6201ab64890Smrg    for (i = 0; i < n; i++) {
6211ab64890Smrg	for (t = *top; t; t = b->tree[t].next) {
6221ab64890Smrg	    if (buf[i].keysym        == b->tree[t].keysym &&
6231ab64890Smrg		buf[i].modifier      == b->tree[t].modifier &&
6241ab64890Smrg		buf[i].modifier_mask == b->tree[t].modifier_mask) {
6251ab64890Smrg		break;
6261ab64890Smrg	    }
6271ab64890Smrg	}
6281ab64890Smrg	if (t) {
6291ab64890Smrg	    p = &b->tree[t];
6301ab64890Smrg	    top = &p->succession;
6311ab64890Smrg	} else {
6321ab64890Smrg	    while (b->treeused >= b->treesize) {
6331ab64890Smrg		DefTree *old     = b->tree;
6341ab64890Smrg		int      oldsize = b->treesize;
6351ab64890Smrg		b->treesize = b->treesize ? b->treesize * 1.5 : 256;
6361ab64890Smrg		if (! (b->tree = Xrealloc (b->tree, sizeof(DefTree) * b->treesize)) )
6371ab64890Smrg		    goto error;
6381ab64890Smrg		if (top >= (DTIndex *) old && top < (DTIndex *) &old[oldsize])
6391ab64890Smrg		    top = (DTIndex *) (((char *) top) + (((char *)b->tree)-(char *)old));
6401ab64890Smrg	    }
6411ab64890Smrg	    p = &b->tree[b->treeused];
6421ab64890Smrg	    p->keysym        = buf[i].keysym;
6431ab64890Smrg	    p->modifier      = buf[i].modifier;
6441ab64890Smrg	    p->modifier_mask = buf[i].modifier_mask;
6451ab64890Smrg	    p->succession    = 0;
6461ab64890Smrg	    p->next          = *top;
6471ab64890Smrg	    p->mb            = 0;
6481ab64890Smrg	    p->wc            = 0;
6491ab64890Smrg	    p->utf8          = 0;
6501ab64890Smrg	    p->ks            = NoSymbol;
6511ab64890Smrg	    *top = b->treeused;
6521ab64890Smrg	    top = &p->succession;
6531ab64890Smrg	    b->treeused++;
6541ab64890Smrg	}
6551ab64890Smrg    }
6561ab64890Smrg
6571ab64890Smrg    /* old entries no longer freed... */
6581ab64890Smrg    p->mb   = rhs_string_mb   - b->mb;
6591ab64890Smrg    p->wc   = rhs_string_wc   - b->wc;
6601ab64890Smrg    p->utf8 = rhs_string_utf8 - b->utf8;
6611ab64890Smrg    p->ks   = rhs_keysym;
6621ab64890Smrg    return(n);
6631ab64890Smrgerror:
6641ab64890Smrg    while (token != ENDOFLINE && token != ENDOFFILE) {
6651ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
6661ab64890Smrg    }
6671ab64890Smrg    return(0);
6681ab64890Smrg}
6691ab64890Smrg
6701ab64890Smrgvoid
6711ab64890Smrg_XimParseStringFile(
6721ab64890Smrg    FILE *fp,
6731ab64890Smrg    Xim   im)
6741ab64890Smrg{
6751ab64890Smrg    char tb[8192];
6761ab64890Smrg    char* tbp;
6771ab64890Smrg    struct stat st;
6781ab64890Smrg
6791ab64890Smrg    if (fstat (fileno (fp), &st) != -1) {
6801ab64890Smrg	unsigned long size = (unsigned long) st.st_size;
6811ab64890Smrg	if (size <= sizeof tb) tbp = tb;
6821ab64890Smrg	else tbp = malloc (size);
6831ab64890Smrg
6841ab64890Smrg	if (tbp != NULL) {
6851ab64890Smrg	    while (parseline(fp, im, tbp) >= 0) {}
6861ab64890Smrg	    if (tbp != tb) free (tbp);
6871ab64890Smrg	}
6881ab64890Smrg    }
6891ab64890Smrg}
690