1 /*
2  * $Id: wnnrc_op.c,v 1.2 2001/06/14 18:16:18 ura 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 1991, 1992 by Massachusetts Institute of Technology
14  *
15  * Author: OMRON SOFTWARE Co., Ltd. <freewnn@rd.kyoto.omronsoft.co.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, or (at your option)
20  * 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 GNU Emacs; see the file COPYING.  If not, write to the
29  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30  *
31  * Commentary:
32  *
33  * Change log:
34  *
35  * Last modified date: 8,Feb.1999
36  *
37  * Code:
38  *
39  */
40 /*      Version 4.0
41  */
42 /* uumrc operations */
43 
44 #include "stdio.h"
45 #include <pwd.h>
46 #include "commonhd.h"
47 #include "config.h"
48 #include "sdefine.h"
49 #include "xim.h"
50 #include "sheader.h"
51 #include "ext.h"
52 #include "rk_spclval.h"
53 #include "rk_fundecl.h"
54 
55 extern char *getenv ();
56 extern FILE *fopen ();
57 
58 static char open_uumrc_filenm[1024];
59 
60 static void
err_expand(f,s)61 err_expand (f, s)
62      char *f, *s;
63 {
64   print_out2 ("In uumrc file \"%s\", I could not expand %s.", f, s);
65 }
66 
67 static int
change_ascii_to_int(st,dp)68 change_ascii_to_int (st, dp)
69      char *st;
70      int *dp;
71 {
72   register int total, flag;
73 
74   total = 0;
75   flag = 0;
76   while (*st != '\0')
77     {
78       if (isdigit (*st))
79         {
80           total = total * 10 + (*st - '0');
81         }
82       else if (*st == '+')
83         {
84           if (flag != 0)
85             {
86               return (-1);
87             }
88           flag = 1;
89         }
90       else if (*st == '-')
91         {
92           if (flag != 0)
93             {
94               return (-1);
95             }
96           flag = -1;
97         }
98       else
99         {
100           return (-1);
101         }
102       st++;
103     }
104   if (flag == 0)
105     {
106       flag = 1;
107     }
108   *dp = total * flag;
109   return (1);
110 }
111 
112 /*
113  * set parameter
114  */
115 static int
uumrc_set_entry(data,ld,rk_pre_load,root)116 uumrc_set_entry (data, ld, rk_pre_load, root)
117      char *data;
118      XIMLangDataBase *ld;
119      int rk_pre_load, root;
120 {
121   int num, d1;
122   char code[256];
123   char s[7][EXPAND_PATH_LENGTH];
124   char tmp[1024];
125   FILE *fp;
126   char *s0or1;
127   register char **dst1, **dst2, *src1, *src2;
128   register int copy = 0;
129 
130   /*
131    * get one list
132    */
133   if (comment_char (*data) || (num = sscanf (data, "%s %s %s %s %s %s %s %s", code, s[0], s[1], s[2], s[3], s[4], s[5], s[6])) <= 0)
134     return (0);
135 
136   if (strcmp (code, "include") == 0)
137     {
138       if (expand_expr (s[0], ld->lang) != 0)
139         {
140           err_expand (open_uumrc_filenm, s[0]);
141         }
142       if ((fp = fopen (s[0], "r")) != NULL)
143         {
144           while (fgets (tmp, 1024, fp) != NULL)
145             {
146               if (uumrc_set_entry (tmp, ld, rk_pre_load, root) == -1)
147                 {
148                   fclose (fp);
149                   return (-1);
150                 }
151             }
152           fclose (fp);
153         }
154       else
155         {
156           print_out2 ("Could not open uumrc \"%s\" included in \"%s\".", s[0], open_uumrc_filenm);
157         }
158     }
159   else if (strcmp (code, "setrkfile") == 0)
160     {
161       if (!(rkfile_defined_by_option && (rk_pre_load || root) && ld == cur_lang) && (num > 1))
162         {
163           if (expand_expr (s[0], ld->lang) != 0)
164             {
165               err_expand (open_uumrc_filenm, s[0]);
166             }
167           copy++;
168           dst1 = &ld->rkfile_name;
169           src1 = s[0];
170         }
171     }
172   else if (rk_pre_load == 0)
173     {
174       if (strcmp (code, "setuumkey") == 0)
175         {
176           if (num > 1)
177             {
178               if (ld->uumkey_name)
179                 Free (ld->uumkey_name);
180               ld->uumkey_name = NULL;
181               if (expand_expr (s[0], ld->lang) != 0)
182                 {
183                   err_expand (open_uumrc_filenm, s[0]);
184                 }
185               copy++;
186               dst1 = &ld->uumkey_name;
187               src1 = s[0];
188             }
189         }
190       else if (strcmp (code, "setconvenv") == 0)
191         {
192           if (num > 1)
193             {
194               if (get_new_env (NULL, ld, 0) < 0)
195                 goto ERROR_RET;
196               if (num > 2)
197                 {
198                   if (!(strcmp (s[num - 2], "sticky")))
199                     {
200                       ld->normal_env->sticky = 1;
201                       num--;
202                     }
203                 }
204               if (num == 2)
205                 s0or1 = s[0];
206               else
207                 {
208                   s0or1 = s[1];
209                   if (def_servername && *def_servername)
210                     src2 = def_servername;
211                   else
212                     src2 = s[0];
213                   copy++;
214                   dst2 = &ld->normal_env->host_name;
215                 }
216               if (expand_expr (s0or1, ld->lang) != 0)
217                 {
218                   err_expand (open_uumrc_filenm, s0or1);
219                 }
220               copy++;
221               dst1 = &ld->normal_env->envrc_name;
222               src1 = s0or1;
223             }
224         }
225       else if (strcmp (code, "setkankanaenv") == 0)
226         {
227           if (num > 1)
228             {
229               if (get_new_env (NULL, ld, 1) < 0)
230                 goto ERROR_RET;
231               if (num > 2)
232                 {
233                   if (!(strcmp (s[num - 2], "sticky")))
234                     {
235                       ld->reverse_env->sticky = 1;
236                       num--;
237                     }
238                 }
239               if (num == 2)
240                 s0or1 = s[0];
241               else
242                 {
243                   s0or1 = s[1];
244                   if (def_reverse_servername && *def_reverse_servername)
245                     src2 = def_reverse_servername;
246                   else
247                     src2 = s[0];
248                   copy++;
249                   dst2 = &ld->reverse_env->host_name;
250                 }
251               if (expand_expr (s0or1, ld->lang) != 0)
252                 {
253                   err_expand (open_uumrc_filenm, s0or1);
254                 }
255               copy++;
256               dst1 = &ld->reverse_env->envrc_name;
257               src1 = s0or1;
258             }
259         }
260       else if (strcmp (code, "setenv") == 0)
261         {
262           if (num > 2)
263             {
264               if (get_new_env (NULL, ld, 0) < 0)
265                 goto ERROR_RET;
266               if (!(strcmp (s[num - 3], "sticky")))
267                 {
268                   ld->normal_env->sticky = 1;
269                   num--;
270                 }
271               if (num == 3)
272                 s0or1 = s[1];
273               else
274                 {
275                   s0or1 = s[2];
276                   if (def_servername && *def_servername)
277                     src2 = def_servername;
278                   else
279                     src2 = s[1];
280                   copy++;
281                   dst2 = &ld->normal_env->host_name;
282                 }
283               if (expand_expr (s0or1, ld->lang) != 0)
284                 {
285                   err_expand (open_uumrc_filenm, s0or1);
286                 }
287               copy++;
288               dst1 = &ld->normal_env->envrc_name;
289               src1 = s0or1;
290               strcpy (ld->normal_env->env_name_str, s[0]);
291             }
292         }
293       else if (strcmp (code, "setenv_R") == 0)
294         {
295           if (num > 2)
296             {
297               if (get_new_env (NULL, ld, 1) < 0)
298                 goto ERROR_RET;
299               if (!(strcmp (s[num - 3], "sticky")))
300                 {
301                   ld->reverse_env->sticky = 1;
302                   num--;
303                 }
304               if (num == 3)
305                 s0or1 = s[1];
306               else
307                 {
308                   s0or1 = s[2];
309                   if (def_reverse_servername && *def_reverse_servername)
310                     src2 = def_reverse_servername;
311                   else
312                     src2 = s[1];
313                   copy++;
314                   dst2 = &ld->reverse_env->host_name;
315                 }
316               if (expand_expr (s0or1, ld->lang) != 0)
317                 {
318                   err_expand (open_uumrc_filenm, s0or1);
319                 }
320               copy++;
321               dst1 = &ld->reverse_env->envrc_name;
322               src1 = s0or1;
323               strcpy (ld->reverse_env->env_name_str, s[0]);
324             }
325         }
326       else if (strcmp (code, "waking_up_in_henkan_mode") == 0)
327         {
328           if (!(defined_by_option & OPT_WAKING_UP_MODE))
329             {
330               henkan_off_def = 0;
331             }
332         }
333       else if (strcmp (code, "waking_up_no_henkan_mode") == 0)
334         {
335           if (!(defined_by_option & OPT_WAKING_UP_MODE))
336             {
337               henkan_off_def = 1;
338             }
339         }
340       else if (strcmp (code, "simple_delete") == 0)
341         {
342           excellent_delete = 0;
343         }
344       else if (strcmp (code, "excellent_delete") == 0)
345         {
346           excellent_delete = 1;
347         }
348       else if (strcmp (code, "send_ascii_char") == 0)
349         {
350           send_ascii_char_def = 1;
351         }
352       else if (strcmp (code, "not_send_ascii_char") == 0)
353         {
354           send_ascii_char_def = 0;
355         }
356       else if (strcmp (code, "setmaxchg") == 0)
357         {
358           if (change_ascii_to_int (s[0], &d1) != -1)
359             {
360               maxchg = (d1 <= 0) ? maxchg : d1;
361             }
362         }
363       else if (strcmp (code, "setmaxbunsetsu") == 0)
364         {
365           if (num >= 2)
366             {
367               if (change_ascii_to_int (s[0], &d1) != -1)
368                 {
369                   maxbunsetsu = (d1 <= 0) ? maxbunsetsu : d1;
370                 }
371             }
372         }
373       else if (strcmp (code, "setmaxhistory") == 0)
374         {
375           if (num >= 2)
376             {
377               if (change_ascii_to_int (s[0], &d1) != -1)
378                 {
379                   max_history = (d1 <= 0) ? max_history : d1 + 1;
380                 }
381             }
382         }
383       else if (strcmp (code, "setjishopath") == 0)
384         {
385           if (num > 1)
386             {
387               if (expand_expr (s[0], ld->lang) != 0)
388                 {
389                   err_expand (open_uumrc_filenm, s[0]);
390                 }
391               copy++;
392               dst1 = &ld->jishopath;
393               src1 = s[0];
394             }
395         }
396       else if (strcmp (code, "sethindopath") == 0)
397         {
398           if (num > 1)
399             {
400               if (expand_expr (s[0], ld->lang) != 0)
401                 {
402                   err_expand (open_uumrc_filenm, s[0]);
403                 }
404               copy++;
405               dst1 = &ld->hindopath;
406               src1 = s[0];
407             }
408         }
409       else if (strcmp (code, "setfuzokugopath") == 0)
410         {
411           if (num > 1)
412             {
413               if (expand_expr (s[0], ld->lang) != 0)
414                 {
415                   err_expand (open_uumrc_filenm, s[0]);
416                 }
417               copy++;
418               dst1 = &ld->fuzokugopath;
419               src1 = s[0];
420             }
421         }
422       else if (strcmp (code, "touroku_comment") == 0)
423         {
424           touroku_comment = 1;
425         }
426       else if (strcmp (code, "touroku_no_comment") == 0)
427         {
428           touroku_comment = 0;
429         }
430       else if (strcmp (code, "henkan_on_kuten") == 0)
431         {
432           henkan_on_kuten = 1;
433         }
434       else if (strcmp (code, "henkan_off_kuten") == 0)
435         {
436           henkan_on_kuten = 0;
437           /*
438            * unnessesary in xim
439            } else if(strcmp(code , "setkanaromenv") == 0) {
440            } else if(strcmp(code , "flow_control_on") == 0) {
441            } else if(strcmp(code , "flow_control_off") == 0) {
442            } else if(strcmp(code , "convkey_always_on") == 0) {
443            } else if(strcmp(code , "convkey_not_always_on") == 0) {
444            } else if (strcmp(code, "setmaxichirankosu") == 0) {
445            } else if(strcmp(code, "remove_cs") == 0) {
446            } else if(strcmp(code, "not_remove_cs") == 0) {
447            */
448         }
449       else
450         {
451         }
452     }
453   if (copy)
454     {
455       if (copy > 1)
456         {
457           if (!(*dst2 = alloc_and_copy (src2)))
458             return (-1);
459         }
460       if (!(*dst1 = alloc_and_copy (src1)))
461         return (-1);
462     }
463   return (0);
464 ERROR_RET:
465   free_env (ld->normal_env);
466   free_env (ld->reverse_env);
467 #define check_and_free(s) if (s) {Free(s); s = NULL;}
468   check_and_free (ld->uumkey_name);
469   check_and_free (ld->jishopath);
470   check_and_free (ld->hindopath);
471   check_and_free (ld->fuzokugopath);
472 #undef check_and_free
473   return (-1);
474 }
475 
476 static int
copy_wnn_env(src,dst)477 copy_wnn_env (src, dst)
478      register WnnEnv *src, *dst;
479 {
480   if (src->host_name && !(dst->host_name = alloc_and_copy (src->host_name)))
481     return (-1);
482   dst->envrc_name = src->envrc_name;
483   strcpy (dst->env_name_str, src->env_name_str);
484   dst->sticky = src->sticky;
485   return (0);
486 }
487 
488 static int
copy_lang_db(ld)489 copy_lang_db (ld)
490      register XIMLangDataBase *ld;
491 {
492   register WnnEnv *p, *pp;
493 
494   for (p = ld->normal_env; p; p = p->next)
495     {
496       if (get_new_env (c_c, NULL, 0) < 0)
497         goto ERROR_RET;
498       pp = c_c->cur_normal_env;
499       if (copy_wnn_env (p, pp) < 0)
500         goto ERROR_RET;
501     }
502   for (p = ld->reverse_env; p; p = p->next)
503     {
504       if (get_new_env (c_c, NULL, 1) < 0)
505         goto ERROR_RET;
506       pp = c_c->cur_reverse_env;
507       if (copy_wnn_env (p, pp) < 0)
508         goto ERROR_RET;
509     }
510   send_ascii_char = send_ascii_char_def;
511   return (0);
512 ERROR_RET:
513   free_env (c_c->normal_env);
514   free_env (c_c->reverse_env);
515   return (-1);
516 }
517 
518 /*
519  * read uumrc file
520  */
521 int
uumrc_get_entries(ld,rk_pre_load,root)522 uumrc_get_entries (ld, rk_pre_load, root)
523      XIMLangDataBase *ld;
524      int rk_pre_load, root;
525 {
526   FILE *fp = NULL;
527   char data[1024];
528   int err = 0;
529 
530   if (ld->read)
531     {
532       if (copy_lang_db (ld) < 0)
533         return (-1);
534       return (0);
535     }
536   /*
537    * default setting
538    */
539   ld->m_chg = MAXCHG;
540   ld->m_bunsetsu = MAXBUNSETSU;
541   ld->m_history = MAX_HISTORY + 1;
542 
543   if (ld->uumrc_name && *ld->uumrc_name)
544     {
545       strcpy (open_uumrc_filenm, ld->uumrc_name);
546       if ((fp = fopen (open_uumrc_filenm, "r")) == NULL)
547         {
548           print_out2 ("I could not open specified uumrc file \"%s\" for lang \"%s\".", open_uumrc_filenm, ld->lang);
549           err = 1;
550         }
551     }
552   if (fp == NULL)
553     {                           /* Open default uumrc */
554       if (strcpy (open_uumrc_filenm, LIBDIR), strcat (open_uumrc_filenm, "/"), strcat (open_uumrc_filenm, ld->lang), strcat (open_uumrc_filenm, RCFILE), (fp = fopen (open_uumrc_filenm, "r")) != NULL)
555         {
556           if (err)
557             {
558               Free (ld->uumrc_name);
559             }
560           if (rkfile_defined_by_option && (rk_pre_load || root) && ld == cur_lang)
561             {
562               if (!(ld->rkfile_name = alloc_and_copy (root_rkfilename)))
563                 return (-1);
564             }
565           if (!(ld->uumrc_name = alloc_and_copy (open_uumrc_filenm)))
566             return (-1);
567         }
568       else
569         {
570           print_out1 ("I could not open a default uumrc file \"%s\".", open_uumrc_filenm);
571           return (-1);
572         }
573     }
574 
575   while (fgets (data, 1024, fp) != NULL)
576     {
577       if (uumrc_set_entry (data, ld, rk_pre_load, root) == -1)
578         {
579           fclose (fp);
580           return (-1);
581         }
582     }
583   fclose (fp);
584   if (!rk_pre_load)
585     {
586       ld->read = 1;
587       if (copy_lang_db (ld) < 0)
588         return (-1);
589     }
590   set_cswidth (ld->cswidth_id);
591   if (ld->rk_table == NULL)
592     {
593       if ((ld->rk_table = romkan_table_init (NULL, ld->rkfile_name, NULL, bytcntfn, NULL, 0))
594 /*
595                                               bytcntfn, letterpickfn, NULL, 0))
596 */
597           == NULL)
598         {
599           return (-1);
600         }
601     }
602   return (0);
603 }
604 
605 int
read_default_rk()606 read_default_rk ()
607 {
608   register ReadRkfileRec *rr;
609   register XIMLangDataBase *ld;
610 
611   for (rr = read_rkfile_list; rr != NULL; rr = rr->next)
612     {
613       for (ld = language_db; ld; ld = ld->next)
614         {
615           if (!strcmp (ld->lang, rr->name))
616             break;
617         }
618       if (!ld)
619         {
620           print_out1 ("In ximrc, I don't support such language \"%s\".", rr->name);
621           return (-1);
622         }
623       if (uumrc_get_entries (ld, 1, 0) == -1)
624         {
625           return (-1);
626         }
627     }
628   return (0);
629 }
630