File: bug.c

package info (click to toggle)
casacore 3.8.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,908 kB
  • sloc: cpp: 471,569; fortran: 16,372; ansic: 7,416; yacc: 4,714; lex: 2,346; sh: 1,865; python: 629; perl: 531; sed: 499; csh: 201; makefile: 32
file content (370 lines) | stat: -rw-r--r-- 13,762 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/************************************************************************/
/*									*/
/* This handles errors and can abort your program.			*/
/*									*/
/*  History:								*/
/*    rjs,mjs ????    Very mixed history. Created, destroyed, rewritten.*/
/*    rjs     26aug93 Call habort_c.					*/
/*    rjs     14jul98 Add a caste operation in errmsg_c, to attempt	*/
/*		      to appease some compilers.			*/
/*    pjt     23sep01 darwin						*/
/*    pjt      4dec01 bypass fatal errors (for alien clients) if req'd  */
/*                    through the new bugrecover_c() routine            */
/*    pjt     17jun02 prototypes for MIR4                               */
/*    pjt/ram  5dec03 using strerror() for unix                         */
/*    pjt      1jan05 bugv_c: finally, a real stdargs version!!!        */
/*                    though cannot be exported to Fortran              */
/*    pjt     26mar07 bugmessage_c: retrieve last fatal bug message     */
/*    pjt     27mar07 bugseverity_c: also overhauled bug recovery       */
/*                    and removed VMS specific code                     */
/*    pjt     17may07 removed old-non ANSI declaration                  */
/*    pjt      5dec07 add Name to bug output - why took us so long?     */
/*    pkgw     6mar08 declare Name as static to prevent symbol clashes  */
/*    dhem    12feb09 added hooks to allow alien clients to completely  */
/*                    override the default bug handler                  */
/*    pkgw    14dec11 Make errmsg_c public for use in uvio.c            */
/************************************************************************/

#define X_OPEN_SOURCE 500 // For strdup definition
#define _DEFAULT_SOURCE // For sys_nerr

#if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "miriad.h"

char *errmsg_c(int n);
static int  handle_bug_cleanup(int d, char s, Const char *m);

static char *Name = NULL;     /* a slot to store the program name       */
int reentrant=0;              /* keep track of state                    */

/* helper definitions for function pointers */
typedef void (*bug_cleanup_proc)(void);
typedef void (*bug_handler_proc)(char s, Const char *m);

/* external bug cleanup handler, if any */
/* only used by internal bug handler    */
static bug_cleanup_proc bug_cleanup=NULL;

/* bug handler function pointer */
static bug_handler_proc bug_handler=NULL;

/* forward declaration */
static void default_bug_handler_c(char s,Const char *m);

static char *bug_message=0;   /* last message                           */ 
static char bug_severity=0;   /* last severity level (i,w,e or f)       */


#define MAXMSG 256
static char msg[MAXMSG];      /* formatted message for bugv_c()         */


/************************************************************************/
char *bugmessage_c(void)
/** bugmessage_c -- return last fatal error message string              */
/*& pjt                                                                 */
/*: error-handling                                                      */
/*+                                                                    
    This routine does not have a FORTRAN counterpart, as it is normally 
    only called by C clients who have set their own error handler if
    for some reason they don't like the MIRIAD one (e.g. C++ or java
    exceptions, or NEMO's error handler. This way the bugrecover handler
    can call this routine to retrieve the last fatal error message. 

    bugrecover_c(my_handler);
    
    void my_handler(void) {
       char *m = bugmessage_c();
       printf("RECOVERED: %s\n",m);
    }
    ..                                                                  */
/*--                                                                    */
/*----------------------------------------------------------------------*/
{
  return bug_message;
}

