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