00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "magick/studio.h"
00044 #include "magick/client.h"
00045 #include "magick/exception.h"
00046 #include "magick/exception-private.h"
00047 #include "magick/hashmap.h"
00048 #include "magick/locale_.h"
00049 #include "magick/log.h"
00050 #include "magick/magick.h"
00051 #include "magick/memory_.h"
00052 #include "magick/string_.h"
00053 #include "magick/utility.h"
00054
00055
00056
00057
00058 #if defined(__cplusplus) || defined(c_plusplus)
00059 extern "C" {
00060 #endif
00061
00062 static void
00063 DefaultErrorHandler(const ExceptionType,const char *,const char *),
00064 DefaultFatalErrorHandler(const ExceptionType,const char *,const char *),
00065 DefaultWarningHandler(const ExceptionType,const char *,const char *);
00066
00067 #if defined(__cplusplus) || defined(c_plusplus)
00068 }
00069 #endif
00070
00071
00072
00073
00074 static ErrorHandler
00075 error_handler = DefaultErrorHandler;
00076
00077 static FatalErrorHandler
00078 fatal_error_handler = DefaultFatalErrorHandler;
00079
00080 static WarningHandler
00081 warning_handler = DefaultWarningHandler;
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 MagickExport ExceptionInfo *AcquireExceptionInfo(void)
00102 {
00103 ExceptionInfo
00104 *exception;
00105
00106 exception=(ExceptionInfo *) AcquireMagickMemory(sizeof(*exception));
00107 if (exception == (ExceptionInfo *) NULL)
00108 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
00109 GetExceptionInfo(exception);
00110 exception->relinquish=MagickTrue;
00111 return(exception);
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 static void *DestroyExceptionElement(void *exception)
00139 {
00140 register ExceptionInfo
00141 *p;
00142
00143 p=(ExceptionInfo *) exception;
00144 if (p->reason != (char *) NULL)
00145 p->reason=DestroyString(p->reason);
00146 if (p->description != (char *) NULL)
00147 p->description=DestroyString(p->description);
00148 p=(ExceptionInfo *) RelinquishMagickMemory(p);
00149 return((void *) NULL);
00150 }
00151
00152 MagickExport void ClearMagickException(ExceptionInfo *exception)
00153 {
00154 register ExceptionInfo
00155 *p;
00156
00157 assert(exception != (ExceptionInfo *) NULL);
00158 assert(exception->signature == MagickSignature);
00159 if (exception->exceptions == (void *) NULL)
00160 return;
00161 AcquireSemaphoreInfo(&exception->semaphore);
00162 p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
00163 exception->exceptions);
00164 while (p != (ExceptionInfo *) NULL)
00165 {
00166 (void) DestroyExceptionElement(p);
00167 p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
00168 exception->exceptions);
00169 }
00170 exception->severity=UndefinedException;
00171 exception->reason=(char *) NULL;
00172 exception->description=(char *) NULL;
00173 RelinquishSemaphoreInfo(exception->semaphore);
00174 errno=0;
00175 }
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 MagickExport void CatchException(ExceptionInfo *exception)
00201 {
00202 register const ExceptionInfo
00203 *p;
00204
00205 assert(exception != (ExceptionInfo *) NULL);
00206 assert(exception->signature == MagickSignature);
00207 if (exception->exceptions == (void *) NULL)
00208 return;
00209 AcquireSemaphoreInfo(&exception->semaphore);
00210 ResetLinkedListIterator((LinkedListInfo *) exception->exceptions);
00211 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00212 exception->exceptions);
00213 while (p != (const ExceptionInfo *) NULL)
00214 {
00215 if ((p->severity >= WarningException) && (p->severity < ErrorException))
00216 MagickWarning(p->severity,p->reason,p->description);
00217 if ((p->severity >= ErrorException) && (p->severity < FatalErrorException))
00218 MagickError(p->severity,p->reason,p->description);
00219 if (exception->severity >= FatalErrorException)
00220 MagickFatalError(p->severity,p->reason,p->description);
00221 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00222 exception->exceptions);
00223 }
00224 RelinquishSemaphoreInfo(exception->semaphore);
00225 ClearMagickException(exception);
00226 }
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256 static void DefaultErrorHandler(const ExceptionType magick_unused(severity),
00257 const char *reason,const char *description)
00258 {
00259 if (reason == (char *) NULL)
00260 return;
00261 (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
00262 if (description != (char *) NULL)
00263 (void) fprintf(stderr," (%s)",description);
00264 (void) fprintf(stderr,".\n");
00265 (void) fflush(stderr);
00266 }
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297 static void DefaultFatalErrorHandler(
00298 const ExceptionType magick_unused(severity),
00299 const char *reason,const char *description)
00300 {
00301 if (reason == (char *) NULL)
00302 return;
00303 (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
00304 if (description != (char *) NULL)
00305 (void) fprintf(stderr," (%s)",description);
00306 (void) fprintf(stderr,".\n");
00307 (void) fflush(stderr);
00308 MagickCoreTerminus();
00309 exit(1);
00310 }
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 static void DefaultWarningHandler(const ExceptionType magick_unused(severity),
00341 const char *reason,const char *description)
00342 {
00343 if (reason == (char *) NULL)
00344 return;
00345 (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
00346 if (description != (char *) NULL)
00347 (void) fprintf(stderr," (%s)",description);
00348 (void) fprintf(stderr,".\n");
00349 (void) fflush(stderr);
00350 }
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374 MagickExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
00375 {
00376 MagickBooleanType
00377 relinquish;
00378
00379 assert(exception != (ExceptionInfo *) NULL);
00380 assert(exception->signature == MagickSignature);
00381 AcquireSemaphoreInfo(&exception->semaphore);
00382 exception->severity=UndefinedException;
00383 if (exception->exceptions != (void *) NULL)
00384 exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
00385 exception->exceptions,DestroyExceptionElement);
00386 relinquish=exception->relinquish;
00387 if (exception->relinquish != MagickFalse)
00388 exception->signature=(~MagickSignature);
00389 RelinquishSemaphoreInfo(exception->semaphore);
00390 DestroySemaphoreInfo(&exception->semaphore);
00391 if (relinquish != MagickFalse)
00392 exception=(ExceptionInfo *) RelinquishMagickMemory(exception);
00393 return(exception);
00394 }
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418 MagickExport void GetExceptionInfo(ExceptionInfo *exception)
00419 {
00420 assert(exception != (ExceptionInfo *) NULL);
00421 (void) ResetMagickMemory(exception,0,sizeof(*exception));
00422 exception->severity=UndefinedException;
00423 exception->exceptions=(void *) NewLinkedList(0);
00424 exception->signature=MagickSignature;
00425 }
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450 MagickExport char *GetExceptionMessage(const int error)
00451 {
00452 char
00453 exception[MaxTextExtent];
00454
00455 #if defined(MAGICKCORE_HAVE_STRERROR_R)
00456 (void) strerror_r(error,exception,sizeof(exception));
00457 #else
00458 (void) CopyMagickString(exception,strerror(error),sizeof(exception));
00459 #endif
00460 return(ConstantString(exception));
00461 }
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490 static const char *ExceptionSeverityToTag(const ExceptionType severity)
00491 {
00492 switch (severity)
00493 {
00494 case ResourceLimitWarning: return("Resource/Limit/Warning/");
00495 case TypeWarning: return("Type/Warning/");
00496 case OptionWarning: return("Option/Warning/");
00497 case DelegateWarning: return("Delegate/Warning/");
00498 case MissingDelegateWarning: return("Missing/Delegate/Warning/");
00499 case CorruptImageWarning: return("Corrupt/Image/Warning/");
00500 case FileOpenWarning: return("File/Open/Warning/");
00501 case BlobWarning: return("Blob/Warning/");
00502 case StreamWarning: return("Stream/Warning/");
00503 case CacheWarning: return("Cache/Warning/");
00504 case CoderWarning: return("Coder/Warning/");
00505 case ModuleWarning: return("Module/Warning/");
00506 case DrawWarning: return("Draw/Warning/");
00507 case ImageWarning: return("Image/Warning/");
00508 case WandWarning: return("Wand/Warning/");
00509 case XServerWarning: return("XServer/Warning/");
00510 case MonitorWarning: return("Monitor/Warning/");
00511 case RegistryWarning: return("Registry/Warning/");
00512 case ConfigureWarning: return("Configure/Warning/");
00513 case PolicyWarning: return("Policy/Warning/");
00514 case ResourceLimitError: return("Resource/Limit/Error/");
00515 case TypeError: return("Type/Error/");
00516 case OptionError: return("Option/Error/");
00517 case DelegateError: return("Delegate/Error/");
00518 case MissingDelegateError: return("Missing/Delegate/Error/");
00519 case CorruptImageError: return("Corrupt/Image/Error/");
00520 case FileOpenError: return("File/Open/Error/");
00521 case BlobError: return("Blob/Error/");
00522 case StreamError: return("Stream/Error/");
00523 case CacheError: return("Cache/Error/");
00524 case CoderError: return("Coder/Error/");
00525 case ModuleError: return("Module/Error/");
00526 case DrawError: return("Draw/Error/");
00527 case ImageError: return("Image/Error/");
00528 case WandError: return("Wand/Error/");
00529 case XServerError: return("XServer/Error/");
00530 case MonitorError: return("Monitor/Error/");
00531 case RegistryError: return("Registry/Error/");
00532 case ConfigureError: return("Configure/Error/");
00533 case PolicyError: return("Policy/Error/");
00534 case ResourceLimitFatalError: return("Resource/Limit/FatalError/");
00535 case TypeFatalError: return("Type/FatalError/");
00536 case OptionFatalError: return("Option/FatalError/");
00537 case DelegateFatalError: return("Delegate/FatalError/");
00538 case MissingDelegateFatalError: return("Missing/Delegate/FatalError/");
00539 case CorruptImageFatalError: return("Corrupt/Image/FatalError/");
00540 case FileOpenFatalError: return("File/Open/FatalError/");
00541 case BlobFatalError: return("Blob/FatalError/");
00542 case StreamFatalError: return("Stream/FatalError/");
00543 case CacheFatalError: return("Cache/FatalError/");
00544 case CoderFatalError: return("Coder/FatalError/");
00545 case ModuleFatalError: return("Module/FatalError/");
00546 case DrawFatalError: return("Draw/FatalError/");
00547 case ImageFatalError: return("Image/FatalError/");
00548 case WandFatalError: return("Wand/FatalError/");
00549 case XServerFatalError: return("XServer/FatalError/");
00550 case MonitorFatalError: return("Monitor/FatalError/");
00551 case RegistryFatalError: return("Registry/FatalError/");
00552 case ConfigureFatalError: return("Configure/FatalError/");
00553 case PolicyFatalError: return("Policy/FatalError/");
00554 default: break;
00555 }
00556 return("");
00557 }
00558
00559 MagickExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
00560 const char *tag)
00561 {
00562 char
00563 message[MaxTextExtent];
00564
00565 const char
00566 *locale_message;
00567
00568 assert(tag != (const char *) NULL);
00569 (void) FormatMagickString(message,MaxTextExtent,"Exception/%s%s",
00570 ExceptionSeverityToTag(severity),tag);
00571 locale_message=GetLocaleMessage(message);
00572 if (locale_message == (const char *) NULL)
00573 return(tag);
00574 if (locale_message == message)
00575 return(tag);
00576 return(locale_message);
00577 }
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603 MagickExport void InheritException(ExceptionInfo *exception,
00604 const ExceptionInfo *relative)
00605 {
00606 register const ExceptionInfo
00607 *p;
00608
00609 assert(exception != (ExceptionInfo *) NULL);
00610 assert(exception->signature == MagickSignature);
00611 assert(relative != (ExceptionInfo *) NULL);
00612 assert(relative->signature == MagickSignature);
00613 if (relative->exceptions == (void *) NULL)
00614 return;
00615 AcquireSemaphoreInfo(&exception->semaphore);
00616 ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
00617 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00618 relative->exceptions);
00619 while (p != (const ExceptionInfo *) NULL)
00620 {
00621 (void) ThrowException(exception,p->severity,p->reason,p->description);
00622 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00623 relative->exceptions);
00624 }
00625 RelinquishSemaphoreInfo(exception->semaphore);
00626 }
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656 MagickExport void MagickError(const ExceptionType error,const char *reason,
00657 const char *description)
00658 {
00659 if (error_handler != (ErrorHandler) NULL)
00660 (*error_handler)(error,reason,description);
00661 }
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692 MagickExport void MagickFatalError(const ExceptionType error,const char *reason,
00693 const char *description)
00694 {
00695 if (fatal_error_handler != (ErrorHandler) NULL)
00696 (*fatal_error_handler)(error,reason,description);
00697 }
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726 MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
00727 const char *description)
00728 {
00729 if (warning_handler != (WarningHandler) NULL)
00730 (*warning_handler)(warning,reason,description);
00731 }
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756 MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
00757 {
00758 ErrorHandler
00759 previous_handler;
00760
00761 previous_handler=error_handler;
00762 error_handler=handler;
00763 return(previous_handler);
00764 }
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789 MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
00790 {
00791 FatalErrorHandler
00792 previous_handler;
00793
00794 previous_handler=fatal_error_handler;
00795 fatal_error_handler=handler;
00796 return(previous_handler);
00797 }
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822 MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
00823 {
00824 WarningHandler
00825 previous_handler;
00826
00827 previous_handler=warning_handler;
00828 warning_handler=handler;
00829 return(previous_handler);
00830 }
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863 MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
00864 const ExceptionType severity,const char *reason,const char *description)
00865 {
00866 register ExceptionInfo
00867 *p;
00868
00869 assert(exception != (ExceptionInfo *) NULL);
00870 assert(exception->signature == MagickSignature);
00871 p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
00872 exception->exceptions);
00873 if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
00874 (LocaleCompare(exception->reason,reason) == 0) &&
00875 (LocaleCompare(exception->description,description) == 0))
00876 return(MagickTrue);
00877 p=(ExceptionInfo *) AcquireMagickMemory(sizeof(*p));
00878 if (p == (ExceptionInfo *) NULL)
00879 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
00880 (void) ResetMagickMemory(p,0,sizeof(*p));
00881 p->severity=severity;
00882 if (reason != (const char *) NULL)
00883 p->reason=ConstantString(reason);
00884 if (description != (const char *) NULL)
00885 p->description=ConstantString(description);
00886 p->signature=MagickSignature;
00887 (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
00888 exception->severity=p->severity;
00889 exception->reason=p->reason;
00890 exception->description=p->description;
00891 return(MagickTrue);
00892 }
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932 MagickExport MagickBooleanType ThrowMagickExceptionList(
00933 ExceptionInfo *exception,const char *module,const char *function,
00934 const unsigned long line,const ExceptionType severity,const char *tag,
00935 const char *format,va_list operands)
00936 {
00937 char
00938 message[MaxTextExtent],
00939 path[MaxTextExtent],
00940 reason[MaxTextExtent];
00941
00942 const char
00943 *locale;
00944
00945 int
00946 n;
00947
00948 MagickBooleanType
00949 status;
00950
00951 size_t
00952 length;
00953
00954 assert(exception != (ExceptionInfo *) NULL);
00955 assert(exception->signature == MagickSignature);
00956 locale=GetLocaleExceptionMessage(severity,tag);
00957 (void) CopyMagickString(reason,locale,MaxTextExtent);
00958 (void) ConcatenateMagickString(reason," ",MaxTextExtent);
00959 length=strlen(reason);
00960 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
00961 n=vsnprintf(reason+length,MaxTextExtent-length,format,operands);
00962 #else
00963 n=vsprintf(reason+length,format,operands);
00964 #endif
00965 if (n < 0)
00966 reason[MaxTextExtent-1]='\0';
00967 status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
00968 GetPathComponent(module,TailPath,path);
00969 (void) FormatMagickString(message,MaxTextExtent,"%s @ %s/%s/%ld",reason,path,
00970 function,line);
00971 (void) ThrowException(exception,severity,message,(char *) NULL);
00972 return(status);
00973 }
00974
00975 MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception,
00976 const char *module,const char *function,const unsigned long line,
00977 const ExceptionType severity,const char *tag,const char *format,...)
00978 {
00979 MagickBooleanType
00980 status;
00981
00982 va_list
00983 operands;
00984
00985 va_start(operands,format);
00986 status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,
00987 format,operands);
00988 va_end(operands);
00989 return(status);
00990 }