MagickCore 7.0.10
exception.c
Go to the documentation of this file.
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% EEEEE X X CCCC EEEEE PPPP TTTTT IIIII OOO N N %
7% E X X C E P P T I O O NN N %
8% EEE X C EEE PPPP T I O O N N N %
9% E X X C E P T I O O N NN %
10% EEEEE X X CCCC EEEEE P T IIIII OOO N N %
11% %
12% %
13% Wizards Toolkit Exception Methods %
14% %
15% Software Design %
16% Cristy %
17% July 1993 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
43#include "wizard/studio.h"
44#include "wizard/client.h"
45#include "wizard/exception.h"
47#include "wizard/hashmap.h"
48#include "wizard/log.h"
49#include "wizard/memory_.h"
50#include "wizard/string_.h"
51#include "wizard/utility.h"
52#include "wizard/wizard.h"
53
54/*
55 Typedef declarations.
56*/
58{
61
62 char
65
66 void
68
71
74
75 size_t
77};
78
79/*
80 Forward declarations.
81*/
82#if defined(__cplusplus) || defined(c_plusplus)
83extern "C" {
84#endif
85
86static void
87 DefaultErrorHandler(const ExceptionType,const char *,const char *),
88 DefaultFatalErrorHandler(const ExceptionType,const char *,const char *),
89 DefaultWarningHandler(const ExceptionType,const char *,const char *);
90
91#if defined(__cplusplus) || defined(c_plusplus)
92}
93#endif
94
95/*
96 Global declarations.
97*/
98static ErrorHandler
100
103
104static WarningHandler
106
107/*
108%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109% %
110% %
111% %
112% A c q u i r e E x c e p t i o n I n f o %
113% %
114% %
115% %
116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117%
118% AcquireExceptionInfo() allocates the ExceptionInfo structure.
119%
120% The format of the AcquireExceptionInfo method is:
121%
122% ExceptionInfo *AcquireExceptionInfo(void)
123%
124*/
126{
128 *exception;
129
130 exception=(ExceptionInfo *) AcquireWizardMemory(sizeof(*exception));
131 if (exception == (ExceptionInfo *) NULL)
132 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
133 (void) memset(exception,0,sizeof(*exception));
134 GetExceptionInfo(exception);
135 exception->relinquish=WizardTrue;
136 exception->signature=WizardSignature;
137 return(exception);
138}
139
140/*
141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142% %
143% %
144% %
145% C l e a r W i z a r d E x c e p t i o n %
146% %
147% %
148% %
149%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150%
151% ClearWizardException() clears any exception that may not have been caught
152% yet.
153%
154% The format of the ClearWizardException method is:
155%
156% ClearWizardException(ExceptionInfo *exception)
157%
158% A description of each parameter follows:
159%
160% o exception: The exception info.
161%
162*/
163
164static void *DestroyExceptionElement(void *exception)
165{
167 *p;
168
169 p=(ExceptionInfo *) exception;
170 if (p->reason != (char *) NULL)
171 p->reason=(char *) RelinquishWizardMemory(p->reason);
172 if (p->description != (char *) NULL)
175 return((void *) NULL);
176}
177
179{
181 *p;
182
183 assert(exception != (ExceptionInfo *) NULL);
184 assert(exception->signature == WizardSignature);
185 if (exception->exceptions == (void *) NULL)
186 return;
187 LockSemaphoreInfo(exception->semaphore);
189 exception->exceptions);
190 while (p != (ExceptionInfo *) NULL)
191 {
194 exception->exceptions);
195 }
196 exception->severity=UndefinedException;
197 exception->reason=(char *) NULL;
198 exception->description=(char *) NULL;
199 UnlockSemaphoreInfo(exception->semaphore);
200 errno=0;
201}
202
203/*
204%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205% %
206% %
207% %
208% C a t c h E x c e p t i o n %
209% %
210% %
211% %
212%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213%
214% CatchException() returns if no exceptions is found otherwise it reports
215% the exception as a warning, error, or fatal depending on the severity.
216%
217% The format of the CatchException method is:
218%
219% CatchException(ExceptionInfo *exception)
220%
221% A description of each parameter follows:
222%
223% o exception: The exception info.
224%
225*/
227{
228 const ExceptionInfo
229 *p;
230
231 assert(exception != (ExceptionInfo *) NULL);
232 assert(exception->signature == WizardSignature);
233 if (exception->exceptions == (void *) NULL)
234 return;
235 LockSemaphoreInfo(exception->semaphore);
238 exception->exceptions);
239 while (p != (const ExceptionInfo *) NULL)
240 {
241 if ((p->severity >= WarningException) && (p->severity < ErrorException))
245 if (exception->severity >= FatalErrorException)
248 exception->exceptions);
249 }
250 UnlockSemaphoreInfo(exception->semaphore);
251 ClearWizardException(exception);
252}
253
254/*
255%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256% %
257% %
258% %
259+ D e f a u l t E r r o r H a n d l e r %
260% %
261% %
262% %
263%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264%
265% DefaultErrorHandler() displays an error reason.
266%
267% The format of the DefaultErrorHandler method is:
268%
269% void WizardError(const ExceptionType severity,const char *reason,
270% const char *description)
271%
272% A description of each parameter follows:
273%
274% o severity: Specifies the numeric error category.
275%
276% o reason: Specifies the reason to display before terminating the
277% program.
278%
279% o description: Specifies any description to the reason.
280%
281*/
283 const char *reason,const char *description)
284{
285 if (reason == (char *) NULL)
286 return;
287 (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
288 if (description != (char *) NULL)
289 (void) fprintf(stderr," (%s)",description);
290 (void) fprintf(stderr,".\n");
291 (void) fflush(stderr);
292}
293
294/*
295%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296% %
297% %
298% %
299+ D e f a u l t F a t a l E r r o r H a n d l e r %
300% %
301% %
302% %
303%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304%
305% DefaultFatalErrorHandler() displays an error reason and then terminates the
306% program.
307%
308% The format of the DefaultFatalErrorHandler method is:
309%
310% void WizardFatalError(const ExceptionType severity,const char *reason,
311% const char *description)
312%
313% A description of each parameter follows:
314%
315% o severity: Specifies the numeric error category.
316%
317% o reason: Specifies the reason to display before terminating the
318% program.
319%
320% o description: Specifies any description to the reason.
321%
322*/
323static void DefaultFatalErrorHandler(const ExceptionType severity,
324 const char *reason,const char *description)
325{
326 if (reason == (char *) NULL)
327 return;
328 (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
329 if (description != (char *) NULL)
330 (void) fprintf(stderr," (%s)",description);
331 (void) fprintf(stderr,".\n");
332 (void) fflush(stderr);
334 exit(severity);
335}
336
337/*
338%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339% %
340% %
341% %
342+ D e f a u l t W a r n i n g H a n d l e r %
343% %
344% %
345% %
346%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347%
348% DefaultWarningHandler() displays a warning reason.
349%
350% The format of the DefaultWarningHandler method is:
351%
352% void DefaultWarningHandler(const ExceptionType warning,
353% const char *reason,const char *description)
354%
355% A description of each parameter follows:
356%
357% o warning: Specifies the numeric warning category.
358%
359% o reason: Specifies the reason to display before terminating the
360% program.
361%
362% o description: Specifies any description to the reason.
363%
364*/
366 const char *reason,const char *description)
367{
368 if (reason == (char *) NULL)
369 return;
370 (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
371 if (description != (char *) NULL)
372 (void) fprintf(stderr," (%s)",description);
373 (void) fprintf(stderr,".\n");
374 (void) fflush(stderr);
375}
376
377/*
378%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379% %
380% %
381% %
382% D e s t r o y E x c e p t i o n I n f o %
383% %
384% %
385% %
386%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
387%
388% DestroyExceptionInfo() deallocates memory associated with an exception.
389%
390% The format of the DestroyExceptionInfo method is:
391%
392% ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
393%
394% A description of each parameter follows:
395%
396% o exception: The exception info.
397%
398*/
400{
401 assert(exception != (ExceptionInfo *) NULL);
402 assert(exception->signature == WizardSignature);
403 LockSemaphoreInfo(exception->semaphore);
404 exception->severity=UndefinedException;
405 if (exception->exceptions != (void *) NULL)
406 exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
408 exception->signature=(~WizardSignature);
409 UnlockSemaphoreInfo(exception->semaphore);
411 if (exception->relinquish != WizardFalse)
412 exception=(ExceptionInfo *) RelinquishWizardMemory(exception);
413 return(exception);
414}
415
416/*
417%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418% %
419% %
420% %
421% G e t E x c e p t i o n I n f o %
422% %
423% %
424% %
425%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426%
427% GetExceptionInfo() initializes an exception to default values.
428%
429% The format of the GetExceptionInfo method is:
430%
431% GetExceptionInfo(ExceptionInfo *exception)
432%
433% A description of each parameter follows:
434%
435% o exception: The exception info.
436%
437*/
439{
440 assert(exception != (ExceptionInfo *) NULL);
441 (void) memset(exception,0,sizeof(*exception));
442 exception->severity=UndefinedException;
443 exception->exceptions=(void *) NewLinkedList(0);
444 exception->semaphore=AcquireSemaphoreInfo();
445 exception->signature=WizardSignature;
446}
447
448/*
449%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
450% %
451% %
452% %
453% G e t E x c e p t i o n M e s s a g e %
454% %
455% %
456% %
457%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458%
459% GetExceptionMessage() returns the error message defined by the specified
460% error code.
461%
462% The format of the GetExceptionMessage method is:
463%
464% char *GetExceptionMessage(const int error)
465%
466% A description of each parameter follows:
467%
468% o error: the error code.
469%
470*/
471WizardExport char *GetExceptionMessage(const int error)
472{
473 char
474 exception[WizardPathExtent];
475
476 *exception='\0';
477#if defined(WIZARDSTOOLKIT_HAVE_STRERROR_R)
478#if !defined(WIZARDSTOOLKIT_STRERROR_R_CHAR_P)
479 (void) strerror_r(error,exception,sizeof(exception));
480#else
481 (void) CopyWizardString(exception,strerror_r(error,exception,
482 sizeof(exception)),sizeof(exception));
483#endif
484#else
485 (void) CopyWizardString(exception,strerror(error),sizeof(exception));
486#endif
487 return(ConstantString(exception));
488}
489
490/*
491%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492% %
493% %
494% %
495% G e t L o c a l e E x c e p t i o n M e s s a g e %
496% %
497% %
498% %
499%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500%
501% GetLocaleExceptionMessage() converts a enumerated exception severity and tag
502% to a message in the current locale.
503%
504% The format of the GetLocaleExceptionMessage method is:
505%
506% const char *GetLocaleExceptionMessage(const ExceptionType severity,
507% const char *tag)
508%
509% A description of each parameter follows:
510%
511% o severity: the severity of the exception.
512%
513% o tag: the message tag.
514%
515*/
516
517static const char *ExceptionSeverityToTag(const ExceptionType severity)
518{
519 switch (severity)
520 {
521 case OptionWarning: return("Option/Warning/");
522 case RandomWarning: return("Random/Warning/");
523 case HashWarning: return("Hash/Warning/");
524 case MACWarning: return("MAC/Warning/");
525 case EntropyWarning: return("Entropy/Warning/");
526 case ConfigureWarning: return("Configure/Warning/");
527 case CipherWarning: return("Cipher/Warning/");
528 case KeymapWarning: return("Keymap/Warning/");
529 case AuthenticateWarning: return("Authenticate/Warning/");
530 case KeyringWarning: return("Keyring/Warning/");
531 case ParseWarning: return("Parse/Warning/");
532 case UserWarning: return("User/Warning/");
533 case SplayTreeWarning: return("SplayTree/Warning/");
534 case HashmapWarning: return("Hashmap/Warning/");
535 case LogWarning: return("Log/Warning/");
536 case StringWarning: return("String/Warning/");
537 case FileWarning: return("File/Warning/");
538 case BlobWarning: return("Blob/Warning/");
539 case ResourceWarning: return("ResourceLimit/Warning/");
540 case OptionError: return("Option/Error/");
541 case RandomError: return("Random/Error/");
542 case HashError: return("Hash/Error/");
543 case MACError: return("MAC/Error/");
544 case EntropyError: return("Entropy/Error/");
545 case ConfigureError: return("Configure/Error/");
546 case CipherError: return("Cipher/Error/");
547 case KeymapError: return("Keymap/Error/");
548 case AuthenticateError: return("Authenticate/Error/");
549 case KeyringError: return("Keyring/Error/");
550 case ParseError: return("Parse/Error/");
551 case UserError: return("User/Error/");
552 case SplayTreeError: return("SplayTree/Error/");
553 case StringError: return("String/Error/");
554 case FileError: return("File/Error/");
555 case BlobError: return("Blob/Error/");
556 case ResourceError: return("ResourceLimit/Error/");
557 case OptionFatalError: return("Option/FatalError/");
558 case RandomFatalError: return("Random/FatalError/");
559 case HashFatalError: return("Hash/FatalError/");
560 case MACFatalError: return("MAC/FatalError/");
561 case EntropyFatalError: return("Entropy/FatalError/");
562 case ConfigureFatalError: return("Configure/FatalError/");
563 case CipherFatalError: return("Cipher/FatalError/");
564 case KeymapFatalError: return("Keymap/FatalError/");
565 case AuthenticateFatalError: return("Authenticate/FatalError/");
566 case KeyringFatalError: return("Keyring/FatalError/");
567 case ParseFatalError: return("Parse/FatalError/");
568 case UserFatalError: return("User/FatalError/");
569 case SplayTreeFatalError: return("SplayTree/FatalError/");
570 case HashmapFatalError: return("Hashmap/FatalError/");
571 case LogFatalError: return("Log/FatalError/");
572 case StringFatalError: return("String/FatalError/");
573 case FileFatalError: return("File/FatalError/");
574 case BlobFatalError: return("Blob/FatalError/");
575 case ResourceFatalError: return("ResourceLimit/FatalError/");
576 default: break;
577 }
578 return("");
579}
580
582 const char *tag)
583{
584#if defined(WIZARDSTOOLKIT_LOCALE)
585 char
586 message[WizardPathExtent];
587
588 const char
589 *locale_message;
590
591 assert(tag != (const char *) NULL);
592 (void) FormatLocaleString(message,WizardPathExtent,"Exception/%s%s",
593 ExceptionSeverityToTag(severity),tag);
594 locale_message=GetLocaleMessage(message);
595 if (locale_message == (const char *) NULL)
596 return(tag);
597 if (locale_message == message)
598 return(tag);
599 return(locale_message);
600#else
601 return(tag);
602#endif
603}
604
605/*
606%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
607% %
608% %
609% %
610% G e t E x c e p t i o n S e v e r i t y %
611% %
612% %
613% %
614%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
615%
616% GetExceptionSeverity() returns the exception severity.
617%
618% The format of the GetExceptionSeverity method is:
619%
620% ExceptionType GetExceptionSeverity(const ExceptionInfo *exception)
621%
622% A description of each parameter follows:
623%
624% o exception: The exception.
625%
626*/
628{
629 assert(exception != (ExceptionInfo *) NULL);
630 assert(exception->signature == WizardSignature);
631 return(exception->severity);
632}
633
634/*
635%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636% %
637% %
638% %
639% I n h e r i t E x c e p t i o n %
640% %
641% %
642% %
643%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644%
645% InheritException() inherits an exception from a related exception.
646%
647% The format of the InheritException method is:
648%
649% InheritException(ExceptionInfo *exception,const ExceptionInfo *relative)
650%
651% A description of each parameter follows:
652%
653% o exception: The exception info.
654%
655% o relative: The related exception info.
656%
657%
658*/
660 const ExceptionInfo *relative)
661{
662 const ExceptionInfo
663 *p;
664
665 assert(exception != (ExceptionInfo *) NULL);
666 assert(exception->signature == WizardSignature);
667 assert(relative != (ExceptionInfo *) NULL);
668 assert(relative->signature == WizardSignature);
669 if (relative->exceptions == (void *) NULL)
670 return;
671 LockSemaphoreInfo(relative->semaphore);
674 relative->exceptions);
675 while (p != (const ExceptionInfo *) NULL)
676 {
677 (void) ThrowException(exception,p->severity,p->reason,p->description);
679 relative->exceptions);
680 }
682}
683
684/*
685%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
686% %
687% %
688% %
689% W i z a r d E r r o r %
690% %
691% %
692% %
693%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
694%
695% WizardError() calls the exception handler methods with an error reason.
696%
697% The format of the WizardError method is:
698%
699% void WizardError(const ExceptionType error,const char *reason,
700% const char *description)
701%
702% A description of each parameter follows:
703%
704% o exception: Specifies the numeric error category.
705%
706% o reason: Specifies the reason to display before terminating the
707% program.
708%
709% o description: Specifies any description to the reason.
710%
711%
712*/
713WizardExport void WizardError(const ExceptionType error,const char *reason,
714 const char *description)
715{
716 if (error_handler != (ErrorHandler) NULL)
717 (*error_handler)(error,reason,description);
718}
719
720/*
721%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722% %
723% %
724% %
725% W i z a r d F a t al E r r o r %
726% %
727% %
728% %
729%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730%
731% WizardFatalError() calls the fatal exception handler methods with an error
732% reason.
733%
734% The format of the WizardError method is:
735%
736% void WizardFatalError(const ExceptionType error,const char *reason,
737% const char *description)
738%
739% A description of each parameter follows:
740%
741% o exception: Specifies the numeric error category.
742%
743% o reason: Specifies the reason to display before terminating the
744% program.
745%
746% o description: Specifies any description to the reason.
747%
748*/
749WizardExport void WizardFatalError(const ExceptionType error,const char *reason,
750 const char *description)
751{
752 if (fatal_error_handler != (ErrorHandler) NULL)
753 (*fatal_error_handler)(error,reason,description);
754}
755
756/*
757%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
758% %
759% %
760% %
761% W i z a r d W a r n i n g %
762% %
763% %
764% %
765%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766%
767% WizardWarning() calls the warning handler methods with a warning reason.
768%
769% The format of the WizardWarning method is:
770%
771% void WizardWarning(const ExceptionType warning,const char *reason,
772% const char *description)
773%
774% A description of each parameter follows:
775%
776% o warning: The warning severity.
777%
778% o reason: Define the reason for the warning.
779%
780% o description: Describe the warning.
781%
782*/
783WizardExport void WizardWarning(const ExceptionType warning,const char *reason,
784 const char *description)
785{
786 if (warning_handler != (WarningHandler) NULL)
787 (*warning_handler)(warning,reason,description);
788}
789
790/*
791%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
792% %
793% %
794% %
795% S e t E r r o r H a n d l e r %
796% %
797% %
798% %
799%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
800%
801% SetErrorHandler() sets the exception handler to the specified method
802% and returns the previous exception handler.
803%
804% The format of the SetErrorHandler method is:
805%
806% ErrorHandler SetErrorHandler(ErrorHandler handler)
807%
808% A description of each parameter follows:
809%
810% o handler: The method to handle errors.
811%
812*/
814{
816 previous_handler;
817
818 previous_handler=error_handler;
819 error_handler=handler;
820 return(previous_handler);
821}
822
823/*
824%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
825% %
826% %
827% %
828% S e t F a t a l E r r o r H a n d l e r %
829% %
830% %
831% %
832%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
833%
834% SetFatalErrorHandler() sets the fatal exception handler to the specified
835% method and returns the previous fatal exception handler.
836%
837% The format of the SetErrorHandler method is:
838%
839% ErrorHandler SetErrorHandler(ErrorHandler handler)
840%
841% A description of each parameter follows:
842%
843% o handler: The method to handle errors.
844%
845*/
847{
849 previous_handler;
850
851 previous_handler=fatal_error_handler;
852 fatal_error_handler=handler;
853 return(previous_handler);
854}
855
856/*
857%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858% %
859% %
860% %
861% S e t W a r n i n g H a n d l e r %
862% %
863% %
864% %
865%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
866%
867% SetWarningHandler() sets the warning handler to the specified method
868% and returns the previous warning handler.
869%
870% The format of the SetWarningHandler method is:
871%
872% ErrorHandler SetWarningHandler(ErrorHandler handler)
873%
874% A description of each parameter follows:
875%
876% o handler: The method to handle warnings.
877%
878*/
880{
882 previous_handler;
883
884 previous_handler=warning_handler;
885 warning_handler=handler;
886 return(previous_handler);
887}
888
889/*
890%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
891% %
892% %
893% %
894% T h r o w E x c e p t i o n %
895% %
896% %
897% %
898%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
899%
900% ThrowException() throws an exception with the specified severity code,
901% reason, and optional description.
902%
903% The format of the ThrowException method is:
904%
905% WizardBooleanType ThrowException(ExceptionInfo *exception,
906% const ExceptionType severity,const char *reason,
907% const char *description)
908%
909% A description of each parameter follows:
910%
911% o exception: The exception info.
912%
913% o severity: The severity of the exception.
914%
915% o reason: The reason for the exception.
916%
917% o description: The exception description.
918%
919*/
921 const ExceptionType severity,const char *reason,const char *description)
922{
924 *p;
925
926 assert(exception != (ExceptionInfo *) NULL);
927 assert(exception->signature == WizardSignature);
928 if (exception->exceptions == (void *) NULL)
929 return(WizardTrue);
930 LockSemaphoreInfo(exception->semaphore);
932 exception->exceptions);
933 if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
934 (LocaleCompare(exception->reason,reason) == 0) &&
935 (LocaleCompare(exception->description,description) == 0))
936 {
937 UnlockSemaphoreInfo(exception->semaphore);
938 return(WizardTrue);
939 }
940 p=(ExceptionInfo *) AcquireWizardMemory(sizeof(*p));
941 if (p == (ExceptionInfo *) NULL)
942 {
943 UnlockSemaphoreInfo(exception->semaphore);
944 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
945 }
946 (void) memset(p,0,sizeof(*p));
947 p->severity=severity;
948 if (reason != (const char *) NULL)
949 p->reason=ConstantString(reason);
950 if (description != (const char *) NULL)
951 p->description=ConstantString(description);
953 (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
954 exception->severity=p->severity;
955 exception->reason=p->reason;
956 exception->description=p->description;
957 UnlockSemaphoreInfo(exception->semaphore);
958 return(WizardTrue);
959}
960
961/*
962%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
963% %
964% %
965% %
966% T h r o w W i z a r d E x c e p t i o n %
967% %
968% %
969% %
970%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
971%
972% ThrowWizardException logs an exception as determined by the log configuration
973% file. If an error occurs, WizardFalse is returned otherwise WizardTrue.
974%
975% The format of the ThrowWizardException method is:
976%
977% WizardBooleanType ThrowWizardException(ExceptionInfo *exception,
978% const char *module,const char *function,const size_t line,
979% const ExceptionType severity,const char *format,...)
980%
981% A description of each parameter follows:
982%
983% o exception: The exception info.
984%
985% o filename: The source module filename.
986%
987% o function: The function name.
988%
989% o line: The line number of the source module.
990%
991% o severity: Specifies the numeric error category.
992%
993% o format: The output format.
994%
995*/
996
998 ExceptionInfo *exception,const char *module,const char *function,
999 const size_t line,const ExceptionType severity,const char *format,
1000 va_list operands)
1001{
1002 char
1003 message[WizardPathExtent],
1004 reason[WizardPathExtent];
1005
1006 int
1007 n;
1008
1010 status;
1011
1012 assert(exception != (ExceptionInfo *) NULL);
1013 assert(exception->signature == WizardSignature);
1014#if defined(WIZARDSTOOLKIT_HAVE_VSNPRINTF)
1015 n=vsnprintf(reason,WizardPathExtent,format,operands);
1016#else
1017 n=vsprintf(reason,format,operands);
1018#endif
1019 if (n < 0)
1020 reason[WizardPathExtent-1]='\0';
1021 status=LogWizardEvent(exception->severity >= ErrorException ?
1022 ExceptionEvent : WarningEvent,module,function,line,"%s",reason);
1023 (void) FormatLocaleString(message,WizardPathExtent,"%s @ %s/%s/%.20g",reason,
1024 module,function,(double) line);
1025 (void) ThrowException(exception,severity,message,(char *) NULL);
1026 return(status);
1027}
1028
1030 const char *module,const char *function,const size_t line,
1031 const ExceptionType severity,const char *format,...)
1032{
1034 status;
1035
1036 va_list
1037 operands;
1038
1039 va_start(operands,format);
1040 status=ThrowWizardExceptionList(exception,module,function,line,severity,
1041 format,operands);
1042 va_end(operands);
1043 return(status);
1044}
WizardExport const char * GetClientName(void)
Definition client.c:64
#define ThrowFatalException(severity, tag)
WizardExport void WizardWarning(const ExceptionType warning, const char *reason, const char *description)
Definition exception.c:783
static FatalErrorHandler fatal_error_handler
Definition exception.c:102
static WarningHandler warning_handler
Definition exception.c:105
static const char * ExceptionSeverityToTag(const ExceptionType severity)
Definition exception.c:517
static void DefaultErrorHandler(const ExceptionType, const char *, const char *)
WizardExport ExceptionInfo * AcquireExceptionInfo(void)
Definition exception.c:125
WizardExport ExceptionInfo * DestroyExceptionInfo(ExceptionInfo *exception)
Definition exception.c:399
WizardExport ExceptionType GetExceptionSeverity(const ExceptionInfo *exception)
Definition exception.c:627
WizardExport WizardBooleanType ThrowWizardException(ExceptionInfo *exception, const char *module, const char *function, const size_t line, const ExceptionType severity, const char *format,...)
Definition exception.c:1029
WizardExport void CatchException(ExceptionInfo *exception)
Definition exception.c:226
WizardExport void GetExceptionInfo(ExceptionInfo *exception)
Definition exception.c:438
static void DefaultWarningHandler(const ExceptionType, const char *, const char *)
WizardExport WizardBooleanType ThrowWizardExceptionList(ExceptionInfo *exception, const char *module, const char *function, const size_t line, const ExceptionType severity, const char *format, va_list operands)
Definition exception.c:997
WizardExport void ClearWizardException(ExceptionInfo *exception)
Definition exception.c:178
WizardExport ErrorHandler SetErrorHandler(ErrorHandler handler)
Definition exception.c:813
static void * DestroyExceptionElement(void *exception)
Definition exception.c:164
WizardExport void WizardError(const ExceptionType error, const char *reason, const char *description)
Definition exception.c:713
WizardExport WarningHandler SetWarningHandler(WarningHandler handler)
Definition exception.c:879
WizardExport void InheritException(ExceptionInfo *exception, const ExceptionInfo *relative)
Definition exception.c:659
WizardExport WizardBooleanType ThrowException(ExceptionInfo *exception, const ExceptionType severity, const char *reason, const char *description)
Definition exception.c:920
WizardExport const char * GetLocaleExceptionMessage(const ExceptionType severity, const char *tag)
Definition exception.c:581
static void DefaultFatalErrorHandler(const ExceptionType, const char *, const char *)
Definition exception.c:323
static ErrorHandler error_handler
Definition exception.c:99
WizardExport char * GetExceptionMessage(const int error)
Definition exception.c:471
WizardExport void WizardFatalError(const ExceptionType error, const char *reason, const char *description)
Definition exception.c:749
WizardExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
Definition exception.c:846
ExceptionType
Definition exception.h:60
@ MACFatalError
Definition exception.h:106
@ FatalErrorException
Definition exception.h:102
@ UserWarning
Definition exception.h:74
@ ParseWarning
Definition exception.h:73
@ FileError
Definition exception.h:99
@ AuthenticateError
Definition exception.h:91
@ UserFatalError
Definition exception.h:114
@ UndefinedException
Definition exception.h:61
@ LogFatalError
Definition exception.h:117
@ CipherFatalError
Definition exception.h:109
@ ResourceWarning
Definition exception.h:81
@ OptionWarning
Definition exception.h:63
@ ResourceFatalError
Definition exception.h:121
@ BlobWarning
Definition exception.h:80
@ ConfigureError
Definition exception.h:88
@ HashFatalError
Definition exception.h:105
@ KeymapError
Definition exception.h:90
@ RandomWarning
Definition exception.h:64
@ WarningException
Definition exception.h:62
@ MACWarning
Definition exception.h:66
@ KeyringFatalError
Definition exception.h:112
@ EntropyError
Definition exception.h:87
@ BlobError
Definition exception.h:100
@ EntropyFatalError
Definition exception.h:107
@ ConfigureWarning
Definition exception.h:68
@ SplayTreeWarning
Definition exception.h:75
@ CipherWarning
Definition exception.h:69
@ UserError
Definition exception.h:94
@ SplayTreeError
Definition exception.h:95
@ EntropyWarning
Definition exception.h:67
@ BlobFatalError
Definition exception.h:120
@ SplayTreeFatalError
Definition exception.h:115
@ KeyringError
Definition exception.h:92
@ HashWarning
Definition exception.h:65
@ RandomFatalError
Definition exception.h:104
@ ResourceError
Definition exception.h:101
@ OptionFatalError
Definition exception.h:103
@ MACError
Definition exception.h:86
@ KeyringWarning
Definition exception.h:72
@ ErrorException
Definition exception.h:82
@ ParseError
Definition exception.h:93
@ KeymapFatalError
Definition exception.h:110
@ ParseFatalError
Definition exception.h:113
@ HashmapFatalError
Definition exception.h:116
@ AuthenticateWarning
Definition exception.h:71
@ ConfigureFatalError
Definition exception.h:108
@ HashError
Definition exception.h:85
@ StringFatalError
Definition exception.h:118
@ RandomError
Definition exception.h:84
@ AuthenticateFatalError
Definition exception.h:111
@ FileFatalError
Definition exception.h:119
@ KeymapWarning
Definition exception.h:70
@ LogWarning
Definition exception.h:77
@ OptionError
Definition exception.h:83
@ StringError
Definition exception.h:98
@ CipherError
Definition exception.h:89
@ StringWarning
Definition exception.h:78
@ FileWarning
Definition exception.h:79
@ HashmapWarning
Definition exception.h:76
WizardExport const char * GetLocaleMessage(const char *)
Definition locale.c:739
void(* WarningHandler)(const ExceptionType, const char *, const char *)
Definition exception.h:134
void(* FatalErrorHandler)(const ExceptionType, const char *, const char *)
Definition exception.h:131
void(* ErrorHandler)(const ExceptionType, const char *, const char *)
Definition exception.h:128
WizardExport void * GetLastValueInLinkedList(LinkedListInfo *list_info)
Definition hashmap.c:446
WizardExport void * RemoveLastElementFromLinkedList(LinkedListInfo *list_info)
Definition hashmap.c:1826
WizardExport void ResetLinkedListIterator(LinkedListInfo *list_info)
Definition hashmap.c:1919
WizardExport WizardBooleanType AppendValueToLinkedList(LinkedListInfo *list_info, const void *value)
Definition hashmap.c:149
WizardExport LinkedListInfo * NewLinkedList(const size_t capacity)
Definition hashmap.c:1362
WizardExport void * GetNextValueInLinkedList(LinkedListInfo *list_info)
Definition hashmap.c:605
WizardExport LinkedListInfo * DestroyLinkedList(LinkedListInfo *list_info, void *(*relinquish_value)(void *))
Definition hashmap.c:397
WizardExport ssize_t FormatLocaleString(char *string, const size_t length, const char *format,...)
Definition locale.c:465
WizardBooleanType LogWizardEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition log.c:1390
@ WarningEvent
Definition log.h:51
@ ExceptionEvent
Definition log.h:46
WizardExport void * AcquireWizardMemory(const size_t size)
Definition memory.c:586
WizardExport void * RelinquishWizardMemory(void *memory)
Definition memory.c:1039
#define WizardExport
#define WizardPathExtent
#define WizardSignature
#define wizard_unused(x)
WizardExport void RelinquishSemaphoreInfo(SemaphoreInfo **semaphore_info)
Definition semaphore.c:338
WizardExport SemaphoreInfo * AcquireSemaphoreInfo(void)
Definition semaphore.c:156
WizardExport void LockSemaphoreInfo(SemaphoreInfo *semaphore_info)
Definition semaphore.c:282
WizardExport void UnlockSemaphoreInfo(SemaphoreInfo *semaphore_info)
Definition semaphore.c:437
WizardExport int LocaleCompare(const char *p, const char *q)
Definition string.c:1510
WizardExport char * ConstantString(const char *source)
Definition string.c:655
WizardExport size_t CopyWizardString(char *destination, const char *source, const size_t length)
Definition string.c:762
char * reason
Definition exception.c:63
ExceptionType severity
Definition exception.c:60
SemaphoreInfo * semaphore
Definition exception.c:73
WizardBooleanType relinquish
Definition exception.c:70
char * description
Definition exception.c:64
void * exceptions
Definition exception.c:67
size_t signature
Definition exception.c:76
WizardBooleanType
Definition wizard-type.h:26
@ WizardTrue
Definition wizard-type.h:28
@ WizardFalse
Definition wizard-type.h:27
WizardExport void WizardsToolkitTerminus(void)
Definition wizard.c:334