1 #ifndef lint
2 static char rcs_id[] = "$Id: jhlp.c,v 1.2 2001/06/14 18:16:16 ura Exp $";
3 #endif /* lint */
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  *      '99/04/19       TAOKA Satoshi - �IJ� �һ�<taoka@infonets.hiroshima-u.ac.jp>
35  *              index() ����������ȥ����ȡ�
36  *
37  * Last modified date: 19,Apr.1999
38  *
39  * Code:
40  *
41  */
42 /*      Version 4.0
43  */
44 #include <signal.h>
45 #include <pwd.h>
46 #include "commonhd.h"
47 #include "sdefine.h"
48 #include <X11/Xos.h>
49 #include "xim.h"
50 #include "sheader.h"
51 #include "config.h"
52 #include "ext.h"
53 
54 #ifdef  BSD42
55 #include <sgtty.h>
56 #endif /* BSD42 */
57 #ifdef  SYSVR2
58 #include <termio.h>
59 #endif /* SYSVR2 */
60 extern char *optarg;
61 extern int optind;
62 
63 extern char *getenv ();
64 extern void exit ();
65 
66 static void save_signals ();
67 
68 static struct _options
69 {
70   char *opt;
71   char *desc;
72 }
73 options[] =
74 {
75   {
76   "-D servername", "Wnn server to contact"}
77   ,
78   {
79   "-lc langname", "language name"}
80   ,
81   {
82   "-n username", "user name"}
83   ,
84   {
85   "-k uumkeyname", "uumkey file"}
86   ,
87   {
88   "-r rkfilename", "romkan file"}
89   ,
90   {
91   "-X cvtximname", "convert keys file"}
92   ,
93   {
94   "-F cvtfunname", "convert function file"}
95   ,
96   {
97   "-M cvtmetaname", "convert meta file"}
98   ,
99   {
100   "-fs fontnamelist", "font name list"}
101   ,
102   {
103   "-geometry geom", "size (in pixels) and position"}
104   ,
105   {
106   "-fg color", "foreground color"}
107   ,
108   {
109   "-bg color", "background color"}
110   ,
111   {
112   "-bd color", "border color"}
113   ,
114   {
115   "-bw number", "border width in pixels"}
116   ,
117   {
118   "-display displayname", "X server to contact"}
119   ,
120   {
121   "-iconic", "start iconic"}
122   ,
123   {
124   "#geom", "icon window geometry"}
125   ,
126   {
127   "-help", "print out this message"}
128   ,
129   {
130   "-h", "wake up in convertion off mode"}
131   ,
132   {
133   "-H", "wake up in convertion on mode"}
134   ,
135 #ifdef  USING_XJUTIL
136   {
137   "-ju", "xjutil (dictionary utility manager) name"}
138   ,
139 #endif /* USING_XJUTIL */
140   {
141   "-RV", "Roo-mode window can switch map and unmap"}
142   ,
143   {
144   NULL, NULL}
145 };
146 
147 static int
message_open(lang)148 message_open (lang)
149      register char *lang;
150 {
151   char nlspath[MAXPATHLEN];
152 
153   strcpy (nlspath, LIBDIR);
154   strcat (nlspath, "/%L/%N");
155 
156 #ifdef DEBUG
157   cd = msg_open ("/usr/tmp/xim.msg", nlspath, lang);
158 #else
159   cd = msg_open (XIMMSGFILE, nlspath, lang);
160 #endif
161   if (cd->msg_bd == NULL)
162     {
163       print_out1 ("I can't open message_file \"%s\", use default message.", cd->nlspath);
164     }
165   return (0);
166 }
167 
168 static void
usage(bad_option)169 usage (bad_option)
170      char *bad_option;
171 {
172   struct _options *opt;
173   int col;
174 
175   fprintf (stderr, "%s:  bad command line option \"%s\"\r\n\n", prgname, bad_option);
176 
177   fprintf (stderr, "usage:  %s", prgname);
178   col = 8 + strlen (prgname);
179   for (opt = options; opt->opt; opt++)
180     {
181       int len = 3 + strlen (opt->opt);
182       if (col + len > 79)
183         {
184           fprintf (stderr, "\r\n   ");
185           col = 3;
186         }
187       fprintf (stderr, " [%s]", opt->opt);
188       col += len;
189     }
190 
191   fprintf (stderr, "\r\n\nType %s -help for a full description.\r\n\n", prgname);
192   exit (1);
193 }
194 
195 static void
help()196 help ()
197 {
198   struct _options *opt;
199 
200   fprintf (stderr, "usage:\n        %s [-options ...]\n\n", prgname);
201   fprintf (stderr, "where options include:\n");
202   for (opt = options; opt->opt; opt++)
203     {
204       fprintf (stderr, "    %-28s %s\n", opt->opt, opt->desc);
205     }
206   fprintf (stderr, "\n");
207   exit (0);
208 }
209 
210 /*
211  * Parse options
212  */
213 static void
parse_options(argc,argv)214 parse_options (argc, argv)
215      int argc;
216      char **argv;
217 {
218   int i;
219 
220   for (i = 1; i < argc; i++)
221     {
222       if (!strcmp (argv[i], "-h"))
223         {
224           root_henkan_off_def = 1;
225           defined_by_option |= OPT_WAKING_UP_MODE;
226         }
227       else if (!strcmp (argv[i], "-H"))
228         {
229           root_henkan_off_def = 0;
230           defined_by_option |= OPT_WAKING_UP_MODE;
231         }
232       else if (!strcmp (argv[i], "-k"))
233         {
234           if ((++i >= argc) || (argv[i][0] == '-'))
235             usage (argv[i]);
236           root_uumkeyname = alloc_and_copy (argv[i]);
237           if (root_uumkeyname == NULL || *root_uumkeyname == '\0')
238             usage (argv[i]);
239           defined_by_option |= OPT_WNNKEY;
240         }
241       else if (!strcmp (argv[i], "-r"))
242         {
243           if ((++i >= argc) || (argv[i][0] == '-'))
244             usage (argv[i]);
245           root_rkfilename = alloc_and_copy (argv[i]);
246           if (root_rkfilename == NULL || *root_rkfilename == '\0')
247             usage (argv[i]);
248           defined_by_option |= OPT_RKFILE;
249         }
250       else if (!strcmp (argv[i], "-help"))
251         {
252           help ();
253         }
254       else
255         {
256           usage (argv[i]);
257         }
258     }
259 }
260 
261 
262 /* get rubout_code */
263 #define UNDEF_STTY 0xff
264 
265 #ifdef BSD42
266 static void
get_rubout()267 get_rubout ()
268 {
269   struct sgttyb savetmio;
270 
271   if ((ioctl (0, TIOCGETP, &savetmio) < 0) || (savetmio.sg_erase == UNDEF_STTY))
272     {
273       rubout_code = RUBOUT;     /* BackSpase */
274     }
275   else
276     {
277       rubout_code = savetmio.sg_erase;
278     }
279 }
280 #endif /* BSD42 */
281 
282 #ifdef SYSVR2
283 static void
get_rubout()284 get_rubout ()
285 {
286   struct termio tmio;
287 
288   if ((ioctl (0, TCGETA, &tmio) < 0) || (tmio.c_cc[VERASE] == UNDEF_STTY))
289     {
290       rubout_code = RUBOUT;     /* BackSpase */
291     }
292   else
293     {
294       rubout_code = tmio.c_cc[VERASE];
295     }
296 }
297 #endif /* SYSVR2 */
298 
299 void
do_end()300 do_end ()
301 {
302   xw_end ();
303   epilogue ();
304 #ifdef  USING_XJUTIL
305   kill_all_xjutil ();
306 #endif /* USING_XJUTIL */
307   exit (0);
308 }
309 
310 /** Handler of SIGTERM */
311 static void
terminate_handler()312 terminate_handler ()
313 {
314   do_end ();
315 }
316 
317 /** Mail roop */
318 
319 void
in_put(c)320 in_put (c)
321      int c;
322 {
323   register int ml;
324 
325   do
326     {
327       ml = kk (c);
328       if (ml >= 0)
329         {
330           make_history (return_buf, ml);
331           xw_write (return_buf, ml);
332         }
333     }
334   while (if_unget_buf ());
335 }
336 
337 static void
do_main()338 do_main ()
339 {
340   int type;
341 
342   for (;;)
343     {
344       X_flush ();
345       type = wait_for_socket ();
346       switch (type)
347         {
348         case XEVENT_TYPE:
349           XEventDispatch ();
350           break;
351 #ifndef X11R5
352         case REQUEST_TYPE_XIM:
353           XimRequestDispatch ();
354           break;
355 #endif /* !X11R5 */
356 #if defined(X11R5) || defined(BC_X11R5)
357         case REQUEST_TYPE:
358           RequestDispatch ();
359           break;
360 #endif /* defined(X11R5) || defined(BC_X11R5) */
361 #if defined(XJPLIB) && defined(XJPLIB_DIRECT)
362         case XJP_DIRECT_TYPE:
363           XJp_Direct_Dispatch ();
364 #endif /* defined(XJPLIB) && defined(XJPLIB_DIRECT) */
365         default:
366           break;
367         }
368     }
369 }
370 
371 /*
372   signal settings
373  */
374 
375 static void
save_signals()376 save_signals ()
377 {
378   signal (SIGPIPE, SIG_IGN);
379   signal (SIGHUP, SIG_IGN);
380 #ifndef NOTFORK
381   signal (SIGINT, SIG_IGN);
382 #endif /* NOTFORK */
383   signal (SIGQUIT, SIG_IGN);
384   signal (SIGTERM, terminate_handler);
385   signal (SIGCHLD, SIG_IGN);
386 }
387 
388 static int
make_world_lang()389 make_world_lang ()
390 {
391   XIMLangDataBase *ld;
392   register int len = 0;
393 
394   for (ld = language_db; ld; ld = ld->next)
395     {
396       len += strlen (ld->lc_name) + 1;
397     }
398   if (!(world_lang = Malloc (len)))
399     return (-1);
400   world_lang[0] = '\0';
401   for (ld = language_db; ld; ld = ld->next)
402     {
403       strcat (world_lang, ld->lc_name);
404       strcat (world_lang, ";");
405     }
406   if (*world_lang)
407     {
408       world_lang[strlen (world_lang) - 1] = '\0';
409     }
410   return (0);
411 }
412 
413 static int
create_arg_string(argv,argc)414 create_arg_string (argv, argc)
415      char **argv;
416      int argc;
417 {
418   register int nbytes, i;
419   register char *p;
420 
421   for (i = 0, nbytes = 0; i < argc; i++)
422     {
423       nbytes += (argv[i] ? strlen (argv[i]) : 0) + 1;
424     }
425   if (!(p = Malloc (nbytes)))
426     {
427       malloc_error ("allocation of work area");
428       return (-1);
429     }
430   for (i = 0; i < argc; i++)
431     {
432       if (argv[i])
433         {
434           strcpy (p, argv[i]);
435           p += strlen (argv[i]) + 1;
436         }
437     }
438   arg_string = p;
439   return (0);
440 }
441 
442 void
main(argc,argv)443 main (argc, argv)
444      int argc;
445      char **argv;
446 {
447   char *name;
448   char lc_name_buf[64], *lc_name = NULL;
449   register XIMLangDataBase *db = NULL;
450   register XIMLcNameRec *lnl;
451   extern struct passwd *getpwuid ();
452   char *server_env;
453 #ifdef X11R5
454   XLocale xlc;
455 #else
456   XLCd xlc;
457 #endif /* X11R5 */
458   extern char *get_server_env ();
459 /*    extern char *index(); */
460 
461   prgname = argv[0];
462   if (create_arg_string (argv, argc) == -1)
463     exit (1);
464 
465   /*
466    * get application resources
467    */
468   argc = get_application_resources (argc, argv);
469   /*
470    * parse option
471    */
472   parse_options (argc, argv);
473 
474   /*
475    * Get user name and server name
476    */
477   if (root_username == NULL)
478     {
479       if ((name = getenv (WNN_USERNAME_ENV)) != NULL)
480         {
481           if (!(root_username = alloc_and_copy (name)))
482             exit (1);
483         }
484       else
485         {
486           if (!(root_username = alloc_and_copy (getpwuid (getuid ())->pw_name)))
487             exit (1);
488         }
489     }
490 
491   /*
492    * read xim.conf file
493    */
494   if (read_ximconf () == -1)
495     exit (1);
496 
497   if (def_lang)
498     {
499       for (db = language_db; db; db = db->next)
500         {
501           if (!strcmp (db->lang, def_lang))
502             {
503               lc_name = setlocale (LC_ALL, db->lc_name);
504               cur_lang = db;
505               break;
506             }
507         }
508       if (db == NULL || db->lang == NULL)
509         {
510           print_out1 ("Sorry, langName \"%s\" is not my supported language.", def_lang);
511           print_out ("I refer to the system locale.");
512         }
513       else if (lc_name == NULL)
514         {
515           print_out2 ("Sorry, can not set locale \"%s\" related to langName \"%s\".", db->lc_name, def_lang);
516           print_out ("I refer to the system locale.");
517         }
518     }
519   if (lc_name == NULL)
520     {
521       lc_name = setlocale (LC_ALL, "");
522       if (lc_name == NULL || !strcmp (lc_name, "C"))
523         {
524           if (lc_name)
525             {
526               print_out ("The current locale is C.");
527             }
528           else
529             {
530               print_out ("The current locale is wrong.");
531             }
532           print_out1 ("I try to set locale to defualt \"%s\".", def_locale);
533           lc_name = setlocale (LC_ALL, def_locale);
534           if (lc_name == NULL)
535             {
536               print_out ("Could not set locale by setlocale().");
537               print_out ("I will abort.");
538               exit (1);
539             }
540           if (!strcmp (lc_name, "C"))
541             {
542               print_out ("Sorry, can not run under the C locale environment.");
543               print_out ("I will abort.");
544               exit (1);
545             }
546         }
547 #ifdef X11R5
548       if (!(xlc = _XFallBackConvert ()))
549         {
550           print_out ("Could not create locale environment.");
551           print_out ("I will abort.");
552           exit (1);
553         }
554       if (strcmp (lc_name, xlc->xlc_db->lc_name))
555         {
556           lc_name = xlc->xlc_db->lc_name;
557 #else
558       if (!(xlc = _XlcCurrentLC ()))
559         {
560           print_out ("Could not create locale environment.");
561           print_out ("I will abort.");
562           exit (1);
563         }
564       if (strcmp (lc_name, xlc->core->name))
565         {
566           lc_name = xlc->core->name;
567 #endif /* X11R5 */
568         }
569       if (!strcmp (lc_name, world_locale))
570         {
571           lnl = lc_name_list;
572         }
573       else
574         {
575           for (lnl = lc_name_list; lnl; lnl = lnl->next)
576             {
577               if (!strcmp (lnl->lc_name, lc_name))
578                 break;
579             }
580         }
581       if (!lnl)
582         {
583           print_out1 ("Sorry, locale \"%s\" is not my supported locale.", lc_name);
584           print_out ("I will abort.");
585           exit (1);
586         }
587       cur_lang = lnl->lang_db;
588     }
589   strcpy (lc_name_buf, lc_name);
590 
591   if (root_def_servername == NULL)
592     {
593       if (!(server_env = get_server_env (cur_lang->lang)))
594         {
595           server_env = WNN_DEF_SERVER_ENV;
596         }
597       if (name = getenv (server_env))
598         {
599           if (!(root_def_servername = root_def_reverse_servername = alloc_and_copy (name)))
600             exit (1);
601         }
602       else
603         {
604           root_def_servername = root_def_reverse_servername = NULL;
605         }
606     }
607 
608   save_signals ();
609 
610   get_rubout ();
611 
612 #ifndef NOTFORK
613   if (fork ())
614     {
615       signal (SIGTERM, exit);
616       signal (SIGCHLD, exit);
617       pause ();
618     }
619 #endif /* NOTFORK */
620 
621   close (fileno (stdin));
622   close (fileno (stdout));
623   if (make_world_lang () < 0 || /* make world_lang string */
624       read_ximrc () < 0 ||      /* read ximrc file */
625       message_open (cur_lang->lang) < 0 ||      /* open message mechanism */
626       read_default_rk () < 0 || init_xcvtkey () < 0 || alloc_all_buf () < 0 || create_xim (lc_name_buf) < 0)    /* create X window environment */
627     exit (1);
628 
629   if (c_c->use_server && !jl_isconnect (bun_data_))
630     {
631       if (servername && *servername)
632         {
633           print_msg_getc (" I can not connect server(at %s)", servername, NULL, NULL);
634         }
635       else
636         {
637           print_msg_getc (" I can not connect server", NULL, NULL, NULL);
638         }
639     }
640 
641 #ifndef NOTFORK
642   /* End of initialization, kill parent */
643   kill (getppid (), SIGTERM);
644 #endif /* !NOTFORK */
645 
646   clear_c_b ();
647 
648   cur_rk = c_c->rk;
649   cur_rk_table = cur_rk->rk_table;
650   visual_status ();
651   if (henkan_off_flag == 0)
652     {
653       disp_mode ();
654 /*
655     } else {
656         henkan_off();
657 */
658     }
659   do_main ();
660 }
661