home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xlc_Wrap.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  16KB  |  665 lines

  1. /* $TOG: lcWrap.c /main/25 1997/06/02 17:28:04 kaleb $ */
  2. /*
  3.  
  4. Copyright (c) 1991  X Consortium
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. "Software"), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included
  15. in all copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. OTHER DEALINGS IN THE SOFTWARE.
  24.  
  25. Except as contained in this notice, the name of the X Consortium shall
  26. not be used in advertising or otherwise to promote the sale, use or
  27. other dealings in this Software without prior written authorization
  28. from the X Consortium.
  29.  
  30. */
  31. /*
  32.  * Copyright 1991 by the Open Software Foundation
  33.  * Copyright 1993 by the TOSHIBA Corp.
  34.  *
  35.  * Permission to use, copy, modify, distribute, and sell this software and its
  36.  * documentation for any purpose is hereby granted without fee, provided that
  37.  * the above copyright notice appear in all copies and that both that
  38.  * copyright notice and this permission notice appear in supporting
  39.  * documentation, and that the names of Open Software Foundation and TOSHIBA
  40.  * not be used in advertising or publicity pertaining to distribution of the
  41.  * software without specific, written prior permission.  Open Software
  42.  * Foundation and TOSHIBA make no representations about the suitability of this
  43.  * software for any purpose.  It is provided "as is" without express or
  44.  * implied warranty.
  45.  *
  46.  * OPEN SOFTWARE FOUNDATION AND TOSHIBA DISCLAIM ALL WARRANTIES WITH REGARD TO
  47.  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  48.  * FITNESS, IN NO EVENT SHALL OPEN SOFTWARE FOUNDATIONN OR TOSHIBA BE
  49.  * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
  50.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  51.  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  52.  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  53.  * 
  54.  *         M. Collins        OSF  
  55.  *
  56.  *         Katsuhisa Yano        TOSHIBA Corp.
  57.  */                
  58. /* $XFree86: xc/lib/X11/lcWrap.c,v 3.1.4.4 1998/10/04 13:36:25 hohndel Exp $ */
  59.  
  60. #include "Xlib_private.h"
  61. #include "Xlcint.h"
  62. #include <X11/Xlocale.h>
  63. #include <X11/Xos.h>
  64. #ifdef WIN32
  65. #undef close
  66. #endif
  67. #include <X11/Xutil.h>
  68.  
  69. #ifdef __STDC__
  70. #define Const const
  71. #else
  72. #define Const /**/
  73. #endif
  74.  
  75. #ifdef X_NOT_STDC_ENV
  76. extern char *getenv();
  77. #endif
  78.  
  79. extern void _XlcInitLoader(
  80. #if NeedFunctionPrototypes
  81.     void
  82. #endif
  83. );
  84.  
  85. #ifdef XTHREADS
  86. LockInfoPtr _Xi18n_lock;
  87. #endif
  88.  
  89. #if NeedFunctionPrototypes
  90. char *
  91. XSetLocaleModifiers(
  92.     _Xconst char   *modifiers
  93. )
  94. #else
  95. char *
  96. XSetLocaleModifiers(modifiers)
  97.     char        *modifiers;
  98. #endif
  99. {
  100.     DBUG_ENTER("XSetLocaleModifiers")
  101.     XLCd lcd = _XlcCurrentLC();
  102.     char *user_mods;
  103.  
  104.     if (!lcd)
  105.     DBUG_RETURN((char *) NULL);
  106.     if (!modifiers) {
  107.     char *result = lcd->core->modifiers;
  108.     DBUG_RETURN(result);
  109.     }
  110.     user_mods = getenv("XMODIFIERS");
  111.     modifiers = (*lcd->methods->map_modifiers) (lcd,
  112.                         user_mods, (char *)modifiers);
  113.     if (modifiers) {
  114.     if (lcd->core->modifiers)
  115.         Xfree(lcd->core->modifiers);
  116.     lcd->core->modifiers = (char *)modifiers;
  117.     }
  118.     DBUG_RETURN((char *)modifiers);
  119. }
  120.  
  121. Bool
  122. XSupportsLocale()
  123. {
  124.     DBUG_ENTER("XSupportsLocale")
  125.     Bool result = _XlcCurrentLC() != (XLCd)NULL;
  126.     DBUG_RETURN(result);
  127. }
  128.  
  129. Bool _XlcValidModSyntax(mods, valid_mods)
  130.     char *mods;
  131.     char **valid_mods;
  132. {
  133.     DBUG_ENTER("_XlcValidModSyntax")
  134.     int i;
  135.     char **ptr;
  136.  
  137.     while (mods && (*mods == '@')) {
  138.     mods++;
  139.     if (*mods == '@')
  140.         break;
  141.     for (ptr = valid_mods; *ptr; ptr++) {
  142.         i = strlen(*ptr);
  143.         if (strncmp(mods, *ptr, i) || ((mods[i] != '=')
  144. #ifdef WIN32
  145.                        && (mods[i] != '#')
  146. #endif
  147.                        ))
  148.         continue;
  149.         mods = strchr(mods+i+1, '@');
  150.         break;
  151.     }
  152.     }
  153.     DBUG_RETURN(!mods || !*mods);
  154. }
  155.  
  156. static Const char *im_valid[] = {"im", (char *)NULL};
  157.  
  158. /*ARGSUSED*/
  159. char *
  160. _XlcDefaultMapModifiers (lcd, user_mods, prog_mods)
  161.     XLCd lcd;
  162.     char *user_mods;
  163.     char *prog_mods;
  164. {
  165.     DBUG_ENTER("_XlcDefaultMapModifiers")
  166.     int i;
  167.     char *mods;
  168.  
  169.     if (!_XlcValidModSyntax(prog_mods, (char **)im_valid))
  170.     DBUG_RETURN((char *)NULL);
  171.     if (!_XlcValidModSyntax(user_mods, (char **)im_valid))
  172.     DBUG_RETURN((char *)NULL);
  173.     i = strlen(prog_mods) + 1;
  174.     if (user_mods)
  175.     i += strlen(user_mods);
  176.     mods = Xmalloc(i);
  177.     if (mods) {
  178.     strcpy(mods, prog_mods);
  179.     if (user_mods)
  180.         strcat(mods, user_mods);
  181. #ifdef WIN32
  182.     {
  183.         char *s;
  184.         for (s = mods; s = strchr(s, '@'); s++) {
  185.         for (s++; *s && *s != '='; s++) {
  186.             if (*s == '#') {
  187.             *s = '=';
  188.             break;
  189.             }
  190.         }
  191.         }
  192.     }
  193. #endif
  194.     }
  195.     DBUG_RETURN(mods);
  196. }
  197.  
  198. typedef struct _XLCdListRec {
  199.     struct _XLCdListRec *next;
  200.     XLCd lcd;
  201.     int ref_count;
  202. } XLCdListRec, *XLCdList;
  203.  
  204. static XLCdList lcd_list = NULL;
  205.  
  206. typedef struct _XlcLoaderListRec {
  207.     struct _XlcLoaderListRec *next;
  208.     XLCdLoadProc proc;
  209. } XlcLoaderListRec, *XlcLoaderList;
  210.  
  211. static XlcLoaderList loader_list = NULL;
  212.  
  213. void
  214. _XlcRemoveLoader(proc)
  215.     XLCdLoadProc proc;
  216. {
  217.     DBUG_ENTER("_XlcRemoveLoader")
  218.     XlcLoaderList loader, prev;
  219.  
  220.     if (loader_list == NULL)
  221.     DBUG_VOID_RETURN;
  222.  
  223.     prev = loader = loader_list;
  224.     if (loader->proc == proc) {
  225.     loader_list = loader->next;
  226.     Xfree(loader);
  227.     DBUG_VOID_RETURN;
  228.     }
  229.  
  230.     while ((loader = loader->next)) {
  231.     if (loader->proc == proc) {
  232.         prev->next = loader->next;
  233.         Xfree(loader);
  234.         DBUG_VOID_RETURN;
  235.     }
  236.     prev = loader;
  237.     }
  238.  
  239.     DBUG_VOID_RETURN;
  240. }
  241.  
  242. Bool
  243. _XlcAddLoader(proc, position)
  244.     XLCdLoadProc proc;
  245.     XlcPosition position;
  246. {
  247.     DBUG_ENTER("_XlcAddLoader")
  248.     XlcLoaderList loader, last;
  249.  
  250.     _XlcRemoveLoader(proc);        /* remove old loader, if exist */
  251.  
  252.     loader = (XlcLoaderList) Xmalloc(sizeof(XlcLoaderListRec));
  253.     if (loader == NULL)
  254.     DBUG_RETURN(False);
  255.  
  256.     loader->proc = proc;
  257.  
  258.     if (loader_list == NULL)
  259.     position = XlcHead;
  260.  
  261.     if (position == XlcHead) {
  262.     loader->next = loader_list;
  263.     loader_list = loader;
  264.     } else {
  265.     last = loader_list;
  266.     while (last->next)
  267.         last = last->next;
  268.     
  269.     loader->next = NULL;
  270.     last->next = loader;
  271.     }
  272.  
  273.     DBUG_RETURN(True);
  274. }
  275.  
  276. XLCd
  277. _XOpenLC(name)
  278.     _Xconst char *name;
  279. {
  280.     DBUG_ENTER("_XOpenLC")
  281.     XLCd lcd = NULL;
  282.     XlcLoaderList loader;
  283.     XLCdList cur;
  284. #if !defined(X_NOT_STDC_ENV) && !defined(X_LOCALE)
  285.     char siname[256];
  286.     char *_XlcMapOSLocaleName();
  287. #endif
  288.     int num;
  289.  
  290.     if (name == NULL) {
  291.     name = setlocale (LC_CTYPE, (char *)NULL);
  292. #if !defined(X_NOT_STDC_ENV) && !defined(X_LOCALE)
  293.     name = _XlcMapOSLocaleName (name, siname); 
  294. #endif
  295.     }
  296.  
  297.     _XLockMutex(_Xi18n_lock);
  298.  
  299.     /*
  300.      * search for needed lcd, if found return it
  301.      */
  302.     for (cur = lcd_list; cur; cur = cur->next) {
  303.     if (!strcmp (cur->lcd->core->name, name)) {
  304.         lcd = cur->lcd;
  305.         cur->ref_count++;
  306.         goto found;
  307.     }
  308.     }
  309.  
  310.     if (!loader_list)
  311.     _XlcInitLoader();
  312.  
  313.     /*
  314.      * not there, so try to get and add to list
  315.      */
  316.     for (loader = loader_list, num = 0; loader; loader = loader->next, num++) {
  317.     lcd = (*loader->proc)(name);
  318.     if (lcd) {
  319.         cur = (XLCdList) Xmalloc (sizeof(XLCdListRec));
  320.         if (cur) {
  321.         cur->lcd = lcd;
  322.         cur->ref_count = 1;
  323.         cur->next = lcd_list;
  324.         lcd_list = cur;
  325.         } else {
  326. #if defined(DEBUG) && defined(VERBOSE)
  327.         fprintf(stderr,"_XOpenLC: Xmalloc failed!\n");
  328. #endif
  329.         (*lcd->methods->close)(lcd);
  330.         lcd = (XLCd) NULL;
  331.         }
  332.         break;
  333.     }
  334.     }
  335.  
  336. #if defined(DEBUG) && defined(VERBOSE)
  337.     if (!lcd) {
  338.     fprintf(stderr,"_XOpenLC: Out of %d loaders, none worked!\n",num);
  339.     }
  340. #endif
  341. found:
  342.     _XUnlockMutex(_Xi18n_lock);
  343.     DBUG_RETURN(lcd);
  344. }
  345.  
  346. void
  347. _XCloseLC(lcd)
  348.     XLCd lcd;
  349. {
  350.     DBUG_ENTER("_XCloseLC")
  351.     XLCdList cur, *prev;
  352.  
  353.     for (prev = &lcd_list; (cur = *prev); prev = &cur->next) {
  354.     if (cur->lcd == lcd) {
  355.         if (--cur->ref_count < 1) {
  356.         (*lcd->methods->close)(lcd);
  357.         *prev = cur->next;
  358.         Xfree(cur);
  359.         }
  360.         break;
  361.     }
  362.     }
  363.     DBUG_VOID_RETURN;
  364. }
  365.  
  366. /*
  367.  * Get the XLCd for the current locale
  368.  */
  369.  
  370. XLCd
  371. _XlcCurrentLC()
  372. {
  373.     DBUG_ENTER("_XlcCurrentLC")
  374.     XLCd lcd;
  375.     static XLCd last_lcd = NULL;
  376.  
  377.     lcd = _XOpenLC((char *) NULL);
  378.  
  379.     if (last_lcd)
  380.     _XCloseLC(last_lcd);
  381.     
  382.     last_lcd = lcd;
  383.  
  384.     DBUG_RETURN(lcd);
  385. }
  386.  
  387. XrmMethods
  388. _XrmInitParseInfo(state)
  389.     XPointer *state;
  390. {
  391.     DBUG_ENTER("_XrmInitParseInfo")
  392.     XLCd lcd = _XOpenLC((char *) NULL);
  393.     XrmMethods result;
  394.     
  395.     if (lcd == (XLCd) NULL)
  396.     DBUG_RETURN((XrmMethods) NULL);
  397.     
  398.     result = (*lcd->methods->init_parse_info)(lcd, state);
  399.     DBUG_RETURN(result);
  400. }
  401.  
  402. int
  403. XmbTextPropertyToTextList(dpy, text_prop, list_ret, count_ret)
  404.     Display *dpy;
  405.     XTextProperty *text_prop;
  406.     char ***list_ret;
  407.     int *count_ret;
  408. {
  409.     DBUG_ENTER("XmbTextPropertyToTextList")
  410.     XLCd lcd = _XlcCurrentLC();
  411.     int result;
  412.     
  413.     if (lcd == NULL)
  414.     DBUG_RETURN(XLocaleNotSupported);
  415.  
  416.     result = (*lcd->methods->mb_text_prop_to_list)(lcd, dpy, text_prop, list_ret,
  417.                          count_ret);
  418.     DBUG_RETURN(result);
  419. }
  420.  
  421. int
  422. XwcTextPropertyToTextList(dpy, text_prop, list_ret, count_ret)
  423.     Display *dpy;
  424.     XTextProperty *text_prop;
  425.     wchar_t ***list_ret;
  426.     int *count_ret;
  427. {
  428.     DBUG_ENTER("XwcTextPropertyToTextList")
  429.     XLCd lcd = _XlcCurrentLC();
  430.     int result;
  431.     
  432.     if (lcd == NULL)
  433.     DBUG_RETURN(XLocaleNotSupported);
  434.  
  435.     result = (*lcd->methods->wc_text_prop_to_list)(lcd, dpy, text_prop, list_ret,
  436.                          count_ret);
  437.     DBUG_RETURN(result);
  438. }
  439.  
  440. int
  441. XmbTextListToTextProperty(dpy, list, count, style, text_prop)
  442.     Display *dpy;
  443.     char **list;
  444.     int count;
  445.     XICCEncodingStyle style;
  446.     XTextProperty *text_prop;
  447. {
  448.     DBUG_ENTER("XmbTextListToTextProperty")
  449.     XLCd lcd = _XlcCurrentLC();
  450.     int result;
  451.     
  452.     if (lcd == NULL)
  453.     return XLocaleNotSupported;
  454.  
  455.     result = (*lcd->methods->mb_text_list_to_prop)(lcd, dpy, list, count, style,
  456.                          text_prop);
  457.     DBUG_RETURN(result);
  458. }
  459.  
  460. int
  461. XwcTextListToTextProperty(dpy, list, count, style, text_prop)
  462.     Display *dpy;
  463.     wchar_t **list;
  464.     int count;
  465.     XICCEncodingStyle style;
  466.     XTextProperty *text_prop;
  467. {
  468.     DBUG_ENTER("XwcTextListToTextProperty")
  469.     XLCd lcd = _XlcCurrentLC();
  470.     int result;
  471.     
  472.     if (lcd == NULL)
  473.     return XLocaleNotSupported;
  474.  
  475.     result = (*lcd->methods->wc_text_list_to_prop)(lcd, dpy, list, count, style,
  476.                          text_prop);
  477.     DBUG_RETURN(result);
  478. }
  479.  
  480. void
  481. XwcFreeStringList(list)
  482.     wchar_t **list;
  483. {
  484.     DBUG_ENTER("XwcFreeStringList")
  485.     XLCd lcd = _XlcCurrentLC();
  486.     
  487.     if (lcd == NULL)
  488.     DBUG_VOID_RETURN;
  489.  
  490.     (*lcd->methods->wc_free_string_list)(lcd, list);
  491.     DBUG_VOID_RETURN;
  492. }
  493.  
  494. char *
  495. XDefaultString()
  496. {
  497.     DBUG_ENTER("XDefaultString")
  498.     XLCd lcd = _XlcCurrentLC();
  499.     char *result;
  500.     
  501.     if (lcd == NULL)
  502.     DBUG_RETURN((char *) NULL);
  503.     
  504.     result = (*lcd->methods->default_string)(lcd);
  505.     DBUG_RETURN(result);
  506. }
  507.  
  508. void
  509. _XlcCopyFromArg(src, dst, size)
  510.     char *src;
  511.     register char *dst;
  512.     register int size;
  513. {
  514.     DBUG_ENTER("_XlcCopyFromArg")
  515.     if (size == sizeof(long))
  516.     *((long *) dst) = (long) src;
  517. #ifdef LONG64
  518.     else if (size == sizeof(int))
  519.     *((int *) dst) = (int)(long) src;
  520. #endif
  521.     else if (size == sizeof(short))
  522.     *((short *) dst) = (short)(long) src;
  523.     else if (size == sizeof(char))
  524.     *((char *) dst) = (char)(long) src;
  525.     else if (size == sizeof(XPointer))
  526.     *((XPointer *) dst) = (XPointer) src;
  527.     else if (size > sizeof(XPointer))
  528.     memcpy(dst, (char *) src, size);
  529.     else
  530.     memcpy(dst, (char *) &src, size);
  531.     DBUG_VOID_RETURN;
  532. }
  533.  
  534. void
  535. _XlcCopyToArg(src, dst, size)
  536.     register char *src;
  537.     register char **dst;
  538.     register int size;
  539. {
  540.     DBUG_ENTER("_XlcCopyToArg")
  541.     if (size == sizeof(long))
  542.     *((long *) *dst) = *((long *) src);
  543.     else if (size == sizeof(short))
  544.     *((short *) *dst) = *((short *) src);
  545.     else if (size == sizeof(char))
  546.     *((char *) *dst) = *((char *) src);
  547.     else if (size == sizeof(XPointer))
  548.     *((XPointer *) *dst) = *((XPointer *) src);
  549.     else
  550.     memcpy(*dst, src, size);
  551.     DBUG_VOID_RETURN;
  552. }
  553.  
  554. void
  555. _XlcCountVaList(var, count_ret)
  556.     va_list var;
  557.     int *count_ret;
  558. {
  559.     DBUG_ENTER("_XlcCountVaList")
  560.     register int count;
  561.  
  562.     for (count = 0; va_arg(var, char *); count++)
  563.     va_arg(var, XPointer);
  564.     
  565.     *count_ret = count;
  566.     DBUG_VOID_RETURN;
  567. }
  568.  
  569. void
  570. _XlcVaToArgList(var, count, args_ret)
  571.     va_list var;
  572.     register int count;
  573.     XlcArgList *args_ret;
  574. {
  575.     DBUG_ENTER("_XlcVaToArgList")
  576.     register XlcArgList args;
  577.  
  578.     *args_ret = args = (XlcArgList) Xmalloc(sizeof(XlcArg) * count);
  579.     if (args == (XlcArgList) NULL)
  580.     DBUG_VOID_RETURN;
  581.     
  582.     for ( ; count-- > 0; args++) {
  583.     args->name = va_arg(var, char *);
  584.     args->value = va_arg(var, XPointer);
  585.     }
  586.     DBUG_VOID_RETURN;
  587. }
  588.  
  589. void
  590. _XlcCompileResourceList(resources, num_resources)
  591.     register XlcResourceList resources;
  592.     register int num_resources;
  593. {
  594.     DBUG_ENTER("_XlcCompileResourceList")
  595.     for ( ; num_resources-- > 0; resources++)
  596.     resources->xrm_name = XrmPermStringToQuark(resources->name);
  597.     DBUG_VOID_RETURN;
  598. }
  599.  
  600. char *
  601. _XlcGetValues(base, resources, num_resources, args, num_args, mask)
  602.     XPointer base;
  603.     XlcResourceList resources;
  604.     int num_resources;
  605.     XlcArgList args;
  606.     int num_args;
  607.     unsigned long mask;
  608. {
  609.     DBUG_ENTER("_XlcGetValues")
  610.     XlcResourceList res;
  611.     XrmQuark xrm_name;
  612.     int count;
  613.  
  614.     for ( ; num_args-- > 0; args++) {
  615.     res = resources;
  616.     count = num_resources;
  617.     xrm_name = XrmPermStringToQuark(args->name);
  618.  
  619.     for ( ; count-- > 0; res++) {
  620.         if (xrm_name == res->xrm_name && (mask & res->mask)) {
  621.             _XlcCopyToArg(base + res->offset, &args->value, res->size);
  622.         break;
  623.         }
  624.     }
  625.  
  626.     if (count < 0)
  627.         DBUG_RETURN(args->name);
  628.     }
  629.  
  630.     DBUG_RETURN(NULL);
  631. }
  632.  
  633. char *
  634. _XlcSetValues(base, resources, num_resources, args, num_args, mask)
  635.     XPointer base;
  636.     XlcResourceList resources;
  637.     int num_resources;
  638.     XlcArgList args;
  639.     int num_args;
  640.     unsigned long mask;
  641. {
  642.     DBUG_ENTER("_XlcSetValues")
  643.     XlcResourceList res;
  644.     XrmQuark xrm_name;
  645.     int count;
  646.  
  647.     for ( ; num_args-- > 0; args++) {
  648.     res = resources;
  649.     count = num_resources;
  650.     xrm_name = XrmPermStringToQuark(args->name);
  651.  
  652.     for ( ; count-- > 0; res++) {
  653.         if (xrm_name == res->xrm_name && (mask & res->mask)) {
  654.         _XlcCopyFromArg(args->value, base + res->offset, res->size);
  655.         break;
  656.         }
  657.     }
  658.  
  659.     if (count < 0)
  660.         DBUG_RETURN(args->name);
  661.     }
  662.  
  663.     DBUG_RETURN(NULL);
  664. }
  665.