comparison Wnn/uum/prologue.c @ 0:bbc77ca4def5

initial import
author Yoshiki Yazawa <yaz@cc.rim.or.jp>
date Thu, 13 Dec 2007 04:30:14 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bbc77ca4def5
1 /*
2 * $Id: prologue.c,v 1.6 2002/05/12 22:51:17 hiroo Exp $
3 */
4
5 /*
6 * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
7 * This file is part of FreeWnn.
8 *
9 * Copyright Kyoto University Research Institute for Mathematical Sciences
10 * 1987, 1988, 1989, 1990, 1991, 1992
11 * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
12 * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991, 1992
13 * Copyright FreeWnn Project 1999, 2000, 2002
14 *
15 * Maintainer: FreeWnn Project <freewnn@tomo.gr.jp>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 */
31
32 /* Standard Interface
33 * Initialize Routine
34 */
35 #ifdef HAVE_CONFIG_H
36 # include <config.h>
37 #endif
38
39 #include <stdio.h>
40 #include <ctype.h>
41 #if STDC_HEADERS
42 # include <stdlib.h>
43 # include <string.h>
44 #else
45 # if HAVE_MALLOC_H
46 # include <malloc.h>
47 # endif
48 # if HAVE_STRINGS_H
49 # include <strings.h>
50 # endif
51 #endif /* STDC_HEADERS */
52 #include <sys/errno.h>
53 #include <pwd.h>
54 #ifdef HAVE_UNISTD_H
55 # include <unistd.h>
56 #endif
57 #include "jllib.h"
58 #include "commonhd.h"
59 #include "wnn_config.h"
60 #include "buffer.h"
61 #include "rk_spclval.h"
62 #include "sdefine.h"
63 #include "sheader.h"
64 #include "wnn_os.h"
65
66
67 /** romkan のイニシャライズ */
68 int
69 open_romkan ()
70 {
71 char name[PATHNAMELEN];
72 int rk_option_flg;
73
74 romkan_set_lang (lang_dir);
75 if (*rkfile_name_in_uumrc != '\0')
76 {
77 strcpy (name, rkfile_name_in_uumrc);
78 }
79 else
80 {
81 strcpy (name, LIBDIR);
82 strcat (name, RKFILE);
83 }
84 rk_option_flg = RK_REDRAW | RK_CHMOUT | (excellent_delete ? 0 : RK_SIMPLD) | (verbose_option ? RK_VERBOS : 0);
85
86 return (romkan_init5 (name, rubout_code, NULL, NULL, rk_option_flg) == 0 ? 0 : -1);
87 /* romaji-kana henkan junbi */
88 }
89
90
91 /** 漢字かな変換用 envrc ファイルのオープン */
92 int
93 get_envrc_name_reverse (str)
94 char *str;
95 {
96 if (cur_reverse_env == NULL || *reverse_envrcname == 0)
97 return (0);
98 strcpy (str, reverse_envrcname);
99 if (access (str, R_OK) == -1)
100 {
101 return (-1);
102 }
103 return (1);
104 }
105
106
107 /** envrc ファイルのオープン */
108 int
109 get_envrc_name (str)
110 char *str;
111 {
112 if (cur_normal_env == NULL || envrcname[0] == '\0')
113 {
114 strcpy (str, LIBDIR);
115 strcat (str, "/");
116 strcat (str, lang_dir);
117 strcat (str, ENVRCFILE);
118 }
119 else
120 {
121 strcpy (str, envrcname);
122 }
123 return (1);
124 }
125
126 /* uum で必要なエリアをアロケートする。*/
127 int
128 allocate_areas ()
129 {
130 char *area_start;
131 char *area_pter;
132
133 int k;
134 int total_size;
135 int len1 = maxchg * sizeof (w_char);
136 int return_buf_len = len1 * 7;
137
138 total_size = return_buf_len + len1 * 3 + maxbunsetsu * sizeof (int) * 3 + maxbunsetsu * sizeof (struct wnn_env *);
139
140 if ((area_start = malloc (total_size)) == NULL)
141 {
142 /*
143 fprintf(stderr,"初期化でmalloc不能。\n");
144 */
145 fprintf (stderr, MSG_GET (2));
146 return (-1);
147 }
148
149 area_pter = area_start;
150 return_buf = (w_char *) area_pter; /* mojiretsu -> return_buf */
151 area_pter += return_buf_len;
152 input_buffer = (w_char *) area_pter;
153 area_pter += len1;
154 kill_buffer = (w_char *) area_pter;
155 area_pter += len1;
156 remember_buf = (w_char *) area_pter;
157 *remember_buf = 0;
158 area_pter += len1;
159 bunsetsu = (int *) area_pter;
160 area_pter += maxbunsetsu * sizeof (int);
161 bunsetsuend = (int *) area_pter;
162 area_pter += maxbunsetsu * sizeof (int);
163 bunsetsu_env = (struct wnn_env **) area_pter;
164 area_pter += maxbunsetsu * sizeof (int);
165 touroku_bnst = (int *) area_pter;
166 area_pter += maxbunsetsu * sizeof (int);
167 for (k = 0; k < maxbunsetsu; k++)
168 {
169 bunsetsu[k] = 0;
170 bunsetsuend[k] = 0;
171 touroku_bnst[k] = 0;
172 }
173
174 return (0);
175 }
176
177
178 /* henkan initialize */
179 /** uum のイニシャライズ */
180 int
181 init_uum ()
182 {
183 char *p;
184
185 uumrc_get_entries ();
186
187 if (*(p = convkey_name_in_uumrc) == '\0')
188 p = NULL;
189 if (convert_key_setup (p, (verbose_option != 0)) == -1)
190 return (-1);
191
192 if (init_key_table () == -1)
193 {
194 return (-1);
195 }
196
197 if (open_romkan () == -1)
198 {
199 return (-1);
200 }
201
202 if (allocate_areas () == -1)
203 {
204 return (-1);
205 }
206
207 if (init_history () == -1)
208 {
209 return (-1);
210 }
211
212 connect_jserver (0);
213
214 throw_cur_raw (0, crow + conv_lines);
215 if (keypad_fun)
216 set_keypad_on ();
217 scroll_up ();
218 set_scroll_region (0, crow - 1);
219 throw_cur_raw (0, crow - 1);
220 flush ();
221
222 return (0);
223 }
224
225 static void
226 puts1 (s)
227 char *s;
228 {
229 printf ("%s\n", s);
230 flush ();
231 }
232
233 static void
234 puts2 (s)
235 char *s;
236 {
237 throw_c (0);
238 clr_line ();
239 printf ("%s", s);
240 flush ();
241 }
242
243 static int
244 yes_or_no_init (s)
245 char *s;
246 {
247 for (;;)
248 {
249 char x[256];
250 printf (s);
251 flush ();
252 if (fgets (x, 256, stdin) == NULL)
253 {
254 return (0);
255 }
256 if (*x == 'n' || *x == 'N')
257 return (0);
258 if (*x == 'y' || *x == 'Y')
259 return (1);
260 }
261 }
262
263 /** Connecting to jserver */
264 int
265 connect_jserver (first)
266 int first;
267 {
268 char uumrc_name[PATHNAMELEN];
269 char environment[PATHNAMELEN];
270 WnnEnv *p;
271 WnnEnv *save_cur_normal_env = NULL, *save_cur_reverse_env = NULL;
272 int (*yes_no_func) ();
273 void (*puts_func) ();
274
275 if (first == 0)
276 {
277 bun_data_ = NULL;
278 yes_no_func = yes_or_no_init;
279 puts_func = puts1;
280 }
281 else
282 {
283 yes_no_func = yes_or_no;
284 puts_func = puts2;
285 }
286 get_envrc_name (uumrc_name); /* use username for env-name */
287
288 if (cur_normal_env)
289 save_cur_normal_env = cur_normal_env;
290 for (p = normal_env; p; p = p->next)
291 {
292 cur_normal_env = p;
293 get_envrc_name (uumrc_name);
294 strcpy (environment, username);
295 strcat (environment, p->env_name_str);
296 if (servername == NULL && def_servername && *def_servername)
297 {
298 servername = malloc (strlen (def_servername) + 1);
299 strcpy (servername, def_servername);
300 }
301 if (bun_data_ == NULL)
302 {
303 bun_data_ = jl_open_lang (environment, servername, lang_dir, uumrc_name, yes_no_func, puts_func, WNN_TIMEOUT);
304 if (bun_data_ != NULL && bun_data_->env != NULL)
305 {
306 env_normal = bun_data_->env;
307 }
308 }
309 else if (env_normal == 0 || jl_isconnect_e (env_normal) == 0)
310 {
311 env_normal = jl_connect_lang (environment, servername, lang_dir, uumrc_name, yes_no_func, puts_func, WNN_TIMEOUT);
312 }
313 if (env_normal)
314 {
315 if (normal_sticky)
316 jl_env_sticky_e (env_normal);
317 else
318 jl_env_un_sticky_e (env_normal);
319 }
320 }
321 if (save_cur_normal_env)
322 {
323 cur_normal_env = save_cur_normal_env;
324 if (env_normal)
325 jl_env_set (bun_data_, env_normal);
326 }
327 if (normal_env == NULL)
328 {
329 get_new_env (0);
330 cur_normal_env = normal_env;
331 uumrc_name[0] = '\0';
332 bun_data_ = jl_open_lang (username, servername, lang_dir, uumrc_name, yes_no_func, puts_func, WNN_TIMEOUT);
333 }
334
335 if (cur_reverse_env)
336 save_cur_reverse_env = cur_reverse_env;
337 for (p = reverse_env; p; p = p->next)
338 {
339 cur_reverse_env = p;
340 if (get_envrc_name_reverse (uumrc_name) > 0)
341 {
342 strcpy (environment, username);
343 strcat (environment, p->env_name_str);
344 strcat (environment, "R");
345 if (reverse_servername == NULL && def_reverse_servername && *def_reverse_servername)
346 {
347 reverse_servername = malloc (strlen (def_reverse_servername) + 1);
348 strcpy (reverse_servername, def_reverse_servername);
349 }
350 env_reverse = jl_connect_lang (environment, reverse_servername, lang_dir, uumrc_name, yes_no_func, puts_func, WNN_TIMEOUT);
351 if (env_reverse)
352 {
353 if (reverse_sticky)
354 jl_env_sticky_e (env_reverse);
355 if (!reverse_sticky)
356 jl_env_un_sticky_e (env_reverse);
357 }
358 }
359 }
360 if (save_cur_reverse_env)
361 cur_reverse_env = save_cur_reverse_env;
362 return (0);
363 }
364
365
366 /** 立ち上がり時にメッセージファイルが存在すればそれを表示する。*/
367 int
368 initial_message_out ()
369 {
370 FILE *fp;
371 char data[1024];
372 char fname[PATHNAMELEN];
373
374 strcpy (fname, LIBDIR);
375 strcat (fname, MESSAGEFILE);
376
377 if ((fp = fopen (fname, "r")) != NULL)
378 {
379 while (fgets (data, 1024, fp) != NULL)
380 {
381 fprintf (stderr, "\r%s", data);
382 }
383 fclose (fp);
384 fprintf (stderr, "\r\n");
385 }
386 return (1);
387 }