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

  1. /* $TOG: lcGenConv.c /main/15 1997/05/20 15:19:59 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.  *   Modifier: Masayoshi Shimamura      FUJITSU LIMITED
  32.  *
  33.  */
  34. /* $XFree86: xc/lib/X11/lcGenConv.c,v 3.6.2.2 1998/01/25 06:11:07 dawes Exp $ */
  35.  
  36.  
  37. #include "Xlib_private.h"
  38. #include "XlcGeneric.h"
  39. #include <stdio.h>
  40.  
  41. #if !defined(X_NOT_STDC_ENV) && !defined(macII) && !defined(Lynx_22)
  42. #define STDCVT
  43. #endif
  44.  
  45. typedef struct _CTDataRec {
  46.     char *name;
  47.     char *encoding;             /* Compound Text encoding */
  48. } CTDataRec, *CTData;
  49.  
  50. extern CTDataRec *default_ct_data_list();
  51. extern size_t      default_ct_data_list_num();
  52.  
  53. static CTDataRec directionality_data[] =
  54. {
  55.     { "BEGIN_LEFT-TO-RIGHT_TEXT", "\2331]" },
  56.     { "BEGIN_RIGHT-TO-LEFT_TEXT", "\2332]" },
  57.     { "END_OF_STRING", "\233]" },
  58. };
  59.  
  60. typedef struct _StateRec {
  61.     XLCd lcd;
  62.     XlcCharSet charset;        /* charset of current state */
  63.     XlcCharSet GL_charset;    /* charset of initial state in GL */
  64.     XlcCharSet GR_charset;    /* charset of initial state in GR */
  65. } StateRec, *State;
  66.  
  67. #define GR      0x80    /* begins right-side (non-ascii) region */
  68. #define GL      0x7f    /* ends left-side (ascii) region        */
  69. #define ESC    0x1b
  70. #define CSI    0x9b
  71. #define STX    0x02
  72.  
  73. #define isleftside(c)   (((c) & GR) ? 0 : 1)
  74. #define isrightside(c)  (!isleftside(c))
  75.  
  76.  
  77. /* Forward declarations for local routines. */
  78. static int mbstocts();
  79. static int ctstombs();
  80.  
  81.  
  82. /* ------------------------------------------------------------------------- */
  83. /*                Misc                                         */
  84. /* ------------------------------------------------------------------------- */
  85.  
  86. static int
  87. compare(src, encoding, length)
  88.     register char *src;
  89.     register char *encoding;
  90.     register int length;
  91. {
  92.     DBUG_ENTER("compare")
  93.     char *start = src;
  94.  
  95.     while (length-- > 0) {
  96.     if (*src++ != *encoding++)
  97.         DBUG_RETURN(0);
  98.     if (*encoding == '\0')
  99.         DBUG_RETURN(src - start);
  100.     }
  101.  
  102.     DBUG_RETURN(0);
  103. }
  104.  
  105. static unsigned long
  106. conv_to_dest(conv, code)
  107.     Conversion conv;
  108.     unsigned long code;
  109. {
  110.     DBUG_ENTER("conv_to_dest")
  111.     int i;
  112.     int conv_num = conv->conv_num;
  113.     FontScope convlist = conv->convlist;
  114.  
  115.     for (i = 0; i < conv_num; i++) {
  116.       if (convlist[i].start <= code && code <= convlist[i].end) {
  117.         switch (convlist[i].shift_direction) {
  118.         case '+':
  119.             DBUG_RETURN(code + convlist[i].shift);
  120.         case '-':
  121.         DBUG_RETURN(code - convlist[i].shift);
  122.             default:
  123.         DBUG_RETURN(code);
  124.         }
  125.     }
  126.     }
  127.  
  128.     DBUG_RETURN(code);
  129. }
  130.  
  131. static unsigned long
  132. conv_to_source(conv, code)
  133.     Conversion conv;
  134.     unsigned long code;
  135. {
  136.     DBUG_ENTER("conv_to_source")
  137.     int i;
  138.     int conv_num;
  139.     FontScope convlist;
  140.     unsigned long start_p;
  141.     unsigned long start_m;
  142.     unsigned long end_p;
  143.     unsigned long end_m;
  144.  
  145.     if (!conv)
  146.     DBUG_RETURN(code);
  147.  
  148.     conv_num = conv->conv_num;
  149.     convlist = conv->convlist;
  150.  
  151.     for (i = 0; i < conv_num; i++) {
  152.         start_p = convlist[i].start + convlist[i].shift;
  153.         start_m = convlist[i].start - convlist[i].shift;
  154.         end_p = convlist[i].end + convlist[i].shift;
  155.         end_m = convlist[i].end - convlist[i].shift;
  156.  
  157.         switch (convlist[i].shift_direction) {
  158.     case '+':
  159.         if (start_p <= code && code <= end_p)
  160.         DBUG_RETURN(code - convlist[i].shift);
  161.             break;
  162.     case '-':
  163.         if (start_m <= code && code <= end_m)
  164.         DBUG_RETURN(code + convlist[i].shift);
  165.             break;
  166.         default:
  167.         continue;
  168.     }
  169.     }
  170.  
  171.     DBUG_RETURN(code);
  172. }
  173.  
  174. static unsigned long
  175. mb_to_gi(mb, codeset)
  176.     unsigned long mb;
  177.     CodeSet codeset;
  178. {
  179.     DBUG_ENTER("mb_to_gi")
  180.     int i;
  181.     unsigned long mb_tmp, mask = 0;
  182.  
  183.     if (codeset->mbconv) {
  184.     mb_tmp = conv_to_dest(codeset->mbconv, mb);
  185.     if (mb_tmp != mb)
  186.         DBUG_RETURN(mb_tmp);
  187.     }
  188.  
  189.     if (codeset->side == XlcC0 || codeset->side == XlcGL || 
  190.     codeset->side == XlcC1 || codeset->side == XlcGR) {
  191.  
  192.         for (i = 0; i < codeset->length; i++)
  193.         mask = (mask << 8) | GL;
  194.     mb = mb & mask;
  195.     }
  196.  
  197.     DBUG_RETURN(mb);
  198. }
  199.  
  200. static unsigned long
  201. gi_to_mb(glyph_index, codeset)
  202.     unsigned long glyph_index;
  203.     CodeSet codeset;
  204. {
  205.     DBUG_ENTER("gi_to_mb")
  206.     int i;
  207.     unsigned long mask = 0;
  208.  
  209.     if (codeset->side == XlcC1 || codeset->side == XlcGR) {
  210.         for (i = 0; i < codeset->length; i++)
  211.         mask = (mask << 8) | GR;
  212.     glyph_index = glyph_index | mask;
  213.     }
  214.  
  215.     if (codeset->mbconv) {
  216.         unsigned long result = conv_to_source(codeset->mbconv, glyph_index);
  217.     DBUG_RETURN(result);
  218.     }
  219.  
  220.     DBUG_RETURN(glyph_index);
  221. }
  222.  
  223. static Bool
  224. gi_to_wc(lcd, glyph_index, codeset, wc)
  225.     XLCd lcd;
  226.     unsigned long glyph_index;
  227.     CodeSet codeset;
  228.     wchar_t *wc;
  229. {
  230.     DBUG_ENTER("gi_to_wc")
  231.     unsigned char mask = 0;
  232.     unsigned long wc_encoding = codeset->wc_encoding;
  233.     int i, length = codeset->length;
  234.     unsigned long wc_shift_bits = XLC_GENERIC(lcd, wc_shift_bits);
  235.  
  236.     for (i = wc_shift_bits; i > 0; i--)
  237.     mask = (mask << 1) | 0x01;
  238.  
  239.     for (*wc = 0, length--; length >= 0; length--)
  240.     *wc = (*wc << wc_shift_bits) | ((glyph_index >> (length * 8 )) & mask);
  241.  
  242.     *wc = *wc | wc_encoding;
  243.  
  244.     DBUG_RETURN(True);
  245. }
  246.  
  247. static Bool
  248. wc_to_gi(lcd, wc, glyph_index, codeset)
  249.     XLCd lcd;
  250.     wchar_t wc;
  251.     unsigned long *glyph_index;
  252.     CodeSet *codeset;
  253. {
  254.     DBUG_ENTER("wc_to_gi")
  255.     int i;
  256.     unsigned char mask = 0;
  257.     unsigned long wc_encoding;
  258.     unsigned long wc_encode_mask = XLC_GENERIC(lcd, wc_encode_mask);
  259.     unsigned long wc_shift_bits = XLC_GENERIC(lcd, wc_shift_bits);
  260.     int codeset_num = XLC_GENERIC(lcd, codeset_num);
  261.     CodeSet *codeset_list = XLC_GENERIC(lcd, codeset_list);
  262.  
  263.     wc_encoding = wc & wc_encode_mask;
  264.     for (*codeset = NULL, i = 0; i < codeset_num; i++) {
  265.     if (wc_encoding == codeset_list[i]->wc_encoding) {
  266.         *codeset = codeset_list[i];
  267.         break;
  268.         }
  269.     }
  270.     if (*codeset == NULL)
  271.     DBUG_RETURN(False);
  272.  
  273.     for (i = wc_shift_bits; i > 0; i--)
  274.     mask = (mask << 1) | 0x01;
  275.  
  276.     wc = wc & ~wc_encode_mask;
  277.     for (*glyph_index = 0, i = (*codeset)->length - 1; i >= 0; i--)
  278.     *glyph_index = (*glyph_index << 8) | 
  279.               ( ((unsigned long)wc >> (i * wc_shift_bits)) & mask );
  280.  
  281.     DBUG_RETURN(True);
  282. }
  283.  
  284. static CodeSet
  285. byteM_parse_codeset(lcd, inbufptr)
  286.     XLCd lcd;
  287.     XPointer inbufptr;
  288. {
  289.     DBUG_ENTER("byteM_parse_codeset")
  290.     unsigned char ch;
  291.     CodeSet codeset;
  292.     ByteInfoList byteM;
  293.     ByteInfoListRec byteM_rec;
  294.     ByteInfo byteinfo;
  295.     ByteInfoRec byteinfo_rec;
  296.     Bool hit;
  297.     int i, j, k;
  298.  
  299.     int codeset_num               = XLC_GENERIC(lcd, codeset_num);
  300.     CodeSet *codeset_list         = XLC_GENERIC(lcd, codeset_list);
  301.  
  302.     for (i = 0; i < codeset_num; i++) {
  303.         codeset = codeset_list[i];
  304.         byteM = codeset->byteM;
  305.         if (codeset->side != XlcNONE || byteM == NULL)
  306.         continue;
  307.  
  308.         for (j = 0; j < codeset->length; j++) {
  309.         ch = *((unsigned char *)(inbufptr + j)); 
  310.         byteM_rec = byteM[j];
  311.         byteinfo = byteM_rec.byteinfo;
  312.  
  313.         for (hit=False,k=0; k < byteM_rec.byteinfo_num; k++) {
  314.             byteinfo_rec = byteinfo[k];
  315.             if (byteinfo_rec.start <= ch && ch <= byteinfo_rec.end) {
  316.                 hit = True;
  317.             break;
  318.                 }
  319.             }
  320.  
  321.             if (!hit)
  322.         break;
  323.         }
  324.  
  325.         if (hit)
  326.         DBUG_RETURN(codeset);
  327.     }
  328.  
  329.     DBUG_RETURN(NULL);
  330. }                
  331.  
  332. static CodeSet
  333. GLGR_parse_codeset(lcd, ch)
  334.     XLCd lcd;
  335.     unsigned char ch;
  336. {
  337.     DBUG_ENTER("GLGR_parse_codeset")
  338.     int i;
  339.     CodeSet initial_state_GL      = XLC_GENERIC(lcd, initial_state_GL);
  340.     CodeSet initial_state_GR      = XLC_GENERIC(lcd, initial_state_GR);
  341.     CodeSet *codeset_list         = XLC_GENERIC(lcd, codeset_list);
  342.     int codeset_num               = XLC_GENERIC(lcd, codeset_num);
  343.  
  344.     XlcSide side = XlcGL;
  345.     CodeSet codeset = initial_state_GL;
  346.  
  347.     if (isrightside(ch)) {
  348.         side = XlcGR;
  349.         codeset = initial_state_GR;
  350.     }
  351.  
  352.     if (codeset)
  353.     DBUG_RETURN(codeset);
  354.  
  355.     for (i = 0; i < codeset_num; i++) {
  356.     codeset = codeset_list[i];
  357.     if (codeset->side == side)
  358.         DBUG_RETURN(codeset);
  359.     }
  360.     
  361.     DBUG_RETURN(NULL);
  362. }
  363.  
  364. static XlcCharSet
  365. gi_parse_charset(glyph_index, codeset)
  366.     unsigned long glyph_index;
  367.     CodeSet codeset;
  368. {
  369.     DBUG_ENTER("gi_parse_charset")
  370.     int i;
  371.     XlcCharSet *charset_list = codeset->charset_list;
  372.     int num_charsets = codeset->num_charsets;
  373.     ExtdSegment ctextseg = codeset->ctextseg;
  374.     XlcCharSet charset;
  375.     int area_num;
  376.     FontScope area;
  377.     CTDataRec *default_ct_data = default_ct_data_list();
  378.     size_t table_size = default_ct_data_list_num();
  379.  
  380.     /* lockup ct sequence */
  381.     for (i = 0; i < num_charsets; i++) {
  382.     charset = charset_list[i];
  383.         if (*charset->ct_sequence != '\0')
  384.         break;
  385.     }
  386.     if (i >= num_charsets)
  387.     DBUG_RETURN(NULL);
  388.  
  389.     /* Standard Character Set Encoding ? */
  390.     for (i = 0; i < table_size; i++)
  391.         if (compare(charset->ct_sequence, 
  392.            default_ct_data[i].encoding, strlen(charset->ct_sequence)))
  393.         goto check_extended_seg;
  394.  
  395.     DBUG_RETURN(charset);
  396.  
  397. check_extended_seg:
  398.     if (!ctextseg)
  399.         DBUG_RETURN(charset);
  400.  
  401.     area = ctextseg->area;
  402.     area_num = ctextseg->area_num;
  403.  
  404.     for (i = 0; i < area_num; i++) {
  405.  
  406.         if (area[i].start <= glyph_index && glyph_index <= area[i].end) {
  407.  
  408.         charset = ctextseg->charset;
  409.  
  410.             if (*charset->ct_sequence == '\0')
  411.                 DBUG_RETURN(NULL);
  412.  
  413.         break;
  414.     }
  415.     }
  416.  
  417.     DBUG_RETURN(charset);
  418. }
  419.  
  420. static Bool
  421. ct_parse_csi(inbufptr, ctr_seq_len)
  422.     XPointer inbufptr;
  423.     int *ctr_seq_len;
  424. {
  425.     DBUG_ENTER("ct_parse_csi")
  426.     int i;
  427.     int num = sizeof(directionality_data) / sizeof(directionality_data[0]);
  428.  
  429.     for (i = 0; i < num; i++) {
  430.     if ( !(*ctr_seq_len = strlen(directionality_data[i].encoding)) )
  431.         continue;
  432.  
  433.     if ( strncmp(inbufptr, directionality_data[i].encoding, 
  434.                         *ctr_seq_len) == 0)
  435.             DBUG_RETURN(True);
  436.     }
  437.  
  438.     DBUG_RETURN(False);
  439. }
  440.  
  441. static int
  442. cmp_esc_sequence(inbufptr, ct_sequence, encoding_name)
  443.     XPointer inbufptr;
  444.     char *ct_sequence;
  445.     char *encoding_name;
  446. {
  447.     DBUG_ENTER("cmp_esc_sequence")
  448.     int i, seq_len, name_len, total_len;
  449.     unsigned char byte_m, byte_l;
  450.     CTDataRec *default_ct_data = default_ct_data_list();
  451.     size_t table_size = default_ct_data_list_num();
  452.  
  453.     /* check esc sequence */
  454.     if ( !(seq_len = strlen(ct_sequence) ) )
  455.     DBUG_RETURN(0);
  456.     if ( strncmp(inbufptr, ct_sequence, seq_len) != 0)
  457.     DBUG_RETURN(0);
  458.  
  459.     /* Standard Character Set Encoding ? */
  460.     for (i = 0; i < table_size; i++) {
  461.         if (compare(ct_sequence, 
  462.                 default_ct_data[i].encoding, strlen(ct_sequence)))
  463.             DBUG_RETURN(seq_len);
  464.     }
  465.  
  466.     /*
  467.      *   Non-Standard Character Set Encoding
  468.      *
  469.      * +-----+-----+-----+-----+-----+-----+-----+----   ----+-----+-----+
  470.      * |     esc sequence      |  M  |  L  |     encoding name     | STX |
  471.      * +-----+-----+-----+-----+-----+-----+-----+----   ----+-----+-----+
  472.      *           4bytes         1byte 1byte     variable length     1byte 
  473.      *                        |                                         |
  474.      *                        +-----------------------------------------+
  475.      *                          name length  = ((M - 128) * 128) + (L - 128)
  476.      */
  477.  
  478.     /* get length of encoding name */
  479.     inbufptr += seq_len;
  480.     byte_m = *inbufptr++;
  481.     byte_l = *inbufptr++;
  482.     name_len = ((byte_m - 128) * 128) + (byte_l - 128);
  483.     total_len = seq_len + name_len;
  484.  
  485.     /* compare encoding names */
  486.     if ( strncmp(inbufptr, encoding_name, name_len - 3) != 0 )
  487.     DBUG_RETURN(0);
  488.  
  489.     /* check STX (Start of Text) */
  490.     inbufptr = inbufptr + name_len - 3;
  491.     if ( *inbufptr != STX )
  492.     DBUG_RETURN(0);
  493.  
  494.     DBUG_RETURN(total_len);
  495. }
  496.  
  497. static Bool
  498. ct_parse_charset(lcd, inbufptr, charset, ctr_seq_len)
  499.     XLCd lcd;
  500.     XPointer inbufptr;
  501.     XlcCharSet *charset;
  502.     int *ctr_seq_len;
  503. {
  504.     DBUG_ENTER("ct_parse_charset")
  505.     int i, j;
  506.     ExtdSegment ctextseg;
  507.     int num_charsets;
  508.     XlcCharSet *charset_list;
  509.     CodeSet codeset;
  510.     int codeset_num       = XLC_GENERIC(lcd, codeset_num);
  511.     CodeSet *codeset_list = XLC_GENERIC(lcd, codeset_list);
  512.     int segment_conv_num  = XLC_GENERIC(lcd, segment_conv_num);
  513.     SegConv segment_conv  = XLC_GENERIC(lcd, segment_conv);
  514.  
  515.  
  516.     /* get charset from XLC_XLOCALE by escape sequence */
  517.  
  518.     for (i = 0; i < codeset_num; i++) {
  519.     codeset = codeset_list[i];
  520.  
  521.     num_charsets = codeset->num_charsets;
  522.     charset_list = codeset->charset_list;
  523.     ctextseg     = codeset->ctextseg;
  524.  
  525.     for (j = 0; j < num_charsets; j++) {
  526.         *charset = charset_list[j];
  527.             if ( (*ctr_seq_len = cmp_esc_sequence(inbufptr, 
  528.             (*charset)->ct_sequence, (*charset)->encoding_name)) )
  529.         DBUG_RETURN(True);
  530.     }
  531.  
  532.     if (ctextseg) {
  533.         *charset = ctextseg->charset;
  534.             if ( (*ctr_seq_len = cmp_esc_sequence(inbufptr, 
  535.             (*charset)->ct_sequence, (*charset)->encoding_name)) )
  536.         DBUG_RETURN(True);
  537.     }
  538.     }
  539.  
  540.     /* get charset from XLC_SEGMENTCONVERSION by escape sequence */
  541.  
  542.     if (!segment_conv)
  543.     DBUG_RETURN(False);
  544.  
  545.     for (i = 0; i < segment_conv_num; i++) {
  546.     *charset = segment_conv[i].source;
  547.         if ( (*ctr_seq_len = cmp_esc_sequence(inbufptr, 
  548.             (*charset)->ct_sequence, (*charset)->encoding_name)) )
  549.         DBUG_RETURN(True);
  550.     *charset = segment_conv[i].dest;
  551.         if ( (*ctr_seq_len = cmp_esc_sequence(inbufptr, 
  552.             (*charset)->ct_sequence, (*charset)->encoding_name)) )
  553.         DBUG_RETURN(True);
  554.     }
  555.     
  556.     DBUG_RETURN(False);
  557. }
  558.  
  559. static Bool
  560. segment_conversion(lcd, charset, glyph_index)
  561.     XLCd lcd;
  562.     XlcCharSet *charset;
  563.     unsigned long *glyph_index;
  564. {
  565.     DBUG_ENTER("segment_conversion")
  566.     int i;
  567.     int segment_conv_num = XLC_GENERIC(lcd, segment_conv_num);
  568.     SegConv segment_conv = XLC_GENERIC(lcd, segment_conv);
  569.     FontScopeRec range;
  570.     ConversionRec conv_rec;
  571.  
  572.     if (!segment_conv)
  573.     DBUG_RETURN(True);
  574.  
  575.     for (i = 0; i < segment_conv_num; i++) {
  576.     if (segment_conv[i].source == *charset)
  577.         break;
  578.     }
  579.  
  580.     if (i >= segment_conv_num)
  581.     DBUG_RETURN(True);
  582.  
  583.     range = segment_conv[i].range;
  584.     if (*glyph_index < range.start || range.end < *glyph_index)
  585.     DBUG_RETURN(True);
  586.     
  587.     *charset = segment_conv[i].dest;
  588.     conv_rec.conv_num = segment_conv[i].conv_num;
  589.     conv_rec.convlist = segment_conv[i].conv;
  590.     *glyph_index = conv_to_dest(&conv_rec, *glyph_index);
  591.  
  592.     DBUG_RETURN(True);
  593. }
  594.  
  595. CodeSet
  596. _XlcGetCodeSetFromName(lcd, name)
  597.     XLCd lcd;
  598.     char *name;
  599. {
  600.     DBUG_ENTER("_XlcGetCodeSetFromName")
  601.     int i, j;
  602.     XlcCharSet charset;
  603.     int num_charsets;
  604.     XlcCharSet *charset_list;
  605.     CodeSet codeset;
  606.  
  607.     int codeset_num       = XLC_GENERIC(lcd, codeset_num);
  608.     CodeSet *codeset_list = XLC_GENERIC(lcd, codeset_list);
  609.  
  610.     for (i = 0; i < codeset_num; i++) {
  611.         codeset = codeset_list[i];
  612.  
  613.         num_charsets = codeset->num_charsets;
  614.         charset_list = codeset->charset_list;
  615.  
  616.         for (j = 0; j < num_charsets; j++) {
  617.             charset = charset_list[j];
  618.  
  619.             if (!strlen(charset->name))
  620.                 continue;
  621.             if ( strcmp(charset->name, name) == 0)
  622.                 DBUG_RETURN(codeset);
  623.         }
  624.     }
  625.  
  626.     DBUG_RETURN(NULL);
  627. }
  628.  
  629. static Bool
  630. _XlcGetCodeSetFromCharSet(lcd, charset, codeset, glyph_index)
  631.     XLCd lcd;
  632.     XlcCharSet charset;
  633.     CodeSet *codeset;
  634.     unsigned long *glyph_index;
  635. {
  636.     DBUG_ENTER("_XlcGetCodeSetFromCharSet")
  637.     int i, j, num;
  638.     CodeSet *codeset_list = XLC_GENERIC(lcd, codeset_list);
  639.     XlcCharSet *charset_list;
  640.     int codeset_num, num_charsets;
  641.     Conversion ctconv;
  642.     unsigned long glyph_index_tmp;
  643.     ExtdSegment ctextseg;
  644.     CTDataRec *default_ct_data = default_ct_data_list();
  645.     size_t table_size = default_ct_data_list_num();
  646.  
  647.     codeset_num = XLC_GENERIC(lcd, codeset_num);
  648.  
  649.     for (num = 0 ; num < codeset_num; num++) {
  650.         *codeset = codeset_list[num];
  651.         ctconv = (*codeset)->ctconv;
  652.     ctextseg = (*codeset)->ctextseg;
  653.  
  654.         num_charsets = (*codeset)->num_charsets;
  655.         charset_list = (*codeset)->charset_list;
  656.  
  657.         glyph_index_tmp = conv_to_source(ctconv, *glyph_index);
  658.  
  659.         /* Standard Character Set Encoding ? */
  660.         for (i = 0; i < table_size; i++) {
  661.             if (compare(charset->ct_sequence, 
  662.                default_ct_data[i].encoding, strlen(charset->ct_sequence)))
  663.             break;
  664.         }
  665.  
  666.         if (i < table_size) {
  667.  
  668.             /* Standard Character Set Encoding */
  669.         if (glyph_index_tmp == *glyph_index) {
  670.                 for (j = 0; j < num_charsets; j++) {
  671.                     if (charset_list[j] == charset) {
  672.                         goto end_loop;
  673.                     }
  674.                 }
  675.             }
  676.  
  677.     } else {
  678.  
  679.             /* Non-Standard Character Set Encoding */
  680.             for (j = 0; j < num_charsets; j++) {
  681.                 if (charset_list[j] == charset) {
  682.                     goto end_loop;
  683.                 }
  684.             }
  685.  
  686.             if (glyph_index_tmp != *glyph_index) {
  687.         if (ctextseg->charset == charset) {
  688.             goto end_loop;
  689.                 }
  690.             }
  691.  
  692.     }
  693.  
  694.     }
  695.  
  696. end_loop:
  697.     if (num < codeset_num) {
  698.     *glyph_index = glyph_index_tmp;
  699.     DBUG_RETURN(True);
  700.     }
  701.  
  702.     DBUG_RETURN(False);
  703. }
  704.  
  705. static Bool
  706. check_string_encoding(codeset)
  707.     CodeSet codeset;
  708. {
  709.     DBUG_ENTER("check_string_encoding")
  710.     int i;
  711.     XlcCharSet charset;
  712.     XlcCharSet *charset_list = codeset->charset_list;
  713.     int num_charsets = codeset->num_charsets;
  714.  
  715.     for (i = 0; i < num_charsets; i++) {
  716.         charset = charset_list[i];
  717.         if ( strcmp(charset->encoding_name, "ISO8859-1") == 0 || 
  718.          charset->string_encoding)
  719.             DBUG_RETURN(True);
  720.     }
  721.  
  722.     DBUG_RETURN(False);
  723. }
  724.  
  725. static void
  726. output_ulong_value(outbufptr, code, length, side)
  727.     XPointer outbufptr;
  728.     unsigned long code;
  729.     int length;
  730.     XlcSide side;
  731. {
  732.     DBUG_ENTER("output_ulong_value")
  733.     int i;
  734.     unsigned long mask = 0xff;
  735.  
  736.     for (i = 0; i < length; i++) {
  737.     *outbufptr = ( code >> (8 * (length - i - 1)) ) & mask;
  738.  
  739.     if (side == XlcC0 || side == XlcGL) {
  740.         *outbufptr = *outbufptr & GL;
  741.     } else if (side == XlcC1 || side == XlcGR) {
  742.         *outbufptr = *outbufptr | GR;
  743.     }
  744.  
  745.     outbufptr++;
  746.     }
  747.     DBUG_VOID_RETURN;
  748. }
  749.  
  750. /* -------------------------------------------------------------------------- */
  751. /*                Init                                          */
  752. /* -------------------------------------------------------------------------- */
  753.  
  754. static void
  755. init_state(conv)
  756.     XlcConv conv;
  757. {
  758.     DBUG_ENTER("init_state")
  759.     register State state = (State) conv->state;
  760.  
  761.     /* for CT */
  762.     state->charset = NULL;
  763.     state->GL_charset = _XlcGetCharSet("ISO8859-1:GL");
  764.     state->GR_charset = _XlcGetCharSet("ISO8859-1:GR");
  765.     DBUG_VOID_RETURN;
  766. }
  767.  
  768. /* -------------------------------------------------------------------------- */
  769. /*                Convert                                       */
  770. /* -------------------------------------------------------------------------- */
  771.  
  772. static int
  773. mbstowcs_org(conv, from, from_left, to, to_left, args, num_args)
  774.     XlcConv conv;
  775.     XPointer *from;
  776.     int *from_left;
  777.     XPointer *to;
  778.     int *to_left;
  779.     XPointer *args;
  780.     int num_args;
  781. {
  782.     DBUG_ENTER("mbstowcs_org")
  783.     State state = (State) conv->state;
  784.     XLCd lcd = state->lcd;
  785.  
  786.     unsigned char ch;
  787.     unsigned long mb = 0;
  788.     wchar_t wc;
  789.  
  790.     int length = 0, len_left = 0;
  791.     int unconv_num = 0;
  792.     int num;
  793.     Bool ss_flag = 0;
  794.  
  795.     CodeSet codeset = NULL;
  796.     ParseInfo parse_info;
  797.  
  798.     XPointer inbufptr = *from;
  799.     wchar_t *outbufptr = (wchar_t *) *to;
  800.     int from_size = *from_left;
  801.  
  802.     unsigned char *mb_parse_table = XLC_GENERIC(lcd, mb_parse_table);
  803.     ParseInfo *mb_parse_list      = XLC_GENERIC(lcd, mb_parse_list);
  804.  
  805.  
  806.     if (*from_left > *to_left)
  807.         *from_left = *to_left;
  808.  
  809.     while (*from_left && *to_left) {
  810.  
  811.     ch = *inbufptr++;
  812.     (*from_left)--;
  813.  
  814.     /* null ? */
  815.     if (!ch) {
  816.             if (outbufptr) {*outbufptr++ = L'\0';}
  817.         (*to_left)--;
  818.  
  819.         /* error check */
  820.             if (len_left) {
  821.             unconv_num += (length - len_left);
  822.         len_left = 0;
  823.             }
  824.  
  825.         continue;
  826.     }
  827.  
  828.     /* same mb char data */
  829.         if (len_left)
  830.         goto output_one_wc;
  831.  
  832.         /* next mb char data for single shift ? */
  833.     if (mb_parse_table) {
  834.         if ((num = mb_parse_table[ch]) > 0) {
  835.         parse_info = mb_parse_list[num - 1];
  836.  
  837.                 codeset = parse_info->codeset;
  838.         length = len_left = codeset->length;
  839.         mb = 0;
  840.         ss_flag = 1;
  841.  
  842.         continue;
  843.         }
  844.         } 
  845.     
  846.     /* next mb char data for byteM ? */
  847.     if ((codeset = byteM_parse_codeset(lcd, (inbufptr - 1))))
  848.         goto next_mb_char;
  849.  
  850.     /* next mb char data for GL or GR side ? */
  851.     if ((codeset = GLGR_parse_codeset(lcd, ch)))
  852.         goto next_mb_char;
  853.         
  854.         /* can't find codeset for the ch */
  855.         unconv_num++;
  856.         continue;
  857.  
  858. next_mb_char:
  859.         length = len_left = codeset->length;
  860.     mb = 0;
  861.     ss_flag = 0;
  862.  
  863. output_one_wc:
  864.     mb = (mb << 8) | ch;  /* 1 byte left shift */
  865.     len_left--;
  866.  
  867.         /* last of one mb char data */
  868.         if (!len_left) {
  869.             gi_to_wc(lcd, mb_to_gi(mb, codeset), codeset, &wc);
  870.             if (outbufptr) {*outbufptr++ = wc;}
  871.         (*to_left)--;
  872.         }
  873.  
  874.     } /* end of while */
  875.  
  876.     /* error check on last char */
  877.     if (len_left) {
  878.     inbufptr -= (length - len_left + ss_flag);
  879.     (*from_left) += (length - len_left + ss_flag);
  880.     unconv_num += (length - len_left + ss_flag);
  881.     }
  882.  
  883.     *from = *from + from_size;
  884.     *from_left = 0;
  885.     *to = (XPointer)outbufptr;
  886.  
  887.     DBUG_RETURN(unconv_num);
  888. }
  889.  
  890. static int
  891. stdc_mbstowcs(conv, from, from_left, to, to_left, args, num_args)
  892.     XlcConv conv;
  893.     XPointer *from;
  894.     int *from_left;
  895.     XPointer *to;
  896.     int *to_left;
  897.     XPointer *args;
  898.     int num_args;
  899. {
  900.     DBUG_ENTER("stdc_mbstowcs")
  901.     char *src = *((char **) from);
  902.     wchar_t *dst = *((wchar_t **) to);
  903.     int src_left = *from_left;
  904.     int dst_left = *to_left;
  905.     int length, unconv_num = 0;
  906.  
  907.     while (src_left > 0 && dst_left > 0) {
  908.     length = mbtowc(dst, src, src_left);
  909.  
  910.     if (length > 0) {
  911.         src += length;
  912.         src_left -= length;
  913.         if (dst)
  914.             dst++;
  915.         dst_left--;
  916.     } else if (length < 0) {
  917.         src++;
  918.         src_left--;
  919.         unconv_num++;
  920.         } else {
  921.             /* null ? */
  922.             src++;
  923.             src_left--;
  924.             if (dst) 
  925.                 *dst++ = L'\0';
  926.             dst_left--;
  927.         }
  928.     }
  929.  
  930.     *from = (XPointer) src;
  931.     if (dst)
  932.     *to = (XPointer) dst;
  933.     *from_left = src_left;
  934.     *to_left = dst_left;
  935.  
  936.     DBUG_RETURN(unconv_num);
  937. }
  938.  
  939. static int
  940. wcstombs_org(conv, from, from_left, to, to_left, args, num_args)
  941.     XlcConv conv;
  942.     XPointer *from;
  943.     int *from_left;
  944.     XPointer *to;
  945.     int *to_left;
  946.     XPointer *args;
  947.     int num_args;
  948. {
  949.     DBUG_ENTER("wcstombs_org")
  950.     State state = (State) conv->state;
  951.     XLCd lcd = state->lcd;
  952.  
  953.     char *encoding;
  954.     unsigned long mb, glyph_index;
  955.     wchar_t wc;
  956.  
  957.     int length;
  958.     int unconv_num = 0;
  959.  
  960.     CodeSet codeset;
  961.  
  962.     wchar_t *inbufptr = (wchar_t *) *from;
  963.     XPointer outbufptr = *to;
  964.     int from_size = *from_left;
  965.     
  966.     char *default_string = XLC_PUBLIC(lcd, default_string);
  967.     int defstr_len = strlen(default_string);
  968.  
  969.  
  970.     if (*from_left > *to_left)
  971.         *from_left = *to_left;
  972.  
  973.     while (*from_left && *to_left) {
  974.  
  975.         wc = *inbufptr++;
  976.         (*from_left)--;
  977.  
  978.         /* null ? */
  979.         if (!wc) {
  980.             if (outbufptr) {*outbufptr++ = '\0';}
  981.             (*to_left)--;
  982.  
  983.             continue;
  984.         }
  985.  
  986.         /* convert */
  987.     if ( !wc_to_gi(lcd, wc, &glyph_index, &codeset) ) {
  988.  
  989.         /* output default_string of XDefaultString() */
  990.             if (*to_left < defstr_len)
  991.         break;
  992.         if (outbufptr) {strncpy((char *)outbufptr, default_string, defstr_len);}
  993.  
  994.         if (outbufptr) {outbufptr += defstr_len;}
  995.         (*to_left) -= defstr_len;
  996.  
  997.             unconv_num++;
  998.  
  999.         } else {
  1000.             mb = gi_to_mb(glyph_index, codeset);
  1001.         if (codeset->parse_info) {
  1002.  
  1003.         /* output shift sequence */
  1004.         encoding = codeset->parse_info->encoding;
  1005.                 length = strlen(encoding);
  1006.                 if (*to_left < length)
  1007.             break;
  1008.             if (outbufptr) {strncpy((char *)outbufptr, encoding, length);}
  1009.  
  1010.             if (outbufptr) {outbufptr += length;}
  1011.             (*to_left) -= length;
  1012.             }
  1013.  
  1014.             /* output characters */
  1015.         length = codeset->length;
  1016.             if (*to_left < length)
  1017.         break;
  1018.  
  1019.         if (outbufptr) {
  1020.         output_ulong_value(outbufptr, mb, length, XlcNONE);
  1021.             outbufptr += length;
  1022.         }
  1023.  
  1024.         (*to_left) -= length;
  1025.         }
  1026.  
  1027.     } /* end of while */
  1028.  
  1029.     *from = *from + from_size;
  1030.     *from_left = 0;
  1031.     *to = outbufptr;
  1032.  
  1033.     DBUG_RETURN(unconv_num);
  1034. }
  1035.  
  1036. static int
  1037. stdc_wcstombs(conv, from, from_left, to, to_left, args, num_args)
  1038.     XlcConv conv;
  1039.     XPointer *from;
  1040.     int *from_left;
  1041.     XPointer *to;
  1042.     int *to_left;
  1043.     XPointer *args;
  1044.     int num_args;
  1045. {
  1046.     DBUG_ENTER("stdc_wcstombs")
  1047.     wchar_t *src = *((wchar_t **) from);
  1048.     char *dst = *((char **) to);
  1049.     int src_left = *from_left;
  1050.     int dst_left = *to_left;
  1051.     int length, unconv_num = 0;
  1052.  
  1053.     while (src_left > 0 && dst_left >= MB_CUR_MAX) {
  1054.     length = wctomb(dst, *src);        /* XXX */
  1055.  
  1056.         if (length > 0) {
  1057.         src++;
  1058.         src_left--;
  1059.         if (dst) 
  1060.         dst += length;
  1061.         dst_left -= length;
  1062.     } else if (length < 0) {
  1063.         src++;
  1064.         src_left--;
  1065.         unconv_num++;
  1066.     } 
  1067.     }
  1068.  
  1069.     *from = (XPointer) src;
  1070.     if (dst)
  1071.       *to = (XPointer) dst;
  1072.     *from_left = src_left;
  1073.     *to_left = dst_left;
  1074.  
  1075.     DBUG_RETURN(unconv_num);
  1076. }
  1077.  
  1078. static int
  1079. wcstocts(conv, from, from_left, to, to_left, args, num_args)
  1080.     XlcConv conv;
  1081.     XPointer *from;
  1082.     int *from_left;
  1083.     XPointer *to;
  1084.     int *to_left;
  1085.     XPointer *args;
  1086.     int num_args;
  1087. {
  1088.     DBUG_ENTER("wcstocts")
  1089.     State state = (State) conv->state;
  1090.     XLCd lcd = state->lcd;
  1091.     CTDataRec *default_ct_data = default_ct_data_list();
  1092.     size_t table_size = default_ct_data_list_num();
  1093.  
  1094.     unsigned long glyph_index;
  1095.     wchar_t wc;
  1096.  
  1097.     int i, total_len, seq_len, name_len;
  1098.     int unconv_num = 0;
  1099.     Bool first_flag = True, standard_flag;
  1100.     XlcSide side;
  1101.  
  1102.     CodeSet codeset;
  1103.     XlcCharSet charset, old_charset = NULL;
  1104.     char *ct_sequence;
  1105.     XPointer p;
  1106.  
  1107.     wchar_t *inbufptr = (wchar_t *) *from;
  1108.     XPointer outbufptr = *to;
  1109.     int from_size = *from_left;
  1110.  
  1111.  
  1112.     if (*from_left > *to_left)
  1113.         *from_left = *to_left;
  1114.  
  1115.     while (*from_left && *to_left) {
  1116.  
  1117.         wc = *inbufptr++;
  1118.         (*from_left)--;
  1119.  
  1120.         /* null ? */
  1121.         if (!wc) {
  1122.             if (outbufptr) {*outbufptr++ = '\0';}
  1123.             (*to_left)--;
  1124.  
  1125.             continue;
  1126.         }
  1127.  
  1128.         /* convert */
  1129.     if ( !wc_to_gi(lcd, wc, &glyph_index, &codeset) ) {
  1130.             unconv_num++;
  1131.         continue;
  1132.         }
  1133.  
  1134.         /* parse charset */
  1135.         if ( !(charset = gi_parse_charset(glyph_index, codeset)) ) {
  1136.             unconv_num++;
  1137.         continue;
  1138.         }
  1139.  
  1140.         /* Standard Character Set Encoding ? */
  1141.     standard_flag = False;
  1142.         for (i = 0; i < table_size; i++)
  1143.             if (compare(charset->ct_sequence, 
  1144.                     default_ct_data[i].encoding, strlen(charset->ct_sequence)))
  1145.                 standard_flag = True;
  1146.  
  1147.         /*
  1148.          *   Non-Standard Character Set Encoding
  1149.          *
  1150.          * +-----+-----+-----+-----+-----+-----+-----+----   ----+-----+-----+
  1151.          * |     esc sequence      |  M  |  L  |     encoding name     | STX |
  1152.          * +-----+-----+-----+-----+-----+-----+-----+----   ----+-----+-----+
  1153.          *           4bytes         1byte 1byte     variable length     1byte 
  1154.          *                        |                                         |
  1155.          *                        +-----------------------------------------+
  1156.          *                          name length  = ((M - 128) * 128) + (L - 128)
  1157.          */
  1158.  
  1159.         /* make encoding data */
  1160.     ct_sequence = charset->ct_sequence;
  1161.     side = charset->side;
  1162.         seq_len = strlen(charset->ct_sequence);
  1163.     if (standard_flag) {
  1164.             name_len = 0;
  1165.         total_len = seq_len;
  1166.     } else {
  1167.             name_len = 2 + strlen(charset->encoding_name) + 1;
  1168.         total_len = seq_len + name_len;
  1169.     }
  1170.  
  1171.         /* output escape sequence of CT */
  1172.     if ( (charset != old_charset) &&
  1173.         !(first_flag && (strcmp(charset->encoding_name,"ISO8859-1")==0))) {
  1174.  
  1175.         if (*to_left < total_len + 1) {
  1176.                 unconv_num++;
  1177.             break;
  1178.         }
  1179.  
  1180.         if (outbufptr) {
  1181.             strcpy((char *)outbufptr, charset->ct_sequence);
  1182.         outbufptr += seq_len;
  1183.  
  1184.                 if (!standard_flag) {
  1185.             *outbufptr++ = name_len / 128 + 128;
  1186.             *outbufptr++ = name_len % 128 + 128;
  1187.                 strcpy((char *)outbufptr, charset->encoding_name);
  1188.             outbufptr = outbufptr + name_len - 2 - 1;
  1189.             *outbufptr++ = STX;
  1190.                 }
  1191.         }
  1192.  
  1193.         (*to_left) -= total_len;
  1194.  
  1195.         first_flag = False;
  1196.         old_charset = charset;
  1197.     }
  1198.  
  1199.         /* output glyph index */
  1200.     if (codeset->ctconv)
  1201.             glyph_index = conv_to_dest(codeset->ctconv, glyph_index);
  1202.         if (*to_left < charset->char_size) {
  1203.             unconv_num++;
  1204.         break;
  1205.         }
  1206.  
  1207.     if (outbufptr) {
  1208.        output_ulong_value(outbufptr, glyph_index, charset->char_size, side);
  1209.        outbufptr += charset->char_size;
  1210.     }
  1211.  
  1212.     (*to_left) -= charset->char_size;
  1213.  
  1214.     } /* end of while */
  1215.  
  1216.     *from = *from + from_size;
  1217.     *from_left = 0;
  1218.     *to = outbufptr;
  1219.  
  1220.     DBUG_RETURN(unconv_num);
  1221. }
  1222.  
  1223. static int
  1224. stdc_wcstocts(conv, from, from_left, to, to_left, args, num_args)
  1225.     XlcConv conv;
  1226.     XPointer *from;
  1227.     int *from_left;
  1228.     XPointer *to;
  1229.     int *to_left;
  1230.     XPointer *args;
  1231.     int num_args;
  1232. {
  1233.     DBUG_ENTER("stdc_wcstocts")
  1234.     XPointer buf = Xmalloc((*from_left) * MB_CUR_MAX);
  1235.     XPointer buf_ptr1 = buf;
  1236.     int buf_left1 = (*from_left) * MB_CUR_MAX;
  1237.     XPointer buf_ptr2 = buf_ptr1;
  1238.     int buf_left2;
  1239.     int unconv_num1 = 0, unconv_num2 = 0;
  1240.  
  1241.     unconv_num1 = stdc_wcstombs(conv, 
  1242.         from, from_left, &buf_ptr1, &buf_left1, args, num_args);
  1243.     if (unconv_num1 < 0)
  1244.         goto ret;
  1245.  
  1246.     buf_left2 = buf_ptr1 - buf_ptr2;
  1247.  
  1248.     unconv_num2 = mbstocts(conv, 
  1249.         &buf_ptr2, &buf_left2, to, to_left, args, num_args);
  1250.     if (unconv_num2 < 0)
  1251.         goto ret;
  1252.  
  1253. ret:
  1254.     if (buf)
  1255.     Xfree((char *)buf);
  1256.  
  1257.     DBUG_RETURN(unconv_num1 + unconv_num2);
  1258. }
  1259.  
  1260. static int
  1261. ctstowcs(conv, from, from_left, to, to_left, args, num_args)
  1262.     XlcConv conv;
  1263.     XPointer *from;
  1264.     int *from_left;
  1265.     XPointer *to;
  1266.     int *to_left;
  1267.     XPointer *args;
  1268.     int num_args;
  1269. {
  1270.     DBUG_ENTER("ctstowcs")
  1271.     State state = (State) conv->state;
  1272.     XLCd lcd = state->lcd;
  1273.  
  1274.     unsigned char ch;
  1275.     unsigned long glyph_index = 0;
  1276.     wchar_t wc;
  1277.  
  1278.     int ctr_seq_len = 0, gi_len_left = 0, gi_len = 0;
  1279.     int unconv_num = 0;
  1280.  
  1281.     CodeSet codeset = NULL;
  1282.     XlcCharSet charset_tmp;
  1283.  
  1284.     XPointer inbufptr = *from;
  1285.     wchar_t *outbufptr = (wchar_t *) *to;
  1286.     int from_size = *from_left;
  1287.  
  1288.  
  1289.     init_state(conv);
  1290.  
  1291.     if (from == NULL || *from == NULL) {
  1292.     init_state(conv);
  1293.         DBUG_RETURN( 0 );
  1294.     }
  1295.  
  1296.     if (*from_left > *to_left)
  1297.         *from_left = *to_left;
  1298.  
  1299.     while (*from_left && *to_left) {
  1300.  
  1301.     ch = *inbufptr++;
  1302.     (*from_left)--;
  1303.  
  1304.     /* null ? */
  1305.     if (!ch) {
  1306.             if (outbufptr) {*outbufptr++ = L'\0';}
  1307.         (*to_left)--;
  1308.  
  1309.         /* error check */
  1310.             if (gi_len_left) {
  1311.             unconv_num += (gi_len - gi_len_left);
  1312.         gi_len_left = 0;
  1313.             }
  1314.  
  1315.         continue;
  1316.     }
  1317.  
  1318.     /* same glyph_index data */
  1319.         if (gi_len_left)
  1320.         goto output_one_wc;
  1321.  
  1322.         /* control sequence ? */
  1323.         if (ch == CSI) {
  1324.             if ( !ct_parse_csi(inbufptr - 1, &ctr_seq_len) )
  1325.             goto skip_the_seg;
  1326.  
  1327.         if (*from_left < ctr_seq_len) {
  1328.         inbufptr--;
  1329.         (*from_left)++;
  1330.         unconv_num += *from_left;
  1331.         break;
  1332.         }
  1333.  
  1334.             /* skip the control sequence */
  1335.         inbufptr += (ctr_seq_len - 1);
  1336.         *from_left -= (ctr_seq_len - 1);
  1337.  
  1338.             continue;
  1339.     }
  1340.  
  1341.         /* escape sequence ? */
  1342.         if (ch == ESC) {
  1343.         if ( !ct_parse_charset(lcd, 
  1344.             inbufptr - 1, &state->charset, &ctr_seq_len) )
  1345.         goto skip_the_seg;
  1346.  
  1347.         if (state->charset->side == XlcC0 || 
  1348.         state->charset->side == XlcGL)
  1349.           {
  1350.         state->GL_charset = state->charset;
  1351.           }
  1352.         else if (state->charset->side == XlcC1 || 
  1353.              state->charset->side == XlcGR)
  1354.           {
  1355.         state->GR_charset = state->charset;
  1356.           }    
  1357.  
  1358.         if (*from_left < ctr_seq_len) {
  1359.         inbufptr--;
  1360.         (*from_left)++;
  1361.         unconv_num += *from_left;
  1362.         break;
  1363.         }
  1364.  
  1365.             /* skip the escape sequence */
  1366.         inbufptr += (ctr_seq_len - 1);
  1367.         *from_left -= (ctr_seq_len - 1);
  1368.  
  1369.             continue;
  1370.         } 
  1371.  
  1372.      /* check current state */
  1373.     if (isleftside(ch))
  1374.       state->charset = state->GL_charset;
  1375.     else
  1376.       state->charset = state->GR_charset;
  1377.  
  1378.     gi_len = gi_len_left = state->charset->char_size;
  1379.     glyph_index = 0;
  1380.  
  1381. output_one_wc:
  1382.         if (state->charset->side == XlcC1 || state->charset->side == XlcGR)
  1383.             glyph_index = (glyph_index << 8) | (ch & GL);
  1384.         else
  1385.             glyph_index = (glyph_index << 8) | ch;
  1386.  
  1387.     gi_len_left--;
  1388.  
  1389.         /* last of one glyph_index data */
  1390.         if (!gi_len_left) {
  1391.  
  1392.         /* segment conversion */
  1393.         charset_tmp = state->charset;
  1394.         if ( !segment_conversion(lcd, &charset_tmp, &glyph_index) ) {
  1395.         unconv_num += gi_len;
  1396.         continue;
  1397.             }
  1398.  
  1399.             /* get codeset */
  1400.             if ( !_XlcGetCodeSetFromCharSet(lcd, charset_tmp, 
  1401.                         &codeset, &glyph_index) ) {
  1402.         unconv_num += gi_len;
  1403.         continue;
  1404.             }
  1405.  
  1406.             /* convert glyph index to wicd char */
  1407.             gi_to_wc(lcd, glyph_index, codeset, &wc);
  1408.             if (outbufptr) {*outbufptr++ = wc;}
  1409.         (*to_left)--;
  1410.         }
  1411.  
  1412.         continue;
  1413.  
  1414. skip_the_seg:
  1415.     /* skip until next escape or control sequence */
  1416.         while ( *from_left ) {
  1417.         ch = *inbufptr++;
  1418.         (*from_left)--;
  1419.         unconv_num++;
  1420.  
  1421.             if (ch == ESC || ch == CSI) {
  1422.         inbufptr--;
  1423.         (*from_left)++;
  1424.         unconv_num--;
  1425.         break;
  1426.         }
  1427.         }
  1428.  
  1429.         if ( !(*from_left) )
  1430.         break;
  1431.  
  1432.     } /* end of while */
  1433.  
  1434.     /* error check on last char */
  1435.     if (gi_len_left) {
  1436.     inbufptr -= (gi_len - gi_len_left);
  1437.     (*from_left) += (gi_len - gi_len_left);
  1438.     unconv_num += (gi_len - gi_len_left);
  1439.     }
  1440.  
  1441.     *from = *from + from_size;
  1442.     *from_left = 0;
  1443.     *to = (XPointer)outbufptr;
  1444.  
  1445.     DBUG_RETURN(unconv_num);
  1446. }
  1447.  
  1448. static int
  1449. stdc_ctstowcs(conv, from, from_left, to, to_left, args, num_args)
  1450.     XlcConv conv;
  1451.     XPointer *from;
  1452.     int *from_left;
  1453.     XPointer *to;
  1454.     int *to_left;
  1455.     XPointer *args;
  1456.     int num_args;
  1457. {
  1458.     DBUG_ENTER("stdc_ctstowcs")
  1459.     XPointer buf = Xmalloc((*from_left) * MB_CUR_MAX);
  1460.     XPointer buf_ptr1 = buf;
  1461.     int buf_left1 = (*from_left) * MB_CUR_MAX;
  1462.     XPointer buf_ptr2 = buf_ptr1;
  1463.     int buf_left2;
  1464.     int unconv_num1 = 0, unconv_num2 = 0;
  1465.  
  1466.     unconv_num1 = ctstombs(conv, 
  1467.         from, from_left, &buf_ptr1, &buf_left1, args, num_args);
  1468.     if (unconv_num1 < 0)
  1469.         goto ret;
  1470.  
  1471.     buf_left2 = buf_ptr1 - buf_ptr2;
  1472.  
  1473.     unconv_num2 = stdc_mbstowcs(conv, 
  1474.         &buf_ptr2, &buf_left2, to, to_left, args, num_args);
  1475.     if (unconv_num2 < 0)
  1476.         goto ret;
  1477.  
  1478. ret:
  1479.     if (buf)
  1480.     Xfree((char *)buf);
  1481.  
  1482.     DBUG_RETURN(unconv_num1 + unconv_num2);
  1483. }
  1484.  
  1485. static int
  1486. mbstocts(conv, from, from_left, to, to_left, args, num_args)
  1487.     XlcConv conv;
  1488.     XPointer *from;
  1489.     int *from_left;
  1490.     XPointer *to;
  1491.     int *to_left;
  1492.     XPointer *args;
  1493.     int num_args;
  1494. {
  1495.     DBUG_ENTER("mbstocts")
  1496.     XPointer buf = Xmalloc((*from_left) * sizeof(wchar_t));
  1497.     XPointer buf_ptr1 = buf;
  1498.     int buf_left1 = (*from_left);
  1499.     XPointer buf_ptr2 = buf_ptr1;
  1500.     int buf_left2;
  1501.     int unconv_num1 = 0, unconv_num2 = 0;
  1502.  
  1503.     unconv_num1 = mbstowcs_org(conv, 
  1504.         from, from_left, &buf_ptr1, &buf_left1, args, num_args);
  1505.     if (unconv_num1 < 0)
  1506.         goto ret;
  1507.  
  1508.     buf_left2 = (buf_ptr1 - buf_ptr2) / sizeof(wchar_t);
  1509.  
  1510.     unconv_num2 += wcstocts(conv, 
  1511.         &buf_ptr2, &buf_left2, to, to_left, args, num_args);
  1512.     if (unconv_num2 < 0)
  1513.         goto ret;
  1514.  
  1515. ret:
  1516.     if (buf)
  1517.     Xfree((char *)buf);
  1518.  
  1519.     DBUG_RETURN(unconv_num1 + unconv_num2);
  1520. }
  1521.  
  1522. static int
  1523. mbstostr(conv, from, from_left, to, to_left, args, num_args)
  1524.     XlcConv conv;
  1525.     XPointer *from;
  1526.     int *from_left;
  1527.     XPointer *to;
  1528.     int *to_left;
  1529.     XPointer *args;
  1530.     int num_args;
  1531. {
  1532.     DBUG_ENTER("mbstostr")
  1533.     State state = (State) conv->state;
  1534.     XLCd lcd = state->lcd;
  1535.  
  1536.     unsigned char ch;
  1537.     unsigned long mb = 0;
  1538.  
  1539.  
  1540.     int length = 0, len_left = 0;
  1541.     int unconv_num = 0;
  1542.     int num;
  1543.     Bool ss_flag = 0;
  1544.  
  1545.     CodeSet codeset = NULL;
  1546.     ParseInfo parse_info;
  1547.  
  1548.     XPointer inbufptr = *from;
  1549.     XPointer outbufptr = *to;
  1550.     int from_size = *from_left;
  1551.  
  1552.     unsigned char *mb_parse_table = XLC_GENERIC(lcd, mb_parse_table);
  1553.     ParseInfo *mb_parse_list      = XLC_GENERIC(lcd, mb_parse_list);
  1554.  
  1555.  
  1556.     if (*from_left > *to_left)
  1557.         *from_left = *to_left;
  1558.  
  1559.     while (*from_left && *to_left) {
  1560.  
  1561.     ch = *inbufptr++;
  1562.     (*from_left)--;
  1563.  
  1564.     /* null ? */
  1565.     if (!ch) {
  1566.             if (outbufptr) {*outbufptr++ = '\0';}
  1567.         (*to_left)--;
  1568.  
  1569.             /* error check */
  1570.             if (len_left) {
  1571.                 unconv_num += (length - len_left);
  1572.                 len_left = 0;
  1573.             }
  1574.  
  1575.         continue;
  1576.     }
  1577.  
  1578.         /* same mb char data */
  1579.         if (len_left)
  1580.             goto output_one_mb;
  1581.  
  1582.         /* next mb char data for single shift ? */
  1583.     if (mb_parse_table) {
  1584.         if ((num = mb_parse_table[ch]) > 0) {
  1585.         parse_info = mb_parse_list[num - 1];
  1586.  
  1587.                 codeset = parse_info->codeset;
  1588.                 length = len_left = codeset->length;
  1589.                 mb = 0;
  1590.                 ss_flag = 1;
  1591.  
  1592.                 continue;
  1593.         }
  1594.         } 
  1595.     
  1596.     /* next char data : byteM ? */
  1597.     if ((codeset = byteM_parse_codeset(lcd, (inbufptr - 1))))
  1598.         goto next_mb_char;
  1599.  
  1600.     /* next char data : GL or GR side ? */
  1601.     if ((codeset = GLGR_parse_codeset(lcd, ch)))
  1602.         goto next_mb_char;
  1603.         
  1604.         /* can't find codeset for the ch */
  1605.         unconv_num++;
  1606.         continue;
  1607.  
  1608. next_mb_char:
  1609.         length = len_left = codeset->length;
  1610.         mb = 0;
  1611.         ss_flag = 0;
  1612.  
  1613. output_one_mb:
  1614.         mb = (mb << 8) | ch;  /* 1 byte left shift */
  1615.         len_left--;
  1616.  
  1617.         /* last of one mb char data */
  1618.         if (!len_left) {
  1619.             if (check_string_encoding(codeset)) {
  1620.                 if (outbufptr) {*outbufptr++ = mb & 0xff;}
  1621.         (*to_left)--;
  1622.             } else {
  1623.             unconv_num++;
  1624.             }
  1625.         }
  1626.  
  1627.     } /* end of while */
  1628.  
  1629.     /* error check on last char */
  1630.     if (len_left) {
  1631.         inbufptr -= (length - len_left + ss_flag);
  1632.         (*from_left) += (length - len_left + ss_flag);
  1633.         unconv_num += (length - len_left + ss_flag);
  1634.     }
  1635.  
  1636.     *from = *from + from_size;
  1637.     *from_left = 0;
  1638.     *to = (XPointer)outbufptr;
  1639.  
  1640.     DBUG_RETURN(unconv_num);
  1641. }
  1642.  
  1643. static int
  1644. mbtocs(conv, from, from_left, to, to_left, args, num_args)
  1645.     XlcConv conv;
  1646.     XPointer *from;
  1647.     int *from_left;
  1648.     XPointer *to;
  1649.     int *to_left;
  1650.     XPointer *args;
  1651.     int num_args;
  1652. {
  1653.     DBUG_ENTER("mbtocs")
  1654.     State state = (State) conv->state;
  1655.     XLCd lcd = state->lcd;
  1656.  
  1657.     unsigned char ch;
  1658.     unsigned long mb = 0;
  1659.     unsigned long glyph_index;
  1660.  
  1661.     int length = 0, len_left = 0, char_len;
  1662.     int unconv_num = 0;
  1663.     int i, num;
  1664.     XlcSide side;
  1665.  
  1666.     CodeSet codeset = NULL;
  1667.     XlcCharSet charset;
  1668.     ParseInfo parse_info;
  1669.     XPointer p;
  1670.  
  1671.     XPointer inbufptr = *from;
  1672.     XPointer outbufptr = *to;
  1673.     int from_size = *from_left;
  1674.  
  1675.  
  1676.     unsigned char *mb_parse_table = XLC_GENERIC(lcd, mb_parse_table);
  1677.     ParseInfo *mb_parse_list      = XLC_GENERIC(lcd, mb_parse_list);
  1678.  
  1679.  
  1680.     if (*from_left > *to_left)
  1681.         *from_left = *to_left;
  1682.  
  1683.     while (*from_left && *to_left) {
  1684.  
  1685.     ch = *inbufptr++;
  1686.     (*from_left)--;
  1687.  
  1688.     /* null ? */
  1689.     if (!ch) {
  1690.             unconv_num = 1;
  1691.             if (len_left)
  1692.             unconv_num += (length - len_left);
  1693.         break;
  1694.     }
  1695.  
  1696.     /* same mb char data */
  1697.         if (len_left)
  1698.         goto output;
  1699.  
  1700.         /* next mb char data for single shift ? */
  1701.     if (mb_parse_table) {
  1702.         if ((num = mb_parse_table[ch]) > 0) {
  1703.         parse_info = mb_parse_list[num - 1];
  1704.  
  1705.                 codeset = parse_info->codeset;
  1706.         length = len_left = codeset->length;
  1707.         mb = 0;
  1708.  
  1709.         continue;
  1710.         }
  1711.         } 
  1712.     
  1713.     /* next mb char data for byteM ? */
  1714.     if ((codeset = byteM_parse_codeset(lcd, (inbufptr - 1))))
  1715.         goto next_mb_char;
  1716.  
  1717.     /* next mb char data for GL or GR side ? */
  1718.     if ((codeset = GLGR_parse_codeset(lcd, ch)))
  1719.         goto next_mb_char;
  1720.         
  1721.         /* can't find codeset for the ch */
  1722.         unconv_num = 1;
  1723.         break;
  1724.  
  1725. next_mb_char:
  1726.         length = len_left = codeset->length;
  1727.     mb = 0;
  1728.  
  1729. output:
  1730.     mb = (mb << 8) | ch;  /* 1 byte left shift */
  1731.     len_left--;
  1732.  
  1733.         /* last of one mb char data */
  1734.         if (!len_left) {
  1735.             glyph_index = mb_to_gi(mb, codeset);
  1736.             if (!(charset = gi_parse_charset(glyph_index, codeset))) {
  1737.                 unconv_num = length;
  1738.                 break;
  1739.             }
  1740.             char_len = charset->char_size;
  1741.         side = charset->side;
  1742.  
  1743.             /* output glyph index */
  1744.             if (codeset->ctconv)
  1745.                 glyph_index = conv_to_dest(codeset->ctconv, glyph_index);
  1746.             if (*to_left < char_len) {
  1747.                 unconv_num = length;
  1748.                 break;
  1749.             }
  1750.  
  1751.         if (outbufptr) {
  1752.             output_ulong_value(outbufptr, glyph_index, char_len, side);
  1753.             outbufptr += char_len;
  1754.         }
  1755.  
  1756.             (*to_left) -= char_len;
  1757.  
  1758.             break;
  1759.         }
  1760.  
  1761.     } /* end of while */
  1762.  
  1763.     /* error end */
  1764.     if (unconv_num) {
  1765.         *from = *from + from_size;
  1766.         *from_left = 0;
  1767.         *to = outbufptr;
  1768.     DBUG_RETURN(-1);
  1769.     }
  1770.  
  1771.     /* nomal end */
  1772.     *from = inbufptr;
  1773.     *to = outbufptr;
  1774.  
  1775.     if (num_args > 0)
  1776.         *((XlcCharSet *) args[0]) = charset;
  1777.  
  1778.     DBUG_RETURN(0);
  1779. }
  1780.  
  1781. static int
  1782. mbstocs(conv, from, from_left, to, to_left, args, num_args)
  1783.     XlcConv conv;
  1784.     XPointer *from;
  1785.     int *from_left;
  1786.     XPointer *to;
  1787.     int *to_left;
  1788.     XPointer *args;
  1789.     int num_args;
  1790. {
  1791.     DBUG_ENTER("mbstocs")
  1792.     int ret;
  1793.     XlcCharSet charset_old, charset = NULL;
  1794.     XPointer tmp_args[1];
  1795.  
  1796.     XPointer inbufptr;
  1797.     int    in_left;
  1798.     XPointer outbufptr;
  1799.     int    out_left;
  1800.     tmp_args[0] = (XPointer) &charset;
  1801.  
  1802.     ret = mbtocs(conv, from, from_left, to, to_left, tmp_args, 1);
  1803.     charset_old = charset;
  1804.     inbufptr = *from;
  1805.     in_left = *from_left;
  1806.     outbufptr = *to;
  1807.     out_left = *to_left;
  1808.  
  1809.     while ( ret == 0 && *from_left && *to_left && charset_old == charset ) {
  1810.     charset_old = charset;
  1811.     inbufptr = *from;
  1812.     in_left = *from_left;
  1813.     outbufptr = *to;
  1814.     out_left = *to_left;
  1815.         ret = mbtocs(conv, from, from_left, to, to_left, tmp_args, 1);
  1816.     }
  1817.  
  1818.     *from = inbufptr;
  1819.     *from_left = in_left;
  1820.     *to = outbufptr;
  1821.     *to_left = out_left;
  1822.  
  1823.     if (num_args > 0)
  1824.         *((XlcCharSet *) args[0]) = charset_old;
  1825.  
  1826.     /* error end */
  1827.     if (ret != 0)
  1828.     DBUG_RETURN( -1 );
  1829.  
  1830.     DBUG_RETURN(0);
  1831. }
  1832.  
  1833. static int
  1834. wcstostr(conv, from, from_left, to, to_left, args, num_args)
  1835.     XlcConv conv;
  1836.     XPointer *from;
  1837.     int *from_left;
  1838.     XPointer *to;
  1839.     int *to_left;
  1840.     XPointer *args;
  1841.     int num_args;
  1842. {
  1843.     DBUG_ENTER("wcstostr")
  1844.     State state = (State) conv->state;
  1845.     XLCd lcd = state->lcd;
  1846.  
  1847.     char *encoding;
  1848.     unsigned long mb, glyph_index;
  1849.     wchar_t wc;
  1850.  
  1851.     int length;
  1852.     int unconv_num = 0;
  1853.  
  1854.     CodeSet codeset;
  1855.  
  1856.     wchar_t *inbufptr = (wchar_t *) *from;
  1857.     XPointer outbufptr = *to;
  1858.     int from_size = *from_left;
  1859.     
  1860.     char *default_string = XLC_PUBLIC(lcd, default_string);
  1861.     int defstr_len = strlen(default_string);
  1862.  
  1863.  
  1864.     if (*from_left > *to_left)
  1865.         *from_left = *to_left;
  1866.  
  1867.     while (*from_left && *to_left) {
  1868.  
  1869.         wc = *inbufptr++;
  1870.         (*from_left)--;
  1871.  
  1872.         /* null ? */
  1873.         if (!wc) {
  1874.             if (outbufptr) {*outbufptr++ = '\0';}
  1875.             (*to_left)--;
  1876.  
  1877.             continue;
  1878.         }
  1879.  
  1880.         /* convert */
  1881.     if ( !wc_to_gi(lcd, wc, &glyph_index, &codeset) ) {
  1882.  
  1883.         /* output default_string of XDefaultString() */
  1884.             if (*to_left < defstr_len)
  1885.         break;
  1886.         if (outbufptr) {strncpy((char *)outbufptr, default_string, defstr_len);}
  1887.  
  1888.         if (outbufptr) {outbufptr += defstr_len;}
  1889.         (*to_left) -= defstr_len;
  1890.  
  1891.             unconv_num++;
  1892.  
  1893.         } else {
  1894.             mb = gi_to_mb(glyph_index, codeset);
  1895.         if (codeset->parse_info) {
  1896.  
  1897.         /* output shift sequence */
  1898.         encoding = codeset->parse_info->encoding;
  1899.                 length = strlen(encoding);
  1900.                 if (*to_left < length)
  1901.             break;
  1902.             if (check_string_encoding(codeset)) {
  1903.                 if (outbufptr) {strncpy((char *)outbufptr, encoding, length);}
  1904.                 if (outbufptr) {outbufptr += length;}
  1905.                 (*to_left) -= length;
  1906.                 }
  1907.             }
  1908.  
  1909.             /* output characters */
  1910.         length = codeset->length;
  1911.             if (*to_left < length)
  1912.         break;
  1913.             if (check_string_encoding(codeset)) {
  1914.  
  1915.             if (outbufptr) {
  1916.                 output_ulong_value(outbufptr, mb, length, XlcNONE);
  1917.                 outbufptr += length;
  1918.             }
  1919.  
  1920.             (*to_left) -= length;
  1921.             } else {
  1922.         unconv_num++;
  1923.             }
  1924.         }
  1925.  
  1926.     } /* end of while */
  1927.  
  1928.     *from = *from + from_size;
  1929.     *from_left = 0;
  1930.     *to = outbufptr;
  1931.  
  1932.     DBUG_RETURN(unconv_num);
  1933. }
  1934.  
  1935. static int
  1936. stdc_wcstostr(conv, from, from_left, to, to_left, args, num_args)
  1937.     XlcConv conv;
  1938.     XPointer *from;
  1939.     int *from_left;
  1940.     XPointer *to;
  1941.     int *to_left;
  1942.     XPointer *args;
  1943.     int num_args;
  1944. {
  1945.     DBUG_ENTER("stdc_wcstostr")
  1946.     XPointer buf = Xmalloc((*from_left) * MB_CUR_MAX);
  1947.     XPointer buf_ptr1 = buf;
  1948.     int buf_left1 = (*from_left) * MB_CUR_MAX;
  1949.     XPointer buf_ptr2 = buf_ptr1;
  1950.     int buf_left2;
  1951.     int unconv_num1 = 0, unconv_num2 = 0;
  1952.  
  1953.     unconv_num1 = stdc_wcstombs(conv, 
  1954.         from, from_left, &buf_ptr1, &buf_left1, args, num_args);
  1955.     if (unconv_num1 < 0)
  1956.         goto ret;
  1957.  
  1958.     buf_left2 = buf_ptr1 - buf_ptr2;
  1959.  
  1960.     unconv_num2 = mbstostr(conv, 
  1961.         &buf_ptr2, &buf_left2, to, to_left, args, num_args);
  1962.     if (unconv_num2 < 0)
  1963.         goto ret;
  1964.  
  1965. ret:
  1966.     if (buf)
  1967.     Xfree((char *)buf);
  1968.  
  1969.     DBUG_RETURN(unconv_num1 + unconv_num2);
  1970. }
  1971.  
  1972. static int
  1973. wctocs(conv, from, from_left, to, to_left, args, num_args)
  1974.     XlcConv conv;
  1975.     XPointer *from;
  1976.     int *from_left;
  1977.     XPointer *to;
  1978.     int *to_left;
  1979.     XPointer *args;
  1980.     int num_args;
  1981. {
  1982.     DBUG_ENTER("wctocs")
  1983.     State state = (State) conv->state;
  1984.     XLCd lcd = state->lcd;
  1985.  
  1986.     wchar_t wc;
  1987.     unsigned long glyph_index;
  1988.  
  1989.     int i, char_len;
  1990.     int unconv_num = 0;
  1991.     XlcSide side;
  1992.  
  1993.     CodeSet codeset;
  1994.     XlcCharSet charset;
  1995.     XPointer p;
  1996.  
  1997.     wchar_t *inbufptr = (wchar_t *) *from;
  1998.     XPointer outbufptr = *to;
  1999.     int from_size = *from_left;
  2000.  
  2001.  
  2002.  
  2003.     if (*from_left > *to_left)
  2004.         *from_left = *to_left;
  2005.  
  2006.     if (*from_left && *to_left) {
  2007.  
  2008.         wc = *inbufptr++;
  2009.         (*from_left)--;
  2010.  
  2011.         /* null ? */
  2012.         if (!wc) {
  2013.             unconv_num = 1;
  2014.             goto end;
  2015.         }
  2016.  
  2017.         /* convert */
  2018.     if ( !wc_to_gi(lcd, wc, &glyph_index, &codeset) ) {
  2019.             unconv_num = 1;
  2020.         goto end;
  2021.         }
  2022.  
  2023.         if ( !(charset = gi_parse_charset(glyph_index, codeset)) ) {
  2024.             unconv_num = 1;
  2025.         goto end;
  2026.         }
  2027.     char_len = charset->char_size;
  2028.     side = charset->side;
  2029.  
  2030.         /* output glyph index */
  2031.     if (codeset->ctconv)
  2032.             glyph_index = conv_to_dest(codeset->ctconv, glyph_index);
  2033.         if (*to_left < char_len) {
  2034.             unconv_num++;
  2035.         goto end;
  2036.         }
  2037.  
  2038.         if (outbufptr) {
  2039.             output_ulong_value(outbufptr, glyph_index, char_len, side);
  2040.             outbufptr += char_len;
  2041.         }
  2042.  
  2043.     (*to_left) -= char_len;
  2044.  
  2045.     }
  2046.  
  2047. end:
  2048.  
  2049.      /* error end */
  2050.     if (unconv_num) {
  2051.         *from = *from + from_size;
  2052.         *from_left = 0;
  2053.         *to = outbufptr;
  2054.         DBUG_RETURN(-1);
  2055.     }
  2056.  
  2057.     /* nomal end */
  2058.     *from = (XPointer)inbufptr;
  2059.     *to = outbufptr;
  2060.  
  2061.     if (num_args > 0)
  2062.         *((XlcCharSet *) args[0]) = charset;
  2063.  
  2064.     DBUG_RETURN(0);
  2065. }
  2066.  
  2067. static int
  2068. stdc_wctocs(conv, from, from_left, to, to_left, args, num_args)
  2069.     XlcConv conv;
  2070.     XPointer *from;
  2071.     int *from_left;
  2072.     XPointer *to;
  2073.     int *to_left;
  2074.     XPointer *args;
  2075.     int num_args;
  2076. {
  2077.     DBUG_ENTER("stdc_wctocs")
  2078.     wchar_t wch, *src = *((wchar_t **) from);
  2079.     XPointer tmp_from, save_from = *from;
  2080.     char tmp[32];
  2081.     int length, ret, src_left = *from_left;
  2082.     int from_size = *from_left;
  2083.  
  2084.     if (src_left > 0 && *to_left > 0) {
  2085.     if ((wch = *src)) {
  2086.         length = wctomb(tmp, wch);
  2087.     } else {
  2088.         goto end;
  2089.     }
  2090.         
  2091.     if (length < 0)
  2092.         goto end;
  2093.  
  2094.     tmp_from = (XPointer) tmp;
  2095.     ret = mbtocs(conv, &tmp_from, &length, to, to_left, args, num_args);
  2096.     if (ret < 0)
  2097.         goto end;
  2098.  
  2099.     src++;
  2100.     src_left--;
  2101.     }
  2102.  
  2103. end:
  2104.      /* error end */
  2105.     if (save_from == (XPointer) src) {
  2106.         *from = *from + from_size;
  2107.         *from_left = 0;
  2108.     DBUG_RETURN(-1);
  2109.     }
  2110.  
  2111.     /* nomal end */
  2112.     *from = (XPointer) src;
  2113.     *from_left = src_left;
  2114.  
  2115.     DBUG_RETURN(0);
  2116. }
  2117.  
  2118. static int
  2119. wcstocs(conv, from, from_left, to, to_left, args, num_args)
  2120.     XlcConv conv;
  2121.     XPointer *from;
  2122.     int *from_left;
  2123.     XPointer *to;
  2124.     int *to_left;
  2125.     XPointer *args;
  2126.     int num_args;
  2127. {
  2128.     DBUG_ENTER("wcstocs")
  2129.     int ret;
  2130.     XlcCharSet charset_old, charset = NULL;
  2131.     XPointer tmp_args[1];
  2132.  
  2133.     wchar_t *inbufptr;
  2134.     int    in_left;
  2135.     XPointer outbufptr;
  2136.     int    out_left;
  2137.     tmp_args[0] = (XPointer) &charset;
  2138.  
  2139.     ret = wctocs(conv, from, from_left, to, to_left, tmp_args, 1);
  2140.     charset_old = charset;
  2141.     inbufptr = (wchar_t *)(*from);
  2142.     in_left = *from_left;
  2143.     outbufptr = *to;
  2144.     out_left = *to_left;
  2145.  
  2146.     while ( ret == 0 && *from_left && *to_left && charset_old == charset ) {
  2147.     charset_old = charset;
  2148.     inbufptr = (wchar_t *)(*from);
  2149.     in_left = *from_left;
  2150.     outbufptr = *to;
  2151.     out_left = *to_left;
  2152.         ret = wctocs(conv, from, from_left, to, to_left, tmp_args, 1);
  2153.     }
  2154.  
  2155.     *from = (XPointer)inbufptr;
  2156.     *from_left = in_left;
  2157.     *to = outbufptr;
  2158.     *to_left = out_left;
  2159.  
  2160.     if (num_args > 0)
  2161.         *((XlcCharSet *) args[0]) = charset_old;
  2162.  
  2163.     /* error end */
  2164.     if (ret != 0)
  2165.     DBUG_RETURN( -1 );
  2166.  
  2167.     DBUG_RETURN(0);
  2168. }
  2169.  
  2170. static int
  2171. stdc_wcstocs(conv, from, from_left, to, to_left, args, num_args)
  2172.     XlcConv conv;
  2173.     XPointer *from;
  2174.     int *from_left;
  2175.     XPointer *to;
  2176.     int *to_left;
  2177.     XPointer *args;
  2178.     int num_args;
  2179. {
  2180.     DBUG_ENTER("stdc_wcstocs")
  2181.     int ret;
  2182.     XlcCharSet charset_old, charset = NULL;
  2183.     XPointer tmp_args[1];
  2184.  
  2185.     wchar_t *inbufptr;
  2186.     int    in_left;
  2187.     XPointer outbufptr;
  2188.     int    out_left;
  2189.     tmp_args[0] = (XPointer) &charset;
  2190.  
  2191.     ret = stdc_wctocs(conv, from, from_left, to, to_left, tmp_args, 1);
  2192.     charset_old = charset;
  2193.     inbufptr = (wchar_t *)(*from);
  2194.     in_left = *from_left;
  2195.     outbufptr = *to;
  2196.     out_left = *to_left;
  2197.  
  2198.     while ( ret == 0 && *from_left && *to_left && charset_old == charset ) {
  2199.     charset_old = charset;
  2200.     inbufptr = (wchar_t *)(*from);
  2201.     in_left = *from_left;
  2202.     outbufptr = *to;
  2203.     out_left = *to_left;
  2204.         ret = stdc_wctocs(conv, from, from_left, to, to_left, tmp_args, 1);
  2205.     }
  2206.  
  2207.     *from = (XPointer)inbufptr;
  2208.     *from_left = in_left;
  2209.     *to = outbufptr;
  2210.     *to_left = out_left;
  2211.  
  2212.     if (num_args > 0)
  2213.         *((XlcCharSet *) args[0]) = charset_old;
  2214.  
  2215.     /* error end */
  2216.     if (ret != 0)
  2217.     DBUG_RETURN( -1 );
  2218.  
  2219.     DBUG_RETURN(0);
  2220. }
  2221.  
  2222. static int
  2223. ctstombs(conv, from, from_left, to, to_left, args, num_args)
  2224.     XlcConv conv;
  2225.     XPointer *from;
  2226.     int *from_left;
  2227.     XPointer *to;
  2228.     int *to_left;
  2229.     XPointer *args;
  2230.     int num_args;
  2231. {
  2232.     DBUG_ENTER("ctstombs")
  2233.     XPointer buf = Xmalloc((*from_left) * sizeof(wchar_t));
  2234.     XPointer buf_ptr1 = buf;
  2235.     int buf_left1 = (*from_left);
  2236.     XPointer buf_ptr2 = buf_ptr1;
  2237.     int buf_left2;
  2238.     int unconv_num1 = 0, unconv_num2 = 0;
  2239.  
  2240.     unconv_num1 = ctstowcs(conv, 
  2241.         from, from_left, &buf_ptr1, &buf_left1, args, num_args);
  2242.     if (unconv_num1 < 0)
  2243.         goto ret;
  2244.  
  2245.     buf_left2 = (buf_ptr1 - buf_ptr2) / sizeof(wchar_t);
  2246.  
  2247.     unconv_num2 += wcstombs_org(conv, 
  2248.         &buf_ptr2, &buf_left2, to, to_left, args, num_args);
  2249.     if (unconv_num2 < 0)
  2250.         goto ret;
  2251.  
  2252. ret:
  2253.     if (buf)
  2254.     Xfree((char *)buf);
  2255.  
  2256.     DBUG_RETURN(unconv_num1 + unconv_num2);
  2257. }
  2258.  
  2259. static int
  2260. strtombs(conv, from, from_left, to, to_left, args, num_args)
  2261.     XlcConv conv;
  2262.     XPointer *from;
  2263.     int *from_left;
  2264.     XPointer *to;
  2265.     int *to_left;
  2266.     XPointer *args;
  2267.     int num_args;
  2268. {
  2269.     DBUG_ENTER("strtombs")
  2270.     State state = (State) conv->state;
  2271.     XLCd lcd = state->lcd;
  2272.  
  2273.     char *encoding;
  2274.     unsigned long mb, glyph_index;
  2275.     unsigned char ch;
  2276.  
  2277.     int length;
  2278.     int unconv_num = 0;
  2279.  
  2280.     CodeSet codeset;
  2281.  
  2282.     XPointer inbufptr = *from;
  2283.     XPointer outbufptr = *to;
  2284.     int from_size = *from_left;
  2285.  
  2286.     if (*from_left > *to_left)
  2287.         *from_left = *to_left;
  2288.  
  2289.     while (*from_left && *to_left) {
  2290.  
  2291.         ch = *inbufptr++;
  2292.         (*from_left)--;
  2293.  
  2294.         /* null ? */
  2295.         if (!ch) {
  2296.             if (outbufptr) {*outbufptr++ = '\0';}
  2297.             (*to_left)--;
  2298.  
  2299.             continue;
  2300.         }
  2301.  
  2302.         /* convert */
  2303.         if (isleftside(ch)) {
  2304.         glyph_index = ch;
  2305.         codeset = _XlcGetCodeSetFromName(lcd, "ISO8859-1:GL");
  2306.     } else {
  2307.         glyph_index = ch & GL;
  2308.         codeset = _XlcGetCodeSetFromName(lcd, "ISO8859-1:GR");
  2309.     }
  2310.  
  2311.         if (!codeset) {
  2312.         unconv_num++;
  2313.         continue;
  2314.         }
  2315.  
  2316.         mb = gi_to_mb(glyph_index, codeset);
  2317.     if (codeset->parse_info) {
  2318.  
  2319.         /* output shift sequence */
  2320.         encoding = codeset->parse_info->encoding;
  2321.             length = strlen(encoding);
  2322.             if (*to_left < length)
  2323.         break;
  2324.         if (outbufptr) {strncpy((char *)outbufptr, encoding, length);}
  2325.  
  2326.         if (outbufptr) {outbufptr += length;}
  2327.         (*to_left) -= length;
  2328.         }
  2329.  
  2330.         /* output characters */
  2331.     length = codeset->length;
  2332.         if (*to_left < length)
  2333.         break;
  2334.  
  2335.         if (outbufptr) {
  2336.             output_ulong_value(outbufptr, mb, length, XlcNONE);
  2337.             outbufptr += length;
  2338.         }
  2339.  
  2340.     (*to_left) -= length;
  2341.  
  2342.     } /* end of while */
  2343.  
  2344.     *from = *from + from_size;
  2345.     *from_left = 0;
  2346.     *to = outbufptr;
  2347.  
  2348.     DBUG_RETURN(unconv_num);
  2349. }
  2350.  
  2351. static int
  2352. strtowcs(conv, from, from_left, to, to_left, args, num_args)
  2353.     XlcConv conv;
  2354.     XPointer *from;
  2355.     int *from_left;
  2356.     XPointer *to;
  2357.     int *to_left;
  2358.     XPointer *args;
  2359.     int num_args;
  2360. {
  2361.     DBUG_ENTER("strtowcs")
  2362.     State state = (State) conv->state;
  2363.     XLCd lcd = state->lcd;
  2364.  
  2365.     unsigned char ch;
  2366.     unsigned long glyph_index;
  2367.     wchar_t wc;
  2368.  
  2369.     int unconv_num = 0;
  2370.     CodeSet codeset;
  2371.  
  2372.     XPointer inbufptr = *from;
  2373.     wchar_t *outbufptr = (wchar_t *)*to;
  2374.     int from_size = *from_left;
  2375.  
  2376.     if (*from_left > *to_left)
  2377.         *from_left = *to_left;
  2378.  
  2379.     while (*from_left && *to_left) {
  2380.  
  2381.         ch = *inbufptr++;
  2382.         (*from_left)--;
  2383.  
  2384.         /* null ? */
  2385.         if (!ch) {
  2386.             if (outbufptr) {*outbufptr++ = L'\0';}
  2387.             (*to_left)--;
  2388.  
  2389.             continue;
  2390.         }
  2391.  
  2392.         /* convert */
  2393.         if (isleftside(ch)) {
  2394.         glyph_index = ch;
  2395.         codeset = _XlcGetCodeSetFromName(lcd, "ISO8859-1:GL");
  2396.     } else {
  2397.         glyph_index = ch & GL;
  2398.         codeset = _XlcGetCodeSetFromName(lcd, "ISO8859-1:GR");
  2399.     }
  2400.  
  2401.         if (!codeset) {
  2402.         unconv_num++;
  2403.         continue;
  2404.         }
  2405.  
  2406.         gi_to_wc(lcd, glyph_index, codeset, &wc);
  2407.     if (outbufptr) {*outbufptr++ = wc;}
  2408.     (*to_left)--;
  2409.  
  2410.     } /* end of while */
  2411.  
  2412.     *from = *from + from_size;
  2413.     *from_left = 0;
  2414.     *to = (XPointer)outbufptr;
  2415.  
  2416.     DBUG_RETURN(unconv_num);
  2417. }
  2418.  
  2419. static int
  2420. stdc_strtowcs(conv, from, from_left, to, to_left, args, num_args)
  2421.     XlcConv conv;
  2422.     XPointer *from;
  2423.     int *from_left;
  2424.     XPointer *to;
  2425.     int *to_left;
  2426.     XPointer *args;
  2427.     int num_args;
  2428. {
  2429.     DBUG_ENTER("stdc_strtowcs")
  2430.     XPointer buf = Xmalloc((*from_left) * MB_CUR_MAX);
  2431.     XPointer buf_ptr1 = buf;
  2432.     int buf_left1 = (*from_left) * MB_CUR_MAX;
  2433.     XPointer buf_ptr2 = buf_ptr1;
  2434.     int buf_left2;
  2435.     int unconv_num1 = 0, unconv_num2 = 0;
  2436.  
  2437.     unconv_num1 = strtombs(conv, 
  2438.         from, from_left, &buf_ptr1, &buf_left1, args, num_args);
  2439.     if (unconv_num1 < 0)
  2440.         goto ret;
  2441.  
  2442.     buf_left2 = buf_ptr1 - buf_ptr2;
  2443.  
  2444.     unconv_num2 = stdc_mbstowcs(conv, 
  2445.         &buf_ptr2, &buf_left2, to, to_left, args, num_args);
  2446.     if (unconv_num2 < 0)
  2447.         goto ret;
  2448.  
  2449. ret:
  2450.     if (buf)
  2451.     Xfree((char *)buf);
  2452.  
  2453.     DBUG_RETURN(unconv_num1 + unconv_num2);
  2454. }
  2455.  
  2456. /* -------------------------------------------------------------------------- */
  2457. /*                Close                                         */
  2458. /* -------------------------------------------------------------------------- */
  2459.  
  2460. static void
  2461. close_converter(conv)
  2462.     XlcConv conv;
  2463. {
  2464.     DBUG_ENTER("close_converter")
  2465.     if (conv->state) {
  2466.     Xfree((char *) conv->state);
  2467.     }
  2468.  
  2469.     if (conv->methods) {
  2470.     Xfree((char *) conv->methods);
  2471.     }
  2472.  
  2473.     Xfree((char *) conv);
  2474.     DBUG_VOID_RETURN;
  2475. }
  2476.  
  2477. /* -------------------------------------------------------------------------- */
  2478. /*                Open                                          */
  2479. /* -------------------------------------------------------------------------- */
  2480.  
  2481. static XlcConv
  2482. create_conv(lcd, methods)
  2483.     XLCd lcd;
  2484.     XlcConvMethods methods;
  2485. {
  2486.     DBUG_ENTER("create_conv")
  2487.     XlcConv conv;
  2488.     State state;
  2489.  
  2490.     conv = (XlcConv) Xmalloc(sizeof(XlcConvRec));
  2491.     if (conv == NULL)
  2492.     DBUG_RETURN((XlcConv) NULL);
  2493.     
  2494.     conv->methods = (XlcConvMethods) Xmalloc(sizeof(XlcConvMethodsRec));
  2495.     if (conv->methods == NULL)
  2496.     goto err;
  2497.     *conv->methods = *methods;
  2498.     if (XLC_PUBLIC(lcd, is_state_depend))
  2499.     conv->methods->reset = init_state;
  2500.  
  2501.     conv->state = (XPointer) Xmalloc(sizeof(StateRec));
  2502.     if (conv->state == NULL)
  2503.     goto err;
  2504.     bzero((char *) conv->state, sizeof(StateRec));
  2505.     
  2506.     state = (State) conv->state;
  2507.     state->lcd = lcd;
  2508.     init_state(conv);
  2509.     
  2510.     DBUG_RETURN(conv);
  2511.  
  2512. err:
  2513.     close_converter(conv);
  2514.  
  2515.     DBUG_RETURN((XlcConv) NULL);
  2516. }
  2517.  
  2518. static XlcConvMethodsRec mbstocts_methods = {
  2519.     close_converter,
  2520.     mbstocts,
  2521.     NULL
  2522. } ;
  2523.  
  2524. static XlcConv
  2525. open_mbstocts(from_lcd, from_type, to_lcd, to_type)
  2526.     XLCd from_lcd;
  2527.     char *from_type;
  2528.     XLCd to_lcd;
  2529.     char *to_type;
  2530. {
  2531.     DBUG_ENTER("open_mbstocts")
  2532.     XlcConv result = create_conv(from_lcd, &mbstocts_methods);
  2533.     DBUG_RETURN(result);
  2534. }
  2535.  
  2536. static XlcConvMethodsRec mbstostr_methods = {
  2537.     close_converter,
  2538.     mbstostr,
  2539.     NULL
  2540. } ;
  2541.  
  2542. static XlcConv
  2543. open_mbstostr(from_lcd, from_type, to_lcd, to_type)
  2544.     XLCd from_lcd;
  2545.     char *from_type;
  2546.     XLCd to_lcd;
  2547.     char *to_type;
  2548. {
  2549.     DBUG_ENTER("open_mbstostr")
  2550.     XlcConv result = create_conv(from_lcd, &mbstostr_methods);
  2551.     DBUG_RETURN(result);
  2552. }
  2553.  
  2554. static XlcConvMethodsRec mbstocs_methods = {
  2555.     close_converter,
  2556.     mbstocs,
  2557.     NULL
  2558. } ;
  2559.  
  2560. static XlcConv
  2561. open_mbstocs(from_lcd, from_type, to_lcd, to_type)
  2562.     XLCd from_lcd;
  2563.     char *from_type;
  2564.     XLCd to_lcd;
  2565.     char *to_type;
  2566. {
  2567.     DBUG_ENTER("open_mbstocs")
  2568.     XlcConv result = create_conv(from_lcd, &mbstocs_methods);
  2569.     DBUG_RETURN(result);
  2570. }
  2571.  
  2572. static XlcConvMethodsRec mbtocs_methods = {
  2573.     close_converter,
  2574.     mbtocs,
  2575.     NULL
  2576. } ;
  2577.  
  2578. static XlcConv
  2579. open_mbtocs(from_lcd, from_type, to_lcd, to_type)
  2580.     XLCd from_lcd;
  2581.     char *from_type;
  2582.     XLCd to_lcd;
  2583.     char *to_type;
  2584. {
  2585.     DBUG_ENTER("open_mbtocs")
  2586.     XlcConv result = create_conv(from_lcd, &mbtocs_methods);
  2587.     DBUG_RETURN(result);
  2588. }
  2589.  
  2590. static XlcConvMethodsRec ctstombs_methods = {
  2591.     close_converter,
  2592.     ctstombs,
  2593.     NULL
  2594. } ;
  2595.  
  2596. static XlcConv
  2597. open_ctstombs(from_lcd, from_type, to_lcd, to_type)
  2598.     XLCd from_lcd;
  2599.     char *from_type;
  2600.     XLCd to_lcd;
  2601.     char *to_type;
  2602. {
  2603.     DBUG_ENTER("open_ctstombs")
  2604.     XlcConv result = create_conv(from_lcd, &ctstombs_methods);
  2605.     DBUG_RETURN(result);
  2606. }
  2607.  
  2608. static XlcConvMethodsRec strtombs_methods = {
  2609.     close_converter,
  2610.     strtombs,
  2611.     NULL
  2612. } ;
  2613.  
  2614. static XlcConv
  2615. open_strtombs(from_lcd, from_type, to_lcd, to_type)
  2616.     XLCd from_lcd;
  2617.     char *from_type;
  2618.     XLCd to_lcd;
  2619.     char *to_type;
  2620. {
  2621.     DBUG_ENTER("open_strtombs")
  2622.     XlcConv result = create_conv(from_lcd, &strtombs_methods);
  2623.     DBUG_RETURN(result);
  2624. }
  2625.  
  2626. #ifdef STDCVT
  2627.  
  2628. static XlcConvMethodsRec stdc_mbstowcs_methods = {
  2629.     close_converter,
  2630.     stdc_mbstowcs,
  2631.     NULL
  2632. } ;
  2633.  
  2634. static XlcConv
  2635. open_stdc_mbstowcs(from_lcd, from_type, to_lcd, to_type)
  2636.     XLCd from_lcd;
  2637.     char *from_type;
  2638.     XLCd to_lcd;
  2639.     char *to_type;
  2640. {
  2641.     DBUG_ENTER("open_stdc_mbstowcs")
  2642.     XlcConv result = create_conv(from_lcd, &stdc_mbstowcs_methods);
  2643.     DBUG_RETURN(result);
  2644. }
  2645.  
  2646. static XlcConvMethodsRec stdc_wcstombs_methods = {
  2647.     close_converter,
  2648.     stdc_wcstombs,
  2649.     NULL
  2650. } ;
  2651.  
  2652. static XlcConv
  2653. open_stdc_wcstombs(from_lcd, from_type, to_lcd, to_type)
  2654.     XLCd from_lcd;
  2655.     char *from_type;
  2656.     XLCd to_lcd;
  2657.     char *to_type;
  2658. {
  2659.     DBUG_ENTER("open_stdc_wcstombs")
  2660.     XlcConv result = create_conv(from_lcd, &stdc_wcstombs_methods);
  2661.     DBUG_RETURN(result);
  2662. }
  2663.  
  2664. static XlcConvMethodsRec stdc_wcstocts_methods = {
  2665.     close_converter,
  2666.     stdc_wcstocts,
  2667.     NULL
  2668. } ;
  2669.  
  2670. static XlcConv
  2671. open_stdc_wcstocts(from_lcd, from_type, to_lcd, to_type)
  2672.     XLCd from_lcd;
  2673.     char *from_type;
  2674.     XLCd to_lcd;
  2675.     char *to_type;
  2676. {
  2677.     DBUG_ENTER("open_stdc_wcstocts")
  2678.     XlcConv result = create_conv(from_lcd, &stdc_wcstocts_methods);
  2679.     DBUG_RETURN(result);
  2680. }
  2681.  
  2682. static XlcConvMethodsRec stdc_wcstostr_methods = {
  2683.     close_converter,
  2684.     stdc_wcstostr,
  2685.     NULL
  2686. } ;
  2687.  
  2688. static XlcConv
  2689. open_stdc_wcstostr(from_lcd, from_type, to_lcd, to_type)
  2690.     XLCd from_lcd;
  2691.     char *from_type;
  2692.     XLCd to_lcd;
  2693.     char *to_type;
  2694. {
  2695.     DBUG_ENTER("open_stdc_wcstostr")
  2696.     XlcConv result = create_conv(from_lcd, &stdc_wcstostr_methods);
  2697.     DBUG_RETURN(result);
  2698. }
  2699.  
  2700. static XlcConvMethodsRec stdc_wcstocs_methods = {
  2701.     close_converter,
  2702.     stdc_wcstocs,
  2703.     NULL
  2704. } ;
  2705.  
  2706. static XlcConv
  2707. open_stdc_wcstocs(from_lcd, from_type, to_lcd, to_type)
  2708.     XLCd from_lcd;
  2709.     char *from_type;
  2710.     XLCd to_lcd;
  2711.     char *to_type;
  2712. {
  2713.     DBUG_ENTER("open_stdc_wcstocs")
  2714.     XlcConv result = create_conv(from_lcd, &stdc_wcstocs_methods);
  2715.     DBUG_RETURN(result);
  2716. }
  2717.  
  2718. static XlcConvMethodsRec stdc_wctocs_methods = {
  2719.     close_converter,
  2720.     stdc_wctocs,
  2721.     NULL
  2722. } ;
  2723.  
  2724. static XlcConv
  2725. open_stdc_wctocs(from_lcd, from_type, to_lcd, to_type)
  2726.     XLCd from_lcd;
  2727.     char *from_type;
  2728.     XLCd to_lcd;
  2729.     char *to_type;
  2730. {
  2731.     DBUG_ENTER("open_stdc_wctocs")
  2732.     XlcConv result = create_conv(from_lcd, &stdc_wctocs_methods);
  2733.     DBUG_RETURN(result);
  2734. }
  2735.  
  2736. static XlcConvMethodsRec stdc_ctstowcs_methods = {
  2737.     close_converter,
  2738.     stdc_ctstowcs,
  2739.     NULL
  2740. } ;
  2741.  
  2742. static XlcConv
  2743. open_stdc_ctstowcs(from_lcd, from_type, to_lcd, to_type)
  2744.     XLCd from_lcd;
  2745.     char *from_type;
  2746.     XLCd to_lcd;
  2747.     char *to_type;
  2748. {
  2749.     DBUG_ENTER("open_stdc_ctstowcs")
  2750.     XlcConv result = create_conv(from_lcd, &stdc_ctstowcs_methods);
  2751.     DBUG_RETURN(result);
  2752. }
  2753.  
  2754. static XlcConvMethodsRec stdc_strtowcs_methods = {
  2755.     close_converter,
  2756.     stdc_strtowcs,
  2757.     NULL
  2758. } ;
  2759.  
  2760. static XlcConv
  2761. open_stdc_strtowcs(from_lcd, from_type, to_lcd, to_type)
  2762.     XLCd from_lcd;
  2763.     char *from_type;
  2764.     XLCd to_lcd;
  2765.     char *to_type;
  2766. {
  2767.     DBUG_ENTER("open_stdc_strtowcs")
  2768.     XlcConv result = create_conv(from_lcd, &stdc_strtowcs_methods);
  2769.     DBUG_RETURN(result);
  2770. }
  2771. #endif /* STDCVT */
  2772. static XlcConvMethodsRec mbstowcs_methods = {
  2773.     close_converter,
  2774.     mbstowcs_org,
  2775.     NULL
  2776. } ;
  2777. static XlcConv
  2778. open_mbstowcs(from_lcd, from_type, to_lcd, to_type)
  2779.     XLCd from_lcd;
  2780.     char *from_type;
  2781.     XLCd to_lcd;
  2782.     char *to_type;
  2783. {
  2784.     DBUG_ENTER("open_mbstowcs")
  2785.     XlcConv result = create_conv(from_lcd, &mbstowcs_methods);
  2786.     DBUG_RETURN(result);
  2787. }
  2788. static XlcConvMethodsRec wcstombs_methods = {
  2789.     close_converter,
  2790.     wcstombs_org,
  2791.     NULL
  2792. } ;
  2793. static XlcConv
  2794. open_wcstombs(from_lcd, from_type, to_lcd, to_type)
  2795.     XLCd from_lcd;
  2796.     char *from_type;
  2797.     XLCd to_lcd;
  2798.     char *to_type;
  2799. {
  2800.     DBUG_ENTER("open_wcstombs")
  2801.     XlcConv result = create_conv(from_lcd, &wcstombs_methods);
  2802.     DBUG_RETURN(result);
  2803. }
  2804. static XlcConvMethodsRec wcstocts_methods = {
  2805.     close_converter,
  2806.     wcstocts,
  2807.     NULL
  2808. } ;
  2809. static XlcConv
  2810. open_wcstocts(from_lcd, from_type, to_lcd, to_type)
  2811.     XLCd from_lcd;
  2812.     char *from_type;
  2813.     XLCd to_lcd;
  2814.     char *to_type;
  2815. {
  2816.     DBUG_ENTER("open_wcstocts")
  2817.     XlcConv result = create_conv(from_lcd, &wcstocts_methods);
  2818.     DBUG_RETURN(result);
  2819. }
  2820. static XlcConvMethodsRec wcstostr_methods = {
  2821.     close_converter,
  2822.     wcstostr,
  2823.     NULL
  2824. } ;
  2825. static XlcConv
  2826. open_wcstostr(from_lcd, from_type, to_lcd, to_type)
  2827.     XLCd from_lcd;
  2828.     char *from_type;
  2829.     XLCd to_lcd;
  2830.     char *to_type;
  2831. {
  2832.     DBUG_ENTER("open_wcstostr")
  2833.     XlcConv result = create_conv(from_lcd, &wcstostr_methods);
  2834.     DBUG_RETURN(result);
  2835. }
  2836. static XlcConvMethodsRec wcstocs_methods = {
  2837.     close_converter,
  2838.     wcstocs,
  2839.     NULL
  2840. } ;
  2841. static XlcConv
  2842. open_wcstocs(from_lcd, from_type, to_lcd, to_type)
  2843.     XLCd from_lcd;
  2844.     char *from_type;
  2845.     XLCd to_lcd;
  2846.     char *to_type;
  2847. {
  2848.     DBUG_ENTER("open_wcstocs")
  2849.     XlcConv result = create_conv(from_lcd, &wcstocs_methods);
  2850.     DBUG_RETURN(result);
  2851. }
  2852. static XlcConvMethodsRec wctocs_methods = {
  2853.     close_converter,
  2854.     wctocs,
  2855.     NULL
  2856. } ;
  2857. static XlcConv
  2858. open_wctocs(from_lcd, from_type, to_lcd, to_type)
  2859.     XLCd from_lcd;
  2860.     char *from_type;
  2861.     XLCd to_lcd;
  2862.     char *to_type;
  2863. {
  2864.     DBUG_ENTER("open_wctocs")
  2865.     XlcConv result = create_conv(from_lcd, &wctocs_methods);
  2866.     DBUG_RETURN(result);
  2867. }
  2868. static XlcConvMethodsRec ctstowcs_methods = {
  2869.     close_converter,
  2870.     ctstowcs,
  2871.     NULL
  2872. } ;
  2873. static XlcConv
  2874. open_ctstowcs(from_lcd, from_type, to_lcd, to_type)
  2875.     XLCd from_lcd;
  2876.     char *from_type;
  2877.     XLCd to_lcd;
  2878.     char *to_type;
  2879. {
  2880.     DBUG_ENTER("open_ctstowcs")
  2881.     XlcConv result = create_conv(from_lcd, &ctstowcs_methods);
  2882.     DBUG_RETURN(result);
  2883. }
  2884. static XlcConvMethodsRec strtowcs_methods = {
  2885.     close_converter,
  2886.     strtowcs,
  2887.     NULL
  2888. } ;
  2889. static XlcConv
  2890. open_strtowcs(from_lcd, from_type, to_lcd, to_type)
  2891.     XLCd from_lcd;
  2892.     char *from_type;
  2893.     XLCd to_lcd;
  2894.     char *to_type;
  2895. {
  2896.     DBUG_ENTER("open_strtowcs")
  2897.     XlcConv result = create_conv(from_lcd, &strtowcs_methods);
  2898.     DBUG_RETURN(result);
  2899. }
  2900. /* -------------------------------------------------------------------------- */
  2901. /*                Loader                                        */
  2902. /* -------------------------------------------------------------------------- */
  2903. XLCd
  2904. _XlcGenericLoader(name)
  2905.     _Xconst char *name;
  2906. {
  2907.     DBUG_ENTER("_XlcGenericLoader")
  2908.     XLCd lcd;
  2909.     XLCdGenericPart *gen;
  2910.     lcd = _XlcCreateLC(name, _XlcGenericMethods);
  2911.     if (lcd == NULL) {
  2912. #if defined(DEBUG) && defined(VERBOSE)
  2913.     fprintf(stderr, "_XlcGenericLoader: _XlcCreateLC failed!");
  2914. #endif
  2915.     DBUG_RETURN(lcd);
  2916.     }
  2917.     _XlcSetConverter(lcd, XlcNMultiByte, lcd, XlcNCompoundText, open_mbstocts);
  2918.     _XlcSetConverter(lcd, XlcNMultiByte, lcd, XlcNString,       open_mbstostr);
  2919.     _XlcSetConverter(lcd, XlcNMultiByte, lcd, XlcNCharSet,      open_mbstocs);
  2920.     _XlcSetConverter(lcd, XlcNMultiByte, lcd, XlcNChar,         open_mbtocs);
  2921.     _XlcSetConverter(lcd, XlcNCompoundText, lcd, XlcNMultiByte, open_ctstombs);
  2922.     _XlcSetConverter(lcd, XlcNString,    lcd, XlcNMultiByte,    open_strtombs);
  2923. #ifdef STDCVT
  2924.      gen = XLC_GENERIC_PART(lcd);
  2925.      if (gen->use_stdc_env != True) {
  2926. #endif
  2927.         _XlcSetConverter(lcd, XlcNMultiByte, lcd, XlcNWideChar,     open_mbstowcs);
  2928.         _XlcSetConverter(lcd, XlcNWideChar,  lcd, XlcNMultiByte,    open_wcstombs);
  2929.         _XlcSetConverter(lcd, XlcNWideChar,  lcd, XlcNCompoundText, open_wcstocts);
  2930.         _XlcSetConverter(lcd, XlcNWideChar,  lcd, XlcNString,       open_wcstostr);
  2931.         _XlcSetConverter(lcd, XlcNWideChar,  lcd, XlcNCharSet,      open_wcstocs);
  2932.         _XlcSetConverter(lcd, XlcNWideChar,  lcd, XlcNChar,         open_wctocs);
  2933.         _XlcSetConverter(lcd, XlcNCompoundText, lcd, XlcNWideChar,  open_ctstowcs);
  2934.         _XlcSetConverter(lcd, XlcNString,    lcd, XlcNWideChar,     open_strtowcs);
  2935. #ifdef STDCVT
  2936.     }
  2937. #endif
  2938. #ifdef STDCVT
  2939.     if (gen->use_stdc_env == True) {
  2940.         _XlcSetConverter(lcd, XlcNMultiByte, lcd, XlcNWideChar,     open_stdc_mbstowcs);
  2941.         _XlcSetConverter(lcd, XlcNWideChar,  lcd, XlcNMultiByte,    open_stdc_wcstombs);
  2942.         _XlcSetConverter(lcd, XlcNWideChar,  lcd, XlcNCompoundText, open_stdc_wcstocts);
  2943.         _XlcSetConverter(lcd, XlcNWideChar,  lcd, XlcNString,       open_stdc_wcstostr);
  2944.         _XlcSetConverter(lcd, XlcNWideChar,  lcd, XlcNCharSet,      open_stdc_wcstocs);
  2945.         _XlcSetConverter(lcd, XlcNWideChar,  lcd, XlcNChar,         open_stdc_wctocs);
  2946.         _XlcSetConverter(lcd, XlcNCompoundText, lcd, XlcNWideChar,  open_stdc_ctstowcs);
  2947.         _XlcSetConverter(lcd, XlcNString,    lcd, XlcNWideChar,     open_stdc_strtowcs);
  2948.     }
  2949. #endif
  2950.     DBUG_RETURN(lcd);
  2951. }
  2952.