1 /*
2 * Copyright (c) 1991-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: sjrc.c,v $
32 * $SonyRevision: 1.3 $
33 * $SonyDate: 1997/01/23 11:26:31 $
34 */
35
36
37
38
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <locale.h>
44 #include <ctype.h>
45 #include <sys/types.h>
46 #include "sj3dic.h"
47 #include "sjctype.h"
48 #include "sjtool.h"
49
50 char RCfile[LONGLENGTH];
51
52 static char *rcfile = ".sjrc";
53
54
55 static int user_euc = 0;
56 static int file_code = SYS_SJIS;
57
58 struct functbl funcs[] = {
59 { "dictionary", set_dict },
60 { "userdic", set_dict },
61 { "server", set_server },
62 { NULL, NULL }
63 };
64
65 #define TOLOWER(c) (isupper(c) ? tolower(c) : (c))
66
67 int
sjset_code()68 sjset_code()
69 {
70 char *loc;
71
72 #ifdef LACKOF_SETLOCALE
73 loc = getenv("LANG");
74 #else
75 loc = setlocale(LC_CTYPE, "");
76 #endif
77 if (!loc) {
78 fprintf(stderr, "Error: can't get current locale\n");
79 exit(1);
80 }
81 if (strncmp(loc, "ja_JP.SJIS", 10) == 0) {
82 user_euc = 0;
83 } else if (strncmp(loc, "ja", 2) == 0 ||
84 strncmp(loc, "Ja", 2) == 0) {
85 user_euc = 1;
86 file_code = -1;
87 } else
88 #ifdef __sony_news
89 return 0;
90 #else
91 {
92 fprintf(stderr, "Warrning: In this locale japanese will not be used.\n");
93 user_euc = 1;
94 file_code = -1;
95 }
96 #endif
97 return 1;
98 }
99
100
101 int
getsjrc()102 getsjrc()
103 {
104 FILE *fd, *fopen();
105 char *p;
106 char *getenv();
107
108 sjset_code();
109 RCfile[0] = '\0';
110 if ((p = getenv("SJRC")) != NULL && *p != '\0') {
111 if (*p != '/')
112 snprintf(RCfile, sizeof(RCfile), "%s/%s", home_dir, p);
113 else
114 snprintf(RCfile, sizeof(RCfile), "%s%s", home_dir, p);
115 if ((fd = fopen (RCfile, "r")) != NULL) {
116 setrc (RCfile, fd);
117 fclose (fd);
118 return TRUE;
119 }
120 }
121 if (home_dir[0] != '\0') {
122 snprintf(RCfile, sizeof(RCfile), "%s/%s", home_dir, rcfile);
123 if ((fd = fopen (RCfile, "r")) != NULL) {
124 setrc (RCfile, fd);
125 fclose (fd);
126 return TRUE;
127 }
128 }
129 RCfile[0] = '\0';
130 return FALSE;
131 }
132
133
134 void
setrc(char * file,FILE * fd)135 setrc(char *file, FILE *fd)
136 {
137 char line[MAXLINE];
138 int w;
139 struct wordent word[WORDN];
140 char *p;
141 struct functbl *functp;
142
143 while ((p = fgets (line, MAXLINE, fd)) != NULL) {
144 if ((w = getword(line, word)) <= 0)
145 continue;
146 functp = funcs;
147 p = word[0].word_str;
148 while (functp->keyword != NULL) {
149 if (much(p, functp->keyword)) {
150 if (functp->func) (*(functp->func))(word);
151 break;;
152 }
153 functp++;
154 }
155 }
156 }
157
158
159 int
much(char * s1,char * s2)160 much(char *s1, char *s2)
161 {
162 char c1, c2;
163
164 if (s1 == NULL)
165 return 0;
166 while (*s2 != '\0') {
167 c1 = *s1++;
168 c2 = *s2++;
169 if (TOLOWER(c1) != TOLOWER(c2))
170 return 0;
171 }
172 return 1;
173 }
174
175
176 int
getword(char * s,struct wordent word[])177 getword(char *s, struct wordent word[])
178 {
179 u_char c, cc;
180 char *p;
181 int i, wcount;
182
183 if (IsDelimitor(*s)) {
184 s++;
185 } else {
186 p = s;
187 while(!IsDelimitor(*s)) {
188 if (IsTerminator(*s) || isTerminator(*s))
189 break;
190 s++;
191 }
192 *s++ = '\0';
193 if (much(p, term_name) == 0)
194 return 0;
195 }
196 i = wcount = 0;
197 p = word[wcount].word_str;
198 while (*s != '\0') {
199 c = *s++;
200 if (file_code == SYS_SJIS) {
201 if (issjis1(c) && issjis2(*s)) {
202 if (i < MAXWORD - 2) {
203 *p++ = c;
204 *p++ = *s;
205 i += 2;
206 }
207 s++;
208 continue;
209 }
210 } else if (file_code == SYS_EUC) {
211 if (iseuc(c) && iseuc(*s)) {
212 if (i < MAXWORD - 2) {
213 cc = (c << 8) + (*s & 0xff);
214 cc = euc2sjis(cc);
215 *p++ = (cc >> 8) & 0xff;
216 *p++ = cc & 0xff;
217 i += 2;
218 }
219 s++;
220 continue;
221 }
222 if (iseuckana(c) && iskana2(*s))
223 c = *s++;
224 } else {
225 if (issjis1(c) && issjis2(*s)) {
226 file_code = SYS_SJIS;
227 s--;
228 continue;
229 }
230 if ((iseuc(c) && iseuc(*s)) ||
231 (iseuckana(c) && iskana2(*s))) {
232 file_code = SYS_EUC;
233 s--;
234 continue;
235 }
236 }
237 if (isTerminator(c))
238 break;
239 if (IsDelimitor(c)) {
240 if (i > 0) {
241 if (++wcount >= WORDN - 1)
242 break;
243 *p = '\0';
244 i = 0;
245 p = word[wcount].word_str;
246 }
247 continue;
248 }
249 if (IsEscape(c))
250 c = *s++;
251 if (IsTerminator(c))
252 break;
253 if (i < MAXWORD - 1) {
254 *p++ = c;
255 i++;
256 }
257 }
258 *p = '\0';
259 if (word[wcount].word_str[0] != '\0')
260 word[++wcount].word_str[0] = '\0';
261 return wcount;
262 }
263
264 int
IsTerminator(u_char c)265 IsTerminator(u_char c)
266 {
267 return (c == '\n') ? 1 : 0;
268 }
269
270 int
isTerminator(u_char c)271 isTerminator(u_char c)
272 {
273 return (c == '#') ? 1 : 0;
274 }
275
276 int
IsEscape(u_char c)277 IsEscape(u_char c)
278 {
279 return (c == '\\') ? 1 : 0;
280 }
281
282 int
IsDelimitor(u_char c)283 IsDelimitor(u_char c)
284 {
285 return (c == ' ' || c == '\t' || c == '.') ? 1 : 0;
286 }
287
288 void
set_dict(struct wordent word[])289 set_dict(struct wordent word[])
290 {
291 setdicname(word[1].word_str);
292 }
293
294 void
set_server(struct wordent word[])295 set_server(struct wordent word[])
296 {
297 setsjserv(word[1].word_str);
298 }
299