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 "wizard/studio.h"
00044 #include "wizard/client.h"
00045 #include "wizard/exception.h"
00046 #include "wizard/exception-private.h"
00047 #include "wizard/hashmap.h"
00048 #include "wizard/log.h"
00049 #include "wizard/memory_.h"
00050 #include "wizard/string_.h"
00051 #include "wizard/utility.h"
00052 #include "wizard/wizard.h"
00053
00054
00055
00056
00057 struct _ExceptionInfo
00058 {
00059 ExceptionType
00060 severity;
00061
00062 char
00063 *reason,
00064 *description;
00065
00066 void
00067 *exceptions;
00068
00069 WizardBooleanType
00070 relinquish;
00071
00072 SemaphoreInfo
00073 *semaphore;
00074
00075 size_t
00076 signature;
00077 };
00078
00079
00080
00081
00082 #if defined(__cplusplus) || defined(c_plusplus)
00083 extern "C" {
00084 #endif
00085
00086 static void
00087 DefaultErrorHandler(const ExceptionType,const char *,const char *),
00088 DefaultFatalErrorHandler(const ExceptionType,const char *,const char *),
00089 DefaultWarningHandler(const ExceptionType,const char *,const char *);
00090
00091 #if defined(__cplusplus) || defined(c_plusplus)
00092 }
00093 #endif
00094
00095
00096
00097
00098 static ErrorHandler
00099 error_handler = DefaultErrorHandler;
00100
00101 static FatalErrorHandler
00102 fatal_error_handler = DefaultFatalErrorHandler;
00103
00104 static WarningHandler
00105 warning_handler = DefaultWarningHandler;
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 WizardExport ExceptionInfo *AcquireExceptionInfo(void)
00126 {
00127 ExceptionInfo
00128 *exception;
00129
00130 exception=(ExceptionInfo *) AcquireAlignedMemory(1,sizeof(*exception));
00131 if (exception == (ExceptionInfo *) NULL)
00132 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
00133 (void) ResetWizardMemory(exception,0,sizeof(*exception));
00134 GetExceptionInfo(exception);
00135 exception->relinquish=WizardTrue;
00136 exception->signature=WizardSignature;
00137 return(exception);
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 static void *DestroyExceptionElement(void *exception)
00165 {
00166 register ExceptionInfo
00167 *p;
00168
00169 p=(ExceptionInfo *) exception;
00170 if (p->reason != (char *) NULL)
00171 p->reason=(char *) RelinquishWizardMemory(p->reason);
00172 if (p->description != (char *) NULL)
00173 p->description=(char *) RelinquishWizardMemory(p->description);
00174 p=(ExceptionInfo *) RelinquishWizardMemory(p);
00175 return((void *) NULL);
00176 }
00177
00178 WizardExport void ClearWizardException(ExceptionInfo *exception)
00179 {
00180 register ExceptionInfo
00181 *p;
00182
00183 assert(exception != (ExceptionInfo *) NULL);
00184 assert(exception->signature == WizardSignature);
00185 if (exception->exceptions == (void *) NULL)
00186 return;
00187 LockSemaphoreInfo(exception->semaphore);
00188 p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
00189 exception->exceptions);
00190 while (p != (ExceptionInfo *) NULL)
00191 {
00192 p=(ExceptionInfo *) DestroyExceptionElement(p);
00193 p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
00194 exception->exceptions);
00195 }
00196 exception->severity=UndefinedException;
00197 exception->reason=(char *) NULL;
00198 exception->description=(char *) NULL;
00199 UnlockSemaphoreInfo(exception->semaphore);
00200 errno=0;
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 WizardExport void CatchException(ExceptionInfo *exception)
00227 {
00228 register const ExceptionInfo
00229 *p;
00230
00231 assert(exception != (ExceptionInfo *) NULL);
00232 assert(exception->signature == WizardSignature);
00233 if (exception->exceptions == (void *) NULL)
00234 return;
00235 LockSemaphoreInfo(exception->semaphore);
00236 ResetLinkedListIterator((LinkedListInfo *) exception->exceptions);
00237 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00238 exception->exceptions);
00239 while (p != (const ExceptionInfo *) NULL)
00240 {
00241 if ((p->severity >= WarningException) && (p->severity < ErrorException))
00242 WizardWarning(p->severity,p->reason,p->description);
00243 if ((p->severity >= ErrorException) && (p->severity < FatalErrorException))
00244 WizardError(p->severity,p->reason,p->description);
00245 if (exception->severity >= FatalErrorException)
00246 WizardFatalError(p->severity,p->reason,p->description);
00247 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00248 exception->exceptions);
00249 }
00250 UnlockSemaphoreInfo(exception->semaphore);
00251 ClearWizardException(exception);
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 static void DefaultErrorHandler(const ExceptionType wizard_unused(severity),
00283 const char *reason,const char *description)
00284 {
00285 if (reason == (char *) NULL)
00286 return;
00287 (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
00288 if (description != (char *) NULL)
00289 (void) fprintf(stderr," (%s)",description);
00290 (void) fprintf(stderr,".\n");
00291 (void) fflush(stderr);
00292 }
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 static void DefaultFatalErrorHandler(const ExceptionType severity,
00324 const char *reason,const char *description)
00325 {
00326 if (reason == (char *) NULL)
00327 return;
00328 (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
00329 if (description != (char *) NULL)
00330 (void) fprintf(stderr," (%s)",description);
00331 (void) fprintf(stderr,".\n");
00332 (void) fflush(stderr);
00333 WizardsToolkitTerminus();
00334 exit(1);
00335 }
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 static void DefaultWarningHandler(const ExceptionType wizard_unused(severity),
00366 const char *reason,const char *description)
00367 {
00368 if (reason == (char *) NULL)
00369 return;
00370 (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
00371 if (description != (char *) NULL)
00372 (void) fprintf(stderr," (%s)",description);
00373 (void) fprintf(stderr,".\n");
00374 (void) fflush(stderr);
00375 }
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399 WizardExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
00400 {
00401 assert(exception != (ExceptionInfo *) NULL);
00402 assert(exception->signature == WizardSignature);
00403 LockSemaphoreInfo(exception->semaphore);
00404 exception->severity=UndefinedException;
00405 if (exception->exceptions != (void *) NULL)
00406 exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
00407 exception->exceptions,DestroyExceptionElement);
00408 exception->signature=(~WizardSignature);
00409 UnlockSemaphoreInfo(exception->semaphore);
00410 DestroySemaphoreInfo(&exception->semaphore);
00411 if (exception->relinquish != WizardFalse)
00412 exception=(ExceptionInfo *) RelinquishWizardMemory(exception);
00413 return(exception);
00414 }
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438 WizardExport void GetExceptionInfo(ExceptionInfo *exception)
00439 {
00440 assert(exception != (ExceptionInfo *) NULL);
00441 (void) ResetWizardMemory(exception,0,sizeof(*exception));
00442 exception->severity=UndefinedException;
00443 exception->exceptions=(void *) NewLinkedList(0);
00444 exception->semaphore=AllocateSemaphoreInfo();
00445 exception->signature=WizardSignature;
00446 }
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471 WizardExport char *GetExceptionMessage(const int error)
00472 {
00473 char
00474 exception[MaxTextExtent];
00475
00476 *exception='\0';
00477 #if defined(WIZARDSTOOLKIT_HAVE_STRERROR_R)
00478 (void) strerror_r(error,exception,sizeof(exception));
00479 #else
00480 (void) CopyWizardString(exception,strerror(error),sizeof(exception));
00481 #endif
00482 return(ConstantString(exception));
00483 }
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512 static const char *ExceptionSeverityToTag(const ExceptionType severity)
00513 {
00514 switch (severity)
00515 {
00516 case OptionWarning: return("Option/Warning/");
00517 case RandomWarning: return("Random/Warning/");
00518 case HashWarning: return("Hash/Warning/");
00519 case MACWarning: return("MAC/Warning/");
00520 case EntropyWarning: return("Entropy/Warning/");
00521 case ConfigureWarning: return("Configure/Warning/");
00522 case CipherWarning: return("Cipher/Warning/");
00523 case KeymapWarning: return("Keymap/Warning/");
00524 case AuthenticateWarning: return("Authenticate/Warning/");
00525 case KeyringWarning: return("Keyring/Warning/");
00526 case ParseWarning: return("Parse/Warning/");
00527 case UserWarning: return("User/Warning/");
00528 case SplayTreeWarning: return("SplayTree/Warning/");
00529 case HashmapWarning: return("Hashmap/Warning/");
00530 case LogWarning: return("Log/Warning/");
00531 case StringWarning: return("String/Warning/");
00532 case FileWarning: return("File/Warning/");
00533 case BlobWarning: return("Blob/Warning/");
00534 case ResourceWarning: return("ResourceLimit/Warning/");
00535 case OptionError: return("Option/Error/");
00536 case RandomError: return("Random/Error/");
00537 case HashError: return("Hash/Error/");
00538 case MACError: return("MAC/Error/");
00539 case EntropyError: return("Entropy/Error/");
00540 case ConfigureError: return("Configure/Error/");
00541 case CipherError: return("Cipher/Error/");
00542 case KeymapError: return("Keymap/Error/");
00543 case AuthenticateError: return("Authenticate/Error/");
00544 case KeyringError: return("Keyring/Error/");
00545 case ParseError: return("Parse/Error/");
00546 case UserError: return("User/Error/");
00547 case SplayTreeError: return("SplayTree/Error/");
00548 case StringError: return("String/Error/");
00549 case FileError: return("File/Error/");
00550 case BlobError: return("Blob/Error/");
00551 case ResourceError: return("ResourceLimit/Error/");
00552 case OptionFatalError: return("Option/FatalError/");
00553 case RandomFatalError: return("Random/FatalError/");
00554 case HashFatalError: return("Hash/FatalError/");
00555 case MACFatalError: return("MAC/FatalError/");
00556 case EntropyFatalError: return("Entropy/FatalError/");
00557 case ConfigureFatalError: return("Configure/FatalError/");
00558 case CipherFatalError: return("Cipher/FatalError/");
00559 case KeymapFatalError: return("Keymap/FatalError/");
00560 case AuthenticateFatalError: return("Authenticate/FatalError/");
00561 case KeyringFatalError: return("Keyring/FatalError/");
00562 case ParseFatalError: return("Parse/FatalError/");
00563 case UserFatalError: return("User/FatalError/");
00564 case SplayTreeFatalError: return("SplayTree/FatalError/");
00565 case HashmapFatalError: return("Hashmap/FatalError/");
00566 case LogFatalError: return("Log/FatalError/");
00567 case StringFatalError: return("String/FatalError/");
00568 case FileFatalError: return("File/FatalError/");
00569 case BlobFatalError: return("Blob/FatalError/");
00570 case ResourceFatalError: return("ResourceLimit/FatalError/");
00571 default: break;
00572 }
00573 return("");
00574 }
00575
00576 WizardExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
00577 const char *tag)
00578 {
00579 #if defined(WIZARDSTOOLKIT_LOCALE)
00580 char
00581 message[MaxTextExtent];
00582
00583 const char
00584 *locale_message;
00585
00586 assert(tag != (const char *) NULL);
00587 (void) FormatWizardString(message,MaxTextExtent,"Exception/%s%s",
00588 ExceptionSeverityToTag(severity),tag);
00589 locale_message=GetLocaleMessage(message);
00590 if (locale_message == (const char *) NULL)
00591 return(tag);
00592 if (locale_message == message)
00593 return(tag);
00594 return(locale_message);
00595 #else
00596 return(tag);
00597 #endif
00598 }
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622 WizardExport ExceptionType GetExceptionSeverity(const ExceptionInfo *exception)
00623 {
00624 assert(exception != (ExceptionInfo *) NULL);
00625 assert(exception->signature == WizardSignature);
00626 return(exception->severity);
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 WizardExport void InheritException(ExceptionInfo *exception,
00655 const ExceptionInfo *relative)
00656 {
00657 register const ExceptionInfo
00658 *p;
00659
00660 assert(exception != (ExceptionInfo *) NULL);
00661 assert(exception->signature == WizardSignature);
00662 assert(relative != (ExceptionInfo *) NULL);
00663 assert(relative->signature == WizardSignature);
00664 if (relative->exceptions == (void *) NULL)
00665 return;
00666 LockSemaphoreInfo(exception->semaphore);
00667 ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
00668 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00669 relative->exceptions);
00670 while (p != (const ExceptionInfo *) NULL)
00671 {
00672 (void) ThrowException(exception,p->severity,p->reason,p->description);
00673 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00674 relative->exceptions);
00675 }
00676 UnlockSemaphoreInfo(exception->semaphore);
00677 }
00678
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 WizardExport void WizardError(const ExceptionType error,const char *reason,
00709 const char *description)
00710 {
00711 if (error_handler != (ErrorHandler) NULL)
00712 (*error_handler)(error,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
00739
00740
00741
00742
00743
00744 WizardExport void WizardFatalError(const ExceptionType error,const char *reason,
00745 const char *description)
00746 {
00747 if (fatal_error_handler != (ErrorHandler) NULL)
00748 (*fatal_error_handler)(error,reason,description);
00749 }
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778 WizardExport void WizardWarning(const ExceptionType warning,const char *reason,
00779 const char *description)
00780 {
00781 if (warning_handler != (WarningHandler) NULL)
00782 (*warning_handler)(warning,reason,description);
00783 }
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808 WizardExport ErrorHandler SetErrorHandler(ErrorHandler handler)
00809 {
00810 ErrorHandler
00811 previous_handler;
00812
00813 previous_handler=error_handler;
00814 error_handler=handler;
00815 return(previous_handler);
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 WizardExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
00842 {
00843 FatalErrorHandler
00844 previous_handler;
00845
00846 previous_handler=fatal_error_handler;
00847 fatal_error_handler=handler;
00848 return(previous_handler);
00849 }
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874 WizardExport WarningHandler SetWarningHandler(WarningHandler handler)
00875 {
00876 WarningHandler
00877 previous_handler;
00878
00879 previous_handler=warning_handler;
00880 warning_handler=handler;
00881 return(previous_handler);
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
00915 WizardExport WizardBooleanType ThrowException(ExceptionInfo *exception,
00916 const ExceptionType severity,const char *reason,const char *description)
00917 {
00918 register ExceptionInfo
00919 *p;
00920
00921 assert(exception != (ExceptionInfo *) NULL);
00922 assert(exception->signature == WizardSignature);
00923 if (exception->exceptions == (void *) NULL)
00924 return(WizardTrue);
00925 p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
00926 exception->exceptions);
00927 if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
00928 (LocaleCompare(exception->reason,reason) == 0) &&
00929 (LocaleCompare(exception->description,description) == 0))
00930 return(WizardTrue);
00931 p=(ExceptionInfo *) AcquireAlignedMemory(1,sizeof(*p));
00932 if (p == (ExceptionInfo *) NULL)
00933 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
00934 (void) ResetWizardMemory(p,0,sizeof(*p));
00935 p->severity=severity;
00936 if (reason != (const char *) NULL)
00937 p->reason=ConstantString(reason);
00938 if (description != (const char *) NULL)
00939 p->description=ConstantString(description);
00940 p->signature=WizardSignature;
00941 (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
00942 exception->severity=p->severity;
00943 exception->reason=p->reason;
00944 exception->description=p->description;
00945 return(WizardTrue);
00946 }
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984 WizardExport WizardBooleanType ThrowWizardExceptionList(
00985 ExceptionInfo *exception,const char *module,const char *function,
00986 const size_t line,const ExceptionType severity,const char *format,
00987 va_list operands)
00988 {
00989 char
00990 message[MaxTextExtent],
00991 reason[MaxTextExtent];
00992
00993 int
00994 n;
00995
00996 WizardBooleanType
00997 status;
00998
00999 assert(exception != (ExceptionInfo *) NULL);
01000 assert(exception->signature == WizardSignature);
01001 #if defined(WIZARDSTOOLKIT_HAVE_VSNPRINTF)
01002 n=vsnprintf(reason,MaxTextExtent,format,operands);
01003 #else
01004 n=vsprintf(reason,format,operands);
01005 #endif
01006 if (n < 0)
01007 reason[MaxTextExtent-1]='\0';
01008 status=LogWizardEvent(exception->severity >= ErrorException ?
01009 ExceptionEvent : WarningEvent,module,function,line,"%s",reason);
01010 (void) FormatWizardString(message,MaxTextExtent,"%s @ %s/%s/%.20g",reason,
01011 module,function,(double) line);
01012 (void) ThrowException(exception,severity,message,(char *) NULL);
01013 return(status);
01014 }
01015
01016 WizardExport WizardBooleanType ThrowWizardException(ExceptionInfo *exception,
01017 const char *module,const char *function,const size_t line,
01018 const ExceptionType severity,const char *format,...)
01019 {
01020 WizardBooleanType
01021 status;
01022
01023 va_list
01024 operands;
01025
01026 va_start(operands,format);
01027 status=ThrowWizardExceptionList(exception,module,function,line,severity,
01028 format,operands);
01029 va_end(operands);
01030 return(status);
01031 }