| 1 |
|
| /* |
| 2 |
|
| =head1 NAME |
| 3 |
|
| |
| 4 |
|
| error.c - error reporting code for Imager |
| 5 |
|
| |
| 6 |
|
| =head1 SYNOPSIS |
| 7 |
|
| |
| 8 |
|
| // user code: |
| 9 |
|
| int new_fatal; // non-zero if errors are fatal |
| 10 |
|
| int old_fatal = i_set_failure_fatal(new_fatal); |
| 11 |
|
| i_set_argv0("name of your program"); |
| 12 |
|
| extern void error_cb(char const *); |
| 13 |
|
| i_error_cb old_ecb; |
| 14 |
|
| old_ecb = i_set_error_cb(error_cb); |
| 15 |
|
| i_failed_cb old_fcb; |
| 16 |
|
| extern void failed_cb(char **errors); |
| 17 |
|
| old_fcb = i_set_failed_cb(failed_cb); |
| 18 |
|
| if (!i_something(...)) { |
| 19 |
|
| char **errors = i_errors(); |
| 20 |
|
| } |
| 21 |
|
| |
| 22 |
|
| // imager code: |
| 23 |
|
| undef_int i_something(...) { |
| 24 |
|
| i_clear_error(); |
| 25 |
|
| if (!some_lower_func(...)) { |
| 26 |
|
| return i_failed("could not something"); |
| 27 |
|
| } |
| 28 |
|
| return 1; |
| 29 |
|
| } |
| 30 |
|
| undef_int some_lower_func(...) { |
| 31 |
|
| if (somethingelse_failed()) { |
| 32 |
|
| i_push_error("could not somethingelse"); |
| 33 |
|
| return 0; |
| 34 |
|
| } |
| 35 |
|
| return 1; |
| 36 |
|
| } |
| 37 |
|
| |
| 38 |
|
| =head1 DESCRIPTION |
| 39 |
|
| |
| 40 |
|
| This module provides the C level error handling functionality for |
| 41 |
|
| Imager. |
| 42 |
|
| |
| 43 |
|
| A few functions return or pass in an i_errmsg *, this is list of error |
| 44 |
|
| structures, terminated by an entry with a NULL msg value, each of |
| 45 |
|
| which contains a msg and an error code. Even though these aren't |
| 46 |
|
| passed as i_errmsg const * pointers, don't modify the strings |
| 47 |
|
| or the pointers. |
| 48 |
|
| |
| 49 |
|
| The interface as currently defined isn't thread safe, unfortunately. |
| 50 |
|
| |
| 51 |
|
| This code uses Imager's mymalloc() for memory allocation, so out of |
| 52 |
|
| memory errors are I<always> fatal. |
| 53 |
|
| |
| 54 |
|
| =head1 INTERFACE |
| 55 |
|
| |
| 56 |
|
| These functions form the interface that a user of Imager sees (from |
| 57 |
|
| C). The Perl level won't use all of this. |
| 58 |
|
| |
| 59 |
|
| =over |
| 60 |
|
| |
| 61 |
|
| =cut |
| 62 |
|
| */ |
| 63 |
|
| |
| 64 |
|
| #include "imageri.h" |
| 65 |
|
| #include <stdio.h> |
| 66 |
|
| #include <stdlib.h> |
| 67 |
|
| |
| 68 |
|
| /* |
| 69 |
|
| =item im_errors(ctx) |
| 70 |
|
| =synopsis i_errmsg *errors = im_errors(aIMCTX); |
| 71 |
|
| =synopsis i_errmsg *errors = i_errors(); |
| 72 |
|
| |
| 73 |
|
| Returns a pointer to the first element of an array of error messages, |
| 74 |
|
| terminated by a NULL pointer. The highest level message is first. |
| 75 |
|
| |
| 76 |
|
| Also callable as C<i_errors()>. |
| 77 |
|
| |
| 78 |
|
| =cut |
| 79 |
|
| */ |
| 80 |
569 |
| i_errmsg *im_errors(im_context_t ctx) { |
| 81 |
569 |
| return ctx->error_stack + ctx->error_sp; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
|
| /* |
| 85 |
|
| =back |
| 86 |
|
| |
| 87 |
|
| =head1 INTERNAL FUNCTIONS |
| 88 |
|
| |
| 89 |
|
| These functions are called by Imager to report errors through the |
| 90 |
|
| above interface. |
| 91 |
|
| |
| 92 |
|
| It may be desirable to have functions to mark the stack and reset to |
| 93 |
|
| the mark. |
| 94 |
|
| |
| 95 |
|
| =over |
| 96 |
|
| |
| 97 |
|
| =item im_clear_error(ctx) |
| 98 |
|
| X<im_clear_error API>X<i_clear_error API> |
| 99 |
|
| =synopsis im_clear_error(aIMCTX); |
| 100 |
|
| =synopsis i_clear_error(); |
| 101 |
|
| =category Error handling |
| 102 |
|
| |
| 103 |
|
| Clears the error stack. |
| 104 |
|
| |
| 105 |
|
| Called by any Imager function before doing any other processing. |
| 106 |
|
| |
| 107 |
|
| Also callable as C<i_clear_error()>. |
| 108 |
|
| |
| 109 |
|
| =cut |
| 110 |
|
| */ |
| 111 |
|
| |
| 112 |
|
| void |
| 113 |
3399 |
| im_clear_error(im_context_t ctx) { |
| 114 |
|
| int i; |
| 115 |
|
| |
| 116 |
71379 |
▶ | for (i = 0; i < IM_ERROR_COUNT; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < IM_ERROR_COUNT; ++i) { |
67980 |
3399 |
71379 |
|
| 117 |
67980 |
▶ | if (ctx->error_alloc[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->error_alloc[i]) { |
780 |
67200 |
67980 |
|
| 118 |
780 |
| myfree(ctx->error_stack[i].msg); |
| 119 |
780 |
| ctx->error_stack[i].msg = NULL; |
| 120 |
780 |
| ctx->error_alloc[i] = 0; |
| 121 |
|
| } |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
3399 |
| ctx->error_sp = IM_ERROR_COUNT-1; |
| 125 |
3399 |
| } |
| 126 |
|
| |
| 127 |
|
| /* |
| 128 |
|
| =item im_push_error(ctx, code, message) |
| 129 |
|
| X<im_push_error API>X<i_push_error API> |
| 130 |
|
| =synopsis i_push_error(0, "Yep, it's broken"); |
| 131 |
|
| =synopsis i_push_error(errno, "Error writing"); |
| 132 |
|
| =synopsis im_push_error(aIMCTX, 0, "Something is wrong"); |
| 133 |
|
| =category Error handling |
| 134 |
|
| |
| 135 |
|
| Called by an Imager function to push an error message onto the stack. |
| 136 |
|
| |
| 137 |
|
| No message is pushed if the stack is full (since this means someone |
| 138 |
|
| forgot to call i_clear_error(), or that a function that doesn't do |
| 139 |
|
| error handling is calling function that does.). |
| 140 |
|
| |
| 141 |
|
| =cut |
| 142 |
|
| */ |
| 143 |
|
| void |
| 144 |
788 |
| im_push_error(im_context_t ctx, int code, char const *msg) { |
| 145 |
788 |
| size_t size = strlen(msg)+1; |
| 146 |
|
| |
| 147 |
788 |
▶ | if (ctx->error_sp <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->error_sp <= 0) |
0 |
788 |
788 |
|
| 148 |
|
| /* bad, bad programmer */ |
| 149 |
0 |
| return; |
| 150 |
|
| |
| 151 |
788 |
| --ctx->error_sp; |
| 152 |
788 |
▶ | if (ctx->error_alloc[ctx->error_sp] < size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->error_alloc[ctx->error_sp] < size) { |
788 |
0 |
788 |
|
| 153 |
788 |
▶ | if (ctx->error_stack[ctx->error_sp].msg) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->error_stack[ctx->error_sp].msg) |
0 |
788 |
788 |
|
| 154 |
0 |
| myfree(ctx->error_stack[ctx->error_sp].msg); |
| 155 |
|
| /* memory allocated on the following line is only ever released when |
| 156 |
|
| we need a bigger string */ |
| 157 |
|
| /* size is size (len+1) of an existing string, overflow would mean |
| 158 |
|
| the system is broken anyway */ |
| 159 |
788 |
| ctx->error_stack[ctx->error_sp].msg = mymalloc(size); /* checked 17jul05 tonyc */ |
| 160 |
788 |
| ctx->error_alloc[ctx->error_sp] = size; |
| 161 |
|
| } |
| 162 |
788 |
| strcpy(ctx->error_stack[ctx->error_sp].msg, msg); |
| 163 |
788 |
| ctx->error_stack[ctx->error_sp].code = code; |
| 164 |
|
| } |
| 165 |
|
| |
| 166 |
|
| /* |
| 167 |
|
| =item im_push_errorvf(ctx, code, format, args) |
| 168 |
|
| X<im_push_error_vf API>X<i_push_errorvf API> |
| 169 |
|
| =synopsis va_args args; |
| 170 |
|
| =synopsis va_start(args, lastarg); |
| 171 |
|
| =synopsis im_push_errorvf(ctx, code, format, args); |
| 172 |
|
| =category Error handling |
| 173 |
|
| |
| 174 |
|
| Intended for use by higher level functions, takes a varargs pointer |
| 175 |
|
| and a format to produce the finally pushed error message. |
| 176 |
|
| |
| 177 |
|
| Does not support perl specific format codes. |
| 178 |
|
| |
| 179 |
|
| Also callable as C<i_push_errorvf(code, format, args)> |
| 180 |
|
| |
| 181 |
|
| =cut |
| 182 |
|
| */ |
| 183 |
|
| void |
| 184 |
171 |
| im_push_errorvf(im_context_t ctx, int code, char const *fmt, va_list ap) { |
| 185 |
|
| char buf[1024]; |
| 186 |
|
| #if defined(IMAGER_VSNPRINTF) |
| 187 |
171 |
| vsnprintf(buf, sizeof(buf), fmt, ap); |
| 188 |
|
| #elif defined(_MSC_VER) |
| 189 |
|
| _vsnprintf(buf, sizeof(buf), fmt, ap); |
| 190 |
|
| #else |
| 191 |
|
| /* is there a way to detect vsnprintf()? |
| 192 |
|
| for this and other functions we need some mechanism to handle |
| 193 |
|
| detection (like perl's Configure, or autoconf) |
| 194 |
|
| */ |
| 195 |
|
| vsprintf(buf, fmt, ap); |
| 196 |
|
| #endif |
| 197 |
171 |
| im_push_error(ctx, code, buf); |
| 198 |
171 |
| } |
| 199 |
|
| |
| 200 |
|
| /* |
| 201 |
|
| =item i_push_errorf(int code, char const *fmt, ...) |
| 202 |
|
| =synopsis i_push_errorf(errno, "Cannot open file %s: %d", filename, errno); |
| 203 |
|
| =category Error handling |
| 204 |
|
| |
| 205 |
|
| A version of i_push_error() that does printf() like formatting. |
| 206 |
|
| |
| 207 |
|
| Does not support perl specific format codes. |
| 208 |
|
| |
| 209 |
|
| =cut |
| 210 |
|
| */ |
| 211 |
|
| void |
| 212 |
92 |
| i_push_errorf(int code, char const *fmt, ...) { |
| 213 |
|
| va_list ap; |
| 214 |
92 |
| va_start(ap, fmt); |
| 215 |
92 |
| i_push_errorvf(code, fmt, ap); |
| 216 |
92 |
| va_end(ap); |
| 217 |
92 |
| } |
| 218 |
|
| |
| 219 |
|
| /* |
| 220 |
|
| =item im_push_errorf(ctx, code, char const *fmt, ...) |
| 221 |
|
| =synopsis im_push_errorf(aIMCTX, errno, "Cannot open file %s: %d", filename, errno); |
| 222 |
|
| =category Error handling |
| 223 |
|
| |
| 224 |
|
| A version of im_push_error() that does printf() like formatting. |
| 225 |
|
| |
| 226 |
|
| Does not support perl specific format codes. |
| 227 |
|
| |
| 228 |
|
| =cut |
| 229 |
|
| */ |
| 230 |
|
| void |
| 231 |
79 |
| im_push_errorf(im_context_t ctx, int code, char const *fmt, ...) { |
| 232 |
|
| va_list ap; |
| 233 |
79 |
| va_start(ap, fmt); |
| 234 |
79 |
| im_push_errorvf(ctx, code, fmt, ap); |
| 235 |
79 |
| va_end(ap); |
| 236 |
79 |
| } |
| 237 |
|
| |
| 238 |
|
| #ifdef IMAGER_I_FAILED |
| 239 |
|
| #error "This isn't used and is untested" |
| 240 |
|
| |
| 241 |
|
| /* |
| 242 |
|
| =item i_failed(char const *msg) |
| 243 |
|
| |
| 244 |
|
| Called by Imager code to indicate that a top-level has failed. |
| 245 |
|
| |
| 246 |
|
| msg can be NULL, in which case no error is pushed. |
| 247 |
|
| |
| 248 |
|
| Calls the current failed callback, if any. |
| 249 |
|
| |
| 250 |
|
| Aborts the program with an error, if failures have been set to be fatal. |
| 251 |
|
| |
| 252 |
|
| Returns zero if it does not abort. |
| 253 |
|
| |
| 254 |
|
| =cut |
| 255 |
|
| */ |
| 256 |
|
| int i_failed(int code, char const *msg) { |
| 257 |
|
| if (msg) |
| 258 |
|
| i_push_error(code, msg); |
| 259 |
|
| if (failed_cb) |
| 260 |
|
| failed_cb(error_stack + error_sp); |
| 261 |
|
| if (failures_fatal) { |
| 262 |
|
| int sp; |
| 263 |
|
| size_t total; /* total length of error messages */ |
| 264 |
|
| char *full; /* full message for logging */ |
| 265 |
|
| if (argv0) |
| 266 |
|
| fprintf(stderr, "%s: ", argv0); |
| 267 |
|
| fputs("error:\n", stderr); |
| 268 |
|
| sp = error_sp; |
| 269 |
|
| while (error_stack[sp].msg) { |
| 270 |
|
| fprintf(stderr, " %s\n", error_stack[sp].msg); |
| 271 |
|
| ++sp; |
| 272 |
|
| } |
| 273 |
|
| /* we want to log the error too, build an error message to hand to |
| 274 |
|
| i_fatal() */ |
| 275 |
|
| total = 1; /* remember the NUL */ |
| 276 |
|
| for (sp = error_sp; error_stack[sp].msg; ++sp) { |
| 277 |
|
| size_t new_total += strlen(error_stack[sp].msg) + 2; |
| 278 |
|
| if (new_total < total) { |
| 279 |
|
| /* overflow, somehow */ |
| 280 |
|
| break; |
| 281 |
|
| } |
| 282 |
|
| } |
| 283 |
|
| full = mymalloc(total); |
| 284 |
|
| if (!full) { |
| 285 |
|
| /* just quit, at least it's on stderr */ |
| 286 |
|
| exit(EXIT_FAILURE); |
| 287 |
|
| } |
| 288 |
|
| *full = 0; |
| 289 |
|
| for (sp = error_sp; error_stack[sp].msg; ++sp) { |
| 290 |
|
| strcat(full, error_stack[sp].msg); |
| 291 |
|
| strcat(full, ": "); |
| 292 |
|
| } |
| 293 |
|
| /* lose the extra ": " */ |
| 294 |
|
| full[strlen(full)-2] = '\0'; |
| 295 |
|
| i_fatal(EXIT_FAILURE, "%s", full); |
| 296 |
|
| } |
| 297 |
|
| |
| 298 |
|
| return 0; |
| 299 |
|
| } |
| 300 |
|
| |
| 301 |
|
| #endif |
| 302 |
|
| |
| 303 |
|
| #ifdef IM_ASSERT |
| 304 |
|
| |
| 305 |
|
| /* |
| 306 |
|
| =item im_assert_fail(file, line, message) |
| 307 |
|
| |
| 308 |
|
| Called when an im_assert() assertion fails. |
| 309 |
|
| |
| 310 |
|
| Only available when Imager is built with assertions. |
| 311 |
|
| |
| 312 |
|
| =cut |
| 313 |
|
| */ |
| 314 |
|
| |
| 315 |
|
| void |
| 316 |
0 |
| im_assert_fail(char const *file, int line, char const *message) { |
| 317 |
0 |
| fprintf(stderr, "Assertion failed line %d file %s: %s\n", |
| 318 |
|
| line, file, message); |
| 319 |
0 |
| abort(); |
| 320 |
|
| } |
| 321 |
|
| |
| 322 |
|
| #endif |
| 323 |
|
| |
| 324 |
|
| /* |
| 325 |
|
| =back |
| 326 |
|
| |
| 327 |
|
| =head1 BUGS |
| 328 |
|
| |
| 329 |
|
| This interface isn't thread safe. |
| 330 |
|
| |
| 331 |
|
| =head1 AUTHOR |
| 332 |
|
| |
| 333 |
|
| Tony Cook <tony@develop-help.com> |
| 334 |
|
| |
| 335 |
|
| Stack concept by Arnar Mar Hrafnkelsson <addi@umich.edu> |
| 336 |
|
| |
| 337 |
|
| =cut |
| 338 |
|
| */ |