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

  1. /* $TOG: lcGeneric.c /main/11 1997/07/17 21:48:57 kaleb $ */
  2. /*
  3.  * Copyright 1992, 1993 by TOSHIBA Corp.
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose and without fee is hereby granted, provided
  7.  * that the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation, and that the name of TOSHIBA not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission. TOSHIBA make no representations about the
  12.  * suitability of this software for any purpose.  It is provided "as is"
  13.  * without express or implied warranty.
  14.  *
  15.  * TOSHIBA DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17.  * TOSHIBA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19.  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21.  * SOFTWARE.
  22.  *
  23.  * Author: Katsuhisa Yano    TOSHIBA Corp.
  24.  *                   mopi@osa.ilab.toshiba.co.jp
  25.  */
  26. /*
  27.  *  (c) Copyright 1995 FUJITSU LIMITED
  28.  *  This is source code modified by FUJITSU LIMITED under the Joint
  29.  *  Development Agreement for the CDE/Motif PST.
  30.  */
  31. /* $XFree86: xc/lib/X11/lcGeneric.c,v 3.3.2.3 1998/05/19 07:31:41 dawes Exp $ */
  32.  
  33. #include <stdio.h>
  34. #include "Xlib_private.h"
  35. #include "XlcGeneric.h"
  36.  
  37. static XLCd create();
  38. static Bool initialize();
  39. static void destroy();
  40.  
  41. static XLCdPublicMethodsRec genericMethods = {
  42.     { NULL },                   /* use default methods */
  43.     {
  44.     NULL,
  45.     create,
  46.     initialize,
  47.     destroy,
  48.     NULL
  49.     }
  50. };
  51.  
  52. XLCdMethods _XlcGenericMethods = (XLCdMethods) &genericMethods;
  53.  
  54. static XLCd
  55. create(name, methods)
  56.     char *name;
  57.     XLCdMethods methods;
  58. {
  59.     DBUG_ENTER("create")
  60.     XLCd lcd;
  61.     XLCdPublicMethods new;
  62.  
  63.     lcd = (XLCd) Xmalloc(sizeof(XLCdRec));
  64.     if (lcd == NULL)
  65.         DBUG_RETURN((XLCd) NULL);
  66.     bzero((char *) lcd, sizeof(XLCdRec));
  67.  
  68.     lcd->core = (XLCdCore) Xmalloc(sizeof(XLCdGenericRec));
  69.     if (lcd->core == NULL)
  70.     goto err;
  71.     bzero((char *) lcd->core, sizeof(XLCdGenericRec));
  72.  
  73.     new = (XLCdPublicMethods) Xmalloc(sizeof(XLCdPublicMethodsRec));
  74.     if (new == NULL)
  75.     goto err;
  76.     *new = *((XLCdPublicMethods) methods);
  77.     lcd->methods = (XLCdMethods) new;
  78.  
  79.     DBUG_RETURN(lcd);
  80.  
  81. err:
  82.     Xfree(lcd);
  83.     DBUG_RETURN((XLCd) NULL);
  84. }
  85.  
  86. static Bool
  87. string_to_encoding(str, encoding)
  88.     char *str;
  89.     char *encoding;
  90. {
  91.     DBUG_ENTER("string_to_encoding")
  92.     char *next;
  93.     long value;
  94.     int base;
  95.  
  96.     while (*str) {
  97.     if (*str == '\\') {
  98.         switch (*(str + 1)) {
  99.         case 'x':
  100.         case 'X':
  101.             base = 16;
  102.             break;
  103.         default:
  104.             base = 8;
  105.             break;
  106.         }
  107.         value = strtol(str + 2, &next, base);
  108.         if (str + 2 != next) {
  109.         *((unsigned char *) encoding++) = (unsigned char) value;
  110.         str = next;
  111.         continue;
  112.         }
  113.     }
  114.     *encoding++ = *str++;
  115.     }
  116.  
  117.     *encoding = '\0';
  118.  
  119.     DBUG_RETURN(True);
  120. }
  121.  
  122. static Bool
  123. string_to_ulong(str, value)
  124. char    *str;
  125. unsigned long    *value;
  126. {
  127.      DBUG_ENTER("string_to_ulong")
  128.      char    *tmp1 = str;
  129.      int     base;
  130.  
  131.      if(*tmp1++ != '\\'){
  132.       tmp1--;
  133.       base = 10;
  134.      }else{
  135.       switch(*tmp1++){
  136.       case 'x':
  137.            base = 16;
  138.            break;
  139.       case 'o':
  140.            base = 8;
  141.            break;
  142.       case 'd':
  143.            base = 10;
  144.            break;
  145.       default:
  146.            DBUG_RETURN(False);
  147.       }
  148.      }
  149.     *value = (unsigned) strtol(tmp1, NULL, base);
  150.     DBUG_RETURN(True);
  151. }
  152.  
  153.  
  154. static Bool
  155. add_charset(codeset, charset)
  156.     CodeSet codeset;
  157.     XlcCharSet charset;
  158. {
  159.     DBUG_ENTER("add_charset")
  160.     XlcCharSet *new_list;
  161.     int num;
  162.  
  163.     if ((num = codeset->num_charsets))
  164.         new_list = (XlcCharSet *) Xrealloc(codeset->charset_list,
  165.                                         (num + 1) * sizeof(XlcCharSet));
  166.     else
  167.         new_list = (XlcCharSet *) Xmalloc(sizeof(XlcCharSet));
  168.  
  169.     if (new_list == NULL)
  170.     DBUG_RETURN(False);
  171.  
  172.     new_list[num] = charset;
  173.     codeset->charset_list = new_list;
  174.     codeset->num_charsets = num + 1;
  175.  
  176.     DBUG_RETURN(True);
  177. }
  178.  
  179. static CodeSet
  180. add_codeset(gen)
  181.     XLCdGenericPart *gen;
  182. {
  183.     DBUG_ENTER("add_codeset")
  184.     CodeSet new, *new_list;
  185.     int num;
  186.  
  187.     new = (CodeSet) Xmalloc(sizeof(CodeSetRec));
  188.     if (new == NULL)
  189.         DBUG_RETURN(NULL);
  190.     bzero((char *) new, sizeof(CodeSetRec));
  191.  
  192.     if ((num = gen->codeset_num))
  193.         new_list = (CodeSet *) Xrealloc(gen->codeset_list,
  194.                                         (num + 1) * sizeof(CodeSet));
  195.     else
  196.         new_list = (CodeSet *) Xmalloc(sizeof(CodeSet));
  197.  
  198.     if (new_list == NULL)
  199.         goto err;
  200.  
  201.     new_list[num] = new;
  202.     gen->codeset_list = new_list;
  203.     gen->codeset_num = num + 1;
  204.  
  205.     DBUG_RETURN(new);
  206.  
  207. err:
  208.     Xfree(new);
  209.  
  210.     DBUG_RETURN(NULL);
  211. }
  212.  
  213. static Bool
  214. add_parse_list(gen, type, encoding, codeset)
  215.     XLCdGenericPart *gen;
  216.     EncodingType type;
  217.     char *encoding;
  218.     CodeSet codeset;
  219. {
  220.     DBUG_ENTER("add_parse_list")
  221.     ParseInfo new, *new_list;
  222.     char *str;
  223.     unsigned char ch;
  224.     int num;
  225.  
  226.     str = (char *) Xmalloc(strlen(encoding) + 1);
  227.     if (str == NULL)
  228.         DBUG_RETURN(False);
  229.     strcpy(str, encoding);
  230.  
  231.     new = (ParseInfo) Xmalloc(sizeof(ParseInfoRec));
  232.     if (new == NULL)
  233.         goto err;
  234.     bzero((char *) new, sizeof(ParseInfoRec));
  235.  
  236.     if (gen->mb_parse_table == NULL) {
  237.         gen->mb_parse_table = (unsigned char *) Xmalloc(256); /* 2^8 */
  238.         if (gen->mb_parse_table == NULL)
  239.             goto err;
  240.         bzero((char *) gen->mb_parse_table, 256);
  241.     }
  242.  
  243.     if ((num = gen->mb_parse_list_num))
  244.         new_list = (ParseInfo *) Xrealloc(gen->mb_parse_list,
  245.                                           (num + 2) * sizeof(ParseInfo));
  246.     else {
  247.         new_list = (ParseInfo *) Xmalloc(2 * sizeof(ParseInfo));
  248.     }
  249.  
  250.     if (new_list == NULL)
  251.         goto err;
  252.  
  253.     new_list[num] = new;
  254.     new_list[num + 1] = NULL;
  255.     gen->mb_parse_list = new_list;
  256.     gen->mb_parse_list_num = num + 1;
  257.  
  258.     ch = (unsigned char) *str;
  259.     if (gen->mb_parse_table[ch] == 0)
  260.         gen->mb_parse_table[ch] = num + 1;
  261.  
  262.     new->type = type;
  263.     new->encoding = str;
  264.     new->codeset = codeset;
  265.  
  266.     if (codeset->parse_info == NULL)
  267.         codeset->parse_info = new;
  268.  
  269.     DBUG_RETURN(True);
  270.  
  271. err:
  272.     Xfree(str);
  273.     if (new)
  274.         Xfree(new);
  275.  
  276.     DBUG_RETURN(False);
  277. }
  278.  
  279. static void
  280. free_charset(lcd)
  281.     XLCd lcd;
  282. {
  283.     DBUG_ENTER("free_charset")
  284.     XLCdGenericPart *gen = XLC_GENERIC_PART(lcd);
  285.     ParseInfo *parse_info;
  286.     int num;
  287.  
  288.     if (gen->mb_parse_table)
  289.         Xfree(gen->mb_parse_table);
  290.     if ((num = gen->mb_parse_list_num)) {
  291.         for (parse_info = gen->mb_parse_list; num-- > 0; parse_info++) {
  292.             if ((*parse_info)->encoding)
  293.                 Xfree((*parse_info)->encoding);
  294.             Xfree(*parse_info);
  295.         }
  296.         Xfree(gen->mb_parse_list);
  297.     }
  298.  
  299.     if ((num = gen->codeset_num))
  300.         Xfree(gen->codeset_list);
  301.     DBUG_VOID_RETURN;
  302. }
  303. /* For VW/UDC */
  304.  
  305. #define FORWARD  (unsigned long)'+'
  306. #define BACKWARD (unsigned long)'-'
  307.  
  308. static char *getscope(str,scp)
  309. char *str;
  310. FontScope scp;
  311. {
  312.     DBUG_ENTER("getscope")
  313.     char *next;
  314.     unsigned long start=0,end=0,dest=0,shift=0,direction=0;
  315.     sscanf(str,"[\\x%lx,\\x%lx]->\\x%lx", &start, &end,&dest);
  316.     if( dest ){
  317.         if(dest >= start ){
  318.             shift = dest - start;
  319.             direction = FORWARD ;
  320.         } else {
  321.             shift = start - dest;
  322.             direction = BACKWARD;
  323.         }
  324.     }
  325.     scp->start = start      ;
  326.     scp->end   = end        ;
  327.     scp->shift = shift      ;
  328.     scp->shift_direction
  329.                = direction  ;
  330.     /* .......... */
  331.     while(*str){
  332.         if(*str == ',' && *(str+1) == '['){
  333.             break;
  334.         }
  335.         str++;
  336.     }
  337.     next = str+1 ;
  338.     DBUG_RETURN(next);
  339. }
  340.  
  341. static int count_scopemap(str)
  342. char *str;
  343. {
  344.     DBUG_ENTER("count_scopemap")
  345.     char *ptr;
  346.     int num=0;
  347.     for(ptr=str;*ptr;ptr++){
  348.         if(*ptr == ']'){
  349.             num ++;
  350.         }
  351.     }
  352.     DBUG_RETURN(num);
  353. }
  354.  
  355. FontScope _XlcParse_scopemaps(str,size)
  356. char *str;
  357. int *size;
  358. {
  359.     DBUG_ENTER("_XlcParse_scopemaps")
  360.         int num=0,i;
  361.         FontScope scope,sc_ptr;
  362.         char *str_sc;
  363.         num = count_scopemap(str);
  364.         scope = (FontScope )Xmalloc(num * sizeof(FontScopeRec));
  365.         if(scope == NULL) {
  366.                 DBUG_RETURN(NULL);
  367.         }
  368.         for (i=0,str_sc=str,sc_ptr=scope;
  369.                         i < num; i++,sc_ptr++){
  370.                 str_sc = getscope(str_sc,sc_ptr);
  371.         }
  372.         *size = num;
  373.         DBUG_RETURN(scope);
  374. }
  375.  
  376. void _XlcDbg_printValue(str,value,num)
  377. char *str;
  378. char **value;
  379. int num;
  380. {
  381. /*
  382.     int i;
  383.     for(i=0;i<num;i++){
  384.         fprintf(stderr,"%s value[%d] = %s\n",str,i,value[i]);
  385.     }
  386. */
  387. }
  388.  
  389. static void dmpscope(name,sc,num)
  390. char* name;
  391. FontScope sc;
  392. int num;
  393. {
  394. /*
  395.     int i;
  396.     fprintf(stderr,"dmpscope %s\n",name);
  397.     for(i=0;i<num;i++){
  398.         fprintf(stderr,"%x %x %x %x \n",
  399.                 sc[i].start,
  400.                 sc[i].end,
  401.                 sc[i].shift,
  402.                 sc[i].shift_direction);
  403.     }
  404.     fprintf(stderr,"dmpscope end\n");
  405. */
  406. }
  407.  
  408. static XlcCharSet srch_charset_define(name,new)
  409. char *name;
  410. int *new;
  411. {
  412.     DBUG_ENTER("srch_carset_define")
  413.     XlcCharSet charset = NULL;
  414.     *new = 0;
  415.     charset = _XlcGetCharSet(name);
  416.     if (charset == NULL &&
  417.         (charset = _XlcCreateDefaultCharSet(name, ""))) {
  418.         _XlcAddCharSet(charset);
  419.         *new = 1;
  420.     }
  421.     DBUG_RETURN(charset);
  422. }
  423.  
  424. static void read_charset_define(lcd,gen)
  425. XLCd lcd;
  426. XLCdGenericPart *gen;
  427. {
  428.     DBUG_ENTER("read_charset_define")
  429.     int i=0;
  430.     char csd[16],cset_name[256];
  431.     char name[BUFSIZ];
  432.     XlcCharSet charsetd;
  433.     char **value;
  434.     int num,new;
  435.     XlcSide side = XlcUnknown;
  436.     char *tmp;
  437.  
  438.     for(i=0;;i++){ /* loop start */
  439.         charsetd = 0;
  440.         sprintf(csd, "csd%d", i);
  441.  
  442.         /* charset_name   */
  443.         sprintf(name, "%s.%s", csd , "charset_name");
  444.         _XlcGetResource(lcd, "XLC_CHARSET_DEFINE", name, &value, &num);
  445.         _XlcDbg_printValue(name,value,num);
  446.         if (num > 0 && strlen(value[0]) + 5 <= sizeof(cset_name)) {
  447.             strcpy(cset_name,value[0]);
  448.             sprintf(name, "%s.%s", csd , "side");
  449.             _XlcGetResource(lcd, "XLC_CHARSET_DEFINE", name, &value, &num);
  450.             if (num > 0) {
  451.                 _XlcDbg_printValue(name,value,num);
  452.                 if( !_XlcNCompareISOLatin1(value[0], "none", 4) ){
  453.                     side =  XlcNONE ;
  454.                     strcat(cset_name,":none");
  455.                 } else
  456.                 if( !_XlcNCompareISOLatin1(value[0], "GL", 2) ){
  457.                     side =  XlcGL ;
  458.                     strcat(cset_name,":GL");
  459.                 } else {
  460.                     side =  XlcGR ;
  461.                     strcat(cset_name,":GR");
  462.                 }
  463.                 if (charsetd == NULL &&
  464.                     (charsetd = srch_charset_define(cset_name,&new)) == NULL)
  465.                     DBUG_VOID_RETURN;
  466.             }
  467.         } else {
  468.             if(i == 0){
  469.                 continue ;
  470.             } else {
  471.                 break ;
  472.             }
  473.         }
  474.         if(new){
  475.             tmp = (char *)Xmalloc(strlen(cset_name)+1);
  476.             if(tmp == NULL){
  477.                 DBUG_VOID_RETURN;
  478.             }
  479.             strcpy(tmp,cset_name);
  480.             charsetd->name = tmp;
  481.         }
  482.         /* side   */
  483.         charsetd->side =  side ;
  484.         /* length */
  485.         sprintf(name, "%s.%s", csd , "length");
  486.         _XlcGetResource(lcd, "XLC_CHARSET_DEFINE", name, &value, &num);
  487.         if (num > 0) {
  488.             _XlcDbg_printValue(name,value,num);
  489.             charsetd->char_size = atoi(value[0]);
  490.         }
  491.         /* gc_number */
  492.         sprintf(name, "%s.%s", csd , "gc_number");
  493.         _XlcGetResource(lcd, "XLC_CHARSET_DEFINE", name, &value, &num);
  494.         if (num > 0) {
  495.             _XlcDbg_printValue(name,value,num);
  496.             charsetd->set_size = atoi(value[0]);
  497.         }
  498.         /* string_encoding */
  499.         sprintf(name, "%s.%s", csd , "string_encoding");
  500.         _XlcGetResource(lcd, "XLC_CHARSET_DEFINE", name, &value, &num);
  501.         if (num > 0) {
  502.             _XlcDbg_printValue(name,value,num);
  503.             if(!strcmp("False",value[0])){
  504.                 charsetd->string_encoding = False;
  505.             } else {
  506.                 charsetd->string_encoding = True;
  507.             }
  508.         }
  509.         /* sequence */
  510.         sprintf(name, "%s.%s", csd , "sequence");
  511.         _XlcGetResource(lcd, "XLC_CHARSET_DEFINE", name, &value, &num);
  512.         if (num > 0) {
  513.             _XlcDbg_printValue(name,value,num);
  514. /*
  515.             if(charsetd->ct_sequence){
  516.                 Xfree(charsetd->ct_sequence);
  517.             }
  518. */
  519.             tmp = (char *)Xmalloc(strlen(value[0])+1);
  520.             if(tmp == NULL){
  521.                 DBUG_VOID_RETURN;
  522.             }
  523.             charsetd->ct_sequence = tmp;
  524.             string_to_encoding(value[0],tmp);
  525.         }
  526.         /* encoding_name */
  527.         sprintf(name, "%s.%s", csd , "encoding_name");
  528.         _XlcGetResource(lcd, "XLC_CHARSET_DEFINE", name, &value, &num);
  529.         if (num > 0) {
  530.             _XlcDbg_printValue(name,value,num);
  531. /*
  532.             if(charsetd->encoding_name){
  533.                 Xfree(charsetd->encoding_name);
  534.             }
  535. */
  536.             tmp = (char *)Xmalloc(strlen(value[0]+1));
  537.             strcpy(tmp,value[0]);
  538.             charsetd->encoding_name = tmp;
  539.             charsetd->xrm_encoding_name =
  540.                 XrmStringToQuark(tmp);
  541.         }
  542.     }
  543.     DBUG_VOID_RETURN;
  544. }
  545.  
  546. SegConv
  547. add_conversion(gen)
  548. XLCdGenericPart *gen;
  549. {
  550.     DBUG_ENTER("add_conversion")
  551.     SegConv new_list;
  552.     int num;
  553.  
  554.     if ((num = gen->segment_conv_num)){
  555.         new_list = (SegConv) Xrealloc(gen->segment_conv,
  556.                                         (num + 1) * sizeof(SegConvRec));
  557.     } else {
  558.         new_list = (SegConv) Xmalloc(sizeof(SegConvRec));
  559.     }
  560.  
  561.     if (new_list == NULL)
  562.         DBUG_RETURN(False);
  563.  
  564.     gen->segment_conv = new_list;
  565.     gen->segment_conv_num = num + 1;
  566.  
  567.     DBUG_RETURN(&new_list[num]);
  568.  
  569. }
  570. static void read_segmentconversion(lcd,gen)
  571. XLCd lcd;
  572. XLCdGenericPart *gen;
  573. {
  574.     DBUG_ENTER("read_segmentconversion")
  575.     int i=0;
  576.     char conv[16];
  577.     char name[BUFSIZ];
  578.     char **value;
  579.     int num,new;
  580.     SegConv conversion;
  581.     for(i=0 ; ; i++){ /* loop start */
  582.         conversion = 0;
  583.         sprintf(conv, "conv%d", i);
  584.  
  585.         /* length                */
  586.         sprintf(name, "%s.%s", conv , "length");
  587.         _XlcGetResource(lcd, "XLC_SEGMENTCONVERSION", name, &value, &num);
  588.         if (num > 0) {
  589.             char *tmp;
  590.             if (conversion == NULL &&
  591.                 (conversion = add_conversion(gen)) == NULL) {
  592.                 DBUG_VOID_RETURN;
  593.             }
  594.             _XlcDbg_printValue(name,value,num);
  595.         } else {
  596.             if(i == 0){
  597.                 continue;
  598.             } else {
  599.                 break ;
  600.             }
  601.         }
  602.         conversion->length = atoi(value[0]);
  603.  
  604.         /* source_encoding       */
  605.         sprintf(name, "%s.%s", conv , "source_encoding");
  606.         _XlcGetResource(lcd, "XLC_SEGMENTCONVERSION", name, &value, &num);
  607.         if (num > 0) {
  608.             char *tmp;
  609.             _XlcDbg_printValue(name,value,num);
  610.             tmp = (char *)Xmalloc(strlen(value[0])+1);
  611.             if(tmp == NULL){
  612.                 DBUG_VOID_RETURN;
  613.             }
  614.             strcpy(tmp,value[0]);
  615.             conversion->source_encoding = tmp;
  616.             conversion->source = srch_charset_define(tmp,&new);
  617.             if(new){
  618.                 tmp = (char *)Xmalloc(strlen(conversion->source_encoding)+1);
  619.                 if(tmp == NULL){
  620.                     DBUG_VOID_RETURN;
  621.                 }
  622.                 strcpy(tmp,conversion->source_encoding);
  623.                 conversion->source->name = tmp;
  624.             }
  625.         }
  626.         /* destination_encoding  */
  627.         sprintf(name, "%s.%s", conv , "destination_encoding");
  628.         _XlcGetResource(lcd, "XLC_SEGMENTCONVERSION", name, &value, &num);
  629.         if (num > 0) {
  630.             char *tmp;
  631.             _XlcDbg_printValue(name,value,num);
  632.             tmp = (char *)Xmalloc(strlen(value[0])+1);
  633.             if(tmp == NULL){
  634.                 DBUG_VOID_RETURN;
  635.             }
  636.             strcpy(tmp,value[0]);
  637.             conversion->destination_encoding = tmp;
  638.             conversion->dest = srch_charset_define(tmp,&new);
  639.             if(new){
  640.                 tmp = (char *)Xmalloc(
  641.                     strlen(conversion->destination_encoding)+1);
  642.                 if(tmp == NULL){
  643.                     DBUG_VOID_RETURN;
  644.                 }
  645.                 strcpy(tmp,conversion->destination_encoding);
  646.                 conversion->dest->name = tmp;
  647.             }
  648.         }
  649.         /* range                 */
  650.         sprintf(name, "%s.%s", conv , "range");
  651.         _XlcGetResource(lcd, "XLC_SEGMENTCONVERSION", name, &value, &num);
  652.         if (num > 0) {
  653.             char *tmp;
  654.             _XlcDbg_printValue(name,value,num);
  655.             sscanf(value[0],"\\x%lx,\\x%lx",
  656.                 &(conversion->range.start),
  657.                 &(conversion->range.end));
  658.         }
  659.         /* conversion            */
  660.         sprintf(name, "%s.%s", conv , "conversion");
  661.         _XlcGetResource(lcd, "XLC_SEGMENTCONVERSION", name, &value, &num);
  662.         if (num > 0) {
  663.             char *tmp;
  664.             _XlcDbg_printValue(name,value,num);
  665.             conversion->conv =
  666.                 _XlcParse_scopemaps(value[0],&conversion->conv_num);
  667.         }
  668.     }  /* loop end */
  669.     DBUG_VOID_RETURN;
  670. }
  671.  
  672. static ExtdSegment create_ctextseg(value,num)
  673. char **value;
  674. int num;
  675. {
  676.     DBUG_ENTER("create_ctextseg")
  677.     ExtdSegment ret;
  678.     char *ptr;
  679.     char *cset_name = NULL;
  680.     int i,new;
  681.     FontScope scope;
  682.     ret = (ExtdSegment)Xmalloc(sizeof(ExtdSegmentRec));
  683.     if(ret == NULL){
  684.         DBUG_RETURN(NULL);
  685.     }
  686.     if(strchr(value[0],':')){
  687.         ret->name = (char *)Xmalloc(strlen(value[0])+1);
  688.         if(ret->name == NULL){
  689.         Xfree(ret);
  690.             DBUG_RETURN(NULL);
  691.         }
  692.         strcpy(ret->name,value[0]);
  693.         ptr = strchr(ret->name,':');
  694.         *ptr = '\0';
  695.         ptr++;
  696.     cset_name = (char *)Xmalloc(strlen(ret->name) + 5 + 1);
  697.     if (cset_name == NULL){
  698.         Xfree(ret);
  699.             DBUG_RETURN(NULL);
  700.         }
  701.         if( !_XlcNCompareISOLatin1(ptr, "none", 4) ){
  702.             ret->side =  XlcNONE ;
  703.             sprintf(cset_name,"%s:%s",ret->name,"none");
  704.         } else
  705.         if( !_XlcNCompareISOLatin1(ptr, "GL", 2) ){
  706.             ret->side =  XlcGL ;
  707.             sprintf(cset_name,"%s:%s",ret->name,"GL");
  708.         } else {
  709.             ret->side =  XlcGR ;
  710.             sprintf(cset_name,"%s:%s",ret->name,"GR");
  711.         }
  712.     } else {
  713.         ret->name = (char *)Xmalloc(strlen(value[0])+1);
  714.         if(ret->name == NULL){
  715.         Xfree(ret);
  716.             DBUG_RETURN(NULL);
  717.         }
  718.         strcpy(ret->name,value[0]);
  719.     }
  720.     ret->area = (FontScope)Xmalloc((num - 1)*sizeof(FontScopeRec));
  721.     if(ret->area == NULL){
  722.     Xfree(ret);
  723.         DBUG_RETURN(NULL);
  724.     }
  725.     ret->area_num  = num - 1;
  726.     scope = ret->area ;
  727.     for(i=1;i<num;i++){
  728.         sscanf(value[i],"\\x%lx,\\x%lx",
  729.                 &scope[i-1].start,&scope[i-1].end);
  730.     }
  731.     ret->charset = srch_charset_define(cset_name,&new);
  732.     if(new){
  733.     ret->charset->name = cset_name;
  734.     } else {
  735.     if (cset_name != NULL)
  736.         Xfree(cset_name);
  737.     }
  738.     DBUG_RETURN(ret);
  739. }
  740. /* For VW/UDC end */
  741.  
  742. static Bool
  743. load_generic(lcd)
  744.     XLCd lcd;
  745. {
  746.     DBUG_ENTER("load_generic")
  747.     XLCdGenericPart *gen = XLC_GENERIC_PART(lcd);
  748.     char **value;
  749.     int num;
  750.     unsigned long l;
  751.     int i;
  752.     int M,ii;
  753.  
  754.     gen->codeset_num = 0;
  755.  
  756.     /***** wc_encoding_mask *****/
  757.     _XlcGetResource(lcd, "XLC_XLOCALE", "wc_encoding_mask", &value, &num);
  758.     if (num > 0) {
  759.     if (string_to_ulong(value[0], &l) == False) 
  760.         goto err;
  761.     gen->wc_encode_mask = l;
  762.     }
  763.     /***** wc_shift_bits *****/
  764.     _XlcGetResource(lcd, "XLC_XLOCALE", "wc_shift_bits", &value, &num);
  765.     if (num > 0)
  766.     gen->wc_shift_bits = atoi(value[0]);
  767.     if (gen->wc_shift_bits < 1)
  768.     gen->wc_shift_bits = 8;
  769. #ifndef X_NOT_STDC_ENV
  770.     /***** use_stdc_env *****/
  771.     _XlcGetResource(lcd, "XLC_XLOCALE", "use_stdc_env", &value, &num);
  772.     if (num > 0 && !_XlcCompareISOLatin1(value[0], "True"))
  773.     gen->use_stdc_env = True;
  774.     else
  775.     gen->use_stdc_env = False;
  776.     /***** force_convert_to_mb *****/
  777.     _XlcGetResource(lcd, "XLC_XLOCALE", "force_convert_to_mb", &value, &num);
  778.     if (num > 0 && !_XlcCompareISOLatin1(value[0], "True"))
  779.     gen->force_convert_to_mb = True;
  780.     else
  781.     gen->force_convert_to_mb = False;
  782. #endif
  783.     
  784.     for (i = 0; ; i++) {
  785.     CodeSetRec *codeset = NULL;
  786.     char cs[16];
  787.     char name[BUFSIZ];
  788.  
  789.     sprintf(cs, "cs%d", i);
  790.  
  791.     /***** codeset.side *****/
  792.     sprintf(name, "%s.%s", cs , "side");
  793.     _XlcGetResource(lcd, "XLC_XLOCALE", name, &value, &num);
  794.     if (num > 0) {
  795.         char *tmp;
  796.  
  797.         if (codeset == NULL && (codeset = add_codeset(gen)) == NULL)
  798.         goto err;
  799.  
  800.             /* 3.4.1 side */
  801.             if( !_XlcNCompareISOLatin1(value[0], "none", 4) ){
  802.                 codeset->side =  XlcNONE ;
  803.             } else
  804.             if( !_XlcNCompareISOLatin1(value[0], "GL", 2) ){
  805.                 codeset->side =  XlcGL ;
  806.             } else {
  807.                 codeset->side =  XlcGR ;
  808.             }
  809.  
  810.         tmp = strrchr(value[0], ':');
  811.         if (tmp != NULL && !_XlcCompareISOLatin1(tmp + 1, "Default")) {
  812.         if (codeset->side == XlcGR)
  813.             gen->initial_state_GR = codeset;
  814.         else
  815.             gen->initial_state_GL = codeset;
  816.         }
  817.     }
  818.  
  819.     /***** codeset.length *****/
  820.     sprintf(name, "%s.%s", cs , "length");
  821.     _XlcGetResource(lcd, "XLC_XLOCALE", name, &value, &num);
  822.     if (num > 0) {
  823.         if (codeset == NULL && (codeset = add_codeset(gen)) == NULL)
  824.         goto err;
  825.         codeset->length = atoi(value[0]);
  826.         if (codeset->length < 1)
  827.         codeset->length = 1;
  828.     }
  829.  
  830.     /***** codeset.mb_encoding *****/
  831.     sprintf(name, "%s.%s", cs, "mb_encoding");
  832.     _XlcGetResource(lcd, "XLC_XLOCALE", name, &value, &num);
  833.     if (num > 0) {
  834.         static struct { 
  835.         char *str;
  836.         int type;
  837.         } shifts[] = {
  838.         {"<SS>", E_SS},
  839.         {"<LSL>", E_LSL},
  840.         {"<LSR>", E_LSR},
  841.         0
  842.         };
  843.         int j;
  844.  
  845.         if (codeset == NULL && (codeset = add_codeset(gen)) == NULL)
  846.         goto err;
  847.         for ( ; num-- > 0; value++) {
  848.         char encoding[256];
  849.         char *tmp = *value;
  850.         int type = E_SS;    /* for BC */
  851.         for (j = 0; shifts[j].str; j++) {
  852.             if (!_XlcNCompareISOLatin1(tmp, shifts[j].str,
  853.                            strlen(shifts[j].str))) {
  854.             type = shifts[j].type;
  855.             tmp += strlen(shifts[j].str);
  856.             break;
  857.             }
  858.         }
  859.         if (strlen(tmp) >= sizeof(encoding))
  860.             goto err;
  861.         if (string_to_encoding(tmp, encoding) == False)
  862.             goto err;
  863.         add_parse_list(gen, type, encoding, codeset);
  864.         }
  865.     }
  866.  
  867.     /***** codeset.wc_encoding *****/
  868.     sprintf(name, "%s.%s", cs, "wc_encoding");
  869.     _XlcGetResource(lcd, "XLC_XLOCALE", name, &value, &num);
  870.     if (num > 0) {
  871.         if (codeset == NULL && (codeset = add_codeset(gen)) == NULL)
  872.         goto err;
  873.         if (string_to_ulong(value[0], &l) == False) 
  874.         goto err;
  875.         codeset->wc_encoding = l;
  876.     }
  877.   
  878.     /***** codeset.ct_encoding *****/
  879.     sprintf(name, "%s.%s", cs, "ct_encoding");
  880.     _XlcGetResource(lcd, "XLC_XLOCALE", name, &value, &num);
  881.     if (num > 0) {
  882.         XlcCharSet charset;
  883.         char *encoding;
  884.  
  885.         if (codeset == NULL && (codeset = add_codeset(gen)) == NULL)
  886.         goto err;
  887.         for ( ; num-- > 0; value++) {
  888.         if (strlen(*value) >= sizeof(name))
  889.             continue;
  890.         string_to_encoding(*value, name);
  891.         charset = NULL;
  892.         if ((encoding = strchr(name, ':')) &&
  893.             (encoding = strchr(encoding + 1, ':'))) {
  894.             *encoding++ = '\0';
  895.             charset = _XlcAddCT(name, encoding);
  896.         }
  897.         if (charset == NULL) {
  898.             charset = _XlcGetCharSet(name);
  899.             if (charset == NULL &&
  900.             (charset = _XlcCreateDefaultCharSet(name, ""))) {
  901.             charset->side = codeset->side;
  902.             charset->char_size = codeset->length;
  903.             _XlcAddCharSet(charset);
  904.             }
  905.         }
  906.         if (charset) {
  907.             if (add_charset(codeset, charset) == False)
  908.             goto err;
  909.         }
  910.         }
  911.     }
  912.  
  913.     if (codeset == NULL)
  914.         break;
  915.     codeset->cs_num = i;
  916.         /* For VW/UDC */
  917.         /***** 3.4.2 byteM (1 <= M <= length)*****/
  918.         for(M=1; M-1  < codeset->length; M++){
  919.             long start,end;
  920.             ByteInfo tmpb;
  921.  
  922.             sprintf(name,"%s.%s%d",cs,"byte",M);
  923.             _XlcGetResource(lcd, "XLC_XLOCALE", name, &value, &num);
  924.  
  925.             if( M == 1){
  926.                 if(num < 1) {
  927.                     codeset->byteM = NULL ;
  928.                     break ;
  929.                 }
  930.                 codeset->byteM =
  931.                     (ByteInfoListRec *)Xmalloc(
  932.                          (codeset->length)*sizeof(ByteInfoListRec));
  933.                 if(codeset->byteM == NULL){
  934.                     goto err;
  935.                 }
  936.             }
  937.  
  938.             if(num > 0){
  939.                 _XlcDbg_printValue(name,value,num);
  940.                 (codeset->byteM)[M-1].M = M;
  941.                 (codeset->byteM)[M-1].byteinfo_num = num;
  942.                 (codeset->byteM)[M-1].byteinfo =
  943.                     (ByteInfo)Xmalloc( num * sizeof(ByteInfoRec));
  944.                 for(ii = 0 ; ii < num ; ii++){
  945.                     tmpb = (codeset->byteM)[M-1].byteinfo ;
  946.                     /* default 0x00 - 0xff */
  947.                     sscanf(value[ii],"\\x%lx,\\x%lx",&start,&end);
  948.                     tmpb[ii].start = (unsigned char)start;
  949.                     tmpb[ii].end  = (unsigned char)end;
  950.                 }
  951.             }
  952.             /* .... */
  953.         }
  954.  
  955.  
  956.         /***** codeset.mb_conversion *****/
  957.         sprintf(name, "%s.%s", cs, "mb_conversion");
  958.         _XlcGetResource(lcd, "XLC_XLOCALE", name, &value, &num);
  959.         if (num > 0) {
  960.                 _XlcDbg_printValue(name,value,num);
  961.                 codeset->mbconv = Xmalloc(sizeof(ConversionRec));
  962.                 codeset->mbconv->convlist =
  963.                 _XlcParse_scopemaps(value[0],&(codeset->mbconv->conv_num));
  964.                 dmpscope("mb_conv",codeset->mbconv->convlist,
  965.                         codeset->mbconv->conv_num);
  966.                 /* [\x%x,\x%x]->\x%x,... */
  967.         }
  968.         /***** codeset.ct_conversion *****/
  969.         sprintf(name, "%s.%s", cs, "ct_conversion");
  970.         _XlcGetResource(lcd, "XLC_XLOCALE", name, &value, &num);
  971.         if (num > 0) {
  972.                 _XlcDbg_printValue(name,value,num);
  973.                 codeset->ctconv = Xmalloc(sizeof(ConversionRec));
  974.                 codeset->ctconv->convlist =
  975.                 _XlcParse_scopemaps(value[0],&(codeset->ctconv->conv_num));
  976.                 dmpscope("ctconv",codeset->ctconv->convlist,
  977.                         codeset->ctconv->conv_num);
  978.                 /* [\x%x,\x%x]->\x%x,... */
  979.         }
  980.         /***** codeset.ct_conversion_file *****/
  981.         sprintf(name, "%s.%s", cs, "ct_conversion_file");
  982.         _XlcGetResource(lcd, "XLC_XLOCALE", name, &value, &num);
  983.         if (num > 0) {
  984.                 _XlcDbg_printValue(name,value,num);
  985.                 /* [\x%x,\x%x]->\x%x,... */
  986.         }
  987.         /***** codeset.ct_extended_segment *****/
  988.         sprintf(name, "%s.%s", cs, "ct_extended_segment");
  989.         _XlcGetResource(lcd, "XLC_XLOCALE", name, &value, &num);
  990.         if (num > 0) {
  991.                 _XlcDbg_printValue(name,value,num);
  992.                 codeset->ctextseg = create_ctextseg(value,num);
  993.                 /* [\x%x,\x%x]->\x%x,... */
  994.         }
  995.         /* For VW/UDC end */
  996.  
  997.     }
  998.      
  999.     read_charset_define(lcd,gen);       /* For VW/UDC */
  1000.     read_segmentconversion(lcd,gen);    /* For VW/UDC */
  1001.  
  1002.     DBUG_RETURN(True);
  1003.  
  1004. err:
  1005.     free_charset(lcd);
  1006.  
  1007.     DBUG_RETURN(False);
  1008. }
  1009.  
  1010. static Bool
  1011. initialize(lcd)
  1012.     XLCd lcd;
  1013. {
  1014.     DBUG_ENTER("initialize")
  1015.     XLCdPublicMethods superclass = (XLCdPublicMethods) _XlcPublicMethods;
  1016.  
  1017.     XLC_PUBLIC_METHODS(lcd)->superclass = superclass;
  1018.  
  1019.     if (superclass->pub.initialize) {
  1020.     if ((*superclass->pub.initialize)(lcd) == False) {
  1021. #if defined(DEBUG) && defined(VERBOSE)
  1022.         fprintf(stderr,"(generic)initialize: superclass pub.initialize method failed!\n");
  1023. #endif
  1024.         DBUG_RETURN(False);
  1025.     }
  1026.     }
  1027.  
  1028.     if(load_generic(lcd) == False) {
  1029. #if defined(DEBUG) && defined(VERBOSE)
  1030.     fprintf(stderr,"(generic)initialize: load_generic() failed!\n");
  1031. #endif
  1032.     DBUG_RETURN(False);
  1033. }
  1034.  
  1035.     DBUG_RETURN(True);
  1036. }
  1037. /* VW/UDC start 95.01.08 */
  1038. static void 
  1039. freeByteM(codeset)
  1040.     CodeSet codeset;
  1041. {
  1042.     DBUG_ENTER("freeByteM")
  1043.     int i;
  1044.     ByteInfoList blst;
  1045.     if(codeset->byteM == NULL) {
  1046.     DBUG_VOID_RETURN;
  1047.     }
  1048.     blst = codeset->byteM;
  1049.     for(i=0;i<codeset->length;i++){
  1050.     if(blst[i].byteinfo){
  1051.         Xfree(blst[i].byteinfo);
  1052.         blst[i].byteinfo = NULL;
  1053.     }
  1054.     }
  1055.     Xfree(codeset->byteM); 
  1056.     codeset->byteM = NULL;
  1057.     DBUG_VOID_RETURN;
  1058. }
  1059. static void 
  1060. freeConversion(codeset)
  1061.     CodeSet codeset;
  1062. {
  1063.     DBUG_ENTER("freeConversion")
  1064.     int i;
  1065.     Conversion mbconv,ctconv;
  1066.     if( codeset->mbconv ) {
  1067.     mbconv = codeset->mbconv;
  1068.     /*  ...  */
  1069.     if(mbconv->convlist){
  1070.         Xfree(mbconv->convlist);
  1071.         mbconv->convlist = NULL;
  1072.     }
  1073.     Xfree(mbconv);
  1074.     codeset->mbconv = NULL;
  1075.     }
  1076.     if( codeset->ctconv ) {
  1077.     ctconv = codeset->ctconv;
  1078.     /*  ...  */
  1079.     if(ctconv->convlist){
  1080.         Xfree(ctconv->convlist);
  1081.         ctconv->convlist = NULL;
  1082.     }
  1083.     Xfree(ctconv);
  1084.     codeset->ctconv = NULL;
  1085.     }
  1086.     DBUG_VOID_RETURN;
  1087. }
  1088. static void 
  1089. freeExtdSegment(codeset)
  1090.     CodeSet codeset;
  1091. {
  1092.     DBUG_ENTER("freeExtdSegment")
  1093.     int i;
  1094.     ExtdSegment ctextseg;
  1095.     if(codeset->ctextseg == NULL) {
  1096.     DBUG_VOID_RETURN;
  1097.     }
  1098.     ctextseg = codeset->ctextseg;
  1099.     if(ctextseg->name){
  1100.     Xfree(ctextseg->name);
  1101.     ctextseg->name = NULL;
  1102.     }
  1103.     if(ctextseg->area){
  1104.     Xfree(ctextseg->area);
  1105.     ctextseg->area = NULL;
  1106.     }
  1107.     Xfree(codeset->ctextseg); 
  1108.     codeset->ctextseg = NULL;
  1109.     DBUG_VOID_RETURN;
  1110. }
  1111. static void 
  1112. freeParseInfo(codeset)
  1113.     CodeSet codeset;
  1114. {
  1115.     DBUG_ENTER("freeParseInfo")
  1116.     int i;
  1117.     ParseInfo parse_info;
  1118.     if(codeset->parse_info == NULL) {
  1119.     DBUG_VOID_RETURN;
  1120.     }
  1121.     parse_info = codeset->parse_info;
  1122.     if(parse_info->encoding){
  1123.     Xfree(parse_info->encoding);
  1124.     parse_info->encoding = NULL;
  1125.     }
  1126.     Xfree(codeset->parse_info); 
  1127.     codeset->parse_info = NULL;
  1128.     DBUG_VOID_RETURN;
  1129. }
  1130. static void
  1131. destroy_CodeSetList(gen)
  1132.     XLCdGenericPart *gen ;
  1133. {
  1134.     DBUG_ENTER("destroy_CodeSetList")
  1135.     CodeSet *codeset = gen->codeset_list;
  1136.     int i;
  1137.     if(gen->codeset_num == 0) {
  1138.     DBUG_VOID_RETURN;
  1139.     }
  1140.     for(i=0;i<gen->codeset_num;i++){
  1141.         freeByteM(codeset[i]);
  1142.     freeConversion(codeset[i]);
  1143.     freeExtdSegment(codeset[i]);
  1144.     freeParseInfo(codeset[i]);
  1145.     if(codeset[i]->charset_list){
  1146.         Xfree(codeset[i]->charset_list);
  1147.         codeset[i]->charset_list = NULL;
  1148.     }
  1149.     Xfree(codeset[i]); codeset[i]=NULL;
  1150.     }
  1151.     Xfree(codeset); gen->codeset_list = NULL;
  1152.     DBUG_VOID_RETURN;
  1153. }
  1154. /*  */
  1155. static void
  1156. destroy_SegConv(gen)
  1157.     XLCdGenericPart *gen ;
  1158. {
  1159.     DBUG_ENTER("destroy_SegConv")
  1160.     SegConv seg = gen->segment_conv;
  1161.     int i;
  1162.     if(gen->segment_conv_num == 0) {
  1163.     DBUG_VOID_RETURN;
  1164.     }
  1165.     for(i=0;i<gen->segment_conv_num;i++){
  1166.     if(seg[i].source_encoding){
  1167.         Xfree(seg[i].source_encoding);
  1168.         seg[i].source_encoding = NULL;
  1169.     }
  1170.     if(seg[i].destination_encoding){
  1171.         Xfree(seg[i].destination_encoding); 
  1172.         seg[i].destination_encoding = NULL;
  1173.     }
  1174.     if(seg[i].conv){
  1175.         Xfree(seg[i].conv); seg[i].conv = NULL;
  1176.     }
  1177.     }
  1178.     Xfree(seg); gen->segment_conv = NULL;
  1179.     DBUG_VOID_RETURN;
  1180. }
  1181.  
  1182. static void
  1183. destroy_gen(lcd)
  1184.     XLCd lcd;
  1185. {
  1186.     DBUG_ENTER("destroy_gen")
  1187.     XLCdGenericPart *gen = XLC_GENERIC_PART(lcd);
  1188.     destroy_SegConv(gen);
  1189.     destroy_CodeSetList(gen);
  1190.     if(gen->mb_parse_table){
  1191.     Xfree(gen->mb_parse_table);
  1192.     gen->mb_parse_table = NULL;
  1193.     }
  1194.     if(gen->mb_parse_list){
  1195.     Xfree(gen->mb_parse_list);
  1196.     gen->mb_parse_list = NULL;
  1197.     }
  1198.     DBUG_VOID_RETURN;
  1199. }
  1200. /* VW/UDC end 95.01.08 */
  1201. static void
  1202. destroy(lcd)
  1203.     XLCd lcd;
  1204. {
  1205.     DBUG_ENTER("destroy")
  1206.     XLCdPublicMethods superclass = XLC_PUBLIC_METHODS(lcd)->superclass;
  1207.  
  1208.     destroy_gen(lcd); /* ADD 1996.01.08 */
  1209.     if (superclass && superclass->pub.destroy)
  1210.     (*superclass->pub.destroy)(lcd);
  1211.     DBUG_VOID_RETURN;
  1212. }
  1213.