1 #ifdef _cplusplus
2 extern "C" {
3 #endif
4 #include "wiseerror.h"
5 
6 static Flag fatal_flag    = 3;
7 static Flag warning_flag  = 3;
8 static Flag info_flag     = 3;
9 static Flag report_flag   = 3;
10 
11 static ErrorDisplayType display_type = ERROR_DISPLAY_PROGRAM;
12 static char * log_display_string = NULL;
13 
14 #define flag_of_type(c) (c == FATAL ? fatal_flag : c == WARNING ? warning_flag :c == INFO ? info_flag : report_flag )
15 
16 
17 static int eventc=0;
18 static FILE * errlog=NULL;
19 
20 static void (*error_call)(char *,int)= NULL;
21 
22 static char * error_msg_stack[MAXMSGSTACKERROR];
23 
24 static char * (*((error_msg_call)[MAXMSGSTACKERROR]))(void);
25 static int  msg_stack_no=0;
26 
27 /* Function:  set_error_display_style(t)
28  *
29  * Descrip:    sets error display style
30  *
31  *
32  * Arg:        t [UNKN ] Undocumented argument [ErrorDisplayType]
33  *
34  */
35 # line 85 "wiseerror.dy"
set_error_display_style(ErrorDisplayType t)36 void set_error_display_style(ErrorDisplayType t)
37 {
38   display_type = t;
39 }
40 
41 /* Function:  set_log_display_string(str)
42  *
43  * Descrip:    sets string for log display type
44  *
45  *
46  * Arg:        str [UNKN ] Undocumented argument [char *]
47  *
48  */
49 # line 93 "wiseerror.dy"
set_log_display_string(char * str)50 void set_log_display_string(char * str)
51 {
52   if( log_display_string != NULL ) {
53     ckfree(log_display_string);
54   }
55   log_display_string = stringalloc(str);
56 }
57 
58 
59 
60 /* Function:  push_errormsg_stack(msg,)
61  *
62  * Descrip:    This adds a function onto the error message
63  *             stack for stacking errors. this is for
64  *             decent parsers etc for allowing 'error scope'
65  *             to be propagated down.
66  *
67  *             It is very very bad form to push an errormsg
68  *             stack and not pop it at the end.
69  *
70  *
71  * Arg:        msg [UNKN ] Undocumented argument [char *]
72  * Arg:            [UNKN ] Undocumented argument [.]
73  *
74  * Return [UNKN ]  Undocumented return value [boolean]
75  *
76  */
77 # line 113 "wiseerror.dy"
push_errormsg_stack(char * msg,...)78 boolean push_errormsg_stack(char * msg, ...)
79 {
80   char buffer[1024];
81   va_list ap;
82 
83 
84 
85   va_start(ap,msg);
86   vsprintf(buffer,msg,ap);
87 
88   if( msg_stack_no >= MAXMSGSTACKERROR ) {
89     warn("Too many messages held on stack, [%s] discarded\n",buffer);
90     /*** still should up the number ***/
91     msg_stack_no++;
92     return FALSE;
93   }
94   error_msg_call[msg_stack_no] = NULL;
95   error_msg_stack[msg_stack_no++] = stringalloc(buffer);
96   return TRUE;
97 }
98 
99 
100 
101 /* Function:  push_errormsg_stack_call(ecall)
102  *
103  * Descrip:    This adds a function call for people who want
104  *             to register error handling functions
105  *
106  *             Probably best wrapped by a separate function
107  *
108  *
109  * Arg:        ecall [UNKN ] Undocumented argument [NullString]
110  *
111  * Return [UNKN ]  Undocumented return value [boolean]
112  *
113  */
114 # line 142 "wiseerror.dy"
push_errormsg_stack_call(char * (* ecall)(void))115 boolean push_errormsg_stack_call( char * (*ecall)(void))
116 {
117 
118   if( msg_stack_no >= MAXMSGSTACKERROR ) {
119     warn("Too many messages held on stack, Error message call discarded\n");
120     /*** still should up the number ***/
121     msg_stack_no++;
122     return FALSE;
123   }
124   error_msg_call[msg_stack_no] = ecall;
125   error_msg_stack[msg_stack_no++] = NULL;
126   return TRUE;
127 }
128 
129 /* Function:  pop_errormsg_stack(void)
130  *
131  * Descrip:    This removes a error message from the stack
132  *
133  *
134  *
135  */
136 # line 159 "wiseerror.dy"
pop_errormsg_stack(void)137 void pop_errormsg_stack(void)
138 {
139   if( msg_stack_no < MAXMSGSTACKERROR && msg_stack_no > 0 && error_msg_stack[msg_stack_no-1] != NULL)
140     ckfree(error_msg_stack[msg_stack_no-1]);
141   if( msg_stack_no > 0)
142     msg_stack_no--;
143 }
144 
145 /* Function:  show_message_stack(ofp)
146  *
147  * Descrip:    This shows the error message set to
148  *             everyone
149  *
150  *
151  * Arg:        ofp [UNKN ] Undocumented argument [FILE *]
152  *
153  */
154 # line 172 "wiseerror.dy"
show_message_stack(FILE * ofp)155 void show_message_stack(FILE * ofp)
156 {
157   register int i;
158   register int j;
159   register int count;
160 
161   for(i=0;i<msg_stack_no;i++) {
162     if( error_msg_call[i] != NULL ) {
163       show_text( (*(error_msg_call[i]))(),65,ofp);
164     } else {
165       show_text(error_msg_stack[i],65,ofp);
166     }
167   }
168 }
169 
170 
171 /* Function:  add_log_file(ofp)
172  *
173  * Descrip:    Makes ofp the log file for the errors. Discards
174  *             any previous one
175  *
176  *
177  * Arg:        ofp [UNKN ] Undocumented argument [FILE *]
178  *
179  */
180 # line 193 "wiseerror.dy"
add_log_file(FILE * ofp)181 void add_log_file(FILE * ofp)
182 {
183   /* you must also switch on logging errors as well! */
184 
185   errlog=ofp;
186 }
187 
188 
189 /* Function:  add_log_filename(filename)
190  *
191  * Descrip:    Opens filename as the log file.
192  *
193  *
194  * Arg:        filename [UNKN ] Undocumented argument [char *]
195  *
196  * Return [UNKN ]  Undocumented return value [boolean]
197  *
198  */
199 # line 204 "wiseerror.dy"
add_log_filename(char * filename)200 boolean add_log_filename(char * filename)
201 {
202   register FILE * ofp;
203   if( (ofp=openfile(filename,"w")) == NULL) {
204     warn("Could not open %s as a log filename",filename);
205     return FALSE;
206   }
207 
208   add_log_file(ofp);
209   return TRUE;
210 }
211 
212 /* Function:  error_off(type)
213  *
214  * Descrip:    Really for the API. Wraps some
215  *             of the error concepts..
216  *
217  *
218  * Arg:        type [UNKN ] Undocumented argument [int]
219  *
220  */
221 # line 220 "wiseerror.dy"
error_off(int type)222 void error_off(int type)
223 {
224   error_flag_off(type,ERRORUSE);
225 }
226 
227 /* Function:  error_on(type)
228  *
229  * Descrip:    Really for the API. Wraps some
230  *             of the error concepts..
231  *
232  *
233  * Arg:        type [UNKN ] Undocumented argument [int]
234  *
235  */
236 # line 229 "wiseerror.dy"
error_on(int type)237 void error_on(int type)
238 {
239   error_flag_on(type,ERRORUSE);
240 }
241 
242 
243 /* Function:  error_flag_on(type,f)
244  *
245  * Descrip:    Turns on the particular type of error flag
246  *             (eg, STDERR etc).
247  *
248  *
249  * Arg:        type [UNKN ] Undocumented argument [int]
250  * Arg:           f [UNKN ] Undocumented argument [Flag]
251  *
252  */
253 # line 239 "wiseerror.dy"
error_flag_on(int type,Flag f)254 void error_flag_on(int type,Flag f)
255 {
256   switch (type) {
257     case FATAL    : fatal_flag     = fatal_flag | f; return;
258     case WARNING  : warning_flag   = warning_flag | f; return;
259     case INFO     : info_flag      = info_flag | f; return;
260     case REPORT   : report_flag    = report_flag | f ; return;
261     default : log_full_error(WARNING,0,"In error system, tried to change flag %d which doesn't exist!",type); return;
262 
263     }
264 }
265 
266 /* Function:  error_flag_off(type,f)
267  *
268  * Descrip:    Turns off the particular type of error flag
269  *             (eg, STDERR etc).
270  *
271  *
272  * Arg:        type [UNKN ] Undocumented argument [int]
273  * Arg:           f [UNKN ] Undocumented argument [Flag]
274  *
275  */
276 # line 255 "wiseerror.dy"
error_flag_off(int type,Flag f)277 void error_flag_off(int type,Flag f)
278 {
279   switch (type) {
280     case FATAL    : fatal_flag     = fatal_flag & ~f; return;
281     case WARNING  : warning_flag   = warning_flag & ~f; return;
282     case INFO     : info_flag      = info_flag & ~f; return;
283     case REPORT   : report_flag    = report_flag & ~f ; return;
284     default : log_full_error(WARNING,0,"In error system, tried to change flag %d which doesn't exist!",type); return;
285     }
286 }
287 
288 /* Function:  catch_errors(catch)
289  *
290  * Descrip:    This is a wrapper for the error handling
291  *             system. It does the following things
292  *
293  *             Sets function as the function to process errors
294  *
295  *             Switches the INFO,ERROR and FATAL flags off on STDERR
296  *             and on to ERROR CALL.
297  *
298  *
299  * Arg:        catch [FUNCP]  [void (*catch]
300  *
301  * Return [UNKN ]  Undocumented return value [boolean]
302  *
303  */
304 # line 277 "wiseerror.dy"
catch_errors(void (* catch)(char *,int))305 boolean catch_errors(void (*catch)(char *,int))
306 {
307   erroron(INFO);
308   erroron(WARNING);
309   erroron(FATAL);
310 
311   errorstderroff(INFO);
312   errorstderroff(WARNING);
313   errorstderroff(FATAL);
314 
315   errorcallon(INFO);
316   errorcallon(WARNING);
317   errorcallon(FATAL);
318 
319   push_error_call(catch);
320 
321   return TRUE;
322 }
323 
324 /* Function:  stop_catching_errors(void)
325  *
326  * Descrip:    Switches off error catching,
327  *             putting flags back on to STDERR
328  *
329  *
330  *
331  * Return [UNKN ]  Undocumented return value [boolean]
332  *
333  */
334 # line 300 "wiseerror.dy"
stop_catching_errors(void)335 boolean stop_catching_errors(void)
336 {
337   errorstderron(INFO);
338   errorstderron(WARNING);
339   errorstderron(FATAL);
340 
341   errorcalloff(INFO);
342   errorcalloff(WARNING);
343   errorcalloff(FATAL);
344 
345   pop_error_call();
346 
347   return TRUE;
348 }
349 
350 /* Function:  push_error_call()
351  *
352  * Descrip:    Registers this function for dealing with errors
353  *
354  *             Try to use catch_errors instead
355  *
356  *
357  *
358  * Arg:         [UNKN ] Undocumented argument [NullString]
359  *
360  */
361 # line 321 "wiseerror.dy"
push_error_call(void (* func)(char *,int))362 void push_error_call(void (* func)(char *,int))
363 {
364   error_call=func;
365 }
366 
367 /* Function:  pop_error_call(void)
368  *
369  * Descrip:    Discards current function for dealing with errors
370  *
371  *
372  *
373  */
374 # line 329 "wiseerror.dy"
pop_error_call(void)375 void pop_error_call(void)
376 {
377   error_call=NULL;
378 }
379 
380 /* Function:  type_to_error(type)
381  *
382  * Descrip:    Turns int error types to Names
383  *             for display purposes.
384  *
385  *
386  * Arg:        type [UNKN ] Undocumented argument [int]
387  *
388  * Return [UNKN ]  Undocumented return value [char *]
389  *
390  */
391 # line 338 "wiseerror.dy"
type_to_error(int type)392 char * type_to_error(int type)
393 {
394   switch (type) {
395   case FATAL :  return "Fatal Error";
396   case WARNING : return "Warning Error";
397   case INFO   :  return "Information";
398   case REPORT :  return "Debug Report";
399   default	: return "Strange error type???";
400   }
401 }
402 
403 /* Function:  info(msg,)
404  *
405  * Descrip:    Produces a 'info' error message.
406  *
407  *
408  * Arg:        msg [UNKN ] Undocumented argument [char *]
409  * Arg:            [UNKN ] Undocumented argument [.]
410  *
411  */
412 # line 352 "wiseerror.dy"
info(char * msg,...)413 void info(char * msg, ...)
414 {
415   char buffer[1024];
416   va_list ap;
417   int type = INFO;
418 
419   va_start(ap,msg);
420   vsprintf(buffer,msg,ap);
421 
422   eventc++;
423 
424   show_error(flag_of_type(type),buffer,type);
425 
426   return;
427 }
428 
429 /* Function:  debug_report(msg,)
430  *
431  * Descrip:    Produces a 'debug report' error message.
432  *
433  *
434  * Arg:        msg [UNKN ] Undocumented argument [char *]
435  * Arg:            [UNKN ] Undocumented argument [.]
436  *
437  */
438 # line 371 "wiseerror.dy"
debug_report(char * msg,...)439 void debug_report(char * msg, ...)
440 {
441   char buffer[1024];
442   va_list ap;
443   int type = REPORT;
444 
445   va_start(ap,msg);
446   vsprintf(buffer,msg,ap);
447 
448   eventc++;
449 
450   show_error(flag_of_type(type),buffer,type);
451 
452   return;
453 }
454 
455 /* Function:  warn(msg,)
456  *
457  * Descrip:    Produces a 'warn' error message.
458  *
459  *
460  * Arg:        msg [UNKN ] Undocumented argument [char *]
461  * Arg:            [UNKN ] Undocumented argument [.]
462  *
463  */
464 # line 390 "wiseerror.dy"
warn(char * msg,...)465 void warn(char * msg, ...)
466 {
467   char buffer[1024];
468   va_list ap;
469   int type = WARNING;
470 
471   va_start(ap,msg);
472   vsprintf(buffer,msg,ap);
473 
474   eventc++;
475 
476   show_error(flag_of_type(type),buffer,type);
477 
478   return;
479 }
480 
481 /* Function:  fatal(msg,)
482  *
483  * Descrip:    Produces a 'fatal' error message.
484  *
485  *
486  * Arg:        msg [UNKN ] Undocumented argument [char *]
487  * Arg:            [UNKN ] Undocumented argument [.]
488  *
489  */
490 # line 409 "wiseerror.dy"
fatal(char * msg,...)491 void fatal(char * msg, ...)
492 {
493   char buffer[1024];
494   va_list ap;
495   int type = FATAL;
496 
497   va_start(ap,msg);
498   vsprintf(buffer,msg,ap);
499 
500   eventc++;
501 
502   show_error(flag_of_type(type),buffer,type);
503   fputc('\n',stderr);
504   exit(2);
505 
506   return;
507 }
508 
509 /* Function:  log_full_error(type,location,msg,)
510  *
511  * Descrip:    Deprecated
512  *
513  *             produces any of the error types
514  *
515  *
516  * Arg:            type [UNKN ] Undocumented argument [int]
517  * Arg:        location [UNKN ] Undocumented argument [int]
518  * Arg:             msg [UNKN ] Undocumented argument [char *]
519  * Arg:                 [UNKN ] Undocumented argument [.]
520  *
521  */
522 # line 432 "wiseerror.dy"
log_full_error(int type,int location,char * msg,...)523 void log_full_error(int type,int location,char * msg, ...)
524 {
525   char buffer[1024];
526   va_list ap;
527 
528   va_start(ap,msg);
529   vsprintf(buffer,msg,ap);
530 
531   eventc++;
532 
533   if(type == FATAL) {
534     show_error(fatal_flag,buffer,FATAL);
535     fputc('\n',stderr);
536     exit(2);
537   }
538 
539   show_error(flag_of_type(type),buffer,type);
540 
541   return;
542 }
543 
544 /* Function:  start_reporting(msg,)
545  *
546  * Descrip:    Starts a % reporting run. This is the header message
547  *
548  *
549  * Arg:        msg [UNKN ] Undocumented argument [char *]
550  * Arg:            [UNKN ] Undocumented argument [.]
551  *
552  */
553 # line 456 "wiseerror.dy"
start_reporting(char * msg,...)554 void start_reporting(char * msg,...)
555 {
556   char buffer[1024];
557   va_list ap;
558 
559   if( !(report_flag & ERRORUSE) )
560     return;
561 
562   va_start(ap,msg);
563   vsprintf(buffer,msg,ap);
564 
565   fputs(buffer,stderr);
566   start_overlay(stderr);
567 }
568 
569 /* Function:  stop_reporting(void)
570  *
571  * Descrip:    Stops a % reporting run.
572  *
573  *
574  *
575  */
576 # line 474 "wiseerror.dy"
stop_reporting(void)577 void stop_reporting(void)
578 {
579 
580   if( !(report_flag & ERRORUSE) )
581     return;
582   stop_overlay();
583 }
584 
585 /* Function:  show_error(flag,othermsg,type)
586  *
587  * Descrip:    Actually shows the error
588  *
589  *
590  * Arg:            flag [UNKN ] Undocumented argument [Flag]
591  * Arg:        othermsg [UNKN ] Undocumented argument [char *]
592  * Arg:            type [UNKN ] Undocumented argument [int]
593  *
594  */
595 # line 486 "wiseerror.dy"
show_error(Flag flag,char * othermsg,int type)596 void show_error(Flag flag,char * othermsg,int type)
597 {
598   time_t curr_time;
599   struct tm * loctime;
600   char time_buffer[50];
601 
602   if( !(flag & ERRORUSE) )
603     return;
604 
605 
606   if( type == REPORT ) {
607     print_overlay(othermsg);
608     return;
609   }
610 
611   if(flag&ERRORTOSTDERR) {
612     if( display_type == ERROR_DISPLAY_SERVER ) {
613       curr_time = time(NULL);
614       loctime = localtime(&curr_time);
615 
616       asctime_r(loctime,time_buffer);
617       time_buffer[24] = '\0';
618 
619       fprintf(stderr,"%s: [%s] [%s] %s\n",type_to_error(type),
620 	      log_display_string == NULL ? "[no log string]" : log_display_string,
621 	      time_buffer,
622 	      othermsg);
623     } else {
624       fputs(type_to_error(type),stderr);
625       fputc('\n',stderr);
626       if( msg_stack_no > 0 )
627 	show_message_stack(stderr);
628       fputs("\t",stderr);
629       if( type == FATAL )
630 	fputs(othermsg,stderr);
631       else	show_text(othermsg,70,stderr);
632     }
633   }
634 
635   if( flag&ERRORTOLOG && errlog != NULL) {
636     fputs(type_to_error(type),errlog);
637     fputc('\n',stderr);
638     if( msg_stack_no > 0 )
639       show_message_stack(errlog);
640     fputs("\n\t",errlog);
641     show_text(othermsg,70,errlog);
642   }
643 
644   if( flag&ERRORTOCALL && error_call != NULL)
645     {
646       (*(error_call))(othermsg,type);
647     }
648 
649 
650 
651   return;
652 }
653 
654 
655 # line 604 "wiseerror.c"
656 
657 #ifdef _cplusplus
658 }
659 #endif
660