1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 1998-2002 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*
7*0Sstevel@tonic-gate  * Copyright (c) 1997,1999 by Internet Software Consortium.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
10*0Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
11*0Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14*0Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16*0Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17*0Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18*0Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19*0Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20*0Sstevel@tonic-gate  * SOFTWARE.
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
26*0Sstevel@tonic-gate static const char rcsid[] = "$Id: assertions.c,v 8.4 2001/05/29 05:49:22 marka Exp $";
27*0Sstevel@tonic-gate #endif
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include "port_before.h"
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <errno.h>
32*0Sstevel@tonic-gate #include <stdio.h>
33*0Sstevel@tonic-gate #include <stdlib.h>
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <isc/assertions.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include "port_after.h"
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate /*
41*0Sstevel@tonic-gate  * Forward.
42*0Sstevel@tonic-gate  */
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate static void default_assertion_failed(const char *, int, assertion_type,
45*0Sstevel@tonic-gate 				     const char *, int);
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /*
48*0Sstevel@tonic-gate  * Public.
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate assertion_failure_callback __assertion_failed = default_assertion_failed;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate void
54*0Sstevel@tonic-gate set_assertion_failure_callback(assertion_failure_callback f) {
55*0Sstevel@tonic-gate 	if (f == NULL)
56*0Sstevel@tonic-gate 		__assertion_failed = default_assertion_failed;
57*0Sstevel@tonic-gate 	else
58*0Sstevel@tonic-gate 		__assertion_failed = f;
59*0Sstevel@tonic-gate }
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate const char *
62*0Sstevel@tonic-gate assertion_type_to_text(assertion_type type) {
63*0Sstevel@tonic-gate 	const char *result;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	switch (type) {
66*0Sstevel@tonic-gate 	case assert_require:
67*0Sstevel@tonic-gate 		result = "REQUIRE";
68*0Sstevel@tonic-gate 		break;
69*0Sstevel@tonic-gate 	case assert_ensure:
70*0Sstevel@tonic-gate 		result = "ENSURE";
71*0Sstevel@tonic-gate 		break;
72*0Sstevel@tonic-gate 	case assert_insist:
73*0Sstevel@tonic-gate 		result = "INSIST";
74*0Sstevel@tonic-gate 		break;
75*0Sstevel@tonic-gate 	case assert_invariant:
76*0Sstevel@tonic-gate 		result = "INVARIANT";
77*0Sstevel@tonic-gate 		break;
78*0Sstevel@tonic-gate 	default:
79*0Sstevel@tonic-gate 		result = NULL;
80*0Sstevel@tonic-gate 	}
81*0Sstevel@tonic-gate 	return (result);
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate /*
85*0Sstevel@tonic-gate  * Private.
86*0Sstevel@tonic-gate  */
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate static void
89*0Sstevel@tonic-gate default_assertion_failed(const char *file, int line, assertion_type type,
90*0Sstevel@tonic-gate 			 const char *cond, int print_errno)
91*0Sstevel@tonic-gate {
92*0Sstevel@tonic-gate 	fprintf(stderr, "%s:%d: %s(%s)%s%s failed.\n",
93*0Sstevel@tonic-gate 		file, line, assertion_type_to_text(type), cond,
94*0Sstevel@tonic-gate 		(print_errno) ? ": " : "",
95*0Sstevel@tonic-gate 		(print_errno) ? strerror(errno) : "");
96*0Sstevel@tonic-gate 	abort();
97*0Sstevel@tonic-gate 	/* NOTREACHED */
98*0Sstevel@tonic-gate }
99