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 ">"
710f8248bfSmrg *	MODIFIER_LIST ::= (["!"] {MODIFIER} ) | "None"
720f8248bfSmrg *	MODIFIER      ::= ["~"] MODIFIER_NAME
730f8248bfSmrg *	MODIFIER_NAME ::= ("Ctrl"|"Lock"|"Caps"|"Shift"|"Alt"|"Meta")
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;
312eb411b4bSmrg   char dir[XLC_BUFSIZE] = "";
313eb411b4bSmrg   char *i = name, *ret = NULL, *j;
314eb411b4bSmrg   size_t l = 0;
3151ab64890Smrg
3161ab64890Smrg   while (*i) {
3171ab64890Smrg      if (*i == '%') {
3181ab64890Smrg   	  i++;
3191ab64890Smrg   	  switch (*i) {
3201ab64890Smrg   	      case '%':
3211ab64890Smrg                 l++;
3221ab64890Smrg   	         break;
3231ab64890Smrg   	      case 'H':
324eb411b4bSmrg                 if (home == NULL)
325eb411b4bSmrg                     home = getenv("HOME");
326eb411b4bSmrg                 if (home) {
327eb411b4bSmrg                     size_t Hsize = strlen(home);
328eb411b4bSmrg                     if (Hsize > PATH_MAX)
329eb411b4bSmrg                         /* your home directory length is ridiculous */
330eb411b4bSmrg                         goto end;
331eb411b4bSmrg                     l += Hsize;
332eb411b4bSmrg                 }
3331ab64890Smrg   	         break;
3341ab64890Smrg   	      case 'L':
33557f47464Smrg                 if (lcCompose == NULL)
33657f47464Smrg                     lcCompose = _XlcFileName(im->core.lcd, COMPOSE_FILE);
337eb411b4bSmrg                 if (lcCompose) {
338eb411b4bSmrg                     size_t Lsize = strlen(lcCompose);
339eb411b4bSmrg                     if (Lsize > PATH_MAX)
340eb411b4bSmrg                         /* your compose pathname length is ridiculous */
341eb411b4bSmrg                         goto end;
342eb411b4bSmrg                     l += Lsize;
343eb411b4bSmrg                 }
3441ab64890Smrg   	         break;
345b4ee4795Smrg   	      case 'S':
346eb411b4bSmrg                 if (dir[0] == '\0')
347eb411b4bSmrg                     xlocaledir(dir, XLC_BUFSIZE);
348eb411b4bSmrg                 if (dir[0]) {
349eb411b4bSmrg                     size_t Ssize = strlen(dir);
350eb411b4bSmrg                     if (Ssize > PATH_MAX)
351eb411b4bSmrg                         /* your locale directory path length is ridiculous */
352eb411b4bSmrg                         goto end;
353eb411b4bSmrg                     l += Ssize;
354eb411b4bSmrg                 }
355b4ee4795Smrg   	         break;
3561ab64890Smrg   	  }
3571ab64890Smrg      } else {
3581ab64890Smrg      	  l++;
3591ab64890Smrg      }
3601ab64890Smrg      i++;
361eb411b4bSmrg      if (l > PATH_MAX)
362eb411b4bSmrg          /* your expanded path length is ridiculous */
363eb411b4bSmrg          goto end;
3641ab64890Smrg   }
3651ab64890Smrg
3661ab64890Smrg   j = ret = Xmalloc(l+1);
3671ab64890Smrg   if (ret == NULL)
368eb411b4bSmrg      goto end;
3691ab64890Smrg   i = name;
3701ab64890Smrg   while (*i) {
3711ab64890Smrg      if (*i == '%') {
3721ab64890Smrg   	  i++;
3731ab64890Smrg   	  switch (*i) {
3741ab64890Smrg   	      case '%':
3751ab64890Smrg                 *j++ = '%';
3761ab64890Smrg   	         break;
3771ab64890Smrg   	      case 'H':
3781ab64890Smrg   	         if (home) {
3791ab64890Smrg   	             strcpy(j, home);
3801ab64890Smrg   	             j += strlen(home);
3811ab64890Smrg   	         }
3821ab64890Smrg   	         break;
3831ab64890Smrg   	      case 'L':
3841ab64890Smrg   	         if (lcCompose) {
3851ab64890Smrg                    strcpy(j, lcCompose);
3861ab64890Smrg                    j += strlen(lcCompose);
3871ab64890Smrg                 }
3881ab64890Smrg   	         break;
389b4ee4795Smrg   	      case 'S':
390b4ee4795Smrg                 strcpy(j, dir);
391b4ee4795Smrg                 j += strlen(dir);
392b4ee4795Smrg   	         break;
3931ab64890Smrg   	  }
3941ab64890Smrg          i++;
3951ab64890Smrg      } else {
3961ab64890Smrg      	  *j++ = *i++;
3971ab64890Smrg      }
3981ab64890Smrg   }
3991ab64890Smrg   *j = '\0';
400eb411b4bSmrgend:
40157f47464Smrg   Xfree(lcCompose);
4021ab64890Smrg   return ret;
4031ab64890Smrg}
4041ab64890Smrg
4051ab64890Smrg#ifndef MB_LEN_MAX
4061ab64890Smrg#define MB_LEN_MAX 6
4071ab64890Smrg#endif
4081ab64890Smrg
4091ab64890Smrgstatic int
4101ab64890Smrgget_mb_string (Xim im, char *buf, KeySym ks)
4111ab64890Smrg{
4121ab64890Smrg    XPointer from, to;
4131ab64890Smrg    int from_len, to_len, len;
4141ab64890Smrg    XPointer args[1];
4151ab64890Smrg    XlcCharSet charset;
4161ab64890Smrg    char local_buf[MB_LEN_MAX];
4171ab64890Smrg    unsigned int ucs;
4181ab64890Smrg    ucs = KeySymToUcs4(ks);
4191ab64890Smrg
4201ab64890Smrg    from = (XPointer) &ucs;
4211ab64890Smrg    to =   (XPointer) local_buf;
4221ab64890Smrg    from_len = 1;
4231ab64890Smrg    to_len = MB_LEN_MAX;
4241ab64890Smrg    args[0] = (XPointer) &charset;
4251ab64890Smrg    if (_XlcConvert(im->private.local.ucstoc_conv,
4261ab64890Smrg                    &from, &from_len, &to, &to_len, args, 1 ) != 0) {
4271ab64890Smrg         return 0;
4281ab64890Smrg    }
4291ab64890Smrg
4301ab64890Smrg    from = (XPointer) local_buf;
4311ab64890Smrg    to =   (XPointer) buf;
4321ab64890Smrg    from_len = MB_LEN_MAX - to_len;
4331ab64890Smrg    to_len = MB_LEN_MAX + 1;
4341ab64890Smrg    args[0] = (XPointer) charset;
4351ab64890Smrg    if (_XlcConvert(im->private.local.cstomb_conv,
4361ab64890Smrg                    &from, &from_len, &to, &to_len, args, 1 ) != 0) {
4371ab64890Smrg         return 0;
4381ab64890Smrg    }
4391ab64890Smrg    len = MB_LEN_MAX + 1 - to_len;
4401ab64890Smrg    buf[len] = '\0';
4411ab64890Smrg    return len;
4421ab64890Smrg}
4431ab64890Smrg
44461b2299dSmrg#define AllMask (ShiftMask | LockMask | ControlMask | Mod1Mask)
4451ab64890Smrg#define LOCAL_WC_BUFSIZE 128
4461ab64890Smrg#define LOCAL_UTF8_BUFSIZE 256
4471ab64890Smrg#define SEQUENCE_MAX	10
4481ab64890Smrg
4491ab64890Smrgstatic int
4501ab64890Smrgparseline(
4511ab64890Smrg    FILE *fp,
4521ab64890Smrg    Xim   im,
453eb411b4bSmrg    char* tokenbuf,
454eb411b4bSmrg    int   depth)
4551ab64890Smrg{
4561ab64890Smrg    int token;
4571ab64890Smrg    DTModifier modifier_mask;
4581ab64890Smrg    DTModifier modifier;
4591ab64890Smrg    DTModifier tmp;
4601ab64890Smrg    KeySym keysym = NoSymbol;
4611ab64890Smrg    DTIndex *top = &im->private.local.top;
4621ab64890Smrg    DefTreeBase *b   = &im->private.local.base;
4631ab64890Smrg    DTIndex t;
4641ab64890Smrg    DefTree *p = NULL;
4651ab64890Smrg    Bool exclam, tilde;
4661ab64890Smrg    KeySym rhs_keysym = 0;
4671ab64890Smrg    char *rhs_string_mb;
4681ab64890Smrg    int l;
4691ab64890Smrg    int lastch = 0;
4701ab64890Smrg    char local_mb_buf[MB_LEN_MAX+1];
4711ab64890Smrg    wchar_t local_wc_buf[LOCAL_WC_BUFSIZE], *rhs_string_wc;
4721ab64890Smrg    char local_utf8_buf[LOCAL_UTF8_BUFSIZE], *rhs_string_utf8;
4731ab64890Smrg
4741ab64890Smrg    struct DefBuffer {
4751ab64890Smrg	DTModifier modifier_mask;
4761ab64890Smrg	DTModifier modifier;
4771ab64890Smrg	KeySym keysym;
4781ab64890Smrg    };
4791ab64890Smrg
4801ab64890Smrg    struct DefBuffer buf[SEQUENCE_MAX];
4811ab64890Smrg    int i, n;
4821ab64890Smrg
4831ab64890Smrg    do {
4841ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
4851ab64890Smrg    } while (token == ENDOFLINE);
48661b2299dSmrg
4871ab64890Smrg    if (token == ENDOFFILE) {
4881ab64890Smrg	return(-1);
4891ab64890Smrg    }
4901ab64890Smrg
4911ab64890Smrg    n = 0;
4921ab64890Smrg    do {
4931ab64890Smrg    	if ((token == KEY) && (strcmp("include", tokenbuf) == 0)) {
4941ab64890Smrg            char *filename;
4951ab64890Smrg            FILE *infp;
4961ab64890Smrg            token = nexttoken(fp, tokenbuf, &lastch);
4971ab64890Smrg            if (token != KEY && token != STRING)
4981ab64890Smrg                goto error;
499eb411b4bSmrg            if (++depth > 100)
500eb411b4bSmrg                goto error;
5012d67cb4fSmrg            if ((filename = TransFileName(im, tokenbuf)) == NULL)
5022d67cb4fSmrg                goto error;
5031ab64890Smrg            infp = _XFopenFile(filename, "r");
5042d67cb4fSmrg            Xfree(filename);
5051ab64890Smrg            if (infp == NULL)
5061ab64890Smrg                goto error;
507eb411b4bSmrg            parsestringfile(infp, im, depth);
5081ab64890Smrg            fclose(infp);
5091ab64890Smrg            return (0);
5101ab64890Smrg	} else if ((token == KEY) && (strcmp("None", tokenbuf) == 0)) {
5111ab64890Smrg	    modifier = 0;
5121ab64890Smrg	    modifier_mask = AllMask;
5131ab64890Smrg	    token = nexttoken(fp, tokenbuf, &lastch);
5141ab64890Smrg	} else {
5151ab64890Smrg	    modifier_mask = modifier = 0;
5161ab64890Smrg	    exclam = False;
5171ab64890Smrg	    if (token == EXCLAM) {
5181ab64890Smrg		exclam = True;
5191ab64890Smrg		token = nexttoken(fp, tokenbuf, &lastch);
5201ab64890Smrg	    }
5211ab64890Smrg	    while (token == TILDE || token == KEY) {
5221ab64890Smrg		tilde = False;
5231ab64890Smrg		if (token == TILDE) {
5241ab64890Smrg		    tilde = True;
5251ab64890Smrg		    token = nexttoken(fp, tokenbuf, &lastch);
5261ab64890Smrg		    if (token != KEY)
5271ab64890Smrg			goto error;
5281ab64890Smrg		}
5291ab64890Smrg		tmp = modmask(tokenbuf);
5301ab64890Smrg		if (!tmp) {
5311ab64890Smrg		    goto error;
5321ab64890Smrg		}
5331ab64890Smrg		modifier_mask |= tmp;
5341ab64890Smrg		if (tilde) {
5351ab64890Smrg		    modifier &= ~tmp;
5361ab64890Smrg		} else {
5371ab64890Smrg		    modifier |= tmp;
5381ab64890Smrg		}
5391ab64890Smrg		token = nexttoken(fp, tokenbuf, &lastch);
5401ab64890Smrg	    }
5411ab64890Smrg	    if (exclam) {
5421ab64890Smrg		modifier_mask = AllMask;
5431ab64890Smrg	    }
5441ab64890Smrg	}
5451ab64890Smrg
5461ab64890Smrg	if (token != LESS) {
5471ab64890Smrg	    goto error;
5481ab64890Smrg	}
5491ab64890Smrg
5501ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
5511ab64890Smrg	if (token != KEY) {
5521ab64890Smrg	    goto error;
5531ab64890Smrg	}
5541ab64890Smrg
5551ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
5561ab64890Smrg	if (token != GREATER) {
5571ab64890Smrg	    goto error;
5581ab64890Smrg	}
5591ab64890Smrg
5601ab64890Smrg	keysym = XStringToKeysym(tokenbuf);
5611ab64890Smrg	if (keysym == NoSymbol) {
5621ab64890Smrg	    goto error;
5631ab64890Smrg	}
5641ab64890Smrg
5651ab64890Smrg	buf[n].keysym = keysym;
5661ab64890Smrg	buf[n].modifier = modifier;
5671ab64890Smrg	buf[n].modifier_mask = modifier_mask;
5681ab64890Smrg	n++;
5691ab64890Smrg	if( n >= SEQUENCE_MAX )
5701ab64890Smrg	    goto error;
5711ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
5721ab64890Smrg    } while (token != COLON);
5731ab64890Smrg
5741ab64890Smrg    token = nexttoken(fp, tokenbuf, &lastch);
5751ab64890Smrg    if (token == STRING) {
5761ab64890Smrg	l = strlen(tokenbuf) + 1;
5771ab64890Smrg	while (b->mbused + l > b->mbsize) {
578818534a1Smrg	    DTCharIndex newsize = b->mbsize ? b->mbsize * 1.5 : 1024;
579818534a1Smrg	    char *newmb = Xrealloc (b->mb, newsize);
580818534a1Smrg	    if (newmb == NULL)
5811ab64890Smrg		goto error;
582818534a1Smrg	    b->mb = newmb;
583818534a1Smrg	    b->mbsize = newsize;
5841ab64890Smrg	}
5851ab64890Smrg	rhs_string_mb = &b->mb[b->mbused];
5861ab64890Smrg	b->mbused    += l;
5871ab64890Smrg	strcpy(rhs_string_mb, tokenbuf);
5881ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
5891ab64890Smrg	if (token == KEY) {
5901ab64890Smrg	    rhs_keysym = XStringToKeysym(tokenbuf);
5911ab64890Smrg	    if (rhs_keysym == NoSymbol) {
5921ab64890Smrg		goto error;
5931ab64890Smrg	    }
5941ab64890Smrg	    token = nexttoken(fp, tokenbuf, &lastch);
5951ab64890Smrg	}
5961ab64890Smrg	if (token != ENDOFLINE && token != ENDOFFILE) {
5971ab64890Smrg	    goto error;
5981ab64890Smrg	}
5991ab64890Smrg    } else if (token == KEY) {
6001ab64890Smrg	rhs_keysym = XStringToKeysym(tokenbuf);
6011ab64890Smrg	if (rhs_keysym == NoSymbol) {
6021ab64890Smrg	    goto error;
6031ab64890Smrg	}
6041ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
6051ab64890Smrg	if (token != ENDOFLINE && token != ENDOFFILE) {
6061ab64890Smrg	    goto error;
6071ab64890Smrg	}
6081ab64890Smrg
6091ab64890Smrg        l = get_mb_string(im, local_mb_buf, rhs_keysym);
6101ab64890Smrg	while (b->mbused + l + 1 > b->mbsize) {
611818534a1Smrg	    DTCharIndex newsize = b->mbsize ? b->mbsize * 1.5 : 1024;
612818534a1Smrg	    char *newmb = Xrealloc (b->mb, newsize);
613818534a1Smrg	    if (newmb == NULL)
6141ab64890Smrg		goto error;
615818534a1Smrg	    b->mb = newmb;
616818534a1Smrg	    b->mbsize = newsize;
6171ab64890Smrg	}
6181ab64890Smrg	rhs_string_mb = &b->mb[b->mbused];
6191ab64890Smrg	b->mbused    += l + 1;
6201ab64890Smrg        memcpy(rhs_string_mb, local_mb_buf, l);
6211ab64890Smrg	rhs_string_mb[l] = '\0';
6221ab64890Smrg    } else {
6231ab64890Smrg	goto error;
6241ab64890Smrg    }
6251ab64890Smrg
6261ab64890Smrg    l = _Xmbstowcs(local_wc_buf, rhs_string_mb, LOCAL_WC_BUFSIZE - 1);
6271ab64890Smrg    if (l == LOCAL_WC_BUFSIZE - 1) {
6281ab64890Smrg	local_wc_buf[l] = (wchar_t)'\0';
6291ab64890Smrg    }
6301ab64890Smrg    while (b->wcused + l + 1 > b->wcsize) {
631818534a1Smrg	DTCharIndex newsize = b->wcsize ? b->wcsize * 1.5 : 512;
632818534a1Smrg	wchar_t *newwc = Xrealloc (b->wc, sizeof(wchar_t) * newsize);
633818534a1Smrg	if (newwc == NULL)
6341ab64890Smrg	    goto error;
635818534a1Smrg	b->wc = newwc;
636818534a1Smrg	b->wcsize = newsize;
6371ab64890Smrg    }
6381ab64890Smrg    rhs_string_wc = &b->wc[b->wcused];
6391ab64890Smrg    b->wcused    += l + 1;
6401ab64890Smrg    memcpy((char *)rhs_string_wc, (char *)local_wc_buf, (l + 1) * sizeof(wchar_t) );
6411ab64890Smrg
6421ab64890Smrg    l = _Xmbstoutf8(local_utf8_buf, rhs_string_mb, LOCAL_UTF8_BUFSIZE - 1);
6431ab64890Smrg    if (l == LOCAL_UTF8_BUFSIZE - 1) {
64461b2299dSmrg	local_utf8_buf[l] = '\0';
6451ab64890Smrg    }
6461ab64890Smrg    while (b->utf8used + l + 1 > b->utf8size) {
647818534a1Smrg	DTCharIndex newsize = b->utf8size ? b->utf8size * 1.5 : 1024;
648818534a1Smrg	char *newutf8 = Xrealloc (b->utf8, newsize);
649818534a1Smrg	if (newutf8 == NULL)
6501ab64890Smrg	    goto error;
651818534a1Smrg	b->utf8 = newutf8;
652818534a1Smrg	b->utf8size = newsize;
6531ab64890Smrg    }
6541ab64890Smrg    rhs_string_utf8 = &b->utf8[b->utf8used];
6551ab64890Smrg    b->utf8used    += l + 1;
6561ab64890Smrg    memcpy(rhs_string_utf8, local_utf8_buf, l + 1);
6571ab64890Smrg
6581ab64890Smrg    for (i = 0; i < n; i++) {
6591ab64890Smrg	for (t = *top; t; t = b->tree[t].next) {
6601ab64890Smrg	    if (buf[i].keysym        == b->tree[t].keysym &&
6611ab64890Smrg		buf[i].modifier      == b->tree[t].modifier &&
6621ab64890Smrg		buf[i].modifier_mask == b->tree[t].modifier_mask) {
6631ab64890Smrg		break;
6641ab64890Smrg	    }
6651ab64890Smrg	}
6661ab64890Smrg	if (t) {
6671ab64890Smrg	    p = &b->tree[t];
6681ab64890Smrg	    top = &p->succession;
6691ab64890Smrg	} else {
6701ab64890Smrg	    while (b->treeused >= b->treesize) {
6711ab64890Smrg		DefTree *old     = b->tree;
6721ab64890Smrg		int      oldsize = b->treesize;
673818534a1Smrg		int      newsize = b->treesize ? b->treesize * 1.5 : 256;
674818534a1Smrg		DefTree *new     = Xrealloc (b->tree, sizeof(DefTree) * newsize);
675818534a1Smrg		if (new == NULL)
6761ab64890Smrg		    goto error;
677818534a1Smrg		b->tree = new;
678818534a1Smrg		b->treesize = newsize;
679e9628295Smrg		/* Re-derive top after realloc() to avoid undefined behaviour
680e9628295Smrg		   (and crashes on architectures that track pointer bounds). */
6810efe039aSmartin		if (old && top >= (DTIndex *) old && top < (DTIndex *) &old[oldsize])
682e9628295Smrg		    top = (DTIndex *) (((char *)new) + (((char *)top)-(char *)old));
6831ab64890Smrg	    }
6841ab64890Smrg	    p = &b->tree[b->treeused];
6851ab64890Smrg	    p->keysym        = buf[i].keysym;
6861ab64890Smrg	    p->modifier      = buf[i].modifier;
6871ab64890Smrg	    p->modifier_mask = buf[i].modifier_mask;
6881ab64890Smrg	    p->succession    = 0;
6891ab64890Smrg	    p->next          = *top;
6901ab64890Smrg	    p->mb            = 0;
6911ab64890Smrg	    p->wc            = 0;
6921ab64890Smrg	    p->utf8          = 0;
6931ab64890Smrg	    p->ks            = NoSymbol;
6941ab64890Smrg	    *top = b->treeused;
6951ab64890Smrg	    top = &p->succession;
6961ab64890Smrg	    b->treeused++;
6971ab64890Smrg	}
6981ab64890Smrg    }
6991ab64890Smrg
7001ab64890Smrg    /* old entries no longer freed... */
7011ab64890Smrg    p->mb   = rhs_string_mb   - b->mb;
7021ab64890Smrg    p->wc   = rhs_string_wc   - b->wc;
7031ab64890Smrg    p->utf8 = rhs_string_utf8 - b->utf8;
7041ab64890Smrg    p->ks   = rhs_keysym;
7051ab64890Smrg    return(n);
7061ab64890Smrgerror:
7071ab64890Smrg    while (token != ENDOFLINE && token != ENDOFFILE) {
7081ab64890Smrg	token = nexttoken(fp, tokenbuf, &lastch);
7091ab64890Smrg    }
7101ab64890Smrg    return(0);
7111ab64890Smrg}
7121ab64890Smrg
7131ab64890Smrgvoid
7141ab64890Smrg_XimParseStringFile(
7151ab64890Smrg    FILE *fp,
7161ab64890Smrg    Xim   im)
717eb411b4bSmrg{
718eb411b4bSmrg    parsestringfile(fp, im, 0);
719eb411b4bSmrg}
720eb411b4bSmrg
721eb411b4bSmrgstatic void
722eb411b4bSmrgparsestringfile(
723eb411b4bSmrg    FILE *fp,
724eb411b4bSmrg    Xim   im,
725eb411b4bSmrg    int   depth)
7261ab64890Smrg{
7271ab64890Smrg    char tb[8192];
7281ab64890Smrg    char* tbp;
7291ab64890Smrg    struct stat st;
7301ab64890Smrg
7311ab64890Smrg    if (fstat (fileno (fp), &st) != -1) {
7321ab64890Smrg	unsigned long size = (unsigned long) st.st_size;
733eb411b4bSmrg	if (st.st_size >= INT_MAX)
734eb411b4bSmrg	    return;
7351ab64890Smrg	if (size <= sizeof tb) tbp = tb;
7361ab64890Smrg	else tbp = malloc (size);
7371ab64890Smrg
7381ab64890Smrg	if (tbp != NULL) {
739eb411b4bSmrg	    while (parseline(fp, im, tbp, depth) >= 0) {}
7401ab64890Smrg	    if (tbp != tb) free (tbp);
7411ab64890Smrg	}
7421ab64890Smrg    }
7431ab64890Smrg}
744