[comp.sources.x] v04i010: kterm -- Kanji

argv@island.uu.net (Dan Heller) (05/31/89)

Submitted-by: Hiroto Kagotani <kagotani@cs.titech.ac.jp>
Posting-number: Volume 4, Issue 10
Archive-name: kterm/part01

[ This posting contains this shar file and 3 other patch files.  The patches
  apply to the default xterm source (see the readme).  The result to all this
  is a very impressive japanese xterm with some selection features, too.
  I applied the patches without error, but I had to manually install the
  old fonts from X11R2 (r14, a14, and k14) into my R3 fonts.  This may have
  been my fault in my original installation of R3, but if not, you might need
  to do this.  Note that the 3 patches (in the next three postings) are not
  in shar format.  --argv ]

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then feed it
# into a shell via "sh file" or similar.  To overwrite existing files,
# type "sh file -c".
# The tool that generated this appeared in the comp.sources.unix newsgroup;
# send mail to comp-sources-unix@uunet.uu.net if you want that tool.
# If this archive is complete, you will see the following message at the end:
#		"End of shell archive."
# Contents:  README README.j.uu kterm.man patchlevel.c convert.c
# Wrapped by argv@island on Tue May 30 22:50:09 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'README' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'README'\"
else
echo shar: Extracting \"'README'\" \(2070 characters\)
sed "s/^X//" >'README' <<'END_OF_FILE'
XKterm is a terminal emulator allowing to use Japanese on X11 Window System.
XIt is expansion of xterm and have some bugs like xterm. Further there may
Xbe some bugs in the expanded part. If you find such bugs, tell me please.
X
X                                          1989 May 24  Hiroto Kagotani
X                                              kagotani@cs.titech.ac.jp
X
XInstalling
X
XThis version of kterm is based on xterm in the X11R3 distribution. Some
Xofficial patches for X11R3 exist and the first 4 patches must be applied
Xfor xterm. Patches No.5 through No.9 are all right, because they include
Xno patch for xterm. But patches after them are not warranted to be applied
Xcorrectly.
X
XApply this patch to the xterm sources in a suitable directory. Then modify
XImakefile for following options and do compile with it.
X-DKANJI		allow you to see, cut and paste Japanese texts.
X-DSTATUSLINE	allow you to use a status line at the bottom of kterm.
X-DKEEPALIVE	allow you to choose connection to X server with or without
X		KEEPALIVE option.
XIf none of these options is given, kterm provides same function as xterm.
X
XInstall kterm.
XMake kterm entry in your termcap.
XInstall a14, r14 and k14 fonts which are in the X11R3 distribution tape.
X"cat"ing README.j file on your kterm can test it.
X
X
XCutting and Pasting
X
XKterm uses selection with COMPOUND_TEXT encoding for cutting Japanese
Xbecause any CutBuffers does not allowed to contain non-ASCII character as
Xdefined in the ICCCM. Therefore non-ASCII text cutted this version of
Xkterm can not be pasted to previously released kterm nor NEmacs, though
Xpasting in opposite direction works well.
X
XEncoding used in COMPOUND_TEXT is based on "Compound Text Encoding Version 1"
Xand G0 and G1 are used as follows:
X	G0 is invoked into GL and G1 is invoked into GR, and they are fixed.
X	ASCII		designated into G0 always.
X	Latin-1 right	normaly, designated into G1.
X	JIS Kana	if needed, designated into G1.
X	JIS Kanji	if needed, designated into G1.
XBut for pasting, any character sets designated into any of G0 or G1 are
Xrecognized correctly.
END_OF_FILE
if test 2070 -ne `wc -c <'README'`; then
    echo shar: \"'README'\" unpacked with wrong size!