/************************************************************************/
char bugseverity_c(void)
/** bugseverity_c -- return last severity level                         */
/*& pjt                                                                 */
/*: error-handling                                                      */
/*+                                                                    
    This routine does not have a FORTRAN counterpart, as it is normally 
    only called by C clients who have set their own error handler if
    for some reason they don't like the MIRIAD one (e.g. C++ or java
    exceptions, or NEMO's error handler. This way the bugrecover handler
    can call this routine to retrieve the last severity level 

    bugrecover_c(my_handler);
    
    void my_handler(void) {
       char  s = bugseverity_c();
       char *m = bugmessage_c();
       printf("RECOVERED: (%c) %s\n",s,m);
       if (s=='f') exit(1);
    }
    ..                                                                  */
/*--                                                                    */
/*----------------------------------------------------------------------*/
{
  return bug_severity;
}

/************************************************************************/
void bughandler_c(bug_handler_proc new_bug_handler)
/** bughandler_c -- specify the bug handler callback function           */
/*& dhem                                                                */
/*: error-handling                                                      */
/*+                                                                    
    This routine does not have a FORTRAN counterpart, as it is normally 
    only called by C clients who need to set their own error handler if
    for some reason they don't like the MIRIAD one (e.g. C++ or java
    exceptions, or NEMO's error handler, or scripting languages such as
    Ruby and Python that provide their own exception handling
    capabilities).
    
    Absolutely nothing is done before or after the bug handler is
    called.  This is even more of an override than bugrecover_c
    provides because on fatal errors, habort_c is called before the bug
    cleanup routine installed by bugrecover_c is called.  Another
    difference is that bug_message and bug_severity are not set;
    instead the severity and message are passed as parameters to the
    bug handler callback function..

    If NULL is passed as the new_bug_handler parameter, the default bug
    handler will be reinstated.

    Example of usage:

    void my_bug_handler(char s, onst char *m) {
        ....
    }


    ..
    bughandler_c(my_bug_handler);
    ..                                                                  */
/*--                                                                    */
/*----------------------------------------------------------------------*/
{
    bug_handler = new_bug_handler;
    if (bug_message) free(bug_message);
    bug_message = strdup("no bug_message has been set yet");
}

/************************************************************************/
void bugrecover_c(void (*cl)(void))
/** bugrecover_c -- bypass fatal bug calls for alien clients            */
/*& pjt                                                                 */
/*: error-handling                                                      */
/*+                                                                    
    This routine does not have a FORTRAN counterpart, as it is normally 
    only called by C clients who need to set their own error handler if
    for some reason they don't like the MIRIAD one (e.g. C++ or java
    exceptions, or NEMO's error handler 
    Example of usage:

    void my_bug_cleanup(void) {
        ....
    }


    ..
    bugrecover_c(my_bug_cleanup);
    ..                                                                  */
/*--                                                                    */
/*----------------------------------------------------------------------*/
{
    bug_cleanup = cl;
    if (bug_message) free(bug_message);
    bug_message = strdup("no bug_message has been set yet");
}

/************************************************************************/
void buglabel_c(Const char *name)
/** buglabel -- Give the "program name" to be used as a label in messages. */
/*& pjt									*/
/*: error-handling							*/
/*+ FORTRAN call sequence:
	subroutine buglabel(name)

	implicit none
	character name*(*)

  Give the name that is to be used as a label in error messages. Usually
  this is the program name and should be set by the user interface.

  Input:
    name	The name to be given as a label in error messages.	*/
/*--									*/
/*----------------------------------------------------------------------*/
{
  if(Name != NULL)free(Name);
  Name = malloc(strlen(name)+1);
  strcpy(Name,name);
}
/************************************************************************/
void bug_c(char s,Const char *m)
/** bug -- Issue an error message, given by the caller.			*/
/*& pjt									*/
/*: error-handling							*/
/*+ FORTRAN call sequence:
	subroutine bug(severity,message)

	implicit none
	character severity*1
	character message*(*)

  Output the error message given by the caller, and abort if needed.

  Input:
    severity	Error severity. Can be one of 'i', 'w', 'e' or 'f'
		for "informational", "warning", "error", or "fatal"
    message	The error message text.					*/
