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: libif.c,v $
32  * $SonyRevision: 1.3 $
33  * $SonyDate: 1996/02/23 11:04:02 $
34  */
35 
36 
37 #include <stdlib.h>
38 #include <string.h>
39 #include "sj_sysvdef.h"
40 #ifdef SVR4
41 #include <sys/euc.h>
42 #endif
43 #include "wchar16.h"
44 #include <locale.h>
45 #include "kctype.h"
46 #include "common.h"
47 
48 #include "sj3.h"
49 
50 
51 int (*len_func[3])() = {0, sjislen, euclen};
52 
53 
54 #define YOMILEN 32
55 #define KLEN    64
56 
57 
58 
59 
60 
61 int
SJ2_henkan(wchar16_t * yomiout,struct bunsetu_sj3 * bun,wchar16_t * kanjiout,int kanjilen)62 SJ2_henkan(wchar16_t *yomiout, struct bunsetu_sj3 *bun, wchar16_t *kanjiout, int kanjilen)
63 {
64 	int retv;
65 	SJ3_BUNSETU sbun[BUFFLENGTH];
66 	int i, sentou, saigo;
67 	u_char yomi[BUFFLENGTH * 3];
68 	u_char kanji[BUFFLENGTH * 6];
69 
70 	(void) wcstombs((char *) yomi, yomiout, BUFFLENGTH*3);
71 	if (current_locale == LC_CTYPE_SHIFTJIS) {
72 		retv = sj3_getkan(yomi, sbun, kanji, BUFFLENGTH * 6);
73 	} else {
74 		retv = sj3_getkan_euc(yomi, sbun, kanji, BUFFLENGTH * 6);
75 	}
76 
77 	if (retv <= 0) return(retv);
78 	(void) mbstowcs(kanjiout, (char *)kanji, kanjilen);
79 
80 	for(i = 0; i < retv; i++) {
81 		sentou = (*len_func[current_locale])(yomi, sbun[i].srcstr - yomi);
82 		saigo  = (*len_func[current_locale])(sbun[i].srcstr, sbun[i].srclen);
83 		bun[i].srclen = saigo;
84 		bun[i].srcstr = yomiout + sentou;
85 		sentou = (*len_func[current_locale])(kanji, sbun[i].deststr - kanji);
86 		saigo  = (*len_func[current_locale])(sbun[i].deststr, sbun[i].destlen);
87 		bun[i].destlen = saigo;
88                 bun[i].deststr = kanjiout + sentou;
89 		(void) memcpy(&(bun[i].dcid), &(sbun[i].dcid), sizeof(sbun[i].dcid));
90 	}
91 
92 	return(retv);
93 }
94 int
sjislen(unsigned char * str,int max)95 sjislen(unsigned char *str, int max)
96 {
97 	int len = 0, bytes = 1;
98 
99 	if (!max) return 0;
100 	while ((bytes <= max) && *str) {
101 		if (issjis1(*str)) {
102 			str++;
103 			bytes++;
104 			if (!*str) break;
105 			len++;
106 		} else {
107 			len++;
108 		}
109 		str++;
110 		bytes++;
111 	}
112 	return len;
113 }
114 
115 int
euclen(unsigned char * str,int max)116 euclen(unsigned char *str, int max)
117 {
118 	int len = 0, bytes = 1;
119 
120 	if (!max) return 0;
121 	while ((bytes <= max) && *str) {
122 		if (*str == SS3) {
123 			str++;
124 			bytes++;
125 			if (!*str) break;
126 			str++;
127 			bytes++;
128 			if (!*str) break;
129 			len++;
130 		} else	if ((*str == SS2) || (*str & 0x80)) {
131 			str++;
132 			bytes++;
133 			if (!*str) break;
134 			len++;
135 		} else {
136 			len++;
137 		}
138 		str++;
139 		bytes++;
140 	}
141 	return len;
142 }
143 
144 
145 
146 int
SJ2_getdouon(wchar16_t * s,struct douon_sj3 * d,int n,int choice,int reconv)147 SJ2_getdouon(wchar16_t *s, struct douon_sj3 *d, int n, int choice, int reconv)
148 {
149 	int i, ret;
150 	SJ3_DOUON d_sjis[DOUON_N];
151 	u_char yomi[BUFFLENGTH * 3];
152 
153 	(void) wcstombs((char *) yomi, s, BUFFLENGTH*3);
154 
155 	if (current_locale == LC_CTYPE_SHIFTJIS) {
156 		ret = sj3_getdouon(yomi, d_sjis);
157 	} else {
158 		ret = sj3_getdouon_euc(yomi, d_sjis);
159 	}
160 	for (i = 0; i < ret; i++) {
161 		(void) memcpy(&(d[i].dcid),
162 			      &(d_sjis[i].dcid), SJ3_WORD_ID_SIZE);
163 		d[i].wlen = (int) mbstowcs(d[i].ddata, (char *)d_sjis[i].ddata,
164 					   SJ3_BUNSETU_KANJI*2);
165 		d[i].dlen = wcbyte(d[i].ddata);
166  	    	d[i].valid = (char)1;
167 	}
168  	if (ret < DOUON_N) {
169  		(void) memset(&(d[ret].dcid), 0, SJ3_WORD_ID_SIZE);
170  		d[ret].wlen = wslen(s);
171  		for (i = 0; i < d[ret].wlen; i++) {
172  			d[ret].ddata[i] = sj_tokata(s[i]);
173  		}
174  		d[ret].ddata[i] = (wchar_t) 0;
175  		d[ret].dlen = wcbyte(s);
176  	    	d[ret].valid = (char)0;
177  	}
178  	return(ret + 1);
179 }
180 
181 void
SJ2_study(struct studyrec * dcid)182 SJ2_study(struct studyrec *dcid)
183 {
184 	sj3_gakusyuu(dcid);
185 }
186 
187 void
SJ2_clstudy(wchar16_t * yomi1,wchar16_t * yomi2,struct studyrec * dcid)188 SJ2_clstudy(wchar16_t *yomi1, wchar16_t *yomi2, struct studyrec *dcid)
189 {
190 	u_char y1[BUFFLENGTH*2];
191 	u_char y2[BUFFLENGTH*2];
192 
193 	(void) wcstombs((char *) y1, yomi1, BUFFLENGTH*2);
194 	(void) wcstombs((char *) y2, yomi2, BUFFLENGTH*2);
195 
196 	if (current_locale == LC_CTYPE_SHIFTJIS) {
197 		sj3_gakusyuu2(y1, y2, dcid);
198 	} else {
199 		sj3_gakusyuu2_euc(y1, y2, dcid);
200 	}
201 }
202 
203 int
SJ2_toroku(wchar16_t * yomi,wchar16_t * kanji,int hinshi)204 SJ2_toroku(wchar16_t *yomi, wchar16_t *kanji, int hinshi)
205 {
206 	u_char  y[(YOMILEN+1)*3];
207         u_char  k[(KLEN+1)*3];
208 	int ret;
209 
210 	(void)wcstombs((char *) y, yomi, (YOMILEN+1)*3);
211 	(void)wcstombs((char *) k, kanji, (KLEN+1)*3);
212 	if (current_locale == LC_CTYPE_SHIFTJIS) {
213 		ret = sj3_touroku (y, k, hinshi);
214 	} else {
215 		ret = sj3_touroku_euc (y, k, hinshi);
216 	}
217 	return(ret);
218 }
219 
220 int
SJ2_syoukyo(wchar16_t * yomi,wchar16_t * kanji,int hinshi)221 SJ2_syoukyo(wchar16_t *yomi, wchar16_t *kanji, int hinshi)
222 {
223 	u_char  y[(YOMILEN+1)*3];
224         u_char  k[(KLEN+1)*3];
225 	int ret;
226 
227 	(void)wcstombs((char *) y, yomi, (YOMILEN+1)*3);
228 	(void)wcstombs((char *) k, kanji, (KLEN+1)*3);
229 	if (current_locale == LC_CTYPE_SHIFTJIS) {
230 		ret = sj3_syoukyo (y, k, hinshi);
231 	} else {
232 		ret = sj3_syoukyo_euc (y, k, hinshi);
233 	}
234 	return(ret);
235 }
236 
237 void
sj_check_error(int err)238 sj_check_error(int err)
239 {
240 	if (err & SJ3_SERVER_DEAD)
241 		SJ_warnning(WCServer_Dead);
242 	if (err & SJ3_ALREADY_CONNECTED)
243 		SJ_warnning(WCConnected);
244 	if (err & SJ3_CONNECT_ERROR)
245 		SJ_warnning(WCNconnect);
246 	if (err & SJ3_CANNOT_OPEN_MDICT)
247 		SJ_warnning(WCNopen_Mdic);
248 	if (err & SJ3_CANNOT_MAKE_UDIR)
249 		SJ_warnning(WCNmake_Udir);
250 	if (err & SJ3_CANNOT_MAKE_UDICT)
251 		SJ_warnning(WCNmake_Udic);
252 	if (err & SJ3_CANNOT_OPEN_UDICT)
253 		SJ_warnning(WCNopen_Udic);
254 	if (err & SJ3_CANNOT_MAKE_STUDY)
255 		SJ_warnning(WCNmake_Study);
256 	if (err & SJ3_CANNOT_OPEN_STUDY)
257 		SJ_warnning(WCNopen_Study);
258 }
259 
260 int
SJ2_reconnect()261 SJ2_reconnect()
262 {
263 	int err;
264 
265         sj3_close();
266 	if (dict_num && dict_list)
267 	  err = sj3_open_with_list(chmyhname(cur_serv), user_name, dict_num, dict_list, NULL, NULL);
268 	else
269 	  err = sj3_open(chmyhname(cur_serv), user_name);
270         return(err);
271 }
272 
273 
274 void
SJ2_henkan_end()275 SJ2_henkan_end()
276 {
277 	sj3_close ();
278 }
279 
280 int
SJ2_henkan_init()281 SJ2_henkan_init()
282 {
283         int i, err;
284 	int *err_dict = NULL;
285 	int  err_num = 0;
286 
287         sj3_set_timeout(5);
288 
289 	if (dict_num && dict_list)
290 	  err = sj3_open_with_list(chmyhname(cur_serv), user_name, dict_num,
291 				   dict_list, &err_num, &err_dict);
292 	else
293 	  err = sj3_open(chmyhname(cur_serv), user_name);
294         i = 0;
295 	while (err && i < MAXSERVER - 1 && serv_list[i + 1] != NULL) {
296 		if (dict_num && dict_list)
297 		  err = sj3_open_with_list(chmyhname(serv_list[++i]), user_name,
298 					   dict_num, dict_list, &err_num, &err_dict);
299 		else
300 		  err = sj3_open(chmyhname(cur_serv), user_name);
301 	}
302 	if (err) {
303 		printf("Warning ");
304 		sj_check_error(err);
305 		printf("\n\r");
306 		set_dcflag(1);
307 		return(1);
308 	} else if (err_num && err_dict) {
309 		printf("Warning ");
310 		for( i = 0; i < err_num; i++) {
311 			printf("%s ", dict_list[err_dict[i]]);
312 		}
313 		printf(" files can't open\n");
314                 cur_serv = serv_list[i];
315                 return (0);
316 	} else {
317                 cur_serv = serv_list[i];
318                 return (0);
319         }
320 }
321 
322 int
sj3_autocon()323 sj3_autocon()
324 {
325 	int i, err;
326 
327 	if (dict_num && dict_list) {
328 		if (!(err = sj3_open_with_list(chmyhname(cur_serv), user_name, dict_num,
329 					       dict_list, NULL, NULL)))
330 		    return(0);
331 	} else {
332 		if (!(err = sj3_open(chmyhname(cur_serv), user_name)))
333 		    return(0);
334 	}
335         i = 0;
336         while (serv_list[i] != NULL && i < MAXSERVER) {
337                 if (strcmp(cur_serv, serv_list[i]) != 0) {
338 			if (dict_num && dict_list) {
339 				err = sj3_open_with_list(chmyhname(serv_list[i]), user_name,
340 							 dict_num, dict_list, NULL, NULL);
341 			} else {
342 				err = sj3_open(chmyhname(cur_serv), user_name);
343 			}
344                         if (!err) {
345                                 cur_serv = serv_list[i];
346                                 break;
347                         }
348                 }
349                 i++;
350         }
351         return(err);
352 }
353