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 AcquireSemaphoreInfo(&exception->semaphore);
00160 p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
00161 exception->exceptions);
00162 while (p != (ExceptionInfo *) NULL)
00163 {
00164 (void) DestroyExceptionElement(p);
00165 p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
00166 exception->exceptions);
00167 }
00168 exception->severity=UndefinedException;
00169 exception->reason=(char *) NULL;
00170 exception->description=(char *) NULL;
00171 RelinquishSemaphoreInfo(exception->semaphore);
00172 errno=0;
00173 }
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 MagickExport void CatchException(ExceptionInfo *exception)
00199 {
00200 register const ExceptionInfo
00201 *p;
00202
00203 assert(exception != (ExceptionInfo *) NULL);
00204 assert(exception->signature == MagickSignature);
00205 AcquireSemaphoreInfo(&exception->semaphore);
00206 ResetLinkedListIterator((LinkedListInfo *) exception->exceptions);
00207 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00208 exception->exceptions);
00209 while (p != (const ExceptionInfo *) NULL)
00210 {
00211 if ((p->severity >= WarningException) && (p->severity < ErrorException))
00212 MagickWarning(p->severity,p->reason,p->description);
00213 if ((p->severity >= ErrorException) && (p->severity < FatalErrorException))
00214 MagickError(p->severity,p->reason,p->description);
00215 if (exception->severity >= FatalErrorException)
00216 MagickFatalError(p->severity,p->reason,p->description);
00217 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00218 exception->exceptions);
00219 }
00220 RelinquishSemaphoreInfo(exception->semaphore);
00221 ClearMagickException(exception);
00222 }
00223
00224
00225
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 static void DefaultErrorHandler(const ExceptionType magick_unused(severity),
00253 const char *reason,const char *description)
00254 {
00255 if (reason == (char *) NULL)
00256 return;
00257 (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
00258 if (description != (char *) NULL)
00259 (void) fprintf(stderr," (%s)",description);
00260 (void) fprintf(stderr,".\n");
00261 (void) fflush(stderr);
00262 }
00263
00264
00265
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 static void DefaultFatalErrorHandler(const ExceptionType magick_unused(severity),
00294 const char *reason,const char *description)
00295 {
00296 if (reason == (char *) NULL)
00297 return;
00298 (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
00299 if (description != (char *) NULL)
00300 (void) fprintf(stderr," (%s)",description);
00301 (void) fprintf(stderr,".\n");
00302 (void) fflush(stderr);
00303 MagickCoreTerminus();
00304 exit(1);
00305 }
00306
00307
00308
00309
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 static void DefaultWarningHandler(const ExceptionType magick_unused(severity),
00336 const char *reason,const char *description)
00337 {
00338 if (reason == (char *) NULL)
00339 return;
00340 (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
00341 if (description != (char *) NULL)
00342 (void) fprintf(stderr," (%s)",description);
00343 (void) fprintf(stderr,".\n");
00344 (void) fflush(stderr);
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369 MagickExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
00370 {
00371 MagickBooleanType
00372 relinquish;
00373
00374 assert(exception != (ExceptionInfo *) NULL);
00375 assert(exception->signature == MagickSignature);
00376 AcquireSemaphoreInfo(&exception->semaphore);
00377 exception->severity=UndefinedException;
00378 if (exception->exceptions != (void *) NULL)
00379 exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
00380 exception->exceptions,DestroyExceptionElement);
00381 relinquish=exception->relinquish;
00382 if (exception->relinquish != MagickFalse)
00383 exception->signature=(~MagickSignature);
00384 RelinquishSemaphoreInfo(exception->semaphore);
00385 DestroySemaphoreInfo(&exception->semaphore);
00386 if (relinquish != MagickFalse)
00387 exception=(ExceptionInfo *) RelinquishMagickMemory(exception);
00388 return(exception);
00389 }
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413 MagickExport void GetExceptionInfo(ExceptionInfo *exception)
00414 {
00415 assert(exception != (ExceptionInfo *) NULL);
00416 (void) ResetMagickMemory(exception,0,sizeof(*exception));
00417 exception->severity=UndefinedException;
00418 exception->exceptions=(void *) NewLinkedList(0);
00419 exception->signature=MagickSignature;
00420 }
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445 MagickExport char *GetExceptionMessage(const int error_code)
00446 {
00447 return(ConstantString(strerror(error_code)));
00448 }
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477 static const char *ExceptionSeverityToTag(const ExceptionType severity)
00478 {
00479 switch (severity)
00480 {
00481 case ResourceLimitWarning: return("Resource/Limit/Warning/");
00482 case TypeWarning: return("Type/Warning/");
00483 case OptionWarning: return("Option/Warning/");
00484 case DelegateWarning: return("Delegate/Warning/");
00485 case MissingDelegateWarning: return("Missing/Delegate/Warning/");
00486 case CorruptImageWarning: return("Corrupt/Image/Warning/");
00487 case FileOpenWarning: return("File/Open/Warning/");
00488 case BlobWarning: return("Blob/Warning/");
00489 case StreamWarning: return("Stream/Warning/");
00490 case CacheWarning: return("Cache/Warning/");
00491 case CoderWarning: return("Coder/Warning/");
00492 case ModuleWarning: return("Module/Warning/");
00493 case DrawWarning: return("Draw/Warning/");
00494 case ImageWarning: return("Image/Warning/");
00495 case WandWarning: return("Wand/Warning/");
00496 case XServerWarning: return("XServer/Warning/");
00497 case MonitorWarning: return("Monitor/Warning/");
00498 case RegistryWarning: return("Registry/Warning/");
00499 case ConfigureWarning: return("Configure/Warning/");
00500 case ResourceLimitError: return("Resource/Limit/Error/");
00501 case TypeError: return("Type/Error/");
00502 case OptionError: return("Option/Error/");
00503 case DelegateError: return("Delegate/Error/");
00504 case MissingDelegateError: return("Missing/Delegate/Error/");
00505 case CorruptImageError: return("Corrupt/Image/Error/");
00506 case FileOpenError: return("File/Open/Error/");
00507 case BlobError: return("Blob/Error/");
00508 case StreamError: return("Stream/Error/");
00509 case CacheError: return("Cache/Error/");
00510 case CoderError: return("Coder/Error/");
00511 case ModuleError: return("Module/Error/");
00512 case DrawError: return("Draw/Error/");
00513 case ImageError: return("Image/Error/");
00514 case WandError: return("Wand/Error/");
00515 case XServerError: return("XServer/Error/");
00516 case MonitorError: return("Monitor/Error/");
00517 case RegistryError: return("Registry/Error/");
00518 case ConfigureError: return("Configure/Error/");
00519 case ResourceLimitFatalError: return("Resource/Limit/FatalError/");
00520 case TypeFatalError: return("Type/FatalError/");
00521 case OptionFatalError: return("Option/FatalError/");
00522 case DelegateFatalError: return("Delegate/FatalError/");
00523 case MissingDelegateFatalError: return("Missing/Delegate/FatalError/");
00524 case CorruptImageFatalError: return("Corrupt/Image/FatalError/");
00525 case FileOpenFatalError: return("File/Open/FatalError/");
00526 case BlobFatalError: return("Blob/FatalError/");
00527 case StreamFatalError: return("Stream/FatalError/");
00528 case CacheFatalError: return("Cache/FatalError/");
00529 case CoderFatalError: return("Coder/FatalError/");
00530 case ModuleFatalError: return("Module/FatalError/");
00531 case DrawFatalError: return("Draw/FatalError/");
00532 case ImageFatalError: return("Image/FatalError/");
00533 case WandFatalError: return("Wand/FatalError/");
00534 case XServerFatalError: return("XServer/FatalError/");
00535 case MonitorFatalError: return("Monitor/FatalError/");
00536 case RegistryFatalError: return("Registry/FatalError/");
00537 case ConfigureFatalError: return("Configure/FatalError/");
00538 default: break;
00539 }
00540 return("");
00541 }
00542
00543 MagickExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
00544 const char *tag)
00545 {
00546 char
00547 message[MaxTextExtent];
00548
00549 const char
00550 *locale_message;
00551
00552 assert(tag != (const char *) NULL);
00553 (void) FormatMagickString(message,MaxTextExtent,"Exception/%s%s",
00554 ExceptionSeverityToTag(severity),tag);
00555 locale_message=GetLocaleMessage(message);
00556 if (locale_message == (const char *) NULL)
00557 return(tag);
00558 if (locale_message == message)
00559 return(tag);
00560 return(locale_message);
00561 }
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587 MagickExport void InheritException(ExceptionInfo *exception,
00588 const ExceptionInfo *relative)
00589 {
00590 register const ExceptionInfo
00591 *p;
00592
00593 assert(exception != (ExceptionInfo *) NULL);
00594 assert(exception->signature == MagickSignature);
00595 assert(relative != (ExceptionInfo *) NULL);
00596 assert(relative->signature == MagickSignature);
00597 AcquireSemaphoreInfo(&exception->semaphore);
00598 ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
00599 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00600 relative->exceptions);
00601 while (p != (const ExceptionInfo *) NULL)
00602 {
00603 (void) ThrowException(exception,p->severity,p->reason,p->description);
00604 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00605 relative->exceptions);
00606 }
00607 RelinquishSemaphoreInfo(exception->semaphore);
00608 }
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638 MagickExport void MagickError(const ExceptionType error,const char *reason,
00639 const char *description)
00640 {
00641 if (error_handler != (ErrorHandler) NULL)
00642 (*error_handler)(error,reason,description);
00643 }
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674 MagickExport void MagickFatalError(const ExceptionType error,const char *reason,
00675 const char *description)
00676 {
00677 if (fatal_error_handler != (ErrorHandler) NULL)
00678 (*fatal_error_handler)(error,reason,description);
00679 }
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708 MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
00709 const char *description)
00710 {
00711 if (warning_handler != (WarningHandler) NULL)
00712 (*warning_handler)(warning,reason,description);
00713 }
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738 MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
00739 {
00740 ErrorHandler
00741 previous_handler;
00742
00743 previous_handler=error_handler;
00744 error_handler=handler;
00745 return(previous_handler);
00746 }
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771 MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
00772 {
00773 FatalErrorHandler
00774 previous_handler;
00775
00776 previous_handler=fatal_error_handler;
00777 fatal_error_handler=handler;
00778 return(previous_handler);
00779 }
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804 MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
00805 {
00806 WarningHandler
00807 previous_handler;
00808
00809 previous_handler=warning_handler;
00810 warning_handler=handler;
00811 return(previous_handler);
00812 }
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845 MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
00846 const ExceptionType severity,const char *reason,const char *description)
00847 {
00848 register ExceptionInfo
00849 *p;
00850
00851 assert(exception != (ExceptionInfo *) NULL);
00852 assert(exception->signature == MagickSignature);
00853 p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
00854 exception->exceptions);
00855 if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
00856 (LocaleCompare(exception->reason,reason) == 0) &&
00857 (LocaleCompare(exception->description,description) == 0))
00858 return(MagickTrue);
00859 p=(ExceptionInfo *) AcquireMagickMemory(sizeof(*p));
00860 if (p == (ExceptionInfo *) NULL)
00861 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
00862 (void) ResetMagickMemory(p,0,sizeof(*p));
00863 p->severity=severity;
00864 if (reason != (const char *) NULL)
00865 p->reason=ConstantString(reason);
00866 if (description != (const char *) NULL)
00867 p->description=ConstantString(description);
00868 p->signature=MagickSignature;
00869 (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
00870 exception->severity=p->severity;
00871 exception->reason=p->reason;
00872 exception->description=p->description;
00873 return(MagickTrue);
00874 }
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914 MagickExport MagickBooleanType ThrowMagickExceptionList(
00915 ExceptionInfo *exception,const char *module,const char *function,
00916 const unsigned long line,const ExceptionType severity,const char *tag,
00917 const char *format,va_list operands)
00918 {
00919 char
00920 message[MaxTextExtent],
00921 reason[MaxTextExtent];
00922
00923 const char
00924 *locale;
00925
00926 int
00927 n;
00928
00929 MagickBooleanType
00930 status;
00931
00932 size_t
00933 length;
00934
00935 assert(exception != (ExceptionInfo *) NULL);
00936 assert(exception->signature == MagickSignature);
00937 locale=GetLocaleExceptionMessage(severity,tag);
00938 (void) CopyMagickString(reason,locale,MaxTextExtent);
00939 (void) ConcatenateMagickString(reason," ",MaxTextExtent);
00940 length=strlen(reason);
00941 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
00942 n=vsnprintf(reason+length,MaxTextExtent-length,format,operands);
00943 #else
00944 n=vsprintf(reason+length,format,operands);
00945 #endif
00946 if (n < 0)
00947 reason[MaxTextExtent-1]='\0';
00948 status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
00949 (void) FormatMagickString(message,MaxTextExtent,"%s @ %s/%s/%ld",reason,
00950 module,function,line);
00951 (void) ThrowException(exception,severity,message,(char *) NULL);
00952 return(status);
00953 }
00954
00955 MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception,
00956 const char *module,const char *function,const unsigned long line,
00957 const ExceptionType severity,const char *tag,const char *format,...)
00958 {
00959 MagickBooleanType
00960 status;
00961
00962 va_list
00963 operands;
00964
00965 va_start(operands,format);
00966 status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,
00967 format,operands);
00968 va_end(operands);
00969 return(status);
00970 }