1 /*
2 * $Id: wnnkill.c,v 1.10 2005/01/30 17:14:20 aonoto 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, 2004, 2005
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 #ifndef lint
33 static char *rcs_id = "$Id: wnnkill.c,v 1.10 2005/01/30 17:14:20 aonoto Exp $";
34 #endif /* lint */
35
36 #ifdef HAVE_CONFIG_H
37 # include <config.h>
38 #endif
39
40 #include <stdio.h>
41 #if STDC_HEADERS
42 # include <stdlib.h>
43 # include <string.h>
44 #elif HAVE_STRINGS_H
45 # include <strings.h>
46 #endif /* STDC_HEADERS */
47 #if HAVE_UNISTD_H
48 # include <unistd.h>
49 #endif
50 #include <stdarg.h> /* assume ANSI-C */
51 #include "jllib.h"
52 #include "jslib.h"
53 #include "jd_sock.h"
54 #include "commonhd.h"
55 #include "wnn_config.h"
56 #include "wnn_os.h"
57
58 WNN_JSERVER_ID *js;
59 struct wnn_ret_buf rb = { 0, NULL };
60 #define BUFSTRLEN 1024
61
62 #ifdef JAPANESE
63 int ocode = TTY_KCODE;
64 #endif
65 #ifdef CHINESE
66 #ifdef TAIWANESE
67 int ocode = (TTY_TCODE + 6);
68 #else /* TAIWANESE */
69 int ocode = (TTY_CCODE + 4);
70 #endif /* TAIWANESE */
71 #endif /* CHINESE */
72 #ifdef KOREAN
73 int ocode = TTY_HCODE;
74 #endif
75
76 static void out (const char* format, ...);
77 static void usage (char *prog);
78
79 int
main(int argc,char ** argv)80 main (int argc, char** argv)
81 {
82 int c;
83 char *serv;
84 int x;
85 static char lang[64] = { 0 };
86 char *server_env = NULL;
87 char *prog = argv[0];
88 extern char *_wnn_get_machine_of_serv_defs (), *get_server_env ();
89
90 /*
91 char* p;
92
93 if ((p = getenv("LANG")) != NULL) {
94 strcpy(lang, p);
95 lang[5] = '\0';
96 } else {
97 lang[0] = '\0';
98 }
99
100 if (*lang == '\0')
101 */
102 strcpy (lang, WNN_DEFAULT_LANG);
103
104 #ifdef JAPANESE
105 # define OPTSTRING "USJL:"
106 #endif
107 #ifdef CHINESE
108 # define OPTSTRING "USJBCL:"
109 #endif /* CHINESE */
110 #ifdef KOREAN
111 # define OPTSTRING "UL:"
112 #endif
113
114 while ((c = getopt (argc, argv, OPTSTRING)) != EOF)
115 {
116 switch (c)
117 {
118 case 'U':
119 #ifdef JAPANESE
120 ocode = J_EUJIS;
121 #endif
122 #ifdef CHINESE
123 ocode = C_EUGB;
124 #endif
125 #ifdef KOREAN
126 ocode = K_EUKSC;
127 #endif
128 break;
129 #ifdef JAPANESE
130 case 'J':
131 ocode = J_JIS;
132 break;
133 case 'S':
134 ocode = J_SJIS;
135 break;
136 #endif
137 #ifdef CHINESE
138 case 'B':
139 ocode = (C_BIG5 + 6);
140 break;
141 case 'C':
142 ocode = (C_ECNS11643 + 6);
143 break;
144 #endif /* CHINESE */
145 case 'L':
146 strcpy (lang, optarg);
147 break;
148 default:
149 usage (prog);
150 exit (1);
151 break;
152 }
153 }
154 if (optind)
155 {
156 optind--;
157 argc -= optind;
158 argv += optind;
159 }
160
161 if ((server_env = get_server_env (lang)) == NULL)
162 {
163 server_env = WNN_DEF_SERVER_ENV;
164 }
165 if (argc > 1)
166 {
167 serv = argv[1];
168 }
169 else if (!(serv = getenv (server_env)))
170 {
171 serv = "";
172 }
173
174 if (!*serv)
175 {
176 if (serv = _wnn_get_machine_of_serv_defs (lang))
177 {
178 if ((js = js_open_lang (serv, lang, WNN_TIMEOUT)) == NULL)
179 {
180 serv = "";
181 }
182 }
183 }
184 if (js == NULL && (js = js_open_lang (serv, lang, WNN_TIMEOUT)) == NULL)
185 {
186 out ("%s:", prog);
187 if (serv && *serv)
188 out ("%s", serv);
189 out ("%s\n", wnn_perror_lang (lang));
190 /* fprintf(stderr, "Can't connect to jserver.\n"); */
191 exit (255);
192 }
193 if ((x = js_kill (js)) > 0)
194 {
195 if (x == 1)
196 out ("%d User Exists.\n", x);
197 else
198 out ("%d Users Exist.\n", x);
199 out ("%s Not Killed.\n", server_env);
200 exit (1);
201 }
202 else if (x == 0)
203 {
204 out ("%s Terminated\n", server_env);
205 exit (0);
206 }
207 else
208 {
209 out ("%s Terminated\n", server_env);
210 exit (2);
211 }
212 exit (0);
213 }
214
215 #ifdef JAPANESE
216 extern int eujis_to_jis8 (), eujis_to_sjis ();
217 #endif
218 #ifdef CHINESE
219 extern int ecns_to_big5 ();
220 #endif
221
222 static void
out(const char * format,...)223 out (const char* format, ...)
224 {
225 va_list ap;
226 char buf[BUFSTRLEN];
227 char jbuf[BUFSTRLEN];
228 int len;
229
230 va_start (ap, format);
231 #ifdef HAVE_SNPRINTF /* Should also have vsnprintf */
232 vsnprintf (buf, BUFSTRLEN, format, ap);
233 #else
234 vsprintf (buf, format, ap); /* Dangerous */
235 #endif /* HAVE_SNPRINTF */
236 va_end (ap);
237 len = strlen (buf);
238
239 switch (ocode)
240 {
241 #ifdef JAPANESE
242 case J_EUJIS:
243 #endif
244 #ifdef CHINESE
245 case (C_EUGB + 4):
246 case (C_ECNS11643 + 6):
247 #endif /* CHINESE */
248 #ifdef KOREAN
249 case K_EUKSC:
250 #endif
251 strncpy (jbuf, buf, len + 1);
252 break;
253 #ifdef JAPANESE
254 case J_JIS:
255 eujis_to_jis8 (jbuf, buf, len + 1);
256 break;
257 case J_SJIS:
258 eujis_to_sjis (jbuf, buf, len + 1);
259 break;
260 #endif
261 #ifdef CHINESE
262 case (C_BIG5 + 6):
263 ecns_to_big5 (jbuf, buf, len + 1);
264 break;
265 #endif /* CHINESE */
266 }
267 fprintf (stderr, "%s", jbuf);
268 }
269
270 static void
usage(char * prog)271 usage (char *prog)
272 {
273 fprintf (stderr, "Usage: %s [-%s] [-L lang] [hostname]\n", prog,
274 #if defined(JAPANESE)
275 "USJ"
276 #elif defined(CHINESE)
277 "USJBC"
278 #elif defined(KOREAN)
279 "U"
280 #else
281 "<Unexpected Situation!>"
282 #endif
283 );
284 }
285