1 /*
2  *  $Id: wddel.c,v 1.8 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 /*
33  * UJIS �ե�������ɤߤȤäơ�ñ����Ͽ��Ԥʤ���
34  */
35 
36 #ifndef lint
37 static char *rcs_id = "$Id: wddel.c,v 1.8 2002/05/12 22:51:17 hiroo Exp $";
38 #endif /* lint */
39 
40 #ifdef HAVE_CONFIG_H
41 #  include <config.h>
42 #endif
43 
44 #include <stdio.h>
45 #if STDC_HEADERS
46 #  include <stdlib.h>
47 #else
48 #  if HAVE_MALLOC_H
49 #    include <malloc.h>
50 #  endif
51 #endif /* STDC_HEADERS */
52 #if HAVE_UNISTD_H
53 #  include <unistd.h>
54 #endif
55 
56 #include "commonhd.h"
57 #include "wnn_config.h"
58 #include "jllib.h"
59 #include "jslib.h"
60 #include "wnn_string.h"
61 #include "wnn_os.h"
62 
63 #define WORD_DELETE 1
64 #define COMMENT_SET 2
65 #define HINDO_SET 3
66 
67 #define LINE_SIZE 256
68 
69 char *def_server;
70 
71 int dic_no = -1;
72 char *server_n;
73 char *env_n = "wddel";
74 char *fname = NULL;
75 int fid;
76 int client = 0;                 /* server site dict */
77 int what = WORD_DELETE;
78 
79 WNN_JSERVER_ID *js;
80 struct wnn_env *env, *rev_env;
81 static struct wnn_ret_buf rb = { 0, NULL };
82 
83 static void
usage()84 usage ()
85 {
86   fprintf (stderr, "wddel [-D server_name] [-n env_name][-d dic_no][-L][-E][-H][-C] [file_name]  < text dict\n");
87   fprintf (stderr, "file_name or -d dic_no must be specified\n");
88   fprintf (stderr, "default env_name = wddel\n");
89   fprintf (stderr, "default server_name = %s\n", def_server);
90   fprintf (stderr, "L is to specify that the file at the client site.\n");
91   fprintf (stderr, "-E is word_delete, -C is word_comment_set, -H is hindo_set.\n");
92   exit (1);
93 }
94 
95 static void
err()96 err ()
97 {
98   printf ((char *) wnn_perror ());
99   printf ("\n bye.\n");
100   exit (1);
101 }
102 
103 int
main(argc,argv)104 main (argc, argv)
105      int argc;
106      char **argv;
107 {
108   extern char *getenv ();
109   extern int optind;
110   extern char *optarg;
111   struct wnn_dic_info *info;
112   int c;
113   int k;
114   int num;
115   int sno;
116   char s[LINE_SIZE];
117   char *cswidth_name;
118   extern char *get_cswidth_name ();
119   extern void set_cswidth ();
120 
121   if (getenv (WNN_DEF_SERVER_ENV))
122     {
123       def_server = getenv (WNN_DEF_SERVER_ENV);
124     }
125   else
126     {
127       def_server = "";
128     }
129   server_n = def_server;
130 
131   if (cswidth_name = get_cswidth_name (WNN_DEFAULT_LANG))
132     set_cswidth (create_cswidth (cswidth_name));
133   while ((c = getopt (argc, argv, "D:n:d:CHEL")) != EOF)
134     {
135       switch (c)
136         {
137         case 'D':
138           server_n = optarg;
139           break;
140         case 'n':
141           env_n = optarg;
142           break;
143         case 'd':
144           dic_no = atoi (optarg);
145           break;
146         case 'C':
147           what = COMMENT_SET;
148           break;
149         case 'H':
150           what = HINDO_SET;
151           break;
152         case 'E':
153           what = WORD_DELETE;
154           break;
155         case 'L':
156           client = 1;
157           break;
158         default:
159           usage ();
160           break;
161         }
162     }
163   if (optind)
164     {
165       optind--;
166       argc -= optind;
167       argv += optind;
168     }
169   if (argc > 1)
170     {
171       fname = argv[1];
172     }
173   if (fname && dic_no != -1)
174     usage ();
175   if (!fname && dic_no == -1)
176     usage ();
177 
178 
179   rb.buf = (char *) malloc ((unsigned) (rb.size = 0));
180 
181   if ((js = js_open (server_n, WNN_TIMEOUT)) == NULL)
182     err ();
183   if ((env = js_connect (js, env_n)) == NULL)
184     err ();
185   if (fname)
186     {
187       if (client)
188         {
189           if ((fid = js_file_send (env, fname)) == -1)
190             err ();
191         }
192       else
193         {
194           if ((fid = js_file_read (env, fname)) == -1)
195             err ();
196         }
197       if ((dic_no = js_dic_add (env, fid, -1, 0, 0, WNN_DIC_RW, WNN_DIC_RW, "", "")) == -1)
198         err ();
199     }
200 
201   if ((num = js_dic_list (env, &rb)) == -1)
202     err ();
203 
204   info = (struct wnn_dic_info *) (rb.buf);
205   for (k = 0; k < num; k++)
206     {
207       if (info[k].dic_no == dic_no)
208         break;
209     }
210   if (k == num)
211     {
212       /*
213          fprintf(stderr, "���ꤵ�줿�ֹ�μ���ϡ��Ķ���¸�ߤ��ޤ���\n");
214        */
215       fprintf (stderr, "The specified dictionary isn't exist in current environment\n");
216       exit (1);
217     }
218   if (info[k].type != WNN_UD_DICT && info[k].type != WNN_REV_DICT)
219     {
220       /*
221          fprintf(stderr, "���ꤵ�줿�ֹ�μ���ϡ���Ͽ��ǽ�ǤϤ���ޤ���\n");
222        */
223       fprintf (stderr, "The specified dictionary isn't registable\n");
224       exit (1);
225     }
226   while (fgets (s, sizeof (s), stdin))
227     {
228       char com[LENGTHYOMI];
229       char Com[LENGTHYOMI];
230       int ima, hindo;
231       if (s[0] == '\\')
232         continue;
233       switch (what)
234         {
235         case WORD_DELETE:
236           if (sscanf (s, "%d", &sno) <= 0)
237             {
238               fprintf (stderr, "Bad line \"%s\"", s);
239               continue;
240             }
241           if (js_word_delete (env, dic_no, sno) == -1)
242             {
243               fprintf (stderr, "serial-no = %d\n", sno);
244               err ();
245             }
246           break;
247         case COMMENT_SET:
248           if (sscanf (s, "%d %s", &sno, com) <= 1)
249             {
250               fprintf (stderr, "Bad line \"%s\"", s);
251               continue;
252             }
253           wnn_Sstrcpy (Com, com);
254           if (js_word_comment_set (env, dic_no, sno, Com) == -1)
255             {
256               fprintf (stderr, "serial-no = %d\n", sno);
257               err ();
258             }
259           break;
260         case HINDO_SET:
261           if (sscanf (s, "%d %n %n", &sno, &ima, &hindo) <= 2)
262             {
263               fprintf (stderr, "Bad line \"%s\"", s);
264               continue;
265             }
266           if (js_hindo_set (env, dic_no, sno, ima, hindo) == -1)
267             {
268               fprintf (stderr, "serial-no = %d\n", sno);
269               err ();
270             }
271           break;
272         }
273     }
274   if (fname)
275     {
276       if (client)
277         {
278           if (js_file_receive (env, fid, "") == -1)
279             err ();
280         }
281       else
282         {
283           if (js_file_write (env, fid, "") == -1)
284             err ();
285         }
286       fprintf (stderr, "Wrote the file back.\n");
287     }
288   exit (0);
289 }
290