FreeBASIC  0.91.0
error.c
Go to the documentation of this file.
1 /* runtime error handling */
2 
3 #include "fb.h"
4 
5 static const char *messages[] = {
6  "", /* FB_RTERROR_OK */
7  "illegal function call", /* FB_RTERROR_ILLEGALFUNCTIONCALL */
8  "file not found", /* FB_RTERROR_FILENOTFOUND */
9  "file I/O error", /* FB_RTERROR_FILEIO */
10  "out of memory", /* FB_RTERROR_OUTOFMEM */
11  "illegal resume", /* FB_RTERROR_ILLEGALRESUME */
12  "out of bounds array access", /* FB_RTERROR_OUTOFBOUNDS */
13  "null pointer access", /* FB_RTERROR_NULLPTR */
14  "no priviledges", /* FB_RTERROR_NOPRIVILEDGES */
15  "\"interrupted\" signal", /* FB_RTERROR_SIGINT */
16  "\"illegal instruction\" signal", /* FB_RTERROR_SIGILL */
17  "\"floating point error\" signal", /* FB_RTERROR_SIGFPE */
18  "\"segmentation violation\" signal", /* FB_RTERROR_SIGSEGV */
19  "\"termination request\" signal", /* FB_RTERROR_SIGTERM */
20  "\"abnormal termination\" signal", /* FB_RTERROR_SIGTERM */
21  "\"quit request\" signal", /* FB_RTERROR_SIGABRT */
22  "return without gosub", /* FB_RTERROR_RETURNWITHOUTGOSUB */
23  "end of file" /* FB_RTERROR_ENDOFFILE */
24 };
25 
26 static void fb_Die
27  (
28  int err_num,
29  int line_num,
30  const char *mod_name,
31  const char *fun_name
32  )
33 {
34  int pos = 0;
35 
36  pos += snprintf( &__fb_errmsg[pos], FB_ERRMSG_SIZE - pos,
37  "\nAborting due to runtime error %d", err_num );
38 
39  if( (err_num >= 0) && (err_num < FB_RTERROR_MAX) )
40  pos += snprintf( &__fb_errmsg[pos], FB_ERRMSG_SIZE - pos,
41  " (%s)", messages[err_num] );
42 
43  if( line_num > 0 )
44  pos += snprintf( &__fb_errmsg[pos], FB_ERRMSG_SIZE - pos,
45  " at line %d", line_num );
46 
47  if( mod_name != NULL )
48  if( fun_name != NULL )
49  pos += snprintf( &__fb_errmsg[pos], FB_ERRMSG_SIZE - pos,
50  " %s %s::%s()\n\n", (char *)(line_num > 0? &"of" : &"in"),
51  (char *)mod_name, (char *)fun_name );
52  else
53  pos += snprintf( &__fb_errmsg[pos], FB_ERRMSG_SIZE - pos,
54  " %s %s()\n\n", (char *)(line_num > 0? &"of" : &"in"),
55  (char *)mod_name );
56  else
57  pos += snprintf( &__fb_errmsg[pos], FB_ERRMSG_SIZE - pos, "\n\n" );
58 
59  __fb_errmsg[FB_ERRMSG_SIZE-1] = '\0';
60 
61  /* Let fb_hRtExit() show the message */
63 
64  fb_End( err_num );
65 }
66 
68  (
69  int err_num,
70  int line_num,
71  const char *mod_name,
72  void *res_label,
73  void *resnext_label
74  )
75 {
76  FB_ERRORCTX *ctx = FB_TLSGETCTX( ERROR );
77 
78  if( ctx->handler )
79  {
80  ctx->err_num = err_num;
81  ctx->line_num = line_num;
82  if( mod_name != NULL )
83  ctx->mod_name = mod_name;
84  ctx->res_lbl = res_label;
85  ctx->resnxt_lbl = resnext_label;
86 
87  return ctx->handler;
88  }
89 
90  /* if no user handler defined, die */
91  fb_Die( err_num,
92  line_num,
93  (mod_name != NULL? mod_name: ctx->mod_name),
94  ctx->fun_name );
95 
96  return NULL;
97 }
98 
100  (
101  int line_num,
102  const char *mod_name,
103  void *res_label,
104  void *resnext_label
105  )
106 {
107  FB_ERRORCTX *ctx = FB_TLSGETCTX( ERROR );
108 
109  return fb_ErrorThrowEx( ctx->err_num, line_num, mod_name, res_label, resnext_label );
110 
111 }
112 
114 {
115  FB_ERRORCTX *ctx = FB_TLSGETCTX( ERROR );
116  FB_ERRHANDLER oldhandler;
117 
118  oldhandler = ctx->handler;
119 
120  ctx->handler = newhandler;
121 
122  return oldhandler;
123 }
124 
125 void *fb_ErrorResume( void )
126 {
127  FB_ERRORCTX *ctx = FB_TLSGETCTX( ERROR );
128  void *label = ctx->res_lbl;
129 
130  /* not defined? die */
131  if( label == NULL )
133 
134  /* don't loop forever */
135  ctx->res_lbl = NULL;
136  ctx->resnxt_lbl = NULL;
137 
138  return label;
139 }
140 
141 void *fb_ErrorResumeNext( void )
142 {
143  FB_ERRORCTX *ctx = FB_TLSGETCTX( ERROR );
144  void *label = ctx->resnxt_lbl;
145 
146  /* not defined? die */
147  if( label == NULL )
149 
150  /* don't loop forever */
151  ctx->res_lbl = NULL;
152  ctx->resnxt_lbl = NULL;
153 
154  return label;
155 }