imLcPrs.c revision eb411b4b
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>
44eb411b4bSmrg#include <limits.h>
45eb411b4bSmrg#include "pathmax.h"
461ab64890Smrg
47b4ee4795Smrg#define XLC_BUFSIZE 256
48b4ee4795Smrg
491ab64890Smrgextern int _Xmbstowcs(
501ab64890Smrg    wchar_t	*wstr,
511ab64890Smrg    char	*str,
521ab64890Smrg    int		len
531ab64890Smrg);
541ab64890Smrg
551ab64890Smrgextern int _Xmbstoutf8(
561ab64890Smrg    char	*ustr,
571ab64890Smrg    const char	*str,
581ab64890Smrg    int		len
591ab64890Smrg);
601ab64890Smrg
61eb411b4bSmrgstatic void parsestringfile(FILE *fp, Xim im, int depth);
62eb411b4bSmrg
631ab64890Smrg/*
641ab64890Smrg *	Parsing File Format:
651ab64890Smrg *
661ab64890Smrg *	FILE          ::= { [PRODUCTION] [COMMENT] "\n"}
671ab64890Smrg *	PRODUCTION    ::= LHS ":" RHS [ COMMENT ]
681ab64890Smrg *	COMMENT       ::= "#" {<any character except null or newline>}
691ab64890Smrg *	LHS           ::= EVENT { EVENT }
701ab64890Smrg *	EVENT         ::= [MODIFIER_LIST] "<" keysym ">"
711ab64890Smrg *	MODIFIER_LIST ::= ("!" {MODIFIER} ) | "None"
721ab64890Smrg *	MODIFIER      ::= ["~"] modifier_name
731ab64890Smrg *	RHS           ::= ( STRING | keysym | STRING keysym )
741ab64890Smrg *	STRING        ::= '"' { CHAR } '"'
751ab64890Smrg *	CHAR          ::= GRAPHIC_CHAR | ESCAPED_CHAR
761ab64890Smrg *	GRAPHIC_CHAR  ::= locale (codeset) dependent code
771ab64890Smrg *	ESCAPED_CHAR  ::= ('\\' | '\"' | OCTAL | HEX )
781ab64890Smrg *	OCTAL         ::= '\' OCTAL_CHAR [OCTAL_CHAR [OCTAL_CHAR]]
791ab64890Smrg *	OCTAL_CHAR    ::= (0|1|2|3|4|5|6|7)
801ab64890Smrg *	HEX           ::= '\' (x|X) HEX_CHAR [HEX_CHAR]]
811ab64890Smrg *	HEX_CHAR      ::= (0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|a|b|c|d|e|f)
821ab64890Smrg *
831ab64890Smrg */
841ab64890Smrg
851ab64890Smrgstatic int
861ab64890Smrgnextch(
871ab64890Smrg    FILE *fp,
881ab64890Smrg    int *lastch)
891ab64890Smrg{
901ab64890Smrg    int c;
911ab64890Smrg
921ab64890Smrg    if (*lastch != 0) {
931ab64890Smrg	c = *lastch;
941ab64890Smrg	*lastch = 0;
951ab64890Smrg    } else {
961ab64890Smrg	c = getc(fp);
971ab64890Smrg	if (c == '\\') {
981ab64890Smrg	    c = getc(fp);
991ab64890Smrg	    if (c == '\n') {
1001ab64890Smrg		c = getc(fp);
1011ab64890Smrg	    } else {
1021ab64890Smrg		ungetc(c, fp);
1031ab64890Smrg		c = '\\';
1041ab64890Smrg	    }
1051ab64890Smrg	}
1061ab64890Smrg    }
1071ab64890Smrg    return(c);
1081ab64890Smrg}
1091ab64890Smrg
1101ab64890Smrgstatic void
1111ab64890Smrgputbackch(
1121ab64890Smrg    int c,
1131ab64890Smrg    int *lastch)
1141ab64890Smrg{
1151ab64890Smrg    *lastch = c;
1161ab64890Smrg}
1171ab64890Smrg
1181ab64890Smrg#define ENDOFFILE 0
1191ab64890Smrg#define ENDOFLINE 1
1201ab64890Smrg#define COLON 2
1211ab64890Smrg#define LESS 3
1221ab64890Smrg#define GREATER 4
1231ab64890Smrg#define EXCLAM 5
1241ab64890Smrg#define TILDE 6
1251ab64890Smrg#define STRING 7
1261ab64890Smrg#define KEY 8
1271ab64890Smrg#define ERROR 9
1281ab64890Smrg
1291ab64890Smrg#ifndef isalnum
1301ab64890Smrg#define isalnum(c)      \
1311ab64890Smrg    (('0' <= (c) && (c) <= '9')  || \
1321ab64890Smrg     ('A' <= (c) && (c) <= 'Z')  || \
1331ab64890Smrg     ('a' <= (c) && (c) <= 'z'))
1341ab64890Smrg#endif
1351ab64890Smrg
1361ab64890Smrgstatic int
1371ab64890Smrgnexttoken(
1381ab64890Smrg    FILE *fp,
1391ab64890Smrg    char *tokenbuf,
1401ab64890Smrg    int *lastch)
1411ab64890Smrg{
1421ab64890Smrg    int c;
1431ab64890Smrg    int token;
1441ab64890Smrg    char *p;
1451ab64890Smrg    int i, j;
1461ab64890Smrg
1471ab64890Smrg    while ((c = nextch(fp, lastch)) == ' ' || c == '\t') {
1481ab64890Smrg    }
1491ab64890Smrg    switch (c) {
1501ab64890Smrg      case EOF:
1511ab64890Smrg	token = ENDOFFILE;
1521ab64890Smrg	break;
1531ab64890Smrg      case '\n':
1541ab64890Smrg	token = ENDOFLINE;
1551ab64890Smrg	break;
1561ab64890Smrg      case '<':
1571ab64890Smrg	token = LESS;
1581ab64890Smrg	break;
1591ab64890Smrg      case '>':
1601ab64890Smrg	token = GREATER;
1611ab64890Smrg	break;
1621ab64890Smrg      case ':':
1631ab64890Smrg	token = COLON;
1641ab64890Smrg	break;
1651ab64890Smrg      case '!':
1661ab64890Smrg	token = EXCLAM;
1671ab64890Smrg	break;
1681ab64890Smrg      case '~':
1691ab64890Smrg	token = TILDE;
1701ab64890Smrg	break;
1711ab64890Smrg      case '"':
1721ab64890Smrg	p = tokenbuf;
1731ab64890Smrg	while ((c = nextch(fp, lastch)) != '"') {
1741ab64890Smrg	    if (c == '\n' || c == EOF) {
1751ab64890Smrg		putbackch(c, lastch);
1761ab64890Smrg		token = ERROR;
1771ab64890Smrg		goto string_error;
1781ab64890Smrg	    } else if (c == '\\') {
1791ab64890Smrg		c = nextch(fp, lastch);
1801ab64890Smrg		switch (c) {
1811ab64890Smrg		  case '\\':
1821ab64890Smrg		  case '"':
1831ab64890Smrg		    *p++ = c;
1841ab64890Smrg		    break;
1851ab64890Smrg		  case 'n':
1861ab64890Smrg		    *p++ = '\n';
1871ab64890Smrg		    break;
1881ab64890Smrg		  case 'r':
1891ab64890Smrg		    *p++ = '\r';
1901ab64890Smrg		    break;
1911ab64890Smrg		  case 't':
1921ab64890Smrg		    *p++ = '\t';
1931ab64890Smrg		    break;
1941ab64890Smrg		  case '0':
1951ab64890Smrg		  case '1':
1961ab64890Smrg		  case '2':
1971ab64890Smrg		  case '3':
1981ab64890Smrg		  case '4':
1991ab64890Smrg		  case '5':
2001ab64890Smrg		  case '6':
2011ab64890Smrg		  case '7':
2021ab64890Smrg		    i = c - '0';
2031ab64890Smrg		    c = nextch(fp, lastch);
2041ab64890Smrg		    for (j = 0; j < 2 && c >= '0' && c <= '7'; j++) {
2051ab64890Smrg			i <<= 3;
2061ab64890Smrg			i += c - '0';
2071ab64890Smrg			c = nextch(fp, lastch);
2081ab64890Smrg		    }
2091ab64890Smrg		    putbackch(c, lastch);
2101ab64890Smrg		    *p++ = (char)i;
2111ab64890Smrg		    break;
2121ab64890Smrg		  case 'X':
2131ab64890Smrg		  case 'x':
2141ab64890Smrg		    i = 0;
2151ab64890Smrg		    for (j = 0; j < 2; j++) {
2161ab64890Smrg			c = nextch(fp, lastch);
2171ab64890Smrg 			i <<= 4;
2181ab64890Smrg			if (c >= '0' && c <= '9') {
2191ab64890Smrg			    i += c - '0';
2201ab64890Smrg			} else if (c >= 'A' && c <= 'F') {
2211ab64890Smrg			    i += c - 'A' + 10;
2221ab64890Smrg			} else if (c >= 'a' && c <= 'f') {
2231ab64890Smrg			    i += c - 'a' + 10;
2241ab64890Smrg			} else {
2251ab64890Smrg			    putbackch(c, lastch);
2261ab64890Smrg			    i >>= 4;
2271ab64890Smrg			    break;
2281ab64890Smrg		        }
2291ab64890Smrg		    }
2301ab64890Smrg		    if (j == 0) {
2311ab64890Smrg		        token = ERROR;
2321ab64890Smrg		        goto string_error;
2331ab64890Smrg		    }
2341ab64890Smrg		    *p++ = (char)i;
2351ab64890Smrg		    break;
2361ab64890Smrg		  case EOF:
2371ab64890Smrg		    putbackch(c, lastch);
2381ab64890Smrg		    token = ERROR;
2391ab64890Smrg		    goto string_error;
2401ab64890Smrg		  default:
2411ab64890Smrg		    *p++ = c;
2421ab64890Smrg		    break;
2431ab64890Smrg		}
2441ab64890Smrg	    } else {
2451ab64890Smrg		*p++ = c;
2461ab64890Smrg	    }
2471ab64890Smrg	}
2481ab64890Smrg	*p = '\0';
2491ab64890Smrg	token = STRING;
2501ab64890Smrg	break;
2511ab64890Smrg      case '#':
2521ab64890Smrg	while ((c = nextch(fp, lastch)) != '\n' && c != EOF) {
2531ab64890Smrg	}
2541ab64890Smrg	if (c == '\n') {
2551ab64890Smrg	    token = ENDOFLINE;
2561ab64890Smrg	} else {
2571ab64890Smrg	    token = ENDOFFILE;
2581ab64890Smrg	}
2591ab64890Smrg	break;
2601ab64890Smrg      default:
2611ab64890Smrg	if (isalnum(c) || c == '_' || c == '-') {
2621ab64890Smrg	    p = tokenbuf;
2631ab64890Smrg	    *p++ = c;
2641ab64890Smrg	    c = nextch(fp, lastch);
2651ab64890Smrg	    while (isalnum(c) || c == '_' || c == '-') {
2661ab64890Smrg		*p++ = c;
2671ab64890Smrg		c = nextch(fp, lastch);
2681ab64890Smrg	    }
2691ab64890Smrg	    *p = '\0';
2701ab64890Smrg	    putbackch(c, lastch);
2711ab64890Smrg	    token = KEY;
2721ab64890Smrg	} else {
2731ab64890Smrg	    token = ERROR;
2741ab64890Smrg	}
2751ab64890Smrg	break;
2761ab64890Smrg    }
2771ab64890Smrgstring_error:
2781ab64890Smrg    return(token);
2791ab64890Smrg}
2801ab64890Smrg
2811ab64890Smrgstatic long
2821ab64890Smrgmodmask(
2831ab64890Smrg    char *name)
2841ab64890Smrg{
2851ab64890Smrg    struct _modtbl {
2861ab64890Smrg	const char name[6];
2871ab64890Smrg	long mask;
2881ab64890Smrg    };
2891ab64890Smrg
2901ab64890Smrg    static const struct _modtbl tbl[] = {
2911ab64890Smrg	{ "Ctrl",	ControlMask	},
2921ab64890Smrg        { "Lock",	LockMask	},
2931ab64890Smrg        { "Caps",	LockMask	},
2941ab64890Smrg        { "Shift",	ShiftMask	},
2951ab64890Smrg        { "Alt",	Mod1Mask	},
2961ab64890Smrg        { "Meta",	Mod1Mask	}};
2971ab64890Smrg
2981ab64890Smrg    int i, num_entries = sizeof (tbl) / sizeof (tbl[0]);
2991ab64890Smrg
3001ab64890Smrg    for (i = 0; i < num_entries; i++)
3011ab64890Smrg        if (!strcmp (name, tbl[i].name))
3021ab64890Smrg            return tbl[i].mask;
3031ab64890Smrg
3041ab64890Smrg    return 0;
3051ab64890Smrg}
3061ab64890Smrg
3071ab64890Smrgstatic char*
3081ab64890SmrgTransFileName(Xim im, char *name)
3091ab64890Smrg{
3101ab64890Smrg   char *home = NULL, *lcCompose = NULL;
311eb411b4bSmrg   char dir[XLC_BUFSIZE] = "";
312eb411b4bSmrg   char *i = name, *ret = NULL, *j;
313eb411b4bSmrg   size_t l = 0;
3141ab64890Smrg
3151ab64890Smrg   while (*i) {
3161ab64890Smrg      if (*i == '%') {
3171ab64890Smrg   	  i++;
3181ab64890Smrg   	  switch (*i) {
3191ab64890Smrg   	      case '%':
3201ab64890Smrg                 l++;
3211ab64890Smrg   	         break;
3221ab64890Smrg   	      case 'H':
323eb411b4bSmrg                 if (home == NULL)
324eb411b4bSmrg                     home = getenv("HOME");
325eb411b4bSmrg                 if (home) {
326eb411b4bSmrg                     size_t Hsize = strlen(home);
327eb411b4bSmrg                     if (Hsize > PATH_MAX)
328eb411b4bSmrg                         /* your home directory length is ridiculous */
329eb411b4bSmrg                         goto end;
330eb411b4bSmrg                     l += Hsize;
331eb411b4bSmrg                 }
3321ab64890Smrg   	         break;
3331ab64890Smrg   	      case 'L':
33457f47464Smrg                 if (lcCompose == NULL)
33557f47464Smrg                     lcCompose = _XlcFileName(im->core.lcd, COMPOSE_FILE);
336eb411b4bSmrg                 if (lcCompose) {
337eb411b4bSmrg                     size_t Lsize = strlen(lcCompose);
338eb411b4bSmrg                     if (Lsize > PATH_MAX)
339eb411b4bSmrg                         /* your compose pathname length is ridiculous */
340eb411b4bSmrg                         goto end;
341eb411b4bSmrg                     l += Lsize;
342eb411b4bSmrg                 }
3431ab64890Smrg   	         break;
344b4ee4795Smrg   	      case 'S':
345eb411b4bSmrg                 if (dir[0] == '\0')
346eb411b4bSmrg                     xlocaledir(dir, XLC_BUFSIZE);
347eb411b4bSmrg                 if (dir[0]) {
348eb411b4bSmrg                     size_t Ssize = strlen(dir);
349eb411b4bSmrg                     if (Ssize > PATH_MAX)
350eb411b4bSmrg                         /* your locale directory path length is ridiculous */
351eb411b4bSmrg                         goto end;
352eb411b4bSmrg                     l += Ssize;
353eb411b4bSmrg                 }
354b4ee4795Smrg   	         break;
3551ab64890Smrg   	  }
3561ab64890Smrg      } else {
3571ab64890Smrg      	  l++;
3581ab64890Smrg      }
3591ab64890Smrg      i++;
360eb411b4bSmrg      if (l > PATH_MAX)
361eb411b4bSmrg          /* your expanded path length is ridiculous */
362eb411b4bSmrg          goto end;
3631ab64890Smrg   }
3641ab64890Smrg
3651ab64890Smrg   j = ret = Xmalloc(l+1);
3661ab64890Smrg   if (ret == NULL)
367eb411b4bSmrg      goto end;
3681ab64890Smrg   i = name;
3691ab64890Smrg   while (*i) {
3701ab64890Smrg      if (*i == '%') {
3711ab64890Smrg   	  i++;
3721ab64890Smrg   	  switch (*i) {
3731ab64890Smrg   	      case '%':
3741ab64890Smrg                 *j++ = '%';
3751ab64890Smrg   	         break;
3761ab64890Smrg   	      case 'H':
3771ab64890Smrg   	         if (home) {
3781ab64890Smrg   	             strcpy(j, home);
3791ab64890Smrg   	             j += strlen(home);
3801ab64890Smrg   	         }
3811ab64890Smrg   	         break;
3821ab64890Smrg   	      case 'L':
3831ab64890Smrg   	         if (lcCompose) {
3841ab64890Smrg                    strcpy(j, lcCompose);
3851ab64890Smrg                    j += strlen(lcCompose);
3861ab64890Smrg                 }
3871ab64890Smrg   	         break;
388b4ee4795Smrg   	      case 'S':
389b4ee4795Smrg                 strcpy(j, dir);
390b4ee4795Smrg                 j += strlen(dir);
391b4ee4795Smrg   	         break;
3921ab64890Smrg   	  }
3931ab64890Smrg          i++;
3941ab64890Smrg      } else {
3951ab64890Smrg      	  *j++ = *i++;
3961ab64890Smrg      }
3971ab64890Smrg   }
3981ab64890Smrg   *j = '\0';
399eb411b4bSmrgend:
40057f47464Smrg   Xfree(lcCompose);
4011ab64890Smrg   return ret;
4021ab64890Smrg}
4031ab64890Smrg
4041ab64890Smrg#ifndef MB_LEN_MAX
4051ab64890Smrg#define MB_LEN_MAX 6
4061ab64890Smrg#endif
4071ab64890Smrg
4081ab64890Smrgstatic int
4091ab64890Smrgget_mb_string (Xim im, char *buf, KeySym ks)
4101ab64890Smrg{
4111ab64890Smrg    XPointer from, to;
4121ab64890Smrg    int from_len, to_len, len;
4131ab64890Smrg    XPointer args[1];
4141ab64890Smrg    XlcCharSet charset;
4151ab64890Smrg    char local_buf[MB_LEN_MAX];
4161ab64890Smrg    unsigned int ucs;
4171ab64890Smrg    ucs = KeySymToUcs4(ks);
4181ab64890Smrg
4191ab64890Smrg    from = (XPointer) &ucs;
4201ab64890Smrg    to =   (XPointer) local_buf;
4211ab64890Smrg    from_len = 1;
4221ab64890Smrg    to_len = MB_LEN_MAX;
4231ab64890Smrg    args[0] = (XPointer) &charset;
4241ab64890Smrg    if (_XlcConvert(im->private.local.ucstoc_conv,
4251ab64890Smrg                    &from, &from_len, &to, &to_len, args, 1 ) != 0) {
4261ab64890Smrg         return 0;
4271ab64890Smrg    }
4281ab64890Smrg
4291ab64890Smrg    from = (XPointer) local_buf;
4301ab64890Smrg    to =   (XPointer) buf;
4311ab64890Smrg    from_len = MB_LEN_MAX - to_len;
4321ab64890Smrg    to_len = MB_LEN_MAX + 1;
4331ab64890Smrg    args[0] = (XPointer) charset;
4341ab64890Smrg    if (_XlcConvert(im->private.local.cstomb_conv,
4351ab64890Smrg                    &from, &from_len, &to, &to_len, args, 1 ) != 0) {
4361ab64890Smrg         return 0;
4371ab64890Smrg    }
4381ab64890Smrg    len = MB_LEN_MAX + 1 - to_len;
4391ab64890Smrg    buf[len] = '\0';
4401ab64890Smrg    return len;
4411ab64890Smrg}
4421ab64890Smrg
44361b2299dSmrg#define AllMask (ShiftMask | LockMask | ControlMask | Mod1Mask)
4441ab64890Smrg#define LOCAL_WC_BUFSIZE 128
4451ab64890Smrg#define LOCAL_UTF8_BUFSIZE 256
4461ab64890Smrg#define SEQUENCE_MAX	10
4471ab64890Smrg
4481ab64890Smrgstatic int
4491ab64890Smrgparseline(
4501ab64890Smrg    FILE *fp,
4511ab64890Smrg    Xim   im,
452eb411b4bSmrg    char* tokenbuf,
453eb411b4bSmrg    int   depth)
4541ab64890Smrg{
4551ab64890Smrg    int token;
4561ab64890Smrg    DTModifier modifier_mask;
4571ab64890Smrg    DTModifier modifier;
4581ab64890Smrg    DTModifier tmp;
4591ab64890Smrg    KeySym keysym = NoSymbol;
4601ab64890Smrg    DTIndex *top = &im->private.local.top;
4611ab64890Smrg    DefTreeBase *b   = &im->private.local.base;
4621ab64890Smrg    DTIndex t;
4631ab64890Smrg    DefTree *p = NULL;
4641ab64890Smrg    Bool exclam, tilde;
4651ab64890Smrg    KeySym rhs_keysym = 0;
4661ab64890Smrg    char *rhs_string_mb;
4671ab64890Smrg    int l;
4681ab64890Smrg    int lastch = 0;
4691ab64890Smrg    char local_mb_buf[MB_LEN_MAX+1];
4701ab64890Smrg    wchar_t local_wc_buf[LOCAL_WC_BUFSIZE], *rhs_string_wc;
4711ab64890Smrg    char local_utf8_buf[LOCAL_UTF8_BUFSIZE], *rhs_string_utf8;
4721ab64890Smrg
4731ab64890Smrg    struct DefBuffer {
4741ab64890Smrg	DTModifier modifier_mask;
4751ab64890Smrg	DTModifier modifier;
4761ab64890Smrg	KeySym keysym;
4771ab64890Smrg    };
4781ab64890Smrg
4791ab64890Smrg    struct DefBuffer buf[SEQUENCE_MAX];
4801ab64890Smrg    int i, n;
4811ab64890Smrg
4821ab64890Smrg    do {
4831ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
4841ab64890Smrg    } while (token == ENDOFLINE);
48561b2299dSmrg
4861ab64890Smrg    if (token == ENDOFFILE) {
4871ab64890Smrg	return(-1);
4881ab64890Smrg    }
4891ab64890Smrg
4901ab64890Smrg    n = 0;
4911ab64890Smrg    do {
4921ab64890Smrg    	if ((token == KEY) && (strcmp("include", tokenbuf) == 0)) {
4931ab64890Smrg            char *filename;
4941ab64890Smrg            FILE *infp;
4951ab64890Smrg            token = nexttoken(fp, tokenbuf, &lastch);
4961ab64890Smrg            if (token != KEY && token != STRING)
4971ab64890Smrg                goto error;
4981ab64890Smrg            if ((filename = TransFileName(im, tokenbuf)) == NULL)
4991ab64890Smrg                goto error;
500eb411b4bSmrg            if (++depth > 100)
501eb411b4bSmrg                goto error;
5021ab64890Smrg            infp = _XFopenFile(filename, "r");
5031ab64890Smrg                Xfree(filename);
5041ab64890Smrg            if (infp == NULL)
5051ab64890Smrg                goto error;
506eb411b4bSmrg            parsestringfile(infp, im, depth);
5071ab64890Smrg            fclose(infp);
5081ab64890Smrg            return (0);
5091ab64890Smrg	} else if ((token == KEY) && (strcmp("None", tokenbuf) == 0)) {
5101ab64890Smrg	    modifier = 0;
5111ab64890Smrg	    modifier_mask = AllMask;
5121ab64890Smrg	    token = nexttoken(fp, tokenbuf, &lastch);
5131ab64890Smrg	} else {
5141ab64890Smrg	    modifier_mask = modifier = 0;
5151ab64890Smrg	    exclam = False;
5161ab64890Smrg	    if (token == EXCLAM) {
5171ab64890Smrg		exclam = True;
5181ab64890Smrg		token = nexttoken(fp, tokenbuf, &lastch);
5191ab64890Smrg	    }
5201ab64890Smrg	    while (token == TILDE || token == KEY) {
5211ab64890Smrg		tilde = False;
5221ab64890Smrg		if (token == TILDE) {
5231ab64890Smrg		    tilde = True;
5241ab64890Smrg		    token = nexttoken(fp, tokenbuf, &lastch);
5251ab64890Smrg		    if (token != KEY)
5261ab64890Smrg			goto error;
5271ab64890Smrg		}
5281ab64890Smrg		tmp = modmask(tokenbuf);
5291ab64890Smrg		if (!tmp) {
5301ab64890Smrg		    goto error;
5311ab64890Smrg		}
5321ab64890Smrg		modifier_mask |= tmp;
5331ab64890Smrg		if (tilde) {
5341ab64890Smrg		    modifier &= ~tmp;
5351ab64890Smrg		} else {
5361ab64890Smrg		    modifier |= tmp;
5371ab64890Smrg		}
5381ab64890Smrg		token = nexttoken(fp, tokenbuf, &lastch);
5391ab64890Smrg	    }
5401ab64890Smrg	    if (exclam) {
5411ab64890Smrg		modifier_mask = AllMask;
5421ab64890Smrg	    }
5431ab64890Smrg	}
5441ab64890Smrg
5451ab64890Smrg	if (token != LESS) {
5461ab64890Smrg	    goto error;
5471ab64890Smrg	}
5481ab64890Smrg
5491ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
5501ab64890Smrg	if (token != KEY) {
5511ab64890Smrg	    goto error;
5521ab64890Smrg	}
5531ab64890Smrg
5541ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
5551ab64890Smrg	if (token != GREATER) {
5561ab64890Smrg	    goto error;
5571ab64890Smrg	}
5581ab64890Smrg
5591ab64890Smrg	keysym = XStringToKeysym(tokenbuf);
5601ab64890Smrg	if (keysym == NoSymbol) {
5611ab64890Smrg	    goto error;
5621ab64890Smrg	}
5631ab64890Smrg
5641ab64890Smrg	buf[n].keysym = keysym;
5651ab64890Smrg	buf[n].modifier = modifier;
5661ab64890Smrg	buf[n].modifier_mask = modifier_mask;
5671ab64890Smrg	n++;
5681ab64890Smrg	if( n >= SEQUENCE_MAX )
5691ab64890Smrg	    goto error;
5701ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
5711ab64890Smrg    } while (token != COLON);
5721ab64890Smrg
5731ab64890Smrg    token = nexttoken(fp, tokenbuf, &lastch);
5741ab64890Smrg    if (token == STRING) {
5751ab64890Smrg	l = strlen(tokenbuf) + 1;
5761ab64890Smrg	while (b->mbused + l > b->mbsize) {
5771ab64890Smrg	    b->mbsize = b->mbsize ? b->mbsize * 1.5 : 1024;
5781ab64890Smrg	    if (! (b->mb = Xrealloc (b->mb, b->mbsize)) )
5791ab64890Smrg		goto error;
5801ab64890Smrg	}
5811ab64890Smrg	rhs_string_mb = &b->mb[b->mbused];
5821ab64890Smrg	b->mbused    += l;
5831ab64890Smrg	strcpy(rhs_string_mb, tokenbuf);
5841ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
5851ab64890Smrg	if (token == KEY) {
5861ab64890Smrg	    rhs_keysym = XStringToKeysym(tokenbuf);
5871ab64890Smrg	    if (rhs_keysym == NoSymbol) {
5881ab64890Smrg		goto error;
5891ab64890Smrg	    }
5901ab64890Smrg	    token = nexttoken(fp, tokenbuf, &lastch);
5911ab64890Smrg	}
5921ab64890Smrg	if (token != ENDOFLINE && token != ENDOFFILE) {
5931ab64890Smrg	    goto error;
5941ab64890Smrg	}
5951ab64890Smrg    } else if (token == KEY) {
5961ab64890Smrg	rhs_keysym = XStringToKeysym(tokenbuf);
5971ab64890Smrg	if (rhs_keysym == NoSymbol) {
5981ab64890Smrg	    goto error;
5991ab64890Smrg	}
6001ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
6011ab64890Smrg	if (token != ENDOFLINE && token != ENDOFFILE) {
6021ab64890Smrg	    goto error;
6031ab64890Smrg	}
6041ab64890Smrg
6051ab64890Smrg        l = get_mb_string(im, local_mb_buf, rhs_keysym);
6061ab64890Smrg	while (b->mbused + l + 1 > b->mbsize) {
6071ab64890Smrg	    b->mbsize = b->mbsize ? b->mbsize * 1.5 : 1024;
6081ab64890Smrg	    if (! (b->mb = Xrealloc (b->mb, b->mbsize)) )
6091ab64890Smrg		goto error;
6101ab64890Smrg	}
6111ab64890Smrg	rhs_string_mb = &b->mb[b->mbused];
6121ab64890Smrg	b->mbused    += l + 1;
6131ab64890Smrg        memcpy(rhs_string_mb, local_mb_buf, l);
6141ab64890Smrg	rhs_string_mb[l] = '\0';
6151ab64890Smrg    } else {
6161ab64890Smrg	goto error;
6171ab64890Smrg    }
6181ab64890Smrg
6191ab64890Smrg    l = _Xmbstowcs(local_wc_buf, rhs_string_mb, LOCAL_WC_BUFSIZE - 1);
6201ab64890Smrg    if (l == LOCAL_WC_BUFSIZE - 1) {
6211ab64890Smrg	local_wc_buf[l] = (wchar_t)'\0';
6221ab64890Smrg    }
6231ab64890Smrg    while (b->wcused + l + 1 > b->wcsize) {
6241ab64890Smrg	b->wcsize = b->wcsize ? b->wcsize * 1.5 : 512;
6251ab64890Smrg	if (! (b->wc = Xrealloc (b->wc, sizeof(wchar_t) * b->wcsize)) )
6261ab64890Smrg	    goto error;
6271ab64890Smrg    }
6281ab64890Smrg    rhs_string_wc = &b->wc[b->wcused];
6291ab64890Smrg    b->wcused    += l + 1;
6301ab64890Smrg    memcpy((char *)rhs_string_wc, (char *)local_wc_buf, (l + 1) * sizeof(wchar_t) );
6311ab64890Smrg
6321ab64890Smrg    l = _Xmbstoutf8(local_utf8_buf, rhs_string_mb, LOCAL_UTF8_BUFSIZE - 1);
6331ab64890Smrg    if (l == LOCAL_UTF8_BUFSIZE - 1) {
63461b2299dSmrg	local_utf8_buf[l] = '\0';
6351ab64890Smrg    }
6361ab64890Smrg    while (b->utf8used + l + 1 > b->utf8size) {
6371ab64890Smrg	b->utf8size = b->utf8size ? b->utf8size * 1.5 : 1024;
6381ab64890Smrg	if (! (b->utf8 = Xrealloc (b->utf8, b->utf8size)) )
6391ab64890Smrg	    goto error;
6401ab64890Smrg    }
6411ab64890Smrg    rhs_string_utf8 = &b->utf8[b->utf8used];
6421ab64890Smrg    b->utf8used    += l + 1;
6431ab64890Smrg    memcpy(rhs_string_utf8, local_utf8_buf, l + 1);
6441ab64890Smrg
6451ab64890Smrg    for (i = 0; i < n; i++) {
6461ab64890Smrg	for (t = *top; t; t = b->tree[t].next) {
6471ab64890Smrg	    if (buf[i].keysym        == b->tree[t].keysym &&
6481ab64890Smrg		buf[i].modifier      == b->tree[t].modifier &&
6491ab64890Smrg		buf[i].modifier_mask == b->tree[t].modifier_mask) {
6501ab64890Smrg		break;
6511ab64890Smrg	    }
6521ab64890Smrg	}
6531ab64890Smrg	if (t) {
6541ab64890Smrg	    p = &b->tree[t];
6551ab64890Smrg	    top = &p->succession;
6561ab64890Smrg	} else {
6571ab64890Smrg	    while (b->treeused >= b->treesize) {
6581ab64890Smrg		DefTree *old     = b->tree;
6591ab64890Smrg		int      oldsize = b->treesize;
6601ab64890Smrg		b->treesize = b->treesize ? b->treesize * 1.5 : 256;
6611ab64890Smrg		if (! (b->tree = Xrealloc (b->tree, sizeof(DefTree) * b->treesize)) )
6621ab64890Smrg		    goto error;
6631ab64890Smrg		if (top >= (DTIndex *) old && top < (DTIndex *) &old[oldsize])
6641ab64890Smrg		    top = (DTIndex *) (((char *) top) + (((char *)b->tree)-(char *)old));
6651ab64890Smrg	    }
6661ab64890Smrg	    p = &b->tree[b->treeused];
6671ab64890Smrg	    p->keysym        = buf[i].keysym;
6681ab64890Smrg	    p->modifier      = buf[i].modifier;
6691ab64890Smrg	    p->modifier_mask = buf[i].modifier_mask;
6701ab64890Smrg	    p->succession    = 0;
6711ab64890Smrg	    p->next          = *top;
6721ab64890Smrg	    p->mb            = 0;
6731ab64890Smrg	    p->wc            = 0;
6741ab64890Smrg	    p->utf8          = 0;
6751ab64890Smrg	    p->ks            = NoSymbol;
6761ab64890Smrg	    *top = b->treeused;
6771ab64890Smrg	    top = &p->succession;
6781ab64890Smrg	    b->treeused++;
6791ab64890Smrg	}
6801ab64890Smrg    }
6811ab64890Smrg
6821ab64890Smrg    /* old entries no longer freed... */
6831ab64890Smrg    p->mb   = rhs_string_mb   - b->mb;
6841ab64890Smrg    p->wc   = rhs_string_wc   - b->wc;
6851ab64890Smrg    p->utf8 = rhs_string_utf8 - b->utf8;
6861ab64890Smrg    p->ks   = rhs_keysym;
6871ab64890Smrg    return(n);
6881ab64890Smrgerror:
6891ab64890Smrg    while (token != ENDOFLINE && token != ENDOFFILE) {
6901ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
6911ab64890Smrg    }
6921ab64890Smrg    return(0);
6931ab64890Smrg}
6941ab64890Smrg
6951ab64890Smrgvoid
6961ab64890Smrg_XimParseStringFile(
6971ab64890Smrg    FILE *fp,
6981ab64890Smrg    Xim   im)
699eb411b4bSmrg{
700eb411b4bSmrg    parsestringfile(fp, im, 0);
701eb411b4bSmrg}
702eb411b4bSmrg
703eb411b4bSmrgstatic void
704eb411b4bSmrgparsestringfile(
705eb411b4bSmrg    FILE *fp,
706eb411b4bSmrg    Xim   im,
707eb411b4bSmrg    int   depth)
7081ab64890Smrg{
7091ab64890Smrg    char tb[8192];
7101ab64890Smrg    char* tbp;
7111ab64890Smrg    struct stat st;
7121ab64890Smrg
7131ab64890Smrg    if (fstat (fileno (fp), &st) != -1) {
7141ab64890Smrg	unsigned long size = (unsigned long) st.st_size;
715eb411b4bSmrg	if (st.st_size >= INT_MAX)
716eb411b4bSmrg	    return;
7171ab64890Smrg	if (size <= sizeof tb) tbp = tb;
7181ab64890Smrg	else tbp = malloc (size);
7191ab64890Smrg
7201ab64890Smrg	if (tbp != NULL) {
721eb411b4bSmrg	    while (parseline(fp, im, tbp, depth) >= 0) {}
7221ab64890Smrg	    if (tbp != tb) free (tbp);
7231ab64890Smrg	}
7241ab64890Smrg    }
7251ab64890Smrg}
726