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