1 /*
2  *  $Id: error.c,v 1.16 2003/06/08 03:09:51 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, 2001, 2002, 2003
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 #if defined(HAVE_CONFIG_H)
33 #include <config.h>
34 #endif
35 
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <signal.h>
40 #if STDC_HEADERS
41 #  include <stdlib.h>
42 #  include <stdarg.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 #if TIME_WITH_SYS_TIME
53 #  include <sys/time.h>
54 #  include <time.h>
55 #else
56 #  if HAVE_SYS_TIME_H
57 #    include <sys/time.h>
58 #  else
59 #    include <time.h>
60 #  endif /* HAVE_SYS_TIME_H */
61 #endif /* TIME_WITH_SYS_TIME */
62 #if HAVE_SYSLOG_H
63 #  include <syslog.h>
64 #endif
65 
66 #include "commonhd.h"
67 #include "de_header.h"
68 #include "jslib.h"
69 
70 /* static void exit_hand (); */		/* Not used for now */
71 static void vwrite_log (const char *, va_list);
72 #ifndef HAVE_VASPRINTF
73 /* ansidecl.h from libiberty includes some useful macros. */
74 #include "ansidecl.h"
75 
76 /* Have taken from libiberty.h - defined here because it isn't used anywhere */
77 extern int vasprintf PARAMS ((char **, const char *, va_list))
78   ATTRIBUTE_PRINTF(2,0);
79 #endif
80 
81 #if 0		/* Not used for now */
82 void
83 error_exit1 (x, y1, y2, y3, y4, y5)
84      char *x;
85      int y1, y2, y3, y4, y5;
86 {
87   /* Need care with buffer size */
88   char buf[512];
89 
90   strcpy (buf, "Fatal error. Exiting...: ");
91   strcat (buf, x);
92   log_err (buf, y1, y2, y3, y4, y5);
93   exit_hand ();
94 }
95 
96 void
97 error_exit (x)
98      char *x;
99 {
100   log_err("%s: %s", "Fatal error. Exiting...", x);
101   exit_hand ();
102 }
103 #endif		/* Not used for now */
104 
105 
106 #if 0
107 /* write_log() : variable argument version of vwrite_log() */
108 /* not used for now */
109 void
110 write_log(const char *format, ...)
111 {
112   va_list ap;
113 
114   va_start(ap, format);
115   vwrite_log(format, ap);
116   va_end(ap);
117 }
118 #endif
119 
120 /*
121    vwrite_log: ������my_error()���֤�������ؿ���
122    ���ꤷ��FILE * (���ΤȤ���stderr)�إե����ޥåȤ����ƽ��Ϥ��롣
123    ���Ѱ�����Ȥ�ʬ����ʻȤ������Ǥ��뤬���ܿ�����»�ʤ��뤫��
124    ����ʤ���
125 
126    ������:
127    "Dec/25/2001:17:30:55 (client=hoge|server): <formatted message> ,errno=X"
128    (����η�����Ƨ�����Ĥ�(�ۤ�)��ԤˤޤȤᡢ�����Ϥ��䤹���ʤ�褦
129    ��θ��������ǡ��?)
130  */
131 static void
vwrite_log(const char * format,va_list ap)132 vwrite_log(const char *format, va_list ap)
133 {
134 #ifdef PRINT_ERRNO
135   int saved_errno = errno;
136 #endif
137   FILE *fplog = stderr;
138   time_t obakenoQ;
139   struct tm *Q;
140   char *chopped = NULL;
141   char client_uname[WNN_ENVNAME_LEN];
142   unsigned int formlen;
143   const char *month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
144 			 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
145 
146   obakenoQ = time(NULL);
147   Q = localtime(&obakenoQ);
148 
149   fprintf(fplog, "%s/%02d/%4d:%02d:%02d:%02d ",
150 	  month[Q->tm_mon], Q->tm_mday, Q->tm_year + 1900,
151 	  Q->tm_hour, Q->tm_min, Q->tm_sec);
152   if(c_c) {
153     if (isgraph (c_c->user_name[0])) {
154       strncpy (client_uname, c_c->user_name, WNN_ENVNAME_LEN);
155       client_uname[WNN_ENVNAME_LEN-1]='\0';
156     } else {
157       strcpy (client_uname, "<UNKNOWN>");
158     }
159     fprintf(fplog, "client=%s", client_uname);
160     /* getpwuid(c_c->uid)->pw_name ? */
161   } else {
162     fputs("server", fplog);
163   }
164   fputs(": ", fplog);
165 
166   formlen = strlen(format);
167   if(formlen > 0  && format[formlen - 1] == '\n') {
168     /* chop(format) (in Perl)-like handling -- for compatibility */
169     if((chopped = (char *) malloc(formlen + 1)) != NULL) {
170       strcpy(chopped, format);
171       formlen--;
172       while(formlen >= 0 && chopped[formlen] == '\n') {
173 	chopped[formlen] = '\0';
174 	formlen--;
175       }
176     }
177     /* Need to add 'malloc failed' message? */
178   }
179 
180   if(chopped != NULL) {
181     vfprintf(fplog, chopped, ap);
182     free(chopped);
183   } else {
184     vfprintf(fplog, format, ap);
185   }
186 
187 #ifdef PRINT_ERRNO	/* I don't know it's useful ... (aono) */
188   fprintf(fplog, " ,errno=%d", saved_errno);
189 #endif
190   fputc('\n', fplog);
191   fflush(fplog);
192 }
193 
194 void
log_debug(const char * fmt,...)195 log_debug(const char *fmt, ...)
196 {
197   if(noisy) {
198     va_list ap;
199 
200     va_start(ap, fmt);
201     vwrite_log(fmt, ap);
202     va_end(ap);
203   }
204 }
205 
206 void
log_err(const char * fmt,...)207 log_err(const char *fmt, ...)
208 {
209   va_list ap;
210 
211   va_start(ap, fmt);
212   if(noisy) {
213     vwrite_log(fmt, ap);
214   } else {
215 #ifdef HAVE_VSYSLOG
216     vsyslog(LOG_ERR, fmt, ap);
217 #elif HAVE_SYSLOG
218     char *tmpstr = NULL;
219 
220     vasprintf(&tmpstr, fmt, ap);
221     if(tmpstr != NULL) {
222       syslog(LOG_ERR, "%s", tmpstr);
223       free(tmpstr);
224     } else {
225       syslog(LOG_ERR, "(Memory allocation failed. Cannot log messages.)");
226     }
227 #endif /* HAVE_VSYSLOG */
228   }
229   va_end(ap);
230 }
231 
232 
233 RETSIGTYPE
signal_hand(x)234 signal_hand (x)
235      int x;
236 {
237   log_debug ("signal catched signal_no = %d (ignored)", x);
238   re_signal (x, signal_hand);	/* See <wnn_os.h> */
239 
240   /* not reached */
241 #ifndef RETSIGTYPE_VOID
242   return 0;
243 #endif
244 }
245 
246 RETSIGTYPE
terminate_hand()247 terminate_hand ()
248 {
249   daemon_fin ();
250   exit (0);
251 
252   /* not reached */
253 #ifndef RETSIGTYPE_VOID
254   return 0;
255 #endif
256 }
257 
258 #if 0		/* Not used for now */
259 static void
260 exit_hand ()
261 {
262   daemon_fin ();
263   exit (250);
264 }
265 #endif
266 
267 /* FIXME: following section is not touched yet. */
268 
269 /* Replace with log_debug()? */
270 void
out(x,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12)271 out (x, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12)
272      char *x;
273      int y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12;
274 {
275   if (!noisy)
276     return;
277   fprintf (stderr, x, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12);
278   fflush (stderr);
279 }
280 
281 
282 
283 #ifdef  DEBUG
284 /*
285         debug print
286 */
287 
288 #ifdef  putwchar
289 #undef  putwchar
290 #endif
291 void
putwchar(x)292 putwchar (x)
293      unsigned short x;
294 {
295   if (!noisy)
296     return;
297   putc (x >> 8, stderr);
298   putc (x, stderr);
299   /*
300      putchar( x >> 8);
301      putchar( x );
302    */
303   fflush (stdout);
304 }
305 
306 void
wsputs(buf)307 wsputs (buf)
308      short *buf;
309 {
310   if (!noisy)
311     return;
312 
313   for (;;)
314     {
315       if (*buf == 0)
316         return;
317       putwchar (*buf++);
318     }
319 }
320 #endif /* DEBUG */
321