fi
# end of 'README'
fi
if test -f 'README.j.uu' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'README.j.uu'\"
else
echo shar: Extracting \"'README.j.uu'\" \(3679 characters\)
sed "s/^X//" >'README.j.uu' <<'END_OF_FILE'
Xbegin 444 README.j
XM("!K=&5R;2 ;)$(D3R$B&RA"6#$Q(!LD0B4F)2,E<R5))28E-R4Y)48E8#YE
XM)$=&?$M<.&PD<DUX35$D.21K)#\D821.)3\A/"5?)4HE:QLH0@H;)$(E*"5?
XM)64E;"$\)3\D1R0Y(2,;*$)K=&5R;2 ;)$(D3QLH0B!X=&5R;2 ;)$(D<C@U
XM)$LZ;B1I)&PD1B0D)%XD.21.)$<A(ALH0GAT97)M(!LD0B1+-%@D.1LH0@H;
XM)$(D:R50)3 D3R$B)%LD2"1S)$D;*$(@:W1E<FT@&R1")$LD8D5V)$8D3R1>
XM)&HD7B0Y(2,D7B0_&RA"(&MT97)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F)$<D3R$B)$DD
XM3B4M)6,E:24O)3\E.R5#)4@D+!LH0B!',"Q',2 ;)$(D3B0D)#HD;"1+.U@\
XH*!LH0@H;)$(D-21L)$8D8D U)#<D+S)R/&$D-21L)%XD.2$C&RA""B1+
X 
Xend
END_OF_FILE
if test 3679 -ne `wc -c <'README.j.uu'`; then
    echo shar: \"'README.j.uu'\" unpacked with wrong size!
