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 (void) LockSemaphoreInfo(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 (void) UnlockSemaphoreInfo(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 (void) LockSemaphoreInfo(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 (void) UnlockSemaphoreInfo(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 (void) LockSemaphoreInfo(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 (void) UnlockSemaphoreInfo(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->semaphore=AllocateSemaphoreInfo();
00425 exception->signature=MagickSignature;
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
00451 MagickExport char *GetExceptionMessage(const int error)
00452 {
00453 char
00454 exception[MaxTextExtent];
00455
00456 #if defined(MAGICKCORE_HAVE_STRERROR_R)
00457 (void) strerror_r(error,exception,sizeof(exception));
00458 #else
00459 (void) CopyMagickString(exception,strerror(error),sizeof(exception));
00460 #endif
00461 return(ConstantString(exception));
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
00491 static const char *ExceptionSeverityToTag(const ExceptionType severity)
00492 {
00493 switch (severity)
00494 {
00495 case ResourceLimitWarning: return("Resource/Limit/Warning/");
00496 case TypeWarning: return("Type/Warning/");
00497 case OptionWarning: return("Option/Warning/");
00498 case DelegateWarning: return("Delegate/Warning/");
00499 case MissingDelegateWarning: return("Missing/Delegate/Warning/");
00500 case CorruptImageWarning: return("Corrupt/Image/Warning/");
00501 case FileOpenWarning: return("File/Open/Warning/");
00502 case BlobWarning: return("Blob/Warning/");
00503 case StreamWarning: return("Stream/Warning/");
00504 case CacheWarning: return("Cache/Warning/");
00505 case CoderWarning: return("Coder/Warning/");
00506 case ModuleWarning: return("Module/Warning/");
00507 case DrawWarning: return("Draw/Warning/");
00508 case ImageWarning: return("Image/Warning/");
00509 case WandWarning: return("Wand/Warning/");
00510 case XServerWarning: return("XServer/Warning/");
00511 case MonitorWarning: return("Monitor/Warning/");
00512 case RegistryWarning: return("Registry/Warning/");
00513 case ConfigureWarning: return("Configure/Warning/");
00514 case PolicyWarning: return("Policy/Warning/");
00515 case ResourceLimitError: return("Resource/Limit/Error/");
00516 case TypeError: return("Type/Error/");
00517 case OptionError: return("Option/Error/");
00518 case DelegateError: return("Delegate/Error/");
00519 case MissingDelegateError: return("Missing/Delegate/Error/");
00520 case CorruptImageError: return("Corrupt/Image/Error/");
00521 case FileOpenError: return("File/Open/Error/");
00522 case BlobError: return("Blob/Error/");
00523 case StreamError: return("Stream/Error/");
00524 case CacheError: return("Cache/Error/");
00525 case CoderError: return("Coder/Error/");
00526 case ModuleError: return("Module/Error/");
00527 case DrawError: return("Draw/Error/");
00528 case ImageError: return("Image/Error/");
00529 case WandError: return("Wand/Error/");
00530 case XServerError: return("XServer/Error/");
00531 case MonitorError: return("Monitor/Error/");
00532 case RegistryError: return("Registry/Error/");
00533 case ConfigureError: return("Configure/Error/");
00534 case PolicyError: return("Policy/Error/");
00535 case ResourceLimitFatalError: return("Resource/Limit/FatalError/");
00536 case TypeFatalError: return("Type/FatalError/");
00537 case OptionFatalError: return("Option/FatalError/");
00538 case DelegateFatalError: return("Delegate/FatalError/");
00539 case MissingDelegateFatalError: return("Missing/Delegate/FatalError/");
00540 case CorruptImageFatalError: return("Corrupt/Image/FatalError/");
00541 case FileOpenFatalError: return("File/Open/FatalError/");
00542 case BlobFatalError: return("Blob/FatalError/");
00543 case StreamFatalError: return("Stream/FatalError/");
00544 case CacheFatalError: return("Cache/FatalError/");
00545 case CoderFatalError: return("Coder/FatalError/");
00546 case ModuleFatalError: return("Module/FatalError/");
00547 case DrawFatalError: return("Draw/FatalError/");
00548 case ImageFatalError: return("Image/FatalError/");
00549 case WandFatalError: return("Wand/FatalError/");
00550 case XServerFatalError: return("XServer/FatalError/");
00551 case MonitorFatalError: return("Monitor/FatalError/");
00552 case RegistryFatalError: return("Registry/FatalError/");
00553 case ConfigureFatalError: return("Configure/FatalError/");
00554 case PolicyFatalError: return("Policy/FatalError/");
00555 default: break;
00556 }
00557 return("");
00558 }
00559
00560 MagickExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
00561 const char *tag)
00562 {
00563 char
00564 message[MaxTextExtent];
00565
00566 const char
00567 *locale_message;
00568
00569 assert(tag != (const char *) NULL);
00570 (void) FormatMagickString(message,MaxTextExtent,"Exception/%s%s",
00571 ExceptionSeverityToTag(severity),tag);
00572 locale_message=GetLocaleMessage(message);
00573 if (locale_message == (const char *) NULL)
00574 return(tag);
00575 if (locale_message == message)
00576 return(tag);
00577 return(locale_message);
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
00604 MagickExport void InheritException(ExceptionInfo *exception,
00605 const ExceptionInfo *relative)
00606 {
00607 register const ExceptionInfo
00608 *p;
00609
00610 assert(exception != (ExceptionInfo *) NULL);
00611 assert(exception->signature == MagickSignature);
00612 assert(relative != (ExceptionInfo *) NULL);
00613 assert(relative->signature == MagickSignature);
00614 if (relative->exceptions == (void *) NULL)
00615 return;
00616 (void) LockSemaphoreInfo(exception->semaphore);
00617 ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
00618 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00619 relative->exceptions);
00620 while (p != (const ExceptionInfo *) NULL)
00621 {
00622 (void) ThrowException(exception,p->severity,p->reason,p->description);
00623 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00624 relative->exceptions);
00625 }
00626 (void) UnlockSemaphoreInfo(exception->semaphore);
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
00657 MagickExport void MagickError(const ExceptionType error,const char *reason,
00658 const char *description)
00659 {
00660 if (error_handler != (ErrorHandler) NULL)
00661 (*error_handler)(error,reason,description);
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
00693 MagickExport void MagickFatalError(const ExceptionType error,const char *reason,
00694 const char *description)
00695 {
00696 if (fatal_error_handler != (ErrorHandler) NULL)
00697 (*fatal_error_handler)(error,reason,description);
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
00727 MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
00728 const char *description)
00729 {
00730 if (warning_handler != (WarningHandler) NULL)
00731 (*warning_handler)(warning,reason,description);
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
00757 MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
00758 {
00759 ErrorHandler
00760 previous_handler;
00761
00762 previous_handler=error_handler;
00763 error_handler=handler;
00764 return(previous_handler);
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
00790 MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
00791 {
00792 FatalErrorHandler
00793 previous_handler;
00794
00795 previous_handler=fatal_error_handler;
00796 fatal_error_handler=handler;
00797 return(previous_handler);
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
00823 MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
00824 {
00825 WarningHandler
00826 previous_handler;
00827
00828 previous_handler=warning_handler;
00829 warning_handler=handler;
00830 return(previous_handler);
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
00864 MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
00865 const ExceptionType severity,const char *reason,const char *description)
00866 {
00867 register ExceptionInfo
00868 *p;
00869
00870 assert(exception != (ExceptionInfo *) NULL);
00871 assert(exception->signature == MagickSignature);
00872 p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
00873 exception->exceptions);
00874 if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
00875 (LocaleCompare(exception->reason,reason) == 0) &&
00876 (LocaleCompare(exception->description,description) == 0))
00877 return(MagickTrue);
00878 p=(ExceptionInfo *) AcquireMagickMemory(sizeof(*p));
00879 if (p == (ExceptionInfo *) NULL)
00880 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
00881 (void) ResetMagickMemory(p,0,sizeof(*p));
00882 p->severity=severity;
00883 if (reason != (const char *) NULL)
00884 p->reason=ConstantString(reason);
00885 if (description != (const char *) NULL)
00886 p->description=ConstantString(description);
00887 p->signature=MagickSignature;
00888 (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
00889 exception->severity=p->severity;
00890 exception->reason=p->reason;
00891 exception->description=p->description;
00892 return(MagickTrue);
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
00933 MagickExport MagickBooleanType ThrowMagickExceptionList(
00934 ExceptionInfo *exception,const char *module,const char *function,
00935 const unsigned long line,const ExceptionType severity,const char *tag,
00936 const char *format,va_list operands)
00937 {
00938 char
00939 message[MaxTextExtent],
00940 path[MaxTextExtent],
00941 reason[MaxTextExtent];
00942
00943 const char
00944 *locale;
00945
00946 int
00947 n;
00948
00949 MagickBooleanType
00950 status;
00951
00952 size_t
00953 length;
00954
00955 assert(exception != (ExceptionInfo *) NULL);
00956 assert(exception->signature == MagickSignature);
00957 locale=GetLocaleExceptionMessage(severity,tag);
00958 (void) CopyMagickString(reason,locale,MaxTextExtent);
00959 (void) ConcatenateMagickString(reason," ",MaxTextExtent);
00960 length=strlen(reason);
00961 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
00962 n=vsnprintf(reason+length,MaxTextExtent-length,format,operands);
00963 #else
00964 n=vsprintf(reason+length,format,operands);
00965 #endif
00966 if (n < 0)
00967 reason[MaxTextExtent-1]='\0';
00968 status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
00969 GetPathComponent(module,TailPath,path);
00970 (void) FormatMagickString(message,MaxTextExtent,"%s @ %s/%s/%ld",reason,path,
00971 function,line);
00972 (void) ThrowException(exception,severity,message,(char *) NULL);
00973 return(status);
00974 }
00975
00976 MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception,
00977 const char *module,const char *function,const unsigned long line,
00978 const ExceptionType severity,const char *tag,const char *format,...)
00979 {
00980 MagickBooleanType
00981 status;
00982
00983 va_list
00984 operands;
00985
00986 va_start(operands,format);
00987 status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,
00988 format,operands);
00989 va_end(operands);
00990 return(status);
00991 }