/*--									*/
/*----------------------------------------------------------------------*/
{
  if(bug_handler == NULL) {
    bug_handler = default_bug_handler_c;
  }

  bug_handler(s, m);
}
/************************************************************************/
static void default_bug_handler_c(char s, Const char *m)
/*
  Default bug handler.
------------------------------------------------------------------------*/
{
  char *p;
  int doabort;

  doabort = 0;
  if      (s == 'i' || s == 'I') p = "Informational";
  else if (s == 'w' || s == 'W') p = "Warning";
  else if (s == 'e' || s == 'E') p = "Error";
  else {doabort = 1;		 p = "Fatal Error"; }

  if (!bug_cleanup)
  {
    if ( Name == NULL )
      buglabel_c("(NOT SET)");
    fprintf(stderr,"### %s [%s]:  %s\n",p,Name,m);
  }

  if(doabort){
    reentrant = !reentrant;
    if(reentrant)habort_c();
    if (!handle_bug_cleanup(doabort,s,m))
      exit(1);
  } else
    handle_bug_cleanup(doabort,s,m);
}
/************************************************************************/
void bugv_c(char s,Const char *m, ...)
/** bugv_c -- Issue a dynamic error message, given by the caller.	*/
/*& pjt									*/
/*: error-handling							*/
/*+ C call sequence:
	bugv_c(severity,message,....)

  Output the error message given by the caller, and abort if needed.
  Note this routine has no counterpart in FORTRAN.

  Input:
    severity	Error severity character. 
                Can be one of 'i', 'w', 'e' or 'f'
		for "informational", "warning", "error", or "fatal"
    message	The error message string, can contain %-printf style 
                directives, as used by the following arguments.
     ...         Optional argument, in the printf() style               */
/*--									*/
/*----------------------------------------------------------------------*/
{
  va_list ap;

  va_start(ap,m);
  vsnprintf(msg,MAXMSG,m,ap);
  msg[MAXMSG-1] = '\0'; /* backstop */
  va_end(ap);

  bug_c(s, msg);
}

/************************************************************************/
void bugno_c(char s,int n)
/** bugno -- Issue an error message, given a system error number.	*/
/*& pjt									*/
/*: error-handling							*/
/*+ FORTRAN call sequence:
	subroutine bugno(severity,errno)

	implicit none
	character severity*1
	integer errno

  Output the error message associated with a particular error number.

  Input:
    severity	Error severity. Can be one of 'i', 'w', 'e' or 'f'
		for "informational", "warning", "error", or "fatal"
    errno	host error number.					*/
/*--									*/
/*----------------------------------------------------------------------*/
{
  if (n == -1)bug_c(s,"End of file detected");
  else bug_c(s,errmsg_c(n));
}
/************************************************************************/
char *errmsg_c(int n)
/*
  Return the error message associated with some error number.
------------------------------------------------------------------------*/
{
/* check for linux leaves this compat with old style build
 * this should be removed in favor of HAVE_STRERROR once
 * is only supported using autotools/configure
 */
#if defined(linux) || defined(__GNU__) || (defined(HAVE_STRERROR) && HAVE_STRERROR)
  /* new POSIX.1 style, 20 years old now... (1988) */
  if(n == -1)
    return "End of file detected";
  return strerror(n);
#else
  /* very old style code -- stdio.h is supposed to supply this */
#  if 0
  extern int sys_nerr;
  extern char *sys_errlist[];
#  endif
  if(n > 0 && n <= sys_nerr)return((char *)sys_errlist[n]);
  else {
    sprintf(msg,"Unknown error with number %d detected.",n);
    return msg;
  }
#endif
}
/************************************************************************/
static int handle_bug_cleanup(int doabort, char s, Const char *m)
/*
  Handle cleaning up a bug 
------------------------------------------------------------------------*/
{
  if (bug_cleanup) {
    if (bug_message) free(bug_message);
    bug_message = strdup(m);      /* save last message */
    bug_severity = s;             /* save last severity */
    (*bug_cleanup)();             /* call handler ; this may exit */
    if (doabort)                  /* if it got here, handler didn't exit */
      fprintf(stderr,"### handle_bug_cleanup: WARNING: code should not come here\n");
    return 1;
  }
  return 0;
}