fi
# end of 'README.j.uu'
fi
if test -f 'kterm.man' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'kterm.man'\"
else
echo shar: Extracting \"'kterm.man'\" \(6204 characters\)
sed "s/^X//" >'kterm.man' <<'END_OF_FILE'
X.TH KTERM 1 "19 May 1989" "X Version 11"
X.SH NAME
Xkterm \- terminal emulator for X
X.SH SYNOPSIS
X.B kterm
X[-\fItoolkitoption\fP ...] [-option ...]
X.SH DESCRIPTION
XThe \fIkterm\fP program is a Kanji terminal emulator for the X Window System.
XIt provides DEC VT102 and Tektronix 4014 
Xcompatible terminals for programs that can't
Xuse the window system directly.
XIt comes from \fIxterm\fP in the core programs of the distribution of the
XX Window System.
XThe most of the functions are the same as original \fIxterm\fP's, however,
Xit has capabilities of displaying Kanji strings and handling of the status line,
Xif compiled with -DKANJI or -DSTATUSLINE compile time options.
X.PP
X.SH OPTIONS
XThe \fIkterm\fP terminal emulator 
Xaccepts all of the standard \fIxterm\fP command line options along with the 
Xadditional options listed below (if the option begins with a
X.RB ` + '
Xinstead of a
X.RB ` \- ',
Xthe option is restored to its default value):
X.TP 8
X.BI \-fk " kanji-font"
XThis option specifies a Kanji font to be used when displaying Kanji text.  
XThis font must be the same height and width as the ascii font.
XThe default is ``a14''.
X.TP 8
X.BI \-fkb " kanji-bold-font"
XThis option specifies a Kanji bold font to be used when displaying bold text.  
XThis font must be the same height and width as the kanji font.
XIf no Kanji bold font is specified, it will be used as the
Xnormal font and the bold font will be produced by overstriking this font.
XThe default is not specified.
X.TP 8
X.BI \-fr " kana-font"
XThis option specifies a Kana font, which may be used as GR in 8bit environment.
XThis font is used if ``ESC ( I'' is appeared in JIS Kanji mode, SS2 (0x8e)
Xis appeared in EUC Kanji mode, and not used so frequntly in normal Japanese
Xtext.
XThe default is ``kana14''.
X.TP 8
X.BI \-frb " kana-bold-font"
XThis option specifies a Kana bold font.
X.TP 8
X.BI \-ka
XThis option indicates that \fIkterm\fP should connect to the X server
Xwith KEEPALIVE socket option. It is usefull when the server is a X terminal
Xthat is frequently powered off. This option is accepted only if \fIkterm\fP
Xhas been compiled with -DKEEPALIVE compile option.
X.TP 8
X.BI \+ka
XThis option indicates that \fIkterm\fP should connect to the X server
Xwithout KEEPALIVE socket option.
X.TP 8
X.BI \-km " kanji-mode"
XThis option specifies the Kanji code from the pty output.
XIf kanji-mode is ``jis'', then it assumes the input/output is coded by JIS code,
Xi.e., each Kanji string is proceeded by ``ESC $ B'' or ``ESC $ @'' and each
Xascii string is proceeded by ``ESC ( B'' or ``ESC ( J''.
XThis mode does not require 8 bit passing tty module because 7 bit encoding
Xwith appropriate escape sequences is used.
XIf kanji-mode is ``euc'', then it assumes the input/output is coded by EUC.
XIf kanji-mode is ``sjis'', then it assumes the input/output is coded by
XShift-JIS code (which is the same as MicroSoft Kanji code).
XThe default mode is ``jis''.
X.TP 8
X.B \-sn
XBy default, the status line is in reverse-video (relative to the rest of the
Xwindow).
XThis option causes the status line to be in normal video (the status line
Xis still enclosed in a box).
X.TP 8
X.B \-st
XThis option causes the status line to be displayed on startup.
X.SH "X DEFAULTS"
XThe program understands all of the core xterm resource names and
Xclasses as well as:
X.TP 8
X.B "kanjiFont (\fPclass\fB KanjiFont)"
XSpecifies the name of the kanji font.  The default is ``a14.''
X.TP 8
X.B "kanjiboldFont (\fPclass\fB KanjiFont)"
XSpecifies the name of the bold font.  The default is not specified.
X.TP 8
X.B "kanaFont (\fPclass\fB KanaFont)"
XSpecifies the name of the kana font.  The default is ``kana14.''
X.TP 8
X.B "kanaboldFont (\fPclass\fB KanaFont)"
XSpecifies the name of the bold font.  The default is not specified.
X.TP 8
X.B "kanjiMode (\fPclass\fB KanjiMode)"
XSpecifies the Kanji code of pty output. The default is ``jis.''
X.TP 8
X.B "keepAlive (\fPclass\fB KeepAlive)"
XSpecifies whether or not \fIkterm\fP should connection to the X server
Xwith KEEPALIVE socket option. The default is ``false.''
X.TP 8
X.B "statusLine (\fPclass\fB StatusLine)"
XCauses the status line to be displayed on startup. The default is ``false.''
X.TP 8
X.B "statusNormal (\fPclass\fB StatusNormal)"
XSpecifies whether or not the status line should be in normal video.
XThe default is ``false.''
X.SH EMULATIONS
XThe VT102 emulation is fairly complete, but does not support the blinking
Xcharacter attribute nor the double-wide and double-size character sets.
X.IR Termcap (5)
Xentries that work with
X.I xterm
Xinclude ``kterm'', ``xterm'', ``vt102'', ``vt100'' and ``ansi'', and
X.I xterm
Xautomatically searches the termcap file in this order for these entries and then
Xsets the ``TERM'' and the ``TERMCAP'' environment variables.
X.SH "SEE ALSO"
Xxterm(1), resize(1), X(1), pty(4), tty(4)
X.br
X.I ``Xterm Control Sequences''
X.SH BUGS
X\fIKterm\fP assumes that the width of Kanji characters is as twice as
Xthat of ASCII characters. Of course variable-width fonts are not handled
Xlike \fIxterm\fP. 
X.PP
XThis version of
X.I kterm
Xuses ``ESC $ B'' to designate the Kanji character set in tty output when JIS
Xmode is selected.
XThere is no way to specify ``ESC $ ( B'' instead.
X.PP
XBecause of a historical reason in Japan, the sequence to designate
XSwedish character set ``ESC ( H'' is treated as that of Japanese roman
Xcharacter set.
X.PP
XThere is no distinction between ASCII and Japnese roman character sets,
Xthough the latter includes the ``Yen'' character, interrupted bar and
Xupper score.
X.PP
XCurrently available \fIxterm\fP
Xnor previously released \fIkterm\fP can not paste a Kanji text from this
X\fIkterm\fP. Because we are not allowed to store the cutted Kanji text
Xinto the cut buffer or selection with STRING type, as defined in the ICCCM,
X.SH COPYRIGHT
XCopyright 1988 and 1989, XXI working group in Japan Unix Society Japan.
X.br
XSee \fIX(1)\fP for a full statement of rights and permissions.
X.SH AUTHORS
XFar too many people, including:
X.sp
XKatsuya Sano (Tokyo Inst. of Tech.),
XMichael Irie (Sony Corp.),
XAkira Kato (Tokyo Inst. of Tech.),
XMichiharu Ariza (Software Research Associates),
XMakoto Ishisone (Software Research Associates),
XHiroto Kagotani (Tokyo Inst. of Tech.)
X.sp
XSend bugs to kagotani@cs.titech.ac.jp, please.
END_OF_FILE
if test 6204 -ne `wc -c <'kterm.man'`; then
    echo shar: \"'kterm.man'\" unpacked with wrong size!
fi
# end of 'kterm.man'
fi
if test -f 'patchlevel.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'patchlevel.c'\"
else
echo shar: Extracting \"'patchlevel.c'\" \(204 characters\)
sed "s/^X//" >'patchlevel.c' <<'END_OF_FILE'
X#ifdef	KANJI
X/*
X * Patchlevel for the difference between xterm and kterm.
X */
Xstatic char kterm_patchlevel[] = "kterm 3.1.0 based on xterm in release 3";
Xchar menuname[] = "kterm X11/3.1.0";
X#endif	KANJI
END_OF_FILE
if test 204 -ne `wc -c <'patchlevel.c'`; then
    echo shar: \"'patchlevel.c'\" unpacked with wrong size!
fi
# end of 'patchlevel.c'
fi
if test -f 'convert.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'convert.c'\"
else
echo shar: Extracting \"'convert.c'\" \(12910 characters\)
sed "s/^X//" >'convert.c' <<'END_OF_FILE'
X/*
X *	convert.c -- code converters for kterm
X */
X
X/*
X * Copyright (c) 1989  Software Research Associates, Inc.
X *
X * Permission to use, copy, modify, and distribute this software and its
X * documentation for any purpose and without fee is hereby granted, provided
X * that the above copyright notice appear in all copies and that both that
X * copyright notice and this permission notice appear in supporting
X * documentation, and that the name of Software Research Associates not be
X * used in advertising or publicity pertaining to distribution of the
X * software without specific, written prior permission.  Software Research
X * Associates makes no representations about the suitability of this software
X * for any purpose.  It is provided "as is" without express or implied
X * warranty.
X *
X * Author:  Makoto Ishisone, Software Research Associates, Inc., Japan
X */
X
X#ifndef lint
Xstatic char kterm_id[] = "$Header: convert.c,v 1.1 89/05/30 14:53:36 kagotani Rel $";
X#endif /* lint */
X
X#ifdef KANJI
X
Xtypedef unsigned short	wchar_t;
X
X#define NULL	0
X
X/* WS -> JIS */
X#define ASCII	0
X#define KANJI	1
X#define KANA	2
X
Xint
XconvWStoJIS(ws, js)
Xwchar_t		*ws;
Xunsigned char	*js;
X{
X	int	c;
X	int	kanji = ASCII;
X	int	n = 0;
X
X	while (c = *ws++) {
X		switch (c & 0x8080) {
X		case 0:
X			if (kanji != ASCII) {
X				if (js) {
X					*js++ = '\033';
X					*js++ = '(';
X					*js++ = 'J';
X				}
X				n += 3;
X			}
X			if (js)
X				*js++ = c & 0x7f;
X			n++;
X			kanji = ASCII;
X			break;
X		case 0x80:
X			if (kanji != KANA) {
X				if (js) {
X					*js++ = '\033';
X					*js++ = '(';
X					*js++ = 'I';
X				}
X				n += 3;
X			}
X			if (js)
X				*js++ = c & 0x7f;
X			n++;
X			kanji = KANA;
X			break;
X		case 0x8080:
X			if (kanji != KANJI) {
X				if (js) {
X					*js++ = '\033';
X					*js++ = '$';
X					*js++ = 'B';
X				}
X				n += 3;
X			}
X			if (js) {
X				*js++ = (c >> 8) & 0x7f;
X				*js++ = c & 0x7f;
X			}
X			n += 2;
X			kanji = KANJI;
X			break;
X		}
X	}
X	if (kanji != ASCII) {
X		if (js) {
X			*js++ = '\033';
X			*js++ = '(';
X			*js++ = 'J';
X		}
X		n += 3;
X	}
X	if (js)
X		*js = '\0';
X
X	return n;
X}
X
X/* WS -> EUC */
Xint
XconvWStoEUC(ws, es)
Xwchar_t		*ws;
Xunsigned char	*es;
X{
X	int	c;
X	int	n = 0;
X
X	while (c = *ws++) {
X		switch (c & 0x8080) {
X		case 0:
X			if (es)
X				*es++ = c & 0x7f;
X			n++;
X			break;
X		case 0x80:
X			if (es) {
X				*es++ = 0x8e;	/* SS2 */
X				*es++ = c & 0xff;
X			}
X			n += 2;
X			break;
X		case 0x8000:
X			if (es) {
X				*es++ = 0x8f;	/* SS3 */
X				*es++ = (c >> 8) | 0x80;
X				*es++ = (c & 0xff) | 0x80;
X			}
X			n += 3;
X			break;
X		case 0x8080:
X			if (es) {
X				*es++ = c >> 8;
X				*es++ = c & 0xff;
X			}
X			n += 2;
X			break;
X		}
X	}
X	if (es)
X		*es = '\0';
X
X	return n;
X}
X
X/* WS -> SJIS */
Xint
XconvWStoSJIS(ws, ss)
Xwchar_t		*ws;
Xunsigned char	*ss;
X{
X	int	c1, c2;
X	int	n = 0;
X
X	while (c1 = *ws++) {
X		switch (c1 & 0x8080) {
X		case 0:
X		case 0x80:
X			if (ss)
X				*ss++ = c1 & 0xff;
X			n++;
X			break;
X		case 0x8080:
X			c2 = c1 & 0x7f;
X			c1 = (c1 >> 8) & 0x7f;
X			if (ss) {
X				*ss++ = (c1 - 0x21) / 2 +
X				    ((c1 <= 0x5e) ? 0x81 : 0xc1);
X				if (c1 & 1)	/* odd */
X				    *ss++ = c2 + ((c2 <= 0x5f) ? 0x1f : 0x20);
X				else
X				    *ss++ = c2 + 0x7e;
X			}
X			n += 2;
X			break;
X		}
X	}
X	if (ss)
X		*ss = '\0';
X
X	return n;
X}
X
X/* WS -> ISO Latin-1 */
Xint
XconvJWStoLatin1(ws, ls)
Xwchar_t *ws;
Xunsigned char *ls;
X{
X    int	n = 0;
X    int c;
X
X    if (ls == NULL) {
X	while (c = *ws++) {
X	    if ((c & 0x8080) == 0)
X	        n++;
X	}
X    } else {
X	while (c = *ws++) {
X	    if ((c & 0x8080) == 0) {
X		*ls++ = c & 0x7f;
X		n++;
X	    }
X	}
X    }
X    return n;
X}
X
X/******************************************************************************
XCOMPOUND_TEXT Summary (Comopund Text Encoding Version 1 -- public review)
X(1) Only G0 and G1 are used. G2 and G3 are not.
X(2) G0 is invoked into GL and G1 into GR. These invocation are not changed.
X	(In other words, Locking Shift and Single Shift are not used)
X(3) In initial state, ISO Latin-1 is designated into G0/G1.
X(4) To designate MBCS into G0, ESC-$-F is not used but ESC-$-(-F.
X(5) In C0, only HT, NL, and ESC are used.
X(6) In C1, only CSI is used.
X(7) Text direction can be indecated.
X	begin left-to-right string
X	begin right-to-left string
X	end of string
X******************************************************************************/
X
X#define CS96	0x100	/* 96chars CS */
X#define MBCS	0x200	/* Multibyte CS */
X
X/* convJWStoCT -- Japanese Wide Character String -> COMPOUND_TEXT */
Xint
XconvJWStoCT(wstr, xstr, jisroman)
Xregister wchar_t *wstr;
Xregister unsigned char *xstr;
Xint jisroman;	/* if true, map YEN-mark etc.to right part of Latin-1 */
X/* Convert Wide Character String wstr to COMPOUND_TEXT xstr, return
X * length of xstr in bytes (not including the terminating null character).
X * If xstr is NULL, no conversion is done, but return length of xstr.
X */
X{
X	register int	c;
X	register int	g0, g1;
X	register int	n = 0;
X	
X	g0 = 'B';
X	g1 = CS96|'A';
X	
X	/*
X	 * G0 and G1 are used for following character sets
X	 *  G0: ASCII
X	 *  G1: KANJI / KANA / JIS-ROMAN emulation (8859/1 right part)
X	 */
X	
X	while (c = *wstr++) {
X		switch (c & 0x8080) {
X		case 0:		/* ASCII or C0 */
X			if (c < ' ') {
X				/* C0 */
X				if (c == '\t' || c == '\n') {
X					if (xstr) *xstr++ = c;
X					n++;
X				}
X				break;
X			}
X			if (jisroman && (c == '\\' || c == '|' || c == '~')) {
X				if (g1 != (CS96|'A')) {
X					if (xstr) {
X						*xstr++ = '\033';
X						*xstr++ = '-';
X						*xstr++ = 'A';
X					}
X					n += 3;
X					g1 = CS96|'A';
X				}
X				if (xstr) {
X					if (c == '\\') *xstr++ = 0xa5;
X					else if (c == '|') *xstr++ = 0xa6;
X					else *xstr++ = 0xaf;
X				}
X				n++;
X				break;
X			}
X			if (g0 != 'B') {
X				if (xstr) {
X					*xstr++ = '\033';
X					*xstr++ = '(';
X					*xstr++ = 'B';
X				}
X				n += 3;
X				g0 = 'B';
X			}
X			if (xstr) *xstr++ = c & 0x7f;
X			n++;
X			break;
X		case 0x80:	/* KANA */
X			if (g1 != 'I') {
X				if (xstr) {
X					*xstr++ = '\033';
X					*xstr++ = ')';
X					*xstr++ = 'I';
X				}
X				n += 3;
X				g1 = 'I';
X			}
X			if (xstr) *xstr++ = c & 0xff;
X			n++;
X			break;
X		case 0x8080:	/* KANJI */
X			if (g1 != (MBCS|'B')) {
X				if (xstr) {
X					*xstr++ = '\033';
X					*xstr++ = '$';
X					*xstr++ = ')';
X					*xstr++ = 'B';
X				}
X				n += 4;
X				g1 = MBCS|'B';
X			}
X			if (xstr) {
X				*xstr++ = (c >> 8) & 0xff;
X				*xstr++ = c & 0xff;
X			}
X			n += 2;
X			break;
X		default:
X			/* ignored */
X			break;
X		}
X	}
X	if (xstr) *xstr = '\0';
X	return n;
X}
X
Xstatic unsigned char *
Xgetesc(str, len)
Xunsigned char *str;
Xint len;
X{
X	register int	c;
X
X	/* Find intermediate characters and final character
X	 * following the escape character in an escape sequence.
X	 */
X	/* The intermediate character is 02/00 to 02/15 */
X	while (len > 0) {
X		c = *str;
X		if (c < 0x20 || 0x2f < c)
X			break;
X		len--, str++;
X	}
X	/* The final character is 03/00 to 07/14 */
X	if (--len < 0 || (c = *str++) < 0x30 || 0x7e < c)
X		return (unsigned char *)NULL;
X
X	return str;
X}
X
Xstatic unsigned char *
Xgetcsi(str, len)
Xunsigned char *str;
Xint len;
X{
X	register int	c;
X
X	/* Find parameter characters, intermediate characters
X	 * and final character following the CSI character
X	 * in a CSI sequence.
X	 */
X	/* The parameter characters is 03/00 to 03/15 */
X	while (len > 0) {
X		c = *str;
X		if (c < 0x30 || 0x3f < c)
X			break;
X		len--, str++;
X	}
X	/* The intermediate character is 02/00 to 02/15 */
X	while (len > 0) {
X		c = *str;
X		if (c < 0x20 || 0x2f < c)
X			break;
X		len--, str++;
X	}
X	/* The final character is 04/00 to 07/14 */
X	if (--len < 0 || (c = *str++) < 0x40 || 0x7e < c)
X		return (unsigned char *)NULL;
X
X	return str;
X}
X
X/* convCTtoJWS -- COMPOUND_TEXT -> Japanese Wide Character String */
Xint
XconvCTtoJWS(xstr, len, wstr)
Xregister unsigned char *xstr;
Xint len;
Xwchar_t *wstr;
X/* Convert COMPOUND_TEXT xstr to Wide Character String wstr, return
X * length of wstr in characters (not including the terminating null character).
X * If wstr is NULL, no conversion is done, but return length of wstr.
X */
X{
X	register int	c;
X	int	nskip;
X	int	n = 0;
X	int	g0, g1, gs;
X	unsigned char	*xstr1;
X
X	/*
X	 * Compound Text can include null octet. Therefore the length
X	 * of xstr is able to be specified by parameter len.
X	 * But if len is zero or negative, get length by strlen() assuming
X	 * that no null octet exists.
X	 */
X	if (len <= 0) {
X		len = strlen((char *)xstr);
X	}
X
X	/* In initial state, ISO 8859/1 is designated into G0/G1 */
X	g0 = 'B';	/* ASCII -> G0 */
X	g1 = CS96|'A';	/* Latin/1 right hand part -> G1 */
X
X	while (len-- > 0) {
X		switch (c = *xstr++) {
X		case '\n':	/* NEWLINE */
X		case '\t':	/* TAB */
X			if (wstr) *wstr++ = c;
X			n++;
X			break;
X		case 0x9b:	/* CSI */
X			/*
X			 * CSI sequence is generally in following form:
X			 *	CSI {P} {I} F
X			 *        P : 03/00 to 03/15
X			 *        I : 02/00 to 02/15
X			 *        F : 04/00 to 07/14
X			 */
X			/*
X			 * Currently only directionality is definde
X			 * as following:
X			 *	CSI-1-]		begin left-to-right text
X			 *	CSI-2-]		begin right-to-left text
X			 *	CSI-]		end of string
X			 * But this implementation ignores them.
X			 */
X			xstr1 = getcsi(xstr, len);
X			if (xstr1 == NULL)
X				return -1;
X			len -= xstr1 - xstr;
X			xstr = xstr1;
X			break;
X		case '\033':	/* ESC */
X			/*
X			 * ESC sequence is generally in following form:
X			 *	ESC {I} F
X			 *        I : 02/00 to 02/15
X			 *        F : 03/00 to 07/14
X			 */
X			/*
X			 * Currently, following functions are defined:
X			 *   Standard character set
X			 *	ESC-(-F
X			 *	ESC-$-(-F
X			 *	ESC-)-F
X			 *	ESC---F
X			 *	ESC-$-)-F
X			 *   Non standard character set
X			 *	ESC-%-/-[0123]
X			 * Standard character set must be accepted correctly.
X			 * Non standard one is ignored but must be parsed
X			 * for skipping data.
X			 */
X			xstr1 = getesc(xstr, len);
X			if (xstr1 == NULL)
X				return -1;
X			len -= xstr1 - xstr;
X			switch (xstr1 - xstr) {
X			case 2:		/* ESC - I - F */
X				switch (*xstr++) {
X				case '(':	/* 94chars CS -> G0 */
X					g0 = *xstr;
X					break;
X				case ')':	/* 94chars CS -> G1 */
X					g1 = *xstr;
X					break;
X				case '-':	/* 96chars CS -> G1 */
X					g1 = *xstr | CS96;
X					break;
X				default:	/* ignore */
X					break;
X				}
X				break;
X			case 3:		/* ESC - I - I - F */
X				switch (*xstr++) {
X				case '$':
X					switch (*xstr++) {
X					case '(':	/* 94chars MBCS -> G0 */
X						g0 = *xstr | MBCS;
X						break;
X					case ')':	/* 94chars MBCS -> G1 */
X						g1 = *xstr | MBCS;
X						break;
X					case '-':	/* 96chars MBCS -> G1 */
X						g1 = *xstr | CS96 | MBCS;
X						break;
X					default:	/* ignore */
X						break;
X					}
X					break;
X				case '%':
X					if (*xstr++ != '/') {
X						/* unknown sequence */
X						break;
X					}
X					/*
X					 * Private encoding is ignored.
X					 * But following data must be skipped.
X					 *	ESC-%-/-F-M-L
X					 */
X					len -= 2;
X					if (len < 0)
X						return -1;
X					nskip = (*xstr1 & 0x7f) * 128 +
X					    (*(xstr1 + 1) & 0x7f);
X					if ((len -= nskip) < 0)
X						return -1;
X					xstr1 += nskip + 2;
X					break;
X				default:
X					break;
X				}
X				break;
X			default:
X				break;
X			}
X			xstr = xstr1;
X			break;
X		default:
X			if (!(c & 0x60)) {
X				/*
X				 * Non NL/TAB/ESC/CSI character in C0 or C1
X				 * is an obvious error.
X				 */
X				return -1;
X			}
X			gs = (c & 0x80) ? g1 : g0;
X			c &= 0x7f;
X			if (gs & MBCS) {
X				switch (gs & 0x70) {
X				case 0x70:	/* 4byte/char */
X					if (--len < 0) return -1;
X					c = (c << 8) | (*xstr++ & 0x7f);
X				case 0x60:	/* 3byte/char */
X					if (--len < 0) return -1;
X					c = (c << 8) | (*xstr++ & 0x7f);
X				case 0x50:	/* 2byte/char */
X				case 0x40:	/* 2byte/char */
X					if (--len < 0) return -1;
X					c = (c << 8) | (*xstr++ & 0x7f);
X					break;
X				default:
X					return -1;
X				}
X			}
X			if (wstr) {
X				switch (gs) {
X				case 'B':
X					*wstr++ = c;
X					n++;
X					break;
X				case 'I':
X					*wstr++ = 0x80 | c;
X					n++;
X					break;
X				case CS96|'A':
X					switch (c) {
X					case 0x25:	/* yen */
X						*wstr++ = '\\';
X						n++;
X						break;
X					case 0x26:	/* bar */
X						*wstr++ = '|';
X						n++;
X						break;
X					case 0x2f:	/* upperscore */
X						*wstr++ = '~';
X						n++;
X						break;
X					}
X					break;
X				case MBCS|'B':
X					*wstr++ = 0x8080 | c;
X					n++;
X					break;
X				}
X			} else {
X				switch (gs) {
X				case 'B':
X				case 'I':
X					n++;
X					break;
X				case CS96|'A':
X					switch (c) {
X					case 0x25:	/* yen */
X					case 0x26:	/* bar */
X					case 0x2f:	/* upperscore */
X						n++;
X						break;
X					}
X					break;
X				case MBCS|'B':
X					n++;
X					break;
X				}
X			}
X			break;
X		}
X	}
X	if (wstr) *wstr = 0;
X	return n;
X}
X
X#ifdef KCLIENT
X/* EUC -> WS */
XconvEUCtoWS(es, ws)
Xunsigned char	*es;
Xwchar_t		*ws;
X{
X	int	c;
X	int	n = 0;
X
X	while (c = *es++) {
X		if (c == 0x8e) {	/* SS2 */
X			if (ws)
X				*ws++ = *es | 0x80;
X			es++;
X			n++;
X		} else if (c == 0x8f) {	/* SS3 */
X			c = *es++;
X			if (es)
X				*ws++ = (c << 8) | (*es & 0x7f) | 0x8000;
X			es++;
X			n++;
X		} else if (c & 0x80) {
X			if (ws)
X				*ws++ = (c << 8) | *es | 0x8080;
X			es++;
X			n++;
X		} else {
X			if (ws)
X				*ws++ = c;
X			n++;
X		}
X	}
X	if (ws)
X		*ws = 0;
X
X	return n;
X}
X#endif	/* KCLIENT */
X#endif
END_OF_FILE
if test 12910 -ne `wc -c <'convert.c'`; then
    echo shar: \"'convert.c'\" unpacked with wrong size!
fi
# end of 'convert.c'
fi
echo shar: End of shell archive.
exit 0