1 /*
2 * Copyright (c) 1994 Sony Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL SONY CORPORATION BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Except as contained in this notice, the name of Sony Corporation
24 * shall not be used in advertising or otherwise to promote the sale, use
25 * or other dealings in this Software without prior written authorization
26 * from Sony Corporation.
27 *
28 */
29
30 /*
31 * $SonyRCSfile: code.c,v $
32 * $SonyRevision: 1.1 $
33 * $SonyDate: 1994/06/03 08:03:09 $
34 */
35
36
37
38 #include "sj_sysvdef.h"
39 #include "wchar16.h"
40 #include "kctype.h"
41 #if defined(__sony_news) && defined(SVR4)
42 #include "jctype.h"
43 #endif
44 #include "sj2.h"
45 #include "key.h"
46 #include "rk.h"
47
48 #include "sj3.h"
49
50 static u_char cbuf[5];
51 static int cur_code = CODE_EUC;
52 static int ccnt = 0;
53
54 void
codeconv(wchar16_t c,u_int * rkstr)55 codeconv(wchar16_t c, u_int *rkstr)
56 {
57 u_short ccode, i;
58
59 if (c == erase_char) {
60 if (ccnt) {
61 ccnt--;
62 *rkstr++ = erase_char + ERRCODE;
63 } else {
64 *rkstr++ = erase_char;
65 }
66 } else if (iswcntrl(c) || (WcIsKANJI(c) && ccnt == 0)) {
67 *rkstr++ = c;
68 if (iswcntrl(c))
69 ccnt = 0;
70 } else if ((ccode = CheckCcode(c)) > 0) {
71 if (ccnt == 4) {
72 if ((cur_code != CODE_EUC) || (cur_code != CODE_EUC2)) {
73 if ((cur_code == CODE_KUTEN) || (cur_code == CODE_KUTEN2))
74 ccode += 0x2020;
75 ccode = jis2euc(ccode & 0x7f7f);
76 }
77 if ((cur_code == CODE_KUTEN2) ||
78 (cur_code == CODE_JIS2) || (cur_code == CODE_EUC2))
79 ccode &= WcX0212MASK;
80 ccnt--;
81 for (; ccnt > 0; ccnt--)
82 *rkstr++ = erase_char + ERRCODE;
83 *rkstr++ = SetMojilen(4) + ccode;
84 } else
85 *rkstr++ = c + ERRCODE;
86 } else
87 *rkstr++ = ERRBEL;
88 *rkstr = RKEND;
89 }
90
91 u_short
CheckCcode(wchar16_t c)92 CheckCcode(wchar16_t c)
93 {
94 u_char c1;
95 u_short code;
96
97 if (!WcIsASCII(c) || !isxdigit(WcLowByte(c)) ||
98 (cur_code == CODE_KUTEN && !isdigit(WcLowByte(c))))
99 return(0);
100 cbuf[ccnt] = WcLowByte(c);
101 cbuf[ccnt + 1] = '\0';
102
103 if ((cur_code == CODE_KUTEN) || (cur_code == CODE_KUTEN2)) {
104 if (ccnt % 2) {
105 code = (cbuf[ccnt-1] - '0') * 10 + cbuf[ccnt] - '0';
106 if (code < 1 || code > 94)
107 return(0);
108 if (ccnt >= 3)
109 code += ((cbuf[0]-'0') * 10 + cbuf[1]-'0') << 8;
110 } else
111 code = 1;
112 } else {
113 code = (u_short)strtol((char *)cbuf, (char **)NULL, 16);
114 if (ccnt % 2)
115 c1 = code & 0xff;
116 else
117 c1 = code & 0xf;
118 switch(cur_code) {
119 case CODE_SJIS:
120 if (ccnt == 0 && (c1 == 8 || c1 == 9 || c1 >= 0xe))
121 break;
122 if (ccnt == 1 && issjis1(c1))
123 break;
124 if (ccnt == 2 && c1 >= 4)
125 break;
126 if (ccnt == 3 && issjis2(c1))
127 break;
128 return(0);
129 case CODE_EUC:
130 case CODE_EUC2:
131 if (ccnt % 2 && iseuc(c1))
132 break;
133 if (!(ccnt % 2) && (c1 >= 0xa && c1 <= 0xf))
134 break;
135 return(0);
136 case CODE_JIS:
137 case CODE_JIS2:
138 if (ccnt % 2 && isjjis(c1))
139 break;
140 if (!(ccnt % 2) && (c1 >= 2 && c1 <= 7))
141 break;
142 return(0);
143 }
144 }
145 ccnt++;
146 return(code);
147 }
148
149 u_short
strtocode()150 strtocode()
151 {
152 int c;
153 u_short code;
154
155 while (ccnt < 4) {
156 c = inkey();
157 if (AnotherConversion ()) {
158 unget_key(c);
159 ccnt = 0;
160 return(-1);
161 }
162 if ((code = CheckCcode(c)) == 0) {
163 ccnt = 0;
164 beep();
165 return(0);
166 }
167 }
168 ccnt = 0;
169 switch (cur_code) {
170 case CODE_SJIS :
171 return(sjis2jis(code));
172 case CODE_KUTEN :
173 case CODE_KUTEN2:
174 return(code + 0x2020);
175 default :
176 return(code & 0x7f7f);
177 }
178 }
179
180 void
ChangeCode()181 ChangeCode()
182 {
183 if (cur_code == CODE_KUTEN2)
184 cur_code = CODE_SJIS;
185 else {
186 if (current_locale == LC_CTYPE_SHIFTJIS) {
187 if( cur_code == CODE_KUTEN) {
188 cur_code = CODE_SJIS;
189 return;
190 }
191 if(cur_code == CODE_SJIS) {
192 cur_code++;
193 } else {
194 cur_code += 2;
195 }
196 } else
197 cur_code++;
198 }
199 }
200
201 void
SetCode(int code)202 SetCode(int code)
203 {
204 cur_code = code;
205 }
206
207 int
GetCcode()208 GetCcode()
209 {
210 return(cur_code);
211 }
212
213