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