0
|
1 /*
|
|
2 * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
|
|
3 * This file is part of FreeWnn.
|
|
4 *
|
|
5 * Copyright Kyoto University Research Institute for Mathematical Sciences
|
|
6 * 1987, 1988, 1989, 1990, 1991, 1992
|
|
7 * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
|
|
8 * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991, 1992
|
|
9 * Copyright FreeWnn Project 1999, 2000, 2002, 2003
|
|
10 *
|
|
11 * Maintainer: FreeWnn Project <freewnn@tomo.gr.jp>
|
|
12 *
|
|
13 * This program is free software; you can redistribute it and/or modify
|
|
14 * it under the terms of the GNU General Public License as published by
|
|
15 * the Free Software Foundation; either version 2 of the License, or
|
|
16 * (at your option) any later version.
|
|
17 *
|
|
18 * This program is distributed in the hope that it will be useful,
|
|
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 * GNU General Public License for more details.
|
|
22 *
|
|
23 * You should have received a copy of the GNU General Public License
|
|
24 * along with this program; if not, write to the Free Software
|
|
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
26 */
|
|
27
|
|
28 #if defined(HAVE_CONFIG_H)
|
|
29 #include <config.h>
|
|
30 #endif
|
|
31
|
|
32 #include <stdio.h>
|
|
33 #include <ctype.h>
|
|
34 #if STDC_HEADERS
|
|
35 # include <stdlib.h>
|
|
36 # include <string.h>
|
|
37 #else
|
|
38 # if HAVE_MALLOC_H
|
|
39 # include <malloc.h>
|
|
40 # endif
|
|
41 # if HAVE_STRINGS_H
|
|
42 # include <strings.h>
|
|
43 # endif /* HAVE_STRINGS_H */
|
|
44 #endif /* STDC_HEADERS */
|
|
45 #include <sys/types.h>
|
|
46
|
|
47 #include "commonhd.h"
|
|
48 #include "de_header.h"
|
|
49 #include "wnn_config.h"
|
|
50 #include "wnn_os.h"
|
|
51 #include "kaiseki.h"
|
|
52
|
|
53 struct wnn_param default_para = {
|
|
54 2, 10,
|
|
55 /* N 小文節数 */
|
|
56 2, 34, 5, 80, 5, 1, 20, 0,
|
|
57 /* 頻度 小文節長 幹語長 今使ったよ 辞書 小文節評価 大文節長 小文節数 */
|
|
58 400, -100, 400, 80, 200, 2, 200
|
|
59 /* 数字 カナ 英数 記号 閉括弧 付属語 開括弧 */
|
|
60 };
|
|
61
|
|
62 static int expand_expr (char *s);
|
|
63 static int read_default_file (char *buffer, size_t buffer_size);
|
|
64 #ifndef CHINESE
|
|
65 static int expand_argument (unsigned char *st);
|
|
66 static int get_bcksla (char *st);
|
|
67 #endif /* !CHINESE */
|
|
68
|
|
69 static int
|
|
70 expand_expr (char *s)
|
|
71 /** @LIBDIRの展開(但し、文字列の先頭のみ)。できない時は-1が
|
|
72 返り、その場合sの中身は着々とそのまんま。sの長さ<256と仮定してる。*/
|
|
73 /** @USR (env名、logname), @LANG の展開 */
|
|
74 {
|
|
75 char tmp[1+EXPAND_PATH_LENGTH];
|
|
76 register char *p, *s1;
|
|
77 int noerr, expandsuc;
|
|
78
|
|
79 if (*s != '@')
|
|
80 {
|
|
81 strcpy (tmp, s);
|
|
82 *s = '\0';
|
|
83 noerr = 1;
|
|
84 }
|
|
85 else
|
|
86 {
|
|
87 if ((int) strlen (s) >= EXPAND_PATH_LENGTH)
|
|
88 return (-1);
|
|
89
|
|
90 s1 = s;
|
|
91 if (NULL != (p = strchr (++s1, '/')))
|
|
92 {
|
|
93 strcpy (tmp, p);
|
|
94 *p = '\0';
|
|
95 }
|
|
96 else
|
|
97 *tmp = '\0';
|
|
98 /* ここまでは準備。s…先頭、s1…2文字目、p…最初の'/'のあったところ
|
|
99 (ここで一旦切る)、tmp…それ以後のコピー。 */
|
|
100
|
|
101 if (!strcmp (s1, "LIBDIR"))
|
|
102 {
|
|
103 noerr = expandsuc = ((int) (strlen (p = LIBDIR) + strlen (tmp)) < EXPAND_PATH_LENGTH);
|
|
104 }
|
|
105 else
|
|
106 { /* @LIBDIR igai ha kaenai */
|
|
107 noerr = 1;
|
|
108 expandsuc = 0;
|
|
109 }
|
|
110 if (expandsuc)
|
|
111 strcpy (s, p);
|
|
112 }
|
|
113
|
|
114 if (noerr)
|
|
115 {
|
|
116 int len1 = strlen ("@LANG");
|
|
117 p = tmp;
|
|
118 for (; *p; p++)
|
|
119 {
|
|
120 if (!strncmp (p, "@LANG", len1))
|
|
121 {
|
|
122 if ((int) (strlen (lang_dir) + strlen (p) + strlen (s) - len1) < EXPAND_PATH_LENGTH)
|
|
123 {
|
|
124 strcat (s, lang_dir);
|
|
125 p += len1 - 1;
|
|
126 }
|
|
127 else
|
|
128 {
|
|
129 return (-1);
|
|
130 }
|
|
131 }
|
|
132 else
|
|
133 {
|
|
134 strncat (s, p, 1);
|
|
135 }
|
|
136 }
|
|
137 }
|
|
138 return (noerr ? 0 : -1);
|
|
139 }
|
|
140
|
|
141 /* daemon initialize routine */
|
|
142
|
|
143 /* Read [cjkt]server configuration file and set the parameters. */
|
|
144 /* RETURN VALUE: 0: success; -1: failure */
|
|
145 int
|
|
146 read_default (void)
|
|
147 {
|
|
148 static const char sep[] = " \t";
|
|
149 FILE *fp;
|
|
150 char data[EXPAND_PATH_LENGTH +1];
|
|
151 char code[EXPAND_PATH_LENGTH +1];
|
|
152 char param[EXPAND_PATH_LENGTH +1];
|
|
153 char *word;
|
|
154 int i, num, *v[17];
|
|
155
|
|
156 strcpy (jserver_dir, JSERVER_DIR);
|
|
157
|
|
158 if ((fp = fopen (jserverrcfile, "r")) == NULL)
|
|
159 {
|
|
160 log_err ("can't open %s\n", jserverrcfile);
|
|
161 return (-1);
|
|
162 }
|
|
163
|
|
164 /* copy the pointer for looping */
|
|
165 v[0] = &default_para.n;
|
|
166 v[1] = &default_para.nsho;
|
|
167 v[2] = &default_para.p1;
|
|
168 v[3] = &default_para.p2;
|
|
169 v[4] = &default_para.p3;
|
|
170 v[5] = &default_para.p4;
|
|
171 v[6] = &default_para.p5;
|
|
172 v[7] = &default_para.p6;
|
|
173 v[8] = &default_para.p7;
|
|
174 v[9] = &default_para.p8;
|
|
175 v[10] = &default_para.p9;
|
|
176 v[11] = &default_para.p10;
|
|
177 v[12] = &default_para.p11;
|
|
178 v[13] = &default_para.p12;
|
|
179 v[14] = &default_para.p13;
|
|
180 v[15] = &default_para.p14;
|
|
181 v[16] = &default_para.p15;
|
|
182
|
|
183 while (fgets (data, EXPAND_PATH_LENGTH, fp) != NULL)
|
|
184 {
|
|
185 num = sscanf (data, "%s", code);
|
|
186 if (num <= 0)
|
|
187 {
|
|
188 /* no command on the line */
|
|
189 continue;
|
|
190 }
|
|
191 code[EXPAND_PATH_LENGTH] = '\0';
|
|
192 if (code[0] == ';')
|
|
193 {
|
|
194 /* the line is commented out */
|
|
195 continue;
|
|
196 }
|
|
197
|
|
198 /*
|
|
199 if(strcmp(code, "jt_len") == 0){
|
|
200 jt_len = atoi(s[0]);
|
|
201 }else if(strcmp(code, "hjt_len") == 0){
|
|
202 hjt_len = atoi(s[0]);
|
|
203 }
|
|
204 */
|
|
205
|
|
206 if (strcmp (code, "max_client") == 0)
|
|
207 {
|
|
208 num = sscanf (data, "%s %d ", code, &max_client);
|
|
209 if (num != 2)
|
|
210 {
|
|
211 log_err ("command %s invalid.", code);
|
|
212 continue;
|
|
213 }
|
|
214 log_debug ("max_client=%d", max_client);
|
|
215 }
|
|
216 else if (strcmp (code, "max_sticky_env") == 0)
|
|
217 {
|
|
218 num = sscanf (data, "%s %d ", code, &max_sticky_env);
|
|
219 if (num != 2)
|
|
220 {
|
|
221 log_err ("command %s invalid.", code);
|
|
222 continue;
|
|
223 }
|
|
224 log_debug ("max_sticky_env=%d", max_sticky_env);
|
|
225 }
|
|
226 else if (strcmp ((code + 1), "server_dir") == 0)
|
|
227 {
|
|
228 num = sscanf (data , "%s %s", code, param);
|
|
229 if (num != 2)
|
|
230 {
|
|
231 log_err ("command %s invalid.", code);
|
|
232 continue;
|
|
233 }
|
|
234 if (expand_expr (param) != 0)
|
|
235 {
|
|
236 log_err ("command %s: can't expand %s\n", code, param);
|
|
237 }
|
|
238 strcpy (jserver_dir, param);
|
|
239 log_debug ("jserver_dir=%s", jserver_dir);
|
|
240 }
|
|
241 else if (strcmp (code, "def_param") == 0)
|
|
242 {
|
|
243 word = strtok (data, sep); /* discard first word "def_param" */
|
|
244 for (i = 0; (i <= 16); i++) {
|
|
245 word = strtok (NULL, sep);
|
|
246 if (word == NULL)
|
|
247 {
|
|
248 log_err ("command %s has only %d parameters.", code, i);
|
|
249 return (-1);
|
|
250 }
|
|
251 *v[i] = atoi (word); /* XXX: default to 0 if error */
|
|
252 }
|
|
253 log_debug ("command %s has %d parameters", code, i);
|
|
254 }
|
|
255 #ifndef CHINESE
|
|
256 /* else if (strcmp (code, "set_giji_eisuu") == 0 && num >= 2) */
|
|
257 else if (strcmp (code, "set_giji_eisuu") == 0)
|
|
258 {
|
|
259 word = strtok (data, sep); /* discard first word "set_giji_eisuu" */
|
|
260 for (i = 0; (word || i < 20); i++)
|
|
261 {
|
|
262 word = strtok (NULL, sep);
|
|
263 if (word == NULL)
|
|
264 {
|
|
265 break;
|
|
266 }
|
3
|
267 giji_eisuu[i] = expand_argument ((unsigned char *)word);
|
0
|
268 }
|
|
269 log_debug ("command %s has %d parameters.", code, i);
|
|
270 for (; i < 20; i++)
|
|
271 {
|
|
272 giji_eisuu[i] = 0xffff;
|
|
273 }
|
|
274 }
|
|
275 #endif
|
|
276 }
|
|
277 fclose (fp);
|
|
278 return (0);
|
|
279 }
|
|
280
|
|
281 int
|
|
282 read_default_files (void)
|
|
283 {
|
|
284 FILE *fp;
|
|
285 char data[EXPAND_PATH_LENGTH+1];
|
|
286 int num;
|
|
287 char code[EXPAND_PATH_LENGTH+1];
|
|
288 char file[EXPAND_PATH_LENGTH+1];
|
|
289
|
|
290 if ((fp = fopen (jserverrcfile, "r")) == NULL)
|
|
291 {
|
|
292 perror ("");
|
|
293 printf ("Error can't open %s\n", jserverrcfile);
|
|
294 return (-1);
|
|
295 }
|
|
296 while (fgets (data, 256, fp) != NULL)
|
|
297 {
|
|
298 num = sscanf (data, "%s %s", code, file);
|
|
299 if (strcmp (code, "readfile") == 0 && num == 2)
|
|
300 {
|
|
301 read_default_file (file, EXPAND_PATH_LENGTH+1);
|
|
302 }
|
|
303 }
|
|
304 fclose (fp);
|
|
305
|
|
306 printf ("Finished Reading Files\n");
|
|
307
|
|
308 return (0);
|
|
309 }
|
|
310
|
|
311 static int
|
|
312 read_default_file (char* buffer, size_t buffer_size)
|
|
313 {
|
|
314 int fid;
|
|
315
|
|
316 buffer = expand_file_name (buffer, buffer_size);
|
|
317 if (!buffer)
|
|
318 {
|
|
319 log_err ("read_default_file: filename too long. %s", buffer);
|
|
320 return (-1);
|
|
321 }
|
|
322
|
|
323 fid = file_loaded (buffer);
|
|
324 if (fid == -1)
|
|
325 { /* Not correct file */
|
|
326 printf ("Error reading %s\n", buffer);
|
|
327 return (-1);
|
|
328 }
|
|
329 if (FILE_NOT_READ != fid)
|
|
330 { /* already read */
|
|
331 return (-1);
|
|
332 }
|
|
333 if ((fid = get_new_fid ()) == -1)
|
|
334 { /* no more file */
|
|
335 printf ("Error reading %s\n", buffer);
|
|
336 return (-1);
|
|
337 }
|
|
338
|
|
339 files[fid].localf = LOCAL;
|
|
340 strcpy (files[fid].name, buffer);
|
|
341 printf ("Reading %s\t Fid = %d\n", buffer, fid);
|
|
342 if (read_file (&files[fid]) == -1)
|
|
343 {
|
|
344 printf ("Error reading %s\n", buffer);
|
|
345 files[fid].ref_count = -1; /* fail */
|
|
346 return (-1);
|
|
347 }
|
|
348 files[fid].ref_count = 1;
|
|
349 return (fid);
|
|
350 }
|
|
351
|
|
352 #ifndef CHINESE
|
|
353 static int
|
|
354 expand_argument (unsigned char *st)
|
|
355 {
|
|
356 int num;
|
|
357
|
|
358 if (*st == '^')
|
|
359 {
|
|
360 return ((*(st + 1)) & 0x1f);
|
|
361 }
|
|
362 else if (*st == '\\')
|
|
363 {
|
3
|
364 return (get_bcksla ((char *)(st + 1)));
|
0
|
365 }
|
|
366 else if (*st == '0')
|
|
367 {
|
|
368 if (*(st + 1) == 'x' || *(st + 1) == 'X')
|
|
369 {
|
|
370 sscanf ((char *) (st + 2), "%x", &num);
|
|
371 return (num);
|
|
372 }
|
|
373 else
|
|
374 {
|
|
375 sscanf ((char *) st, "%o", &num);
|
|
376 return (num);
|
|
377 }
|
|
378 }
|
|
379 else if (*st == '\'')
|
|
380 {
|
|
381 if (*(st + 1) < 0x80)
|
|
382 return (*(st + 1));
|
|
383 else
|
|
384 return (*(st + 1) * 0x100 + *(st + 2));
|
|
385 }
|
|
386 else if (!('0' <= *st && *st <= '9'))
|
|
387 {
|
|
388 return (-1);
|
|
389 }
|
|
390 else
|
|
391 {
|
3
|
392 return (atoi ((char *)st));
|
0
|
393 }
|
|
394 }
|
|
395
|
|
396 static int
|
|
397 get_bcksla (char *st)
|
|
398 {
|
|
399 int num;
|
|
400
|
|
401 switch (*st)
|
|
402 {
|
|
403 case 'n':
|
|
404 return ('\n');
|
|
405 case 't':
|
|
406 return ('\t');
|
|
407 case 'b':
|
|
408 return ('\b');
|
|
409 case 'r':
|
|
410 return ('\r');
|
|
411 case 'f':
|
|
412 return ('\f');
|
|
413 case 'x':
|
|
414 case 'X':
|
|
415 sscanf (st + 1, "%x", &num);
|
|
416 return (num);
|
|
417 case 'd':
|
|
418 case 'D':
|
|
419 return (atoi (st + 1));
|
|
420 case 'o':
|
|
421 case 'O':
|
|
422 sscanf (st + 1, "%o", &num);
|
|
423 return (num);
|
|
424 }
|
|
425 return (-1);
|
|
426 }
|
|
427 #endif /* !CHINESE */
|
|
428
|