11ab64890Smrg/******************************************************************
21ab64890Smrg
31ab64890Smrg          Copyright 1992, 1993, 1994 by FUJITSU LIMITED
41ab64890Smrg          Copyright 1993 by Digital Equipment Corporation
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 that
91ab64890Smrgboth that copyright notice and this permission notice appear in
101ab64890Smrgsupporting documentation, and that the name of FUJITSU LIMITED and
111ab64890SmrgDigital Equipment Corporation not be used in advertising or publicity
121ab64890Smrgpertaining to distribution of the software without specific, written
131ab64890Smrgprior permission.  FUJITSU LIMITED and Digital Equipment Corporation
141ab64890Smrgmakes no representations about the suitability of this software for
151ab64890Smrgany purpose.  It is provided "as is" without express or implied
161ab64890Smrgwarranty.
171ab64890Smrg
1861b2299dSmrgFUJITSU LIMITED AND DIGITAL EQUIPMENT CORPORATION DISCLAIM ALL
1961b2299dSmrgWARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
2061b2299dSmrgWARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
2161b2299dSmrgFUJITSU LIMITED AND DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR
2261b2299dSmrgANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2361b2299dSmrgWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
2461b2299dSmrgIN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
2561b2299dSmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
261ab64890SmrgTHIS SOFTWARE.
271ab64890Smrg
2861b2299dSmrg  Author:    Takashi Fujiwara     FUJITSU LIMITED
291ab64890Smrg                               	  fujiwara@a80.tech.yk.fujitsu.co.jp
301ab64890Smrg  Modifier:  Franky Ling          Digital Equipment Corporation
311ab64890Smrg	                          frankyling@hgrd01.enet.dec.com
321ab64890Smrg
331ab64890Smrg******************************************************************/
341ab64890Smrg
351ab64890Smrg#ifdef HAVE_CONFIG_H
361ab64890Smrg#include <config.h>
371ab64890Smrg#endif
381ab64890Smrg#include <stdio.h>
391ab64890Smrg
401ab64890Smrg#include <X11/Xmd.h>
411ab64890Smrg#include <X11/Xatom.h>
421ab64890Smrg#include <X11/Xos.h>
431ab64890Smrg#include "Xlibint.h"
441ab64890Smrg#include "Xlcint.h"
451ab64890Smrg#include "XlcPublic.h"
461ab64890Smrg#include "XlcPubI.h"
471ab64890Smrg#include "Ximint.h"
481ab64890Smrg#include <ctype.h>
491ab64890Smrg#include <assert.h>
501ab64890Smrg
511ab64890Smrg#ifdef COMPOSECACHE
521ab64890Smrg#  include <sys/types.h>
531ab64890Smrg#  include <sys/stat.h>
541ab64890Smrg#  include <sys/mman.h>
551ab64890Smrg#  include <langinfo.h>
561ab64890Smrg#endif
571ab64890Smrg
581ab64890Smrg
591ab64890Smrg#ifdef COMPOSECACHE
601ab64890Smrg
611ab64890Smrg/* include trailing '/' for cache directory, file prefix otherwise */
621ab64890Smrg#define XIM_GLOBAL_CACHE_DIR "/var/cache/libx11/compose/"
631ab64890Smrg#define XIM_HOME_CACHE_DIR   "/.compose-cache/"
641ab64890Smrg#define XIM_CACHE_MAGIC      ('X' | 'i'<<8 | 'm'<<16 | 'C'<<24)
651ab64890Smrg#define XIM_CACHE_VERSION    4
661ab64890Smrg#define XIM_CACHE_TREE_ALIGNMENT 4
671ab64890Smrg
681ab64890Smrg#define XIM_HASH_PRIME_1 13
691ab64890Smrg#define XIM_HASH_PRIME_2 1234096939
701ab64890Smrg
711ab64890Smrgtypedef INT32 DTStructIndex;
721ab64890Smrgstruct _XimCacheStruct {
731ab64890Smrg    INT32           id;
741ab64890Smrg    INT32           version;
751ab64890Smrg    DTStructIndex   tree;
761ab64890Smrg    DTStructIndex   mb;
771ab64890Smrg    DTStructIndex   wc;
781ab64890Smrg    DTStructIndex   utf8;
791ab64890Smrg    DTStructIndex   size;
801ab64890Smrg    DTIndex         top;
811ab64890Smrg    DTIndex         treeused;
821ab64890Smrg    DTCharIndex     mbused;
831ab64890Smrg    DTCharIndex     wcused;
841ab64890Smrg    DTCharIndex     utf8used;
853233502eSmrg    char            fname[];
863233502eSmrg    /* char encoding[] */
871ab64890Smrg};
881ab64890Smrg
89eb411b4bSmrgstatic struct  _XimCacheStruct* _XimCache_mmap = NULL;
90eb411b4bSmrgstatic DefTreeBase _XimCachedDefaultTreeBase;
91eb411b4bSmrgstatic int     _XimCachedDefaultTreeRefcount = 0;
921ab64890Smrg
931ab64890Smrg#endif
941ab64890Smrg
951ab64890Smrg
96eb411b4bSmrgBool
9761b2299dSmrg_XimCheckIfLocalProcessing(Xim im)
981ab64890Smrg{
991ab64890Smrg    FILE        *fp;
1001ab64890Smrg    char        *name;
1011ab64890Smrg
1021ab64890Smrg    if(strcmp(im->core.im_name, "") == 0) {
1031ab64890Smrg	name = _XlcFileName(im->core.lcd, COMPOSE_FILE);
1041ab64890Smrg	if (name != (char *)NULL) {
1051ab64890Smrg	    fp = _XFopenFile (name, "r");
1061ab64890Smrg	    Xfree(name);
1071ab64890Smrg	    if (fp != (FILE *)NULL) {
1081ab64890Smrg		fclose(fp);
1091ab64890Smrg		return(True);
1101ab64890Smrg	    }
1111ab64890Smrg	}
1121ab64890Smrg	return(False);
1131ab64890Smrg    } else if(strcmp(im->core.im_name, "local") == 0 ||
1141ab64890Smrg	      strcmp(im->core.im_name, "none" ) == 0 ) {
1151ab64890Smrg	return(True);
1161ab64890Smrg    }
1171ab64890Smrg    return(False);
1181ab64890Smrg}
1191ab64890Smrg
120eb411b4bSmrgstatic void
1211ab64890SmrgXimFreeDefaultTree(
1221ab64890Smrg    DefTreeBase *b)
1231ab64890Smrg{
1241ab64890Smrg    if (!b) return;
1251ab64890Smrg    if (b->tree == NULL) return;
1261ab64890Smrg#ifdef COMPOSECACHE
1271ab64890Smrg    if (b->tree == _XimCachedDefaultTreeBase.tree) {
1281ab64890Smrg        _XimCachedDefaultTreeRefcount--;
1291ab64890Smrg        /* No deleting, it's a cache after all. */
1301ab64890Smrg        return;
1311ab64890Smrg    }
1321ab64890Smrg#endif
1331ab64890Smrg    Xfree (b->tree);
1341ab64890Smrg    b->tree = NULL;
1350f8248bfSmrg    Xfree (b->mb);
1361ab64890Smrg    b->mb   = NULL;
1370f8248bfSmrg    Xfree (b->wc);
1381ab64890Smrg    b->wc   = NULL;
1390f8248bfSmrg    Xfree (b->utf8);
1401ab64890Smrg    b->utf8 = NULL;
1410f8248bfSmrg
1421ab64890Smrg    b->treeused = b->treesize = 0;
1431ab64890Smrg    b->mbused   = b->mbsize   = 0;
1441ab64890Smrg    b->wcused   = b->wcsize   = 0;
1451ab64890Smrg    b->utf8used = b->utf8size = 0;
1461ab64890Smrg}
1471ab64890Smrg
148eb411b4bSmrgvoid
1491ab64890Smrg_XimLocalIMFree(
1501ab64890Smrg    Xim		im)
1511ab64890Smrg{
1521ab64890Smrg    XimFreeDefaultTree(&im->private.local.base);
1531ab64890Smrg    im->private.local.top = 0;
1541ab64890Smrg
1550f8248bfSmrg    Xfree(im->core.im_resources);
1560f8248bfSmrg    im->core.im_resources = NULL;
1570f8248bfSmrg
1580f8248bfSmrg    Xfree(im->core.ic_resources);
1590f8248bfSmrg    im->core.ic_resources = NULL;
1600f8248bfSmrg
1610f8248bfSmrg    Xfree(im->core.im_values_list);
1620f8248bfSmrg    im->core.im_values_list = NULL;
1630f8248bfSmrg
1640f8248bfSmrg    Xfree(im->core.ic_values_list);
1650f8248bfSmrg    im->core.ic_values_list = NULL;
1660f8248bfSmrg
1670f8248bfSmrg    Xfree(im->core.styles);
1680f8248bfSmrg    im->core.styles = NULL;
1690f8248bfSmrg
1700f8248bfSmrg    Xfree(im->core.res_name);
1710f8248bfSmrg    im->core.res_name = NULL;
1720f8248bfSmrg
1730f8248bfSmrg    Xfree(im->core.res_class);
1740f8248bfSmrg    im->core.res_class = NULL;
1750f8248bfSmrg
1760f8248bfSmrg    Xfree(im->core.im_name);
1770f8248bfSmrg    im->core.im_name = NULL;
1780f8248bfSmrg
1791ab64890Smrg    if (im->private.local.ctom_conv) {
1801ab64890Smrg	_XlcCloseConverter(im->private.local.ctom_conv);
1811ab64890Smrg        im->private.local.ctom_conv = NULL;
1821ab64890Smrg    }
1831ab64890Smrg    if (im->private.local.ctow_conv) {
1841ab64890Smrg	_XlcCloseConverter(im->private.local.ctow_conv);
1851ab64890Smrg	im->private.local.ctow_conv = NULL;
1861ab64890Smrg    }
1871ab64890Smrg    if (im->private.local.ctoutf8_conv) {
1881ab64890Smrg	_XlcCloseConverter(im->private.local.ctoutf8_conv);
1891ab64890Smrg	im->private.local.ctoutf8_conv = NULL;
1901ab64890Smrg    }
1911ab64890Smrg    if (im->private.local.cstomb_conv) {
1921ab64890Smrg	_XlcCloseConverter(im->private.local.cstomb_conv);
1931ab64890Smrg        im->private.local.cstomb_conv = NULL;
1941ab64890Smrg    }
1951ab64890Smrg    if (im->private.local.cstowc_conv) {
1961ab64890Smrg	_XlcCloseConverter(im->private.local.cstowc_conv);
1971ab64890Smrg	im->private.local.cstowc_conv = NULL;
1981ab64890Smrg    }
1991ab64890Smrg    if (im->private.local.cstoutf8_conv) {
2001ab64890Smrg	_XlcCloseConverter(im->private.local.cstoutf8_conv);
2011ab64890Smrg	im->private.local.cstoutf8_conv = NULL;
2021ab64890Smrg    }
2031ab64890Smrg    if (im->private.local.ucstoc_conv) {
2041ab64890Smrg	_XlcCloseConverter(im->private.local.ucstoc_conv);
2051ab64890Smrg	im->private.local.ucstoc_conv = NULL;
2061ab64890Smrg    }
2071ab64890Smrg    if (im->private.local.ucstoutf8_conv) {
2081ab64890Smrg	_XlcCloseConverter(im->private.local.ucstoutf8_conv);
2091ab64890Smrg	im->private.local.ucstoutf8_conv = NULL;
2101ab64890Smrg    }
2111ab64890Smrg    return;
2121ab64890Smrg}
2131ab64890Smrg
214eb411b4bSmrgstatic Status
2151ab64890Smrg_XimLocalCloseIM(
2161ab64890Smrg    XIM		xim)
2171ab64890Smrg{
2181ab64890Smrg    Xim		im = (Xim)xim;
2191ab64890Smrg    XIC		ic;
2201ab64890Smrg    XIC		next;
2211ab64890Smrg
2221ab64890Smrg    ic = im->core.ic_chain;
2231ab64890Smrg    im->core.ic_chain = NULL;
2241ab64890Smrg    while (ic) {
2251ab64890Smrg	(*ic->methods->destroy) (ic);
2261ab64890Smrg	next = ic->core.next;
227818534a1Smrg	Xfree (ic);
2281ab64890Smrg	ic = next;
2291ab64890Smrg    }
2301ab64890Smrg    _XimLocalIMFree(im);
2311ab64890Smrg    _XimDestroyIMStructureList(im);
2321ab64890Smrg    return(True);
2331ab64890Smrg}
2341ab64890Smrg
235eb411b4bSmrgchar *
2361ab64890Smrg_XimLocalGetIMValues(
2371ab64890Smrg    XIM			 xim,
2381ab64890Smrg    XIMArg		*values)
2391ab64890Smrg{
2401ab64890Smrg    Xim			 im = (Xim)xim;
2411ab64890Smrg    XimDefIMValues	 im_values;
2421ab64890Smrg
2431ab64890Smrg    _XimGetCurrentIMValues(im, &im_values);
2441ab64890Smrg    return(_XimGetIMValueData(im, (XPointer)&im_values, values,
2451ab64890Smrg			im->core.im_resources, im->core.im_num_resources));
2461ab64890Smrg}
2471ab64890Smrg
248eb411b4bSmrgchar *
2491ab64890Smrg_XimLocalSetIMValues(
2501ab64890Smrg    XIM			 xim,
2511ab64890Smrg    XIMArg		*values)
2521ab64890Smrg{
2531ab64890Smrg    Xim			 im = (Xim)xim;
2541ab64890Smrg    XimDefIMValues	 im_values;
2551ab64890Smrg    char		*name = (char *)NULL;
2561ab64890Smrg
2571ab64890Smrg    _XimGetCurrentIMValues(im, &im_values);
2581ab64890Smrg    name = _XimSetIMValueData(im, (XPointer)&im_values, values,
2591ab64890Smrg		im->core.im_resources, im->core.im_num_resources);
2601ab64890Smrg    _XimSetCurrentIMValues(im, &im_values);
2611ab64890Smrg    return(name);
2621ab64890Smrg}
2631ab64890Smrg
2641ab64890Smrg
2651ab64890Smrg#ifdef COMPOSECACHE
2661ab64890Smrg
267eb411b4bSmrgstatic Bool
2681ab64890Smrg_XimReadCachedDefaultTree(
2691ab64890Smrg    int          fd_cache,
2701ab64890Smrg    const char  *name,
2711ab64890Smrg    const char  *encoding,
2721ab64890Smrg    DTStructIndex size)
2731ab64890Smrg{
2741ab64890Smrg    struct _XimCacheStruct* m;
2751ab64890Smrg    int namelen = strlen (name) + 1;
2761ab64890Smrg    int encodinglen = strlen (encoding) + 1;
2771ab64890Smrg
2781ab64890Smrg    m = mmap (NULL, size, PROT_READ, MAP_PRIVATE, fd_cache, 0);
2791ab64890Smrg    if (m == NULL || m == MAP_FAILED)
2801ab64890Smrg        return False;
2811ab64890Smrg    assert (m->id == XIM_CACHE_MAGIC);
2821ab64890Smrg    assert (m->version == XIM_CACHE_VERSION);
2831ab64890Smrg    if (size != m->size ||
2843233502eSmrg	size < sizeof (struct _XimCacheStruct) + namelen + encodinglen) {
2851ab64890Smrg	fprintf (stderr, "Ignoring broken XimCache %s [%s]\n", name, encoding);
2861ab64890Smrg        munmap (m, size);
2871ab64890Smrg        return False;
2881ab64890Smrg    }
2891ab64890Smrg    if (strncmp (name, m->fname, namelen) != 0) {
2901ab64890Smrg	/* m->fname may *not* be terminated - but who cares here */
2911ab64890Smrg	fprintf (stderr, "Filename hash clash - expected %s, got %s\n",
2921ab64890Smrg		 name, m->fname);
2931ab64890Smrg        munmap (m, size);
2941ab64890Smrg        return False;
2951ab64890Smrg    }
2961ab64890Smrg    if (strncmp (encoding, m->fname + namelen, encodinglen) != 0) {
2971ab64890Smrg	/* m->fname+namelen may *not* be terminated - but who cares here */
2981ab64890Smrg	fprintf (stderr, "Enoding hash clash - expected %s, got %s\n",
2991ab64890Smrg		 encoding, m->fname + namelen);
3001ab64890Smrg        munmap (m, size);
3011ab64890Smrg        return False;
3021ab64890Smrg    }
3031ab64890Smrg    _XimCache_mmap    = m;
3041ab64890Smrg    _XimCachedDefaultTreeBase.tree = (DefTree *) (((char *) m) + m->tree);
3051ab64890Smrg    _XimCachedDefaultTreeBase.mb   =             (((char *) m) + m->mb);
3061ab64890Smrg    _XimCachedDefaultTreeBase.wc   = (wchar_t *) (((char *) m) + m->wc);
3071ab64890Smrg    _XimCachedDefaultTreeBase.utf8 =             (((char *) m) + m->utf8);
3081ab64890Smrg    _XimCachedDefaultTreeBase.treeused = m->treeused;
3091ab64890Smrg    _XimCachedDefaultTreeBase.mbused   = m->mbused;
3101ab64890Smrg    _XimCachedDefaultTreeBase.wcused   = m->wcused;
3111ab64890Smrg    _XimCachedDefaultTreeBase.utf8used = m->utf8used;
3121ab64890Smrg    /* treesize etc. is ignored because only used during parsing */
3131ab64890Smrg    _XimCachedDefaultTreeRefcount = 0;
3141ab64890Smrg/* fprintf (stderr, "read cached tree at %p: %s\n", (void *) m, name); */
3151ab64890Smrg    return True;
3161ab64890Smrg}
3171ab64890Smrg
318eb411b4bSmrgstatic unsigned int strToHash (
3191ab64890Smrg    const char *name)
3201ab64890Smrg{
3211ab64890Smrg    unsigned int hash = 0;
3221ab64890Smrg    while (*name)
3231ab64890Smrg	hash = hash * XIM_HASH_PRIME_1 + *(unsigned const char *)name++;
3241ab64890Smrg    return hash % XIM_HASH_PRIME_2;
3251ab64890Smrg}
3261ab64890Smrg
3271ab64890Smrg
3281ab64890Smrg/* Returns read-only fd of cache file, -1 if none.
3291ab64890Smrg * Sets *res to cache filename if safe. Sets *size to file size of cache. */
330eb411b4bSmrgstatic int _XimCachedFileName (
3311ab64890Smrg    const char *dir, const char *name,
3321ab64890Smrg    const char *intname, const char *encoding,
3331ab64890Smrg    uid_t uid, int isglobal, char **res, off_t *size)
3341ab64890Smrg{
3351ab64890Smrg    struct stat st_name, st;
3361ab64890Smrg    int    fd;
3371ab64890Smrg    unsigned int len, hash, hash2;
3381ab64890Smrg    struct _XimCacheStruct *m;
3391ab64890Smrg    /* There are some races here with 'dir', but we are either in our own home
3401ab64890Smrg     * or the global cache dir, and not inside some public writable dir */
3411ab64890Smrg/* fprintf (stderr, "XimCachedFileName for dir %s name %s intname %s encoding %s uid %d\n", dir, name, intname, encoding, uid); */
3421ab64890Smrg    if (stat (name, &st_name) == -1 || ! S_ISREG (st_name.st_mode)
3431ab64890Smrg       || stat (dir, &st) == -1 || ! S_ISDIR (st.st_mode) || st.st_uid != uid
3441ab64890Smrg       || (st.st_mode & 0022) != 0000) {
3451ab64890Smrg       *res = NULL;
3461ab64890Smrg       return -1;
3471ab64890Smrg    }
3481ab64890Smrg    len   = strlen (dir);
3491ab64890Smrg    hash  = strToHash (intname);
3501ab64890Smrg    hash2 = strToHash (encoding);
3511ab64890Smrg    *res  = Xmalloc (len + 1 + 27 + 1);  /* Max VERSION 9999 */
3521ab64890Smrg
3531ab64890Smrg    if (len == 0 || dir [len-1] != '/')
3541ab64890Smrg       sprintf (*res, "%s/%c%d_%03x_%08x_%08x", dir, _XimGetMyEndian(),
35588de56ccSmrg		XIM_CACHE_VERSION, (unsigned int)sizeof (DefTree), hash, hash2);
3561ab64890Smrg    else
3571ab64890Smrg       sprintf (*res, "%s%c%d_%03x_%08x_%08x", dir, _XimGetMyEndian(),
35888de56ccSmrg		XIM_CACHE_VERSION, (unsigned int)sizeof (DefTree), hash, hash2);
35961b2299dSmrg
3601ab64890Smrg/* fprintf (stderr, "-> %s\n", *res); */
3611ab64890Smrg    if ( (fd = _XOpenFile (*res, O_RDONLY)) == -1)
3621ab64890Smrg       return -1;
3631ab64890Smrg
3641ab64890Smrg    if (fstat (fd, &st) == -1) {
3651ab64890Smrg       Xfree (*res);
3661ab64890Smrg       *res = NULL;
3671ab64890Smrg       close (fd);
3681ab64890Smrg       return -1;
3691ab64890Smrg    }
3701ab64890Smrg    *size = st.st_size;
3711ab64890Smrg
3721ab64890Smrg    if (! S_ISREG (st.st_mode) || st.st_uid != uid
3731ab64890Smrg       || (st.st_mode & 0022) != 0000 || st.st_mtime <= st_name.st_mtime
3741ab64890Smrg       || (st.st_mtime < time (NULL) - 24*60*60 && ! isglobal)) {
3751ab64890Smrg
3761ab64890Smrg       close (fd);
3771ab64890Smrg       if (unlink (*res) != 0) {
3781ab64890Smrg           Xfree (*res);
3791ab64890Smrg           *res = NULL;                /* cache is not safe */
3801ab64890Smrg       }
3811ab64890Smrg       return -1;
3821ab64890Smrg    }
3831ab64890Smrg
3841ab64890Smrg    m = mmap (NULL, sizeof (struct _XimCacheStruct), PROT_READ, MAP_PRIVATE,
3851ab64890Smrg	      fd, 0);
3861ab64890Smrg    if (m == NULL || m == MAP_FAILED) {
3871ab64890Smrg	close (fd);
3881ab64890Smrg	Xfree (*res);
3891ab64890Smrg	*res = NULL;
3901ab64890Smrg        return -1;
3911ab64890Smrg    }
3921ab64890Smrg    if (*size < sizeof (struct _XimCacheStruct) || m->id != XIM_CACHE_MAGIC) {
3931ab64890Smrg	munmap (m, sizeof (struct _XimCacheStruct));
3941ab64890Smrg	close (fd);
3951ab64890Smrg	fprintf (stderr, "Ignoring broken XimCache %s\n", *res);
3961ab64890Smrg	Xfree (*res);
3971ab64890Smrg	*res = NULL;
3981ab64890Smrg        return -1;
3991ab64890Smrg    }
4001ab64890Smrg    if (m->version != XIM_CACHE_VERSION) {
4011ab64890Smrg	munmap (m, sizeof (struct _XimCacheStruct));
4021ab64890Smrg	close (fd);
4031ab64890Smrg	if (unlink (*res) != 0) {
4041ab64890Smrg	    Xfree (*res);
4051ab64890Smrg	    *res = NULL;                /* cache is not safe */
4061ab64890Smrg	}
4071ab64890Smrg	return -1;
4081ab64890Smrg    }
4091ab64890Smrg    munmap (m, sizeof (struct _XimCacheStruct));
41061b2299dSmrg
4111ab64890Smrg    return fd;
4121ab64890Smrg}
4131ab64890Smrg
4141ab64890Smrg
415eb411b4bSmrgstatic Bool _XimLoadCache (
4161ab64890Smrg    int         fd,
4171ab64890Smrg    const char *name,
4181ab64890Smrg    const char *encoding,
4191ab64890Smrg    off_t       size,
4201ab64890Smrg    Xim         im)
4211ab64890Smrg{
4221ab64890Smrg    if (_XimCache_mmap ||
4231ab64890Smrg       _XimReadCachedDefaultTree (fd, name, encoding, size)) {
4241ab64890Smrg       _XimCachedDefaultTreeRefcount++;
4251ab64890Smrg       memcpy (&im->private.local.base, &_XimCachedDefaultTreeBase,
4261ab64890Smrg	       sizeof (_XimCachedDefaultTreeBase));
4271ab64890Smrg       im->private.local.top = _XimCache_mmap->top;
4281ab64890Smrg       return True;
4291ab64890Smrg    }
4301ab64890Smrg
4311ab64890Smrg    return False;
4321ab64890Smrg}
4331ab64890Smrg
4341ab64890Smrg
435eb411b4bSmrgstatic void
4361ab64890Smrg_XimWriteCachedDefaultTree(
4371ab64890Smrg    const char *name,
4381ab64890Smrg    const char *encoding,
4391ab64890Smrg    const char *cachename,
4401ab64890Smrg    Xim                im)
4411ab64890Smrg{
4421ab64890Smrg    int   fd;
4431ab64890Smrg    FILE *fp;
4441ab64890Smrg    struct _XimCacheStruct *m;
4453233502eSmrg    int   msize = (sizeof(struct _XimCacheStruct)
4461ab64890Smrg		   + strlen(name) + strlen(encoding) + 2
4471ab64890Smrg		   + XIM_CACHE_TREE_ALIGNMENT-1) & -XIM_CACHE_TREE_ALIGNMENT;
4481ab64890Smrg    DefTreeBase *b = &im->private.local.base;
4491ab64890Smrg
4502d67cb4fSmrg    if (! b->tree && ! (b->tree = Xcalloc (1, sizeof(DefTree))) )
4511ab64890Smrg	return;
4521ab64890Smrg    if (! b->mb   && ! (b->mb   = Xmalloc (1)) )
4531ab64890Smrg	return;
4541ab64890Smrg    if (! b->wc   && ! (b->wc   = Xmalloc (sizeof(wchar_t))) )
4551ab64890Smrg	return;
4561ab64890Smrg    if (! b->utf8 && ! (b->utf8 = Xmalloc (1)) )
4571ab64890Smrg	return;
4581ab64890Smrg
4591ab64890Smrg    /* First entry is always unused */
4601ab64890Smrg    b->mb[0]   = 0;
4611ab64890Smrg    b->wc[0]   = 0;
4621ab64890Smrg    b->utf8[0] = 0;
4631ab64890Smrg
4642d67cb4fSmrg    m = Xcalloc (1, msize);
4651ab64890Smrg    m->id       = XIM_CACHE_MAGIC;
4661ab64890Smrg    m->version  = XIM_CACHE_VERSION;
4671ab64890Smrg    m->top      = im->private.local.top;
4681ab64890Smrg    m->treeused = b->treeused;
4691ab64890Smrg    m->mbused   = b->mbused;
4701ab64890Smrg    m->wcused   = b->wcused;
4711ab64890Smrg    m->utf8used = b->utf8used;
4721ab64890Smrg    /* Tree first, then wide chars, then the rest due to alignment */
4731ab64890Smrg    m->tree     = msize;
4741ab64890Smrg    m->wc       = msize   + sizeof (DefTree) * m->treeused;
4751ab64890Smrg    m->mb       = m->wc   + sizeof (wchar_t) * m->wcused;
4761ab64890Smrg    m->utf8     = m->mb   +                    m->mbused;
4771ab64890Smrg    m->size     = m->utf8 +                    m->utf8used;
4781ab64890Smrg    strcpy (m->fname, name);
4791ab64890Smrg    strcpy (m->fname+strlen(name)+1, encoding);
4801ab64890Smrg
4811ab64890Smrg    /* This STILL might be racy on NFS */
4821ab64890Smrg    if ( (fd = _XOpenFileMode (cachename, O_WRONLY | O_CREAT | O_EXCL,
4836cc2b21fSmrg			       0600)) < 0) {
4846cc2b21fSmrg       Xfree(m);
4851ab64890Smrg       return;
4866cc2b21fSmrg    }
4871ab64890Smrg    if (! (fp = fdopen (fd, "wb")) ) {
4881ab64890Smrg       close (fd);
4896cc2b21fSmrg       Xfree(m);
4901ab64890Smrg       return;
4911ab64890Smrg    }
4921ab64890Smrg    fwrite (m, msize, 1, fp);
4931ab64890Smrg    fwrite (im->private.local.base.tree, sizeof(DefTree), m->treeused, fp);
4941ab64890Smrg    fwrite (im->private.local.base.wc,   sizeof(wchar_t), m->wcused,   fp);
4951ab64890Smrg    fwrite (im->private.local.base.mb,   1,               m->mbused,   fp);
4961ab64890Smrg    fwrite (im->private.local.base.utf8, 1,               m->utf8used, fp);
4971ab64890Smrg    if (fclose (fp) != 0)
4981ab64890Smrg	unlink (cachename);
4991ab64890Smrg    _XimCache_mmap = m;
5001ab64890Smrg    memcpy (&_XimCachedDefaultTreeBase, &im->private.local.base,
5011ab64890Smrg	    sizeof (_XimCachedDefaultTreeBase));
5021ab64890Smrg/* fprintf (stderr, "wrote tree %s size %ld to %s\n", name, m->size, cachename); */
5031ab64890Smrg}
5041ab64890Smrg
5051ab64890Smrg#endif
5061ab64890Smrg
5071ab64890Smrg
508eb411b4bSmrgstatic void
5091ab64890Smrg_XimCreateDefaultTree(
5101ab64890Smrg    Xim		im)
5111ab64890Smrg{
5121ab64890Smrg    FILE *fp = NULL;
5131ab64890Smrg    char *name, *tmpname = NULL, *intname;
5141ab64890Smrg    char *cachename = NULL;
5151ab64890Smrg    /* Should use getpwent() instead of $HOME (cross-platform?) */
5161ab64890Smrg    char *home = getenv("HOME");
5171ab64890Smrg    char *cachedir = NULL;
5181ab64890Smrg    char *tmpcachedir = NULL;
5191ab64890Smrg    int   hl = home ? strlen (home) : 0;
5201ab64890Smrg#ifdef COMPOSECACHE
5211ab64890Smrg    const char *encoding = nl_langinfo (CODESET);
5221ab64890Smrg    uid_t euid = geteuid ();
5231ab64890Smrg    gid_t egid = getegid ();
5241ab64890Smrg    int   cachefd = -1;
5251ab64890Smrg    off_t size;
5261ab64890Smrg#endif
5271ab64890Smrg
5281ab64890Smrg    name = getenv("XCOMPOSEFILE");
5291ab64890Smrg    if (name == (char *) NULL) {
5301ab64890Smrg    	if (home != (char *) NULL) {
5311ab64890Smrg            tmpname = name = Xmalloc(hl + 10 + 1);
5321ab64890Smrg            if (name != (char *) NULL) {
5331ab64890Smrg		int fd;
5341ab64890Smrg            	strcpy(name, home);
5351ab64890Smrg            	strcpy(name + hl, "/.XCompose");
5361ab64890Smrg		if ( (fd = _XOpenFile (name, O_RDONLY)) < 0) {
5371ab64890Smrg		    Xfree (name);
5381ab64890Smrg		    name = tmpname = NULL;
5391ab64890Smrg		} else
5401ab64890Smrg		    close (fd);
5411ab64890Smrg            }
5421ab64890Smrg        }
5431ab64890Smrg    }
5441ab64890Smrg
5451ab64890Smrg    if (name == (char *) NULL) {
5461ab64890Smrg        tmpname = name = _XlcFileName(im->core.lcd, COMPOSE_FILE);
5471ab64890Smrg    }
5481ab64890Smrg    intname = name;
54961b2299dSmrg
5501ab64890Smrg#ifdef COMPOSECACHE
5511ab64890Smrg    if (getuid () == euid && getgid () == egid && euid != 0) {
5521ab64890Smrg	char *c;
5531ab64890Smrg	/* Usage: XCOMPOSECACHE=<cachedir>[=<filename>]
5541ab64890Smrg	 * cachedir: directory of cache files
5551ab64890Smrg	 * filename: internally used name for cache file */
5561ab64890Smrg        cachedir = getenv("XCOMPOSECACHE");
5571ab64890Smrg	if (cachedir && (c = strchr (cachedir, '='))) {
5581ab64890Smrg	    tmpcachedir = strdup (cachedir);
5591ab64890Smrg	    intname = tmpcachedir + (c-cachedir) + 1;
5601ab64890Smrg	    tmpcachedir[c-cachedir] = '\0';
5611ab64890Smrg	    cachedir = tmpcachedir;
5621ab64890Smrg	}
5631ab64890Smrg    }
5641ab64890Smrg
5651ab64890Smrg    if (! cachedir) {
5661ab64890Smrg	cachefd = _XimCachedFileName (XIM_GLOBAL_CACHE_DIR, name, intname,
5671ab64890Smrg				      encoding, 0, 1, &cachename, &size);
5681ab64890Smrg	if (cachefd != -1) {
5691ab64890Smrg	    if (_XimLoadCache (cachefd, intname, encoding, size, im)) {
5700f8248bfSmrg	        Xfree (tmpcachedir);
5710f8248bfSmrg		Xfree (tmpname);
5720f8248bfSmrg		Xfree (cachename);
5731ab64890Smrg		close (cachefd);
5741ab64890Smrg		return;
5751ab64890Smrg	    }
5761ab64890Smrg	    close (cachefd);
5771ab64890Smrg	}
5780f8248bfSmrg	Xfree (cachename);
5791ab64890Smrg	cachename = NULL;
5801ab64890Smrg    }
58161b2299dSmrg
5821ab64890Smrg    if (getuid () == euid && getgid () == egid && euid != 0 && home) {
5831ab64890Smrg
5841ab64890Smrg	if (! cachedir) {
5851ab64890Smrg	    tmpcachedir = cachedir = Xmalloc (hl+strlen(XIM_HOME_CACHE_DIR)+1);
5861ab64890Smrg	    strcpy (cachedir, home);
5871ab64890Smrg	    strcat (cachedir, XIM_HOME_CACHE_DIR);
5881ab64890Smrg	}
5891ab64890Smrg	cachefd = _XimCachedFileName (cachedir, name, intname, encoding,
5901ab64890Smrg				      euid, 0, &cachename, &size);
5911ab64890Smrg	if (cachefd != -1) {
5921ab64890Smrg	    if (_XimLoadCache (cachefd, intname, encoding, size, im)) {
5930f8248bfSmrg	        Xfree (tmpcachedir);
5940f8248bfSmrg		Xfree (tmpname);
5950f8248bfSmrg		Xfree (cachename);
5961ab64890Smrg		close (cachefd);
5971ab64890Smrg		return;
5981ab64890Smrg	    }
5991ab64890Smrg	    close (cachefd);
6001ab64890Smrg	}
6011ab64890Smrg    }
6021ab64890Smrg#endif
6031ab64890Smrg
6041ab64890Smrg    if (! (fp = _XFopenFile (name, "r"))) {
6050f8248bfSmrg	Xfree (tmpcachedir);
6060f8248bfSmrg	Xfree (tmpname);
6070f8248bfSmrg	Xfree (cachename);
6081ab64890Smrg        return;
6091ab64890Smrg    }
6101ab64890Smrg    _XimParseStringFile(fp, im);
6111ab64890Smrg    fclose(fp);
6121ab64890Smrg
6131ab64890Smrg#ifdef COMPOSECACHE
6141ab64890Smrg    if (cachename) {
6151ab64890Smrg	assert (euid != 0);
6161ab64890Smrg	_XimWriteCachedDefaultTree (intname, encoding, cachename, im);
6171ab64890Smrg    }
6181ab64890Smrg#endif
61961b2299dSmrg
6200f8248bfSmrg    Xfree (tmpcachedir);
6210f8248bfSmrg    Xfree (tmpname);
6220f8248bfSmrg    Xfree (cachename);
6231ab64890Smrg}
6241ab64890Smrg
625eb411b4bSmrgstatic XIMMethodsRec      Xim_im_local_methods = {
6261ab64890Smrg    _XimLocalCloseIM,           /* close */
6271ab64890Smrg    _XimLocalSetIMValues,       /* set_values */
6281ab64890Smrg    _XimLocalGetIMValues,       /* get_values */
6291ab64890Smrg    _XimLocalCreateIC,          /* create_ic */
6301ab64890Smrg    _XimLcctstombs,		/* ctstombs */
6311ab64890Smrg    _XimLcctstowcs,		/* ctstowcs */
6321ab64890Smrg    _XimLcctstoutf8		/* ctstoutf8 */
6331ab64890Smrg};
6341ab64890Smrg
635eb411b4bSmrgBool
6361ab64890Smrg_XimLocalOpenIM(
6371ab64890Smrg    Xim			 im)
6381ab64890Smrg{
6391ab64890Smrg    XLCd		 lcd = im->core.lcd;
6401ab64890Smrg    XlcConv		 conv;
6411ab64890Smrg    XimDefIMValues	 im_values;
6421ab64890Smrg    XimLocalPrivateRec*  private = &im->private.local;
6431ab64890Smrg
6441ab64890Smrg    _XimInitialResourceInfo();
6451ab64890Smrg    if(_XimSetIMResourceList(&im->core.im_resources,
6461ab64890Smrg		 		&im->core.im_num_resources) == False) {
6471ab64890Smrg	goto Open_Error;
6481ab64890Smrg    }
6491ab64890Smrg    if(_XimSetICResourceList(&im->core.ic_resources,
6501ab64890Smrg				&im->core.ic_num_resources) == False) {
6511ab64890Smrg	goto Open_Error;
6521ab64890Smrg    }
6531ab64890Smrg
6541ab64890Smrg    _XimSetIMMode(im->core.im_resources, im->core.im_num_resources);
6551ab64890Smrg
6561ab64890Smrg    _XimGetCurrentIMValues(im, &im_values);
6571ab64890Smrg    if(_XimSetLocalIMDefaults(im, (XPointer)&im_values,
6581ab64890Smrg		im->core.im_resources, im->core.im_num_resources) == False) {
6591ab64890Smrg	goto Open_Error;
6601ab64890Smrg    }
6611ab64890Smrg    _XimSetCurrentIMValues(im, &im_values);
6621ab64890Smrg
6631ab64890Smrg    if (!(conv = _XlcOpenConverter(lcd,	XlcNCompoundText, lcd, XlcNMultiByte)))
6641ab64890Smrg	goto Open_Error;
6651ab64890Smrg    private->ctom_conv = conv;
6661ab64890Smrg
6671ab64890Smrg    if (!(conv = _XlcOpenConverter(lcd,	XlcNCompoundText, lcd, XlcNWideChar)))
6681ab64890Smrg	goto Open_Error;
6691ab64890Smrg    private->ctow_conv = conv;
6701ab64890Smrg
6711ab64890Smrg    if (!(conv = _XlcOpenConverter(lcd,	XlcNCompoundText, lcd, XlcNUtf8String)))
6721ab64890Smrg	goto Open_Error;
6731ab64890Smrg    private->ctoutf8_conv = conv;
6741ab64890Smrg
6751ab64890Smrg    if (!(conv = _XlcOpenConverter(lcd,	XlcNCharSet, lcd, XlcNMultiByte)))
6761ab64890Smrg	goto Open_Error;
6771ab64890Smrg    private->cstomb_conv = conv;
6781ab64890Smrg
6791ab64890Smrg    if (!(conv = _XlcOpenConverter(lcd,	XlcNCharSet, lcd, XlcNWideChar)))
6801ab64890Smrg	goto Open_Error;
6811ab64890Smrg    private->cstowc_conv = conv;
6821ab64890Smrg
6831ab64890Smrg    if (!(conv = _XlcOpenConverter(lcd,	XlcNCharSet, lcd, XlcNUtf8String)))
6841ab64890Smrg	goto Open_Error;
6851ab64890Smrg    private->cstoutf8_conv = conv;
6861ab64890Smrg
6871ab64890Smrg    if (!(conv = _XlcOpenConverter(lcd,	XlcNUcsChar, lcd, XlcNChar)))
6881ab64890Smrg	goto Open_Error;
6891ab64890Smrg    private->ucstoc_conv = conv;
6901ab64890Smrg
6911ab64890Smrg    if (!(conv = _XlcOpenConverter(lcd,	XlcNUcsChar, lcd, XlcNUtf8String)))
6921ab64890Smrg	goto Open_Error;
6931ab64890Smrg    private->ucstoutf8_conv = conv;
6941ab64890Smrg
6951ab64890Smrg    private->base.treeused = 1;
6961ab64890Smrg    private->base.mbused   = 1;
6971ab64890Smrg    private->base.wcused   = 1;
6981ab64890Smrg    private->base.utf8used = 1;
6991ab64890Smrg
7001ab64890Smrg    _XimCreateDefaultTree(im);
7011ab64890Smrg
7021ab64890Smrg    im->methods = &Xim_im_local_methods;
7031ab64890Smrg    private->current_ic = (XIC)NULL;
7041ab64890Smrg
7051ab64890Smrg    return(True);
7061ab64890Smrg
7071ab64890SmrgOpen_Error :
7081ab64890Smrg    _XimLocalIMFree(im);
7091ab64890Smrg    return(False);
7101ab64890Smrg}
711